@weft-framework/server 0.0.0-stage → 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/account.d.ts +124 -0
- package/dist/account.js +344 -0
- package/dist/action.d.ts +43 -0
- package/dist/action.js +135 -0
- package/dist/admin.d.ts +99 -0
- package/dist/admin.js +431 -0
- package/dist/api.d.ts +21 -0
- package/dist/api.js +72 -0
- package/dist/app.d.ts +202 -0
- package/dist/app.js +3692 -0
- package/dist/assets.d.ts +4 -0
- package/dist/assets.js +91 -0
- package/dist/auth.d.ts +150 -0
- package/dist/auth.js +215 -0
- package/dist/backup.d.ts +46 -0
- package/dist/backup.js +345 -0
- package/dist/build.d.ts +33 -0
- package/dist/build.js +274 -0
- package/dist/bun.d.ts +30 -0
- package/dist/bun.js +53 -0
- package/dist/calendar.d.ts +161 -0
- package/dist/calendar.js +690 -0
- package/dist/compiler.d.ts +13 -0
- package/dist/compiler.js +58 -0
- package/dist/compress.d.ts +6 -0
- package/dist/compress.js +78 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.js +317 -0
- package/dist/context.d.ts +9 -0
- package/dist/context.js +1 -0
- package/dist/contract.d.ts +18 -0
- package/dist/contract.js +171 -0
- package/dist/datamap.d.ts +24 -0
- package/dist/datamap.js +365 -0
- package/dist/db-backup.d.ts +27 -0
- package/dist/db-backup.js +115 -0
- package/dist/dev-events.d.ts +14 -0
- package/dist/dev-events.js +135 -0
- package/dist/dev.d.ts +1 -0
- package/dist/dev.js +240 -0
- package/dist/devtools-client.d.ts +1 -0
- package/dist/devtools-client.js +362 -0
- package/dist/devtools.d.ts +87 -0
- package/dist/devtools.js +68 -0
- package/dist/edge.d.ts +3 -0
- package/dist/edge.js +7 -0
- package/dist/email-signin.d.ts +92 -0
- package/dist/email-signin.js +259 -0
- package/dist/env.d.ts +1 -0
- package/dist/env.js +15 -0
- package/dist/explain.d.ts +12 -0
- package/dist/explain.js +217 -0
- package/dist/files.d.ts +6 -0
- package/dist/files.js +170 -0
- package/dist/grants.d.ts +69 -0
- package/dist/grants.js +206 -0
- package/dist/image/bitmap.d.ts +11 -0
- package/dist/image/bitmap.js +172 -0
- package/dist/image/jpeg.d.ts +10 -0
- package/dist/image/jpeg.js +697 -0
- package/dist/image/png.d.ts +8 -0
- package/dist/image/png.js +282 -0
- package/dist/image/transform.d.ts +20 -0
- package/dist/image/transform.js +52 -0
- package/dist/image/worker.d.ts +1 -0
- package/dist/image/worker.js +11 -0
- package/dist/images.d.ts +39 -0
- package/dist/images.js +283 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +31 -0
- package/dist/inspect-hub.d.ts +42 -0
- package/dist/inspect-hub.js +14 -0
- package/dist/inspector.d.ts +51 -0
- package/dist/inspector.js +193 -0
- package/dist/job-admin.d.ts +55 -0
- package/dist/job-admin.js +89 -0
- package/dist/jobs.d.ts +222 -0
- package/dist/jobs.js +789 -0
- package/dist/live.d.ts +72 -0
- package/dist/live.js +481 -0
- package/dist/load-config.d.ts +2 -0
- package/dist/load-config.js +41 -0
- package/dist/log.d.ts +20 -0
- package/dist/log.js +143 -0
- package/dist/mail.d.ts +189 -0
- package/dist/mail.js +999 -0
- package/dist/media.d.ts +129 -0
- package/dist/media.js +470 -0
- package/dist/meta.d.ts +54 -0
- package/dist/meta.js +146 -0
- package/dist/middleware.d.ts +20 -0
- package/dist/middleware.js +101 -0
- package/dist/migrate.d.ts +12 -0
- package/dist/migrate.js +88 -0
- package/dist/node.d.ts +26 -0
- package/dist/node.js +96 -0
- package/dist/notice.d.ts +21 -0
- package/dist/notice.js +48 -0
- package/dist/npm.d.ts +16 -0
- package/dist/npm.js +347 -0
- package/dist/oauth.d.ts +76 -0
- package/dist/oauth.js +408 -0
- package/dist/otel.d.ts +57 -0
- package/dist/otel.js +293 -0
- package/dist/passkey.d.ts +99 -0
- package/dist/passkey.js +572 -0
- package/dist/password.d.ts +8 -0
- package/dist/password.js +65 -0
- package/dist/pdf.d.ts +46 -0
- package/dist/pdf.js +332 -0
- package/dist/personal-data.d.ts +22 -0
- package/dist/personal-data.js +148 -0
- package/dist/pointers.d.ts +9 -0
- package/dist/pointers.js +148 -0
- package/dist/postcss.d.ts +3 -0
- package/dist/postcss.js +89 -0
- package/dist/presence.d.ts +27 -0
- package/dist/presence.js +161 -0
- package/dist/preview.d.ts +25 -0
- package/dist/preview.js +353 -0
- package/dist/production.d.ts +16 -0
- package/dist/production.js +119 -0
- package/dist/project.d.ts +23 -0
- package/dist/project.js +536 -0
- package/dist/regions.d.ts +47 -0
- package/dist/regions.js +511 -0
- package/dist/replay.d.ts +44 -0
- package/dist/replay.js +259 -0
- package/dist/router.d.ts +29 -0
- package/dist/router.js +146 -0
- package/dist/rules-diff.d.ts +18 -0
- package/dist/rules-diff.js +165 -0
- package/dist/rules-test.d.ts +17 -0
- package/dist/rules-test.js +108 -0
- package/dist/saml.d.ts +160 -0
- package/dist/saml.js +959 -0
- package/dist/seal.d.ts +35 -0
- package/dist/seal.js +107 -0
- package/dist/secret.d.ts +14 -0
- package/dist/secret.js +110 -0
- package/dist/security.d.ts +53 -0
- package/dist/security.js +250 -0
- package/dist/seed.d.ts +16 -0
- package/dist/seed.js +94 -0
- package/dist/seo.d.ts +42 -0
- package/dist/seo.js +175 -0
- package/dist/settings.d.ts +79 -0
- package/dist/settings.js +212 -0
- package/dist/shared.d.ts +15 -0
- package/dist/shared.js +283 -0
- package/dist/shell.d.ts +12 -0
- package/dist/shell.js +118 -0
- package/dist/storage.d.ts +13 -0
- package/dist/storage.js +55 -0
- package/dist/support.d.ts +83 -0
- package/dist/support.js +236 -0
- package/dist/sync.d.ts +35 -0
- package/dist/sync.js +167 -0
- package/dist/tailwind.d.ts +14 -0
- package/dist/tailwind.js +117 -0
- package/dist/two-factor.d.ts +103 -0
- package/dist/two-factor.js +435 -0
- package/dist/uploads.d.ts +185 -0
- package/dist/uploads.js +991 -0
- package/dist/webhooks.d.ts +169 -0
- package/dist/webhooks.js +758 -0
- package/dist/webpush.d.ts +97 -0
- package/dist/webpush.js +372 -0
- package/dist/wizard.d.ts +141 -0
- package/dist/wizard.js +410 -0
- package/dist/workflows.d.ts +134 -0
- package/dist/workflows.js +511 -0
- package/package.json +186 -4
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Model } from "@weft-framework/data";
|
|
2
|
+
import type { SessionStore } from "./auth.js";
|
|
3
|
+
type Revealable = string | {
|
|
4
|
+
reveal(): string;
|
|
5
|
+
};
|
|
6
|
+
export declare class RegionWriteError extends Error {
|
|
7
|
+
constructor(model: string);
|
|
8
|
+
}
|
|
9
|
+
export declare const REGION_PATH = "/_weft/region/";
|
|
10
|
+
export declare function encodeRows(value: unknown): string;
|
|
11
|
+
export declare function decodeRows<T>(text: string): T;
|
|
12
|
+
export interface RegionsOptions {
|
|
13
|
+
region: string;
|
|
14
|
+
primary: string;
|
|
15
|
+
primaryUrl: string;
|
|
16
|
+
secret: Revealable;
|
|
17
|
+
models: readonly Model<any, any>[];
|
|
18
|
+
forward?: "proxy" | "fly-replay";
|
|
19
|
+
logSize?: number;
|
|
20
|
+
maxLagMs?: number;
|
|
21
|
+
sessionCacheMs?: number;
|
|
22
|
+
onError?: (err: unknown) => void;
|
|
23
|
+
fetch?: typeof fetch;
|
|
24
|
+
now?: () => number;
|
|
25
|
+
}
|
|
26
|
+
export declare class Regions {
|
|
27
|
+
#private;
|
|
28
|
+
readonly region: string;
|
|
29
|
+
readonly primary: string;
|
|
30
|
+
readonly isPrimary: boolean;
|
|
31
|
+
constructor(o: RegionsOptions);
|
|
32
|
+
handle(req: Request): Promise<Response | null>;
|
|
33
|
+
sessions(store: SessionStore): SessionStore;
|
|
34
|
+
get seq(): string;
|
|
35
|
+
start(): Promise<void>;
|
|
36
|
+
get ready(): boolean;
|
|
37
|
+
stop(): void;
|
|
38
|
+
route(req: Request, o: {
|
|
39
|
+
routeOf(path: string): object | null;
|
|
40
|
+
holdsPage(lid: string | null): boolean;
|
|
41
|
+
}): Promise<Request | Response | null>;
|
|
42
|
+
remember(route: object): void;
|
|
43
|
+
forward(req: Request): Promise<Response>;
|
|
44
|
+
stamp(req: Request, res: Response): Response;
|
|
45
|
+
}
|
|
46
|
+
export declare function defineRegions(o: RegionsOptions): Regions;
|
|
47
|
+
export {};
|
package/dist/regions.js
ADDED
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
+
const reveal = (v) => (typeof v === "string" ? v : v.reveal());
|
|
3
|
+
export class RegionWriteError extends Error {
|
|
4
|
+
constructor(model) {
|
|
5
|
+
super(`${model}: this region's database is a read-only replica; writes go to the primary region`);
|
|
6
|
+
this.name = "RegionWriteError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export const REGION_PATH = "/_weft/region/";
|
|
10
|
+
const AUTH = "x-weft-region-auth";
|
|
11
|
+
const URL_HEADER = "x-weft-region-url";
|
|
12
|
+
const SEQ_HEADER = "x-weft-region-seq";
|
|
13
|
+
const SEQ_COOKIE = "weft-region-seq";
|
|
14
|
+
const HOP = new Set(["connection", "keep-alive", "proxy-connection", "transfer-encoding", "te", "trailer", "upgrade", "host", "content-length"]);
|
|
15
|
+
const NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
|
16
|
+
const DATE = "$weft:date";
|
|
17
|
+
const BIG = "$weft:bigint";
|
|
18
|
+
export function encodeRows(value) {
|
|
19
|
+
return JSON.stringify(value, function (key, v) {
|
|
20
|
+
const raw = this[key];
|
|
21
|
+
if (raw instanceof Date)
|
|
22
|
+
return { [DATE]: raw.toISOString() };
|
|
23
|
+
if (typeof raw === "bigint")
|
|
24
|
+
return { [BIG]: raw.toString() };
|
|
25
|
+
return v;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
export function decodeRows(text) {
|
|
29
|
+
return JSON.parse(text, (_k, v) => {
|
|
30
|
+
if (v !== null && typeof v === "object" && !Array.isArray(v) && Object.keys(v).length === 1) {
|
|
31
|
+
if (typeof v[DATE] === "string")
|
|
32
|
+
return new Date(v[DATE]);
|
|
33
|
+
if (typeof v[BIG] === "string" && /^-?\d+$/.test(v[BIG]))
|
|
34
|
+
return BigInt(v[BIG]);
|
|
35
|
+
}
|
|
36
|
+
return v;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function mac(secret, region, ts, method, path) {
|
|
40
|
+
return createHmac("sha256", secret).update(`weft-region\n${region}\n${ts}\n${method}\n${path}`).digest("hex");
|
|
41
|
+
}
|
|
42
|
+
function sign(secret, region, method, url, now) {
|
|
43
|
+
const ts = Math.floor(now / 1000);
|
|
44
|
+
return `${region}.${ts}.${mac(secret, region, ts, method.toUpperCase(), url.pathname + url.search)}`;
|
|
45
|
+
}
|
|
46
|
+
function verify(secret, req, now) {
|
|
47
|
+
const h = req.headers.get(AUTH);
|
|
48
|
+
const m = h ? /^([a-z][a-z0-9-]{0,31})\.(\d{1,12})\.([0-9a-f]{64})$/.exec(h) : null;
|
|
49
|
+
if (!m)
|
|
50
|
+
return null;
|
|
51
|
+
const ts = Number(m[2]);
|
|
52
|
+
if (Math.abs(now / 1000 - ts) > 60)
|
|
53
|
+
return null;
|
|
54
|
+
const url = new URL(req.url);
|
|
55
|
+
const want = Buffer.from(mac(secret, m[1], ts, req.method.toUpperCase(), url.pathname + url.search));
|
|
56
|
+
return timingSafeEqual(Buffer.from(m[3]), want) ? m[1] : null;
|
|
57
|
+
}
|
|
58
|
+
export class Regions {
|
|
59
|
+
region;
|
|
60
|
+
primary;
|
|
61
|
+
isPrimary;
|
|
62
|
+
#secret;
|
|
63
|
+
#primaryUrl;
|
|
64
|
+
#models;
|
|
65
|
+
#o;
|
|
66
|
+
#epoch = randomBytes(9).toString("base64url");
|
|
67
|
+
#seq = 0;
|
|
68
|
+
#log = [];
|
|
69
|
+
#waiters = new Set();
|
|
70
|
+
#unsubscribe = [];
|
|
71
|
+
#remoteEpoch = "";
|
|
72
|
+
#applied = 0;
|
|
73
|
+
#applying = false;
|
|
74
|
+
#ready = false;
|
|
75
|
+
#stopped = false;
|
|
76
|
+
#appliedWaiters = new Set();
|
|
77
|
+
#toPrimary = new WeakSet();
|
|
78
|
+
constructor(o) {
|
|
79
|
+
const fail = (m) => {
|
|
80
|
+
throw new Error(`defineRegions: ${m}`);
|
|
81
|
+
};
|
|
82
|
+
if (!o || typeof o !== "object")
|
|
83
|
+
fail("needs options");
|
|
84
|
+
for (const k of ["region", "primary"])
|
|
85
|
+
if (typeof o[k] !== "string" || !NAME.test(o[k]))
|
|
86
|
+
fail(`${k} is a region name like \`fra\` or \`us-east\``);
|
|
87
|
+
let url;
|
|
88
|
+
try {
|
|
89
|
+
url = new URL(o.primaryUrl);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
url = fail("primaryUrl is the primary's URL");
|
|
93
|
+
}
|
|
94
|
+
if (url.protocol !== "https:" && url.protocol !== "http:")
|
|
95
|
+
fail("primaryUrl is an http(s) URL");
|
|
96
|
+
const secret = reveal(o.secret);
|
|
97
|
+
if (typeof secret !== "string" || secret.length < 32)
|
|
98
|
+
fail("secret is at least 32 characters");
|
|
99
|
+
if (!Array.isArray(o.models) || o.models.length === 0)
|
|
100
|
+
fail("models lists the replicated models");
|
|
101
|
+
this.region = o.region;
|
|
102
|
+
this.primary = o.primary;
|
|
103
|
+
this.isPrimary = o.region === o.primary;
|
|
104
|
+
this.#secret = secret;
|
|
105
|
+
this.#primaryUrl = new URL(url.origin);
|
|
106
|
+
this.#models = new Map(o.models.map((m) => [m.name, m]));
|
|
107
|
+
this.#o = {
|
|
108
|
+
forward: o.forward ?? "proxy",
|
|
109
|
+
logSize: o.logSize ?? 10_000,
|
|
110
|
+
maxLagMs: o.maxLagMs ?? 2000,
|
|
111
|
+
sessionCacheMs: o.sessionCacheMs ?? 10_000,
|
|
112
|
+
onError: o.onError ?? ((err) => console.error("[weft] regions:", err)),
|
|
113
|
+
fetch: o.fetch ?? fetch,
|
|
114
|
+
now: o.now ?? Date.now,
|
|
115
|
+
};
|
|
116
|
+
if (this.isPrimary)
|
|
117
|
+
this.#startLog();
|
|
118
|
+
else
|
|
119
|
+
this.#readOnly();
|
|
120
|
+
}
|
|
121
|
+
#startLog() {
|
|
122
|
+
const feeds = new Set([...this.#models.values()].map((m) => m.feed));
|
|
123
|
+
for (const feed of feeds) {
|
|
124
|
+
this.#unsubscribe.push(feed.subscribe((c) => {
|
|
125
|
+
const m = this.#models.get(c.model);
|
|
126
|
+
if (!m || m.feed !== feed)
|
|
127
|
+
return;
|
|
128
|
+
const e = { seq: ++this.#seq, model: c.model, op: c.op, key: c.key };
|
|
129
|
+
if (c.after !== undefined && c.op !== "delete")
|
|
130
|
+
e.row = c.after;
|
|
131
|
+
if (c.regions && c.regions.length > 0)
|
|
132
|
+
e.regions = c.regions;
|
|
133
|
+
if (c.redacted)
|
|
134
|
+
e.key = undefined;
|
|
135
|
+
this.#log.push(e);
|
|
136
|
+
if (this.#log.length > this.#o.logSize)
|
|
137
|
+
this.#log.splice(0, this.#log.length - this.#o.logSize);
|
|
138
|
+
for (const w of [...this.#waiters])
|
|
139
|
+
w();
|
|
140
|
+
}));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
static #reaches(regions, to) {
|
|
144
|
+
return !regions || regions.length === 0 || regions.every((r) => r === to);
|
|
145
|
+
}
|
|
146
|
+
async handle(req) {
|
|
147
|
+
const url = new URL(req.url);
|
|
148
|
+
if (!url.pathname.startsWith(REGION_PATH))
|
|
149
|
+
return null;
|
|
150
|
+
if (!this.isPrimary)
|
|
151
|
+
return new Response("Not found", { status: 404 });
|
|
152
|
+
const from = verify(this.#secret, req, this.#o.now());
|
|
153
|
+
if (from === null)
|
|
154
|
+
return new Response("Forbidden", { status: 403 });
|
|
155
|
+
const what = url.pathname.slice(REGION_PATH.length);
|
|
156
|
+
const json = (v, status = 200) => new Response(encodeRows(v), { status, headers: { "content-type": "application/json", "cache-control": "no-store" } });
|
|
157
|
+
if (what === "log" && req.method === "GET") {
|
|
158
|
+
const epoch = url.searchParams.get("epoch") ?? "";
|
|
159
|
+
const since = Number(url.searchParams.get("since") ?? "0");
|
|
160
|
+
const wait = Math.min(25_000, Math.max(0, Number(url.searchParams.get("wait") ?? "0") || 0));
|
|
161
|
+
const oldest = this.#log[0]?.seq ?? this.#seq + 1;
|
|
162
|
+
if (epoch !== this.#epoch || !Number.isSafeInteger(since) || since < oldest - 1 || since > this.#seq) {
|
|
163
|
+
return json({ epoch: this.#epoch, seq: this.#seq, reset: true, entries: [] });
|
|
164
|
+
}
|
|
165
|
+
if (since === this.#seq && wait > 0) {
|
|
166
|
+
await new Promise((done) => {
|
|
167
|
+
const t = setTimeout(finish, wait);
|
|
168
|
+
function finish() {
|
|
169
|
+
clearTimeout(t);
|
|
170
|
+
waiters.delete(finish);
|
|
171
|
+
done();
|
|
172
|
+
}
|
|
173
|
+
const waiters = this.#waiters;
|
|
174
|
+
waiters.add(finish);
|
|
175
|
+
req.signal?.addEventListener("abort", finish);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
const entries = this.#log
|
|
179
|
+
.filter((e) => e.seq > since)
|
|
180
|
+
.slice(0, 1000)
|
|
181
|
+
.map((e) => (Regions.#reaches(e.regions, from) ? e : { seq: e.seq, model: e.model, op: e.op, key: undefined }));
|
|
182
|
+
return json({ epoch: this.#epoch, seq: entries.at(-1)?.seq ?? since, entries });
|
|
183
|
+
}
|
|
184
|
+
if (what === "snapshot" && req.method === "GET") {
|
|
185
|
+
const models = {};
|
|
186
|
+
for (const [name, m] of this.#models) {
|
|
187
|
+
models[name] = m.driver.select([]).filter((row) => Regions.#reaches(m.regionsOf(row, undefined), from));
|
|
188
|
+
}
|
|
189
|
+
return json({ epoch: this.#epoch, seq: this.#seq, models });
|
|
190
|
+
}
|
|
191
|
+
if (what === "session")
|
|
192
|
+
return this.#sessionEndpoint(req, url);
|
|
193
|
+
return new Response("Not found", { status: 404 });
|
|
194
|
+
}
|
|
195
|
+
#sessionStore = null;
|
|
196
|
+
async #sessionEndpoint(req, url) {
|
|
197
|
+
const store = this.#sessionStore;
|
|
198
|
+
if (!store)
|
|
199
|
+
return new Response("No session store", { status: 404 });
|
|
200
|
+
const sid = url.searchParams.get("sid") ?? "";
|
|
201
|
+
const user = url.searchParams.get("user");
|
|
202
|
+
const json = (v) => new Response(encodeRows(v), { headers: { "content-type": "application/json", "cache-control": "no-store" } });
|
|
203
|
+
if (req.method === "GET" && user !== null)
|
|
204
|
+
return json(store.forUser ? await store.forUser(user) : []);
|
|
205
|
+
if (sid.length < 16 || sid.length > 256)
|
|
206
|
+
return new Response("Bad request", { status: 400 });
|
|
207
|
+
if (req.method === "GET")
|
|
208
|
+
return json((await store.get(sid)) ?? null);
|
|
209
|
+
if (req.method === "PUT") {
|
|
210
|
+
const text = await req.text();
|
|
211
|
+
if (text.length > 64_000)
|
|
212
|
+
return new Response("Too large", { status: 413 });
|
|
213
|
+
await store.set(sid, decodeRows(text));
|
|
214
|
+
return json(true);
|
|
215
|
+
}
|
|
216
|
+
if (req.method === "DELETE") {
|
|
217
|
+
await store.delete(sid);
|
|
218
|
+
return json(true);
|
|
219
|
+
}
|
|
220
|
+
return new Response("Method not allowed", { status: 405 });
|
|
221
|
+
}
|
|
222
|
+
sessions(store) {
|
|
223
|
+
if (this.isPrimary) {
|
|
224
|
+
this.#sessionStore = store;
|
|
225
|
+
return store;
|
|
226
|
+
}
|
|
227
|
+
const cache = new Map();
|
|
228
|
+
const call = async (method, query, body) => {
|
|
229
|
+
const url = new URL(`${REGION_PATH}session?${query}`, this.#primaryUrl);
|
|
230
|
+
const res = await this.#o.fetch(url, {
|
|
231
|
+
method,
|
|
232
|
+
headers: { [AUTH]: sign(this.#secret, this.region, method, url, this.#o.now()), "content-type": "application/json" },
|
|
233
|
+
...(body !== undefined ? { body } : {}),
|
|
234
|
+
signal: AbortSignal.timeout(10_000),
|
|
235
|
+
});
|
|
236
|
+
if (!res.ok)
|
|
237
|
+
throw new Error(`regions: the primary answered ${res.status} for a session`);
|
|
238
|
+
return decodeRows(await res.text());
|
|
239
|
+
};
|
|
240
|
+
return {
|
|
241
|
+
get: async (sid) => {
|
|
242
|
+
const hit = cache.get(sid);
|
|
243
|
+
if (hit && this.#o.now() - hit.at < this.#o.sessionCacheMs)
|
|
244
|
+
return hit.record;
|
|
245
|
+
const record = (await call("GET", `sid=${encodeURIComponent(sid)}`));
|
|
246
|
+
cache.set(sid, { at: this.#o.now(), record });
|
|
247
|
+
if (cache.size > 10_000)
|
|
248
|
+
cache.delete(cache.keys().next().value);
|
|
249
|
+
return record;
|
|
250
|
+
},
|
|
251
|
+
set: async (sid, record) => {
|
|
252
|
+
cache.set(sid, { at: this.#o.now(), record });
|
|
253
|
+
await call("PUT", `sid=${encodeURIComponent(sid)}`, encodeRows(record));
|
|
254
|
+
},
|
|
255
|
+
delete: async (sid) => {
|
|
256
|
+
cache.delete(sid);
|
|
257
|
+
await call("DELETE", `sid=${encodeURIComponent(sid)}`);
|
|
258
|
+
},
|
|
259
|
+
forUser: async (user) => (await call("GET", `user=${encodeURIComponent(user)}`)),
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
get seq() {
|
|
263
|
+
return `${this.#epoch}.${this.#seq}`;
|
|
264
|
+
}
|
|
265
|
+
#readOnly() {
|
|
266
|
+
for (const m of this.#models.values()) {
|
|
267
|
+
const d = m.driver;
|
|
268
|
+
for (const op of ["insert", "update", "delete"]) {
|
|
269
|
+
const real = d[op].bind(d);
|
|
270
|
+
d[op] = (...args) => {
|
|
271
|
+
if (!this.#applying)
|
|
272
|
+
throw new RegionWriteError(m.name);
|
|
273
|
+
return real(...args);
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
async start() {
|
|
279
|
+
if (this.isPrimary)
|
|
280
|
+
return;
|
|
281
|
+
await this.#snapshot();
|
|
282
|
+
void this.#follow();
|
|
283
|
+
}
|
|
284
|
+
get ready() {
|
|
285
|
+
return this.isPrimary || this.#ready;
|
|
286
|
+
}
|
|
287
|
+
stop() {
|
|
288
|
+
this.#stopped = true;
|
|
289
|
+
for (const u of this.#unsubscribe)
|
|
290
|
+
u();
|
|
291
|
+
for (const w of [...this.#waiters])
|
|
292
|
+
w();
|
|
293
|
+
}
|
|
294
|
+
async #get(path, signal) {
|
|
295
|
+
const url = new URL(path, this.#primaryUrl);
|
|
296
|
+
const res = await this.#o.fetch(url, { headers: { [AUTH]: sign(this.#secret, this.region, "GET", url, this.#o.now()) }, ...(signal ? { signal } : {}) });
|
|
297
|
+
if (!res.ok)
|
|
298
|
+
throw new Error(`regions: the primary answered ${res.status} for ${url.pathname}`);
|
|
299
|
+
return res.text();
|
|
300
|
+
}
|
|
301
|
+
async #snapshot() {
|
|
302
|
+
const snap = decodeRows(await this.#get(`${REGION_PATH}snapshot`, AbortSignal.timeout(60_000)));
|
|
303
|
+
this.#applying = true;
|
|
304
|
+
try {
|
|
305
|
+
for (const [name, m] of this.#models) {
|
|
306
|
+
const rows = snap.models[name] ?? [];
|
|
307
|
+
const d = m.driver;
|
|
308
|
+
for (const old of d.select([]))
|
|
309
|
+
d.delete(old[m.key]);
|
|
310
|
+
for (const row of rows)
|
|
311
|
+
d.insert(row);
|
|
312
|
+
m.publishAll();
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
finally {
|
|
316
|
+
this.#applying = false;
|
|
317
|
+
}
|
|
318
|
+
this.#remoteEpoch = snap.epoch;
|
|
319
|
+
this.#setApplied(snap.seq);
|
|
320
|
+
this.#ready = true;
|
|
321
|
+
}
|
|
322
|
+
#setApplied(seq) {
|
|
323
|
+
this.#applied = seq;
|
|
324
|
+
for (const w of [...this.#appliedWaiters])
|
|
325
|
+
w();
|
|
326
|
+
}
|
|
327
|
+
#apply(e) {
|
|
328
|
+
const m = this.#models.get(e.model);
|
|
329
|
+
if (!m)
|
|
330
|
+
return;
|
|
331
|
+
if (e.key === undefined) {
|
|
332
|
+
m.publishAll();
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const d = m.driver;
|
|
336
|
+
const before = d.select([], e.key).find((r) => r[m.key] === e.key || String(r[m.key]) === String(e.key));
|
|
337
|
+
this.#applying = true;
|
|
338
|
+
try {
|
|
339
|
+
if (e.op === "delete" || e.row === undefined) {
|
|
340
|
+
if (before !== undefined)
|
|
341
|
+
d.delete(e.key);
|
|
342
|
+
}
|
|
343
|
+
else if (before !== undefined)
|
|
344
|
+
d.update(e.key, e.row);
|
|
345
|
+
else
|
|
346
|
+
d.insert(e.row);
|
|
347
|
+
}
|
|
348
|
+
finally {
|
|
349
|
+
this.#applying = false;
|
|
350
|
+
}
|
|
351
|
+
m.publish(e.op === "delete" ? "delete" : before === undefined ? "create" : "update", e.key, before, e.op === "delete" ? undefined : e.row);
|
|
352
|
+
}
|
|
353
|
+
async #follow() {
|
|
354
|
+
let backoff = 500;
|
|
355
|
+
while (!this.#stopped) {
|
|
356
|
+
try {
|
|
357
|
+
const reply = decodeRows(await this.#get(`${REGION_PATH}log?epoch=${encodeURIComponent(this.#remoteEpoch)}&since=${this.#applied}&wait=20000`, AbortSignal.timeout(30_000)));
|
|
358
|
+
if (reply.reset) {
|
|
359
|
+
await this.#snapshot();
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
for (const e of reply.entries)
|
|
363
|
+
this.#apply(e);
|
|
364
|
+
this.#setApplied(reply.seq);
|
|
365
|
+
}
|
|
366
|
+
backoff = 500;
|
|
367
|
+
}
|
|
368
|
+
catch (err) {
|
|
369
|
+
if (this.#stopped)
|
|
370
|
+
return;
|
|
371
|
+
this.#o.onError(err);
|
|
372
|
+
await new Promise((r) => setTimeout(r, backoff));
|
|
373
|
+
backoff = Math.min(backoff * 2, 15_000);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
async #caughtUp(token, ms) {
|
|
378
|
+
if (!token)
|
|
379
|
+
return true;
|
|
380
|
+
const m = /^([A-Za-z0-9_-]{1,32})\.(\d{1,15})$/.exec(token);
|
|
381
|
+
if (!m)
|
|
382
|
+
return true;
|
|
383
|
+
const seq = Number(m[2]);
|
|
384
|
+
const ok = () => this.#ready && (m[1] !== this.#remoteEpoch ? false : this.#applied >= seq);
|
|
385
|
+
if (ok())
|
|
386
|
+
return true;
|
|
387
|
+
if (m[1] !== this.#remoteEpoch && this.#ready)
|
|
388
|
+
return false;
|
|
389
|
+
return new Promise((done) => {
|
|
390
|
+
const t = setTimeout(() => {
|
|
391
|
+
this.#appliedWaiters.delete(check);
|
|
392
|
+
done(ok());
|
|
393
|
+
}, ms);
|
|
394
|
+
const check = () => {
|
|
395
|
+
if (!ok())
|
|
396
|
+
return;
|
|
397
|
+
clearTimeout(t);
|
|
398
|
+
this.#appliedWaiters.delete(check);
|
|
399
|
+
done(true);
|
|
400
|
+
};
|
|
401
|
+
this.#appliedWaiters.add(check);
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
async route(req, o) {
|
|
405
|
+
const url = new URL(req.url);
|
|
406
|
+
if (this.isPrimary) {
|
|
407
|
+
const original = req.headers.get(URL_HEADER);
|
|
408
|
+
if (original === null)
|
|
409
|
+
return req;
|
|
410
|
+
if (verify(this.#secret, req, this.#o.now()) === null)
|
|
411
|
+
return new Response("Forbidden", { status: 403 });
|
|
412
|
+
const headers = new Headers(req.headers);
|
|
413
|
+
headers.delete(URL_HEADER);
|
|
414
|
+
headers.delete(AUTH);
|
|
415
|
+
return new Request(original, {
|
|
416
|
+
method: req.method,
|
|
417
|
+
headers,
|
|
418
|
+
...(req.method === "GET" || req.method === "HEAD" ? {} : { body: req.body, duplex: "half" }),
|
|
419
|
+
signal: req.signal,
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
if (url.pathname.startsWith(REGION_PATH))
|
|
423
|
+
return new Response("Not found", { status: 404 });
|
|
424
|
+
if (url.pathname === "/_weft/health" || url.pathname === "/_weft/ready")
|
|
425
|
+
return null;
|
|
426
|
+
const reading = req.method === "GET" || req.method === "HEAD";
|
|
427
|
+
if (url.pathname.startsWith("/_weft/live")) {
|
|
428
|
+
return o.holdsPage(url.searchParams.get("lid")) ? null : this.forward(req);
|
|
429
|
+
}
|
|
430
|
+
if (!reading || url.pathname.startsWith("/_weft/auth") || url.pathname.startsWith("/_weft/uploads"))
|
|
431
|
+
return this.forward(req);
|
|
432
|
+
const route = o.routeOf(url.pathname);
|
|
433
|
+
if (route !== null && this.#toPrimary.has(route))
|
|
434
|
+
return this.forward(req);
|
|
435
|
+
if (!this.#ready)
|
|
436
|
+
return this.forward(req);
|
|
437
|
+
if (!(await this.#caughtUp(cookie(req, SEQ_COOKIE), this.#o.maxLagMs)))
|
|
438
|
+
return this.forward(req);
|
|
439
|
+
return null;
|
|
440
|
+
}
|
|
441
|
+
remember(route) {
|
|
442
|
+
this.#toPrimary.add(route);
|
|
443
|
+
}
|
|
444
|
+
async forward(req) {
|
|
445
|
+
if (this.#o.forward === "fly-replay") {
|
|
446
|
+
return new Response(null, { status: 409, headers: { "fly-replay": `region=${this.primary}`, "cache-control": "no-store" } });
|
|
447
|
+
}
|
|
448
|
+
const url = new URL(req.url);
|
|
449
|
+
const target = new URL(url.pathname + url.search, this.#primaryUrl);
|
|
450
|
+
const headers = new Headers();
|
|
451
|
+
req.headers.forEach((v, k) => {
|
|
452
|
+
if (!HOP.has(k) && !k.startsWith("x-weft-region"))
|
|
453
|
+
headers.set(k, v);
|
|
454
|
+
});
|
|
455
|
+
headers.set(URL_HEADER, req.url);
|
|
456
|
+
headers.set(AUTH, sign(this.#secret, this.region, req.method, target, this.#o.now()));
|
|
457
|
+
let res;
|
|
458
|
+
try {
|
|
459
|
+
res = await this.#o.fetch(target, {
|
|
460
|
+
method: req.method,
|
|
461
|
+
headers,
|
|
462
|
+
redirect: "manual",
|
|
463
|
+
...(req.method === "GET" || req.method === "HEAD" ? {} : { body: req.body, duplex: "half" }),
|
|
464
|
+
signal: req.signal,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
catch (err) {
|
|
468
|
+
this.#o.onError(err);
|
|
469
|
+
return new Response("The primary region cannot be reached", { status: 503, headers: { "retry-after": "5" } });
|
|
470
|
+
}
|
|
471
|
+
const out = new Headers();
|
|
472
|
+
res.headers.forEach((v, k) => {
|
|
473
|
+
if (!HOP.has(k) && k !== "set-cookie" && k !== SEQ_HEADER && k !== "content-encoding")
|
|
474
|
+
out.set(k, v);
|
|
475
|
+
});
|
|
476
|
+
for (const c of res.headers.getSetCookie())
|
|
477
|
+
out.append("set-cookie", c);
|
|
478
|
+
const seq = res.headers.get(SEQ_HEADER);
|
|
479
|
+
if (seq && /^[A-Za-z0-9_-]{1,32}\.\d{1,15}$/.test(seq) && !(req.method === "GET" || req.method === "HEAD")) {
|
|
480
|
+
out.append("set-cookie", `${SEQ_COOKIE}=${seq}; Path=/; Max-Age=60; HttpOnly; SameSite=Lax${url.protocol === "https:" ? "; Secure" : ""}`);
|
|
481
|
+
}
|
|
482
|
+
return new Response(res.body, { status: res.status, statusText: res.statusText, headers: out });
|
|
483
|
+
}
|
|
484
|
+
stamp(req, res) {
|
|
485
|
+
if (!this.isPrimary || req.method === "GET" || req.method === "HEAD")
|
|
486
|
+
return res;
|
|
487
|
+
try {
|
|
488
|
+
res.headers.set(SEQ_HEADER, this.seq);
|
|
489
|
+
return res;
|
|
490
|
+
}
|
|
491
|
+
catch {
|
|
492
|
+
const h = new Headers(res.headers);
|
|
493
|
+
h.set(SEQ_HEADER, this.seq);
|
|
494
|
+
return new Response(res.body, { status: res.status, statusText: res.statusText, headers: h });
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
function cookie(req, name) {
|
|
499
|
+
const h = req.headers.get("cookie");
|
|
500
|
+
if (!h)
|
|
501
|
+
return null;
|
|
502
|
+
for (const part of h.split(";")) {
|
|
503
|
+
const eq = part.indexOf("=");
|
|
504
|
+
if (eq > 0 && part.slice(0, eq).trim() === name)
|
|
505
|
+
return part.slice(eq + 1).trim();
|
|
506
|
+
}
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
export function defineRegions(o) {
|
|
510
|
+
return new Regions(o);
|
|
511
|
+
}
|
package/dist/replay.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { EventPayload, ServerMessage } from "@weft-framework/runtime";
|
|
2
|
+
export declare class ReplayError extends Error {
|
|
3
|
+
}
|
|
4
|
+
export interface RecordedStep {
|
|
5
|
+
at: number;
|
|
6
|
+
id: string;
|
|
7
|
+
handler: string;
|
|
8
|
+
event: EventPayload;
|
|
9
|
+
args?: string;
|
|
10
|
+
outcome: string[];
|
|
11
|
+
}
|
|
12
|
+
export interface Recording {
|
|
13
|
+
weft: "recording";
|
|
14
|
+
v: 1;
|
|
15
|
+
path: string;
|
|
16
|
+
actor: string;
|
|
17
|
+
at: number;
|
|
18
|
+
steps: RecordedStep[];
|
|
19
|
+
}
|
|
20
|
+
export declare const RECORDING_MAX = 500;
|
|
21
|
+
export declare function redactPayload(ev: EventPayload): EventPayload;
|
|
22
|
+
export declare function redactPath(url: URL): string;
|
|
23
|
+
export declare function outcomeOf(msgs: ServerMessage[]): string[];
|
|
24
|
+
export declare function parseRecording(text: string): Recording;
|
|
25
|
+
export interface ReplayStep {
|
|
26
|
+
n: number;
|
|
27
|
+
what: string;
|
|
28
|
+
expected: string[];
|
|
29
|
+
got: string[];
|
|
30
|
+
same: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface ReplayResult {
|
|
33
|
+
steps: ReplayStep[];
|
|
34
|
+
ok: boolean;
|
|
35
|
+
}
|
|
36
|
+
export interface ReplayOptions {
|
|
37
|
+
url: string;
|
|
38
|
+
cookie?: string;
|
|
39
|
+
timeoutMs?: number;
|
|
40
|
+
fetch?: typeof fetch;
|
|
41
|
+
onStep?(step: ReplayStep): void;
|
|
42
|
+
}
|
|
43
|
+
export declare function replay(rec: Recording, opts: ReplayOptions): Promise<ReplayResult>;
|
|
44
|
+
export declare function recordingTest(rec: Recording, name: string, url: string): string;
|