@xylabs/threads
Version:
Web workers & worker threads as simple as a function call
1 lines • 6.75 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/master/get-bundle-url.browser.ts","../../../src/master/implementation.browser.ts"],"sourcesContent":["// Source: <https://github.com/parcel-bundler/parcel/blob/master/packages/core/parcel-bundler/src/builtins/bundle-url.js>\n\nlet bundleURL: string | undefined\n\nfunction getBundleURLCached(): string {\n if (!bundleURL) {\n bundleURL = getBundleURL()\n }\n\n return bundleURL\n}\n\nfunction getBundleURL(): string {\n // Attempt to find the URL of the current script and use that as the base URL\n try {\n throw new Error('getBundleURL failed')\n } catch (ex) {\n const err = ex as Error\n const matches = ('' + err.stack).match(/(https?|file|ftp|chrome-extension|moz-extension):\\/\\/[^\\n)]+/g)\n if (matches) {\n return getBaseURL(matches[0])\n }\n }\n\n return '/'\n}\n\nfunction getBaseURL(url: string): string {\n return ('' + url).replace(/^((?:https?|file|ftp|chrome-extension|moz-extension):\\/\\/.+)?\\/[^/]+(?:\\?.*)?$/, '$1') + '/'\n}\n\nexport { getBundleURLCached as getBundleURL }\n","/* eslint-disable @stylistic/max-len */\n/* eslint-disable import-x/no-internal-modules */\n// tslint:disable max-classes-per-file\n\nimport type { ImplementationExport, ThreadsWorkerOptions } from '../types/master.ts'\nimport { getBundleURL } from './get-bundle-url.browser.ts'\n\nexport const defaultPoolSize = typeof navigator !== 'undefined' && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4\n\nconst isAbsoluteURL = (value: string) => /^[A-Za-z][\\d+.A-Za-z\\-]*:/.test(value)\n\nfunction createSourceBlobURL(code: string): string {\n const blob = new Blob([code], { type: 'application/javascript' })\n return URL.createObjectURL(blob)\n}\n\nfunction selectWorkerImplementation(): ImplementationExport {\n if (typeof Worker === 'undefined') {\n // Might happen on Safari, for instance\n // The idea is to only fail if the constructor is actually used\n return class NoWebWorker {\n constructor() {\n throw new Error(\n \"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.\",\n )\n }\n } as unknown as ImplementationExport\n }\n\n class WebWorker extends Worker {\n constructor(url: string | URL, options?: ThreadsWorkerOptions) {\n if (typeof url === 'string' && options && options._baseURL) {\n url = new URL(url, options._baseURL)\n } else if (typeof url === 'string' && !isAbsoluteURL(url) && /^file:\\/\\//i.test(getBundleURL())) {\n url = new URL(url, getBundleURL().replace(/\\/[^/]+$/, '/'))\n if (options?.CORSWorkaround ?? true) {\n url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)\n }\n }\n if (\n typeof url === 'string'\n && isAbsoluteURL(url) // Create source code blob loading JS file via `importScripts()`\n // to circumvent worker CORS restrictions\n && (options?.CORSWorkaround ?? true)\n ) {\n url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)\n }\n super(url, options)\n }\n }\n\n class BlobWorker extends WebWorker {\n constructor(blob: Blob, options?: ThreadsWorkerOptions) {\n const url = globalThis.URL.createObjectURL(blob)\n super(url, options)\n }\n\n static fromText(source: string, options?: ThreadsWorkerOptions): WebWorker {\n const blob = new globalThis.Blob([source], { type: 'text/javascript' })\n return new BlobWorker(blob, options)\n }\n }\n\n return {\n blob: BlobWorker,\n default: WebWorker,\n }\n}\n\nlet implementation: ImplementationExport\n\nexport function getWorkerImplementation(): ImplementationExport {\n if (!implementation) {\n implementation = selectWorkerImplementation()\n }\n return implementation\n}\n\nexport function isWorkerRuntime() {\n const isWindowContext = typeof globalThis !== 'undefined' && typeof Window !== 'undefined' && globalThis instanceof Window\n return typeof globalThis !== 'undefined' && self['postMessage'] && !isWindowContext ? true : false\n}\n"],"mappings":";;;;AAEA,IAAIA;AAEJ,SAASC,qBAAAA;AACP,MAAI,CAACD,WAAW;AACdA,gBAAYE,aAAAA;EACd;AAEA,SAAOF;AACT;AANSC;AAQT,SAASC,eAAAA;AAEP,MAAI;AACF,UAAM,IAAIC,MAAM,qBAAA;EAClB,SAASC,IAAI;AACX,UAAMC,MAAMD;AACZ,UAAME,WAAW,KAAKD,IAAIE,OAAOC,MAAM,+DAAA;AAC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AAiBkB,KAAKC,UAAUP,GAAAA,CAAAA,IAAQ;MACpE;AACA,YAAMA,KAAKC,OAAAA;IACb;EACF;AAEA,MAAMO,aAAN,MAAMA,oBAAmBT,UAAAA;IAnD3B,OAmD2BA;;;IACvB,YAAYT,MAAYW,SAAgC;AACtD,YAAMD,MAAMS,WAAWhB,IAAIC,gBAAgBJ,IAAAA;AAC3C,YAAMU,KAAKC,OAAAA;IACb;IAEA,OAAOS,SAASC,QAAgBV,SAA2C;AACzE,YAAMX,OAAO,IAAImB,WAAWlB,KAAK;QAACoB;SAAS;QAAEnB,MAAM;MAAkB,CAAA;AACrE,aAAO,IAAIgB,YAAWlB,MAAMW,OAAAA;IAC9B;EACF;AAEA,SAAO;IACLX,MAAMkB;IACNI,SAASb;EACX;AACF;AAnDSJ;AAqDT,IAAIkB;AAEG,SAASC,0BAAAA;AACd,MAAI,CAACD,gBAAgB;AACnBA,qBAAiBlB,2BAAAA;EACnB;AACA,SAAOkB;AACT;AALgBC;AAOT,SAASC,kBAAAA;AACd,QAAMC,kBAAkB,OAAOP,eAAe,eAAe,OAAOQ,WAAW,eAAeR,sBAAsBQ;AACpH,SAAO,OAAOR,eAAe,eAAeS,KAAK,aAAA,KAAkB,CAACF,kBAAkB,OAAO;AAC/F;AAHgBD;","names":["bundleURL","getBundleURLCached","getBundleURL","Error","ex","err","matches","stack","match","getBaseURL","url","replace","defaultPoolSize","navigator","hardwareConcurrency","isAbsoluteURL","value","test","createSourceBlobURL","code","blob","Blob","type","URL","createObjectURL","selectWorkerImplementation","Worker","NoWebWorker","Error","WebWorker","url","options","_baseURL","getBundleURL","replace","CORSWorkaround","JSON","stringify","BlobWorker","globalThis","fromText","source","default","implementation","getWorkerImplementation","isWorkerRuntime","isWindowContext","Window","self"]}