Files
codeql-action/node_modules/ava/lib/worker/plugin.cjs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

131 lines
3.0 KiB
JavaScript
Raw Normal View History

2021-10-21 15:24:20 -07:00
const pkg = require('../../package.json');
2022-02-01 18:01:11 +00:00
const {registerSharedWorker: register} = require('./channel.cjs');
const options = require('./options.cjs');
const {sharedWorkerTeardowns, waitForReady} = require('./state.cjs');
require('./guard-environment.cjs'); // eslint-disable-line import/no-unassigned-import
2021-10-21 15:24:20 -07:00
const workers = new Map();
const workerTeardownFns = new WeakMap();
function createSharedWorker(filename, initialData, teardown) {
2022-02-01 18:01:11 +00:00
const {channel, forceUnref, ready} = register(filename, initialData, teardown);
waitForReady.push(ready);
sharedWorkerTeardowns.push(async () => {
try {
await teardown();
} finally {
forceUnref();
}
});
2021-10-21 15:24:20 -07:00
class ReceivedMessage {
2022-02-01 18:01:11 +00:00
constructor(id, data) {
2021-10-21 15:24:20 -07:00
this.id = id;
2022-02-01 18:01:11 +00:00
this.data = data;
2021-10-21 15:24:20 -07:00
}
reply(data) {
return publishMessage(data, this.id);
}
}
// Ensure that, no matter how often it's received, we have a stable message
// object.
const messageCache = new WeakMap();
async function * receiveMessages(replyTo) {
for await (const evt of channel.receive()) {
if (replyTo === undefined && evt.replyTo !== undefined) {
continue;
}
if (replyTo !== undefined && evt.replyTo !== replyTo) {
continue;
}
let message = messageCache.get(evt);
if (message === undefined) {
2022-02-01 18:01:11 +00:00
message = new ReceivedMessage(evt.messageId, evt.data);
2021-10-21 15:24:20 -07:00
messageCache.set(evt, message);
}
yield message;
}
}
function publishMessage(data, replyTo) {
2022-02-01 18:01:11 +00:00
const id = channel.post(data, replyTo);
2021-10-21 15:24:20 -07:00
return {
id,
async * replies() {
yield * receiveMessages(id);
2022-02-01 18:01:11 +00:00
},
2021-10-21 15:24:20 -07:00
};
}
return {
available: channel.available,
2022-02-01 18:01:11 +00:00
protocol: 'ava-4',
2021-10-21 15:24:20 -07:00
get currentlyAvailable() {
return channel.currentlyAvailable;
},
publish(data) {
return publishMessage(data);
},
async * subscribe() {
yield * receiveMessages();
2022-02-01 18:01:11 +00:00
},
2021-10-21 15:24:20 -07:00
};
}
function registerSharedWorker({
filename,
initialData,
supportedProtocols,
2022-02-01 18:01:11 +00:00
teardown,
2021-10-21 15:24:20 -07:00
}) {
2022-02-01 18:01:11 +00:00
const options_ = options.get();
2021-10-21 15:24:20 -07:00
2022-02-01 18:01:11 +00:00
if (!options_.workerThreads) {
throw new Error('Shared workers can be used only when worker threads are enabled');
2021-10-21 15:24:20 -07:00
}
2022-02-01 18:01:11 +00:00
if (!supportedProtocols.includes('ava-4')) {
throw new Error(`This version of AVA (${pkg.version}) does not support any of the desired shared worker protocols: ${supportedProtocols.join(',')}`);
2021-10-21 15:24:20 -07:00
}
2022-02-01 18:01:11 +00:00
filename = String(filename); // Allow URL instances.
2021-10-21 15:24:20 -07:00
let worker = workers.get(filename);
if (worker === undefined) {
worker = createSharedWorker(filename, initialData, async () => {
// Run possibly asynchronous teardown functions serially, in reverse
// order. Any error will crash the worker.
const teardownFns = workerTeardownFns.get(worker);
if (teardownFns !== undefined) {
for await (const fn of [...teardownFns].reverse()) {
await fn();
}
}
});
workers.set(filename, worker);
}
if (teardown !== undefined) {
if (workerTeardownFns.has(worker)) {
workerTeardownFns.get(worker).push(teardown);
} else {
workerTeardownFns.set(worker, [teardown]);
}
}
return worker;
}
exports.registerSharedWorker = registerSharedWorker;