-
-
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.
Merge pull request #90 from shgysk8zer0/feature/streams
Add `ReadableStream.from()`
- Loading branch information
Showing
7 changed files
with
59 additions
and
4 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* @copyright 2023-2024 Chris Zuber <[email protected]> | ||
* @copyright 2023-2025 Chris Zuber <[email protected]> | ||
*/ | ||
import './globalThis.js'; | ||
import './string.js'; | ||
|
@@ -12,6 +12,7 @@ import './function.js'; | |
import './url.js'; | ||
import './number.js'; | ||
import './iterator.js'; | ||
import './streams.js'; | ||
import './math.js'; | ||
import './secure-context.js'; | ||
import './navigator.js'; | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,35 @@ | ||
import { polyfillMethod } from './utils.js'; | ||
|
||
polyfillMethod(ReadableStream, 'from', function from(anyIterable) { | ||
if (anyIterable[Symbol.iterator] instanceof Function) { | ||
return new ReadableStream({ | ||
start(controller) { | ||
try { | ||
for (const chunk of anyIterable) { | ||
controller.enqueue(chunk); | ||
} | ||
} catch(err) { | ||
controller.error(err); | ||
} finally { | ||
controller.close(); | ||
} | ||
} | ||
}); | ||
} else if (anyIterable[Symbol.asyncIterator] instanceof Function) { | ||
return new ReadableStream({ | ||
async start(controller) { | ||
try { | ||
for await (const chunk of anyIterable) { | ||
controller.enqueue(chunk); | ||
} | ||
} catch(err) { | ||
controller.error(err); | ||
} finally { | ||
controller.close(); | ||
} | ||
} | ||
}); | ||
} else { | ||
throw new TypeError('`ReadableStream.from()` requires an iterable source.'); | ||
} | ||
}); |
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,13 @@ | ||
import './streams.js'; | ||
import { describe, test } from 'node:test'; | ||
import { strictEqual, throws } from 'node:assert'; | ||
|
||
describe('Check iterator polyfills', () => { | ||
test('Test `ReadableStream.from()` polyfill', async () => { | ||
const text = 'Hello, World!'; | ||
const stream = ReadableStream.from(text).pipeThrough(new TextEncoderStream()); | ||
|
||
strictEqual(await new Response(stream).text(), text, 'Stream should result in the original text input.'); | ||
throws(() => ReadableStream.from(null), { name: 'TypeError' }, 'Should throw a `TypeError` for non-iterable objects.'); | ||
}); | ||
}); |