nuxt-skew-protection 2.0.1 → 2.0.3

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/dist/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "nuxt": "^4.6.0 || ^5.0.0-0"
5
5
  },
6
6
  "configKey": "skewProtection",
7
- "version": "2.0.1",
7
+ "version": "2.0.3",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.3",
10
10
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -1153,7 +1153,7 @@ export {}
1153
1153
  for (const { route, seconds, source, fromMaxAgeAlone } of caching) {
1154
1154
  if (seconds > ceiling)
1155
1155
  logger.warn(`routeRules['${route}'] (${source}) caches a document for ${seconds}s. Retention only keeps a build for ${ceiling}s. The cached document will outlive its chunks. Lower the window to ${ceiling}s or less.`);
1156
- if (fromMaxAgeAlone)
1156
+ if (fromMaxAgeAlone && options.cookie !== false)
1157
1157
  logger.warn(`routeRules['${route}'] caches documents with \`max-age\` and no \`s-maxage\`. The version cookie is dropped there, so \`isClientOutdated\` falls back to the build id. Use \`s-maxage\` for CDN caching, or \`private\` if you meant browser caching only.`);
1158
1158
  }
1159
1159
  }
@@ -10,8 +10,11 @@ function getInternal(peer) {
10
10
  function getCtx(peer) {
11
11
  return getInternal(peer)?.durable?.ctx || null;
12
12
  }
13
+ function getOpenWebSockets(ctx) {
14
+ return ctx.getWebSockets().filter((ws) => ws.readyState === WebSocket.OPEN);
15
+ }
13
16
  function getStats(ctx, forId) {
14
- const websockets = ctx.getWebSockets();
17
+ const websockets = getOpenWebSockets(ctx);
15
18
  const versions = {};
16
19
  const routes = {};
17
20
  const connections = [];
@@ -27,10 +30,10 @@ function getStats(ctx, forId) {
27
30
  return { total: websockets.length, versions, routes, connections, yourId: forId };
28
31
  }
29
32
  function broadcastToSubscribers(ctx) {
30
- const websockets = ctx.getWebSockets();
33
+ const websockets = getOpenWebSockets(ctx);
31
34
  const subscribers = websockets.filter((ws) => {
32
35
  const attachment = ws.deserializeAttachment?.() || {};
33
- return attachment.s === true;
36
+ return attachment.stats?._tag === "Authorized";
34
37
  });
35
38
  if (subscribers.length === 0)
36
39
  return;
@@ -67,8 +70,10 @@ export default defineNitroPlugin((nitroApp) => {
67
70
  nitroApp.hooks.hook("skew:subscribe-stats", async ({ id, event, peer }) => {
68
71
  const ws = getInternal(peer)?.ws;
69
72
  const ctx = getCtx(peer);
70
- if (!ws || !ctx)
73
+ if (!ws || !ctx || ws.readyState !== WebSocket.OPEN)
71
74
  return;
75
+ const request = crypto.randomUUID();
76
+ ws.serializeAttachment?.({ ...ws.deserializeAttachment?.() || {}, stats: { _tag: "Pending", request } });
72
77
  let authorized = false;
73
78
  await nitroApp.hooks.callHook("skew:authorize-stats", {
74
79
  event,
@@ -76,10 +81,14 @@ export default defineNitroPlugin((nitroApp) => {
76
81
  authorized = true;
77
82
  }
78
83
  });
84
+ const attachment = ws.deserializeAttachment?.();
85
+ if (ws.readyState !== WebSocket.OPEN || attachment?.stats?._tag !== "Pending" || attachment.stats.request !== request)
86
+ return;
79
87
  if (authorized) {
80
- ws.serializeAttachment?.({ ...ws.deserializeAttachment?.() || {}, s: true });
88
+ ws.serializeAttachment?.({ ...attachment, stats: { _tag: "Authorized" } });
81
89
  ws.send(JSON.stringify({ type: "stats", ...getStats(ctx, id) }));
82
90
  } else {
91
+ ws.serializeAttachment?.({ ...attachment, stats: void 0 });
83
92
  ws.send(JSON.stringify({ type: "stats-unauthorized" }));
84
93
  }
85
94
  });
@@ -1,7 +1,7 @@
1
1
  import { defineNitroPlugin } from "#nuxtseo/nitro";
2
2
  export default defineNitroPlugin((nitroApp) => {
3
3
  const connections = /* @__PURE__ */ new Map();
4
- const statsSubscribers = /* @__PURE__ */ new Set();
4
+ const statsSubscriptions = /* @__PURE__ */ new Map();
5
5
  const getStats = (forId) => {
6
6
  const versions = {};
7
7
  const routes = {};
@@ -14,9 +14,11 @@ export default defineNitroPlugin((nitroApp) => {
14
14
  return { total: connections.size, versions, routes, connections: connectionList, yourId: forId };
15
15
  };
16
16
  const broadcastToSubscribers = () => {
17
- if (statsSubscribers.size === 0)
17
+ if (statsSubscriptions.size === 0)
18
18
  return;
19
- for (const id of statsSubscribers) {
19
+ for (const [id, subscription] of statsSubscriptions) {
20
+ if (subscription._tag !== "Authorized")
21
+ continue;
20
22
  const conn = connections.get(id);
21
23
  conn?.send({ type: "stats", ...getStats(id) });
22
24
  }
@@ -37,10 +39,15 @@ export default defineNitroPlugin((nitroApp) => {
37
39
  });
38
40
  nitroApp.hooks.hook("skew:connection:close", ({ id }) => {
39
41
  connections.delete(id);
40
- statsSubscribers.delete(id);
42
+ statsSubscriptions.delete(id);
41
43
  broadcastToSubscribers();
42
44
  });
43
45
  nitroApp.hooks.hook("skew:subscribe-stats", async ({ id, event }) => {
46
+ const conn = connections.get(id);
47
+ if (!conn)
48
+ return;
49
+ const pending = { _tag: "Pending" };
50
+ statsSubscriptions.set(id, pending);
44
51
  let authorized = false;
45
52
  await nitroApp.hooks.callHook("skew:authorize-stats", {
46
53
  id,
@@ -49,13 +56,14 @@ export default defineNitroPlugin((nitroApp) => {
49
56
  authorized = true;
50
57
  }
51
58
  });
59
+ if (statsSubscriptions.get(id) !== pending || connections.get(id) !== conn)
60
+ return;
52
61
  if (authorized) {
53
- statsSubscribers.add(id);
54
- const conn = connections.get(id);
55
- conn?.send({ type: "stats", ...getStats(id) });
62
+ statsSubscriptions.set(id, { _tag: "Authorized" });
63
+ conn.send({ type: "stats", ...getStats(id) });
56
64
  } else {
57
- const conn = connections.get(id);
58
- conn?.send({ type: "stats-unauthorized" });
65
+ statsSubscriptions.delete(id);
66
+ conn.send({ type: "stats-unauthorized" });
59
67
  }
60
68
  });
61
69
  });
@@ -2,7 +2,7 @@ import { defineEventHandler, readBody } from "nuxt/server";
2
2
  import { useNitroApp } from "#nuxtseo/nitro";
3
3
  export default defineEventHandler(async (event) => {
4
4
  const body = await readBody(event);
5
- if (!body?.connectionId || !body?.route) {
5
+ if (!body || typeof body !== "object" || !("connectionId" in body) || typeof body.connectionId !== "string" || !body.connectionId || !("route" in body) || typeof body.route !== "string" || !body.route) {
6
6
  return { ok: false };
7
7
  }
8
8
  await useNitroApp().hooks.callHook("skew:connection:route-update", {
@@ -31,7 +31,7 @@ export default defineWebSocketHandler({
31
31
  },
32
32
  message(peer, message) {
33
33
  const text = message.text();
34
- if (text.includes("ping")) {
34
+ if (text === SKEW_MESSAGE_TYPE.PING) {
35
35
  peer.send(JSON.stringify({ type: "pong" }));
36
36
  return;
37
37
  }
@@ -41,7 +41,11 @@ export default defineWebSocketHandler({
41
41
  } catch {
42
42
  return;
43
43
  }
44
- if (data.type === SKEW_MESSAGE_TYPE.ROUTE_UPDATE && data.route) {
44
+ if (!data || typeof data !== "object" || !("type" in data))
45
+ return;
46
+ if (data.type === SKEW_MESSAGE_TYPE.PING) {
47
+ peer.send(JSON.stringify({ type: "pong" }));
48
+ } else if (data.type === SKEW_MESSAGE_TYPE.ROUTE_UPDATE && "route" in data && typeof data.route === "string" && data.route) {
45
49
  useNitroApp().hooks.callHook("skew:connection:route-update", {
46
50
  id: peer.id,
47
51
  route: data.route,
@@ -56,6 +60,7 @@ export default defineWebSocketHandler({
56
60
  }
57
61
  },
58
62
  close(peer) {
59
- useNitroApp().hooks.callHook("skew:connection:close", { id: peer.id, peer });
63
+ peer.close();
64
+ return useNitroApp().hooks.callHook("skew:connection:close", { id: peer.id, peer });
60
65
  }
61
66
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-skew-protection",
3
3
  "type": "module",
4
- "version": "2.0.1",
4
+ "version": "2.0.3",
5
5
  "description": "Solve Nuxt version skews with persistent assets and instant updates.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",