UNPKG

3.33 kBJavaScriptView Raw
1"use strict";
2// tslint:disable max-classes-per-file
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.isWorkerRuntime = exports.getWorkerImplementation = exports.defaultPoolSize = void 0;
5const get_bundle_url_browser_1 = require("./get-bundle-url.browser");
6exports.defaultPoolSize = typeof navigator !== "undefined" && navigator.hardwareConcurrency
7 ? navigator.hardwareConcurrency
8 : 4;
9const isAbsoluteURL = (value) => /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(value);
10function createSourceBlobURL(code) {
11 const blob = new Blob([code], { type: "application/javascript" });
12 return URL.createObjectURL(blob);
13}
14function selectWorkerImplementation() {
15 if (typeof Worker === "undefined") {
16 // Might happen on Safari, for instance
17 // The idea is to only fail if the constructor is actually used
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 // Create source code blob loading JS file via `importScripts()`
38 // to circumvent worker CORS restrictions
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}
61let implementation;
62function getWorkerImplementation() {
63 if (!implementation) {
64 implementation = selectWorkerImplementation();
65 }
66 return implementation;
67}
68exports.getWorkerImplementation = getWorkerImplementation;
69function isWorkerRuntime() {
70 const isWindowContext = typeof self !== "undefined" && typeof Window !== "undefined" && self instanceof Window;
71 return typeof self !== "undefined" && self.postMessage && !isWindowContext ? true : false;
72}
73exports.isWorkerRuntime = isWorkerRuntime;