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

@@ -37,4 +37,9 @@ export abstract class CoreResponse<T = any> implements types.ICoreResponse<T> {
* Get response as ArrayBuffer
*/
abstract arrayBuffer(): Promise<ArrayBuffer>;
/**
* Get response as a web-style ReadableStream
*/
abstract stream(): ReadableStream<Uint8Array> | null;
}

View File

@@ -77,4 +77,5 @@ export interface ICoreResponse<T = any> {
json(): Promise<T>;
text(): Promise<string>;
arrayBuffer(): Promise<ArrayBuffer>;
stream(): ReadableStream<Uint8Array> | null; // Always returns web-style stream
}

View File

@@ -69,6 +69,13 @@ export class CoreResponse<T = any> extends AbstractCoreResponse<T> implements ty
return this.response.body;
}
/**
* Node.js stream method - not available in browser
*/
streamNode(): never {
throw new Error('streamNode() is not available in browser/fetch environment. Use stream() for web-style ReadableStream.');
}
/**
* Get the raw Response object
*/

View File

@@ -7,8 +7,8 @@ export * from '../core_base/types.js';
* Fetch-specific response extensions
*/
export interface IFetchResponse<T = any> extends baseTypes.ICoreResponse<T> {
// Fetch-specific methods
stream(): ReadableStream<Uint8Array> | null;
// Node.js stream method that throws in browser
streamNode(): never;
// Access to raw Response object
raw(): Response;

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;