This commit is contained in:
2025-07-28 22:50:12 +00:00
parent eb2ccd8d9f
commit 8e75047d1f
6 changed files with 53 additions and 5 deletions

View File

@@ -84,9 +84,44 @@ export class CoreResponse<T = any> extends AbstractCoreResponse<T> implements ty
}
/**
* Get response as a readable stream
* Get response as a web-style ReadableStream
*/
stream(): NodeJS.ReadableStream {
stream(): ReadableStream<Uint8Array> | null {
this.ensureNotConsumed();
// Convert Node.js stream to web stream
// In Node.js 16.5+ we can use Readable.toWeb()
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;
}

View File

@@ -16,7 +16,7 @@ export interface IExtendedIncomingMessage<T = any> extends plugins.http.Incoming
*/
export interface INodeResponse<T = any> extends baseTypes.ICoreResponse<T> {
// Node.js specific methods
stream(): NodeJS.ReadableStream;
streamNode(): NodeJS.ReadableStream; // Returns Node.js style stream
// Legacy compatibility
raw(): plugins.http.IncomingMessage;