@jaypie/express 1.2.4-rc0 → 1.2.4-rc2

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.
@@ -9,11 +9,7 @@ export declare class LambdaResponseStreaming extends Writable {
9
9
  statusCode: number;
10
10
  statusMessage: string;
11
11
  readonly socket: {
12
- cork: () => void;
13
- destroy: () => void;
14
12
  remoteAddress: string;
15
- uncork: () => void;
16
- writable: boolean;
17
13
  };
18
14
  private _headers;
19
15
  private _headersSent;
@@ -1,12 +1,12 @@
1
1
  import type { Application } from "express";
2
- import type { CreateLambdaHandlerOptions, FunctionUrlEvent, LambdaContext, LambdaHandler, LambdaStreamHandler } from "./types.js";
2
+ import type { CreateLambdaHandlerOptions, LambdaContext, LambdaEvent, LambdaHandler, LambdaStreamHandler } from "./types.js";
3
3
  /**
4
4
  * Get the current Lambda invoke context.
5
5
  * Used by getCurrentInvokeUuid adapter to get the request ID.
6
6
  */
7
7
  export declare function getCurrentInvoke(): {
8
8
  context: LambdaContext;
9
- event: FunctionUrlEvent;
9
+ event: LambdaEvent;
10
10
  } | null;
11
11
  /**
12
12
  * Create a Lambda handler that buffers the Express response.
@@ -47,4 +47,4 @@ export declare function createLambdaStreamHandler(app: Application, _options?: C
47
47
  export { LambdaRequest, createLambdaRequest } from "./LambdaRequest.js";
48
48
  export { LambdaResponseBuffered } from "./LambdaResponseBuffered.js";
49
49
  export { LambdaResponseStreaming } from "./LambdaResponseStreaming.js";
50
- export type { AwsLambdaGlobal, CreateLambdaHandlerOptions, FunctionUrlEvent, HttpResponseStreamMetadata, LambdaContext, LambdaHandler, LambdaHandlerFactory, LambdaResponse, LambdaStreamHandler, LambdaStreamHandlerFactory, ResponseStream, } from "./types.js";
50
+ export type { ApiGatewayV1Event, AwsLambdaGlobal, CreateLambdaHandlerOptions, FunctionUrlEvent, HttpResponseStreamMetadata, LambdaContext, LambdaEvent, LambdaHandler, LambdaHandlerFactory, LambdaResponse, LambdaStreamHandler, LambdaStreamHandlerFactory, ResponseStream, } from "./types.js";
@@ -10,6 +10,37 @@ export interface LambdaContext {
10
10
  memoryLimitInMB?: string;
11
11
  [key: string]: unknown;
12
12
  }
13
+ export interface ApiGatewayV1Event {
14
+ body?: string | null;
15
+ headers: Record<string, string>;
16
+ httpMethod: string;
17
+ isBase64Encoded: boolean;
18
+ multiValueHeaders?: Record<string, string[]>;
19
+ multiValueQueryStringParameters?: Record<string, string[]> | null;
20
+ path: string;
21
+ pathParameters?: Record<string, string> | null;
22
+ queryStringParameters?: Record<string, string> | null;
23
+ requestContext: {
24
+ accountId: string;
25
+ apiId: string;
26
+ domainName?: string;
27
+ httpMethod: string;
28
+ identity: {
29
+ sourceIp: string;
30
+ userAgent?: string;
31
+ };
32
+ path: string;
33
+ protocol: string;
34
+ requestId: string;
35
+ requestTime?: string;
36
+ requestTimeEpoch?: number;
37
+ resourceId?: string;
38
+ resourcePath?: string;
39
+ stage: string;
40
+ };
41
+ resource?: string;
42
+ stageVariables?: Record<string, string> | null;
43
+ }
13
44
  export interface FunctionUrlEvent {
14
45
  body?: string;
15
46
  cookies?: string[];
@@ -38,6 +69,7 @@ export interface FunctionUrlEvent {
38
69
  routeKey: string;
39
70
  version: "2.0";
40
71
  }
72
+ export type LambdaEvent = ApiGatewayV1Event | FunctionUrlEvent;
41
73
  export interface LambdaResponse {
42
74
  body: string;
43
75
  cookies?: string[];
@@ -63,8 +95,8 @@ export interface AwsLambdaGlobal {
63
95
  };
64
96
  streamifyResponse<TEvent = unknown>(handler: (event: TEvent, responseStream: ResponseStream, context: LambdaContext) => Promise<void>): (event: TEvent, context: LambdaContext) => Promise<void>;
65
97
  }
66
- export type LambdaHandler = (event: FunctionUrlEvent, context: LambdaContext) => Promise<LambdaResponse>;
67
- export type LambdaStreamHandler = (event: FunctionUrlEvent, context: LambdaContext) => Promise<void>;
98
+ export type LambdaHandler = (event: LambdaEvent, context: LambdaContext) => Promise<LambdaResponse>;
99
+ export type LambdaStreamHandler = (event: LambdaEvent, context: LambdaContext) => Promise<void>;
68
100
  export type CreateLambdaHandlerOptions = {
69
101
  /**
70
102
  * Optional name for logging and debugging
@@ -1,5 +1,5 @@
1
1
  export { createLambdaHandler, createLambdaStreamHandler, getCurrentInvoke, LambdaRequest, LambdaResponseBuffered, LambdaResponseStreaming, } from "./adapter/index.js";
2
- export type { CreateLambdaHandlerOptions, FunctionUrlEvent, LambdaHandler, LambdaResponse, LambdaStreamHandler, } from "./adapter/index.js";
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
5
  export type { CorsConfig } from "./cors.helper.js";
package/dist/esm/index.js CHANGED
@@ -79,16 +79,63 @@ class LambdaRequest extends Readable {
79
79
  }
80
80
  //
81
81
  //
82
+ // Type Guards
83
+ //
84
+ /**
85
+ * Check if event is a Function URL / HTTP API v2 event.
86
+ */
87
+ function isFunctionUrlEvent(event) {
88
+ return "requestContext" in event && "http" in event.requestContext;
89
+ }
90
+ /**
91
+ * Check if event is an API Gateway REST API v1 event.
92
+ */
93
+ function isApiGatewayV1Event(event) {
94
+ return "httpMethod" in event;
95
+ }
96
+ //
97
+ //
82
98
  // Factory Function
83
99
  //
84
100
  /**
85
- * Create a LambdaRequest from a Function URL event.
101
+ * Create a LambdaRequest from a Lambda event (Function URL, HTTP API v2, or REST API v1).
86
102
  */
87
103
  function createLambdaRequest(event, context) {
88
- // Build URL with query string
89
- const url = event.rawQueryString
90
- ? `${event.rawPath}?${event.rawQueryString}`
91
- : event.rawPath;
104
+ let url;
105
+ let method;
106
+ let protocol;
107
+ let remoteAddress;
108
+ const headers = { ...event.headers };
109
+ if (isFunctionUrlEvent(event)) {
110
+ // Function URL / HTTP API v2 format
111
+ url = event.rawQueryString
112
+ ? `${event.rawPath}?${event.rawQueryString}`
113
+ : event.rawPath;
114
+ method = event.requestContext.http.method;
115
+ protocol = event.requestContext.http.protocol.split("/")[0].toLowerCase();
116
+ remoteAddress = event.requestContext.http.sourceIp;
117
+ // Normalize cookies into Cookie header if not already present
118
+ if (event.cookies && event.cookies.length > 0 && !headers.cookie) {
119
+ headers.cookie = event.cookies.join("; ");
120
+ }
121
+ }
122
+ else if (isApiGatewayV1Event(event)) {
123
+ // API Gateway REST API v1 format
124
+ const queryParams = event.queryStringParameters;
125
+ if (queryParams && Object.keys(queryParams).length > 0) {
126
+ const queryString = new URLSearchParams(queryParams).toString();
127
+ url = `${event.path}?${queryString}`;
128
+ }
129
+ else {
130
+ url = event.path;
131
+ }
132
+ method = event.httpMethod;
133
+ protocol = event.requestContext.protocol.split("/")[0].toLowerCase();
134
+ remoteAddress = event.requestContext.identity.sourceIp;
135
+ }
136
+ else {
137
+ throw new Error("Unsupported Lambda event format. Expected Function URL, HTTP API v2, or REST API v1 event.");
138
+ }
92
139
  // Decode body if present
93
140
  let body = null;
94
141
  if (event.body) {
@@ -96,19 +143,14 @@ function createLambdaRequest(event, context) {
96
143
  ? Buffer.from(event.body, "base64")
97
144
  : Buffer.from(event.body, "utf8");
98
145
  }
99
- // Normalize cookies into Cookie header if not already present
100
- const headers = { ...event.headers };
101
- if (event.cookies && event.cookies.length > 0 && !headers.cookie) {
102
- headers.cookie = event.cookies.join("; ");
103
- }
104
146
  return new LambdaRequest({
105
147
  body,
106
148
  headers,
107
149
  lambdaContext: context,
108
150
  lambdaEvent: event,
109
- method: event.requestContext.http.method,
110
- protocol: event.requestContext.http.protocol.split("/")[0].toLowerCase(),
111
- remoteAddress: event.requestContext.http.sourceIp,
151
+ method,
152
+ protocol,
153
+ remoteAddress,
112
154
  url,
113
155
  });
114
156
  }
@@ -141,11 +183,7 @@ class LambdaResponseBuffered extends Writable {
141
183
  this.statusMessage = "OK";
142
184
  // Mock socket to satisfy Express/finalhandler checks
143
185
  this.socket = {
144
- cork: () => { },
145
- destroy: () => { },
146
186
  remoteAddress: "127.0.0.1",
147
- uncork: () => { },
148
- writable: true,
149
187
  };
150
188
  this._chunks = [];
151
189
  this._headers = new Map();
@@ -313,11 +351,7 @@ class LambdaResponseStreaming extends Writable {
313
351
  this.statusMessage = "OK";
314
352
  // Mock socket to satisfy Express/finalhandler checks
315
353
  this.socket = {
316
- cork: () => { },
317
- destroy: () => { },
318
354
  remoteAddress: "127.0.0.1",
319
- uncork: () => { },
320
- writable: true,
321
355
  };
322
356
  this._headers = new Map();
323
357
  this._headersSent = false;