zip-peek 0.2.3-alpha.0 → 0.3.0
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/index.js +101 -24
- package/dist/types.d.ts +5 -0
- package/dist/types.js +26 -0
- package/dist/zipServiceWorker.js +1 -1
- package/dist/zipServiceWorker.js.map +1 -1
- package/package.json +1 -4
- package/dist/registerZipServiceWorker.d.ts +0 -12
- package/dist/registerZipServiceWorker.js +0 -87
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zip-peek",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Service worker based ZIP asset loader for browser applications.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"packageManager": "pnpm@10.23.0",
|
|
@@ -20,15 +20,12 @@
|
|
|
20
20
|
"clean": "rm -rf dist",
|
|
21
21
|
"build": "pnpm run clean && tsc -p tsconfig.json && webpack --config webpack.worker.config.js",
|
|
22
22
|
"build:service-worker": "rm -rf dist && webpack --config webpack.worker.config.js",
|
|
23
|
-
"upload:sentry-sourcemaps": "node scripts/upload-sentry-sourcemaps.mjs",
|
|
24
|
-
"build:with-sentry": "pnpm run build && pnpm run upload:sentry-sourcemaps",
|
|
25
23
|
"prepack": "pnpm run build"
|
|
26
24
|
},
|
|
27
25
|
"dependencies": {
|
|
28
26
|
"fflate": "^0.8.2"
|
|
29
27
|
},
|
|
30
28
|
"devDependencies": {
|
|
31
|
-
"@sentry/cli": "^3.5.0",
|
|
32
29
|
"ts-loader": "^9.5.2",
|
|
33
30
|
"typescript": "^5.8.3",
|
|
34
31
|
"webpack": "^5.100.2",
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
type RegisterZipServiceWorkerOptions = {
|
|
2
|
-
workerUrl: string;
|
|
3
|
-
scopeUrl: string;
|
|
4
|
-
reloadOnFirstInstall?: boolean;
|
|
5
|
-
};
|
|
6
|
-
/**
|
|
7
|
-
* Registers the zip worker script URL with the caller-provided scope.
|
|
8
|
-
*
|
|
9
|
-
* @returns true if the page is reloading and callers should abort init.
|
|
10
|
-
*/
|
|
11
|
-
export declare function ensureZipServiceWorkerRegistered({ workerUrl, scopeUrl, reloadOnFirstInstall, }: RegisterZipServiceWorkerOptions): Promise<boolean>;
|
|
12
|
-
export {};
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ensureZipServiceWorkerRegistered = ensureZipServiceWorkerRegistered;
|
|
4
|
-
function normalizeScopePath(href) {
|
|
5
|
-
let p = new URL(href).pathname;
|
|
6
|
-
if (p.length > 1 && p.endsWith("/")) {
|
|
7
|
-
p = p.slice(0, -1);
|
|
8
|
-
}
|
|
9
|
-
return p;
|
|
10
|
-
}
|
|
11
|
-
function zipServiceWorkerScriptMatches(scriptURL, expectedWorkerUrl) {
|
|
12
|
-
if (!scriptURL) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
try {
|
|
16
|
-
const expected = new URL(expectedWorkerUrl, window.location.href).href;
|
|
17
|
-
const resolved = new URL(scriptURL, window.location.href).href;
|
|
18
|
-
return resolved === expected;
|
|
19
|
-
}
|
|
20
|
-
catch {
|
|
21
|
-
try {
|
|
22
|
-
return /\/zipServiceWorker(\.[0-9a-f]{8})?\.js(\?|$)/.test(new URL(scriptURL).pathname);
|
|
23
|
-
}
|
|
24
|
-
catch {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Removes prior registration that used the same script URL but a different scope, so an old
|
|
31
|
-
* default/narrow scope does not win over the caller-provided package scope.
|
|
32
|
-
*/
|
|
33
|
-
async function unregisterMismatchedScopeZipServiceWorkerIfNeeded(expectedWorkerUrl, expectedScopeUrl) {
|
|
34
|
-
const expectedWorkerHref = new URL(expectedWorkerUrl, window.location.href).href;
|
|
35
|
-
const wantScopePath = normalizeScopePath(expectedScopeUrl);
|
|
36
|
-
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
37
|
-
for (const reg of registrations) {
|
|
38
|
-
const scriptURL = reg.installing?.scriptURL ?? reg.waiting?.scriptURL ?? reg.active?.scriptURL;
|
|
39
|
-
if (!scriptURL) {
|
|
40
|
-
continue;
|
|
41
|
-
}
|
|
42
|
-
let scriptHref;
|
|
43
|
-
try {
|
|
44
|
-
scriptHref = new URL(scriptURL, window.location.href).href;
|
|
45
|
-
}
|
|
46
|
-
catch {
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
if (scriptHref !== expectedWorkerHref) {
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
const regScopePath = normalizeScopePath(reg.scope);
|
|
53
|
-
if (regScopePath !== wantScopePath) {
|
|
54
|
-
await reg.unregister();
|
|
55
|
-
console.log(`Unregistered zip service worker (scope ${reg.scope}); re-registering with ${expectedScopeUrl}`);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Registers the zip worker script URL with the caller-provided scope.
|
|
61
|
-
*
|
|
62
|
-
* @returns true if the page is reloading and callers should abort init.
|
|
63
|
-
*/
|
|
64
|
-
async function ensureZipServiceWorkerRegistered({ workerUrl, scopeUrl, reloadOnFirstInstall = true, }) {
|
|
65
|
-
if (!("serviceWorker" in navigator)) {
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
await unregisterMismatchedScopeZipServiceWorkerIfNeeded(workerUrl, scopeUrl);
|
|
69
|
-
const existing = await navigator.serviceWorker.getRegistration(scopeUrl);
|
|
70
|
-
const alreadyRegistered = existing &&
|
|
71
|
-
(zipServiceWorkerScriptMatches(existing.active?.scriptURL, workerUrl) ||
|
|
72
|
-
zipServiceWorkerScriptMatches(existing.waiting?.scriptURL, workerUrl) ||
|
|
73
|
-
zipServiceWorkerScriptMatches(existing.installing?.scriptURL, workerUrl));
|
|
74
|
-
if (!alreadyRegistered) {
|
|
75
|
-
await navigator.serviceWorker.register(workerUrl, { scope: scopeUrl });
|
|
76
|
-
console.log("Zip service worker registered successfully");
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
console.log("Zip service worker already registered; skipping register().");
|
|
80
|
-
}
|
|
81
|
-
await navigator.serviceWorker.ready;
|
|
82
|
-
if (reloadOnFirstInstall && !navigator.serviceWorker.controller) {
|
|
83
|
-
window.location.reload();
|
|
84
|
-
return true;
|
|
85
|
-
}
|
|
86
|
-
return false;
|
|
87
|
-
}
|