nuxt-skew-protection 1.4.0 → 1.4.1
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
package/dist/module.mjs
CHANGED
|
@@ -187,6 +187,86 @@ 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
|
+
function isLegacyCloudflareEntry(id) {
|
|
198
|
+
const normalizedId = id.split("?", 1)[0].replaceAll("\\", "/");
|
|
199
|
+
return legacyCloudflareAdapterSuffixes.some((suffix) => normalizedId.endsWith(suffix));
|
|
200
|
+
}
|
|
201
|
+
function normalizeBuildAssetsDir(value) {
|
|
202
|
+
const path = value.replace(/^\/+|\/+$/g, "");
|
|
203
|
+
return `/${path}/`;
|
|
204
|
+
}
|
|
205
|
+
function renderCloudflareEntry(options) {
|
|
206
|
+
const nitroEntryId = JSON.stringify(options.nitroEntryId);
|
|
207
|
+
const runtimeHelperId = JSON.stringify(options.runtimeHelperId);
|
|
208
|
+
const buildAssetsDir = JSON.stringify(normalizeBuildAssetsDir(options.buildAssetsDir));
|
|
209
|
+
return `import nitroEntry from ${nitroEntryId};
|
|
210
|
+
import { fetchCloudflareBuildAsset } from ${runtimeHelperId};
|
|
211
|
+
export * from ${nitroEntryId};
|
|
212
|
+
|
|
213
|
+
if (!nitroEntry || typeof nitroEntry.fetch !== "function") {
|
|
214
|
+
throw new TypeError("[nuxt-skew-protection] Nitro's Cloudflare entry does not expose a fetch handler.");
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export default {
|
|
218
|
+
...nitroEntry,
|
|
219
|
+
fetch(request, env, context) {
|
|
220
|
+
const assetResponse = fetchCloudflareBuildAsset(request, env.ASSETS, ${buildAssetsDir});
|
|
221
|
+
return assetResponse ?? nitroEntry.fetch(request, env, context);
|
|
222
|
+
},
|
|
223
|
+
};`;
|
|
224
|
+
}
|
|
225
|
+
function createCloudflareAssetProtectionPlugin(options) {
|
|
226
|
+
let nitroEntryId;
|
|
227
|
+
let wrapperLoaded = false;
|
|
228
|
+
return {
|
|
229
|
+
name: "nuxt-skew-protection:cloudflare-asset-fetch",
|
|
230
|
+
options(inputOptions) {
|
|
231
|
+
if (typeof inputOptions.input !== "string") {
|
|
232
|
+
this.error(
|
|
233
|
+
"[nuxt-skew-protection] Asset 404 protection requires one Nitro entry."
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
if (isLegacyCloudflareEntry(inputOptions.input)) {
|
|
237
|
+
this.error(
|
|
238
|
+
"[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."
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
nitroEntryId = inputOptions.input;
|
|
242
|
+
return {
|
|
243
|
+
...inputOptions,
|
|
244
|
+
input: virtualEntryId
|
|
245
|
+
};
|
|
246
|
+
},
|
|
247
|
+
resolveId(id) {
|
|
248
|
+
return id === virtualEntryId ? resolvedVirtualEntryId : null;
|
|
249
|
+
},
|
|
250
|
+
load(id) {
|
|
251
|
+
if (id !== resolvedVirtualEntryId || !nitroEntryId) {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
wrapperLoaded = true;
|
|
255
|
+
return renderCloudflareEntry({
|
|
256
|
+
...options,
|
|
257
|
+
nitroEntryId
|
|
258
|
+
});
|
|
259
|
+
},
|
|
260
|
+
buildEnd(error) {
|
|
261
|
+
if (!error && !wrapperLoaded) {
|
|
262
|
+
this.error(
|
|
263
|
+
"[nuxt-skew-protection] Nitro's Cloudflare entry was not bundled. Refusing to build without asset 404 protection."
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
190
270
|
const RE_ENOTFOUND = /ENOTFOUND\s+(\S+)/;
|
|
191
271
|
const RE_GETADDRINFO = /getaddrinfo\s+\S+\s+(\S+)/;
|
|
192
272
|
const RE_SIZE_PROP = /(['"]?)size\1\s*:\s*\d+/;
|
|
@@ -238,7 +318,7 @@ function calculateDeletedChunks(currentAssets, previousAssets) {
|
|
|
238
318
|
return previousAssets.filter((asset) => !currentSet.has(asset)).filter((asset) => asset.endsWith(".js"));
|
|
239
319
|
}
|
|
240
320
|
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);
|
|
321
|
+
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
322
|
const currentIndex = sortedVersions.findIndex((v) => v.id === currentVersionId);
|
|
243
323
|
if (currentIndex === -1 || currentIndex === sortedVersions.length - 1) {
|
|
244
324
|
return null;
|
|
@@ -978,6 +1058,19 @@ export {}
|
|
|
978
1058
|
const isCloudflareRuntime = nitroPreset?.includes("cloudflare");
|
|
979
1059
|
const isVercel = nitroPreset?.includes("vercel") || process.env.VERCEL_SKEW_PROTECTION_ENABLED === "1";
|
|
980
1060
|
const isStatic = isNuxtGenerate(nuxt);
|
|
1061
|
+
if (nitroPreset === "cloudflare-module" || nitroPreset === "cloudflare-durable") {
|
|
1062
|
+
nuxt.hook("nitro:config", (nitroConfig) => {
|
|
1063
|
+
nitroConfig.rollupConfig ||= {};
|
|
1064
|
+
const existingPlugins = nitroConfig.rollupConfig.plugins;
|
|
1065
|
+
nitroConfig.rollupConfig.plugins = [
|
|
1066
|
+
...Array.isArray(existingPlugins) ? existingPlugins : existingPlugins ? [existingPlugins] : [],
|
|
1067
|
+
createCloudflareAssetProtectionPlugin({
|
|
1068
|
+
buildAssetsDir: nuxt.options.app.buildAssetsDir,
|
|
1069
|
+
runtimeHelperId: resolver.resolve("./runtime/server/utils/cloudflare-asset-fetch")
|
|
1070
|
+
})
|
|
1071
|
+
];
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
981
1074
|
const isAdapter = isSkewAdapter(options.updateStrategy);
|
|
982
1075
|
let resolvedStrategy = "polling";
|
|
983
1076
|
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.
|
|
4
|
+
"version": "1.4.1",
|
|
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.
|
|
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.
|
|
85
|
-
"nuxtseo-shared": "^5.3.
|
|
84
|
+
"nuxt-site-config": "^4.2.0",
|
|
85
|
+
"nuxtseo-shared": "^5.3.10",
|
|
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.
|
|
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.
|
|
99
|
+
"@nuxtjs/robots": "^6.1.4",
|
|
100
100
|
"@types/ws": "^8.18.1",
|
|
101
101
|
"ably": "^2.26.0",
|
|
102
|
-
"better-sqlite3": "^13.0.
|
|
103
|
-
"bumpp": "^12.
|
|
104
|
-
"eslint": "^10.8.
|
|
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.
|
|
107
|
-
"happy-dom": "^20.11.
|
|
106
|
+
"eventsource": "^4.1.1",
|
|
107
|
+
"happy-dom": "^20.11.2",
|
|
108
108
|
"ioredis": "^6.0.0",
|
|
109
|
-
"nuxt": "^4.5.
|
|
110
|
-
"nuxt-auth-utils": "^0.5.
|
|
111
|
-
"nuxtseo-layer-devtools": "^5.3.
|
|
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.
|
|
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.
|
|
119
|
+
"vue": "^3.5.41",
|
|
120
120
|
"vue-router": "^5.2.0",
|
|
121
121
|
"vue-tsc": "^3.3.9",
|
|
122
|
-
"wrangler": "^4.
|
|
123
|
-
"ws": "^8.21.
|
|
122
|
+
"wrangler": "^4.120.0",
|
|
123
|
+
"ws": "^8.21.3"
|
|
124
124
|
},
|
|
125
125
|
"resolutions": {
|
|
126
126
|
"nuxt-skew-protection": "workspace:*",
|