@jaypie/express 1.2.33 → 1.2.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -178,6 +178,13 @@ declare class LambdaResponseBuffered extends Writable {
178
178
  */
179
179
  get headers(): Record<string, string | string[] | undefined>;
180
180
  writeHead(statusCode: number, statusMessageOrHeaders?: OutgoingHttpHeaders | string, headers?: OutgoingHttpHeaders): this;
181
+ /**
182
+ * Node's implicit-header hook, called before the first body byte commits.
183
+ * Routes through this.writeHead resolved at call time so wrappers installed
184
+ * mid-request (on-headers, and through it express-session, morgan,
185
+ * compression, express-openid-connect) run their listeners.
186
+ */
187
+ _implicitHeader(): void;
181
188
  get headersSent(): boolean;
182
189
  /**
183
190
  * Express-style alias for getHeader().
@@ -241,8 +248,16 @@ declare class LambdaResponseStreaming extends Writable {
241
248
  */
242
249
  get headers(): Record<string, string | string[] | undefined>;
243
250
  writeHead(statusCode: number, statusMessageOrHeaders?: OutgoingHttpHeaders | string, headers?: OutgoingHttpHeaders): this;
251
+ /**
252
+ * Node's implicit-header hook, called before the first body byte commits.
253
+ * Routes through this.writeHead resolved at call time so wrappers installed
254
+ * mid-request (on-headers, and through it express-session, morgan,
255
+ * compression, express-openid-connect) run their listeners.
256
+ */
257
+ _implicitHeader(): void;
244
258
  get headersSent(): boolean;
245
259
  flushHeaders(): void;
260
+ private _commitHeaders;
246
261
  /**
247
262
  * Express-style alias for getHeader().
248
263
  * Used by middleware like decorateResponse that use res.get().
@@ -322,8 +337,9 @@ declare const EXPRESS: {
322
337
  };
323
338
  };
324
339
 
340
+ type CorsOrigin = string | RegExp | Array<string | RegExp>;
325
341
  interface CorsConfig {
326
- origin?: string | string[];
342
+ origin?: CorsOrigin;
327
343
  overrides?: Record<string, unknown>;
328
344
  }
329
345
  /**
@@ -458,4 +474,4 @@ declare const notFoundRoute: (req: express.Request, res: express.Response) => Pr
458
474
  declare const notImplementedRoute: (req: express.Request, res: express.Response, ...params: unknown[]) => Promise<unknown>;
459
475
 
460
476
  export { EXPRESS, LambdaRequest, LambdaResponseBuffered, LambdaResponseStreaming, badRequestRoute, _default as cors, createLambdaHandler, createLambdaStreamHandler, echoRoute, expressHandler, httpHandler as expressHttpCodeHandler, expressStreamHandler, fabricApiResponse, forbiddenRoute, getCurrentInvoke, getCurrentInvokeUuid, goneRoute, methodNotAllowedRoute, noContentRoute, notFoundRoute, notImplementedRoute };
461
- export type { ApiGatewayV1Event, CorsConfig, CreateLambdaHandlerOptions, ExpressHandlerLocals, ExpressHandlerOptions, ExpressStreamHandler, ExpressStreamHandlerLocals, ExpressStreamHandlerOptions, FunctionUrlEvent, JaypieHandlerSetup, JaypieHandlerTeardown, JaypieHandlerValidate, JaypieStreamHandlerSetup, JaypieStreamHandlerTeardown, JaypieStreamHandlerValidate, LambdaContext, LambdaEvent, LambdaHandler, LambdaResponse, LambdaStreamHandler, ResponseStream };
477
+ export type { ApiGatewayV1Event, CorsConfig, CorsOrigin, CreateLambdaHandlerOptions, ExpressHandlerLocals, ExpressHandlerOptions, ExpressStreamHandler, ExpressStreamHandlerLocals, ExpressStreamHandlerOptions, FunctionUrlEvent, JaypieHandlerSetup, JaypieHandlerTeardown, JaypieHandlerValidate, JaypieStreamHandlerSetup, JaypieStreamHandlerTeardown, JaypieStreamHandlerValidate, LambdaContext, LambdaEvent, LambdaHandler, LambdaResponse, LambdaStreamHandler, ResponseStream };
@@ -2,7 +2,7 @@ export { createLambdaHandler, createLambdaStreamHandler, getCurrentInvoke, Lambd
2
2
  export type { ApiGatewayV1Event, CreateLambdaHandlerOptions, FunctionUrlEvent, LambdaContext, LambdaEvent, LambdaHandler, LambdaResponse, LambdaStreamHandler, ResponseStream, } from "./adapter/index.js";
3
3
  export { EXPRESS } from "./constants.js";
4
4
  export { default as cors } from "./cors.helper.js";
5
- export type { CorsConfig } from "./cors.helper.js";
5
+ export type { CorsConfig, CorsOrigin } from "./cors.helper.js";
6
6
  export { default as fabricApiResponse } from "./fabricApiResponse.js";
7
7
  export { default as expressHandler } from "./expressHandler.js";
8
8
  export type { ExpressHandlerLocals, ExpressHandlerOptions, JaypieHandlerSetup, JaypieHandlerTeardown, JaypieHandlerValidate, } from "./expressHandler.js";
@@ -36,6 +36,13 @@ export declare class LambdaResponseBuffered extends Writable {
36
36
  */
37
37
  get headers(): Record<string, string | string[] | undefined>;
38
38
  writeHead(statusCode: number, statusMessageOrHeaders?: OutgoingHttpHeaders | string, headers?: OutgoingHttpHeaders): this;
39
+ /**
40
+ * Node's implicit-header hook, called before the first body byte commits.
41
+ * Routes through this.writeHead resolved at call time so wrappers installed
42
+ * mid-request (on-headers, and through it express-session, morgan,
43
+ * compression, express-openid-connect) run their listeners.
44
+ */
45
+ _implicitHeader(): void;
39
46
  get headersSent(): boolean;
40
47
  /**
41
48
  * Express-style alias for getHeader().
@@ -36,8 +36,16 @@ export declare class LambdaResponseStreaming extends Writable {
36
36
  */
37
37
  get headers(): Record<string, string | string[] | undefined>;
38
38
  writeHead(statusCode: number, statusMessageOrHeaders?: OutgoingHttpHeaders | string, headers?: OutgoingHttpHeaders): this;
39
+ /**
40
+ * Node's implicit-header hook, called before the first body byte commits.
41
+ * Routes through this.writeHead resolved at call time so wrappers installed
42
+ * mid-request (on-headers, and through it express-session, morgan,
43
+ * compression, express-openid-connect) run their listeners.
44
+ */
45
+ _implicitHeader(): void;
39
46
  get headersSent(): boolean;
40
47
  flushHeaders(): void;
48
+ private _commitHeaders;
41
49
  /**
42
50
  * Express-style alias for getHeader().
43
51
  * Used by middleware like decorateResponse that use res.get().
@@ -1,10 +1,11 @@
1
1
  import type { Request, Response, NextFunction } from "express";
2
+ export type CorsOrigin = string | RegExp | Array<string | RegExp>;
2
3
  export interface CorsConfig {
3
- origin?: string | string[];
4
+ origin?: CorsOrigin;
4
5
  overrides?: Record<string, unknown>;
5
6
  }
6
7
  type CorsCallback = (err: Error | null, allow?: boolean) => void;
7
- export declare const dynamicOriginCallbackHandler: (origin?: string | string[]) => ((requestOrigin: string | undefined, callback: CorsCallback) => void);
8
+ export declare const dynamicOriginCallbackHandler: (origin?: CorsOrigin) => ((requestOrigin: string | undefined, callback: CorsCallback) => void);
8
9
  /**
9
10
  * CORS middleware with Lambda streaming support.
10
11
  *
@@ -178,6 +178,13 @@ declare class LambdaResponseBuffered extends Writable {
178
178
  */
179
179
  get headers(): Record<string, string | string[] | undefined>;
180
180
  writeHead(statusCode: number, statusMessageOrHeaders?: OutgoingHttpHeaders | string, headers?: OutgoingHttpHeaders): this;
181
+ /**
182
+ * Node's implicit-header hook, called before the first body byte commits.
183
+ * Routes through this.writeHead resolved at call time so wrappers installed
184
+ * mid-request (on-headers, and through it express-session, morgan,
185
+ * compression, express-openid-connect) run their listeners.
186
+ */
187
+ _implicitHeader(): void;
181
188
  get headersSent(): boolean;
182
189
  /**
183
190
  * Express-style alias for getHeader().
@@ -241,8 +248,16 @@ declare class LambdaResponseStreaming extends Writable {
241
248
  */
242
249
  get headers(): Record<string, string | string[] | undefined>;
243
250
  writeHead(statusCode: number, statusMessageOrHeaders?: OutgoingHttpHeaders | string, headers?: OutgoingHttpHeaders): this;
251
+ /**
252
+ * Node's implicit-header hook, called before the first body byte commits.
253
+ * Routes through this.writeHead resolved at call time so wrappers installed
254
+ * mid-request (on-headers, and through it express-session, morgan,
255
+ * compression, express-openid-connect) run their listeners.
256
+ */
257
+ _implicitHeader(): void;
244
258
  get headersSent(): boolean;
245
259
  flushHeaders(): void;
260
+ private _commitHeaders;
246
261
  /**
247
262
  * Express-style alias for getHeader().
248
263
  * Used by middleware like decorateResponse that use res.get().
@@ -322,8 +337,9 @@ declare const EXPRESS: {
322
337
  };
323
338
  };
324
339
 
340
+ type CorsOrigin = string | RegExp | Array<string | RegExp>;
325
341
  interface CorsConfig {
326
- origin?: string | string[];
342
+ origin?: CorsOrigin;
327
343
  overrides?: Record<string, unknown>;
328
344
  }
329
345
  /**
@@ -458,4 +474,4 @@ declare const notFoundRoute: (req: express.Request, res: express.Response) => Pr
458
474
  declare const notImplementedRoute: (req: express.Request, res: express.Response, ...params: unknown[]) => Promise<unknown>;
459
475
 
460
476
  export { EXPRESS, LambdaRequest, LambdaResponseBuffered, LambdaResponseStreaming, badRequestRoute, _default as cors, createLambdaHandler, createLambdaStreamHandler, echoRoute, expressHandler, httpHandler as expressHttpCodeHandler, expressStreamHandler, fabricApiResponse, forbiddenRoute, getCurrentInvoke, getCurrentInvokeUuid, goneRoute, methodNotAllowedRoute, noContentRoute, notFoundRoute, notImplementedRoute };
461
- export type { ApiGatewayV1Event, CorsConfig, CreateLambdaHandlerOptions, ExpressHandlerLocals, ExpressHandlerOptions, ExpressStreamHandler, ExpressStreamHandlerLocals, ExpressStreamHandlerOptions, FunctionUrlEvent, JaypieHandlerSetup, JaypieHandlerTeardown, JaypieHandlerValidate, JaypieStreamHandlerSetup, JaypieStreamHandlerTeardown, JaypieStreamHandlerValidate, LambdaContext, LambdaEvent, LambdaHandler, LambdaResponse, LambdaStreamHandler, ResponseStream };
477
+ export type { ApiGatewayV1Event, CorsConfig, CorsOrigin, CreateLambdaHandlerOptions, ExpressHandlerLocals, ExpressHandlerOptions, ExpressStreamHandler, ExpressStreamHandlerLocals, ExpressStreamHandlerOptions, FunctionUrlEvent, JaypieHandlerSetup, JaypieHandlerTeardown, JaypieHandlerValidate, JaypieStreamHandlerSetup, JaypieStreamHandlerTeardown, JaypieStreamHandlerValidate, LambdaContext, LambdaEvent, LambdaHandler, LambdaResponse, LambdaStreamHandler, ResponseStream };
package/dist/esm/index.js CHANGED
@@ -2,7 +2,7 @@ import { flushLlmObs, hasDatadogEnv, submitMetric, DATADOG, loadDatadogApiKey }
2
2
  import { Readable, Writable } from 'node:stream';
3
3
  import { ServerResponse } from 'node:http';
4
4
  import { CorsError, BadRequestError, UnhandledError, GatewayTimeoutError, UnavailableError, BadGatewayError, InternalError, TeapotError, GoneError, MethodNotAllowedError, NotFoundError, ForbiddenError, UnauthorizedError, NotImplementedError } from '@jaypie/errors';
5
- import { force, envBoolean, JAYPIE, HTTP, getHeaderFrom, jaypieHandler } from '@jaypie/kit';
5
+ import { force, isLocalEnv, envBoolean, JAYPIE, HTTP, getHeaderFrom, jaypieHandler } from '@jaypie/kit';
6
6
  import expressCors from 'cors';
7
7
  import { loadEnvVariables, loadEnvSecrets, getContentTypeForFormat, formatStreamError } from '@jaypie/aws';
8
8
  import { log, redactAuth } from '@jaypie/logger';
@@ -382,6 +382,7 @@ class LambdaResponseBuffered extends Writable {
382
382
  this.getHeaders = this.getHeaders.bind(this);
383
383
  this.getHeaderNames = this.getHeaderNames.bind(this);
384
384
  this.writeHead = this.writeHead.bind(this);
385
+ this._implicitHeader = this._implicitHeader.bind(this);
385
386
  this.get = this.get.bind(this);
386
387
  this.set = this.set.bind(this);
387
388
  this.status = this.status.bind(this);
@@ -538,6 +539,9 @@ class LambdaResponseBuffered extends Writable {
538
539
  });
539
540
  }
540
541
  writeHead(statusCode, statusMessageOrHeaders, headers) {
542
+ if (this._headersSent) {
543
+ return this;
544
+ }
541
545
  this.statusCode = statusCode;
542
546
  let headersToSet;
543
547
  if (typeof statusMessageOrHeaders === "string") {
@@ -559,6 +563,17 @@ class LambdaResponseBuffered extends Writable {
559
563
  this._headersSent = true;
560
564
  return this;
561
565
  }
566
+ /**
567
+ * Node's implicit-header hook, called before the first body byte commits.
568
+ * Routes through this.writeHead resolved at call time so wrappers installed
569
+ * mid-request (on-headers, and through it express-session, morgan,
570
+ * compression, express-openid-connect) run their listeners.
571
+ */
572
+ _implicitHeader() {
573
+ if (!this._headersSent) {
574
+ this.writeHead(this.statusCode);
575
+ }
576
+ }
562
577
  get headersSent() {
563
578
  return this._headersSent;
564
579
  }
@@ -630,11 +645,13 @@ class LambdaResponseBuffered extends Writable {
630
645
  const buffer = Buffer.isBuffer(chunk)
631
646
  ? chunk
632
647
  : Buffer.from(chunk, encoding);
648
+ this._implicitHeader();
633
649
  this._chunks.push(buffer);
634
650
  this._headersSent = true;
635
651
  callback();
636
652
  }
637
653
  _final(callback) {
654
+ this._implicitHeader();
638
655
  this._ended = true;
639
656
  if (this._resolve) {
640
657
  this._resolve(this.buildResult());
@@ -726,6 +743,8 @@ class LambdaResponseStreaming extends Writable {
726
743
  this.getHeaderNames = this.getHeaderNames.bind(this);
727
744
  this.writeHead = this.writeHead.bind(this);
728
745
  this.flushHeaders = this.flushHeaders.bind(this);
746
+ this._implicitHeader = this._implicitHeader.bind(this);
747
+ this._commitHeaders = this._commitHeaders.bind(this);
729
748
  this.get = this.get.bind(this);
730
749
  this.set = this.set.bind(this);
731
750
  this.status = this.status.bind(this);
@@ -890,13 +909,27 @@ class LambdaResponseStreaming extends Writable {
890
909
  }
891
910
  }
892
911
  }
893
- this.flushHeaders();
912
+ this._commitHeaders();
894
913
  return this;
895
914
  }
915
+ /**
916
+ * Node's implicit-header hook, called before the first body byte commits.
917
+ * Routes through this.writeHead resolved at call time so wrappers installed
918
+ * mid-request (on-headers, and through it express-session, morgan,
919
+ * compression, express-openid-connect) run their listeners.
920
+ */
921
+ _implicitHeader() {
922
+ if (!this._headersSent) {
923
+ this.writeHead(this.statusCode);
924
+ }
925
+ }
896
926
  get headersSent() {
897
927
  return this._headersSent;
898
928
  }
899
929
  flushHeaders() {
930
+ this._implicitHeader();
931
+ }
932
+ _commitHeaders() {
900
933
  if (this._headersSent) {
901
934
  return;
902
935
  }
@@ -1002,7 +1035,7 @@ class LambdaResponseStreaming extends Writable {
1002
1035
  // Buffer writes until headers are sent
1003
1036
  this._pendingWrites.push({ callback: () => callback(), chunk: buffer });
1004
1037
  // Auto-flush headers on first write
1005
- this.flushHeaders();
1038
+ this._implicitHeader();
1006
1039
  }
1007
1040
  else {
1008
1041
  this._wrappedStream.write(buffer);
@@ -1010,9 +1043,7 @@ class LambdaResponseStreaming extends Writable {
1010
1043
  }
1011
1044
  }
1012
1045
  _final(callback) {
1013
- if (!this._headersSent) {
1014
- this.flushHeaders();
1015
- }
1046
+ this._implicitHeader();
1016
1047
  // For converted 204 responses, write empty JSON body
1017
1048
  // Lambda streaming requires body content for metadata to be transmitted
1018
1049
  if (this._convertedFrom204 && this._wrappedStream) {
@@ -1268,8 +1299,9 @@ const dynamicOriginCallbackHandler = (origin) => {
1268
1299
  const additionalOrigins = force.array(origin);
1269
1300
  allowedOrigins.push(...additionalOrigins);
1270
1301
  }
1271
- // Add localhost origins in sandbox
1272
- if (process.env.PROJECT_ENV === SANDBOX_ENV ||
1302
+ // Add localhost origins in local and sandbox environments
1303
+ if (isLocalEnv() ||
1304
+ process.env.PROJECT_ENV === SANDBOX_ENV ||
1273
1305
  envBoolean("PROJECT_SANDBOX_MODE")) {
1274
1306
  allowedOrigins.push("http://localhost");
1275
1307
  allowedOrigins.push(/^http:\/\/localhost:\d+$/);