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