UNPKG

@xylabs/threads

Version:

Web workers & worker threads as simple as a function call

114 lines (112 loc) 3.77 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/master/get-bundle-url.browser.ts var bundleURL; function getBundleURLCached() { if (!bundleURL) { bundleURL = getBundleURL(); } return bundleURL; } __name(getBundleURLCached, "getBundleURLCached"); function getBundleURL() { try { throw new Error("getBundleURL failed"); } catch (ex) { const err = ex; const matches = ("" + err.stack).match(/(https?|file|ftp|chrome-extension|moz-extension):\/\/[^\n)]+/g); if (matches) { return getBaseURL(matches[0]); } } return "/"; } __name(getBundleURL, "getBundleURL"); function getBaseURL(url) { return ("" + url).replace(/^((?:https?|file|ftp|chrome-extension|moz-extension):\/\/.+)?\/[^/]+(?:\?.*)?$/, "$1") + "/"; } __name(getBaseURL, "getBaseURL"); // src/master/implementation.browser.ts var defaultPoolSize = typeof navigator !== "undefined" && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4; var isAbsoluteURL = /* @__PURE__ */ __name((value) => /^[A-Za-z][\d+.A-Za-z\-]*:/.test(value), "isAbsoluteURL"); function createSourceBlobURL(code) { const blob = new Blob([ code ], { type: "application/javascript" }); return URL.createObjectURL(blob); } __name(createSourceBlobURL, "createSourceBlobURL"); function selectWorkerImplementation() { if (typeof Worker === "undefined") { return class NoWebWorker { static { __name(this, "NoWebWorker"); } constructor() { throw new 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."); } }; } let WebWorker = class WebWorker extends Worker { static { __name(this, "WebWorker"); } constructor(url, options) { if (typeof url === "string" && options && options._baseURL) { url = new URL(url, options._baseURL); } else if (typeof url === "string" && !isAbsoluteURL(url) && /^file:\/\//i.test(getBundleURLCached())) { url = new URL(url, getBundleURLCached().replace(/\/[^/]+$/, "/")); if (options?.CORSWorkaround ?? true) { url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`); } } if (typeof url === "string" && isAbsoluteURL(url) && (options?.CORSWorkaround ?? true)) { url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`); } super(url, options); } }; let BlobWorker = class BlobWorker2 extends WebWorker { static { __name(this, "BlobWorker"); } constructor(blob, options) { const url = globalThis.URL.createObjectURL(blob); super(url, options); } static fromText(source, options) { const blob = new globalThis.Blob([ source ], { type: "text/javascript" }); return new BlobWorker2(blob, options); } }; return { blob: BlobWorker, default: WebWorker }; } __name(selectWorkerImplementation, "selectWorkerImplementation"); var implementation; function getWorkerImplementation() { if (!implementation) { implementation = selectWorkerImplementation(); } return implementation; } __name(getWorkerImplementation, "getWorkerImplementation"); function isWorkerRuntime() { const isWindowContext = typeof globalThis !== "undefined" && typeof Window !== "undefined" && globalThis instanceof Window; return typeof globalThis !== "undefined" && self["postMessage"] && !isWindowContext ? true : false; } __name(isWorkerRuntime, "isWorkerRuntime"); export { defaultPoolSize, getWorkerImplementation, isWorkerRuntime }; //# sourceMappingURL=implementation.browser.mjs.map