@server/next 0.21.8 → 0.21.9

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.8",
3
+ "version": "0.21.9",
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/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 }));
@@ -1,5 +1,6 @@
1
1
  import authMod from "../auth/index.js";
2
2
  import assets from "./assets.js";
3
+ import timer from "./timer.js";
3
4
 
4
5
  const auth = authMod.middle;
5
- export { assets, auth };
6
+ export { assets, auth, timer };
@@ -0,0 +1,5 @@
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
+ }
@@ -46,6 +46,18 @@ export default async function parseResponse(out, ctx) {
46
46
  out.headers.set("Access-Control-Allow-Methods", methods);
47
47
  }
48
48
 
49
+ // Only attach the headers if the user is using the timing API
50
+ // 1 item is the `init` so it doesn't count
51
+ 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);
59
+ }
60
+
49
61
  // If we have a session, we need to persist it into a cookie
50
62
  if (Object.keys(ctx.session || {}).length) {
51
63
  if (!ctx.options.session?.store) {
@@ -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 () => {