@statewalker/webrun-http-browser 0.1.0 → 0.2.0
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/index-sw-umd.js +413 -270
- package/dist/index-sw-umd.min.js +2 -2
- package/dist/index-sw.js +409 -265
- package/dist/index-sw.min.js +2 -2
- package/dist/index-umd.js +286 -417
- package/dist/index-umd.min.js +2 -2
- package/dist/index.js +277 -412
- package/dist/index.min.js +2 -2
- package/package.json +1 -1
- package/public/index.html +61 -93
- package/public-relay/heartbeat.js +1 -1
- package/public-relay/relay-sw.js +6 -343
- package/public-relay/relay.html +5 -26
- package/src/{data-channels.js → core/data-channels.js} +26 -10
- package/src/core/index.js +4 -0
- package/src/http/HttpError.js +59 -0
- package/src/{http-send-recieve.js → http/http-send-recieve.js} +1 -1
- package/src/http/index.js +3 -0
- package/src/index.js +4 -9
- package/src/relay/index-sw.js +216 -0
- package/{public-relay/relay-lib.js → src/relay/index.js} +80 -6
- package/src/relay/splitServiceUrl.js +24 -0
- package/src/{http-sw-dispatcher.js → sw/http-sw-dispatcher.js} +1 -1
- package/src/{sw-dispatcher.js → sw/sw-dispatcher.js} +1 -1
- package/public/abc.txt +0 -1
- package/public/index-service-worker.js +0 -1
- /package/src/{data-calls.js → core/data-calls.js} +0 -0
- /package/src/{data-send-recieve.js → core/data-send-recieve.js} +0 -0
- /package/src/{errors.js → core/errors.js} +0 -0
- /package/src/{http-stubs.js → http/http-stubs.js} +0 -0
- /package/src/{readable-streams.js → http/readable-streams.js} +0 -0
package/dist/index-umd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// @statewalker/webrun-http-browser v0.
|
|
1
|
+
// @statewalker/webrun-http-browser v0.2.0 https://github.com/statewalker/statewalker-webrun-http-browser Copyright 2023 Mikhail Kotelnikov
|
|
2
2
|
(function (global, factory) {
|
|
3
3
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
4
4
|
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
@@ -205,7 +205,10 @@ function newChannel(port) {
|
|
|
205
205
|
await listener(data);
|
|
206
206
|
}
|
|
207
207
|
};
|
|
208
|
-
const channel = newInvokationChannel(
|
|
208
|
+
const channel = newInvokationChannel({
|
|
209
|
+
port,
|
|
210
|
+
handler: notifyAll,
|
|
211
|
+
});
|
|
209
212
|
|
|
210
213
|
const start = () => channel.start();
|
|
211
214
|
|
|
@@ -243,15 +246,20 @@ function newChannel(port) {
|
|
|
243
246
|
};
|
|
244
247
|
}
|
|
245
248
|
|
|
246
|
-
function newInvokationChannel(
|
|
247
|
-
|
|
249
|
+
function newInvokationChannel({
|
|
250
|
+
port,
|
|
251
|
+
handler = () => {
|
|
252
|
+
throw new Error("Handler not implemented");
|
|
253
|
+
},
|
|
254
|
+
onError = console.error,
|
|
255
|
+
newCallId = () =>
|
|
256
|
+
newInvokationChannel.__counter = (newInvokationChannel.__counter || 0) + 1,
|
|
248
257
|
}) {
|
|
249
258
|
const MESSAGE_TYPE_REQUEST = "REQUEST";
|
|
250
259
|
const MESSAGE_TYPE_RESPONSE = "RESPONSE";
|
|
251
260
|
const EVENT_MESSAGE = "message";
|
|
252
261
|
|
|
253
262
|
const requests = {};
|
|
254
|
-
let requestCounter = 0;
|
|
255
263
|
const listener = async (event) => {
|
|
256
264
|
const data = event.data || {};
|
|
257
265
|
const { type, callId } = data;
|
|
@@ -288,17 +296,25 @@ function newInvokationChannel(port, handler = () => {
|
|
|
288
296
|
};
|
|
289
297
|
|
|
290
298
|
const start = async () => {
|
|
291
|
-
|
|
292
|
-
|
|
299
|
+
try {
|
|
300
|
+
port.addEventListener(EVENT_MESSAGE, listener);
|
|
301
|
+
port.start && await port.start();
|
|
302
|
+
} catch (e) {
|
|
303
|
+
onError(e);
|
|
304
|
+
}
|
|
293
305
|
};
|
|
294
306
|
|
|
295
307
|
const close = async () => {
|
|
296
|
-
|
|
297
|
-
|
|
308
|
+
try {
|
|
309
|
+
port.removeEventListener(EVENT_MESSAGE, listener);
|
|
310
|
+
port.close && await port.close();
|
|
311
|
+
} catch (e) {
|
|
312
|
+
onError(e);
|
|
313
|
+
}
|
|
298
314
|
};
|
|
299
315
|
|
|
300
316
|
const invoke = async function (request = {}, ...transfers) {
|
|
301
|
-
const callId =
|
|
317
|
+
const callId = newCallId();
|
|
302
318
|
return new Promise((resolve, reject) => {
|
|
303
319
|
try {
|
|
304
320
|
requests[callId] = { resolve, reject };
|
|
@@ -517,458 +533,311 @@ async function sendHttpRequest(communicationPort, request) {
|
|
|
517
533
|
return await clientStub(request);
|
|
518
534
|
}
|
|
519
535
|
|
|
520
|
-
function
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
if (!defaultGetStoreFunc) {
|
|
537
|
-
defaultGetStoreFunc = createStore('keyval-store', 'keyval');
|
|
538
|
-
}
|
|
539
|
-
return defaultGetStoreFunc;
|
|
540
|
-
}
|
|
541
|
-
/**
|
|
542
|
-
* Get a value by its key.
|
|
543
|
-
*
|
|
544
|
-
* @param key
|
|
545
|
-
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
|
546
|
-
*/
|
|
547
|
-
function get(key, customStore = defaultGetStore()) {
|
|
548
|
-
return customStore('readonly', (store) => promisifyRequest(store.get(key)));
|
|
549
|
-
}
|
|
550
|
-
/**
|
|
551
|
-
* Set a value with a key.
|
|
552
|
-
*
|
|
553
|
-
* @param key
|
|
554
|
-
* @param value
|
|
555
|
-
* @param customStore Method to get a custom store. Use with caution (see the docs).
|
|
556
|
-
*/
|
|
557
|
-
function set(key, value, customStore = defaultGetStore()) {
|
|
558
|
-
return customStore('readwrite', (store) => {
|
|
559
|
-
store.put(value, key);
|
|
560
|
-
return promisifyRequest(store.transaction);
|
|
561
|
-
});
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
class SwPortHandler {
|
|
565
|
-
constructor(options) {
|
|
566
|
-
this.options = options;
|
|
567
|
-
if (!this.key) throw new Error("Key is not defined.");
|
|
568
|
-
if (!this.scope) throw new Error("Scope is not defined");
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
get key() {
|
|
572
|
-
return this.options.key;
|
|
536
|
+
function splitServiceUrl(url, separator = "~") {
|
|
537
|
+
let key = "";
|
|
538
|
+
let path = "";
|
|
539
|
+
let str = url + "";
|
|
540
|
+
const idx = str.indexOf(separator);
|
|
541
|
+
let baseUrl = "";
|
|
542
|
+
if (idx >= 0) {
|
|
543
|
+
baseUrl = str.substring(0, idx + separator.length);
|
|
544
|
+
str.substring(idx + separator.length)
|
|
545
|
+
.replace(/^([^\/]+)/, (match, $1) => {
|
|
546
|
+
baseUrl += match;
|
|
547
|
+
if (baseUrl.length < str.length) baseUrl += "/";
|
|
548
|
+
key = $1;
|
|
549
|
+
path = str.substring(baseUrl.length);
|
|
550
|
+
return "";
|
|
551
|
+
});
|
|
573
552
|
}
|
|
553
|
+
return {
|
|
554
|
+
url,
|
|
555
|
+
key,
|
|
556
|
+
baseUrl,
|
|
557
|
+
path,
|
|
558
|
+
};
|
|
559
|
+
}
|
|
574
560
|
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
561
|
+
function newServiceWorkerPort() {
|
|
562
|
+
// Bind the service worker to
|
|
563
|
+
const channel = new MessageChannel();
|
|
564
|
+
channel.port1.onmessage = (event) => {
|
|
565
|
+
navigator.serviceWorker.controller.postMessage(event.data, event.ports);
|
|
566
|
+
};
|
|
567
|
+
navigator.serviceWorker.addEventListener("message", (event) => {
|
|
568
|
+
channel.port1.postMessage(event.data, event.ports);
|
|
569
|
+
});
|
|
570
|
+
return channel.port2;
|
|
571
|
+
}
|
|
578
572
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
573
|
+
async function initServiceWorker1({ swUrl, scopeUrl, type }) {
|
|
574
|
+
const {
|
|
575
|
+
installing,
|
|
576
|
+
waiting,
|
|
577
|
+
active,
|
|
578
|
+
} = await navigator.serviceWorker.register(swUrl, {
|
|
579
|
+
// type: "module",
|
|
580
|
+
scope: scopeUrl,
|
|
581
|
+
});
|
|
582
|
+
(installing || waiting || active).addEventListener("statechange", (e) => {
|
|
583
|
+
console.log("state", e.target.state);
|
|
584
|
+
});
|
|
582
585
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
586
|
+
const serviceWorker = navigator.serviceWorker;
|
|
587
|
+
await new Promise((resolve) => {
|
|
588
|
+
// Resolve right away if this page is already controlled.
|
|
589
|
+
if (serviceWorker.controller) {
|
|
590
|
+
resolve();
|
|
591
|
+
} else {
|
|
592
|
+
const onChange = () => {
|
|
593
|
+
resolve();
|
|
594
|
+
serviceWorker.removeEventListener("controllerchange", onChange);
|
|
595
|
+
serviceWorker.oncontrollerchange = null;
|
|
596
|
+
};
|
|
597
|
+
serviceWorker.addEventListener("controllerchange", onChange);
|
|
598
|
+
serviceWorker.oncontrollerchange = onChange;
|
|
589
599
|
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
async
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
async _updateCommunicationChannel() {
|
|
603
|
-
// console.log("[_updateCommunicationChannel]", this._serviceWorker);
|
|
604
|
-
if (!this._serviceWorker) return;
|
|
605
|
-
const [port1, port2] = await this._newCommunicationChannel();
|
|
606
|
-
await this._setCommunicationPort(port1);
|
|
607
|
-
await callChannel(
|
|
608
|
-
this._serviceWorker,
|
|
609
|
-
"UPDATE_COMMUNICATION_PORT",
|
|
610
|
-
this._getRegistrationInfo(),
|
|
611
|
-
port2,
|
|
612
|
-
);
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
_getRegistrationInfo() {
|
|
616
|
-
return { key: this.key };
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
/**
|
|
620
|
-
* Starts the server.
|
|
621
|
-
*/
|
|
622
|
-
async start() {
|
|
623
|
-
return this._registrationPromise = this._registrationPromise ||
|
|
624
|
-
(async () => {
|
|
625
|
-
const [register, cleanup] = newRegistry();
|
|
626
|
-
this._cleanupRegistrations = cleanup;
|
|
627
|
-
register(() => this._cleanupRegistrations = null);
|
|
628
|
-
const registration = await navigator.serviceWorker.register(
|
|
629
|
-
this.serviceWorkerUrl,
|
|
630
|
-
{
|
|
631
|
-
// type: "module",
|
|
632
|
-
scope: this.scope,
|
|
633
|
-
},
|
|
634
|
-
);
|
|
635
|
-
register(() => registration.unregister());
|
|
636
|
-
|
|
637
|
-
// (example: when the service worker is the same but it is waked up/activated after an idle period)
|
|
638
|
-
register(
|
|
639
|
-
handleChannelCalls(
|
|
640
|
-
navigator.serviceWorker,
|
|
641
|
-
"UPDATE_COMMUNICATION_PORT",
|
|
642
|
-
async (event, params, port) => {
|
|
643
|
-
console.log("[UPDATE_COMMUNICATION_PORT]", port);
|
|
644
|
-
this._setCommunicationPort(port);
|
|
645
|
-
return this._getRegistrationInfo();
|
|
646
|
-
},
|
|
647
|
-
),
|
|
648
|
-
);
|
|
649
|
-
|
|
650
|
-
this._serviceWorker = await getServiceWorkerController();
|
|
651
|
-
await awaitServiceWorkerActivation(this._serviceWorker);
|
|
652
|
-
await this._updateCommunicationChannel();
|
|
653
|
-
|
|
654
|
-
async function getServiceWorkerController() {
|
|
655
|
-
return await new Promise((resolve) => {
|
|
656
|
-
// Resolve right away if this page is already controlled.
|
|
657
|
-
const serviceWorker = navigator.serviceWorker;
|
|
658
|
-
if (serviceWorker.controller) {
|
|
659
|
-
resolve(serviceWorker.controller);
|
|
660
|
-
} else {
|
|
661
|
-
const onChange = () => {
|
|
662
|
-
resolve(serviceWorker.controller);
|
|
663
|
-
serviceWorker.removeEventListener("controllerchange", onChange);
|
|
664
|
-
serviceWorker.oncontrollerchange = null;
|
|
665
|
-
};
|
|
666
|
-
serviceWorker.addEventListener("controllerchange", onChange);
|
|
667
|
-
serviceWorker.oncontrollerchange = onChange;
|
|
668
|
-
}
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
// ------------------------------
|
|
673
|
-
// Await until the service is ready
|
|
674
|
-
async function awaitServiceWorkerActivation(worker) {
|
|
675
|
-
const isActivated = () => worker.state === "activated";
|
|
676
|
-
await new Promise(async (resolve) => {
|
|
677
|
-
if (isActivated()) resolve();
|
|
678
|
-
else {
|
|
679
|
-
worker.onstatechange = () => {
|
|
680
|
-
if (!isActivated()) return;
|
|
681
|
-
worker.onstatechange = null;
|
|
682
|
-
resolve();
|
|
683
|
-
};
|
|
684
|
-
}
|
|
685
|
-
});
|
|
686
|
-
}
|
|
687
|
-
})();
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
/**
|
|
691
|
-
* Stops the server and removes associated handlers.
|
|
692
|
-
*/
|
|
693
|
-
async stop() {
|
|
694
|
-
if (this._cleanupRegistrations) this._cleanupRegistrations();
|
|
695
|
-
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
696
|
-
for (let registration of registrations) {
|
|
697
|
-
try {
|
|
698
|
-
await registration.unregister();
|
|
699
|
-
} catch (error) {
|
|
700
|
-
console.log("Service Worker registration failed: ", error);
|
|
701
|
-
}
|
|
600
|
+
});
|
|
601
|
+
const worker = serviceWorker.controller;
|
|
602
|
+
const isActivated = () => worker.state === "activated";
|
|
603
|
+
return await new Promise(async (resolve) => {
|
|
604
|
+
if (isActivated()) resolve(worker);
|
|
605
|
+
else {
|
|
606
|
+
worker.onstatechange = () => {
|
|
607
|
+
if (!isActivated()) return;
|
|
608
|
+
worker.onstatechange = null;
|
|
609
|
+
resolve(worker);
|
|
610
|
+
};
|
|
702
611
|
}
|
|
703
|
-
}
|
|
612
|
+
});
|
|
704
613
|
}
|
|
705
614
|
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
status: 410,
|
|
730
|
-
});
|
|
615
|
+
async function initServiceWorker({ swUrl, scopeUrl, type }) {
|
|
616
|
+
await navigator.serviceWorker.register(swUrl, {
|
|
617
|
+
type,
|
|
618
|
+
scope: scopeUrl,
|
|
619
|
+
});
|
|
620
|
+
const worker = await getServiceWorkerController();
|
|
621
|
+
await awaitServiceWorkerActivation(worker);
|
|
622
|
+
return worker;
|
|
623
|
+
|
|
624
|
+
async function getServiceWorkerController() {
|
|
625
|
+
return await new Promise((resolve) => {
|
|
626
|
+
// Resolve right away if this page is already controlled.
|
|
627
|
+
const serviceWorker = navigator.serviceWorker;
|
|
628
|
+
if (serviceWorker.controller) {
|
|
629
|
+
resolve(serviceWorker.controller);
|
|
630
|
+
} else {
|
|
631
|
+
const onChange = () => {
|
|
632
|
+
resolve(serviceWorker.controller);
|
|
633
|
+
serviceWorker.removeEventListener("controllerchange", onChange);
|
|
634
|
+
serviceWorker.oncontrollerchange = null;
|
|
635
|
+
};
|
|
636
|
+
serviceWorker.addEventListener("controllerchange", onChange);
|
|
637
|
+
serviceWorker.oncontrollerchange = onChange;
|
|
731
638
|
}
|
|
732
|
-
result = channelInfo;
|
|
733
|
-
break;
|
|
734
|
-
}
|
|
735
|
-
return result;
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
start() {
|
|
739
|
-
this._cleanup = handleChannelCalls(
|
|
740
|
-
this.self,
|
|
741
|
-
"UPDATE_COMMUNICATION_PORT",
|
|
742
|
-
async (event, channelInfo, port) => {
|
|
743
|
-
const clientId = event.source.id;
|
|
744
|
-
this.log("[UPDATE_COMMUNICATION_PORT]", clientId, channelInfo);
|
|
745
|
-
await this._updateChannelInfo({ ...channelInfo, port, clientId });
|
|
746
|
-
return { ...channelInfo };
|
|
747
|
-
},
|
|
748
|
-
);
|
|
749
|
-
this.self.onmessage = () => {};
|
|
750
|
-
|
|
751
|
-
this.self.addEventListener("install", (event) => {
|
|
752
|
-
this.log("Skip waiting on install.", event);
|
|
753
|
-
this.self.skipWaiting();
|
|
754
|
-
});
|
|
755
|
-
|
|
756
|
-
this.self.addEventListener("activate", async (event) => {
|
|
757
|
-
this.log(
|
|
758
|
-
"Claim control over all clients.",
|
|
759
|
-
event,
|
|
760
|
-
clients,
|
|
761
|
-
this.self.clients,
|
|
762
|
-
);
|
|
763
|
-
event.waitUntil((async () => {
|
|
764
|
-
await this.self.clients.claim();
|
|
765
|
-
await this._checkActivation();
|
|
766
|
-
})());
|
|
767
639
|
});
|
|
768
640
|
}
|
|
769
641
|
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
642
|
+
// ------------------------------
|
|
643
|
+
// Await until the service is ready
|
|
644
|
+
async function awaitServiceWorkerActivation(worker) {
|
|
645
|
+
const isActivated = () => worker.state === "activated";
|
|
646
|
+
await new Promise(async (resolve) => {
|
|
647
|
+
if (isActivated()) resolve();
|
|
648
|
+
else {
|
|
649
|
+
worker.onstatechange = () => {
|
|
650
|
+
if (!isActivated()) return;
|
|
651
|
+
worker.onstatechange = null;
|
|
652
|
+
resolve();
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
});
|
|
780
656
|
}
|
|
657
|
+
}
|
|
781
658
|
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
}
|
|
796
|
-
}
|
|
659
|
+
async function initHttpService(handler, {
|
|
660
|
+
key,
|
|
661
|
+
port,
|
|
662
|
+
}) {
|
|
663
|
+
return await registerConnectionsHandler({
|
|
664
|
+
key,
|
|
665
|
+
communicationPort: port,
|
|
666
|
+
handler: async (event, data, callPort) => {
|
|
667
|
+
handleHttpRequests(callPort, handler);
|
|
668
|
+
return true;
|
|
669
|
+
},
|
|
670
|
+
});
|
|
671
|
+
}
|
|
797
672
|
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
673
|
+
async function callHttpService(request, {
|
|
674
|
+
key,
|
|
675
|
+
port,
|
|
676
|
+
}) {
|
|
677
|
+
const callPort = await initializeConnection({
|
|
678
|
+
key,
|
|
679
|
+
communicationPort: port,
|
|
680
|
+
});
|
|
681
|
+
const result = await sendHttpRequest(callPort, request);
|
|
682
|
+
return result;
|
|
683
|
+
}
|
|
803
684
|
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
685
|
+
function getRelayWindowMessageHandler({
|
|
686
|
+
swUrl = new URL("./index-sw.js", (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('index-umd.js', document.baseURI).href))) + "",
|
|
687
|
+
scopeUrl = new URL("../", (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('index-umd.js', document.baseURI).href))) + "",
|
|
688
|
+
} = {}) {
|
|
689
|
+
let externalPort;
|
|
690
|
+
return async (ev) => {
|
|
691
|
+
if (ev.data.type === "CONNECT") {
|
|
692
|
+
const newExternalPort = (ev.ports || [])[0];
|
|
693
|
+
if (externalPort) {
|
|
694
|
+
newExternalPort.close();
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
externalPort = newExternalPort;
|
|
698
|
+
await initServiceWorker({
|
|
699
|
+
swUrl,
|
|
700
|
+
scopeUrl,
|
|
701
|
+
});
|
|
702
|
+
const serviceWorkerPort = newServiceWorkerPort();
|
|
703
|
+
serviceWorkerPort.onmessage = (event) => {
|
|
704
|
+
externalPort.postMessage(event.data, event.ports);
|
|
705
|
+
};
|
|
808
706
|
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
clientIds.add(clientId);
|
|
813
|
-
}
|
|
814
|
-
const index = new Map();
|
|
815
|
-
for (let clientId of clientIds) {
|
|
816
|
-
const client = await this.self.clients.get(clientId);
|
|
817
|
-
if (client) index.set(clientId, client);
|
|
818
|
-
else this.handlersIndex.delete(clientId);
|
|
707
|
+
externalPort.onmessage = (event) => {
|
|
708
|
+
serviceWorkerPort.postMessage(event.data, event.ports);
|
|
709
|
+
};
|
|
819
710
|
}
|
|
820
|
-
|
|
821
|
-
return index;
|
|
822
|
-
}
|
|
711
|
+
};
|
|
823
712
|
}
|
|
824
713
|
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
714
|
+
async function newRemoteRelayChannel({
|
|
715
|
+
baseUrl = new URL("./", (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('index-umd.js', document.baseURI).href))),
|
|
716
|
+
url = new URL("./relay.html", baseUrl),
|
|
717
|
+
container = document.body,
|
|
718
|
+
} = {}) {
|
|
719
|
+
const messageChannel = new MessageChannel();
|
|
720
|
+
const { iframe, promise } = await newIFrame(url);
|
|
721
|
+
Object.assign(iframe.style, {
|
|
722
|
+
// height: "100px"
|
|
723
|
+
position: "fixed",
|
|
724
|
+
width: "1px",
|
|
725
|
+
height: "1px",
|
|
726
|
+
top: "-1000",
|
|
727
|
+
left: "-1000",
|
|
728
|
+
display: "block",
|
|
729
|
+
opacity: "0",
|
|
730
|
+
border: "none",
|
|
731
|
+
outline: "none",
|
|
732
|
+
});
|
|
733
|
+
container.appendChild(iframe);
|
|
734
|
+
promise.then(() => {
|
|
735
|
+
iframe.contentWindow.postMessage(
|
|
736
|
+
{
|
|
737
|
+
type: "CONNECT",
|
|
738
|
+
},
|
|
739
|
+
"*",
|
|
740
|
+
[messageChannel.port1],
|
|
838
741
|
);
|
|
839
|
-
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
break;
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
return !handler
|
|
852
|
-
? new Response(null, {
|
|
853
|
-
status: 404,
|
|
854
|
-
statusMessage: "Error 404: Not found",
|
|
855
|
-
})
|
|
856
|
-
: handler(request);
|
|
857
|
-
}
|
|
742
|
+
});
|
|
743
|
+
return {
|
|
744
|
+
port: messageChannel.port2,
|
|
745
|
+
close: () => {
|
|
746
|
+
container.parentElement && container.parentElement.removeChild(container);
|
|
747
|
+
messageChannel.port1.close();
|
|
748
|
+
messageChannel.port2.close();
|
|
749
|
+
},
|
|
750
|
+
};
|
|
858
751
|
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
752
|
+
async function newIFrame(url) {
|
|
753
|
+
const iframe = document.createElement("iframe");
|
|
754
|
+
iframe.src = url;
|
|
755
|
+
Object.assign(iframe.style, {
|
|
756
|
+
padding: 0,
|
|
757
|
+
margin: 0,
|
|
758
|
+
border: "none",
|
|
759
|
+
outline: "none",
|
|
760
|
+
width: "100%",
|
|
761
|
+
height: "100%",
|
|
762
|
+
});
|
|
870
763
|
return {
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
764
|
+
iframe,
|
|
765
|
+
promise: new Promise((resolve, reject) => {
|
|
766
|
+
try {
|
|
767
|
+
iframe.onerror = reject;
|
|
768
|
+
iframe.onload = () => resolve(iframe);
|
|
769
|
+
} catch (error) {
|
|
770
|
+
reject(error);
|
|
771
|
+
}
|
|
772
|
+
}),
|
|
876
773
|
};
|
|
877
774
|
}
|
|
878
775
|
}
|
|
776
|
+
// -------------------------------------------------------
|
|
879
777
|
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
const key = requestUrl
|
|
899
|
-
.substring(rootUrl.length)
|
|
900
|
-
.replace(/^\/?([^\/]+).*$/, "$1");
|
|
901
|
-
// console.log(`* rootUrl="${rootUrl}" requestUrl="${requestUrl}" key="${key}"`);
|
|
902
|
-
channelInfo = await this.loadChannelInfo(key);
|
|
903
|
-
}
|
|
904
|
-
if (channelInfo && channelInfo.port) {
|
|
905
|
-
return await sendHttpRequest(channelInfo.port, request);
|
|
906
|
-
} else {
|
|
907
|
-
const newRequest = new Request(requestUrl, {
|
|
908
|
-
method: request.method,
|
|
909
|
-
headers: request.headers,
|
|
910
|
-
body: request.body,
|
|
911
|
-
referrer: request.referrer,
|
|
912
|
-
referrerPolicy: request.referrerPolicy,
|
|
913
|
-
// mode: request.mode,
|
|
914
|
-
credentials: request.credentials,
|
|
915
|
-
cache: request.cache,
|
|
916
|
-
redirect: request.redirect,
|
|
917
|
-
integrity: request.integrity,
|
|
918
|
-
});
|
|
919
|
-
return await fetch(newRequest);
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
// const requestUrl = request.url;
|
|
923
|
-
// let url = requestUrl.replace(
|
|
924
|
-
// /^.*\/[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}(.*)$/,
|
|
925
|
-
// "$1",
|
|
926
|
-
// );
|
|
927
|
-
// if (url !== requestUrl) {
|
|
928
|
-
// url = new URL(
|
|
929
|
-
// "/packages/webrun-http-browser/src" + url,
|
|
930
|
-
// self.location.href,
|
|
931
|
-
// );
|
|
932
|
-
// }
|
|
933
|
-
} catch (error) {
|
|
934
|
-
console.error(error);
|
|
935
|
-
return new Response(null, {
|
|
936
|
-
status: 500,
|
|
937
|
-
statusText: "Error 500: Internal error",
|
|
938
|
-
});
|
|
939
|
-
}
|
|
940
|
-
})());
|
|
941
|
-
}
|
|
778
|
+
async function initializeConnection({
|
|
779
|
+
key,
|
|
780
|
+
communicationPort,
|
|
781
|
+
...options
|
|
782
|
+
}) {
|
|
783
|
+
const channel = new MessageChannel();
|
|
784
|
+
const accepted = await callChannel(
|
|
785
|
+
communicationPort,
|
|
786
|
+
"CONNECT",
|
|
787
|
+
{ key, ...options },
|
|
788
|
+
channel.port2,
|
|
789
|
+
);
|
|
790
|
+
if (!accepted) {
|
|
791
|
+
channel.port1.close();
|
|
792
|
+
channel.port2.close();
|
|
793
|
+
return null;
|
|
794
|
+
}
|
|
795
|
+
return channel.port1;
|
|
942
796
|
}
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
797
|
+
|
|
798
|
+
async function registerConnectionsHandler({
|
|
799
|
+
key,
|
|
800
|
+
handler,
|
|
801
|
+
communicationPort,
|
|
802
|
+
}) {
|
|
803
|
+
const [register, cleanup] = newRegistry();
|
|
804
|
+
await callChannel(communicationPort, "REGISTER", { key });
|
|
805
|
+
register(() => callChannel(communicationPort, "UNREGISTER", { key }));
|
|
806
|
+
register(
|
|
807
|
+
handleChannelCalls(
|
|
808
|
+
communicationPort,
|
|
809
|
+
"CONNECT",
|
|
810
|
+
handler,
|
|
811
|
+
),
|
|
812
|
+
);
|
|
813
|
+
return cleanup;
|
|
949
814
|
}
|
|
950
815
|
|
|
951
|
-
exports.SwHttpAdapter = SwHttpAdapter;
|
|
952
|
-
exports.SwHttpDispatcher = SwHttpDispatcher;
|
|
953
|
-
exports.SwPortDispatcher = SwPortDispatcher;
|
|
954
|
-
exports.SwPortHandler = SwPortHandler;
|
|
955
816
|
exports.callChannel = callChannel;
|
|
817
|
+
exports.callHttpService = callHttpService;
|
|
956
818
|
exports.deserializeError = deserializeError;
|
|
957
819
|
exports.fromReadableStream = fromReadableStream;
|
|
820
|
+
exports.getRelayWindowMessageHandler = getRelayWindowMessageHandler;
|
|
958
821
|
exports.handleChannelCalls = handleChannelCalls;
|
|
959
822
|
exports.handleHttpRequests = handleHttpRequests;
|
|
960
823
|
exports.handleStreams = handleStreams;
|
|
961
|
-
exports.
|
|
824
|
+
exports.initHttpService = initHttpService;
|
|
825
|
+
exports.initServiceWorker = initServiceWorker;
|
|
826
|
+
exports.initServiceWorker1 = initServiceWorker1;
|
|
827
|
+
exports.initializeConnection = initializeConnection;
|
|
962
828
|
exports.newHttpClientStub = newHttpClientStub;
|
|
963
829
|
exports.newHttpServerStub = newHttpServerStub;
|
|
964
830
|
exports.newInvokationChannel = newInvokationChannel;
|
|
965
831
|
exports.newRegistry = newRegistry;
|
|
832
|
+
exports.newRemoteRelayChannel = newRemoteRelayChannel;
|
|
833
|
+
exports.newServiceWorkerPort = newServiceWorkerPort;
|
|
966
834
|
exports.recieveData = recieveData;
|
|
835
|
+
exports.registerConnectionsHandler = registerConnectionsHandler;
|
|
967
836
|
exports.sendData = sendData;
|
|
968
837
|
exports.sendHttpRequest = sendHttpRequest;
|
|
969
838
|
exports.sendStream = sendStream;
|
|
970
839
|
exports.serializeError = serializeError;
|
|
971
|
-
exports.
|
|
840
|
+
exports.splitServiceUrl = splitServiceUrl;
|
|
972
841
|
exports.toReadableStream = toReadableStream;
|
|
973
842
|
|
|
974
843
|
}));
|