create-substrat 0.13.25 → 0.14.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.
- package/package.json +1 -1
- package/project-files.js +10 -10
- package/template/AGENTS.md +10 -9
- package/template/src/server.ts +1 -1
- package/template/src/worker.ts +33 -150
- package/template/tsconfig.worker.json +1 -1
- package/template/src/config-do.ts +0 -86
package/package.json
CHANGED
package/project-files.js
CHANGED
|
@@ -30,12 +30,12 @@ import { DEV_SERVERS } from './dev-servers.js';
|
|
|
30
30
|
// The runtime packages release together off one version line (the changesets `fixed`
|
|
31
31
|
// group), so one constant is right for all of them. Engines do NOT share a line —
|
|
32
32
|
// each versions on its own, so one pin per engine, deliberately.
|
|
33
|
-
const SUBSTRAT = '^0.
|
|
34
|
-
const ENGINE_WORKORDER = '^0.13.
|
|
35
|
-
const ENGINE_INVOICING = '^0.11.
|
|
33
|
+
const SUBSTRAT = '^0.187.0';
|
|
34
|
+
const ENGINE_WORKORDER = '^0.13.12';
|
|
35
|
+
const ENGINE_INVOICING = '^0.11.71';
|
|
36
36
|
const BOUNDARY_LINT = '^0.8.2';
|
|
37
|
-
const DEV_ISSUER = '^0.
|
|
38
|
-
const ATTACHMENT_EXTRACTORS = '^0.2.
|
|
37
|
+
const DEV_ISSUER = '^0.4.0';
|
|
38
|
+
const ATTACHMENT_EXTRACTORS = '^0.2.48';
|
|
39
39
|
|
|
40
40
|
export const TSCONFIG = `${JSON.stringify(
|
|
41
41
|
{
|
|
@@ -56,7 +56,7 @@ export const TSCONFIG = `${JSON.stringify(
|
|
|
56
56
|
// under their own config (tsconfig.worker.json) — the node config must not
|
|
57
57
|
// see them. `src/routes.ts` is deliberately NOT excluded: the shared route
|
|
58
58
|
// table must typecheck under both, which is what keeps it host-agnostic.
|
|
59
|
-
exclude: ['src/worker.ts'
|
|
59
|
+
exclude: ['src/worker.ts'],
|
|
60
60
|
},
|
|
61
61
|
null,
|
|
62
62
|
2,
|
|
@@ -104,10 +104,10 @@ export function packageJson(name) {
|
|
|
104
104
|
{ binding: 'SCOPE', class: 'ScopeDO' },
|
|
105
105
|
// The scope-local sweep singleton — the deployment's own timer (#461).
|
|
106
106
|
{ binding: 'SWEEPER', class: 'SweeperDO' },
|
|
107
|
-
//
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
{ binding: '
|
|
107
|
+
// The tenant's identity directory: who is bound to what, the owner seat,
|
|
108
|
+
// and the settings the platform delivers (/internal/configure). One DO per
|
|
109
|
+
// tenant, rows per scope.
|
|
110
|
+
{ binding: 'AUTH', class: 'IdentityDO' },
|
|
111
111
|
],
|
|
112
112
|
},
|
|
113
113
|
// No envSpec copy here (#1206): the declared per-instance settings live in
|
package/template/AGENTS.md
CHANGED
|
@@ -50,7 +50,6 @@ src/personas.ts the dev cast, read by the issuer and the seed ← harness
|
|
|
50
50
|
src/routes.ts the routes, DERIVED from the operations ← harness
|
|
51
51
|
src/server.ts the dev entrypoint (node + persona picker) ← harness
|
|
52
52
|
src/worker.ts the deployable Cloudflare worker ← harness
|
|
53
|
-
src/config-do.ts per-instance config store (Cloudflare only) ← harness
|
|
54
53
|
test/scenario.test.ts the scenario — including the denials
|
|
55
54
|
test/entities.test.ts the registry, held to the tables it migrates
|
|
56
55
|
test/setup.ts the 50-byte LIKE/GLOB pattern limit, on by default (vitest setupFiles)
|
|
@@ -79,10 +78,12 @@ modules defined anywhere else will run locally and silently not deploy.
|
|
|
79
78
|
`worker.ts` **mounts** the platform's `/internal/*` management contract via
|
|
80
79
|
`mountPlatformSurface` from `@substrat-run/vertical-host` (one call — the routes
|
|
81
80
|
and the `{ error }` envelope are authored there, not here, so they can't drift or
|
|
82
|
-
ship half-done).
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
ship half-done). Sign-in is `workerDoor`, from `@substrat-run/vertical-host/worker-door`:
|
|
82
|
+
the issuer the dashboard delivers, the tenant's identity directory, and the owner seat
|
|
83
|
+
the first person takes. Every route gets its stub through `door.scope`, which hands an
|
|
84
|
+
operation the caller's verified address as `ctx.verifiedEmail`. There is no dev header to
|
|
85
|
+
forget to turn off. `src/server.ts` is the same shape, against the local OIDC issuer
|
|
86
|
+
`pnpm dev` starts.
|
|
86
87
|
|
|
87
88
|
**A UI ships as declared assets.** If `app/` exists, `substrat.runtimeNeeds.assets` must
|
|
88
89
|
point at its build output and `runtimeNeeds.build` must produce it — otherwise the deployed
|
|
@@ -90,15 +91,15 @@ vertical serves the API and 404s on `/`, with every local gate green. Declare it
|
|
|
90
91
|
change that creates `app/`, not at deploy time. (`substrat push` refuses an undeclared UI,
|
|
91
92
|
but by then you are already deploying.)
|
|
92
93
|
|
|
93
|
-
Among the hooks
|
|
94
|
+
Among the hooks `door.platform` passes, **`onConfigure` is the one you must not drop.** It is
|
|
94
95
|
how per-instance settings reach the running app: the dashboard's Settings → Env
|
|
95
96
|
and Identity tabs POST to `/internal/configure`, and a vertical that supplies no
|
|
96
97
|
hook answers **501** to that call for its whole life — the setting is saved, the
|
|
97
98
|
dashboard reports `delivered: false`, and the app never sees it. That includes the
|
|
98
99
|
`substrat:auth` issuer choice, i.e. the difference between a working login and
|
|
99
|
-
401-on-everything.
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
401-on-everything. Deliveries land in the tenant's identity directory, and
|
|
101
|
+
`door.settings(env, node)` reads them back. Read settings that way and never off
|
|
102
|
+
`env` directly: an `envSpec` default rides as a worker binding shared by
|
|
102
103
|
every install of one serving script, so `env.FOO` is the same string for every
|
|
103
104
|
tenant no matter what any of them saved. Declare a setting once, in
|
|
104
105
|
`src/manifest.ts` (`SHOP_ENV`) — `src/provision.ts` re-exports it as `envSpec`,
|
package/template/src/server.ts
CHANGED
|
@@ -91,7 +91,7 @@ app.get('/api/me', async (c) => {
|
|
|
91
91
|
async function stub(c: Context): Promise<ScopeStub> {
|
|
92
92
|
const caller = await login.caller(c.req.raw.headers);
|
|
93
93
|
if (!caller) throw new HTTPException(401, { message: 'unauthorized' });
|
|
94
|
-
return host.getScope(caller.principal, caller.tenantId, caller.scopeId);
|
|
94
|
+
return host.getScope(caller.principal, caller.tenantId, caller.scopeId, { verifiedEmail: caller.verifiedEmail });
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
// Everything else — including `/api/invoke` and the shared error envelope.
|
package/template/src/worker.ts
CHANGED
|
@@ -11,35 +11,16 @@
|
|
|
11
11
|
* package.json (entry = this file, stores = the DO classes exported here) —
|
|
12
12
|
* you never author wrangler config.
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* It used to resolve a caller through an `x-principal` header gated on
|
|
21
|
-
* ALLOW_DEV_HEADER. That is an impersonation bypass — a cross-tenant hole with a
|
|
22
|
-
* UI, one environment variable from being live — and it is gone. `src/server.ts`
|
|
23
|
-
* shows the shape you want instead: @substrat-run/vertical-auth's
|
|
24
|
-
* `oidcRpAuthProvider` verifies the request against your issuer and hands you a
|
|
25
|
-
* subject; an identity directory maps that subject to a PrincipalId. The dev
|
|
26
|
-
* server can use `host.admin` as that directory. A hosted worker needs a durable
|
|
27
|
-
* one — @substrat-run/vertical-auth's per-tenant `IdentityDO` is the platform's,
|
|
28
|
-
* and it also gives you the owner-claim and invite flows a new install needs.
|
|
29
|
-
* ───────────────────────────────────────────────────────────────────────────
|
|
14
|
+
* Sign-in is `workerDoor`'s, from @substrat-run/vertical-host: the issuer the dashboard delivers
|
|
15
|
+
* for this instance, the tenant's identity directory (the `AUTH` store), and the owner seat the
|
|
16
|
+
* first person takes. There is no credential store here and no dev header: a request names no
|
|
17
|
+
* caller, the session does. Member invites are yours to add: who may invite is your own rule, so
|
|
18
|
+
* mount `mountInviteRoutes` from @substrat-run/vertical-auth with your admin gate.
|
|
30
19
|
*/
|
|
31
20
|
import { Hono } from 'hono';
|
|
32
21
|
import type { Context } from 'hono';
|
|
33
22
|
import { HTTPException } from 'hono/http-exception';
|
|
34
|
-
import {
|
|
35
|
-
resolveScopedEnvSpec,
|
|
36
|
-
scopeId,
|
|
37
|
-
tenantId,
|
|
38
|
-
z,
|
|
39
|
-
type PrincipalId,
|
|
40
|
-
type ScopeId,
|
|
41
|
-
type TenantId,
|
|
42
|
-
} from '@substrat-run/contracts';
|
|
23
|
+
import { scopeId, tenantId, type ScopeId, type TenantId } from '@substrat-run/contracts';
|
|
43
24
|
import {
|
|
44
25
|
CloudflareScopeHost,
|
|
45
26
|
defineScopeDO,
|
|
@@ -56,21 +37,21 @@ import {
|
|
|
56
37
|
RouterAssertionError,
|
|
57
38
|
invocationLog,
|
|
58
39
|
} from '@substrat-run/vertical-host';
|
|
40
|
+
import { IdentityDO, workerDoor } from '@substrat-run/vertical-host/worker-door';
|
|
59
41
|
import { MODULES, OWNER_ROLE_KEY, ROLES, permissions } from './provision.js';
|
|
60
42
|
import { SHOP_ENV } from './manifest.js';
|
|
61
43
|
import { mountApi } from './routes.js';
|
|
62
|
-
import { AUTH_CONFIG_KEY, ConfigDO, type ConfigDo } from './config-do.js';
|
|
63
44
|
|
|
64
45
|
/** The scope-DO class = the app binary: kernel + engines + this vertical, bundled. */
|
|
65
46
|
export const ScopeDO = defineScopeDO(MODULES, {}, { permissions });
|
|
66
47
|
|
|
67
48
|
/**
|
|
68
|
-
* The
|
|
69
|
-
* Declared as a store in package.json `substrat.runtimeNeeds.stores`, like
|
|
70
|
-
* above and `SweeperDO` below. Re-exported because workerd resolves a DO class from the
|
|
71
|
-
* ENTRY module's exports
|
|
49
|
+
* The tenant's identity directory: who is bound to what, the owner seat, and the settings the
|
|
50
|
+
* platform delivers. Declared as a store in package.json `substrat.runtimeNeeds.stores`, like
|
|
51
|
+
* `ScopeDO` above and `SweeperDO` below. Re-exported because workerd resolves a DO class from the
|
|
52
|
+
* ENTRY module's exports.
|
|
72
53
|
*/
|
|
73
|
-
export {
|
|
54
|
+
export { IdentityDO };
|
|
74
55
|
|
|
75
56
|
/**
|
|
76
57
|
* The deployment's own timer (#461): a roster-keeping singleton whose alarm runs
|
|
@@ -117,8 +98,8 @@ interface Env {
|
|
|
117
98
|
SCOPE: DurableObjectNamespace;
|
|
118
99
|
/** The roster-keeping sweep singleton — the deployment's own timer (#461). */
|
|
119
100
|
SWEEPER: DurableObjectNamespace;
|
|
120
|
-
/**
|
|
121
|
-
|
|
101
|
+
/** The tenant's identity directory, and what the platform delivers to this instance. */
|
|
102
|
+
AUTH: DurableObjectNamespace<IdentityDO>;
|
|
122
103
|
/** Local `wrangler dev` only: when 'true', fall back to DEV_NODE if no router
|
|
123
104
|
* asserted a node. Addresses an instance; authenticates nobody. */
|
|
124
105
|
ALLOW_DEV_NODE?: string;
|
|
@@ -152,75 +133,17 @@ function hostFor(env: Env): CloudflareScopeHost {
|
|
|
152
133
|
return host;
|
|
153
134
|
}
|
|
154
135
|
|
|
155
|
-
/**
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
* The scope's delivered auth choice. Parsed LENIENTLY on purpose: an absent or
|
|
162
|
-
* malformed entry means "nothing delivered", never a throw, so a bad delivery can
|
|
163
|
-
* never lock an instance out of its own login.
|
|
164
|
-
*/
|
|
165
|
-
const authChoice = z.object({
|
|
166
|
-
mode: z.literal('oidc'),
|
|
167
|
-
issuer: z.string().min(1),
|
|
168
|
-
clientId: z.string().min(1).optional(),
|
|
169
|
-
clientSecret: z.string().min(1).optional(),
|
|
136
|
+
/** Who is calling, for every route below. It hands an operation the caller's verified address. */
|
|
137
|
+
const door = workerDoor<Env>({
|
|
138
|
+
directory: (env, node) => env.AUTH.get(env.AUTH.idFromName(node.tenantId)),
|
|
139
|
+
nodeFor,
|
|
140
|
+
hostFor,
|
|
141
|
+
envSpec: SHOP_ENV,
|
|
170
142
|
});
|
|
171
143
|
|
|
172
|
-
/**
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
* `substrat:auth` choice the dashboard's Identity tab sends.
|
|
176
|
-
*
|
|
177
|
-
* Reading settings THROUGH this — rather than off `env` — is the whole reason the
|
|
178
|
-
* `/internal/configure` hook exists: a spec `default` rides as a worker binding shared
|
|
179
|
-
* by every install of one serving script, so `env.SHOP_NAME` is the same string for
|
|
180
|
-
* every tenant no matter what any of them saved.
|
|
181
|
-
*/
|
|
182
|
-
async function instanceConfig(env: Env, node: Node) {
|
|
183
|
-
const delivered = await configDo(env, node).getScopeConfig(node.scopeId);
|
|
184
|
-
const settings = resolveScopedEnvSpec(SHOP_ENV, env as unknown as Record<string, unknown>, delivered).values;
|
|
185
|
-
let identity: z.infer<typeof authChoice> | null = null;
|
|
186
|
-
const raw = delivered[AUTH_CONFIG_KEY];
|
|
187
|
-
if (raw) {
|
|
188
|
-
try {
|
|
189
|
-
const parsed = authChoice.safeParse(JSON.parse(raw));
|
|
190
|
-
identity = parsed.success ? parsed.data : null;
|
|
191
|
-
} catch {
|
|
192
|
-
identity = null;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
return { settings, identity };
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* THE AUTH SEAM (see the header comment): resolve the caller to a PrincipalId, or
|
|
200
|
-
* null for nobody. Two steps, and this starter ships neither:
|
|
201
|
-
*
|
|
202
|
-
* 1. Verify the request → a subject. `instanceConfig` already reads the issuer
|
|
203
|
-
* the dashboard delivered as `substrat:auth`, so this is
|
|
204
|
-
* `oidcRpAuthProvider({ issuer, clientId, clientSecret, sessionSecret }).resolve(...)`
|
|
205
|
-
* — and you mount the same provider's `handle` on `/api/auth/*` for the login
|
|
206
|
-
* round-trip. `src/server.ts` does exactly this against the local dev issuer.
|
|
207
|
-
* 2. Map that subject → a PrincipalId, per scope. This needs a durable store the
|
|
208
|
-
* worker owns; @substrat-run/vertical-auth's `IdentityDO` is one, and carries
|
|
209
|
-
* the owner-claim (first sign-in takes the seat) and invite flows with it.
|
|
210
|
-
*
|
|
211
|
-
* Returning null unconditionally is the safe default, not an oversight: a starter
|
|
212
|
-
* that guessed here would be a starter that let the wrong person in.
|
|
213
|
-
*/
|
|
214
|
-
async function authenticatedPrincipal(_req: Request, _env: Env): Promise<PrincipalId | null> {
|
|
215
|
-
return null; // ← wire real auth here (see the two steps above)
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** Resolve caller + routed node → a scope stub. 401 if nobody. */
|
|
219
|
-
async function stub(c: Context<{ Bindings: Env }>): Promise<ScopeStub> {
|
|
220
|
-
const node = nodeFor(c.req.raw, c.env);
|
|
221
|
-
const principal = await authenticatedPrincipal(c.req.raw, c.env);
|
|
222
|
-
if (!principal) throw new HTTPException(401, { message: await unauthorizedReason(c.env, node) });
|
|
223
|
-
return hostFor(c.env).getScope(principal, node.tenantId, node.scopeId, {
|
|
144
|
+
/** The caller's scope stub. 401 if nobody. */
|
|
145
|
+
function stub(c: Context<{ Bindings: Env }>): Promise<ScopeStub> {
|
|
146
|
+
return door.scope(c, {
|
|
224
147
|
// An operation that called `ctx.requestPlatform`, or committed an event of a type this
|
|
225
148
|
// vertical EXPORTS (whether or not another app imports it yet), flags its response. The
|
|
226
149
|
// router then has the platform act on this scope within seconds instead of at the next
|
|
@@ -231,27 +154,6 @@ async function stub(c: Context<{ Bindings: Env }>): Promise<ScopeStub> {
|
|
|
231
154
|
});
|
|
232
155
|
}
|
|
233
156
|
|
|
234
|
-
/**
|
|
235
|
-
* Why the caller is nobody — a DIAGNOSIS, not a bare "unauthorized".
|
|
236
|
-
*
|
|
237
|
-
* The expensive case to debug is the one that looks like a platform bug and is not:
|
|
238
|
-
* a tenant picks an identity provider in the dashboard, the platform delivers it here
|
|
239
|
-
* successfully, and every request is still 401 — because this starter ships no auth.
|
|
240
|
-
* Saying so, and naming the seam, is the difference between a five-minute fix and a
|
|
241
|
-
* support thread. Only runs on the failure path, so it costs a DO hop on 401s alone.
|
|
242
|
-
*/
|
|
243
|
-
async function unauthorizedReason(env: Env, node: Node): Promise<string> {
|
|
244
|
-
try {
|
|
245
|
-
const { identity } = await instanceConfig(env, node);
|
|
246
|
-
if (identity) {
|
|
247
|
-
return `unauthorized — this instance has an identity provider configured (${identity.issuer}), but this vertical has not wired it up yet: implement \`authenticatedPrincipal\` in src/worker.ts (the auth seam)`;
|
|
248
|
-
}
|
|
249
|
-
} catch {
|
|
250
|
-
// The config store is unreachable — that is not the caller's problem to hear about.
|
|
251
|
-
}
|
|
252
|
-
return 'unauthorized';
|
|
253
|
-
}
|
|
254
|
-
|
|
255
157
|
const app = new Hono<{ Bindings: Env }>();
|
|
256
158
|
|
|
257
159
|
// FIRST, before any route: Hono composes handlers in registration order and stops at the
|
|
@@ -269,16 +171,9 @@ app.use(
|
|
|
269
171
|
}),
|
|
270
172
|
);
|
|
271
173
|
|
|
272
|
-
//
|
|
273
|
-
//
|
|
274
|
-
|
|
275
|
-
app.get('/api/me', async (c) => {
|
|
276
|
-
const node = nodeFor(c.req.raw, c.env);
|
|
277
|
-
const principal = await authenticatedPrincipal(c.req.raw, c.env);
|
|
278
|
-
if (!principal) return c.json({ error: await unauthorizedReason(c.env, node) }, 401);
|
|
279
|
-
const { settings, identity } = await instanceConfig(c.env, node);
|
|
280
|
-
return c.json({ principal, settings, identity: identity ? { issuer: identity.issuer } : null });
|
|
281
|
-
});
|
|
174
|
+
// Sign-in: the login round-trip at `/api/auth/*`, who is signed in at `/api/me`, and the owner
|
|
175
|
+
// seat at `/api/claim-owner`. Accounts and passwords live at the issuer, not here.
|
|
176
|
+
door.mount(app);
|
|
282
177
|
|
|
283
178
|
// ── The vertical's API — the SAME table `server.ts` mounts (src/routes.ts) ───
|
|
284
179
|
// Including `/api/invoke`. Mounted BEFORE the platform surface: Hono keeps only the
|
|
@@ -293,35 +188,23 @@ mountApi(app, stub);
|
|
|
293
188
|
// and bookmarks/rewind — plus the guaranteed { error } envelope is authored ONCE
|
|
294
189
|
// in @substrat-run/vertical-host (issue #510); mount it and it cannot drift.
|
|
295
190
|
//
|
|
296
|
-
//
|
|
297
|
-
//
|
|
298
|
-
//
|
|
299
|
-
// (that lives with real auth, the auth seam), so `resolveOwner` is omitted. The platform's
|
|
300
|
-
// reconcile then only seats what each version declares (schedule, peer and connection
|
|
301
|
-
// grants) and re-grants no owner, until you wire auth and supply one.
|
|
191
|
+
// The hooks keep the deployment's sweep roster (#461) in step: a newly provisioned scope joins it
|
|
192
|
+
// (so its schedules run), and a deleted one leaves it. The door's hooks do the rest: the owner seat
|
|
193
|
+
// at provision, and the settings the dashboard's Env and Identity tabs deliver.
|
|
302
194
|
mountPlatformSurface<Env>(app, {
|
|
303
195
|
platformSecret: (env) => env.PLATFORM_SECRET,
|
|
304
196
|
hostFor,
|
|
305
197
|
roles: ROLES,
|
|
306
198
|
ownerRoleKey: OWNER_ROLE_KEY,
|
|
307
|
-
|
|
308
|
-
// returning it here would make the platform's response shape depend on what a
|
|
309
|
-
// vertical happens to hand back. Await, discard.
|
|
199
|
+
...door.platform,
|
|
310
200
|
onProvision: async (env, b) => {
|
|
201
|
+
await door.platform.onProvision(env, b);
|
|
311
202
|
await sweeper(env).noteScope(b.tenantId, b.scopeId);
|
|
312
203
|
},
|
|
313
|
-
onDeleteScope: async (env, s) => {
|
|
204
|
+
onDeleteScope: async (env, s, tenantId) => {
|
|
205
|
+
await door.platform.onDeleteScope(env, s, tenantId);
|
|
314
206
|
await sweeper(env).forgetScope(s);
|
|
315
207
|
},
|
|
316
|
-
// Per-instance config delivery (the dashboard's Settings → Env and Identity tabs).
|
|
317
|
-
// WITHOUT this hook `/internal/configure` answers 501 for the life of the app: the
|
|
318
|
-
// dashboard saves the setting, reports `delivered: false`, and the running worker
|
|
319
|
-
// never sees it — including the `substrat:auth` issuer choice that is the difference
|
|
320
|
-
// between a working login and 401-on-everything. Store it; `instanceConfig` reads it
|
|
321
|
-
// back. Idempotent, so the platform's reconciliation sweep can re-deliver safely.
|
|
322
|
-
onConfigure: async (env, b) => {
|
|
323
|
-
await configDo(env, { tenantId: b.tenantId, scopeId: b.scopeId }).setScopeConfig(b.scopeId, b.entries);
|
|
324
|
-
},
|
|
325
208
|
});
|
|
326
209
|
|
|
327
210
|
// Unmatched /api/* fails as JSON; everything else gets a pointer, not a UI —
|
|
@@ -9,5 +9,5 @@
|
|
|
9
9
|
"skipLibCheck": true,
|
|
10
10
|
"noEmit": true
|
|
11
11
|
},
|
|
12
|
-
"include": ["src/worker.ts", "src/routes.ts", "src/
|
|
12
|
+
"include": ["src/worker.ts", "src/routes.ts", "src/provision.ts", "src/manifest.ts", "src/migrations.ts", "src/module.ts"]
|
|
13
13
|
}
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { DurableObject } from 'cloudflare:workers';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The key the platform delivers a scope's identity-provider choice under.
|
|
5
|
-
*
|
|
6
|
-
* It lives HERE rather than in `worker.ts` for a runtime reason worth knowing: workerd
|
|
7
|
-
* requires every named export of the entry module to be a handler or a Durable Object
|
|
8
|
-
* class, so exporting a plain constant from `worker.ts` makes the whole worker fail to
|
|
9
|
-
* boot — with a `tsc`-clean tree and a green test suite. Config vocabulary belongs with
|
|
10
|
-
* the config store anyway.
|
|
11
|
-
*/
|
|
12
|
-
export const AUTH_CONFIG_KEY = 'substrat:auth';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The vertical's own per-instance CONFIG store — the durable half of
|
|
16
|
-
* `/internal/configure`.
|
|
17
|
-
*
|
|
18
|
-
* The platform delivers per-install settings (the dashboard's Settings → Env and
|
|
19
|
-
* Identity tabs) by POSTing them to `/internal/configure` on this worker. A vertical
|
|
20
|
-
* that supplies no `onConfigure` hook answers 501 to that call for its whole life:
|
|
21
|
-
* the dashboard records the setting, reports `delivered: false`, and the running app
|
|
22
|
-
* never sees it. This DO is what makes the hook answerable.
|
|
23
|
-
*
|
|
24
|
-
* It is a HARNESS store, not module code — the config a scope runs on is not domain
|
|
25
|
-
* data, and it must survive a scope-DO storage wipe (a restore, a rebind), so it
|
|
26
|
-
* deliberately lives outside the scope's own DO. One DO per TENANT, rows keyed by
|
|
27
|
-
* scope; the table matches `scope_config` in `@substrat-run/vertical-auth`'s
|
|
28
|
-
* `IdentityDO` exactly, so a project that later adopts vertical-auth for real logins
|
|
29
|
-
* swaps the binding and keeps its rows.
|
|
30
|
-
*
|
|
31
|
-
* Sandbox-clean (D-18): this is one of the deployment's OWN Durable Object classes,
|
|
32
|
-
* declared in package.json `substrat.runtimeNeeds.stores`. No control-plane binding,
|
|
33
|
-
* no service binding — the platform refuses those.
|
|
34
|
-
*/
|
|
35
|
-
export class ConfigDO extends DurableObject<Record<string, never>> {
|
|
36
|
-
private ready = false;
|
|
37
|
-
|
|
38
|
-
/** Lazily create the table — cheaper than a blockConcurrencyWhile on every wake. */
|
|
39
|
-
private init(): void {
|
|
40
|
-
if (this.ready) return;
|
|
41
|
-
this.ctx.storage.sql.exec(
|
|
42
|
-
`CREATE TABLE IF NOT EXISTS scope_config (
|
|
43
|
-
scope_id TEXT NOT NULL, key TEXT NOT NULL, value TEXT NOT NULL,
|
|
44
|
-
PRIMARY KEY (scope_id, key))`,
|
|
45
|
-
);
|
|
46
|
-
this.ready = true;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Upsert config delivered for one scope. Key-by-key rather than a replace, so a
|
|
51
|
-
* partial delivery composes with what is already there; idempotent, so the
|
|
52
|
-
* platform's reconciliation sweep can re-run it safely.
|
|
53
|
-
*/
|
|
54
|
-
async setScopeConfig(scopeId: string, entries: Array<{ key: string; value: string }>): Promise<void> {
|
|
55
|
-
this.init();
|
|
56
|
-
for (const { key, value } of entries) {
|
|
57
|
-
this.ctx.storage.sql.exec(
|
|
58
|
-
'INSERT OR REPLACE INTO scope_config (scope_id, key, value) VALUES (?, ?, ?)',
|
|
59
|
-
scopeId, key, value,
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* The config delivered to one scope, as a plain map — exactly the `delivered`
|
|
66
|
-
* argument of `resolveScopedEnvSpec(spec, env, delivered)`. Reading it back is what
|
|
67
|
-
* keeps the hook from being write-only: an env-spec key set per install must be
|
|
68
|
-
* overlaid here, because env-spec defaults ride as worker bindings SHARED by every
|
|
69
|
-
* install of one serving script, so reading `env` alone always yields the shared
|
|
70
|
-
* default no matter what the tenant saved.
|
|
71
|
-
*/
|
|
72
|
-
async getScopeConfig(scopeId: string): Promise<Record<string, string>> {
|
|
73
|
-
this.init();
|
|
74
|
-
const config: Record<string, string> = {};
|
|
75
|
-
for (const row of this.ctx.storage.sql.exec('SELECT key, value FROM scope_config WHERE scope_id = ?', scopeId)) {
|
|
76
|
-
config[row.key as string] = row.value as string;
|
|
77
|
-
}
|
|
78
|
-
return config;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** The typed stub surface — what `worker.ts` calls across the DO boundary. */
|
|
83
|
-
export interface ConfigDo {
|
|
84
|
-
setScopeConfig(scopeId: string, entries: Array<{ key: string; value: string }>): Promise<void>;
|
|
85
|
-
getScopeConfig(scopeId: string): Promise<Record<string, string>>;
|
|
86
|
-
}
|