@rdlabo/workers-hono-kit 0.10.4 → 0.10.5
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/offline/index.d.ts
CHANGED
|
@@ -16,3 +16,5 @@ export { assertOfflineJournalCursorRetained, compactOfflineJournal, OfflineJourn
|
|
|
16
16
|
export type { CompactOfflineJournalOptions, OfflineJournalRetentionCandidate, OfflineJournalRetentionStore, OfflineJournalRetentionTransaction, } from './journal-retention.js';
|
|
17
17
|
export { assertOfflineJournalCoverage, runOfflineJournalMutation } from './journal-mutation.js';
|
|
18
18
|
export type { OfflineJournalMutationChange, OfflineJournalMutationStore, OfflineJournalMutationTransaction, } from './journal-mutation.js';
|
|
19
|
+
export { defineOfflineWireCompatibility, resolveOfflineWireCompatibility } from './wire-compatibility.js';
|
|
20
|
+
export type { OfflineWireAcceptedFingerprint, OfflineWireCompatibility, OfflineWireCompatibilityResolution, OfflineWireFingerprint, } from './wire-compatibility.js';
|
package/dist/offline/index.js
CHANGED
|
@@ -12,3 +12,4 @@ export { defineRestDbMethodConverter } from './rest-db-method-converter.js';
|
|
|
12
12
|
export { decodeOfflineSnapshotCursor, encodeOfflineSnapshotCursor } from './snapshot-cursor.js';
|
|
13
13
|
export { assertOfflineJournalCursorRetained, compactOfflineJournal, OfflineJournalRebaselineRequiredError, } from './journal-retention.js';
|
|
14
14
|
export { assertOfflineJournalCoverage, runOfflineJournalMutation } from './journal-mutation.js';
|
|
15
|
+
export { defineOfflineWireCompatibility, resolveOfflineWireCompatibility } from './wire-compatibility.js';
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exact offline wire protocol identity: integer version plus content hash.
|
|
3
|
+
*
|
|
4
|
+
* Products own how the hash is computed; this kit never materializes schema
|
|
5
|
+
* projections or storage. Resolution always requires an exact `{version,hash}` pair.
|
|
6
|
+
*/
|
|
7
|
+
export interface OfflineWireFingerprint {
|
|
8
|
+
/** Monotonic published protocol version. */
|
|
9
|
+
readonly version: number;
|
|
10
|
+
/** Content fingerprint for that published version. */
|
|
11
|
+
readonly hash: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A previously published fingerprint that remains accepted until an explicit expiry.
|
|
15
|
+
*
|
|
16
|
+
* Non-current entries must name the product adapter/projection that serves that wire
|
|
17
|
+
* shape. Silently allowlisting a hash alone is forbidden.
|
|
18
|
+
*/
|
|
19
|
+
export interface OfflineWireAcceptedFingerprint extends OfflineWireFingerprint {
|
|
20
|
+
/** Instant at which acceptance ends (exclusive); later clocks reject the fingerprint. */
|
|
21
|
+
readonly expiresAt: Date;
|
|
22
|
+
/** Product-owned adapter or projection identifier for this prior wire shape. */
|
|
23
|
+
readonly adapterId: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Validated current fingerprint plus optional accepted prior fingerprints.
|
|
27
|
+
*
|
|
28
|
+
* Produced only by {@link defineOfflineWireCompatibility}.
|
|
29
|
+
*/
|
|
30
|
+
export interface OfflineWireCompatibility {
|
|
31
|
+
/** The currently published protocol fingerprint. */
|
|
32
|
+
readonly current: OfflineWireFingerprint;
|
|
33
|
+
/** Prior fingerprints accepted until their explicit expiry. */
|
|
34
|
+
readonly accepted: readonly OfflineWireAcceptedFingerprint[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Successful resolution of an incoming fingerprint against a compatibility table.
|
|
38
|
+
*
|
|
39
|
+
* Unmatched, expired, or inexact fingerprints resolve to `undefined` so products can map
|
|
40
|
+
* the miss to their own conflict response (for example HTTP 409).
|
|
41
|
+
*/
|
|
42
|
+
export type OfflineWireCompatibilityResolution = {
|
|
43
|
+
readonly kind: 'current';
|
|
44
|
+
readonly fingerprint: OfflineWireFingerprint;
|
|
45
|
+
} | {
|
|
46
|
+
readonly kind: 'accepted';
|
|
47
|
+
readonly fingerprint: OfflineWireFingerprint;
|
|
48
|
+
readonly adapterId: string;
|
|
49
|
+
readonly expiresAt: Date;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Validates a current fingerprint plus optional accepted prior fingerprints.
|
|
53
|
+
*
|
|
54
|
+
* Rejects duplicate versions, duplicate hashes, invalid fingerprints, invalid expiry
|
|
55
|
+
* instants, and accepted entries that omit a product adapter/projection identifier.
|
|
56
|
+
*
|
|
57
|
+
* @param options - Current fingerprint and optional accepted prior fingerprints.
|
|
58
|
+
* @returns A validated compatibility table safe to pass to {@link resolveOfflineWireCompatibility}.
|
|
59
|
+
*/
|
|
60
|
+
export declare function defineOfflineWireCompatibility(options: {
|
|
61
|
+
readonly current: OfflineWireFingerprint;
|
|
62
|
+
readonly accepted?: readonly OfflineWireAcceptedFingerprint[];
|
|
63
|
+
}): OfflineWireCompatibility;
|
|
64
|
+
/**
|
|
65
|
+
* Resolves an incoming `{version,hash}` against a validated compatibility table.
|
|
66
|
+
*
|
|
67
|
+
* Matches only exact fingerprints. Current always wins; accepted prior fingerprints match
|
|
68
|
+
* only while the injectable clock is strictly before their `expiresAt`.
|
|
69
|
+
*
|
|
70
|
+
* @param compatibility - Table from {@link defineOfflineWireCompatibility}.
|
|
71
|
+
* @param fingerprint - Incoming client fingerprint.
|
|
72
|
+
* @param clock - Injectable wall clock; defaults to the system wall clock.
|
|
73
|
+
* @returns The match, or `undefined` when the fingerprint is unknown, inexact, or expired.
|
|
74
|
+
*/
|
|
75
|
+
export declare function resolveOfflineWireCompatibility(compatibility: OfflineWireCompatibility, fingerprint: OfflineWireFingerprint, clock?: () => Date): OfflineWireCompatibilityResolution | undefined;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates a current fingerprint plus optional accepted prior fingerprints.
|
|
3
|
+
*
|
|
4
|
+
* Rejects duplicate versions, duplicate hashes, invalid fingerprints, invalid expiry
|
|
5
|
+
* instants, and accepted entries that omit a product adapter/projection identifier.
|
|
6
|
+
*
|
|
7
|
+
* @param options - Current fingerprint and optional accepted prior fingerprints.
|
|
8
|
+
* @returns A validated compatibility table safe to pass to {@link resolveOfflineWireCompatibility}.
|
|
9
|
+
*/
|
|
10
|
+
export function defineOfflineWireCompatibility(options) {
|
|
11
|
+
const current = normalizeFingerprint(options.current, 'current');
|
|
12
|
+
const acceptedInput = options.accepted ?? [];
|
|
13
|
+
const versions = new Set([current.version]);
|
|
14
|
+
const hashes = new Set([current.hash]);
|
|
15
|
+
const accepted = [];
|
|
16
|
+
for (const [index, entry] of acceptedInput.entries()) {
|
|
17
|
+
const label = `accepted[${index}]`;
|
|
18
|
+
const fingerprint = normalizeFingerprint(entry, label);
|
|
19
|
+
if (versions.has(fingerprint.version)) {
|
|
20
|
+
throw new Error(`Offline wire compatibility rejects duplicate version ${fingerprint.version}.`);
|
|
21
|
+
}
|
|
22
|
+
if (hashes.has(fingerprint.hash)) {
|
|
23
|
+
throw new Error(`Offline wire compatibility rejects duplicate hash '${fingerprint.hash}'.`);
|
|
24
|
+
}
|
|
25
|
+
if (!(entry.expiresAt instanceof Date) || Number.isNaN(entry.expiresAt.getTime())) {
|
|
26
|
+
throw new RangeError(`Offline wire compatibility ${label}.expiresAt must be a valid Date.`);
|
|
27
|
+
}
|
|
28
|
+
const adapterId = entry.adapterId;
|
|
29
|
+
if (typeof adapterId !== 'string' || adapterId.trim().length === 0) {
|
|
30
|
+
throw new Error(`Offline wire compatibility ${label} requires a non-empty product adapter/projection identifier.`);
|
|
31
|
+
}
|
|
32
|
+
versions.add(fingerprint.version);
|
|
33
|
+
hashes.add(fingerprint.hash);
|
|
34
|
+
accepted.push({
|
|
35
|
+
version: fingerprint.version,
|
|
36
|
+
hash: fingerprint.hash,
|
|
37
|
+
expiresAt: entry.expiresAt,
|
|
38
|
+
adapterId,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return { current, accepted };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Resolves an incoming `{version,hash}` against a validated compatibility table.
|
|
45
|
+
*
|
|
46
|
+
* Matches only exact fingerprints. Current always wins; accepted prior fingerprints match
|
|
47
|
+
* only while the injectable clock is strictly before their `expiresAt`.
|
|
48
|
+
*
|
|
49
|
+
* @param compatibility - Table from {@link defineOfflineWireCompatibility}.
|
|
50
|
+
* @param fingerprint - Incoming client fingerprint.
|
|
51
|
+
* @param clock - Injectable wall clock; defaults to the system wall clock.
|
|
52
|
+
* @returns The match, or `undefined` when the fingerprint is unknown, inexact, or expired.
|
|
53
|
+
*/
|
|
54
|
+
export function resolveOfflineWireCompatibility(compatibility, fingerprint, clock = () => new Date()) {
|
|
55
|
+
const incoming = normalizeFingerprint(fingerprint, 'incoming');
|
|
56
|
+
if (incoming.version === compatibility.current.version) {
|
|
57
|
+
if (incoming.hash !== compatibility.current.hash) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
return { kind: 'current', fingerprint: compatibility.current };
|
|
61
|
+
}
|
|
62
|
+
const now = clock();
|
|
63
|
+
if (Number.isNaN(now.getTime())) {
|
|
64
|
+
throw new RangeError('Offline wire compatibility clock must return a valid Date.');
|
|
65
|
+
}
|
|
66
|
+
const nowMs = now.getTime();
|
|
67
|
+
for (const entry of compatibility.accepted) {
|
|
68
|
+
if (entry.version !== incoming.version) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (entry.hash !== incoming.hash) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
if (nowMs >= entry.expiresAt.getTime()) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
kind: 'accepted',
|
|
79
|
+
fingerprint: { version: entry.version, hash: entry.hash },
|
|
80
|
+
adapterId: entry.adapterId,
|
|
81
|
+
expiresAt: entry.expiresAt,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
function normalizeFingerprint(fingerprint, label) {
|
|
87
|
+
if (!Number.isSafeInteger(fingerprint.version) || fingerprint.version < 0) {
|
|
88
|
+
throw new RangeError(`Offline wire compatibility ${label}.version must be a non-negative safe integer.`);
|
|
89
|
+
}
|
|
90
|
+
if (typeof fingerprint.hash !== 'string' || fingerprint.hash.trim().length === 0) {
|
|
91
|
+
throw new Error(`Offline wire compatibility ${label}.hash must be a non-empty string.`);
|
|
92
|
+
}
|
|
93
|
+
return { version: fingerprint.version, hash: fingerprint.hash };
|
|
94
|
+
}
|