@feltdb/core 0.11.7 → 0.11.8
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/create/package-versions.js +1 -1
- package/dist/create/server-source/crates/feltdb-server/src/backup.rs +290 -7
- package/dist/create/server-source/crates/feltdb-server/src/main.rs +302 -13
- package/dist/create/server-source/crates/feltdb-wasm/test_wasm.db.lock +0 -0
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +23 -2
- package/dist/embedded-query.d.ts.map +1 -1
- package/dist/embedded-query.js +20 -6
- package/dist/file-db.d.ts +17 -0
- package/dist/file-db.d.ts.map +1 -1
- package/dist/file-db.js +34 -2
- package/dist/runtime-snapshot.d.ts +76 -2
- package/dist/runtime-snapshot.d.ts.map +1 -1
- package/dist/runtime-snapshot.js +68 -7
- package/dist/stable-hash.d.ts +17 -0
- package/dist/stable-hash.d.ts.map +1 -0
- package/dist/stable-hash.js +82 -0
- package/dist/studio-app/assets/index-Ccecp6rc.js +30 -0
- package/dist/studio-app/index.html +1 -1
- package/package.json +1 -1
- package/dist/studio-app/assets/index-gR6d3Tys.js +0 -30
package/dist/file-db.js
CHANGED
|
@@ -6,6 +6,31 @@ import { decideTransaction, embeddedResult, EmbeddedTransactionRejected, } from
|
|
|
6
6
|
import { EmbeddedQueryEngine } from './embedded-query.js';
|
|
7
7
|
import { encodeRecord, scanJournal, JournalCorruptionError, PersistenceProfiler, } from './file-journal.js';
|
|
8
8
|
export { JournalCorruptionError } from './file-journal.js';
|
|
9
|
+
/** The newest local file snapshot format this build reads and writes. */
|
|
10
|
+
export const FILE_SNAPSHOT_FORMAT_VERSION = 2;
|
|
11
|
+
/**
|
|
12
|
+
* The runtime directory was written in a durable format newer than this build
|
|
13
|
+
* understands.
|
|
14
|
+
*
|
|
15
|
+
* Refused rather than recovered around: treating a newer snapshot as invalid
|
|
16
|
+
* and falling back to an older backup — or to an empty database — silently
|
|
17
|
+
* discards every commit the newer format carries. That is exactly what a
|
|
18
|
+
* package-only rollback onto upgraded data would do, so it fails closed.
|
|
19
|
+
*/
|
|
20
|
+
export class UnsupportedDurableFormatError extends Error {
|
|
21
|
+
statePath;
|
|
22
|
+
found;
|
|
23
|
+
code = 'UNSUPPORTED_DURABLE_FORMAT';
|
|
24
|
+
constructor(statePath, found) {
|
|
25
|
+
super(`UNSUPPORTED_DURABLE_FORMAT: ${statePath} is in local durable format ${found}; this FeltDB `
|
|
26
|
+
+ `build reads formats 1-${FILE_SNAPSHOT_FORMAT_VERSION}. It was written by a newer FeltDB. `
|
|
27
|
+
+ 'The database was not opened and was not modified. Upgrade FeltDB, or restore the backup '
|
|
28
|
+
+ 'taken before the upgrade.');
|
|
29
|
+
this.statePath = statePath;
|
|
30
|
+
this.found = found;
|
|
31
|
+
this.name = 'UnsupportedDurableFormatError';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
9
34
|
// Synchronous versions for sync API (will throw if called in browser)
|
|
10
35
|
function getFsSync() {
|
|
11
36
|
if (typeof nodeFs.existsSync === 'function')
|
|
@@ -98,6 +123,9 @@ export class FileJsDb {
|
|
|
98
123
|
parseSnapshot(path) {
|
|
99
124
|
const content = getFsSync().readFileSync(path, 'utf-8');
|
|
100
125
|
const snapshot = JSON.parse(content);
|
|
126
|
+
if (typeof snapshot.version === 'number' && snapshot.version > FILE_SNAPSHOT_FORMAT_VERSION) {
|
|
127
|
+
throw new UnsupportedDurableFormatError(path, snapshot.version);
|
|
128
|
+
}
|
|
101
129
|
const known = snapshot.version === 1
|
|
102
130
|
|| (snapshot.version === 2 && Number.isInteger(snapshot.journal?.generation) && Number.isInteger(snapshot.journal?.commit));
|
|
103
131
|
if (!known || !snapshot.meta || !snapshot.rows || !snapshot.events)
|
|
@@ -119,6 +147,8 @@ export class FileJsDb {
|
|
|
119
147
|
console.warn(`Invalid snapshot structure at ${this.statePath}, attempting backup recovery`);
|
|
120
148
|
}
|
|
121
149
|
catch (error) {
|
|
150
|
+
if (error instanceof UnsupportedDurableFormatError)
|
|
151
|
+
throw error;
|
|
122
152
|
if (!isMissing(error)) {
|
|
123
153
|
console.warn(`Failed to load snapshot from ${this.statePath}: ${error}`);
|
|
124
154
|
console.warn(`Attempting to recover from backup...`);
|
|
@@ -134,7 +164,9 @@ export class FileJsDb {
|
|
|
134
164
|
return { snapshot: parsed.snapshot, bytes: parsed.bytes, source: 'backup' };
|
|
135
165
|
}
|
|
136
166
|
}
|
|
137
|
-
catch {
|
|
167
|
+
catch (error) {
|
|
168
|
+
if (error instanceof UnsupportedDurableFormatError)
|
|
169
|
+
throw error;
|
|
138
170
|
// Backup is also corrupted or invalid
|
|
139
171
|
}
|
|
140
172
|
console.warn(`Backup recovery failed, reinitializing state`);
|
|
@@ -553,7 +585,7 @@ export class FileJsDb {
|
|
|
553
585
|
this.syncDirectory();
|
|
554
586
|
this.state.peers = [...this.peers];
|
|
555
587
|
const snapshot = {
|
|
556
|
-
version:
|
|
588
|
+
version: FILE_SNAPSHOT_FORMAT_VERSION,
|
|
557
589
|
timestamp: Date.now(),
|
|
558
590
|
meta: {
|
|
559
591
|
instance_id: this.origin,
|
|
@@ -132,6 +132,44 @@ export interface SnapshotStaleness {
|
|
|
132
132
|
/** Why currency could not be decided. */
|
|
133
133
|
reason?: string;
|
|
134
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Which authoritative state a published snapshot represents, stated explicitly.
|
|
137
|
+
*
|
|
138
|
+
* Two snapshots with the same `id` represent the same durable state, derived
|
|
139
|
+
* by the same definition, under the same authority. The identity is computed
|
|
140
|
+
* from what the snapshot already is — its revision, definition, and authority
|
|
141
|
+
* — rather than stored as parallel metadata that could disagree with it.
|
|
142
|
+
*/
|
|
143
|
+
export interface SnapshotIdentity {
|
|
144
|
+
/** `snap_` + SHA-256 over the fields below. Deterministic across handles and processes. */
|
|
145
|
+
id: string;
|
|
146
|
+
name: string;
|
|
147
|
+
/**
|
|
148
|
+
* The authoritative revision value, when the runtime has one. `null` when it
|
|
149
|
+
* does not, in which case `contentDigest` identifies the state instead.
|
|
150
|
+
*/
|
|
151
|
+
version: number | null;
|
|
152
|
+
/** The revision's consistency domain, when there is a revision. */
|
|
153
|
+
scope?: string;
|
|
154
|
+
/** Publication counter of the handle. Informational; not part of `id`. */
|
|
155
|
+
generation: number;
|
|
156
|
+
/** The source collections, sorted. */
|
|
157
|
+
sourceSet: readonly string[];
|
|
158
|
+
/** SHA-256 of the definition's sources, filters, and indexes. */
|
|
159
|
+
definitionDigest: string;
|
|
160
|
+
/** The authority the materialization read under (`tenant/…`, `platform`, or `unscoped`). */
|
|
161
|
+
authorityContext: string;
|
|
162
|
+
/**
|
|
163
|
+
* SHA-256 of the canonical materialized records. Present only when no
|
|
164
|
+
* authoritative revision exists, because then the content is the only honest
|
|
165
|
+
* identity there is.
|
|
166
|
+
*/
|
|
167
|
+
contentDigest?: string;
|
|
168
|
+
/** How `id` was derived. */
|
|
169
|
+
derivation: 'revision' | 'content';
|
|
170
|
+
/** When materialization completed. Reporting only; not part of `id`. */
|
|
171
|
+
createdAt: number;
|
|
172
|
+
}
|
|
135
173
|
export interface SnapshotIndexDescription {
|
|
136
174
|
collection: string;
|
|
137
175
|
name: string;
|
|
@@ -175,6 +213,14 @@ export interface SnapshotHandleExplanation extends SnapshotExplanation {
|
|
|
175
213
|
* claim.
|
|
176
214
|
*/
|
|
177
215
|
durableReads: number;
|
|
216
|
+
/**
|
|
217
|
+
* `refresh()` calls answered by the fast path: the active snapshot already
|
|
218
|
+
* represented the current authoritative revision, so nothing was read or
|
|
219
|
+
* rebuilt.
|
|
220
|
+
*/
|
|
221
|
+
reused: number;
|
|
222
|
+
/** The authority the handle materializes under. */
|
|
223
|
+
authorityContext: string;
|
|
178
224
|
}
|
|
179
225
|
export declare class SnapshotDefinitionError extends Error {
|
|
180
226
|
constructor(message: string);
|
|
@@ -209,6 +255,11 @@ export interface RuntimeSnapshot<D = undefined> {
|
|
|
209
255
|
/** Materialized records, across every source. */
|
|
210
256
|
readonly records: number;
|
|
211
257
|
readonly collections: readonly string[];
|
|
258
|
+
/**
|
|
259
|
+
* Which authoritative state this snapshot represents. Computed on first
|
|
260
|
+
* access and memoized, so the hot read path never pays for it.
|
|
261
|
+
*/
|
|
262
|
+
readonly identity: SnapshotIdentity;
|
|
212
263
|
get<T = Record<string, unknown>>(collection: string, id: string | number): T | undefined;
|
|
213
264
|
has(collection: string, id: string | number): boolean;
|
|
214
265
|
/** Every materialized record of one collection, in materialization order. Frozen. */
|
|
@@ -231,6 +282,17 @@ export interface RuntimeSnapshot<D = undefined> {
|
|
|
231
282
|
export interface SnapshotSourceRuntime {
|
|
232
283
|
/** Read every authoritative record of one collection. */
|
|
233
284
|
readRecords(collection: string): Promise<Array<Record<string, unknown>>>;
|
|
285
|
+
/**
|
|
286
|
+
* Read only the authoritative records that satisfy `where`, through the
|
|
287
|
+
* runtime's own bounded query (and its indexes, where it has them).
|
|
288
|
+
*
|
|
289
|
+
* Optional. When present it is the bounded source-selection fast path: a
|
|
290
|
+
* snapshot that selects ten records of a large collection reads ten records,
|
|
291
|
+
* not the collection. It must return exactly the records `readRecords` would
|
|
292
|
+
* after filtering by the same conditions — the build re-applies the filter
|
|
293
|
+
* regardless, so a runtime that over-returns cannot widen a snapshot.
|
|
294
|
+
*/
|
|
295
|
+
readSelected?(collection: string, where: SnapshotCondition[]): Promise<Array<Record<string, unknown>>>;
|
|
234
296
|
/** What freshness validation the underlying runtime can honestly support. */
|
|
235
297
|
freshness(): Promise<FreshnessCapability>;
|
|
236
298
|
/** The runtime's current revision. Called only when freshness advertises one. */
|
|
@@ -254,7 +316,10 @@ export declare class RuntimeSnapshotHandle<D = undefined> {
|
|
|
254
316
|
private durableReads;
|
|
255
317
|
private lastFailure?;
|
|
256
318
|
private closed;
|
|
257
|
-
|
|
319
|
+
private reused;
|
|
320
|
+
private readonly definitionDigest;
|
|
321
|
+
readonly authorityContext: string;
|
|
322
|
+
constructor(definition: SnapshotDefinition<D>, source: SnapshotSourceRuntime, options?: RuntimeSnapshotOptions);
|
|
258
323
|
get name(): string;
|
|
259
324
|
/**
|
|
260
325
|
* The active snapshot, or `undefined` before the first successful build.
|
|
@@ -325,7 +390,16 @@ export declare function assertSnapshotDefinition(definition: SnapshotDefinition<
|
|
|
325
390
|
* `StateFirstDB.snapshot()` is the application-facing form; this is the same
|
|
326
391
|
* primitive for runtimes and tools that hold a source of their own.
|
|
327
392
|
*/
|
|
328
|
-
export declare function createRuntimeSnapshot<D = undefined>(definition: SnapshotDefinition<D>, source: SnapshotSourceRuntime): RuntimeSnapshotHandle<D>;
|
|
393
|
+
export declare function createRuntimeSnapshot<D = undefined>(definition: SnapshotDefinition<D>, source: SnapshotSourceRuntime, options?: RuntimeSnapshotOptions): RuntimeSnapshotHandle<D>;
|
|
394
|
+
export interface RuntimeSnapshotOptions {
|
|
395
|
+
/**
|
|
396
|
+
* The authority the source reads under. `db.snapshot()` supplies the
|
|
397
|
+
* database's own authority scope; the source it hands the handle is already
|
|
398
|
+
* bound to that scope, so a snapshot can never read more than its database
|
|
399
|
+
* could. Recorded in every snapshot's identity.
|
|
400
|
+
*/
|
|
401
|
+
authorityContext?: string;
|
|
402
|
+
}
|
|
329
403
|
/**
|
|
330
404
|
* Whether two definitions describe the same snapshot.
|
|
331
405
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime-snapshot.d.ts","sourceRoot":"","sources":["../src/runtime-snapshot.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAEL,KAAK,mBAAmB,EACxB,KAAK,QAAQ,EACd,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAyC,KAAK,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAC3G,OAAO,KAAK,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAC9E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"run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GAKG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,GAAG,SAAS,EACjD,UAAU,EAAE,kBAAkB,CAAC,CAAC,CAAC,EACjC,MAAM,EAAE,qBAAqB,EAC7B,OAAO,GAAE,sBAA2B,GACnC,qBAAqB,CAAC,CAAC,CAAC,CAE1B;AAED,MAAM,WAAW,sBAAsB;IACrC;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAYD;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,kBAAkB,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,kBAAkB,CAAC,GAAG,CAAC,GAAG,OAAO,CAc/G"}
|
package/dist/runtime-snapshot.js
CHANGED
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
*/
|
|
31
31
|
import { isCacheCurrent, } from './freshness.js';
|
|
32
32
|
import { conditionMatches, EmbeddedQueryEngine } from './embedded-query.js';
|
|
33
|
+
import { canonicalJson, sha256Hex } from './stable-hash.js';
|
|
33
34
|
export class SnapshotDefinitionError extends Error {
|
|
34
35
|
constructor(message) { super(message); this.name = 'SnapshotDefinitionError'; }
|
|
35
36
|
}
|
|
@@ -56,17 +57,57 @@ class PublishedSnapshot {
|
|
|
56
57
|
generation;
|
|
57
58
|
basis;
|
|
58
59
|
materialization;
|
|
60
|
+
definitionDigest;
|
|
61
|
+
authorityContext;
|
|
59
62
|
collections;
|
|
60
63
|
records;
|
|
61
|
-
|
|
64
|
+
// Memo cell, not snapshot state: identity is a pure function of what is
|
|
65
|
+
// already frozen, computed once on first access.
|
|
66
|
+
memo = {};
|
|
67
|
+
constructor(name, generation, basis, materialization, definitionDigest, authorityContext) {
|
|
62
68
|
this.name = name;
|
|
63
69
|
this.generation = generation;
|
|
64
70
|
this.basis = basis;
|
|
65
71
|
this.materialization = materialization;
|
|
72
|
+
this.definitionDigest = definitionDigest;
|
|
73
|
+
this.authorityContext = authorityContext;
|
|
66
74
|
this.collections = Object.freeze([...materialization.byCollection.keys()]);
|
|
67
75
|
this.records = materialization.records.size;
|
|
68
76
|
Object.freeze(this);
|
|
69
77
|
}
|
|
78
|
+
get identity() {
|
|
79
|
+
if (this.memo.identity)
|
|
80
|
+
return this.memo.identity;
|
|
81
|
+
const sourceSet = Object.freeze([...this.collections].sort());
|
|
82
|
+
const revision = this.basis.validation === 'revision' ? this.basis.revision : undefined;
|
|
83
|
+
const contentDigest = revision ? undefined : sha256Hex(canonicalJson(sourceSet.map(collection => [
|
|
84
|
+
collection,
|
|
85
|
+
[...(this.materialization.byCollection.get(collection) ?? EMPTY)]
|
|
86
|
+
.map(record => canonicalJson(record))
|
|
87
|
+
.sort(),
|
|
88
|
+
])));
|
|
89
|
+
const id = `snap_${sha256Hex(canonicalJson([
|
|
90
|
+
'feltdb.snapshot.v1',
|
|
91
|
+
this.name,
|
|
92
|
+
this.definitionDigest,
|
|
93
|
+
this.authorityContext,
|
|
94
|
+
revision ? ['revision', revision.scope, revision.value] : ['content', contentDigest],
|
|
95
|
+
]))}`;
|
|
96
|
+
this.memo.identity = Object.freeze({
|
|
97
|
+
id,
|
|
98
|
+
name: this.name,
|
|
99
|
+
version: revision ? revision.value : null,
|
|
100
|
+
...(revision ? { scope: revision.scope } : {}),
|
|
101
|
+
generation: this.generation,
|
|
102
|
+
sourceSet,
|
|
103
|
+
definitionDigest: this.definitionDigest,
|
|
104
|
+
authorityContext: this.authorityContext,
|
|
105
|
+
...(contentDigest ? { contentDigest } : {}),
|
|
106
|
+
derivation: revision ? 'revision' : 'content',
|
|
107
|
+
createdAt: this.basis.materializedAt,
|
|
108
|
+
});
|
|
109
|
+
return this.memo.identity;
|
|
110
|
+
}
|
|
70
111
|
get revision() { return this.basis.revision; }
|
|
71
112
|
get materializedAt() { return this.basis.materializedAt; }
|
|
72
113
|
get(collection, id) {
|
|
@@ -129,10 +170,15 @@ export class RuntimeSnapshotHandle {
|
|
|
129
170
|
durableReads = 0;
|
|
130
171
|
lastFailure;
|
|
131
172
|
closed = false;
|
|
132
|
-
|
|
173
|
+
reused = 0;
|
|
174
|
+
definitionDigest;
|
|
175
|
+
authorityContext;
|
|
176
|
+
constructor(definition, source, options = {}) {
|
|
133
177
|
this.definition = definition;
|
|
134
178
|
this.source = source;
|
|
135
179
|
assertSnapshotDefinition(definition);
|
|
180
|
+
this.definitionDigest = sha256Hex(definitionShape(definition));
|
|
181
|
+
this.authorityContext = options.authorityContext ?? 'unscoped';
|
|
136
182
|
}
|
|
137
183
|
get name() { return this.definition.name; }
|
|
138
184
|
/**
|
|
@@ -162,8 +208,10 @@ export class RuntimeSnapshotHandle {
|
|
|
162
208
|
this.assertOpen();
|
|
163
209
|
if (!options.force && this.active) {
|
|
164
210
|
const staleness = await this.staleness();
|
|
165
|
-
if (staleness.state === 'current')
|
|
211
|
+
if (staleness.state === 'current') {
|
|
212
|
+
this.reused += 1;
|
|
166
213
|
return this.active;
|
|
214
|
+
}
|
|
167
215
|
}
|
|
168
216
|
return this.build();
|
|
169
217
|
}
|
|
@@ -240,6 +288,8 @@ export class RuntimeSnapshotHandle {
|
|
|
240
288
|
failures: this.failures,
|
|
241
289
|
...(this.lastFailure ? { lastFailure: this.lastFailure } : {}),
|
|
242
290
|
durableReads: this.durableReads,
|
|
291
|
+
reused: this.reused,
|
|
292
|
+
authorityContext: this.authorityContext,
|
|
243
293
|
};
|
|
244
294
|
}
|
|
245
295
|
/**
|
|
@@ -322,7 +372,9 @@ export class RuntimeSnapshotHandle {
|
|
|
322
372
|
const indexes = [];
|
|
323
373
|
for (const source of this.definition.sources) {
|
|
324
374
|
this.durableReads += 1;
|
|
325
|
-
const durable =
|
|
375
|
+
const durable = source.where?.length && this.source.readSelected
|
|
376
|
+
? await this.source.readSelected(source.collection, source.where)
|
|
377
|
+
: await this.source.readRecords(source.collection);
|
|
326
378
|
const kept = [];
|
|
327
379
|
for (const record of durable) {
|
|
328
380
|
if (!record || typeof record !== 'object' || Array.isArray(record))
|
|
@@ -389,7 +441,7 @@ export class RuntimeSnapshotHandle {
|
|
|
389
441
|
* visible.
|
|
390
442
|
*/
|
|
391
443
|
publish(materialization, basis) {
|
|
392
|
-
const candidate = new PublishedSnapshot(this.name, this.generation + 1, Object.freeze(basis), materialization);
|
|
444
|
+
const candidate = new PublishedSnapshot(this.name, this.generation + 1, Object.freeze(basis), materialization, this.definitionDigest, this.authorityContext);
|
|
393
445
|
this.definition.validate?.(candidate);
|
|
394
446
|
this.active = candidate;
|
|
395
447
|
this.generation = candidate.generation;
|
|
@@ -455,8 +507,17 @@ export function assertSnapshotDefinition(definition) {
|
|
|
455
507
|
* `StateFirstDB.snapshot()` is the application-facing form; this is the same
|
|
456
508
|
* primitive for runtimes and tools that hold a source of their own.
|
|
457
509
|
*/
|
|
458
|
-
export function createRuntimeSnapshot(definition, source) {
|
|
459
|
-
return new RuntimeSnapshotHandle(definition, source);
|
|
510
|
+
export function createRuntimeSnapshot(definition, source, options = {}) {
|
|
511
|
+
return new RuntimeSnapshotHandle(definition, source, options);
|
|
512
|
+
}
|
|
513
|
+
/** The identity-bearing shape of a definition: sources, filters, and indexes. */
|
|
514
|
+
function definitionShape(definition) {
|
|
515
|
+
return canonicalJson({
|
|
516
|
+
name: definition.name,
|
|
517
|
+
sources: [...definition.sources]
|
|
518
|
+
.map(source => ({ collection: source.collection, where: source.where ?? [], indexes: source.indexes ?? [] }))
|
|
519
|
+
.sort((left, right) => (left.collection < right.collection ? -1 : left.collection > right.collection ? 1 : 0)),
|
|
520
|
+
});
|
|
460
521
|
}
|
|
461
522
|
/**
|
|
462
523
|
* Whether two definitions describe the same snapshot.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synchronous, dependency-free SHA-256 and canonical JSON.
|
|
3
|
+
*
|
|
4
|
+
* Snapshot identity must be computable in every runtime `@feltdb/core` ships
|
|
5
|
+
* to — Node, browsers, workers — and synchronously, because a published
|
|
6
|
+
* snapshot answers without awaiting anything. WebCrypto is asynchronous and
|
|
7
|
+
* `node:crypto` is not available in a browser, so this is the small portable
|
|
8
|
+
* implementation both need. It is used for identity, not for security.
|
|
9
|
+
*/
|
|
10
|
+
/** SHA-256 of a UTF-8 string, as lowercase hex. */
|
|
11
|
+
export declare function sha256Hex(input: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* JSON with object keys sorted at every depth, so two structurally equal
|
|
14
|
+
* values serialize to the same bytes regardless of insertion order.
|
|
15
|
+
*/
|
|
16
|
+
export declare function canonicalJson(value: unknown): string;
|
|
17
|
+
//# sourceMappingURL=stable-hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stable-hash.d.ts","sourceRoot":"","sources":["../src/stable-hash.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAaH,mDAAmD;AACnD,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CA+B/C;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAUpD"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synchronous, dependency-free SHA-256 and canonical JSON.
|
|
3
|
+
*
|
|
4
|
+
* Snapshot identity must be computable in every runtime `@feltdb/core` ships
|
|
5
|
+
* to — Node, browsers, workers — and synchronously, because a published
|
|
6
|
+
* snapshot answers without awaiting anything. WebCrypto is asynchronous and
|
|
7
|
+
* `node:crypto` is not available in a browser, so this is the small portable
|
|
8
|
+
* implementation both need. It is used for identity, not for security.
|
|
9
|
+
*/
|
|
10
|
+
const K = new Uint32Array([
|
|
11
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
12
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
13
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
14
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
15
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
16
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
17
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
18
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
19
|
+
]);
|
|
20
|
+
/** SHA-256 of a UTF-8 string, as lowercase hex. */
|
|
21
|
+
export function sha256Hex(input) {
|
|
22
|
+
const bytes = new TextEncoder().encode(input);
|
|
23
|
+
const bitLength = bytes.length * 8;
|
|
24
|
+
const padded = new Uint8Array(((bytes.length + 9 + 63) >> 6) << 6);
|
|
25
|
+
padded.set(bytes);
|
|
26
|
+
padded[bytes.length] = 0x80;
|
|
27
|
+
const view = new DataView(padded.buffer);
|
|
28
|
+
view.setUint32(padded.length - 8, Math.floor(bitLength / 0x100000000));
|
|
29
|
+
view.setUint32(padded.length - 4, bitLength >>> 0);
|
|
30
|
+
const h = new Uint32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]);
|
|
31
|
+
const w = new Uint32Array(64);
|
|
32
|
+
for (let offset = 0; offset < padded.length; offset += 64) {
|
|
33
|
+
for (let i = 0; i < 16; i += 1)
|
|
34
|
+
w[i] = view.getUint32(offset + i * 4);
|
|
35
|
+
for (let i = 16; i < 64; i += 1) {
|
|
36
|
+
const a = w[i - 15], b = w[i - 2];
|
|
37
|
+
const s0 = ((a >>> 7) | (a << 25)) ^ ((a >>> 18) | (a << 14)) ^ (a >>> 3);
|
|
38
|
+
const s1 = ((b >>> 17) | (b << 15)) ^ ((b >>> 19) | (b << 13)) ^ (b >>> 10);
|
|
39
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
|
|
40
|
+
}
|
|
41
|
+
let [a, b, c, d, e, f, g, hh] = h;
|
|
42
|
+
for (let i = 0; i < 64; i += 1) {
|
|
43
|
+
const s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
|
|
44
|
+
const t1 = (hh + s1 + ((e & f) ^ (~e & g)) + K[i] + w[i]) >>> 0;
|
|
45
|
+
const s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
|
|
46
|
+
const t2 = (s0 + ((a & b) ^ (a & c) ^ (b & c))) >>> 0;
|
|
47
|
+
hh = g;
|
|
48
|
+
g = f;
|
|
49
|
+
f = e;
|
|
50
|
+
e = (d + t1) >>> 0;
|
|
51
|
+
d = c;
|
|
52
|
+
c = b;
|
|
53
|
+
b = a;
|
|
54
|
+
a = (t1 + t2) >>> 0;
|
|
55
|
+
}
|
|
56
|
+
h[0] += a;
|
|
57
|
+
h[1] += b;
|
|
58
|
+
h[2] += c;
|
|
59
|
+
h[3] += d;
|
|
60
|
+
h[4] += e;
|
|
61
|
+
h[5] += f;
|
|
62
|
+
h[6] += g;
|
|
63
|
+
h[7] += hh;
|
|
64
|
+
}
|
|
65
|
+
return Array.from(h, word => word.toString(16).padStart(8, '0')).join('');
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* JSON with object keys sorted at every depth, so two structurally equal
|
|
69
|
+
* values serialize to the same bytes regardless of insertion order.
|
|
70
|
+
*/
|
|
71
|
+
export function canonicalJson(value) {
|
|
72
|
+
if (value === null || typeof value !== 'object') {
|
|
73
|
+
return value === undefined ? 'null' : JSON.stringify(value);
|
|
74
|
+
}
|
|
75
|
+
if (Array.isArray(value))
|
|
76
|
+
return `[${value.map(canonicalJson).join(',')}]`;
|
|
77
|
+
const entries = Object.keys(value)
|
|
78
|
+
.filter(key => value[key] !== undefined)
|
|
79
|
+
.sort()
|
|
80
|
+
.map(key => `${JSON.stringify(key)}:${canonicalJson(value[key])}`);
|
|
81
|
+
return `{${entries.join(',')}}`;
|
|
82
|
+
}
|