@php-wasm/web-service-worker 3.1.34 → 3.1.35

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.
Files changed (3) hide show
  1. package/index.cjs +1 -1
  2. package/index.js +147 -116
  3. package/package.json +4 -4
package/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@php-wasm/scopes"),q=require("@php-wasm/universal");var R=typeof document<"u"?document.currentScript:null;const x=25e3;let v=0;function H(e,t,...o){const a=S();return e.postMessage({...t,requestId:a},...o),a}function S(){return++v}function U(e,t,o=x){return new Promise((a,s)=>{const r=i=>{i.data.type==="response"&&i.data.requestId===t&&(e.removeEventListener("message",r),clearTimeout(n),a(i.data.response))},n=setTimeout(()=>{s(new Error("Request timed out")),e.removeEventListener("message",r)},o);e.addEventListener("message",r)})}function I(e,t){return{type:"response",requestId:e,response:t}}async function A(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 o=e.request.headers.get("content-type"),a=e.request.method==="POST"?new Uint8Array(await e.request.clone().arrayBuffer()):void 0,s={};for(const c of e.request.headers.entries())s[c[0]]=c[1];let r;try{const c={method:"request",args:[{body:a,url:t.toString(),method:e.request.method,headers:{...s,Host:t.host,"User-agent":self.navigator.userAgent,"Content-type":o}}]},l=d.getURLScope(t);if(l===null)throw new Error(`The URL ${t.toString()} is not scoped. This should not happen.`);const u=await L(c,l);if(r=await U(self,u),delete r.headers["x-frame-options"],r.headers["content-security-policy"]){const f=r.headers["content-security-policy"].map(y=>C("frame-ancestors",y)).filter(y=>y.trim().length>0);f.length>0?r.headers["content-security-policy"]=f:delete r.headers["content-security-policy"]}}catch(c){throw console.error(c,{url:t.toString()}),c}if(r.httpStatusCode>=300&&r.httpStatusCode<=399&&r.headers.location){const c=d.getURLScope(t);let l=new URL(r.headers.location[0],t.toString());return c&&!d.isURLScoped(l)&&(l=d.setURLScope(l,c)),Response.redirect(l.toString(),r.httpStatusCode)}const n=[101,103,204,205,304].includes(r.httpStatusCode);let i=null;return n||(r.bodyPort?i=q.portToStream(r.bodyPort):i=r.bytes),new Response(i,{headers:r.headers,status:r.httpStatusCode})}async function L(e,t){const o=S();for(const a of await self.clients.matchAll({includeUncontrolled:!0}))a.postMessage({...e,scope:t,requestId:o});return o}async function g(e,t){let o;return["GET","HEAD"].includes(e.method)?o=void 0:"body"in t?o=t.body:!e.bodyUsed&&e.body?o=e.body:o=await e.arrayBuffer(),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,...o instanceof ReadableStream&&{duplex:"half"},...t})}let h;const O={resetStreamBodySupported(){h=void 0}};async function T(){if(h!==void 0)return h;try{const e=new ReadableStream({start(t){t.close()}});await fetch("data:,",{method:"POST",body:e,duplex:"half"}),h=!0}catch{h=!1}return h}function _(e){const t={};return e.headers.forEach((o,a)=>{t[a]=o}),t}function C(e,t){const o=/^[\u{9}\u{A}\u{C}\u{D}\u{20}]+/u,a=/[\u{9}\u{A}\u{C}\u{D}\u{20}]+$/u,s=/[\u{9}\u{A}\u{C}\u{D}\u{20}]/u;return t.split(";").filter(r=>{const n=r.replace(o,"").replace(a,""),[i]=n.split(s,1);return i.toLowerCase()!==e.toLowerCase()}).join(";")}class E extends Error{constructor(t,o,a){super(`Could not fetch ${t} – your network appears to be blocking this request (HTTP ${o}). 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=o,this.statusText=a}}const B="X-Playground-Cors-Proxy";async function D(e,t,o,a){var l;let s=typeof e=="string"?new Request(e,t):e;const r=a?new URL(a):null;let n=r?new URL(s.url,r):new URL(s.url);if(n.hostname==="localhost"||n.hostname==="127.0.0.1"||n.hostname==="[::1]"||n.hostname==="::1")return await fetch(s);if(n.protocol==="http:"){n.protocol="https:";const u=n.toString();s=await g(s,{url:u}),n=new URL(u)}if(!o)return await fetch(s);if(r&&n.protocol===r.protocol&&n.hostname===r.hostname&&n.port===r.port&&n.pathname.startsWith(r.pathname))return await fetch(s);const c=s.clone();try{return await fetch(s)}catch{const u=new Headers(s.headers),f=((l=u.get("x-cors-proxy-allowed-request-headers"))==null?void 0:l.split(","))||[],y=f.includes("authorization")||f.includes("cookie"),w=u.get("content-type");w&&w.toLowerCase().includes("multipart/form-data")&&(u.set("x-cors-proxy-content-type",w),u.set("content-type","application/octet-stream"));let p=null;const b=s.method.toUpperCase();b!=="GET"&&b!=="HEAD"&&(await T()?p=c.body:p=await c.arrayBuffer()),p instanceof ReadableStream&&new URL(o,typeof document>"u"?require("url").pathToFileURL(__filename).href:R&&R.tagName.toUpperCase()==="SCRIPT"&&R.src||new URL("index.cjs",document.baseURI).href).protocol==="http:"&&(p=await new Response(p).arrayBuffer());const P=await g(s,{url:`${o}${s.url}`,headers:u,body:p,...y&&{credentials:"include"}}),m=await fetch(P);if(!m.headers.has(B))throw new E(s.url,m.status,m.statusText);return m}}exports.FirewallInterferenceError=E;exports.__testing=O;exports.awaitReply=U;exports.broadcastMessageExpectReply=L;exports.cloneRequest=g;exports.convertFetchEventToPHPRequest=A;exports.fetchWithCorsProxy=D;exports.getNextRequestId=S;exports.getRequestHeaders=_;exports.postMessageExpectReply=H;exports.removeContentSecurityPolicyDirective=C;exports.responseTo=I;exports.supportsReadableStreamBody=T;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@php-wasm/scopes"),H=require("@php-wasm/universal");var R=typeof document<"u"?document.currentScript:null;const q=25e3;let x=0;function v(e,t,...o){const n=g();return e.postMessage({...t,requestId:n},...o),n}function g(){return++x}function E(e,t,o=q){return new Promise((n,s)=>{const r=u=>{u.data.type==="response"&&u.data.requestId===t&&(e.removeEventListener("message",r),clearTimeout(a),n(u.data.response))},a=setTimeout(()=>{s(new Error("Request timed out")),e.removeEventListener("message",r)},o);e.addEventListener("message",r)})}function A(e,t){return{type:"response",requestId:e,response:t}}async function _(e){let t=new URL(e.request.url);if(!l.isURLScoped(t))try{const i=new URL(e.request.referrer);t=l.setURLScope(t,l.getURLScope(i))}catch{}const o=e.request.headers.get("content-type"),n=e.request.method==="POST"?new Uint8Array(await e.request.clone().arrayBuffer()):void 0,s={};for(const i of e.request.headers.entries())s[i[0]]=i[1];let r;try{const i={method:"request",args:[{body:n,url:t.toString(),method:e.request.method,headers:{...s,Host:t.host,"User-agent":self.navigator.userAgent,"Content-type":o}}]},c=l.getURLScope(t);if(c===null)throw new Error(`The URL ${t.toString()} is not scoped. This should not happen.`);const f=await b(i,c);if(r=await E(self,f),delete r.headers["x-frame-options"],r.headers["content-security-policy"]){const y=r.headers["content-security-policy"].map(d=>C("frame-ancestors",d)).filter(d=>d.trim().length>0);y.length>0?r.headers["content-security-policy"]=y:delete r.headers["content-security-policy"]}}catch(i){throw console.error(i,{url:t.toString()}),i}if(r.httpStatusCode>=300&&r.httpStatusCode<=399&&r.headers.location){const i=l.getURLScope(t);let c=new URL(r.headers.location[0],t.toString());return i&&!l.isURLScoped(c)&&(c=l.setURLScope(c,i)),Response.redirect(c.toString(),r.httpStatusCode)}const a=[101,103,204,205,304].includes(r.httpStatusCode);let u=null;return a||(r.bodyPort?u=H.portToStream(r.bodyPort):u=r.bytes),new Response(u,{headers:r.headers,status:r.httpStatusCode})}async function b(e,t){const o=g();for(const n of await self.clients.matchAll({includeUncontrolled:!0}))n.postMessage({...e,scope:t,requestId:o});return o}async function w(e,t){let o;return["GET","HEAD"].includes(e.method)?o=void 0:"body"in t?o=t.body:!e.bodyUsed&&e.body?o=e.body:o=await e.arrayBuffer(),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,...o instanceof ReadableStream&&{duplex:"half"},...t})}let h;const I={resetStreamBodySupported(){h=void 0}};async function T(){if(h!==void 0)return h;try{const e=new ReadableStream({start(t){t.close()}});await fetch("data:,",{method:"POST",body:e,duplex:"half"}),h=!0}catch{h=!1}return h}function O(e){const t={};return e.headers.forEach((o,n)=>{t[n]=o}),t}function C(e,t){const o=/^[\u{9}\u{A}\u{C}\u{D}\u{20}]+/u,n=/[\u{9}\u{A}\u{C}\u{D}\u{20}]+$/u,s=/[\u{9}\u{A}\u{C}\u{D}\u{20}]/u;return t.split(";").filter(r=>{const a=r.replace(o,"").replace(n,""),[u]=a.split(s,1);return u.toLowerCase()!==e.toLowerCase()}).join(";")}class U extends Error{constructor(t,o,n){super(`Could not fetch ${t} – your network appears to be blocking this request (HTTP ${o}). 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=o,this.statusText=n}}const D="X-Playground-Cors-Proxy",L=new Map([["api.anthropic.com",{"anthropic-dangerous-direct-browser-access":"true"}],["api.openai.com",{}],["generativelanguage.googleapis.com",{}]]);async function B(e,t,o,n){var i;let s=typeof e=="string"?new Request(e,t):e;const r=n?new URL(n):null;let a=r?new URL(s.url,r):new URL(s.url);if(M(a))return await fetch(s);if(N(a))return s=await j(s,a),await fetch(s);if(a.protocol==="http:"){a.protocol="https:";const c=a.toString();s=await w(s,{url:c}),a=new URL(c)}if(!o)return await fetch(s);if(r&&a.protocol===r.protocol&&a.hostname===r.hostname&&a.port===r.port&&a.pathname.startsWith(r.pathname))return await fetch(s);const u=s.clone();try{return await fetch(s)}catch{const c=new Headers(s.headers),f=((i=c.get("x-cors-proxy-allowed-request-headers"))==null?void 0:i.split(","))||[],y=f.includes("authorization")||f.includes("cookie"),d=c.get("content-type");d&&d.toLowerCase().includes("multipart/form-data")&&(c.set("x-cors-proxy-content-type",d),c.set("content-type","application/octet-stream"));let p=null;const S=s.method.toUpperCase();S!=="GET"&&S!=="HEAD"&&(await T()?p=u.body:p=await u.arrayBuffer()),p instanceof ReadableStream&&new URL(o,typeof document>"u"?require("url").pathToFileURL(__filename).href:R&&R.tagName.toUpperCase()==="SCRIPT"&&R.src||new URL("index.cjs",document.baseURI).href).protocol==="http:"&&(p=await new Response(p).arrayBuffer());const P=await w(s,{url:`${o}${s.url}`,headers:c,body:p,...y&&{credentials:"include"}}),m=await fetch(P);if(!m.headers.has(D))throw new U(s.url,m.status,m.statusText);return m}}function M(e){return e.hostname==="localhost"||e.hostname==="127.0.0.1"||e.hostname==="[::1]"||e.hostname==="::1"}function N(e){return e.protocol==="https:"&&L.has(e.hostname)}async function j(e,t){const o=L.get(t.hostname);if(!o)return e;const n=new Headers(e.headers);for(const[s,r]of Object.entries(o))n.has(s)||n.set(s,r);return await w(e,{headers:n})}exports.FirewallInterferenceError=U;exports.__testing=I;exports.awaitReply=E;exports.broadcastMessageExpectReply=b;exports.cloneRequest=w;exports.convertFetchEventToPHPRequest=_;exports.fetchWithCorsProxy=B;exports.getNextRequestId=g;exports.getRequestHeaders=O;exports.postMessageExpectReply=v;exports.removeContentSecurityPolicyDirective=C;exports.responseTo=A;exports.supportsReadableStreamBody=T;
package/index.js CHANGED
@@ -1,55 +1,55 @@
1
- import { isURLScoped as g, setURLScope as S, getURLScope as w } from "@php-wasm/scopes";
1
+ import { isURLScoped as R, setURLScope as S, getURLScope as m } from "@php-wasm/scopes";
2
2
  import { portToStream as C } from "@php-wasm/universal";
3
3
  const L = 25e3;
4
4
  let U = 0;
5
- function D(e, t, ...o) {
6
- const a = E();
5
+ function M(e, t, ...r) {
6
+ const n = E();
7
7
  return e.postMessage(
8
8
  {
9
9
  ...t,
10
- requestId: a
10
+ requestId: n
11
11
  },
12
- ...o
13
- ), a;
12
+ ...r
13
+ ), n;
14
14
  }
15
15
  function E() {
16
16
  return ++U;
17
17
  }
18
- function P(e, t, o = L) {
19
- return new Promise((a, s) => {
20
- const r = (c) => {
21
- c.data.type === "response" && c.data.requestId === t && (e.removeEventListener("message", r), clearTimeout(n), a(c.data.response));
22
- }, n = setTimeout(() => {
23
- s(new Error("Request timed out")), e.removeEventListener("message", r);
24
- }, o);
25
- e.addEventListener("message", r);
18
+ function H(e, t, r = L) {
19
+ return new Promise((n, s) => {
20
+ const o = (d) => {
21
+ d.data.type === "response" && d.data.requestId === t && (e.removeEventListener("message", o), clearTimeout(a), n(d.data.response));
22
+ }, a = setTimeout(() => {
23
+ s(new Error("Request timed out")), e.removeEventListener("message", o);
24
+ }, r);
25
+ e.addEventListener("message", o);
26
26
  });
27
27
  }
28
- function v(e, t) {
28
+ function N(e, t) {
29
29
  return {
30
30
  type: "response",
31
31
  requestId: e,
32
32
  response: t
33
33
  };
34
34
  }
35
- async function _(e) {
35
+ async function $(e) {
36
36
  let t = new URL(e.request.url);
37
- if (!g(t))
37
+ if (!R(t))
38
38
  try {
39
- const i = new URL(e.request.referrer);
40
- t = S(t, w(i));
39
+ const c = new URL(e.request.referrer);
40
+ t = S(t, m(c));
41
41
  } catch {
42
42
  }
43
- const o = e.request.headers.get("content-type"), a = e.request.method === "POST" ? new Uint8Array(await e.request.clone().arrayBuffer()) : void 0, s = {};
44
- for (const i of e.request.headers.entries())
45
- s[i[0]] = i[1];
46
- let r;
43
+ const r = e.request.headers.get("content-type"), n = e.request.method === "POST" ? new Uint8Array(await e.request.clone().arrayBuffer()) : void 0, s = {};
44
+ for (const c of e.request.headers.entries())
45
+ s[c[0]] = c[1];
46
+ let o;
47
47
  try {
48
- const i = {
48
+ const c = {
49
49
  method: "request",
50
50
  args: [
51
51
  {
52
- body: a,
52
+ body: n,
53
53
  url: t.toString(),
54
54
  method: e.request.method,
55
55
  headers: {
@@ -58,58 +58,58 @@ async function _(e) {
58
58
  // Safari and Firefox don't make the User-Agent header
59
59
  // available in the fetch event. Let's add it manually:
60
60
  "User-agent": self.navigator.userAgent,
61
- "Content-type": o
61
+ "Content-type": r
62
62
  }
63
63
  }
64
64
  ]
65
- }, l = w(t);
66
- if (l === null)
65
+ }, i = m(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 u = await x(i, l);
71
- if (r = await P(self, u), delete r.headers["x-frame-options"], r.headers["content-security-policy"]) {
72
- const h = r.headers["content-security-policy"].map(
73
- (f) => A(
70
+ const p = await P(c, i);
71
+ if (o = await H(self, p), delete o.headers["x-frame-options"], o.headers["content-security-policy"]) {
72
+ const f = o.headers["content-security-policy"].map(
73
+ (l) => x(
74
74
  "frame-ancestors",
75
- f
75
+ l
76
76
  )
77
- ).filter((f) => f.trim().length > 0);
78
- h.length > 0 ? r.headers["content-security-policy"] = h : delete r.headers["content-security-policy"];
77
+ ).filter((l) => l.trim().length > 0);
78
+ f.length > 0 ? o.headers["content-security-policy"] = f : delete o.headers["content-security-policy"];
79
79
  }
80
- } catch (i) {
81
- throw console.error(i, { url: t.toString() }), i;
80
+ } catch (c) {
81
+ throw console.error(c, { url: t.toString() }), c;
82
82
  }
83
- if (r.httpStatusCode >= 300 && r.httpStatusCode <= 399 && r.headers.location) {
84
- const i = w(t);
85
- let l = new URL(
86
- r.headers.location[0],
83
+ if (o.httpStatusCode >= 300 && o.httpStatusCode <= 399 && o.headers.location) {
84
+ const c = m(t);
85
+ let i = new URL(
86
+ o.headers.location[0],
87
87
  t.toString()
88
88
  );
89
- return i && !g(l) && (l = S(l, i)), Response.redirect(
90
- l.toString(),
91
- r.httpStatusCode
89
+ return c && !R(i) && (i = S(i, c)), Response.redirect(
90
+ i.toString(),
91
+ o.httpStatusCode
92
92
  );
93
93
  }
94
- const n = [101, 103, 204, 205, 304].includes(
95
- r.httpStatusCode
94
+ const a = [101, 103, 204, 205, 304].includes(
95
+ o.httpStatusCode
96
96
  );
97
- let c = null;
98
- return n || (r.bodyPort ? c = C(r.bodyPort) : c = r.bytes), new Response(c, {
99
- headers: r.headers,
100
- status: r.httpStatusCode
97
+ let d = null;
98
+ return a || (o.bodyPort ? d = C(o.bodyPort) : d = o.bytes), new Response(d, {
99
+ headers: o.headers,
100
+ status: o.httpStatusCode
101
101
  });
102
102
  }
103
- async function x(e, t) {
104
- const o = E();
105
- for (const a of await self.clients.matchAll({
103
+ async function P(e, t) {
104
+ const r = E();
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
- a.postMessage({
112
+ n.postMessage({
113
113
  ...e,
114
114
  /**
115
115
  * Attach the scope with a URL starting with `/scope:` to this message.
@@ -119,14 +119,14 @@ async function x(e, t) {
119
119
  * helps WASM workers ignore requests meant for other WASM workers.
120
120
  */
121
121
  scope: t,
122
- requestId: o
122
+ requestId: r
123
123
  });
124
- return o;
124
+ return r;
125
125
  }
126
- async function b(e, t) {
127
- let o;
128
- return ["GET", "HEAD"].includes(e.method) ? o = void 0 : "body" in t ? o = t.body : !e.bodyUsed && e.body ? o = e.body : o = await e.arrayBuffer(), new Request(t.url || e.url, {
129
- body: o,
126
+ async function w(e, t) {
127
+ let r;
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
+ body: r,
130
130
  method: e.method,
131
131
  headers: e.headers,
132
132
  referrer: e.referrer,
@@ -148,19 +148,19 @@ async function b(e, t) {
148
148
  *
149
149
  * See MDN: https://developer.mozilla.org/en-US/docs/Web/API/Request/duplex
150
150
  */
151
- ...o instanceof ReadableStream && { duplex: "half" },
151
+ ...r instanceof ReadableStream && { duplex: "half" },
152
152
  ...t
153
153
  });
154
154
  }
155
- let p;
156
- const $ = {
155
+ let h;
156
+ const j = {
157
157
  resetStreamBodySupported() {
158
- p = void 0;
158
+ h = void 0;
159
159
  }
160
160
  };
161
- async function q() {
162
- if (p !== void 0)
163
- return p;
161
+ async function A() {
162
+ if (h !== void 0)
163
+ return h;
164
164
  try {
165
165
  const e = new ReadableStream({
166
166
  start(t) {
@@ -171,70 +171,84 @@ async function q() {
171
171
  method: "POST",
172
172
  body: e,
173
173
  duplex: "half"
174
- }), p = !0;
174
+ }), h = !0;
175
175
  } catch {
176
- p = !1;
176
+ h = !1;
177
177
  }
178
- return p;
178
+ return h;
179
179
  }
180
180
  function k(e) {
181
181
  const t = {};
182
- return e.headers.forEach((o, a) => {
183
- t[a] = o;
182
+ return e.headers.forEach((r, n) => {
183
+ t[n] = r;
184
184
  }), t;
185
185
  }
186
- function A(e, t) {
187
- const o = /^[\u{9}\u{A}\u{C}\u{D}\u{20}]+/u, a = /[\u{9}\u{A}\u{C}\u{D}\u{20}]+$/u, s = /[\u{9}\u{A}\u{C}\u{D}\u{20}]/u;
188
- return t.split(";").filter((r) => {
189
- const n = r.replace(o, "").replace(a, ""), [c] = n.split(
186
+ function x(e, t) {
187
+ 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, s = /[\u{9}\u{A}\u{C}\u{D}\u{20}]/u;
188
+ return t.split(";").filter((o) => {
189
+ const a = o.replace(r, "").replace(n, ""), [d] = a.split(
190
190
  s,
191
191
  // The directive name is the first token.
192
192
  1
193
193
  );
194
- return c.toLowerCase() !== e.toLowerCase();
194
+ return d.toLowerCase() !== e.toLowerCase();
195
195
  }).join(";");
196
196
  }
197
- class H extends Error {
198
- constructor(t, o, a) {
197
+ class O extends Error {
198
+ constructor(t, r, n) {
199
199
  super(
200
- `Could not fetch ${t} – your network appears to be blocking this request (HTTP ${o}). This often happens on school, university, or corporate networks. Try switching to a different network or using a VPN.`
201
- ), this.name = "FirewallInterferenceError", this.url = t, this.status = o, this.statusText = a;
200
+ `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.`
201
+ ), this.name = "FirewallInterferenceError", this.url = t, this.status = r, this.statusText = n;
202
202
  }
203
203
  }
204
- const I = "X-Playground-Cors-Proxy";
205
- async function M(e, t, o, a) {
206
- var l;
204
+ const q = "X-Playground-Cors-Proxy", b = /* @__PURE__ */ new Map([
205
+ [
206
+ "api.anthropic.com",
207
+ {
208
+ "anthropic-dangerous-direct-browser-access": "true"
209
+ }
210
+ ],
211
+ ["api.openai.com", {}],
212
+ ["generativelanguage.googleapis.com", {}]
213
+ ]);
214
+ async function W(e, t, r, n) {
215
+ var c;
207
216
  let s = typeof e == "string" ? new Request(e, t) : e;
208
- const r = a ? new URL(a) : null;
209
- let n = r ? new URL(s.url, r) : new URL(s.url);
210
- if (n.hostname === "localhost" || n.hostname === "127.0.0.1" || n.hostname === "[::1]" || n.hostname === "::1")
217
+ const o = n ? new URL(n) : null;
218
+ let a = o ? new URL(s.url, o) : new URL(s.url);
219
+ if (D(a))
211
220
  return await fetch(s);
212
- if (n.protocol === "http:") {
213
- n.protocol = "https:";
214
- const u = n.toString();
215
- s = await b(s, { url: u }), n = new URL(u);
221
+ if (B(a))
222
+ return s = await I(
223
+ s,
224
+ a
225
+ ), await fetch(s);
226
+ if (a.protocol === "http:") {
227
+ a.protocol = "https:";
228
+ const i = a.toString();
229
+ s = await w(s, { url: i }), a = new URL(i);
216
230
  }
217
- if (!o)
231
+ if (!r)
218
232
  return await fetch(s);
219
- if (r && n.protocol === r.protocol && n.hostname === r.hostname && n.port === r.port && n.pathname.startsWith(r.pathname))
233
+ if (o && a.protocol === o.protocol && a.hostname === o.hostname && a.port === o.port && a.pathname.startsWith(o.pathname))
220
234
  return await fetch(s);
221
- const i = s.clone();
235
+ const d = s.clone();
222
236
  try {
223
237
  return await fetch(s);
224
238
  } catch {
225
- const u = new Headers(s.headers), h = ((l = u.get("x-cors-proxy-allowed-request-headers")) == null ? void 0 : l.split(",")) || [], f = h.includes("authorization") || h.includes("cookie"), m = u.get("content-type");
226
- m && m.toLowerCase().includes("multipart/form-data") && (u.set("x-cors-proxy-content-type", m), u.set("content-type", "application/octet-stream"));
227
- let d = null;
228
- const R = s.method.toUpperCase();
229
- R !== "GET" && R !== "HEAD" && (await q() ? d = i.body : d = await i.arrayBuffer()), d instanceof ReadableStream && new URL(o, import.meta.url).protocol === "http:" && (d = await new Response(d).arrayBuffer());
230
- const T = await b(s, {
231
- url: `${o}${s.url}`,
232
- headers: u,
233
- body: d,
239
+ const i = new Headers(s.headers), p = ((c = i.get("x-cors-proxy-allowed-request-headers")) == null ? void 0 : c.split(",")) || [], f = p.includes("authorization") || p.includes("cookie"), l = i.get("content-type");
240
+ l && l.toLowerCase().includes("multipart/form-data") && (i.set("x-cors-proxy-content-type", l), i.set("content-type", "application/octet-stream"));
241
+ let u = null;
242
+ const g = s.method.toUpperCase();
243
+ g !== "GET" && g !== "HEAD" && (await A() ? u = d.body : u = await d.arrayBuffer()), u instanceof ReadableStream && new URL(r, import.meta.url).protocol === "http:" && (u = await new Response(u).arrayBuffer());
244
+ const T = await w(s, {
245
+ url: `${r}${s.url}`,
246
+ headers: i,
247
+ body: u,
234
248
  ...f && { credentials: "include" }
235
249
  }), y = await fetch(T);
236
- if (!y.headers.has(I))
237
- throw new H(
250
+ if (!y.headers.has(q))
251
+ throw new O(
238
252
  s.url,
239
253
  y.status,
240
254
  y.statusText
@@ -242,18 +256,35 @@ async function M(e, t, o, a) {
242
256
  return y;
243
257
  }
244
258
  }
259
+ function D(e) {
260
+ return e.hostname === "localhost" || e.hostname === "127.0.0.1" || e.hostname === "[::1]" || e.hostname === "::1";
261
+ }
262
+ function B(e) {
263
+ return e.protocol === "https:" && b.has(e.hostname);
264
+ }
265
+ async function I(e, t) {
266
+ const r = b.get(t.hostname);
267
+ if (!r)
268
+ return e;
269
+ const n = new Headers(e.headers);
270
+ for (const [s, o] of Object.entries(r))
271
+ n.has(s) || n.set(s, o);
272
+ return await w(e, {
273
+ headers: n
274
+ });
275
+ }
245
276
  export {
246
- H as FirewallInterferenceError,
247
- $ as __testing,
248
- P as awaitReply,
249
- x as broadcastMessageExpectReply,
250
- b as cloneRequest,
251
- _ as convertFetchEventToPHPRequest,
252
- M as fetchWithCorsProxy,
277
+ O as FirewallInterferenceError,
278
+ j as __testing,
279
+ H as awaitReply,
280
+ P as broadcastMessageExpectReply,
281
+ w as cloneRequest,
282
+ $ as convertFetchEventToPHPRequest,
283
+ W as fetchWithCorsProxy,
253
284
  E as getNextRequestId,
254
285
  k as getRequestHeaders,
255
- D as postMessageExpectReply,
256
- A as removeContentSecurityPolicyDirective,
257
- v as responseTo,
258
- q as supportsReadableStreamBody
286
+ M as postMessageExpectReply,
287
+ x as removeContentSecurityPolicyDirective,
288
+ N as responseTo,
289
+ A as supportsReadableStreamBody
259
290
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@php-wasm/web-service-worker",
3
- "version": "3.1.34",
3
+ "version": "3.1.35",
4
4
  "description": "PHP.wasm – service worker utils",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,14 +31,14 @@
31
31
  "main": "./index.cjs",
32
32
  "module": "./index.js",
33
33
  "license": "GPL-2.0-or-later",
34
- "gitHead": "86daccc84fe31900eb57c4c8e1c3ba21a7ae8d13",
34
+ "gitHead": "9d29b73246e12465902d8ce42c0fe747125bc69a",
35
35
  "engines": {
36
36
  "node": ">=20.10.0",
37
37
  "npm": ">=10.2.3"
38
38
  },
39
39
  "dependencies": {
40
- "@php-wasm/scopes": "3.1.34",
41
- "@php-wasm/universal": "3.1.34"
40
+ "@php-wasm/scopes": "3.1.35",
41
+ "@php-wasm/universal": "3.1.35"
42
42
  },
43
43
  "packageManager": "npm@10.9.2",
44
44
  "overrides": {