(
responseStream: Stream.Writable,
standardResponse: StandardResponse,
options: SendStandardResponseOptions = {},
)
| 7 | export interface SendStandardResponseOptions extends ToLambdaBodyOptions {} |
| 8 | |
| 9 | export function sendStandardResponse( |
| 10 | responseStream: Stream.Writable, |
| 11 | standardResponse: StandardResponse, |
| 12 | options: SendStandardResponseOptions = {}, |
| 13 | ): Promise<void> { |
| 14 | return new Promise((resolve, reject) => { |
| 15 | responseStream.once('error', reject) |
| 16 | responseStream.once('close', resolve) |
| 17 | |
| 18 | const [body, standardHeaders] = toLambdaBody(standardResponse.body, standardResponse.headers, options) |
| 19 | const [headers, setCookies] = toLambdaHeaders(standardHeaders) |
| 20 | |
| 21 | // awslambda is global aws lambda global object |
| 22 | ;(globalThis as any).awslambda.HttpResponseStream.from(responseStream, { |
| 23 | statusCode: standardResponse.status, |
| 24 | headers, |
| 25 | cookies: setCookies, |
| 26 | }) |
| 27 | |
| 28 | if (body === undefined) { |
| 29 | responseStream.end() |
| 30 | } |
| 31 | else if (typeof body === 'string') { |
| 32 | responseStream.write(body) |
| 33 | responseStream.end() // awslambda will throw if we call .end with args |
| 34 | } |
| 35 | else { |
| 36 | responseStream.once('close', () => { |
| 37 | if (!body.closed) { |
| 38 | body.destroy(responseStream.errored ?? undefined) |
| 39 | } |
| 40 | }) |
| 41 | |
| 42 | body.once('error', error => responseStream.destroy(error)) |
| 43 | |
| 44 | body.pipe(responseStream) |
| 45 | } |
| 46 | }) |
| 47 | } |
no test coverage detected