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