@server/next 0.21.10 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.21.10",
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",
@@ -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
  }
@@ -1,19 +1,24 @@
1
- export default function cors(headers, options, ctx) {
2
- headers.set("Access-Control-Allow-Methods", options.methods);
3
- headers.set("Access-Control-Allow-Headers", options.headers);
1
+ export default function cors(configOrigin, origin = "") {
2
+ // A star should always return a star
3
+ if (configOrigin === "*") return "*";
4
4
 
5
- const origin = ctx.headers.origin.toLowerCase();
6
- if (!ctx.platform.production && /http:\/\/localhost:\d+/.test(origin)) {
7
- return headers.set("Access-Control-Allow-Origin", origin);
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
- if (options.origin === "*") {
11
- return headers.set("Access-Control-Allow-Origin", "*");
13
+ // Coming from localhost
14
+ if (/^https?:\/\/localhost(:\d+)?$/.test(origin)) {
15
+ return origin;
12
16
  }
13
17
 
14
- if (options.origin.toLowerCase().split(/\,\s/g).includes(origin)) {
15
- return headers.set("Access-Control-Allow-Origin", origin);
18
+ if (configOrigin.toLowerCase().split(/\,\s/g).includes(origin)) {
19
+ return origin;
16
20
  }
17
21
 
18
- throw new Error(`Invalid origin ${origin}`);
22
+ console.warn(`CORS: Origin "${origin}" is not allowed`);
23
+ return null;
19
24
  }
@@ -1,6 +1,7 @@
1
1
  export { default as createCookies } from "./createCookies.js";
2
2
  export { default as createId } from "./createId.js";
3
3
  export { default as config } from "./config.js";
4
+ export { default as cors } from "./cors.js";
4
5
  export { default as define } from "./define.js";
5
6
  export { default as getMachine } from "./getMachine.js";
6
7
  export { default as handleRequest } from "./handleRequest.js";
@@ -1,6 +1,6 @@
1
1
  // import { Readable } from "node:stream";
2
2
 
3
- import { createCookies, createId } from "./helpers/index.js";
3
+ import { cors, createCookies, createId } from "./helpers/index.js";
4
4
  import { json } from "./reply.js";
5
5
  import ServerError from "./ServerError.js";
6
6
 
@@ -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(out.headers, ctx.options.cors, ctx);
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