Skip to content

Commit c55658b

Browse files
authored
🤖 Merge PR DefinitelyTyped#72417 feat(aws-lambda): add response streaming types by @zirkelc
1 parent 98adc91 commit c55658b

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

‎types/aws-lambda/aws-lambda-tests.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,20 @@ const mixedUntypedCallbackTypedHandler: CustomHandler = (
201201
context: AWSLambda.Context,
202202
cb: AWSLambda.Callback,
203203
) => {};
204+
205+
// Test streamifyResponse
206+
const streamifyResponseHandler: AWSLambda.StreamifyHandler = (event, responseStream, context) => {
207+
const metadata = {
208+
statusCode: 200,
209+
headers: {
210+
"Content-Type": "application/json",
211+
"CustomHeader": "outerspace",
212+
},
213+
};
214+
responseStream = awslambda.HttpResponseStream.from(responseStream, metadata);
215+
responseStream.setContentType("text/plain");
216+
responseStream.write("Hello, world!");
217+
responseStream.end();
218+
};
219+
220+
awslambda.streamifyResponse(streamifyResponseHandler);

‎types/aws-lambda/handler.d.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Writable } from "node:stream";
12
/**
23
* The interface that AWS Lambda will invoke your handler with.
34
* There are more specialized types for many cases where AWS services
@@ -170,3 +171,97 @@ export interface ClientContextEnv {
170171
* Pass `null` or `undefined` for the `error` parameter to use this parameter.
171172
*/
172173
export type Callback<TResult = any> = (error?: Error | string | null, result?: TResult) => void;
174+
175+
/**
176+
* Interface for using response streaming from AWS Lambda.
177+
* To indicate to the runtime that Lambda should stream your function’s responses, you must wrap your function handler with the `awslambda.streamifyResponse()` decorator.
178+
*
179+
* The `streamifyResponse` decorator accepts the following additional parameter, `responseStream`, besides the default node handler parameters, `event`, and `context`.
180+
* The new `responseStream` object provides a stream object that your function can write data to. Data written to this stream is sent immediately to the client. You can optionally set the Content-Type header of the response to pass additional metadata to your client about the contents of the stream.
181+
*
182+
* {@link https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/ AWS blog post}
183+
* {@link https://docs.aws.amazon.com/lambda/latest/dg/config-rs-write-functions.html AWS documentation}
184+
*
185+
* @example <caption>Writing to the response stream</caption>
186+
* import 'aws-lambda';
187+
*
188+
* export const handler = awslambda.streamifyResponse(
189+
* async (event, responseStream, context) => {
190+
* responseStream.setContentType("text/plain");
191+
* responseStream.write("Hello, world!");
192+
* responseStream.end();
193+
* }
194+
* );
195+
*
196+
* @example <caption>Using pipeline</caption>
197+
* import 'aws-lambda';
198+
* import { Readable } from 'stream';
199+
* import { pipeline } from 'stream/promises';
200+
* import zlib from 'zlib';
201+
*
202+
* export const handler = awslambda.streamifyResponse(
203+
* async (event, responseStream, context) => {
204+
* // As an example, convert event to a readable stream.
205+
* const requestStream = Readable.from(Buffer.from(JSON.stringify(event)));
206+
*
207+
* await pipeline(requestStream, zlib.createGzip(), responseStream);
208+
* }
209+
* );
210+
*/
211+
export type StreamifyHandler<TEvent = any, TResult = any> = (
212+
event: TEvent,
213+
responseStream: awslambda.HttpResponseStream,
214+
context: Context,
215+
) => TResult | Promise<TResult>;
216+
217+
declare global {
218+
namespace awslambda {
219+
class HttpResponseStream extends Writable {
220+
static from(
221+
writable: Writable,
222+
metadata: Record<string, unknown>,
223+
): HttpResponseStream;
224+
setContentType: (contentType: string) => void;
225+
}
226+
227+
/**
228+
* Decorator for using response streaming from AWS Lambda.
229+
* To indicate to the runtime that Lambda should stream your function’s responses, you must wrap your function handler with the `awslambda.streamifyResponse()` decorator.
230+
*
231+
* The `streamifyResponse` decorator accepts the following additional parameter, `responseStream`, besides the default node handler parameters, `event`, and `context`.
232+
* The new `responseStream` object provides a stream object that your function can write data to. Data written to this stream is sent immediately to the client. You can optionally set the Content-Type header of the response to pass additional metadata to your client about the contents of the stream.
233+
*
234+
* {@link https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/ AWS blog post}
235+
* {@link https://docs.aws.amazon.com/lambda/latest/dg/config-rs-write-functions.html AWS documentation}
236+
*
237+
* @example <caption>Writing to the response stream</caption>
238+
* import 'aws-lambda';
239+
*
240+
* export const handler = awslambda.streamifyResponse(
241+
* async (event, responseStream, context) => {
242+
* responseStream.setContentType("text/plain");
243+
* responseStream.write("Hello, world!");
244+
* responseStream.end();
245+
* }
246+
* );
247+
*
248+
* @example <caption>Using pipeline</caption>
249+
* import 'aws-lambda';
250+
* import { Readable } from 'stream';
251+
* import { pipeline } from 'stream/promises';
252+
* import zlib from 'zlib';
253+
*
254+
* export const handler = awslambda.streamifyResponse(
255+
* async (event, responseStream, context) => {
256+
* // As an example, convert event to a readable stream.
257+
* const requestStream = Readable.from(Buffer.from(JSON.stringify(event)));
258+
*
259+
* await pipeline(requestStream, zlib.createGzip(), responseStream);
260+
* }
261+
* );
262+
*/
263+
function streamifyResponse<TEvent = any, TResult = void>(
264+
handler: StreamifyHandler<TEvent, TResult>,
265+
): StreamifyHandler<TEvent, TResult>;
266+
}
267+
}

0 commit comments

Comments
 (0)