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