@serwist/window 9.5.6 → 9.5.8
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/chunks/isCurrentPageOutOfScope-vi3IAsNY.js +10 -0
- package/dist/chunks/isCurrentPageOutOfScope-vi3IAsNY.js.map +1 -0
- package/dist/index.d.mts +246 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.internal.d.mts +5 -0
- package/dist/index.internal.d.mts.map +1 -0
- package/dist/index.internal.mjs +2 -0
- package/dist/index.mjs +545 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +13 -14
- package/dist/Serwist.d.ts +0 -281
- package/dist/Serwist.d.ts.map +0 -1
- package/dist/index.d.ts +0 -5
- package/dist/index.d.ts.map +0 -1
- package/dist/index.internal.d.ts +0 -3
- package/dist/index.internal.d.ts.map +0 -1
- package/dist/index.internal.js +0 -7
- package/dist/index.js +0 -304
- package/dist/messageSW.d.ts +0 -14
- package/dist/messageSW.d.ts.map +0 -1
- package/dist/utils/SerwistEvent.d.ts +0 -39
- package/dist/utils/SerwistEvent.d.ts.map +0 -1
- package/dist/utils/SerwistEventTarget.d.ts +0 -38
- package/dist/utils/SerwistEventTarget.d.ts.map +0 -1
- package/dist/utils/isCurrentPageOutOfScope.d.ts +0 -2
- package/dist/utils/isCurrentPageOutOfScope.d.ts.map +0 -1
- package/dist/utils/urlsMatch.d.ts +0 -11
- package/dist/utils/urlsMatch.d.ts.map +0 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/utils/isCurrentPageOutOfScope.ts
|
|
2
|
+
const isCurrentPageOutOfScope = (scope) => {
|
|
3
|
+
const scopeURL = new URL(scope, document.baseURI);
|
|
4
|
+
const scopeURLBasePath = new URL("./", scopeURL.href).pathname;
|
|
5
|
+
return !location.pathname.startsWith(scopeURLBasePath);
|
|
6
|
+
};
|
|
7
|
+
//#endregion
|
|
8
|
+
export { isCurrentPageOutOfScope as t };
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=isCurrentPageOutOfScope-vi3IAsNY.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isCurrentPageOutOfScope-vi3IAsNY.js","names":[],"sources":["../../src/utils/isCurrentPageOutOfScope.ts"],"sourcesContent":["export const isCurrentPageOutOfScope = (scope: string) => {\n const scopeURL = new URL(scope, document.baseURI);\n const scopeURLBasePath = new URL(\"./\", scopeURL.href).pathname;\n return !location.pathname.startsWith(scopeURLBasePath);\n};\n"],"mappings":";AAAA,MAAa,2BAA2B,UAAkB;CACxD,MAAM,WAAW,IAAI,IAAI,OAAO,SAAS,QAAQ;CACjD,MAAM,mBAAmB,IAAI,IAAI,MAAM,SAAS,KAAK,CAAC;AACtD,QAAO,CAAC,SAAS,SAAS,WAAW,iBAAiB"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
//#region src/messageSW.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Sends a data object to a service worker via `postMessage` and resolves with
|
|
4
|
+
* a response (if any).
|
|
5
|
+
*
|
|
6
|
+
* A response can be sent by calling `event.ports[0].postMessage(...)`, which will
|
|
7
|
+
* resolve the promise returned by `messageSW()`. If no response is sent, the promise
|
|
8
|
+
* will never resolve.
|
|
9
|
+
*
|
|
10
|
+
* @param sw The service worker to send the message to.
|
|
11
|
+
* @param data An object to send to the service worker.
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
declare const messageSW: (sw: ServiceWorker, data: any) => Promise<any>;
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/utils/SerwistEvent.d.ts
|
|
17
|
+
/**
|
|
18
|
+
* A minimal `Event` subclass shim.
|
|
19
|
+
* This doesn't *actually* subclass `Event` because not all browsers support
|
|
20
|
+
* constructable `EventTarget`, and using a real `Event` will error.
|
|
21
|
+
* @private
|
|
22
|
+
*/
|
|
23
|
+
declare class SerwistEvent<K extends keyof SerwistEventMap> {
|
|
24
|
+
type: K;
|
|
25
|
+
target?: SerwistEventTarget;
|
|
26
|
+
sw?: ServiceWorker;
|
|
27
|
+
originalEvent?: Event;
|
|
28
|
+
isExternal?: boolean;
|
|
29
|
+
constructor(type: K, props: Omit<SerwistEventMap[K], "target" | "type">);
|
|
30
|
+
}
|
|
31
|
+
interface SerwistMessageEvent extends SerwistEvent<"message"> {
|
|
32
|
+
data: any;
|
|
33
|
+
originalEvent: Event;
|
|
34
|
+
ports: readonly MessagePort[];
|
|
35
|
+
}
|
|
36
|
+
interface SerwistLifecycleEvent extends SerwistEvent<keyof SerwistLifecycleEventMap> {
|
|
37
|
+
isUpdate?: boolean;
|
|
38
|
+
}
|
|
39
|
+
interface SerwistLifecycleWaitingEvent extends SerwistLifecycleEvent {
|
|
40
|
+
wasWaitingBeforeRegister?: boolean;
|
|
41
|
+
}
|
|
42
|
+
interface SerwistLifecycleEventMap {
|
|
43
|
+
installing: SerwistLifecycleEvent;
|
|
44
|
+
installed: SerwistLifecycleEvent;
|
|
45
|
+
waiting: SerwistLifecycleWaitingEvent;
|
|
46
|
+
activating: SerwistLifecycleEvent;
|
|
47
|
+
activated: SerwistLifecycleEvent;
|
|
48
|
+
controlling: SerwistLifecycleEvent;
|
|
49
|
+
redundant: SerwistLifecycleEvent;
|
|
50
|
+
}
|
|
51
|
+
interface SerwistEventMap extends SerwistLifecycleEventMap {
|
|
52
|
+
message: SerwistMessageEvent;
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/utils/SerwistEventTarget.d.ts
|
|
56
|
+
/**
|
|
57
|
+
* A minimal `EventTarget` shim.
|
|
58
|
+
* This is necessary because not all browsers support constructable
|
|
59
|
+
* `EventTarget`, so using a real `EventTarget` will error.
|
|
60
|
+
* @private
|
|
61
|
+
*/
|
|
62
|
+
declare class SerwistEventTarget {
|
|
63
|
+
private readonly _eventListenerRegistry;
|
|
64
|
+
/**
|
|
65
|
+
* @param type
|
|
66
|
+
* @param listener
|
|
67
|
+
* @private
|
|
68
|
+
*/
|
|
69
|
+
addEventListener<K extends keyof SerwistEventMap>(type: K, listener: (event: SerwistEventMap[K]) => any): void;
|
|
70
|
+
/**
|
|
71
|
+
* @param type
|
|
72
|
+
* @param listener
|
|
73
|
+
* @private
|
|
74
|
+
*/
|
|
75
|
+
removeEventListener<K extends keyof SerwistEventMap>(type: K, listener: (event: SerwistEventMap[K]) => any): void;
|
|
76
|
+
/**
|
|
77
|
+
* @param event
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
dispatchEvent(event: SerwistEvent<any>): void;
|
|
81
|
+
/**
|
|
82
|
+
* Returns a Set of listeners associated with the passed event type.
|
|
83
|
+
* If no handlers have been registered, an empty Set is returned.
|
|
84
|
+
*
|
|
85
|
+
* @param type The event type.
|
|
86
|
+
* @returns An array of handler functions.
|
|
87
|
+
* @private
|
|
88
|
+
*/
|
|
89
|
+
private _getEventListenersByType;
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/Serwist.d.ts
|
|
93
|
+
/**
|
|
94
|
+
* A class to aid in handling service worker registration, updates, and
|
|
95
|
+
* reacting to service worker lifecycle events.
|
|
96
|
+
*
|
|
97
|
+
* @fires `@serwist/window.Serwist.message`
|
|
98
|
+
* @fires `@serwist/window.Serwist.installed`
|
|
99
|
+
* @fires `@serwist/window.Serwist.waiting`
|
|
100
|
+
* @fires `@serwist/window.Serwist.controlling`
|
|
101
|
+
* @fires `@serwist/window.Serwist.activated`
|
|
102
|
+
* @fires `@serwist/window.Serwist.redundant`
|
|
103
|
+
*/
|
|
104
|
+
declare class Serwist extends SerwistEventTarget {
|
|
105
|
+
private readonly _scriptURL;
|
|
106
|
+
private readonly _registerOptions;
|
|
107
|
+
private _updateFoundCount;
|
|
108
|
+
private readonly _swDeferred;
|
|
109
|
+
private readonly _activeDeferred;
|
|
110
|
+
private readonly _controllingDeferred;
|
|
111
|
+
private _registrationTime;
|
|
112
|
+
private _isUpdate?;
|
|
113
|
+
private _compatibleControllingSW?;
|
|
114
|
+
private _registration?;
|
|
115
|
+
private _sw?;
|
|
116
|
+
private readonly _ownSWs;
|
|
117
|
+
private _externalSW?;
|
|
118
|
+
private _waitingTimeout?;
|
|
119
|
+
/**
|
|
120
|
+
* Creates a new Serwist instance with a script URL and service worker
|
|
121
|
+
* options. The script URL and options are the same as those used when
|
|
122
|
+
* calling [navigator.serviceWorker.register(scriptURL, options)](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register).
|
|
123
|
+
*
|
|
124
|
+
* @param scriptURL The service worker script associated with this instance. Using a
|
|
125
|
+
* [`TrustedScriptURL`](https://developer.mozilla.org/en-US/docs/Web/API/TrustedScriptURL) is supported.
|
|
126
|
+
* @param registerOptions The service worker options associated with this instance.
|
|
127
|
+
*/
|
|
128
|
+
constructor(scriptURL: string | TrustedScriptURL, registerOptions?: RegistrationOptions);
|
|
129
|
+
/**
|
|
130
|
+
* Registers a service worker for this instances script URL and service
|
|
131
|
+
* worker options. By default this method delays registration until after
|
|
132
|
+
* the window has loaded.
|
|
133
|
+
*
|
|
134
|
+
* @param options
|
|
135
|
+
*/
|
|
136
|
+
register({
|
|
137
|
+
immediate
|
|
138
|
+
}?: {
|
|
139
|
+
/**
|
|
140
|
+
* Setting this to true will register the service worker immediately,
|
|
141
|
+
* even if the window has not loaded (not recommended).
|
|
142
|
+
*/
|
|
143
|
+
immediate?: boolean;
|
|
144
|
+
}): Promise<ServiceWorkerRegistration | undefined>;
|
|
145
|
+
/**
|
|
146
|
+
* Checks for updates of the registered service worker.
|
|
147
|
+
*/
|
|
148
|
+
update(): Promise<void>;
|
|
149
|
+
/**
|
|
150
|
+
* Resolves to the service worker registered by this instance as soon as it
|
|
151
|
+
* is active. If a service worker was already controlling at registration
|
|
152
|
+
* time then it will resolve to that if the script URLs (and optionally
|
|
153
|
+
* script versions) match, otherwise it will wait until an update is found
|
|
154
|
+
* and activates.
|
|
155
|
+
*
|
|
156
|
+
* @returns
|
|
157
|
+
*/
|
|
158
|
+
get active(): Promise<ServiceWorker>;
|
|
159
|
+
/**
|
|
160
|
+
* Resolves to the service worker registered by this instance as soon as it
|
|
161
|
+
* is controlling the page. If a service worker was already controlling at
|
|
162
|
+
* registration time then it will resolve to that if the script URLs (and
|
|
163
|
+
* optionally script versions) match, otherwise it will wait until an update
|
|
164
|
+
* is found and starts controlling the page.
|
|
165
|
+
* Note: the first time a service worker is installed it will active but
|
|
166
|
+
* not start controlling the page unless `clients.claim()` is called in the
|
|
167
|
+
* service worker.
|
|
168
|
+
*
|
|
169
|
+
* @returns
|
|
170
|
+
*/
|
|
171
|
+
get controlling(): Promise<ServiceWorker>;
|
|
172
|
+
/**
|
|
173
|
+
* Resolves with a reference to a service worker that matches the script URL
|
|
174
|
+
* of this instance, as soon as it's available.
|
|
175
|
+
*
|
|
176
|
+
* If, at registration time, there's already an active or waiting service
|
|
177
|
+
* worker with a matching script URL, it will be used (with the waiting
|
|
178
|
+
* service worker taking precedence over the active service worker if both
|
|
179
|
+
* match, since the waiting service worker would have been registered more
|
|
180
|
+
* recently).
|
|
181
|
+
* If there's no matching active or waiting service worker at registration
|
|
182
|
+
* time then the promise will not resolve until an update is found and starts
|
|
183
|
+
* installing, at which point the installing service worker is used.
|
|
184
|
+
*
|
|
185
|
+
* @returns
|
|
186
|
+
*/
|
|
187
|
+
getSW(): Promise<ServiceWorker>;
|
|
188
|
+
/**
|
|
189
|
+
* Sends the passed data object to the service worker registered by this
|
|
190
|
+
* instance (via `getSW`) and resolves with a response (if any).
|
|
191
|
+
*
|
|
192
|
+
* A response can be sent by calling `event.ports[0].postMessage(...)`, which will
|
|
193
|
+
* resolve the promise returned by `messageSW()`. If no response is sent, the promise
|
|
194
|
+
* will never resolve.
|
|
195
|
+
*
|
|
196
|
+
* @param data An object to send to the service worker
|
|
197
|
+
* @returns
|
|
198
|
+
*/
|
|
199
|
+
messageSW(data: any): Promise<any>;
|
|
200
|
+
/**
|
|
201
|
+
* Sends a `{ type: "SKIP_WAITING" }` message to the service worker that is
|
|
202
|
+
* currently waiting and associated with the current registration.
|
|
203
|
+
*
|
|
204
|
+
* If there is no current registration, or no service worker is waiting,
|
|
205
|
+
* calling this will have no effect.
|
|
206
|
+
*/
|
|
207
|
+
messageSkipWaiting(): void;
|
|
208
|
+
/**
|
|
209
|
+
* Checks for a service worker already controlling the page and returns
|
|
210
|
+
* it if its script URL matches.
|
|
211
|
+
*
|
|
212
|
+
* @private
|
|
213
|
+
* @returns
|
|
214
|
+
*/
|
|
215
|
+
private _getControllingSWIfCompatible;
|
|
216
|
+
/**
|
|
217
|
+
* Registers a service worker for this instances script URL and register
|
|
218
|
+
* options and tracks the time registration was complete.
|
|
219
|
+
*
|
|
220
|
+
* @private
|
|
221
|
+
*/
|
|
222
|
+
private _registerScript;
|
|
223
|
+
/**
|
|
224
|
+
* @private
|
|
225
|
+
* @param originalEvent
|
|
226
|
+
*/
|
|
227
|
+
private readonly _onUpdateFound;
|
|
228
|
+
/**
|
|
229
|
+
* @private
|
|
230
|
+
* @param originalEvent
|
|
231
|
+
*/
|
|
232
|
+
private readonly _onStateChange;
|
|
233
|
+
/**
|
|
234
|
+
* @private
|
|
235
|
+
* @param originalEvent
|
|
236
|
+
*/
|
|
237
|
+
private readonly _onControllerChange;
|
|
238
|
+
/**
|
|
239
|
+
* @private
|
|
240
|
+
* @param originalEvent
|
|
241
|
+
*/
|
|
242
|
+
private readonly _onMessage;
|
|
243
|
+
}
|
|
244
|
+
//#endregion
|
|
245
|
+
export { Serwist, SerwistEvent, SerwistEventMap, SerwistLifecycleEvent, SerwistLifecycleEventMap, SerwistLifecycleWaitingEvent, SerwistMessageEvent, messageSW };
|
|
246
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/messageSW.ts","../src/utils/SerwistEvent.ts","../src/utils/SerwistEventTarget.ts","../src/Serwist.ts"],"mappings":";;AAoBA;;;;;;;;;;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