@dxos/feed-store 0.8.4-main.3eb6e50203 → 0.8.4-main.3fbcb4aa9b
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/lib/browser/{chunk-I36R5SR3.mjs → chunk-MB63GMGR.mjs} +20 -142
- package/dist/lib/browser/chunk-MB63GMGR.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +26 -178
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +6 -9
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/{chunk-HJBZDWE4.mjs → chunk-VZUET36D.mjs} +20 -142
- package/dist/lib/node-esm/chunk-VZUET36D.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +26 -178
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +6 -9
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/feed-factory.d.ts.map +1 -1
- package/dist/types/src/feed-iterator.d.ts.map +1 -1
- package/dist/types/src/feed-queue.d.ts.map +1 -1
- package/dist/types/src/feed-set-iterator.d.ts.map +1 -1
- package/dist/types/src/feed-store.d.ts.map +1 -1
- package/dist/types/src/feed-wrapper.d.ts +1 -1
- package/dist/types/src/feed-wrapper.d.ts.map +1 -1
- package/dist/types/src/feed-writer.d.ts.map +1 -1
- package/dist/types/src/testing/mocks.d.ts.map +1 -1
- package/dist/types/src/testing/test-builder.d.ts.map +1 -1
- package/dist/types/src/testing/test-generator.d.ts +1 -1
- package/dist/types/src/testing/test-generator.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +18 -25
- package/src/feed-queue.ts +0 -1
- package/src/feed-set-iterator.test.ts +6 -6
- package/src/feed-store.node.test.ts +2 -2
- package/src/feed-wrapper.test.ts +9 -10
- package/src/feed-wrapper.ts +0 -1
- package/src/testing/test-builder.ts +0 -1
- package/src/testing/test-generator.ts +4 -4
- package/dist/lib/browser/chunk-I36R5SR3.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HJBZDWE4.mjs.map +0 -7
|
@@ -23,15 +23,7 @@ var FeedWrapper = class {
|
|
|
23
23
|
this._storageDirectory = _storageDirectory;
|
|
24
24
|
assertArgument(hypercore2, "hypercore");
|
|
25
25
|
this._hypercore = hypercore2;
|
|
26
|
-
invariant(this._key, void 0, {
|
|
27
|
-
F: __dxlog_file,
|
|
28
|
-
L: 41,
|
|
29
|
-
S: this,
|
|
30
|
-
A: [
|
|
31
|
-
"this._key",
|
|
32
|
-
""
|
|
33
|
-
]
|
|
34
|
-
});
|
|
26
|
+
invariant(this._key, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 27, S: this, A: ["this._key", ""] });
|
|
35
27
|
this._writeLock.wake();
|
|
36
28
|
}
|
|
37
29
|
[inspect.custom]() {
|
|
@@ -76,21 +68,8 @@ var FeedWrapper = class {
|
|
|
76
68
|
log("write", {
|
|
77
69
|
feed: this._key,
|
|
78
70
|
seq: this._hypercore.length
|
|
79
|
-
}, {
|
|
80
|
-
|
|
81
|
-
L: 100,
|
|
82
|
-
S: this,
|
|
83
|
-
C: (f, a) => f(...a)
|
|
84
|
-
});
|
|
85
|
-
invariant(!this._closed, "Feed closed", {
|
|
86
|
-
F: __dxlog_file,
|
|
87
|
-
L: 101,
|
|
88
|
-
S: this,
|
|
89
|
-
A: [
|
|
90
|
-
"!this._closed",
|
|
91
|
-
"'Feed closed'"
|
|
92
|
-
]
|
|
93
|
-
});
|
|
71
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 74, S: this });
|
|
72
|
+
invariant(!this._closed, "Feed closed", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 78, S: this, A: ["!this._closed", "'Feed closed'"] });
|
|
94
73
|
const stackTrace = new StackTrace();
|
|
95
74
|
try {
|
|
96
75
|
this._pendingWrites.add(stackTrace);
|
|
@@ -112,24 +91,11 @@ var FeedWrapper = class {
|
|
|
112
91
|
}
|
|
113
92
|
async appendWithReceipt(data) {
|
|
114
93
|
const seq = await this.append(data);
|
|
115
|
-
invariant(seq < this.length, "Invalid seq after write", {
|
|
116
|
-
F: __dxlog_file,
|
|
117
|
-
L: 132,
|
|
118
|
-
S: this,
|
|
119
|
-
A: [
|
|
120
|
-
"seq < this.length",
|
|
121
|
-
"'Invalid seq after write'"
|
|
122
|
-
]
|
|
123
|
-
});
|
|
94
|
+
invariant(seq < this.length, "Invalid seq after write", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 103, S: this, A: ["seq < this.length", "'Invalid seq after write'"] });
|
|
124
95
|
log("write complete", {
|
|
125
96
|
feed: this._key,
|
|
126
97
|
seq
|
|
127
|
-
}, {
|
|
128
|
-
F: __dxlog_file,
|
|
129
|
-
L: 133,
|
|
130
|
-
S: this,
|
|
131
|
-
C: (f, a) => f(...a)
|
|
132
|
-
});
|
|
98
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 104, S: this });
|
|
133
99
|
const receipt = {
|
|
134
100
|
feedKey: this.key,
|
|
135
101
|
seq
|
|
@@ -176,12 +142,7 @@ var FeedWrapper = class {
|
|
|
176
142
|
feed: this._key,
|
|
177
143
|
count: this._pendingWrites.size,
|
|
178
144
|
pendingWrites: Array.from(this._pendingWrites.values()).map((stack) => stack.getStack())
|
|
179
|
-
}, {
|
|
180
|
-
F: __dxlog_file,
|
|
181
|
-
L: 187,
|
|
182
|
-
S: this,
|
|
183
|
-
C: (f, a) => f(...a)
|
|
184
|
-
});
|
|
145
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 149, S: this });
|
|
185
146
|
}
|
|
186
147
|
this._closed = true;
|
|
187
148
|
await this.flushToDisk();
|
|
@@ -228,15 +189,7 @@ var FeedWrapper = class {
|
|
|
228
189
|
* Clear and check for integrity.
|
|
229
190
|
*/
|
|
230
191
|
async safeClear(from, to) {
|
|
231
|
-
invariant(from >= 0 && from < to && to <= this.length, "Invalid range", {
|
|
232
|
-
F: __dxlog_file,
|
|
233
|
-
L: 250,
|
|
234
|
-
S: this,
|
|
235
|
-
A: [
|
|
236
|
-
"from >= 0 && from < to && to <= this.length",
|
|
237
|
-
"'Invalid range'"
|
|
238
|
-
]
|
|
239
|
-
});
|
|
192
|
+
invariant(from >= 0 && from < to && to <= this.length, "Invalid range", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 198, S: this, A: ["from >= 0 && from < to && to <= this.length", "'Invalid range'"] });
|
|
240
193
|
const CHECK_MESSAGES = 20;
|
|
241
194
|
const checkBegin = to;
|
|
242
195
|
const checkEnd = Math.min(checkBegin + CHECK_MESSAGES, this.length);
|
|
@@ -269,24 +222,8 @@ var BatchedReadStream = class extends Readable {
|
|
|
269
222
|
super({
|
|
270
223
|
objectMode: true
|
|
271
224
|
});
|
|
272
|
-
invariant(opts.live === true, "Only live mode supported", {
|
|
273
|
-
|
|
274
|
-
L: 292,
|
|
275
|
-
S: this,
|
|
276
|
-
A: [
|
|
277
|
-
"opts.live === true",
|
|
278
|
-
"'Only live mode supported'"
|
|
279
|
-
]
|
|
280
|
-
});
|
|
281
|
-
invariant(opts.batch !== void 0 && opts.batch > 1, void 0, {
|
|
282
|
-
F: __dxlog_file,
|
|
283
|
-
L: 293,
|
|
284
|
-
S: this,
|
|
285
|
-
A: [
|
|
286
|
-
"opts.batch !== undefined && opts.batch > 1",
|
|
287
|
-
""
|
|
288
|
-
]
|
|
289
|
-
});
|
|
225
|
+
invariant(opts.live === true, "Only live mode supported", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 231, S: this, A: ["opts.live === true", "'Only live mode supported'"] });
|
|
226
|
+
invariant(opts.batch !== void 0 && opts.batch > 1, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 232, S: this, A: ["opts.batch !== undefined && opts.batch > 1", ""] });
|
|
290
227
|
this._feed = feed;
|
|
291
228
|
this._batch = opts.batch;
|
|
292
229
|
this._cursor = opts.start ?? 0;
|
|
@@ -350,12 +287,7 @@ var FeedFactory = class {
|
|
|
350
287
|
constructor({ root, signer, hypercore: hypercore2 }) {
|
|
351
288
|
log2("FeedFactory", {
|
|
352
289
|
options: hypercore2
|
|
353
|
-
}, {
|
|
354
|
-
F: __dxlog_file2,
|
|
355
|
-
L: 43,
|
|
356
|
-
S: this,
|
|
357
|
-
C: (f, a) => f(...a)
|
|
358
|
-
});
|
|
290
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 17, S: this });
|
|
359
291
|
this._root = root ?? failUndefined();
|
|
360
292
|
this._signer = signer;
|
|
361
293
|
this._hypercoreOptions = hypercore2;
|
|
@@ -368,12 +300,7 @@ var FeedFactory = class {
|
|
|
368
300
|
throw new Error("Signer required to create writable feeds.");
|
|
369
301
|
}
|
|
370
302
|
if (options?.secretKey) {
|
|
371
|
-
log2.warn("Secret key ignored due to signer.", void 0, {
|
|
372
|
-
F: __dxlog_file2,
|
|
373
|
-
L: 58,
|
|
374
|
-
S: this,
|
|
375
|
-
C: (f, a) => f(...a)
|
|
376
|
-
});
|
|
303
|
+
log2.warn("Secret key ignored due to signer.", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 32, S: this });
|
|
377
304
|
}
|
|
378
305
|
const key = await subtleCrypto.digest("SHA-256", Buffer.from(publicKey.toHex()));
|
|
379
306
|
const opts = defaultsDeep({}, this._hypercoreOptions, {
|
|
@@ -387,12 +314,7 @@ var FeedFactory = class {
|
|
|
387
314
|
const { type, native } = storageDir.getOrCreateFile(filename);
|
|
388
315
|
log2("created", {
|
|
389
316
|
path: `${type}:${this._root.path}/${publicKey.truncate()}/${filename}`
|
|
390
|
-
}, {
|
|
391
|
-
F: __dxlog_file2,
|
|
392
|
-
L: 82,
|
|
393
|
-
S: this,
|
|
394
|
-
C: (f, a) => f(...a)
|
|
395
|
-
});
|
|
317
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 46, S: this });
|
|
396
318
|
return native;
|
|
397
319
|
};
|
|
398
320
|
const core = hypercore(makeStorage, Buffer.from(key), opts);
|
|
@@ -436,30 +358,9 @@ var FeedStore = class {
|
|
|
436
358
|
async openFeed(feedKey, { writable, sparse } = {}) {
|
|
437
359
|
log3("opening feed", {
|
|
438
360
|
feedKey
|
|
439
|
-
}, {
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
S: this,
|
|
443
|
-
C: (f, a) => f(...a)
|
|
444
|
-
});
|
|
445
|
-
invariant2(feedKey, void 0, {
|
|
446
|
-
F: __dxlog_file3,
|
|
447
|
-
L: 56,
|
|
448
|
-
S: this,
|
|
449
|
-
A: [
|
|
450
|
-
"feedKey",
|
|
451
|
-
""
|
|
452
|
-
]
|
|
453
|
-
});
|
|
454
|
-
invariant2(!this._closed, "Feed store is closed", {
|
|
455
|
-
F: __dxlog_file3,
|
|
456
|
-
L: 57,
|
|
457
|
-
S: this,
|
|
458
|
-
A: [
|
|
459
|
-
"!this._closed",
|
|
460
|
-
"'Feed store is closed'"
|
|
461
|
-
]
|
|
462
|
-
});
|
|
361
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 36, S: this });
|
|
362
|
+
invariant2(feedKey, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 39, S: this, A: ["feedKey", ""] });
|
|
363
|
+
invariant2(!this._closed, "Feed store is closed", { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 40, S: this, A: ["!this._closed", "'Feed store is closed'"] });
|
|
463
364
|
const mutex = defaultMap(this._mutexes, feedKey, () => new Mutex());
|
|
464
365
|
return mutex.executeSynchronized(async () => {
|
|
465
366
|
let feed = this.getFeed(feedKey);
|
|
@@ -482,12 +383,7 @@ var FeedStore = class {
|
|
|
482
383
|
this.feedOpened.emit(feed);
|
|
483
384
|
log3("opened", {
|
|
484
385
|
feedKey
|
|
485
|
-
}, {
|
|
486
|
-
F: __dxlog_file3,
|
|
487
|
-
L: 85,
|
|
488
|
-
S: this,
|
|
489
|
-
C: (f, a) => f(...a)
|
|
490
|
-
});
|
|
386
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 63, S: this });
|
|
491
387
|
return feed;
|
|
492
388
|
});
|
|
493
389
|
}
|
|
@@ -495,32 +391,14 @@ var FeedStore = class {
|
|
|
495
391
|
* Close all feeds.
|
|
496
392
|
*/
|
|
497
393
|
async close() {
|
|
498
|
-
log3("closing...", void 0, {
|
|
499
|
-
F: __dxlog_file3,
|
|
500
|
-
L: 94,
|
|
501
|
-
S: this,
|
|
502
|
-
C: (f, a) => f(...a)
|
|
503
|
-
});
|
|
394
|
+
log3("closing...", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 72, S: this });
|
|
504
395
|
this._closed = true;
|
|
505
396
|
await Promise.all(Array.from(this._feeds.values()).map(async (feed) => {
|
|
506
397
|
await feed.close();
|
|
507
|
-
invariant2(feed.closed, void 0, {
|
|
508
|
-
F: __dxlog_file3,
|
|
509
|
-
L: 99,
|
|
510
|
-
S: this,
|
|
511
|
-
A: [
|
|
512
|
-
"feed.closed",
|
|
513
|
-
""
|
|
514
|
-
]
|
|
515
|
-
});
|
|
398
|
+
invariant2(feed.closed, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 76, S: this, A: ["feed.closed", ""] });
|
|
516
399
|
}));
|
|
517
400
|
this._feeds.clear();
|
|
518
|
-
log3("closed", void 0, {
|
|
519
|
-
F: __dxlog_file3,
|
|
520
|
-
L: 108,
|
|
521
|
-
S: this,
|
|
522
|
-
C: (f, a) => f(...a)
|
|
523
|
-
});
|
|
401
|
+
log3("closed", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 83, S: this });
|
|
524
402
|
}
|
|
525
403
|
};
|
|
526
404
|
|
|
@@ -529,4 +407,4 @@ export {
|
|
|
529
407
|
FeedFactory,
|
|
530
408
|
FeedStore
|
|
531
409
|
};
|
|
532
|
-
//# sourceMappingURL=chunk-
|
|
410
|
+
//# sourceMappingURL=chunk-MB63GMGR.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/feed-wrapper.ts", "../../../src/feed-factory.ts", "../../../src/feed-store.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { inspect } from 'node:util';\nimport { promisify } from 'node:util';\nimport { Readable, Transform } from 'streamx';\n\nimport { Trigger } from '@dxos/async';\nimport { StackTrace, inspectObject } from '@dxos/debug';\nimport type { Hypercore, HypercoreProperties, ReadStreamOptions } from '@dxos/hypercore';\nimport { assertArgument, invariant } from '@dxos/invariant';\nimport { type PublicKey } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { type Directory } from '@dxos/random-access-storage';\nimport { arrayToBuffer, rangeFromTo } from '@dxos/util';\nimport type { GetOptions, Proof } from '@dxos/vendor-hypercore/hypercore';\n\nimport { type FeedWriter, type WriteReceipt } from './feed-writer';\n\n/**\n * Async feed wrapper.\n */\nexport class FeedWrapper<T extends {}> {\n private _hypercore: Hypercore<T>;\n private readonly _pendingWrites = new Set<StackTrace>();\n\n // Pending while writes are happening. Resolves when there are no pending writes.\n private readonly _writeLock = new Trigger();\n\n private _closed = false;\n\n constructor(\n hypercore: Hypercore<T>,\n private _key: PublicKey, // TODO(burdon): Required since currently patching the key inside factory.\n private _storageDirectory: Directory,\n ) {\n assertArgument(hypercore, 'hypercore');\n this._hypercore = hypercore;\n invariant(this._key);\n this._writeLock.wake();\n }\n\n [inspect.custom](): string {\n return inspectObject(this);\n }\n\n toJSON(): { feedKey: PublicKey; length: number; opened: boolean; closed: boolean } {\n return {\n feedKey: this._key,\n length: this.properties.length,\n opened: this.properties.opened,\n closed: this.properties.closed,\n };\n }\n\n get key(): PublicKey {\n return this._key;\n }\n\n get core(): Hypercore<T> {\n return this._hypercore;\n }\n\n // TODO(burdon): Create proxy.\n get properties(): HypercoreProperties {\n return this._hypercore;\n }\n\n createReadableStream(opts?: ReadStreamOptions): Readable {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const self = this;\n const transform = new Transform({\n transform(data: any, cb: (err?: Error | null, data?: any) => void) {\n // Delay until write is complete.\n void self._writeLock.wait().then(() => {\n this.push(data);\n cb();\n });\n },\n });\n const readStream =\n opts?.batch !== undefined && opts?.batch > 1\n ? new BatchedReadStream(this._hypercore, opts)\n : this._hypercore.createReadStream(opts);\n\n readStream.pipe(transform, (err: any) => {\n // Ignore errors.\n // We might get \"Writable stream closed prematurely\" error.\n // Its okay since the pipeline is closed and does not expect more messages.\n });\n\n return transform;\n }\n\n createFeedWriter(): FeedWriter<T> {\n return {\n write: async (data: T, { afterWrite } = {}) => {\n log('write', { feed: this._key, seq: this._hypercore.length });\n invariant(!this._closed, 'Feed closed');\n const stackTrace = new StackTrace();\n\n try {\n // Pending writes pause the read stream.\n this._pendingWrites.add(stackTrace);\n if (this._pendingWrites.size === 1) {\n this._writeLock.reset();\n }\n\n const receipt = await this.appendWithReceipt(data);\n\n // TODO(dmaretskyi): Removing this will make user-intiated writes faster but might result in a data-loss.\n await this.flushToDisk();\n\n await afterWrite?.(receipt);\n\n return receipt;\n } finally {\n // Unblock the read stream after the write (and callback) is complete.\n this._pendingWrites.delete(stackTrace);\n if (this._pendingWrites.size === 0) {\n this._writeLock.wake();\n }\n }\n },\n };\n }\n\n async appendWithReceipt(data: T): Promise<WriteReceipt> {\n const seq = await this.append(data);\n invariant(seq < this.length, 'Invalid seq after write');\n log('write complete', { feed: this._key, seq });\n const receipt: WriteReceipt = {\n feedKey: this.key,\n seq,\n };\n return receipt;\n }\n\n /**\n * Flush pending changes to disk.\n * Calling this is not required unless you want to explicitly wait for data to be written.\n */\n async flushToDisk(): Promise<void> {\n await this._storageDirectory.flush();\n }\n\n get opened(): boolean {\n return this._hypercore.opened;\n }\n\n get closed(): boolean {\n return this._hypercore.closed;\n }\n\n get readable(): boolean {\n return this._hypercore.readable;\n }\n\n get length(): number {\n return this._hypercore.length;\n }\n\n get byteLength(): number {\n return this._hypercore.byteLength;\n }\n\n on(...args: any[]) {\n return (this._hypercore as any).on(...args);\n }\n\n off(...args: any[]) {\n return (this._hypercore as any).off(...args);\n }\n\n open(...args: Parameters<Hypercore<T>['open']>) {\n return promisify(this._hypercore.open.bind(this._hypercore) as any)(...args);\n }\n\n _close(...args: Parameters<Hypercore<T>['close']>) {\n return promisify(this._hypercore.close.bind(this._hypercore) as any)(...args);\n }\n\n close = async () => {\n if (this._pendingWrites.size) {\n log.warn('Closing feed with pending writes', {\n feed: this._key,\n count: this._pendingWrites.size,\n pendingWrites: Array.from(this._pendingWrites.values()).map((stack) => stack.getStack()),\n });\n }\n this._closed = true;\n await this.flushToDisk();\n await this._close();\n };\n\n has(start: number, end?: number) {\n return this._hypercore.has(start, end);\n }\n\n get(index: number, options?: GetOptions) {\n return promisify(this._hypercore.get.bind(this._hypercore) as any)(index, options);\n }\n\n // TODO(dmaretskyi): Type better\n append(data: any | any[]): Promise<number> {\n return promisify(this._hypercore.append.bind(this._hypercore))(data);\n }\n\n /**\n * Will not resolve if `end` parameter is not specified and the feed is not closed.\n */\n download(...args: Parameters<Hypercore<T>['download']>) {\n return this._hypercore.download(...args);\n }\n\n undownload(...args: Parameters<Hypercore<T>['undownload']>) {\n return this._hypercore.undownload(...args);\n }\n\n setDownloading(...args: Parameters<Hypercore<T>['setDownloading']>) {\n return this._hypercore.setDownloading(...args);\n }\n\n replicate(...args: Parameters<Hypercore<T>['replicate']>) {\n return this._hypercore.replicate(...args);\n }\n\n clear(start: number, end?: number) {\n return promisify(this._hypercore.clear.bind(this._hypercore))(start, end);\n }\n\n proof(index: number, options?: any) {\n return promisify(this._hypercore.proof.bind(this._hypercore))(index);\n }\n\n put(index: number, data: T, proof: Proof) {\n return promisify(this._hypercore.put.bind(this._hypercore))(index, data, proof);\n }\n\n putBuffer(index: number, data: Buffer | Uint8Array, proof: Proof, peer: null): Promise<void> {\n return promisify((this._hypercore as any)._putBuffer.bind(this._hypercore) as any)(index, data, proof, peer);\n }\n\n /**\n * Clear and check for integrity.\n */\n async safeClear(from: number, to: number): Promise<void> {\n invariant(from >= 0 && from < to && to <= this.length, 'Invalid range');\n\n const CHECK_MESSAGES = 20;\n const checkBegin = to;\n const checkEnd = Math.min(checkBegin + CHECK_MESSAGES, this.length);\n\n const messagesBefore = await Promise.all(\n rangeFromTo(checkBegin, checkEnd).map((idx) =>\n this.get(idx, {\n valueEncoding: { decode: (x: Uint8Array) => x } as any,\n }),\n ),\n );\n\n await this.clear(from, to);\n\n const messagesAfter = await Promise.all(\n rangeFromTo(checkBegin, checkEnd).map((idx) =>\n this.get(idx, {\n valueEncoding: { decode: (x: Uint8Array) => x } as any,\n }),\n ),\n );\n\n for (let i = 0; i < messagesBefore.length; i++) {\n const before = arrayToBuffer(messagesBefore[i]);\n const after = arrayToBuffer(messagesAfter[i]);\n if (!before.equals(after)) {\n throw new Error('Feed corruption on clear. There has likely been a data loss.');\n }\n }\n }\n}\n\nclass BatchedReadStream extends Readable {\n private readonly _feed: Hypercore<any>;\n private readonly _batch: number;\n private _cursor: number;\n private _reading = false;\n\n constructor(feed: Hypercore<any>, opts: ReadStreamOptions = {}) {\n super({ objectMode: true });\n invariant(opts.live === true, 'Only live mode supported');\n invariant(opts.batch !== undefined && opts.batch > 1);\n this._feed = feed;\n this._batch = opts.batch;\n this._cursor = opts.start ?? 0;\n }\n\n override _open(cb: (err: Error | null) => void): void {\n this._feed.ready(cb);\n }\n\n override _read(cb: (err: Error | null) => void): void {\n if (this._reading) {\n return;\n }\n\n if (this._feed.bitfield!.total(this._cursor, this._cursor + this._batch) === this._batch) {\n this._batchedRead(cb);\n } else {\n this._nonBatchedRead(cb);\n }\n }\n\n private _nonBatchedRead(cb: (err: Error | null) => void): void {\n this._feed.get(this._cursor, { wait: true }, (err, data) => {\n if (err) {\n cb(err);\n } else {\n this._cursor++;\n this._reading = false;\n this.push(data);\n cb(null);\n }\n });\n }\n\n private _batchedRead(cb: (err: Error | null) => void): void {\n this._feed.getBatch(this._cursor, this._cursor + this._batch, { wait: true }, (err, data) => {\n if (err) {\n cb(err);\n } else {\n this._cursor += data.length;\n this._reading = false;\n for (const item of data) {\n this.push(item);\n }\n cb(null);\n }\n });\n }\n}\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport defaultsDeep from 'lodash.defaultsdeep';\n\nimport { type Signer, subtleCrypto } from '@dxos/crypto';\nimport { failUndefined } from '@dxos/debug';\nimport type { HypercoreOptions } from '@dxos/hypercore';\nimport { createCrypto, hypercore } from '@dxos/hypercore';\nimport { type PublicKey } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { type Directory } from '@dxos/random-access-storage';\n\nimport { FeedWrapper } from './feed-wrapper';\n\nexport type FeedFactoryOptions = {\n root: Directory;\n signer?: Signer;\n hypercore?: HypercoreOptions;\n};\n\nexport type FeedOptions = HypercoreOptions & {\n writable?: boolean;\n /**\n * Optional hook called before data is written after being verified.\n * Called for writes done by this peer as well as for data replicated from other peers.\n * NOTE: The callback must be invoked to complete the write operation.\n * @param peer Always null in hypercore@9.12.0.\n */\n onwrite?: (index: number, data: any, peer: null, cb: (err: Error | null) => void) => void;\n};\n\n/**\n * Hypercore factory.\n */\nexport class FeedFactory<T extends {}> {\n private readonly _root: Directory;\n private readonly _signer?: Signer;\n private readonly _hypercoreOptions?: HypercoreOptions;\n\n constructor({ root, signer, hypercore }: FeedFactoryOptions) {\n log('FeedFactory', { options: hypercore });\n this._root = root ?? failUndefined();\n this._signer = signer;\n this._hypercoreOptions = hypercore;\n }\n\n get storageRoot() {\n return this._root;\n }\n\n async createFeed(publicKey: PublicKey, options?: FeedOptions): Promise<FeedWrapper<T>> {\n if (options?.writable && !this._signer) {\n throw new Error('Signer required to create writable feeds.');\n }\n if (options?.secretKey) {\n log.warn('Secret key ignored due to signer.');\n }\n\n // Required due to hypercore's 32-byte key limit.\n const key = await subtleCrypto.digest('SHA-256', Buffer.from(publicKey.toHex()));\n\n const opts = defaultsDeep(\n {\n // sparse: false,\n // stats: false,\n },\n this._hypercoreOptions,\n {\n secretKey: this._signer && options?.writable ? Buffer.from('secret') : undefined,\n crypto: this._signer ? createCrypto(this._signer, publicKey) : undefined,\n onwrite: options?.onwrite,\n noiseKeyPair: {}, // We're not using noise.\n },\n options,\n );\n\n const storageDir = this._root.createDirectory(publicKey.toHex());\n const makeStorage = (filename: string) => {\n const { type, native } = storageDir.getOrCreateFile(filename);\n log('created', {\n path: `${type}:${this._root.path}/${publicKey.truncate()}/${filename}`,\n });\n\n return native;\n };\n\n const core = hypercore(makeStorage, Buffer.from(key), opts);\n return new FeedWrapper(core, publicKey, storageDir);\n }\n}\n", "//\n// Copyright 2019 DXOS.org\n//\n\nimport { Event, Mutex } from '@dxos/async';\nimport { failUndefined } from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\nimport { PublicKey } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { ComplexMap, defaultMap } from '@dxos/util';\n\nimport { type FeedFactory, type FeedOptions } from './feed-factory';\nimport { type FeedWrapper } from './feed-wrapper';\n\nexport interface FeedStoreOptions<T extends {}> {\n factory: FeedFactory<T>;\n}\n\n/**\n * Persistent hypercore store.\n */\nexport class FeedStore<T extends {}> {\n private readonly _feeds: ComplexMap<PublicKey, FeedWrapper<T>> = new ComplexMap(PublicKey.hash);\n private readonly _mutexes = new ComplexMap<PublicKey, Mutex>(PublicKey.hash);\n private readonly _factory: FeedFactory<T>;\n\n private _closed = false;\n\n readonly feedOpened = new Event<FeedWrapper<T>>();\n\n constructor({ factory }: FeedStoreOptions<T>) {\n this._factory = factory ?? failUndefined();\n }\n\n get size() {\n return this._feeds.size;\n }\n\n get feeds() {\n return Array.from(this._feeds.values());\n }\n\n /**\n * Get the open feed if it exists.\n */\n getFeed(publicKey: PublicKey): FeedWrapper<T> | undefined {\n return this._feeds.get(publicKey);\n }\n\n /**\n * Gets or opens a feed.\n * The feed is readonly unless a secret key is provided.\n */\n async openFeed(feedKey: PublicKey, { writable, sparse }: FeedOptions = {}): Promise<FeedWrapper<T>> {\n log('opening feed', { feedKey });\n invariant(feedKey);\n invariant(!this._closed, 'Feed store is closed');\n\n const mutex = defaultMap(this._mutexes, feedKey, () => new Mutex());\n\n return mutex.executeSynchronized(async () => {\n let feed = this.getFeed(feedKey);\n if (feed) {\n // TODO(burdon): Need to check that there's another instance being used (create test and break this).\n // TODO(burdon): Remove from store if feed is closed externally? (remove wrapped open/close methods?)\n if (writable && !feed.properties.writable) {\n throw new Error(`Read-only feed is already open: ${feedKey.truncate()}`);\n } else if ((sparse ?? false) !== feed.properties.sparse) {\n throw new Error(\n `Feed already open with different sparse setting: ${feedKey.truncate()} [${sparse} !== ${\n feed.properties.sparse\n }]`,\n );\n } else {\n await feed.open();\n return feed;\n }\n }\n\n feed = await this._factory.createFeed(feedKey, { writable, sparse });\n this._feeds.set(feed.key, feed);\n\n await feed.open();\n this.feedOpened.emit(feed);\n log('opened', { feedKey });\n return feed;\n });\n }\n\n /**\n * Close all feeds.\n */\n async close(): Promise<void> {\n log('closing...');\n this._closed = true;\n await Promise.all(\n Array.from(this._feeds.values()).map(async (feed) => {\n await feed.close();\n invariant(feed.closed);\n // TODO(burdon): SpaceProxy still being initialized.\n // SpaceProxy.initialize => Database.createItem => ... => FeedWrapper.append\n // Uncaught Error: Closed [random-access-storage/index.js:181:38]\n // await sleep(100);\n }),\n );\n\n this._feeds.clear();\n log('closed');\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;AAIA,SAASA,eAAe;AACxB,SAASC,iBAAiB;AAC1B,SAASC,UAAUC,iBAAiB;AAEpC,SAASC,eAAe;AACxB,SAASC,YAAYC,qBAAqB;AAE1C,SAASC,gBAAgBC,iBAAiB;AAE1C,SAASC,WAAW;AAEpB,SAASC,eAAeC,mBAAmB;AAK3C,IAAA,eAAA;;EAIUC;EACSC;EAEjB;EACiBC,iBAAiBV,oBAAAA,IAAAA;;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,KAAA,CAAA;;;",
|
|
6
|
+
"names": ["inspect", "promisify", "Readable", "Transform", "Trigger", "StackTrace", "inspectObject", "assertArgument", "invariant", "log", "arrayToBuffer", "rangeFromTo", "_hypercore", "_pendingWrites", "_writeLock", "hypercore", "_key", "_storageDirectory", "wake", "custom", "toJSON", "feedKey", "length", "opened", "properties", "closed", "key", "core", "createReadableStream", "transform", "self", "cb", "readStream", "createFeedWriter", "seq", "stackTrace", "reset", "receipt", "flushToDisk", "readable", "byteLength", "open", "args", "_close", "close", "feed", "count", "pendingWrites", "_closed", "start", "index", "options", "append", "undownload", "setDownloading", "replicate", "clear", "proof", "data", "putBuffer", "CHECK_MESSAGES", "checkBegin", "to", "checkEnd", "min", "messagesBefore", "valueEncoding", "x", "messagesAfter", "i", "after", "before", "equals", "Error", "_batch", "_cursor", "_reading", "objectMode", "live", "opts", "_open", "_read", "_nonBatchedRead", "err", "push", "_batchedRead", "wait", "item", "defaultsDeep", "subtleCrypto", "failUndefined", "createCrypto", "hypercore", "log", "__dxlog_file", "_signer", "_hypercoreOptions", "options", "hypercore", "log", "root", "signer", "storageRoot", "createFeed", "secretKey", "crypto", "createCrypto", "onwrite", "noiseKeyPair", "storageDir", "makeStorage", "native", "getOrCreateFile", "filename", "path", "type", "truncate", "core", "Event", "Mutex", "failUndefined", "invariant", "PublicKey", "log", "ComplexMap", "defaultMap", "__dxlog_file", "_mutexes", "_factory", "_closed", "feedOpened", "factory", "size", "feeds", "feedKey", "mutex", "executeSynchronized", "feed", "getFeed", "sparse", "properties", "Error", "emit", "from", "_feeds", "close", "closed"]
|
|
7
|
+
}
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
FeedFactory,
|
|
4
4
|
FeedStore,
|
|
5
5
|
FeedWrapper
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-MB63GMGR.mjs";
|
|
7
7
|
|
|
8
8
|
// src/feed-iterator.ts
|
|
9
9
|
import safeRace from "race-as-promised";
|
|
@@ -76,12 +76,7 @@ var FeedQueue = class {
|
|
|
76
76
|
this._index = options.start ?? 0;
|
|
77
77
|
log("opening", {
|
|
78
78
|
feedKey: this._feed.key
|
|
79
|
-
}, {
|
|
80
|
-
F: __dxlog_file,
|
|
81
|
-
L: 93,
|
|
82
|
-
S: this,
|
|
83
|
-
C: (f, a) => f(...a)
|
|
84
|
-
});
|
|
79
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 69, S: this });
|
|
85
80
|
const opts = Object.assign({}, defaultReadStreamOptions, options);
|
|
86
81
|
const feedStream = this._feed.createReadableStream(opts);
|
|
87
82
|
this._feedConsumer = new Writable({
|
|
@@ -119,12 +114,7 @@ var FeedQueue = class {
|
|
|
119
114
|
}
|
|
120
115
|
log.catch(err, {
|
|
121
116
|
feedKey: this._feed.key
|
|
122
|
-
}, {
|
|
123
|
-
F: __dxlog_file,
|
|
124
|
-
L: 138,
|
|
125
|
-
S: this,
|
|
126
|
-
C: (f, a) => f(...a)
|
|
127
|
-
});
|
|
117
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 108, S: this });
|
|
128
118
|
};
|
|
129
119
|
this._feed.core.once("close", onClose);
|
|
130
120
|
this._feedConsumer.on("error", onError);
|
|
@@ -135,55 +125,24 @@ var FeedQueue = class {
|
|
|
135
125
|
}
|
|
136
126
|
this._destroyConsumer();
|
|
137
127
|
});
|
|
138
|
-
log("opened", void 0, {
|
|
139
|
-
F: __dxlog_file,
|
|
140
|
-
L: 156,
|
|
141
|
-
S: this,
|
|
142
|
-
C: (f, a) => f(...a)
|
|
143
|
-
});
|
|
128
|
+
log("opened", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 124, S: this });
|
|
144
129
|
}
|
|
145
130
|
/**
|
|
146
131
|
* Closes the queue.
|
|
147
132
|
*/
|
|
148
133
|
async close() {
|
|
149
134
|
if (this.isOpen) {
|
|
150
|
-
invariant(this._feedConsumer, void 0, {
|
|
151
|
-
|
|
152
|
-
L: 164,
|
|
153
|
-
S: this,
|
|
154
|
-
A: [
|
|
155
|
-
"this._feedConsumer",
|
|
156
|
-
""
|
|
157
|
-
]
|
|
158
|
-
});
|
|
159
|
-
invariant(!this._feed.properties.closed, void 0, {
|
|
160
|
-
F: __dxlog_file,
|
|
161
|
-
L: 165,
|
|
162
|
-
S: this,
|
|
163
|
-
A: [
|
|
164
|
-
"!this._feed.properties.closed",
|
|
165
|
-
""
|
|
166
|
-
]
|
|
167
|
-
});
|
|
135
|
+
invariant(this._feedConsumer, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 130, S: this, A: ["this._feedConsumer", ""] });
|
|
136
|
+
invariant(!this._feed.properties.closed, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 131, S: this, A: ["!this._feed.properties.closed", ""] });
|
|
168
137
|
log("closing", {
|
|
169
138
|
feedKey: this._feed.key
|
|
170
|
-
}, {
|
|
171
|
-
F: __dxlog_file,
|
|
172
|
-
L: 167,
|
|
173
|
-
S: this,
|
|
174
|
-
C: (f, a) => f(...a)
|
|
175
|
-
});
|
|
139
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 132, S: this });
|
|
176
140
|
const [closed, setClosed] = latch();
|
|
177
141
|
this._feedConsumer.once("close", setClosed);
|
|
178
142
|
this._feedConsumer.destroy();
|
|
179
143
|
this._next?.();
|
|
180
144
|
await closed();
|
|
181
|
-
log("closed", void 0, {
|
|
182
|
-
F: __dxlog_file,
|
|
183
|
-
L: 173,
|
|
184
|
-
S: this,
|
|
185
|
-
C: (f, a) => f(...a)
|
|
186
|
-
});
|
|
145
|
+
log("closed", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 140, S: this });
|
|
187
146
|
}
|
|
188
147
|
}
|
|
189
148
|
/**
|
|
@@ -212,12 +171,7 @@ var FeedQueue = class {
|
|
|
212
171
|
if (this._feedConsumer) {
|
|
213
172
|
log("queue closed", {
|
|
214
173
|
feedKey: this._feed.key
|
|
215
|
-
}, {
|
|
216
|
-
F: __dxlog_file,
|
|
217
|
-
L: 206,
|
|
218
|
-
S: this,
|
|
219
|
-
C: (f, a) => f(...a)
|
|
220
|
-
});
|
|
174
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 165, S: this });
|
|
221
175
|
this._feedConsumer = void 0;
|
|
222
176
|
this._next = void 0;
|
|
223
177
|
this._currentBlock = void 0;
|
|
@@ -246,66 +200,30 @@ var AbstractFeedIterator = class {
|
|
|
246
200
|
}
|
|
247
201
|
async open() {
|
|
248
202
|
if (!this._open) {
|
|
249
|
-
log2("opening...", void 0, {
|
|
250
|
-
F: __dxlog_file2,
|
|
251
|
-
L: 41,
|
|
252
|
-
S: this,
|
|
253
|
-
C: (f, a) => f(...a)
|
|
254
|
-
});
|
|
203
|
+
log2("opening...", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 29, S: this });
|
|
255
204
|
await this._onOpen();
|
|
256
205
|
this._open = true;
|
|
257
206
|
await this.start();
|
|
258
|
-
log2("opened", void 0, {
|
|
259
|
-
F: __dxlog_file2,
|
|
260
|
-
L: 46,
|
|
261
|
-
S: this,
|
|
262
|
-
C: (f, a) => f(...a)
|
|
263
|
-
});
|
|
207
|
+
log2("opened", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 33, S: this });
|
|
264
208
|
}
|
|
265
209
|
}
|
|
266
210
|
async close() {
|
|
267
211
|
if (this._open) {
|
|
268
|
-
log2("closing...", void 0, {
|
|
269
|
-
F: __dxlog_file2,
|
|
270
|
-
L: 52,
|
|
271
|
-
S: this,
|
|
272
|
-
C: (f, a) => f(...a)
|
|
273
|
-
});
|
|
212
|
+
log2("closing...", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 38, S: this });
|
|
274
213
|
await this.stop();
|
|
275
214
|
await this._onClose();
|
|
276
215
|
this._open = false;
|
|
277
|
-
log2("closed", void 0, {
|
|
278
|
-
F: __dxlog_file2,
|
|
279
|
-
L: 57,
|
|
280
|
-
S: this,
|
|
281
|
-
C: (f, a) => f(...a)
|
|
282
|
-
});
|
|
216
|
+
log2("closed", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 42, S: this });
|
|
283
217
|
}
|
|
284
218
|
}
|
|
285
219
|
async start() {
|
|
286
|
-
invariant2(this._open, void 0, {
|
|
287
|
-
F: __dxlog_file2,
|
|
288
|
-
L: 62,
|
|
289
|
-
S: this,
|
|
290
|
-
A: [
|
|
291
|
-
"this._open",
|
|
292
|
-
""
|
|
293
|
-
]
|
|
294
|
-
});
|
|
220
|
+
invariant2(this._open, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 46, S: this, A: ["this._open", ""] });
|
|
295
221
|
if (!this._running) {
|
|
296
222
|
this._running = true;
|
|
297
223
|
}
|
|
298
224
|
}
|
|
299
225
|
async stop() {
|
|
300
|
-
invariant2(this._open, void 0, {
|
|
301
|
-
F: __dxlog_file2,
|
|
302
|
-
L: 69,
|
|
303
|
-
S: this,
|
|
304
|
-
A: [
|
|
305
|
-
"this._open",
|
|
306
|
-
""
|
|
307
|
-
]
|
|
308
|
-
});
|
|
226
|
+
invariant2(this._open, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 52, S: this, A: ["this._open", ""] });
|
|
309
227
|
if (this._running) {
|
|
310
228
|
this._running = false;
|
|
311
229
|
this._stopTrigger.wake();
|
|
@@ -318,12 +236,7 @@ var AbstractFeedIterator = class {
|
|
|
318
236
|
return this._generator();
|
|
319
237
|
}
|
|
320
238
|
async *_generator() {
|
|
321
|
-
log2("started", void 0, {
|
|
322
|
-
F: __dxlog_file2,
|
|
323
|
-
L: 85,
|
|
324
|
-
S: this,
|
|
325
|
-
C: (f, a) => f(...a)
|
|
326
|
-
});
|
|
239
|
+
log2("started", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 65, S: this });
|
|
327
240
|
while (this._running) {
|
|
328
241
|
const block = await safeRace([
|
|
329
242
|
this._stopTrigger.wait(),
|
|
@@ -334,12 +247,7 @@ var AbstractFeedIterator = class {
|
|
|
334
247
|
}
|
|
335
248
|
yield block;
|
|
336
249
|
}
|
|
337
|
-
log2("stopped", void 0, {
|
|
338
|
-
F: __dxlog_file2,
|
|
339
|
-
L: 97,
|
|
340
|
-
S: this,
|
|
341
|
-
C: (f, a) => f(...a)
|
|
342
|
-
});
|
|
250
|
+
log2("stopped", void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 77, S: this });
|
|
343
251
|
}
|
|
344
252
|
};
|
|
345
253
|
var FeedIterator = class extends AbstractFeedIterator {
|
|
@@ -383,24 +291,8 @@ var FeedSetIterator = class extends AbstractFeedIterator {
|
|
|
383
291
|
stalled = new Event2();
|
|
384
292
|
constructor(_selector, options = defaultFeedSetIteratorOptions) {
|
|
385
293
|
super(), this._selector = _selector, this.options = options;
|
|
386
|
-
invariant3(_selector, void 0, {
|
|
387
|
-
|
|
388
|
-
L: 55,
|
|
389
|
-
S: this,
|
|
390
|
-
A: [
|
|
391
|
-
"_selector",
|
|
392
|
-
""
|
|
393
|
-
]
|
|
394
|
-
});
|
|
395
|
-
invariant3(options, void 0, {
|
|
396
|
-
F: __dxlog_file3,
|
|
397
|
-
L: 56,
|
|
398
|
-
S: this,
|
|
399
|
-
A: [
|
|
400
|
-
"options",
|
|
401
|
-
""
|
|
402
|
-
]
|
|
403
|
-
});
|
|
294
|
+
invariant3(_selector, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 29, S: this, A: ["_selector", ""] });
|
|
295
|
+
invariant3(options, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 30, S: this, A: ["options", ""] });
|
|
404
296
|
}
|
|
405
297
|
[inspect2.custom]() {
|
|
406
298
|
return inspectObject2(this);
|
|
@@ -428,32 +320,11 @@ var FeedSetIterator = class extends AbstractFeedIterator {
|
|
|
428
320
|
this._trigger.wake();
|
|
429
321
|
}
|
|
430
322
|
async addFeed(feed) {
|
|
431
|
-
invariant3(!this._feedQueues.has(feed.key), `Feed already added: ${feed.key}`, {
|
|
432
|
-
|
|
433
|
-
L: 91,
|
|
434
|
-
S: this,
|
|
435
|
-
A: [
|
|
436
|
-
"!this._feedQueues.has(feed.key)",
|
|
437
|
-
"`Feed already added: ${feed.key}`"
|
|
438
|
-
]
|
|
439
|
-
});
|
|
440
|
-
invariant3(feed.properties.opened, void 0, {
|
|
441
|
-
F: __dxlog_file3,
|
|
442
|
-
L: 92,
|
|
443
|
-
S: this,
|
|
444
|
-
A: [
|
|
445
|
-
"feed.properties.opened",
|
|
446
|
-
""
|
|
447
|
-
]
|
|
448
|
-
});
|
|
323
|
+
invariant3(!this._feedQueues.has(feed.key), `Feed already added: ${feed.key}`, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 58, S: this, A: ["!this._feedQueues.has(feed.key)", "`Feed already added: ${feed.key}`"] });
|
|
324
|
+
invariant3(feed.properties.opened, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 59, S: this, A: ["feed.properties.opened", ""] });
|
|
449
325
|
log3("feed added", {
|
|
450
326
|
feedKey: feed.key
|
|
451
|
-
}, {
|
|
452
|
-
F: __dxlog_file3,
|
|
453
|
-
L: 93,
|
|
454
|
-
S: this,
|
|
455
|
-
C: (f, a) => f(...a)
|
|
456
|
-
});
|
|
327
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 60, S: this });
|
|
457
328
|
const queue = new FeedQueue(feed);
|
|
458
329
|
this._feedQueues.set(feed.key, queue);
|
|
459
330
|
this._subscriptions.add(queue.updated.on(() => {
|
|
@@ -490,12 +361,7 @@ var FeedSetIterator = class extends AbstractFeedIterator {
|
|
|
490
361
|
log3("selected", {
|
|
491
362
|
idx,
|
|
492
363
|
blocks
|
|
493
|
-
}, {
|
|
494
|
-
F: __dxlog_file3,
|
|
495
|
-
L: 143,
|
|
496
|
-
S: this,
|
|
497
|
-
C: (f, a) => f(...a)
|
|
498
|
-
});
|
|
364
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 100, S: this });
|
|
499
365
|
if (idx === void 0) {
|
|
500
366
|
if (t === void 0) {
|
|
501
367
|
t = setTimeout(() => {
|
|
@@ -512,33 +378,15 @@ var FeedSetIterator = class extends AbstractFeedIterator {
|
|
|
512
378
|
throw new Error(`Index out of bounds: ${idx} of ${blocks.length}`);
|
|
513
379
|
}
|
|
514
380
|
const queue = this._feedQueues.get(blocks[idx].feedKey);
|
|
515
|
-
log3("popping", queue.toJSON(), {
|
|
516
|
-
F: __dxlog_file3,
|
|
517
|
-
L: 163,
|
|
518
|
-
S: this,
|
|
519
|
-
C: (f, a) => f(...a)
|
|
520
|
-
});
|
|
381
|
+
log3("popping", queue.toJSON(), { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 122, S: this });
|
|
521
382
|
try {
|
|
522
383
|
const message = await queue.pop();
|
|
523
|
-
invariant3(message === blocks[idx], void 0, {
|
|
524
|
-
F: __dxlog_file3,
|
|
525
|
-
L: 166,
|
|
526
|
-
S: this,
|
|
527
|
-
A: [
|
|
528
|
-
"message === blocks[idx]",
|
|
529
|
-
""
|
|
530
|
-
]
|
|
531
|
-
});
|
|
384
|
+
invariant3(message === blocks[idx], void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 125, S: this, A: ["message === blocks[idx]", ""] });
|
|
532
385
|
return message;
|
|
533
386
|
} catch (err) {
|
|
534
387
|
log3.warn("queue closed", {
|
|
535
388
|
feedKey: queue.feed.key
|
|
536
|
-
}, {
|
|
537
|
-
F: __dxlog_file3,
|
|
538
|
-
L: 170,
|
|
539
|
-
S: this,
|
|
540
|
-
C: (f, a) => f(...a)
|
|
541
|
-
});
|
|
389
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 129, S: this });
|
|
542
390
|
}
|
|
543
391
|
}
|
|
544
392
|
}
|