Comments (7)
Yeah, but those are still quite different from the specced behavior.
I would recommend something like this:
ReadableStream.prototype.values ??= function({ preventCancel = false } = {}) {
const reader = this.getReader();
return {
async next() {
try {
const result = await reader.read();
if (result.done) {
reader.releaseLock();
}
return result;
} catch (e) {
reader.releaseLock();
throw e;
}
},
async return(value) {
if (!preventCancel) {
const cancelPromise = reader.cancel(value);
reader.releaseLock();
await cancelPromise;
} else {
reader.releaseLock();
}
return { done: true, value };
},
[Symbol.asyncIterator]() {
return this;
}
};
};
ReadableStream.prototype[Symbol.asyncIterator] ??= ReadableStream.prototype.values;
from web-streams-polyfill.
Thanks yet again for recreating the recommendations.
from web-streams-polyfill.
You know... with this in place combined with a polyfill for ReadableStream.from(iterable)
then this would make it a grate solution for converting your polyfilled ReadableStream
instance into a native ReadableStream
instance that could be transfered with postMessage
.
import { ReadableStream as Polyfill } from 'pkg'
var native = globalThis.ReadableStream.from(new Polyfill(...))
// and it would work the same the other way around
var polyfill = Polyfill.from(new globalThis.ReadableStream(...))
it would make the web-streams-adapter a bit obsolete.
kind of solves the conversion between node:streams to web streams as well when having a polyfill for .from()
as well.
Perhaps dose not cover all the bits and pieces such as sink and BYOB reader and other stuff (?). But could get the job done for most stuff.
from web-streams-polyfill.
I should really have put that in a Gist...
Did you manage to find it again? I can't seem to find it myself. 😅
from web-streams-polyfill.
I should really have put that in a Gist...
dito
Did you manage to find it again?
nope, maybe i have to rewrite it. but i don't remember what your suggestions where to make it more spec compatible.
All i can say is that it started with ReadableStream.prototype[Symbol.asyncIterator] ??=
😅
Gona try searching my computer for it.
from web-streams-polyfill.
found this in one readme file.
import 'fast-readable-async-iterator'
// or
if (typeof ReadableStream !== 'undefined' && !ReadableStream.prototype[Symbol.asyncIterator]) {
ReadableStream.prototype[Symbol.asyncIterator] = function () {
const reader = this.getReader()
let last = reader.read()
return {
next () {
const temp = last
last = reader.read()
return temp
},
return () {
return reader.releaseLock()
},
throw (err) {
this.return()
throw err
},
[Symbol.asyncIterator] () {
return this
}
}
}
}
And also this one:
ReadableStream.prototype[Symbol.asyncIterator] ??= function () {
const reader = this.getReader()
return {
next: _ => reader.read(),
return: _ => { reader.releaseLock() },
throw: err => {
this.return()
throw err
},
[Symbol.asyncIterator] () {
return this
}
}
}
from web-streams-polyfill.
Yes, that was deliberate, see whatwg/streams#1083 (comment). 😁
I really need to find some time to work on web-streams-polyfill
again and add ReadableStream.from()
. Converting from a native ReadableStream
or a Node.js Readable
has always been painful with the polyfill (even when you use web-streams-adapter
), and from()
would help a lot.
from web-streams-polyfill.
Related Issues (20)
- Issue with dynamic import HOT 3
- The stream is not in a state that permits enqueue; new Response(readable).blob().text() resolve to [object ReadableStream] not underlying source HOT 10
- ReadableStream and File/Blob HOT 2
- Use in a streaming library for nodejs and browsers HOT 6
- dom-exception.ts HOT 6
- closing a stream followed by respond(0) can error if close is not syncronously putting stream in closed state HOT 5
- `pipeTo` doesn't use `signal.reason` as error HOT 2
- Example for polyfilling TextDecoderStream in previous browsers HOT 2
- `pipeTo` with `preventCancel: true` never settle if `readable` doesn't produce new chunks HOT 2
- Upgrading to Typescript 4.9 results in error TS2322 when using web-streams-polyfill HOT 3
- Abilities to support web streams under IE8-? HOT 3
- Question: how to detach a TypedArray. HOT 1
- Add `ReadableStream.from(asyncIterable)` HOT 1
- Q: how to best upgrade to byte stream? HOT 1
- Node version bump from 8 to 18 breaks Remix installation HOT 8
- ReadableStream typing does not match async iterator spec HOT 3
- [Question] TransformStream and Response as specified and implemented HOT 6
- Upgrade to 4.0.0 from 3.3.3 causes type issues in node HOT 3
- Could you please decrease the size of the package? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from web-streams-polyfill.