update
This commit is contained in:
@@ -37,4 +37,9 @@ export abstract class CoreResponse<T = any> implements types.ICoreResponse<T> {
|
|||||||
* Get response as ArrayBuffer
|
* Get response as ArrayBuffer
|
||||||
*/
|
*/
|
||||||
abstract arrayBuffer(): Promise<ArrayBuffer>;
|
abstract arrayBuffer(): Promise<ArrayBuffer>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get response as a web-style ReadableStream
|
||||||
|
*/
|
||||||
|
abstract stream(): ReadableStream<Uint8Array> | null;
|
||||||
}
|
}
|
@@ -77,4 +77,5 @@ export interface ICoreResponse<T = any> {
|
|||||||
json(): Promise<T>;
|
json(): Promise<T>;
|
||||||
text(): Promise<string>;
|
text(): Promise<string>;
|
||||||
arrayBuffer(): Promise<ArrayBuffer>;
|
arrayBuffer(): Promise<ArrayBuffer>;
|
||||||
|
stream(): ReadableStream<Uint8Array> | null; // Always returns web-style stream
|
||||||
}
|
}
|
@@ -69,6 +69,13 @@ export class CoreResponse<T = any> extends AbstractCoreResponse<T> implements ty
|
|||||||
return this.response.body;
|
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
|
* Get the raw Response object
|
||||||
*/
|
*/
|
||||||
|
@@ -7,8 +7,8 @@ export * from '../core_base/types.js';
|
|||||||
* Fetch-specific response extensions
|
* Fetch-specific response extensions
|
||||||
*/
|
*/
|
||||||
export interface IFetchResponse<T = any> extends baseTypes.ICoreResponse<T> {
|
export interface IFetchResponse<T = any> extends baseTypes.ICoreResponse<T> {
|
||||||
// Fetch-specific methods
|
// Node.js stream method that throws in browser
|
||||||
stream(): ReadableStream<Uint8Array> | null;
|
streamNode(): never;
|
||||||
|
|
||||||
// Access to raw Response object
|
// Access to raw Response object
|
||||||
raw(): Response;
|
raw(): Response;
|
||||||
|
@@ -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();
|
this.ensureNotConsumed();
|
||||||
return this.incomingMessage;
|
return this.incomingMessage;
|
||||||
}
|
}
|
||||||
|
@@ -16,7 +16,7 @@ export interface IExtendedIncomingMessage<T = any> extends plugins.http.Incoming
|
|||||||
*/
|
*/
|
||||||
export interface INodeResponse<T = any> extends baseTypes.ICoreResponse<T> {
|
export interface INodeResponse<T = any> extends baseTypes.ICoreResponse<T> {
|
||||||
// Node.js specific methods
|
// Node.js specific methods
|
||||||
stream(): NodeJS.ReadableStream;
|
streamNode(): NodeJS.ReadableStream; // Returns Node.js style stream
|
||||||
|
|
||||||
// Legacy compatibility
|
// Legacy compatibility
|
||||||
raw(): plugins.http.IncomingMessage;
|
raw(): plugins.http.IncomingMessage;
|
||||||
|
Reference in New Issue
Block a user