-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use same dependencies as mapeo-mobile (#10)
- Loading branch information
Showing
9 changed files
with
306 additions
and
560 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { pEventIterator } from 'p-event' | ||
|
||
/** | ||
* This is needed because the stream returned by | ||
* `Hypercore.prototype.createReadStream` is not iterable with `for await`. | ||
* Normally, Node streams don't need this wrapper. | ||
* @param {NodeJS.ReadableStream} readable | ||
* @returns {AsyncIterable<unknown>} | ||
*/ | ||
export const readableStreamToAsyncIterable = (readable) => | ||
pEventIterator(readable, 'data', { resolutionEvents: ['end'] }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import assert from 'node:assert/strict' | ||
import test from 'node:test' | ||
import { Readable } from 'node:stream' | ||
import { readableStreamToAsyncIterable } from '../../src/lib/readableStreamToAsyncIterable.js' | ||
|
||
class TestStream extends Readable { | ||
#count = 0 | ||
|
||
/** | ||
* @param {undefined | string} value | ||
* @returns {void} | ||
*/ | ||
#push(value) { | ||
this.push(value ? Buffer.from(value) : null) | ||
this.#count++ | ||
} | ||
|
||
/** | ||
* @override | ||
*/ | ||
_read() { | ||
this.#push(['foo', 'bar', 'baz'][this.#count]) | ||
} | ||
|
||
/** | ||
* @override | ||
*/ | ||
[Symbol.asyncIterator]() { | ||
// This is a hack to satisfy TypeScript, which will otherwise complain that | ||
// this method returns the wrong type. | ||
if (Date.now()) { | ||
assert.fail('Wrapper should not call this method') | ||
} | ||
return super[Symbol.asyncIterator]() | ||
} | ||
} | ||
|
||
test('returns an async iterable', async () => { | ||
const result = readableStreamToAsyncIterable(new TestStream()) | ||
|
||
const chunks = [] | ||
for await (const chunk of result) chunks.push(chunk) | ||
assert.deepEqual( | ||
chunks, | ||
['foo', 'bar', 'baz'].map((str) => Buffer.from(str)) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters