11import * as http from 'node:http' ;
22import { buffer } from 'node:stream/consumers' ;
3- import { dsnStringToIngestEndpoint } from './utils ' ;
3+ import { debug , dsnFromString , getEnvelopeEndpointWithUrlEncodedAuth } from '@sentry/core ' ;
44
55/**
66 * The Extension API Client.
@@ -16,9 +16,8 @@ export class AwsLambdaExtension {
1616
1717 /**
1818 * Register this extension as an external extension with AWS.
19- * @returns The extension identifier, or null if the extension was not registered.
2019 */
21- public async register ( ) : Promise < string | null > {
20+ public async register ( ) : Promise < void > {
2221 const res = await fetch ( `${ this . _baseUrl } /register` , {
2322 method : 'POST' ,
2423 body : JSON . stringify ( {
@@ -35,13 +34,10 @@ export class AwsLambdaExtension {
3534 }
3635
3736 this . _extensionId = res . headers . get ( 'lambda-extension-identifier' ) ;
38- return res . headers . get ( 'lambda-extension-identifier' ) ;
3937 }
4038
4139 /**
4240 * Advances the extension to the next event.
43- * @param extensionId The extension identifier.
44- * @returns The event data, or null if the extension is not ready to process events.
4541 */
4642 public async next ( ) : Promise < void > {
4743 if ( ! this . _extensionId ) {
@@ -59,14 +55,15 @@ export class AwsLambdaExtension {
5955 throw new Error ( `Failed to advance to next event: ${ await res . text ( ) } ` ) ;
6056 }
6157
62- const event = await res . json ( ) ;
58+ const event = ( await res . json ( ) ) as { eventType : string } ;
6359
64- console . log ( 'EXTENSION EVENT' , event ) ;
60+ if ( event . eventType === 'SHUTDOWN' ) {
61+ await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
62+ }
6563 }
6664
6765 /**
6866 * Reports an error to the extension API.
69- * @param extensionId The extension identifier.
7067 * @param phase The phase of the extension.
7168 * @param err The error to report.
7269 */
@@ -92,7 +89,7 @@ export class AwsLambdaExtension {
9289 } ) ;
9390
9491 if ( ! res . ok ) {
95- throw new Error ( `Failed to report error: ${ await res . text ( ) } ` ) ;
92+ debug . error ( `Failed to report error: ${ await res . text ( ) } ` ) ;
9693 }
9794
9895 throw err ;
@@ -110,34 +107,32 @@ export class AwsLambdaExtension {
110107 const envelope = new TextDecoder ( ) . decode ( envelopeBytes ) ;
111108 const piece = envelope . split ( '\n' ) [ 0 ] ;
112109 const header = JSON . parse ( piece ?? '{}' ) as { dsn ?: string } ;
113- const upstreamSentryUrl = dsnStringToIngestEndpoint ( header . dsn || '' ) ;
114-
115- console . log ( 'tunneling to sentry' , { upstreamSentryUrl } ) ;
116- console . log ( 'headers' , req . headers ) ;
117-
118- fetch ( upstreamSentryUrl , {
110+ if ( ! header . dsn ) {
111+ throw new Error ( 'DSN is not set' ) ;
112+ }
113+ const dsn = dsnFromString ( header . dsn ) ;
114+ if ( ! dsn ) {
115+ throw new Error ( 'Invalid DSN' ) ;
116+ }
117+ const upstreamSentryUrl = getEnvelopeEndpointWithUrlEncodedAuth ( dsn ) ;
118+
119+ await fetch ( upstreamSentryUrl , {
119120 method : 'POST' ,
120121 body : envelopeBytes ,
121- } )
122- . then ( ( ) => {
123- console . log ( 'tunneled to sentry' ) ;
124- } )
125- . catch ( err => {
126- console . error ( 'error tunneling to sentry' , err ) ;
127- } ) ;
122+ } ) ;
128123
129124 res . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
130125 res . end ( JSON . stringify ( { } ) ) ;
131126 } catch ( e ) {
132- console . error ( 'error tunneling to sentry ' , e ) ;
127+ debug . error ( 'Error tunneling to Sentry ' , e ) ;
133128 res . writeHead ( 500 , { 'Content-Type' : 'application/json' } ) ;
134- res . end ( JSON . stringify ( { error : 'error tunneling to sentry ' } ) ) ;
129+ res . end ( JSON . stringify ( { error : 'Error tunneling to Sentry ' } ) ) ;
135130 }
136131 }
137132 } ) ;
138133
139134 server . listen ( 9000 , ( ) => {
140- console . log ( 'server listening on port 9000' ) ;
135+ debug . log ( 'Sentry proxy listening on port 9000' ) ;
141136 } ) ;
142137 }
143138}
0 commit comments