@swoff/cli 0.3.9 → 0.3.10
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/__tests__/assemble-sw.test.js +8 -17
- package/dist/__tests__/assemble-sw.test.js.map +1 -1
- package/dist/__tests__/cli-integration.test.js +2 -2
- package/dist/__tests__/cli-integration.test.js.map +1 -1
- package/dist/__tests__/config-types.test.js +18 -8
- package/dist/__tests__/config-types.test.js.map +1 -1
- package/dist/__tests__/file-generators.test.js +3 -3
- package/dist/__tests__/file-generators.test.js.map +1 -1
- package/dist/__tests__/loader.test.js +5 -1
- package/dist/__tests__/loader.test.js.map +1 -1
- package/dist/__tests__/validator.test.js +6 -13
- package/dist/__tests__/validator.test.js.map +1 -1
- package/dist/index.js +39 -35
- package/dist/index.js.map +1 -1
- package/dist/lib/cli/help.js +13 -8
- package/dist/lib/cli/help.js.map +1 -1
- package/dist/lib/commands/add.js +38 -8
- package/dist/lib/commands/add.js.map +1 -1
- package/dist/lib/commands/assets.js +37 -0
- package/dist/lib/commands/assets.js.map +1 -0
- package/dist/lib/commands/clean.js +33 -17
- package/dist/lib/commands/clean.js.map +1 -1
- package/dist/lib/commands/generate-assets.js +44 -0
- package/dist/lib/commands/generate-assets.js.map +1 -0
- package/dist/lib/commands/generate-guide.js +22 -29
- package/dist/lib/commands/generate-guide.js.map +1 -1
- package/dist/lib/commands/generate.js +16 -9
- package/dist/lib/commands/generate.js.map +1 -1
- package/dist/lib/commands/info.js +36 -45
- package/dist/lib/commands/info.js.map +1 -1
- package/dist/lib/commands/init.js +25 -3
- package/dist/lib/commands/init.js.map +1 -1
- package/dist/lib/commands/validate.js +1 -1
- package/dist/lib/commands/validate.js.map +1 -1
- package/dist/lib/config/loader.js +25 -20
- package/dist/lib/config/loader.js.map +1 -1
- package/dist/lib/config/validator.js +20 -11
- package/dist/lib/config/validator.js.map +1 -1
- package/dist/lib/generators/asset-generator/dependency-resolver.js +76 -0
- package/dist/lib/generators/asset-generator/dependency-resolver.js.map +1 -0
- package/dist/lib/generators/asset-generator/generate.js +111 -0
- package/dist/lib/generators/asset-generator/generate.js.map +1 -0
- package/dist/lib/generators/asset-generator/guide.js +43 -0
- package/dist/lib/generators/asset-generator/guide.js.map +1 -0
- package/dist/lib/generators/asset-generator/html-patcher.js +65 -0
- package/dist/lib/generators/asset-generator/html-patcher.js.map +1 -0
- package/dist/lib/generators/asset-generator/ico-encoder.js +29 -0
- package/dist/lib/generators/asset-generator/ico-encoder.js.map +1 -0
- package/dist/lib/generators/asset-generator/manifest-patcher.js +23 -0
- package/dist/lib/generators/asset-generator/manifest-patcher.js.map +1 -0
- package/dist/lib/generators/asset-generator/maskable.js +15 -0
- package/dist/lib/generators/asset-generator/maskable.js.map +1 -0
- package/dist/lib/generators/asset-generator/rasterize.js +32 -0
- package/dist/lib/generators/asset-generator/rasterize.js.map +1 -0
- package/dist/lib/generators/asset-generator/sizes.js +25 -0
- package/dist/lib/generators/asset-generator/sizes.js.map +1 -0
- package/dist/lib/generators/file-generators/api-config.js +6 -0
- package/dist/lib/generators/file-generators/api-config.js.map +1 -0
- package/dist/lib/generators/file-generators/auth-state.js +2 -34
- package/dist/lib/generators/file-generators/auth-state.js.map +1 -1
- package/dist/lib/generators/file-generators/auth-store.js +2 -250
- package/dist/lib/generators/file-generators/auth-store.js.map +1 -1
- package/dist/lib/generators/file-generators/auth-user.js +3 -81
- package/dist/lib/generators/file-generators/auth-user.js.map +1 -1
- package/dist/lib/generators/file-generators/background-sync.js +2 -57
- package/dist/lib/generators/file-generators/background-sync.js.map +1 -1
- package/dist/lib/generators/file-generators/cache.js +2 -30
- package/dist/lib/generators/file-generators/cache.js.map +1 -1
- package/dist/lib/generators/file-generators/client-injector.js +2 -130
- package/dist/lib/generators/file-generators/client-injector.js.map +1 -1
- package/dist/lib/generators/file-generators/fetch-state.js +6 -0
- package/dist/lib/generators/file-generators/fetch-state.js.map +1 -0
- package/dist/lib/generators/file-generators/fetch-wrapper.js +3 -295
- package/dist/lib/generators/file-generators/fetch-wrapper.js.map +1 -1
- package/dist/lib/generators/file-generators/generate-framework-adapters.js +56 -0
- package/dist/lib/generators/file-generators/generate-framework-adapters.js.map +1 -0
- package/dist/lib/generators/file-generators/generate-hooks.js +4 -3
- package/dist/lib/generators/file-generators/generate-hooks.js.map +1 -1
- package/dist/lib/generators/file-generators/gql-wrapper.js +3 -132
- package/dist/lib/generators/file-generators/gql-wrapper.js.map +1 -1
- package/dist/lib/generators/file-generators/guide-generator.js +29 -1
- package/dist/lib/generators/file-generators/guide-generator.js.map +1 -1
- package/dist/lib/generators/file-generators/invalidation-tags.js +3 -258
- package/dist/lib/generators/file-generators/invalidation-tags.js.map +1 -1
- package/dist/lib/generators/file-generators/manifest.js +13 -3
- package/dist/lib/generators/file-generators/manifest.js.map +1 -1
- package/dist/lib/generators/file-generators/mutation-queue.js +3 -305
- package/dist/lib/generators/file-generators/mutation-queue.js.map +1 -1
- package/dist/lib/generators/file-generators/mutation-state.js +2 -117
- package/dist/lib/generators/file-generators/mutation-state.js.map +1 -1
- package/dist/lib/generators/file-generators/push.js +6 -138
- package/dist/lib/generators/file-generators/push.js.map +1 -1
- package/dist/lib/generators/file-generators/pwa-install.js +11 -84
- package/dist/lib/generators/file-generators/pwa-install.js.map +1 -1
- package/dist/lib/generators/file-generators/reset.js +6 -0
- package/dist/lib/generators/file-generators/reset.js.map +1 -0
- package/dist/lib/generators/file-generators/server-push.js +3 -134
- package/dist/lib/generators/file-generators/server-push.js.map +1 -1
- package/dist/lib/generators/file-generators/sw-generator-build.js +4 -4
- package/dist/lib/generators/file-generators/sw-injector.js +9 -230
- package/dist/lib/generators/file-generators/sw-injector.js.map +1 -1
- package/dist/lib/generators/file-generators/type-definitions.js +2 -111
- package/dist/lib/generators/file-generators/type-definitions.js.map +1 -1
- package/dist/lib/generators/sw-generator.js +5 -5
- package/dist/lib/generators/sw-generator.js.map +1 -1
- package/dist/lib/generators/sw-sections/activate-handler.js +25 -3
- package/dist/lib/generators/sw-sections/activate-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/assemble-sw.js +13 -12
- package/dist/lib/generators/sw-sections/assemble-sw.js.map +1 -1
- package/dist/lib/generators/sw-sections/background-sync-handler.js +14 -7
- package/dist/lib/generators/sw-sections/background-sync-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/config-header.js +1 -1
- package/dist/lib/generators/sw-sections/config-header.js.map +1 -1
- package/dist/lib/generators/sw-sections/fetch-handler.js +165 -104
- package/dist/lib/generators/sw-sections/fetch-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/glob-matcher-code.js.map +1 -0
- package/dist/lib/generators/sw-sections/server-push-handler.js +12 -0
- package/dist/lib/generators/sw-sections/server-push-handler.js.map +1 -1
- package/dist/lib/generators/sw-sections/tag-management.js +6 -4
- package/dist/lib/generators/sw-sections/tag-management.js.map +1 -1
- package/dist/lib/generators/swoff-files-generator.js +66 -35
- package/dist/lib/generators/swoff-files-generator.js.map +1 -1
- package/dist/lib/shared/config-types.js +89 -28
- package/dist/lib/shared/config-types.js.map +1 -1
- package/dist/runtime/api-config.js +18 -0
- package/dist/runtime/api-config.js.map +1 -0
- package/dist/runtime/auth-state.js +35 -0
- package/dist/runtime/auth-state.js.map +1 -0
- package/dist/runtime/auth-store.js +273 -0
- package/dist/runtime/auth-store.js.map +1 -0
- package/dist/runtime/auth-user.js +168 -0
- package/dist/runtime/auth-user.js.map +1 -0
- package/dist/runtime/background-sync.js +59 -0
- package/dist/runtime/background-sync.js.map +1 -0
- package/dist/runtime/cache.js +30 -0
- package/dist/runtime/cache.js.map +1 -0
- package/dist/runtime/client-injector.js +136 -0
- package/dist/runtime/client-injector.js.map +1 -0
- package/dist/runtime/fetch-state.js +42 -0
- package/dist/runtime/fetch-state.js.map +1 -0
- package/dist/runtime/fetch-wrapper.js +355 -0
- package/dist/runtime/fetch-wrapper.js.map +1 -0
- package/dist/runtime/gql-wrapper.js +135 -0
- package/dist/runtime/gql-wrapper.js.map +1 -0
- package/dist/runtime/invalidation-tags.js +260 -0
- package/dist/runtime/invalidation-tags.js.map +1 -0
- package/dist/runtime/mutation-queue.js +304 -0
- package/dist/runtime/mutation-queue.js.map +1 -0
- package/dist/runtime/mutation-state.js +117 -0
- package/dist/runtime/mutation-state.js.map +1 -0
- package/dist/runtime/push.js +138 -0
- package/dist/runtime/push.js.map +1 -0
- package/dist/runtime/pwa-index.js +11 -0
- package/dist/runtime/pwa-index.js.map +1 -0
- package/dist/runtime/pwa-injector.js +44 -0
- package/dist/runtime/pwa-injector.js.map +1 -0
- package/dist/runtime/pwa-install.js +83 -0
- package/dist/runtime/pwa-install.js.map +1 -0
- package/dist/runtime/pwa-prompt.js +54 -0
- package/dist/runtime/pwa-prompt.js.map +1 -0
- package/dist/runtime/reset.js +148 -0
- package/dist/runtime/reset.js.map +1 -0
- package/dist/runtime/server-push.js +148 -0
- package/dist/runtime/server-push.js.map +1 -0
- package/dist/runtime/sw-injector.js +236 -0
- package/dist/runtime/sw-injector.js.map +1 -0
- package/dist/runtime/type-definitions.js +138 -0
- package/dist/runtime/type-definitions.js.map +1 -0
- package/dist/runtime/utils.js +27 -0
- package/dist/runtime/utils.js.map +1 -0
- package/package.json +3 -2
- package/templates/react/useAuth.jsx +106 -0
- package/templates/react/useAuth.tsx +118 -0
- package/templates/{hooks → react}/useBackgroundSync.jsx +6 -4
- package/templates/{hooks → react}/useBackgroundSync.tsx +6 -4
- package/templates/{hooks → react}/useCacheInvalidation.jsx +2 -2
- package/templates/react/useCacheInvalidation.tsx +52 -0
- package/templates/react/useCachedFetch.jsx +140 -0
- package/templates/react/useCachedFetch.tsx +160 -0
- package/templates/react/useIsFetching.jsx +32 -0
- package/templates/react/useIsFetching.tsx +32 -0
- package/templates/react/useMutation.jsx +141 -0
- package/templates/react/useMutation.tsx +180 -0
- package/templates/react/useMutationQueue.jsx +69 -0
- package/templates/react/useMutationQueue.tsx +83 -0
- package/templates/react/useNetworkStatus.jsx +61 -0
- package/templates/react/useNetworkStatus.tsx +76 -0
- package/templates/react/usePrefetch.jsx +32 -0
- package/templates/react/usePrefetch.tsx +42 -0
- package/templates/{hooks → react}/usePushSubscription.jsx +4 -4
- package/templates/{hooks → react}/usePushSubscription.tsx +4 -4
- package/templates/{hooks → react}/useSWUpdate.jsx +3 -3
- package/templates/{hooks → react}/useSWUpdate.tsx +4 -4
- package/templates/react/useSwoffReset.jsx +32 -0
- package/templates/react/useSwoffReset.tsx +39 -0
- package/docs/API.md +0 -565
- package/docs/ARCHITECTURE.md +0 -374
- package/docs/CLI.md +0 -148
- package/docs/COMPARISON.md +0 -192
- package/docs/CONFIG.md +0 -277
- package/docs/ECOSYSTEM.md +0 -33
- package/templates/hooks/useAuth.jsx +0 -30
- package/templates/hooks/useAuth.tsx +0 -30
- package/templates/hooks/useCacheInvalidation.tsx +0 -19
- package/templates/hooks/useCachedFetch.jsx +0 -69
- package/templates/hooks/useCachedFetch.tsx +0 -81
- package/templates/hooks/useMutation.jsx +0 -76
- package/templates/hooks/useMutation.tsx +0 -109
- package/templates/hooks/useMutationQueue.jsx +0 -43
- package/templates/hooks/useMutationQueue.tsx +0 -50
- package/templates/hooks/useNetworkStatus.jsx +0 -19
- package/templates/hooks/useNetworkStatus.tsx +0 -19
- package/templates/hooks/usePrefetch.jsx +0 -10
- package/templates/hooks/usePrefetch.tsx +0 -25
- /package/templates/{hooks → react}/useMutationState.jsx +0 -0
- /package/templates/{hooks → react}/useMutationState.tsx +0 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { T, R } from "./utils.js";
|
|
2
|
+
export function generateMutationStateCode(ctx) {
|
|
3
|
+
const { ext, ts } = ctx;
|
|
4
|
+
return `/**
|
|
5
|
+
* Swoff Mutation State
|
|
6
|
+
* Lightweight in-memory mutation state tracker.
|
|
7
|
+
* Enables per-mutation status UI (loading spinners, error states, success indicators).
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import { trackMutation, getMutationState, clearMutationState } from './swoff/mutation-state.${ext}';
|
|
11
|
+
*
|
|
12
|
+
* // After queueing a mutation
|
|
13
|
+
* const id = "mutation-" + crypto.randomUUID();
|
|
14
|
+
* trackMutation(id, "pending");
|
|
15
|
+
*
|
|
16
|
+
* // Check state later
|
|
17
|
+
* const state = getMutationState(id);
|
|
18
|
+
* if (state?.status === "error") { showError(state.error); }
|
|
19
|
+
*
|
|
20
|
+
* // Clear when done
|
|
21
|
+
* clearMutationState(id);
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export type MutationStatus = "idle" | "pending" | "success" | "error";
|
|
25
|
+
|
|
26
|
+
export interface MutationState {
|
|
27
|
+
id${T(ts, "string")};
|
|
28
|
+
status${T(ts, "MutationStatus")};
|
|
29
|
+
error${T(ts, "Error | null")};
|
|
30
|
+
data${T(ts, "unknown")};
|
|
31
|
+
timestamp${T(ts, "number")};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const mutations = new Map<string, MutationState>();
|
|
35
|
+
|
|
36
|
+
let listenerId = 0;
|
|
37
|
+
const listeners = new Map<number, (state: MutationState) => void>();
|
|
38
|
+
|
|
39
|
+
/** Track a mutation with the given ID and initial status. */
|
|
40
|
+
export function trackMutation(id${T(ts, "string")}, status${T(ts, "MutationStatus")} = "idle")${R(ts, "MutationState")}{
|
|
41
|
+
let state = mutations.get(id);
|
|
42
|
+
if (!state) {
|
|
43
|
+
state = { id, status, error: null, data: null, timestamp: Date.now() };
|
|
44
|
+
mutations.set(id, state);
|
|
45
|
+
} else {
|
|
46
|
+
state.status = status;
|
|
47
|
+
state.timestamp = Date.now();
|
|
48
|
+
}
|
|
49
|
+
notifyListeners(state);
|
|
50
|
+
window.dispatchEvent(new CustomEvent("mutation-state-changed", { detail: state }));
|
|
51
|
+
return state;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Update an existing mutation's state. */
|
|
55
|
+
export function updateMutationState(id${T(ts, "string")}, partial${T(ts, "Partial<MutationState>")})${R(ts, "MutationState | null")}{
|
|
56
|
+
const state = mutations.get(id);
|
|
57
|
+
if (!state) return null;
|
|
58
|
+
Object.assign(state, partial);
|
|
59
|
+
state.timestamp = Date.now();
|
|
60
|
+
notifyListeners(state);
|
|
61
|
+
window.dispatchEvent(new CustomEvent("mutation-state-changed", { detail: state }));
|
|
62
|
+
return state;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Mark a mutation as successful with optional data. */
|
|
66
|
+
export function resolveMutation(id${T(ts, "string")}, data${T(ts, "unknown")} = null)${R(ts, "void")}{
|
|
67
|
+
updateMutationState(id, { status: "success", data, error: null });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Mark a mutation as failed with an error. */
|
|
71
|
+
export function rejectMutation(id${T(ts, "string")}, error${T(ts, "Error")})${R(ts, "void")}{
|
|
72
|
+
updateMutationState(id, { status: "error", error });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Get the current state of a mutation by ID. */
|
|
76
|
+
export function getMutationState(id${T(ts, "string")})${R(ts, "MutationState | undefined")}{
|
|
77
|
+
return mutations.get(id);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Remove a mutation's state from the tracker. */
|
|
81
|
+
export function clearMutationState(id${T(ts, "string")})${R(ts, "void")}{
|
|
82
|
+
mutations.delete(id);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Get all tracked mutations. */
|
|
86
|
+
export function getAllMutationStates()${R(ts, "MutationState[]")}{
|
|
87
|
+
return Array.from(mutations.values());
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Get the count of mutations in a given status. */
|
|
91
|
+
export function getMutationCount(status${T(ts, "MutationStatus")})${R(ts, "number")}{
|
|
92
|
+
let count = 0;
|
|
93
|
+
for (const state of mutations.values()) {
|
|
94
|
+
if (state.status === status) count++;
|
|
95
|
+
}
|
|
96
|
+
return count;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Subscribe to mutation state changes. Returns an unsubscribe function. */
|
|
100
|
+
export function onMutationStateChange(callback${T(ts, "(state: MutationState) => void")})${R(ts, "() => void")}{
|
|
101
|
+
const id = ++listenerId;
|
|
102
|
+
listeners.set(id, callback);
|
|
103
|
+
return () => { listeners.delete(id); };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function notifyListeners(state${T(ts, "MutationState")})${R(ts, "void")}{
|
|
107
|
+
for (const cb of listeners.values()) {
|
|
108
|
+
try {
|
|
109
|
+
cb(state);
|
|
110
|
+
} catch {
|
|
111
|
+
// Handle listener errors silently
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
`;
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=mutation-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutation-state.js","sourceRoot":"","sources":["../../src/runtime/mutation-state.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,YAAY,CAAC;AAElC,MAAM,UAAU,yBAAyB,CAAC,GAAmB;IAC3D,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC;IACxB,OAAO;;;;;;mGAM0F,GAAG;;;;;;;;;;;;;;;;;MAiBhG,CAAC,CAAC,EAAE,EAAE,QAAQ,CAAC;UACX,CAAC,CAAC,EAAE,EAAE,gBAAgB,CAAC;SACxB,CAAC,CAAC,EAAE,EAAE,cAAc,CAAC;QACtB,CAAC,CAAC,EAAE,EAAE,SAAS,CAAC;aACX,CAAC,CAAC,EAAE,EAAE,QAAQ,CAAC;;;;;;;;;kCASM,CAAC,CAAC,EAAE,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC,EAAE,EAAE,gBAAgB,CAAC,aAAa,CAAC,CAAC,EAAE,EAAE,eAAe,CAAC;;;;;;;;;;;;;;;wCAe9E,CAAC,CAAC,EAAE,EAAE,QAAQ,CAAC,YAAY,CAAC,CAAC,EAAE,EAAE,wBAAwB,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,sBAAsB,CAAC;;;;;;;;;;;oCAW/F,CAAC,CAAC,EAAE,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,SAAS,CAAC,WAAW,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC;;;;;mCAKjE,CAAC,CAAC,EAAE,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC;;;;;qCAKtD,CAAC,CAAC,EAAE,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,2BAA2B,CAAC;;;;;uCAKnD,CAAC,CAAC,EAAE,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC;;;;;wCAK/B,CAAC,CAAC,EAAE,EAAE,iBAAiB,CAAC;;;;;yCAKvB,CAAC,CAAC,EAAE,EAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,QAAQ,CAAC;;;;;;;;;gDASnC,CAAC,CAAC,EAAE,EAAE,gCAAgC,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,YAAY,CAAC;;;;;;gCAM9E,CAAC,CAAC,EAAE,EAAE,eAAe,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC;;;;;;;;;CAStE,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { T, R, PT, AS } from "./utils.js";
|
|
2
|
+
export function generatePushCode(ctx) {
|
|
3
|
+
const { ext, ts } = ctx;
|
|
4
|
+
const vapidKey = ctx.vapidKey;
|
|
5
|
+
return `/**
|
|
6
|
+
* Swoff Push Notifications
|
|
7
|
+
* Push subscription management with IndexedDB persistence.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import { subscribeToPush, unsubscribeFromPush, isSubscribed } from './swoff/realtime/notifications.${ext}';
|
|
11
|
+
*
|
|
12
|
+
* // Enable (triggers permission prompt)
|
|
13
|
+
* const subscription = await subscribeToPush();
|
|
14
|
+
* await fetch("/api/push/subscribe", {
|
|
15
|
+
* method: "POST",
|
|
16
|
+
* body: JSON.stringify(subscription.toJSON()),
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* // Disable
|
|
20
|
+
* await unsubscribeFromPush();
|
|
21
|
+
*
|
|
22
|
+
* Window events:
|
|
23
|
+
* push-permission-changed - Permission granted/denied (detail: { permission })
|
|
24
|
+
* push-subscription-changed - Subscribed/unsubscribed (detail: { subscribed })
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
const SUBSCRIPTION_DB = "swoff-push";
|
|
28
|
+
const SUBSCRIPTION_STORE = "subscription";
|
|
29
|
+
// Bump this when adding new indexes/stores for schema migration
|
|
30
|
+
const DB_VERSION = 1;
|
|
31
|
+
const VAPID_PUBLIC_KEY = ${JSON.stringify(vapidKey)};
|
|
32
|
+
|
|
33
|
+
let permissionState${T(ts, "NotificationPermission | undefined")} = typeof Notification !== "undefined" ? Notification.permission : undefined;
|
|
34
|
+
|
|
35
|
+
function openPushDB()${R(ts, "Promise<IDBDatabase>")}{
|
|
36
|
+
return new Promise${PT(ts, "IDBDatabase")}((resolve, reject) => {
|
|
37
|
+
const request = indexedDB.open(SUBSCRIPTION_DB, DB_VERSION);
|
|
38
|
+
request.onupgradeneeded = (e) => {
|
|
39
|
+
const db = (e.target${AS(ts, "IDBOpenDBRequest")}).result;
|
|
40
|
+
if (!db.objectStoreNames.contains(SUBSCRIPTION_STORE)) {
|
|
41
|
+
db.createObjectStore(SUBSCRIPTION_STORE, { keyPath: "id" });
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
request.onsuccess = (e) => resolve((e.target${AS(ts, "IDBOpenDBRequest")}).result);
|
|
45
|
+
request.onerror = (e) => reject((e.target${AS(ts, "IDBRequest")}).error);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Request notification permission from the user. Returns true if granted. */
|
|
50
|
+
export async function requestNotificationPermission()${R(ts, "Promise<boolean>")}{
|
|
51
|
+
if (permissionState === "granted") return true;
|
|
52
|
+
if (permissionState === "denied") return false;
|
|
53
|
+
|
|
54
|
+
const result = await Notification.requestPermission();
|
|
55
|
+
permissionState = result;
|
|
56
|
+
window.dispatchEvent(
|
|
57
|
+
new CustomEvent("push-permission-changed", { detail: { permission: result } }),
|
|
58
|
+
);
|
|
59
|
+
return result === "granted";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Get the current push subscription, or null if not subscribed. */
|
|
63
|
+
export async function getPushSubscription()${R(ts, "Promise<PushSubscription | null>")}{
|
|
64
|
+
try {
|
|
65
|
+
const registration = await navigator.serviceWorker.ready;
|
|
66
|
+
return await registration.pushManager.getSubscription();
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Subscribe to push notifications. Returns the subscription or null if permission denied. Uses the VAPID public key from your swoff.config.json. */
|
|
73
|
+
export async function subscribeToPush()${R(ts, "Promise<PushSubscription | null>")}{
|
|
74
|
+
const granted = await requestNotificationPermission();
|
|
75
|
+
if (!granted) return null;
|
|
76
|
+
|
|
77
|
+
const registration = await navigator.serviceWorker.ready;
|
|
78
|
+
const subscription = await registration.pushManager.subscribe({
|
|
79
|
+
userVisibleOnly: true,
|
|
80
|
+
applicationServerKey: urlBase64ToUint8Array(VAPID_PUBLIC_KEY)${AS(ts, "BufferSource")},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const db = await openPushDB();
|
|
84
|
+
const tx = db.transaction(SUBSCRIPTION_STORE, "readwrite");
|
|
85
|
+
tx.objectStore(SUBSCRIPTION_STORE).put({
|
|
86
|
+
id: "current",
|
|
87
|
+
endpoint: subscription.endpoint,
|
|
88
|
+
keys: subscription.toJSON().keys,
|
|
89
|
+
subscribedAt: Date.now(),
|
|
90
|
+
});
|
|
91
|
+
await new Promise<void>((resolve, reject) => {
|
|
92
|
+
tx.oncomplete = () => resolve();
|
|
93
|
+
tx.onerror = () => reject(tx.error);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
window.dispatchEvent(
|
|
97
|
+
new CustomEvent("push-subscription-changed", { detail: { subscribed: true } }),
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
return subscription;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Unsubscribe from push notifications. */
|
|
104
|
+
export async function unsubscribeFromPush()${R(ts, "Promise<void>")}{
|
|
105
|
+
const subscription = await getPushSubscription();
|
|
106
|
+
if (!subscription) return;
|
|
107
|
+
|
|
108
|
+
await subscription.unsubscribe();
|
|
109
|
+
|
|
110
|
+
const db = await openPushDB();
|
|
111
|
+
const tx = db.transaction(SUBSCRIPTION_STORE, "readwrite");
|
|
112
|
+
tx.objectStore(SUBSCRIPTION_STORE).delete("current");
|
|
113
|
+
await new Promise<void>((resolve, reject) => {
|
|
114
|
+
tx.oncomplete = () => resolve();
|
|
115
|
+
tx.onerror = () => reject(tx.error);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
window.dispatchEvent(
|
|
119
|
+
new CustomEvent("push-subscription-changed", { detail: { subscribed: false } }),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Check if the user is subscribed to push notifications. */
|
|
124
|
+
export async function isSubscribed()${R(ts, "Promise<boolean>")}{
|
|
125
|
+
const sub = await getPushSubscription();
|
|
126
|
+
return sub !== null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Convert a base64 VAPID public key to a Uint8Array for pushManager.subscribe(). */
|
|
130
|
+
function urlBase64ToUint8Array(base64String${T(ts, "string")})${R(ts, "Uint8Array")}{
|
|
131
|
+
const padding = "=".repeat((4 - (base64String.length % 4)) % 4);
|
|
132
|
+
const base64 = (base64String + padding).replace(/-/g, "+").replace(/_/g, "/");
|
|
133
|
+
const rawData = atob(base64);
|
|
134
|
+
return Uint8Array.from(rawData, (c) => c.charCodeAt(0));
|
|
135
|
+
}
|
|
136
|
+
`;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=push.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"push.js","sourceRoot":"","sources":["../../src/runtime/push.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,YAAY,CAAC;AAE1C,MAAM,UAAU,gBAAgB,CAAC,GAA0C;IACzE,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC;IACxB,MAAM,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC;IAC9B,OAAO;;;;;0GAKiG,GAAG;;;;;;;;;;;;;;;;;;;;;2BAqBlF,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC;;qBAE9B,CAAC,CAAC,EAAE,EAAE,oCAAoC,CAAC;;uBAEzC,CAAC,CAAC,EAAE,EAAE,sBAAsB,CAAC;sBAC9B,EAAE,CAAC,EAAE,EAAE,aAAa,CAAC;;;4BAGf,EAAE,CAAC,EAAE,EAAE,kBAAkB,CAAC;;;;;kDAKJ,EAAE,CAAC,EAAE,EAAE,kBAAkB,CAAC;+CAC7B,EAAE,CAAC,EAAE,EAAE,YAAY,CAAC;;;;;uDAKZ,CAAC,CAAC,EAAE,EAAE,kBAAkB,CAAC;;;;;;;;;;;;;6CAanC,CAAC,CAAC,EAAE,EAAE,kCAAkC,CAAC;;;;;;;;;;yCAU7C,CAAC,CAAC,EAAE,EAAE,kCAAkC,CAAC;;;;;;;mEAOf,EAAE,CAAC,EAAE,EAAE,cAAc,CAAC;;;;;;;;;;;;;;;;;;;;;;;;6CAwB5C,CAAC,CAAC,EAAE,EAAE,eAAe,CAAC;;;;;;;;;;;;;;;;;;;;sCAoB7B,CAAC,CAAC,EAAE,EAAE,kBAAkB,CAAC;;;;;;6CAMlB,CAAC,CAAC,EAAE,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,YAAY,CAAC;;;;;;CAMlF,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function generatePwaIndexCode(ctx) {
|
|
2
|
+
const { ext } = ctx;
|
|
3
|
+
return `/**
|
|
4
|
+
* Swoff PWA
|
|
5
|
+
* Re-exports from PWA sub-modules for convenience imports.
|
|
6
|
+
*/
|
|
7
|
+
export { setupPwaInstall } from "./injector.${ext}";
|
|
8
|
+
export { isInstallable, promptInstall } from "./prompt.${ext}";
|
|
9
|
+
`;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=pwa-index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pwa-index.js","sourceRoot":"","sources":["../../src/runtime/pwa-index.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,oBAAoB,CAAC,GAAmB;IACtD,MAAM,EAAE,GAAG,EAAE,GAAG,GAAG,CAAC;IAEpB,OAAO;;;;8CAIqC,GAAG;yDACQ,GAAG;CAC3D,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { R } from "./utils.js";
|
|
2
|
+
export function generatePwaInjectorCode(ctx) {
|
|
3
|
+
const { ext, ts } = ctx;
|
|
4
|
+
const preventLine = ctx.preventDefaultInstall ? " e.preventDefault();\n" : "";
|
|
5
|
+
return `/**
|
|
6
|
+
* Swoff PWA Injector
|
|
7
|
+
* Service worker installation event wiring for PWA installability.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import { setupPwaInstall } from './swoff/pwa/injector.${ext}';
|
|
11
|
+
* setupPwaInstall();
|
|
12
|
+
*
|
|
13
|
+
* Window properties:
|
|
14
|
+
* window.deferredInstallPrompt - The captured BeforeInstallPromptEvent
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Set up beforeinstallprompt and appinstalled event listeners. Call once at app startup. */
|
|
18
|
+
export function setupPwaInstall()${R(ts, "void")}{
|
|
19
|
+
window.addEventListener("beforeinstallprompt", (e) => {
|
|
20
|
+
window.deferredInstallPrompt = e as BeforeInstallPromptEvent;
|
|
21
|
+
window.pwaInstallable = true;
|
|
22
|
+
|
|
23
|
+
${preventLine}
|
|
24
|
+
window.dispatchEvent(
|
|
25
|
+
new CustomEvent("pwa-installable", {
|
|
26
|
+
detail: { isInstallable: true },
|
|
27
|
+
})
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
window.addEventListener("appinstalled", () => {
|
|
32
|
+
window.deferredInstallPrompt = null;
|
|
33
|
+
window.pwaInstallable = false;
|
|
34
|
+
|
|
35
|
+
window.dispatchEvent(
|
|
36
|
+
new CustomEvent("pwa-installed", {
|
|
37
|
+
detail: { outcome: "accepted" },
|
|
38
|
+
})
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
`;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=pwa-injector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pwa-injector.js","sourceRoot":"","sources":["../../src/runtime/pwa-injector.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,YAAY,CAAC;AAE/B,MAAM,UAAU,uBAAuB,CAAC,GAAwD;IAC9F,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC;IACxB,MAAM,WAAW,GAAG,GAAG,CAAC,qBAAqB,CAAC,CAAC,CAAC,2BAA2B,CAAC,CAAC,CAAC,EAAE,CAAC;IAEjF,OAAO;;;;;6DAKoD,GAAG;;;;;;;;mCAQ7B,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC;;;;;EAK9C,WAAW;;;;;;;;;;;;;;;;;;;CAmBZ,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { R } from "./utils.js";
|
|
2
|
+
export function generatePwaInstallCode(ctx) {
|
|
3
|
+
const { ext, ts } = ctx;
|
|
4
|
+
const preventLine = ctx.preventDefaultInstall ? " e.preventDefault();\n" : "";
|
|
5
|
+
return `/**
|
|
6
|
+
* Swoff PWA Install Support
|
|
7
|
+
* Manual install trigger, installability check, and event listener setup.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import { setupPwaInstall, isInstallable, promptInstall } from './swoff/pwa/prompt.${ext}';
|
|
11
|
+
*
|
|
12
|
+
* setupPwaInstall(); // sets up beforeinstallprompt and appinstalled listeners
|
|
13
|
+
*
|
|
14
|
+
* if (isInstallable()) {
|
|
15
|
+
* const result = await promptInstall();
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* Window properties:
|
|
19
|
+
* window.deferredInstallPrompt - The captured BeforeInstallPromptEvent
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** Set up beforeinstallprompt and appinstalled event listeners. Call once at app startup. */
|
|
23
|
+
export function setupPwaInstall()${R(ts, "void")}{
|
|
24
|
+
window.addEventListener("beforeinstallprompt", (e) => {
|
|
25
|
+
window.deferredInstallPrompt = e as BeforeInstallPromptEvent;
|
|
26
|
+
window.pwaInstallable = true;
|
|
27
|
+
|
|
28
|
+
${preventLine}
|
|
29
|
+
window.dispatchEvent(
|
|
30
|
+
new CustomEvent("pwa-installable", {
|
|
31
|
+
detail: { isInstallable: true },
|
|
32
|
+
})
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
window.addEventListener("appinstalled", () => {
|
|
37
|
+
window.deferredInstallPrompt = null;
|
|
38
|
+
window.pwaInstallable = false;
|
|
39
|
+
|
|
40
|
+
window.dispatchEvent(
|
|
41
|
+
new CustomEvent("pwa-installed", {
|
|
42
|
+
detail: { outcome: "accepted" },
|
|
43
|
+
})
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Check if the PWA install prompt is available (user has met install criteria). */
|
|
49
|
+
export function isInstallable()${R(ts, "boolean")}{
|
|
50
|
+
return !!window.deferredInstallPrompt;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Show the browser's native install prompt. Throws if prompt is not available. */
|
|
54
|
+
export async function promptInstall()${R(ts, "Promise<{ outcome: string }>")}{
|
|
55
|
+
if (!window.deferredInstallPrompt) {
|
|
56
|
+
throw new Error("Install prompt not available");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const promptEvent = window.deferredInstallPrompt;
|
|
60
|
+
await promptEvent.prompt();
|
|
61
|
+
|
|
62
|
+
const choice = await promptEvent.userChoice;
|
|
63
|
+
|
|
64
|
+
if (choice.outcome === "accepted") {
|
|
65
|
+
window.dispatchEvent(
|
|
66
|
+
new CustomEvent("pwa-installed", {
|
|
67
|
+
detail: { outcome: "accepted" },
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
} else {
|
|
71
|
+
window.dispatchEvent(
|
|
72
|
+
new CustomEvent("pwa-dismissed", {
|
|
73
|
+
detail: { outcome: "dismissed" },
|
|
74
|
+
})
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
window.deferredInstallPrompt = null;
|
|
79
|
+
return choice;
|
|
80
|
+
}
|
|
81
|
+
`;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=pwa-install.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pwa-install.js","sourceRoot":"","sources":["../../src/runtime/pwa-install.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,YAAY,CAAC;AAE/B,MAAM,UAAU,sBAAsB,CAAC,GAAwD;IAC7F,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC;IACxB,MAAM,WAAW,GAAG,GAAG,CAAC,qBAAqB,CAAC,CAAC,CAAC,2BAA2B,CAAC,CAAC,CAAC,EAAE,CAAC;IAEjF,OAAO;;;;;yFAKgF,GAAG;;;;;;;;;;;;;mCAazD,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC;;;;;EAK9C,WAAW;;;;;;;;;;;;;;;;;;;;;iCAqBoB,CAAC,CAAC,EAAE,EAAE,SAAS,CAAC;;;;;uCAKV,CAAC,CAAC,EAAE,EAAE,8BAA8B,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2B3E,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { R } from "./utils.js";
|
|
2
|
+
export function generatePwaPromptCode(ctx) {
|
|
3
|
+
const { ext, ts } = ctx;
|
|
4
|
+
return `/**
|
|
5
|
+
* Swoff PWA Install Prompt
|
|
6
|
+
* Installability check and manual prompt trigger.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* import { isInstallable, promptInstall } from './swoff/pwa/prompt.${ext}';
|
|
10
|
+
*
|
|
11
|
+
* if (isInstallable()) {
|
|
12
|
+
* const result = await promptInstall();
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* Window properties:
|
|
16
|
+
* window.deferredInstallPrompt - The captured BeforeInstallPromptEvent
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** Check if the PWA install prompt is available (user has met install criteria). */
|
|
20
|
+
export function isInstallable()${R(ts, "boolean")}{
|
|
21
|
+
return !!window.deferredInstallPrompt;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Show the browser's native install prompt. Throws if prompt is not available. */
|
|
25
|
+
export async function promptInstall()${R(ts, "Promise<{ outcome: string }>")}{
|
|
26
|
+
if (!window.deferredInstallPrompt) {
|
|
27
|
+
throw new Error("Install prompt not available");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const promptEvent = window.deferredInstallPrompt;
|
|
31
|
+
await promptEvent.prompt();
|
|
32
|
+
|
|
33
|
+
const choice = await promptEvent.userChoice;
|
|
34
|
+
|
|
35
|
+
if (choice.outcome === "accepted") {
|
|
36
|
+
window.dispatchEvent(
|
|
37
|
+
new CustomEvent("pwa-installed", {
|
|
38
|
+
detail: { outcome: "accepted" },
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
} else {
|
|
42
|
+
window.dispatchEvent(
|
|
43
|
+
new CustomEvent("pwa-dismissed", {
|
|
44
|
+
detail: { outcome: "dismissed" },
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
window.deferredInstallPrompt = null;
|
|
50
|
+
return choice;
|
|
51
|
+
}
|
|
52
|
+
`;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=pwa-prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pwa-prompt.js","sourceRoot":"","sources":["../../src/runtime/pwa-prompt.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,YAAY,CAAC;AAE/B,MAAM,UAAU,qBAAqB,CAAC,GAAmB;IACvD,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC;IAExB,OAAO;;;;;wEAK+D,GAAG;;;;;;;;;;;iCAW1C,CAAC,CAAC,EAAE,EAAE,SAAS,CAAC;;;;;uCAKV,CAAC,CAAC,EAAE,EAAE,8BAA8B,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2B3E,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { T, R } from "./utils.js";
|
|
2
|
+
export function generateResetCode(ctx) {
|
|
3
|
+
const { ext, ts } = ctx;
|
|
4
|
+
return `/**
|
|
5
|
+
* Swoff Reset
|
|
6
|
+
* Nuclear option: wipe all swoff data (caches, IndexedDB, localStorage),
|
|
7
|
+
* unregister the service worker, then re-register from scratch.
|
|
8
|
+
*
|
|
9
|
+
* Usage:
|
|
10
|
+
* import { resetSwoff } from './swoff/reset.${ext}';
|
|
11
|
+
*
|
|
12
|
+
* // Full reset (default)
|
|
13
|
+
* const result = await resetSwoff();
|
|
14
|
+
* console.log(result.unregistered, result.reregistered, result.warnings);
|
|
15
|
+
*
|
|
16
|
+
* // Selective reset — keep caches but reset IDB + SW
|
|
17
|
+
* await resetSwoff({ clearCache: false });
|
|
18
|
+
*
|
|
19
|
+
* Events dispatched:
|
|
20
|
+
* swoff:reset-start — before any cleanup begins
|
|
21
|
+
* swoff:reset-complete — after everything finishes (detail: { unregistered, reregistered, warnings })
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { initServiceWorker } from "./sw/injector.${ext}";
|
|
25
|
+
|
|
26
|
+
export interface ResetSwoffOptions {
|
|
27
|
+
/** Clear all Cache Storage caches (default: true) */
|
|
28
|
+
clearCache${T(ts, "boolean")};
|
|
29
|
+
/** Delete all swoff-* IndexedDB databases (default: true) */
|
|
30
|
+
clearIdb${T(ts, "boolean")};
|
|
31
|
+
/** Clear localStorage keys like swRegisteredVersion (default: true) */
|
|
32
|
+
clearStorage${T(ts, "boolean")};
|
|
33
|
+
/** Unregister SW then re-register (default: true) */
|
|
34
|
+
unregisterSW${T(ts, "boolean")};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const DEFAULT_OPTIONS: ResetSwoffOptions = {
|
|
38
|
+
clearCache: true,
|
|
39
|
+
clearIdb: true,
|
|
40
|
+
clearStorage: true,
|
|
41
|
+
unregisterSW: true,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const KNOWN_DB_NAMES = [
|
|
45
|
+
"swoff-auth",
|
|
46
|
+
"swoff-auth-user",
|
|
47
|
+
"swoff-queue",
|
|
48
|
+
"swoff-cache-tags",
|
|
49
|
+
"swoff-push",
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
/** Delete all known swoff-* IndexedDB databases. Also tries indexedDB.databases() API to catch any unknown swoff-* databases. */
|
|
53
|
+
async function deleteSwoffDatabases(warnings${T(ts, "string[]")})${R(ts, "Promise<void>")}{
|
|
54
|
+
const dbNames = [...KNOWN_DB_NAMES];
|
|
55
|
+
|
|
56
|
+
// Use the modern databases() API if available to catch unknown swoff-* DBs
|
|
57
|
+
try {
|
|
58
|
+
const allDbs = await indexedDB.databases?.();
|
|
59
|
+
if (allDbs) {
|
|
60
|
+
for (const db of allDbs) {
|
|
61
|
+
if (db.name && db.name.startsWith("swoff-") && !dbNames.includes(db.name)) {
|
|
62
|
+
dbNames.push(db.name);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} catch { /* not supported — use known list */ }
|
|
67
|
+
|
|
68
|
+
for (const name of dbNames) {
|
|
69
|
+
try {
|
|
70
|
+
indexedDB.deleteDatabase(name);
|
|
71
|
+
} catch (e) {
|
|
72
|
+
warnings.push(\`Failed to delete database "\${name}": \${e}\`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Nuclear reset: wipes all swoff-persisted data, unregisters the SW, then re-registers from scratch. Returns detailed results. */
|
|
78
|
+
export async function resetSwoff(opts${T(ts, "Partial<ResetSwoffOptions>")} = {})${R(ts, "Promise<{ unregistered: boolean; reregistered: boolean; warnings: string[] }>")}{
|
|
79
|
+
const options${T(ts, "ResetSwoffOptions")} = { ...DEFAULT_OPTIONS, ...opts };
|
|
80
|
+
const warnings${T(ts, "string[]")} = [];
|
|
81
|
+
|
|
82
|
+
window.dispatchEvent(new CustomEvent("swoff:reset-start"));
|
|
83
|
+
|
|
84
|
+
// 1. Clear Cache Storage
|
|
85
|
+
if (options.clearCache) {
|
|
86
|
+
try {
|
|
87
|
+
if (typeof caches !== "undefined") {
|
|
88
|
+
const keys = await caches.keys();
|
|
89
|
+
await Promise.all(keys.map((k) => caches.delete(k)));
|
|
90
|
+
}
|
|
91
|
+
} catch (e) {
|
|
92
|
+
warnings.push(\`Failed to clear caches: \${e}\`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 2. Delete IndexedDB databases
|
|
97
|
+
if (options.clearIdb) {
|
|
98
|
+
await deleteSwoffDatabases(warnings);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 3. Clear localStorage
|
|
102
|
+
if (options.clearStorage) {
|
|
103
|
+
try {
|
|
104
|
+
localStorage.removeItem("swRegisteredVersion");
|
|
105
|
+
} catch (e) {
|
|
106
|
+
warnings.push(\`Failed to clear localStorage: \${e}\`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 4. Unregister SW
|
|
111
|
+
let unregistered = false;
|
|
112
|
+
if (options.unregisterSW) {
|
|
113
|
+
try {
|
|
114
|
+
if ("serviceWorker" in navigator) {
|
|
115
|
+
const reg = await navigator.serviceWorker.getRegistration();
|
|
116
|
+
if (reg) {
|
|
117
|
+
unregistered = await reg.unregister();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
} catch (e) {
|
|
121
|
+
warnings.push(\`Failed to unregister SW: \${e}\`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Always clear the version marker before re-registering so initServiceWorker
|
|
125
|
+
// detects a fresh install rather than trying to reuse a stale registration
|
|
126
|
+
try {
|
|
127
|
+
localStorage.removeItem("swRegisteredVersion");
|
|
128
|
+
} catch { /* ignore */ }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 5. Re-register SW
|
|
132
|
+
let reregistered = false;
|
|
133
|
+
if (options.unregisterSW) {
|
|
134
|
+
try {
|
|
135
|
+
await initServiceWorker();
|
|
136
|
+
reregistered = true;
|
|
137
|
+
} catch (e) {
|
|
138
|
+
warnings.push(\`Failed to re-register SW: \${e}\`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const result = { unregistered, reregistered, warnings };
|
|
143
|
+
window.dispatchEvent(new CustomEvent("swoff:reset-complete", { detail: result }));
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
`;
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=reset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reset.js","sourceRoot":"","sources":["../../src/runtime/reset.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,YAAY,CAAC;AAElC,MAAM,UAAU,iBAAiB,CAAC,GAAmB;IACnD,MAAM,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,GAAG,CAAC;IACxB,OAAO;;;;;;iDAMwC,GAAG;;;;;;;;;;;;;;mDAcD,GAAG;;;;cAIxC,CAAC,CAAC,EAAE,EAAE,SAAS,CAAC;;YAElB,CAAC,CAAC,EAAE,EAAE,SAAS,CAAC;;gBAEZ,CAAC,CAAC,EAAE,EAAE,SAAS,CAAC;;gBAEhB,CAAC,CAAC,EAAE,EAAE,SAAS,CAAC;;;;;;;;;;;;;;;;;;;8CAmBc,CAAC,CAAC,EAAE,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,eAAe,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;uCAyBlD,CAAC,CAAC,EAAE,EAAE,4BAA4B,CAAE,SAAS,CAAC,CAAC,EAAE,EAAE,+EAA+E,CAAC;iBACzJ,CAAC,CAAC,EAAE,EAAE,mBAAmB,CAAC;kBACzB,CAAC,CAAC,EAAE,EAAE,UAAU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkElC,CAAC;AACF,CAAC"}
|