Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function createWorker({
createMessenger = createWorkerMessenger,
}: CreateWorkerOptions = {}): Endpoint['call'] {
if (scriptUrl) {
const endpoint = createEndpoint(createMessenger(scriptUrl));
const {call: caller} = endpoint;
workerEndpointCache.set(caller, endpoint);
return caller as any;
}
// The babel plugin that comes with this package actually turns the argument
// into a string (the public path of the worker script). If it’s a function,
// it’s because we’re in an environment where we didn’t transform it into a
// worker. In that case, we can use the fact that we will get access to the
// real module and pretend to be a worker that way.
if (typeof script === 'function') {
return new Proxy(
{},
{
export function expose(api: any) {
const endpoint = createEndpoint(fromWebWorker(self as any));
self.addEventListener('message', ({data}: MessageEvent) => {
if (data == null) {
return;
}
if (data.__replace instanceof MessagePort) {
endpoint.replace(data.__replace);
data.__replace.start();
}
});
Reflect.defineProperty(self, 'endpoint', {
value: endpoint,
});