@server/next 0.21.11 → 0.21.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/package.json +1 -1
- package/src/helpers/config.js +3 -0
- package/src/helpers/cors.js +16 -11
- package/src/parseResponse.js +6 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@server/next",
|
|
3
|
-
"version": "0.21.
|
|
3
|
+
"version": "0.21.12",
|
|
4
4
|
"description": "A fully-fledged web server with routing, file uploads, sessions, static files, schema validation, websockets, testing, etc.",
|
|
5
5
|
"homepage": "https://server-js.com/",
|
|
6
6
|
"repository": "https://github.com/franciscop/server-next.git",
|
package/src/helpers/config.js
CHANGED
|
@@ -18,6 +18,9 @@ export default function config(options = {}) {
|
|
|
18
18
|
if (typeof options.cors === "string") {
|
|
19
19
|
options.cors = { origin: options.cors };
|
|
20
20
|
}
|
|
21
|
+
if (options.cors && typeof options.cors.origin === "string") {
|
|
22
|
+
options.cors.origin = options.cors.origin.toLowerCase();
|
|
23
|
+
}
|
|
21
24
|
if (options.cors && !options.cors.methods) {
|
|
22
25
|
options.cors.methods = "GET,HEAD,POST,PUT,PATCH";
|
|
23
26
|
}
|
package/src/helpers/cors.js
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
|
-
export default function cors(
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export default function cors(configOrigin, origin = "") {
|
|
2
|
+
// A star should always return a star
|
|
3
|
+
if (configOrigin === "*") return "*";
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
origin = origin.toLowerCase();
|
|
6
|
+
|
|
7
|
+
// No origin
|
|
8
|
+
if (!origin) {
|
|
9
|
+
console.warn("CORS: Missing Origin header");
|
|
10
|
+
return null;
|
|
8
11
|
}
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
// Coming from localhost
|
|
14
|
+
if (/^https?:\/\/localhost(:\d+)?$/.test(origin)) {
|
|
15
|
+
return origin;
|
|
12
16
|
}
|
|
13
17
|
|
|
14
|
-
if (
|
|
15
|
-
return
|
|
18
|
+
if (configOrigin.toLowerCase().split(/\,\s/g).includes(origin)) {
|
|
19
|
+
return origin;
|
|
16
20
|
}
|
|
17
21
|
|
|
18
|
-
|
|
22
|
+
console.warn(`CORS: Origin "${origin}" is not allowed`);
|
|
23
|
+
return null;
|
|
19
24
|
}
|
package/src/parseResponse.js
CHANGED
|
@@ -42,7 +42,12 @@ export default async function parseResponse(out, ctx) {
|
|
|
42
42
|
// If we have CORS, set it up
|
|
43
43
|
if (ctx.options.cors) {
|
|
44
44
|
// Set the proper CORS headers
|
|
45
|
-
cors(
|
|
45
|
+
const origin = cors(ctx.options.cors, ctx.headers.origin);
|
|
46
|
+
if (origin) {
|
|
47
|
+
headers.set("Access-Control-Allow-Methods", options.methods);
|
|
48
|
+
headers.set("Access-Control-Allow-Headers", options.headers);
|
|
49
|
+
headers.set("Access-Control-Allow-Origin", origin);
|
|
50
|
+
}
|
|
46
51
|
}
|
|
47
52
|
|
|
48
53
|
// Only attach the headers if the user is using the timing API
|