@server/next 0.21.11 → 0.21.13

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.11",
3
+ "version": "0.21.13",
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",
@@ -12,17 +12,22 @@ export default function config(options = {}) {
12
12
 
13
13
  // CORS
14
14
  options.cors = options.cors || env.CORS || null;
15
- if (options.cors === true) {
16
- options.cors = { origin: options.domain || "*" };
17
- }
18
- if (typeof options.cors === "string") {
19
- options.cors = { origin: options.cors };
20
- }
21
- if (options.cors && !options.cors.methods) {
22
- options.cors.methods = "GET,HEAD,POST,PUT,PATCH";
23
- }
24
- if (options.cors && !options.cors.headers) {
25
- options.cors.methods = "*";
15
+ if (options.cors) {
16
+ if (options.cors === true) {
17
+ options.cors = { origin: true };
18
+ }
19
+ if (typeof options.cors === "string") {
20
+ options.cors = { origin: options.cors };
21
+ }
22
+ if (typeof options.cors.origin === "string") {
23
+ options.cors.origin = options.cors.origin.toLowerCase();
24
+ }
25
+ if (!options.cors.methods) {
26
+ options.cors.methods = "GET,HEAD,POST,PUT,PATCH";
27
+ }
28
+ if (!options.cors.headers) {
29
+ options.cors.methods = "*";
30
+ }
26
31
  }
27
32
 
28
33
  // Bucket
@@ -1,19 +1,32 @@
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
+ // Based on https://expressjs.com/en/resources/middleware/cors.html#configuration-options
2
+ export default function cors(configOrigin, origin = "") {
3
+ origin = origin.toLowerCase();
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);
8
- }
5
+ // When it's true, reflect the origin
6
+ if (configOrigin === true) return origin || null;
7
+
8
+ // A star should always return a star
9
+ if (configOrigin === "*") return "*";
10
+
11
+ // No origin, it's okay since that means we don't need CORS
12
+ if (!origin) return null;
9
13
 
10
- if (options.origin === "*") {
11
- return headers.set("Access-Control-Allow-Origin", "*");
14
+ // Coming from localhost
15
+ if (/^https?:\/\/localhost(:\d+)?$/.test(origin)) {
16
+ return origin;
12
17
  }
13
18
 
14
- if (options.origin.toLowerCase().split(/\,\s/g).includes(origin)) {
15
- return headers.set("Access-Control-Allow-Origin", origin);
19
+ if (
20
+ configOrigin
21
+ .toLowerCase()
22
+ .split(/\s*,\s*/g)
23
+ .includes(origin)
24
+ ) {
25
+ return origin;
16
26
  }
17
27
 
18
- throw new Error(`Invalid origin ${origin}`);
28
+ console.warn(
29
+ `CORS: Origin "${origin}" not allowed. Allowed origins: ${configOrigin}`,
30
+ );
31
+ return null;
19
32
  }
@@ -42,7 +42,15 @@ 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.origin, ctx.headers.origin);
46
+ if (origin) {
47
+ headers.set("Access-Control-Allow-Methods", ctx.options.cors.methods);
48
+ headers.set("Access-Control-Allow-Headers", ctx.options.cors.headers);
49
+ headers.set("Access-Control-Allow-Origin", origin);
50
+ if (ctx.options.cors.credentials) {
51
+ headers.set("Access-Control-Allow-Credentials", "true");
52
+ }
53
+ }
46
54
  }
47
55
 
48
56
  // Only attach the headers if the user is using the timing API