@server/next 0.21.9 → 0.21.10

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.9",
3
+ "version": "0.21.10",
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",
@@ -21,6 +21,9 @@ export default function config(options = {}) {
21
21
  if (options.cors && !options.cors.methods) {
22
22
  options.cors.methods = "GET,HEAD,POST,PUT,PATCH";
23
23
  }
24
+ if (options.cors && !options.cors.headers) {
25
+ options.cors.methods = "*";
26
+ }
24
27
 
25
28
  // Bucket
26
29
  options.views = options.views ? Bucket(options.views) : null;
@@ -0,0 +1,19 @@
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);
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
+ }
9
+
10
+ if (options.origin === "*") {
11
+ return headers.set("Access-Control-Allow-Origin", "*");
12
+ }
13
+
14
+ if (options.origin.toLowerCase().split(/\,\s/g).includes(origin)) {
15
+ return headers.set("Access-Control-Allow-Origin", origin);
16
+ }
17
+
18
+ throw new Error(`Invalid origin ${origin}`);
19
+ }
@@ -2,4 +2,13 @@ export default function timer(ctx) {
2
2
  const times = [["init", performance.now()]];
3
3
  ctx.time = (name) => times.push([name, performance.now()]);
4
4
  ctx.time.times = times;
5
+ ctx.time.headers = () => {
6
+ const r = (t) => Math.round(t);
7
+ const times = ctx.time.times;
8
+ const timing = times
9
+ .slice(1)
10
+ .map(([name, time], i) => `${name};dur=${r(time - times[i][1])}`)
11
+ .join(", ");
12
+ return timing;
13
+ };
5
14
  }
@@ -41,21 +41,14 @@ export default async function parseResponse(out, ctx) {
41
41
 
42
42
  // If we have CORS, set it up
43
43
  if (ctx.options.cors) {
44
- const { origin, methods } = ctx.options.cors;
45
- out.headers.set("Access-Control-Allow-Origin", origin);
46
- out.headers.set("Access-Control-Allow-Methods", methods);
44
+ // Set the proper CORS headers
45
+ cors(out.headers, ctx.options.cors, ctx);
47
46
  }
48
47
 
49
48
  // Only attach the headers if the user is using the timing API
50
49
  // 1 item is the `init` so it doesn't count
51
50
  if (ctx.time.times.length > 1) {
52
- const r = (t) => Math.round(t);
53
- const times = ctx.time.times;
54
- const timing = times
55
- .slice(1)
56
- .map(([name, time], i) => `${name};dur=${r(time - times[i][1])}`)
57
- .join(", ");
58
- out.headers.set("Server-Timing", timing);
51
+ out.headers.set("Server-Timing", ctx.time.headers());
59
52
  }
60
53
 
61
54
  // If we have a session, we need to persist it into a cookie