nuxt-skew-protection 0.3.4 → 0.3.6

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.0.0"
5
5
  },
6
6
  "configKey": "skewProtection",
7
- "version": "0.3.4",
7
+ "version": "0.3.6",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.2",
10
10
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -100,8 +100,11 @@ stderr: ${result.stderr}`
100
100
  const prefixedKey = prefixKey(key);
101
101
  const tmpFile = join(tmpdir(), `kv-${Date.now()}.bin`);
102
102
  writeFileSync(tmpFile, value);
103
- await execWrangler(`wrangler kv key put "${prefixedKey}" --path="${tmpFile}" --namespace-id="${namespaceId}" ${locationFlag}`);
104
- unlinkSync(tmpFile);
103
+ try {
104
+ await execWrangler(`wrangler kv key put "${prefixedKey}" --path="${tmpFile}" --namespace-id="${namespaceId}" ${locationFlag}`);
105
+ } finally {
106
+ unlinkSync(tmpFile);
107
+ }
105
108
  },
106
109
  async removeItem(key) {
107
110
  const prefixedKey = prefixKey(key);
@@ -182,6 +185,15 @@ async function resolveBuildTimeDriver(storage) {
182
185
  return lazyDriver({ base, ...driverOptions });
183
186
  }
184
187
 
188
+ async function processBatch(items, batchSize, processor) {
189
+ const results = [];
190
+ for (let i = 0; i < items.length; i += batchSize) {
191
+ const batch = items.slice(i, i + batchSize);
192
+ const batchResults = await Promise.all(batch.map(processor));
193
+ results.push(...batchResults);
194
+ }
195
+ return results;
196
+ }
185
197
  function extractFileId(assetPath) {
186
198
  const filename = assetPath.split("/").pop();
187
199
  if (!filename)
@@ -285,14 +297,13 @@ function createAssetManager(options) {
285
297
  continue;
286
298
  }
287
299
  if (isExpired || exceedsCount) {
288
- const assetPromises = version.assets.map(
289
- (asset) => storage.removeItem(`${version.id}/${asset}`).catch((error) => {
300
+ await processBatch(version.assets, 50, async (asset) => {
301
+ await storage.removeItem(`${version.id}/${asset}`).catch((error) => {
290
302
  if (options.debug) {
291
303
  logger.warn(`Failed to remove asset ${asset}:`, error);
292
304
  }
293
- })
294
- );
295
- await Promise.all(assetPromises);
305
+ });
306
+ });
296
307
  delete manifest.versions[version.id];
297
308
  if (manifest.fileIdToVersion) {
298
309
  for (const [fileId, versionId] of Object.entries(manifest.fileIdToVersion)) {
@@ -309,7 +320,6 @@ function createAssetManager(options) {
309
320
  }
310
321
  }
311
322
  if (removedVersions.length > 0) {
312
- await setVersionManifest(manifest, storage);
313
323
  logger.log(`Removing outdated build artifacts...`);
314
324
  removedVersions.forEach((item, index) => {
315
325
  const isLast = index === removedVersions.length - 1;
@@ -320,6 +330,22 @@ function createAssetManager(options) {
320
330
  logger.log(colors.gray(`${prefix} ${versionInfo} ${assetInfo} ${reasonInfo}`));
321
331
  });
322
332
  }
333
+ if (manifest.fileIdToVersion) {
334
+ const validVersionIds = new Set(Object.keys(manifest.versions));
335
+ let prunedCount = 0;
336
+ for (const [fileId, versionId] of Object.entries(manifest.fileIdToVersion)) {
337
+ if (!validVersionIds.has(versionId)) {
338
+ delete manifest.fileIdToVersion[fileId];
339
+ prunedCount++;
340
+ }
341
+ }
342
+ if (prunedCount > 0 && options.debug) {
343
+ logger.debug(`Pruned ${prunedCount} orphaned fileId entries from map`);
344
+ }
345
+ }
346
+ if (removedVersions.length > 0 || manifest.fileIdToVersion && Object.keys(manifest.fileIdToVersion).length > 0) {
347
+ await setVersionManifest(manifest, storage);
348
+ }
323
349
  }
324
350
  async function storeAssetsInStorage(buildId, outputDir, assets) {
325
351
  const publicDir = join$1(outputDir, "public");
@@ -334,7 +360,7 @@ function createAssetManager(options) {
334
360
  const previousAssets = previousVersionId ? manifest.versions[previousVersionId]?.assets || [] : [];
335
361
  currentVersion.deletedChunks = calculateDeletedChunks(assets, previousAssets);
336
362
  }
337
- for (const asset of assets) {
363
+ await processBatch(assets, 50, async (asset) => {
338
364
  const fileId = extractFileId(asset);
339
365
  const assetPath = join$1(publicDir, asset);
340
366
  const assetData = await promises.readFile(assetPath).catch((error) => {
@@ -368,7 +394,7 @@ function createAssetManager(options) {
368
394
  fileIdToVersion[fileId] = buildId;
369
395
  }
370
396
  }
371
- }
397
+ });
372
398
  await setVersionManifest(manifest, storage);
373
399
  }
374
400
  async function listExistingVersions() {
@@ -403,12 +429,12 @@ function createAssetManager(options) {
403
429
  }
404
430
  }
405
431
  const now = Date.now();
432
+ const assetTasks = [];
406
433
  for (const [versionId, versionData] of Object.entries(manifest.versions)) {
407
434
  if (versionId === currentBuildId) {
408
435
  continue;
409
436
  }
410
- const assets = versionData.assets;
411
- for (const asset of assets) {
437
+ for (const asset of versionData.assets) {
412
438
  if (currentAssetsSet.has(asset)) {
413
439
  continue;
414
440
  }
@@ -416,49 +442,55 @@ function createAssetManager(options) {
416
442
  if (fileId && currentFileIds.has(fileId)) {
417
443
  continue;
418
444
  }
419
- const storageKey = `${versionId}/${asset}`;
420
- await storage.getItemRaw(storageKey).then(async (assetData) => {
421
- if (assetData) {
422
- const targetPath = join$1(publicDir, asset);
423
- await promises.mkdir(dirname(targetPath), { recursive: true });
424
- let dataToWrite;
425
- if (Buffer.isBuffer(assetData)) {
426
- dataToWrite = assetData;
427
- } else if (typeof assetData === "object" && assetData.type === "Buffer" && Array.isArray(assetData.data)) {
428
- dataToWrite = Buffer.from(assetData.data);
429
- } else {
430
- dataToWrite = Buffer.from(assetData);
431
- }
432
- await promises.writeFile(targetPath, dataToWrite);
433
- const versionTimestamp = new Date(versionData.timestamp).getTime();
434
- const ageMs = now - versionTimestamp;
435
- const ageMinutes = Math.floor(ageMs / 6e4);
436
- const ageHours = Math.floor(ageMinutes / 60);
437
- const ageDays = Math.floor(ageHours / 24);
438
- let ageStr;
439
- if (ageDays > 0) {
440
- ageStr = `${ageDays}d ago`;
441
- } else if (ageHours > 0) {
442
- ageStr = `${ageHours}h ago`;
443
- } else if (ageMinutes > 0) {
444
- ageStr = `${ageMinutes}m ago`;
445
- } else {
446
- ageStr = "just now";
447
- }
448
- restoredAssets.push({
449
- asset,
450
- size: dataToWrite.byteLength,
451
- versionId,
452
- age: ageStr
453
- });
454
- }
455
- }).catch((error) => {
456
- if (options.debug) {
457
- logger.warn(`Failed to restore asset ${asset} from version ${versionId}:`, error);
458
- }
459
- });
445
+ assetTasks.push({ versionId, asset, versionData });
460
446
  }
461
447
  }
448
+ const batchResults = await processBatch(assetTasks, 50, async ({ versionId, asset, versionData }) => {
449
+ const storageKey = `${versionId}/${asset}`;
450
+ return await storage.getItemRaw(storageKey).then(async (assetData) => {
451
+ if (assetData) {
452
+ const targetPath = join$1(publicDir, asset);
453
+ await promises.mkdir(dirname(targetPath), { recursive: true });
454
+ let dataToWrite;
455
+ if (Buffer.isBuffer(assetData)) {
456
+ dataToWrite = assetData;
457
+ } else if (typeof assetData === "object" && assetData.type === "Buffer" && Array.isArray(assetData.data)) {
458
+ dataToWrite = Buffer.from(assetData.data);
459
+ } else {
460
+ dataToWrite = Buffer.from(assetData);
461
+ }
462
+ await promises.writeFile(targetPath, dataToWrite);
463
+ const versionTimestamp = new Date(versionData.timestamp).getTime();
464
+ const ageMs = now - versionTimestamp;
465
+ const ageMinutes = Math.floor(ageMs / 6e4);
466
+ const ageHours = Math.floor(ageMinutes / 60);
467
+ const ageDays = Math.floor(ageHours / 24);
468
+ let ageStr;
469
+ if (ageDays > 0) {
470
+ ageStr = `${ageDays}d ago`;
471
+ } else if (ageHours > 0) {
472
+ ageStr = `${ageHours}h ago`;
473
+ } else if (ageMinutes > 0) {
474
+ ageStr = `${ageMinutes}m ago`;
475
+ } else {
476
+ ageStr = "just now";
477
+ }
478
+ return {
479
+ asset,
480
+ size: dataToWrite.byteLength,
481
+ versionId,
482
+ age: ageStr
483
+ };
484
+ }
485
+ return null;
486
+ }).catch((error) => {
487
+ if (options.debug) {
488
+ logger.warn(`Failed to restore asset ${asset} from version ${versionId}:`, error);
489
+ }
490
+ return null;
491
+ });
492
+ });
493
+ restoredAssets.push(...batchResults.filter((r) => r !== null));
462
494
  if (restoredAssets.length > 0) {
463
495
  restoredAssets.forEach((item, index) => {
464
496
  const isLast = index === restoredAssets.length - 1;
@@ -545,7 +577,7 @@ const module$1 = defineNuxtModule({
545
577
  },
546
578
  storage: {
547
579
  driver: "fs",
548
- base: "node_modules/.cache/nuxt/skew-protection"
580
+ base: "node_modules/.cache/nuxt-seo/skew-protection"
549
581
  },
550
582
  debug: false
551
583
  },
@@ -12,6 +12,7 @@ const props = defineProps({
12
12
  const emit = defineEmits(["dismiss", "reload", "update:open", "chunksOutdated"]);
13
13
  const skewProtection = useSkewProtection();
14
14
  const version = skewProtection.clientVersion;
15
+ const isOnline = skewProtection.isOnline;
15
16
  const chunksOutdated = ref(false);
16
17
  const appOutdated = ref(false);
17
18
  const outdatedPayload = ref(null);
@@ -24,6 +25,8 @@ skewProtection.onAppOutdated(() => {
24
25
  appOutdated.value = true;
25
26
  });
26
27
  const isCurrentChunksOutdated = computed(() => {
28
+ if (!isOnline.value)
29
+ return false;
27
30
  if (dismissed.value)
28
31
  return false;
29
32
  if (props.forceOpen)
@@ -31,6 +34,8 @@ const isCurrentChunksOutdated = computed(() => {
31
34
  return chunksOutdated.value;
32
35
  });
33
36
  const isAppOutdated = computed(() => {
37
+ if (!isOnline.value)
38
+ return false;
34
39
  if (dismissed.value)
35
40
  return false;
36
41
  if (props.forceOpen)
@@ -4,6 +4,7 @@ export declare function checkForUpdates(): Promise<void>;
4
4
  export declare function useSkewProtection(): {
5
5
  manifest: import("vue").Ref<NuxtAppManifestMeta | undefined, NuxtAppManifestMeta | undefined>;
6
6
  clientVersion: any;
7
+ isOnline: Readonly<import("vue").ShallowRef<boolean>>;
7
8
  isOutdated: import("vue").ComputedRef<boolean | undefined>;
8
9
  onCurrentChunksOutdated: (callback: (payload: ChunksOutdatedPayload) => void | Promise<void>) => () => void;
9
10
  onAppOutdated: (callback: (manifest?: NuxtAppManifestMeta) => void | Promise<void>) => () => void;
@@ -1,8 +1,11 @@
1
1
  import { buildAssetsURL } from "#internal/nuxt/paths";
2
+ import { useOnline } from "@vueuse/core";
2
3
  import { useNuxtApp } from "nuxt/app";
3
4
  import { computed, onUnmounted, ref } from "vue";
4
5
  import { useRuntimeConfigSkewProtection } from "./useRuntimeConfigSkewProtection.js";
5
6
  export async function checkForUpdates() {
7
+ if (import.meta.client && !useOnline().value)
8
+ return;
6
9
  const nuxtApp = useNuxtApp();
7
10
  const runtimeConfig = useRuntimeConfigSkewProtection();
8
11
  const clientVersion = nuxtApp.$skewProtectionBuildId?.value || runtimeConfig.buildId;
@@ -42,6 +45,7 @@ export function useSkewProtection() {
42
45
  return {
43
46
  manifest,
44
47
  clientVersion,
48
+ isOnline: useOnline(),
45
49
  isOutdated: computed(() => manifest.value && clientVersion !== manifest.value.id),
46
50
  onCurrentChunksOutdated,
47
51
  onAppOutdated,
@@ -19,7 +19,11 @@ export default defineEventHandler(async (event) => {
19
19
  timestamp: Date.now()
20
20
  });
21
21
  }, 3e4);
22
+ let cleanupDone = false;
22
23
  const close = async () => {
24
+ if (cleanupDone)
25
+ return;
26
+ cleanupDone = true;
23
27
  clearInterval(keepaliveInterval);
24
28
  await stream.close();
25
29
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-skew-protection",
3
3
  "type": "module",
4
- "version": "0.3.4",
4
+ "version": "0.3.6",
5
5
  "description": "Solve Nuxt version skews with persistent assets and instant updates.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",
@@ -30,7 +30,7 @@
30
30
  ],
31
31
  "dependencies": {
32
32
  "@nuxt/kit": "^4.2.1",
33
- "@nuxtjs/robots": "^5.6.0",
33
+ "@nuxtjs/robots": "^5.6.1",
34
34
  "@vueuse/core": "^14.1.0",
35
35
  "consola": "^3.4.2",
36
36
  "cookie-es": "^2.0.0",
@@ -43,42 +43,42 @@
43
43
  "ufo": "^1.6.1"
44
44
  },
45
45
  "devDependencies": {
46
- "@antfu/eslint-config": "^6.2.0",
46
+ "@antfu/eslint-config": "^6.4.2",
47
47
  "@arethetypeswrong/cli": "^0.18.2",
48
- "@nuxt/content": "^3.8.2",
48
+ "@nuxt/content": "^3.9.0",
49
49
  "@nuxt/devtools-ui-kit": "^3.1.1",
50
50
  "@nuxt/module-builder": "^1.0.2",
51
- "@nuxt/test-utils": "^3.20.1",
51
+ "@nuxt/test-utils": "^3.21.0",
52
52
  "@nuxt/ui": "^4.2.1",
53
53
  "@nuxtjs/color-mode": "^4.0.0",
54
54
  "@nuxtjs/eslint-config-typescript": "^12.1.0",
55
55
  "@nuxtjs/i18n": "^10.2.1",
56
56
  "@types/ioredis": "^5.0.0",
57
57
  "@types/ws": "^8.18.1",
58
- "@unocss/nuxt": "66.5.10-beta.1",
59
- "@unocss/preset-icons": "66.5.10-beta.1",
60
- "@unocss/preset-uno": "66.5.10-beta.1",
61
- "@unocss/runtime": "66.5.10-beta.1",
58
+ "@unocss/nuxt": "66.5.10",
59
+ "@unocss/preset-icons": "66.5.10",
60
+ "@unocss/preset-uno": "66.5.10",
61
+ "@unocss/runtime": "66.5.10",
62
62
  "@vueuse/nuxt": "^14.1.0",
63
63
  "better-sqlite3": "^12.5.0",
64
64
  "bumpp": "^10.3.2",
65
65
  "eslint": "^9.39.1",
66
66
  "eventsource": "^4.1.0",
67
67
  "execa": "^9.6.1",
68
- "firebase-functions": "^7.0.0",
68
+ "firebase-functions": "^7.0.1",
69
69
  "happy-dom": "^20.0.11",
70
70
  "ioredis": "^5.8.2",
71
71
  "nuxt": "^4.2.1",
72
72
  "playwright-core": "^1.57.0",
73
73
  "tsx": "^4.21.0",
74
74
  "typescript": "^5.9.3",
75
- "unocss": "66.5.10-beta.1",
75
+ "unocss": "66.5.10",
76
76
  "unstorage": "^1.17.3",
77
77
  "vitest": "^4.0.15",
78
78
  "vue": "^3.5.25",
79
79
  "vue-router": "^4.6.3",
80
- "vue-tsc": "^3.1.5",
81
- "wrangler": "^4.51.0",
80
+ "vue-tsc": "^3.1.6",
81
+ "wrangler": "^4.53.0",
82
82
  "ws": "^8.18.3"
83
83
  },
84
84
  "resolutions": {