Compare commits
32 Commits
33251d9b77
...
hydrators-
| Author | SHA1 | Date | |
|---|---|---|---|
| 5947dcdc86 | |||
| f0aca17a0a | |||
| 5be7b84972 | |||
| ae077886ba | |||
| 8318e60c33 | |||
| cf04ecc78a | |||
| 82e87577cc | |||
| 98f1f554c1 | |||
| 554f0e0044 | |||
| 530e3dccef | |||
| 91780b6dca | |||
| a39ed37a03 | |||
| 35165dcefe | |||
| dbd4e0a687 | |||
| 7b271da2b8 | |||
| 940cef138e | |||
| 296e460326 | |||
| 738e622fdc | |||
| 034b035a91 | |||
| f352ae44e1 | |||
| 341db4f821 | |||
| eabec3816b | |||
| b752eb5080 | |||
| 1ed5aa4b33 | |||
| 4d1c30b874 | |||
| 02edf259f0 | |||
| db1f2151de | |||
| 6e669d025a | |||
| a1dbf71de4 | |||
| 0afc3efa5d | |||
| 6f2ca2c15d | |||
| 6a41273835 |
8
.gitignore
vendored
8
.gitignore
vendored
@@ -1,5 +1,5 @@
|
||||
**/node_modules
|
||||
framework/downloads
|
||||
framework/binaries
|
||||
framework/.nodejs
|
||||
framework/.nodejs-config
|
||||
diachron/downloads
|
||||
diachron/binaries
|
||||
diachron/.nodejs
|
||||
diachron/.nodejs-config
|
||||
|
||||
44
AGENTS.md
Normal file
44
AGENTS.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# Agent Instructions
|
||||
|
||||
Read and follow the instructions in `diachron/AGENTS.md`. That file
|
||||
contains framework conventions, commands, and structure that apply to
|
||||
all coding agents working on diachron-based projects.
|
||||
|
||||
This project uses **bd** (beads) for issue tracking. Run `bd onboard` to get started.
|
||||
|
||||
## Quick Reference
|
||||
|
||||
```bash
|
||||
bd ready # Find available work
|
||||
bd show <id> # View issue details
|
||||
bd update <id> --status in_progress # Claim work
|
||||
bd close <id> # Complete work
|
||||
bd sync # Sync with git
|
||||
```
|
||||
|
||||
## Landing the Plane (Session Completion)
|
||||
|
||||
**When ending a work session**, you MUST complete ALL steps below. Work is NOT complete until `git push` succeeds.
|
||||
|
||||
**MANDATORY WORKFLOW:**
|
||||
|
||||
1. **File issues for remaining work** - Create issues for anything that needs follow-up
|
||||
2. **Run quality gates** (if code changed) - Tests, linters, builds
|
||||
3. **Update issue status** - Close finished work, update in-progress items
|
||||
4. **PUSH TO REMOTE** - This is MANDATORY:
|
||||
```bash
|
||||
git pull --rebase
|
||||
bd sync
|
||||
git push
|
||||
git status # MUST show "up to date with origin"
|
||||
```
|
||||
5. **Clean up** - Clear stashes, prune remote branches
|
||||
6. **Verify** - All changes committed AND pushed
|
||||
7. **Hand off** - Provide context for next session
|
||||
|
||||
**CRITICAL RULES:**
|
||||
- Work is NOT complete until `git push` succeeds
|
||||
- NEVER stop before pushing - that leaves work stranded locally
|
||||
- NEVER say "ready to push when you are" - YOU must push
|
||||
- If push fails, resolve and retry until it succeeds
|
||||
|
||||
18
CLAUDE.md
18
CLAUDE.md
@@ -3,6 +3,10 @@
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with
|
||||
code in this repository.
|
||||
|
||||
Read and follow the instructions in `diachron/AGENTS.md`. That file
|
||||
contains framework conventions, commands, and structure that apply to
|
||||
all coding agents working on diachron-based projects.
|
||||
|
||||
## Project Overview
|
||||
|
||||
Diachron is an opinionated TypeScript/Node.js web framework with a Go-based
|
||||
@@ -38,7 +42,7 @@ master process. Key design principles:
|
||||
|
||||
**Format TypeScript code:**
|
||||
```bash
|
||||
cd express && ../cmd pnpm biome check --write .
|
||||
cd backend && ../cmd pnpm biome check --write .
|
||||
```
|
||||
|
||||
**Build Go master process:**
|
||||
@@ -54,9 +58,9 @@ cd master && go build
|
||||
|
||||
### Components
|
||||
|
||||
- **express/** - TypeScript/Express.js backend application
|
||||
- **backend/** - TypeScript/Express.js backend application
|
||||
- **master/** - Go-based master process for file watching and process management
|
||||
- **framework/** - Managed binaries (Node.js, pnpm), command wrappers, and
|
||||
- **diachron/** - Managed binaries (Node.js, pnpm), command wrappers, and
|
||||
framework-specific library code
|
||||
- **monitor/** - Go file watcher that triggers rebuilds (experimental)
|
||||
|
||||
@@ -68,7 +72,7 @@ Responsibilities:
|
||||
- Proxy web requests to backend workers
|
||||
- Behaves identically in all environments (no dev/prod distinction)
|
||||
|
||||
### Express App Structure
|
||||
### Backend App Structure
|
||||
|
||||
- `app.ts` - Main Express application setup with route matching
|
||||
- `routes.ts` - Route definitions
|
||||
@@ -78,7 +82,7 @@ Responsibilities:
|
||||
|
||||
### Framework Command System
|
||||
|
||||
Commands flow through: `./cmd` → `framework/cmd.d/*` → `framework/shims/*` → managed binaries in `framework/binaries/`
|
||||
Commands flow through: `./cmd` → `diachron/cmd.d/*` → `diachron/shims/*` → managed binaries in `diachron/binaries/`
|
||||
|
||||
This ensures consistent tooling versions across the team without system-wide installations.
|
||||
|
||||
@@ -94,8 +98,8 @@ This ensures consistent tooling versions across the team without system-wide ins
|
||||
|
||||
## Platform Requirements
|
||||
|
||||
Linux x86_64 only (currently). Requires:
|
||||
- Modern libc for Go binaries
|
||||
Linux or macOS on x86_64. Requires:
|
||||
- Modern libc for Go binaries (Linux)
|
||||
- docker compose (for full stack)
|
||||
- fd, shellcheck, shfmt (for development)
|
||||
|
||||
|
||||
184
DIACHRON.md
Normal file
184
DIACHRON.md
Normal file
@@ -0,0 +1,184 @@
|
||||
# What is diachron?
|
||||
|
||||
diachron is a web framework for TypeScript and Node.js. It uses a Go-based
|
||||
master process that handles file watching, building, process management, and
|
||||
request proxying. The application code is TypeScript running on Express.js.
|
||||
|
||||
If you're joining a project that uses diachron, this document will orient you.
|
||||
|
||||
## Why diachron exists
|
||||
|
||||
diachron was built around a few frustrations with mainstream web frameworks:
|
||||
|
||||
- **No dev/prod split.** Most frameworks behave differently in development and
|
||||
production. diachron doesn't. The master process watches files, rebuilds,
|
||||
and manages workers the same way everywhere. There is no `NODE_ENV`.
|
||||
|
||||
- **Managed tooling.** Node.js, pnpm, and other tools are downloaded and
|
||||
pinned to exact versions inside the project. You don't install them
|
||||
system-wide. Everyone on the team runs the same binaries.
|
||||
|
||||
- **PostgreSQL, directly.** No ORM, no database abstraction layer. You write
|
||||
SQL (via Kysely for type safety) and talk to PostgreSQL. If you need
|
||||
MySQL or SQLite support, this is not the framework for you.
|
||||
|
||||
- **Debuggability over magic.** Everything is explicit and inspectable.
|
||||
Logging and observability are first-class concerns, not afterthoughts.
|
||||
|
||||
diachron is inspired by the
|
||||
[Taking PHP Seriously](https://slack.engineering/taking-php-seriously/) essay
|
||||
from Slack Engineering. It's designed for small to medium systems (what we
|
||||
call "Ring 0 and Ring 1") -- not heavy-compliance or banking-scale
|
||||
applications.
|
||||
|
||||
## How it works
|
||||
|
||||
When you run `./master`, the following happens:
|
||||
|
||||
1. The Go master process starts and watches your TypeScript source files.
|
||||
2. It builds the backend using `@vercel/ncc`, producing a single bundled JS
|
||||
file.
|
||||
3. It starts one or more Node.js worker processes running your Express app.
|
||||
4. It proxies HTTP requests from port 8080 to the workers.
|
||||
5. When you edit a source file, it rebuilds and restarts the workers
|
||||
automatically.
|
||||
6. If a worker crashes, it restarts automatically.
|
||||
|
||||
There is no separate "dev server" or "hot module replacement." The master
|
||||
process is the only way to run the application.
|
||||
|
||||
## Project structure
|
||||
|
||||
A diachron project looks like this:
|
||||
|
||||
```
|
||||
.
|
||||
├── DIACHRON.md # This file (framework overview for newcomers)
|
||||
├── master/ # Go master process (framework-owned)
|
||||
├── logger/ # Go logging service (framework-owned)
|
||||
├── diachron/ # Managed binaries, shims, framework library
|
||||
│ ├── AGENTS.md # Guide for AI coding agents
|
||||
│ ├── binaries/ # Downloaded Node.js, pnpm (gitignored)
|
||||
│ ├── cmd.d/ # Commands available via ./cmd
|
||||
│ ├── shims/ # Wrappers that use managed binaries
|
||||
│ └── ...
|
||||
├── backend/ # Your application code
|
||||
│ ├── app.ts # Entry point
|
||||
│ ├── routes.ts # Route definitions
|
||||
│ ├── handlers.ts # Route handlers
|
||||
│ ├── services.ts # Service layer
|
||||
│ ├── types.ts # Application types
|
||||
│ ├── config.ts # Application configuration
|
||||
│ └── diachron/ # Framework library code (framework-owned)
|
||||
├── cmd # Run managed commands (./cmd pnpm install, etc.)
|
||||
├── develop # Development-only commands (./develop reset-db, etc.)
|
||||
├── mgmt # Management commands safe for production
|
||||
├── sync.sh # Install/update all dependencies
|
||||
├── master # The compiled master binary (after sync)
|
||||
└── docker-compose.yml
|
||||
```
|
||||
|
||||
### File ownership
|
||||
|
||||
There are two owners of files in a diachron project:
|
||||
|
||||
- **You own** everything in `backend/` (except `backend/diachron/`), plus
|
||||
`docker-compose.yml`, `package.json`, and anything else you create.
|
||||
- **The framework owns** `master/`, `logger/`, `diachron/`,
|
||||
`backend/diachron/`, and the top-level scripts (`cmd`, `develop`, `mgmt`,
|
||||
`sync.sh`, `check.sh`).
|
||||
|
||||
Don't modify framework-owned files unless you need to. This separation
|
||||
keeps framework upgrades clean. If you do need to change framework files
|
||||
(especially early on, there are rough edges), you can extract your changes
|
||||
as a patch:
|
||||
|
||||
```bash
|
||||
./diff-upstream.sh # full diff against upstream
|
||||
./diff-upstream.sh --stat # just list changed files
|
||||
```
|
||||
|
||||
This diffs every file in `file-list` against the upstream ref recorded in
|
||||
`.diachron-version`.
|
||||
|
||||
When you do change framework files, make each change in its own commit with
|
||||
a clear message explaining what the change is and why it's needed. Mixing
|
||||
framework fixes with application work in a single commit makes it much
|
||||
harder to upstream later. A clean history of discrete, well-explained
|
||||
framework commits is the easiest thing to turn into contributions.
|
||||
|
||||
## Getting started
|
||||
|
||||
```bash
|
||||
# Install dependencies and build the master process
|
||||
./sync.sh
|
||||
|
||||
# Start the application
|
||||
./master
|
||||
```
|
||||
|
||||
The app will be available at `http://localhost:8080`.
|
||||
|
||||
You need Linux or macOS on x86_64. For the full stack (database, Redis,
|
||||
etc.), you also need `docker compose`.
|
||||
|
||||
## The command system
|
||||
|
||||
diachron has three types of commands, separated by intent and safety:
|
||||
|
||||
- **`./cmd <command>`** -- Run managed tools (node, pnpm, tsx, etc.). These
|
||||
use the project's pinned versions, not whatever is installed on your system.
|
||||
|
||||
```bash
|
||||
./cmd pnpm install
|
||||
./cmd pnpm test
|
||||
```
|
||||
|
||||
- **`./mgmt <command>`** -- Management commands that are safe to run in
|
||||
production. Migrations, user management, that sort of thing.
|
||||
|
||||
```bash
|
||||
./mgmt migrate
|
||||
./mgmt add-user
|
||||
```
|
||||
|
||||
- **`./develop <command>`** -- Development commands that may be destructive.
|
||||
Database resets, fixture loading, etc. These are gated in production.
|
||||
|
||||
```bash
|
||||
./develop reset-db
|
||||
./develop db # Open a database shell
|
||||
```
|
||||
|
||||
The rule of thumb: if you'd run it at 3am while tired and worried, it's a
|
||||
`mgmt` command. If it destroys data on purpose, it's a `develop` command.
|
||||
|
||||
## Key concepts
|
||||
|
||||
### Call and Result
|
||||
|
||||
diachron wraps Express's `Request` and `Response` in its own types called
|
||||
`Call` and `Result`. This avoids shadowing and keeps the framework's
|
||||
interface distinct from Express internals. Your handlers receive a `Call`
|
||||
and return a `Result`.
|
||||
|
||||
### Routes
|
||||
|
||||
Routes are defined as data (arrays of `Route` objects in `routes.ts`), not
|
||||
through decorators or method chaining. The framework processes them into
|
||||
Express handlers.
|
||||
|
||||
### No environment variables for behavior
|
||||
|
||||
There is no `NODE_ENV`, no `DEBUG`, no mode switching. Configuration that
|
||||
must vary between deployments (database URLs, secrets) lives in
|
||||
configuration files, but the application's behavior doesn't branch on
|
||||
environment.
|
||||
|
||||
## Further reading
|
||||
|
||||
- `README.md` -- Project introduction and requirements
|
||||
- `diachron/AGENTS.md` -- Guide for AI coding agents
|
||||
- `docs/` -- Design documents and philosophy
|
||||
- `docs/commands.md` -- Detailed explanation of the command system
|
||||
- `docs/concentric-circles.md` -- What diachron is (and isn't) designed for
|
||||
25
README.md
25
README.md
@@ -2,16 +2,13 @@ diachron
|
||||
|
||||
## Introduction
|
||||
|
||||
Is your answer to some of these questions "yes"? If so, you might like
|
||||
diachron. (When it comes to that dev/test/prod one, hear us out first, ok?)
|
||||
|
||||
- Do you want to share a lot of backend and frontend code?
|
||||
|
||||
- Are you tired of your web stack breaking when you blink too hard?
|
||||
|
||||
- Have you read [Taking PHP
|
||||
Seriously](https://slack.engineering/taking-php-seriously/) and wish you had
|
||||
something similar for Typescript?
|
||||
Seriously](https://slack.engineering/taking-php-seriously/) and do you wish
|
||||
you had something similar for Typescript?
|
||||
|
||||
- Do you think that ORMs are not all that? Do you wish you had first class
|
||||
unmediated access to your database? And do you think that database
|
||||
@@ -35,6 +32,9 @@ diachron. (When it comes to that dev/test/prod one, hear us out first, ok?)
|
||||
you're trying to fix? We're talking authentication, authorization, XSS,
|
||||
https, nested paths, all that stuff.
|
||||
|
||||
Is your answer to some of these questions "yes"? If so, you might like
|
||||
diachron. (When it comes to that dev/test/prod one, hear us out first, ok?)
|
||||
|
||||
## Getting started
|
||||
|
||||
Different situations require different getting started docs.
|
||||
@@ -44,12 +44,21 @@ Different situations require different getting started docs.
|
||||
|
||||
## Requirements
|
||||
|
||||
To run diachron, you currently need to have a Linux box running x86_64 with a
|
||||
new enough libc to run golang binaries. Support for other platforms will come
|
||||
eventually.
|
||||
To run diachron, you need Linux or macOS on x86_64. Linux requires a new
|
||||
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:
|
||||
|
||||
7
TODO.md
7
TODO.md
@@ -56,6 +56,13 @@ CREATE TABLE app.customer_metadata (...);
|
||||
leaves around `master-bin`, `logger-bin`, and `diachron:nnnn` processes.
|
||||
Huge problem.
|
||||
|
||||
- [ ] Fix format used by master (and logger?)'s output: it should be logfmt
|
||||
- A lot of other stuff should probably be logfmt too but maybe we can get to
|
||||
that later
|
||||
|
||||
- [ ] master rebuilds (or tries to) too many times; need some sort of debounce
|
||||
or whatever it's called
|
||||
|
||||
## medium importance
|
||||
|
||||
- [ ] Add a log viewer
|
||||
|
||||
0
express/.gitignore → backend/.gitignore
vendored
0
express/.gitignore → backend/.gitignore
vendored
1
backend/.npmrc
Normal file
1
backend/.npmrc
Normal file
@@ -0,0 +1 @@
|
||||
shamefully-hoist=true
|
||||
20
backend/app.ts
Normal file
20
backend/app.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
// This is a sample file provided by diachron. You are encouraged to modify it.
|
||||
|
||||
|
||||
|
||||
|
||||
import { core } from "./diachron/core";
|
||||
|
||||
import { routes } from "./routes";
|
||||
import {makeApp}from'./diachron/app'
|
||||
|
||||
|
||||
const app = makeApp({routes});
|
||||
|
||||
|
||||
core.logging.log({ source: "logging", text: ["1"] });
|
||||
|
||||
|
||||
|
||||
|
||||
app.start()
|
||||
@@ -6,4 +6,4 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
cd "$DIR"
|
||||
|
||||
../cmd pnpm ncc build ./app.ts -o dist
|
||||
../cmd pnpm ncc build ./app.ts -o dist --source-map
|
||||
66
backend/check-deps.ts
Normal file
66
backend/check-deps.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
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.");
|
||||
@@ -8,7 +8,8 @@ check_dir="$DIR"
|
||||
|
||||
out_dir="$check_dir/out"
|
||||
|
||||
source "$check_dir"/../framework/shims/common
|
||||
source "$check_dir"/../framework/shims/node.common
|
||||
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"
|
||||
137
backend/diachron/app.ts
Normal file
137
backend/diachron/app.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
// FIXME: rename this to make-app.ts and adjust imports accordingly
|
||||
|
||||
import{contentTypes} from './content-types'
|
||||
import{httpCodes}from'./http-codes'
|
||||
import express, {
|
||||
type 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 { cli } from "./cli";
|
||||
import{processRoutes}from'./routing'
|
||||
|
||||
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error(formatError(err));
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason) => {
|
||||
console.error(formatError(reason));
|
||||
});
|
||||
|
||||
|
||||
type MakeAppArgs={routes:Route[],
|
||||
processTitle?: string,
|
||||
}
|
||||
|
||||
export interface DiachronApp extends Express {
|
||||
start: () => void
|
||||
}
|
||||
|
||||
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, match.params);
|
||||
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
|
||||
const retval: Result = {
|
||||
code: httpCodes.clientErrors.NotFound,
|
||||
contentType: contentTypes.text.plain,
|
||||
result: "not found!",
|
||||
};
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
// I don't like going around tsc but this is simple enough that it's probably OK.
|
||||
const app = express() as DiachronApp
|
||||
app.start = function() {
|
||||
this.listen(cli.listen.port, cli.listen.host, () => {
|
||||
console.log(`Listening on ${cli.listen.host}:${cli.listen.port}`);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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,5 +1,5 @@
|
||||
import { DateTime } from "ts-luxon";
|
||||
import { get, User } from "../framework/hydrators/user";
|
||||
import { get, User } from "../hydrators/user";
|
||||
import { request } from "../request";
|
||||
import { html, render } from "../request/util";
|
||||
import type { Call, Result, Route } from "../types";
|
||||
@@ -10,9 +10,16 @@ const routes: Record<string, Route> = {
|
||||
hello: {
|
||||
path: "/hello",
|
||||
methods: ["GET"],
|
||||
handler: async (_call: Call): Promise<Result> => {
|
||||
handler: async (call: Call): Promise<Result> => {
|
||||
const now = DateTime.now();
|
||||
const c = await render("basic/hello", { now });
|
||||
const args: {now: DateTime,greeting?: string} = {now};
|
||||
|
||||
if (call.path !== '/hello') {
|
||||
const greeting = call. path.replaceAll('/','').replaceAll('-', ' ')
|
||||
args.greeting = greeting;
|
||||
}
|
||||
|
||||
const c = await render("basic/hello", args);
|
||||
|
||||
return html(c);
|
||||
},
|
||||
@@ -1,5 +1,6 @@
|
||||
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.
|
||||
@@ -40,6 +41,7 @@ const misc = {
|
||||
const core = {
|
||||
conf,
|
||||
database,
|
||||
errors: { formatError, formatErrorHtml },
|
||||
logging,
|
||||
misc,
|
||||
random,
|
||||
@@ -113,7 +113,7 @@ async function raw<T = unknown>(
|
||||
//
|
||||
// Migrations directory: express/migrations/
|
||||
|
||||
const FRAMEWORK_MIGRATIONS_DIR = path.join(__dirname, "framework/migrations");
|
||||
const FRAMEWORK_MIGRATIONS_DIR = path.join(__dirname, "diachron/migrations");
|
||||
const APP_MIGRATIONS_DIR = path.join(__dirname, "migrations");
|
||||
const MIGRATIONS_TABLE = "_migrations";
|
||||
|
||||
229
backend/diachron/errors.ts
Normal file
229
backend/diachron/errors.ts
Normal file
@@ -0,0 +1,229 @@
|
||||
// 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,7 +1,7 @@
|
||||
import { Kysely, PostgresDialect } from "kysely";
|
||||
import { Pool } from "pg";
|
||||
import { connectionConfig } from "../../database";
|
||||
import type { DB } from "../../generated/db";
|
||||
import { connectionConfig } from "../database";
|
||||
|
||||
const db = new Kysely<DB>({
|
||||
dialect: new PostgresDialect({
|
||||
1
backend/diachron/hydrators/index.ts
Normal file
1
backend/diachron/hydrators/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type Hydrators = {};
|
||||
@@ -2,7 +2,7 @@
|
||||
// Run: DB_PORT=5433 DB_USER=diachron_test DB_PASSWORD=diachron_test DB_NAME=diachron_test npx tsx --test tests/*.test.ts
|
||||
|
||||
import { Pool } from "pg";
|
||||
import { connectionConfig, migrate } from "../../../database";
|
||||
import { connectionConfig, migrate } from "../../database";
|
||||
|
||||
const pool = new Pool(connectionConfig);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Tests for user hydrator
|
||||
// Run with: cd express && DB_PORT=5433 DB_USER=diachron_test DB_PASSWORD=diachron_test DB_NAME=diachron_test ../cmd npx tsx --test framework/hydrators/tests/user.test.ts
|
||||
// Run with: cd express && DB_PORT=5433 DB_USER=diachron_test DB_PASSWORD=diachron_test DB_NAME=diachron_test ../cmd npx tsx --test diachron/hydrators/tests/user.test.ts
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { after, before, beforeEach, describe, it } from "node:test";
|
||||
59
backend/diachron/hydrators/user.ts
Normal file
59
backend/diachron/hydrators/user.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import {
|
||||
ColumnType,
|
||||
Generated,
|
||||
Insertable,
|
||||
JSONColumnType,
|
||||
Selectable,
|
||||
Updateable,
|
||||
} from "kysely";
|
||||
import type { TypeID } from "typeid-js";
|
||||
import { z } from "zod";
|
||||
import { db, Hydrator } from "./hydrator";
|
||||
|
||||
const parser = z.object({
|
||||
// id: z.uuidv7(),
|
||||
id: z.uuid(),
|
||||
display_name: z.string(),
|
||||
// FIXME: status is duplicated elsewhere
|
||||
status: z.union([
|
||||
z.literal("active"),
|
||||
z.literal("suspended"),
|
||||
z.literal("pending"),
|
||||
]),
|
||||
email: z.email(),
|
||||
});
|
||||
|
||||
const tp = parser.parse({
|
||||
id: "cfae0a19-6515-4813-bc2d-1e032b72b203",
|
||||
display_name: "foo",
|
||||
status: "active",
|
||||
email: "mw@philologue.net",
|
||||
});
|
||||
|
||||
export type User = z.infer<typeof parser>;
|
||||
|
||||
const get = async (id: string): Promise<null | User> => {
|
||||
const ret = await db
|
||||
.selectFrom("users")
|
||||
.where("users.id", "=", id)
|
||||
.innerJoin("user_emails", "user_emails.user_id", "users.id")
|
||||
.select([
|
||||
"users.id",
|
||||
"users.status",
|
||||
"users.display_name",
|
||||
"user_emails.email",
|
||||
])
|
||||
.executeTakeFirst();
|
||||
|
||||
if (ret === undefined) {
|
||||
return null;
|
||||
}
|
||||
|
||||
console.dir(ret);
|
||||
|
||||
const parsed = parser.parse(ret);
|
||||
|
||||
return parsed;
|
||||
};
|
||||
|
||||
export { get };
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "express",
|
||||
"name": "diachron",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
94
backend/diachron/routing.ts
Normal file
94
backend/diachron/routing.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
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 {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";
|
||||
|
||||
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,
|
||||
params: Record<string, string>,
|
||||
): 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: params,
|
||||
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;
|
||||
}
|
||||
|
||||
export{processRoutes}
|
||||
@@ -29,13 +29,13 @@ export type Call = {
|
||||
pattern: string;
|
||||
path: string;
|
||||
method: Method;
|
||||
parameters: object;
|
||||
parameters: Record<string, string>;
|
||||
request: ExpressRequest;
|
||||
user: User;
|
||||
session: Session;
|
||||
};
|
||||
|
||||
export type InternalHandler = (req: ExpressRequest) => Promise<Result>;
|
||||
export type InternalHandler = (req: ExpressRequest, params: Record<string, string>) => Promise<Result>;
|
||||
|
||||
export type Handler = (call: Call) => Promise<Result>;
|
||||
export type ProcessedRoute = {
|
||||
109
backend/generated/db.d.ts
vendored
Normal file
109
backend/generated/db.d.ts
vendored
Normal file
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* This file was generated by kysely-codegen.
|
||||
* Please do not edit it manually.
|
||||
*/
|
||||
|
||||
import type { ColumnType } from "kysely";
|
||||
|
||||
export type Generated<T> =
|
||||
T extends ColumnType<infer S, infer I, infer U>
|
||||
? ColumnType<S, I | undefined, U>
|
||||
: ColumnType<T, T | undefined, T>;
|
||||
|
||||
export type Timestamp = ColumnType<Date, Date | string, Date | string>;
|
||||
|
||||
export interface _Migrations {
|
||||
applied_at: Generated<Timestamp>;
|
||||
id: Generated<number>;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface Capabilities {
|
||||
description: string | null;
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface Groups {
|
||||
created_at: Generated<Timestamp>;
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface RoleCapabilities {
|
||||
capability_id: string;
|
||||
granted_at: Generated<Timestamp>;
|
||||
revoked_at: Timestamp | null;
|
||||
role_id: string;
|
||||
}
|
||||
|
||||
export interface Roles {
|
||||
description: string | null;
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface Sessions {
|
||||
auth_method: string;
|
||||
created_at: Generated<Timestamp>;
|
||||
expires_at: Timestamp;
|
||||
id: Generated<string>;
|
||||
ip_address: string | null;
|
||||
is_used: Generated<boolean | null>;
|
||||
revoked_at: Timestamp | null;
|
||||
token_hash: string;
|
||||
token_type: string;
|
||||
user_agent: string | null;
|
||||
user_email_id: string | null;
|
||||
user_id: string;
|
||||
}
|
||||
|
||||
export interface UserCredentials {
|
||||
created_at: Generated<Timestamp>;
|
||||
credential_type: Generated<string>;
|
||||
id: string;
|
||||
password_hash: string | null;
|
||||
updated_at: Generated<Timestamp>;
|
||||
user_id: string;
|
||||
}
|
||||
|
||||
export interface UserEmails {
|
||||
created_at: Generated<Timestamp>;
|
||||
email: string;
|
||||
id: string;
|
||||
is_primary: Generated<boolean>;
|
||||
is_verified: Generated<boolean>;
|
||||
normalized_email: string;
|
||||
revoked_at: Timestamp | null;
|
||||
user_id: string;
|
||||
verified_at: Timestamp | null;
|
||||
}
|
||||
|
||||
export interface UserGroupRoles {
|
||||
granted_at: Generated<Timestamp>;
|
||||
group_id: string;
|
||||
revoked_at: Timestamp | null;
|
||||
role_id: string;
|
||||
user_id: string;
|
||||
}
|
||||
|
||||
export interface Users {
|
||||
created_at: Generated<Timestamp>;
|
||||
display_name: string | null;
|
||||
id: string;
|
||||
status: Generated<string>;
|
||||
updated_at: Generated<Timestamp>;
|
||||
}
|
||||
|
||||
export interface DB {
|
||||
_migrations: _Migrations;
|
||||
capabilities: Capabilities;
|
||||
groups: Groups;
|
||||
role_capabilities: RoleCapabilities;
|
||||
roles: Roles;
|
||||
sessions: Sessions;
|
||||
user_credentials: UserCredentials;
|
||||
user_emails: UserEmails;
|
||||
user_group_roles: UserGroupRoles;
|
||||
users: Users;
|
||||
}
|
||||
@@ -4,12 +4,12 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import type { Request as ExpressRequest } from "express";
|
||||
import { Session } from "./auth/types";
|
||||
import { contentTypes } from "./content-types";
|
||||
import { Session } from "./diachron/auth/types";
|
||||
import { contentTypes } from "./diachron/content-types";
|
||||
import { multiHandler } from "./handlers";
|
||||
import { httpCodes } from "./http-codes";
|
||||
import type { Call } from "./types";
|
||||
import { anonymousUser } from "./user";
|
||||
import { httpCodes } from "./diachron/http-codes";
|
||||
import type { Call } from "./diachron/types";
|
||||
import { anonymousUser } from "./diachron/user";
|
||||
|
||||
// Helper to create a minimal mock Call
|
||||
function createMockCall(overrides: Partial<Call> = {}): Call {
|
||||
@@ -1,7 +1,9 @@
|
||||
import { contentTypes } from "./content-types";
|
||||
import { core } from "./core";
|
||||
import { httpCodes } from "./http-codes";
|
||||
import type { Call, Handler, Result } from "./types";
|
||||
// This is a sample file provided by diachron. You are encouraged to modify it.
|
||||
|
||||
import { contentTypes } from "./diachron/content-types";
|
||||
import { core } from "./diachron/core";
|
||||
import { httpCodes } from "./diachron/http-codes";
|
||||
import type { Call, Handler, Result } from "./diachron/types";
|
||||
|
||||
const multiHandler: Handler = async (call: Call): Promise<Result> => {
|
||||
const code = httpCodes.success.OK;
|
||||
@@ -0,0 +1 @@
|
||||
CREATE TABLE test_application_table ();
|
||||
1
backend/migrations/2026-01-15_01.sql
Normal file
1
backend/migrations/2026-01-15_01.sql
Normal file
@@ -0,0 +1 @@
|
||||
CREATE TABLE test_application_table ();
|
||||
21
backend/package.json
Normal file
21
backend/package.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "my app",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "DB_PORT=5433 DB_USER=diachron_test DB_PASSWORD=diachron_test DB_NAME=diachron_test tsx --test '**/*.{test,spec}.ts'",
|
||||
"test:watch": "DB_PORT=5433 DB_USER=diachron_test DB_PASSWORD=diachron_test DB_NAME=diachron_test tsx --test --watch '**/*.{test,spec}.ts'",
|
||||
"nodemon": "nodemon dist/index.js",
|
||||
"kysely-codegen": "kysely-codegen"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"packageManager": "pnpm@10.12.4",
|
||||
"dependencies": {
|
||||
"diachron": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
}
|
||||
}
|
||||
2
backend/pnpm-workspace.yaml
Normal file
2
backend/pnpm-workspace.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
packages:
|
||||
- 'diachron'
|
||||
@@ -1,14 +1,16 @@
|
||||
// This is a sample file provided by diachron. You are encouraged to modify it.
|
||||
|
||||
/// <reference lib="dom" />
|
||||
|
||||
import nunjucks from "nunjucks";
|
||||
import { DateTime } from "ts-luxon";
|
||||
import { authRoutes } from "./auth/routes";
|
||||
import { routes as basicRoutes } from "./basic/routes";
|
||||
import { contentTypes } from "./content-types";
|
||||
import { core } from "./core";
|
||||
import { authRoutes } from "./diachron/auth/routes";
|
||||
import { routes as basicRoutes } from "./diachron/basic/routes";
|
||||
import { contentTypes } from "./diachron/content-types";
|
||||
import { core } from "./diachron/core";
|
||||
import { multiHandler } from "./handlers";
|
||||
import { httpCodes } from "./http-codes";
|
||||
import type { Call, Result, Route } from "./types";
|
||||
import { httpCodes } from "./diachron/http-codes";
|
||||
import type { Call, Result, Route } from "./diachron/types";
|
||||
|
||||
// FIXME: Obviously put this somewhere else
|
||||
const okText = (result: string): Result => {
|
||||
@@ -27,6 +29,9 @@ const routes: Route[] = [
|
||||
...authRoutes,
|
||||
basicRoutes.home,
|
||||
basicRoutes.hello,
|
||||
{...basicRoutes.hello,
|
||||
path: "/yo-whats-up"
|
||||
},
|
||||
basicRoutes.login,
|
||||
basicRoutes.logout,
|
||||
{
|
||||
@@ -35,7 +40,7 @@ const routes: Route[] = [
|
||||
handler: async (_call: Call): Promise<Result> => {
|
||||
console.log("starting slow request");
|
||||
|
||||
await core.misc.sleep(2);
|
||||
await core.misc.sleep(5000);
|
||||
|
||||
console.log("finishing slow request");
|
||||
const retval = okText("that was slow");
|
||||
@@ -72,7 +77,6 @@ const routes: Route[] = [
|
||||
`;
|
||||
const result = nunjucks.renderString(template, { rrr });
|
||||
|
||||
const _listing = lr(routes).join(", ");
|
||||
return {
|
||||
code,
|
||||
result,
|
||||
@@ -6,4 +6,4 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
cd "$DIR"
|
||||
|
||||
exec ../cmd node dist/index.js "$@"
|
||||
exec ../cmd node --enable-source-maps dist/index.js "$@"
|
||||
15
backend/services.ts
Normal file
15
backend/services.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
// This is a sample file provided by diachron. You are encouraged to modify it.
|
||||
|
||||
// Application services go here. A service encapsulates a capability that
|
||||
// handlers depend on: database queries, external API calls, business logic
|
||||
// that doesn't belong in a handler.
|
||||
//
|
||||
// The framework provides core services via `core` (from ./diachron/core):
|
||||
// core.database, core.logging, core.misc, etc. This file is for your
|
||||
// application's own services.
|
||||
|
||||
import { core } from "./diachron/core";
|
||||
|
||||
const db = core.database.db;
|
||||
|
||||
export { db };
|
||||
@@ -6,7 +6,7 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
check_dir="$DIR"
|
||||
|
||||
source "$check_dir"/../framework/shims/common
|
||||
source "$check_dir"/../framework/shims/node.common
|
||||
source "$check_dir"/../diachron/shims/common
|
||||
source "$check_dir"/../diachron/shims/node.common
|
||||
|
||||
$ROOT/cmd pnpm tsc --showConfig
|
||||
@@ -9,5 +9,6 @@
|
||||
"strict": true,
|
||||
"types": ["node"],
|
||||
"outDir": "out"
|
||||
}
|
||||
},
|
||||
"exclude": ["**/*.spec.ts", "**/*.test.ts", "check-deps.ts"]
|
||||
}
|
||||
8
backend/types.ts
Normal file
8
backend/types.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
// This is a sample file provided by diachron. You are encouraged to modify it.
|
||||
|
||||
// Application-specific types go here. Framework types (Call, Result, Route,
|
||||
// Handler, etc.) are defined in ./diachron/types and should be imported from
|
||||
// there.
|
||||
//
|
||||
// This file is for your domain types: the nouns and shapes that are specific
|
||||
// to your application.
|
||||
@@ -6,8 +6,8 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
check_dir="$DIR"
|
||||
|
||||
source "$check_dir"/../framework/shims/common
|
||||
source "$check_dir"/../framework/shims/node.common
|
||||
source "$check_dir"/../diachron/shims/common
|
||||
source "$check_dir"/../diachron/shims/node.common
|
||||
|
||||
# $ROOT/cmd pnpm tsc --lib ES2023 --esModuleInterop -w $check_dir/app.ts
|
||||
# $ROOT/cmd pnpm tsc -w $check_dir/app.ts
|
||||
49
bootstrap.sh
Executable file
49
bootstrap.sh
Executable file
@@ -0,0 +1,49 @@
|
||||
#!/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 '^#' | sed 's/^?//') | (cd "$here" && tar xf -)
|
||||
|
||||
echo "$ref" > "$here/.diachron-version"
|
||||
|
||||
echo "Now, run the command ./sync.sh"
|
||||
6
check.sh
6
check.sh
@@ -10,7 +10,7 @@ cd "$DIR"
|
||||
#
|
||||
exclusions="SC2002"
|
||||
|
||||
source "$DIR/framework/versions"
|
||||
source "$DIR/diachron/versions"
|
||||
|
||||
if [[ $# -ne 0 ]]; then
|
||||
shellcheck --exclude="$exclusions" "$@"
|
||||
@@ -20,10 +20,10 @@ fi
|
||||
shell_scripts="$(fd .sh | xargs)"
|
||||
|
||||
# The files we need to check all either end in .sh or else they're the files
|
||||
# in framework/cmd.d and framework/shims. -x instructs shellcheck to also
|
||||
# in diachron/cmd.d and diachron/shims. -x instructs shellcheck to also
|
||||
# check `source`d files.
|
||||
|
||||
shellcheck -x --exclude="$exclusions" "$DIR/cmd" "$DIR"/framework/cmd.d/* "$DIR"/framework/shims/* "$shell_scripts"
|
||||
shellcheck -x --exclude="$exclusions" "$DIR/cmd" "$DIR"/diachron/cmd.d/* "$DIR"/diachron/shims/* "$shell_scripts"
|
||||
|
||||
pushd "$DIR/master"
|
||||
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:$golangci_lint golangci-lint run
|
||||
|
||||
4
cmd
4
cmd
@@ -13,7 +13,7 @@ if [ $# -lt 1 ]; then
|
||||
echo "Usage: ./cmd <command> [args...]"
|
||||
echo ""
|
||||
echo "Available commands:"
|
||||
for cmd in "$DIR"/framework/cmd.d/*; do
|
||||
for cmd in "$DIR"/diachron/cmd.d/*; do
|
||||
if [ -x "$cmd" ]; then
|
||||
basename "$cmd"
|
||||
fi
|
||||
@@ -24,4 +24,4 @@ fi
|
||||
subcmd="$1"
|
||||
shift
|
||||
|
||||
exec "$DIR"/framework/cmd.d/"$subcmd" "$@"
|
||||
exec "$DIR"/diachron/cmd.d/"$subcmd" "$@"
|
||||
|
||||
4
develop
4
develop
@@ -13,7 +13,7 @@ if [ $# -lt 1 ]; then
|
||||
echo "Usage: ./develop <command> [args...]"
|
||||
echo ""
|
||||
echo "Available commands:"
|
||||
for cmd in "$DIR"/framework/develop.d/*; do
|
||||
for cmd in "$DIR"/diachron/develop.d/*; do
|
||||
if [ -x "$cmd" ]; then
|
||||
basename "$cmd"
|
||||
fi
|
||||
@@ -24,4 +24,4 @@ fi
|
||||
subcmd="$1"
|
||||
shift
|
||||
|
||||
exec "$DIR"/framework/develop.d/"$subcmd" "$@"
|
||||
exec "$DIR"/diachron/develop.d/"$subcmd" "$@"
|
||||
|
||||
0
diachron/.nodejs/.gitignore
vendored
Normal file
0
diachron/.nodejs/.gitignore
vendored
Normal file
214
diachron/AGENTS.md
Normal file
214
diachron/AGENTS.md
Normal file
@@ -0,0 +1,214 @@
|
||||
# Working with diachron (Agent Guide)
|
||||
|
||||
This document helps AI coding agents work effectively with projects built on
|
||||
the diachron framework. It covers the conventions, commands, and structures
|
||||
you need to know.
|
||||
|
||||
## Quick orientation
|
||||
|
||||
diachron is a TypeScript/Express web framework with a Go master process.
|
||||
Your application code lives in `backend/`. The framework owns `master/`,
|
||||
`logger/`, `diachron/`, and `backend/diachron/`.
|
||||
|
||||
**Do not modify framework-owned files** unless explicitly asked to work on
|
||||
the framework itself.
|
||||
|
||||
## Running the application
|
||||
|
||||
```bash
|
||||
./sync.sh # Install dependencies (run once, or after pulling changes)
|
||||
./master # Start the application (watches files, rebuilds, proxies)
|
||||
```
|
||||
|
||||
By default, the app listens on port 8080 (proxy) and workers run on
|
||||
ports starting at 3000.
|
||||
|
||||
## Commands you'll need
|
||||
|
||||
All tools (node, pnpm, tsx, etc.) are managed by the framework. Do not
|
||||
invoke system-installed versions.
|
||||
|
||||
```bash
|
||||
./cmd pnpm install # Install npm packages
|
||||
./cmd pnpm test # Run tests
|
||||
./cmd pnpm biome check . # Lint (run from backend/)
|
||||
./develop db # Open database shell
|
||||
./develop reset-db # Drop and recreate database
|
||||
./develop migrate # Run migrations (development)
|
||||
./mgmt migrate # Run migrations (production-safe)
|
||||
```
|
||||
|
||||
### Formatting and linting
|
||||
|
||||
```bash
|
||||
cd backend && ../cmd pnpm biome check --write .
|
||||
```
|
||||
|
||||
### Building Go code
|
||||
|
||||
```bash
|
||||
cd master && go build
|
||||
cd logger && go build
|
||||
```
|
||||
|
||||
### Quality checks
|
||||
|
||||
```bash
|
||||
./check.sh # shellcheck + golangci-lint
|
||||
```
|
||||
|
||||
## Application structure
|
||||
|
||||
### Where to put code
|
||||
|
||||
| What | Where |
|
||||
|--------------------------|-----------------------------|
|
||||
| Application entry point | `backend/app.ts` |
|
||||
| Route definitions | `backend/routes.ts` |
|
||||
| Route handlers | `backend/handlers.ts` |
|
||||
| Service layer | `backend/services.ts` |
|
||||
| Application types | `backend/types.ts` |
|
||||
| Application config | `backend/config.ts` |
|
||||
| Database migrations | `backend/migrations/` |
|
||||
| Framework library code | `backend/diachron/` |
|
||||
|
||||
### Types and naming
|
||||
|
||||
- HTTP request wrapper: `Call` (not Request)
|
||||
- HTTP response wrapper: `Result` (not Response)
|
||||
- Route definitions: arrays of `Route` objects
|
||||
- Handlers: functions that take a `Call` and return a `Result`
|
||||
|
||||
These names are intentional. Use them consistently.
|
||||
|
||||
Import framework types from `./diachron/types`:
|
||||
|
||||
```typescript
|
||||
import type { Call, Result, Route, Handler } from "./diachron/types";
|
||||
```
|
||||
|
||||
Application-specific domain types go in `backend/types.ts`.
|
||||
|
||||
### Services
|
||||
|
||||
Application services go in `backend/services.ts`. Framework services are
|
||||
accessed through the `core` object:
|
||||
|
||||
```typescript
|
||||
import { core } from "./diachron/core";
|
||||
|
||||
core.database.db // Kysely database instance
|
||||
core.logging.log // Logging
|
||||
core.misc.sleep // Utilities
|
||||
```
|
||||
|
||||
### Exports
|
||||
|
||||
When a TypeScript file exports symbols, they should be listed in
|
||||
alphabetical order.
|
||||
|
||||
## Database
|
||||
|
||||
diachron uses PostgreSQL exclusively, accessed through Kysely (type-safe
|
||||
query builder). There is no ORM.
|
||||
|
||||
- Write SQL via Kysely, not raw query strings (unless Kysely can't express
|
||||
the query)
|
||||
- Migrations live in `backend/migrations/`
|
||||
- Run `./develop codegen` after schema changes to regenerate Kysely types
|
||||
|
||||
## Key conventions
|
||||
|
||||
### No dev/prod distinction
|
||||
|
||||
There is no `NODE_ENV`. The application behaves identically everywhere.
|
||||
Do not introduce environment-based branching.
|
||||
|
||||
### Managed tooling
|
||||
|
||||
Never reference globally installed `node`, `npm`, `npx`, or `pnpm`.
|
||||
Always use `./cmd node`, `./cmd pnpm`, etc.
|
||||
|
||||
### File ownership boundary
|
||||
|
||||
```
|
||||
You may edit: backend/* (except backend/diachron/)
|
||||
Do not edit: master/*, logger/*, diachron/*, backend/diachron/*
|
||||
```
|
||||
|
||||
If a task requires framework changes, confirm with the user first.
|
||||
When framework files are modified, the changes can be extracted as a
|
||||
diff against upstream with `./diff-upstream.sh` (or `--stat` to list
|
||||
changed files only).
|
||||
|
||||
When committing framework changes, keep them in separate commits from
|
||||
application code. Each framework commit should have a clear message
|
||||
explaining what was changed and why. This makes it much easier to
|
||||
upstream the changes later.
|
||||
|
||||
### Command safety tiers
|
||||
|
||||
- `./cmd` -- Tool wrappers, always safe
|
||||
- `./mgmt` -- Production-safe operations (migrations, user management)
|
||||
- `./develop` -- Destructive operations, development only
|
||||
|
||||
Never use `./develop` commands against production data.
|
||||
|
||||
## Common tasks
|
||||
|
||||
### Add a new route
|
||||
|
||||
1. Define the route in `backend/routes.ts` as a `Route` object
|
||||
2. Implement the handler in `backend/handlers.ts`
|
||||
3. Add any needed types to `backend/types.ts`
|
||||
|
||||
### Add a database migration
|
||||
|
||||
1. Create a migration file in `backend/migrations/`
|
||||
2. Run `./develop migrate` to apply it
|
||||
3. Run `./develop codegen` to regenerate Kysely types
|
||||
|
||||
### Install a package
|
||||
|
||||
```bash
|
||||
cd backend && ../cmd pnpm add <package>
|
||||
```
|
||||
|
||||
### Run a one-off TypeScript script
|
||||
|
||||
```bash
|
||||
./develop tsx backend/path/to/script.ts
|
||||
```
|
||||
|
||||
## file-list
|
||||
|
||||
The root `file-list` file is a manifest of all files that ship with the
|
||||
framework. When you create or delete a file that is part of the project
|
||||
(not a scratch file or generated output), you must update `file-list` to
|
||||
match. Keep it sorted alphabetically.
|
||||
|
||||
Entries can have a `?` prefix (e.g. `?backend/app.ts`). These are
|
||||
**sample files** -- starter code that `bootstrap.sh` copies into a new
|
||||
project but that `upgrade.sh` will not overwrite. Once the user has the
|
||||
file, it belongs to them. On upgrade, new sample files that don't exist
|
||||
yet in the project are copied in; existing ones are left untouched.
|
||||
|
||||
Unprefixed entries are **framework-owned** and are always replaced on
|
||||
upgrade. When adding a new file to `file-list`, decide which category
|
||||
it belongs to:
|
||||
|
||||
- Framework-owned (no prefix): infrastructure scripts, framework
|
||||
library code, build tooling, config that must stay in sync.
|
||||
- Sample (`?` prefix): application starter code the user is expected
|
||||
to edit (routes, handlers, services, types, package.json, etc.).
|
||||
|
||||
## Things to avoid
|
||||
|
||||
- Do not introduce `.env` files or `dotenv` without checking with the
|
||||
team first. The configuration story is still being decided.
|
||||
- Do not introduce webpack, vite, or other bundlers. The master process
|
||||
handles building via `@vercel/ncc`.
|
||||
- Do not add express middleware directly. Use the framework's route
|
||||
processing in `backend/diachron/routing.ts`.
|
||||
- Do not use `npm` or globally installed `pnpm`. Use `./cmd pnpm`.
|
||||
- Do not add `NODE_ENV` checks or development/production branches.
|
||||
0
diachron/binaries/.gitignore
vendored
Normal file
0
diachron/binaries/.gitignore
vendored
Normal file
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user