@buddhilive/sandbox-sw 0.1.0-beta.2 → 0.1.0-beta.4
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 +64 -55
- package/package.json +1 -1
package/dist/sw.js
CHANGED
|
@@ -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 a = new C();
|
|
29
|
+
async function E(e, t, r) {
|
|
30
|
+
const s = a.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,70 +41,70 @@ 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((l, i) => {
|
|
47
|
+
f[i] = l;
|
|
48
48
|
});
|
|
49
49
|
const m = t.method !== "GET" && t.method !== "HEAD" ? await t.arrayBuffer() : null;
|
|
50
|
-
return new Promise((
|
|
51
|
-
let
|
|
52
|
-
const
|
|
50
|
+
return new Promise((l) => {
|
|
51
|
+
let i = null, p = !1, h = 200, y = "OK";
|
|
52
|
+
const c = new Headers({
|
|
53
53
|
"Cross-Origin-Opener-Policy": "same-origin",
|
|
54
54
|
"Cross-Origin-Embedder-Policy": "require-corp"
|
|
55
55
|
}), w = setTimeout(() => {
|
|
56
|
-
|
|
56
|
+
p || (p = !0, l(
|
|
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
|
-
|
|
63
|
+
o.port1.onmessage = (b) => {
|
|
64
64
|
clearTimeout(w);
|
|
65
|
-
const
|
|
66
|
-
if (
|
|
67
|
-
if (
|
|
68
|
-
for (const [
|
|
69
|
-
|
|
65
|
+
const n = b.data;
|
|
66
|
+
if (n.type === "headers") {
|
|
67
|
+
if (h = n.status || 200, y = n.statusText || "OK", n.headers)
|
|
68
|
+
for (const [u, d] of Object.entries(n.headers))
|
|
69
|
+
c.set(u, String(d));
|
|
70
70
|
return;
|
|
71
71
|
}
|
|
72
|
-
if (
|
|
73
|
-
if (
|
|
74
|
-
|
|
72
|
+
if (n.type === "chunk") {
|
|
73
|
+
if (p)
|
|
74
|
+
i && i.enqueue(new Uint8Array(n.data));
|
|
75
75
|
else {
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
start(
|
|
79
|
-
|
|
76
|
+
p = !0;
|
|
77
|
+
const u = new ReadableStream({
|
|
78
|
+
start(d) {
|
|
79
|
+
i = d, d.enqueue(new Uint8Array(n.data));
|
|
80
80
|
}
|
|
81
81
|
});
|
|
82
|
-
|
|
83
|
-
new Response(
|
|
84
|
-
status:
|
|
82
|
+
l(
|
|
83
|
+
new Response(u, {
|
|
84
|
+
status: h,
|
|
85
85
|
statusText: y,
|
|
86
|
-
headers:
|
|
86
|
+
headers: c
|
|
87
87
|
})
|
|
88
88
|
);
|
|
89
89
|
}
|
|
90
90
|
return;
|
|
91
91
|
}
|
|
92
|
-
if (
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
else if (!
|
|
96
|
-
if (
|
|
97
|
-
for (const [
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
status:
|
|
101
|
-
statusText:
|
|
102
|
-
headers:
|
|
92
|
+
if (n.type === "end" || !n.type) {
|
|
93
|
+
if (i)
|
|
94
|
+
n.body && n.body.byteLength > 0 && i.enqueue(new Uint8Array(n.body)), i.close();
|
|
95
|
+
else if (!p) {
|
|
96
|
+
if (p = !0, n.headers)
|
|
97
|
+
for (const [d, P] of Object.entries(n.headers))
|
|
98
|
+
c.set(d, String(P));
|
|
99
|
+
const u = new Response(n.body || null, {
|
|
100
|
+
status: n.status || h,
|
|
101
|
+
statusText: n.statusText || y,
|
|
102
|
+
headers: c
|
|
103
103
|
});
|
|
104
|
-
|
|
104
|
+
l(u);
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
|
-
},
|
|
107
|
+
}, s.messagePort.postMessage(
|
|
108
108
|
{
|
|
109
109
|
type: "http:request",
|
|
110
110
|
port: e,
|
|
@@ -112,9 +112,9 @@ async function v(e, t, r) {
|
|
|
112
112
|
method: t.method,
|
|
113
113
|
headers: f,
|
|
114
114
|
body: m,
|
|
115
|
-
replyPort:
|
|
115
|
+
replyPort: o.port2
|
|
116
116
|
},
|
|
117
|
-
[
|
|
117
|
+
[o.port2]
|
|
118
118
|
);
|
|
119
119
|
});
|
|
120
120
|
}
|
|
@@ -134,16 +134,25 @@ function R() {
|
|
|
134
134
|
if (typeof BroadcastChannel > "u") return;
|
|
135
135
|
const e = new BroadcastChannel("buddhilive-sandbox-sw");
|
|
136
136
|
e.onmessage = (t) => {
|
|
137
|
-
const { type: r, port:
|
|
138
|
-
r === "port:register" && typeof
|
|
137
|
+
const { type: r, port: s } = t.data;
|
|
138
|
+
r === "port:register" && typeof s == "number" ? (a.register(s), e.postMessage({ type: "port:registered", port: s })) : r === "port:unregister" && typeof s == "number" ? a.unregister(s) : r === "sw:ping" && e.postMessage({
|
|
139
139
|
type: "sw:pong",
|
|
140
|
-
ports:
|
|
140
|
+
ports: a.list()
|
|
141
141
|
});
|
|
142
142
|
}, e.postMessage({
|
|
143
143
|
type: "sw:ready",
|
|
144
|
-
ports:
|
|
144
|
+
ports: a.list()
|
|
145
145
|
});
|
|
146
146
|
}
|
|
147
|
+
self.addEventListener("message", (e) => {
|
|
148
|
+
const t = e.data;
|
|
149
|
+
if (!t || typeof t != "object") return;
|
|
150
|
+
const { type: r, port: s } = t;
|
|
151
|
+
if (r === "port:register" && typeof s == "number") {
|
|
152
|
+
const o = e.ports && e.ports[0];
|
|
153
|
+
a.register(s, o), e.source && "postMessage" in e.source && e.source.postMessage({ type: "port:registered", port: s });
|
|
154
|
+
} else r === "port:unregister" && typeof s == "number" && a.unregister(s);
|
|
155
|
+
});
|
|
147
156
|
self.addEventListener("install", (e) => {
|
|
148
157
|
e.waitUntil(self.skipWaiting());
|
|
149
158
|
});
|
|
@@ -157,17 +166,17 @@ self.addEventListener("activate", (e) => {
|
|
|
157
166
|
self.addEventListener("fetch", (e) => {
|
|
158
167
|
const t = new URL(e.request.url), r = t.pathname.match(/^\/__preview\/(\d+)(.*)/);
|
|
159
168
|
if (r) {
|
|
160
|
-
const
|
|
161
|
-
e.respondWith(
|
|
169
|
+
const s = parseInt(r[1], 10), o = r[2] || "/";
|
|
170
|
+
e.respondWith(E(s, e.request, o));
|
|
162
171
|
return;
|
|
163
172
|
}
|
|
164
173
|
t.origin === self.location.origin && e.respondWith(
|
|
165
174
|
(async () => {
|
|
166
|
-
const
|
|
167
|
-
return
|
|
168
|
-
status:
|
|
169
|
-
statusText:
|
|
170
|
-
headers:
|
|
175
|
+
const s = await fetch(e.request), o = new Headers(s.headers);
|
|
176
|
+
return o.set("Cross-Origin-Opener-Policy", "same-origin"), o.set("Cross-Origin-Embedder-Policy", "require-corp"), new Response(s.body, {
|
|
177
|
+
status: s.status,
|
|
178
|
+
statusText: s.statusText,
|
|
179
|
+
headers: o
|
|
171
180
|
});
|
|
172
181
|
})()
|
|
173
182
|
);
|