nuxt-skew-protection 1.4.0 → 1.4.2

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": "1.4.0",
7
+ "version": "1.4.2",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.3",
10
10
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -187,6 +187,98 @@ async function resolveBuildTimeDriver(storage) {
187
187
  return lazyDriver({ base, ...driverOptions });
188
188
  }
189
189
 
190
+ const virtualEntryId = "virtual:nuxt-skew-protection/cloudflare-entry";
191
+ const resolvedVirtualEntryId = `\0${virtualEntryId}`;
192
+ const legacyCloudflareAdapterSuffixes = [
193
+ "/nitropack/dist/runtime/entries/cloudflare-module.mjs",
194
+ "/nitropack/dist/runtime/entries/cloudflare.mjs",
195
+ "/nitropack/dist/runtime/entries/cloudflare-pages.mjs"
196
+ ];
197
+ const nitroPrerenderEntrySuffix = "/nitropack/dist/presets/_nitro/runtime/nitro-prerenderer.mjs";
198
+ function isNitroPrerenderEntry(id) {
199
+ return id.split("?", 1)[0].endsWith(nitroPrerenderEntrySuffix);
200
+ }
201
+ function isLegacyCloudflareEntry(id) {
202
+ const normalizedId = id.split("?", 1)[0].replaceAll("\\", "/");
203
+ return legacyCloudflareAdapterSuffixes.some((suffix) => normalizedId.endsWith(suffix));
204
+ }
205
+ function normalizeBuildAssetsDir(value) {
206
+ const path = value.replace(/^\/+|\/+$/g, "");
207
+ return `/${path}/`;
208
+ }
209
+ function renderCloudflareEntry(options) {
210
+ const nitroEntryId = JSON.stringify(options.nitroEntryId);
211
+ const runtimeHelperId = JSON.stringify(options.runtimeHelperId);
212
+ const buildAssetsDir = JSON.stringify(normalizeBuildAssetsDir(options.buildAssetsDir));
213
+ return `import nitroEntry from ${nitroEntryId};
214
+ import { fetchCloudflareBuildAsset } from ${runtimeHelperId};
215
+ export * from ${nitroEntryId};
216
+
217
+ if (!nitroEntry || typeof nitroEntry.fetch !== "function") {
218
+ throw new TypeError("[nuxt-skew-protection] Nitro's Cloudflare entry does not expose a fetch handler.");
219
+ }
220
+
221
+ export default {
222
+ ...nitroEntry,
223
+ fetch(request, env, context) {
224
+ const assetResponse = fetchCloudflareBuildAsset(request, env.ASSETS, ${buildAssetsDir});
225
+ return assetResponse ?? nitroEntry.fetch(request, env, context);
226
+ },
227
+ };`;
228
+ }
229
+ function createCloudflareAssetProtectionPlugin(options) {
230
+ let nitroEntryId;
231
+ let wrapperLoaded = false;
232
+ let shouldWrapEntry = true;
233
+ return {
234
+ name: "nuxt-skew-protection:cloudflare-asset-fetch",
235
+ options(inputOptions) {
236
+ nitroEntryId = void 0;
237
+ wrapperLoaded = false;
238
+ shouldWrapEntry = false;
239
+ if (typeof inputOptions.input !== "string") {
240
+ this.error(
241
+ "[nuxt-skew-protection] Asset 404 protection requires one Nitro entry."
242
+ );
243
+ }
244
+ if (isNitroPrerenderEntry(inputOptions.input)) {
245
+ return inputOptions;
246
+ }
247
+ if (isLegacyCloudflareEntry(inputOptions.input)) {
248
+ this.error(
249
+ "[nuxt-skew-protection] Asset 404 protection requires nitropack >= 2.10.0. Your project uses an older adapter without the ASSETS binding. Upgrade nitropack (or nuxt) to enable asset 404 protection."
250
+ );
251
+ }
252
+ nitroEntryId = inputOptions.input;
253
+ shouldWrapEntry = true;
254
+ return {
255
+ ...inputOptions,
256
+ input: virtualEntryId
257
+ };
258
+ },
259
+ resolveId(id) {
260
+ return id === virtualEntryId ? resolvedVirtualEntryId : null;
261
+ },
262
+ load(id) {
263
+ if (id !== resolvedVirtualEntryId || !nitroEntryId) {
264
+ return null;
265
+ }
266
+ wrapperLoaded = true;
267
+ return renderCloudflareEntry({
268
+ ...options,
269
+ nitroEntryId
270
+ });
271
+ },
272
+ buildEnd(error) {
273
+ if (shouldWrapEntry && !error && !wrapperLoaded) {
274
+ this.error(
275
+ "[nuxt-skew-protection] Nitro's Cloudflare entry was not bundled. Refusing to build without asset 404 protection."
276
+ );
277
+ }
278
+ }
279
+ };
280
+ }
281
+
190
282
  const RE_ENOTFOUND = /ENOTFOUND\s+(\S+)/;
191
283
  const RE_GETADDRINFO = /getaddrinfo\s+\S+\s+(\S+)/;
192
284
  const RE_SIZE_PROP = /(['"]?)size\1\s*:\s*\d+/;
@@ -238,7 +330,7 @@ function calculateDeletedChunks(currentAssets, previousAssets) {
238
330
  return previousAssets.filter((asset) => !currentSet.has(asset)).filter((asset) => asset.endsWith(".js"));
239
331
  }
240
332
  function getPreviousVersion(manifest, currentVersionId) {
241
- const sortedVersions = Object.entries(manifest.versions).map(([id, data]) => ({ id, timestamp: new Date(data.timestamp).getTime() })).sort((a, b) => b.timestamp - a.timestamp);
333
+ const sortedVersions = Object.entries(manifest.versions).map(([id, data], index) => ({ id, timestamp: new Date(data.timestamp).getTime(), index })).sort((a, b) => b.timestamp - a.timestamp || b.index - a.index);
242
334
  const currentIndex = sortedVersions.findIndex((v) => v.id === currentVersionId);
243
335
  if (currentIndex === -1 || currentIndex === sortedVersions.length - 1) {
244
336
  return null;
@@ -978,6 +1070,19 @@ export {}
978
1070
  const isCloudflareRuntime = nitroPreset?.includes("cloudflare");
979
1071
  const isVercel = nitroPreset?.includes("vercel") || process.env.VERCEL_SKEW_PROTECTION_ENABLED === "1";
980
1072
  const isStatic = isNuxtGenerate(nuxt);
1073
+ if (nitroPreset === "cloudflare-module" || nitroPreset === "cloudflare-durable") {
1074
+ nuxt.hook("nitro:config", (nitroConfig) => {
1075
+ nitroConfig.rollupConfig ||= {};
1076
+ const existingPlugins = nitroConfig.rollupConfig.plugins;
1077
+ nitroConfig.rollupConfig.plugins = [
1078
+ ...Array.isArray(existingPlugins) ? existingPlugins : existingPlugins ? [existingPlugins] : [],
1079
+ createCloudflareAssetProtectionPlugin({
1080
+ buildAssetsDir: nuxt.options.app.buildAssetsDir,
1081
+ runtimeHelperId: resolver.resolve("./runtime/server/utils/cloudflare-asset-fetch")
1082
+ })
1083
+ ];
1084
+ });
1085
+ }
981
1086
  const isAdapter = isSkewAdapter(options.updateStrategy);
982
1087
  let resolvedStrategy = "polling";
983
1088
  if (isAdapter) {
@@ -0,0 +1,6 @@
1
+ interface CloudflareAssetBinding {
2
+ fetch: (request: Request) => Promise<Response>;
3
+ }
4
+ export declare function fetchCloudflareAsset(request: Request, assets: CloudflareAssetBinding): Promise<Response>;
5
+ export declare function fetchCloudflareBuildAsset(request: Request, assets: CloudflareAssetBinding | undefined, buildAssetsDir: string): Promise<Response> | undefined;
6
+ export {};
@@ -0,0 +1,34 @@
1
+ function disableCaching(response) {
2
+ const headers = new Headers(response.headers);
3
+ headers.set("cache-control", "no-store");
4
+ headers.set("cdn-cache-control", "no-store");
5
+ headers.set("cloudflare-cdn-cache-control", "no-store");
6
+ return new Response(response.body, {
7
+ status: response.status,
8
+ statusText: response.statusText,
9
+ headers
10
+ });
11
+ }
12
+ function createRetryRequest(request) {
13
+ return new Request(request, { cache: "no-cache" });
14
+ }
15
+ export async function fetchCloudflareAsset(request, assets) {
16
+ const response = await assets.fetch(request);
17
+ if (response.status < 400) {
18
+ return response;
19
+ }
20
+ if (response.status !== 404) {
21
+ return disableCaching(response);
22
+ }
23
+ const retryResponse = await assets.fetch(createRetryRequest(request));
24
+ return retryResponse.status < 400 ? retryResponse : disableCaching(retryResponse);
25
+ }
26
+ export function fetchCloudflareBuildAsset(request, assets, buildAssetsDir) {
27
+ if (!new URL(request.url).pathname.startsWith(buildAssetsDir)) {
28
+ return void 0;
29
+ }
30
+ if (!assets) {
31
+ return Promise.resolve(disableCaching(new Response(null, { status: 404 })));
32
+ }
33
+ return fetchCloudflareAsset(request, assets);
34
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-skew-protection",
3
3
  "type": "module",
4
- "version": "1.4.0",
4
+ "version": "1.4.2",
5
5
  "description": "Solve Nuxt version skews with persistent assets and instant updates.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",
@@ -77,12 +77,12 @@
77
77
  }
78
78
  },
79
79
  "dependencies": {
80
- "@nuxt/kit": "^4.5.1",
80
+ "@nuxt/kit": "^4.5.2",
81
81
  "@vueuse/core": "^14.4.0",
82
82
  "consola": "^3.4.2",
83
83
  "cookie-es": "^3.1.1",
84
- "nuxt-site-config": "^4.1.4",
85
- "nuxtseo-shared": "^5.3.9",
84
+ "nuxt-site-config": "^4.2.0",
85
+ "nuxtseo-shared": "^5.3.11",
86
86
  "ofetch": "^1.5.1",
87
87
  "pathe": "^2.0.3",
88
88
  "pkg-types": "^2.3.1",
@@ -91,36 +91,36 @@
91
91
  "zod": "^4.4.3"
92
92
  },
93
93
  "devDependencies": {
94
- "@antfu/eslint-config": "^9.2.0",
94
+ "@antfu/eslint-config": "^9.3.0",
95
95
  "@arethetypeswrong/cli": "^0.18.5",
96
96
  "@nuxt/module-builder": "^1.0.3",
97
97
  "@nuxt/test-utils": "^4.1.0",
98
98
  "@nuxt/ui": "^4.10.0",
99
- "@nuxtjs/robots": "^6.1.3",
99
+ "@nuxtjs/robots": "^6.1.4",
100
100
  "@types/ws": "^8.18.1",
101
101
  "ably": "^2.26.0",
102
- "better-sqlite3": "^13.0.2",
103
- "bumpp": "^12.1.1",
104
- "eslint": "^10.8.0",
102
+ "better-sqlite3": "^13.0.3",
103
+ "bumpp": "^12.2.0",
104
+ "eslint": "^10.8.1",
105
105
  "eslint-plugin-harlanzw": "^0.17.1",
106
- "eventsource": "^4.1.0",
107
- "happy-dom": "^20.11.1",
106
+ "eventsource": "^4.1.1",
107
+ "happy-dom": "^20.11.2",
108
108
  "ioredis": "^6.0.0",
109
- "nuxt": "^4.5.1",
110
- "nuxt-auth-utils": "^0.5.29",
111
- "nuxtseo-layer-devtools": "^5.3.6",
109
+ "nuxt": "^4.5.2",
110
+ "nuxt-auth-utils": "^0.5.30",
111
+ "nuxtseo-layer-devtools": "^5.3.10",
112
112
  "playwright-core": "^1.62.1",
113
113
  "pusher-js": "^8.6.0",
114
114
  "std-env": "^4.2.0",
115
- "tsx": "^4.23.5",
115
+ "tsx": "^4.23.11",
116
116
  "typescript": "6.0.3",
117
117
  "unbuild": "^3.6.1",
118
118
  "vitest": "^4.1.10",
119
- "vue": "^3.5.40",
119
+ "vue": "^3.5.41",
120
120
  "vue-router": "^5.2.0",
121
121
  "vue-tsc": "^3.3.9",
122
- "wrangler": "^4.118.0",
123
- "ws": "^8.21.1"
122
+ "wrangler": "^4.120.0",
123
+ "ws": "^8.21.3"
124
124
  },
125
125
  "resolutions": {
126
126
  "nuxt-skew-protection": "workspace:*",