@php-wasm/web-service-worker 1.0.29 → 1.1.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/index.cjs +1 -1
- package/index.js +68 -58
- package/package.json +13 -6
- package/utils.d.ts +8 -0
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@php-wasm/scopes"),f=25e3;let y=0;function R(e,t,...o){const n=u();return e.postMessage({...t,requestId:n},...o),n}function u(){return++y}function l(e,t,o=f){return new Promise((n,c)=>{const r=a=>{a.data.type==="response"&&a.data.requestId===t&&(e.removeEventListener("message",r),clearTimeout(s),n(a.data.response))},s=setTimeout(()=>{c(new Error("Request timed out")),e.removeEventListener("message",r)},o);e.addEventListener("message",r)})}function g(e,t){return{type:"response",requestId:e,response:t}}async function m(e){let t=new URL(e.request.url);if(!d.isURLScoped(t))try{const s=new URL(e.request.referrer);t=d.setURLScope(t,d.getURLScope(s))}catch{}const o=e.request.headers.get("content-type"),n=e.request.method==="POST"?new Uint8Array(await e.request.clone().arrayBuffer()):void 0,c={};for(const s of e.request.headers.entries())c[s[0]]=s[1];let r;try{const s={method:"request",args:[{body:n,url:t.toString(),method:e.request.method,headers:{...c,Host:t.host,"User-agent":self.navigator.userAgent,"Content-type":o}}]},a=d.getURLScope(t);if(a===null)throw new Error(`The URL ${t.toString()} is not scoped. This should not happen.`);const h=await p(s,a);r=await l(self,h),delete r.headers["x-frame-options"]}catch(s){throw console.error(s,{url:t.toString()}),s}return r.httpStatusCode>=300&&r.httpStatusCode<=399&&r.headers.location?Response.redirect(r.headers.location[0],r.httpStatusCode):new Response(r.bytes,{headers:r.headers,status:r.httpStatusCode})}async function p(e,t){const o=u();for(const n of await self.clients.matchAll({includeUncontrolled:!0}))n.postMessage({...e,scope:t,requestId:o});return o}async function i(e,t){const o=["GET","HEAD"].includes(e.method)||"body"in t?void 0:await e.blob();return new Request(t.url||e.url,{body:o,method:e.method,headers:e.headers,referrer:e.referrer,referrerPolicy:e.referrerPolicy,mode:e.mode==="navigate"?"same-origin":e.mode,credentials:e.credentials,cache:e.cache,redirect:e.redirect,integrity:e.integrity,...t})}async function w(e){if(!e.body)return[e,e];const[t,o]=e.body.tee();return[await i(e,{body:t,duplex:"half"}),await i(e,{body:o,duplex:"half"})]}function E(e){const t={};return e.headers.forEach((o,n)=>{t[n]=o}),t}exports.awaitReply=l;exports.broadcastMessageExpectReply=p;exports.cloneRequest=i;exports.convertFetchEventToPHPRequest=m;exports.getNextRequestId=u;exports.getRequestHeaders=E;exports.postMessageExpectReply=R;exports.responseTo=g;exports.teeRequest=w;
|
package/index.js
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
import { isURLScoped as
|
|
2
|
-
const
|
|
3
|
-
let
|
|
4
|
-
function
|
|
5
|
-
const
|
|
1
|
+
import { isURLScoped as h, setURLScope as p, getURLScope as d } from "@php-wasm/scopes";
|
|
2
|
+
const f = 25e3;
|
|
3
|
+
let y = 0;
|
|
4
|
+
function w(e, t, ...r) {
|
|
5
|
+
const s = u();
|
|
6
6
|
return e.postMessage(
|
|
7
7
|
{
|
|
8
8
|
...t,
|
|
9
|
-
requestId:
|
|
9
|
+
requestId: s
|
|
10
10
|
},
|
|
11
|
-
...
|
|
12
|
-
),
|
|
11
|
+
...r
|
|
12
|
+
), s;
|
|
13
13
|
}
|
|
14
|
-
function
|
|
15
|
-
return ++
|
|
14
|
+
function u() {
|
|
15
|
+
return ++y;
|
|
16
16
|
}
|
|
17
|
-
function m(e, t,
|
|
18
|
-
return new Promise((
|
|
19
|
-
const
|
|
20
|
-
a.data.type === "response" && a.data.requestId === t && (e.removeEventListener("message",
|
|
21
|
-
},
|
|
22
|
-
c(new Error("Request timed out")), e.removeEventListener("message",
|
|
23
|
-
},
|
|
24
|
-
e.addEventListener("message",
|
|
17
|
+
function m(e, t, r = f) {
|
|
18
|
+
return new Promise((s, c) => {
|
|
19
|
+
const o = (a) => {
|
|
20
|
+
a.data.type === "response" && a.data.requestId === t && (e.removeEventListener("message", o), clearTimeout(n), s(a.data.response));
|
|
21
|
+
}, n = setTimeout(() => {
|
|
22
|
+
c(new Error("Request timed out")), e.removeEventListener("message", o);
|
|
23
|
+
}, r);
|
|
24
|
+
e.addEventListener("message", o);
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
|
-
function
|
|
27
|
+
function E(e, t) {
|
|
28
28
|
return {
|
|
29
29
|
type: "response",
|
|
30
30
|
requestId: e,
|
|
31
31
|
response: t
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
|
-
async function
|
|
34
|
+
async function b(e) {
|
|
35
35
|
let t = new URL(e.request.url);
|
|
36
|
-
if (!
|
|
36
|
+
if (!h(t))
|
|
37
37
|
try {
|
|
38
|
-
const
|
|
39
|
-
t =
|
|
38
|
+
const n = new URL(e.request.referrer);
|
|
39
|
+
t = p(t, d(n));
|
|
40
40
|
} catch {
|
|
41
41
|
}
|
|
42
|
-
const
|
|
43
|
-
for (const
|
|
44
|
-
c[
|
|
45
|
-
let
|
|
42
|
+
const r = e.request.headers.get("content-type"), s = e.request.method === "POST" ? new Uint8Array(await e.request.clone().arrayBuffer()) : void 0, c = {};
|
|
43
|
+
for (const n of e.request.headers.entries())
|
|
44
|
+
c[n[0]] = n[1];
|
|
45
|
+
let o;
|
|
46
46
|
try {
|
|
47
|
-
const
|
|
47
|
+
const n = {
|
|
48
48
|
method: "request",
|
|
49
49
|
args: [
|
|
50
50
|
{
|
|
51
|
-
body:
|
|
51
|
+
body: s,
|
|
52
52
|
url: t.toString(),
|
|
53
53
|
method: e.request.method,
|
|
54
54
|
headers: {
|
|
@@ -57,38 +57,38 @@ async function E(e) {
|
|
|
57
57
|
// Safari and Firefox don't make the User-Agent header
|
|
58
58
|
// available in the fetch event. Let's add it manually:
|
|
59
59
|
"User-agent": self.navigator.userAgent,
|
|
60
|
-
"Content-type":
|
|
60
|
+
"Content-type": r
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
]
|
|
64
|
-
}, a =
|
|
64
|
+
}, a = d(t);
|
|
65
65
|
if (a === null)
|
|
66
66
|
throw new Error(
|
|
67
67
|
`The URL ${t.toString()} is not scoped. This should not happen.`
|
|
68
68
|
);
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
} catch (
|
|
72
|
-
throw console.error(
|
|
69
|
+
const l = await R(n, a);
|
|
70
|
+
o = await m(self, l), delete o.headers["x-frame-options"];
|
|
71
|
+
} catch (n) {
|
|
72
|
+
throw console.error(n, { url: t.toString() }), n;
|
|
73
73
|
}
|
|
74
|
-
return
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
) : new Response(
|
|
78
|
-
headers:
|
|
79
|
-
status:
|
|
74
|
+
return o.httpStatusCode >= 300 && o.httpStatusCode <= 399 && o.headers.location ? Response.redirect(
|
|
75
|
+
o.headers.location[0],
|
|
76
|
+
o.httpStatusCode
|
|
77
|
+
) : new Response(o.bytes, {
|
|
78
|
+
headers: o.headers,
|
|
79
|
+
status: o.httpStatusCode
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
|
-
async function
|
|
83
|
-
const
|
|
84
|
-
for (const
|
|
82
|
+
async function R(e, t) {
|
|
83
|
+
const r = u();
|
|
84
|
+
for (const s of await self.clients.matchAll({
|
|
85
85
|
// Sometimes the client that triggered the current fetch()
|
|
86
86
|
// event is considered uncontrolled in Google Chrome. This
|
|
87
87
|
// only happens on the first few fetches() after the initial
|
|
88
88
|
// registration of the service worker.
|
|
89
89
|
includeUncontrolled: !0
|
|
90
90
|
}))
|
|
91
|
-
|
|
91
|
+
s.postMessage({
|
|
92
92
|
...e,
|
|
93
93
|
/**
|
|
94
94
|
* Attach the scope with a URL starting with `/scope:` to this message.
|
|
@@ -98,14 +98,14 @@ async function y(e, t) {
|
|
|
98
98
|
* helps WASM workers ignore requests meant for other WASM workers.
|
|
99
99
|
*/
|
|
100
100
|
scope: t,
|
|
101
|
-
requestId:
|
|
101
|
+
requestId: r
|
|
102
102
|
});
|
|
103
|
-
return
|
|
103
|
+
return r;
|
|
104
104
|
}
|
|
105
|
-
async function
|
|
106
|
-
const
|
|
105
|
+
async function i(e, t) {
|
|
106
|
+
const r = ["GET", "HEAD"].includes(e.method) || "body" in t ? void 0 : await e.blob();
|
|
107
107
|
return new Request(t.url || e.url, {
|
|
108
|
-
body:
|
|
108
|
+
body: r,
|
|
109
109
|
method: e.method,
|
|
110
110
|
headers: e.headers,
|
|
111
111
|
referrer: e.referrer,
|
|
@@ -118,19 +118,29 @@ async function S(e, t) {
|
|
|
118
118
|
...t
|
|
119
119
|
});
|
|
120
120
|
}
|
|
121
|
+
async function S(e) {
|
|
122
|
+
if (!e.body)
|
|
123
|
+
return [e, e];
|
|
124
|
+
const [t, r] = e.body.tee();
|
|
125
|
+
return [
|
|
126
|
+
await i(e, { body: t, duplex: "half" }),
|
|
127
|
+
await i(e, { body: r, duplex: "half" })
|
|
128
|
+
];
|
|
129
|
+
}
|
|
121
130
|
function U(e) {
|
|
122
131
|
const t = {};
|
|
123
|
-
return e.headers.forEach((
|
|
124
|
-
t[
|
|
132
|
+
return e.headers.forEach((r, s) => {
|
|
133
|
+
t[s] = r;
|
|
125
134
|
}), t;
|
|
126
135
|
}
|
|
127
136
|
export {
|
|
128
137
|
m as awaitReply,
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
138
|
+
R as broadcastMessageExpectReply,
|
|
139
|
+
i as cloneRequest,
|
|
140
|
+
b as convertFetchEventToPHPRequest,
|
|
141
|
+
u as getNextRequestId,
|
|
133
142
|
U as getRequestHeaders,
|
|
134
|
-
|
|
135
|
-
|
|
143
|
+
w as postMessageExpectReply,
|
|
144
|
+
E as responseTo,
|
|
145
|
+
S as teeRequest
|
|
136
146
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/web-service-worker",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "PHP.wasm – service worker utils",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -31,12 +31,19 @@
|
|
|
31
31
|
"main": "./index.cjs",
|
|
32
32
|
"module": "./index.js",
|
|
33
33
|
"license": "GPL-2.0-or-later",
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "9dbbe2b22ccbacda9c0da4444c51d5739e784302",
|
|
35
35
|
"engines": {
|
|
36
|
-
"node": ">=
|
|
37
|
-
"npm": ">=
|
|
36
|
+
"node": ">=20.18.3",
|
|
37
|
+
"npm": ">=10.1.0"
|
|
38
|
+
},
|
|
39
|
+
"overrides": {
|
|
40
|
+
"rollup": "^4.34.6",
|
|
41
|
+
"react": "18.3.1",
|
|
42
|
+
"react-dom": "18.3.1",
|
|
43
|
+
"typescript": "5.4.5",
|
|
44
|
+
"ws": "^8.18.0"
|
|
38
45
|
},
|
|
39
46
|
"dependencies": {
|
|
40
|
-
"@php-wasm/scopes": "1.0
|
|
47
|
+
"@php-wasm/scopes": "1.1.0"
|
|
41
48
|
}
|
|
42
|
-
}
|
|
49
|
+
}
|
package/utils.d.ts
CHANGED
|
@@ -34,6 +34,14 @@ export declare function broadcastMessageExpectReply(message: any, scope: string)
|
|
|
34
34
|
* @returns The new request.
|
|
35
35
|
*/
|
|
36
36
|
export declare function cloneRequest(request: Request, overrides: Record<string, any>): Promise<Request>;
|
|
37
|
+
/**
|
|
38
|
+
* Tee a request to ensure the body stream is not consumed
|
|
39
|
+
* when executing or cloning the request.
|
|
40
|
+
*
|
|
41
|
+
* @param request
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
export declare function teeRequest(request: Request): Promise<[Request, Request]>;
|
|
37
45
|
/**
|
|
38
46
|
* Extracts headers from a Request as a plain key->value JS object.
|
|
39
47
|
*
|