File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ import http from "node:http" ;
2+ import stream from "node:stream/promises" ;
3+ import { Request } from "../Request.js" ;
4+ import { Response } from "./Response.js" ;
5+
6+ /**
7+ * A response that streams data from a readable stream.
8+ */
9+ export class StreamedResponse < A > extends Response < A > {
10+ private readonly stream : NodeJS . ReadableStream ;
11+
12+ /**
13+ * Construct a StreamedResponse.
14+ * @param stream The readable stream to send in the response body.
15+ * @param [statusCode=200] The HTTP response status code to send.
16+ * @param [headers] The HTTP response headers to send.
17+ */
18+ public constructor ( stream : NodeJS . ReadableStream , statusCode = 200 , headers ?: HeadersInit ) {
19+ super ( statusCode , headers ) ;
20+ this . stream = stream ;
21+ if ( ! this . headers . has ( "transfer-encoding" ) )
22+ this . headers . set ( "transfer-encoding" , "chunked" ) ;
23+ }
24+
25+ protected override async send ( res : http . ServerResponse , req ?: Request < A > ) : Promise < void > {
26+ this . writeHead ( res , req ) ;
27+ await stream . pipeline ( this . stream , res ) ;
28+ }
29+ }
Original file line number Diff line number Diff line change @@ -3,5 +3,6 @@ export * from "./BufferResponse.js";
33export * from "./EmptyResponse.js" ;
44export * from "./JsonResponse.js" ;
55export * from "./Response.js" ;
6+ export * from "./StreamedResponse.js" ;
67export * from "./TextResponse.js" ;
78export * from "./ThrowableResponse.js" ;
You can’t perform that action at this time.
0 commit comments