@statewalker/webrun-http-browser 0.3.2 → 0.3.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/LICENSE +1 -1
- package/README.md +321 -50
- package/demo/demo-1.html +109 -194
- package/demo/demo-2.html +119 -580
- package/dist/core/data-calls.d.ts +5 -0
- package/dist/core/data-calls.d.ts.map +1 -0
- package/dist/core/data-channels.d.ts +17 -0
- package/dist/core/data-channels.d.ts.map +1 -0
- package/dist/core/index.d.ts +6 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/message-target.d.ts +16 -0
- package/dist/core/message-target.d.ts.map +1 -0
- package/dist/core/registry.d.ts +18 -0
- package/dist/core/registry.d.ts.map +1 -0
- package/dist/http/http-send-recieve.d.ts +23 -0
- package/dist/http/http-send-recieve.d.ts.map +1 -0
- package/dist/http/index.d.ts +4 -0
- package/dist/http/index.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2541 -807
- package/dist/relay/index-sw.d.ts +7 -0
- package/dist/relay/index-sw.d.ts.map +1 -0
- package/dist/relay/index.d.ts +69 -0
- package/dist/relay/index.d.ts.map +1 -0
- package/dist/relay/split-service-url.d.ts +13 -0
- package/dist/relay/split-service-url.d.ts.map +1 -0
- package/dist/relay-sw.d.ts +2 -0
- package/dist/relay-sw.d.ts.map +1 -0
- package/dist/relay-sw.js +878 -0
- package/dist/sw/http-sw-dispatcher.d.ts +28 -0
- package/dist/sw/http-sw-dispatcher.d.ts.map +1 -0
- package/dist/sw/index.d.ts +3 -0
- package/dist/sw/index.d.ts.map +1 -0
- package/dist/sw/sw-dispatcher.d.ts +74 -0
- package/dist/sw/sw-dispatcher.d.ts.map +1 -0
- package/dist/sw-worker.d.ts +2 -0
- package/dist/sw-worker.d.ts.map +1 -0
- package/dist/sw-worker.js +813 -0
- package/dist/sw.d.ts +2 -0
- package/dist/sw.d.ts.map +1 -0
- package/dist/sw.js +1076 -0
- package/package.json +50 -37
- package/public/index.html +61 -97
- package/public/index.js +7 -4
- package/public/sw-worker.js +4 -0
- package/public-relay/heartbeat.js +4 -3
- package/public-relay/relay-sw.js +4 -6
- package/public-relay/relay.html +16 -11
- package/src/core/data-calls.ts +46 -0
- package/src/core/data-channels.ts +224 -0
- package/src/core/index.ts +9 -0
- package/src/core/message-target.ts +18 -0
- package/src/core/registry.ts +54 -0
- package/src/http/http-send-recieve.ts +81 -0
- package/src/http/index.ts +7 -0
- package/src/index.ts +3 -0
- package/src/relay/index-sw.ts +145 -0
- package/src/relay/index.ts +261 -0
- package/src/relay/split-service-url.ts +30 -0
- package/src/relay-sw.ts +7 -0
- package/src/sw/http-sw-dispatcher.ts +122 -0
- package/src/sw/index.ts +2 -0
- package/src/sw/sw-dispatcher.ts +307 -0
- package/src/sw-worker.ts +7 -0
- package/src/sw.ts +1 -0
- package/CHANGELOG.md +0 -1
- package/demo/demo-3.html +0 -167
- package/dist/index-sw-umd.js +0 -798
- package/dist/index-sw-umd.min.js +0 -2
- package/dist/index-sw.js +0 -791
- package/dist/index-sw.min.js +0 -2
- package/dist/index-umd.js +0 -853
- package/dist/index-umd.min.js +0 -2
- package/dist/index.min.js +0 -2
- package/index.js +0 -1
- package/src/core/data-calls.js +0 -36
- package/src/core/data-channels.js +0 -192
- package/src/core/data-send-recieve.js +0 -28
- package/src/core/errors.js +0 -13
- package/src/core/index.js +0 -4
- package/src/http/HttpError.js +0 -59
- package/src/http/http-send-recieve.js +0 -40
- package/src/http/http-stubs.js +0 -135
- package/src/http/index.js +0 -3
- package/src/http/readable-streams.js +0 -33
- package/src/index.js +0 -7
- package/src/relay/index-sw.js +0 -216
- package/src/relay/index.js +0 -263
- package/src/relay/splitServiceUrl.js +0 -24
- package/src/sw/http-sw-dispatcher.js +0 -128
- package/src/sw/sw-dispatcher.js +0 -264
package/src/sw/sw-dispatcher.js
DELETED
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import { newRegistry } from "@statewalker/utils";
|
|
2
|
-
import { get, set } from "idb-keyval";
|
|
3
|
-
import { callChannel, handleChannelCalls } from "../core/data-calls.js";
|
|
4
|
-
|
|
5
|
-
export class SwPortHandler {
|
|
6
|
-
constructor(options) {
|
|
7
|
-
this.options = options;
|
|
8
|
-
if (!this.key) throw new Error("Key is not defined.");
|
|
9
|
-
if (!this.scope) throw new Error("Scope is not defined");
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
get key() {
|
|
13
|
-
return this.options.key;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
get scope() {
|
|
17
|
-
return this.options.scope || new URL("./", this.serviceWorkerUrl).pathname;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
get rootUrl() {
|
|
21
|
-
return new URL(this.scope, import.meta.url);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
get serviceWorkerUrl() {
|
|
25
|
-
if (!this._serviceWorkerUrl) {
|
|
26
|
-
let url = this.options.serviceWorkerUrl
|
|
27
|
-
? new URL(this.options.serviceWorkerUrl)
|
|
28
|
-
: new URL("./index-sw.js", this.rootUrl);
|
|
29
|
-
this._serviceWorkerUrl = url + "";
|
|
30
|
-
}
|
|
31
|
-
return this._serviceWorkerUrl;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async _newCommunicationChannel() {
|
|
35
|
-
const messageChannel = new MessageChannel();
|
|
36
|
-
return [messageChannel.port1, messageChannel.port2];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async _setCommunicationPort(port) {
|
|
40
|
-
await this.options.bindPort(port);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async _updateCommunicationChannel() {
|
|
44
|
-
// console.log("[_updateCommunicationChannel]", this._serviceWorker);
|
|
45
|
-
if (!this._serviceWorker) return;
|
|
46
|
-
const [port1, port2] = await this._newCommunicationChannel();
|
|
47
|
-
await this._setCommunicationPort(port1);
|
|
48
|
-
await callChannel(
|
|
49
|
-
this._serviceWorker,
|
|
50
|
-
"UPDATE_COMMUNICATION_PORT",
|
|
51
|
-
this._getRegistrationInfo(),
|
|
52
|
-
port2,
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
_getRegistrationInfo() {
|
|
57
|
-
return { key: this.key };
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Starts the server.
|
|
62
|
-
*/
|
|
63
|
-
async start() {
|
|
64
|
-
return this._registrationPromise = this._registrationPromise ||
|
|
65
|
-
(async () => {
|
|
66
|
-
const [register, cleanup] = newRegistry();
|
|
67
|
-
this._cleanupRegistrations = cleanup;
|
|
68
|
-
register(() => this._cleanupRegistrations = null);
|
|
69
|
-
const registration = await navigator.serviceWorker.register(
|
|
70
|
-
this.serviceWorkerUrl,
|
|
71
|
-
{
|
|
72
|
-
// type: "module",
|
|
73
|
-
scope: this.scope,
|
|
74
|
-
},
|
|
75
|
-
);
|
|
76
|
-
register(() => registration.unregister());
|
|
77
|
-
|
|
78
|
-
// (example: when the service worker is the same but it is waked up/activated after an idle period)
|
|
79
|
-
register(
|
|
80
|
-
handleChannelCalls(
|
|
81
|
-
navigator.serviceWorker,
|
|
82
|
-
"UPDATE_COMMUNICATION_PORT",
|
|
83
|
-
async (event, params, port) => {
|
|
84
|
-
console.log("[UPDATE_COMMUNICATION_PORT]", port);
|
|
85
|
-
this._setCommunicationPort(port);
|
|
86
|
-
return this._getRegistrationInfo();
|
|
87
|
-
},
|
|
88
|
-
),
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
this._serviceWorker = await getServiceWorkerController();
|
|
92
|
-
await awaitServiceWorkerActivation(this._serviceWorker);
|
|
93
|
-
await this._updateCommunicationChannel();
|
|
94
|
-
|
|
95
|
-
async function getServiceWorkerController() {
|
|
96
|
-
return await new Promise((resolve) => {
|
|
97
|
-
// Resolve right away if this page is already controlled.
|
|
98
|
-
const serviceWorker = navigator.serviceWorker;
|
|
99
|
-
if (serviceWorker.controller) {
|
|
100
|
-
resolve(serviceWorker.controller);
|
|
101
|
-
} else {
|
|
102
|
-
const onChange = () => {
|
|
103
|
-
resolve(serviceWorker.controller);
|
|
104
|
-
serviceWorker.removeEventListener("controllerchange", onChange);
|
|
105
|
-
serviceWorker.oncontrollerchange = null;
|
|
106
|
-
};
|
|
107
|
-
serviceWorker.addEventListener("controllerchange", onChange);
|
|
108
|
-
serviceWorker.oncontrollerchange = onChange;
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// ------------------------------
|
|
114
|
-
// Await until the service is ready
|
|
115
|
-
async function awaitServiceWorkerActivation(worker) {
|
|
116
|
-
const isActivated = () => worker.state === "activated";
|
|
117
|
-
await new Promise(async (resolve) => {
|
|
118
|
-
if (isActivated()) resolve();
|
|
119
|
-
else {
|
|
120
|
-
worker.onstatechange = () => {
|
|
121
|
-
if (!isActivated()) return;
|
|
122
|
-
worker.onstatechange = null;
|
|
123
|
-
resolve();
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
})();
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Stops the server and removes associated handlers.
|
|
133
|
-
*/
|
|
134
|
-
async stop() {
|
|
135
|
-
if (this._cleanupRegistrations) this._cleanupRegistrations();
|
|
136
|
-
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
137
|
-
for (let registration of registrations) {
|
|
138
|
-
try {
|
|
139
|
-
await registration.unregister();
|
|
140
|
-
} catch (error) {
|
|
141
|
-
console.log("Service Worker registration failed: ", error);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export class SwPortDispatcher {
|
|
148
|
-
constructor({ self, log }) {
|
|
149
|
-
this.self = self;
|
|
150
|
-
this.log = log;
|
|
151
|
-
// Contains mapping between path prefixes and clientIds
|
|
152
|
-
this.handlersIndex = new Map();
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
get scope() {
|
|
156
|
-
return new URL("./", import.meta.url) + "";
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
async loadChannelInfo(key) {
|
|
160
|
-
await this._checkActivation();
|
|
161
|
-
let result;
|
|
162
|
-
for (let channelInfo of this.handlersIndex.values()) {
|
|
163
|
-
if (channelInfo.key !== key) continue;
|
|
164
|
-
const clientId = channelInfo.clientId;
|
|
165
|
-
const client = await self.clients.get(clientId);
|
|
166
|
-
if (!client) {
|
|
167
|
-
// Client gone. Remove the corresponding handlers from the index.
|
|
168
|
-
this.handlersIndex.delete(clientId);
|
|
169
|
-
throw Object.assign(new Error("Error 410: Resource Gone"), {
|
|
170
|
-
status: 410,
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
result = channelInfo;
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
return result;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
start() {
|
|
180
|
-
this._cleanup = handleChannelCalls(
|
|
181
|
-
this.self,
|
|
182
|
-
"UPDATE_COMMUNICATION_PORT",
|
|
183
|
-
async (event, channelInfo, port) => {
|
|
184
|
-
const clientId = event.source.id;
|
|
185
|
-
this.log("[UPDATE_COMMUNICATION_PORT]", clientId, channelInfo);
|
|
186
|
-
await this._updateChannelInfo({ ...channelInfo, port, clientId });
|
|
187
|
-
return { ...channelInfo };
|
|
188
|
-
},
|
|
189
|
-
);
|
|
190
|
-
this.self.onmessage = () => {};
|
|
191
|
-
|
|
192
|
-
this.self.addEventListener("install", (event) => {
|
|
193
|
-
this.log("Skip waiting on install.", event);
|
|
194
|
-
this.self.skipWaiting();
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
this.self.addEventListener("activate", async (event) => {
|
|
198
|
-
this.log(
|
|
199
|
-
"Claim control over all clients.",
|
|
200
|
-
event,
|
|
201
|
-
clients,
|
|
202
|
-
this.self.clients,
|
|
203
|
-
);
|
|
204
|
-
event.waitUntil((async () => {
|
|
205
|
-
await this.self.clients.claim();
|
|
206
|
-
await this._checkActivation();
|
|
207
|
-
})());
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
async stop() {
|
|
212
|
-
if (this._cleanup) {
|
|
213
|
-
await this._cleanup();
|
|
214
|
-
this._cleanup = undefined;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
async _checkActivation() {
|
|
219
|
-
return this._activationPromise = this._activationPromise ||
|
|
220
|
-
this._activate();
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
async _activate() {
|
|
224
|
-
this.log("[checkActivation]");
|
|
225
|
-
let clientsIndex = await this._loadClientIds();
|
|
226
|
-
for (let [clientId, client] of clientsIndex.entries()) {
|
|
227
|
-
const messageChannel = new MessageChannel();
|
|
228
|
-
const port = messageChannel.port1;
|
|
229
|
-
const channelInfo = await callChannel(
|
|
230
|
-
client,
|
|
231
|
-
"UPDATE_COMMUNICATION_PORT",
|
|
232
|
-
{},
|
|
233
|
-
messageChannel.port2,
|
|
234
|
-
);
|
|
235
|
-
await this._updateChannelInfo({ ...channelInfo, port, clientId });
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
async _updateChannelInfo(channelInfo) {
|
|
240
|
-
this.log("[updateClientInfo]", channelInfo);
|
|
241
|
-
this.handlersIndex.set(channelInfo.clientId, channelInfo);
|
|
242
|
-
await this._updateClientIds([]);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
async _loadClientIds() {
|
|
246
|
-
const clientIds = new Set((await get("clientIds")) || []);
|
|
247
|
-
return await this._updateClientIds(clientIds);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
async _updateClientIds(clientIds) {
|
|
251
|
-
clientIds = new Set(clientIds);
|
|
252
|
-
for (let clientId of this.handlersIndex.keys()) {
|
|
253
|
-
clientIds.add(clientId);
|
|
254
|
-
}
|
|
255
|
-
const index = new Map();
|
|
256
|
-
for (let clientId of clientIds) {
|
|
257
|
-
const client = await this.self.clients.get(clientId);
|
|
258
|
-
if (client) index.set(clientId, client);
|
|
259
|
-
else this.handlersIndex.delete(clientId);
|
|
260
|
-
}
|
|
261
|
-
await set("clientIds", [...index.keys()].sort());
|
|
262
|
-
return index;
|
|
263
|
-
}
|
|
264
|
-
}
|