@jaypie/express 1.2.11-dev.6 → 1.2.12

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
@@ -854,8 +854,6 @@ class LambdaResponseStreaming extends Writable {
854
854
  for (const [key, value] of this._headers) {
855
855
  headers[key] = Array.isArray(value) ? value.join(", ") : String(value);
856
856
  }
857
- // Add version header for deployment verification
858
- headers["x-jaypie-streaming"] = "1.2.11-dev.6";
859
857
  // Lambda streaming requires body content for metadata to be transmitted.
860
858
  // Convert 204 No Content to 200 OK with empty JSON body as workaround.
861
859
  // See: https://github.com/finlaysonstudio/jaypie/issues/178
@@ -1613,15 +1611,22 @@ function safeSendJson(res, statusCode, data) {
1613
1611
  }
1614
1612
  // Fall back to standard Express methods for real responses
1615
1613
  res.status(statusCode);
1616
- // CRITICAL: For Lambda streaming responses, flush headers before send to
1617
- // initialize the stream wrapper. This ensures the status code is captured
1618
- // before any writes occur (which would auto-flush with default 200).
1614
+ // CRITICAL: For Lambda streaming responses, set content-type BEFORE flushing
1615
+ // headers, then flush to initialize the stream wrapper with all headers.
1619
1616
  // Uses Symbol marker for reliable detection that survives prototype manipulation.
1620
- if (isLambdaStreamingResponse(res) &&
1621
- typeof res.flushHeaders === "function") {
1622
- res.flushHeaders();
1617
+ if (isLambdaStreamingResponse(res)) {
1618
+ // Set content-type before flushing so it's included in the metadata
1619
+ res.setHeader("content-type", "application/json");
1620
+ if (typeof res.flushHeaders === "function") {
1621
+ res.flushHeaders();
1622
+ }
1623
+ // Write JSON directly and end the stream (don't call res.json which would
1624
+ // try to set content-type again after headers are already sent)
1625
+ res.end(JSON.stringify(data));
1626
+ }
1627
+ else {
1628
+ res.json(data);
1623
1629
  }
1624
- res.json(data);
1625
1630
  }
1626
1631
  /**
1627
1632
  * Safely send a response body, avoiding dd-trace interception.
@@ -1653,15 +1658,25 @@ function safeSend(res, statusCode, body) {
1653
1658
  // initialize the stream wrapper. This ensures the status code is captured
1654
1659
  // before any writes occur (which would auto-flush with default 200).
1655
1660
  // Uses Symbol marker for reliable detection that survives prototype manipulation.
1656
- if (isLambdaStreamingResponse(res) &&
1657
- typeof res.flushHeaders === "function") {
1658
- res.flushHeaders();
1659
- }
1660
- if (body !== undefined) {
1661
- res.send(body);
1661
+ if (isLambdaStreamingResponse(res)) {
1662
+ if (typeof res.flushHeaders === "function") {
1663
+ res.flushHeaders();
1664
+ }
1665
+ // Write directly to stream and end (bypass Express send middleware)
1666
+ if (body !== undefined) {
1667
+ res.end(body);
1668
+ }
1669
+ else {
1670
+ res.end();
1671
+ }
1662
1672
  }
1663
1673
  else {
1664
- res.send();
1674
+ if (body !== undefined) {
1675
+ res.send(body);
1676
+ }
1677
+ else {
1678
+ res.send();
1679
+ }
1665
1680
  }
1666
1681
  }
1667
1682
  function expressHandler(handlerOrOptions, optionsOrHandler) {