-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimjoy-rpc.js
More file actions
2234 lines (1875 loc) · 132 KB
/
imjoy-rpc.js
File metadata and controls
2234 lines (1875 loc) · 132 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define("imjoyRPC", [], factory);
else if(typeof exports === 'object')
exports["imjoyRPC"] = factory();
else
root["imjoyRPC"] = factory();
})(window, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./src/main.js");
/******/ })
/************************************************************************/
/******/ ({
/***/ "./node_modules/worker-loader/dist/workers/InlineWorker.js":
/*!*****************************************************************!*\
!*** ./node_modules/worker-loader/dist/workers/InlineWorker.js ***!
\*****************************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
// http://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string
var URL = window.URL || window.webkitURL;
module.exports = function (content, url) {
try {
try {
var blob;
try {
// BlobBuilder = Deprecated, but widely implemented
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
blob = new BlobBuilder();
blob.append(content);
blob = blob.getBlob();
} catch (e) {
// The proposed API
blob = new Blob([content]);
}
return new Worker(URL.createObjectURL(blob));
} catch (e) {
return new Worker('data:application/javascript,' + encodeURIComponent(content));
}
} catch (e) {
if (!url) {
throw Error('Inline worker is not supported');
}
return new Worker(url);
}
};
/***/ }),
/***/ "./package.json":
/*!**********************!*\
!*** ./package.json ***!
\**********************/
/*! exports provided: name, version, description, module, scripts, repository, keywords, author, license, bugs, homepage, dependencies, devDependencies, eslintConfig, default */
/***/ (function(module) {
module.exports = JSON.parse("{\"name\":\"imjoy-rpc\",\"version\":\"0.5.9\",\"description\":\"Remote procedure calls for ImJoy.\",\"module\":\"index.js\",\"scripts\":{\"build\":\"rm -rf dist && npm run build-umd\",\"build-umd\":\"webpack --config webpack.config.js --mode development && NODE_ENV=production webpack --config webpack.config.js --mode production --devtool source-map \",\"watch\":\"NODE_ENV=production webpack --watch --progress --config webpack.config.js --mode production --devtool source-map\",\"publish-npm\":\"npm install && npm run build && npm publish\",\"serve\":\"webpack-dev-server\",\"stats\":\"webpack --profile --json > stats.json\",\"stats-prod\":\"webpack --profile --json --mode production > stats-prod.json\",\"analyze\":\"webpack-bundle-analyzer -p 9999 stats.json\",\"analyze-prod\":\"webpack-bundle-analyzer -p 9999 stats-prod.json\",\"clean\":\"rimraf dist/*\",\"deploy\":\"npm run build && node deploy-site.js\",\"format\":\"prettier --write \\\"{src,tests}/**/**\\\"\",\"check-format\":\"prettier --check \\\"{src,tests}/**/**\\\"\",\"test\":\"karma start --single-run --browsers ChromeHeadless,FirefoxHeadless karma.conf.js\",\"test-watch\":\"karma start --auto-watch --browsers ChromeDebugging karma.conf.js --debug\"},\"repository\":{\"type\":\"git\",\"url\":\"git+https://github.com/imjoy-team/imjoy-rpc.git\"},\"keywords\":[\"imjoy\",\"rpc\"],\"author\":\"imjoy-team <imjoy.team@gmail.com>\",\"license\":\"MIT\",\"bugs\":{\"url\":\"https://github.com/imjoy-team/imjoy-rpc/issues\"},\"homepage\":\"https://github.com/imjoy-team/imjoy-rpc\",\"dependencies\":{\"@msgpack/msgpack\":\"^2.7.1\",\"socket.io-client\":\"^4.4.1\"},\"devDependencies\":{\"@babel/core\":\"^7.16.12\",\"@babel/plugin-syntax-dynamic-import\":\"^7.8.3\",\"@babel/polyfill\":\"^7.12.1\",\"@babel/preset-env\":\"^7.16.11\",\"@types/requirejs\":\"^2.1.34\",\"babel-core\":\"^6.26.0\",\"babel-eslint\":\"^10.1.0\",\"babel-loader\":\"^8.2.3\",\"babel-runtime\":\"^6.26.0\",\"chai\":\"^4.3.6\",\"clean-webpack-plugin\":\"^0.1.19\",\"copy-webpack-plugin\":\"^5.1.2\",\"eslint\":\"^6.8.0\",\"eslint-config-prettier\":\"^4.2.0\",\"eslint-loader\":\"^4.0.2\",\"file-loader\":\"^0.11.2\",\"fs-extra\":\"^0.30.0\",\"gh-pages\":\"^2.0.1\",\"html-loader\":\"^0.5.5\",\"html-webpack-plugin\":\"^3.2.0\",\"json-loader\":\"^0.5.4\",\"karma\":\"^6.3.12\",\"karma-chrome-launcher\":\"^3.1.0\",\"karma-firefox-launcher\":\"^1.3.0\",\"karma-mocha\":\"^1.3.0\",\"karma-sourcemap-loader\":\"^0.3.8\",\"karma-spec-reporter\":\"0.0.32\",\"karma-webpack\":\"^4.0.2\",\"lerna\":\"^3.22.1\",\"lodash.debounce\":\"^4.0.8\",\"mocha\":\"^7.2.0\",\"postcss\":\"^7.0.36\",\"prettier\":\"^1.6.1\",\"rimraf\":\"^2.6.2\",\"schema-utils\":\"^0.4.3\",\"style-loader\":\"^0.18.1\",\"url-loader\":\"^0.5.9\",\"webpack\":\"^4.46.0\",\"webpack-bundle-analyzer\":\"^3.9.0\",\"webpack-cli\":\"^3.3.12\",\"webpack-dev-server\":\"^3.11.3\",\"webpack-merge\":\"^4.1.1\",\"workbox-webpack-plugin\":\"^4.3.1\",\"worker-loader\":\"^2.0.0\",\"write-file-webpack-plugin\":\"^4.5.1\"},\"eslintConfig\":{\"globals\":{\"document\":true,\"window\":true}}}");
/***/ }),
/***/ "./src/main.js":
/*!*********************!*\
!*** ./src/main.js ***!
\*********************/
/*! exports provided: RPC, API_VERSION, VERSION, loadRequirements, waitForInitialization, setupRPC */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "waitForInitialization", function() { return waitForInitialization; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "setupRPC", function() { return setupRPC; });
/* harmony import */ var _plugin_webworker_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./plugin.webworker.js */ "./src/plugin.webworker.js");
/* harmony import */ var _plugin_webworker_js__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_plugin_webworker_js__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _pluginIframe_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./pluginIframe.js */ "./src/pluginIframe.js");
/* harmony import */ var _utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils.js */ "./src/utils.js");
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "loadRequirements", function() { return _utils_js__WEBPACK_IMPORTED_MODULE_2__["loadRequirements"]; });
/* harmony import */ var _rpc_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./rpc.js */ "./src/rpc.js");
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "RPC", function() { return _rpc_js__WEBPACK_IMPORTED_MODULE_3__["RPC"]; });
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "API_VERSION", function() { return _rpc_js__WEBPACK_IMPORTED_MODULE_3__["API_VERSION"]; });
/* harmony import */ var _package_json__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../package.json */ "./package.json");
var _package_json__WEBPACK_IMPORTED_MODULE_4___namespace = /*#__PURE__*/__webpack_require__.t(/*! ../package.json */ "./package.json", 1);
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "VERSION", function() { return _package_json__WEBPACK_IMPORTED_MODULE_4__["version"]; });
/**
* Contains the code executed in the sandboxed frame under web-browser
*
* Tries to create a Web-Worker inside the frame and set up the
* communication between the worker and the parent window. Some
* browsers restrict creating a worker inside a sandboxed iframe - if
* this happens, the plugin initialized right inside the frame (in the
* same thread)
*/
function _inIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
function _inWebWorker() {
return typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope;
}
/**
* Initializes the plugin inside a web worker. May throw an exception
* in case this was not permitted by the browser.
*/
function setupWebWorker(config) {
if (!config.allow_execution) throw new Error("web-worker plugin can only work with allow_execution=true");
let broadcastChannel = null;
if (config.broadcastChannel) {
broadcastChannel = new BroadcastChannel(config.broadcastChannel);
}
const worker = new _plugin_webworker_js__WEBPACK_IMPORTED_MODULE_0___default.a(); // mixed content warning in Chrome silently skips worker
// initialization without exception, handling this with timeout
const fallbackTimeout = setTimeout(function () {
worker.terminate();
console.warn(`Plugin failed to start as a web-worker, running in an iframe instead.`);
Object(_pluginIframe_js__WEBPACK_IMPORTED_MODULE_1__["default"])(config);
}, 2000);
const peer_id = Object(_utils_js__WEBPACK_IMPORTED_MODULE_2__["randId"])(); // forwarding messages between the worker and parent window
worker.addEventListener("message", function (e) {
let transferables = undefined;
const m = e.data;
if (m.type === "worker-ready") {
// send config to the worker
worker.postMessage({
type: "connectRPC",
config: config
});
clearTimeout(fallbackTimeout);
return;
} else if (m.type === "initialized") {
// complete the missing fields
m.config = Object.assign({}, config, m.config);
m.origin = window.location.origin;
m.peer_id = peer_id;
} else if (m.type === "imjoy_remote_api_ready") {
// if it's a webworker, there will be no api object returned
window.dispatchEvent(new CustomEvent("imjoy_remote_api_ready", {
detail: null
}));
} else if (m.type === "cacheRequirements" && typeof cache_requirements === "function") {
cache_requirements(m.requirements);
} else if (m.type === "disconnect") {
worker.terminate();
} else {
if (m.__transferables__) {
transferables = m.__transferables__;
delete m.__transferables__;
}
}
if (broadcastChannel) broadcastChannel.postMessage(m);else parent.postMessage(m, config.target_origin || "*", transferables);
});
(broadcastChannel || window).addEventListener("message", function (e) {
if (e.type === "message" && (broadcastChannel || config.target_origin === "*" || e.origin === config.target_origin)) {
let transferables = undefined;
const m = e.data;
if (m.__transferables__) {
transferables = m.__transferables__;
delete m.__transferables__;
}
if (m.peer_id === peer_id) {
worker.postMessage(m, transferables);
} else if (config.debug) {
console.log(`connection peer id mismatch ${m.peer_id} !== ${peer_id}`);
}
}
});
}
function waitForInitialization(config) {
if (_inWebWorker()) {
globalThis.parent = self;
}
config = config || {};
if (config.enable_service_worker) {
Object(_utils_js__WEBPACK_IMPORTED_MODULE_2__["setupServiceWorker"])(config.base_url, config.target_origin, config.cache_requirements);
config.enable_service_worker = false;
}
if (config.cache_requirements) {
delete config.cache_requirements;
}
const targetOrigin = config.target_origin || "*";
if (config.credential_required && typeof config.verify_credential !== "function") {
throw new Error("Please also provide the `verify_credential` function with `credential_required`.");
}
if (config.credential_required && targetOrigin === "*") {
throw new Error("`target_origin` was set to `*` with `credential_required=true`, there is a security risk that you may leak the credential to website from other origin. Please specify the `target_origin` explicitly.");
}
const done = () => {
globalThis.removeEventListener("message", handleEvent);
};
const peer_id = Object(_utils_js__WEBPACK_IMPORTED_MODULE_2__["randId"])();
const handleEvent = e => {
if (e.type === "message" && (!e.origin || targetOrigin === "*" || e.origin === targetOrigin)) {
if (e.data.type === "initialize") {
done();
if (e.data.peer_id !== peer_id) {
// TODO: throw an error when we are sure all the peers will send the peer_id
console.warn(`${e.data.config && e.data.config.name}: connection peer id mismatch ${e.data.peer_id} !== ${peer_id}`);
}
const cfg = e.data.config; // override the target_origin setting if it's configured by the rpc client
// otherwise take the setting from the core
if (targetOrigin !== "*") {
cfg.target_origin = targetOrigin;
}
if (config.credential_required) {
config.verify_credential(cfg.credential).then(result => {
if (result && result.auth && !result.error) {
// pass the authentication information with tokens
cfg.auth = result.auth;
setupRPC(cfg).then(() => {
console.log("ImJoy RPC loaded successfully!");
});
} else {
throw new Error("Failed to verify the credentail:" + (result && result.error));
}
});
} else {
setupRPC(cfg).then(() => {
console.log("ImJoy RPC loaded successfully!");
});
}
} else {
throw new Error(`unrecognized message: ${e.data}`);
}
}
};
globalThis.addEventListener("message", handleEvent);
if (_inWebWorker()) {
parent.postMessage({
type: "imjoyRPCReady",
config: config,
peer_id: peer_id
});
} else {
parent.postMessage({
type: "imjoyRPCReady",
config: config,
peer_id: peer_id
}, "*");
}
}
function setupRPC(config) {
config = config || {};
config.name = config.name || Object(_utils_js__WEBPACK_IMPORTED_MODULE_2__["randId"])();
config = Object(_utils_js__WEBPACK_IMPORTED_MODULE_2__["normalizeConfig"])(config);
if (config.enable_service_worker) {
Object(_utils_js__WEBPACK_IMPORTED_MODULE_2__["setupServiceWorker"])(config.base_url, config.target_origin, config.cache_requirements);
}
if (config.cache_requirements) {
delete config.cache_requirements;
}
return new Promise((resolve, reject) => {
const handleEvent = e => {
const api = e.detail;
if (config.expose_api_globally) {
globalThis.api = api;
} // imjoy plugin api
resolve(api);
globalThis.removeEventListener("imjoy_remote_api_ready", handleEvent);
};
if (_inIframe()) {
if (config.type === "web-worker") {
try {
setupWebWorker(config);
} catch (e) {
// fallback to iframe
Object(_pluginIframe_js__WEBPACK_IMPORTED_MODULE_1__["default"])(config);
}
} else if (["rpc-window", "rpc-worker", "iframe", "window"].includes(config.type)) {
Object(_pluginIframe_js__WEBPACK_IMPORTED_MODULE_1__["default"])(config);
} else {
console.error("Unsupported plugin type: " + config.type);
reject("Unsupported plugin type: " + config.type);
return;
}
globalThis.addEventListener("imjoy_remote_api_ready", handleEvent);
} else if (_inWebWorker()) {
// inside a webworker
Object(_pluginIframe_js__WEBPACK_IMPORTED_MODULE_1__["default"])(config);
} else {
reject(new Error("imjoy-rpc should only run inside an iframe or a webworker."));
}
});
}
/***/ }),
/***/ "./src/plugin.webworker.js":
/*!*********************************!*\
!*** ./src/plugin.webworker.js ***!
\*********************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
module.exports = function() {
return __webpack_require__(/*! !./node_modules/worker-loader/dist/workers/InlineWorker.js */ "./node_modules/worker-loader/dist/workers/InlineWorker.js")("/******/ (function(modules) { // webpackBootstrap\n/******/ \t// The module cache\n/******/ \tvar installedModules = {};\n/******/\n/******/ \t// The require function\n/******/ \tfunction __webpack_require__(moduleId) {\n/******/\n/******/ \t\t// Check if module is in cache\n/******/ \t\tif(installedModules[moduleId]) {\n/******/ \t\t\treturn installedModules[moduleId].exports;\n/******/ \t\t}\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\ti: moduleId,\n/******/ \t\t\tl: false,\n/******/ \t\t\texports: {}\n/******/ \t\t};\n/******/\n/******/ \t\t// Execute the module function\n/******/ \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n/******/\n/******/ \t\t// Flag the module as loaded\n/******/ \t\tmodule.l = true;\n/******/\n/******/ \t\t// Return the exports of the module\n/******/ \t\treturn module.exports;\n/******/ \t}\n/******/\n/******/\n/******/ \t// expose the modules object (__webpack_modules__)\n/******/ \t__webpack_require__.m = modules;\n/******/\n/******/ \t// expose the module cache\n/******/ \t__webpack_require__.c = installedModules;\n/******/\n/******/ \t// define getter function for harmony exports\n/******/ \t__webpack_require__.d = function(exports, name, getter) {\n/******/ \t\tif(!__webpack_require__.o(exports, name)) {\n/******/ \t\t\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\n/******/ \t\t}\n/******/ \t};\n/******/\n/******/ \t// define __esModule on exports\n/******/ \t__webpack_require__.r = function(exports) {\n/******/ \t\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n/******/ \t\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n/******/ \t\t}\n/******/ \t\tObject.defineProperty(exports, '__esModule', { value: true });\n/******/ \t};\n/******/\n/******/ \t// create a fake namespace object\n/******/ \t// mode & 1: value is a module id, require it\n/******/ \t// mode & 2: merge all properties of value into the ns\n/******/ \t// mode & 4: return value when already ns object\n/******/ \t// mode & 8|1: behave like require\n/******/ \t__webpack_require__.t = function(value, mode) {\n/******/ \t\tif(mode & 1) value = __webpack_require__(value);\n/******/ \t\tif(mode & 8) return value;\n/******/ \t\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\n/******/ \t\tvar ns = Object.create(null);\n/******/ \t\t__webpack_require__.r(ns);\n/******/ \t\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\n/******/ \t\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\n/******/ \t\treturn ns;\n/******/ \t};\n/******/\n/******/ \t// getDefaultExport function for compatibility with non-harmony modules\n/******/ \t__webpack_require__.n = function(module) {\n/******/ \t\tvar getter = module && module.__esModule ?\n/******/ \t\t\tfunction getDefault() { return module['default']; } :\n/******/ \t\t\tfunction getModuleExports() { return module; };\n/******/ \t\t__webpack_require__.d(getter, 'a', getter);\n/******/ \t\treturn getter;\n/******/ \t};\n/******/\n/******/ \t// Object.prototype.hasOwnProperty.call\n/******/ \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(__webpack_require__.s = \"./src/plugin.webworker.js\");\n/******/ })\n/************************************************************************/\n/******/ ({\n\n/***/ \"./src/plugin.webworker.js\":\n/*!*********************************!*\\\n !*** ./src/plugin.webworker.js ***!\n \\*********************************/\n/*! no exports provided */\n/***/ (function(module, __webpack_exports__, __webpack_require__) {\n\n\"use strict\";\n__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _pluginCore_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./pluginCore.js */ \"./src/pluginCore.js\");\n/* harmony import */ var _rpc_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./rpc.js */ \"./src/rpc.js\");\n/* harmony import */ var _utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils.js */ \"./src/utils.js\");\n/**\n * Contains the routines loaded by the plugin Worker under web-browser.\n *\n * Initializes the web environment version of the platform-dependent\n * connection object for the plugin site\n */\n\n\n\n\n// make sure this runs inside a webworker\nif (\n typeof WorkerGlobalScope === \"undefined\" ||\n !self ||\n !(self instanceof WorkerGlobalScope)\n) {\n throw new Error(\"This script can only loaded in a webworker\");\n}\n\nasync function executeEsModule(content) {\n const dataUri =\n \"data:text/javascript;charset=utf-8,\" + encodeURIComponent(content);\n await import(/* webpackIgnore: true */ dataUri);\n}\n\n/**\n * Connection object provided to the RPC constructor,\n * plugin site implementation for the web-based environment.\n * Global will be then cleared to prevent exposure into the\n * Worker, so we put this local connection object into a closure\n */\nclass Connection extends _utils_js__WEBPACK_IMPORTED_MODULE_2__[\"MessageEmitter\"] {\n constructor(config) {\n super(config && config.debug);\n this.config = config || {};\n }\n connect() {\n self.addEventListener(\"message\", e => {\n this._fire(e.data.type, e.data);\n });\n this.emit({\n type: \"initialized\",\n config: this.config\n });\n }\n disconnect() {\n this._fire(\"beforeDisconnect\");\n self.close();\n this._fire(\"disconnected\");\n }\n emit(data) {\n let transferables = undefined;\n if (data.__transferables__) {\n transferables = data.__transferables__;\n delete data.__transferables__;\n }\n self.postMessage(data, transferables);\n }\n async execute(code) {\n if (code.type === \"requirements\") {\n await Object(_utils_js__WEBPACK_IMPORTED_MODULE_2__[\"loadRequirementsInWebworker\"])(code.requirements);\n } else if (code.type === \"script\") {\n try {\n if (code.attrs.type === \"module\") {\n await executeEsModule(code.content);\n } else {\n eval(code.content);\n }\n } catch (e) {\n console.error(e.message, e.stack);\n throw e;\n }\n } else {\n throw \"unsupported code type.\";\n }\n if (code.type === \"requirements\") {\n self.postMessage({\n type: \"cacheRequirements\",\n requirements: code.requirements\n });\n }\n }\n}\nconst config = {\n type: \"web-worker\",\n dedicated_thread: true,\n allow_execution: true,\n lang: \"javascript\",\n api_version: _rpc_js__WEBPACK_IMPORTED_MODULE_1__[\"API_VERSION\"]\n};\nconst conn = new Connection(config);\nconn.on(\"connectRPC\", data => {\n Object(_pluginCore_js__WEBPACK_IMPORTED_MODULE_0__[\"connectRPC\"])(conn, Object.assign(data.config, config));\n});\nconn.connect();\nself.postMessage({\n type: \"worker-ready\"\n});\n\n\n/***/ }),\n\n/***/ \"./src/pluginCore.js\":\n/*!***************************!*\\\n !*** ./src/pluginCore.js ***!\n \\***************************/\n/*! exports provided: connectRPC */\n/***/ (function(module, __webpack_exports__, __webpack_require__) {\n\n\"use strict\";\n__webpack_require__.r(__webpack_exports__);\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"connectRPC\", function() { return connectRPC; });\n/* harmony import */ var _rpc_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./rpc.js */ \"./src/rpc.js\");\n/**\n * Core plugin script loaded into the plugin process/thread.\n *\n * Initializes the plugin-site API global methods.\n */\n\nfunction connectRPC(connection, config) {\n config = config || {};\n const codecs = {};\n const rpc = new _rpc_js__WEBPACK_IMPORTED_MODULE_0__[\"RPC\"](connection, config, codecs);\n rpc.on(\"getInterface\", function () {\n launchConnected();\n });\n rpc.on(\"remoteReady\", function () {\n const api = rpc.getRemote() || {};\n\n api.registerCodec = function (config) {\n if (!config[\"name\"] || !config[\"encoder\"] && !config[\"decoder\"]) {\n throw new Error(\"Invalid codec format, please make sure you provide a name, type, encoder and decoder.\");\n } else {\n if (config.type) {\n for (let k of Object.keys(codecs)) {\n if (codecs[k].type === config.type || k === config.name) {\n delete codecs[k];\n console.warn(\"Remove duplicated codec: \" + k);\n }\n }\n }\n\n codecs[config[\"name\"]] = config;\n }\n };\n\n api.init = function (config) {\n // register a minimal plugin api\n rpc.setInterface({\n setup() {}\n\n }, config);\n };\n\n api.disposeObject = function (obj) {\n rpc.disposeObject(obj);\n };\n\n api.export = function (_interface, config) {\n rpc.setInterface(_interface, config);\n };\n\n api.onLoad = function (handler) {\n handler = checkHandler(handler);\n\n if (connected) {\n handler();\n } else {\n connectedHandlers.push(handler);\n }\n };\n\n api.dispose = function (_interface) {\n rpc.disconnect();\n };\n\n api._rpc = rpc;\n\n if (typeof WorkerGlobalScope !== \"undefined\" && self instanceof WorkerGlobalScope) {\n self.api = api;\n self.postMessage({\n type: \"imjoy_remote_api_ready\"\n });\n self.dispatchEvent(new CustomEvent(\"imjoy_remote_api_ready\", {\n detail: api\n }));\n } else if (typeof window) {\n window.dispatchEvent(new CustomEvent(\"imjoy_remote_api_ready\", {\n detail: api\n }));\n }\n });\n let connected = false;\n const connectedHandlers = [];\n\n const launchConnected = function () {\n if (!connected) {\n connected = true;\n let handler;\n\n while (handler = connectedHandlers.pop()) {\n handler();\n }\n }\n };\n\n const checkHandler = function (handler) {\n const type = typeof handler;\n\n if (type !== \"function\") {\n const msg = \"A function may only be subsribed to the event, \" + type + \" was provided instead\";\n throw new Error(msg);\n }\n\n return handler;\n };\n\n return rpc;\n}\n\n/***/ }),\n\n/***/ \"./src/rpc.js\":\n/*!********************!*\\\n !*** ./src/rpc.js ***!\n \\********************/\n/*! exports provided: API_VERSION, RPC */\n/***/ (function(module, __webpack_exports__, __webpack_require__) {\n\n\"use strict\";\n__webpack_require__.r(__webpack_exports__);\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"API_VERSION\", function() { return API_VERSION; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"RPC\", function() { return RPC; });\n/* harmony import */ var _utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils.js */ \"./src/utils.js\");\n/**\n * Contains the RPC object used both by the application\n * site, and by each plugin\n */\n\nconst API_VERSION = \"0.2.3\";\nconst ArrayBufferView = Object.getPrototypeOf(Object.getPrototypeOf(new Uint8Array())).constructor;\n\nfunction _appendBuffer(buffer1, buffer2) {\n const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);\n tmp.set(new Uint8Array(buffer1), 0);\n tmp.set(new Uint8Array(buffer2), buffer1.byteLength);\n return tmp.buffer;\n}\n\nfunction indexObject(obj, is) {\n if (!is) throw new Error(\"undefined index\");\n if (typeof is === \"string\") return indexObject(obj, is.split(\".\"));else if (is.length === 0) return obj;else return indexObject(obj[is[0]], is.slice(1));\n}\n/**\n * RPC object represents a single site in the\n * communication protocol between the application and the plugin\n *\n * @param {Object} connection a special object allowing to send\n * and receive messages from the opposite site (basically it\n * should only provide send() and onMessage() methods)\n */\n\n\nclass RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__[\"MessageEmitter\"] {\n constructor(connection, config, codecs) {\n super(config && config.debug);\n this._connection = connection;\n this.config = config || {};\n this._codecs = codecs || {};\n this._object_store = {};\n this._method_weakmap = new WeakMap();\n this._object_weakmap = new WeakMap();\n this._local_api = null;\n this._remote_set = false; // make sure there is an execute function\n\n const name = this.config.name;\n\n this._connection.execute = this._connection.execute || function () {\n throw new Error(`connection.execute not implemented (in \"${name}\")`);\n };\n\n this._store = new ReferenceStore();\n this._method_refs = new ReferenceStore();\n\n this._method_refs.onReady(() => {\n this._fire(\"remoteIdle\");\n });\n\n this._method_refs.onBusy(() => {\n this._fire(\"remoteBusy\");\n });\n\n this._setupMessageHanlders();\n }\n\n init() {\n this._connection.emit({\n type: \"initialized\",\n config: this.config,\n peer_id: this._connection.peer_id\n });\n }\n\n setConfig(config) {\n if (config) for (const k of Object.keys(config)) {\n this.config[k] = config[k];\n }\n }\n /**\n * Set a handler to be called when received a responce from the\n * remote site reporting that the previously provided interface\n * has been successfully set as remote for that site\n *\n * @param {Function} handler\n */\n\n\n getRemoteCallStack() {\n return this._method_refs.getStack();\n }\n /**\n * @returns {Object} set of remote interface methods\n */\n\n\n getRemote() {\n return this._remote_interface;\n }\n /**\n * Sets the interface of this site making it available to the\n * remote site by sending a message with a set of methods names\n *\n * @param {Object} _interface to set\n */\n\n\n setInterface(_interface, config) {\n config = config || {};\n this.config.name = config.name || this.config.name;\n this.config.description = config.description || this.config.description;\n\n if (this.config.forwarding_functions) {\n for (let func_name of this.config.forwarding_functions) {\n const _remote = this._remote_interface;\n\n if (_remote[func_name]) {\n if (_interface.constructor === Object) {\n if (!_interface[func_name]) {\n _interface[func_name] = (...args) => {\n _remote[func_name](...args);\n };\n }\n } else if (_interface.constructor.constructor === Function) {\n if (!_interface.constructor.prototype[func_name]) {\n _interface.constructor.prototype[func_name] = (...args) => {\n _remote[func_name](...args);\n };\n }\n }\n }\n }\n }\n\n this._local_api = _interface;\n if (!this._remote_set) this._fire(\"interfaceAvailable\");else this.sendInterface();\n return new Promise(resolve => {\n this.once(\"interfaceSetAsRemote\", resolve);\n });\n }\n /**\n * Sends the actual interface to the remote site upon it was\n * updated or by a special request of the remote site\n */\n\n\n sendInterface() {\n if (!this._local_api) {\n throw new Error(\"interface is not set.\");\n }\n\n this._encode(this._local_api, true).then(api => {\n this._connection.emit({\n type: \"setInterface\",\n api: api\n });\n });\n }\n\n _disposeObject(objectId) {\n if (this._object_store[objectId]) {\n delete this._object_store[objectId];\n } else {\n throw new Error(`Object (id=${objectId}) not found.`);\n }\n }\n\n disposeObject(obj) {\n return new Promise((resolve, reject) => {\n if (this._object_weakmap.has(obj)) {\n const objectId = this._object_weakmap.get(obj);\n\n this._connection.once(\"disposed\", data => {\n if (data.error) reject(new Error(data.error));else resolve();\n });\n\n this._connection.emit({\n type: \"disposeObject\",\n object_id: objectId\n });\n } else {\n throw new Error(\"Invalid object\");\n }\n });\n }\n /**\n * Handles a message from the remote site\n */\n\n\n _setupMessageHanlders() {\n this._connection.on(\"init\", this.init);\n\n this._connection.on(\"execute\", data => {\n Promise.resolve(this._connection.execute(data.code)).then(() => {\n this._connection.emit({\n type: \"executed\"\n });\n }).catch(e => {\n console.error(e);\n\n this._connection.emit({\n type: \"executed\",\n error: String(e)\n });\n });\n });\n\n this._connection.on(\"method\", async data => {\n let resolve, reject, method, method_this, args, result;\n\n try {\n if (data.promise) {\n [resolve, reject] = await this._unwrap(data.promise, false);\n }\n\n const _interface = this._object_store[data.object_id];\n method = indexObject(_interface, data.name);\n\n if (data.name.includes(\".\")) {\n const tmp = data.name.split(\".\");\n const intf_index = tmp.slice(0, tmp.length - 1).join(\".\");\n method_this = indexObject(_interface, intf_index);\n } else {\n method_this = _interface;\n }\n\n args = await this._unwrap(data.args, true);\n\n if (data.promise) {\n result = method.apply(method_this, args);\n\n if (result instanceof Promise || method.constructor && method.constructor.name === \"AsyncFunction\") {\n result.then(resolve).catch(reject);\n } else {\n resolve(result);\n }\n } else {\n method.apply(method_this, args);\n }\n } catch (err) {\n console.error(this.config.name, err);\n\n if (reject) {\n reject(err);\n }\n }\n });\n\n this._connection.on(\"callback\", async data => {\n let resolve, reject, method, args, result;\n\n try {\n if (data.promise) {\n [resolve, reject] = await this._unwrap(data.promise, false);\n }\n\n if (data.promise) {\n method = this._store.fetch(data.id);\n args = await this._unwrap(data.args, true);\n\n if (!method) {\n throw new Error(\"Callback function can only called once, if you want to call a function for multiple times, please make it as a plugin api function. See https://imjoy.io/docs for more details.\");\n }\n\n result = method.apply(null, args);\n\n if (result instanceof Promise || method.constructor && method.constructor.name === \"AsyncFunction\") {\n result.then(resolve).catch(reject);\n } else {\n resolve(result);\n }\n } else {\n method = this._store.fetch(data.id);\n args = await this._unwrap(data.args, true);\n\n if (!method) {\n throw new Error(\"Please notice that callback function can only called once, if you want to call a function for multiple times, please make it as a plugin api function. See https://imjoy.io/docs for more details.\");\n }\n\n method.apply(null, args);\n }\n } catch (err) {\n console.error(this.config.name, err);\n\n if (reject) {\n reject(err);\n }\n }\n });\n\n this._connection.on(\"disposeObject\", data => {\n try {\n this._disposeObject(data.object_id);\n\n this._connection.emit({\n type: \"disposed\"\n });\n } catch (e) {\n console.error(e);\n\n this._connection.emit({\n type: \"disposed\",\n error: String(e)\n });\n }\n });\n\n this._connection.on(\"setInterface\", data => {\n this._setRemoteInterface(data.api);\n });\n\n this._connection.on(\"getInterface\", () => {\n this._fire(\"getInterface\");\n\n if (this._local_api) {\n this.sendInterface();\n } else {\n this.once(\"interfaceAvailable\", () => {\n this.sendInterface();\n });\n }\n });\n\n this._connection.on(\"interfaceSetAsRemote\", () => {\n this._remote_set = true;\n\n this._fire(\"interfaceSetAsRemote\");\n });\n\n this._connection.on(\"disconnect\", () => {\n this._fire(\"beforeDisconnect\");\n\n this._connection.disconnect();\n\n this._fire(\"disconnected\");\n });\n }\n /**\n * Sends a requests to the remote site asking it to provide its\n * current interface\n */\n\n\n requestRemote() {\n this._connection.emit({\n type: \"getInterface\"\n });\n }\n\n _ndarray(typedArray, shape, dtype) {\n const _dtype = Object(_utils_js__WEBPACK_IMPORTED_MODULE_0__[\"typedArrayToDtype\"])(typedArray);\n\n if (dtype && dtype !== _dtype) {\n throw \"dtype doesn't match the type of the array: \" + _dtype + \" != \" + dtype;\n }\n\n shape = shape || [typedArray.length];\n return {\n _rtype: \"ndarray\",\n _rvalue: typedArray.buffer,\n _rshape: shape,\n _rdtype: _dtype\n };\n }\n /**\n * Sets the new remote interface provided by the other site\n *\n * @param {Array} names list of function names\n */\n\n\n _setRemoteInterface(api) {\n this._decode(api).then(intf => {\n // update existing interface instead of recreating it\n // this will preserve the object reference\n if (this._remote_interface) {\n // clear the interface\n for (let k in this._remote_interface) delete this._remote_interface[k]; // then assign the new interfaces\n\n\n Object.assign(this._remote_interface, intf);\n } else this._remote_interface = intf;\n\n this._fire(\"remoteReady\");\n\n this._reportRemoteSet();\n });\n }\n /**\n * Generates the wrapped function corresponding to a single remote\n * method. When the generated function is called, it will send the\n * corresponding message to the remote site asking it to execute\n * the particular method of its interface\n *\n * @param {String} name of the remote method\n *\n * @returns {Function} wrapped remote method\n */\n\n\n _genRemoteMethod(targetId, name, objectId) {\n const me = this;\n\n const remoteMethod = function () {\n return new Promise(async (resolve, reject) => {\n let id = null;\n\n try {\n id = me._method_refs.put(objectId ? objectId + \"/\" + name : name);\n\n const wrapped_resolve = function () {\n if (id !== null) me._method_refs.fetch(id);\n return resolve.apply(this, arguments);\n };\n\n const wrapped_reject = function () {\n if (id !== null) me._method_refs.fetch(id);\n return reject.apply(this, arguments);\n };\n\n const encodedPromise = await me._wrap([wrapped_resolve, wrapped_reject]); // store the key id for removing them from the reference store together\n\n wrapped_resolve.__promise_pair = encodedPromise[1]._rvalue;\n wrapped_reject.__promise_pair = encodedPromise[0]._rvalue;\n let args = Array.prototype.slice.call(arguments);\n const argLength = args.length; // if the last argument is an object, mark it as kwargs\n\n const withKwargs = argLength > 0 && typeof args[argLength - 1] === \"object\" && args[argLength - 1] !== null && args[argLength - 1]._rkwargs;\n if (withKwargs) delete args[argLength - 1]._rkwargs;\n\n if (name === \"register\" || name === \"registerService\" || name === \"register_service\" || name === \"export\" || name === \"on\") {\n args = await me._wrap(args, true);\n } else {\n args = await me._wrap(args);\n }\n\n const transferables = args.__transferables__;\n if (transferables) delete args.__transferables__;\n\n me._connection.emit({\n type: \"method\",\n target_id: targetId,\n name: name,\n object_id: objectId,\n args: args,\n promise: encodedPromise,\n with_kwargs: withKwargs\n }, transferables);\n } catch (e) {\n if (id) me._method_refs.fetch(id);\n reject(`Failed to exectue remote method (interface: ${objectId || me.id}, method: ${name}), error: ${e}`);\n }\n });\n };\n\n remoteMethod.__remote_method = true;\n return remoteMethod;\n }\n /**\n * Sends a responce reporting that interface just provided by the\n * remote site was successfully set by this site as remote\n */\n\n\n _reportRemoteSet() {\n this._connection.emit({\n type: \"interfaceSetAsRemote\"\n });\n }\n /**\n * Prepares the provided set of remote method arguments for\n * sending to the remote site, replaces all the callbacks with\n * identifiers\n *\n * @param {Array} args to wrap\n *\n * @returns {Array} wrapped arguments\n */\n\n\n async _encode(aObject, asInterface, objectId) {\n const aType = typeof aObject;\n\n if (aType === \"number\" || aType === \"string\" || aType === \"boolean\" || aObject === null || aObject === undefined || aObject instanceof ArrayBuffer) {\n return aObject;\n }\n\n let bObject;\n\n if (typeof aObject === \"function\") {\n if (asInterface) {\n if (!objectId) throw new Error(\"objectId is not specified.\");\n bObject = {\n _rtype: \"interface\",\n _rtarget_id: this._connection.peer_id,\n _rintf: objectId,\n _rvalue: asInterface\n };\n\n this._method_weakmap.set(aObject, bObject);\n } else if (this._method_weakmap.has(aObject)) {\n bObject = this._method_weakmap.get(aObject);\n } else {\n const cid = this._store.put(aObject);\n\n bObject = {\n _rtype: \"callback\",\n _rtarget_id: this._connection.peer_id,\n _rname: aObject.constructor && aObject.constructor.name || cid,\n _rvalue: cid\n };\n }\n\n return bObject;\n } // skip if already encoded\n\n\n if (aObject.constructor instanceof Object && aObject._rtype) {\n // make sure the interface functions are encoded\n if (aObject._rintf) {\n const temp = aObject._rtype;\n delete aObject._rtype;\n bObject = await this._encode(aObject, asInterface, objectId);\n bObject._rtype = temp;\n } else {\n bObject = aObject;\n }\n\n return bObject;\n }\n\n const transferables = [];\n const _transfer = aObject._transfer;\n const isarray = Array.isArray(aObject);\n\n for (let tp of Object.keys(this._codecs)) {\n const codec = this._codecs[tp];\n\n if (codec.encoder && aObject instanceof codec.type) {\n // TODO: what if multiple encoders found\n let encodedObj = await Promise.resolve(codec.encoder(aObject));\n if (encodedObj && !encodedObj._rtype) encodedObj._rtype = codec.name; // encode the functions in the interface object\n\n if (encodedObj && encodedObj._rintf) {\n const temp = encodedObj._rtype;\n delete encodedObj._rtype;\n encodedObj = await this._encode(encodedObj, asInterface, objectId);\n encodedObj._rtype = temp;\n }\n\n bObject = encodedObj;\n return bObject;\n }\n }\n\n if (\n /*global tf*/\n typeof tf !== \"undefined\" && tf.Tensor && aObject instanceof tf.Tensor) {\n const v_buffer = aObject.dataSync();\n\n if (aObject._transfer || _transfer) {\n transferables.push(v_buffer.buffer);\n delete aObject._transfer;\n }\n\n bObject = {\n _rtype: \"ndarray\",\n _rvalue: v_buffer.buffer,\n _rshape: aObject.shape,\n _rdtype: aObject.dtype\n };\n } else if (\n /*global nj*/\n typeof nj !== \"undefined\" && nj.NdArray && aObject instanceof nj.NdArray) {\n const dtype = Object(_utils_js__WEBPACK_IMPORTED_MODULE_0__[\"typedArrayToDtype\"])(aObject.selection.data);\n\n if (aObject._transfer || _transfer) {\n transferables.push(aObject.selection.data.buffer);\n delete aObject._transfer;\n }\n\n bObject = {\n _rtype: \"ndarray\",\n _rvalue: aObject.selection.data.buffer,\n _rshape: aObject.shape,\n _rdtype: dtype\n };\n } else if (aObject instanceof Error) {\n console.error(aObject);\n bObject = {\n _rtype: \"error\",\n _rvalue: aObject.toString()\n };\n } else if (typeof File !== \"undefined\" && aObject instanceof File) {\n bObject = {\n _rtype: \"file\",\n _rvalue: aObject,\n _rpath: aObject._path || aObject.webkitRelativePath\n };\n } // send objects supported by structure clone algorithm\n // https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm\n else if (aObject !== Object(aObject) || aObject instanceof Boolean || aObject instanceof String || aObject instanceof Date || aObject instanceof RegExp || aObject instanceof ImageData || typeof FileList !== \"undefined\" && aObject instanceof FileList || typeof FileSystemDirectoryHandle !== \"undefined\" && aObject instanceof FileSystemDirectoryHandle || typeof FileSystemFileHandle !== \"undefined\" && aObject instanceof FileSystemFileHandle || typeof FileSystemHandle !== \"undefined\" && aObject instanceof FileSystemHandle || typeof FileSystemWritableFileStream !== \"undefined\" && aObject instanceof FileSystemWritableFileStream) {\n bObject = aObject; // TODO: avoid object such as DynamicPlugin instance.\n } else if (typeof File !== \"undefined\" && aObject instanceof File) {\n bObject = {\n _rtype: \"file\",\n _rname: aObject.name,\n _rmime: aObject.type,\n _rvalue: aObject,\n _rpath: aObject._path || aObject.webkitRelativePath\n };\n } else if (aObject instanceof Blob) {\n bObject = {\n _rtype: \"blob\",\n _rvalue: aObject\n };\n } else if (aObject instanceof ArrayBufferView) {\n if (aObject._transfer || _transfer) {\n transferables.push(aObject.buffer);\n delete aObject._transfer;\n }\n\n const dtype = Object(_utils_js__WEBPACK_IMPORTED_MODULE_0__[\"typedArrayToDtype\"])(aObject);\n bObject = {\n _rtype: \"typedarray\",\n _rvalue: aObject.buffer,\n _rdtype: dtype\n };\n } else if (aObject instanceof DataView) {\n if (aObject._transfer || _transfer) {\n transferables.push(aObject.buffer);\n delete aObject._transfer;\n }\n\n bObject = {\n _rtype: \"memoryview\",\n _rvalue: aObject.buffer\n };\n } else if (aObject instanceof Set) {\n bObject = {\n _rtype: \"set\",\n _rvalue: await this._encode(Array.from(aObject), asInterface)\n };\n } else if (aObject instanceof Map) {\n bObject = {\n _rtype: \"orderedmap\",\n _rvalue: await this._encode(Array.from(aObject), asInterface)\n };\n } else if (aObject.constructor instanceof Object || Array.isArray(aObject)) {\n bObject = isarray ? [] : {};\n let keys; // an object/array\n\n if (aObject.constructor === Object || Array.isArray(aObject)) {\n keys = Object.keys(aObject);\n } // a class\n else if (aObject.constructor === Function) {\n throw new Error(\"Please instantiate the class before exportting it.\");\n } // instance of a class\n else if (aObject.constructor.constructor === Function) {\n keys = Object.getOwnPropertyNames(Object.getPrototypeOf(aObject)).concat(Object.keys(aObject)); // TODO: use a proxy object to represent the actual object\n // always encode class instance as interface\n\n asInterface = true;\n } else {\n throw Error(\"Unsupported interface type\");\n }\n\n let hasFunction = false; // encode interfaces\n\n if (aObject._rintf || asInterface) {\n if (!objectId) {\n if (typeof aObject._rintf === \"string\" && aObject._rintf.length > 0) {\n objectId = aObject._rintf; // enable custom object id\n } else {\n objectId = Object(_utils_js__WEBPACK_IMPORTED_MODULE_0__[\"randId\"])();\n } // Note: object with the same id will be overwritten\n\n\n if (this._object_store[objectId]) console.warn(`Overwritting interface object with the same id: ${objectId}`);\n this._object_store[objectId] = aObject;\n }\n\n for (let k of keys) {\n if (k === \"constructor\") continue;\n\n if (k.startsWith(\"_\")) {\n continue;\n }\n\n bObject[k] = await this._encode(aObject[k], typeof asInterface === \"string\" ? asInterface + \".\" + k : k, objectId);\n\n if (typeof aObject[k] === \"function\") {\n hasFunction = true;\n }\n } // object id for dispose the object remotely\n\n\n if (hasFunction) bObject._rintf = objectId; // remove interface when closed\n\n if (aObject.on && typeof aObject.on === \"function\") {\n aObject.on(\"close\", () => {\n delete this._object_store[objectId];\n });\n }\n } else {\n for (let k of keys) {\n if ([\"hasOwnProperty\", \"constructor\"].includes(k)) continue;\n bObject[k] = await this._encode(aObject[k]);\n }\n } // for example, browserFS object\n\n } else if (typeof aObject === \"object\") {\n const keys = Object.getOwnPropertyNames(Object.getPrototypeOf(aObject)).concat(Object.keys(aObject));\n const objectId = Object(_utils_js__WEBPACK_IMPORTED_MODULE_0__[\"randId\"])();\n\n for (let k of keys) {\n if ([\"hasOwnProperty\", \"constructor\"].includes(k)) continue; // encode as interface\n\n bObject[k] = await this._encode(aObject[k], k, bObject);\n } // object id, used for dispose the object\n\n\n bObject._rintf = objectId;\n } else {\n throw \"imjoy-rpc: Unsupported data type:\" + aObject;\n }\n\n if (transferables.length > 0) {\n bObject.__transferables__ = transferables;\n }\n\n if (!bObject) {\n throw new Error(\"Failed to encode object\");\n }\n\n return bObject;\n }\n\n async _decode(aObject, withPromise) {\n if (!aObject) {\n return aObject;\n }\n\n let bObject;\n\n if (aObject[\"_rtype\"]) {\n if (this._codecs[aObject._rtype] && this._codecs[aObject._rtype].decoder) {\n if (aObject._rintf) {\n const temp = aObject._rtype;\n delete aObject._rtype;\n aObject = await this._decode(aObject, withPromise);\n aObject._rtype = temp;\n }\n\n bObject = await Promise.resolve(this._codecs[aObject._rtype].decoder(aObject));\n } else if (aObject._rtype === \"callback\") {\n bObject = this._genRemoteCallback(aObject._rtarget_id, aObject._rvalue, withPromise);\n } else if (aObject._rtype === \"interface\") {\n bObject = this._genRemoteMethod(aObject._rtarget_id, aObject._rvalue, aObject._rintf);\n } else if (aObject._rtype === \"ndarray\") {\n /*global nj tf*/\n //create build array/tensor if used in the plugin\n if (typeof nj !== \"undefined\" && nj.array) {\n if (Array.isArray(aObject._rvalue)) {\n aObject._rvalue = aObject._rvalue.reduce(_appendBuffer);\n }\n\n bObject = nj.array(new Uint8(aObject._rvalue), aObject._rdtype).reshape(aObject._rshape);\n } else if (typeof tf !== \"undefined\" && tf.Tensor) {\n if (Array.isArray(aObject._rvalue)) {\n aObject._rvalue = aObject._rvalue.reduce(_appendBuffer);\n }\n\n const arraytype = _utils_js__WEBPACK_IMPORTED_MODULE_0__[\"dtypeToTypedArray\"][aObject._rdtype];\n bObject = tf.tensor(new arraytype(aObject._rvalue), aObject._rshape, aObject._rdtype);\n } else {\n //keep it as regular if transfered to the main app\n bObject = aObject;\n }\n } else if (aObject._rtype === \"error\") {\n bObject = new Error(aObject._rvalue);\n } else if (aObject._rtype === \"file\") {\n if (aObject._rvalue instanceof File) {\n bObject = aObject._rvalue; //patch _path\n\n bObject._path = aObject._rpath;\n } else {\n bObject = new File([aObject._rvalue], aObject._rname, {\n type: aObject._rmime\n });\n bObject._path = aObject._rpath;\n }\n } else if (aObject._rtype === \"typedarray\") {\n const arraytype = _utils_js__WEBPACK_IMPORTED_MODULE_0__[\"dtypeToTypedArray\"][aObject._rdtype];\n if (!arraytype) throw new Error(\"unsupported dtype: \" + aObject._rdtype);\n bObject = new arraytype(aObject._rvalue);\n } else if (aObject._rtype === \"memoryview\") {\n bObject = new DataView(aObject._rvalue);\n } else if (aObject._rtype === \"blob\") {\n if (aObject._rvalue instanceof Blob) {\n bObject = aObject._rvalue;\n } else {\n bObject = new Blob([aObject._rvalue], {\n type: aObject._rmime\n });\n }\n } else if (aObject._rtype === \"orderedmap\") {\n bObject = new Map(await this._decode(aObject._rvalue, withPromise));\n } else if (aObject._rtype === \"set\") {\n bObject = new Set(await this._decode(aObject._rvalue, withPromise));\n } else {\n // make sure all the interface functions are decoded\n if (aObject._rintf) {\n const temp = aObject._rtype;\n delete aObject._rtype;\n bObject = await this._decode(aObject, withPromise);\n bObject._rtype = temp;\n } else bObject = aObject;\n }\n } else if (aObject.constructor === Object || Array.isArray(aObject)) {\n const isarray = Array.isArray(aObject);\n bObject = isarray ? [] : {};\n\n for (let k of Object.keys(aObject)) {\n if (isarray || aObject.hasOwnProperty(k)) {\n const v = aObject[k];\n bObject[k] = await this._decode(v, withPromise);\n }\n }\n } else {\n bObject = aObject;\n }\n\n if (bObject === undefined) {\n throw new Error(\"Failed to decode object\");\n } // store the object id for dispose\n\n\n if (aObject._rintf) {\n this._object_weakmap.set(bObject, aObject._rintf);\n }\n\n return bObject;\n }\n\n async _wrap(args, asInterface) {\n return await this._encode(args, asInterface);\n }\n /**\n * Unwraps the set of arguments delivered from the remote site,\n * replaces all callback identifiers with a function which will\n * initiate sending that callback identifier back to other site\n *\n * @param {Object} args to unwrap\n *\n * @param {Boolean} withPromise is true means this the callback should contain a promise\n *\n * @returns {Array} unwrapped args\n */\n\n\n async _unwrap(args, withPromise) {\n return await this._decode(args, withPromise);\n }\n /**\n * Generates the wrapped function corresponding to a single remote\n * callback. When the generated function is called, it will send\n * the corresponding message to the remote site asking it to\n * execute the particular callback previously saved during a call\n * by the remote site a method from the interface of this site\n *\n * @param {Number} id of the remote callback to execute\n * @param {Number} argNum argument index of the callback\n * @param {Boolean} withPromise is true means this the callback should contain a promise\n *\n * @returns {Function} wrapped remote callback\n */\n\n\n _genRemoteCallback(targetId, cid, withPromise) {\n const me = this;\n let remoteCallback;\n\n if (withPromise) {\n remoteCallback = function () {\n return new Promise(async (resolve, reject) => {\n const args = await me._wrap(Array.prototype.slice.call(arguments));\n const argLength = args.length; // if the last argument is an object, mark it as kwargs\n\n const withKwargs = argLength > 0 && typeof args[argLength - 1] === \"object\" && args[argLength - 1] !== null && args[argLength - 1]._rkwargs;\n if (withKwargs) delete args[argLength - 1]._rkwargs;\n const transferables = args.__transferables__;\n if (transferables) delete args.__transferables__;\n const encodedPromise = await me._wrap([resolve, reject]); // store the key id for removing them from the reference store together\n\n resolve.__promise_pair = encodedPromise[1]._rvalue;\n reject.__promise_pair = encodedPromise[0]._rvalue;\n\n try {\n me._connection.emit({\n type: \"callback\",\n target_id: targetId,\n id: cid,\n args: args,\n promise: encodedPromise,\n with_kwargs: withKwargs\n }, transferables);\n } catch (e) {\n reject(`Failed to exectue remote callback ( id: ${cid}).`);\n }\n });\n };\n\n return remoteCallback;\n } else {\n remoteCallback = async function () {\n const args = await me._wrap(Array.prototype.slice.call(arguments));\n const argLength = args.length; // if the last argument is an object, mark it as kwargs\n\n const withKwargs = argLength > 0 && typeof args[argLength - 1] === \"object\" && args[argLength - 1] !== null && args[argLength - 1]._rkwargs;\n if (withKwargs) delete args[argLength - 1]._rkwargs;\n const transferables = args.__transferables__;\n if (transferables) delete args.__transferables__;\n return me._connection.emit({\n type: \"callback\",\n target_id: targetId,\n id: cid,\n args: args,\n with_kwargs: withKwargs\n }, transferables);\n };\n\n return remoteCallback;\n }\n }\n\n reset() {\n this._event_handlers = {};\n this._once_handlers = {};\n this._remote_interface = null;\n this._object_store = {};\n this._method_weakmap = new WeakMap();\n this._object_weakmap = new WeakMap();\n this._local_api = null;\n this._store = new ReferenceStore();\n this._method_refs = new ReferenceStore();\n }\n /**\n * Sends the notification message and breaks the connection\n */\n\n\n disconnect() {\n this._connection.emit({\n type: \"disconnect\"\n });\n\n this.reset();\n setTimeout(() => {\n this._connection.disconnect();\n }, 2000);\n }\n\n}\n/**\n * ReferenceStore is a special object which stores other objects\n * and provides the references (number) instead. This reference\n * may then be sent over a json-based communication channel (IPC\n * to another Node.js process or a message to the Worker). Other\n * site may then provide the reference in the responce message\n * implying the given object should be activated.\n *\n * Primary usage for the ReferenceStore is a storage for the\n * callbacks, which therefore makes it possible to initiate a\n * callback execution by the opposite site (which normally cannot\n * directly execute functions over the communication channel).\n *\n * Each stored object can only be fetched once and is not\n * available for the second time. Each stored object must be\n * fetched, since otherwise it will remain stored forever and\n * consume memory.\n *\n * Stored object indeces are simply the numbers, which are however\n * released along with the objects, and are later reused again (in\n * order to postpone the overflow, which should not likely happen,\n * but anyway).\n */\n\nclass ReferenceStore {\n constructor() {\n this._store = {}; // stored object\n\n this._indices = [0]; // smallest available indices\n\n this._readyHandler = function () {};\n\n this._busyHandler = function () {};\n\n this._readyHandler();\n }\n /**\n * call handler when the store is empty\n *\n * @param {FUNCTION} id of a handler\n */\n\n\n onReady(readyHandler) {\n this._readyHandler = readyHandler || function () {};\n }\n /**\n * call handler when the store is not empty\n *\n * @param {FUNCTION} id of a handler\n */\n\n\n onBusy(busyHandler) {\n this._busyHandler = busyHandler || function () {};\n }\n /**\n * get the length of the store\n *\n */\n\n\n getStack() {\n return Object.keys(this._store).length;\n }\n /**\n * @function _genId() generates the new reference id\n *\n * @returns {Number} smallest available id and reserves it\n */\n\n\n _genId() {\n let id;\n\n if (this._indices.length === 1) {\n id = this._indices[0]++;\n } else {\n id = this._indices.shift();\n }\n\n return id;\n }\n /**\n * Releases the given reference id so that it will be available by\n * another object stored\n *\n * @param {Number} id to release\n */\n\n\n _releaseId(id) {\n for (let i = 0; i < this._indices.length; i++) {\n if (id < this._indices[i]) {\n this._indices.splice(i, 0, id);\n\n break;\n }\n } // cleaning-up the sequence tail\n\n\n for (let i = this._indices.length - 1; i >= 0; i--) {\n if (this._indices[i] - 1 === this._indices[i - 1]) {\n this._indices.pop();\n } else {\n break;\n }\n }\n }\n /**\n * Stores the given object and returns the refernce id instead\n *\n * @param {Object} obj to store\n *\n * @returns {Number} reference id of the stored object\n */\n\n\n put(obj) {\n if (this._busyHandler && Object.keys(this._store).length === 0) {\n this._busyHandler();\n }\n\n const id = this._genId();\n\n this._store[id] = obj;\n return id;\n }\n /**\n * Retrieves previously stored object and releases its reference\n *\n * @param {Number} id of an object to retrieve\n */\n\n\n fetch(id) {\n const obj = this._store[id];\n\n if (obj && !obj.__remote_method) {\n delete this._store[id];\n\n this._releaseId(id);\n\n if (this._readyHandler && Object.keys(this._store).length === 0) {\n this._readyHandler();\n }\n }\n\n if (obj && obj.__promise_pair) {\n this.fetch(obj.__promise_pair);\n }\n\n return obj;\n }\n\n}\n\n/***/ }),\n\n/***/ \"./src/utils.js\":\n/*!**********************!*\\\n !*** ./src/utils.js ***!\n \\**********************/\n/*! exports provided: randId, dtypeToTypedArray, loadRequirementsInWindow, loadRequirementsInWebworker, loadRequirements, normalizeConfig, typedArrayToDtypeMapping, typedArrayToDtype, cacheRequirements, setupServiceWorker, urlJoin, MessageEmitter */\n/***/ (function(module, __webpack_exports__, __webpack_require__) {\n\n\"use strict\";\n__webpack_require__.r(__webpack_exports__);\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"randId\", function() { return randId; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"dtypeToTypedArray\", function() { return dtypeToTypedArray; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"loadRequirementsInWindow\", function() { return loadRequirementsInWindow; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"loadRequirementsInWebworker\", function() { return loadRequirementsInWebworker; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"loadRequirements\", function() { return loadRequirements; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"normalizeConfig\", function() { return normalizeConfig; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"typedArrayToDtypeMapping\", function() { return typedArrayToDtypeMapping; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"typedArrayToDtype\", function() { return typedArrayToDtype; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"cacheRequirements\", function() { return cacheRequirements; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"setupServiceWorker\", function() { return setupServiceWorker; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"urlJoin\", function() { return urlJoin; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"MessageEmitter\", function() { return MessageEmitter; });\nfunction randId() {\n return Math.random().toString(36).substr(2, 10) + new Date().getTime();\n}\nconst dtypeToTypedArray = {\n int8: Int8Array,\n int16: Int16Array,\n int32: Int32Array,\n uint8: Uint8Array,\n uint16: Uint16Array,\n uint32: Uint32Array,\n float32: Float32Array,\n float64: Float64Array,\n array: Array\n};\nasync function loadRequirementsInWindow(requirements) {\n function _importScript(url) {\n //url is URL of external file, implementationCode is the code\n //to be called from the file, location is the location to\n //insert the <script> element\n return new Promise((resolve, reject) => {\n var scriptTag = document.createElement(\"script\");\n scriptTag.src = url;\n scriptTag.type = \"text/javascript\";\n scriptTag.onload = resolve;\n\n scriptTag.onreadystatechange = function () {\n if (this.readyState === \"loaded\" || this.readyState === \"complete\") {\n resolve();\n }\n };\n\n scriptTag.onerror = reject;\n document.head.appendChild(scriptTag);\n });\n } // support importScripts outside web worker\n\n\n async function importScripts() {\n var args = Array.prototype.slice.call(arguments),\n len = args.length,\n i = 0;\n\n for (; i < len; i++) {\n await _importScript(args[i]);\n }\n }\n\n if (requirements && (Array.isArray(requirements) || typeof requirements === \"string\")) {\n try {\n var link_node;\n requirements = typeof requirements === \"string\" ? [requirements] : requirements;\n\n if (Array.isArray(requirements)) {\n for (var i = 0; i < requirements.length; i++) {\n if (requirements[i].toLowerCase().endsWith(\".css\") || requirements[i].startsWith(\"css:\")) {\n if (requirements[i].startsWith(\"css:\")) {\n requirements[i] = requirements[i].slice(4);\n }\n\n link_node = document.createElement(\"link\");\n link_node.rel = \"stylesheet\";\n link_node.href = requirements[i];\n document.head.appendChild(link_node);\n } else if (requirements[i].toLowerCase().endsWith(\".mjs\") || requirements[i].startsWith(\"mjs:\")) {\n // import esmodule\n if (requirements[i].startsWith(\"mjs:\")) {\n requirements[i] = requirements[i].slice(4);\n }\n\n await import(\n /* webpackIgnore: true */\n requirements[i]);\n } else if (requirements[i].toLowerCase().endsWith(\".js\") || requirements[i].startsWith(\"js:\")) {\n if (requirements[i].startsWith(\"js:\")) {\n requirements[i] = requirements[i].slice(3);\n }\n\n await importScripts(requirements[i]);\n } else if (requirements[i].startsWith(\"http\")) {\n await importScripts(requirements[i]);\n } else if (requirements[i].startsWith(\"cache:\")) {//ignore cache\n } else {\n console.log(\"Unprocessed requirements url: \" + requirements[i]);\n }\n }\n } else {\n throw \"unsupported requirements definition\";\n }\n } catch (e) {\n throw \"failed to import required scripts: \" + requirements.toString();\n }\n }\n}\nasync function loadRequirementsInWebworker(requirements) {\n if (requirements && (Array.isArray(requirements) || typeof requirements === \"string\")) {\n try {\n if (!Array.isArray(requirements)) {\n requirements = [requirements];\n }\n\n for (var i = 0; i < requirements.length; i++) {\n if (requirements[i].toLowerCase().endsWith(\".css\") || requirements[i].startsWith(\"css:\")) {\n throw \"unable to import css in a webworker\";\n } else if (requirements[i].toLowerCase().endsWith(\".js\") || requirements[i].startsWith(\"js:\")) {\n if (requirements[i].startsWith(\"js:\")) {\n requirements[i] = requirements[i].slice(3);\n }\n\n importScripts(requirements[i]);\n } else if (requirements[i].startsWith(\"http\")) {\n importScripts(requirements[i]);\n } else if (requirements[i].startsWith(\"cache:\")) {//ignore cache\n } else {\n console.log(\"Unprocessed requirements url: \" + requirements[i]);\n }\n }\n } catch (e) {\n throw \"failed to import required scripts: \" + requirements.toString();\n }\n }\n}\nfunction loadRequirements(requirements) {\n if (typeof WorkerGlobalScope !== \"undefined\" && self instanceof WorkerGlobalScope) {\n return loadRequirementsInWebworker(requirements);\n } else {\n return loadRequirementsInWindow(requirements);\n }\n}\nfunction normalizeConfig(config) {\n config.version = config.version || \"0.1.0\";\n config.description = config.description || `[TODO: add description for ${config.name} ]`;\n config.type = config.type || \"rpc-window\";\n config.id = config.id || randId();\n config.target_origin = config.target_origin || \"*\";\n config.allow_execution = config.allow_execution || false; // remove functions\n\n config = Object.keys(config).reduce((p, c) => {\n if (typeof config[c] !== \"function\") p[c] = config[c];\n return p;\n }, {});\n return config;\n}\nconst typedArrayToDtypeMapping = {\n Int8Array: \"int8\",\n Int16Array: \"int16\",\n Int32Array: \"int32\",\n Uint8Array: \"uint8\",\n Uint16Array: \"uint16\",\n Uint32Array: \"uint32\",\n Float32Array: \"float32\",\n Float64Array: \"float64\",\n Array: \"array\"\n};\nconst typedArrayToDtypeKeys = [];\n\nfor (const arrType of Object.keys(typedArrayToDtypeMapping)) {\n typedArrayToDtypeKeys.push(eval(arrType));\n}\n\nfunction typedArrayToDtype(obj) {\n let dtype = typedArrayToDtypeMapping[obj.constructor.name];\n\n if (!dtype) {\n const pt = Object.getPrototypeOf(obj);\n\n for (const arrType of typedArrayToDtypeKeys) {\n if (pt instanceof arrType) {\n dtype = typedArrayToDtypeMapping[arrType.name];\n break;\n }\n }\n }\n\n return dtype;\n}\n\nfunction cacheUrlInServiceWorker(url) {\n return new Promise(function (resolve, reject) {\n const message = {\n command: \"add\",\n url: url\n };\n\n if (!navigator.serviceWorker || !navigator.serviceWorker.register) {\n reject(\"Service worker is not supported.\");\n return;\n }\n\n const messageChannel = new MessageChannel();\n\n messageChannel.port1.onmessage = function (event) {\n if (event.data && event.data.error) {\n reject(event.data.error);\n } else {\n resolve(event.data && event.data.result);\n }\n };\n\n if (navigator.serviceWorker && navigator.serviceWorker.controller) {\n navigator.serviceWorker.controller.postMessage(message, [messageChannel.port2]);\n } else {\n reject(\"Service worker controller is not available\");\n }\n });\n}\n\nasync function cacheRequirements(requirements) {\n requirements = requirements || [];\n\n if (!Array.isArray(requirements)) {\n requirements = [requirements];\n }\n\n for (let req of requirements) {\n //remove prefix\n if (req.startsWith(\"js:\")) req = req.slice(3);\n if (req.startsWith(\"css:\")) req = req.slice(4);\n if (req.startsWith(\"cache:\")) req = req.slice(6);\n if (!req.startsWith(\"http\")) continue;\n await cacheUrlInServiceWorker(req).catch(e => {\n console.error(e);\n });\n }\n}\nfunction setupServiceWorker(baseUrl, targetOrigin, cacheCallback) {\n // register service worker for offline access\n if (\"serviceWorker\" in navigator) {\n baseUrl = baseUrl || \"/\";\n navigator.serviceWorker.register(baseUrl + \"plugin-service-worker.js\").then(function (registration) {\n // Registration was successful\n console.log(\"ServiceWorker registration successful with scope: \", registration.scope);\n }, function (err) {\n // registration failed :(\n console.log(\"ServiceWorker registration failed: \", err);\n });\n targetOrigin = targetOrigin || \"*\";\n cacheCallback = cacheCallback || cacheRequirements;\n\n if (cacheCallback && typeof cacheCallback !== \"function\") {\n throw new Error(\"config.cache_requirements must be a function\");\n }\n\n window.addEventListener(\"message\", function (e) {\n if (targetOrigin === \"*\" || e.origin === targetOrigin) {\n const m = e.data;\n\n if (m.type === \"cacheRequirements\") {\n cacheCallback(m.requirements);\n }\n }\n });\n }\n} //#Source https://bit.ly/2neWfJ2\n\nfunction urlJoin(...args) {\n return args.join(\"/\").replace(/[\\/]+/g, \"/\").replace(/^(.+):\\//, \"$1://\").replace(/^file:/, \"file:/\").replace(/\\/(\\?|&|#[^!])/g, \"$1\").replace(/\\?/g, \"&\").replace(\"&\", \"?\");\n}\nclass MessageEmitter {\n constructor(debug) {\n this._event_handlers = {};\n this._once_handlers = {};\n this._debug = debug;\n }\n\n emit() {\n throw new Error(\"emit is not implemented\");\n }\n\n on(event, handler) {\n if (!this._event_handlers[event]) {\n this._event_handlers[event] = [];\n }\n\n this._event_handlers[event].push(handler);\n }\n\n once(event, handler) {\n handler.___event_run_once = true;\n this.on(event, handler);\n }\n\n off(event, handler) {\n if (!event && !handler) {\n // remove all events handlers\n this._event_handlers = {};\n } else if (event && !handler) {\n // remove all hanlders for the event\n if (this._event_handlers[event]) this._event_handlers[event] = [];\n } else {\n // remove a specific handler\n if (this._event_handlers[event]) {\n const idx = this._event_handlers[event].indexOf(handler);\n\n if (idx >= 0) {\n this._event_handlers[event].splice(idx, 1);\n }\n }\n }\n }\n\n _fire(event, data) {\n if (this._event_handlers[event]) {\n var i = this._event_handlers[event].length;\n\n while (i--) {\n const handler = this._event_handlers[event][i];\n\n try {\n handler(data);\n } catch (e) {\n console.error(e);\n } finally {\n if (handler.___event_run_once) {\n this._event_handlers[event].splice(i, 1);\n }\n }\n }\n } else {\n if (this._debug) {\n console.warn(\"unhandled event\", event, data);\n }\n }\n }\n\n}\n\n/***/ })\n\n/******/ });\n//# sourceMappingURL=plugin.webworker.js.map", null);
};
/***/ }),
/***/ "./src/pluginCore.js":
/*!***************************!*\
!*** ./src/pluginCore.js ***!
\***************************/
/*! exports provided: connectRPC */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "connectRPC", function() { return connectRPC; });
/* harmony import */ var _rpc_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./rpc.js */ "./src/rpc.js");
/**
* Core plugin script loaded into the plugin process/thread.
*
* Initializes the plugin-site API global methods.
*/
function connectRPC(connection, config) {
config = config || {};
const codecs = {};
const rpc = new _rpc_js__WEBPACK_IMPORTED_MODULE_0__["RPC"](connection, config, codecs);
rpc.on("getInterface", function () {
launchConnected();
});
rpc.on("remoteReady", function () {
const api = rpc.getRemote() || {};
api.registerCodec = function (config) {
if (!config["name"] || !config["encoder"] && !config["decoder"]) {
throw new Error("Invalid codec format, please make sure you provide a name, type, encoder and decoder.");
} else {
if (config.type) {
for (let k of Object.keys(codecs)) {
if (codecs[k].type === config.type || k === config.name) {
delete codecs[k];
console.warn("Remove duplicated codec: " + k);
}
}
}
codecs[config["name"]] = config;
}
};
api.init = function (config) {
// register a minimal plugin api
rpc.setInterface({
setup() {}
}, config);
};
api.disposeObject = function (obj) {
rpc.disposeObject(obj);
};
api.export = function (_interface, config) {
rpc.setInterface(_interface, config);
};
api.onLoad = function (handler) {
handler = checkHandler(handler);
if (connected) {
handler();
} else {
connectedHandlers.push(handler);
}
};
api.dispose = function (_interface) {
rpc.disconnect();
};
api._rpc = rpc;
if (typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope) {
self.api = api;
self.postMessage({
type: "imjoy_remote_api_ready"
});
self.dispatchEvent(new CustomEvent("imjoy_remote_api_ready", {
detail: api
}));
} else if (typeof window) {
window.dispatchEvent(new CustomEvent("imjoy_remote_api_ready", {
detail: api
}));
}
});
let connected = false;
const connectedHandlers = [];
const launchConnected = function () {
if (!connected) {
connected = true;
let handler;
while (handler = connectedHandlers.pop()) {
handler();
}
}
};
const checkHandler = function (handler) {
const type = typeof handler;
if (type !== "function") {
const msg = "A function may only be subsribed to the event, " + type + " was provided instead";
throw new Error(msg);
}
return handler;
};
return rpc;
}
/***/ }),
/***/ "./src/pluginIframe.js":
/*!*****************************!*\
!*** ./src/pluginIframe.js ***!
\*****************************/
/*! exports provided: Connection, default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "Connection", function() { return Connection; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "default", function() { return setupIframe; });
/* harmony import */ var _pluginCore_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./pluginCore.js */ "./src/pluginCore.js");
/* harmony import */ var _rpc_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./rpc.js */ "./src/rpc.js");
/* harmony import */ var _utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils.js */ "./src/utils.js");
/**
* Contains the routines loaded by the plugin iframe under web-browser
* in case when worker failed to initialize
*
* Initializes the web environment version of the platform-dependent
* connection object for the plugin site
*/
// Create a new, plain <span> element
function _htmlToElement(html) {
var template = document.createElement("template");
html = html.trim(); // Never return a text node of whitespace as the result
template.innerHTML = html;
return template.content.firstChild;
}
const _inWebWorker = typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope;
async function executeEsModule(content) {
const dataUri = "data:text/javascript;charset=utf-8," + encodeURIComponent(content);
await import(
/* webpackIgnore: true */
dataUri);
}
class Connection extends _utils_js__WEBPACK_IMPORTED_MODULE_2__["MessageEmitter"] {
constructor(config) {
super(config && config.debug);
this.config = config || {};
this.peer_id = Object(_utils_js__WEBPACK_IMPORTED_MODULE_2__["randId"])();
}
connect() {
this.config.target_origin = this.config.target_origin || "*"; // this will call handleEvent function
if (this.config.broadcastChannel) {
this.broadcastChannel = new BroadcastChannel(this.config.broadcastChannel);
} else {
this.broadcastChannel = null;
}
if (this.broadcastChannel) this.broadcastChannel.addEventListener("message", this);else globalThis.addEventListener("message", this);
this.emit({
type: "initialized",
config: this.config,
origin: globalThis.location.origin,
peer_id: this.peer_id
});
this._fire("connected");
}
handleEvent(e) {
if (e.type === "message" && (this.broadcastChannel || this.config.target_origin === "*" || !e.origin || e.origin === this.config.target_origin)) {
if (e.data.peer_id === this.peer_id) {
this._fire(e.data.type, e.data);
} else if (this.config.debug) {
console.log(`connection peer id mismatch ${e.data.peer_id} !== ${this.peer_id}`);
}
}
}
disconnect() {
this._fire("beforeDisconnect");
globalThis.removeEventListener("message", this);
this._fire("disconnected");
}
emit(data) {
let transferables;
if (this.broadcastChannel) this.broadcastChannel.postMessage(data);else {
if (data.__transferables__) {
transferables = data.__transferables__;
delete data.__transferables__;
} else if (_inWebWorker) self.postMessage(data, transferables);else parent.postMessage(data, this.config.target_origin, transferables);
}
}
async execute(code) {
try {
if (code.type === "requirements") {
await Object(_utils_js__WEBPACK_IMPORTED_MODULE_2__["loadRequirementsInWindow"])(code.requirements);
} else if (code.type === "script") {
if (code.src) {
var script_node = document.createElement("script");
script_node.setAttribute("type", code.attrs.type);
script_node.setAttribute("src", code.src);
document.head.appendChild(script_node);
} else {
if (code.content && code.attrs.lang === "javascript") {
// document.addEventListener("DOMContentLoaded", function(){
if (code.attrs.type === "module") {
await executeEsModule(code.content);
} else {
eval(code.content);
} // });
} else {
var node = document.createElement("script");
for (let k in code.attrs) {
node.setAttribute(k, code.attrs[k]);
}
node.appendChild(document.createTextNode(code.content));
document.body.appendChild(node);
}
}
} else if (code.type === "style") {
const style_node = document.createElement("style");
if (code.src) {
style_node.src = code.src;
}
style_node.innerHTML = code.content;
document.head.appendChild(style_node);
} else if (code.type === "link") {
const link_node_ = document.createElement("link");
if (code.rel) {
link_node_.rel = code.rel;
}
if (code.href) {
link_node_.href = code.href;
}
if (code.attrs && code.attrs.type) {
link_node_.type = code.attrs.type;
}
document.head.appendChild(link_node_);
} else if (code.type === "html") {
document.body.appendChild(_htmlToElement(code.content));
} else {
throw "unsupported code type.";
}
if (_inWebWorker) self.postMessage({
type: "executed"
});else parent.postMessage({
type: "executed"
}, this.config.target_origin);
} catch (e) {
console.error("failed to execute scripts: ", code, e);
if (_inWebWorker) self.postMessage({
type: "executed",
error: e.stack || String(e)
});else parent.postMessage({
type: "executed",
error: e.stack || String(e)
}, this.config.target_origin);
}
}
}
function setupIframe(config) {
config = config || {};
config.dedicated_thread = false;
config.lang = "javascript";
config.api_version = _rpc_js__WEBPACK_IMPORTED_MODULE_1__["API_VERSION"];
const conn = new Connection(config);
Object(_pluginCore_js__WEBPACK_IMPORTED_MODULE_0__["connectRPC"])(conn, config);
conn.connect();
}
/***/ }),
/***/ "./src/rpc.js":
/*!********************!*\
!*** ./src/rpc.js ***!
\********************/
/*! exports provided: API_VERSION, RPC */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "API_VERSION", function() { return API_VERSION; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "RPC", function() { return RPC; });
/* harmony import */ var _utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils.js */ "./src/utils.js");
/**
* Contains the RPC object used both by the application
* site, and by each plugin
*/
const API_VERSION = "0.2.3";
const ArrayBufferView = Object.getPrototypeOf(Object.getPrototypeOf(new Uint8Array())).constructor;
function _appendBuffer(buffer1, buffer2) {
const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
}
function indexObject(obj, is) {
if (!is) throw new Error("undefined index");
if (typeof is === "string") return indexObject(obj, is.split("."));else if (is.length === 0) return obj;else return indexObject(obj[is[0]], is.slice(1));
}
/**
* RPC object represents a single site in the
* communication protocol between the application and the plugin
*
* @param {Object} connection a special object allowing to send
* and receive messages from the opposite site (basically it
* should only provide send() and onMessage() methods)
*/
class RPC extends _utils_js__WEBPACK_IMPORTED_MODULE_0__["MessageEmitter"] {
constructor(connection, config, codecs) {
super(config && config.debug);
this._connection = connection;
this.config = config || {};
this._codecs = codecs || {};
this._object_store = {};
this._method_weakmap = new WeakMap();
this._object_weakmap = new WeakMap();
this._local_api = null;
this._remote_set = false; // make sure there is an execute function
const name = this.config.name;
this._connection.execute = this._connection.execute || function () {
throw new Error(`connection.execute not implemented (in "${name}")`);
};
this._store = new ReferenceStore();
this._method_refs = new ReferenceStore();
this._method_refs.onReady(() => {
this._fire("remoteIdle");
});
this._method_refs.onBusy(() => {
this._fire("remoteBusy");
});
this._setupMessageHanlders();
}
init() {
this._connection.emit({
type: "initialized",
config: this.config,
peer_id: this._connection.peer_id
});
}
setConfig(config) {
if (config) for (const k of Object.keys(config)) {
this.config[k] = config[k];
}
}
/**
* Set a handler to be called when received a responce from the
* remote site reporting that the previously provided interface
* has been successfully set as remote for that site
*
* @param {Function} handler
*/
getRemoteCallStack() {
return this._method_refs.getStack();
}
/**
* @returns {Object} set of remote interface methods
*/
getRemote() {
return this._remote_interface;
}
/**
* Sets the interface of this site making it available to the
* remote site by sending a message with a set of methods names
*
* @param {Object} _interface to set
*/
setInterface(_interface, config) {
config = config || {};
this.config.name = config.name || this.config.name;
this.config.description = config.description || this.config.description;
if (this.config.forwarding_functions) {
for (let func_name of this.config.forwarding_functions) {
const _remote = this._remote_interface;
if (_remote[func_name]) {
if (_interface.constructor === Object) {
if (!_interface[func_name]) {
_interface[func_name] = (...args) => {
_remote[func_name](...args);
};
}
} else if (_interface.constructor.constructor === Function) {
if (!_interface.constructor.prototype[func_name]) {
_interface.constructor.prototype[func_name] = (...args) => {
_remote[func_name](...args);
};
}
}
}
}
}
this._local_api = _interface;
if (!this._remote_set) this._fire("interfaceAvailable");else this.sendInterface();
return new Promise(resolve => {
this.once("interfaceSetAsRemote", resolve);
});
}
/**
* Sends the actual interface to the remote site upon it was
* updated or by a special request of the remote site
*/
sendInterface() {
if (!this._local_api) {
throw new Error("interface is not set.");
}
this._encode(this._local_api, true).then(api => {
this._connection.emit({
type: "setInterface",
api: api
});
});
}
_disposeObject(objectId) {
if (this._object_store[objectId]) {
delete this._object_store[objectId];
} else {
throw new Error(`Object (id=${objectId}) not found.`);
}
}
disposeObject(obj) {
return new Promise((resolve, reject) => {
if (this._object_weakmap.has(obj)) {
const objectId = this._object_weakmap.get(obj);
this._connection.once("disposed", data => {
if (data.error) reject(new Error(data.error));else resolve();
});
this._connection.emit({
type: "disposeObject",
object_id: objectId
});
} else {
throw new Error("Invalid object");
}
});
}
/**
* Handles a message from the remote site
*/
_setupMessageHanlders() {
this._connection.on("init", this.init);
this._connection.on("execute", data => {
Promise.resolve(this._connection.execute(data.code)).then(() => {
this._connection.emit({
type: "executed"
});
}).catch(e => {
console.error(e);
this._connection.emit({
type: "executed",
error: String(e)
});
});
});
this._connection.on("method", async data => {
let resolve, reject, method, method_this, args, result;
try {
if (data.promise) {
[resolve, reject] = await this._unwrap(data.promise, false);
}
const _interface = this._object_store[data.object_id];
method = indexObject(_interface, data.name);
if (data.name.includes(".")) {
const tmp = data.name.split(".");
const intf_index = tmp.slice(0, tmp.length - 1).join(".");
method_this = indexObject(_interface, intf_index);
} else {
method_this = _interface;
}
args = await this._unwrap(data.args, true);
if (data.promise) {
result = method.apply(method_this, args);
if (result instanceof Promise || method.constructor && method.constructor.name === "AsyncFunction") {
result.then(resolve).catch(reject);
} else {
resolve(result);
}
} else {
method.apply(method_this, args);
}
} catch (err) {
console.error(this.config.name, err);