nuxt-skew-protection 0.7.2 → 0.7.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
@@ -9,7 +9,7 @@
9
9
  "version": ">=5.6.7"
10
10
  }
11
11
  },
12
- "version": "0.7.2",
12
+ "version": "0.7.3",
13
13
  "builder": {
14
14
  "@nuxt/module-builder": "1.0.2",
15
15
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -10,6 +10,7 @@ import { join } from 'node:path';
10
10
  import { tmpdir } from 'node:os';
11
11
  import { x } from 'tinyexec';
12
12
  import { defineDriver, createStorage } from 'unstorage';
13
+ import { createHash } from 'node:crypto';
13
14
  import { join as join$1, dirname } from 'pathe';
14
15
 
15
16
  const logger = useLogger("nuxt-skew-protection");
@@ -666,9 +667,10 @@ function createAssetManager(options) {
666
667
  }
667
668
  logger.debug(`restoreOldAssetsToPublic: total ${formatDuration(Date.now() - startTime)}`);
668
669
  }
669
- async function augmentBuildMetadata(buildId, publicDir) {
670
+ async function augmentBuildMetadata(buildId, publicDir, serverDir) {
670
671
  const manifest = await getVersionManifest(storage);
671
672
  const latestPath = join$1(publicDir, "_nuxt", "builds", "latest.json");
673
+ let newLatestContent;
672
674
  try {
673
675
  const latestData = await promises.readFile(latestPath, "utf-8");
674
676
  const latestJson = JSON.parse(latestData);
@@ -682,7 +684,8 @@ function createAssetManager(options) {
682
684
  latestJson.skewProtection = {
683
685
  versions: clientVersions
684
686
  };
685
- await promises.writeFile(latestPath, JSON.stringify(latestJson, null, 2), "utf-8");
687
+ newLatestContent = JSON.stringify(latestJson, null, 2);
688
+ await promises.writeFile(latestPath, newLatestContent, "utf-8");
686
689
  } catch (error) {
687
690
  if (options.debug) {
688
691
  logger.warn("Failed to augment builds/latest.json:", error);
@@ -705,6 +708,64 @@ function createAssetManager(options) {
705
708
  logger.warn(`Failed to augment builds/meta/${buildId}.json:`, error);
706
709
  }
707
710
  }
711
+ if (serverDir && newLatestContent) {
712
+ await patchNitroManifest(serverDir, "/_nuxt/builds/latest.json", newLatestContent);
713
+ }
714
+ }
715
+ async function patchNitroManifest(serverDir, assetPath, newContent) {
716
+ const nitroPath = join$1(serverDir, "chunks", "nitro", "nitro.mjs");
717
+ let nitro = await promises.readFile(nitroPath, "utf-8").catch(() => null);
718
+ if (!nitro)
719
+ return;
720
+ const assetIdx = nitro.indexOf(`"${assetPath}":`);
721
+ if (assetIdx === -1)
722
+ return;
723
+ const size = Buffer.byteLength(newContent, "utf-8");
724
+ const hash = createHash("sha1").update(newContent).digest("base64").substring(0, 27);
725
+ const newEtag = `"${size.toString(16)}-${hash}"`;
726
+ const start = nitro.indexOf("{", assetIdx);
727
+ if (start === -1)
728
+ return;
729
+ let depth = 0;
730
+ let stringChar = null;
731
+ let end = -1;
732
+ for (let i = start; i < nitro.length; i++) {
733
+ const char = nitro[i];
734
+ if (stringChar) {
735
+ if (char === "\\")
736
+ i++;
737
+ else if (char === stringChar)
738
+ stringChar = null;
739
+ } else {
740
+ if (char === '"' || char === "'") {
741
+ stringChar = char;
742
+ } else if (char === "{") {
743
+ depth++;
744
+ } else if (char === "}") {
745
+ depth--;
746
+ if (depth === 0) {
747
+ end = i;
748
+ break;
749
+ }
750
+ }
751
+ }
752
+ }
753
+ if (end === -1)
754
+ return;
755
+ let entryStr = nitro.substring(start, end + 1);
756
+ entryStr = entryStr.replace(/(['"]?)size\1\s*:\s*\d+/, (match) => {
757
+ const prefix = match.match(/^(['"]?)size\1\s*:\s*/)?.[0] || "size:";
758
+ return `${prefix}${size}`;
759
+ });
760
+ entryStr = entryStr.replace(/(['"]?)etag\1\s*:\s*(['"])(?:[^\\]|\\.)*?\2/, (match) => {
761
+ const keyMatch = match.match(/^(['"]?)etag\1\s*:\s*/);
762
+ const prefix = keyMatch?.[0] || "etag:";
763
+ const quoteChar = match.match(/:\s*(['"])/)?.[1] || '"';
764
+ const escapedEtag = quoteChar === "'" ? newEtag : newEtag.replace(/"/g, '\\"');
765
+ return `${prefix}${quoteChar}${escapedEtag}${quoteChar}`;
766
+ });
767
+ nitro = nitro.substring(0, start) + entryStr + nitro.substring(end + 1);
768
+ await promises.writeFile(nitroPath, nitro, "utf-8");
708
769
  }
709
770
  return {
710
771
  getAssetsFromBuild,
@@ -1002,7 +1063,8 @@ export {}
1002
1063
  logger.log(`Restoring build files from ${versionCount} release${versionCount > 1 ? "s" : ""} (${totalAssets} assets) [${versionSizes.join(", ")}]...`);
1003
1064
  }
1004
1065
  await assetManager.restoreOldAssetsToPublic(buildId, publicDir, assets, isExistingVersion);
1005
- await assetManager.augmentBuildMetadata(buildId, publicDir);
1066
+ const serverDir = nitro.options.output.serverDir;
1067
+ await assetManager.augmentBuildMetadata(buildId, publicDir, serverDir);
1006
1068
  });
1007
1069
  nitro.hooks.hook("close", async () => {
1008
1070
  if (assetManager) {
@@ -1,4 +1,8 @@
1
1
  import type { SkewAdapterConfig } from '../types.js';
2
2
  export type { SkewAdapterConfig };
3
- declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
3
+ declare const _default: import("nuxt/app").Plugin<{
4
+ skewConnection: import("../utils/create-skew-connection.js").SkewConnection;
5
+ }> & import("nuxt/app").ObjectPlugin<{
6
+ skewConnection: import("../utils/create-skew-connection.js").SkewConnection;
7
+ }>;
4
8
  export default _default;
@@ -6,7 +6,7 @@ export default defineNuxtPlugin({
6
6
  name: "skew-protection:adapter-updates",
7
7
  async setup() {
8
8
  if (import.meta.prerender)
9
- return;
9
+ return {};
10
10
  const nuxtApp = useNuxtApp();
11
11
  const adapterName = useRuntimeConfig().public.skewProtection.adapterName;
12
12
  const config = {
@@ -1,4 +1,8 @@
1
1
  import type { SkewSSEConfig } from '../types.js';
2
2
  export type { SkewSSEConfig };
3
- declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
3
+ declare const _default: import("nuxt/app").Plugin<{
4
+ skewConnection: import("../utils/create-skew-connection.js").SkewConnection;
5
+ }> & import("nuxt/app").ObjectPlugin<{
6
+ skewConnection: import("../utils/create-skew-connection.js").SkewConnection;
7
+ }>;
4
8
  export default _default;
@@ -6,7 +6,7 @@ export default defineNuxtPlugin({
6
6
  name: "skew-protection:sse-updates",
7
7
  async setup() {
8
8
  if (import.meta.prerender)
9
- return;
9
+ return {};
10
10
  const nuxtApp = useNuxtApp();
11
11
  const router = useRouter();
12
12
  const runtimeConfig = useRuntimeConfig();
@@ -1,4 +1,8 @@
1
1
  import type { SkewWebSocketConfig } from '../types.js';
2
2
  export type { SkewWebSocketConfig };
3
- declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
3
+ declare const _default: import("nuxt/app").Plugin<{
4
+ skewConnection: import("../utils/create-skew-connection.js").SkewConnection;
5
+ }> & import("nuxt/app").ObjectPlugin<{
6
+ skewConnection: import("../utils/create-skew-connection.js").SkewConnection;
7
+ }>;
4
8
  export default _default;
@@ -7,7 +7,7 @@ export default defineNuxtPlugin({
7
7
  name: "skew-protection:ws-updates",
8
8
  async setup() {
9
9
  if (import.meta.prerender)
10
- return;
10
+ return {};
11
11
  const nuxtApp = useNuxtApp();
12
12
  const router = useRouter();
13
13
  const runtimeConfig = useRuntimeConfig();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-skew-protection",
3
3
  "type": "module",
4
- "version": "0.7.2",
4
+ "version": "0.7.3",
5
5
  "description": "Solve Nuxt version skews with persistent assets and instant updates.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",
@@ -55,7 +55,7 @@
55
55
  "consola": "^3.4.2",
56
56
  "cookie-es": "^2.0.0",
57
57
  "defu": "^6.1.4",
58
- "nuxt-site-config": "^3.2.14",
58
+ "nuxt-site-config": "^3.2.17",
59
59
  "ofetch": "^1.5.1",
60
60
  "pathe": "^2.0.3",
61
61
  "pkg-types": "^2.3.0",
@@ -84,15 +84,15 @@
84
84
  "@unocss/preset-uno": "66.5.12",
85
85
  "@unocss/runtime": "66.5.12",
86
86
  "@vueuse/nuxt": "^14.1.0",
87
- "ably": "^2.16.0",
88
- "better-sqlite3": "^12.5.0",
87
+ "ably": "^2.17.0",
88
+ "better-sqlite3": "^12.6.0",
89
89
  "bumpp": "^10.3.2",
90
90
  "eslint": "^9.39.2",
91
91
  "eventsource": "^4.1.0",
92
92
  "execa": "^9.6.1",
93
- "firebase-functions": "^7.0.2",
94
- "happy-dom": "^20.0.11",
95
- "ioredis": "^5.9.0",
93
+ "firebase-functions": "^7.0.3",
94
+ "happy-dom": "^20.1.0",
95
+ "ioredis": "^5.9.1",
96
96
  "nuxt": "^4.2.2",
97
97
  "nuxt-auth-utils": "^0.5.26",
98
98
  "playwright-core": "^1.57.0",
@@ -100,11 +100,11 @@
100
100
  "tsx": "^4.21.0",
101
101
  "typescript": "^5.9.3",
102
102
  "unocss": "66.5.12",
103
- "vitest": "^4.0.16",
103
+ "vitest": "^4.0.17",
104
104
  "vue": "^3.5.26",
105
105
  "vue-router": "^4.6.4",
106
106
  "vue-tsc": "^3.2.2",
107
- "wrangler": "^4.54.0",
107
+ "wrangler": "^4.58.0",
108
108
  "ws": "^8.19.0"
109
109
  },
110
110
  "resolutions": {