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