diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/accretion.mjs | 6 | ||||
-rw-r--r-- | src/api.mjs | 1 | ||||
-rwxr-xr-x | src/bin.mjs | 4 | ||||
-rw-r--r-- | src/db.mjs | 20 | ||||
-rw-r--r-- | src/hero.mjs | 23 | ||||
-rw-r--r-- | src/ircd.mjs | 1 | ||||
-rw-r--r-- | src/web.mjs | 38 |
7 files changed, 44 insertions, 49 deletions
diff --git a/src/accretion.mjs b/src/accretion.mjs index 117e291..a9adb97 100644 --- a/src/accretion.mjs +++ b/src/accretion.mjs @@ -5,7 +5,7 @@ import url from "node:url"; import sqlite from "./sqlite.cjs"; -import { difference, log } from "./utils.mjs"; +import * as u from "./utils.mjs"; const DIRNAME = path.dirname(url.fileURLToPath(import.meta.url)); @@ -25,12 +25,12 @@ export const runMigrations = async db => { const done = [...(await db.all("SELECT filename FROM migrations;"))] .map(row => row.filename); const allFiles = fs.readdirSync(MIGRATIONS_DIR, "UTF-8"); - const pending = difference(new Set(allFiles), new Set(done)); + const pending = u.difference(new Set(allFiles), new Set(done)); const sortedPending = [...pending] .sort((a, b) => a.localeCompare(b, "POSIX")); for (const filename of sortedPending) { - log({ log: "exec-migration", filename }); + u.log({ log: "exec-migration", filename }); const sql = fs.readFileSync(MIGRATIONS_DIR + filename, "UTF-8"); await db.exec("BEGIN TRANSACTION;"); await db.exec(sql); diff --git a/src/api.mjs b/src/api.mjs index 27a14b1..2ec7cbf 100644 --- a/src/api.mjs +++ b/src/api.mjs @@ -1,4 +1,3 @@ -import { eq } from "./utils.mjs"; import * as ircd from "./ircd.mjs"; import * as web from "./web.mjs"; diff --git a/src/bin.mjs b/src/bin.mjs index 5dbbb25..e79904f 100755 --- a/src/bin.mjs +++ b/src/bin.mjs @@ -1,4 +1,4 @@ #!/usr/bin/env node -import { main } from "./api.mjs"; +import * as api from "./api.mjs"; -main(); +api.main(); @@ -5,15 +5,15 @@ import url from "node:url"; import sqlite from "./sqlite.cjs"; -import { promisify } from "./utils.mjs"; -import { runMigrations } from "./accretion.mjs"; +import * as u from "./utils.mjs"; +import * as accretion from "./accretion.mjs"; export const promisifyDb = dbHandle => ({ ref: dbHandle, - all: promisify((...args) => dbHandle.all(...args)), - exec: promisify((...args) => dbHandle.exec(...args)), - run: promisify((...args) => dbHandle.run(...args)), + all: u.promisify((...args) => dbHandle.all(...args)), + exec: u.promisify((...args) => dbHandle.exec(...args)), + run: u.promisify((...args) => dbHandle.run(...args)), each: (...args) => new Promise((resolve, reject) => { const cb = args[args.length - 1]; @@ -27,8 +27,8 @@ export const promisifyDb = dbHandle => ({ new Promise((resolve, reject) => { const mkStmt = stmtRef => ({ ref: stmtRef, - run: promisify((...args) => stmtRef.run(...args)), - finalize: promisify((...args) => stmtRef.finalize(...args)), + run: u.promisify((...args) => stmtRef.run(...args)), + finalize: u.promisify((...args) => stmtRef.finalize(...args)), }); const ref = dbHandle.prepare( ...args, @@ -47,9 +47,9 @@ export const open = (...args) => )); }); -export let db = null; +export let handle = null; export const init = async (dbName = process.env.PAPO_DB_PATH || ":memory:") => { - db = await open(dbName); - await runMigrations(db); + handle = await open(dbName); + await accretion.runMigrations(handle); }; diff --git a/src/hero.mjs b/src/hero.mjs index 38fdde4..aa4a5a4 100644 --- a/src/hero.mjs +++ b/src/hero.mjs @@ -2,7 +2,8 @@ import assert from "node:assert/strict"; import crypto from "node:crypto"; import http from "node:http"; -import { assocIn, getIn, first, log, promisify } from "./utils.mjs"; +import * as u from "./utils.mjs"; + export const normalizeSegments = segments => segments.length === 1 && segments[0] === "" ? @@ -45,13 +46,13 @@ export const addRoute = (table, methods, path, handlerFn) => { const segments = pathToSegments(path); const kw = hasPathParams(segments) ? "dynamic" : "static"; return methods.reduce( - (acc, el) => assocIn(acc, [kw, el].concat(segments), handlerFn), + (acc, el) => u.assocIn(acc, [kw, el].concat(segments), handlerFn), table, ); }; export const findStaticHandler = (table, method, segments) => { - const handlerFn = getIn(table, ["static", method].concat(segments)); + const handlerFn = u.getIn(table, ["static", method].concat(segments)); return !handlerFn ? null : { handlerFn, params: {} }; }; @@ -87,7 +88,7 @@ export const firstParamMatch = (tree, segments, params) => { const paramOptions = Object.keys(tree) .filter(s => s.startsWith(":")) .sort(); - return first(paramOptions, param => firstParamMatch(tree[param], nextSegments, { + return u.first(paramOptions, param => firstParamMatch(tree[param], nextSegments, { ...params, [param.slice(1)]: seg })); @@ -148,7 +149,7 @@ export const interceptorsFn = ({ logger, } = { uuidFn: crypto.randomUUID, - logger: log, + logger: u.log, }) => ({ requestId: (req, next) => next({ ...req, id: uuidFn() }), logged: async (req, next) => { @@ -204,6 +205,13 @@ export const interceptorsFn = ({ export const interceptors = interceptorsFn(); +export const defaultInterceptors = [ + interceptors.serverError, + interceptors.contentType, + interceptors.requestId, + interceptors.logged, +]; + export const chainInterceptors = arr => req => arr.length === 0 ? req : @@ -231,8 +239,9 @@ export const buildRoutes = (routes, globalInterceptors = []) => export const promisifyServer = serverHandle => ({ ref: serverHandle, - listen: promisify((...args) => serverHandle.listen(...args)), - close: promisify((...args) => serverHandle.close(...args)), + listen: u.promisify((...args) => serverHandle.listen(...args)), + close: u.promisify((...args) => serverHandle.close(...args)), + events: serverHandle, }); export const buildServer = (routes, globalInterceptors = []) => { diff --git a/src/ircd.mjs b/src/ircd.mjs index 38903f8..af4d280 100644 --- a/src/ircd.mjs +++ b/src/ircd.mjs @@ -1,5 +1,4 @@ import net from "node:net"; -import {} from "./db.mjs"; const server = net.createServer(socket => { socket.write("olar\r\n"); diff --git a/src/web.mjs b/src/web.mjs index 8eed5b4..cd875a0 100644 --- a/src/web.mjs +++ b/src/web.mjs @@ -1,30 +1,18 @@ -import http from "node:http"; +import * as u from "./utils.mjs"; +import { buildServer, defaultInterceptors } from "./hero.mjs"; -const listProducts = () => {}; -const getProduct = () => {}; -const routes = { - GET: { - "/products": listProducts, - "/products/:id": getProduct, - }, -}; +const listProducts = req => ({ status: 200 }); +const getProduct = req => ({ status: 200 }); +const getChat = req => ({ status: 200, body: "something\n" }); -const server = http.createServer((req, res) => { - const { headers ,url, method } = req; - console.log({ - headers, - url, - method, - }); - res.writeHead(200, { - "Content-Type": "text/plain", - }); - res.end("Hello, web!\n"); -}); +const server = buildServer([ + [ "GET", "/products", listProducts ], + [ "GET", "/products/:id", getProduct ], + [ "GET", "/chat", getChat ], +], defaultInterceptors); -export const app = udsPath => { - server.listen(udsPath, () => { - console.log("I'm web."); - }); +export const app = async udsPath => { + await server.listen(udsPath); + u.log({ type: "starting-server", udsPath }); }; |