toiljs 0.0.116 → 0.0.118
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/CHANGELOG.md +14 -0
- package/README.md +16 -7
- package/build/backend/.tsbuildinfo +1 -1
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +2375 -258
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/auth.d.ts +3 -0
- package/build/client/auth.js +19 -10
- package/build/client/dev/devtools.js +1 -1
- package/build/client/dev/error-overlay.js +1 -1
- package/build/client/index.d.ts +1 -1
- package/build/client/index.js +1 -1
- package/build/client/navigation/navigation.js +28 -3
- package/build/client/routing/Router.js +5 -2
- package/build/client/routing/mount.js +1 -1
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/auth-emails.js +7 -3
- package/build/compiler/emails.js +1 -1
- package/build/compiler/index.d.ts +1 -0
- package/build/compiler/index.js +26 -5
- package/build/compiler/pages.js +2 -3
- package/build/compiler/prerender.d.ts +3 -8
- package/build/compiler/prerender.js +88 -127
- package/build/compiler/ssg.js +3 -4
- package/build/compiler/template-build.js +3 -4
- package/build/compiler/toil-docs.generated.js +8 -7
- package/build/compiler/vite.js +4 -6
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/db/database.js +7 -3
- package/build/devserver/db/types.js +1 -1
- package/build/devserver/production.js +4 -3
- package/build/devserver/runtime/crypto.d.ts +1 -1
- package/build/io/.tsbuildinfo +1 -1
- package/build/logger/.tsbuildinfo +1 -1
- package/build/shared/.tsbuildinfo +1 -1
- package/examples/basic/client/routes/analytics.tsx +7 -5
- package/examples/basic/client/routes/pq.tsx +2 -1
- package/examples/basic/server/tsconfig.json +1 -3
- package/package.json +45 -44
- package/presets/no-uint8array-tostring.js +120 -153
- package/presets/oxlint.d.ts +5 -0
- package/presets/oxlint.js +224 -0
- package/server/auth/AuthController.ts +53 -14
- package/server/auth/FactorSettings.ts +41 -0
- package/server/auth/SessionEpochs.ts +58 -0
- package/server/auth/SessionRevocations.ts +13 -0
- package/server/globals/auth.ts +27 -2
- package/server/runtime/tsconfig.json +1 -3
- package/tsconfig.json +2 -11
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
- package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
- package/.github/ISSUE_TEMPLATE/config.yml +0 -8
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
- package/.github/changelog-config.json +0 -45
- package/.github/dependabot.yml +0 -27
- package/.github/workflows/ci.yml +0 -222
- package/.vscode/settings.json +0 -10
- package/docs/README.md +0 -107
- package/docs/auth/README.md +0 -104
- package/docs/auth/configuration.md +0 -105
- package/docs/auth/emails.md +0 -92
- package/docs/auth/extending.md +0 -202
- package/docs/auth/how-it-works.md +0 -138
- package/docs/auth/usage.md +0 -188
- package/docs/backend/README.md +0 -143
- package/docs/backend/data.md +0 -351
- package/docs/backend/rest.md +0 -402
- package/docs/backend/rpc.md +0 -226
- package/docs/background/README.md +0 -114
- package/docs/background/daemons.md +0 -276
- package/docs/background/derive.md +0 -179
- package/docs/cli/README.md +0 -379
- package/docs/concepts/ai-guide.md +0 -132
- package/docs/concepts/config.md +0 -416
- package/docs/concepts/decorators.md +0 -127
- package/docs/concepts/security.md +0 -108
- package/docs/concepts/tiers.md +0 -166
- package/docs/concepts/types.md +0 -216
- package/docs/database/README.md +0 -143
- package/docs/database/capacity.md +0 -350
- package/docs/database/counters.md +0 -174
- package/docs/database/documents.md +0 -277
- package/docs/database/events.md +0 -307
- package/docs/database/membership.md +0 -246
- package/docs/database/setup.md +0 -155
- package/docs/database/unique.md +0 -216
- package/docs/database/views.md +0 -246
- package/docs/frontend/README.md +0 -104
- package/docs/frontend/components.md +0 -286
- package/docs/frontend/data-fetching.md +0 -260
- package/docs/frontend/images.md +0 -148
- package/docs/frontend/metadata.md +0 -344
- package/docs/frontend/navigation.md +0 -296
- package/docs/frontend/rendering.md +0 -236
- package/docs/frontend/routing.md +0 -346
- package/docs/frontend/scripts.md +0 -118
- package/docs/frontend/search.md +0 -191
- package/docs/frontend/styling.md +0 -147
- package/docs/frontend/toil-global.md +0 -216
- package/docs/getting-started/README.md +0 -81
- package/docs/getting-started/create-project.md +0 -131
- package/docs/getting-started/deploy.md +0 -101
- package/docs/getting-started/first-app.md +0 -215
- package/docs/getting-started/installation.md +0 -124
- package/docs/getting-started/migrating.md +0 -125
- package/docs/getting-started/project-structure.md +0 -163
- package/docs/introduction/README.md +0 -66
- package/docs/introduction/design-principles.md +0 -81
- package/docs/introduction/distributed.md +0 -91
- package/docs/introduction/how-it-works.md +0 -121
- package/docs/introduction/hyperscale.md +0 -70
- package/docs/introduction/modern-stack.md +0 -85
- package/docs/introduction/vs-other-frameworks.md +0 -64
- package/docs/introduction/why-toil.md +0 -99
- package/docs/llms.txt +0 -95
- package/docs/realtime/README.md +0 -102
- package/docs/realtime/channels.md +0 -211
- package/docs/realtime/streams.md +0 -369
- package/docs/services/README.md +0 -60
- package/docs/services/analytics.md +0 -279
- package/docs/services/caching.md +0 -175
- package/docs/services/cookies.md +0 -235
- package/docs/services/crypto.md +0 -209
- package/docs/services/email.md +0 -289
- package/docs/services/environment.md +0 -141
- package/docs/services/ratelimit.md +0 -174
- package/docs/services/time.md +0 -85
- package/eslint.config.js +0 -57
- package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
- package/presets/eslint.js +0 -88
- package/src/backend/index.ts +0 -204
- package/src/cli/configure.ts +0 -363
- package/src/cli/create.ts +0 -924
- package/src/cli/db.ts +0 -158
- package/src/cli/diagnostics.ts +0 -665
- package/src/cli/doctor.ts +0 -982
- package/src/cli/features.ts +0 -170
- package/src/cli/index.ts +0 -326
- package/src/cli/notify.ts +0 -120
- package/src/cli/proc.ts +0 -56
- package/src/cli/ui.ts +0 -169
- package/src/cli/update.ts +0 -248
- package/src/cli/updates.ts +0 -92
- package/src/cli/validate.ts +0 -31
- package/src/cli/version-check.ts +0 -125
- package/src/client/auth.ts +0 -869
- package/src/client/channel/channel.ts +0 -146
- package/src/client/components/Form.tsx +0 -65
- package/src/client/components/Image.tsx +0 -160
- package/src/client/components/Script.tsx +0 -113
- package/src/client/components/Slot.tsx +0 -21
- package/src/client/dev/devtools.tsx +0 -1089
- package/src/client/dev/error-overlay.tsx +0 -225
- package/src/client/errors.ts +0 -11
- package/src/client/head/head.ts +0 -182
- package/src/client/head/metadata.ts +0 -110
- package/src/client/index.ts +0 -130
- package/src/client/navigation/Link.tsx +0 -108
- package/src/client/navigation/NavLink.tsx +0 -86
- package/src/client/navigation/navigation.ts +0 -235
- package/src/client/navigation/prefetch.ts +0 -169
- package/src/client/navigation/scroll.ts +0 -53
- package/src/client/routing/Router.tsx +0 -216
- package/src/client/routing/action.ts +0 -122
- package/src/client/routing/error-boundary.tsx +0 -43
- package/src/client/routing/hooks.ts +0 -140
- package/src/client/routing/lazy.ts +0 -77
- package/src/client/routing/loader.ts +0 -381
- package/src/client/routing/match.ts +0 -47
- package/src/client/routing/mount.tsx +0 -90
- package/src/client/routing/params-context.ts +0 -10
- package/src/client/routing/slot-context.ts +0 -7
- package/src/client/rpc.ts +0 -97
- package/src/client/search/search.ts +0 -189
- package/src/client/search/use-page-search.ts +0 -73
- package/src/client/ssr/markers.tsx +0 -172
- package/src/client/stream/client.ts +0 -237
- package/src/client/types.ts +0 -73
- package/src/compiler/auth-emails.ts +0 -312
- package/src/compiler/config.ts +0 -369
- package/src/compiler/docs.ts +0 -127
- package/src/compiler/email-preview.ts +0 -314
- package/src/compiler/emails.ts +0 -404
- package/src/compiler/fonts.ts +0 -89
- package/src/compiler/generate.ts +0 -471
- package/src/compiler/image-blur.ts +0 -42
- package/src/compiler/image-report.ts +0 -88
- package/src/compiler/index.ts +0 -1362
- package/src/compiler/pages.ts +0 -49
- package/src/compiler/plugin.ts +0 -391
- package/src/compiler/prerender.ts +0 -263
- package/src/compiler/routes.ts +0 -170
- package/src/compiler/seo.ts +0 -415
- package/src/compiler/sri.ts +0 -144
- package/src/compiler/ssg.ts +0 -177
- package/src/compiler/ssr-codegen.ts +0 -85
- package/src/compiler/template-build.ts +0 -676
- package/src/compiler/template.ts +0 -262
- package/src/compiler/toil-docs.generated.ts +0 -77
- package/src/compiler/vite.ts +0 -245
- package/src/devserver/analytics/index.ts +0 -317
- package/src/devserver/config/dotenv.ts +0 -102
- package/src/devserver/config/env.ts +0 -30
- package/src/devserver/config/ratelimit.ts +0 -123
- package/src/devserver/daemon/catalog.ts +0 -120
- package/src/devserver/daemon/cron.ts +0 -87
- package/src/devserver/daemon/host.ts +0 -134
- package/src/devserver/daemon/index.ts +0 -369
- package/src/devserver/daemon/runtime.ts +0 -48
- package/src/devserver/db/catalog.ts +0 -109
- package/src/devserver/db/database.ts +0 -1834
- package/src/devserver/db/derives.ts +0 -121
- package/src/devserver/db/index.ts +0 -20
- package/src/devserver/db/routeKinds.ts +0 -191
- package/src/devserver/db/types.ts +0 -168
- package/src/devserver/email/caps.ts +0 -0
- package/src/devserver/email/config.ts +0 -141
- package/src/devserver/email/console.ts +0 -348
- package/src/devserver/email/index.ts +0 -155
- package/src/devserver/email/providers.ts +0 -131
- package/src/devserver/email/status.ts +0 -23
- package/src/devserver/email/validate.ts +0 -37
- package/src/devserver/email/wire.ts +0 -55
- package/src/devserver/http/cache.ts +0 -0
- package/src/devserver/http/envelope.ts +0 -157
- package/src/devserver/http/proxy.ts +0 -172
- package/src/devserver/http/runtime.ts +0 -387
- package/src/devserver/index.ts +0 -43
- package/src/devserver/production-ipc.ts +0 -63
- package/src/devserver/production-worker.ts +0 -83
- package/src/devserver/production.ts +0 -718
- package/src/devserver/runtime/crypto.ts +0 -587
- package/src/devserver/runtime/host.ts +0 -427
- package/src/devserver/runtime/module.ts +0 -442
- package/src/devserver/server.ts +0 -251
- package/src/devserver/ssr.ts +0 -208
- package/src/devserver/stream/catalog.ts +0 -106
- package/src/devserver/stream/host.ts +0 -42
- package/src/devserver/stream/index.ts +0 -301
- package/src/devserver/stream/manager.ts +0 -167
- package/src/devserver/stream/router.ts +0 -101
- package/src/devserver/stream/wire.ts +0 -58
- package/src/devserver/stream/ws.ts +0 -78
- package/src/devserver/wasm/sections.ts +0 -59
- package/src/devserver/wasm/surface.ts +0 -115
- package/src/io/FastMap.ts +0 -151
- package/src/io/FastSet.ts +0 -113
- package/src/io/codec.ts +0 -362
- package/src/io/index.ts +0 -10
- package/src/io/lengths.ts +0 -14
- package/src/io/types.ts +0 -19
- package/src/logger/index.ts +0 -22
- package/src/shared/index.ts +0 -46
- package/test/analytics-dev.test.ts +0 -113
- package/test/assembly/aspect-shim.ts +0 -33
- package/test/assembly/cookie.spec.ts +0 -303
- package/test/assembly/example.spec.ts +0 -22
- package/test/assembly/ssr.spec.ts +0 -95
- package/test/assembly.test.ts +0 -59
- package/test/auth-emails.test.ts +0 -118
- package/test/built-ssr.test.ts +0 -148
- package/test/channel.test.ts +0 -21
- package/test/config-host-port.test.ts +0 -59
- package/test/configure.test.ts +0 -94
- package/test/daemon-build.test.ts +0 -278
- package/test/daemon-catalog.test.ts +0 -349
- package/test/daemon-emulation.test.ts +0 -256
- package/test/db.test.ts +0 -0
- package/test/devserver-database.test.ts +0 -1102
- package/test/devserver-email.test.ts +0 -306
- package/test/devserver-pqauth.test.ts +0 -93
- package/test/devserver-secrets.test.ts +0 -63
- package/test/devserver.test.ts +0 -259
- package/test/doctor.test.ts +0 -298
- package/test/dom/Image.test.tsx +0 -144
- package/test/dom/Link.test.tsx +0 -67
- package/test/dom/NavLink.test.tsx +0 -37
- package/test/dom/Script.test.tsx +0 -48
- package/test/dom/action.test.tsx +0 -146
- package/test/dom/error-overlay.test.tsx +0 -44
- package/test/dom/loader.test.tsx +0 -121
- package/test/dom/navigation.test.ts +0 -59
- package/test/dom/revalidate.test.tsx +0 -38
- package/test/dom/route-head.test.tsx +0 -56
- package/test/dom/router-loading.test.tsx +0 -44
- package/test/dom/scroll.test.ts +0 -56
- package/test/dom/slot.test.tsx +0 -131
- package/test/dom/use-metadata.test.tsx +0 -58
- package/test/dom/view-transitions.test.tsx +0 -53
- package/test/dynamic-routing.test.ts +0 -132
- package/test/email-console.test.ts +0 -86
- package/test/email-preview.test.ts +0 -73
- package/test/emails.test.ts +0 -58
- package/test/errors.test.ts +0 -21
- package/test/example-guestbook.test.ts +0 -123
- package/test/features.test.ts +0 -149
- package/test/fixtures/bignum-wire/spec.ts +0 -25
- package/test/fixtures/daemon-app.ts +0 -65
- package/test/fixtures/stream-echo.ts +0 -26
- package/test/fixtures/stream-gate.ts +0 -28
- package/test/fixtures/stream-trap.ts +0 -18
- package/test/fixtures/stream-typed.ts +0 -41
- package/test/fonts.test.ts +0 -28
- package/test/global-setup.ts +0 -17
- package/test/head.test.ts +0 -42
- package/test/image-blur.test.ts +0 -50
- package/test/io.test.ts +0 -117
- package/test/metadata.test.ts +0 -40
- package/test/navlink.test.ts +0 -28
- package/test/pages.test.ts +0 -105
- package/test/placeholder.test.ts +0 -9
- package/test/pqauth-e2e.test.ts +0 -216
- package/test/pqauth-email.test.ts +0 -801
- package/test/prerender.test.ts +0 -116
- package/test/prettier-plugin.test.ts +0 -46
- package/test/routes.test.ts +0 -76
- package/test/rpc-bignum-wire.test.ts +0 -164
- package/test/rpc-dispatch.test.ts +0 -132
- package/test/rpc-kinds.test.ts +0 -18
- package/test/rpc.test.ts +0 -66
- package/test/search.test.ts +0 -114
- package/test/seo.test.ts +0 -175
- package/test/slot-layouts.test.ts +0 -69
- package/test/sri.test.ts +0 -129
- package/test/ssg.test.ts +0 -36
- package/test/ssr-hydration.test.tsx +0 -122
- package/test/ssr-render.test.ts +0 -197
- package/test/ssr-template.test.tsx +0 -482
- package/test/stream-emulation.test.ts +0 -479
- package/test/ui.test.ts +0 -40
- package/test/update.test.ts +0 -58
- package/test/validate.test.ts +0 -42
- package/test/version-check.test.ts +0 -130
- package/tests/data-parity/generated-parity.ts +0 -99
- package/tests/data-parity/parity.ts +0 -80
- package/tests/data-parity/spec.ts +0 -46
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { customSection } from '../wasm/sections.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Parses the `toildb.derives` wiring section emitted by toilscript for a
|
|
5
|
-
* `@database` class with `@derive` materializer methods (see the compiler's
|
|
6
|
-
* `buildToilDbDerives`). It maps each derive to its owning `@database` class, so
|
|
7
|
-
* the dev runtime (`runtime/module.ts`) can, after a dispatch writes a source
|
|
8
|
-
* collection, re-run that database's derives under FunctionKind=Derive. Fails
|
|
9
|
-
* closed: any malformed byte yields `[]` (no derive runs) rather than throwing.
|
|
10
|
-
*
|
|
11
|
-
* Section layout (LE), mirroring `buildToilDbDerives`:
|
|
12
|
-
* u16 format_version = 1
|
|
13
|
-
* u16 n_derives
|
|
14
|
-
* per derive: u16 derive_id, str db_name, str method_name (str = u32 len + bytes)
|
|
15
|
-
*/
|
|
16
|
-
export interface DeriveEntry {
|
|
17
|
-
readonly deriveId: number;
|
|
18
|
-
readonly dbName: string;
|
|
19
|
-
readonly methodName: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const SECTION = 'toildb.derives';
|
|
23
|
-
const VERSION = 1;
|
|
24
|
-
const MAX_SECTION_BYTES = 128 * 1024;
|
|
25
|
-
const MAX_DERIVES = 1024;
|
|
26
|
-
const MAX_NAME_BYTES = 1024;
|
|
27
|
-
const UTF8_DECODER = new TextDecoder('utf-8', { fatal: true });
|
|
28
|
-
|
|
29
|
-
export function parseDerives(wasm: Buffer): readonly DeriveEntry[] {
|
|
30
|
-
let section: Buffer | null;
|
|
31
|
-
try {
|
|
32
|
-
section = customSection(wasm, SECTION);
|
|
33
|
-
} catch {
|
|
34
|
-
return [];
|
|
35
|
-
}
|
|
36
|
-
if (section === null) return [];
|
|
37
|
-
if (section.length > MAX_SECTION_BYTES) return [];
|
|
38
|
-
|
|
39
|
-
const r = new Reader(section);
|
|
40
|
-
const version = r.u16();
|
|
41
|
-
if (!r.ok || version !== VERSION) return [];
|
|
42
|
-
const count = r.u16();
|
|
43
|
-
if (!r.ok || count > MAX_DERIVES) return [];
|
|
44
|
-
|
|
45
|
-
const derives: DeriveEntry[] = [];
|
|
46
|
-
for (let i = 0; i < count && r.ok; i++) {
|
|
47
|
-
const deriveId = r.u16();
|
|
48
|
-
const dbName = r.string();
|
|
49
|
-
const methodName = r.string();
|
|
50
|
-
if (!r.ok || dbName.length === 0) return [];
|
|
51
|
-
derives.push({ deriveId, dbName, methodName });
|
|
52
|
-
}
|
|
53
|
-
if (!r.ok || r.remaining() !== 0) return [];
|
|
54
|
-
return derives;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* The derives whose owning `@database` had at least one source collection
|
|
59
|
-
* written during this dispatch. `written` holds "Db/coll" store keys; the
|
|
60
|
-
* database is the prefix before the first `/`. Each affected derive appears at
|
|
61
|
-
* most once (coalescing: many writes to one database run its derives once).
|
|
62
|
-
*/
|
|
63
|
-
export function derivesForWrites(
|
|
64
|
-
derives: readonly DeriveEntry[],
|
|
65
|
-
written: ReadonlySet<string>,
|
|
66
|
-
): readonly DeriveEntry[] {
|
|
67
|
-
if (derives.length === 0 || written.size === 0) return [];
|
|
68
|
-
const dbs = new Set<string>();
|
|
69
|
-
for (const key of written) {
|
|
70
|
-
const slash = key.indexOf('/');
|
|
71
|
-
dbs.add(slash >= 0 ? key.slice(0, slash) : key);
|
|
72
|
-
}
|
|
73
|
-
return derives.filter((d) => dbs.has(d.dbName));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
class Reader {
|
|
77
|
-
private pos = 0;
|
|
78
|
-
ok = true;
|
|
79
|
-
|
|
80
|
-
constructor(private readonly bytes: Buffer) {}
|
|
81
|
-
|
|
82
|
-
remaining(): number {
|
|
83
|
-
return this.bytes.length - this.pos;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
u16(): number {
|
|
87
|
-
if (!this.ok || this.pos + 2 > this.bytes.length) {
|
|
88
|
-
this.ok = false;
|
|
89
|
-
return 0;
|
|
90
|
-
}
|
|
91
|
-
const out = this.bytes.readUInt16LE(this.pos);
|
|
92
|
-
this.pos += 2;
|
|
93
|
-
return out;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
u32(): number {
|
|
97
|
-
if (!this.ok || this.pos + 4 > this.bytes.length) {
|
|
98
|
-
this.ok = false;
|
|
99
|
-
return 0;
|
|
100
|
-
}
|
|
101
|
-
const out = this.bytes.readUInt32LE(this.pos);
|
|
102
|
-
this.pos += 4;
|
|
103
|
-
return out;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
string(): string {
|
|
107
|
-
const len = this.u32();
|
|
108
|
-
if (!this.ok || len > MAX_NAME_BYTES || this.pos + len > this.bytes.length) {
|
|
109
|
-
this.ok = false;
|
|
110
|
-
return '';
|
|
111
|
-
}
|
|
112
|
-
try {
|
|
113
|
-
const out = UTF8_DECODER.decode(this.bytes.subarray(this.pos, this.pos + len));
|
|
114
|
-
this.pos += len;
|
|
115
|
-
return out;
|
|
116
|
-
} catch {
|
|
117
|
-
this.ok = false;
|
|
118
|
-
return '';
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The dev ToilDB subsystem: the {@link DevDatabase} class + its process
|
|
3
|
-
* singleton, the per-request state, the catalog parser, and the `data.*` host
|
|
4
|
-
* imports. The production edge backs the SAME imports with ScyllaDB.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
export {
|
|
8
|
-
__resetDbForTests,
|
|
9
|
-
__setDbCatalogForTests,
|
|
10
|
-
buildDatabaseImports,
|
|
11
|
-
commitDeriveCheckpoints,
|
|
12
|
-
configureDbPersistence,
|
|
13
|
-
DevDatabase,
|
|
14
|
-
devDb,
|
|
15
|
-
persistDb,
|
|
16
|
-
setDbCatalog,
|
|
17
|
-
} from './database.js';
|
|
18
|
-
export { parseCatalog } from './catalog.js';
|
|
19
|
-
export { type DeriveEntry, derivesForWrites, parseDerives } from './derives.js';
|
|
20
|
-
export { CollectionFamily, DbFunctionKind, type DbDevState, freshDbState } from './types.js';
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
import { customSection } from '../wasm/sections.js';
|
|
2
|
-
import { DbFunctionKind } from './types.js';
|
|
3
|
-
|
|
4
|
-
export interface RouteKindEntry {
|
|
5
|
-
readonly method: number;
|
|
6
|
-
readonly kind: DbFunctionKind;
|
|
7
|
-
readonly pattern: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface RpcKindEntry {
|
|
11
|
-
readonly methodId: number;
|
|
12
|
-
readonly kind: DbFunctionKind;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const SECTION = 'toildb.route_kinds';
|
|
16
|
-
const RPC_SECTION = 'toildb.rpc_kinds';
|
|
17
|
-
const VERSION = 1;
|
|
18
|
-
const MAX_SECTION_BYTES = 128 * 1024;
|
|
19
|
-
const MAX_ROUTES = 2048;
|
|
20
|
-
const MAX_PATTERN_BYTES = 2048;
|
|
21
|
-
const UTF8_DECODER = new TextDecoder('utf-8', { fatal: true });
|
|
22
|
-
|
|
23
|
-
const METHOD_CODES: Readonly<Record<string, number>> = {
|
|
24
|
-
GET: 0,
|
|
25
|
-
POST: 1,
|
|
26
|
-
PUT: 2,
|
|
27
|
-
DELETE: 3,
|
|
28
|
-
PATCH: 4,
|
|
29
|
-
HEAD: 5,
|
|
30
|
-
OPTIONS: 6,
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export function parseRouteKinds(wasm: Buffer): readonly RouteKindEntry[] {
|
|
34
|
-
let section: Buffer | null;
|
|
35
|
-
try {
|
|
36
|
-
section = customSection(wasm, SECTION);
|
|
37
|
-
} catch {
|
|
38
|
-
return [];
|
|
39
|
-
}
|
|
40
|
-
if (section === null) return [];
|
|
41
|
-
if (section.length > MAX_SECTION_BYTES) return [];
|
|
42
|
-
|
|
43
|
-
const r = new Reader(section);
|
|
44
|
-
const version = r.u16();
|
|
45
|
-
if (!r.ok || version !== VERSION) return [];
|
|
46
|
-
const count = r.u16();
|
|
47
|
-
if (!r.ok || count > MAX_ROUTES) return [];
|
|
48
|
-
|
|
49
|
-
const routes: RouteKindEntry[] = [];
|
|
50
|
-
for (let i = 0; i < count && r.ok; i++) {
|
|
51
|
-
const method = r.u8();
|
|
52
|
-
const kindByte = r.u8();
|
|
53
|
-
const pattern = r.string();
|
|
54
|
-
const kind =
|
|
55
|
-
kindByte === 0 ? DbFunctionKind.Query : kindByte === 1 ? DbFunctionKind.Action : null;
|
|
56
|
-
if (!r.ok || method < 0 || method > 6 || kind === null || !validPattern(pattern)) return [];
|
|
57
|
-
routes.push({ method, kind, pattern });
|
|
58
|
-
}
|
|
59
|
-
if (!r.ok || r.remaining() !== 0) return [];
|
|
60
|
-
return routes;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function routeKindForRequest(
|
|
64
|
-
routes: readonly RouteKindEntry[],
|
|
65
|
-
method: string,
|
|
66
|
-
path: string,
|
|
67
|
-
): DbFunctionKind | null {
|
|
68
|
-
const methodCode = METHOD_CODES[method.toUpperCase()];
|
|
69
|
-
if (methodCode === undefined) return null;
|
|
70
|
-
for (const route of routes) {
|
|
71
|
-
if (route.method === methodCode && routeMatches(route.pattern, path)) return route.kind;
|
|
72
|
-
}
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function parseRpcKinds(wasm: Buffer): readonly RpcKindEntry[] {
|
|
77
|
-
let section: Buffer | null;
|
|
78
|
-
try {
|
|
79
|
-
section = customSection(wasm, RPC_SECTION);
|
|
80
|
-
} catch {
|
|
81
|
-
return [];
|
|
82
|
-
}
|
|
83
|
-
if (section === null) return [];
|
|
84
|
-
if (section.length > MAX_SECTION_BYTES) return [];
|
|
85
|
-
|
|
86
|
-
const r = new Reader(section);
|
|
87
|
-
const version = r.u16();
|
|
88
|
-
if (!r.ok || version !== VERSION) return [];
|
|
89
|
-
const count = r.u16();
|
|
90
|
-
if (!r.ok || count > MAX_ROUTES) return [];
|
|
91
|
-
|
|
92
|
-
const methods: RpcKindEntry[] = [];
|
|
93
|
-
for (let i = 0; i < count && r.ok; i++) {
|
|
94
|
-
const methodId = r.u32();
|
|
95
|
-
const kindByte = r.u8();
|
|
96
|
-
const kind =
|
|
97
|
-
kindByte === 0 ? DbFunctionKind.Query : kindByte === 1 ? DbFunctionKind.Action : null;
|
|
98
|
-
if (!r.ok || kind === null) return [];
|
|
99
|
-
methods.push({ methodId, kind });
|
|
100
|
-
}
|
|
101
|
-
if (!r.ok || r.remaining() !== 0) return [];
|
|
102
|
-
return methods;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** DB kind for an RPC method id (the `dacely-rpc` header). An id absent from rpc_kinds is read-only
|
|
106
|
-
* (Query) - the safe default for RPC, matching the host gate. */
|
|
107
|
-
export function rpcKindForId(methods: readonly RpcKindEntry[], methodId: number): DbFunctionKind {
|
|
108
|
-
for (const m of methods) {
|
|
109
|
-
if (m.methodId === methodId) return m.kind;
|
|
110
|
-
}
|
|
111
|
-
return DbFunctionKind.Query;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function validPattern(pattern: string): boolean {
|
|
115
|
-
if (pattern.length === 0 || !pattern.startsWith('/')) return false;
|
|
116
|
-
for (let i = 0; i < pattern.length; i++) {
|
|
117
|
-
const c = pattern.charCodeAt(i);
|
|
118
|
-
if (c < 0x20 || c > 0x7e) return false;
|
|
119
|
-
}
|
|
120
|
-
return true;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function routeMatches(pattern: string, pathWithQuery: string): boolean {
|
|
124
|
-
const q = pathWithQuery.indexOf('?');
|
|
125
|
-
const path = q >= 0 ? pathWithQuery.slice(0, q) : pathWithQuery;
|
|
126
|
-
const patternSegs = pattern.split('/').filter(Boolean);
|
|
127
|
-
const pathSegs = path.split('/').filter(Boolean);
|
|
128
|
-
if (patternSegs.length !== pathSegs.length) return false;
|
|
129
|
-
for (let i = 0; i < patternSegs.length; i++) {
|
|
130
|
-
const p = patternSegs[i] ?? '';
|
|
131
|
-
const a = pathSegs[i] ?? '';
|
|
132
|
-
if (p.startsWith(':') && p.length > 1 && a.length > 0) continue;
|
|
133
|
-
if (p !== a) return false;
|
|
134
|
-
}
|
|
135
|
-
return true;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
class Reader {
|
|
139
|
-
private pos = 0;
|
|
140
|
-
ok = true;
|
|
141
|
-
|
|
142
|
-
constructor(private readonly bytes: Buffer) {}
|
|
143
|
-
|
|
144
|
-
remaining(): number {
|
|
145
|
-
return this.bytes.length - this.pos;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
u8(): number {
|
|
149
|
-
if (!this.ok || this.pos + 1 > this.bytes.length) {
|
|
150
|
-
this.ok = false;
|
|
151
|
-
return 0;
|
|
152
|
-
}
|
|
153
|
-
return this.bytes[this.pos++] ?? 0;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
u16(): number {
|
|
157
|
-
if (!this.ok || this.pos + 2 > this.bytes.length) {
|
|
158
|
-
this.ok = false;
|
|
159
|
-
return 0;
|
|
160
|
-
}
|
|
161
|
-
const out = this.bytes.readUInt16LE(this.pos);
|
|
162
|
-
this.pos += 2;
|
|
163
|
-
return out;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
u32(): number {
|
|
167
|
-
if (!this.ok || this.pos + 4 > this.bytes.length) {
|
|
168
|
-
this.ok = false;
|
|
169
|
-
return 0;
|
|
170
|
-
}
|
|
171
|
-
const out = this.bytes.readUInt32LE(this.pos);
|
|
172
|
-
this.pos += 4;
|
|
173
|
-
return out;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
string(): string {
|
|
177
|
-
const len = this.u32();
|
|
178
|
-
if (!this.ok || len > MAX_PATTERN_BYTES || this.pos + len > this.bytes.length) {
|
|
179
|
-
this.ok = false;
|
|
180
|
-
return '';
|
|
181
|
-
}
|
|
182
|
-
try {
|
|
183
|
-
const out = UTF8_DECODER.decode(this.bytes.subarray(this.pos, this.pos + len));
|
|
184
|
-
this.pos += len;
|
|
185
|
-
return out;
|
|
186
|
-
} catch {
|
|
187
|
-
this.ok = false;
|
|
188
|
-
return '';
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared types, limits, and ABI return codes for the dev ToilDB emulation
|
|
3
|
-
* (see `./database.ts`). The wire ABI mirrors the production edge byte-for-byte
|
|
4
|
-
* (toildb/ABI.md): every byte region is a (ptr,len) into guest memory; returns
|
|
5
|
-
* are `>= 0` success (a length/handle/flag), `-1` too-small, `-2` absent,
|
|
6
|
-
* `<= -1000` a typed error (`-(1000+TDLnnn)`).
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
export enum CollectionFamily {
|
|
10
|
-
Record = 0,
|
|
11
|
-
View = 1,
|
|
12
|
-
Events = 2,
|
|
13
|
-
Counter = 3,
|
|
14
|
-
Membership = 4,
|
|
15
|
-
Unique = 5,
|
|
16
|
-
Capacity = 6,
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export enum DbFunctionKind {
|
|
20
|
-
Query = 'query',
|
|
21
|
-
Action = 'action',
|
|
22
|
-
Derive = 'derive',
|
|
23
|
-
Job = 'job',
|
|
24
|
-
Admin = 'admin',
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function isCollectionFamily(value: number): value is CollectionFamily {
|
|
28
|
-
return value >= CollectionFamily.Record && value <= CollectionFamily.Capacity;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface DevField {
|
|
32
|
-
name: string;
|
|
33
|
-
typeName: string;
|
|
34
|
-
isArray: boolean;
|
|
35
|
-
unique: boolean;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface DevCollectionHandle {
|
|
39
|
-
name: string;
|
|
40
|
-
family: CollectionFamily;
|
|
41
|
-
schemaVersion: number;
|
|
42
|
-
replication: number;
|
|
43
|
-
placement: number;
|
|
44
|
-
fillMaxWaitMs: number;
|
|
45
|
-
fillAllowStale: boolean;
|
|
46
|
-
/** The value type's field layout (declaration order); present when the catalog
|
|
47
|
-
* carried one. Drives @unique enforcement. */
|
|
48
|
-
fields?: DevField[];
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export type DbCatalogState =
|
|
52
|
-
| { kind: 'no-section' }
|
|
53
|
-
| { kind: 'malformed' }
|
|
54
|
-
| { kind: 'present'; collections: Map<string, DevCollectionHandle> };
|
|
55
|
-
|
|
56
|
-
/** Per-request data state: resolved handles + the last variable-length result +
|
|
57
|
-
* the schema_version of that result's row (surfaced via result_schema_version). */
|
|
58
|
-
export interface DbDevState {
|
|
59
|
-
handles: DevCollectionHandle[];
|
|
60
|
-
lastResult: Buffer | null;
|
|
61
|
-
lastResultVersion: number;
|
|
62
|
-
functionKind: DbFunctionKind;
|
|
63
|
-
/** Names ("Db/coll") of source collections written during this dispatch, so
|
|
64
|
-
* the runtime can re-run the affected `@derive` materializers afterward.
|
|
65
|
-
* Only populated for non-Derive dispatches (a derive's own writes must not
|
|
66
|
-
* re-trigger it - see `database.ts` `recordWrite`). */
|
|
67
|
-
writtenCollections: Set<string>;
|
|
68
|
-
/** The derive index currently running (set by `runDerive`), so `events.since` keys its resumable
|
|
69
|
-
* checkpoint per (derive, source key). 0 outside a derive. */
|
|
70
|
-
deriveId: number;
|
|
71
|
-
/** Checkpoint advances `events.since` staged during THIS derive run, keyed `"<deriveId>\0<storeKey>"` ->
|
|
72
|
-
* events-folded count. Committed to the durable `deriveCheckpoints` only after the fold's `derive_run`
|
|
73
|
-
* returns (its `view.publish`es have landed): a trapped fold discards this overlay, so the next run
|
|
74
|
-
* re-reads the batch rather than skipping it (publish-then-persist, mirroring the edge). */
|
|
75
|
-
pendingCheckpoints: Map<string, number>;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function freshDbState(): DbDevState {
|
|
79
|
-
return {
|
|
80
|
-
handles: [],
|
|
81
|
-
lastResult: null,
|
|
82
|
-
lastResultVersion: -1,
|
|
83
|
-
functionKind: DbFunctionKind.Job,
|
|
84
|
-
writtenCollections: new Set<string>(),
|
|
85
|
-
deriveId: 0,
|
|
86
|
-
pendingCheckpoints: new Map<string, number>(),
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** A finite-resource escrow: a ceiling + a set of reservations, each held (in
|
|
91
|
-
* flight, TTL'd) or confirmed (a permanent consume). Both count against available;
|
|
92
|
-
* a confirmed reservation never expires. Mirrors `toildb::capacity::Escrow`. */
|
|
93
|
-
export interface Reservation {
|
|
94
|
-
amount: bigint;
|
|
95
|
-
expiresMs: number;
|
|
96
|
-
confirmed: boolean;
|
|
97
|
-
}
|
|
98
|
-
export interface CapLedger {
|
|
99
|
-
total: bigint;
|
|
100
|
-
reservations: Map<bigint, Reservation>;
|
|
101
|
-
nextId: bigint;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** The on-disk snapshot shape: dev data + its versions, JSON with base64 buffers. */
|
|
105
|
-
export interface DbSnapshot {
|
|
106
|
-
store: Record<string, { v: string; sv: number }>; // records + unique owners (+ schema_version)
|
|
107
|
-
recordIdem?: Record<
|
|
108
|
-
string,
|
|
109
|
-
{ requestHash: string; state: 'pending' | 'done'; outcome?: RecordOutcomeSnapshot }
|
|
110
|
-
>;
|
|
111
|
-
uniqueIdem?: Record<string, string>;
|
|
112
|
-
views: Record<string, { v: string; sv: number }>;
|
|
113
|
-
members: Record<string, Record<string, { v: string; sv: number }>>;
|
|
114
|
-
counters: Record<string, string>;
|
|
115
|
-
counterIdem?: Record<string, string>;
|
|
116
|
-
events: Record<string, { v: string; sv: number }[]>;
|
|
117
|
-
eventDedup: Record<string, string[]>;
|
|
118
|
-
/** Durable `events.since` cursors, `"<deriveId>\0<storeKey>"` -> events-folded count, so a restart
|
|
119
|
-
* resumes an incremental `@derive` instead of re-folding the whole log onto the persisted view. */
|
|
120
|
-
deriveCheckpoints?: Record<string, number>;
|
|
121
|
-
capacity: Record<
|
|
122
|
-
string,
|
|
123
|
-
{
|
|
124
|
-
total: string;
|
|
125
|
-
nextId: string;
|
|
126
|
-
reservations: [string, { amount: string; expiresMs: number; confirmed: boolean }][];
|
|
127
|
-
}
|
|
128
|
-
>;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export type RecordOutcomeSnapshot =
|
|
132
|
-
| { kind: 'unit' }
|
|
133
|
-
| { kind: 'value'; v: string; sv: number }
|
|
134
|
-
| { kind: 'absent' }
|
|
135
|
-
| { kind: 'already_exists' }
|
|
136
|
-
| { kind: 'not_found' }
|
|
137
|
-
| { kind: 'conflict' };
|
|
138
|
-
|
|
139
|
-
/** Edge caps (toildb::capacity::escrow): bound the reservation count + the hold TTL. */
|
|
140
|
-
export const MAX_RESERVATIONS = 4096;
|
|
141
|
-
export const MAX_RESERVATION_TTL_MS = 86_400_000; // 24h
|
|
142
|
-
|
|
143
|
-
export const MAX_NAME = 512;
|
|
144
|
-
export const MAX_KEY = 4096;
|
|
145
|
-
export const MAX_VALUE = 256 * 1024;
|
|
146
|
-
export const DEFAULT_FILL_WAIT_MS = 50;
|
|
147
|
-
export const MAX_FILL_WAIT_MS = 60_000;
|
|
148
|
-
|
|
149
|
-
// i64 saturation bounds (the edge `MemEngine`/`ScyllaEngine` counters are i64).
|
|
150
|
-
const I64_MIN = -(2n ** 63n);
|
|
151
|
-
const I64_MAX = 2n ** 63n - 1n;
|
|
152
|
-
export function satI64(v: bigint): bigint {
|
|
153
|
-
return v < I64_MIN ? I64_MIN : v > I64_MAX ? I64_MAX : v;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Return codes, mirroring the edge ABI (`toildb::observe::diagnostics`): a typed
|
|
157
|
-
// error is `-(1000 + TDLnnn)`; a plain absence is ABSENT (-2), not a typed error.
|
|
158
|
-
export const ABSENT = -2; // NotFound / absent
|
|
159
|
-
export const TOO_SMALL = -1;
|
|
160
|
-
export const INVALID_HANDLE = -1001; // TDL001
|
|
161
|
-
export const ALREADY_EXISTS = -1003; // TDL003 (create on an existing key)
|
|
162
|
-
export const CONFLICT = -1004; // TDL004 (e.g. unique release by a non-owner)
|
|
163
|
-
export const UNAVAILABLE = -1031; // TDL031 (retryable in-flight/uncertain op)
|
|
164
|
-
export const CODEC_ERR = -1006; // TDL006 (e.g. a non-positive reserve amount)
|
|
165
|
-
export const OP_NOT_ALLOWED_FOR_FAMILY = -1010; // TDL010
|
|
166
|
-
export const OP_NOT_ALLOWED_IN_KIND = -1011; // TDL011
|
|
167
|
-
export const TOO_MANY_KEYS = -1020; // TDL020 (get_many over the per-call cap)
|
|
168
|
-
export const SCHEMA_UNAVAILABLE = -1070; // TDL070
|
|
Binary file
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Resolve the dev / self-host email config: merge the typed `toil.config.ts`
|
|
3
|
-
* `email` section (non-secret) with the reserved `TOIL_EMAIL_*` env keys (which
|
|
4
|
-
* WIN, for edge parity + 12-factor), and pull the API key from
|
|
5
|
-
* `TOIL_EMAIL_API_KEY` (a secret, only ever in `.env.secrets`/the environment).
|
|
6
|
-
*
|
|
7
|
-
* Mirrors the edge's `host/email.rs` (`EmailSection::from_reserved` + `resolve`):
|
|
8
|
-
* provider parse, `from` validation, Gmail/SMTP defaults. Returns a `null`
|
|
9
|
-
* config when email is simply not set up (silent), or a `warning` when it is
|
|
10
|
-
* partially configured but invalid (logged at startup, treated as Disabled).
|
|
11
|
-
*/
|
|
12
|
-
import type { EmailBackendConfig } from 'toiljs/shared';
|
|
13
|
-
|
|
14
|
-
import { validFrom } from './validate.js';
|
|
15
|
-
|
|
16
|
-
export type ResolvedProvider = 'resend' | 'smtp';
|
|
17
|
-
|
|
18
|
-
export interface ResolvedSmtp {
|
|
19
|
-
readonly host: string;
|
|
20
|
-
readonly port: number;
|
|
21
|
-
readonly user: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface ResolvedEmailConfig {
|
|
25
|
-
readonly provider: ResolvedProvider;
|
|
26
|
-
readonly from: string;
|
|
27
|
-
readonly apiKey: string;
|
|
28
|
-
readonly maxPerMin: number;
|
|
29
|
-
readonly maxPerDay: number;
|
|
30
|
-
readonly maxPerRecipientPerHour: number;
|
|
31
|
-
/** Present iff `provider === 'smtp'`. */
|
|
32
|
-
readonly smtp?: ResolvedSmtp;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface ResolveResult {
|
|
36
|
-
/** The resolved config, or `null` when email is unconfigured / invalid. */
|
|
37
|
-
readonly config: ResolvedEmailConfig | null;
|
|
38
|
-
/** A reason email is off despite partial config (logged at startup). */
|
|
39
|
-
readonly warning: string | null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** `TOIL_EMAIL_<NAME>` from the reserved map, trimmed; `undefined` if absent/empty. */
|
|
43
|
-
function envOf(reserved: Map<string, string>, name: string): string | undefined {
|
|
44
|
-
const v = reserved.get(`TOIL_EMAIL_${name}`);
|
|
45
|
-
const t = v?.trim();
|
|
46
|
-
return t ? t : undefined;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function parseBool(v: string | undefined): boolean | undefined {
|
|
50
|
-
if (v === undefined) return undefined;
|
|
51
|
-
return v === '1' || v === 'true' || v === 'yes' || v === 'on';
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function parseInt0(v: string | undefined, fallback: number): number {
|
|
55
|
-
if (v === undefined) return fallback;
|
|
56
|
-
const n = Number.parseInt(v, 10);
|
|
57
|
-
return Number.isFinite(n) && n >= 0 ? n : fallback;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function resolveEmailConfig(
|
|
61
|
-
cfg: EmailBackendConfig | null | undefined,
|
|
62
|
-
reserved: Map<string, string>,
|
|
63
|
-
): ResolveResult {
|
|
64
|
-
const c = cfg ?? {};
|
|
65
|
-
// Env (TOIL_EMAIL_*) overrides the config file; api key is env-only.
|
|
66
|
-
const providerId = (envOf(reserved, 'PROVIDER') ?? c.provider ?? 'resend').toLowerCase();
|
|
67
|
-
const from = envOf(reserved, 'FROM') ?? c.from?.trim();
|
|
68
|
-
const apiKey = envOf(reserved, 'API_KEY');
|
|
69
|
-
const enabled = parseBool(envOf(reserved, 'ENABLED'));
|
|
70
|
-
|
|
71
|
-
// Unconfigured: explicitly disabled, or no `from` and no key at all -> silent off.
|
|
72
|
-
if (enabled === false) return { config: null, warning: null };
|
|
73
|
-
if (from === undefined && apiKey === undefined && cfg == null) {
|
|
74
|
-
return { config: null, warning: null };
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (from === undefined) {
|
|
78
|
-
return { config: null, warning: 'email config present but `from` is missing' };
|
|
79
|
-
}
|
|
80
|
-
if (!validFrom(from)) {
|
|
81
|
-
return { config: null, warning: 'email `from` is not a valid address (CRLF or no `@`)' };
|
|
82
|
-
}
|
|
83
|
-
// The API-key requirement is PER PROVIDER: Resend authenticates with a key, and
|
|
84
|
-
// Gmail SMTP needs an app password (also carried in the key), but a plain SMTP
|
|
85
|
-
// relay (a local dev catch-all like Mailpit/MailHog on 127.0.0.1, or an internal
|
|
86
|
-
// open relay) needs NO credential. Requiring a key for plain SMTP silently
|
|
87
|
-
// disabled a perfectly good local-SMTP dev setup.
|
|
88
|
-
let provider: ResolvedProvider;
|
|
89
|
-
let smtp: ResolvedSmtp | undefined;
|
|
90
|
-
if (providerId === 'resend') {
|
|
91
|
-
if (apiKey === undefined) {
|
|
92
|
-
return {
|
|
93
|
-
config: null,
|
|
94
|
-
warning: 'provider `resend` requires TOIL_EMAIL_API_KEY (in .env.secrets)',
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
provider = 'resend';
|
|
98
|
-
} else if (providerId === 'gmail' || providerId === 'smtp') {
|
|
99
|
-
provider = 'smtp';
|
|
100
|
-
const isGmail = providerId === 'gmail';
|
|
101
|
-
const host =
|
|
102
|
-
envOf(reserved, 'SMTP_HOST') ??
|
|
103
|
-
c.smtp?.host?.trim() ??
|
|
104
|
-
(isGmail ? 'smtp.gmail.com' : '');
|
|
105
|
-
if (!host) {
|
|
106
|
-
return { config: null, warning: 'provider `smtp` requires TOIL_EMAIL_SMTP_HOST' };
|
|
107
|
-
}
|
|
108
|
-
// Gmail always needs an app password; plain SMTP may be auth-less (the send
|
|
109
|
-
// path omits `auth` when the key is empty). See providers.ts sendSmtp.
|
|
110
|
-
if (isGmail && apiKey === undefined) {
|
|
111
|
-
return {
|
|
112
|
-
config: null,
|
|
113
|
-
warning: 'provider `gmail` requires TOIL_EMAIL_API_KEY (a Gmail app password)',
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
const port = parseInt0(envOf(reserved, 'SMTP_PORT'), c.smtp?.port ?? 0) || 587;
|
|
117
|
-
const user = envOf(reserved, 'SMTP_USER') ?? c.smtp?.user?.trim() ?? from;
|
|
118
|
-
smtp = { host, port, user };
|
|
119
|
-
} else {
|
|
120
|
-
return {
|
|
121
|
-
config: null,
|
|
122
|
-
warning: `unknown email provider "${providerId}" (resend|gmail|smtp)`,
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return {
|
|
127
|
-
config: {
|
|
128
|
-
provider,
|
|
129
|
-
from,
|
|
130
|
-
apiKey: apiKey ?? '',
|
|
131
|
-
maxPerMin: parseInt0(envOf(reserved, 'MAX_PER_MIN'), c.maxPerMin ?? 60),
|
|
132
|
-
maxPerDay: parseInt0(envOf(reserved, 'MAX_PER_DAY'), c.maxPerDay ?? 0),
|
|
133
|
-
maxPerRecipientPerHour: parseInt0(
|
|
134
|
-
envOf(reserved, 'MAX_PER_RECIPIENT_PER_HOUR'),
|
|
135
|
-
c.maxPerRecipientPerHour ?? 5,
|
|
136
|
-
),
|
|
137
|
-
smtp,
|
|
138
|
-
},
|
|
139
|
-
warning: null,
|
|
140
|
-
};
|
|
141
|
-
}
|