@php-wasm/web-service-worker 1.0.28 → 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 CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@php-wasm/scopes"),h=25e3;let f=0;function y(e,t,...s){const n=d();return e.postMessage({...t,requestId:n},...s),n}function d(){return++f}function u(e,t,s=h){return new Promise((n,c)=>{const r=a=>{a.data.type==="response"&&a.data.requestId===t&&(e.removeEventListener("message",r),clearTimeout(o),n(a.data.response))},o=setTimeout(()=>{c(new Error("Request timed out")),e.removeEventListener("message",r)},s);e.addEventListener("message",r)})}function R(e,t){return{type:"response",requestId:e,response:t}}async function g(e){let t=new URL(e.request.url);if(!i.isURLScoped(t))try{const o=new URL(e.request.referrer);t=i.setURLScope(t,i.getURLScope(o))}catch{}const s=e.request.headers.get("content-type"),n=e.request.method==="POST"?new Uint8Array(await e.request.clone().arrayBuffer()):void 0,c={};for(const o of e.request.headers.entries())c[o[0]]=o[1];let r;try{const o={method:"request",args:[{body:n,url:t.toString(),method:e.request.method,headers:{...c,Host:t.host,"User-agent":self.navigator.userAgent,"Content-type":s}}]},a=i.getURLScope(t);if(a===null)throw new Error(`The URL ${t.toString()} is not scoped. This should not happen.`);const p=await l(o,a);r=await u(self,p),delete r.headers["x-frame-options"]}catch(o){throw console.error(o,{url:t.toString()}),o}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 l(e,t){const s=d();for(const n of await self.clients.matchAll({includeUncontrolled:!0}))n.postMessage({...e,scope:t,requestId:s});return s}async function m(e,t){const s=["GET","HEAD"].includes(e.method)||"body"in t?void 0:await e.blob();return new Request(t.url||e.url,{body:s,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})}function E(e){const t={};return e.headers.forEach((s,n)=>{t[n]=s}),t}exports.awaitReply=u;exports.broadcastMessageExpectReply=l;exports.cloneRequest=m;exports.convertFetchEventToPHPRequest=g;exports.getNextRequestId=d;exports.getRequestHeaders=E;exports.postMessageExpectReply=y;exports.responseTo=R;
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 l, setURLScope as h, getURLScope as i } from "@php-wasm/scopes";
2
- const p = 25e3;
3
- let f = 0;
4
- function g(e, t, ...o) {
5
- const n = d();
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: n
9
+ requestId: s
10
10
  },
11
- ...o
12
- ), n;
11
+ ...r
12
+ ), s;
13
13
  }
14
- function d() {
15
- return ++f;
14
+ function u() {
15
+ return ++y;
16
16
  }
17
- function m(e, t, o = p) {
18
- return new Promise((n, c) => {
19
- const r = (a) => {
20
- a.data.type === "response" && a.data.requestId === t && (e.removeEventListener("message", r), clearTimeout(s), n(a.data.response));
21
- }, s = setTimeout(() => {
22
- c(new Error("Request timed out")), e.removeEventListener("message", r);
23
- }, o);
24
- e.addEventListener("message", r);
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 w(e, t) {
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 E(e) {
34
+ async function b(e) {
35
35
  let t = new URL(e.request.url);
36
- if (!l(t))
36
+ if (!h(t))
37
37
  try {
38
- const s = new URL(e.request.referrer);
39
- t = h(t, i(s));
38
+ const n = new URL(e.request.referrer);
39
+ t = p(t, d(n));
40
40
  } catch {
41
41
  }
42
- const o = e.request.headers.get("content-type"), n = e.request.method === "POST" ? new Uint8Array(await e.request.clone().arrayBuffer()) : void 0, c = {};
43
- for (const s of e.request.headers.entries())
44
- c[s[0]] = s[1];
45
- let r;
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 s = {
47
+ const n = {
48
48
  method: "request",
49
49
  args: [
50
50
  {
51
- body: n,
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": o
60
+ "Content-type": r
61
61
  }
62
62
  }
63
63
  ]
64
- }, a = i(t);
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 u = await y(s, a);
70
- r = await m(self, u), delete r.headers["x-frame-options"];
71
- } catch (s) {
72
- throw console.error(s, { url: t.toString() }), s;
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 r.httpStatusCode >= 300 && r.httpStatusCode <= 399 && r.headers.location ? Response.redirect(
75
- r.headers.location[0],
76
- r.httpStatusCode
77
- ) : new Response(r.bytes, {
78
- headers: r.headers,
79
- status: r.httpStatusCode
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 y(e, t) {
83
- const o = d();
84
- for (const n of await self.clients.matchAll({
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
- n.postMessage({
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: o
101
+ requestId: r
102
102
  });
103
- return o;
103
+ return r;
104
104
  }
105
- async function S(e, t) {
106
- const o = ["GET", "HEAD"].includes(e.method) || "body" in t ? void 0 : await e.blob();
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: o,
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((o, n) => {
124
- t[n] = o;
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
- y as broadcastMessageExpectReply,
130
- S as cloneRequest,
131
- E as convertFetchEventToPHPRequest,
132
- d as getNextRequestId,
138
+ R as broadcastMessageExpectReply,
139
+ i as cloneRequest,
140
+ b as convertFetchEventToPHPRequest,
141
+ u as getNextRequestId,
133
142
  U as getRequestHeaders,
134
- g as postMessageExpectReply,
135
- w as responseTo
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.28",
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": "ed7171a91717c70dd7162e3d93283f66d21c001d",
34
+ "gitHead": "9dbbe2b22ccbacda9c0da4444c51d5739e784302",
35
35
  "engines": {
36
- "node": ">=18.18.0",
37
- "npm": ">=8.11.0"
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.28"
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
  *