@server/next 0.20.13 → 0.20.14

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.20.13",
3
+ "version": "0.20.14",
4
4
  "description": "An experimental reimplementation of server.js focused on the DX",
5
5
  "homepage": "https://node-server.com/",
6
6
  "repository": "https://github.com/franciscop/server-next.git",
@@ -1,8 +1,9 @@
1
- export { default as createId } from "./createId.js";
2
1
  export { default as createCookies } from "./createCookies.js";
2
+ export { default as createId } from "./createId.js";
3
3
  export { default as define } from "./define.js";
4
4
  export { default as getMachine } from "./getMachine.js";
5
5
  export { default as handleRequest } from "./handleRequest.js";
6
6
  export { default as iterate } from "./iterate.js";
7
7
  export { default as parseHeaders } from "./parseHeaders.js";
8
+ export { default as toWeb } from "./toWeb.js";
8
9
  export { default as types } from "./types.js";
@@ -0,0 +1,15 @@
1
+ export default function toWeb(nodeStream) {
2
+ if (typeof ReadableStream === "undefined") {
3
+ throw new Error("Environment not supported, please report this as a bug");
4
+ }
5
+ return new ReadableStream({
6
+ start(controller) {
7
+ nodeStream.on("data", (chunk) => controller.enqueue(chunk));
8
+ nodeStream.on("end", () => controller.close());
9
+ nodeStream.on("error", (err) => controller.error(err));
10
+ },
11
+ cancel() {
12
+ nodeStream.destroy();
13
+ },
14
+ });
15
+ }
package/src/reply.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import fs from "fs/promises";
2
2
 
3
- import { createCookies, types } from "./helpers/index.js";
3
+ import { createCookies, toWeb, types } from "./helpers/index.js";
4
4
 
5
5
  function Reply() {
6
6
  this.res = {
@@ -98,11 +98,22 @@ Reply.prototype.send = function (body = "") {
98
98
  return new Response(body, { status, headers });
99
99
  }
100
100
 
101
- if (body?.constructor?.name === "Buffer") {
101
+ const name = body?.constructor?.name;
102
+ if (name === "Buffer") {
102
103
  const headers = this.generateHeaders();
103
104
  return new Response(body, { status, headers });
104
105
  }
105
106
 
107
+ // WebStream already, just pass it through
108
+ if (name === "ReadableStream") {
109
+ return new Response(body, { status, headers });
110
+ }
111
+
112
+ // Node stream, convert it to web stream
113
+ if (name === "PassThrough" || name === "Readable") {
114
+ return new Response(toWeb(body), { status, headers });
115
+ }
116
+
106
117
  // This is a bit loopy, send({}) => json({}) => send('{}')
107
118
  return this.json(body);
108
119
  };