-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix browser support #122
Fix browser support #122
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const ponyfill = {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sindresorhus Didn't you coin that term? Happy to spread it! 🦄 |
||
|
||
const {prototype} = ReadableStream; | ||
|
||
// Use this library as a ponyfill instead of a polyfill. | ||
// I.e. avoid modifying global variables. | ||
// We can remove this once https://github.com/Sec-ant/readable-stream/issues/2 is fixed | ||
if (prototype[Symbol.asyncIterator] === undefined && prototype.values === undefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This condition is always |
||
await import('@sec-ant/readable-stream'); | ||
ponyfill.asyncIterator = prototype[Symbol.asyncIterator]; | ||
delete prototype[Symbol.asyncIterator]; | ||
delete prototype.values; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not polyfilling ensures we don't get issues for users that use the "official" web streams polyfill. Also, this avoids any global side effects. The author of the polyfill is working on turning it into a ponyfill. When that happens, we can remove this file entirely. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
import {Duplex, Readable} from 'node:stream'; | ||
import {finished} from 'node:stream/promises'; | ||
|
||
export const createStream = streamDef => typeof streamDef === 'function' | ||
? Duplex.from(streamDef) | ||
: Readable.from(streamDef); | ||
|
||
// @todo Use ReadableStream.from() after dropping support for Node 18 | ||
export const readableStreamFrom = chunks => new ReadableStream({ | ||
start(controller) { | ||
for (const chunk of chunks) { | ||
controller.enqueue(chunk); | ||
} | ||
|
||
controller.close(); | ||
}, | ||
}); | ||
|
||
// Tests related to big buffers/strings can be slow. We run them serially and | ||
// with a higher timeout to ensure they do not randomly fail. | ||
export const BIG_TEST_DURATION = '2m'; | ||
|
||
export const onFinishedStream = stream => finished(stream, {cleanup: true}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import test from 'ava'; | ||
|
||
// Emulate browsers that do not support those methods | ||
delete ReadableStream.prototype.values; | ||
delete ReadableStream.prototype[Symbol.asyncIterator]; | ||
|
||
// Run those tests, but emulating browsers | ||
await import('./web-stream.js'); | ||
|
||
test('Should not polyfill ReadableStream', t => { | ||
t.is(ReadableStream.prototype.values, undefined); | ||
t.is(ReadableStream.prototype[Symbol.asyncIterator], undefined); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import test from 'ava'; | ||
import getStream from '../source/index.js'; | ||
import {fixtureString, fixtureMultiString} from './fixtures/index.js'; | ||
import {readableStreamFrom, onFinishedStream} from './helpers/index.js'; | ||
|
||
test('Can use ReadableStream', async t => { | ||
const stream = readableStreamFrom(fixtureMultiString); | ||
t.is(await getStream(stream), fixtureString); | ||
await onFinishedStream(stream); | ||
}); | ||
|
||
test('Can use already ended ReadableStream', async t => { | ||
const stream = readableStreamFrom(fixtureMultiString); | ||
t.is(await getStream(stream), fixtureString); | ||
t.is(await getStream(stream), ''); | ||
await onFinishedStream(stream); | ||
}); | ||
|
||
test('Can use already canceled ReadableStream', async t => { | ||
let canceledValue; | ||
const stream = new ReadableStream({ | ||
cancel(canceledError) { | ||
canceledValue = canceledError; | ||
}, | ||
}); | ||
const error = new Error('test'); | ||
await stream.cancel(error); | ||
t.is(canceledValue, error); | ||
t.is(await getStream(stream), ''); | ||
await onFinishedStream(stream); | ||
}); | ||
|
||
test('Can use already errored ReadableStream', async t => { | ||
const error = new Error('test'); | ||
const stream = new ReadableStream({ | ||
start(controller) { | ||
controller.error(error); | ||
}, | ||
}); | ||
t.is(await t.throwsAsync(getStream(stream)), error); | ||
t.is(await t.throwsAsync(onFinishedStream(stream)), error); | ||
}); | ||
|
||
test('Cancel ReadableStream when maxBuffer is hit', async t => { | ||
let canceled = false; | ||
const stream = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue(fixtureString); | ||
controller.enqueue(fixtureString); | ||
controller.close(); | ||
}, | ||
cancel() { | ||
canceled = true; | ||
}, | ||
}); | ||
const error = await t.throwsAsync( | ||
getStream(stream, {maxBuffer: 1}), | ||
{message: /maxBuffer exceeded/}, | ||
); | ||
t.deepEqual(error.bufferedData, fixtureString[0]); | ||
await onFinishedStream(stream); | ||
t.true(canceled); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dependency is quite small: ~250 lines, no dependencies.
I looked into each implementation, and its behavior is almost identical to both the Node.js implementation, and the 400KB official polyfill for web streams.