1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.isWorkerRuntime = exports.getWorkerImplementation = exports.defaultPoolSize = void 0;
|
5 | const get_bundle_url_browser_1 = require("./get-bundle-url.browser");
|
6 | exports.defaultPoolSize = typeof navigator !== "undefined" && navigator.hardwareConcurrency
|
7 | ? navigator.hardwareConcurrency
|
8 | : 4;
|
9 | const isAbsoluteURL = (value) => /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(value);
|
10 | function createSourceBlobURL(code) {
|
11 | const blob = new Blob([code], { type: "application/javascript" });
|
12 | return URL.createObjectURL(blob);
|
13 | }
|
14 | function selectWorkerImplementation() {
|
15 | if (typeof Worker === "undefined") {
|
16 |
|
17 |
|
18 | return class NoWebWorker {
|
19 | constructor() {
|
20 | throw Error("No web worker implementation available. You might have tried to spawn a worker within a worker in a browser that doesn't support workers in workers.");
|
21 | }
|
22 | };
|
23 | }
|
24 | class WebWorker extends Worker {
|
25 | constructor(url, options) {
|
26 | var _a, _b;
|
27 | if (typeof url === "string" && options && options._baseURL) {
|
28 | url = new URL(url, options._baseURL);
|
29 | }
|
30 | else if (typeof url === "string" && !isAbsoluteURL(url) && get_bundle_url_browser_1.getBundleURL().match(/^file:\/\//i)) {
|
31 | url = new URL(url, get_bundle_url_browser_1.getBundleURL().replace(/\/[^\/]+$/, "/"));
|
32 | if ((_a = options === null || options === void 0 ? void 0 : options.CORSWorkaround) !== null && _a !== void 0 ? _a : true) {
|
33 | url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`);
|
34 | }
|
35 | }
|
36 | if (typeof url === "string" && isAbsoluteURL(url)) {
|
37 |
|
38 |
|
39 | if ((_b = options === null || options === void 0 ? void 0 : options.CORSWorkaround) !== null && _b !== void 0 ? _b : true) {
|
40 | url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`);
|
41 | }
|
42 | }
|
43 | super(url, options);
|
44 | }
|
45 | }
|
46 | class BlobWorker extends WebWorker {
|
47 | constructor(blob, options) {
|
48 | const url = window.URL.createObjectURL(blob);
|
49 | super(url, options);
|
50 | }
|
51 | static fromText(source, options) {
|
52 | const blob = new window.Blob([source], { type: "text/javascript" });
|
53 | return new BlobWorker(blob, options);
|
54 | }
|
55 | }
|
56 | return {
|
57 | blob: BlobWorker,
|
58 | default: WebWorker
|
59 | };
|
60 | }
|
61 | let implementation;
|
62 | function getWorkerImplementation() {
|
63 | if (!implementation) {
|
64 | implementation = selectWorkerImplementation();
|
65 | }
|
66 | return implementation;
|
67 | }
|
68 | exports.getWorkerImplementation = getWorkerImplementation;
|
69 | function isWorkerRuntime() {
|
70 | const isWindowContext = typeof self !== "undefined" && typeof Window !== "undefined" && self instanceof Window;
|
71 | return typeof self !== "undefined" && self.postMessage && !isWindowContext ? true : false;
|
72 | }
|
73 | exports.isWorkerRuntime = isWorkerRuntime;
|