@buddhilive/sandbox-sw 0.1.0-beta.3 → 0.1.0-beta.5
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/dist/sw.js +83 -59
- package/package.json +1 -1
package/dist/sw.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
var
|
|
2
|
-
var O = (e, t, r) => t in e ?
|
|
3
|
-
var
|
|
4
|
-
class
|
|
1
|
+
var C = Object.defineProperty;
|
|
2
|
+
var O = (e, t, r) => t in e ? C(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
|
|
3
|
+
var m = (e, t, r) => O(e, typeof t != "symbol" ? t + "" : t, r);
|
|
4
|
+
class E {
|
|
5
5
|
constructor() {
|
|
6
|
-
|
|
6
|
+
m(this, "ports", /* @__PURE__ */ new Map());
|
|
7
7
|
}
|
|
8
8
|
register(t, r) {
|
|
9
9
|
this.ports.set(t, {
|
|
@@ -25,10 +25,10 @@ class C {
|
|
|
25
25
|
return Array.from(this.ports.keys());
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
const
|
|
29
|
-
async function
|
|
30
|
-
const
|
|
31
|
-
if (!
|
|
28
|
+
const l = new E();
|
|
29
|
+
async function x(e, t, r) {
|
|
30
|
+
const s = l.get(e);
|
|
31
|
+
if (!s)
|
|
32
32
|
return new Response(
|
|
33
33
|
`<html><body><h2>503 Service Unavailable</h2><p>No virtual HTTP server listening on port ${e}.</p></body></html>`,
|
|
34
34
|
{
|
|
@@ -41,47 +41,54 @@ async function v(e, t, r) {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
);
|
|
44
|
-
if (
|
|
45
|
-
const
|
|
46
|
-
t.headers.forEach((
|
|
47
|
-
f[
|
|
44
|
+
if (s.messagePort) {
|
|
45
|
+
const o = new MessageChannel(), f = {};
|
|
46
|
+
t.headers.forEach((c, i) => {
|
|
47
|
+
f[i] = c;
|
|
48
48
|
});
|
|
49
|
-
const
|
|
50
|
-
return new Promise((
|
|
51
|
-
let
|
|
49
|
+
const w = t.method !== "GET" && t.method !== "HEAD" ? await t.arrayBuffer() : null;
|
|
50
|
+
return new Promise((c) => {
|
|
51
|
+
let i = null, d = !1, h = 200, y = "OK";
|
|
52
52
|
const u = new Headers({
|
|
53
53
|
"Cross-Origin-Opener-Policy": "same-origin",
|
|
54
54
|
"Cross-Origin-Embedder-Policy": "require-corp"
|
|
55
|
-
}),
|
|
56
|
-
|
|
55
|
+
}), b = setTimeout(() => {
|
|
56
|
+
d || (d = !0, c(
|
|
57
57
|
new Response(
|
|
58
58
|
`<html><body><h2>504 Gateway Timeout</h2><p>Port ${e} timed out responding.</p></body></html>`,
|
|
59
59
|
{ status: 504, headers: { "Content-Type": "text/html" } }
|
|
60
60
|
)
|
|
61
61
|
));
|
|
62
62
|
}, 15e3);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
63
|
+
let g = !1;
|
|
64
|
+
o.port1.onmessage = (P) => {
|
|
65
|
+
clearTimeout(b);
|
|
66
|
+
const n = P.data;
|
|
67
|
+
if (n.type === "headers") {
|
|
68
|
+
if (h = n.status || 200, y = n.statusText || "OK", n.headers)
|
|
69
|
+
for (const [p, a] of Object.entries(n.headers))
|
|
70
|
+
u.set(p, String(a)), p.toLowerCase() === "content-type" && String(a).includes("text/event-stream") && (g = !0);
|
|
70
71
|
return;
|
|
71
72
|
}
|
|
72
|
-
if (
|
|
73
|
-
if (
|
|
74
|
-
|
|
73
|
+
if (n.type === "chunk") {
|
|
74
|
+
if (d)
|
|
75
|
+
i && i.enqueue(new Uint8Array(n.data));
|
|
75
76
|
else {
|
|
76
|
-
|
|
77
|
+
d = !0;
|
|
77
78
|
const p = new ReadableStream({
|
|
78
|
-
start(
|
|
79
|
-
|
|
79
|
+
start(a) {
|
|
80
|
+
i = a, a.enqueue(new Uint8Array(n.data));
|
|
81
|
+
},
|
|
82
|
+
cancel() {
|
|
83
|
+
try {
|
|
84
|
+
o.port1.postMessage({ type: "abort" }), o.port1.close();
|
|
85
|
+
} catch {
|
|
86
|
+
}
|
|
80
87
|
}
|
|
81
88
|
});
|
|
82
|
-
|
|
89
|
+
c(
|
|
83
90
|
new Response(p, {
|
|
84
|
-
status:
|
|
91
|
+
status: h,
|
|
85
92
|
statusText: y,
|
|
86
93
|
headers: u
|
|
87
94
|
})
|
|
@@ -89,32 +96,40 @@ async function v(e, t, r) {
|
|
|
89
96
|
}
|
|
90
97
|
return;
|
|
91
98
|
}
|
|
92
|
-
if (
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
if (n.type === "close") {
|
|
100
|
+
if (i)
|
|
101
|
+
try {
|
|
102
|
+
i.close();
|
|
103
|
+
} catch {
|
|
104
|
+
}
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (n.type === "end" || !n.type) {
|
|
108
|
+
if (i)
|
|
109
|
+
n.body && n.body.byteLength > 0 && i.enqueue(new Uint8Array(n.body)), g || i.close();
|
|
110
|
+
else if (!d) {
|
|
111
|
+
if (d = !0, n.headers)
|
|
112
|
+
for (const [a, T] of Object.entries(n.headers))
|
|
113
|
+
u.set(a, String(T));
|
|
114
|
+
const p = new Response(n.body || null, {
|
|
115
|
+
status: n.status || h,
|
|
116
|
+
statusText: n.statusText || y,
|
|
102
117
|
headers: u
|
|
103
118
|
});
|
|
104
|
-
|
|
119
|
+
c(p);
|
|
105
120
|
}
|
|
106
121
|
}
|
|
107
|
-
},
|
|
122
|
+
}, s.messagePort.postMessage(
|
|
108
123
|
{
|
|
109
124
|
type: "http:request",
|
|
110
125
|
port: e,
|
|
111
126
|
path: r,
|
|
112
127
|
method: t.method,
|
|
113
128
|
headers: f,
|
|
114
|
-
body:
|
|
115
|
-
replyPort:
|
|
129
|
+
body: w,
|
|
130
|
+
replyPort: o.port2
|
|
116
131
|
},
|
|
117
|
-
[
|
|
132
|
+
[o.port2]
|
|
118
133
|
);
|
|
119
134
|
});
|
|
120
135
|
}
|
|
@@ -134,16 +149,25 @@ function R() {
|
|
|
134
149
|
if (typeof BroadcastChannel > "u") return;
|
|
135
150
|
const e = new BroadcastChannel("buddhilive-sandbox-sw");
|
|
136
151
|
e.onmessage = (t) => {
|
|
137
|
-
const { type: r, port:
|
|
138
|
-
r === "port:register" && typeof
|
|
152
|
+
const { type: r, port: s } = t.data;
|
|
153
|
+
r === "port:register" && typeof s == "number" ? (l.register(s), e.postMessage({ type: "port:registered", port: s })) : r === "port:unregister" && typeof s == "number" ? l.unregister(s) : r === "sw:ping" && e.postMessage({
|
|
139
154
|
type: "sw:pong",
|
|
140
|
-
ports:
|
|
155
|
+
ports: l.list()
|
|
141
156
|
});
|
|
142
157
|
}, e.postMessage({
|
|
143
158
|
type: "sw:ready",
|
|
144
|
-
ports:
|
|
159
|
+
ports: l.list()
|
|
145
160
|
});
|
|
146
161
|
}
|
|
162
|
+
self.addEventListener("message", (e) => {
|
|
163
|
+
const t = e.data;
|
|
164
|
+
if (!t || typeof t != "object") return;
|
|
165
|
+
const { type: r, port: s } = t;
|
|
166
|
+
if (r === "port:register" && typeof s == "number") {
|
|
167
|
+
const o = e.ports && e.ports[0];
|
|
168
|
+
l.register(s, o), e.source && "postMessage" in e.source && e.source.postMessage({ type: "port:registered", port: s });
|
|
169
|
+
} else r === "port:unregister" && typeof s == "number" && l.unregister(s);
|
|
170
|
+
});
|
|
147
171
|
self.addEventListener("install", (e) => {
|
|
148
172
|
e.waitUntil(self.skipWaiting());
|
|
149
173
|
});
|
|
@@ -157,17 +181,17 @@ self.addEventListener("activate", (e) => {
|
|
|
157
181
|
self.addEventListener("fetch", (e) => {
|
|
158
182
|
const t = new URL(e.request.url), r = t.pathname.match(/^\/__preview\/(\d+)(.*)/);
|
|
159
183
|
if (r) {
|
|
160
|
-
const
|
|
161
|
-
e.respondWith(
|
|
184
|
+
const s = parseInt(r[1], 10), o = r[2] || "/";
|
|
185
|
+
e.respondWith(x(s, e.request, o));
|
|
162
186
|
return;
|
|
163
187
|
}
|
|
164
188
|
t.origin === self.location.origin && e.respondWith(
|
|
165
189
|
(async () => {
|
|
166
|
-
const
|
|
167
|
-
return
|
|
168
|
-
status:
|
|
169
|
-
statusText:
|
|
170
|
-
headers:
|
|
190
|
+
const s = await fetch(e.request), o = new Headers(s.headers);
|
|
191
|
+
return o.set("Cross-Origin-Opener-Policy", "same-origin"), o.set("Cross-Origin-Embedder-Policy", "require-corp"), new Response(s.body, {
|
|
192
|
+
status: s.status,
|
|
193
|
+
statusText: s.statusText,
|
|
194
|
+
headers: o
|
|
171
195
|
});
|
|
172
196
|
})()
|
|
173
197
|
);
|