@runeditrun/rer 0.2.80 → 0.2.82
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/README.md +12 -0
- package/cli/deploy-needs.mjs +4 -2
- package/cli/export.mjs +1 -1
- package/cli/ledger-refusal.mjs +18 -0
- package/cli/portability-migrations.mjs +30 -11
- package/cli/portability-worker.mjs +3 -1
- package/cli/portability.mjs +2 -0
- package/cli/release.mjs +4 -4
- package/cli/site-hosting.mjs +90 -0
- package/cli/site-render.mjs +3 -2
- package/cli/site.mjs +5 -2
- package/edition-authoring/base-copies.mjs +12 -1
- package/edition-authoring/copy-checkout.mjs +152 -1
- package/edition-authoring/files.mjs +1 -1
- package/edition-authoring/runtime-dependencies.mjs +108 -0
- package/global/CHANGELOG.md +25 -0
- package/global/README.md +12 -0
- package/modules/demo/README.md +8 -0
- package/modules/demo/navigation.mjs +131 -15
- package/modules/dependencies/auth.mjs +2 -2
- package/modules/deploy-kit/owner.mjs +8 -1
- package/modules/import-kit/src/d1/apply-effects.ts +33 -0
- package/modules/import-kit/src/d1/apply.ts +38 -10
- package/modules/import-kit/src/door/ADOPTING.md +452 -0
- package/modules/import-kit/src/door/README.md +553 -0
- package/modules/import-kit/src/door/admission.ts +45 -0
- package/modules/import-kit/src/door/archive.ts +129 -0
- package/modules/import-kit/src/door/authority.ts +107 -0
- package/modules/import-kit/src/door/failures.ts +70 -0
- package/modules/import-kit/src/door/grants.ts +162 -0
- package/modules/import-kit/src/door/handler.ts +90 -0
- package/modules/import-kit/src/door/index.ts +20 -0
- package/modules/import-kit/src/door/intake/csv.ts +74 -0
- package/modules/import-kit/src/door/intake/json.ts +182 -0
- package/modules/import-kit/src/door/intake/projection.ts +61 -0
- package/modules/import-kit/src/door/intake/types.ts +28 -0
- package/modules/import-kit/src/door/intake-policy.ts +38 -0
- package/modules/import-kit/src/door/intake-upload.ts +91 -0
- package/modules/import-kit/src/door/inventory.ts +68 -0
- package/modules/import-kit/src/door/jobs.ts +134 -0
- package/modules/import-kit/src/door/limits.ts +16 -0
- package/modules/import-kit/src/door/operator.ts +41 -0
- package/modules/import-kit/src/door/planning.ts +227 -0
- package/modules/import-kit/src/door/protocol.ts +38 -0
- package/modules/import-kit/src/door/refusals.ts +51 -0
- package/modules/import-kit/src/door/schema.ts +101 -0
- package/modules/import-kit/src/door/store.ts +152 -0
- package/modules/import-kit/src/door/types.ts +100 -0
- package/modules/import-kit/src/door/uploads.ts +98 -0
- package/modules/import-kit/src/provenance/numeric-credentials.ts +40 -0
- package/modules/import-kit/src/readers/README.md +139 -0
- package/modules/import-kit/src/readers/api-policy.ts +67 -0
- package/modules/import-kit/src/readers/api.ts +86 -0
- package/modules/import-kit/src/readers/bytes.ts +73 -0
- package/modules/import-kit/src/readers/errors.ts +33 -0
- package/modules/import-kit/src/readers/file.ts +74 -0
- package/modules/import-kit/src/readers/index.ts +3 -0
- package/modules/import-kit/src/readers/json.ts +29 -0
- package/modules/import-kit/src/readers/numeric-lexemes.ts +104 -0
- package/modules/import-kit/src/readers/original.ts +31 -0
- package/modules/import-kit/src/readers/records.ts +105 -0
- package/modules/import-kit/src/types.ts +3 -1
- package/modules/operator-auth/README.md +92 -3
- package/modules/operator-auth/accounts/admin.mjs +52 -20
- package/modules/operator-auth/accounts/audit.mjs +24 -0
- package/modules/operator-auth/accounts/business-team.mjs +91 -0
- package/modules/operator-auth/accounts/domain-audit.mjs +24 -0
- package/modules/operator-auth/accounts/native.mjs +3 -1
- package/modules/operator-auth/accounts/settings-page.mjs +15 -2
- package/modules/operator-auth/accounts/status.mjs +7 -2
- package/modules/operator-auth/accounts/store.mjs +32 -6
- package/modules/operator-auth/business-settings.mjs +40 -0
- package/modules/operator-auth/identity/email-link/index.mjs +1 -1
- package/modules/operator-auth/identity/oidc/admin-audit.mjs +31 -0
- package/modules/operator-auth/identity/oidc/index.mjs +12 -5
- package/modules/operator-auth/identity/oidc/store.mjs +2 -0
- package/modules/operator-auth/index.d.mts +40 -1
- package/modules/operator-auth/routes.mjs +11 -6
- package/modules/operator-auth/service.mjs +8 -3
- package/modules/operator-auth/signin/index.mjs +1 -1
- package/modules/portability/README.md +82 -1
- package/modules/portability/export.mjs +6 -4
- package/modules/portability/format.mjs +5 -0
- package/modules/portability/index.d.mts +10 -2
- package/modules/portability/index.mjs +3 -2
- package/modules/portability/ledgers.mjs +27 -4
- package/modules/release/README.md +74 -2
- package/modules/release/sign.mjs +4 -2
- package/modules/release/signature.mjs +14 -7
- package/modules/release/signing-cli.mjs +2 -1
- package/modules/release/statement.mjs +2 -2
- package/modules/release/vectors/generate.mjs +1 -0
- package/modules/release/vectors/publisher-projects.mjs +133 -0
- package/modules/release/vectors/release-signature-vectors.json +5195 -0
- package/modules/release/verify.mjs +5 -1
- package/onboarding/schema/SETUP.schema.json +43 -1
- package/onboarding/schema/business-field-inputs.mjs +0 -1
- package/onboarding/schema/setup-validation.mjs +3 -0
- package/package.json +3 -2
|
@@ -8,7 +8,7 @@ import { isLocalCli } from '../../local-cli.mjs';
|
|
|
8
8
|
import { linkMessage } from './message.mjs';
|
|
9
9
|
import { completionHook } from '../after-confirm.mjs';
|
|
10
10
|
import { demoEffectsEnabled } from '../../../demo/effects.mjs';
|
|
11
|
-
export const STEP_UP_ACTIONS = Object.freeze(['providers:unlink', 'providers:relink', 'domains:change', 'owner:transfer', 'export', 'delete', 'credentials:mint', 'email:change']);
|
|
11
|
+
export const STEP_UP_ACTIONS = Object.freeze(['providers:unlink', 'providers:relink', 'domains:change', 'owner:transfer', 'export', 'delete', 'credentials:mint', 'email:change', 'business:stop']);
|
|
12
12
|
export const STEP_UP_SECONDS = 600;
|
|
13
13
|
|
|
14
14
|
// Persistence owns the atomic authority check, proof consumption and session
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { auditMutation } from '../../accounts/audit.mjs';
|
|
2
|
+
|
|
3
|
+
// Only the authorized OA administration door supplies this context. Provider
|
|
4
|
+
// discovery/first-use pinning and native account adapters retain their own paths.
|
|
5
|
+
export async function adminProviderChange(db, id, provider, metadata, previous, { audit, actorId, fence, now }) {
|
|
6
|
+
const store = { batch: statements => db.batch(statements.map(([sql, args]) => db.prepare(sql).bind(...args))) };
|
|
7
|
+
const snapshot = previous
|
|
8
|
+
? { sql: 'EXISTS(SELECT 1 FROM oa_identity_providers WHERE provider_id=? AND linked_at=? AND status=?)', args: [id, previous.linkedAt, previous.status] }
|
|
9
|
+
: { sql: 'NOT EXISTS(SELECT 1 FROM oa_identity_providers WHERE provider_id=?)', args: [id] };
|
|
10
|
+
const revision = previous ? Math.max(previous.linkedAt + 1, now) : now;
|
|
11
|
+
const statements = [];
|
|
12
|
+
if (provider) {
|
|
13
|
+
const args = [provider.preset, metadata.issuer, provider.clientId, metadata.jwks, metadata.authorization, metadata.token, JSON.stringify(metadata.algorithms), provider.tokenAuthMethod, metadata.responseIssuer ? 1 : 0, metadata.interfaceVersion, revision, id];
|
|
14
|
+
statements.push(previous
|
|
15
|
+
? [`UPDATE oa_identity_providers SET preset=?,status='linked',issuer=?,client_id=?,jwks_uri=?,authorization_endpoint=?,token_endpoint=?,algorithms=?,token_auth_method=?,response_iss=?,interface_version=?,linked_at=?,pending_issuer=NULL,pending_client_id=NULL,pending_preset=NULL,pending_at=NULL,jwks=NULL,jwks_fetched_at=NULL WHERE provider_id=? RETURNING provider_id`, args]
|
|
16
|
+
: [`INSERT INTO oa_identity_providers(preset,issuer,client_id,jwks_uri,authorization_endpoint,token_endpoint,algorithms,token_auth_method,response_iss,interface_version,linked_at,provider_id,status) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,'linked') RETURNING provider_id`, args]);
|
|
17
|
+
} else {
|
|
18
|
+
// Advance this pin's generation too: its old unlinked status cannot witness
|
|
19
|
+
// this request, and trigger side effects on other rows prove nothing here.
|
|
20
|
+
statements.push(["UPDATE oa_identity_providers SET status='unlinked',linked_at=? WHERE provider_id=? AND linked_at=? AND status='linked' RETURNING provider_id", [revision, id, previous.linkedAt]],
|
|
21
|
+
['DELETE FROM oa_domain_rules WHERE provider_id=?', [id]]);
|
|
22
|
+
}
|
|
23
|
+
statements.push([`DELETE FROM oa_identity_provider_health WHERE provider_id LIKE ? AND provider_id NOT IN
|
|
24
|
+
(SELECT 'exchange:' || provider_id || ':' || linked_at FROM oa_identity_providers WHERE provider_id=? AND status='linked')`, [`exchange:${id}:%`, id]]);
|
|
25
|
+
return auditMutation(store, { audit,
|
|
26
|
+
event: { action: provider ? 'providers.relink' : 'providers.unlink', actorId, subjectId: null, detail: id, now },
|
|
27
|
+
before: { sql: `${fence.sql} AND ${snapshot.sql}`, args: [...fence.args, ...snapshot.args] },
|
|
28
|
+
after: { sql: 'EXISTS(SELECT 1 FROM oa_identity_providers WHERE provider_id=? AND linked_at=? AND status=?)', args: [id, revision, provider ? 'linked' : 'unlinked'] },
|
|
29
|
+
statements,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -257,19 +257,26 @@ export function createOidcProviders({ admission, db, config: input, clientIp, fe
|
|
|
257
257
|
return result;
|
|
258
258
|
},
|
|
259
259
|
/** Owner actions; the caller requires a fresh step-up proof first. */
|
|
260
|
-
async relink(id) {
|
|
260
|
+
async relink(id, audit) {
|
|
261
261
|
const provider = providers.get(id);
|
|
262
262
|
check(provider, 'oidc_unknown_provider', 404);
|
|
263
|
+
const previous = audit ? await store.link(id) : undefined;
|
|
263
264
|
const metadata = await observe(provider, () => discover(fetcher, provider, config));
|
|
264
|
-
if (
|
|
265
|
+
if (audit) await store.adminChange(id, provider, metadata, previous, audit);
|
|
266
|
+
else if (await store.link(id)) await store.relink(id, provider, metadata, clock());
|
|
265
267
|
else await store.pin(id, provider, metadata, clock());
|
|
266
268
|
return store.link(id);
|
|
267
269
|
},
|
|
268
|
-
async unlink(id) {
|
|
270
|
+
async unlink(id, audit) {
|
|
269
271
|
check(providers.has(id), 'oidc_unknown_provider', 404);
|
|
270
272
|
// Only a pin can be ended; a provider never linked has nothing to unlink.
|
|
271
|
-
|
|
272
|
-
|
|
273
|
+
const previous = await store.link(id);
|
|
274
|
+
check(previous, 'oidc_not_linked', 409);
|
|
275
|
+
// Preserve the successful response for an ended pin, without an audit
|
|
276
|
+
// callback or another mutation. The unaudited path keeps its behavior.
|
|
277
|
+
if (audit && previous.status === 'unlinked') return;
|
|
278
|
+
if (audit) await store.adminChange(id, null, null, previous, audit);
|
|
279
|
+
else await store.unlink(id);
|
|
273
280
|
},
|
|
274
281
|
async links() {
|
|
275
282
|
const [links, health] = await Promise.all([store.links(), store.health()]);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { adminProviderChange } from './admin-audit.mjs';
|
|
1
2
|
import { check, fail } from './errors.mjs';
|
|
2
3
|
|
|
3
4
|
const linkColumns = `provider_id AS id, preset, status, issuer, client_id AS clientId, jwks_uri AS jwks, authorization_endpoint AS authorization, token_endpoint AS token,
|
|
@@ -14,6 +15,7 @@ export function storage(db) {
|
|
|
14
15
|
(SELECT 'exchange:' || provider_id || ':' || linked_at FROM oa_identity_providers WHERE provider_id=? AND status='linked')`).bind(`exchange:${id}:%`, id);
|
|
15
16
|
const guarded = async operation => { try { return await operation(); } catch { fail('oidc_storage_unavailable', 503); } };
|
|
16
17
|
return {
|
|
18
|
+
adminChange: (...args) => adminProviderChange(db, ...args),
|
|
17
19
|
async limit(bucket, expiresAt, maximum) {
|
|
18
20
|
try {
|
|
19
21
|
const row = await db.prepare('INSERT INTO oa_oidc_limits (bucket,count,expires_at) VALUES (?,1,?) ON CONFLICT(bucket) DO UPDATE SET count=count+1 RETURNING count').bind(bucket, expiresAt).first();
|
|
@@ -10,7 +10,7 @@ export interface AuthPresentation { readonly productName: string; readonly accen
|
|
|
10
10
|
export interface LinkPreview { kind: 'magic' | 'invite' | 'step_up' | 'owner'; expiresInMinutes: number }
|
|
11
11
|
/** A step-up confirmation proves the session's operator again; it issues no new session. */
|
|
12
12
|
export interface StepUpConfirmed { stepUp: StepUpAction; returnTo: string; cookie?: undefined }
|
|
13
|
-
export type StepUpAction = 'providers:unlink' | 'providers:relink' | 'domains:change' | 'owner:transfer' | 'export' | 'delete' | 'credentials:mint' | 'email:change';
|
|
13
|
+
export type StepUpAction = 'providers:unlink' | 'providers:relink' | 'domains:change' | 'owner:transfer' | 'export' | 'delete' | 'credentials:mint' | 'email:change' | 'business:stop';
|
|
14
14
|
export const STEP_UP_ACTIONS: readonly StepUpAction[];
|
|
15
15
|
export type ProviderPreset = 'auth' | 'google' | 'microsoft' | 'okta' | 'legacy-rs256';
|
|
16
16
|
/** One identity provider. Secrets and the private key are server bindings, never source text. */
|
|
@@ -84,14 +84,47 @@ export interface VerifiedIdentity extends VerifiedPrincipal { email: string; ema
|
|
|
84
84
|
/** Deployment-owned association, never populated from token claims or HTTP input. */
|
|
85
85
|
export interface IdentityAdmissionConfig { issuer: string; subjectEmails?: Record<string, string> }
|
|
86
86
|
export type IdentityAdmission<R extends string = Role> = (request: Request, identity: VerifiedPrincipal, input?: { returnTo?: string }) => Promise<CompletedSession<R>>;
|
|
87
|
+
export type AuthAuditAction = 'operators.invite' | 'operators.cancel-invite' | 'operators.deactivate' | 'operators.reactivate' | 'operators.role' | 'sessions.revoke' | 'domains.rule.set' | 'domains.rule.lift' | 'providers.unlink' | 'providers.relink';
|
|
88
|
+
/** OA administrative mutation metadata only; no private identity, record or file data. */
|
|
89
|
+
export interface AuthAuditEvent {
|
|
90
|
+
action: AuthAuditAction;
|
|
91
|
+
/** Opaque oa_operators.id of the authenticated actor. */
|
|
92
|
+
actorId: string;
|
|
93
|
+
/** Target oa_operators.id; null for domain and provider actions. */
|
|
94
|
+
subjectId: string | null;
|
|
95
|
+
/** New role/status, normalized domain, provider id, or null for session revocation. */
|
|
96
|
+
detail: string | null;
|
|
97
|
+
/** Action clock time in milliseconds since the Unix epoch. */
|
|
98
|
+
now: number;
|
|
99
|
+
}
|
|
100
|
+
export type AuthAuditStatement = [sql: string, args: unknown[]];
|
|
87
101
|
export interface AuthHooks<R extends string = Role> {
|
|
88
102
|
/** Before an email link is sent to an active operator. Return false to send nothing; the public answer is unchanged. */
|
|
89
103
|
beforeMagicLink?(input: { request: Request; email: string; operator: Operator<R> }): boolean | void | Promise<boolean | void>;
|
|
90
104
|
/** After an email-link or provider sign-in completes. Return a returnTo to redirect; throw to refuse (the new session is revoked). */
|
|
91
105
|
afterConfirm?(input: { request: Request; operator: Operator<R>; session: CompletedSession<R>['session']; method: 'email' | 'oidc'; returnTo: string }): void | { returnTo?: string } | Promise<void | { returnTo?: string }>;
|
|
106
|
+
/** OA administration only: pure SQL generation for the same mutation batch. Failure refuses the action; no external writes. */
|
|
107
|
+
audit?(input: AuthAuditEvent): AuthAuditStatement[] | Promise<AuthAuditStatement[]>;
|
|
92
108
|
}
|
|
93
109
|
export type { ScheduledPolicy, ScheduledPolicyDoor, PolicyApproval, PolicyRun } from './scheduled/index.mjs';
|
|
110
|
+
export interface BusinessSettingsIntegration {
|
|
111
|
+
publicBrand(): Promise<{ displayName?: string; logo?: string }>;
|
|
112
|
+
read(): Promise<{ document: { business: string } | null; rev: number; appliedAt: string | null; stopped: boolean }>;
|
|
113
|
+
/** The OA actor fence is supplied for an atomic owner-authority check. */
|
|
114
|
+
stop(actor: string, guard?: { sql: string; args: unknown[] }): Promise<void>;
|
|
115
|
+
}
|
|
116
|
+
export interface BusinessTeamRow { email: string; operatorId: string | null; invite: number; state: 'invited' | 'active' | 'inactive' | 'conflict' }
|
|
117
|
+
export interface BusinessTeamAdapter {
|
|
118
|
+
ownerEmail(): Promise<string | null>;
|
|
119
|
+
prepare(input: {
|
|
120
|
+
members: Array<{ email: string; name: string; role: 'admin' | 'member'; invite: number }>;
|
|
121
|
+
managed: BusinessTeamRow[]; ownerEmail: string; roles: { admin: string; member: string };
|
|
122
|
+
/** Evaluated after the Business revision/nonce CAS in the same D1 batch. */
|
|
123
|
+
guard: { sql: string; args: unknown[] };
|
|
124
|
+
}): Promise<{ statements: Statement[]; rows: BusinessTeamRow[]; afterCommit(): Promise<void> }>;
|
|
125
|
+
}
|
|
94
126
|
export interface AuthOptions<R extends string = Role> {
|
|
127
|
+
businessSettings?: BusinessSettingsIntegration;
|
|
95
128
|
/** OA store only: trusted source, re-read at approval/run/action time. */
|
|
96
129
|
scheduledPolicies?: () => import('./scheduled/index.mjs').ScheduledPolicy[];
|
|
97
130
|
db: Database; config: AuthConfig; mailSender?: MailSender; clientIp: (request: Request) => string | Promise<string>; rolePolicy?: RolePolicy; clock?: () => number;
|
|
@@ -145,6 +178,12 @@ export interface IdentityProviders {
|
|
|
145
178
|
}
|
|
146
179
|
export interface DomainRule { domain: string; providerId: string; createdAt: number }
|
|
147
180
|
export interface OperatorAuth<R extends string = Role> {
|
|
181
|
+
/** OA accounts only. Signed Business documents must be verified before calling. */
|
|
182
|
+
readonly businessTeam?: BusinessTeamAdapter;
|
|
183
|
+
readonly businessSettings?: BusinessSettingsIntegration;
|
|
184
|
+
publicPresentation?(): Promise<AuthPresentation>;
|
|
185
|
+
businessSettingsInfo?(request: Request): Promise<{ business: string | null; rev: number; appliedAt: string | null; stopped: boolean }>;
|
|
186
|
+
stopBusinessSettings?(request: Request): Promise<{ stopped: true }>;
|
|
148
187
|
readonly scheduledPolicies?: import('./scheduled/index.mjs').ScheduledPolicyDoor;
|
|
149
188
|
/** Key-gated local development or an isolated hosted demo; unavailable in production. */
|
|
150
189
|
readonly oneClick?: {
|
|
@@ -67,14 +67,14 @@ export function createAuthHandler(auth) {
|
|
|
67
67
|
if (path === '/auth/sessions' && request.method === 'GET') return json(await auth.listSessions(request, readParameters(url.searchParams).operatorId));
|
|
68
68
|
if (path === '/auth/domain-rules' && request.method === 'GET') return json(await auth.listDomainRules(request));
|
|
69
69
|
if (path === '/auth/settings/identity' && ['GET', 'HEAD'].includes(request.method)) {
|
|
70
|
-
const value = await auth.identitySettings(request);
|
|
70
|
+
const value = { ...await auth.identitySettings(request), ...(auth.businessSettingsInfo ? { business: await auth.businessSettingsInfo(request) } : {}) };
|
|
71
71
|
return representation(request) === 'html'
|
|
72
|
-
? await identitySettingsPage(value, auth.presentation, { saved: url.searchParams.get('saved') === '1', method: request.method })
|
|
72
|
+
? await identitySettingsPage(value, auth.publicPresentation ? await auth.publicPresentation() : auth.presentation, { saved: url.searchParams.get('saved') === '1', method: request.method })
|
|
73
73
|
: request.method === 'HEAD' ? new Response(null, { headers: { ...securityHeaders(), 'content-type': 'application/json' } }) : json(value);
|
|
74
74
|
}
|
|
75
75
|
if (path === '/auth/providers' && request.method === 'GET') return json(await auth.listProviders(request));
|
|
76
76
|
const posts = new Set(['/auth/bootstrap', '/auth/magic-link', '/auth/confirm', '/auth/signout', '/auth/invite', '/auth/invite/cancel', '/auth/review-session',
|
|
77
|
-
'/auth/policy/approve', '/auth/policy/revoke', '/auth/operator/status', '/auth/operator/role', '/auth/session/revoke', '/auth/step-up', '/auth/domain-rules', '/auth/providers/unlink', '/auth/providers/relink', '/auth/settings/identity', '/auth/setup-complete', OWNER_SIGNIN_PATH, '/auth/email', '/auth/display-name']);
|
|
77
|
+
'/auth/policy/approve', '/auth/policy/revoke', '/auth/operator/status', '/auth/operator/role', '/auth/session/revoke', '/auth/step-up', '/auth/domain-rules', '/auth/providers/unlink', '/auth/providers/relink', '/auth/settings/identity', '/auth/settings/business', '/auth/setup-complete', OWNER_SIGNIN_PATH, '/auth/email', '/auth/display-name']);
|
|
78
78
|
if (!posts.has(path)) return json({ error: 'not_found' }, 404);
|
|
79
79
|
if (request.method !== 'POST') return json({ error: 'method_not_allowed' }, 405);
|
|
80
80
|
const query = readParameters(url.searchParams);
|
|
@@ -91,6 +91,11 @@ export function createAuthHandler(auth) {
|
|
|
91
91
|
const value = await auth.setIdentitySettings(request, formPost(request) ? { idpSignIn: input.idpSignIn === 'true' } : input);
|
|
92
92
|
return formPost(request) ? seeOther('/auth/settings/identity?saved=1') : json(value);
|
|
93
93
|
}
|
|
94
|
+
case '/auth/settings/business': {
|
|
95
|
+
if (!auth.stopBusinessSettings) return json({ error: 'not_found' }, 404);
|
|
96
|
+
const value = await auth.stopBusinessSettings(request);
|
|
97
|
+
return formPost(request) ? seeOther('/auth/settings/identity') : json(value);
|
|
98
|
+
}
|
|
94
99
|
case '/auth/setup-complete': return json(await auth.completeLocalSetup(request));
|
|
95
100
|
case '/auth/bootstrap': { const value = await auth.bootstrap(request, input); return seeOther(value.returnTo, value.cookie); }
|
|
96
101
|
case '/auth/magic-link': {
|
|
@@ -128,11 +133,11 @@ export function createAuthHandler(auth) {
|
|
|
128
133
|
case '/auth/providers/relink': return json(await auth.relinkProvider(request, input));
|
|
129
134
|
}
|
|
130
135
|
} catch (error) {
|
|
131
|
-
if (
|
|
136
|
+
if (['/auth/settings/identity', '/auth/settings/business'].includes(path) && representation(request) === 'html') {
|
|
132
137
|
const code = error instanceof AuthError ? error.code : 'auth_unavailable';
|
|
133
138
|
const status = error instanceof AuthError ? error.status : 503;
|
|
134
|
-
if (code === 'provider_unlinked') {
|
|
135
|
-
try { return await identitySettingsPage(await auth.identitySettings(request), auth.presentation, { error: code, status, method: request.method }); }
|
|
139
|
+
if (code === 'provider_unlinked' || code === 'step_up_required') {
|
|
140
|
+
try { return await identitySettingsPage({ ...await auth.identitySettings(request), ...(auth.businessSettingsInfo ? { business: await auth.businessSettingsInfo(request) } : {}) }, auth.publicPresentation ? await auth.publicPresentation() : auth.presentation, { error: code, status, method: request.method }); }
|
|
136
141
|
catch { return settingsRefusalPage(request, auth.presentation, 'auth_unavailable', 503); }
|
|
137
142
|
}
|
|
138
143
|
return settingsRefusalPage(request, auth.presentation, code, status);
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Composed identity over Base's OA accounts, or an explicitly paired native
|
|
2
2
|
// accounts/proof adapter. Native composition never constructs the OA store.
|
|
3
|
+
import { businessSettingsControls } from './business-settings.mjs';
|
|
4
|
+
import { businessTeam } from './accounts/business-team.mjs';
|
|
3
5
|
import { scheduledPolicies } from './scheduled/index.mjs';
|
|
4
6
|
import { fencedCredentials } from './accounts/credentials.mjs';
|
|
5
7
|
import { providerControl } from './accounts/provider-control.mjs';
|
|
@@ -29,8 +31,8 @@ export function createOperatorAuth(options) {
|
|
|
29
31
|
const { db, config: input, mailSender, clientIp, rolePolicy = defaultRolePolicy, clock = Date.now, hooks = {}, fetch, waitUntil, accounts: nativeAccounts, proofs } = options;
|
|
30
32
|
const config = configure(input);
|
|
31
33
|
if (typeof clientIp !== 'function') throw new AuthError('client_ip_required', 503);
|
|
32
|
-
if (!hooks || typeof hooks !== 'object' || ['beforeMagicLink', 'afterConfirm'].some(name => hooks[name] !== undefined && typeof hooks[name] !== 'function')) throw new AuthError('invalid_configuration', 503);
|
|
33
|
-
if (Object.hasOwn(options, 'accounts') || Object.hasOwn(options, 'proofs')) return createNativeAuth({ db, config, accounts: nativeAccounts, proofs, mailSender, clientIp, clock, hooks, fetch, waitUntil });
|
|
34
|
+
if (!hooks || typeof hooks !== 'object' || ['beforeMagicLink', 'afterConfirm', 'audit'].some(name => hooks[name] !== undefined && typeof hooks[name] !== 'function')) throw new AuthError('invalid_configuration', 503);
|
|
35
|
+
if (Object.hasOwn(options, 'accounts') || Object.hasOwn(options, 'proofs')) return createNativeAuth({ db, config, accounts: nativeAccounts, proofs, mailSender, clientIp, clock, hooks, fetch, waitUntil, businessSettings: options.businessSettings });
|
|
34
36
|
const store = new AuthStore(db);
|
|
35
37
|
const sessions = oaSessions({ config, store, clock });
|
|
36
38
|
const { hash, newSession, completed, authenticate } = sessions;
|
|
@@ -73,10 +75,13 @@ export function createOperatorAuth(options) {
|
|
|
73
75
|
},
|
|
74
76
|
} });
|
|
75
77
|
const { limit, newToken, sendLink, requireFreshProof } = email;
|
|
76
|
-
const
|
|
78
|
+
const adminContext = { config, store, clock, authorize: rolePolicy, authenticate, newToken, sendLink, limit, requireFreshProof, providers, accounts, audit: hooks.audit };
|
|
79
|
+
const { prepareInvitation: _prepareInvitation, ...admin } = administration(adminContext);
|
|
77
80
|
const external = legacyAdmission({ admission, config });
|
|
78
81
|
return Object.freeze({
|
|
79
82
|
...admin,
|
|
83
|
+
businessTeam: businessTeam(adminContext),
|
|
84
|
+
...businessSettingsControls({ businessSettings: options.businessSettings, config, accounts, authenticate, requireFreshProof, store, clock }),
|
|
80
85
|
...(options.scheduledPolicies ? { scheduledPolicies: scheduledPolicies({ definitions: options.scheduledPolicies, store, config, clock, authenticate, requireFreshProof, rolePolicy }) } : {}),
|
|
81
86
|
...providerControl({ store, accounts, authenticate, config, clock }),
|
|
82
87
|
...fencedCredentials({store,authenticate,requireFreshProof,config,clock,hash}),
|
|
@@ -48,7 +48,7 @@ export function createSignInHandler(auth, { setup, links, defaultReturnTo = '/ap
|
|
|
48
48
|
return async function signIn(request) {
|
|
49
49
|
const url = new URL(request.url);
|
|
50
50
|
if (url.pathname !== path || !['GET', 'HEAD'].includes(request.method)) return null;
|
|
51
|
-
const branding = auth.presentation;
|
|
51
|
+
const branding = auth.publicPresentation ? await auth.publicPresentation() : auth.presentation;
|
|
52
52
|
const returnTo = safeReturnTo(url.searchParams.get('returnTo'), defaultReturnTo);
|
|
53
53
|
const hint = emailHint(url.searchParams.get('login_hint'));
|
|
54
54
|
const providers = auth.providers ? (await auth.providers.list()).filter(p => p.visible) : [];
|
|
@@ -133,6 +133,39 @@ Custom replay runners can supply each plan entry's `aliases` and pass
|
|
|
133
133
|
`{dataOnly}` as `migrationReplay`'s third argument. Verified archive ledger
|
|
134
134
|
hashes take precedence over name-only placement where available.
|
|
135
135
|
|
|
136
|
+
### Leaving RER Managed: the Platform ledger rule
|
|
137
|
+
|
|
138
|
+
A Managed installation's D1 keeps its history in `rer_migrations`, which the
|
|
139
|
+
Platform runner writes; a native installation uses `d1_migrations`. The ledger
|
|
140
|
+
is deployment history, never product schema or data. Base maps it by migration
|
|
141
|
+
identity, name and SHA-256 of the file:
|
|
142
|
+
|
|
143
|
+
- Export from a Platform-ledger source records `manifest.migrationLedger` (see
|
|
144
|
+
"Migration history" below) and never archives `rer_migrations` as a table.
|
|
145
|
+
- Import into a native installation matches every entry to exactly one
|
|
146
|
+
checkout file by hash; a name may confirm that file but cannot point at
|
|
147
|
+
another. A file the checkout does not have or whose content differs refuses
|
|
148
|
+
as `migration_ledger:unknown:<name>`, a name on one file with the hash of
|
|
149
|
+
another as `migration_ledger:conflict:<name>`, and the CLI prints the fix.
|
|
150
|
+
A checkout file the Platform history lacks refuses at the preflight as
|
|
151
|
+
`migration_ledger:unapplied`, naming the file; apply it with Platform first.
|
|
152
|
+
- Adapters with their own archive format (not Base's) call `mapMigrationLedger`
|
|
153
|
+
instead of comparing ledger controllers: it returns the entries named by the
|
|
154
|
+
checkout's files, or refuses with the same constant codes.
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
import { mapMigrationLedger } from '@runeditrun/rer/portability-migrations';
|
|
158
|
+
|
|
159
|
+
// Export: the source's ledger rows (rer_migrations or not), as native identities.
|
|
160
|
+
const ledger = await mapMigrationLedger({ root, binding: 'DB', platformMigrations, entries: rows });
|
|
161
|
+
// Import: the archive's entries must map without refusal; write no ledger rows.
|
|
162
|
+
await mapMigrationLedger({ root, binding: 'DB', platformMigrations, entries: archive.ledger });
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Call `nativeMigrationPreflight` before each native apply as below: a sole,
|
|
166
|
+
fully applied `rer_migrations` returns `{nativeApply:false}` for export and
|
|
167
|
+
import alike, so a seed's own guard must not require `d1_migrations`.
|
|
168
|
+
|
|
136
169
|
### Before an adapter's native migrations
|
|
137
170
|
|
|
138
171
|
Seed adapters sometimes prepare schema before calling `localPortability`.
|
|
@@ -192,7 +225,7 @@ provider operation.
|
|
|
192
225
|
| Compatible `d1_migrations`, every recorded name exactly equal to its checkout filename | `{nativeApply:true}`; Wrangler may apply its pending files. Plain Wrangler names remain supported; tracked history must also match hashes and applied states. |
|
|
193
226
|
| Native alias-only or hash-only relocation, or an unmapped native data-only row | `migration_ledger:incompatible`; restore the matching checkout or reconcile through the owning runner before native apply. |
|
|
194
227
|
| Sole, fully applied matching `rer_migrations` | `{nativeApply:false}`; preserve Platform's ledger and skip Wrangler. |
|
|
195
|
-
| Any pending checkout file with Platform history | `migration_ledger:unapplied
|
|
228
|
+
| Any pending checkout file with Platform history | `migration_ledger:unapplied`, naming the file (`error.migration`); apply it through the owning runner first. |
|
|
196
229
|
| Multiple ledgers, even empty | `migration_ledger:multiple`; reconcile history through its owning runner. |
|
|
197
230
|
| Legacy `_rer_deploy_migrations` alone | `migration_ledger:runner`; use its owning runner, which can adopt the history. |
|
|
198
231
|
| Invalid ledger shape, row or binding | `migration_ledger:incompatible` or `migration_ledger:binding`. |
|
|
@@ -440,6 +473,18 @@ them into a scratch database to find the archive's place in that history:
|
|
|
440
473
|
- Archives record the applied migration names in `manifest.migrations` (since
|
|
441
474
|
additive restore). Every recorded migration must be applied here, and
|
|
442
475
|
replaying exactly those must give the archive's schema identity.
|
|
476
|
+
- Archives exported from an installation RER Platform migrated (a Managed
|
|
477
|
+
installation leaving for native) also carry `manifest.migrationLedger`:
|
|
478
|
+
`{origin:'rer_migrations',entries:[{name,sha256}]}` in application order,
|
|
479
|
+
each entry named by the exporting checkout's file with that SHA-256, so
|
|
480
|
+
`manifest.migrations` lists native file names whatever prefix Platform
|
|
481
|
+
recorded. Where the exporter has no checkout files (a hosted export), the
|
|
482
|
+
ledger's own names are kept beside their hashes. Import resolves each entry
|
|
483
|
+
by hash exactly as it does an archived ledger table (next item), so a
|
|
484
|
+
Platform prefix needs no declaration and a changed file refuses. Platform's
|
|
485
|
+
ledger is never imported as product tables. Native sources record no
|
|
486
|
+
`migrationLedger`; their archives are unchanged, and v1 and earlier v2
|
|
487
|
+
archives remain importable.
|
|
443
488
|
- Older archives that still carry their source's migration ledger as a table
|
|
444
489
|
(`rer_migrations` and the other ledgers above, from before they became
|
|
445
490
|
bookkeeping) are placed by it. Every name it records must be applied here,
|
|
@@ -643,6 +688,42 @@ its v2 output succeeds; do not reset the recovered installation to retry.
|
|
|
643
688
|
`exports.create(actor?)` copies all tables in one D1 batch. Product writes may
|
|
644
689
|
continue after that snapshot. R2 listing records size and etag; changes or missing
|
|
645
690
|
objects fail subsequent reads. D1 and R2 do not share a cross-binding transaction.
|
|
691
|
+
|
|
692
|
+
The direct service factory `createPortability` accepts an optional synchronous
|
|
693
|
+
`onExport({id, actor, scope})` callback returning `[sql, args][]`. Its SQL is
|
|
694
|
+
appended to that same snapshot/job-creating D1 batch. A thrown callback or failed
|
|
695
|
+
statement refuses creation; failed SQL rolls back the job, snapshots and audit
|
|
696
|
+
rows together. With no hook, export behavior is unchanged. This records job
|
|
697
|
+
creation, not successful download: a later build, file-write or download failure
|
|
698
|
+
does not erase a committed creation audit.
|
|
699
|
+
|
|
700
|
+
This hook applies to exports created through that service, including the file
|
|
701
|
+
adapter used by CLI exports. `createPortabilityHandler` does not accept or
|
|
702
|
+
forward `onExport`; the HTTP export handler has no audit hook.
|
|
703
|
+
|
|
704
|
+
`id` is the export job id, `scope` is the constant `'installation'`, and `actor`
|
|
705
|
+
is the opaque caller id, with the direct/CLI sentinel `'direct'` normalized to
|
|
706
|
+
`null`. Job ownership still stores `'direct'` for existing direct callers.
|
|
707
|
+
The callback receives no names, emails, IdP identifiers, tokens, record data,
|
|
708
|
+
object keys or file paths. Pass only opaque operator ids as authenticated actors.
|
|
709
|
+
The seed owns audit storage, migrations, retention, erasure and archive policy;
|
|
710
|
+
Base adds no audit table. For a CLI installation export, wire the seed's existing
|
|
711
|
+
`operation_audit` schema to action `installation.export`, actor_id `NULL` and
|
|
712
|
+
feature `rer export`, for example:
|
|
713
|
+
|
|
714
|
+
```js
|
|
715
|
+
const service = await createPortability({db, buckets, policy,
|
|
716
|
+
onExport: ({id, actor}) => [[
|
|
717
|
+
'INSERT INTO operation_audit(id,action,actor_id,feature) VALUES(?,?,?,?)',
|
|
718
|
+
[id, 'installation.export', actor, 'rer export'],
|
|
719
|
+
]],
|
|
720
|
+
});
|
|
721
|
+
```
|
|
722
|
+
|
|
723
|
+
Adapt the statement to the seed's schema and use the service in its existing
|
|
724
|
+
export adapter. Hooks return SQL only; external side effects cannot share the
|
|
725
|
+
D1 transaction. Do not write these events to the erasure tombstone journal.
|
|
726
|
+
|
|
646
727
|
Transfer bookkeeping lives in sidecar tables, so product tables up to D1's
|
|
647
728
|
100-column limit retain the full column and binding capacity. A keyless table
|
|
648
729
|
with 100 columns or generated columns restores its rowids with two updates in
|
|
@@ -5,7 +5,7 @@ import { createCopy,readCopy,normalizeCopy } from './copies.mjs';
|
|
|
5
5
|
import { canonicalJson, utf8, base64, StreamHash } from './hash.mjs';
|
|
6
6
|
import { FORMAT, makePart, checkPart, ContentVerifier, ArchiveVerifier } from './format.mjs';
|
|
7
7
|
import { journal } from './journal.mjs';
|
|
8
|
-
import {
|
|
8
|
+
import { readLedger, platformOrigin } from './ledgers.mjs';
|
|
9
9
|
import { capturePayload, payloadRange } from './payloads.mjs';
|
|
10
10
|
import { dropProviderData } from './provider-data.mjs';
|
|
11
11
|
import { dropDevData } from './dev-data.mjs';
|
|
@@ -23,11 +23,13 @@ export async function createExport(context,actor='direct') {
|
|
|
23
23
|
}
|
|
24
24
|
objects.sort((a,b)=>a.bucket<b.bucket?-1:a.bucket>b.bucket?1:a.key<b.key?-1:a.key>b.key?1:0);
|
|
25
25
|
const copies=Object.fromEntries(cat.tables.map((t,i)=>[t.name,'rer_portability_snap_'+id.replaceAll('-','')+'_'+i]));
|
|
26
|
-
const
|
|
27
|
-
const manifest={kind:'manifest',format:FORMAT,version:2,product:config.product,exportedAt:new Date().toISOString(),sourceIdentity:s.source_identity,schema:cat.identity,tables:cat.tables,schemaObjects:cat.schemaObjects,objects,payloads:[],excludedTables:cat.excludedTables,excludedColumns:config.secretColumns,requiredConfiguration:config.requiredConfiguration,partCount:0,epoch:s.epoch,journal:await journal(db,s.source_identity),...(migrations?{migrations}:{})};
|
|
26
|
+
const ledger=await readLedger(db),origin=await platformOrigin(ledger,context.migrations),migrations=origin?origin.entries.map(entry=>entry.name):ledger?.entries.map(entry=>entry.name);
|
|
27
|
+
const manifest={kind:'manifest',format:FORMAT,version:2,product:config.product,exportedAt:new Date().toISOString(),sourceIdentity:s.source_identity,schema:cat.identity,tables:cat.tables,schemaObjects:cat.schemaObjects,objects,payloads:[],excludedTables:cat.excludedTables,excludedColumns:config.secretColumns,requiredConfiguration:config.requiredConfiguration,partCount:0,epoch:s.epoch,journal:await journal(db,s.source_identity),...(migrations?{migrations}:{}),...(origin?{migrationLedger:origin}:{})};
|
|
28
28
|
const row={id,kind:'export',actor,phase:'building',completed:0,metadata:{manifest,copies,cursor:{table:0,row:0,binary:0,offset:0},partBytes:config.partBytes}};
|
|
29
|
+
const audit=context.onExport?context.onExport({id,actor:actor==='direct'?null:actor,scope:'installation'}):[];
|
|
29
30
|
await db.batch([guard(db,'EXISTS(SELECT 1 FROM rer_portability_state WHERE id=1 AND applying=0 AND source_identity=? AND epoch=?)',s.source_identity,s.epoch),
|
|
30
|
-
...cat.tables.flatMap(t=>createCopy(db,t,copies[t.name],true)),statement(db,'INSERT INTO rer_portability_jobs(id,kind,actor,phase,metadata) VALUES(?,?,?,?,?)',id,'export',actor,row.phase,canonicalJson(row.metadata)),db.prepare('DELETE FROM rer_portability_guard')
|
|
31
|
+
...cat.tables.flatMap(t=>createCopy(db,t,copies[t.name],true)),statement(db,'INSERT INTO rer_portability_jobs(id,kind,actor,phase,metadata) VALUES(?,?,?,?,?)',id,'export',actor,row.phase,canonicalJson(row.metadata)),db.prepare('DELETE FROM rer_portability_guard'),
|
|
32
|
+
...audit.map(([sql,args])=>statement(db,sql,...args))]);
|
|
31
33
|
try {
|
|
32
34
|
await dropDevData(db,cat.tables,copies);
|
|
33
35
|
await dropProviderData(db,cat.tables,copies);
|
|
@@ -38,6 +38,11 @@ async function checkManifestFields(m) {
|
|
|
38
38
|
insist(Array.isArray(m.tables) && Array.isArray(m.objects) && Array.isArray(m.payloads) && Array.isArray(m.journal) && Array.isArray(m.requiredConfiguration),'manifest_inventory');
|
|
39
39
|
// Applied product migrations, recorded by archives since additive restore.
|
|
40
40
|
insist(m.migrations===undefined || (Array.isArray(m.migrations) && m.migrations.every(x=>typeof x==='string')),'manifest_inventory');
|
|
41
|
+
// The ledger an export from a RER Platform installation records, by migration identity.
|
|
42
|
+
const ledger=m.migrationLedger;
|
|
43
|
+
insist(ledger===undefined || (ledger && typeof ledger==='object' && Object.keys(ledger).sort().join()==='entries,origin' && ledger.origin==='rer_migrations' && Array.isArray(ledger.entries)
|
|
44
|
+
&& ledger.entries.every(e=>e && typeof e==='object' && Object.keys(e).sort().join()==='name,sha256' && typeof e.name==='string' && /^[a-f0-9]{64}$/.test(e.sha256))
|
|
45
|
+
&& canonicalJson(m.migrations)===canonicalJson(ledger.entries.map(e=>e.name)) && new Set(m.migrations).size===m.migrations.length),'manifest_inventory');
|
|
41
46
|
insist(m.tables.every((t,i)=>typeof t.name==='string' && typeof t.sql==='string' && Array.isArray(t.columns) && Array.isArray(t.dependencies) && Number.isSafeInteger(t.count) && t.count>=0 && (!i || m.tables[i-1].name<t.name)),'table_inventory');
|
|
42
47
|
insist(m.schemaObjects===undefined || (Array.isArray(m.schemaObjects) && m.schemaObjects.every((o,i)=>
|
|
43
48
|
o && typeof o.name==='string' && ['index','view','trigger'].includes(o.type) && typeof o.table==='string' && typeof o.sql==='string'
|
|
@@ -13,6 +13,8 @@ export interface Manifest {
|
|
|
13
13
|
excludedTables: string[]; excludedColumns: Record<string,string[]>; requiredConfiguration: string[]; partCount: number; epoch: number; journal: string[];
|
|
14
14
|
/** Applied product migrations; absent from archives made before additive restore. */
|
|
15
15
|
migrations?: string[];
|
|
16
|
+
/** Present when RER Platform's runner migrated the source: each applied migration by native file name and SHA-256, in application order. */
|
|
17
|
+
migrationLedger?: {origin:'rer_migrations';entries:{name:string;sha256:string}[]};
|
|
16
18
|
}
|
|
17
19
|
export type Item = {kind:'row';table:string;index:number;row:Row;rowid?:number} | {kind:'object';bucket:string;key:string;offset:number;data:string} | {kind:'payload';name:string;offset:number;data:string};
|
|
18
20
|
export interface Part {kind:'part';index:number;items:Item[];sha256:string}
|
|
@@ -42,7 +44,13 @@ export interface Status {id:string;kind:'export'|'import';phase:'building'|'stag
|
|
|
42
44
|
export interface Bridge {fromSchema:string;toSchema:string;migrations:string[];tables:string[];columns:Record<string,string[]>;/** Later backfills of tables the archive lacks, replayed at finalize in this order; rows is null until the apply commits. */replayed?:{migration:string;statement:number;table:string;rows:number|null}[]}
|
|
43
45
|
/** A runner's replay of the destination's applied migrations: the archive schema identity after a chosen set, and each migration's statements. */
|
|
44
46
|
export interface MigrationReplay {names:string[];/** Seed-declared data-only Platform entries without migration files. */dataOnly?:string[];/** Names generated by the seed's deployment specification for this file. */aliases?(name:string):string[]|undefined;statements(name:string):string[];/** The migration file's SHA-256 hex, where the runner read the file. */sha256?(name:string):string|undefined;schemaAfter(names:string[]):Promise<string>}
|
|
45
|
-
|
|
47
|
+
/** Export job creation only; no record data, object keys or file paths. */
|
|
48
|
+
export interface ExportEvent {id:string;/** Opaque caller id; the direct/CLI sentinel is normalized to null. */actor:string|null;scope:'installation'}
|
|
49
|
+
export interface PortabilityOptions {
|
|
50
|
+
db:D1Database;buckets?:Record<string,R2Bucket>;policy:ArchivePolicy;migrations?:()=>Promise<MigrationReplay>;
|
|
51
|
+
/** Seed-owned audit SQL, appended to the snapshot/job batch. Throwing or SQL failure refuses creation. */
|
|
52
|
+
onExport?:(event:ExportEvent)=>[sql:string,args:unknown[]][];
|
|
53
|
+
}
|
|
46
54
|
export interface Portability {
|
|
47
55
|
exports: {create(actor?:string):Promise<string>;advance(id:string,options?:{expectedPart?:number;maxParts?:number}):Promise<Status>;status(id:string):Promise<Status>;download(id:string):Promise<ReadableStream<Uint8Array>>;abort(id:string):Promise<Status>};
|
|
48
56
|
imports: {active(actor?:string):Promise<Status|null>;/** ledgers: the rows of each migration ledger table an old archive carries, as read from it; they place it in the destination's history and must equal its parts. */create(manifest:Manifest,actor?:string,options?:{ledgers?:Record<string,Row[]>}):Promise<string>;uploadPart(id:string,index:number,part:Part):Promise<Status>;finalize(id:string,options:{archiveHash:string;contentHash?:string;laterJournal?:LaterJournal}):Promise<Status>;status(id:string):Promise<Status>;abort(id:string):Promise<Status>;/** Aborts the import left staged by a crashed restore and returns its id; call only as the destination's sole operator. */abortAbandoned():Promise<string|null>};
|
|
@@ -52,7 +60,7 @@ export interface Portability {
|
|
|
52
60
|
invalidateTransfersStatements():Promise<D1PreparedStatement[]>;
|
|
53
61
|
}
|
|
54
62
|
export function createPortability(options:PortabilityOptions):Promise<Portability>;
|
|
55
|
-
export interface HandlerOptions extends PortabilityOptions {
|
|
63
|
+
export interface HandlerOptions extends Omit<PortabilityOptions,'onExport'> {
|
|
56
64
|
appOrigin:string;basePath?:string;maxBodyBytes?:number;
|
|
57
65
|
authorize:(request:Request,context:{operation:'export'|'import'|'unknown';jobId:string|null})=>string|null|Promise<string|null>;
|
|
58
66
|
}
|
|
@@ -11,12 +11,13 @@ export { readArchive, makePart, MAX_LINE_BYTES, MAX_PART_BYTES } from './format.
|
|
|
11
11
|
export { canonicalJson, sha256 } from './hash.mjs';
|
|
12
12
|
export { PortabilityError } from './storage.mjs';
|
|
13
13
|
export { createPortabilityHandler } from './http.mjs';
|
|
14
|
-
export async function createPortability({db,buckets={},policy:input,migrations}) {
|
|
14
|
+
export async function createPortability({db,buckets={},policy:input,migrations,onExport}) {
|
|
15
|
+
insist(onExport===undefined || typeof onExport==='function','invalid_on_export');
|
|
15
16
|
db=wireDatabase(db);
|
|
16
17
|
const config=policy(input);await catalogue(db,config);
|
|
17
18
|
for(const name of Object.keys(config.buckets))insist(buckets[name] && typeof buckets[name].get==='function','missing_bucket:'+name);
|
|
18
19
|
await ensureImportLeases(db);
|
|
19
|
-
const context={db,buckets,config,migrations};
|
|
20
|
+
const context={db,buckets,config,migrations,onExport};
|
|
20
21
|
return {
|
|
21
22
|
exports:{create:actor=>createExport(context,actor),advance:(id,options)=>advanceExport(context,id,options),status:id=>exportStatus(context,id),download:id=>downloadExport(context,id),abort:id=>abortExport(context,id)},
|
|
22
23
|
imports:{active:actor=>activeImport(context,actor),create:(manifest,actor,options={})=>createImport(context,manifest,actor,undefined,options.ledgers),uploadPart:(id,index,part)=>uploadPart(context,id,index,part),finalize:(id,options)=>finalizeImport(context,id,options),status:id=>importStatus(context,id),abort:id=>abortImport(context,id),abortAbandoned:()=>abortAbandonedImport(context)},
|
|
@@ -18,8 +18,12 @@ const partial=name=>name.includes('#');
|
|
|
18
18
|
|
|
19
19
|
/** The destination's applied product migrations, [{name,sha256?}] in the ledger's own order, or undefined without a ledger. */
|
|
20
20
|
export async function destinationLedger(db) {
|
|
21
|
+
return (await readLedger(db))?.entries;
|
|
22
|
+
}
|
|
23
|
+
/** The ledger table in use and its applied migrations, {table,entries}, or undefined without a ledger. */
|
|
24
|
+
export async function readLedger(db) {
|
|
21
25
|
const found=(await all(db,`SELECT name FROM sqlite_master WHERE type='table' AND name IN (${ledgerNames.map(n=>`'${n}'`).join(',')})`)).map(x=>x.name);
|
|
22
|
-
if(found.includes('_rer_deploy_migrations'))return (await all(db,"SELECT * FROM _rer_deploy_migrations WHERE state='applied' ORDER BY name")).map(entry);
|
|
26
|
+
if(found.includes('_rer_deploy_migrations'))return {table:'_rer_deploy_migrations',entries:(await all(db,"SELECT * FROM _rer_deploy_migrations WHERE state='applied' ORDER BY name")).map(entry)};
|
|
23
27
|
if(found.includes('d1_migrations')) {
|
|
24
28
|
const columns=(await all(db,'PRAGMA table_info(d1_migrations)')).map(x=>x.name);
|
|
25
29
|
insist(['id','name','applied_at'].every(name=>columns.includes(name)),'schema_mismatch:migration_history',409);
|
|
@@ -29,7 +33,7 @@ export async function destinationLedger(db) {
|
|
|
29
33
|
insist(rows.every(row=>typeof row.name==='string') && new Set(rows.map(row=>row.name)).size===rows.length,'schema_mismatch:migration_history',409);
|
|
30
34
|
if(tracked)insist(rows.every(row=>row.state==='applied'),'schema_mismatch:uncertain_migrations',409);
|
|
31
35
|
if(tracked)insist(rows.every(row=>typeof row.sha256==='string' && /^[a-f0-9]{64}$/.test(row.sha256)),'schema_mismatch:migration_history',409);
|
|
32
|
-
return rows.map(entry);
|
|
36
|
+
return {table:'d1_migrations',entries:rows.map(entry)};
|
|
33
37
|
}
|
|
34
38
|
if(!found.includes('rer_migrations'))return;
|
|
35
39
|
// Platform applies pending files in its plan's order and inserts each ledger
|
|
@@ -39,7 +43,7 @@ export async function destinationLedger(db) {
|
|
|
39
43
|
const rows=await all(db,'SELECT name,sha256 FROM rer_migrations ORDER BY rowid');
|
|
40
44
|
insist(rows.every(row=>typeof row.name==='string' && typeof row.sha256==='string' && /^[a-f0-9]{64}$/.test(row.sha256)) && new Set(rows.map(row=>row.name)).size===rows.length,'schema_mismatch:migration_history',409);
|
|
41
45
|
insist(!rows.some(row=>partial(row.name)),'schema_mismatch:uncertain_migrations',409);
|
|
42
|
-
return rows.map(entry);
|
|
46
|
+
return {table:'rer_migrations',entries:rows.map(entry)};
|
|
43
47
|
}
|
|
44
48
|
const entry=row=>({name:row.name,...(typeof row.sha256==='string'?{sha256:row.sha256}:{})});
|
|
45
49
|
|
|
@@ -67,6 +71,23 @@ export function matchMigrationLedger(entries,files,{dataOnly=[]}={}) {
|
|
|
67
71
|
return matched;
|
|
68
72
|
}
|
|
69
73
|
|
|
74
|
+
/**
|
|
75
|
+
* What an export records when its source was migrated by RER Platform:
|
|
76
|
+
* {origin:'rer_migrations',entries:[{name,sha256}]} in application order, each
|
|
77
|
+
* entry named by the checkout file with that hash (the native identity) where
|
|
78
|
+
* the exporter can read the checkout's files. Declared data-only entries and
|
|
79
|
+
* hosted exporters, which have no files, keep the ledger's own names. Native
|
|
80
|
+
* sources record nothing here, so their archives are unchanged.
|
|
81
|
+
*/
|
|
82
|
+
export async function platformOrigin(ledger,migrations) {
|
|
83
|
+
if(ledger?.table!=='rer_migrations')return;
|
|
84
|
+
const replay=await migrations?.();
|
|
85
|
+
const files=replay?.names.map(name=>({name,sha256:replay.sha256?.(name),aliases:replay.aliases?.(name)}));
|
|
86
|
+
const entries=ledger.entries.map(({name,sha256})=>({name:files?(matchMigrationLedger([{name,sha256}],files,replay)[0]??name):name,sha256}));
|
|
87
|
+
insist(new Set(entries.map(entry=>entry.name)).size===entries.length,'migration_ledger:ambiguous',409);
|
|
88
|
+
return {origin:'rer_migrations',entries};
|
|
89
|
+
}
|
|
90
|
+
|
|
70
91
|
/** The ledger tables an archive carries (old archives carried rer_migrations before it became bookkeeping). */
|
|
71
92
|
export const archivedLedgerTables=manifest=>manifest.tables.filter(t=>ledgerNames.includes(identifier(t.name)));
|
|
72
93
|
|
|
@@ -96,7 +117,9 @@ export function checkArchivedLedgers(manifest,ledgers) {
|
|
|
96
117
|
/** The applied migrations an archive's own ledger records, [{name,sha256?}]; undefined when it carries no ledger. */
|
|
97
118
|
export function archivedHistory(manifest,ledgers) {
|
|
98
119
|
const tables=archivedLedgerTables(manifest);
|
|
99
|
-
|
|
120
|
+
// A Platform-origin export records its ledger in the manifest, never as a table.
|
|
121
|
+
if(manifest.migrationLedger)insist(!tables.length,'schema_mismatch:migration_history');
|
|
122
|
+
if(!tables.length)return manifest.migrationLedger?.entries.map(entry);
|
|
100
123
|
insist(tables.length===1 && ledgers?.[tables[0].name],'schema_mismatch:migration_history');
|
|
101
124
|
const [table]=tables,rows=ledgers[table.name],columns=table.columns.map(c=>c.name);
|
|
102
125
|
if(identifier(table.name)==='rer_migrations') {
|