-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshinylive-sw.js
270 lines (266 loc) · 10.1 KB
/
shinylive-sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Shinylive 0.0.10
// Copyright 2022 RStudio, PBC
// src/assets/shinylive-inject-socket.txt
var shinylive_inject_socket_default = '// src/messageportwebsocket.ts\nvar MessagePortWebSocket = class extends EventTarget {\n constructor(port) {\n super();\n this.readyState = 0;\n this.addEventListener("open", (e) => {\n if (this.onopen) {\n this.onopen(e);\n }\n });\n this.addEventListener("message", (e) => {\n if (this.onmessage) {\n this.onmessage(e);\n }\n });\n this.addEventListener("error", (e) => {\n if (this.onerror) {\n this.onerror(e);\n }\n });\n this.addEventListener("close", (e) => {\n if (this.onclose) {\n this.onclose(e);\n }\n });\n this._port = port;\n port.addEventListener("message", this._onMessage.bind(this));\n port.start();\n }\n accept() {\n if (this.readyState !== 0) {\n return;\n }\n this.readyState = 1;\n this._port.postMessage({ type: "open" });\n }\n send(data) {\n if (this.readyState === 0) {\n throw new DOMException(\n "Can\'t send messages while WebSocket is in CONNECTING state",\n "InvalidStateError"\n );\n }\n if (this.readyState > 1) {\n return;\n }\n this._port.postMessage({ type: "message", value: { data } });\n }\n close(code, reason) {\n if (this.readyState > 1) {\n return;\n }\n this.readyState = 2;\n this._port.postMessage({ type: "close", value: { code, reason } });\n this.readyState = 3;\n this.dispatchEvent(new CloseEvent("close", { code, reason }));\n }\n _onMessage(e) {\n const event = e.data;\n switch (event.type) {\n case "open":\n if (this.readyState === 0) {\n this.readyState = 1;\n this.dispatchEvent(new Event("open"));\n return;\n }\n break;\n case "message":\n if (this.readyState === 1) {\n this.dispatchEvent(new MessageEvent("message", { ...event.value }));\n return;\n }\n break;\n case "close":\n if (this.readyState < 3) {\n this.readyState = 3;\n this.dispatchEvent(new CloseEvent("close", { ...event.value }));\n return;\n }\n break;\n }\n this._reportError(\n `Unexpected event \'${event.type}\' while in readyState ${this.readyState}`,\n 1002\n );\n }\n _reportError(message, code) {\n this.dispatchEvent(new ErrorEvent("error", { message }));\n if (typeof code === "number") {\n this.close(code, message);\n }\n }\n};\n\n// src/shinylive-inject-socket.ts\nwindow.Shiny.createSocket = function() {\n const channel = new MessageChannel();\n window.parent.postMessage(\n {\n type: "openChannel",\n appName: window.location.pathname.replace(\n new RegExp(".*/([^/]+)/$"),\n "$1"\n ),\n path: "/websocket/"\n },\n "*",\n [channel.port2]\n );\n return new MessagePortWebSocket(channel.port1);\n};\n';
// src/utils.ts
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function dirname(path) {
if (path === "/" || path === "") {
return "";
}
return path.replace(/[/]?[^/]+[/]?$/, "");
}
function uint8ArrayToString(buf) {
let result = "";
for (let i = 0; i < buf.length; i++) {
result += String.fromCharCode(buf[i]);
}
return result;
}
// src/messageporthttp.ts
async function fetchASGI(client, resource, init, filter = (bodyChunk) => bodyChunk) {
if (typeof resource === "string" || typeof init !== "undefined") {
resource = new Request(resource, init);
}
const channel = new MessageChannel();
const clientPort = channel.port1;
client.postMessage(
{
type: "makeRequest",
scope: reqToASGI(resource)
},
[channel.port2]
);
const blob = await resource.blob();
if (!blob.size) {
clientPort.postMessage({
type: "http.request",
more_body: false
});
} else {
const reader = blob.stream().getReader();
try {
while (true) {
const { value: theChunk, done } = await reader.read();
clientPort.postMessage({
type: "http.request",
body: theChunk,
more_body: !done
});
if (done) {
break;
}
}
} finally {
reader.releaseLock();
}
}
return new Promise((resolve) => {
let streamController;
const readableStream = new ReadableStream({
start(controller) {
streamController = controller;
},
cancel(reason) {
}
});
let response;
clientPort.addEventListener("message", (event) => {
const msg = event.data;
if (msg.type === "http.response.start") {
response = asgiToRes(msg, readableStream);
resolve(response);
} else if (msg.type === "http.response.body") {
if (msg.body) {
streamController.enqueue(filter(msg.body, response));
}
if (!msg.more_body) {
streamController.close();
clientPort.close();
}
} else {
throw new Error("Unexpected event type from clientPort: " + msg.type);
}
});
clientPort.start();
});
}
function headersToASGI(headers) {
const result = [];
for (const [key, value] of headers.entries()) {
result.push([key, value]);
}
return result;
}
function reqToASGI(req) {
const url = new URL(req.url);
return {
type: "http",
asgi: {
version: "3.0",
spec_version: "2.1"
},
http_version: "1.1",
method: req.method,
scheme: url.protocol.replace(/:$/, ""),
path: url.pathname,
query_string: url.search.replace(/^\?/, ""),
root_path: "",
headers: headersToASGI(req.headers)
};
}
function asgiToRes(res, body) {
return new Response(body, {
headers: res.headers,
status: res.status
});
}
// src/shinylive-sw.ts
var useCaching = false;
var cacheName = "::prismExperimentsServiceworker";
var version = "v6";
self.addEventListener("install", (event) => {
event.waitUntil(
Promise.all([self.skipWaiting(), caches.open(version + cacheName)])
);
});
self.addEventListener("activate", function(event) {
event.waitUntil(
(async () => {
await self.clients.claim();
const keys = await caches.keys();
return Promise.all(
keys.filter(function(key) {
return key.indexOf(version + cacheName) !== 0;
}).map(function(key) {
return caches.delete(key);
})
);
})()
);
});
self.addEventListener("fetch", function(event) {
const request = event.request;
const url = new URL(request.url);
if (self.location.origin !== url.origin)
return;
if (url.pathname == "/esbuild")
return;
const base_path = dirname(self.location.pathname);
if (url.pathname == `${base_path}/shinylive-inject-socket.js`) {
event.respondWith(
new Response(shinylive_inject_socket_default, {
headers: { "Content-Type": "text/javascript" },
status: 200
})
);
return;
}
const appPathRegex = /.*\/(app_[^/]+\/)/;
const m_appPath = appPathRegex.exec(url.pathname);
if (m_appPath) {
event.respondWith(
(async () => {
let pollCount = 5;
while (!apps[m_appPath[1]]) {
if (pollCount == 0) {
return new Response(
`Couldn't find parent page for ${url}. This may be because the Service Worker has updated. Try reloading the page.`,
{
status: 404
}
);
}
console.log("App URL not registered. Waiting 50ms.");
await sleep(50);
pollCount--;
}
url.pathname = url.pathname.replace(appPathRegex, "/");
const isAppRoot = url.pathname === "/";
const filter = isAppRoot ? injectSocketFilter : identityFilter;
const blob = await request.blob();
return fetchASGI(
apps[m_appPath[1]],
new Request(url.toString(), {
method: request.method,
headers: request.headers,
body: request.method === "GET" || request.method === "HEAD" ? void 0 : blob,
credentials: request.credentials,
cache: request.cache,
redirect: request.redirect,
referrer: request.referrer
}),
void 0,
filter
);
})()
);
return;
}
if (request.method !== "GET") {
return;
}
if (useCaching) {
event.respondWith(
(async () => {
const cachedResponse = await caches.match(request);
if (cachedResponse) {
return cachedResponse;
}
try {
const networkResponse = await fetch(request);
const baseUrl = self.location.origin + dirname(self.location.pathname);
if (request.url.startsWith(baseUrl + "/shinylive/") || request.url === baseUrl + "/favicon.ico") {
const cache = await caches.open(version + cacheName);
cache.put(request, networkResponse.clone());
}
return networkResponse;
} catch {
return new Response("Failed to find in cache, or fetch.", {
status: 404
});
}
})()
);
return;
}
});
var apps = {};
(async () => {
const allClients = await self.clients.matchAll();
for (const client of allClients) {
client.postMessage({
type: "serviceworkerStart"
});
}
})();
self.addEventListener("message", (event) => {
const msg = event.data;
if (msg.type === "configureProxyPath") {
const path = msg.path;
const port = event.ports[0];
apps[path] = port;
}
});
function identityFilter(bodyChunk, response) {
return bodyChunk;
}
function injectSocketFilter(bodyChunk, response) {
const contentType = response.headers.get("content-type");
if (contentType && /^text\/html(;|$)/.test(contentType)) {
const bodyChunkStr = uint8ArrayToString(bodyChunk);
const base_path = dirname(self.location.pathname);
const newStr = bodyChunkStr.replace(
/<\/head>/,
`<script src="${base_path}/shinylive-inject-socket.js" type="module"><\/script>
</head>`
);
const newChunk = Uint8Array.from(
newStr.split("").map((s) => s.charCodeAt(0))
);
return newChunk;
}
return bodyChunk;
}