@jaypie/express 1.2.4-rc23 → 1.2.4-rc24

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.
@@ -3,8 +3,6 @@ export type { ApiGatewayV1Event, CreateLambdaHandlerOptions, FunctionUrlEvent, L
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";
6
- export { default as createServer } from "./createServer.js";
7
- export type { CreateServerOptions, ServerResult } from "./createServer.js";
8
6
  export { default as expressHandler } from "./expressHandler.js";
9
7
  export type { ExpressHandlerLocals, ExpressHandlerOptions, JaypieHandlerSetup, JaypieHandlerTeardown, JaypieHandlerValidate, } from "./expressHandler.js";
10
8
  export { default as expressHttpCodeHandler } from "./http.handler.js";
@@ -1,10 +1,10 @@
1
1
  import type { Request } from "express";
2
2
  /**
3
3
  * Get the current invoke UUID from Lambda context.
4
- * Works with Jaypie Lambda adapter and Lambda Web Adapter mode.
4
+ * Works with Jaypie Lambda adapter (createLambdaHandler/createLambdaStreamHandler).
5
5
  *
6
6
  * @param req - Optional Express request object. Used to extract context
7
- * from Web Adapter headers or Jaypie adapter's _lambdaContext.
7
+ * from Jaypie adapter's _lambdaContext.
8
8
  * @returns The AWS request ID or undefined if not in Lambda context
9
9
  */
10
10
  declare function getCurrentInvokeUuid(req?: Request): string | undefined;
@@ -3,8 +3,6 @@ export type { ApiGatewayV1Event, CreateLambdaHandlerOptions, FunctionUrlEvent, L
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";
6
- export { default as createServer } from "./createServer.js";
7
- export type { CreateServerOptions, ServerResult } from "./createServer.js";
8
6
  export { default as expressHandler } from "./expressHandler.js";
9
7
  export type { ExpressHandlerLocals, ExpressHandlerOptions, JaypieHandlerSetup, JaypieHandlerTeardown, JaypieHandlerValidate, } from "./expressHandler.js";
10
8
  export { default as expressHttpCodeHandler } from "./http.handler.js";
package/dist/esm/index.js CHANGED
@@ -3,8 +3,8 @@ import { ServerResponse } from 'node:http';
3
3
  import { CorsError, BadRequestError, UnhandledError, GatewayTimeoutError, UnavailableError, BadGatewayError, InternalError, TeapotError, GoneError, MethodNotAllowedError, NotFoundError, ForbiddenError, UnauthorizedError, NotImplementedError } from '@jaypie/errors';
4
4
  import { force, envBoolean, JAYPIE, HTTP, getHeaderFrom, jaypieHandler } from '@jaypie/kit';
5
5
  import expressCors from 'cors';
6
- import { log } from '@jaypie/logger';
7
6
  import { loadEnvSecrets } from '@jaypie/aws';
7
+ import { log } from '@jaypie/logger';
8
8
  import { hasDatadogEnv, submitMetric, DATADOG } from '@jaypie/datadog';
9
9
 
10
10
  //
@@ -1116,7 +1116,7 @@ const corsHelper = (config = {}) => {
1116
1116
  };
1117
1117
  return expressCors(options);
1118
1118
  };
1119
- var cors = (config) => {
1119
+ var cors_helper = (config) => {
1120
1120
  const cors = corsHelper(config);
1121
1121
  return (req, res, next) => {
1122
1122
  cors(req, res, (error) => {
@@ -1131,154 +1131,10 @@ var cors = (config) => {
1131
1131
  };
1132
1132
  };
1133
1133
 
1134
- //
1135
- //
1136
- // Constants
1137
- //
1138
- const DEFAULT_PORT = 8080;
1139
- //
1140
- //
1141
- // Main
1142
- //
1143
- /**
1144
- * Creates and starts an Express server with standard Jaypie middleware.
1145
- *
1146
- * Features:
1147
- * - CORS handling (configurable)
1148
- * - JSON body parsing
1149
- * - Listens on PORT env var (default 8080)
1150
- *
1151
- * Usage:
1152
- * ```ts
1153
- * import express from "express";
1154
- * import { createServer, expressHandler } from "@jaypie/express";
1155
- *
1156
- * const app = express();
1157
- *
1158
- * app.get("/", expressHandler(async (req, res) => {
1159
- * return { message: "Hello World" };
1160
- * }));
1161
- *
1162
- * const { server, port } = await createServer(app);
1163
- * console.log(`Server running on port ${port}`);
1164
- * ```
1165
- *
1166
- * @param app - Express application instance
1167
- * @param options - Server configuration options
1168
- * @returns Promise resolving to server instance and port
1169
- */
1170
- async function createServer(app, options = {}) {
1171
- const { cors: corsConfig, jsonLimit = "1mb", middleware = [], port: portOption, } = options;
1172
- // Determine port
1173
- const port = typeof portOption === "string"
1174
- ? parseInt(portOption, 10)
1175
- : (portOption ?? parseInt(process.env.PORT || String(DEFAULT_PORT), 10));
1176
- // Apply CORS middleware (unless explicitly disabled)
1177
- if (corsConfig !== false) {
1178
- app.use(cors(corsConfig));
1179
- }
1180
- // Apply JSON body parser
1181
- // Note: We use dynamic import to avoid requiring express as a direct dependency
1182
- const express = await import('express');
1183
- app.use(express.json({ limit: jsonLimit }));
1184
- // Apply additional middleware
1185
- for (const mw of middleware) {
1186
- app.use(mw);
1187
- }
1188
- // Start server
1189
- return new Promise((resolve, reject) => {
1190
- try {
1191
- const server = app.listen(port, () => {
1192
- // Get the actual port (important when port 0 is passed to get an ephemeral port)
1193
- const address = server.address();
1194
- const actualPort = address?.port ?? port;
1195
- log.info(`Server listening on port ${actualPort}`);
1196
- resolve({ port: actualPort, server });
1197
- });
1198
- server.on("error", (error) => {
1199
- log.error("Server error", { error });
1200
- reject(error);
1201
- });
1202
- }
1203
- catch (error) {
1204
- reject(error);
1205
- }
1206
- });
1207
- }
1208
-
1209
- //
1210
- //
1211
- // Constants
1212
- //
1213
- const HEADER_AMZN_REQUEST_ID$1 = "x-amzn-request-id";
1214
- const ENV_AMZN_TRACE_ID = "_X_AMZN_TRACE_ID";
1215
1134
  //
1216
1135
  //
1217
1136
  // Helper Functions
1218
1137
  //
1219
- /**
1220
- * Extract request ID from X-Ray trace ID environment variable
1221
- * Format: Root=1-5e6b4a90-example;Parent=example;Sampled=1
1222
- * We extract the trace ID from the Root segment
1223
- */
1224
- function parseTraceId(traceId) {
1225
- if (!traceId)
1226
- return undefined;
1227
- // Extract the Root segment (format: Root=1-{timestamp}-{uuid})
1228
- const rootMatch = traceId.match(/Root=([^;]+)/);
1229
- if (rootMatch && rootMatch[1]) {
1230
- return rootMatch[1];
1231
- }
1232
- return undefined;
1233
- }
1234
- //
1235
- //
1236
- // Main
1237
- //
1238
- /**
1239
- * Get the current invoke UUID from Lambda Web Adapter context.
1240
- * This function extracts the request ID from either:
1241
- * 1. The x-amzn-request-id header (set by Lambda Web Adapter)
1242
- * 2. The _X_AMZN_TRACE_ID environment variable (set by Lambda runtime)
1243
- *
1244
- * @param req - Optional Express request object to extract headers from
1245
- * @returns The AWS request ID or undefined if not in Lambda context
1246
- */
1247
- function getWebAdapterUuid(req) {
1248
- // First, try to get from request headers
1249
- if (req && req.headers) {
1250
- const headerValue = req.headers[HEADER_AMZN_REQUEST_ID$1];
1251
- if (headerValue) {
1252
- return Array.isArray(headerValue) ? headerValue[0] : headerValue;
1253
- }
1254
- }
1255
- // Fall back to environment variable (X-Ray trace ID)
1256
- const traceId = process.env[ENV_AMZN_TRACE_ID];
1257
- if (traceId) {
1258
- return parseTraceId(traceId);
1259
- }
1260
- return undefined;
1261
- }
1262
-
1263
- //
1264
- //
1265
- // Constants
1266
- //
1267
- const HEADER_AMZN_REQUEST_ID = "x-amzn-request-id";
1268
- //
1269
- //
1270
- // Helper Functions
1271
- //
1272
- /**
1273
- * Detect if we're running in Lambda Web Adapter mode.
1274
- * Web Adapter sets the x-amzn-request-id header on requests.
1275
- */
1276
- function isWebAdapterMode(req) {
1277
- if (req && req.headers && req.headers[HEADER_AMZN_REQUEST_ID]) {
1278
- return true;
1279
- }
1280
- return false;
1281
- }
1282
1138
  /**
1283
1139
  * Get UUID from Jaypie Lambda adapter context.
1284
1140
  * This is set by createLambdaHandler/createLambdaStreamHandler.
@@ -1295,8 +1151,12 @@ function getJaypieAdapterUuid() {
1295
1151
  * The Jaypie adapter attaches _lambdaContext to the request.
1296
1152
  */
1297
1153
  function getRequestContextUuid(req) {
1298
- if (req && req._lambdaContext?.awsRequestId) {
1299
- return req._lambdaContext.awsRequestId;
1154
+ if (req && req._lambdaContext) {
1155
+ const lambdaContext = req
1156
+ ._lambdaContext;
1157
+ if (lambdaContext.awsRequestId) {
1158
+ return lambdaContext.awsRequestId;
1159
+ }
1300
1160
  }
1301
1161
  return undefined;
1302
1162
  }
@@ -1306,29 +1166,20 @@ function getRequestContextUuid(req) {
1306
1166
  //
1307
1167
  /**
1308
1168
  * Get the current invoke UUID from Lambda context.
1309
- * Works with Jaypie Lambda adapter and Lambda Web Adapter mode.
1169
+ * Works with Jaypie Lambda adapter (createLambdaHandler/createLambdaStreamHandler).
1310
1170
  *
1311
1171
  * @param req - Optional Express request object. Used to extract context
1312
- * from Web Adapter headers or Jaypie adapter's _lambdaContext.
1172
+ * from Jaypie adapter's _lambdaContext.
1313
1173
  * @returns The AWS request ID or undefined if not in Lambda context
1314
1174
  */
1315
1175
  function getCurrentInvokeUuid(req) {
1316
- // Priority 1: Web Adapter mode (header-based)
1317
- if (isWebAdapterMode(req)) {
1318
- return getWebAdapterUuid(req);
1319
- }
1320
- // Priority 2: Request has Lambda context attached (Jaypie adapter)
1176
+ // Priority 1: Request has Lambda context attached (Jaypie adapter)
1321
1177
  const requestContextUuid = getRequestContextUuid(req);
1322
1178
  if (requestContextUuid) {
1323
1179
  return requestContextUuid;
1324
1180
  }
1325
- // Priority 3: Global context from Jaypie adapter
1326
- const jaypieAdapterUuid = getJaypieAdapterUuid();
1327
- if (jaypieAdapterUuid) {
1328
- return jaypieAdapterUuid;
1329
- }
1330
- // Fallback: Web Adapter env var
1331
- return getWebAdapterUuid();
1181
+ // Priority 2: Global context from Jaypie adapter
1182
+ return getJaypieAdapterUuid();
1332
1183
  }
1333
1184
 
1334
1185
  //
@@ -2225,5 +2076,5 @@ const noContentRoute = routes.noContentRoute;
2225
2076
  const notFoundRoute = routes.notFoundRoute;
2226
2077
  const notImplementedRoute = routes.notImplementedRoute;
2227
2078
 
2228
- export { EXPRESS, LambdaRequest, LambdaResponseBuffered, LambdaResponseStreaming, badRequestRoute, cors, createLambdaHandler, createLambdaStreamHandler, createServer, echoRoute, expressHandler, httpHandler as expressHttpCodeHandler, expressStreamHandler, forbiddenRoute, getCurrentInvoke, getCurrentInvokeUuid, goneRoute, methodNotAllowedRoute, noContentRoute, notFoundRoute, notImplementedRoute };
2079
+ export { EXPRESS, LambdaRequest, LambdaResponseBuffered, LambdaResponseStreaming, badRequestRoute, cors_helper as cors, createLambdaHandler, createLambdaStreamHandler, echoRoute, expressHandler, httpHandler as expressHttpCodeHandler, expressStreamHandler, forbiddenRoute, getCurrentInvoke, getCurrentInvokeUuid, goneRoute, methodNotAllowedRoute, noContentRoute, notFoundRoute, notImplementedRoute };
2229
2080
  //# sourceMappingURL=index.js.map