@php-wasm/web-service-worker 3.1.8 → 3.1.9
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 +111 -100
- package/package.json +4 -4
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"),q=require("@php-wasm/universal");var g=typeof document<"u"?document.currentScript:null;const I=25e3;let H=0;function v(e,t,...r){const n=S();return e.postMessage({...t,requestId:n},...r),n}function S(){return++H}function U(e,t,r=I){return new Promise((n,a)=>{const o=u=>{u.data.type==="response"&&u.data.requestId===t&&(e.removeEventListener("message",o),clearTimeout(s),n(u.data.response))},s=setTimeout(()=>{a(new Error("Request timed out")),e.removeEventListener("message",o)},r);e.addEventListener("message",o)})}function A(e,t){return{type:"response",requestId:e,response:t}}async function O(e){let t=new URL(e.request.url);if(!d.isURLScoped(t))try{const c=new URL(e.request.referrer);t=d.setURLScope(t,d.getURLScope(c))}catch{}const r=e.request.headers.get("content-type"),n=e.request.method==="POST"?new Uint8Array(await e.request.clone().arrayBuffer()):void 0,a={};for(const c of e.request.headers.entries())a[c[0]]=c[1];let o;try{const c={method:"request",args:[{body:n,url:t.toString(),method:e.request.method,headers:{...a,Host:t.host,"User-agent":self.navigator.userAgent,"Content-type":r}}]},i=d.getURLScope(t);if(i===null)throw new Error(`The URL ${t.toString()} is not scoped. This should not happen.`);const f=await b(c,i);if(o=await U(self,f),delete o.headers["x-frame-options"],o.headers["content-security-policy"]){const l=o.headers["content-security-policy"].map(p=>C("frame-ancestors",p)).filter(p=>p.trim().length>0);l.length>0?o.headers["content-security-policy"]=l:delete o.headers["content-security-policy"]}}catch(c){throw console.error(c,{url:t.toString()}),c}if(o.httpStatusCode>=300&&o.httpStatusCode<=399&&o.headers.location){const c=d.getURLScope(t);let i=new URL(o.headers.location[0],t.toString());return c&&!d.isURLScoped(i)&&(i=d.setURLScope(i,c)),Response.redirect(i.toString(),o.httpStatusCode)}const s=[101,103,204,205,304].includes(o.httpStatusCode);let u=null;return s||(o.bodyPort?u=q.portToStream(o.bodyPort):u=o.bytes),new Response(u,{headers:o.headers,status:o.httpStatusCode})}async function b(e,t){const r=S();for(const n of await self.clients.matchAll({includeUncontrolled:!0}))n.postMessage({...e,scope:t,requestId:r});return r}async function h(e,t){let r;return["GET","HEAD"].includes(e.method)?r=void 0:"body"in t?r=t.body:!e.bodyUsed&&e.body?r=e.body:r=await e.arrayBuffer(),new Request(t.url||e.url,{body:r,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,...r instanceof ReadableStream&&{duplex:"half"},...t})}async function L(e){if(!e.body)return[e,e];const[t,r]=e.body.tee();return[await h(e,{body:t,duplex:"half"}),await h(e,{body:r,duplex:"half"})]}function D(e){const t={};return e.headers.forEach((r,n)=>{t[n]=r}),t}function C(e,t){const r=/^[\u{9}\u{A}\u{C}\u{D}\u{20}]+/u,n=/[\u{9}\u{A}\u{C}\u{D}\u{20}]+$/u,a=/[\u{9}\u{A}\u{C}\u{D}\u{20}]/u;return t.split(";").filter(o=>{const s=o.replace(r,"").replace(n,""),[u]=s.split(a,1);return u.toLowerCase()!==e.toLowerCase()}).join(";")}class T extends Error{constructor(t,r,n){super(`Could not fetch ${t} – your network appears to be blocking this request (HTTP ${r}). This often happens on school, university, or corporate networks. Try switching to a different network or using a VPN.`),this.name="FirewallInterferenceError",this.url=t,this.status=r,this.statusText=n}}const M="X-Playground-Cors-Proxy";async function j(e,t,r,n){var f;let a=typeof e=="string"?new Request(e,t):e;const o=n?new URL(n):null;let s=o?new URL(a.url,o):new URL(a.url);if(s.hostname==="localhost"||s.hostname==="127.0.0.1"||s.hostname==="[::1]"||s.hostname==="::1")return await fetch(a);if(s.protocol==="http:"){s.protocol="https:";const l=s.toString();a=await h(a,{url:l}),s=new URL(l)}if(!r)return await fetch(a);if(o&&s.protocol===o.protocol&&s.hostname===o.hostname&&s.port===o.port&&s.pathname.startsWith(o.pathname))return await fetch(a);const[c,i]=await L(a);try{return await fetch(c)}catch{const l=new Headers(i.headers),p=((f=l.get("x-cors-proxy-allowed-request-headers"))==null?void 0:f.split(","))||[],E=p.includes("authorization")||p.includes("cookie"),m=l.get("content-type");m&&m.toLowerCase().includes("multipart/form-data")&&(l.set("x-cors-proxy-content-type",m),l.set("content-type","application/octet-stream"));const y=new URL(typeof document>"u"?require("url").pathToFileURL(__filename).href:g&&g.tagName.toUpperCase()==="SCRIPT"&&g.src||new URL("index.cjs",document.baseURI).href);y.pathname="",y.search="",y.hash="";const P=new URL(r,y.toString());let w=i.body;w&&new URL(P).protocol==="http:"&&(w=await new Response(w).arrayBuffer());const x=await h(i,{url:`${r}${a.url}`,headers:l,body:w,...E&&{credentials:"include"}}),R=await fetch(x);if(!R.headers.has(M))throw new T(a.url,R.status,R.statusText);return R}}exports.FirewallInterferenceError=T;exports.awaitReply=U;exports.broadcastMessageExpectReply=b;exports.cloneRequest=h;exports.convertFetchEventToPHPRequest=O;exports.fetchWithCorsProxy=j;exports.getNextRequestId=S;exports.getRequestHeaders=D;exports.postMessageExpectReply=v;exports.removeContentSecurityPolicyDirective=C;exports.responseTo=A;exports.teeRequest=L;
|
package/index.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import { isURLScoped as
|
|
1
|
+
import { isURLScoped as g, setURLScope as b, getURLScope as R } from "@php-wasm/scopes";
|
|
2
2
|
import { portToStream as E } from "@php-wasm/universal";
|
|
3
|
-
const
|
|
4
|
-
let
|
|
5
|
-
function
|
|
6
|
-
const
|
|
3
|
+
const T = 25e3;
|
|
4
|
+
let x = 0;
|
|
5
|
+
function j(e, t, ...r) {
|
|
6
|
+
const n = L();
|
|
7
7
|
return e.postMessage(
|
|
8
8
|
{
|
|
9
9
|
...t,
|
|
10
|
-
requestId:
|
|
10
|
+
requestId: n
|
|
11
11
|
},
|
|
12
12
|
...r
|
|
13
|
-
),
|
|
13
|
+
), n;
|
|
14
14
|
}
|
|
15
|
-
function
|
|
16
|
-
return ++
|
|
15
|
+
function L() {
|
|
16
|
+
return ++x;
|
|
17
17
|
}
|
|
18
|
-
function
|
|
19
|
-
return new Promise((
|
|
20
|
-
const o = (
|
|
21
|
-
|
|
22
|
-
},
|
|
18
|
+
function P(e, t, r = T) {
|
|
19
|
+
return new Promise((n, a) => {
|
|
20
|
+
const o = (u) => {
|
|
21
|
+
u.data.type === "response" && u.data.requestId === t && (e.removeEventListener("message", o), clearTimeout(s), n(u.data.response));
|
|
22
|
+
}, s = setTimeout(() => {
|
|
23
23
|
a(new Error("Request timed out")), e.removeEventListener("message", o);
|
|
24
24
|
}, r);
|
|
25
25
|
e.addEventListener("message", o);
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
|
-
function
|
|
28
|
+
function k(e, t) {
|
|
29
29
|
return {
|
|
30
30
|
type: "response",
|
|
31
31
|
requestId: e,
|
|
@@ -34,22 +34,22 @@ function D(e, t) {
|
|
|
34
34
|
}
|
|
35
35
|
async function v(e) {
|
|
36
36
|
let t = new URL(e.request.url);
|
|
37
|
-
if (!
|
|
37
|
+
if (!g(t))
|
|
38
38
|
try {
|
|
39
|
-
const
|
|
40
|
-
t =
|
|
39
|
+
const c = new URL(e.request.referrer);
|
|
40
|
+
t = b(t, R(c));
|
|
41
41
|
} catch {
|
|
42
42
|
}
|
|
43
|
-
const r = e.request.headers.get("content-type"),
|
|
44
|
-
for (const
|
|
45
|
-
a[
|
|
43
|
+
const r = e.request.headers.get("content-type"), n = e.request.method === "POST" ? new Uint8Array(await e.request.clone().arrayBuffer()) : void 0, a = {};
|
|
44
|
+
for (const c of e.request.headers.entries())
|
|
45
|
+
a[c[0]] = c[1];
|
|
46
46
|
let o;
|
|
47
47
|
try {
|
|
48
|
-
const
|
|
48
|
+
const c = {
|
|
49
49
|
method: "request",
|
|
50
50
|
args: [
|
|
51
51
|
{
|
|
52
|
-
body:
|
|
52
|
+
body: n,
|
|
53
53
|
url: t.toString(),
|
|
54
54
|
method: e.request.method,
|
|
55
55
|
headers: {
|
|
@@ -62,54 +62,54 @@ async function v(e) {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
]
|
|
65
|
-
},
|
|
66
|
-
if (
|
|
65
|
+
}, i = R(t);
|
|
66
|
+
if (i === null)
|
|
67
67
|
throw new Error(
|
|
68
68
|
`The URL ${t.toString()} is not scoped. This should not happen.`
|
|
69
69
|
);
|
|
70
|
-
const
|
|
71
|
-
if (o = await
|
|
72
|
-
const
|
|
73
|
-
(d) =>
|
|
70
|
+
const h = await A(c, i);
|
|
71
|
+
if (o = await P(self, h), delete o.headers["x-frame-options"], o.headers["content-security-policy"]) {
|
|
72
|
+
const l = o.headers["content-security-policy"].map(
|
|
73
|
+
(d) => q(
|
|
74
74
|
"frame-ancestors",
|
|
75
75
|
d
|
|
76
76
|
)
|
|
77
77
|
).filter((d) => d.trim().length > 0);
|
|
78
|
-
|
|
78
|
+
l.length > 0 ? o.headers["content-security-policy"] = l : delete o.headers["content-security-policy"];
|
|
79
79
|
}
|
|
80
|
-
} catch (
|
|
81
|
-
throw console.error(
|
|
80
|
+
} catch (c) {
|
|
81
|
+
throw console.error(c, { url: t.toString() }), c;
|
|
82
82
|
}
|
|
83
83
|
if (o.httpStatusCode >= 300 && o.httpStatusCode <= 399 && o.headers.location) {
|
|
84
|
-
const
|
|
85
|
-
let
|
|
84
|
+
const c = R(t);
|
|
85
|
+
let i = new URL(
|
|
86
86
|
o.headers.location[0],
|
|
87
87
|
t.toString()
|
|
88
88
|
);
|
|
89
|
-
return
|
|
90
|
-
|
|
89
|
+
return c && !g(i) && (i = b(i, c)), Response.redirect(
|
|
90
|
+
i.toString(),
|
|
91
91
|
o.httpStatusCode
|
|
92
92
|
);
|
|
93
93
|
}
|
|
94
|
-
const
|
|
94
|
+
const s = [101, 103, 204, 205, 304].includes(
|
|
95
95
|
o.httpStatusCode
|
|
96
96
|
);
|
|
97
|
-
let
|
|
98
|
-
return
|
|
97
|
+
let u = null;
|
|
98
|
+
return s || (o.bodyPort ? u = E(o.bodyPort) : u = o.bytes), new Response(u, {
|
|
99
99
|
headers: o.headers,
|
|
100
100
|
status: o.httpStatusCode
|
|
101
101
|
});
|
|
102
102
|
}
|
|
103
|
-
async function
|
|
104
|
-
const r =
|
|
105
|
-
for (const
|
|
103
|
+
async function A(e, t) {
|
|
104
|
+
const r = L();
|
|
105
|
+
for (const n of await self.clients.matchAll({
|
|
106
106
|
// Sometimes the client that triggered the current fetch()
|
|
107
107
|
// event is considered uncontrolled in Google Chrome. This
|
|
108
108
|
// only happens on the first few fetches() after the initial
|
|
109
109
|
// registration of the service worker.
|
|
110
110
|
includeUncontrolled: !0
|
|
111
111
|
}))
|
|
112
|
-
|
|
112
|
+
n.postMessage({
|
|
113
113
|
...e,
|
|
114
114
|
/**
|
|
115
115
|
* Attach the scope with a URL starting with `/scope:` to this message.
|
|
@@ -123,9 +123,9 @@ async function T(e, t) {
|
|
|
123
123
|
});
|
|
124
124
|
return r;
|
|
125
125
|
}
|
|
126
|
-
async function
|
|
126
|
+
async function w(e, t) {
|
|
127
127
|
let r;
|
|
128
|
-
return ["GET", "HEAD"].includes(e.method)
|
|
128
|
+
return ["GET", "HEAD"].includes(e.method) ? r = void 0 : "body" in t ? r = t.body : !e.bodyUsed && e.body ? r = e.body : r = await e.arrayBuffer(), new Request(t.url || e.url, {
|
|
129
129
|
body: r,
|
|
130
130
|
method: e.method,
|
|
131
131
|
headers: e.headers,
|
|
@@ -136,70 +136,91 @@ async function p(e, t) {
|
|
|
136
136
|
cache: e.cache,
|
|
137
137
|
redirect: e.redirect,
|
|
138
138
|
integrity: e.integrity,
|
|
139
|
+
/**
|
|
140
|
+
* Infer the duplex value in a way that's consistent across browsers. Web browsers
|
|
141
|
+
* only support 'half' as of January 2026, but other values may be supported in the future.
|
|
142
|
+
* Unfortunately, also as of January 2026, we cannot read the duplex value directly from the
|
|
143
|
+
* request object:
|
|
144
|
+
*
|
|
145
|
+
* > Although duplex can be passed as an option when constructing a Request,
|
|
146
|
+
* > it is not currently exposed as a readable property on the resulting Request
|
|
147
|
+
* > object in all browsers.
|
|
148
|
+
*
|
|
149
|
+
* See MDN: https://developer.mozilla.org/en-US/docs/Web/API/Request/duplex
|
|
150
|
+
*/
|
|
139
151
|
...r instanceof ReadableStream && { duplex: "half" },
|
|
140
152
|
...t
|
|
141
153
|
});
|
|
142
154
|
}
|
|
143
|
-
async function
|
|
155
|
+
async function H(e) {
|
|
144
156
|
if (!e.body)
|
|
145
157
|
return [e, e];
|
|
146
158
|
const [t, r] = e.body.tee();
|
|
147
159
|
return [
|
|
148
|
-
await
|
|
149
|
-
await
|
|
160
|
+
await w(e, { body: t, duplex: "half" }),
|
|
161
|
+
await w(e, { body: r, duplex: "half" })
|
|
150
162
|
];
|
|
151
163
|
}
|
|
152
|
-
function
|
|
164
|
+
function B(e) {
|
|
153
165
|
const t = {};
|
|
154
|
-
return e.headers.forEach((r,
|
|
155
|
-
t[
|
|
166
|
+
return e.headers.forEach((r, n) => {
|
|
167
|
+
t[n] = r;
|
|
156
168
|
}), t;
|
|
157
169
|
}
|
|
158
|
-
function
|
|
159
|
-
const r = /^[\u{9}\u{A}\u{C}\u{D}\u{20}]+/u,
|
|
170
|
+
function q(e, t) {
|
|
171
|
+
const r = /^[\u{9}\u{A}\u{C}\u{D}\u{20}]+/u, n = /[\u{9}\u{A}\u{C}\u{D}\u{20}]+$/u, a = /[\u{9}\u{A}\u{C}\u{D}\u{20}]/u;
|
|
160
172
|
return t.split(";").filter((o) => {
|
|
161
|
-
const
|
|
173
|
+
const s = o.replace(r, "").replace(n, ""), [u] = s.split(
|
|
162
174
|
a,
|
|
163
175
|
// The directive name is the first token.
|
|
164
176
|
1
|
|
165
177
|
);
|
|
166
|
-
return
|
|
178
|
+
return u.toLowerCase() !== e.toLowerCase();
|
|
167
179
|
}).join(";");
|
|
168
180
|
}
|
|
169
|
-
class
|
|
170
|
-
constructor(t, r,
|
|
181
|
+
class I extends Error {
|
|
182
|
+
constructor(t, r, n) {
|
|
171
183
|
super(
|
|
172
184
|
`Could not fetch ${t} – your network appears to be blocking this request (HTTP ${r}). This often happens on school, university, or corporate networks. Try switching to a different network or using a VPN.`
|
|
173
|
-
), this.name = "FirewallInterferenceError", this.url = t, this.status = r, this.statusText =
|
|
185
|
+
), this.name = "FirewallInterferenceError", this.url = t, this.status = r, this.statusText = n;
|
|
174
186
|
}
|
|
175
187
|
}
|
|
176
|
-
const
|
|
177
|
-
async function
|
|
178
|
-
var
|
|
188
|
+
const O = "X-Playground-Cors-Proxy";
|
|
189
|
+
async function M(e, t, r, n) {
|
|
190
|
+
var h;
|
|
179
191
|
let a = typeof e == "string" ? new Request(e, t) : e;
|
|
180
|
-
const o =
|
|
181
|
-
let
|
|
182
|
-
if (
|
|
183
|
-
return await
|
|
184
|
-
if (
|
|
185
|
-
|
|
186
|
-
const
|
|
187
|
-
a = await
|
|
192
|
+
const o = n ? new URL(n) : null;
|
|
193
|
+
let s = o ? new URL(a.url, o) : new URL(a.url);
|
|
194
|
+
if (s.hostname === "localhost" || s.hostname === "127.0.0.1" || s.hostname === "[::1]" || s.hostname === "::1")
|
|
195
|
+
return await fetch(a);
|
|
196
|
+
if (s.protocol === "http:") {
|
|
197
|
+
s.protocol = "https:";
|
|
198
|
+
const l = s.toString();
|
|
199
|
+
a = await w(a, { url: l }), s = new URL(l);
|
|
188
200
|
}
|
|
189
201
|
if (!r)
|
|
190
|
-
return await
|
|
191
|
-
if (o &&
|
|
192
|
-
return await
|
|
193
|
-
const [
|
|
202
|
+
return await fetch(a);
|
|
203
|
+
if (o && s.protocol === o.protocol && s.hostname === o.hostname && s.port === o.port && s.pathname.startsWith(o.pathname))
|
|
204
|
+
return await fetch(a);
|
|
205
|
+
const [c, i] = await H(a);
|
|
194
206
|
try {
|
|
195
|
-
return await
|
|
207
|
+
return await fetch(c);
|
|
196
208
|
} catch {
|
|
197
|
-
const d = ((
|
|
209
|
+
const l = new Headers(i.headers), d = ((h = l.get("x-cors-proxy-allowed-request-headers")) == null ? void 0 : h.split(",")) || [], U = d.includes("authorization") || d.includes("cookie"), m = l.get("content-type");
|
|
210
|
+
m && m.toLowerCase().includes("multipart/form-data") && (l.set("x-cors-proxy-content-type", m), l.set("content-type", "application/octet-stream"));
|
|
211
|
+
const p = new URL(import.meta.url);
|
|
212
|
+
p.pathname = "", p.search = "", p.hash = "";
|
|
213
|
+
const S = new URL(r, p.toString());
|
|
214
|
+
let f = i.body;
|
|
215
|
+
f && new URL(S).protocol === "http:" && (f = await new Response(f).arrayBuffer());
|
|
216
|
+
const C = await w(i, {
|
|
198
217
|
url: `${r}${a.url}`,
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
218
|
+
headers: l,
|
|
219
|
+
body: f,
|
|
220
|
+
...U && { credentials: "include" }
|
|
221
|
+
}), y = await fetch(C);
|
|
222
|
+
if (!y.headers.has(O))
|
|
223
|
+
throw new I(
|
|
203
224
|
a.url,
|
|
204
225
|
y.status,
|
|
205
226
|
y.statusText
|
|
@@ -207,27 +228,17 @@ async function k(e, t, r, s) {
|
|
|
207
228
|
return y;
|
|
208
229
|
}
|
|
209
230
|
}
|
|
210
|
-
async function h(e, t) {
|
|
211
|
-
let r = t ? new Request(e, t) : e;
|
|
212
|
-
if (new URL(r.url).protocol === "http:" && r.body) {
|
|
213
|
-
const s = await new Response(r.body).arrayBuffer();
|
|
214
|
-
r = await p(r, {
|
|
215
|
-
body: s
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
return fetch(r);
|
|
219
|
-
}
|
|
220
231
|
export {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
232
|
+
I as FirewallInterferenceError,
|
|
233
|
+
P as awaitReply,
|
|
234
|
+
A as broadcastMessageExpectReply,
|
|
235
|
+
w as cloneRequest,
|
|
225
236
|
v as convertFetchEventToPHPRequest,
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
237
|
+
M as fetchWithCorsProxy,
|
|
238
|
+
L as getNextRequestId,
|
|
239
|
+
B as getRequestHeaders,
|
|
240
|
+
j as postMessageExpectReply,
|
|
241
|
+
q as removeContentSecurityPolicyDirective,
|
|
242
|
+
k as responseTo,
|
|
243
|
+
H as teeRequest
|
|
233
244
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@php-wasm/web-service-worker",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.9",
|
|
4
4
|
"description": "PHP.wasm – service worker utils",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -31,15 +31,15 @@
|
|
|
31
31
|
"main": "./index.cjs",
|
|
32
32
|
"module": "./index.js",
|
|
33
33
|
"license": "GPL-2.0-or-later",
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "97cf88501a336453baaac9a860a17329235a71fb",
|
|
35
35
|
"engines": {
|
|
36
36
|
"node": ">=20.10.0",
|
|
37
37
|
"npm": ">=10.2.3"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"ini": "4.1.2",
|
|
41
|
-
"@php-wasm/scopes": "3.1.
|
|
42
|
-
"@php-wasm/universal": "3.1.
|
|
41
|
+
"@php-wasm/scopes": "3.1.9",
|
|
42
|
+
"@php-wasm/universal": "3.1.9"
|
|
43
43
|
},
|
|
44
44
|
"packageManager": "npm@10.9.2",
|
|
45
45
|
"overrides": {
|