@jaypie/express 1.2.9 → 1.2.10

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.
@@ -1,6 +1,7 @@
1
1
  import type { OutgoingHttpHeaders } from "node:http";
2
2
  import { Writable } from "node:stream";
3
3
  import type { ResponseStream } from "./types.js";
4
+ export declare const JAYPIE_LAMBDA_STREAMING: unique symbol;
4
5
  /**
5
6
  * Mock ServerResponse that streams directly to Lambda responseStream.
6
7
  * Uses awslambda.HttpResponseStream.from() to set status and headers.
package/dist/esm/index.js CHANGED
@@ -624,6 +624,9 @@ class LambdaResponseBuffered extends Writable {
624
624
  //
625
625
  // Constants
626
626
  //
627
+ // Symbol to identify Lambda streaming responses. Uses Symbol.for() to ensure
628
+ // the same symbol is used across bundles/realms. Survives prototype manipulation.
629
+ const JAYPIE_LAMBDA_STREAMING = Symbol.for("@jaypie/express/LambdaStreaming");
627
630
  // Get Node's internal kOutHeaders symbol from ServerResponse prototype.
628
631
  // This is needed for compatibility with Datadog dd-trace instrumentation,
629
632
  // which patches HTTP methods and expects this internal state to exist.
@@ -652,6 +655,10 @@ class LambdaResponseStreaming extends Writable {
652
655
  this._pendingWrites = [];
653
656
  this._wrappedStream = null;
654
657
  this._responseStream = responseStream;
658
+ // Mark as Lambda streaming response using Symbol for reliable detection.
659
+ // Survives prototype manipulation from Express and dd-trace.
660
+ this[JAYPIE_LAMBDA_STREAMING] =
661
+ true;
655
662
  // Initialize Node's internal kOutHeaders for dd-trace compatibility.
656
663
  // dd-trace patches HTTP methods and expects this internal state.
657
664
  if (kOutHeaders) {
@@ -1024,13 +1031,6 @@ function createLambdaHandler(app, _options) {
1024
1031
  await runExpressApp(app, req, res);
1025
1032
  // Get Lambda response - await explicitly to ensure we have the result
1026
1033
  result = await res.getResult();
1027
- // Debug: Log the response before returning
1028
- console.log("[createLambdaHandler] Returning response:", JSON.stringify({
1029
- statusCode: result.statusCode,
1030
- headers: result.headers,
1031
- bodyLength: result.body?.length,
1032
- isBase64Encoded: result.isBase64Encoded,
1033
- }));
1034
1034
  return result;
1035
1035
  }
1036
1036
  catch (error) {
@@ -1551,6 +1551,14 @@ const logger$1 = log;
1551
1551
  function isLambdaMockResponse(res) {
1552
1552
  return (res[JAYPIE_LAMBDA_MOCK] === true);
1553
1553
  }
1554
+ /**
1555
+ * Check if response is a Lambda streaming response.
1556
+ * Uses Symbol marker to survive prototype chain modifications from Express and dd-trace.
1557
+ */
1558
+ function isLambdaStreamingResponse(res) {
1559
+ return (res[JAYPIE_LAMBDA_STREAMING] ===
1560
+ true);
1561
+ }
1554
1562
  /**
1555
1563
  * Safely send a JSON response, avoiding dd-trace interception.
1556
1564
  * For Lambda mock responses, directly manipulates internal state instead of
@@ -1585,9 +1593,11 @@ function safeSendJson(res, statusCode, data) {
1585
1593
  // Fall back to standard Express methods for real responses
1586
1594
  res.status(statusCode);
1587
1595
  // CRITICAL: For Lambda streaming responses, flush headers before send to
1588
- // initialize the stream wrapper. Check for _responseStream which is unique
1589
- // to LambdaResponseStreaming (regular Express res doesn't have this).
1590
- if ("_responseStream" in res && typeof res.flushHeaders === "function") {
1596
+ // initialize the stream wrapper. This ensures the status code is captured
1597
+ // before any writes occur (which would auto-flush with default 200).
1598
+ // Uses Symbol marker for reliable detection that survives prototype manipulation.
1599
+ if (isLambdaStreamingResponse(res) &&
1600
+ typeof res.flushHeaders === "function") {
1591
1601
  res.flushHeaders();
1592
1602
  }
1593
1603
  res.json(data);
@@ -1620,9 +1630,11 @@ function safeSend(res, statusCode, body) {
1620
1630
  // Fall back to standard Express methods for real responses
1621
1631
  res.status(statusCode);
1622
1632
  // CRITICAL: For Lambda streaming responses, flush headers before send to
1623
- // initialize the stream wrapper. Check for _responseStream which is unique
1624
- // to LambdaResponseStreaming (regular Express res doesn't have this).
1625
- if ("_responseStream" in res && typeof res.flushHeaders === "function") {
1633
+ // initialize the stream wrapper. This ensures the status code is captured
1634
+ // before any writes occur (which would auto-flush with default 200).
1635
+ // Uses Symbol marker for reliable detection that survives prototype manipulation.
1636
+ if (isLambdaStreamingResponse(res) &&
1637
+ typeof res.flushHeaders === "function") {
1626
1638
  res.flushHeaders();
1627
1639
  }
1628
1640
  if (body !== undefined) {