Compare commits
9 Commits
91780b6dca
...
hydrators-
| Author | SHA1 | Date | |
|---|---|---|---|
| 09d85c8f22 | |||
| a0ce5183b2 | |||
| c83202b681 | |||
| 2c1d297be1 | |||
| 2d697c1e61 | |||
| 410bb671f1 | |||
| 0ae197f939 | |||
| 370bea5d98 | |||
| 9d34768051 |
10
README.md
10
README.md
@@ -49,16 +49,6 @@ enough libc to run golang binaries.
|
||||
|
||||
To run a more complete system, you also need to have docker compose installed.
|
||||
|
||||
### Database
|
||||
|
||||
To connect to the database, you need psql (PostgreSQL client, for
|
||||
`./diachron/common.d/db`)
|
||||
|
||||
- macOS: `brew install libpq` (and follow the caveat to add it to your PATH),
|
||||
or `brew install postgresql`
|
||||
- Debian/Ubuntu: `apt install postgresql-client`
|
||||
- Fedora/RHEL: `dnf install postgresql`
|
||||
|
||||
### Development requirements
|
||||
|
||||
To hack on diachron itself, you need the following:
|
||||
|
||||
173
backend/app.ts
173
backend/app.ts
@@ -1,30 +1,171 @@
|
||||
import{cli}from'./diachron/cli'
|
||||
import { formatError } from './diachron/errors';
|
||||
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error(formatError(err));
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason) => {
|
||||
console.error(formatError(reason));
|
||||
});
|
||||
|
||||
import express, {
|
||||
type Request as ExpressRequest,
|
||||
type Response as ExpressResponse,
|
||||
} from "express";
|
||||
import { match } from "path-to-regexp";
|
||||
import { Session } from "./diachron/auth";
|
||||
import { cli } from "./diachron/cli";
|
||||
import { contentTypes } from "./diachron/content-types";
|
||||
import { runWithContext } from "./diachron/context";
|
||||
import { core } from "./diachron/core";
|
||||
|
||||
import { httpCodes } from "./diachron/http-codes";
|
||||
import { request } from "./diachron/request";
|
||||
// import { URLPattern } from 'node:url';
|
||||
import {
|
||||
AuthenticationRequired,
|
||||
AuthorizationDenied,
|
||||
type Call,
|
||||
type InternalHandler,
|
||||
isRedirect,
|
||||
type Method,
|
||||
massageMethod,
|
||||
methodParser,
|
||||
type ProcessedRoute,
|
||||
type Result,
|
||||
type Route,
|
||||
} from "./diachron/types";
|
||||
import { routes } from "./routes";
|
||||
import {makeApp}from'./diachron/app'
|
||||
|
||||
const app = express();
|
||||
|
||||
const app = makeApp({routes});
|
||||
|
||||
// Parse request bodies
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
core.logging.log({ source: "logging", text: ["1"] });
|
||||
const processedRoutes: { [K in Method]: ProcessedRoute[] } = {
|
||||
GET: [],
|
||||
POST: [],
|
||||
PUT: [],
|
||||
PATCH: [],
|
||||
DELETE: [],
|
||||
};
|
||||
|
||||
function _isPromise<T>(value: T | Promise<T>): value is Promise<T> {
|
||||
return typeof (value as any)?.then === "function";
|
||||
}
|
||||
|
||||
routes.forEach((route: Route, _idx: number, _allRoutes: Route[]) => {
|
||||
// const pattern /*: URLPattern */ = new URLPattern({ pathname: route.path });
|
||||
const matcher = match<Record<string, string>>(route.path);
|
||||
const methodList = route.methods;
|
||||
|
||||
const handler: InternalHandler = async (
|
||||
expressRequest: ExpressRequest,
|
||||
): Promise<Result> => {
|
||||
const method = massageMethod(expressRequest.method);
|
||||
|
||||
console.log("method", method);
|
||||
|
||||
if (!methodList.includes(method)) {
|
||||
// XXX: Worth asserting this?
|
||||
}
|
||||
|
||||
console.log("request.originalUrl", expressRequest.originalUrl);
|
||||
|
||||
// Authenticate the request
|
||||
const auth = await request.auth.validateRequest(expressRequest);
|
||||
|
||||
const req: Call = {
|
||||
pattern: route.path,
|
||||
path: expressRequest.originalUrl,
|
||||
method,
|
||||
parameters: { one: 1, two: 2 },
|
||||
request: expressRequest,
|
||||
user: auth.user,
|
||||
session: new Session(auth.session, auth.user),
|
||||
};
|
||||
|
||||
try {
|
||||
const retval = await runWithContext({ user: auth.user }, () =>
|
||||
route.handler(req),
|
||||
);
|
||||
return retval;
|
||||
} catch (error) {
|
||||
// Handle authentication errors
|
||||
if (error instanceof AuthenticationRequired) {
|
||||
return {
|
||||
code: httpCodes.clientErrors.Unauthorized,
|
||||
contentType: contentTypes.application.json,
|
||||
result: JSON.stringify({
|
||||
error: "Authentication required",
|
||||
}),
|
||||
};
|
||||
}
|
||||
if (error instanceof AuthorizationDenied) {
|
||||
return {
|
||||
code: httpCodes.clientErrors.Forbidden,
|
||||
contentType: contentTypes.application.json,
|
||||
result: JSON.stringify({ error: "Access denied" }),
|
||||
};
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
for (const [_idx, method] of methodList.entries()) {
|
||||
const pr: ProcessedRoute = { matcher, method, handler };
|
||||
|
||||
processedRoutes[method].push(pr);
|
||||
}
|
||||
});
|
||||
|
||||
async function handler(
|
||||
req: ExpressRequest,
|
||||
_res: ExpressResponse,
|
||||
): Promise<Result> {
|
||||
const method = await methodParser.parseAsync(req.method);
|
||||
|
||||
const byMethod = processedRoutes[method];
|
||||
console.log(
|
||||
"DEBUG: req.path =",
|
||||
JSON.stringify(req.path),
|
||||
"method =",
|
||||
method,
|
||||
);
|
||||
for (const [_idx, pr] of byMethod.entries()) {
|
||||
const match = pr.matcher(req.path);
|
||||
console.log("DEBUG: trying pattern, match result =", match);
|
||||
if (match) {
|
||||
console.log("match", match);
|
||||
const resp = await pr.handler(req);
|
||||
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
|
||||
const retval: Result = {
|
||||
code: httpCodes.clientErrors.NotFound,
|
||||
contentType: contentTypes.text.plain,
|
||||
result: "not found!",
|
||||
};
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
app.use(async (req: ExpressRequest, res: ExpressResponse) => {
|
||||
const result0 = await handler(req, res);
|
||||
|
||||
const code = result0.code.code;
|
||||
const result = result0.result;
|
||||
|
||||
console.log(result);
|
||||
|
||||
// Set any cookies from the result
|
||||
if (result0.cookies) {
|
||||
for (const cookie of result0.cookies) {
|
||||
res.cookie(cookie.name, cookie.value, cookie.options ?? {});
|
||||
}
|
||||
}
|
||||
|
||||
if (isRedirect(result0)) {
|
||||
res.redirect(code, result0.redirect);
|
||||
} else {
|
||||
res.status(code).send(result);
|
||||
}
|
||||
});
|
||||
|
||||
process.title = `diachron:${cli.listen.port}`;
|
||||
|
||||
app.listen(cli.listen.port, cli.listen.host, () => {
|
||||
console.log(`Listening on ${cli.listen.host}:${cli.listen.port}`);
|
||||
|
||||
@@ -6,4 +6,4 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
cd "$DIR"
|
||||
|
||||
../cmd pnpm ncc build ./app.ts -o dist --source-map
|
||||
../cmd pnpm ncc build ./app.ts -o dist
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
interface PackageJson {
|
||||
dependencies?: Record<string, string>;
|
||||
devDependencies?: Record<string, string>;
|
||||
}
|
||||
|
||||
function readPackageJson(path: string): PackageJson {
|
||||
const content = readFileSync(path, "utf-8");
|
||||
return JSON.parse(content);
|
||||
}
|
||||
|
||||
function getAllDependencyNames(pkg: PackageJson): Set<string> {
|
||||
const names = new Set<string>();
|
||||
for (const name of Object.keys(pkg.dependencies ?? {})) {
|
||||
names.add(name);
|
||||
}
|
||||
for (const name of Object.keys(pkg.devDependencies ?? {})) {
|
||||
names.add(name);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
const diachronPkgPath = join(__dirname, "diachron", "package.json");
|
||||
const backendPkgPath = join(__dirname, "package.json");
|
||||
|
||||
const diachronPkg = readPackageJson(diachronPkgPath);
|
||||
const backendPkg = readPackageJson(backendPkgPath);
|
||||
|
||||
const diachronDeps = getAllDependencyNames(diachronPkg);
|
||||
const backendDeps = getAllDependencyNames(backendPkg);
|
||||
|
||||
const duplicates: string[] = [];
|
||||
|
||||
for (const dep of diachronDeps) {
|
||||
if (backendDeps.has(dep)) {
|
||||
duplicates.push(dep);
|
||||
}
|
||||
}
|
||||
|
||||
if (duplicates.length > 0) {
|
||||
console.error("Error: Duplicate dependencies found.");
|
||||
console.error("");
|
||||
console.error(
|
||||
"The following dependencies exist in both backend/package.json and backend/diachron/package.json:",
|
||||
);
|
||||
console.error("");
|
||||
for (const dep of duplicates.sort()) {
|
||||
console.error(` - ${dep}`);
|
||||
}
|
||||
console.error("");
|
||||
console.error(
|
||||
"Dependencies in backend/diachron/package.json are provided by the framework",
|
||||
);
|
||||
console.error(
|
||||
"and must not be duplicated in backend/package.json. Remove them from",
|
||||
);
|
||||
console.error("backend/package.json to fix this error.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log("No duplicate dependencies found.");
|
||||
@@ -11,5 +11,4 @@ out_dir="$check_dir/out"
|
||||
source "$check_dir"/../diachron/shims/common
|
||||
source "$check_dir"/../diachron/shims/node.common
|
||||
|
||||
$ROOT/cmd tsx "$check_dir/check-deps.ts"
|
||||
$ROOT/cmd pnpm tsc --outDir "$out_dir"
|
||||
|
||||
@@ -1,190 +0,0 @@
|
||||
// FIXME: rename this to make-app.ts and adjust imports accordingly
|
||||
|
||||
import{contentTypes} from './content-types'
|
||||
import{httpCodes}from'./http-codes'
|
||||
import express, {
|
||||
type NextFunction,
|
||||
type Request as ExpressRequest,
|
||||
type Response as ExpressResponse,
|
||||
} from "express";
|
||||
import { formatError, formatErrorHtml } from "./errors";
|
||||
import {isRedirect, InternalHandler, AuthenticationRequired,
|
||||
AuthorizationDenied, Call,type Method, type ProcessedRoute,methodParser, type Result, type Route,massageMethod } from "./types";
|
||||
import { runWithContext } from "./context";
|
||||
import { Session } from "./auth";import { request } from "./request";
|
||||
import { match } from "path-to-regexp";
|
||||
|
||||
import { cli } from "./cli";
|
||||
|
||||
type ProcessedRoutes= {[K in Method]: ProcessedRoute[] }
|
||||
const processRoutes=(routes:Route[]) :ProcessedRoutes => {
|
||||
const retval:ProcessedRoutes= {
|
||||
GET: [],
|
||||
POST: [],
|
||||
PUT: [],
|
||||
PATCH: [],
|
||||
DELETE: [],
|
||||
};
|
||||
|
||||
routes.forEach((route: Route, _idx: number, _allRoutes: Route[]) => {
|
||||
// const pattern /*: URLPattern */ = new URLPattern({ pathname: route.path });
|
||||
const matcher = match<Record<string, string>>(route.path);
|
||||
const methodList = route.methods;
|
||||
|
||||
const handler: InternalHandler = async (
|
||||
expressRequest: ExpressRequest,
|
||||
): Promise<Result> => {
|
||||
const method = massageMethod(expressRequest.method);
|
||||
|
||||
console.log("method", method);
|
||||
|
||||
if (!methodList.includes(method)) {
|
||||
// XXX: Worth asserting this?
|
||||
}
|
||||
|
||||
console.log("request.originalUrl", expressRequest.originalUrl);
|
||||
|
||||
// Authenticate the request
|
||||
const auth = await request.auth.validateRequest(expressRequest);
|
||||
|
||||
const req: Call = {
|
||||
pattern: route.path,
|
||||
path: expressRequest.originalUrl,
|
||||
method,
|
||||
parameters: { one: 1, two: 2 },
|
||||
request: expressRequest,
|
||||
user: auth.user,
|
||||
session: new Session(auth.session, auth.user),
|
||||
};
|
||||
|
||||
try {
|
||||
const retval = await runWithContext({ user: auth.user }, () =>
|
||||
route.handler(req),
|
||||
);
|
||||
return retval;
|
||||
} catch (error) {
|
||||
// Handle authentication errors
|
||||
if (error instanceof AuthenticationRequired) {
|
||||
return {
|
||||
code: httpCodes.clientErrors.Unauthorized,
|
||||
contentType: contentTypes.application.json,
|
||||
result: JSON.stringify({
|
||||
error: "Authentication required",
|
||||
}),
|
||||
};
|
||||
}
|
||||
if (error instanceof AuthorizationDenied) {
|
||||
return {
|
||||
code: httpCodes.clientErrors.Forbidden,
|
||||
contentType: contentTypes.application.json,
|
||||
result: JSON.stringify({ error: "Access denied" }),
|
||||
};
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
for (const [_idx, method] of methodList.entries()) {
|
||||
const pr: ProcessedRoute = { matcher, method, handler };
|
||||
|
||||
retval[method].push(pr);
|
||||
}
|
||||
});
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
type MakeAppArgs={routes:Route[],
|
||||
processTitle?: string,
|
||||
}
|
||||
|
||||
const makeApp = ({routes, processTitle}: MakeAppArgs) => {
|
||||
if (process.title) {
|
||||
process.title = `diachron:${cli.listen.port}`;
|
||||
}
|
||||
|
||||
const processedRoutes = processRoutes(routes)
|
||||
|
||||
async function handler(
|
||||
req: ExpressRequest,
|
||||
_res: ExpressResponse,
|
||||
): Promise<Result> {
|
||||
const method = await methodParser.parseAsync(req.method);
|
||||
|
||||
const byMethod = processedRoutes[method];
|
||||
console.log(
|
||||
"DEBUG: req.path =",
|
||||
JSON.stringify(req.path),
|
||||
"method =",
|
||||
method,
|
||||
);
|
||||
for (const [_idx, pr] of byMethod.entries()) {
|
||||
const match = pr.matcher(req.path);
|
||||
console.log("DEBUG: trying pattern, match result =", match);
|
||||
if (match) {
|
||||
console.log("match", match);
|
||||
const resp = await pr.handler(req);
|
||||
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
|
||||
const retval: Result = {
|
||||
code: httpCodes.clientErrors.NotFound,
|
||||
contentType: contentTypes.text.plain,
|
||||
result: "not found!",
|
||||
};
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(express.json())
|
||||
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
app.use(async (req: ExpressRequest, res: ExpressResponse) => {
|
||||
const result0 = await handler(req, res);
|
||||
|
||||
const code = result0.code.code;
|
||||
const result = result0.result;
|
||||
|
||||
console.log(result);
|
||||
|
||||
// Set any cookies from the result
|
||||
if (result0.cookies) {
|
||||
for (const cookie of result0.cookies) {
|
||||
res.cookie(cookie.name, cookie.value, cookie.options ?? {});
|
||||
}
|
||||
}
|
||||
|
||||
if (isRedirect(result0)) {
|
||||
res.redirect(code, result0.redirect);
|
||||
} else {
|
||||
res.status(code).send(result);
|
||||
}
|
||||
});
|
||||
|
||||
app.use(
|
||||
(
|
||||
err: Error,
|
||||
_req: ExpressRequest,
|
||||
res: ExpressResponse,
|
||||
_next: NextFunction,
|
||||
) => {
|
||||
console.error(formatError(err));
|
||||
res.status(500).type("html").send(formatErrorHtml(err));
|
||||
},
|
||||
);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
export{makeApp};
|
||||
|
||||
function _isPromise<T>(value: T | Promise<T>): value is Promise<T> {
|
||||
return typeof (value as any)?.then === "function";
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import nunjucks from "nunjucks";
|
||||
import { db, migrate, migrationStatus } from "../database";
|
||||
import { formatError, formatErrorHtml } from "../errors";
|
||||
import { getLogs, log } from "../logging";
|
||||
|
||||
// FIXME: This doesn't belong here; move it somewhere else.
|
||||
@@ -41,7 +40,6 @@ const misc = {
|
||||
const core = {
|
||||
conf,
|
||||
database,
|
||||
errors: { formatError, formatErrorHtml },
|
||||
logging,
|
||||
misc,
|
||||
random,
|
||||
|
||||
@@ -1,229 +0,0 @@
|
||||
// ANSI escape codes
|
||||
const bold = "\x1b[1m";
|
||||
const red = "\x1b[31m";
|
||||
const cyan = "\x1b[36m";
|
||||
const dim = "\x1b[2m";
|
||||
const reset = "\x1b[0m";
|
||||
|
||||
interface ParsedFrame {
|
||||
raw: string;
|
||||
fn: string;
|
||||
file: string;
|
||||
line: string;
|
||||
col: string;
|
||||
isApp: boolean;
|
||||
}
|
||||
|
||||
const frameRe = /^\s*at\s+(?:(.+?)\s+)?\(?((?:\/|[a-zA-Z]:\\).+?):(\d+):(\d+)\)?$/;
|
||||
|
||||
function parseFrame(line: string): ParsedFrame | null {
|
||||
const m = line.match(frameRe);
|
||||
if (!m) return null;
|
||||
|
||||
const fn = m[1] ?? "<anonymous>";
|
||||
const file = m[2];
|
||||
const lineNum = m[3];
|
||||
const col = m[4];
|
||||
|
||||
const isApp =
|
||||
!file.includes("node_modules") && !file.startsWith("node:");
|
||||
|
||||
return { raw: line, fn, file, line: lineNum, col, isApp };
|
||||
}
|
||||
|
||||
function relativePath(absPath: string): string {
|
||||
const marker = "backend/";
|
||||
const idx = absPath.lastIndexOf(marker);
|
||||
if (idx !== -1) return absPath.slice(idx);
|
||||
return absPath;
|
||||
}
|
||||
|
||||
function libraryName(file: string): string {
|
||||
const nmIdx = file.indexOf("node_modules/");
|
||||
if (nmIdx === -1) return "node";
|
||||
const after = file.slice(nmIdx + "node_modules/".length);
|
||||
// Handle scoped packages like @scope/pkg
|
||||
if (after.startsWith("@")) {
|
||||
const parts = after.split("/");
|
||||
return `${parts[0]}/${parts[1]}`;
|
||||
}
|
||||
return after.split("/")[0];
|
||||
}
|
||||
|
||||
interface ParsedError {
|
||||
message: string;
|
||||
frames: ParsedFrame[];
|
||||
}
|
||||
|
||||
function parseError(error: unknown): ParsedError {
|
||||
if (!(error instanceof Error)) {
|
||||
return { message: String(error), frames: [] };
|
||||
}
|
||||
|
||||
const message = error.message ?? String(error);
|
||||
const stack = error.stack ?? "";
|
||||
|
||||
const lines = stack.split("\n");
|
||||
const frameLines: string[] = [];
|
||||
for (const line of lines) {
|
||||
if (line.trimStart().startsWith("at ")) {
|
||||
frameLines.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
const frames = frameLines
|
||||
.map(parseFrame)
|
||||
.filter((f): f is ParsedFrame => f !== null);
|
||||
|
||||
return { message, frames };
|
||||
}
|
||||
|
||||
// Group consecutive library frames into collapsed runs
|
||||
type FrameGroup =
|
||||
| { kind: "app"; frame: ParsedFrame }
|
||||
| { kind: "lib"; count: number; names: string[] };
|
||||
|
||||
function groupFrames(frames: ParsedFrame[]): FrameGroup[] {
|
||||
const groups: FrameGroup[] = [];
|
||||
let i = 0;
|
||||
while (i < frames.length) {
|
||||
if (frames[i].isApp) {
|
||||
groups.push({ kind: "app", frame: frames[i] });
|
||||
i++;
|
||||
} else {
|
||||
const libNames = new Set<string>();
|
||||
let count = 0;
|
||||
while (i < frames.length && !frames[i].isApp) {
|
||||
libNames.add(libraryName(frames[i].file));
|
||||
count++;
|
||||
i++;
|
||||
}
|
||||
groups.push({ kind: "lib", count, names: [...libNames] });
|
||||
}
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
function libSummary(count: number, names: string[]): string {
|
||||
const s = count === 1 ? "" : "s";
|
||||
return `... ${count} internal frame${s} (${names.join(", ")})`;
|
||||
}
|
||||
|
||||
// --- Console formatting (ANSI) ---
|
||||
|
||||
function formatError(error: unknown): string {
|
||||
const { message, frames } = parseError(error);
|
||||
if (frames.length === 0) {
|
||||
return `${bold}${red}ERROR${reset} ${message}`;
|
||||
}
|
||||
|
||||
const parts: string[] = [];
|
||||
parts.push(`${bold}${red}ERROR${reset} ${message}`);
|
||||
parts.push("");
|
||||
|
||||
for (const group of groupFrames(frames)) {
|
||||
if (group.kind === "app") {
|
||||
const rel = relativePath(group.frame.file);
|
||||
const loc = `${rel}:${group.frame.line}`;
|
||||
parts.push(
|
||||
` ${bold}${cyan}${loc.padEnd(24)}${reset}at ${group.frame.fn}`,
|
||||
);
|
||||
} else {
|
||||
parts.push(
|
||||
` ${dim}${libSummary(group.count, group.names)}${reset}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return parts.join("\n");
|
||||
}
|
||||
|
||||
// --- HTML formatting (browser) ---
|
||||
|
||||
function esc(s: string): string {
|
||||
return s
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """);
|
||||
}
|
||||
|
||||
function formatErrorHtml(error: unknown): string {
|
||||
const { message, frames } = parseError(error);
|
||||
const groups = groupFrames(frames);
|
||||
|
||||
let frameRows = "";
|
||||
for (const group of groups) {
|
||||
if (group.kind === "app") {
|
||||
const rel = relativePath(group.frame.file);
|
||||
const loc = `${rel}:${group.frame.line}`;
|
||||
frameRows += `<tr class="app">
|
||||
<td class="loc">${esc(loc)}</td>
|
||||
<td class="fn">at ${esc(group.frame.fn)}</td>
|
||||
</tr>\n`;
|
||||
} else {
|
||||
frameRows += `<tr class="lib">
|
||||
<td colspan="2">${esc(libSummary(group.count, group.names))}</td>
|
||||
</tr>\n`;
|
||||
}
|
||||
}
|
||||
|
||||
return `<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Error</title>
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body {
|
||||
font-family: "SF Mono", "Menlo", "Consolas", monospace;
|
||||
font-size: 14px;
|
||||
background: #1a1a2e;
|
||||
color: #e0e0e0;
|
||||
padding: 40px;
|
||||
}
|
||||
.error-label {
|
||||
display: inline-block;
|
||||
background: #e74c3c;
|
||||
color: #fff;
|
||||
font-weight: 700;
|
||||
font-size: 12px;
|
||||
padding: 2px 8px;
|
||||
border-radius: 3px;
|
||||
letter-spacing: 0.5px;
|
||||
}
|
||||
.message {
|
||||
margin-top: 12px;
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
color: #f8f8f2;
|
||||
line-height: 1.4;
|
||||
}
|
||||
table {
|
||||
margin-top: 24px;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
tr.app td { padding: 4px 0; }
|
||||
tr.app .loc {
|
||||
color: #56d4dd;
|
||||
font-weight: 600;
|
||||
padding-right: 24px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
tr.app .fn { color: #ccc; }
|
||||
tr.lib td {
|
||||
color: #666;
|
||||
padding: 4px 0;
|
||||
font-style: italic;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<span class="error-label">ERROR</span>
|
||||
<div class="message">${esc(message)}</div>
|
||||
<table>${frameRows}</table>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
export { formatError, formatErrorHtml };
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "diachron",
|
||||
"name": "express",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
"license": "ISC",
|
||||
"packageManager": "pnpm@10.12.4",
|
||||
"dependencies": {
|
||||
"diachron": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
}
|
||||
|
||||
@@ -6,4 +6,4 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
cd "$DIR"
|
||||
|
||||
exec ../cmd node --enable-source-maps dist/index.js "$@"
|
||||
exec ../cmd node dist/index.js "$@"
|
||||
|
||||
@@ -10,5 +10,5 @@
|
||||
"types": ["node"],
|
||||
"outDir": "out"
|
||||
},
|
||||
"exclude": ["**/*.spec.ts", "**/*.test.ts", "check-deps.ts"]
|
||||
"exclude": ["**/*.spec.ts", "**/*.test.ts"]
|
||||
}
|
||||
|
||||
49
bootstrap.sh
49
bootstrap.sh
@@ -1,49 +0,0 @@
|
||||
#!/bin/bash
|
||||
# shellcheck disable=SC2002
|
||||
|
||||
set -eu
|
||||
set -o pipefail
|
||||
IFS=$'\n\t'
|
||||
|
||||
# print useful message on failure
|
||||
trap 's=$?; echo >&2 "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR
|
||||
|
||||
# shellcheck disable=SC2034
|
||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# cd "$DIR"
|
||||
here="$PWD"
|
||||
|
||||
"$DIR/update-cached-repository.sh"
|
||||
|
||||
# repository="${2:-https://gitea.philologue.net/philologue/diachron}"
|
||||
repository="${2:-$HOME/.cache/diachron/v1/repositories/diachron.git}"
|
||||
ref="${1:-hydrators-kysely}"
|
||||
|
||||
echo will bootstrap ref "$ref" of repo "$repository"
|
||||
|
||||
into=$(mktemp -d)
|
||||
cd "$into"
|
||||
echo I am in $(pwd)
|
||||
echo I will clone repository "$repository", ref "$ref"
|
||||
git clone "$repository"
|
||||
|
||||
r=$(ls -1)
|
||||
|
||||
cd "$r"
|
||||
|
||||
echo I am in $(pwd)
|
||||
|
||||
git checkout "$ref"
|
||||
|
||||
ls
|
||||
echo working dir: $PWD
|
||||
# ls backend
|
||||
|
||||
# exit 0
|
||||
|
||||
tar cvf - $(cat "$PWD/file-list" | grep -v '^#') | (cd "$here" && tar xf -)
|
||||
|
||||
echo "$ref" > .diachron-version
|
||||
|
||||
echo "Now, run the command ./sync.sh"
|
||||
@@ -1,11 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -eu
|
||||
|
||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
ROOT="$DIR/../.."
|
||||
|
||||
cd "$ROOT/backend/diachron"
|
||||
|
||||
"$ROOT/cmd" tsx check-deps.ts "$@"
|
||||
@@ -5,5 +5,5 @@ set -eu
|
||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
ROOT="$DIR/../.."
|
||||
|
||||
cd "$ROOT/backend/diachron"
|
||||
cd "$ROOT/backend"
|
||||
"$DIR"/tsx migrate.ts "$@"
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
../common.d/check-deps
|
||||
@@ -1,5 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
"$DIR"/../shims/pnpm tsx "$@"
|
||||
@@ -1 +0,0 @@
|
||||
../common.d/check-deps
|
||||
@@ -1,5 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
"$DIR"/../shims/pnpm tsx "$@"
|
||||
@@ -5,7 +5,6 @@ backend/package.json
|
||||
backend/pnpm-workspace.yaml
|
||||
# express/framework
|
||||
cmd
|
||||
file-list
|
||||
develop
|
||||
diachron
|
||||
logger
|
||||
@@ -13,4 +12,3 @@ master
|
||||
mgmt
|
||||
sync.sh
|
||||
templates
|
||||
upgrade.sh
|
||||
|
||||
64
sync.sh
64
sync.sh
@@ -1,5 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Note: This is kind of AI slop and needs to be more carefully reviewed.
|
||||
|
||||
set -eu
|
||||
|
||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
@@ -23,66 +25,50 @@ pnpm_checksum_var="pnpm_checksum_${platform}"
|
||||
pnpm_binary_url="${!pnpm_binary_var}"
|
||||
pnpm_checksum="${!pnpm_checksum_var}"
|
||||
|
||||
cache_dir="$HOME/.cache/diachron/v1/binaries"
|
||||
local_dir="$DIR/diachron/binaries"
|
||||
mkdir -p "$cache_dir" "$local_dir"
|
||||
# Set up paths for shims to use
|
||||
nodejs_dist_dir="diachron/binaries/$nodejs_dirname"
|
||||
nodejs_bin_dir="$nodejs_dist_dir/bin"
|
||||
|
||||
# read_checksum_file <path>
|
||||
# Prints the contents of a checksum file, or empty string
|
||||
# if the file does not exist.
|
||||
read_checksum_file() {
|
||||
if [ -f "$1" ]; then
|
||||
cat "$1"
|
||||
# Ensure correct node version is installed
|
||||
node_installed_checksum_file="$DIR/diachron/binaries/.node.checksum"
|
||||
node_installed_checksum=""
|
||||
if [ -f "$node_installed_checksum_file" ]; then
|
||||
node_installed_checksum=$(cat "$node_installed_checksum_file")
|
||||
fi
|
||||
}
|
||||
|
||||
# Ensure Node.js is in the cache
|
||||
cached_node_checksum=$(read_checksum_file "$cache_dir/.node.checksum")
|
||||
if [ "$cached_node_checksum" != "$nodejs_checksum" ]; then
|
||||
if [ "$node_installed_checksum" != "$nodejs_checksum" ]; then
|
||||
echo "Downloading Node.js for $platform..."
|
||||
node_archive="$cache_dir/node.tar.xz"
|
||||
node_archive="$DIR/diachron/downloads/node.tar.xz"
|
||||
curl -fsSL "$nodejs_binary" -o "$node_archive"
|
||||
|
||||
echo "Verifying checksum..."
|
||||
echo "$nodejs_checksum $node_archive" | sha256_check
|
||||
|
||||
echo "Extracting Node.js..."
|
||||
rm -rf "${cache_dir:?}/$nodejs_dirname"
|
||||
tar -xf "$node_archive" -C "$cache_dir"
|
||||
tar -xf "$node_archive" -C "$DIR/diachron/binaries"
|
||||
rm "$node_archive"
|
||||
|
||||
echo "$nodejs_checksum" >"$cache_dir/.node.checksum"
|
||||
echo "$nodejs_checksum" >"$node_installed_checksum_file"
|
||||
fi
|
||||
|
||||
# Copy Node.js into the working directory if needed
|
||||
local_node_checksum=$(read_checksum_file "$local_dir/.node.checksum")
|
||||
if [ "$local_node_checksum" != "$nodejs_checksum" ]; then
|
||||
echo "Installing Node.js into project..."
|
||||
rm -rf "${local_dir:?}/$nodejs_dirname"
|
||||
cp -R "$cache_dir/$nodejs_dirname" "$local_dir/$nodejs_dirname"
|
||||
echo "$nodejs_checksum" >"$local_dir/.node.checksum"
|
||||
# Ensure correct pnpm version is installed
|
||||
pnpm_binary="$DIR/diachron/binaries/pnpm"
|
||||
pnpm_installed_checksum_file="$DIR/diachron/binaries/.pnpm.checksum"
|
||||
pnpm_installed_checksum=""
|
||||
if [ -f "$pnpm_installed_checksum_file" ]; then
|
||||
pnpm_installed_checksum=$(cat "$pnpm_installed_checksum_file")
|
||||
fi
|
||||
|
||||
# Ensure pnpm is in the cache
|
||||
cached_pnpm_checksum=$(read_checksum_file "$cache_dir/.pnpm.checksum")
|
||||
if [ "$cached_pnpm_checksum" != "$pnpm_checksum" ]; then
|
||||
if [ "$pnpm_installed_checksum" != "$pnpm_checksum" ]; then
|
||||
echo "Downloading pnpm for $platform..."
|
||||
curl -fsSL "$pnpm_binary_url" -o "$cache_dir/pnpm"
|
||||
curl -fsSL "$pnpm_binary_url" -o "$pnpm_binary"
|
||||
|
||||
echo "Verifying checksum..."
|
||||
echo "$pnpm_checksum $cache_dir/pnpm" | sha256_check
|
||||
echo "$pnpm_checksum $pnpm_binary" | sha256_check
|
||||
|
||||
chmod +x "$cache_dir/pnpm"
|
||||
chmod +x "$pnpm_binary"
|
||||
|
||||
echo "$pnpm_checksum" >"$cache_dir/.pnpm.checksum"
|
||||
fi
|
||||
|
||||
# Copy pnpm into the working directory if needed
|
||||
local_pnpm_checksum=$(read_checksum_file "$local_dir/.pnpm.checksum")
|
||||
if [ "$local_pnpm_checksum" != "$pnpm_checksum" ]; then
|
||||
echo "Installing pnpm into project..."
|
||||
cp "$cache_dir/pnpm" "$local_dir/pnpm"
|
||||
echo "$pnpm_checksum" >"$local_dir/.pnpm.checksum"
|
||||
echo "$pnpm_checksum" >"$pnpm_installed_checksum_file"
|
||||
fi
|
||||
|
||||
# Get golang binaries in place
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -eu
|
||||
set -o pipefail
|
||||
IFS=$'\n\t'
|
||||
|
||||
trap 's=$?; echo >&2 "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR
|
||||
|
||||
upstream=https://gitea.philologue.net/philologue/diachron
|
||||
cache_dir="$HOME/.cache/diachron/v1/repositories"
|
||||
cached_repo="$cache_dir/diachron.git"
|
||||
|
||||
mkdir -p "$cache_dir"
|
||||
|
||||
if [ -d "$cached_repo" ]; then
|
||||
echo "Updating cached repository..."
|
||||
git -C "$cached_repo" fetch --prune origin
|
||||
else
|
||||
echo "Creating cached repository..."
|
||||
git clone --mirror "$upstream" "$cached_repo"
|
||||
fi
|
||||
|
||||
109
upgrade.sh
109
upgrade.sh
@@ -1,109 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -eu
|
||||
set -o pipefail
|
||||
IFS=$'\n\t'
|
||||
|
||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
new_ref="${1:?Usage: upgrade.sh <new-ref>}"
|
||||
|
||||
cached_repo="$HOME/.cache/diachron/v1/repositories/diachron.git"
|
||||
tmpdir=""
|
||||
|
||||
cleanup() {
|
||||
if [ -n "$tmpdir" ]; then
|
||||
rm -rf "$tmpdir"
|
||||
fi
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
echo "=== Diachron Framework Upgrade ==="
|
||||
echo ""
|
||||
echo "This will replace all framework files in your project."
|
||||
echo "Make sure you have committed or backed up any local changes."
|
||||
echo ""
|
||||
read -r -p "Continue? [y/N] " answer
|
||||
if [[ ! "$answer" =~ ^[Yy]$ ]]; then
|
||||
echo "Aborted."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Update cached repository
|
||||
"$DIR/update-cached-repository.sh"
|
||||
|
||||
# Read current version
|
||||
if [ ! -f "$DIR/.diachron-version" ]; then
|
||||
echo "Error: .diachron-version not found." >&2
|
||||
echo "Is this a diachron project?" >&2
|
||||
exit 1
|
||||
fi
|
||||
old_ref=$(cat "$DIR/.diachron-version")
|
||||
|
||||
# Verify both refs exist in cached repo
|
||||
if ! git -C "$cached_repo" rev-parse --verify "$old_ref^{commit}" >/dev/null 2>&1; then
|
||||
echo "Error: current version '$old_ref' not found in cached repository." >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! git -C "$cached_repo" rev-parse --verify "$new_ref^{commit}" >/dev/null 2>&1; then
|
||||
echo "Error: target version '$new_ref' not found in cached repository." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Require a clean working tree
|
||||
if [ -n "$(git -C "$DIR" status --porcelain)" ]; then
|
||||
echo "Error: working tree is not clean." >&2
|
||||
echo "Commit or stash all changes (including untracked files) before upgrading." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Upgrading: $old_ref -> $new_ref"
|
||||
echo ""
|
||||
|
||||
# Read current file-list (files to remove)
|
||||
old_files=()
|
||||
while IFS= read -r line; do
|
||||
[[ "$line" =~ ^[[:space:]]*# ]] && continue
|
||||
[[ -z "$line" ]] && continue
|
||||
old_files+=("$line")
|
||||
done < "$DIR/file-list"
|
||||
|
||||
# Clone and checkout new version into a temp directory
|
||||
tmpdir=$(mktemp -d)
|
||||
git clone --quiet "$cached_repo" "$tmpdir/diachron"
|
||||
git -C "$tmpdir/diachron" checkout --quiet "$new_ref"
|
||||
|
||||
# Read new file-list (files to add)
|
||||
new_files=()
|
||||
while IFS= read -r line; do
|
||||
[[ "$line" =~ ^[[:space:]]*# ]] && continue
|
||||
[[ -z "$line" ]] && continue
|
||||
new_files+=("$line")
|
||||
done < "$tmpdir/diachron/file-list"
|
||||
|
||||
# Remove old framework files
|
||||
for f in "${old_files[@]}"; do
|
||||
git -C "$DIR" rm -rf --quiet --ignore-unmatch "$f"
|
||||
done
|
||||
|
||||
# Copy in new framework files
|
||||
(cd "$tmpdir/diachron" && tar cvf - "${new_files[@]}") | (cd "$DIR" && tar xf -)
|
||||
|
||||
# Stage them
|
||||
for f in "${new_files[@]}"; do
|
||||
git -C "$DIR" add "$f"
|
||||
done
|
||||
|
||||
# Update version marker
|
||||
echo "$new_ref" > "$DIR/.diachron-version"
|
||||
git -C "$DIR" add "$DIR/.diachron-version"
|
||||
|
||||
echo "=== Upgrade staged: $old_ref -> $new_ref ==="
|
||||
echo ""
|
||||
echo "Framework files have been removed, replaced, and staged."
|
||||
echo ""
|
||||
echo "Next steps:"
|
||||
echo " 1. Review: git diff --cached"
|
||||
echo " 2. Commit: git commit -m 'Upgrade diachron to $new_ref'"
|
||||
echo " 3. Install: ./sync.sh"
|
||||
Reference in New Issue
Block a user