@statewalker/webrun-http-browser 0.3.3 → 0.4.2

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.
Files changed (93) hide show
  1. package/README.md +406 -51
  2. package/demo/demo-1.html +109 -194
  3. package/demo/demo-2.html +118 -299
  4. package/dist/core/data-calls.d.ts +5 -0
  5. package/dist/core/data-calls.d.ts.map +1 -0
  6. package/dist/core/data-channels.d.ts +17 -0
  7. package/dist/core/data-channels.d.ts.map +1 -0
  8. package/dist/core/index.d.ts +6 -0
  9. package/dist/core/index.d.ts.map +1 -0
  10. package/dist/core/message-target.d.ts +2 -0
  11. package/dist/core/message-target.d.ts.map +1 -0
  12. package/dist/core/registry.d.ts +18 -0
  13. package/dist/core/registry.d.ts.map +1 -0
  14. package/dist/http/http-send-recieve.d.ts +36 -0
  15. package/dist/http/http-send-recieve.d.ts.map +1 -0
  16. package/dist/http/index.d.ts +4 -0
  17. package/dist/http/index.d.ts.map +1 -0
  18. package/dist/index.d.ts +4 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +2670 -807
  21. package/dist/relay/index-sw.d.ts +7 -0
  22. package/dist/relay/index-sw.d.ts.map +1 -0
  23. package/dist/relay/index.d.ts +69 -0
  24. package/dist/relay/index.d.ts.map +1 -0
  25. package/dist/relay/split-service-url.d.ts +13 -0
  26. package/dist/relay/split-service-url.d.ts.map +1 -0
  27. package/dist/relay-sw.d.ts +2 -0
  28. package/dist/relay-sw.d.ts.map +1 -0
  29. package/dist/relay-sw.js +1660 -0
  30. package/dist/sw/http-sw-dispatcher.d.ts +28 -0
  31. package/dist/sw/http-sw-dispatcher.d.ts.map +1 -0
  32. package/dist/sw/index.d.ts +3 -0
  33. package/dist/sw/index.d.ts.map +1 -0
  34. package/dist/sw/sw-dispatcher.d.ts +74 -0
  35. package/dist/sw/sw-dispatcher.d.ts.map +1 -0
  36. package/dist/sw-worker.d.ts +2 -0
  37. package/dist/sw-worker.d.ts.map +1 -0
  38. package/dist/sw-worker.js +1597 -0
  39. package/dist/sw.d.ts +2 -0
  40. package/dist/sw.d.ts.map +1 -0
  41. package/dist/sw.js +1883 -0
  42. package/package.json +51 -37
  43. package/public/index.html +61 -97
  44. package/public/index.js +7 -4
  45. package/public/sw-worker.js +4 -0
  46. package/public-relay/heartbeat.js +4 -3
  47. package/public-relay/relay-sw.js +4 -6
  48. package/public-relay/relay.html +16 -11
  49. package/src/core/data-calls.ts +46 -0
  50. package/src/core/data-channels.ts +262 -0
  51. package/src/core/index.ts +9 -0
  52. package/src/core/message-target.ts +8 -0
  53. package/src/core/registry.ts +54 -0
  54. package/src/http/http-send-recieve.ts +94 -0
  55. package/src/http/index.ts +7 -0
  56. package/src/index.ts +3 -0
  57. package/src/relay/index-sw.ts +145 -0
  58. package/src/relay/index.ts +261 -0
  59. package/src/relay/split-service-url.ts +30 -0
  60. package/src/relay-sw.ts +7 -0
  61. package/src/sw/http-sw-dispatcher.ts +122 -0
  62. package/src/sw/index.ts +2 -0
  63. package/src/sw/sw-dispatcher.ts +332 -0
  64. package/src/sw-worker.ts +7 -0
  65. package/src/sw.ts +1 -0
  66. package/CHANGELOG.md +0 -1
  67. package/LICENSE +0 -21
  68. package/demo/demo-2 copy.html +0 -598
  69. package/demo/demo-3.html +0 -167
  70. package/dist/index-sw-umd.js +0 -798
  71. package/dist/index-sw-umd.min.js +0 -2
  72. package/dist/index-sw.js +0 -791
  73. package/dist/index-sw.min.js +0 -2
  74. package/dist/index-umd.js +0 -853
  75. package/dist/index-umd.min.js +0 -2
  76. package/dist/index.min.js +0 -2
  77. package/index.js +0 -1
  78. package/src/core/data-calls.js +0 -36
  79. package/src/core/data-channels.js +0 -192
  80. package/src/core/data-send-recieve.js +0 -28
  81. package/src/core/errors.js +0 -13
  82. package/src/core/index.js +0 -4
  83. package/src/http/HttpError.js +0 -59
  84. package/src/http/http-send-recieve.js +0 -40
  85. package/src/http/http-stubs.js +0 -135
  86. package/src/http/index.js +0 -3
  87. package/src/http/readable-streams.js +0 -33
  88. package/src/index.js +0 -7
  89. package/src/relay/index-sw.js +0 -216
  90. package/src/relay/index.js +0 -263
  91. package/src/relay/splitServiceUrl.js +0 -24
  92. package/src/sw/http-sw-dispatcher.js +0 -128
  93. package/src/sw/sw-dispatcher.js +0 -264
@@ -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
- }