stream-mock.ts
This file
Path
runtime-tests/lambda/stream-mock.ts
43 lines · 1.3 KiB · TypeScript
Last changed by
chore: rename `runtime_tests` to `runtime-tests` (#3419)Yusuke Wadabefore adoptiondfbd717
History of this fileTake it
chore: rename `runtime_tests` to `runtime-tests` (#3419)Yusuke Wadabefore adoptiondfbd717
lines 1–43why, one step back1import { vi } from 'vitest'2import { Writable } from 'node:stream'3import type {4 APIGatewayProxyEvent,5 APIGatewayProxyEventV2,6} from '../../src/adapter/aws-lambda/handler'7import type { LambdaContext } from '../../src/adapter/aws-lambda/types'89type StreamifyResponseHandler = (10 handlerFunc: (11 event: APIGatewayProxyEvent | APIGatewayProxyEventV2,12 responseStream: Writable,13 context: LambdaContext14 ) => Promise<void>15) => (event: APIGatewayProxyEvent, context: LambdaContext) => Promise<NodeJS.WritableStream>1617const mockStreamifyResponse: StreamifyResponseHandler = (handlerFunc) => {18 return async (event, context) => {19 const chunks = []20 const mockWritableStream = new Writable({21 write(chunk, encoding, callback) {22 chunks.push(chunk)23 callback()24 },25 })26 mockWritableStream.chunks = chunks27 await handlerFunc(event, mockWritableStream, context)28 mockWritableStream.end()29 return mockWritableStream30 }31}3233const awslambda = {34 streamifyResponse: mockStreamifyResponse,35 HttpResponseStream: {36 from: (stream: Writable, httpResponseMetadata: unknown): Writable => {37 stream.write(Buffer.from(JSON.stringify(httpResponseMetadata)))38 return stream39 },40 },41}4243vi.stubGlobal('awslambda', awslambda)