@php-wasm/web-service-worker 0.1.61 → 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/web-service-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "PHP.wasm – service worker utils",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,8 +23,12 @@
|
|
|
23
23
|
"type": "module",
|
|
24
24
|
"main": "src/index.js",
|
|
25
25
|
"types": "src/index.d.ts",
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "6890ff9243f9a10f0b86755fead101647a8c8535",
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=16.15.1",
|
|
29
|
+
"npm": ">=8.11.0"
|
|
30
|
+
},
|
|
27
31
|
"dependencies": {
|
|
28
|
-
"@php-wasm/scopes": "0.
|
|
32
|
+
"@php-wasm/scopes": "0.3.0"
|
|
29
33
|
}
|
|
30
34
|
}
|
|
@@ -23,37 +23,13 @@ export declare function convertFetchEventToPHPRequest(event: FetchEvent): Promis
|
|
|
23
23
|
* what this function uses to broadcast the message.
|
|
24
24
|
*
|
|
25
25
|
* @param message The message to broadcast.
|
|
26
|
-
* @param scope Target worker
|
|
26
|
+
* @param scope Target web worker scope.
|
|
27
27
|
* @returns The request ID to receive the reply.
|
|
28
28
|
*/
|
|
29
29
|
export declare function broadcastMessageExpectReply(message: any, scope: string): Promise<number>;
|
|
30
30
|
interface ServiceWorkerConfiguration {
|
|
31
|
-
/**
|
|
32
|
-
* The version of the service worker – exposed via the /version endpoint.
|
|
33
|
-
*
|
|
34
|
-
* This is used by the frontend app to know whether it's time to update
|
|
35
|
-
* the service worker registration.
|
|
36
|
-
*/
|
|
37
|
-
version: string | (() => string);
|
|
38
31
|
handleRequest?: (event: FetchEvent) => Promise<Response> | undefined;
|
|
39
32
|
}
|
|
40
|
-
/**
|
|
41
|
-
* Guesses whether the given path looks like a PHP file.
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```js
|
|
45
|
-
* seemsLikeAPHPRequestHandlerPath('/index.php') // true
|
|
46
|
-
* seemsLikeAPHPRequestHandlerPath('/index.php') // true
|
|
47
|
-
* seemsLikeAPHPRequestHandlerPath('/index.php/foo/bar') // true
|
|
48
|
-
* seemsLikeAPHPRequestHandlerPath('/index.html') // false
|
|
49
|
-
* seemsLikeAPHPRequestHandlerPath('/index.html/foo/bar') // false
|
|
50
|
-
* seemsLikeAPHPRequestHandlerPath('/') // true
|
|
51
|
-
* ```
|
|
52
|
-
*
|
|
53
|
-
* @param path The path to check.
|
|
54
|
-
* @returns Whether the path seems like a PHP server path.
|
|
55
|
-
*/
|
|
56
|
-
export declare function seemsLikeAPHPRequestHandlerPath(path: string): boolean;
|
|
57
33
|
/**
|
|
58
34
|
* Copy a request with custom overrides.
|
|
59
35
|
*
|
|
@@ -7,54 +7,13 @@ import { getURLScope, isURLScoped, removeURLScope, setURLScope, } from '@php-was
|
|
|
7
7
|
* @param config
|
|
8
8
|
*/
|
|
9
9
|
export function initializeServiceWorker(config) {
|
|
10
|
-
const {
|
|
11
|
-
/**
|
|
12
|
-
* Enable the client app to force-update the service worker
|
|
13
|
-
* registration.
|
|
14
|
-
*/
|
|
15
|
-
self.addEventListener('message', (event) => {
|
|
16
|
-
if (!event.data) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
if (event.data === 'skip-waiting') {
|
|
20
|
-
self.skipWaiting();
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
/**
|
|
24
|
-
* Ensure the client gets claimed by this service worker right after the registration.
|
|
25
|
-
*
|
|
26
|
-
* Only requests from the "controlled" pages are resolved via the fetch listener below.
|
|
27
|
-
* However, simply registering the worker is not enough to make it the "controller" of
|
|
28
|
-
* the current page. The user still has to reload the page. If they don't an iframe
|
|
29
|
-
* pointing to /index.php will show a 404 message instead of a homepage.
|
|
30
|
-
*
|
|
31
|
-
* This activation handles saves the user reloading the page after the initial confusion.
|
|
32
|
-
* It immediately makes this worker the controller of any client that registers it.
|
|
33
|
-
*/
|
|
34
|
-
self.addEventListener('activate', (event) => {
|
|
35
|
-
// eslint-disable-next-line no-undef
|
|
36
|
-
event.waitUntil(self.clients.claim());
|
|
37
|
-
});
|
|
10
|
+
const { handleRequest = defaultRequestHandler } = config;
|
|
38
11
|
/**
|
|
39
12
|
* The main method. It captures the requests and loop them back to the
|
|
40
13
|
* Worker Thread using the Loopback request
|
|
41
14
|
*/
|
|
42
15
|
self.addEventListener('fetch', (event) => {
|
|
43
16
|
const url = new URL(event.request.url);
|
|
44
|
-
// Provide a custom JSON response in the special /version endpoint
|
|
45
|
-
// so the frontend app can know whether it's time to update the
|
|
46
|
-
// service worker registration.
|
|
47
|
-
if (url.pathname === '/version') {
|
|
48
|
-
event.preventDefault();
|
|
49
|
-
const currentVersion = typeof version === 'function' ? version() : version;
|
|
50
|
-
event.respondWith(new Response(JSON.stringify({ version: currentVersion }), {
|
|
51
|
-
headers: {
|
|
52
|
-
'Content-Type': 'application/json',
|
|
53
|
-
},
|
|
54
|
-
status: 200,
|
|
55
|
-
}));
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
17
|
// Don't handle requests to the service worker script itself.
|
|
59
18
|
if (url.pathname.startsWith(self.location.pathname)) {
|
|
60
19
|
return;
|
|
@@ -85,13 +44,15 @@ export function initializeServiceWorker(config) {
|
|
|
85
44
|
async function defaultRequestHandler(event) {
|
|
86
45
|
event.preventDefault();
|
|
87
46
|
const url = new URL(event.request.url);
|
|
88
|
-
const
|
|
89
|
-
if (
|
|
90
|
-
|
|
47
|
+
const workerResponse = await convertFetchEventToPHPRequest(event);
|
|
48
|
+
if (workerResponse.status === 404 &&
|
|
49
|
+
workerResponse.headers.get('x-file-type') === 'static') {
|
|
50
|
+
const request = await cloneRequest(event.request, {
|
|
91
51
|
url,
|
|
92
|
-
})
|
|
52
|
+
});
|
|
53
|
+
return fetch(request);
|
|
93
54
|
}
|
|
94
|
-
return
|
|
55
|
+
return workerResponse;
|
|
95
56
|
}
|
|
96
57
|
export async function convertFetchEventToPHPRequest(event) {
|
|
97
58
|
let url = new URL(event.request.url);
|
|
@@ -171,7 +132,7 @@ export async function convertFetchEventToPHPRequest(event) {
|
|
|
171
132
|
* what this function uses to broadcast the message.
|
|
172
133
|
*
|
|
173
134
|
* @param message The message to broadcast.
|
|
174
|
-
* @param scope Target worker
|
|
135
|
+
* @param scope Target web worker scope.
|
|
175
136
|
* @returns The request ID to receive the reply.
|
|
176
137
|
*/
|
|
177
138
|
export async function broadcastMessageExpectReply(message, scope) {
|
|
@@ -198,32 +159,6 @@ export async function broadcastMessageExpectReply(message, scope) {
|
|
|
198
159
|
}
|
|
199
160
|
return requestId;
|
|
200
161
|
}
|
|
201
|
-
/**
|
|
202
|
-
* Guesses whether the given path looks like a PHP file.
|
|
203
|
-
*
|
|
204
|
-
* @example
|
|
205
|
-
* ```js
|
|
206
|
-
* seemsLikeAPHPRequestHandlerPath('/index.php') // true
|
|
207
|
-
* seemsLikeAPHPRequestHandlerPath('/index.php') // true
|
|
208
|
-
* seemsLikeAPHPRequestHandlerPath('/index.php/foo/bar') // true
|
|
209
|
-
* seemsLikeAPHPRequestHandlerPath('/index.html') // false
|
|
210
|
-
* seemsLikeAPHPRequestHandlerPath('/index.html/foo/bar') // false
|
|
211
|
-
* seemsLikeAPHPRequestHandlerPath('/') // true
|
|
212
|
-
* ```
|
|
213
|
-
*
|
|
214
|
-
* @param path The path to check.
|
|
215
|
-
* @returns Whether the path seems like a PHP server path.
|
|
216
|
-
*/
|
|
217
|
-
export function seemsLikeAPHPRequestHandlerPath(path) {
|
|
218
|
-
return seemsLikeAPHPFile(path) || seemsLikeADirectoryRoot(path);
|
|
219
|
-
}
|
|
220
|
-
function seemsLikeAPHPFile(path) {
|
|
221
|
-
return path.endsWith('.php') || path.includes('.php/');
|
|
222
|
-
}
|
|
223
|
-
function seemsLikeADirectoryRoot(path) {
|
|
224
|
-
const lastSegment = path.split('/').pop();
|
|
225
|
-
return !lastSegment.includes('.');
|
|
226
|
-
}
|
|
227
162
|
async function rewritePost(request) {
|
|
228
163
|
const contentType = request.headers.get('content-type');
|
|
229
164
|
if (request.method !== 'POST') {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initialize-service-worker.js","sourceRoot":"","sources":["../../../../../packages/php-wasm/web-service-worker/src/initialize-service-worker.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EACN,WAAW,EACX,WAAW,EACX,cAAc,EACd,WAAW,GACX,MAAM,kBAAkB,CAAC;AAE1B;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CAAC,MAAkC;IACzE,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"initialize-service-worker.js","sourceRoot":"","sources":["../../../../../packages/php-wasm/web-service-worker/src/initialize-service-worker.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EACN,WAAW,EACX,WAAW,EACX,cAAc,EACd,WAAW,GACX,MAAM,kBAAkB,CAAC;AAE1B;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CAAC,MAAkC;IACzE,MAAM,EAAE,aAAa,GAAG,qBAAqB,EAAE,GAAG,MAAM,CAAC;IAEzD;;;OAGG;IACH,IAAI,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;QACxC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAEvC,6DAA6D;QAC7D,IAAI,GAAG,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC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