@server/next 0.20.13 → 0.20.15
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 +1 -1
- package/src/helpers/index.js +2 -1
- package/src/helpers/toWeb.js +15 -0
- package/src/index.js +9 -0
- package/src/parseResponse.js +2 -3
- package/src/reply.js +13 -2
package/package.json
CHANGED
package/src/helpers/index.js
CHANGED
|
@@ -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/index.js
CHANGED
|
@@ -51,6 +51,15 @@ const validateOptions = (options, env = {}) => {
|
|
|
51
51
|
options.port = options.port || env.PORT || 3000;
|
|
52
52
|
options.secret = options.secret || env.SECRET || "unsafe-" + createId();
|
|
53
53
|
options.cors = options.cors || env.CORS || null;
|
|
54
|
+
if (options.cors === true) {
|
|
55
|
+
options.cors = { origin: options.domain || "*" };
|
|
56
|
+
}
|
|
57
|
+
if (typeof options.cors === "string") {
|
|
58
|
+
options.cors = { origin: options.cors };
|
|
59
|
+
}
|
|
60
|
+
if (options.cors && !options.cors.methods) {
|
|
61
|
+
options.cors.methods = "GET,HEAD,POST,PUT,PATCH";
|
|
62
|
+
}
|
|
54
63
|
|
|
55
64
|
options.views = options.views ? Bucket(options.views) : null;
|
|
56
65
|
options.public = options.public ? Bucket(options.public) : null;
|
package/src/parseResponse.js
CHANGED
|
@@ -40,9 +40,8 @@ export default async function parseResponse(out, ctx) {
|
|
|
40
40
|
// Here it should be a Response
|
|
41
41
|
|
|
42
42
|
// If we have CORS, set it up
|
|
43
|
-
if (
|
|
44
|
-
const origin = ctx.options.cors
|
|
45
|
-
const methods = "GET,HEAD,POST,PUT,PATCH";
|
|
43
|
+
if (ctx.options.cors) {
|
|
44
|
+
const { origin, methods } = ctx.options.cors;
|
|
46
45
|
out.headers.set("Access-Control-Allow-Origin", origin);
|
|
47
46
|
out.headers.set("Access-Control-Allow-Methods", methods);
|
|
48
47
|
}
|
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
|
-
|
|
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
|
};
|