summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-02-25 05:01:00 -0300
committerEuAndreh <eu@euandre.org>2024-02-25 05:01:00 -0300
commit458bf0fc628d9bcf6a59a0efb898045266dab6ba (patch)
treebb538753668dad5abc7eec4a65bf4f233b34d93a
parentNormalize how modules import and name each other (diff)
downloadpapod-458bf0fc628d9bcf6a59a0efb898045266dab6ba.tar.gz
papod-458bf0fc628d9bcf6a59a0efb898045266dab6ba.tar.xz
Explicit import from "node:process"; move log() to hero.mjs
-rw-r--r--src/accretion.mjs6
-rw-r--r--src/api.mjs2
-rw-r--r--src/db.mjs13
-rw-r--r--src/hero.mjs15
-rw-r--r--src/utils.mjs2
-rw-r--r--tests/js/accretion.mjs14
-rw-r--r--tests/js/db.mjs5
-rw-r--r--tests/js/hero.mjs24
-rw-r--r--tests/js/utils.mjs16
9 files changed, 56 insertions, 41 deletions
diff --git a/src/accretion.mjs b/src/accretion.mjs
index a9adb97..1c70162 100644
--- a/src/accretion.mjs
+++ b/src/accretion.mjs
@@ -9,9 +9,9 @@ import * as u from "./utils.mjs";
const DIRNAME = path.dirname(url.fileURLToPath(import.meta.url));
-const MIGRATIONS_DIR = DIRNAME + "/sql/migrations/";
+export const MIGRATIONS_DIR = DIRNAME + "/sql/migrations/";
-export const runMigrations = async db => {
+export const runMigrations = async (logFn, db) => {
assert(db);
await db.exec(`
@@ -30,7 +30,7 @@ export const runMigrations = async db => {
.sort((a, b) => a.localeCompare(b, "POSIX"));
for (const filename of sortedPending) {
- u.log({ log: "exec-migration", filename });
+ logFn({ 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 2ec7cbf..fb62adc 100644
--- a/src/api.mjs
+++ b/src/api.mjs
@@ -1,3 +1,5 @@
+import process from "node:process";
+
import * as ircd from "./ircd.mjs";
import * as web from "./web.mjs";
diff --git a/src/db.mjs b/src/db.mjs
index 6bb2634..b82cb18 100644
--- a/src/db.mjs
+++ b/src/db.mjs
@@ -1,7 +1,8 @@
-import assert from "node:assert/strict";
-import fs from "node:fs";
-import path from "node:path";
-import url from "node:url";
+import assert from "node:assert/strict";
+import fs from "node:fs";
+import path from "node:path";
+import process from "node:process";
+import url from "node:url";
import sqlite from "./sqlite.cjs";
@@ -49,7 +50,7 @@ export const open = (...args) =>
export let handle = null;
-export const init = async (dbName = process.env.PAPO_DB_PATH || ":memory:") => {
+export const init = async (logFn, dbName = process.env.PAPO_DB_PATH || ":memory:") => {
handle = await open(dbName);
- await accretion.runMigrations(handle);
+ await accretion.runMigrations(logFn, handle);
};
diff --git a/src/hero.mjs b/src/hero.mjs
index aa4a5a4..899e3fb 100644
--- a/src/hero.mjs
+++ b/src/hero.mjs
@@ -1,6 +1,7 @@
-import assert from "node:assert/strict";
-import crypto from "node:crypto";
-import http from "node:http";
+import assert from "node:assert/strict";
+import crypto from "node:crypto";
+import http from "node:http";
+import process from "node:process";
import * as u from "./utils.mjs";
@@ -121,7 +122,7 @@ export const handleRequest = async (table, method, url) => {
if (!handler) {
return {
status: 404,
- body: "Not Found",
+ body: "Not Found\n",
};
}
@@ -144,12 +145,14 @@ export const makeRequestListener = table => async (req, res) => {
res.end(response.body);
};
+export const log = o => console.error(JSON.stringify(o));
+
export const interceptorsFn = ({
uuidFn,
logger,
} = {
uuidFn: crypto.randomUUID,
- logger: u.log,
+ logger: log,
}) => ({
requestId: (req, next) => next({ ...req, id: uuidFn() }),
logged: async (req, next) => {
@@ -197,7 +200,7 @@ export const interceptorsFn = ({
});
return {
status: 500,
- body: "Internal Server Error",
+ body: "Internal Server Error\n",
};
}
},
diff --git a/src/utils.mjs b/src/utils.mjs
index 8ce9076..f2e09e4 100644
--- a/src/utils.mjs
+++ b/src/utils.mjs
@@ -73,8 +73,6 @@ export const first = (arr, fn) => {
return null;
};
-export const log = o => console.error(JSON.stringify(o));
-
export const promisify = fn => (...args) =>
new Promise((resolve, reject) =>
fn(...args, (err, data) => err ? reject(err) : resolve(data)));
diff --git a/tests/js/accretion.mjs b/tests/js/accretion.mjs
index a0fbb66..73c93c8 100644
--- a/tests/js/accretion.mjs
+++ b/tests/js/accretion.mjs
@@ -1,10 +1,12 @@
import assert from "node:assert/strict";
+import fs from "node:fs";
import sqlite from "../../src/sqlite.cjs";
import * as runner from "../runner.mjs";
import * as db from "../../src/db.mjs";
import {
+ MIGRATIONS_DIR,
runMigrations,
} from "../../src/accretion.mjs";
@@ -13,6 +15,8 @@ const test_runMigrations = t => {
t.start("runMigrations()");
t.test("running twice is a noop", async () => {
+ const contents = [];
+ const logFn = x => contents.push(x);
const handle = await db.open(":memory:");
const migrationsFn = () => handle.all("SELECT filename FROM migrations;");
@@ -21,13 +25,19 @@ const test_runMigrations = t => {
{ message: "SQLITE_ERROR: no such table: migrations" },
);
- await runMigrations(handle);
+ await runMigrations(logFn, handle);
const filled = await migrationsFn();
- await runMigrations(handle);
+ await runMigrations(logFn, handle);
const unchanged = await migrationsFn();
assert.deepEqual(filled, unchanged);
+
+ const migrationLogs = fs
+ .readdirSync(MIGRATIONS_DIR)
+ .sort((a, b) => a.localeCompare(b, "POSIX"))
+ .map(filename => ({ log: "exec-migration", filename }));
+ assert.deepEqual(contents, migrationLogs);
});
};
diff --git a/tests/js/db.mjs b/tests/js/db.mjs
index c6c5c85..c02c43e 100644
--- a/tests/js/db.mjs
+++ b/tests/js/db.mjs
@@ -92,9 +92,10 @@ const test_open = t => {
const test_init = t => {
t.start("init()");
t.test("we only know how to deal with 1 database", async () => {
- await init();
+ const logFn = () => {};
+ await init(logFn);
const ref1 = handle;
- await init();
+ await init(logFn);
const ref2 = handle;
assert.notDeepEqual(ref1, ref2);
diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs
index bc9d3d2..5507375 100644
--- a/tests/js/hero.mjs
+++ b/tests/js/hero.mjs
@@ -15,6 +15,7 @@ import {
extractQueryParams,
handleRequest,
makeRequestListener,
+ log,
interceptorsFn,
interceptors,
defaultInterceptors,
@@ -562,7 +563,7 @@ const test_handleRequest = t => {
await handleRequest({}, "GET", "/"),
{
status: 404,
- body: "Not Found",
+ body: "Not Found\n",
},
);
});
@@ -631,6 +632,20 @@ const test_makeRequestListener = t => {
});
};
+const test_log = t => {
+ t.start("log()");
+
+ t.test("we can log data", () => {
+ log({ a: 1, type: "log-test" });
+ });
+
+ t.test("we can't log unserializable things", () => {
+ const obj = { self: null };
+ obj.self = obj;
+ assert.throws(() => log(obj), TypeError);
+ });
+};
+
const test_interceptorsFn = t => {
const next = x => x;
@@ -800,7 +815,7 @@ const test_interceptorsFn = t => {
await interceptorsFn({ logger }).serverError({ id: 123 }, next),
{
status: 500,
- body: "Internal Server Error",
+ body: "Internal Server Error\n",
},
);
assert.deepEqual(
@@ -823,7 +838,7 @@ const test_interceptorsFn = t => {
),
{
status: 500,
- body: "Internal Server Error",
+ body: "Internal Server Error\n",
},
);
assert.deepEqual(
@@ -1158,7 +1173,7 @@ const test_buildServer = t => {
const response = await socketRequest(socketPath, "/anything");
await server.close();
- assert.deepEqual(response, { status: 404, body: "Not Found" });
+ assert.deepEqual(response, { status: 404, body: "Not Found\n" });
});
t.test("integrated application server", async () => {
@@ -1188,6 +1203,7 @@ await runner.runTests([
test_extractQueryParams,
test_handleRequest,
test_makeRequestListener,
+ test_log,
test_interceptorsFn,
test_chainInterceptors,
test_wrapHandler,
diff --git a/tests/js/utils.mjs b/tests/js/utils.mjs
index e2475b0..5a1507c 100644
--- a/tests/js/utils.mjs
+++ b/tests/js/utils.mjs
@@ -8,7 +8,6 @@ import {
assocIn,
getIn,
first,
- log,
promisify,
} from "../../src/utils.mjs";
@@ -245,20 +244,6 @@ const test_first = t => {
});
};
-const test_log = t => {
- t.start("log()");
-
- t.test("we can log data", () => {
- log({ a: 1, type: "log-test" });
- });
-
- t.test("we can't log unserializable things", () => {
- const obj = { self: null };
- obj.self = obj;
- assert.throws(() => log(obj), TypeError);
- });
-};
-
const test_promisify = t => {
t.start("promisify()");
@@ -298,6 +283,5 @@ await runner.runTests([
test_assocIn,
test_getIn,
test_first,
- test_log,
test_promisify,
]);