@jaypie/express 1.2.11-dev.2 → 1.2.11-dev.4

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/esm/index.js CHANGED
@@ -659,10 +659,6 @@ class LambdaResponseStreaming extends Writable {
659
659
  // Survives prototype manipulation from Express and dd-trace.
660
660
  this[JAYPIE_LAMBDA_STREAMING] =
661
661
  true;
662
- // DIAGNOSTIC: Log symbol assignment
663
- console.log("[DIAG:LambdaResponseStreaming:constructor] Symbol set", {
664
- symbolValue: this[JAYPIE_LAMBDA_STREAMING],
665
- });
666
662
  // Initialize Node's internal kOutHeaders for dd-trace compatibility.
667
663
  // dd-trace patches HTTP methods and expects this internal state.
668
664
  if (kOutHeaders) {
@@ -850,15 +846,7 @@ class LambdaResponseStreaming extends Writable {
850
846
  return this._headersSent;
851
847
  }
852
848
  flushHeaders() {
853
- // DIAGNOSTIC: Log every flushHeaders call with stack trace
854
- const caller = new Error().stack?.split("\n").slice(1, 5).join("\n");
855
- console.log("[DIAG:LambdaResponseStreaming:flushHeaders] Called", {
856
- statusCode: this.statusCode,
857
- headersSent: this._headersSent,
858
- caller,
859
- });
860
849
  if (this._headersSent) {
861
- console.log("[DIAG:LambdaResponseStreaming:flushHeaders] Already sent, returning early");
862
850
  return;
863
851
  }
864
852
  const headers = {};
@@ -869,11 +857,19 @@ class LambdaResponseStreaming extends Writable {
869
857
  headers,
870
858
  statusCode: this.statusCode,
871
859
  };
872
- // DIAGNOSTIC: Log what we're sending to Lambda
873
- console.log("[DIAG:LambdaResponseStreaming:flushHeaders] Creating wrapped stream", {
874
- metadata,
875
- });
876
- // Wrap the stream with metadata using awslambda global
860
+ // For 204 No Content, write metadata directly without wrapped stream.
861
+ // Lambda streaming ignores metadata when no body content is written,
862
+ // so we write the prelude manually and skip creating a wrapped stream.
863
+ if (this.statusCode === 204) {
864
+ const prelude = JSON.stringify(metadata);
865
+ this._responseStream.write(prelude);
866
+ // Metadata separator (8 null bytes) signals end of prelude
867
+ this._responseStream.write("\0\0\0\0\0\0\0\0");
868
+ this._headersSent = true;
869
+ // Don't create wrapped stream - 204 has no body
870
+ return;
871
+ }
872
+ // Normal streaming: create wrapped stream
877
873
  this._wrappedStream = awslambda.HttpResponseStream.from(this._responseStream, metadata);
878
874
  this._headersSent = true;
879
875
  // Flush pending writes
@@ -906,14 +902,6 @@ class LambdaResponseStreaming extends Writable {
906
902
  return this;
907
903
  }
908
904
  status(code) {
909
- // DIAGNOSTIC: Log status code changes
910
- const caller = new Error().stack?.split("\n").slice(1, 4).join("\n");
911
- console.log("[DIAG:LambdaResponseStreaming:status] Setting status", {
912
- newCode: code,
913
- previousCode: this.statusCode,
914
- headersSent: this._headersSent,
915
- caller,
916
- });
917
905
  this.statusCode = code;
918
906
  return this;
919
907
  }
@@ -959,17 +947,10 @@ class LambdaResponseStreaming extends Writable {
959
947
  const buffer = Buffer.isBuffer(chunk)
960
948
  ? chunk
961
949
  : Buffer.from(chunk, encoding);
962
- // DIAGNOSTIC: Log every write
963
- console.log("[DIAG:LambdaResponseStreaming:_write] Called", {
964
- chunkLength: buffer.length,
965
- headersSent: this._headersSent,
966
- statusCode: this.statusCode,
967
- });
968
950
  if (!this._headersSent) {
969
951
  // Buffer writes until headers are sent
970
952
  this._pendingWrites.push({ callback: () => callback(), chunk: buffer });
971
953
  // Auto-flush headers on first write
972
- console.log("[DIAG:LambdaResponseStreaming:_write] Auto-flushing headers on first write");
973
954
  this.flushHeaders();
974
955
  }
975
956
  else {
@@ -978,18 +959,18 @@ class LambdaResponseStreaming extends Writable {
978
959
  }
979
960
  }
980
961
  _final(callback) {
981
- // DIAGNOSTIC: Log _final call
982
- console.log("[DIAG:LambdaResponseStreaming:_final] Called", {
983
- headersSent: this._headersSent,
984
- statusCode: this.statusCode,
985
- hasWrappedStream: !!this._wrappedStream,
986
- });
987
962
  if (!this._headersSent) {
988
- console.log("[DIAG:LambdaResponseStreaming:_final] Headers not sent, flushing now");
989
963
  this.flushHeaders();
990
964
  }
991
- this._wrappedStream?.end();
992
- callback();
965
+ if (this._wrappedStream) {
966
+ this._wrappedStream.end();
967
+ }
968
+ else {
969
+ // 204 case: headers were written directly, just end the response stream
970
+ this._responseStream.end();
971
+ }
972
+ // Use setImmediate to ensure stream operations complete before callback
973
+ setImmediate(callback);
993
974
  }
994
975
  }
995
976
 
@@ -1498,8 +1479,7 @@ const decorateResponse = (res, { handler = "", version = process.env.PROJECT_VER
1498
1479
  // X-Powered-By, override "Express" but nothing else
1499
1480
  const currentPoweredBy = safeGetHeader(extRes, HTTP.HEADER.POWERED_BY);
1500
1481
  if (!currentPoweredBy || currentPoweredBy === "Express") {
1501
- // DIAGNOSTIC: Include dev marker to verify build deployment
1502
- safeSetHeader(extRes, HTTP.HEADER.POWERED_BY, `${JAYPIE.LIB.EXPRESS}@1.2.11-dev`);
1482
+ safeSetHeader(extRes, HTTP.HEADER.POWERED_BY, JAYPIE.LIB.EXPRESS);
1503
1483
  }
1504
1484
  // X-Project-Environment
1505
1485
  if (process.env.PROJECT_ENV) {
@@ -1596,16 +1576,8 @@ function isLambdaMockResponse(res) {
1596
1576
  * Uses Symbol marker to survive prototype chain modifications from Express and dd-trace.
1597
1577
  */
1598
1578
  function isLambdaStreamingResponse(res) {
1599
- const symbolValue = res[JAYPIE_LAMBDA_STREAMING];
1600
- const result = symbolValue === true;
1601
- // DIAGNOSTIC: Log symbol check
1602
- console.log("[DIAG:expressHandler:isLambdaStreamingResponse] Check", {
1603
- symbolValue,
1604
- result,
1605
- resConstructorName: res?.constructor?.name,
1606
- hasFlushHeaders: typeof res.flushHeaders === "function",
1607
- });
1608
- return result;
1579
+ return (res[JAYPIE_LAMBDA_STREAMING] ===
1580
+ true);
1609
1581
  }
1610
1582
  /**
1611
1583
  * Safely send a JSON response, avoiding dd-trace interception.
@@ -1634,34 +1606,19 @@ function safeSendJson(res, statusCode, data) {
1634
1606
  res._resolve(res.buildResult());
1635
1607
  }
1636
1608
  // Emit "finish" event so runExpressApp's promise resolves
1637
- console.log("[safeSendJson] Emitting finish event");
1638
1609
  res.emit("finish");
1639
1610
  return;
1640
1611
  }
1641
1612
  // Fall back to standard Express methods for real responses
1642
- // DIAGNOSTIC: Log before setting status
1643
- console.log("[DIAG:expressHandler:safeSendJson] Before res.status()", {
1644
- statusCode,
1645
- currentStatusCode: res.statusCode,
1646
- });
1647
1613
  res.status(statusCode);
1648
- console.log("[DIAG:expressHandler:safeSendJson] After res.status()", {
1649
- statusCode,
1650
- newStatusCode: res.statusCode,
1651
- });
1652
1614
  // CRITICAL: For Lambda streaming responses, flush headers before send to
1653
1615
  // initialize the stream wrapper. This ensures the status code is captured
1654
1616
  // before any writes occur (which would auto-flush with default 200).
1655
1617
  // Uses Symbol marker for reliable detection that survives prototype manipulation.
1656
- const isStreaming = isLambdaStreamingResponse(res);
1657
- console.log("[DIAG:expressHandler:safeSendJson] Streaming check", {
1658
- isStreaming,
1659
- });
1660
- if (isStreaming && typeof res.flushHeaders === "function") {
1661
- console.log("[DIAG:expressHandler:safeSendJson] Calling flushHeaders explicitly");
1618
+ if (isLambdaStreamingResponse(res) &&
1619
+ typeof res.flushHeaders === "function") {
1662
1620
  res.flushHeaders();
1663
1621
  }
1664
- console.log("[DIAG:expressHandler:safeSendJson] Calling res.json()");
1665
1622
  res.json(data);
1666
1623
  }
1667
1624
  /**
@@ -1685,40 +1642,23 @@ function safeSend(res, statusCode, body) {
1685
1642
  res._resolve(res.buildResult());
1686
1643
  }
1687
1644
  // Emit "finish" event so runExpressApp's promise resolves
1688
- console.log("[safeSend] Emitting finish event");
1689
1645
  res.emit("finish");
1690
1646
  return;
1691
1647
  }
1692
1648
  // Fall back to standard Express methods for real responses
1693
- // DIAGNOSTIC: Log before setting status
1694
- console.log("[DIAG:expressHandler:safeSend] Before res.status()", {
1695
- statusCode,
1696
- currentStatusCode: res.statusCode,
1697
- hasBody: body !== undefined,
1698
- });
1699
1649
  res.status(statusCode);
1700
- console.log("[DIAG:expressHandler:safeSend] After res.status()", {
1701
- statusCode,
1702
- newStatusCode: res.statusCode,
1703
- });
1704
1650
  // CRITICAL: For Lambda streaming responses, flush headers before send to
1705
1651
  // initialize the stream wrapper. This ensures the status code is captured
1706
1652
  // before any writes occur (which would auto-flush with default 200).
1707
1653
  // Uses Symbol marker for reliable detection that survives prototype manipulation.
1708
- const isStreaming = isLambdaStreamingResponse(res);
1709
- console.log("[DIAG:expressHandler:safeSend] Streaming check", {
1710
- isStreaming,
1711
- });
1712
- if (isStreaming && typeof res.flushHeaders === "function") {
1713
- console.log("[DIAG:expressHandler:safeSend] Calling flushHeaders explicitly");
1654
+ if (isLambdaStreamingResponse(res) &&
1655
+ typeof res.flushHeaders === "function") {
1714
1656
  res.flushHeaders();
1715
1657
  }
1716
1658
  if (body !== undefined) {
1717
- console.log("[DIAG:expressHandler:safeSend] Calling res.send(body)");
1718
1659
  res.send(body);
1719
1660
  }
1720
1661
  else {
1721
- console.log("[DIAG:expressHandler:safeSend] Calling res.send() (no body)");
1722
1662
  res.send();
1723
1663
  }
1724
1664
  }