import { lookup } from 'node:dns/promises'
import { readFile, unlink, writeFile } from 'node:fs/promises'
import * as http from 'node:http'
-import { hostname } from 'node:os'
+import { homedir, hostname } from 'node:os'
import { join } from 'node:path'
import type { NanoPowOptions, WorkGenerateRequest, WorkGenerateResponse, WorkValidateRequest, WorkValidateResponse } from '#types'
const MAX_REQUEST_SIZE = 1024
const MAX_BODY_SIZE = 158
-const DEBUG: boolean = !!(process.env.NANO_POW_DEBUG || false)
-const EFFORT: number = +(process.env.NANO_POW_EFFORT || 8)
-const PORT: number = +(process.env.NANO_POW_PORT || 5040)
+let DEBUG: boolean = !!(process.env.NANO_POW_DEBUG || false)
+let EFFORT: number = +(process.env.NANO_POW_EFFORT || 8)
+let PORT: number = +(process.env.NANO_POW_PORT || 5040)
let browser: Browser
let page: Page
if (DEBUG) console.log(new Date(Date.now()).toLocaleString(), 'NanoPow', args)
}
+async function loadConfig () {
+ const contents = await readFile(join(homedir(), '.nano-pow', 'config'), 'utf-8')
+ if (typeof contents === 'string') {
+ const config = contents.split('\n')
+ for (const line of config) {
+ const debugMatch = line.match(/^[ \t]*debug[ \t]*(true|false)[ \t]*(#.*)?$/i)
+ if (Array.isArray(debugMatch)) {
+ DEBUG = !!(process.env.NANO_POW_DEBUG) || debugMatch?.[1] === 'true' || false
+ }
+
+ const effortMatch = line.match(/^[ \t]*effort[ \t]*(\d{1,2})[ \t]*(#.*)?$/i)
+ if (Array.isArray(effortMatch)) {
+ EFFORT = +(process.env.NANO_POW_EFFORT || effortMatch?.[1] || 8)
+ }
+
+ const portMatch = line.match(/^[ \t]*port[ \t]*(\d{1,5})[ \t]*(#.*)?$/i)
+ if (Array.isArray(portMatch)) {
+ PORT = +(process.env.NANO_POW_PORT || portMatch?.[1] || 5040)
+ }
+ }
+ }
+}
+await loadConfig()
+
async function respond (res: http.ServerResponse, data: Buffer[]): Promise<void> {
let statusCode: number = 500
let headers: http.OutgoingHttpHeaders = { 'Content-Type': 'application/json' }