@dxos/feed-store 0.8.4-main.03d5cd7b56 → 0.8.4-main.05e74ebcff

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/LICENSE CHANGED
@@ -1,8 +1,105 @@
1
- MIT License
2
- Copyright (c) 2022 DXOS
1
+ # Functional Source License, Version 1.1, ALv2 Future License
3
2
 
4
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3
+ ## Abbreviation
5
4
 
6
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5
+ FSL-1.1-Apache-2.0
7
6
 
8
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7
+ ## Notice
8
+
9
+ Copyright 2026 DXOS
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under
20
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
21
+ Conditions with the Software.
22
+
23
+ ### License Grant
24
+
25
+ Subject to your compliance with this License Grant and the Patents,
26
+ Redistribution and Trademark clauses below, we hereby grant you the right to
27
+ use, copy, modify, create derivative works, publicly perform, publicly display
28
+ and redistribute the Software for any Permitted Purpose identified below.
29
+
30
+ ### Permitted Purpose
31
+
32
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
33
+ means making the Software available to others in a commercial product or
34
+ service that:
35
+
36
+ 1. substitutes for the Software;
37
+
38
+ 2. substitutes for any other product or service we offer using the Software
39
+ that exists as of the date we make the Software available; or
40
+
41
+ 3. offers the same or substantially similar functionality as the Software.
42
+
43
+ Permitted Purposes specifically include using the Software:
44
+
45
+ 1. for your internal use and access;
46
+
47
+ 2. for non-commercial education;
48
+
49
+ 3. for non-commercial research; and
50
+
51
+ 4. in connection with professional services that you provide to a licensee
52
+ using the Software in accordance with these Terms and Conditions.
53
+
54
+ ### Patents
55
+
56
+ To the extent your use for a Permitted Purpose would necessarily infringe our
57
+ patents, the license grant above includes a license under our patents. If you
58
+ make a claim against any party that the Software infringes or contributes to
59
+ the infringement of any patent, then your patent license to the Software ends
60
+ immediately.
61
+
62
+ ### Redistribution
63
+
64
+ The Terms and Conditions apply to all copies, modifications and derivatives of
65
+ the Software.
66
+
67
+ If you redistribute any copies, modifications or derivatives of the Software,
68
+ you must include a copy of or a link to these Terms and Conditions and not
69
+ remove any copyright notices provided in or with the Software.
70
+
71
+ ### Disclaimer
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
74
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
75
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
76
+
77
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
78
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
79
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
80
+
81
+ ### Trademarks
82
+
83
+ Except for displaying the License Details and identifying us as the origin of
84
+ the Software, you have no right under these Terms and Conditions to use our
85
+ trademarks, trade names, service marks or product names.
86
+
87
+ ## Grant of Future License
88
+
89
+ We hereby irrevocably grant you an additional license to use the Software under
90
+ the Apache License, Version 2.0 that is effective on the second anniversary of
91
+ the date we make the Software available. On or after that date, you may use the
92
+ Software under the Apache License, Version 2.0, in which case the following
93
+ will apply:
94
+
95
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
96
+ this file except in compliance with the License.
97
+
98
+ You may obtain a copy of the License at
99
+
100
+ http://www.apache.org/licenses/LICENSE-2.0
101
+
102
+ Unless required by applicable law or agreed to in writing, software distributed
103
+ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
104
+ CONDITIONS OF ANY KIND, either express or implied. See the License for the
105
+ specific language governing permissions and limitations under the License.
package/README.md CHANGED
@@ -18,4 +18,4 @@ pnpm i @dxos/feed-store
18
18
 
19
19
  Your ideas, issues, and code are most welcome. Please take a look at our [community code of conduct](https://github.com/dxos/dxos/blob/main/CODE_OF_CONDUCT.md), the [issue guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-issues), and the [PR contribution guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-prs).
20
20
 
21
- License: [MIT](./LICENSE) Copyright 2022 © DXOS
21
+ License: [FSL-1.1-Apache-2.0](./LICENSE) Copyright 2022 © DXOS
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/feed-iterator.ts", "../../../src/feed-queue.ts", "../../../src/feed-set-iterator.ts", "../../../src/feed-writer.ts"],
4
- "sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport safeRace from 'race-as-promised';\n\nimport { Trigger } from '@dxos/async';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nimport { FeedQueue } from './feed-queue';\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\n/**\n * Base class for an async iterable feed.\n */\nexport abstract class AbstractFeedIterator<T> implements AsyncIterable<FeedBlock<T>> {\n private readonly _stopTrigger = new Trigger();\n\n protected _open = false;\n protected _running = false;\n\n toJSON(): { open: boolean; running: boolean } {\n return {\n open: this.isOpen,\n running: this.isRunning,\n };\n }\n\n get isOpen() {\n return this._open;\n }\n\n get isRunning() {\n return this._running;\n }\n\n async open(): Promise<void> {\n if (!this._open) {\n log('opening...');\n await this._onOpen();\n this._open = true;\n\n await this.start();\n log('opened');\n }\n }\n\n async close(): Promise<void> {\n if (this._open) {\n log('closing...');\n await this.stop();\n\n await this._onClose();\n this._open = false;\n log('closed');\n }\n }\n\n async start(): Promise<void> {\n invariant(this._open);\n if (!this._running) {\n this._running = true;\n }\n }\n\n async stop(): Promise<void> {\n invariant(this._open);\n if (this._running) {\n this._running = false;\n this._stopTrigger.wake();\n }\n }\n\n //\n // AsyncIterable\n //\n\n [Symbol.asyncIterator]() {\n return this._generator();\n }\n\n async *_generator() {\n log('started');\n while (this._running) {\n // https://github.com/nodejs/node/issues/17469\n const block = await safeRace([this._stopTrigger.wait(), this._nextBlock()]);\n\n if (block === undefined) {\n break;\n }\n\n yield block;\n }\n\n log('stopped');\n }\n\n abstract _onOpen(): Promise<void>;\n abstract _onClose(): Promise<void>;\n abstract _nextBlock(): Promise<FeedBlock<T> | undefined>;\n}\n\n/**\n * Iterator that reads blocks from a single feed.\n */\nexport class FeedIterator<T extends {}> extends AbstractFeedIterator<T> {\n private readonly _queue: FeedQueue<T>;\n\n constructor(private readonly _feed: FeedWrapper<T>) {\n super();\n this._queue = new FeedQueue<T>(this._feed);\n }\n\n override async _onOpen(): Promise<void> {\n await this._queue.open();\n }\n\n override async _onClose(): Promise<void> {\n await this._queue.close();\n }\n\n override async _nextBlock(): Promise<FeedBlock<T> | undefined> {\n return this._queue.pop();\n }\n}\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { inspect } from 'node:util';\nimport { Writable } from 'streamx';\n\nimport { Event, Trigger, latch } from '@dxos/async';\nimport { inspectObject } from '@dxos/debug';\nimport type { ReadStreamOptions } from '@dxos/hypercore';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\nexport const defaultReadStreamOptions: ReadStreamOptions = {\n live: true, // Keep reading until closed.\n batch: 1024, // Read in batches.\n};\n\nexport type FeedQueueOptions = {};\n\n/**\n * Async queue using an AsyncIterator created from a hypercore.\n */\nexport class FeedQueue<T extends {}> {\n public updated = new Event<FeedQueue<T>>();\n\n private readonly _messageTrigger = new Trigger<FeedBlock<T>>({\n autoReset: true,\n });\n\n private _feedConsumer?: Writable = undefined;\n private _next?: () => void;\n private _currentBlock?: FeedBlock<T> = undefined;\n private _index = -1;\n\n // prettier-ignore\n constructor(\n private readonly _feed: FeedWrapper<T>,\n private readonly _options: FeedQueueOptions = {},\n ) {}\n\n [inspect.custom](): string {\n return inspectObject(this);\n }\n\n toJSON() {\n return {\n feedKey: this._feed.key,\n index: this.index,\n length: this.length,\n open: this.isOpen,\n };\n }\n\n get feed() {\n return this._feed;\n }\n\n get isOpen(): boolean {\n return Boolean(this._feedConsumer);\n }\n\n get length(): number {\n return this._feed.properties.length;\n }\n\n /**\n * Index (seq) of the NEXT block to be read, or -1 if not open.\n */\n get index() {\n return this._index;\n }\n\n /**\n * Opens (or reopens) the queue.\n * @param options.start Starting index. First mutation to be read would have `seq == options.start`.\n */\n async open(options: ReadStreamOptions = {}): Promise<void> {\n if (this.isOpen) {\n // TODO(burdon): Warn if re-opening (e.g., with different starting point).\n return;\n }\n\n this._index = options.start ?? 0;\n // if (this._index !== 0) {\n // console.warn('Start index not yet supported.');\n // }\n\n log('opening', { feedKey: this._feed.key });\n\n // TODO(burdon): Open with starting range.\n const opts = Object.assign({}, defaultReadStreamOptions, options);\n const feedStream = this._feed.createReadableStream(opts);\n\n this._feedConsumer = new Writable({\n write: (data: any, next: () => void) => {\n this._next = () => {\n this._next = undefined;\n this._currentBlock = undefined;\n this._index++;\n next();\n };\n\n this._currentBlock = {\n feedKey: this._feed.key,\n seq: this._index,\n data,\n };\n\n this._messageTrigger.wake(this._currentBlock);\n this.updated.emit(this);\n },\n });\n\n const onClose = () => {\n this.feed.core.off('close', onClose);\n this._feedConsumer?.off('close', onClose);\n this._feedConsumer?.off('error', onError);\n\n this._destroyConsumer();\n };\n\n const onError = (err?: Error) => {\n if (!this.isOpen) {\n return;\n }\n if (!err) {\n return;\n }\n if (err.message === 'Writable stream closed prematurely' || err.message === 'Feed is closed') {\n return;\n }\n\n log.catch(err, { feedKey: this._feed.key });\n };\n\n // Called if feed is closed externally.\n this._feed.core.once('close', onClose);\n this._feedConsumer.on('error', onError);\n\n // Called when queue is closed. Throws exception if waiting for `pop`.\n this._feedConsumer.once('close', onClose);\n\n // Pipe readable stream into writable consumer.\n feedStream.pipe(this._feedConsumer, (err) => {\n if (err) {\n onError(err);\n }\n this._destroyConsumer();\n });\n\n log('opened');\n }\n\n /**\n * Closes the queue.\n */\n async close(): Promise<void> {\n if (this.isOpen) {\n invariant(this._feedConsumer);\n invariant(!this._feed.properties.closed);\n\n log('closing', { feedKey: this._feed.key });\n const [closed, setClosed] = latch();\n this._feedConsumer.once('close', setClosed);\n this._feedConsumer.destroy();\n this._next?.(); // Release any message currently in the queue (otherwise destroy will block).\n await closed();\n log('closed');\n }\n }\n\n /**\n * Get the block at the head of the queue without removing it.\n */\n peek(): FeedBlock<T> | undefined {\n return this._currentBlock;\n }\n\n /**\n * Pop block at the head of the queue.\n */\n async pop(): Promise<FeedBlock<T>> {\n if (!this.isOpen) {\n throw new Error(`Queue closed: ${this.feed.key.truncate()}`);\n }\n\n let block = this.peek();\n if (!block) {\n block = await this._messageTrigger.wait();\n }\n\n if (block) {\n this._next?.();\n }\n\n return block;\n }\n\n private _destroyConsumer(): void {\n if (this._feedConsumer) {\n log('queue closed', { feedKey: this._feed.key });\n this._feedConsumer = undefined;\n this._next = undefined;\n this._currentBlock = undefined;\n this._index = -1;\n }\n }\n}\n", "//\n// Copyright 2020 DXOS.org\n//\n\nimport { inspect } from 'node:util';\n\nimport { Event, SubscriptionList, Trigger } from '@dxos/async';\nimport { inspectObject } from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\nimport { PublicKey } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { ComplexMap, isNonNullable } from '@dxos/util';\n\nimport { AbstractFeedIterator } from './feed-iterator';\nimport { FeedQueue } from './feed-queue';\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\n/**\n * Select next block.\n */\nexport type FeedBlockSelector<T> = (blocks: FeedBlock<T>[]) => number | undefined;\n\nexport type FeedIndex = {\n feedKey: PublicKey;\n index: number;\n};\n\nexport type FeedSetIteratorOptions = {\n // TODO(burdon): Should we remove this and assume the feeds are positioned before adding?\n start?: FeedIndex[];\n stallTimeout?: number;\n};\n\nexport const defaultFeedSetIteratorOptions = {\n stallTimeout: 1000,\n};\n\n/**\n * Iterator that reads blocks from multiple feeds, ordering them based on a traversal callback.\n */\nexport class FeedSetIterator<T extends {}> extends AbstractFeedIterator<T> {\n private readonly _feedQueues = new ComplexMap<PublicKey, FeedQueue<T>>(PublicKey.hash);\n\n private readonly _trigger = new Trigger({ autoReset: true });\n private readonly _subscriptions = new SubscriptionList();\n\n public readonly stalled = new Event<FeedSetIterator<T>>();\n\n constructor(\n private readonly _selector: FeedBlockSelector<T>,\n public readonly options: FeedSetIteratorOptions = defaultFeedSetIteratorOptions,\n ) {\n super();\n invariant(_selector);\n invariant(options);\n }\n\n [inspect.custom](): string {\n return inspectObject(this);\n }\n\n override toJSON(): { open: boolean; running: boolean; indexes: FeedIndex[] } {\n return {\n open: this.isOpen,\n running: this.isRunning,\n indexes: this.indexes,\n };\n }\n\n get size() {\n return this._feedQueues.size;\n }\n\n get feeds(): FeedWrapper<T>[] {\n return Array.from(this._feedQueues.values()).map((feedQueue) => feedQueue.feed);\n }\n\n get indexes(): FeedIndex[] {\n return Array.from(this._feedQueues.values()).map((feedQueue) => ({\n feedKey: feedQueue.feed.key,\n index: feedQueue.index,\n }));\n }\n\n reiterateBlock(block: FeedBlock<T>): void {\n this._trigger.wake();\n }\n\n async addFeed(feed: FeedWrapper<T>): Promise<void> {\n invariant(!this._feedQueues.has(feed.key), `Feed already added: ${feed.key}`);\n invariant(feed.properties.opened);\n log('feed added', { feedKey: feed.key });\n\n // Create queue and listen for updates.\n const queue = new FeedQueue<T>(feed);\n this._feedQueues.set(feed.key, queue);\n this._subscriptions.add(\n queue.updated.on(() => {\n this._trigger.wake();\n }),\n );\n\n await queue.open({\n start: this.options.start?.find((index) => index.feedKey.equals(feed.key))?.index,\n });\n\n // Wake when feed added or queue updated.\n this._trigger.wake();\n }\n\n hasFeed(feedKey: PublicKey): boolean {\n return this._feedQueues.has(feedKey);\n }\n\n override async _onOpen(): Promise<void> {\n for (const queue of this._feedQueues.values()) {\n await queue.open();\n }\n }\n\n override async _onClose(): Promise<void> {\n this._subscriptions.clear();\n await Promise.all(Array.from(this._feedQueues.values()).map((queue) => queue.close()));\n\n // Wake when feed added or queue updated.\n this._trigger.wake();\n }\n\n /**\n * Gets the next block from the selected queue.\n */\n override async _nextBlock(): Promise<FeedBlock<T> | undefined> {\n let t: NodeJS.Timeout | undefined;\n\n while (this._running) {\n // Get blocks from the head of each queue.\n const queues = Array.from(this._feedQueues.values());\n const blocks = queues.map((queue) => queue.peek()).filter(isNonNullable);\n if (blocks.length) {\n // Get the selected block from candidates.\n const idx = this._selector(blocks);\n log('selected', { idx, blocks });\n if (idx === undefined) {\n // Timeout if all candidates are rejected.\n if (t === undefined) {\n t = setTimeout(() => {\n this.stalled.emit(this);\n this._trigger.wake();\n }, this.options.stallTimeout);\n }\n } else {\n if (t !== undefined) {\n clearTimeout(t);\n t = undefined;\n }\n if (idx >= blocks.length) {\n throw new Error(`Index out of bounds: ${idx} of ${blocks.length}`);\n }\n\n // Pop from queue.\n const queue = this._feedQueues.get(blocks[idx].feedKey)!;\n log('popping', queue.toJSON());\n try {\n const message = await queue.pop();\n invariant(message === blocks[idx]);\n return message;\n } catch (err) {\n // TODO(burdon): Same queue closed twice.\n log.warn('queue closed', { feedKey: queue.feed.key });\n // console.log(Array.from(this._feedQueues.values()));\n }\n }\n }\n\n // Wait until feed added, new block, or closing.\n await this._trigger.wait();\n }\n }\n}\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { type PublicKey } from '@dxos/keys';\n\nexport type WriteReceipt = {\n feedKey: PublicKey;\n seq: number;\n};\n\nexport type WriteOptions = {\n /**\n * Called after the write is complete.\n * Runs and completes before the mutation is read from the pipeline.\n */\n afterWrite?: (receipt: WriteReceipt) => Promise<void>;\n};\n\nexport interface FeedWriter<T extends {}> {\n /**\n * Write data to the feed.\n * Awaits `afterWrite` before returning.\n */\n write(data: T, options?: WriteOptions): Promise<WriteReceipt>;\n}\n\nexport const createFeedWriter = <T extends {}>(cb: (data: T) => Promise<WriteReceipt>): FeedWriter<T> => ({\n write: async (data: T) => {\n return cb(data);\n },\n});\n\nexport const writeMessages = async <T extends {}>(writer: FeedWriter<T>, messages: T[]): Promise<WriteReceipt[]> => {\n const receipts: WriteReceipt[] = [];\n // NOTE: Write messages sequentially.\n for (const message of messages) {\n receipts.push(await writer.write(message));\n }\n return receipts;\n};\n"],
4
+ "sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport safeRace from 'race-as-promised';\n\nimport { Trigger } from '@dxos/async';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nimport { FeedQueue } from './feed-queue';\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\n/**\n * Base class for an async iterable feed.\n */\nexport abstract class AbstractFeedIterator<T> implements AsyncIterable<FeedBlock<T>> {\n private readonly _stopTrigger = new Trigger();\n\n protected _open = false;\n protected _running = false;\n\n toJSON(): { open: boolean; running: boolean } {\n return {\n open: this.isOpen,\n running: this.isRunning,\n };\n }\n\n get isOpen() {\n return this._open;\n }\n\n get isRunning() {\n return this._running;\n }\n\n async open(): Promise<void> {\n if (!this._open) {\n log('opening...');\n await this._onOpen();\n this._open = true;\n\n await this.start();\n log('opened');\n }\n }\n\n async close(): Promise<void> {\n if (this._open) {\n log('closing...');\n await this.stop();\n\n await this._onClose();\n this._open = false;\n log('closed');\n }\n }\n\n async start(): Promise<void> {\n invariant(this._open);\n if (!this._running) {\n this._running = true;\n }\n }\n\n async stop(): Promise<void> {\n invariant(this._open);\n if (this._running) {\n this._running = false;\n this._stopTrigger.wake();\n }\n }\n\n //\n // AsyncIterable\n //\n\n [Symbol.asyncIterator]() {\n return this._generator();\n }\n\n async *_generator() {\n log('started');\n while (this._running) {\n // https://github.com/nodejs/node/issues/17469\n const block = await safeRace([this._stopTrigger.wait(), this._nextBlock()]);\n\n if (block === undefined) {\n break;\n }\n\n yield block;\n }\n\n log('stopped');\n }\n\n abstract _onOpen(): Promise<void>;\n abstract _onClose(): Promise<void>;\n abstract _nextBlock(): Promise<FeedBlock<T> | undefined>;\n}\n\n/**\n * Iterator that reads blocks from a single feed.\n */\nexport class FeedIterator<T extends {}> extends AbstractFeedIterator<T> {\n private readonly _queue: FeedQueue<T>;\n\n constructor(private readonly _feed: FeedWrapper<T>) {\n super();\n this._queue = new FeedQueue<T>(this._feed);\n }\n\n override async _onOpen(): Promise<void> {\n await this._queue.open();\n }\n\n override async _onClose(): Promise<void> {\n await this._queue.close();\n }\n\n override async _nextBlock(): Promise<FeedBlock<T> | undefined> {\n return this._queue.pop();\n }\n}\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { inspect } from 'node:util';\nimport { Writable } from 'streamx';\n\nimport { Event, Trigger, latch } from '@dxos/async';\nimport { inspectObject } from '@dxos/debug';\nimport { type ReadStreamOptions } from '@dxos/hypercore';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\nexport const defaultReadStreamOptions: ReadStreamOptions = {\n live: true, // Keep reading until closed.\n batch: 1024, // Read in batches.\n};\n\nexport type FeedQueueOptions = {};\n\n/**\n * Async queue using an AsyncIterator created from a hypercore.\n */\nexport class FeedQueue<T extends {}> {\n public updated = new Event<FeedQueue<T>>();\n\n private readonly _messageTrigger = new Trigger<FeedBlock<T>>({\n autoReset: true,\n });\n\n private _feedConsumer?: Writable = undefined;\n private _next?: () => void;\n private _currentBlock?: FeedBlock<T> = undefined;\n private _index = -1;\n\n // prettier-ignore\n constructor(\n private readonly _feed: FeedWrapper<T>,\n private readonly _options: FeedQueueOptions = {},\n ) {}\n\n [inspect.custom](): string {\n return inspectObject(this);\n }\n\n toJSON() {\n return {\n feedKey: this._feed.key,\n index: this.index,\n length: this.length,\n open: this.isOpen,\n };\n }\n\n get feed() {\n return this._feed;\n }\n\n get isOpen(): boolean {\n return Boolean(this._feedConsumer);\n }\n\n get length(): number {\n return this._feed.properties.length;\n }\n\n /**\n * Index (seq) of the NEXT block to be read, or -1 if not open.\n */\n get index() {\n return this._index;\n }\n\n /**\n * Opens (or reopens) the queue.\n * @param options.start Starting index. First mutation to be read would have `seq == options.start`.\n */\n async open(options: ReadStreamOptions = {}): Promise<void> {\n if (this.isOpen) {\n // TODO(burdon): Warn if re-opening (e.g., with different starting point).\n return;\n }\n\n this._index = options.start ?? 0;\n // if (this._index !== 0) {\n // console.warn('Start index not yet supported.');\n // }\n\n log('opening', { feedKey: this._feed.key });\n\n // TODO(burdon): Open with starting range.\n const opts = Object.assign({}, defaultReadStreamOptions, options);\n const feedStream = this._feed.createReadableStream(opts);\n\n this._feedConsumer = new Writable({\n write: (data: any, next: () => void) => {\n this._next = () => {\n this._next = undefined;\n this._currentBlock = undefined;\n this._index++;\n next();\n };\n\n this._currentBlock = {\n feedKey: this._feed.key,\n seq: this._index,\n data,\n };\n\n this._messageTrigger.wake(this._currentBlock);\n this.updated.emit(this);\n },\n });\n\n const onClose = () => {\n this.feed.core.off('close', onClose);\n this._feedConsumer?.off('close', onClose);\n this._feedConsumer?.off('error', onError);\n\n this._destroyConsumer();\n };\n\n const onError = (err?: Error) => {\n if (!this.isOpen) {\n return;\n }\n if (!err) {\n return;\n }\n if (err.message === 'Writable stream closed prematurely' || err.message === 'Feed is closed') {\n return;\n }\n\n log.catch(err, { feedKey: this._feed.key });\n };\n\n // Called if feed is closed externally.\n this._feed.core.once('close', onClose);\n this._feedConsumer.on('error', onError);\n\n // Called when queue is closed. Throws exception if waiting for `pop`.\n this._feedConsumer.once('close', onClose);\n\n // Pipe readable stream into writable consumer.\n feedStream.pipe(this._feedConsumer, (err) => {\n if (err) {\n onError(err);\n }\n this._destroyConsumer();\n });\n\n log('opened');\n }\n\n /**\n * Closes the queue.\n */\n async close(): Promise<void> {\n if (this.isOpen) {\n invariant(this._feedConsumer);\n invariant(!this._feed.properties.closed);\n\n log('closing', { feedKey: this._feed.key });\n const [closed, setClosed] = latch();\n this._feedConsumer.once('close', setClosed);\n this._feedConsumer.destroy();\n this._next?.(); // Release any message currently in the queue (otherwise destroy will block).\n await closed();\n log('closed');\n }\n }\n\n /**\n * Get the block at the head of the queue without removing it.\n */\n peek(): FeedBlock<T> | undefined {\n return this._currentBlock;\n }\n\n /**\n * Pop block at the head of the queue.\n */\n async pop(): Promise<FeedBlock<T>> {\n if (!this.isOpen) {\n throw new Error(`Queue closed: ${this.feed.key.truncate()}`);\n }\n\n let block = this.peek();\n if (!block) {\n block = await this._messageTrigger.wait();\n }\n\n if (block) {\n this._next?.();\n }\n\n return block;\n }\n\n private _destroyConsumer(): void {\n if (this._feedConsumer) {\n log('queue closed', { feedKey: this._feed.key });\n this._feedConsumer = undefined;\n this._next = undefined;\n this._currentBlock = undefined;\n this._index = -1;\n }\n }\n}\n", "//\n// Copyright 2020 DXOS.org\n//\n\nimport { inspect } from 'node:util';\n\nimport { Event, SubscriptionList, Trigger } from '@dxos/async';\nimport { inspectObject } from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\nimport { PublicKey } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { ComplexMap, isNonNullable } from '@dxos/util';\n\nimport { AbstractFeedIterator } from './feed-iterator';\nimport { FeedQueue } from './feed-queue';\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\n/**\n * Select next block.\n */\nexport type FeedBlockSelector<T> = (blocks: FeedBlock<T>[]) => number | undefined;\n\nexport type FeedIndex = {\n feedKey: PublicKey;\n index: number;\n};\n\nexport type FeedSetIteratorOptions = {\n // TODO(burdon): Should we remove this and assume the feeds are positioned before adding?\n start?: FeedIndex[];\n stallTimeout?: number;\n};\n\nexport const defaultFeedSetIteratorOptions = {\n stallTimeout: 1000,\n};\n\n/**\n * Iterator that reads blocks from multiple feeds, ordering them based on a traversal callback.\n */\nexport class FeedSetIterator<T extends {}> extends AbstractFeedIterator<T> {\n private readonly _feedQueues = new ComplexMap<PublicKey, FeedQueue<T>>(PublicKey.hash);\n\n private readonly _trigger = new Trigger({ autoReset: true });\n private readonly _subscriptions = new SubscriptionList();\n\n public readonly stalled = new Event<FeedSetIterator<T>>();\n\n constructor(\n private readonly _selector: FeedBlockSelector<T>,\n public readonly options: FeedSetIteratorOptions = defaultFeedSetIteratorOptions,\n ) {\n super();\n invariant(_selector);\n invariant(options);\n }\n\n [inspect.custom](): string {\n return inspectObject(this);\n }\n\n override toJSON(): { open: boolean; running: boolean; indexes: FeedIndex[] } {\n return {\n open: this.isOpen,\n running: this.isRunning,\n indexes: this.indexes,\n };\n }\n\n get size() {\n return this._feedQueues.size;\n }\n\n get feeds(): FeedWrapper<T>[] {\n return Array.from(this._feedQueues.values()).map((feedQueue) => feedQueue.feed);\n }\n\n get indexes(): FeedIndex[] {\n return Array.from(this._feedQueues.values()).map((feedQueue) => ({\n feedKey: feedQueue.feed.key,\n index: feedQueue.index,\n }));\n }\n\n reiterateBlock(block: FeedBlock<T>): void {\n this._trigger.wake();\n }\n\n async addFeed(feed: FeedWrapper<T>): Promise<void> {\n invariant(!this._feedQueues.has(feed.key), `Feed already added: ${feed.key}`);\n invariant(feed.properties.opened);\n log('feed added', { feedKey: feed.key });\n\n // Create queue and listen for updates.\n const queue = new FeedQueue<T>(feed);\n this._feedQueues.set(feed.key, queue);\n this._subscriptions.add(\n queue.updated.on(() => {\n this._trigger.wake();\n }),\n );\n\n await queue.open({\n start: this.options.start?.find((index) => index.feedKey.equals(feed.key))?.index,\n });\n\n // Wake when feed added or queue updated.\n this._trigger.wake();\n }\n\n hasFeed(feedKey: PublicKey): boolean {\n return this._feedQueues.has(feedKey);\n }\n\n override async _onOpen(): Promise<void> {\n for (const queue of this._feedQueues.values()) {\n await queue.open();\n }\n }\n\n override async _onClose(): Promise<void> {\n this._subscriptions.clear();\n await Promise.all(Array.from(this._feedQueues.values()).map((queue) => queue.close()));\n\n // Wake when feed added or queue updated.\n this._trigger.wake();\n }\n\n /**\n * Gets the next block from the selected queue.\n */\n override async _nextBlock(): Promise<FeedBlock<T> | undefined> {\n let t: NodeJS.Timeout | undefined;\n\n while (this._running) {\n // Get blocks from the head of each queue.\n const queues = Array.from(this._feedQueues.values());\n const blocks = queues.map((queue) => queue.peek()).filter(isNonNullable);\n if (blocks.length) {\n // Get the selected block from candidates.\n const idx = this._selector(blocks);\n log('selected', { idx, blocks });\n if (idx === undefined) {\n // Timeout if all candidates are rejected.\n if (t === undefined) {\n t = setTimeout(() => {\n this.stalled.emit(this);\n this._trigger.wake();\n }, this.options.stallTimeout);\n }\n } else {\n if (t !== undefined) {\n clearTimeout(t);\n t = undefined;\n }\n if (idx >= blocks.length) {\n throw new Error(`Index out of bounds: ${idx} of ${blocks.length}`);\n }\n\n // Pop from queue.\n const queue = this._feedQueues.get(blocks[idx].feedKey)!;\n log('popping', queue.toJSON());\n try {\n const message = await queue.pop();\n invariant(message === blocks[idx]);\n return message;\n } catch (err) {\n // TODO(burdon): Same queue closed twice.\n log.warn('queue closed', { feedKey: queue.feed.key });\n // console.log(Array.from(this._feedQueues.values()));\n }\n }\n }\n\n // Wait until feed added, new block, or closing.\n await this._trigger.wait();\n }\n }\n}\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { type PublicKey } from '@dxos/keys';\n\nexport type WriteReceipt = {\n feedKey: PublicKey;\n seq: number;\n};\n\nexport type WriteOptions = {\n /**\n * Called after the write is complete.\n * Runs and completes before the mutation is read from the pipeline.\n */\n afterWrite?: (receipt: WriteReceipt) => Promise<void>;\n};\n\nexport interface FeedWriter<T extends {}> {\n /**\n * Write data to the feed.\n * Awaits `afterWrite` before returning.\n */\n write(data: T, options?: WriteOptions): Promise<WriteReceipt>;\n}\n\nexport const createFeedWriter = <T extends {}>(cb: (data: T) => Promise<WriteReceipt>): FeedWriter<T> => ({\n write: async (data: T) => {\n return cb(data);\n },\n});\n\nexport const writeMessages = async <T extends {}>(writer: FeedWriter<T>, messages: T[]): Promise<WriteReceipt[]> => {\n const receipts: WriteReceipt[] = [];\n // NOTE: Write messages sequentially.\n for (const message of messages) {\n receipts.push(await writer.write(message));\n }\n return receipts;\n};\n"],
5
5
  "mappings": ";;;;;;;;AAIA,OAAOA,cAAc;AAErB,SAASC,WAAAA,gBAAe;AACxB,SAASC,aAAAA,kBAAiB;AAC1B,SAASC,OAAAA,YAAW;;;ACJpB,SAASC,eAAe;AACxB,SAASC,gBAAgB;AAEzB,SAASC,OAAOC,SAASC,aAAa;AACtC,SAASC,qBAAqB;AAE9B,SAASC,iBAAiB;AAC1B,SAASC,WAAW;AAKpB,IAAA,eAAaC;AAEXC,IAAO,2BAAA;EACP,MAAA;EAIF,OAAA;;;EAISC;EAEUC;YACfC,IAAAA,MAAW;EACb,kBAAG,IAAA,QAAA;IAEKC,WAAAA;EACAC,CAAAA;EACAC,gBAA+BC;EAC/BC;EAER,gBAAA;EACA,SAAA;;cAEmBC,OAAAA,WAAAA,CAAAA,GAAAA;AAChB,SAAA,QAAA;AAEFlB,SAAQmB,WAAkB;;EAE3B,CAAA,QAAA,MAAA,IAAA;AAEAC,WAAS,cAAA,IAAA;;WAELC;WACAC;MACAC,SAAQ,KAAKA,MAAM;M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AaC;gBACb,WAAKzC,MAAS6B;AACb,mBAAKrB,QAAQkC,KAAAA,IAAAA;AAClB,mBAAA,SAAA,KAAA;YACK,GAAA,KAAA,QAAA,YAAA;UACL;;cAEEH,MAAIC,QAAAA;AACN,yBAAA,CAAA;AACIF,gBAAAA;;AAEJ,cAAA,OAAA,OAAA,QAAA;AAEA,kBAAA,IAAA,MAAkB,wBAAA,GAAA,OAAA,OAAA,MAAA,EAAA;UAClB;AAEA,gBAAI,QAAA,KAAA,YAAA,IAAA,OAAA,GAAA,EAAA,OAAA;eACF,WAAMK,MAAU,OAAMhB,GAAAA,EAAAA,YAAS,YAAA,GAAA9B,eAAA,GAAA,KAAA,GAAA,KAAA,CAAA;cAC/BU;AACA,kBAAA,UAAOoC,MAAAA,MAAAA,IAAAA;AACP,YAAApC,WAAY,YAAA,OAAA,GAAA,GAAA,QAAA,EAAA,YAAA,YAAA,GAAAV,eAAA,GAAA,KAAA,GAAA,MAAA,GAAA,CAAA,2BAAA,EAAA,EAAA,CAAA;AACZ,mBAAA;mBACI+C,KAAK;AAA0C,YAAAnB,KAAA,KAAA,gBAAA;cACnD,SAAA,MAAA,KAAA;YACF,GAAA,EAAA,YAAA,YAAA,GAAA5B,eAAA,GAAA,KAAA,GAAA,KAAA,CAAA;UAEJ;QAEA;MACA;AAEJ,YAAA,KAAA,SAAA,KAAA;IACF;;;;;ACxJO,IAAMgD,mBAAmB,CAAeC,QAA2D;EACxGC,OAAO,OAAOC,SAAAA;AACZ,WAAOF,GAAGE,IAAAA;EACZ;AACF;AAEO,IAAMC,gBAAgB,OAAqBC,QAAuBC,aAAAA;AACvE,QAAMC,WAA2B,CAAA;AAEjC,aAAWC,WAAWF,UAAU;AAC9BC,aAASE,KAAK,MAAMJ,OAAOH,MAAMM,OAAAA,CAAAA;EACnC;AACA,SAAOD;AACT;",
6
6
  "names": ["safeRace", "Trigger", "invariant", "log", "inspect", "Writable", "Event", "Trigger", "latch", "inspectObject", "invariant", "log", "defaultReadStreamOptions", "batch", "updated", "_messageTrigger", "autoReset", "_feedConsumer", "_next", "_currentBlock", "undefined", "_index", "_options", "custom", "toJSON", "feedKey", "index", "length", "open", "isOpen", "feed", "Object", "assign", "write", "next", "seq", "data", "onError", "_destroyConsumer", "err", "catch", "once", "onClose", "feedStream", "_feed", "closed", "setClosed", "destroy", "block", "peek", "__dxlog_file", "_open", "_running", "Trigger", "toJSON", "open", "running", "isOpen", "isRunning", "_onOpen", "log", "start", "stop", "_onClose", "_stopTrigger", "_generator", "block", "_nextBlock", "undefined", "_queue", "inspect", "Event", "SubscriptionList", "Trigger", "inspectObject", "invariant", "PublicKey", "log", "ComplexMap", "isNonNullable", "__dxlog_file", "defaultFeedSetIteratorOptions", "_feedQueues", "_trigger", "autoReset", "Trigger", "_subscriptions", "stalled", "_selector", "Event", "invariant", "options", "inspect", "custom", "toJSON", "inspectObject", "open", "running", "indexes", "size", "feeds", "feedKey", "index", "feedQueue", "reiterateBlock", "addFeed", "feed", "properties", "log", "FeedQueue", "queue", "updated", "wake", "hasFeed", "_onOpen", "Promise", "_running", "queues", "Array", "blocks", "length", "idx", "t", "undefined", "emit", "stallTimeout", "message", "warn", "createFeedWriter", "cb", "write", "data", "writeMessages", "writer", "messages", "receipts", "message", "push"]
7
7
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/feed-iterator.ts", "../../../src/feed-queue.ts", "../../../src/feed-set-iterator.ts", "../../../src/feed-writer.ts"],
4
- "sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport safeRace from 'race-as-promised';\n\nimport { Trigger } from '@dxos/async';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nimport { FeedQueue } from './feed-queue';\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\n/**\n * Base class for an async iterable feed.\n */\nexport abstract class AbstractFeedIterator<T> implements AsyncIterable<FeedBlock<T>> {\n private readonly _stopTrigger = new Trigger();\n\n protected _open = false;\n protected _running = false;\n\n toJSON(): { open: boolean; running: boolean } {\n return {\n open: this.isOpen,\n running: this.isRunning,\n };\n }\n\n get isOpen() {\n return this._open;\n }\n\n get isRunning() {\n return this._running;\n }\n\n async open(): Promise<void> {\n if (!this._open) {\n log('opening...');\n await this._onOpen();\n this._open = true;\n\n await this.start();\n log('opened');\n }\n }\n\n async close(): Promise<void> {\n if (this._open) {\n log('closing...');\n await this.stop();\n\n await this._onClose();\n this._open = false;\n log('closed');\n }\n }\n\n async start(): Promise<void> {\n invariant(this._open);\n if (!this._running) {\n this._running = true;\n }\n }\n\n async stop(): Promise<void> {\n invariant(this._open);\n if (this._running) {\n this._running = false;\n this._stopTrigger.wake();\n }\n }\n\n //\n // AsyncIterable\n //\n\n [Symbol.asyncIterator]() {\n return this._generator();\n }\n\n async *_generator() {\n log('started');\n while (this._running) {\n // https://github.com/nodejs/node/issues/17469\n const block = await safeRace([this._stopTrigger.wait(), this._nextBlock()]);\n\n if (block === undefined) {\n break;\n }\n\n yield block;\n }\n\n log('stopped');\n }\n\n abstract _onOpen(): Promise<void>;\n abstract _onClose(): Promise<void>;\n abstract _nextBlock(): Promise<FeedBlock<T> | undefined>;\n}\n\n/**\n * Iterator that reads blocks from a single feed.\n */\nexport class FeedIterator<T extends {}> extends AbstractFeedIterator<T> {\n private readonly _queue: FeedQueue<T>;\n\n constructor(private readonly _feed: FeedWrapper<T>) {\n super();\n this._queue = new FeedQueue<T>(this._feed);\n }\n\n override async _onOpen(): Promise<void> {\n await this._queue.open();\n }\n\n override async _onClose(): Promise<void> {\n await this._queue.close();\n }\n\n override async _nextBlock(): Promise<FeedBlock<T> | undefined> {\n return this._queue.pop();\n }\n}\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { inspect } from 'node:util';\nimport { Writable } from 'streamx';\n\nimport { Event, Trigger, latch } from '@dxos/async';\nimport { inspectObject } from '@dxos/debug';\nimport type { ReadStreamOptions } from '@dxos/hypercore';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\nexport const defaultReadStreamOptions: ReadStreamOptions = {\n live: true, // Keep reading until closed.\n batch: 1024, // Read in batches.\n};\n\nexport type FeedQueueOptions = {};\n\n/**\n * Async queue using an AsyncIterator created from a hypercore.\n */\nexport class FeedQueue<T extends {}> {\n public updated = new Event<FeedQueue<T>>();\n\n private readonly _messageTrigger = new Trigger<FeedBlock<T>>({\n autoReset: true,\n });\n\n private _feedConsumer?: Writable = undefined;\n private _next?: () => void;\n private _currentBlock?: FeedBlock<T> = undefined;\n private _index = -1;\n\n // prettier-ignore\n constructor(\n private readonly _feed: FeedWrapper<T>,\n private readonly _options: FeedQueueOptions = {},\n ) {}\n\n [inspect.custom](): string {\n return inspectObject(this);\n }\n\n toJSON() {\n return {\n feedKey: this._feed.key,\n index: this.index,\n length: this.length,\n open: this.isOpen,\n };\n }\n\n get feed() {\n return this._feed;\n }\n\n get isOpen(): boolean {\n return Boolean(this._feedConsumer);\n }\n\n get length(): number {\n return this._feed.properties.length;\n }\n\n /**\n * Index (seq) of the NEXT block to be read, or -1 if not open.\n */\n get index() {\n return this._index;\n }\n\n /**\n * Opens (or reopens) the queue.\n * @param options.start Starting index. First mutation to be read would have `seq == options.start`.\n */\n async open(options: ReadStreamOptions = {}): Promise<void> {\n if (this.isOpen) {\n // TODO(burdon): Warn if re-opening (e.g., with different starting point).\n return;\n }\n\n this._index = options.start ?? 0;\n // if (this._index !== 0) {\n // console.warn('Start index not yet supported.');\n // }\n\n log('opening', { feedKey: this._feed.key });\n\n // TODO(burdon): Open with starting range.\n const opts = Object.assign({}, defaultReadStreamOptions, options);\n const feedStream = this._feed.createReadableStream(opts);\n\n this._feedConsumer = new Writable({\n write: (data: any, next: () => void) => {\n this._next = () => {\n this._next = undefined;\n this._currentBlock = undefined;\n this._index++;\n next();\n };\n\n this._currentBlock = {\n feedKey: this._feed.key,\n seq: this._index,\n data,\n };\n\n this._messageTrigger.wake(this._currentBlock);\n this.updated.emit(this);\n },\n });\n\n const onClose = () => {\n this.feed.core.off('close', onClose);\n this._feedConsumer?.off('close', onClose);\n this._feedConsumer?.off('error', onError);\n\n this._destroyConsumer();\n };\n\n const onError = (err?: Error) => {\n if (!this.isOpen) {\n return;\n }\n if (!err) {\n return;\n }\n if (err.message === 'Writable stream closed prematurely' || err.message === 'Feed is closed') {\n return;\n }\n\n log.catch(err, { feedKey: this._feed.key });\n };\n\n // Called if feed is closed externally.\n this._feed.core.once('close', onClose);\n this._feedConsumer.on('error', onError);\n\n // Called when queue is closed. Throws exception if waiting for `pop`.\n this._feedConsumer.once('close', onClose);\n\n // Pipe readable stream into writable consumer.\n feedStream.pipe(this._feedConsumer, (err) => {\n if (err) {\n onError(err);\n }\n this._destroyConsumer();\n });\n\n log('opened');\n }\n\n /**\n * Closes the queue.\n */\n async close(): Promise<void> {\n if (this.isOpen) {\n invariant(this._feedConsumer);\n invariant(!this._feed.properties.closed);\n\n log('closing', { feedKey: this._feed.key });\n const [closed, setClosed] = latch();\n this._feedConsumer.once('close', setClosed);\n this._feedConsumer.destroy();\n this._next?.(); // Release any message currently in the queue (otherwise destroy will block).\n await closed();\n log('closed');\n }\n }\n\n /**\n * Get the block at the head of the queue without removing it.\n */\n peek(): FeedBlock<T> | undefined {\n return this._currentBlock;\n }\n\n /**\n * Pop block at the head of the queue.\n */\n async pop(): Promise<FeedBlock<T>> {\n if (!this.isOpen) {\n throw new Error(`Queue closed: ${this.feed.key.truncate()}`);\n }\n\n let block = this.peek();\n if (!block) {\n block = await this._messageTrigger.wait();\n }\n\n if (block) {\n this._next?.();\n }\n\n return block;\n }\n\n private _destroyConsumer(): void {\n if (this._feedConsumer) {\n log('queue closed', { feedKey: this._feed.key });\n this._feedConsumer = undefined;\n this._next = undefined;\n this._currentBlock = undefined;\n this._index = -1;\n }\n }\n}\n", "//\n// Copyright 2020 DXOS.org\n//\n\nimport { inspect } from 'node:util';\n\nimport { Event, SubscriptionList, Trigger } from '@dxos/async';\nimport { inspectObject } from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\nimport { PublicKey } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { ComplexMap, isNonNullable } from '@dxos/util';\n\nimport { AbstractFeedIterator } from './feed-iterator';\nimport { FeedQueue } from './feed-queue';\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\n/**\n * Select next block.\n */\nexport type FeedBlockSelector<T> = (blocks: FeedBlock<T>[]) => number | undefined;\n\nexport type FeedIndex = {\n feedKey: PublicKey;\n index: number;\n};\n\nexport type FeedSetIteratorOptions = {\n // TODO(burdon): Should we remove this and assume the feeds are positioned before adding?\n start?: FeedIndex[];\n stallTimeout?: number;\n};\n\nexport const defaultFeedSetIteratorOptions = {\n stallTimeout: 1000,\n};\n\n/**\n * Iterator that reads blocks from multiple feeds, ordering them based on a traversal callback.\n */\nexport class FeedSetIterator<T extends {}> extends AbstractFeedIterator<T> {\n private readonly _feedQueues = new ComplexMap<PublicKey, FeedQueue<T>>(PublicKey.hash);\n\n private readonly _trigger = new Trigger({ autoReset: true });\n private readonly _subscriptions = new SubscriptionList();\n\n public readonly stalled = new Event<FeedSetIterator<T>>();\n\n constructor(\n private readonly _selector: FeedBlockSelector<T>,\n public readonly options: FeedSetIteratorOptions = defaultFeedSetIteratorOptions,\n ) {\n super();\n invariant(_selector);\n invariant(options);\n }\n\n [inspect.custom](): string {\n return inspectObject(this);\n }\n\n override toJSON(): { open: boolean; running: boolean; indexes: FeedIndex[] } {\n return {\n open: this.isOpen,\n running: this.isRunning,\n indexes: this.indexes,\n };\n }\n\n get size() {\n return this._feedQueues.size;\n }\n\n get feeds(): FeedWrapper<T>[] {\n return Array.from(this._feedQueues.values()).map((feedQueue) => feedQueue.feed);\n }\n\n get indexes(): FeedIndex[] {\n return Array.from(this._feedQueues.values()).map((feedQueue) => ({\n feedKey: feedQueue.feed.key,\n index: feedQueue.index,\n }));\n }\n\n reiterateBlock(block: FeedBlock<T>): void {\n this._trigger.wake();\n }\n\n async addFeed(feed: FeedWrapper<T>): Promise<void> {\n invariant(!this._feedQueues.has(feed.key), `Feed already added: ${feed.key}`);\n invariant(feed.properties.opened);\n log('feed added', { feedKey: feed.key });\n\n // Create queue and listen for updates.\n const queue = new FeedQueue<T>(feed);\n this._feedQueues.set(feed.key, queue);\n this._subscriptions.add(\n queue.updated.on(() => {\n this._trigger.wake();\n }),\n );\n\n await queue.open({\n start: this.options.start?.find((index) => index.feedKey.equals(feed.key))?.index,\n });\n\n // Wake when feed added or queue updated.\n this._trigger.wake();\n }\n\n hasFeed(feedKey: PublicKey): boolean {\n return this._feedQueues.has(feedKey);\n }\n\n override async _onOpen(): Promise<void> {\n for (const queue of this._feedQueues.values()) {\n await queue.open();\n }\n }\n\n override async _onClose(): Promise<void> {\n this._subscriptions.clear();\n await Promise.all(Array.from(this._feedQueues.values()).map((queue) => queue.close()));\n\n // Wake when feed added or queue updated.\n this._trigger.wake();\n }\n\n /**\n * Gets the next block from the selected queue.\n */\n override async _nextBlock(): Promise<FeedBlock<T> | undefined> {\n let t: NodeJS.Timeout | undefined;\n\n while (this._running) {\n // Get blocks from the head of each queue.\n const queues = Array.from(this._feedQueues.values());\n const blocks = queues.map((queue) => queue.peek()).filter(isNonNullable);\n if (blocks.length) {\n // Get the selected block from candidates.\n const idx = this._selector(blocks);\n log('selected', { idx, blocks });\n if (idx === undefined) {\n // Timeout if all candidates are rejected.\n if (t === undefined) {\n t = setTimeout(() => {\n this.stalled.emit(this);\n this._trigger.wake();\n }, this.options.stallTimeout);\n }\n } else {\n if (t !== undefined) {\n clearTimeout(t);\n t = undefined;\n }\n if (idx >= blocks.length) {\n throw new Error(`Index out of bounds: ${idx} of ${blocks.length}`);\n }\n\n // Pop from queue.\n const queue = this._feedQueues.get(blocks[idx].feedKey)!;\n log('popping', queue.toJSON());\n try {\n const message = await queue.pop();\n invariant(message === blocks[idx]);\n return message;\n } catch (err) {\n // TODO(burdon): Same queue closed twice.\n log.warn('queue closed', { feedKey: queue.feed.key });\n // console.log(Array.from(this._feedQueues.values()));\n }\n }\n }\n\n // Wait until feed added, new block, or closing.\n await this._trigger.wait();\n }\n }\n}\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { type PublicKey } from '@dxos/keys';\n\nexport type WriteReceipt = {\n feedKey: PublicKey;\n seq: number;\n};\n\nexport type WriteOptions = {\n /**\n * Called after the write is complete.\n * Runs and completes before the mutation is read from the pipeline.\n */\n afterWrite?: (receipt: WriteReceipt) => Promise<void>;\n};\n\nexport interface FeedWriter<T extends {}> {\n /**\n * Write data to the feed.\n * Awaits `afterWrite` before returning.\n */\n write(data: T, options?: WriteOptions): Promise<WriteReceipt>;\n}\n\nexport const createFeedWriter = <T extends {}>(cb: (data: T) => Promise<WriteReceipt>): FeedWriter<T> => ({\n write: async (data: T) => {\n return cb(data);\n },\n});\n\nexport const writeMessages = async <T extends {}>(writer: FeedWriter<T>, messages: T[]): Promise<WriteReceipt[]> => {\n const receipts: WriteReceipt[] = [];\n // NOTE: Write messages sequentially.\n for (const message of messages) {\n receipts.push(await writer.write(message));\n }\n return receipts;\n};\n"],
4
+ "sourcesContent": ["//\n// Copyright 2020 DXOS.org\n//\n\nimport safeRace from 'race-as-promised';\n\nimport { Trigger } from '@dxos/async';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nimport { FeedQueue } from './feed-queue';\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\n/**\n * Base class for an async iterable feed.\n */\nexport abstract class AbstractFeedIterator<T> implements AsyncIterable<FeedBlock<T>> {\n private readonly _stopTrigger = new Trigger();\n\n protected _open = false;\n protected _running = false;\n\n toJSON(): { open: boolean; running: boolean } {\n return {\n open: this.isOpen,\n running: this.isRunning,\n };\n }\n\n get isOpen() {\n return this._open;\n }\n\n get isRunning() {\n return this._running;\n }\n\n async open(): Promise<void> {\n if (!this._open) {\n log('opening...');\n await this._onOpen();\n this._open = true;\n\n await this.start();\n log('opened');\n }\n }\n\n async close(): Promise<void> {\n if (this._open) {\n log('closing...');\n await this.stop();\n\n await this._onClose();\n this._open = false;\n log('closed');\n }\n }\n\n async start(): Promise<void> {\n invariant(this._open);\n if (!this._running) {\n this._running = true;\n }\n }\n\n async stop(): Promise<void> {\n invariant(this._open);\n if (this._running) {\n this._running = false;\n this._stopTrigger.wake();\n }\n }\n\n //\n // AsyncIterable\n //\n\n [Symbol.asyncIterator]() {\n return this._generator();\n }\n\n async *_generator() {\n log('started');\n while (this._running) {\n // https://github.com/nodejs/node/issues/17469\n const block = await safeRace([this._stopTrigger.wait(), this._nextBlock()]);\n\n if (block === undefined) {\n break;\n }\n\n yield block;\n }\n\n log('stopped');\n }\n\n abstract _onOpen(): Promise<void>;\n abstract _onClose(): Promise<void>;\n abstract _nextBlock(): Promise<FeedBlock<T> | undefined>;\n}\n\n/**\n * Iterator that reads blocks from a single feed.\n */\nexport class FeedIterator<T extends {}> extends AbstractFeedIterator<T> {\n private readonly _queue: FeedQueue<T>;\n\n constructor(private readonly _feed: FeedWrapper<T>) {\n super();\n this._queue = new FeedQueue<T>(this._feed);\n }\n\n override async _onOpen(): Promise<void> {\n await this._queue.open();\n }\n\n override async _onClose(): Promise<void> {\n await this._queue.close();\n }\n\n override async _nextBlock(): Promise<FeedBlock<T> | undefined> {\n return this._queue.pop();\n }\n}\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { inspect } from 'node:util';\nimport { Writable } from 'streamx';\n\nimport { Event, Trigger, latch } from '@dxos/async';\nimport { inspectObject } from '@dxos/debug';\nimport { type ReadStreamOptions } from '@dxos/hypercore';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\n\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\nexport const defaultReadStreamOptions: ReadStreamOptions = {\n live: true, // Keep reading until closed.\n batch: 1024, // Read in batches.\n};\n\nexport type FeedQueueOptions = {};\n\n/**\n * Async queue using an AsyncIterator created from a hypercore.\n */\nexport class FeedQueue<T extends {}> {\n public updated = new Event<FeedQueue<T>>();\n\n private readonly _messageTrigger = new Trigger<FeedBlock<T>>({\n autoReset: true,\n });\n\n private _feedConsumer?: Writable = undefined;\n private _next?: () => void;\n private _currentBlock?: FeedBlock<T> = undefined;\n private _index = -1;\n\n // prettier-ignore\n constructor(\n private readonly _feed: FeedWrapper<T>,\n private readonly _options: FeedQueueOptions = {},\n ) {}\n\n [inspect.custom](): string {\n return inspectObject(this);\n }\n\n toJSON() {\n return {\n feedKey: this._feed.key,\n index: this.index,\n length: this.length,\n open: this.isOpen,\n };\n }\n\n get feed() {\n return this._feed;\n }\n\n get isOpen(): boolean {\n return Boolean(this._feedConsumer);\n }\n\n get length(): number {\n return this._feed.properties.length;\n }\n\n /**\n * Index (seq) of the NEXT block to be read, or -1 if not open.\n */\n get index() {\n return this._index;\n }\n\n /**\n * Opens (or reopens) the queue.\n * @param options.start Starting index. First mutation to be read would have `seq == options.start`.\n */\n async open(options: ReadStreamOptions = {}): Promise<void> {\n if (this.isOpen) {\n // TODO(burdon): Warn if re-opening (e.g., with different starting point).\n return;\n }\n\n this._index = options.start ?? 0;\n // if (this._index !== 0) {\n // console.warn('Start index not yet supported.');\n // }\n\n log('opening', { feedKey: this._feed.key });\n\n // TODO(burdon): Open with starting range.\n const opts = Object.assign({}, defaultReadStreamOptions, options);\n const feedStream = this._feed.createReadableStream(opts);\n\n this._feedConsumer = new Writable({\n write: (data: any, next: () => void) => {\n this._next = () => {\n this._next = undefined;\n this._currentBlock = undefined;\n this._index++;\n next();\n };\n\n this._currentBlock = {\n feedKey: this._feed.key,\n seq: this._index,\n data,\n };\n\n this._messageTrigger.wake(this._currentBlock);\n this.updated.emit(this);\n },\n });\n\n const onClose = () => {\n this.feed.core.off('close', onClose);\n this._feedConsumer?.off('close', onClose);\n this._feedConsumer?.off('error', onError);\n\n this._destroyConsumer();\n };\n\n const onError = (err?: Error) => {\n if (!this.isOpen) {\n return;\n }\n if (!err) {\n return;\n }\n if (err.message === 'Writable stream closed prematurely' || err.message === 'Feed is closed') {\n return;\n }\n\n log.catch(err, { feedKey: this._feed.key });\n };\n\n // Called if feed is closed externally.\n this._feed.core.once('close', onClose);\n this._feedConsumer.on('error', onError);\n\n // Called when queue is closed. Throws exception if waiting for `pop`.\n this._feedConsumer.once('close', onClose);\n\n // Pipe readable stream into writable consumer.\n feedStream.pipe(this._feedConsumer, (err) => {\n if (err) {\n onError(err);\n }\n this._destroyConsumer();\n });\n\n log('opened');\n }\n\n /**\n * Closes the queue.\n */\n async close(): Promise<void> {\n if (this.isOpen) {\n invariant(this._feedConsumer);\n invariant(!this._feed.properties.closed);\n\n log('closing', { feedKey: this._feed.key });\n const [closed, setClosed] = latch();\n this._feedConsumer.once('close', setClosed);\n this._feedConsumer.destroy();\n this._next?.(); // Release any message currently in the queue (otherwise destroy will block).\n await closed();\n log('closed');\n }\n }\n\n /**\n * Get the block at the head of the queue without removing it.\n */\n peek(): FeedBlock<T> | undefined {\n return this._currentBlock;\n }\n\n /**\n * Pop block at the head of the queue.\n */\n async pop(): Promise<FeedBlock<T>> {\n if (!this.isOpen) {\n throw new Error(`Queue closed: ${this.feed.key.truncate()}`);\n }\n\n let block = this.peek();\n if (!block) {\n block = await this._messageTrigger.wait();\n }\n\n if (block) {\n this._next?.();\n }\n\n return block;\n }\n\n private _destroyConsumer(): void {\n if (this._feedConsumer) {\n log('queue closed', { feedKey: this._feed.key });\n this._feedConsumer = undefined;\n this._next = undefined;\n this._currentBlock = undefined;\n this._index = -1;\n }\n }\n}\n", "//\n// Copyright 2020 DXOS.org\n//\n\nimport { inspect } from 'node:util';\n\nimport { Event, SubscriptionList, Trigger } from '@dxos/async';\nimport { inspectObject } from '@dxos/debug';\nimport { invariant } from '@dxos/invariant';\nimport { PublicKey } from '@dxos/keys';\nimport { log } from '@dxos/log';\nimport { ComplexMap, isNonNullable } from '@dxos/util';\n\nimport { AbstractFeedIterator } from './feed-iterator';\nimport { FeedQueue } from './feed-queue';\nimport { type FeedWrapper } from './feed-wrapper';\nimport { type FeedBlock } from './types';\n\n/**\n * Select next block.\n */\nexport type FeedBlockSelector<T> = (blocks: FeedBlock<T>[]) => number | undefined;\n\nexport type FeedIndex = {\n feedKey: PublicKey;\n index: number;\n};\n\nexport type FeedSetIteratorOptions = {\n // TODO(burdon): Should we remove this and assume the feeds are positioned before adding?\n start?: FeedIndex[];\n stallTimeout?: number;\n};\n\nexport const defaultFeedSetIteratorOptions = {\n stallTimeout: 1000,\n};\n\n/**\n * Iterator that reads blocks from multiple feeds, ordering them based on a traversal callback.\n */\nexport class FeedSetIterator<T extends {}> extends AbstractFeedIterator<T> {\n private readonly _feedQueues = new ComplexMap<PublicKey, FeedQueue<T>>(PublicKey.hash);\n\n private readonly _trigger = new Trigger({ autoReset: true });\n private readonly _subscriptions = new SubscriptionList();\n\n public readonly stalled = new Event<FeedSetIterator<T>>();\n\n constructor(\n private readonly _selector: FeedBlockSelector<T>,\n public readonly options: FeedSetIteratorOptions = defaultFeedSetIteratorOptions,\n ) {\n super();\n invariant(_selector);\n invariant(options);\n }\n\n [inspect.custom](): string {\n return inspectObject(this);\n }\n\n override toJSON(): { open: boolean; running: boolean; indexes: FeedIndex[] } {\n return {\n open: this.isOpen,\n running: this.isRunning,\n indexes: this.indexes,\n };\n }\n\n get size() {\n return this._feedQueues.size;\n }\n\n get feeds(): FeedWrapper<T>[] {\n return Array.from(this._feedQueues.values()).map((feedQueue) => feedQueue.feed);\n }\n\n get indexes(): FeedIndex[] {\n return Array.from(this._feedQueues.values()).map((feedQueue) => ({\n feedKey: feedQueue.feed.key,\n index: feedQueue.index,\n }));\n }\n\n reiterateBlock(block: FeedBlock<T>): void {\n this._trigger.wake();\n }\n\n async addFeed(feed: FeedWrapper<T>): Promise<void> {\n invariant(!this._feedQueues.has(feed.key), `Feed already added: ${feed.key}`);\n invariant(feed.properties.opened);\n log('feed added', { feedKey: feed.key });\n\n // Create queue and listen for updates.\n const queue = new FeedQueue<T>(feed);\n this._feedQueues.set(feed.key, queue);\n this._subscriptions.add(\n queue.updated.on(() => {\n this._trigger.wake();\n }),\n );\n\n await queue.open({\n start: this.options.start?.find((index) => index.feedKey.equals(feed.key))?.index,\n });\n\n // Wake when feed added or queue updated.\n this._trigger.wake();\n }\n\n hasFeed(feedKey: PublicKey): boolean {\n return this._feedQueues.has(feedKey);\n }\n\n override async _onOpen(): Promise<void> {\n for (const queue of this._feedQueues.values()) {\n await queue.open();\n }\n }\n\n override async _onClose(): Promise<void> {\n this._subscriptions.clear();\n await Promise.all(Array.from(this._feedQueues.values()).map((queue) => queue.close()));\n\n // Wake when feed added or queue updated.\n this._trigger.wake();\n }\n\n /**\n * Gets the next block from the selected queue.\n */\n override async _nextBlock(): Promise<FeedBlock<T> | undefined> {\n let t: NodeJS.Timeout | undefined;\n\n while (this._running) {\n // Get blocks from the head of each queue.\n const queues = Array.from(this._feedQueues.values());\n const blocks = queues.map((queue) => queue.peek()).filter(isNonNullable);\n if (blocks.length) {\n // Get the selected block from candidates.\n const idx = this._selector(blocks);\n log('selected', { idx, blocks });\n if (idx === undefined) {\n // Timeout if all candidates are rejected.\n if (t === undefined) {\n t = setTimeout(() => {\n this.stalled.emit(this);\n this._trigger.wake();\n }, this.options.stallTimeout);\n }\n } else {\n if (t !== undefined) {\n clearTimeout(t);\n t = undefined;\n }\n if (idx >= blocks.length) {\n throw new Error(`Index out of bounds: ${idx} of ${blocks.length}`);\n }\n\n // Pop from queue.\n const queue = this._feedQueues.get(blocks[idx].feedKey)!;\n log('popping', queue.toJSON());\n try {\n const message = await queue.pop();\n invariant(message === blocks[idx]);\n return message;\n } catch (err) {\n // TODO(burdon): Same queue closed twice.\n log.warn('queue closed', { feedKey: queue.feed.key });\n // console.log(Array.from(this._feedQueues.values()));\n }\n }\n }\n\n // Wait until feed added, new block, or closing.\n await this._trigger.wait();\n }\n }\n}\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { type PublicKey } from '@dxos/keys';\n\nexport type WriteReceipt = {\n feedKey: PublicKey;\n seq: number;\n};\n\nexport type WriteOptions = {\n /**\n * Called after the write is complete.\n * Runs and completes before the mutation is read from the pipeline.\n */\n afterWrite?: (receipt: WriteReceipt) => Promise<void>;\n};\n\nexport interface FeedWriter<T extends {}> {\n /**\n * Write data to the feed.\n * Awaits `afterWrite` before returning.\n */\n write(data: T, options?: WriteOptions): Promise<WriteReceipt>;\n}\n\nexport const createFeedWriter = <T extends {}>(cb: (data: T) => Promise<WriteReceipt>): FeedWriter<T> => ({\n write: async (data: T) => {\n return cb(data);\n },\n});\n\nexport const writeMessages = async <T extends {}>(writer: FeedWriter<T>, messages: T[]): Promise<WriteReceipt[]> => {\n const receipts: WriteReceipt[] = [];\n // NOTE: Write messages sequentially.\n for (const message of messages) {\n receipts.push(await writer.write(message));\n }\n return receipts;\n};\n"],
5
5
  "mappings": ";;;;;;;;AAIA,OAAOA,cAAc;AAErB,SAASC,WAAAA,gBAAe;AACxB,SAASC,aAAAA,kBAAiB;AAC1B,SAASC,OAAAA,YAAW;;;ACJpB,SAASC,eAAe;AACxB,SAASC,gBAAgB;AAEzB,SAASC,OAAOC,SAASC,aAAa;AACtC,SAASC,qBAAqB;AAE9B,SAASC,iBAAiB;AAC1B,SAASC,WAAW;AAKpB,IAAA,eAAaC;AAEXC,IAAO,2BAAA;EACP,MAAA;EAIF,OAAA;;;EAISC;EAEUC;YACfC,IAAAA,MAAW;EACb,kBAAG,IAAA,QAAA;IAEKC,WAAAA;EACAC,CAAAA;EACAC,gBAA+BC;EAC/BC;EAER,gBAAA;EACA,SAAA;;cAEmBC,OAAAA,WAAAA,CAAAA,GAAAA;AAChB,SAAA,QAAA;AAEFlB,SAAQmB,WAAkB;;EAE3B,CAAA,QAAA,MAAA,IAAA;AAEAC,WAAS,cAAA,IAAA;;WAELC;WACAC;MACAC,SAAQ,KAAKA,MAAM;MACnBC,OAAM,KAAKC;MACb,QAAA,KAAA;MACF,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,QAAQkC,KAAAA,IAAAA;AAClB,mBAAA,SAAA,KAAA;YACK,GAAA,KAAA,QAAA,YAAA;UACL;;cAEEH,MAAIC,QAAAA;AACN,yBAAA,CAAA;AACIF,gBAAAA;;AAEJ,cAAA,OAAA,OAAA,QAAA;AAEA,kBAAA,IAAA,MAAkB,wBAAA,GAAA,OAAA,OAAA,MAAA,EAAA;UAClB;AAEA,gBAAI,QAAA,KAAA,YAAA,IAAA,OAAA,GAAA,EAAA,OAAA;eACF,WAAMK,MAAU,OAAMhB,GAAAA,EAAAA,YAAS,YAAA,GAAA9B,eAAA,GAAA,KAAA,GAAA,KAAA,CAAA;cAC/BU;AACA,kBAAA,UAAOoC,MAAAA,MAAAA,IAAAA;AACP,YAAApC,WAAY,YAAA,OAAA,GAAA,GAAA,QAAA,EAAA,YAAA,YAAA,GAAAV,eAAA,GAAA,KAAA,GAAA,MAAA,GAAA,CAAA,2BAAA,EAAA,EAAA,CAAA;AACZ,mBAAA;mBACI+C,KAAK;AAA0C,YAAAnB,KAAA,KAAA,gBAAA;cACnD,SAAA,MAAA,KAAA;YACF,GAAA,EAAA,YAAA,YAAA,GAAA5B,eAAA,GAAA,KAAA,GAAA,KAAA,CAAA;UAEJ;QAEA;MACA;AAEJ,YAAA,KAAA,SAAA,KAAA;IACF;;;;;ACxJO,IAAMgD,mBAAmB,CAAeC,QAA2D;EACxGC,OAAO,OAAOC,SAAAA;AACZ,WAAOF,GAAGE,IAAAA;EACZ;AACF;AAEO,IAAMC,gBAAgB,OAAqBC,QAAuBC,aAAAA;AACvE,QAAMC,WAA2B,CAAA;AAEjC,aAAWC,WAAWF,UAAU;AAC9BC,aAASE,KAAK,MAAMJ,OAAOH,MAAMM,OAAAA,CAAAA;EACnC;AACA,SAAOD;AACT;",
6
6
  "names": ["safeRace", "Trigger", "invariant", "log", "inspect", "Writable", "Event", "Trigger", "latch", "inspectObject", "invariant", "log", "defaultReadStreamOptions", "batch", "updated", "_messageTrigger", "autoReset", "_feedConsumer", "_next", "_currentBlock", "undefined", "_index", "_options", "custom", "toJSON", "feedKey", "index", "length", "open", "isOpen", "feed", "Object", "assign", "write", "next", "seq", "data", "onError", "_destroyConsumer", "err", "catch", "once", "onClose", "feedStream", "_feed", "closed", "setClosed", "destroy", "block", "peek", "__dxlog_file", "_open", "_running", "Trigger", "toJSON", "open", "running", "isOpen", "isRunning", "_onOpen", "log", "start", "stop", "_onClose", "_stopTrigger", "_generator", "block", "_nextBlock", "undefined", "_queue", "inspect", "Event", "SubscriptionList", "Trigger", "inspectObject", "invariant", "PublicKey", "log", "ComplexMap", "isNonNullable", "__dxlog_file", "defaultFeedSetIteratorOptions", "_feedQueues", "_trigger", "autoReset", "Trigger", "_subscriptions", "stalled", "_selector", "Event", "invariant", "options", "inspect", "custom", "toJSON", "inspectObject", "open", "running", "indexes", "size", "feeds", "feedKey", "index", "feedQueue", "reiterateBlock", "addFeed", "feed", "properties", "log", "FeedQueue", "queue", "updated", "wake", "hasFeed", "_onOpen", "Promise", "_running", "queues", "Array", "blocks", "length", "idx", "t", "undefined", "emit", "stallTimeout", "message", "warn", "createFeedWriter", "cb", "write", "data", "writeMessages", "writer", "messages", "receipts", "message", "push"]
7
7
  }
@@ -1,6 +1,6 @@
1
1
  import { inspect } from 'node:util';
2
2
  import { Event } from '@dxos/async';
3
- import type { ReadStreamOptions } from '@dxos/hypercore';
3
+ import { type ReadStreamOptions } from '@dxos/hypercore';
4
4
  import { type FeedWrapper } from './feed-wrapper';
5
5
  import { type FeedBlock } from './types';
6
6
  export declare const defaultReadStreamOptions: ReadStreamOptions;
@@ -1 +1 @@
1
- {"version":3,"file":"feed-queue.d.ts","sourceRoot":"","sources":["../../../src/feed-queue.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,OAAO,EAAE,KAAK,EAAkB,MAAM,aAAa,CAAC;AAEpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAIz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