@baukit/data-contracts 0.18.3 → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/identity-error.d.ts +9 -0
- package/dist/identity-error.d.ts.map +1 -0
- package/dist/identity-error.js +21 -0
- package/dist/identity-error.js.map +1 -0
- package/dist/identity.d.ts +2 -76
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +23 -248
- package/dist/identity.js.map +1 -1
- package/dist/import-envelope.d.ts.map +1 -1
- package/dist/import-envelope.js +6 -2
- package/dist/import-envelope.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/limits.js +30 -28
- package/dist/limits.js.map +1 -1
- package/dist/scoped-lifecycle.d.ts +73 -0
- package/dist/scoped-lifecycle.d.ts.map +1 -0
- package/dist/scoped-lifecycle.js +216 -0
- package/dist/scoped-lifecycle.js.map +1 -0
- package/dist/scoped-persistence.vitest.d.ts +12 -0
- package/dist/scoped-persistence.vitest.d.ts.map +1 -0
- package/dist/scoped-persistence.vitest.js +171 -0
- package/dist/scoped-persistence.vitest.js.map +1 -0
- package/dist/vitest.d.ts +1 -9
- package/dist/vitest.d.ts.map +1 -1
- package/dist/vitest.js +111 -266
- package/dist/vitest.js.map +1 -1
- package/package.json +4 -3
- package/src/durable-draft.test.ts +1 -8
- package/src/identity-error.ts +30 -0
- package/src/identity.test.ts +1 -1
- package/src/identity.ts +36 -326
- package/src/import-envelope.test.ts +1 -4
- package/src/import-envelope.ts +9 -9
- package/src/index.ts +1 -0
- package/src/limits.ts +39 -32
- package/src/scoped-lifecycle.ts +290 -0
- package/src/scoped-persistence.vitest.ts +221 -0
- package/src/vitest.ts +124 -308
package/src/identity.test.ts
CHANGED
|
@@ -3,12 +3,12 @@ import { describe, expect, it, vi } from 'vitest';
|
|
|
3
3
|
import {
|
|
4
4
|
InMemoryScopedPersistenceRegistryStore,
|
|
5
5
|
PersistenceIdentityMismatchError,
|
|
6
|
-
ScopedPersistenceLifecycle,
|
|
7
6
|
deriveScopedStoreName,
|
|
8
7
|
recheckServerSubjectBeforeSyncAdoption,
|
|
9
8
|
removeScopedPersistenceRegistryEntry,
|
|
10
9
|
resolveScopedStore,
|
|
11
10
|
} from './identity.js';
|
|
11
|
+
import { ScopedPersistenceLifecycle } from './scoped-lifecycle.js';
|
|
12
12
|
|
|
13
13
|
const digest = (value: string): Promise<string> =>
|
|
14
14
|
Promise.resolve(value.endsWith(':account-e') ? 'e'.repeat(64) : 'f'.repeat(64));
|
package/src/identity.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import type { MaybePromise } from './contracts.js';
|
|
2
|
+
import {
|
|
3
|
+
isPersistenceIdentityMismatchError,
|
|
4
|
+
PersistenceIdentityMismatchError,
|
|
5
|
+
requiredIdentityPart,
|
|
6
|
+
} from './identity-error.js';
|
|
7
|
+
|
|
8
|
+
export { isPersistenceIdentityMismatchError, PersistenceIdentityMismatchError };
|
|
2
9
|
|
|
3
10
|
const REGISTRY_VERSION = 1;
|
|
4
11
|
const SCOPED_STORE_PREFIX = 'baukit-scoped-v1-';
|
|
@@ -30,30 +37,6 @@ export interface ResolvedScopedStore {
|
|
|
30
37
|
/** Product-owned resolver that replaces the derived scoped store name. */
|
|
31
38
|
export type ScopedPersistenceResolver = (subject: string) => MaybePromise<ResolvedScopedStore>;
|
|
32
39
|
|
|
33
|
-
/** Stable blocking failure for corrupt ownership metadata or a subject mismatch. */
|
|
34
|
-
export class PersistenceIdentityMismatchError extends Error {
|
|
35
|
-
public override readonly name = 'PersistenceIdentityMismatchError';
|
|
36
|
-
public readonly code = 'persistence_identity_mismatch' as const;
|
|
37
|
-
|
|
38
|
-
public constructor(
|
|
39
|
-
message = 'Local data belongs to a different authenticated identity.',
|
|
40
|
-
options?: ErrorOptions,
|
|
41
|
-
) {
|
|
42
|
-
super(message, options);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function isPersistenceIdentityMismatchError(
|
|
47
|
-
cause: unknown,
|
|
48
|
-
): cause is PersistenceIdentityMismatchError {
|
|
49
|
-
return (
|
|
50
|
-
cause instanceof PersistenceIdentityMismatchError ||
|
|
51
|
-
(typeof cause === 'object' &&
|
|
52
|
-
cause !== null &&
|
|
53
|
-
Reflect.get(cause, 'code') === 'persistence_identity_mismatch')
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
40
|
interface RegistryEntry {
|
|
58
41
|
readonly subject: string;
|
|
59
42
|
readonly storeName: string;
|
|
@@ -141,13 +124,6 @@ async function sha256(value: string): Promise<string> {
|
|
|
141
124
|
return hexadecimal(new Uint8Array(digest));
|
|
142
125
|
}
|
|
143
126
|
|
|
144
|
-
function requiredIdentityPart(value: string, label: string): string {
|
|
145
|
-
if (value.trim().length === 0) {
|
|
146
|
-
throw new PersistenceIdentityMismatchError(`${label} must not be empty.`);
|
|
147
|
-
}
|
|
148
|
-
return value;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
127
|
function canonicalIdentity(namespace: string, subject: string): string {
|
|
152
128
|
const namespaceBytes = utf8(namespace).byteLength;
|
|
153
129
|
const subjectBytes = utf8(subject).byteLength;
|
|
@@ -233,26 +209,38 @@ function validateRegistryNamespace(
|
|
|
233
209
|
|
|
234
210
|
const subjectNames = new Set<string>();
|
|
235
211
|
for (const entry of subjects) {
|
|
236
|
-
|
|
237
|
-
throw new TypeError('invalid subject entry');
|
|
238
|
-
}
|
|
239
|
-
const subject: unknown = Reflect.get(entry, 'subject');
|
|
240
|
-
const storeName: unknown = Reflect.get(entry, 'storeName');
|
|
241
|
-
if (
|
|
242
|
-
typeof subject !== 'string' ||
|
|
243
|
-
subject.trim().length === 0 ||
|
|
244
|
-
subjectNames.has(subject) ||
|
|
245
|
-
typeof storeName !== 'string' ||
|
|
246
|
-
storeName.trim().length === 0 ||
|
|
247
|
-
storeNames.has(storeName)
|
|
248
|
-
) {
|
|
249
|
-
throw new TypeError('invalid subject metadata');
|
|
250
|
-
}
|
|
251
|
-
subjectNames.add(subject);
|
|
252
|
-
storeNames.add(storeName);
|
|
212
|
+
validateRegistrySubject(entry, subjectNames, storeNames);
|
|
253
213
|
}
|
|
254
214
|
}
|
|
255
215
|
|
|
216
|
+
function isBlank(value: unknown): boolean {
|
|
217
|
+
return typeof value !== 'string' || value.trim().length === 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function validateRegistrySubject(
|
|
221
|
+
entry: unknown,
|
|
222
|
+
subjectNames: Set<string>,
|
|
223
|
+
storeNames: Set<string>,
|
|
224
|
+
): void {
|
|
225
|
+
if (typeof entry !== 'object' || entry === null) {
|
|
226
|
+
throw new TypeError('invalid subject entry');
|
|
227
|
+
}
|
|
228
|
+
const subject: unknown = Reflect.get(entry, 'subject');
|
|
229
|
+
const storeName: unknown = Reflect.get(entry, 'storeName');
|
|
230
|
+
if (
|
|
231
|
+
typeof subject !== 'string' ||
|
|
232
|
+
typeof storeName !== 'string' ||
|
|
233
|
+
isBlank(subject) ||
|
|
234
|
+
isBlank(storeName) ||
|
|
235
|
+
subjectNames.has(subject) ||
|
|
236
|
+
storeNames.has(storeName)
|
|
237
|
+
) {
|
|
238
|
+
throw new TypeError('invalid subject metadata');
|
|
239
|
+
}
|
|
240
|
+
subjectNames.add(subject);
|
|
241
|
+
storeNames.add(storeName);
|
|
242
|
+
}
|
|
243
|
+
|
|
256
244
|
const registryQueues = new WeakMap<object, Promise<void>>();
|
|
257
245
|
|
|
258
246
|
function withRegistryLock<TResult>(
|
|
@@ -349,284 +337,6 @@ export function removeScopedPersistenceRegistryEntry(
|
|
|
349
337
|
);
|
|
350
338
|
}
|
|
351
339
|
|
|
352
|
-
export interface ClosableScopedPersistence {
|
|
353
|
-
close(): Promise<void>;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
export interface OpenScopedPersistenceContext {
|
|
357
|
-
readonly subject: string;
|
|
358
|
-
readonly storeName: string;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
export interface ScopedPersistencePartition<
|
|
362
|
-
TPersistence extends ClosableScopedPersistence,
|
|
363
|
-
> extends OpenScopedPersistenceContext {
|
|
364
|
-
readonly persistence: TPersistence;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
export type ProfileErasureCleanup = 'pending' | 'complete' | 'failed';
|
|
368
|
-
|
|
369
|
-
export type ScopedPersistenceLifecycleState<TPersistence extends ClosableScopedPersistence> =
|
|
370
|
-
| { readonly status: 'signed-out' }
|
|
371
|
-
| { readonly status: 'initializing'; readonly subject: string }
|
|
372
|
-
| {
|
|
373
|
-
readonly status: 'ready';
|
|
374
|
-
readonly partition: ScopedPersistencePartition<TPersistence>;
|
|
375
|
-
}
|
|
376
|
-
| {
|
|
377
|
-
readonly status: 'blocked';
|
|
378
|
-
readonly reason: 'identity-mismatch' | 'initialization-failed' | 'session-expired';
|
|
379
|
-
readonly error: Error;
|
|
380
|
-
}
|
|
381
|
-
| {
|
|
382
|
-
readonly status: 'blocked';
|
|
383
|
-
readonly reason: 'profile-erased';
|
|
384
|
-
readonly cleanup: ProfileErasureCleanup;
|
|
385
|
-
readonly error: Error;
|
|
386
|
-
};
|
|
387
|
-
|
|
388
|
-
export interface ScopedPersistenceLifecycleOptions<
|
|
389
|
-
TPersistence extends ClosableScopedPersistence,
|
|
390
|
-
> extends Omit<ResolveScopedStoreOptions, 'subject'> {
|
|
391
|
-
/** Overrides registry resolution while retaining lifecycle transition guarantees. */
|
|
392
|
-
readonly resolveStore?: ScopedPersistenceResolver;
|
|
393
|
-
readonly open: (context: OpenScopedPersistenceContext) => Promise<TPersistence>;
|
|
394
|
-
/** Deletes a closed partition. Required by eraseActivePartition and handleProfileErased. */
|
|
395
|
-
readonly erase?: (context: OpenScopedPersistenceContext) => Promise<void>;
|
|
396
|
-
readonly resetUserScopedState: () => MaybePromise<void>;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Serializes identity transitions, hides stale handles immediately, and only
|
|
401
|
-
* publishes a partition after the product's open/migrate/hydrate hook resolves.
|
|
402
|
-
*/
|
|
403
|
-
export class ScopedPersistenceLifecycle<TPersistence extends ClosableScopedPersistence> {
|
|
404
|
-
private activePartition: ScopedPersistencePartition<TPersistence> | undefined;
|
|
405
|
-
private lastSelectedPartition: OpenScopedPersistenceContext | undefined;
|
|
406
|
-
private erasedProfile:
|
|
407
|
-
{ readonly subject: string; cleanup: ProfileErasureCleanup; error: Error } | undefined;
|
|
408
|
-
private transitionTail: Promise<void> = Promise.resolve();
|
|
409
|
-
private revision = 0;
|
|
410
|
-
private lifecycleState: ScopedPersistenceLifecycleState<TPersistence> = {
|
|
411
|
-
status: 'signed-out',
|
|
412
|
-
};
|
|
413
|
-
|
|
414
|
-
public constructor(private readonly options: ScopedPersistenceLifecycleOptions<TPersistence>) {}
|
|
415
|
-
|
|
416
|
-
public get state(): ScopedPersistenceLifecycleState<TPersistence> {
|
|
417
|
-
return this.lifecycleState;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
/** Returns no persistence while a transition is pending or blocked. */
|
|
421
|
-
public current(): ScopedPersistencePartition<TPersistence> | undefined {
|
|
422
|
-
return this.lifecycleState.status === 'ready' ? this.activePartition : undefined;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
public selectSubject(
|
|
426
|
-
subject: string,
|
|
427
|
-
): Promise<ScopedPersistencePartition<TPersistence> | undefined> {
|
|
428
|
-
requiredIdentityPart(subject, 'Authenticated subject');
|
|
429
|
-
if (this.lifecycleState.status === 'ready' && this.activePartition?.subject === subject) {
|
|
430
|
-
return Promise.resolve(this.activePartition);
|
|
431
|
-
}
|
|
432
|
-
const revision = ++this.revision;
|
|
433
|
-
this.lifecycleState = { status: 'initializing', subject };
|
|
434
|
-
return this.enqueue(() => this.selectSubjectLocked(subject, revision));
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
public clear(): Promise<void> {
|
|
438
|
-
const revision = ++this.revision;
|
|
439
|
-
this.lifecycleState = { status: 'signed-out' };
|
|
440
|
-
return this.enqueue(async () => {
|
|
441
|
-
await this.closeAndReset();
|
|
442
|
-
if (revision === this.revision) this.lifecycleState = { status: 'signed-out' };
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
/** Closes and deletes the active partition, then removes its registry mapping. */
|
|
447
|
-
public eraseActivePartition(): Promise<boolean> {
|
|
448
|
-
const partition = this.current();
|
|
449
|
-
if (partition === undefined) return Promise.resolve(false);
|
|
450
|
-
const erase = this.options.erase;
|
|
451
|
-
if (erase === undefined) {
|
|
452
|
-
return Promise.reject(
|
|
453
|
-
new Error('Active partition erasure requires a configured persistence erase hook.'),
|
|
454
|
-
);
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
const revision = ++this.revision;
|
|
458
|
-
this.activePartition = undefined;
|
|
459
|
-
this.lifecycleState = { status: 'signed-out' };
|
|
460
|
-
return this.enqueue(async () => {
|
|
461
|
-
await this.closePartitionAndReset(partition);
|
|
462
|
-
await this.erasePartition(partition, erase);
|
|
463
|
-
if (revision === this.revision) this.lifecycleState = { status: 'signed-out' };
|
|
464
|
-
return true;
|
|
465
|
-
});
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
/** Erases the last selected partition, even after session cleanup closed it. */
|
|
469
|
-
public handleProfileErased(): Promise<void> {
|
|
470
|
-
const erase = this.options.erase;
|
|
471
|
-
const revision = ++this.revision;
|
|
472
|
-
let cleanup: ProfileErasureCleanup = 'pending';
|
|
473
|
-
let error = new Error('The server profile was erased; sign in again to continue.');
|
|
474
|
-
this.lifecycleState = { status: 'blocked', reason: 'profile-erased', cleanup, error };
|
|
475
|
-
return this.enqueue(async () => {
|
|
476
|
-
const partition = this.lastSelectedPartition;
|
|
477
|
-
if (partition !== undefined)
|
|
478
|
-
this.erasedProfile = { subject: partition.subject, cleanup, error };
|
|
479
|
-
try {
|
|
480
|
-
if (partition !== undefined) await this.closeAndReset();
|
|
481
|
-
if (erase === undefined) {
|
|
482
|
-
throw new Error('Active partition erasure requires a configured persistence erase hook.');
|
|
483
|
-
}
|
|
484
|
-
if (partition !== undefined) await this.erasePartition(partition, erase);
|
|
485
|
-
cleanup = 'complete';
|
|
486
|
-
} catch (cause) {
|
|
487
|
-
cleanup = 'failed';
|
|
488
|
-
error = cause instanceof Error ? cause : new Error('Local profile erasure failed.');
|
|
489
|
-
throw cause;
|
|
490
|
-
} finally {
|
|
491
|
-
if (this.erasedProfile !== undefined && this.erasedProfile.subject === partition?.subject) {
|
|
492
|
-
this.erasedProfile.cleanup = cleanup;
|
|
493
|
-
this.erasedProfile.error = error;
|
|
494
|
-
}
|
|
495
|
-
if (revision === this.revision) {
|
|
496
|
-
this.lifecycleState = { status: 'blocked', reason: 'profile-erased', cleanup, error };
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
});
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
/** Terminal auth expiry closes the current partition and remains blocked. */
|
|
503
|
-
public handleSessionExpired(): Promise<void> {
|
|
504
|
-
const revision = ++this.revision;
|
|
505
|
-
const error = new Error('The authenticated session expired; sign in again to continue.');
|
|
506
|
-
this.lifecycleState = { status: 'blocked', reason: 'session-expired', error };
|
|
507
|
-
return this.enqueue(async () => {
|
|
508
|
-
await this.closeAndReset();
|
|
509
|
-
if (revision === this.revision) {
|
|
510
|
-
this.lifecycleState = { status: 'blocked', reason: 'session-expired', error };
|
|
511
|
-
}
|
|
512
|
-
});
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
/** Blocks an already-open partition after a server subject recheck fails. */
|
|
516
|
-
public blockIdentityMismatch(error: PersistenceIdentityMismatchError): Promise<void> {
|
|
517
|
-
const revision = ++this.revision;
|
|
518
|
-
this.lifecycleState = { status: 'blocked', reason: 'identity-mismatch', error };
|
|
519
|
-
return this.enqueue(async () => {
|
|
520
|
-
await this.closeAndReset();
|
|
521
|
-
if (revision === this.revision) {
|
|
522
|
-
this.lifecycleState = { status: 'blocked', reason: 'identity-mismatch', error };
|
|
523
|
-
}
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
private enqueue<TResult>(operation: () => Promise<TResult>): Promise<TResult> {
|
|
528
|
-
const result = this.transitionTail.then(operation);
|
|
529
|
-
this.transitionTail = result.then(
|
|
530
|
-
() => undefined,
|
|
531
|
-
() => undefined,
|
|
532
|
-
);
|
|
533
|
-
return result;
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
private async selectSubjectLocked(
|
|
537
|
-
subject: string,
|
|
538
|
-
revision: number,
|
|
539
|
-
): Promise<ScopedPersistencePartition<TPersistence> | undefined> {
|
|
540
|
-
try {
|
|
541
|
-
await this.closeAndReset();
|
|
542
|
-
if (revision !== this.revision) return undefined;
|
|
543
|
-
if (this.erasedProfile?.subject === subject) {
|
|
544
|
-
this.lifecycleState = {
|
|
545
|
-
status: 'blocked',
|
|
546
|
-
reason: 'profile-erased',
|
|
547
|
-
cleanup: this.erasedProfile.cleanup,
|
|
548
|
-
error: this.erasedProfile.error,
|
|
549
|
-
};
|
|
550
|
-
return undefined;
|
|
551
|
-
}
|
|
552
|
-
if (this.lastSelectedPartition?.subject !== subject) this.lastSelectedPartition = undefined;
|
|
553
|
-
const resolved =
|
|
554
|
-
this.options.resolveStore === undefined
|
|
555
|
-
? await resolveScopedStore({ ...this.options, subject })
|
|
556
|
-
: await this.options.resolveStore(subject);
|
|
557
|
-
requiredIdentityPart(resolved.storeName, 'Resolved persistence store name');
|
|
558
|
-
if (revision !== this.revision) return undefined;
|
|
559
|
-
const context = {
|
|
560
|
-
subject,
|
|
561
|
-
storeName: resolved.storeName,
|
|
562
|
-
};
|
|
563
|
-
this.lastSelectedPartition = context;
|
|
564
|
-
const persistence = await this.options.open(context);
|
|
565
|
-
if (revision !== this.revision) {
|
|
566
|
-
await persistence.close();
|
|
567
|
-
await this.options.resetUserScopedState();
|
|
568
|
-
return undefined;
|
|
569
|
-
}
|
|
570
|
-
const partition = { subject, storeName: resolved.storeName, persistence };
|
|
571
|
-
this.activePartition = partition;
|
|
572
|
-
this.lifecycleState = { status: 'ready', partition };
|
|
573
|
-
return partition;
|
|
574
|
-
} catch (cause) {
|
|
575
|
-
if (revision === this.revision) {
|
|
576
|
-
const error =
|
|
577
|
-
cause instanceof Error ? cause : new Error('Local data initialization failed.');
|
|
578
|
-
this.lifecycleState = {
|
|
579
|
-
status: 'blocked',
|
|
580
|
-
reason: isPersistenceIdentityMismatchError(cause)
|
|
581
|
-
? 'identity-mismatch'
|
|
582
|
-
: 'initialization-failed',
|
|
583
|
-
error,
|
|
584
|
-
};
|
|
585
|
-
}
|
|
586
|
-
throw cause;
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
private async erasePartition(
|
|
591
|
-
partition: OpenScopedPersistenceContext,
|
|
592
|
-
erase: NonNullable<ScopedPersistenceLifecycleOptions<TPersistence>['erase']>,
|
|
593
|
-
): Promise<void> {
|
|
594
|
-
await erase({ subject: partition.subject, storeName: partition.storeName });
|
|
595
|
-
await removeScopedPersistenceRegistryEntry({
|
|
596
|
-
namespace: this.options.namespace,
|
|
597
|
-
subject: partition.subject,
|
|
598
|
-
registry: this.options.registry,
|
|
599
|
-
});
|
|
600
|
-
if (this.lastSelectedPartition?.subject === partition.subject) {
|
|
601
|
-
this.lastSelectedPartition = undefined;
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
private async closeAndReset(): Promise<void> {
|
|
606
|
-
const partition = this.activePartition;
|
|
607
|
-
this.activePartition = undefined;
|
|
608
|
-
await this.closePartitionAndReset(partition);
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
private async closePartitionAndReset(
|
|
612
|
-
partition: ScopedPersistencePartition<TPersistence> | undefined,
|
|
613
|
-
): Promise<void> {
|
|
614
|
-
let closeFailure: unknown;
|
|
615
|
-
try {
|
|
616
|
-
await partition?.persistence.close();
|
|
617
|
-
} catch (cause) {
|
|
618
|
-
closeFailure = cause;
|
|
619
|
-
} finally {
|
|
620
|
-
await this.options.resetUserScopedState();
|
|
621
|
-
}
|
|
622
|
-
if (closeFailure !== undefined) {
|
|
623
|
-
throw closeFailure instanceof Error
|
|
624
|
-
? closeFailure
|
|
625
|
-
: new Error('Closing the active local-data partition failed.');
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
|
|
630
340
|
export interface RecheckServerSubjectOptions<TResult> {
|
|
631
341
|
readonly partitionSubject: string;
|
|
632
342
|
readonly readServerSubject: () => Promise<string>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { expect, it, vi } from 'vitest';
|
|
2
|
+
import { isRecord } from '@baukit/type-guards';
|
|
2
3
|
|
|
3
4
|
import fixtureCorpus from '../../../../fixtures/import-envelope/import-envelope-v1.json' with { type: 'json' };
|
|
4
5
|
|
|
@@ -72,10 +73,6 @@ const conformanceFixtures: ImportEnvelopeConformanceFixtures = {
|
|
|
72
73
|
failureHalfwayThroughCommit: fixture('failure-halfway-through-commit'),
|
|
73
74
|
};
|
|
74
75
|
|
|
75
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
76
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
76
|
function decodeFixtureEnvelope(source: ImportEnvelopeSource) {
|
|
80
77
|
if (typeof source !== 'string') throw new Error('fixture_source_invalid');
|
|
81
78
|
const value: unknown = JSON.parse(source);
|
package/src/import-envelope.ts
CHANGED
|
@@ -120,13 +120,11 @@ export async function prepareImportEnvelope<TContext, TCollection extends string
|
|
|
120
120
|
});
|
|
121
121
|
}
|
|
122
122
|
const allowedFields = allowlistFor(options.fieldAllowlist, row.collection, rowIndex);
|
|
123
|
-
const fields = sanitizeFields(
|
|
124
|
-
row.
|
|
125
|
-
allowedFields,
|
|
126
|
-
row.collection,
|
|
123
|
+
const fields = sanitizeFields(row.fields, allowedFields, {
|
|
124
|
+
collection: row.collection,
|
|
127
125
|
rowIndex,
|
|
128
|
-
options.limits.maxStringBytes,
|
|
129
|
-
);
|
|
126
|
+
maxStringBytes: options.limits.maxStringBytes,
|
|
127
|
+
});
|
|
130
128
|
decodedRows.push(
|
|
131
129
|
await options.decodeRow({
|
|
132
130
|
context: envelope.context,
|
|
@@ -179,9 +177,11 @@ function allowlistFor<TCollection extends string>(
|
|
|
179
177
|
function sanitizeFields(
|
|
180
178
|
fields: Readonly<Record<string, unknown>>,
|
|
181
179
|
allowedFields: ReadonlySet<string>,
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
180
|
+
{
|
|
181
|
+
collection,
|
|
182
|
+
rowIndex,
|
|
183
|
+
maxStringBytes,
|
|
184
|
+
}: { readonly collection: string; readonly rowIndex: number; readonly maxStringBytes: number },
|
|
185
185
|
): Readonly<Record<string, unknown>> {
|
|
186
186
|
if (Object.getOwnPropertySymbols(fields).length > 0) {
|
|
187
187
|
throw new ImportEnvelopeError('invalid_row_value', { collection, rowIndex });
|
package/src/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from './contracts.js';
|
|
|
2
2
|
export * from './erasure.js';
|
|
3
3
|
export * from './export.js';
|
|
4
4
|
export * from './identity.js';
|
|
5
|
+
export * from './scoped-lifecycle.js';
|
|
5
6
|
export * from './import-envelope.js';
|
|
6
7
|
export * from './memory.js';
|
|
7
8
|
export * from './web-storage.js';
|
package/src/limits.ts
CHANGED
|
@@ -197,7 +197,7 @@ export function parseLimitsPolicy<const Schema extends LimitsPolicySchema>(
|
|
|
197
197
|
const allowZero = new Set(schema.allowZero ?? []);
|
|
198
198
|
const rangeKeys = new Set(schema.rangeKeys ?? []);
|
|
199
199
|
for (const [section, keys] of Object.entries(schema.sections)) {
|
|
200
|
-
checkPolicySection(policy, section, keys, allowZero, rangeKeys);
|
|
200
|
+
checkPolicySection(policy, { section, keys, allowZero, rangeKeys });
|
|
201
201
|
}
|
|
202
202
|
return policy as LimitsPolicy<Schema>;
|
|
203
203
|
}
|
|
@@ -229,18 +229,22 @@ function assertPolicySchema(schema: LimitsPolicySchema): void {
|
|
|
229
229
|
}
|
|
230
230
|
for (const path of schema.rangeKeys ?? []) {
|
|
231
231
|
if (!known.has(path)) throw new TypeError(`Limits policy rangeKeys names unknown ${path}`);
|
|
232
|
-
if (schema.allowZero?.includes(path)) {
|
|
232
|
+
if (schema.allowZero?.includes(path) === true) {
|
|
233
233
|
throw new TypeError(`Limits policy range key ${path} cannot also use allowZero`);
|
|
234
234
|
}
|
|
235
235
|
}
|
|
236
236
|
}
|
|
237
237
|
|
|
238
|
+
interface PolicySection {
|
|
239
|
+
readonly section: string;
|
|
240
|
+
readonly keys: readonly string[];
|
|
241
|
+
readonly allowZero: ReadonlySet<string>;
|
|
242
|
+
readonly rangeKeys: ReadonlySet<string>;
|
|
243
|
+
}
|
|
244
|
+
|
|
238
245
|
function checkPolicySection(
|
|
239
246
|
policy: Record<string, unknown>,
|
|
240
|
-
section:
|
|
241
|
-
keys: readonly string[],
|
|
242
|
-
allowZero: ReadonlySet<string>,
|
|
243
|
-
rangeKeys: ReadonlySet<string>,
|
|
247
|
+
{ section, keys, allowZero, rangeKeys }: PolicySection,
|
|
244
248
|
): void {
|
|
245
249
|
const path = `${POLICY_ROOT}.${section}`;
|
|
246
250
|
const values = expectPolicyObject(policy[section], path);
|
|
@@ -352,37 +356,40 @@ function assertJsonValue(value: unknown, ancestors: WeakSet<object>): void {
|
|
|
352
356
|
|
|
353
357
|
ancestors.add(value);
|
|
354
358
|
try {
|
|
355
|
-
|
|
356
|
-
throw new ResourceMeasurementError('unsupported_json_value');
|
|
357
|
-
}
|
|
358
|
-
if (Array.isArray(value)) {
|
|
359
|
-
for (let index = 0; index < value.length; index += 1) {
|
|
360
|
-
const descriptor = Object.getOwnPropertyDescriptor(value, index);
|
|
361
|
-
if (descriptor === undefined || !('value' in descriptor)) {
|
|
362
|
-
throw new ResourceMeasurementError('unsupported_json_value');
|
|
363
|
-
}
|
|
364
|
-
assertJsonValue(descriptor.value, ancestors);
|
|
365
|
-
}
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
const prototype = Object.getPrototypeOf(value) as object | null;
|
|
370
|
-
if (prototype !== Object.prototype && prototype !== null) {
|
|
371
|
-
throw new ResourceMeasurementError('unsupported_json_value');
|
|
372
|
-
}
|
|
373
|
-
for (const key of Object.keys(value)) {
|
|
374
|
-
unicodeScalars(key);
|
|
375
|
-
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
376
|
-
if (descriptor === undefined || !('value' in descriptor)) {
|
|
377
|
-
throw new ResourceMeasurementError('unsupported_json_value');
|
|
378
|
-
}
|
|
379
|
-
assertJsonValue(descriptor.value, ancestors);
|
|
380
|
-
}
|
|
359
|
+
assertJsonContainer(value, ancestors);
|
|
381
360
|
} finally {
|
|
382
361
|
ancestors.delete(value);
|
|
383
362
|
}
|
|
384
363
|
}
|
|
385
364
|
|
|
365
|
+
function assertJsonContainer(value: object, ancestors: WeakSet<object>): void {
|
|
366
|
+
if (Object.getOwnPropertySymbols(value).length > 0) {
|
|
367
|
+
throw new ResourceMeasurementError('unsupported_json_value');
|
|
368
|
+
}
|
|
369
|
+
if (Array.isArray(value)) {
|
|
370
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
371
|
+
assertJsonValue(ownDataValue(value, index), ancestors);
|
|
372
|
+
}
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const prototype = Object.getPrototypeOf(value) as object | null;
|
|
376
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
377
|
+
throw new ResourceMeasurementError('unsupported_json_value');
|
|
378
|
+
}
|
|
379
|
+
for (const key of Object.keys(value)) {
|
|
380
|
+
unicodeScalars(key);
|
|
381
|
+
assertJsonValue(ownDataValue(value, key), ancestors);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function ownDataValue(value: object, key: PropertyKey): unknown {
|
|
386
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
387
|
+
if (descriptor === undefined || !('value' in descriptor)) {
|
|
388
|
+
throw new ResourceMeasurementError('unsupported_json_value');
|
|
389
|
+
}
|
|
390
|
+
return descriptor.value;
|
|
391
|
+
}
|
|
392
|
+
|
|
386
393
|
function utf8ByteLength(value: string): number {
|
|
387
394
|
let length = 0;
|
|
388
395
|
for (const scalar of unicodeScalars(value)) {
|