@server/next 0.21.8 → 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 +1 -1
- package/src/helpers/config.js +3 -0
- package/src/helpers/cors.js +19 -0
- package/src/index.js +2 -1
- package/src/middle/index.js +2 -1
- package/src/middle/timer.js +14 -0
- package/src/parseResponse.js +8 -3
- package/src/router.test.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@server/next",
|
|
3
|
-
"version": "0.21.
|
|
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",
|
package/src/helpers/config.js
CHANGED
|
@@ -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
|
+
}
|
package/src/index.js
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
parseHeaders,
|
|
12
12
|
} from "./helpers/index.js";
|
|
13
13
|
|
|
14
|
-
import { assets, auth } from "./middle/index.js";
|
|
14
|
+
import { assets, auth, timer } from "./middle/index.js";
|
|
15
15
|
|
|
16
16
|
// Export the reply helpers
|
|
17
17
|
export * from "./reply.js";
|
|
@@ -74,6 +74,7 @@ export default function server(options = {}) {
|
|
|
74
74
|
this.node();
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
this.use(timer);
|
|
77
78
|
this.use(assets);
|
|
78
79
|
if (this.opts.auth) {
|
|
79
80
|
this.use(auth({ options: this.opts, app: this }));
|
package/src/middle/index.js
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default function timer(ctx) {
|
|
2
|
+
const times = [["init", performance.now()]];
|
|
3
|
+
ctx.time = (name) => times.push([name, performance.now()]);
|
|
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
|
+
};
|
|
14
|
+
}
|
package/src/parseResponse.js
CHANGED
|
@@ -41,9 +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
|
-
|
|
45
|
-
out.headers.
|
|
46
|
-
|
|
44
|
+
// Set the proper CORS headers
|
|
45
|
+
cors(out.headers, ctx.options.cors, ctx);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Only attach the headers if the user is using the timing API
|
|
49
|
+
// 1 item is the `init` so it doesn't count
|
|
50
|
+
if (ctx.time.times.length > 1) {
|
|
51
|
+
out.headers.set("Server-Timing", ctx.time.headers());
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
// If we have a session, we need to persist it into a cookie
|
package/src/router.test.js
CHANGED
|
@@ -15,7 +15,7 @@ describe("can route properly", () => {
|
|
|
15
15
|
// INTERNAL - so this might change in the future
|
|
16
16
|
it("has the correct structure", () => {
|
|
17
17
|
const registeredPaths = app.handlers.get.map((h) => h[1]);
|
|
18
|
-
expect(registeredPaths).toEqual(["*", "/hello", "/api/hello", "/"]);
|
|
18
|
+
expect(registeredPaths).toEqual(["*", "*", "/hello", "/api/hello", "/"]);
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
it("can get fallback when nothing matches", async () => {
|