@@ -75,6 +75,7 @@ const headerGetter: TextMapGetter<APIGatewayProxyEventHeaders> = {
75
75
76
76
export const lambdaMaxInitInMilliseconds = 10_000 ;
77
77
const AWS_HANDLER_STREAMING_SYMBOL = Symbol . for ( 'aws.lambda.runtime.handler.streaming' ) ;
78
+ const AWS_HANDLER_HIGHWATERMARK_SYMBOL = Symbol . for ( 'aws.lambda.runtime.handler.streaming.highWaterMark' ) ;
78
79
const AWS_HANDLER_STREAMING_RESPONSE = 'response' ;
79
80
80
81
/**
@@ -106,11 +107,14 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr
106
107
107
108
// Provide a temporary awslambda polyfill for CommonJS modules during loading
108
109
// This prevents ReferenceError when modules use awslambda.streamifyResponse at load time
110
+ // taken from https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/main/src/UserFunction.js#L205C7-L211C9
109
111
if ( typeof globalThis . awslambda === 'undefined' ) {
110
112
( globalThis as any ) . awslambda = {
111
- streamifyResponse : ( handler : any ) => {
112
- // Add the streaming symbols that the instrumentation looks for
113
+ streamifyResponse : ( handler : any , options : any ) => {
113
114
handler [ AWS_HANDLER_STREAMING_SYMBOL ] = AWS_HANDLER_STREAMING_RESPONSE ;
115
+ if ( typeof options ?. highWaterMark === 'number' ) {
116
+ handler [ AWS_HANDLER_HIGHWATERMARK_SYMBOL ] = parseInt ( options . highWaterMark ) ;
117
+ }
114
118
return handler ;
115
119
} ,
116
120
} ;
@@ -208,11 +212,12 @@ export class AwsLambdaInstrumentation extends InstrumentationBase<AwsLambdaInstr
208
212
const patchedHandler = this . _getPatchHandler ( original , handlerLoadStartTime ) ;
209
213
210
214
// Streaming handlers have special symbols that we need to copy over to the patched handler.
211
- for ( const symbol of Object . getOwnPropertySymbols ( original ) ) {
212
- ( patchedHandler as unknown as Record < symbol , unknown > ) [ symbol ] = (
213
- original as unknown as Record < symbol , unknown >
214
- ) [ symbol ] ;
215
- }
215
+ ( patchedHandler as unknown as Record < symbol , unknown > ) [ AWS_HANDLER_STREAMING_SYMBOL ] = (
216
+ original as unknown as Record < symbol , unknown >
217
+ ) [ AWS_HANDLER_STREAMING_SYMBOL ] ;
218
+ ( patchedHandler as unknown as Record < symbol , unknown > ) [ AWS_HANDLER_HIGHWATERMARK_SYMBOL ] = (
219
+ original as unknown as Record < symbol , unknown >
220
+ ) [ AWS_HANDLER_HIGHWATERMARK_SYMBOL ] ;
216
221
217
222
return wrapHandler ( patchedHandler ) as T ;
218
223
}
0 commit comments