|
| 1 | +import type { Writable } from "node:stream"; |
1 | 2 | /** |
2 | 3 | * The interface that AWS Lambda will invoke your handler with. |
3 | 4 | * There are more specialized types for many cases where AWS services |
@@ -170,3 +171,97 @@ export interface ClientContextEnv { |
170 | 171 | * Pass `null` or `undefined` for the `error` parameter to use this parameter. |
171 | 172 | */ |
172 | 173 | 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