tablinum 0.0.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/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/.context/attachments/pasted_text_2026-03-07_14-02-40.txt +571 -0
- package/.context/attachments/pasted_text_2026-03-07_15-48-27.txt +498 -0
- package/.context/notes.md +0 -0
- package/.context/plans/add-changesets-to-douala-v4.md +48 -0
- package/.context/plans/dexie-js-style-query-language-for-localstr.md +115 -0
- package/.context/plans/dexie-js-style-query-language-with-per-collection-.md +336 -0
- package/.context/plans/implementation-plan-localstr-v0-2.md +263 -0
- package/.context/plans/project-init-effect-v4-bun-oxlint-oxfmt-vitest.md +71 -0
- package/.context/plans/revise-localstr-prd-v0-2.md +132 -0
- package/.context/plans/svelte-5-runes-bindings-for-localstr.md +233 -0
- package/.context/todos.md +0 -0
- package/.github/workflows/release.yml +36 -0
- package/.oxlintrc.json +8 -0
- package/README.md +1 -0
- package/bun.lock +705 -0
- package/examples/svelte/bun.lock +261 -0
- package/examples/svelte/package.json +21 -0
- package/examples/svelte/src/app.html +11 -0
- package/examples/svelte/src/lib/db.ts +44 -0
- package/examples/svelte/src/routes/+page.svelte +322 -0
- package/examples/svelte/svelte.config.js +16 -0
- package/examples/svelte/tsconfig.json +6 -0
- package/examples/svelte/vite.config.ts +6 -0
- package/examples/vanilla/app.ts +219 -0
- package/examples/vanilla/index.html +144 -0
- package/examples/vanilla/serve.ts +42 -0
- package/package.json +46 -0
- package/prds/localstr-v0.2.md +221 -0
- package/prek.toml +10 -0
- package/scripts/validate.ts +392 -0
- package/src/crud/collection-handle.ts +189 -0
- package/src/crud/query-builder.ts +414 -0
- package/src/crud/watch.ts +78 -0
- package/src/db/create-localstr.ts +217 -0
- package/src/db/database-handle.ts +16 -0
- package/src/db/identity.ts +49 -0
- package/src/errors.ts +37 -0
- package/src/index.ts +32 -0
- package/src/main.ts +10 -0
- package/src/schema/collection.ts +53 -0
- package/src/schema/field.ts +25 -0
- package/src/schema/types.ts +19 -0
- package/src/schema/validate.ts +111 -0
- package/src/storage/events-store.ts +24 -0
- package/src/storage/giftwraps-store.ts +23 -0
- package/src/storage/idb.ts +244 -0
- package/src/storage/lww.ts +17 -0
- package/src/storage/records-store.ts +76 -0
- package/src/svelte/collection.svelte.ts +87 -0
- package/src/svelte/database.svelte.ts +83 -0
- package/src/svelte/index.svelte.ts +52 -0
- package/src/svelte/live-query.svelte.ts +29 -0
- package/src/svelte/query.svelte.ts +101 -0
- package/src/sync/gift-wrap.ts +33 -0
- package/src/sync/negentropy.ts +83 -0
- package/src/sync/publish-queue.ts +61 -0
- package/src/sync/relay.ts +239 -0
- package/src/sync/sync-service.ts +183 -0
- package/src/sync/sync-status.ts +17 -0
- package/src/utils/uuid.ts +22 -0
- package/src/vendor/negentropy.js +616 -0
- package/tests/db/create-localstr.test.ts +174 -0
- package/tests/db/identity.test.ts +33 -0
- package/tests/main.test.ts +9 -0
- package/tests/schema/collection.test.ts +27 -0
- package/tests/schema/field.test.ts +41 -0
- package/tests/schema/validate.test.ts +85 -0
- package/tests/setup.ts +1 -0
- package/tests/storage/idb.test.ts +144 -0
- package/tests/storage/lww.test.ts +33 -0
- package/tests/sync/gift-wrap.test.ts +56 -0
- package/tsconfig.json +18 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
// (C) 2023 Doug Hoyte. MIT license
|
|
2
|
+
|
|
3
|
+
const PROTOCOL_VERSION = 0x61; // Version 1
|
|
4
|
+
const ID_SIZE = 32;
|
|
5
|
+
const FINGERPRINT_SIZE = 16;
|
|
6
|
+
|
|
7
|
+
const Mode = {
|
|
8
|
+
Skip: 0,
|
|
9
|
+
Fingerprint: 1,
|
|
10
|
+
IdList: 2,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
class WrappedBuffer {
|
|
14
|
+
constructor(buffer) {
|
|
15
|
+
this._raw = new Uint8Array(buffer || 512);
|
|
16
|
+
this.length = buffer ? buffer.length : 0;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
unwrap() {
|
|
20
|
+
return this._raw.subarray(0, this.length);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get capacity() {
|
|
24
|
+
return this._raw.byteLength;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
extend(buf) {
|
|
28
|
+
if (buf._raw) buf = buf.unwrap();
|
|
29
|
+
if (typeof buf.length !== "number") throw Error("bad length");
|
|
30
|
+
const targetSize = buf.length + this.length;
|
|
31
|
+
if (this.capacity < targetSize) {
|
|
32
|
+
const oldRaw = this._raw;
|
|
33
|
+
const newCapacity = Math.max(this.capacity * 2, targetSize);
|
|
34
|
+
this._raw = new Uint8Array(newCapacity);
|
|
35
|
+
this._raw.set(oldRaw);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
this._raw.set(buf, this.length);
|
|
39
|
+
this.length += buf.length;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
shift() {
|
|
43
|
+
const first = this._raw[0];
|
|
44
|
+
this._raw = this._raw.subarray(1);
|
|
45
|
+
this.length--;
|
|
46
|
+
return first;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
shiftN(n = 1) {
|
|
50
|
+
const firstSubarray = this._raw.subarray(0, n);
|
|
51
|
+
this._raw = this._raw.subarray(n);
|
|
52
|
+
this.length -= n;
|
|
53
|
+
return firstSubarray;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function decodeVarInt(buf) {
|
|
58
|
+
let res = 0;
|
|
59
|
+
|
|
60
|
+
while (1) {
|
|
61
|
+
if (buf.length === 0) throw Error("parse ends prematurely");
|
|
62
|
+
let byte = buf.shift();
|
|
63
|
+
res = (res << 7) | (byte & 127);
|
|
64
|
+
if ((byte & 128) === 0) break;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return res;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function encodeVarInt(n) {
|
|
71
|
+
if (n === 0) return new WrappedBuffer([0]);
|
|
72
|
+
|
|
73
|
+
let o = [];
|
|
74
|
+
|
|
75
|
+
while (n !== 0) {
|
|
76
|
+
o.push(n & 127);
|
|
77
|
+
n >>>= 7;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
o.reverse();
|
|
81
|
+
|
|
82
|
+
for (let i = 0; i < o.length - 1; i++) o[i] |= 128;
|
|
83
|
+
|
|
84
|
+
return new WrappedBuffer(o);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getByte(buf) {
|
|
88
|
+
return getBytes(buf, 1)[0];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function getBytes(buf, n) {
|
|
92
|
+
if (buf.length < n) throw Error("parse ends prematurely");
|
|
93
|
+
return buf.shiftN(n);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
class Accumulator {
|
|
97
|
+
constructor() {
|
|
98
|
+
this.setToZero();
|
|
99
|
+
|
|
100
|
+
if (typeof window === "undefined") {
|
|
101
|
+
// node.js
|
|
102
|
+
const crypto = require("crypto");
|
|
103
|
+
this.sha256 = async (slice) =>
|
|
104
|
+
new Uint8Array(crypto.createHash("sha256").update(slice).digest());
|
|
105
|
+
} else {
|
|
106
|
+
// browser
|
|
107
|
+
this.sha256 = async (slice) => new Uint8Array(await crypto.subtle.digest("SHA-256", slice));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
setToZero() {
|
|
112
|
+
this.buf = new Uint8Array(ID_SIZE);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
add(otherBuf) {
|
|
116
|
+
let currCarry = 0,
|
|
117
|
+
nextCarry = 0;
|
|
118
|
+
let p = new DataView(this.buf.buffer);
|
|
119
|
+
let po = new DataView(otherBuf.buffer);
|
|
120
|
+
|
|
121
|
+
for (let i = 0; i < 8; i++) {
|
|
122
|
+
let offset = i * 4;
|
|
123
|
+
let orig = p.getUint32(offset, true);
|
|
124
|
+
let otherV = po.getUint32(offset, true);
|
|
125
|
+
|
|
126
|
+
let next = orig;
|
|
127
|
+
|
|
128
|
+
next += currCarry;
|
|
129
|
+
next += otherV;
|
|
130
|
+
if (next > 0xffffffff) nextCarry = 1;
|
|
131
|
+
|
|
132
|
+
p.setUint32(offset, next & 0xffffffff, true);
|
|
133
|
+
currCarry = nextCarry;
|
|
134
|
+
nextCarry = 0;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
negate() {
|
|
139
|
+
let p = new DataView(this.buf.buffer);
|
|
140
|
+
|
|
141
|
+
for (let i = 0; i < 8; i++) {
|
|
142
|
+
let offset = i * 4;
|
|
143
|
+
p.setUint32(offset, ~p.getUint32(offset, true));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let one = new Uint8Array(ID_SIZE);
|
|
147
|
+
one[0] = 1;
|
|
148
|
+
this.add(one);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async getFingerprint(n) {
|
|
152
|
+
let input = new WrappedBuffer();
|
|
153
|
+
input.extend(this.buf);
|
|
154
|
+
input.extend(encodeVarInt(n));
|
|
155
|
+
|
|
156
|
+
let hash = await this.sha256(input.unwrap());
|
|
157
|
+
|
|
158
|
+
return hash.subarray(0, FINGERPRINT_SIZE);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
class NegentropyStorageVector {
|
|
163
|
+
constructor() {
|
|
164
|
+
this.items = [];
|
|
165
|
+
this.sealed = false;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
insert(timestamp, id) {
|
|
169
|
+
if (this.sealed) throw Error("already sealed");
|
|
170
|
+
id = loadInputBuffer(id);
|
|
171
|
+
if (id.byteLength !== ID_SIZE) throw Error("bad id size for added item");
|
|
172
|
+
this.items.push({ timestamp, id });
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
seal() {
|
|
176
|
+
if (this.sealed) throw Error("already sealed");
|
|
177
|
+
this.sealed = true;
|
|
178
|
+
|
|
179
|
+
this.items.sort(itemCompare);
|
|
180
|
+
|
|
181
|
+
for (let i = 1; i < this.items.length; i++) {
|
|
182
|
+
if (itemCompare(this.items[i - 1], this.items[i]) === 0)
|
|
183
|
+
throw Error("duplicate item inserted");
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
unseal() {
|
|
188
|
+
this.sealed = false;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
size() {
|
|
192
|
+
this._checkSealed();
|
|
193
|
+
return this.items.length;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
getItem(i) {
|
|
197
|
+
this._checkSealed();
|
|
198
|
+
if (i >= this.items.length) throw Error("out of range");
|
|
199
|
+
return this.items[i];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
iterate(begin, end, cb) {
|
|
203
|
+
this._checkSealed();
|
|
204
|
+
this._checkBounds(begin, end);
|
|
205
|
+
|
|
206
|
+
for (let i = begin; i < end; ++i) {
|
|
207
|
+
if (!cb(this.items[i], i)) break;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
findLowerBound(begin, end, bound) {
|
|
212
|
+
this._checkSealed();
|
|
213
|
+
this._checkBounds(begin, end);
|
|
214
|
+
|
|
215
|
+
return this._binarySearch(this.items, begin, end, (a) => itemCompare(a, bound) < 0);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async fingerprint(begin, end) {
|
|
219
|
+
let out = new Accumulator();
|
|
220
|
+
out.setToZero();
|
|
221
|
+
|
|
222
|
+
this.iterate(begin, end, (item, i) => {
|
|
223
|
+
out.add(item.id);
|
|
224
|
+
return true;
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
return await out.getFingerprint(end - begin);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
_checkSealed() {
|
|
231
|
+
if (!this.sealed) throw Error("not sealed");
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
_checkBounds(begin, end) {
|
|
235
|
+
if (begin > end || end > this.items.length) throw Error("bad range");
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
_binarySearch(arr, first, last, cmp) {
|
|
239
|
+
let count = last - first;
|
|
240
|
+
|
|
241
|
+
while (count > 0) {
|
|
242
|
+
let it = first;
|
|
243
|
+
let step = Math.floor(count / 2);
|
|
244
|
+
it += step;
|
|
245
|
+
|
|
246
|
+
if (cmp(arr[it])) {
|
|
247
|
+
first = ++it;
|
|
248
|
+
count -= step + 1;
|
|
249
|
+
} else {
|
|
250
|
+
count = step;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return first;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
class Negentropy {
|
|
259
|
+
constructor(storage, frameSizeLimit = 0) {
|
|
260
|
+
if (frameSizeLimit !== 0 && frameSizeLimit < 4096) throw Error("frameSizeLimit too small");
|
|
261
|
+
|
|
262
|
+
this.storage = storage;
|
|
263
|
+
this.frameSizeLimit = frameSizeLimit;
|
|
264
|
+
|
|
265
|
+
this.lastTimestampIn = 0;
|
|
266
|
+
this.lastTimestampOut = 0;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
_bound(timestamp, id) {
|
|
270
|
+
return { timestamp, id: id ? id : new Uint8Array(0) };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async initiate() {
|
|
274
|
+
if (this.isInitiator) throw Error("already initiated");
|
|
275
|
+
this.isInitiator = true;
|
|
276
|
+
|
|
277
|
+
let output = new WrappedBuffer();
|
|
278
|
+
output.extend([PROTOCOL_VERSION]);
|
|
279
|
+
|
|
280
|
+
await this.splitRange(0, this.storage.size(), this._bound(Number.MAX_VALUE), output);
|
|
281
|
+
|
|
282
|
+
return this._renderOutput(output);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
setInitiator() {
|
|
286
|
+
this.isInitiator = true;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async reconcile(query) {
|
|
290
|
+
let haveIds = [],
|
|
291
|
+
needIds = [];
|
|
292
|
+
query = new WrappedBuffer(loadInputBuffer(query));
|
|
293
|
+
|
|
294
|
+
this.lastTimestampIn = this.lastTimestampOut = 0; // reset for each message
|
|
295
|
+
|
|
296
|
+
let fullOutput = new WrappedBuffer();
|
|
297
|
+
fullOutput.extend([PROTOCOL_VERSION]);
|
|
298
|
+
|
|
299
|
+
let protocolVersion = getByte(query);
|
|
300
|
+
if (protocolVersion < 0x60 || protocolVersion > 0x6f)
|
|
301
|
+
throw Error("invalid negentropy protocol version byte");
|
|
302
|
+
if (protocolVersion !== PROTOCOL_VERSION) {
|
|
303
|
+
if (this.isInitiator)
|
|
304
|
+
throw Error(
|
|
305
|
+
"unsupported negentropy protocol version requested: " + (protocolVersion - 0x60),
|
|
306
|
+
);
|
|
307
|
+
else return [this._renderOutput(fullOutput), haveIds, needIds];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
let storageSize = this.storage.size();
|
|
311
|
+
let prevBound = this._bound(0);
|
|
312
|
+
let prevIndex = 0;
|
|
313
|
+
let skip = false;
|
|
314
|
+
|
|
315
|
+
while (query.length !== 0) {
|
|
316
|
+
let o = new WrappedBuffer();
|
|
317
|
+
|
|
318
|
+
let doSkip = () => {
|
|
319
|
+
if (skip) {
|
|
320
|
+
skip = false;
|
|
321
|
+
o.extend(this.encodeBound(prevBound));
|
|
322
|
+
o.extend(encodeVarInt(Mode.Skip));
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
let currBound = this.decodeBound(query);
|
|
327
|
+
let mode = decodeVarInt(query);
|
|
328
|
+
|
|
329
|
+
let lower = prevIndex;
|
|
330
|
+
let upper = this.storage.findLowerBound(prevIndex, storageSize, currBound);
|
|
331
|
+
|
|
332
|
+
if (mode === Mode.Skip) {
|
|
333
|
+
skip = true;
|
|
334
|
+
} else if (mode === Mode.Fingerprint) {
|
|
335
|
+
let theirFingerprint = getBytes(query, FINGERPRINT_SIZE);
|
|
336
|
+
let ourFingerprint = await this.storage.fingerprint(lower, upper);
|
|
337
|
+
|
|
338
|
+
if (compareUint8Array(theirFingerprint, ourFingerprint) !== 0) {
|
|
339
|
+
doSkip();
|
|
340
|
+
await this.splitRange(lower, upper, currBound, o);
|
|
341
|
+
} else {
|
|
342
|
+
skip = true;
|
|
343
|
+
}
|
|
344
|
+
} else if (mode === Mode.IdList) {
|
|
345
|
+
let numIds = decodeVarInt(query);
|
|
346
|
+
|
|
347
|
+
let theirElems = {}; // stringified Uint8Array -> original Uint8Array (or hex)
|
|
348
|
+
for (let i = 0; i < numIds; i++) {
|
|
349
|
+
let e = getBytes(query, ID_SIZE);
|
|
350
|
+
if (this.isInitiator) theirElems[e] = e;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (this.isInitiator) {
|
|
354
|
+
skip = true;
|
|
355
|
+
|
|
356
|
+
this.storage.iterate(lower, upper, (item) => {
|
|
357
|
+
let k = item.id;
|
|
358
|
+
|
|
359
|
+
if (!theirElems[k]) {
|
|
360
|
+
// ID exists on our side, but not their side
|
|
361
|
+
if (this.isInitiator)
|
|
362
|
+
haveIds.push(this.wantUint8ArrayOutput ? k : uint8ArrayToHex(k));
|
|
363
|
+
} else {
|
|
364
|
+
// ID exists on both sides
|
|
365
|
+
delete theirElems[k];
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return true;
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
for (let v of Object.values(theirElems)) {
|
|
372
|
+
// ID exists on their side, but not our side
|
|
373
|
+
needIds.push(this.wantUint8ArrayOutput ? v : uint8ArrayToHex(v));
|
|
374
|
+
}
|
|
375
|
+
} else {
|
|
376
|
+
doSkip();
|
|
377
|
+
|
|
378
|
+
let responseIds = new WrappedBuffer();
|
|
379
|
+
let numResponseIds = 0;
|
|
380
|
+
let endBound = currBound;
|
|
381
|
+
|
|
382
|
+
this.storage.iterate(lower, upper, (item, index) => {
|
|
383
|
+
if (this.exceededFrameSizeLimit(fullOutput.length + responseIds.length)) {
|
|
384
|
+
endBound = item;
|
|
385
|
+
upper = index; // shrink upper so that remaining range gets correct fingerprint
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
responseIds.extend(item.id);
|
|
390
|
+
numResponseIds++;
|
|
391
|
+
return true;
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
o.extend(this.encodeBound(endBound));
|
|
395
|
+
o.extend(encodeVarInt(Mode.IdList));
|
|
396
|
+
o.extend(encodeVarInt(numResponseIds));
|
|
397
|
+
o.extend(responseIds);
|
|
398
|
+
|
|
399
|
+
fullOutput.extend(o);
|
|
400
|
+
o = new WrappedBuffer();
|
|
401
|
+
}
|
|
402
|
+
} else {
|
|
403
|
+
throw Error("unexpected mode");
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (this.exceededFrameSizeLimit(fullOutput.length + o.length)) {
|
|
407
|
+
// frameSizeLimit exceeded: Stop range processing and return a fingerprint for the remaining range
|
|
408
|
+
let remainingFingerprint = await this.storage.fingerprint(upper, storageSize);
|
|
409
|
+
|
|
410
|
+
fullOutput.extend(this.encodeBound(this._bound(Number.MAX_VALUE)));
|
|
411
|
+
fullOutput.extend(encodeVarInt(Mode.Fingerprint));
|
|
412
|
+
fullOutput.extend(remainingFingerprint);
|
|
413
|
+
break;
|
|
414
|
+
} else {
|
|
415
|
+
fullOutput.extend(o);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
prevIndex = upper;
|
|
419
|
+
prevBound = currBound;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return [
|
|
423
|
+
fullOutput.length === 1 && this.isInitiator ? null : this._renderOutput(fullOutput),
|
|
424
|
+
haveIds,
|
|
425
|
+
needIds,
|
|
426
|
+
];
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
async splitRange(lower, upper, upperBound, o) {
|
|
430
|
+
let numElems = upper - lower;
|
|
431
|
+
let buckets = 16;
|
|
432
|
+
|
|
433
|
+
if (numElems < buckets * 2) {
|
|
434
|
+
o.extend(this.encodeBound(upperBound));
|
|
435
|
+
o.extend(encodeVarInt(Mode.IdList));
|
|
436
|
+
|
|
437
|
+
o.extend(encodeVarInt(numElems));
|
|
438
|
+
this.storage.iterate(lower, upper, (item) => {
|
|
439
|
+
o.extend(item.id);
|
|
440
|
+
return true;
|
|
441
|
+
});
|
|
442
|
+
} else {
|
|
443
|
+
let itemsPerBucket = Math.floor(numElems / buckets);
|
|
444
|
+
let bucketsWithExtra = numElems % buckets;
|
|
445
|
+
let curr = lower;
|
|
446
|
+
|
|
447
|
+
for (let i = 0; i < buckets; i++) {
|
|
448
|
+
let bucketSize = itemsPerBucket + (i < bucketsWithExtra ? 1 : 0);
|
|
449
|
+
let ourFingerprint = await this.storage.fingerprint(curr, curr + bucketSize);
|
|
450
|
+
curr += bucketSize;
|
|
451
|
+
|
|
452
|
+
let nextBound;
|
|
453
|
+
|
|
454
|
+
if (curr === upper) {
|
|
455
|
+
nextBound = upperBound;
|
|
456
|
+
} else {
|
|
457
|
+
let prevItem, currItem;
|
|
458
|
+
|
|
459
|
+
this.storage.iterate(curr - 1, curr + 1, (item, index) => {
|
|
460
|
+
if (index === curr - 1) prevItem = item;
|
|
461
|
+
else currItem = item;
|
|
462
|
+
return true;
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
nextBound = this.getMinimalBound(prevItem, currItem);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
o.extend(this.encodeBound(nextBound));
|
|
469
|
+
o.extend(encodeVarInt(Mode.Fingerprint));
|
|
470
|
+
o.extend(ourFingerprint);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
_renderOutput(o) {
|
|
476
|
+
o = o.unwrap();
|
|
477
|
+
if (!this.wantUint8ArrayOutput) o = uint8ArrayToHex(o);
|
|
478
|
+
return o;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
exceededFrameSizeLimit(n) {
|
|
482
|
+
return this.frameSizeLimit && n > this.frameSizeLimit - 200;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// Decoding
|
|
486
|
+
|
|
487
|
+
decodeTimestampIn(encoded) {
|
|
488
|
+
let timestamp = decodeVarInt(encoded);
|
|
489
|
+
timestamp = timestamp === 0 ? Number.MAX_VALUE : timestamp - 1;
|
|
490
|
+
if (this.lastTimestampIn === Number.MAX_VALUE || timestamp === Number.MAX_VALUE) {
|
|
491
|
+
this.lastTimestampIn = Number.MAX_VALUE;
|
|
492
|
+
return Number.MAX_VALUE;
|
|
493
|
+
}
|
|
494
|
+
timestamp += this.lastTimestampIn;
|
|
495
|
+
this.lastTimestampIn = timestamp;
|
|
496
|
+
return timestamp;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
decodeBound(encoded) {
|
|
500
|
+
let timestamp = this.decodeTimestampIn(encoded);
|
|
501
|
+
let len = decodeVarInt(encoded);
|
|
502
|
+
if (len > ID_SIZE) throw Error("bound key too long");
|
|
503
|
+
let id = getBytes(encoded, len);
|
|
504
|
+
return { timestamp, id };
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// Encoding
|
|
508
|
+
|
|
509
|
+
encodeTimestampOut(timestamp) {
|
|
510
|
+
if (timestamp === Number.MAX_VALUE) {
|
|
511
|
+
this.lastTimestampOut = Number.MAX_VALUE;
|
|
512
|
+
return encodeVarInt(0);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
let temp = timestamp;
|
|
516
|
+
timestamp -= this.lastTimestampOut;
|
|
517
|
+
this.lastTimestampOut = temp;
|
|
518
|
+
return encodeVarInt(timestamp + 1);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
encodeBound(key) {
|
|
522
|
+
let output = new WrappedBuffer();
|
|
523
|
+
|
|
524
|
+
output.extend(this.encodeTimestampOut(key.timestamp));
|
|
525
|
+
output.extend(encodeVarInt(key.id.length));
|
|
526
|
+
output.extend(key.id);
|
|
527
|
+
|
|
528
|
+
return output;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
getMinimalBound(prev, curr) {
|
|
532
|
+
if (curr.timestamp !== prev.timestamp) {
|
|
533
|
+
return this._bound(curr.timestamp);
|
|
534
|
+
} else {
|
|
535
|
+
let sharedPrefixBytes = 0;
|
|
536
|
+
let currKey = curr.id;
|
|
537
|
+
let prevKey = prev.id;
|
|
538
|
+
|
|
539
|
+
for (let i = 0; i < ID_SIZE; i++) {
|
|
540
|
+
if (currKey[i] !== prevKey[i]) break;
|
|
541
|
+
sharedPrefixBytes++;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
return this._bound(curr.timestamp, curr.id.subarray(0, sharedPrefixBytes + 1));
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
function loadInputBuffer(inp) {
|
|
550
|
+
if (typeof inp === "string") inp = hexToUint8Array(inp);
|
|
551
|
+
else if (__proto__ !== Uint8Array.prototype) inp = new Uint8Array(inp); // node Buffer?
|
|
552
|
+
return inp;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function hexToUint8Array(h) {
|
|
556
|
+
if (h.startsWith("0x")) h = h.substr(2);
|
|
557
|
+
if (h.length % 2 === 1) throw Error("odd length of hex string");
|
|
558
|
+
let arr = new Uint8Array(h.length / 2);
|
|
559
|
+
for (let i = 0; i < arr.length; i++) arr[i] = parseInt(h.substr(i * 2, 2), 16);
|
|
560
|
+
return arr;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const uint8ArrayToHexLookupTable = new Array(256);
|
|
564
|
+
{
|
|
565
|
+
const hexAlphabet = [
|
|
566
|
+
"0",
|
|
567
|
+
"1",
|
|
568
|
+
"2",
|
|
569
|
+
"3",
|
|
570
|
+
"4",
|
|
571
|
+
"5",
|
|
572
|
+
"6",
|
|
573
|
+
"7",
|
|
574
|
+
"8",
|
|
575
|
+
"9",
|
|
576
|
+
"a",
|
|
577
|
+
"b",
|
|
578
|
+
"c",
|
|
579
|
+
"d",
|
|
580
|
+
"e",
|
|
581
|
+
"f",
|
|
582
|
+
];
|
|
583
|
+
for (let i = 0; i < 256; i++) {
|
|
584
|
+
uint8ArrayToHexLookupTable[i] = hexAlphabet[(i >>> 4) & 0xf] + hexAlphabet[i & 0xf];
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function uint8ArrayToHex(arr) {
|
|
589
|
+
let out = "";
|
|
590
|
+
for (let i = 0, edx = arr.length; i < edx; i++) {
|
|
591
|
+
out += uint8ArrayToHexLookupTable[arr[i]];
|
|
592
|
+
}
|
|
593
|
+
return out;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function compareUint8Array(a, b) {
|
|
597
|
+
for (let i = 0; i < a.byteLength; i++) {
|
|
598
|
+
if (a[i] < b[i]) return -1;
|
|
599
|
+
if (a[i] > b[i]) return 1;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
if (a.byteLength > b.byteLength) return 1;
|
|
603
|
+
if (a.byteLength < b.byteLength) return -1;
|
|
604
|
+
|
|
605
|
+
return 0;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function itemCompare(a, b) {
|
|
609
|
+
if (a.timestamp === b.timestamp) {
|
|
610
|
+
return compareUint8Array(a.id, b.id);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
return a.timestamp - b.timestamp;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
export { Negentropy, NegentropyStorageVector };
|