import path from 'path'; import { Worker } from 'worker_threads'; // Meteor only bundles the npm packages it sees imported/required somewhere in // its own compiled module graph. The actual usage of these three is inside // private/workers/unionWorker.js, a plain asset file Meteor's bundler never // traces (it's loaded at runtime via new Worker(), not import/require here). // Without this, `meteor build` drops them from programs/server/node_modules // and the worker fails at startup with "Cannot find module '@turf/circle'". import '@turf/circle'; import '@turf/union'; import '@turf/truncate'; const WORKER_PATH = path.resolve(process.cwd(), 'assets/app/workers/unionWorker.js'); /** * Circle-union of already-validated, already-decorated subscriptions, run in a * worker thread so the main event loop (DDP/HTTP) stays free regardless of * how long any single turf.union call takes. subs must be an array of plain * { location: { lat, lon }, distance } objects. If baseUnion is given, subs * are merged into it instead of a union computed from scratch. */ const calcUnionAsync = (subs, baseUnion = null) => new Promise((resolve, reject) => { const worker = new Worker(WORKER_PATH, { workerData: { subs, baseUnion } }); worker.once('message', (msg) => { worker.terminate(); if (msg.error) { reject(new Error(msg.error)); } else { resolve(msg.union); } }); worker.once('error', (err) => { worker.terminate(); reject(err); }); }); export default calcUnionAsync;