@briefkasten/server 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,102 @@
1
+ import { AdapterRegistry, BriefkastenEvent, ConfigSource, EventStore, InputOf, KeyValueStore, Mailer, OperationId, OutputOf, Principal, Runtime, StreamEnd, StreamItem, TokenVerifier } from "briefkasten";
2
+ //#region src/stream.d.ts
3
+ interface StreamSession {
4
+ readonly items: AsyncIterable<StreamItem>;
5
+ close(): void;
6
+ /**
7
+ * Why the gateway ended the session itself (its tenant was suspended or deleted), once it has;
8
+ * a runtime closes the connection with that (a WebSocket with `WS_CLOSE[reason]`).
9
+ */
10
+ ended(): StreamEnd | undefined;
11
+ }
12
+ //#endregion
13
+ //#region src/gateway.d.ts
14
+ /**
15
+ * The gateway: one `fetch` handler for every route, on any runtime that speaks Request and
16
+ * Response. Its ports (config source, event store, key-value store, adapters, mail) are given;
17
+ * the Node runtime (`@briefkasten/server/node`) and a Cloudflare Worker pick their own.
18
+ *
19
+ * Routes:
20
+ * GET /health liveness
21
+ * POST <hook path> a provider's delivery (the mode's template, e.g. /hooks/:source)
22
+ * GET /api/v1/stream the event stream (SSE; WebSocket through `openStream`)
23
+ * GET /t/<tenant>/stream the same, where a `multi` instance's subscribers connect out
24
+ * * /api/v1/... the management API (`briefkasten/api`)
25
+ * GET /device where a device sign-in sends a person
26
+ *
27
+ * A token belongs to one tenant, so it names the tenant it acts on; an admin token names it with
28
+ * the `Briefkasten-Tenant` header. `publicFetch` serves only `/health`, the hooks and (in `multi`
29
+ * mode) the tenants' streams, for a listener that faces the internet.
30
+ */
31
+ interface GatewayDeps {
32
+ readonly config: ConfigSource;
33
+ readonly store: EventStore;
34
+ readonly kv: KeyValueStore;
35
+ readonly adapters: AdapterRegistry;
36
+ readonly mailer?: Mailer;
37
+ /** More ways to recognise a token, tried after the built-in ones. */
38
+ readonly verifiers?: readonly TokenVerifier[];
39
+ readonly runtime?: Runtime;
40
+ readonly log?: (message: string) => void;
41
+ readonly capture?: (source: string, body: Uint8Array, headers: Headers) => Promise<void>;
42
+ readonly version?: string;
43
+ }
44
+ interface HookInfo {
45
+ readonly tenant: string;
46
+ readonly project: string;
47
+ readonly source: string;
48
+ readonly adapter: string;
49
+ readonly hookUrl: string;
50
+ }
51
+ type StreamOpening = {
52
+ readonly kind: "session";
53
+ readonly session: StreamSession;
54
+ readonly protocol?: string;
55
+ } | {
56
+ readonly kind: "refused";
57
+ readonly response: Response;
58
+ };
59
+ interface Gateway {
60
+ readonly fetch: (request: Request) => Promise<Response>;
61
+ readonly publicFetch: (request: Request) => Promise<Response>;
62
+ /** Authorises a stream request (SSE or a WebSocket upgrade) and opens its session. */
63
+ openStream(request: Request): Promise<StreamOpening>;
64
+ /** Runs an operation as `principal`, the same way the HTTP API does. */
65
+ call<K extends OperationId>(id: K, input: InputOf<K>, principal: Principal, tenant?: string): Promise<OutputOf<K>>;
66
+ /** Every source's hook URL. */
67
+ hooks(): readonly HookInfo[];
68
+ /** Runs every source's start (webhook registration) and the retention timer. */
69
+ start(): Promise<void>;
70
+ /** Loads the config again. */
71
+ reload(): Promise<void>;
72
+ close(): Promise<void>;
73
+ }
74
+ declare function createGateway(deps: GatewayDeps): Promise<Gateway>;
75
+ //#endregion
76
+ //#region src/sql.d.ts
77
+ /**
78
+ * The SQL stores: events (kept for the retention window, in arrival order), their deliveries,
79
+ * the meter and the key-value state, in one SQLite database. A driver runs the statements: the
80
+ * Node runtime's over `node:sqlite` (`@briefkasten/server/node`), the Cloudflare runtime's over a
81
+ * Durable Object's SQLite.
82
+ */
83
+ /** A value a statement binds. */
84
+ type SqlValue = string | number | null;
85
+ /** A SQLite database, synchronous as both runtimes' are. */
86
+ interface SqlDriver {
87
+ /** Runs statements that bind nothing (the schema). */
88
+ script(sql: string): void;
89
+ /** The rows a query answers, as plain objects. */
90
+ all(sql: string, ...values: SqlValue[]): unknown[];
91
+ /** Runs one statement; answers how many rows it changed. */
92
+ run(sql: string, ...values: SqlValue[]): number;
93
+ }
94
+ interface SqlStores {
95
+ readonly events: EventStore;
96
+ readonly kv: KeyValueStore;
97
+ }
98
+ declare function sqlStores(driver: SqlDriver, options?: {
99
+ readonly now?: () => number;
100
+ }): SqlStores;
101
+ //#endregion
102
+ export { Gateway as a, StreamOpening as c, sqlStores as i, createGateway as l, SqlStores as n, GatewayDeps as o, SqlValue as r, HookInfo as s, SqlDriver as t, StreamSession as u };
package/package.json CHANGED
@@ -1,6 +1,68 @@
1
1
  {
2
2
  "name": "@briefkasten/server",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "The briefkasten gateway (createGateway): hooks, the management API and the event stream, with a Node runtime over node:sqlite (@briefkasten/server/node)",
5
+ "keywords": [
6
+ "briefkasten",
7
+ "gateway",
8
+ "server",
9
+ "sqlite",
10
+ "typescript",
11
+ "webhooks"
12
+ ],
13
+ "homepage": "https://briefkasten.fabrik.computer/docs",
14
+ "bugs": {
15
+ "url": "https://github.com/mateffy/fabrik/issues"
16
+ },
17
+ "license": "MIT",
18
+ "author": "Lukas Mateffy",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/mateffy/fabrik.git",
22
+ "directory": "packages/briefkasten-server"
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "README.md",
27
+ "LICENSE"
28
+ ],
29
+ "type": "module",
30
+ "sideEffects": false,
31
+ "main": "./dist/index.mjs",
32
+ "types": "./dist/index.d.mts",
33
+ "exports": {
34
+ ".": {
35
+ "types": "./dist/index.d.mts",
36
+ "import": "./dist/index.mjs",
37
+ "default": "./dist/index.mjs"
38
+ },
39
+ "./node": {
40
+ "types": "./dist/node.d.mts",
41
+ "import": "./dist/node.mjs",
42
+ "default": "./dist/node.mjs"
43
+ }
44
+ },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
+ "dependencies": {
49
+ "zod": "^4.4.3",
50
+ "briefkasten": "0.1.0"
51
+ },
52
+ "devDependencies": {
53
+ "@types/node": "^22.0.0",
54
+ "tsdown": "^0.22.3",
55
+ "typescript": "^5.8.0",
56
+ "vitest": "^4.0.0",
57
+ "@briefkasten/adapter-github": "0.1.0",
58
+ "@briefkasten/adapter-generic": "0.1.0"
59
+ },
60
+ "engines": {
61
+ "node": ">=22.13"
62
+ },
63
+ "scripts": {
64
+ "build": "tsdown",
65
+ "typecheck": "tsc --noEmit",
66
+ "test": "vitest run"
67
+ }
6
68
  }