nuxt-skew-protection 0.3.4 → 0.3.5
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 +1 -1
- package/dist/module.mjs +85 -53
- package/dist/runtime/server/routes/_skew/sse.js +4 -0
- package/package.json +1 -1
package/dist/module.json
CHANGED
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
|
-
|
|
104
|
-
|
|
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
|
-
|
|
289
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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;
|
|
@@ -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
|
};
|