All files / src/lib/scan worker-pool.ts

3.6% Statements 4/111
1.96% Branches 1/51
5.88% Functions 1/17
3.66% Lines 4/109

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324                                                                                                                                      168x               134x                                                                     168x                                                                                                                                                                                                                                                                                                                                                     168x                                                                                    
/**
 * Worker pool for parallel file-grep work.
 *
 * Lazy-initialized singleton. Workers are spawned unref'd and ref'd
 * per-dispatch so the process exits cleanly once all dispatches
 * settle — no explicit shutdown is required from callers.
 *
 * Feature-gated on `isWorkerSupported()`. On runtimes without
 * `Worker` / `Blob` / `URL.createObjectURL` (older Node), callers
 * fall back to the async `mapFilesConcurrent` path.
 */
 
import { availableParallelism } from "node:os";
// Worker source loaded as a string. At dev/test time, the module reads
// the file from disk via readFileSync. At build time, esbuild's
// text-import-plugin replaces the module with an inlined string constant.
import GREP_WORKER_SOURCE from "./grep-worker-source.js";
import type { GrepMatch } from "./types.js";
 
/** Batch dispatched to a worker. */
export type WorkerGrepRequest = {
  paths: string[];
  patternSource: string;
  flags: string;
  maxLineLength: number;
  maxMatchesPerFile: number;
  literal: string | null;
};
 
/** Packed result from a single worker batch. Rehydrate via `decodeWorkerMatches`. */
export type WorkerGrepResult = {
  /** Packed 4-u32-per-match (pathIdx, lineNum, lineOffset, lineLength). */
  ints: Uint32Array;
  /**
   * Concatenated line text as UTF-8 bytes. Decoded on the main side;
   * `ints[i*4 + 2]` / `+3` index into the decoded string.
   */
  linePoolBytes: Uint8Array;
};
 
/**
 * Per-worker state.
 *
 * Each worker has one `onmessage` handler and a FIFO `pending`
 * queue of result resolvers. `addEventListener`-per-dispatch would
 * cause every listener to fire on every `result` message — FIFO
 * shifting matches the order in which the worker processes requests.
 */
type PooledWorker = {
  worker: Worker;
  ready: Promise<void>;
  /** Number of dispatches currently in flight to this worker. */
  inflight: number;
  pending: Array<{
    resolve: (r: WorkerGrepResult) => void;
    reject: (e: unknown) => void;
  }>;
  /** Flipped to false on the first `error` event; dispatch skips dead workers. */
  alive: boolean;
};
 
type WorkerPool = {
  workers: PooledWorker[];
  dispatch(request: WorkerGrepRequest): Promise<WorkerGrepResult>;
  terminate(): void;
};
 
let pool: WorkerPool | null = null;
 
/**
 * True when the runtime supports `new Worker(url)` with a Blob-URL
 * source. Covers Bun (dev + single-file binary) and Node 22+ with
 * the DOM Worker shim.
 */
export function isWorkerSupported(): boolean {
  return (
    typeof Worker !== "undefined" &&
    typeof Blob !== "undefined" &&
    typeof URL !== "undefined" &&
    typeof URL.createObjectURL === "function"
  );
}
 
/**
 * Default pool size: `availableParallelism()` clamped to `[2, 8]`.
 * Bench (10k files, `import.*from`): 4 workers hits the knee
 * (~180ms p50 vs 316ms for 2; 8 workers saves ~10ms). Clamping
 * prevents over-spawn on high-core boxes where stat/read
 * contention dominates.
 */
export function getPoolSize(): number {
  return Math.max(2, Math.min(8, availableParallelism()));
}
 
/**
 * `.ref()` / `.unref()` on a Bun/Node worker if available. DOM
 * Worker shims may not expose them — guarded.
 *
 * Both calls are **idempotent booleans, not reference-counted** —
 * one `.unref()` releases the event-loop hold regardless of how
 * many `.ref()` calls preceded it. Call sites must guard on
 * `inflight === 0` to avoid premature unref.
 */
function refWorker(w: Worker): void {
  (w as unknown as { ref?: () => void }).ref?.();
}
function unrefWorker(w: Worker): void {
  (w as unknown as { unref?: () => void }).unref?.();
}
 
let cachedBlobUrl: string | null = null;
function getWorkerBlobUrl(): string {
  if (cachedBlobUrl !== null) {
    return cachedBlobUrl;
  }
  const blob = new Blob([GREP_WORKER_SOURCE], {
    type: "application/javascript",
  });
  cachedBlobUrl = URL.createObjectURL(blob);
  return cachedBlobUrl;
}
 
/**
 * Get or lazily create the worker pool. Throws if
 * `isWorkerSupported()` is false.
 */
export function getWorkerPool(): WorkerPool {
  if (pool !== null) {
    return pool;
  }
  if (!isWorkerSupported()) {
    throw new Error(
      "Worker pool requested but Workers are unavailable in this runtime"
    );
  }
 
  const size = getPoolSize();
  const url = getWorkerBlobUrl();
  const workers: PooledWorker[] = [];
 
  for (let i = 0; i < size; i += 1) {
    const w = new Worker(url);
    unrefWorker(w);
    const pw: PooledWorker = {
      worker: w,
      ready: new Promise<void>((resolve) => {
        const readyHandler = (event: MessageEvent) => {
          if (event.data?.type === "ready") {
            w.removeEventListener("message", readyHandler);
            resolve();
          }
        };
        w.addEventListener("message", readyHandler);
      }),
      inflight: 0,
      pending: [],
      alive: true,
    };
    w.addEventListener("message", (event) => {
      const data = event.data as { type?: string } & WorkerGrepResult;
      if (data.type !== "result") {
        return;
      }
      const next = pw.pending.shift();
      if (!next) {
        return;
      }
      pw.inflight -= 1;
      if (pw.inflight === 0) {
        unrefWorker(pw.worker);
      }
      next.resolve({ ints: data.ints, linePoolBytes: data.linePoolBytes });
    });
    w.addEventListener("error", (err) => {
      pw.alive = false;
      const errMsg = err.message ?? String(err);
      let slot = pw.pending.shift();
      while (slot !== undefined) {
        slot.reject(new Error(`worker error: ${errMsg}`));
        slot = pw.pending.shift();
      }
      pw.inflight = 0;
      unrefWorker(pw.worker);
    });
    workers.push(pw);
  }
 
  pool = {
    workers,
    dispatch(request: WorkerGrepRequest): Promise<WorkerGrepResult> {
      // Pick least-loaded live worker. Dead workers (error event fired)
      // are skipped — their `inflight` was reset to 0, which would
      // otherwise make them look "least loaded" and silently capture
      // all new dispatches.
      let best: PooledWorker | null = null;
      for (const pw of workers) {
        if (!pw.alive) {
          continue;
        }
        if (best === null || pw.inflight < best.inflight) {
          best = pw;
        }
      }
      if (best === null) {
        return Promise.reject(new Error("worker pool: all workers dead"));
      }
      const chosen = best;
      chosen.inflight += 1;
      refWorker(chosen.worker);
 
      // `ourSlot` is held by the closure so a readiness-failure
      // can remove THIS dispatch's slot by identity, not `pop()` /
      // `shift()` which would misassign errors to siblings.
      let ourSlot: PooledWorker["pending"][number];
      const result = new Promise<WorkerGrepResult>((resolve, reject) => {
        ourSlot = { resolve, reject };
        chosen.pending.push(ourSlot);
      });
      chosen.ready.then(
        () => {
          chosen.worker.postMessage(request);
        },
        (err) => {
          const idx = chosen.pending.indexOf(ourSlot);
          if (idx !== -1) {
            chosen.pending.splice(idx, 1);
            chosen.inflight -= 1;
            if (chosen.inflight === 0) {
              unrefWorker(chosen.worker);
            }
            ourSlot.reject(err);
          }
        }
      );
      return result;
    },
    terminate(): void {
      for (const pw of workers) {
        pw.alive = false;
        let slot = pw.pending.shift();
        while (slot !== undefined) {
          slot.reject(new Error("worker pool terminated"));
          slot = pw.pending.shift();
        }
        pw.inflight = 0;
        unrefWorker(pw.worker);
        try {
          pw.worker.terminate();
        } catch {
          // Already-terminated workers throw in Bun — ignore.
        }
      }
    },
  };
 
  return pool;
}
 
/**
 * Tear down the singleton pool. Primarily for tests; the singleton
 * is otherwise kept alive for the process lifetime.
 */
export function terminatePool(): void {
  if (pool !== null) {
    pool.terminate();
    pool = null;
  }
  if (cachedBlobUrl !== null) {
    URL.revokeObjectURL(cachedBlobUrl);
    cachedBlobUrl = null;
  }
}
 
// `ignoreBOM: true` is load-bearing: without it the decoder silently
// strips a leading U+FEFF, which desynchronises every `lineOffset` /
// `lineLength` index the worker stored against the pre-encode pool
// length. A BOM-prefixed source file lands a U+FEFF at pool index 0,
// and with default (BOM-eating) decode the whole batch's lines would
// shift left by one code unit. `fatal: false` (default) keeps
// replacement-char behavior intact for any invalid sequences — the
// worker's round-trip can't produce them, but it's the safer default.
const LINE_POOL_DECODER = new TextDecoder("utf-8", { ignoreBOM: true });
 
/**
 * Decode a worker's packed `{ints, linePoolBytes}` into `GrepMatch[]`,
 * reconstructing path fields from the caller's `paths` / `relPaths`.
 *
 * Optional `mtimes` is a parallel per-path array; when provided,
 * each match gets `mtime` attached via its `pathIdx`. Walker-side
 * knowledge, not worker-side.
 */
export function decodeWorkerMatches(
  result: WorkerGrepResult,
  paths: readonly string[],
  relPaths: readonly string[],
  mtimes: readonly number[] | null = null
): GrepMatch[] {
  const { ints, linePoolBytes } = result;
  const linePool = LINE_POOL_DECODER.decode(linePoolBytes);
  const matches: GrepMatch[] = [];
  const count = Math.floor(ints.length / 4);
  for (let i = 0; i < count; i += 1) {
    const base = i * 4;
    const pathIdx = ints[base] ?? 0;
    const lineNum = ints[base + 1] ?? 0;
    const lineOffset = ints[base + 2] ?? 0;
    const lineLength = ints[base + 3] ?? 0;
    const absolutePath = paths[pathIdx] ?? "";
    const relativePath = relPaths[pathIdx] ?? absolutePath;
    const line = linePool.slice(lineOffset, lineOffset + lineLength);
    const match: GrepMatch = {
      path: relativePath,
      absolutePath,
      lineNum,
      line,
    };
    if (mtimes !== null) {
      match.mtime = mtimes[pathIdx];
    }
    matches.push(match);
  }
  return matches;
}