Skip to content

Commit

Permalink
Merge pull request #90 from shgysk8zer0/feature/streams
Browse files Browse the repository at this point in the history
Add `ReadableStream.from()`
  • Loading branch information
shgysk8zer0 authored Jan 22, 2025
2 parents 6281381 + db03835 commit 34a6ccc
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v0.4.9] - 2025-01-22

### Added
- Add `ReadableStream.from()` polyfill

## [v0.4.8] - 2024-12-20

### Added
Expand Down
3 changes: 2 additions & 1 deletion browser.js
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';
Expand All @@ -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';
Expand Down
1 change: 1 addition & 0 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import './blob.js';
import './errors.js';
import './function.js';
import './iterator.js';
import './streams.js';
import './map.js';
import './math.js';
import './object.js';
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shgysk8zer0/polyfills",
"version": "0.4.8",
"version": "0.4.9",
"private": false,
"type": "module",
"description": "A collection of JavaScript polyfills",
Expand Down
35 changes: 35 additions & 0 deletions streams.js
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.');
}
});
13 changes: 13 additions & 0 deletions streams.test.js
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.');
});
});

0 comments on commit 34a6ccc

Please sign in to comment.