File size: 1,327 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fs from 'node:fs';
import path from 'node:path';

export type SearchWorkerRuntimeState =
  | { readonly configured: false }
  | { readonly configured: true; readonly path: string };

let configuredPath: string | null = null;

export function configureSearchWorkerRuntime(entry: string): SearchWorkerRuntimeState {
  if (!path.isAbsolute(entry)) {
    throw new TypeError('search worker entry must be an absolute path');
  }
  let stat: fs.Stats;
  try {
    stat = fs.statSync(entry);
  } catch (error) {
    throw new TypeError(
      `search worker entry is not readable: ${error instanceof Error ? error.message : String(error)}`,
      { cause: error },
    );
  }
  if (!stat.isFile()) {
    throw new TypeError('search worker entry must be a regular file');
  }
  if (configuredPath !== null) {
    if (configuredPath !== entry) {
      throw new Error('search worker runtime is already configured');
    }
    return { configured: true, path: configuredPath };
  }
  configuredPath = entry;
  return { configured: true, path: configuredPath };
}

export function resetSearchWorkerRuntime(): void {
  configuredPath = null;
}

export function getSearchWorkerRuntimeState(): SearchWorkerRuntimeState {
  return configuredPath === null
    ? { configured: false }
    : { configured: true, path: configuredPath };
}