BREAKING CHANGE(client/streaming): Unify streaming APIs: remove raw()/streamNode() and standardize on web ReadableStream across runtimes

This commit is contained in:
2025-11-17 14:18:58 +00:00
parent 1305b92ebe
commit 0cf48b3688
15 changed files with 1273 additions and 1524 deletions

View File

@@ -129,41 +129,15 @@ export class CoreResponse<T = any>
stream(): ReadableStream<Uint8Array> | null {
this.ensureNotConsumed();
// Convert Node.js stream to web stream
// In Node.js 16.5+ we can use Readable.toWeb()
// Convert Node.js stream to web stream using Readable.toWeb()
// This creates a proper Node.js-compatible web stream that works with Readable.fromWeb()
if (this.incomingMessage.readableEnded || this.incomingMessage.destroyed) {
return null;
}
// Create a web ReadableStream from the Node.js stream
const nodeStream = this.incomingMessage;
return new ReadableStream<Uint8Array>({
start(controller) {
nodeStream.on('data', (chunk) => {
controller.enqueue(new Uint8Array(chunk));
});
nodeStream.on('end', () => {
controller.close();
});
nodeStream.on('error', (err) => {
controller.error(err);
});
},
cancel() {
nodeStream.destroy();
},
});
}
/**
* Get response as a Node.js readable stream
*/
streamNode(): NodeJS.ReadableStream {
this.ensureNotConsumed();
return this.incomingMessage;
// Use Readable.toWeb() to convert Node.js stream to web stream (Node.js 16.5+)
// The returned type is automatically Node.js ReadableStream which is compatible with Readable.fromWeb()
return plugins.stream.Readable.toWeb(this.incomingMessage) as any;
}
/**