@server/next 0.20.12 → 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 +1 -1
- package/src/helpers/index.js +2 -1
- package/src/helpers/toWeb.js +15 -0
- package/src/index.js +1 -0
- package/src/middle/assets.js +2 -1
- package/src/parseResponse.js +8 -0
- package/src/pathPattern.js +2 -0
- 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
|
@@ -50,6 +50,7 @@ const createNodeServer = async (app, options) => {
|
|
|
50
50
|
const validateOptions = (options, env = {}) => {
|
|
51
51
|
options.port = options.port || env.PORT || 3000;
|
|
52
52
|
options.secret = options.secret || env.SECRET || "unsafe-" + createId();
|
|
53
|
+
options.cors = options.cors || env.CORS || null;
|
|
53
54
|
|
|
54
55
|
options.views = options.views ? Bucket(options.views) : null;
|
|
55
56
|
options.public = options.public ? Bucket(options.public) : null;
|
package/src/middle/assets.js
CHANGED
|
@@ -3,7 +3,8 @@ import { type } from "../reply.js";
|
|
|
3
3
|
export default async function assets(ctx) {
|
|
4
4
|
try {
|
|
5
5
|
// TODO: streaming
|
|
6
|
-
|
|
6
|
+
// Read it as buffer (null)
|
|
7
|
+
const asset = await ctx.options.public.read(ctx.url.pathname, null);
|
|
7
8
|
if (!asset) return;
|
|
8
9
|
return type(ctx.url.pathname.split(".").pop()).send(asset);
|
|
9
10
|
} catch (error) {
|
package/src/parseResponse.js
CHANGED
|
@@ -39,6 +39,14 @@ export default async function parseResponse(out, ctx) {
|
|
|
39
39
|
|
|
40
40
|
// Here it should be a Response
|
|
41
41
|
|
|
42
|
+
// If we have CORS, set it up
|
|
43
|
+
if (typeof ctx.options.cors === "string") {
|
|
44
|
+
const origin = ctx.options.cors || ctx.options.domain || "*";
|
|
45
|
+
const methods = "GET,HEAD,POST,PUT,PATCH";
|
|
46
|
+
out.headers.set("Access-Control-Allow-Origin", origin);
|
|
47
|
+
out.headers.set("Access-Control-Allow-Methods", methods);
|
|
48
|
+
}
|
|
49
|
+
|
|
42
50
|
// If we have a session, we need to persist it into a cookie
|
|
43
51
|
if (Object.keys(ctx.session || {}).length) {
|
|
44
52
|
if (!ctx.options.session?.store) {
|
package/src/pathPattern.js
CHANGED
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
|
};
|