File size: 21,548 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var process_manager_exports = {};
__export(process_manager_exports, {
  ProcessManager: () => ProcessManager,
  QueryProcessManager: () => QueryProcessManager,
  QueryProcessWrapper: () => QueryProcessWrapper,
  RawProcessManager: () => RawProcessManager,
  RawProcessWrapper: () => RawProcessWrapper,
  StreamProcessManager: () => StreamProcessManager,
  StreamProcessWrapper: () => StreamProcessWrapper,
  StreamWorker: () => StreamWorker,
  exec: () => exec,
  processManagers: () => processManagers
});
module.exports = __toCommonJS(process_manager_exports);
var child_process = __toESM(require("child_process"));
var cluster = __toESM(require("cluster"));
var path = __toESM(require("path"));
var Streams = __toESM(require("./streams"));
var import_fs = require("./fs");
/**
 * Process Manager
 * Pokemon Showdown - http://pokemonshowdown.com/
 *
 * This file abstract out multiprocess logic involved in several tasks.
 *
 * Child processes can be queried.
 *
 * @license MIT
 */
const processManagers = [];
function exec(args, execOptions) {
  if (Array.isArray(args)) {
    const cmd = args.shift();
    if (!cmd)
      throw new Error(`You must pass a command to ProcessManager.exec.`);
    return new Promise((resolve, reject) => {
      child_process.execFile(cmd, args, execOptions, (err, stdout, stderr) => {
        if (err)
          reject(err);
        if (typeof stdout !== "string")
          stdout = stdout.toString();
        if (typeof stderr !== "string")
          stderr = stderr.toString();
        resolve({ stdout, stderr });
      });
    });
  } else {
    return new Promise((resolve, reject) => {
      child_process.exec(args, execOptions, (error, stdout, stderr) => {
        if (error)
          reject(error);
        if (typeof stdout !== "string")
          stdout = stdout.toString();
        resolve(stdout);
      });
    });
  }
}
class SubprocessStream extends Streams.ObjectReadWriteStream {
  constructor(process2, taskId2) {
    super();
    this.process = process2;
    this.taskId = taskId2;
    this.process.process.send(`${taskId2}
NEW`);
  }
  _write(message2) {
    if (!this.process.process.connected) {
      this.pushError(new Error(`Process disconnected (possibly crashed?)`));
      return;
    }
    this.process.process.send(`${this.taskId}
WRITE
${message2}`);
  }
  _writeEnd() {
    this.process.process.send(`${this.taskId}
WRITEEND`);
  }
  _destroy() {
    if (!this.process.process.connected)
      return;
    this.process.process.send(`${this.taskId}
DESTROY`);
    this.process.deleteStream(this.taskId);
    this.process = null;
  }
}
class RawSubprocessStream extends Streams.ObjectReadWriteStream {
  constructor(process2) {
    super();
    this.process = process2;
  }
  _write(message2) {
    if (!this.process.getProcess().connected) {
      return;
    }
    this.process.process.send(message2);
  }
}
class QueryProcessWrapper {
  constructor(file, messageCallback) {
    this.process = child_process.fork(file, [], { cwd: import_fs.FS.ROOT_PATH });
    this.taskId = 0;
    this.file = file;
    this.pendingTasks = /* @__PURE__ */ new Map();
    this.pendingRelease = null;
    this.resolveRelease = null;
    this.messageCallback = messageCallback || null;
    this.process.on("message", (message2) => {
      if (message2.startsWith("THROW\n")) {
        const error = new Error();
        error.stack = message2.slice(6);
        throw error;
      }
      if (message2.startsWith("DEBUG\n")) {
        this.debug = message2.slice(6);
        return;
      }
      if (this.messageCallback && message2.startsWith(`CALLBACK
`)) {
        this.messageCallback(message2.slice(9));
        return;
      }
      const nlLoc2 = message2.indexOf("\n");
      if (nlLoc2 <= 0)
        throw new Error(`Invalid response ${message2}`);
      const taskId2 = parseInt(message2.slice(0, nlLoc2));
      const resolve = this.pendingTasks.get(taskId2);
      if (!resolve)
        throw new Error(`Invalid taskId ${message2.slice(0, nlLoc2)}`);
      this.pendingTasks.delete(taskId2);
      const resp = this.safeJSON(message2.slice(nlLoc2 + 1));
      resolve(resp);
      if (this.resolveRelease && !this.getLoad())
        this.destroy();
    });
  }
  safeJSON(obj) {
    if (obj === "undefined") {
      return void 0;
    }
    try {
      return JSON.parse(obj);
    } catch (e) {
      global.Monitor?.crashlog?.(e, `a ${path.basename(this.file)} process`, { result: obj });
      return void 0;
    }
  }
  getProcess() {
    return this.process;
  }
  getLoad() {
    return this.pendingTasks.size;
  }
  query(input) {
    this.taskId++;
    const taskId2 = this.taskId;
    this.process.send(`${taskId2}
${JSON.stringify(input)}`);
    return new Promise((resolve) => {
      this.pendingTasks.set(taskId2, resolve);
    });
  }
  release() {
    if (this.pendingRelease)
      return this.pendingRelease;
    if (!this.getLoad()) {
      this.destroy();
    } else {
      this.pendingRelease = new Promise((resolve) => {
        this.resolveRelease = resolve;
      });
    }
    return this.pendingRelease;
  }
  destroy() {
    if (this.pendingRelease && !this.resolveRelease) {
      return;
    }
    this.process.disconnect();
    for (const resolver of this.pendingTasks.values()) {
      resolver("");
    }
    this.pendingTasks.clear();
    if (this.resolveRelease) {
      this.resolveRelease();
      this.resolveRelease = null;
    } else if (!this.pendingRelease) {
      this.pendingRelease = Promise.resolve();
    }
  }
}
class StreamProcessWrapper {
  constructor(file, messageCallback) {
    this.taskId = 0;
    this.activeStreams = /* @__PURE__ */ new Map();
    this.pendingRelease = null;
    this.resolveRelease = null;
    this.process = child_process.fork(file, [], { cwd: import_fs.FS.ROOT_PATH });
    this.messageCallback = messageCallback;
    this.process.on("message", (message2) => {
      if (message2.startsWith("THROW\n")) {
        const error = new Error();
        error.stack = message2.slice(6);
        throw error;
      }
      if (this.messageCallback && message2.startsWith(`CALLBACK
`)) {
        this.messageCallback(message2.slice(9));
        return;
      }
      if (message2.startsWith("DEBUG\n")) {
        this.setDebug(message2.slice(6));
        return;
      }
      let nlLoc2 = message2.indexOf("\n");
      if (nlLoc2 <= 0)
        throw new Error(`Invalid response ${message2}`);
      const taskId2 = parseInt(message2.slice(0, nlLoc2));
      const stream2 = this.activeStreams.get(taskId2);
      if (!stream2)
        return;
      message2 = message2.slice(nlLoc2 + 1);
      nlLoc2 = message2.indexOf("\n");
      if (nlLoc2 < 0)
        nlLoc2 = message2.length;
      const messageType2 = message2.slice(0, nlLoc2);
      message2 = message2.slice(nlLoc2 + 1);
      if (messageType2 === "END") {
        stream2.pushEnd();
        this.deleteStream(taskId2);
      } else if (messageType2 === "PUSH") {
        stream2.push(message2);
      } else if (messageType2 === "THROW") {
        const error = new Error();
        error.stack = message2;
        stream2.pushError(error, true);
      } else {
        throw new Error(`Unrecognized messageType ${messageType2}`);
      }
    });
  }
  setDebug(message2) {
    this.debug = (this.debug || "").slice(-32768) + "\n=====\n" + message2;
  }
  getLoad() {
    return this.activeStreams.size;
  }
  getProcess() {
    return this.process;
  }
  deleteStream(taskId2) {
    this.activeStreams.delete(taskId2);
    if (this.resolveRelease && !this.getLoad())
      void this.destroy();
  }
  createStream() {
    this.taskId++;
    const taskId2 = this.taskId;
    const stream2 = new SubprocessStream(this, taskId2);
    this.activeStreams.set(taskId2, stream2);
    return stream2;
  }
  release() {
    if (this.pendingRelease)
      return this.pendingRelease;
    if (!this.getLoad()) {
      void this.destroy();
    } else {
      this.pendingRelease = new Promise((resolve) => {
        this.resolveRelease = resolve;
      });
    }
    return this.pendingRelease;
  }
  destroy() {
    if (this.pendingRelease && !this.resolveRelease) {
      return;
    }
    this.process.disconnect();
    const destroyed = [];
    for (const stream2 of this.activeStreams.values()) {
      destroyed.push(stream2.destroy());
    }
    this.activeStreams.clear();
    if (this.resolveRelease) {
      this.resolveRelease();
      this.resolveRelease = null;
    } else if (!this.pendingRelease) {
      this.pendingRelease = Promise.resolve();
    }
    return Promise.all(destroyed);
  }
}
class StreamWorker {
  constructor(stream2) {
    this.load = 0;
    this.workerid = 0;
    this.stream = stream2;
  }
}
class RawProcessWrapper {
  constructor(file, isCluster, env) {
    this.taskId = 0;
    this.pendingRelease = null;
    this.resolveRelease = null;
    this.workerid = 0;
    /** Not managed by RawProcessWrapper itself */
    this.load = 0;
    if (isCluster) {
      this.process = cluster.fork(env);
      this.workerid = this.process.id;
    } else {
      this.process = child_process.fork(file, [], { cwd: import_fs.FS.ROOT_PATH, env });
    }
    this.process.on("message", (message2) => {
      this.stream.push(message2);
    });
    this.stream = new RawSubprocessStream(this);
  }
  setDebug(message2) {
    this.debug = (this.debug || "").slice(-32768) + "\n=====\n" + message2;
  }
  getLoad() {
    return this.load;
  }
  getProcess() {
    return this.process.process ? this.process.process : this.process;
  }
  release() {
    if (this.pendingRelease)
      return this.pendingRelease;
    if (!this.getLoad()) {
      void this.destroy();
    } else {
      this.pendingRelease = new Promise((resolve) => {
        this.resolveRelease = resolve;
      });
    }
    return this.pendingRelease;
  }
  destroy() {
    if (this.pendingRelease && !this.resolveRelease) {
      return;
    }
    void this.stream.destroy();
    this.process.disconnect();
  }
}
const _ProcessManager = class {
  constructor(module2) {
    this.processes = [];
    this.releasingProcesses = [];
    this.crashedProcesses = [];
    this.crashTime = 0;
    this.crashRespawnCount = 0;
    this.filename = module2.filename;
    this.basename = path.basename(module2.filename);
    this.isParentProcess = process.mainModule !== module2 || !process.send;
    this.listen();
  }
  acquire() {
    if (!this.processes.length) {
      return null;
    }
    let lowestLoad = this.processes[0];
    for (const process2 of this.processes) {
      if (process2.getLoad() < lowestLoad.getLoad()) {
        lowestLoad = process2;
      }
    }
    return lowestLoad;
  }
  releaseCrashed(process2) {
    const index = this.processes.indexOf(process2);
    if (index < 0)
      return;
    this.processes.splice(index, 1);
    this.destroyProcess(process2);
    void process2.release().then(() => {
      const releasingIndex = this.releasingProcesses.indexOf(process2);
      if (releasingIndex >= 0) {
        this.releasingProcesses.splice(releasingIndex, 1);
      }
    });
    const now = Date.now();
    if (this.crashTime && now - this.crashTime > 30 * 60 * 1e3) {
      this.crashTime = 0;
      this.crashRespawnCount = 0;
    }
    if (!this.crashTime)
      this.crashTime = now;
    this.crashRespawnCount += 1;
    void Promise.reject(
      new Error(`Process ${this.basename} ${process2.getProcess().pid} crashed and had to be restarted`)
    );
    this.releasingProcesses.push(process2);
    this.crashedProcesses.push(process2);
    if (this.crashRespawnCount <= 5) {
      this.spawn(this.processes.length + 1);
    }
  }
  unspawn() {
    return Promise.all([...this.processes].map(
      (process2) => this.unspawnOne(process2)
    ));
  }
  async unspawnOne(process2) {
    if (!process2)
      return;
    this.destroyProcess(process2);
    const processIndex = this.processes.indexOf(process2);
    if (processIndex < 0)
      throw new Error("Process inactive");
    this.processes.splice(this.processes.indexOf(process2), 1);
    this.releasingProcesses.push(process2);
    await process2.release();
    const index = this.releasingProcesses.indexOf(process2);
    if (index < 0)
      return;
    this.releasingProcesses.splice(index, 1);
  }
  spawn(count = 1, force) {
    if (!this.isParentProcess)
      return;
    if (_ProcessManager.disabled && !force)
      return;
    const spawnCount = count - this.processes.length;
    for (let i = 0; i < spawnCount; i++) {
      this.spawnOne(force);
    }
  }
  spawnOne(force) {
    if (!this.isParentProcess)
      throw new Error("Must use in parent process");
    if (_ProcessManager.disabled && !force)
      return null;
    const process2 = this.createProcess();
    process2.process.on("disconnect", () => this.releaseCrashed(process2));
    this.processes.push(process2);
    return process2;
  }
  respawn(count = null) {
    if (count === null)
      count = this.processes.length;
    const unspawned = this.unspawn();
    this.spawn(count);
    return unspawned;
  }
  destroyProcess(process2) {
  }
  destroy() {
    const index = processManagers.indexOf(this);
    if (index >= 0)
      processManagers.splice(index, 1);
    return this.unspawn();
  }
};
let ProcessManager = _ProcessManager;
ProcessManager.disabled = false;
class QueryProcessManager extends ProcessManager {
  /**
   * @param timeout The number of milliseconds to wait before terminating a query. Defaults to 900000 ms (15 minutes).
   */
  constructor(module2, query, timeout = 15 * 60 * 1e3, debugCallback) {
    super(module2);
    this._query = query;
    this.timeout = timeout;
    this.messageCallback = debugCallback;
    processManagers.push(this);
  }
  async query(input, process2 = this.acquire()) {
    if (!process2)
      return this._query(input);
    const timeout = setTimeout(() => {
      const debugInfo = process2.debug || "No debug information found.";
      process2.destroy();
      this.spawnOne();
      throw new Error(
        `A query originating in ${this.basename} took too long to complete; the process has been respawned.
${debugInfo}`
      );
    }, this.timeout);
    const result = await process2.query(input);
    clearTimeout(timeout);
    return result;
  }
  queryTemporaryProcess(input, force) {
    const process2 = this.spawnOne(force);
    const result = this.query(input, process2);
    void this.unspawnOne(process2);
    return result;
  }
  createProcess() {
    return new QueryProcessWrapper(this.filename, this.messageCallback);
  }
  listen() {
    if (this.isParentProcess)
      return;
    process.on("message", (message) => {
      const nlLoc = message.indexOf("\n");
      if (nlLoc <= 0)
        throw new Error(`Invalid response ${message}`);
      const taskId = message.slice(0, nlLoc);
      message = message.slice(nlLoc + 1);
      if (taskId.startsWith("EVAL")) {
        process.send(`${taskId}
` + eval(message));
        return;
      }
      void Promise.resolve(this._query(JSON.parse(message))).then(
        (response) => process.send(`${taskId}
${JSON.stringify(response)}`)
      );
    });
    process.on("disconnect", () => {
      process.exit();
    });
  }
}
class StreamProcessManager extends ProcessManager {
  constructor(module2, createStream, messageCallback) {
    super(module2);
    this.activeStreams = /* @__PURE__ */ new Map();
    this._createStream = createStream;
    this.messageCallback = messageCallback;
    processManagers.push(this);
  }
  createStream() {
    const process2 = this.acquire();
    if (!process2)
      return this._createStream();
    return process2.createStream();
  }
  createProcess() {
    return new StreamProcessWrapper(this.filename, this.messageCallback);
  }
  async pipeStream(taskId2, stream2) {
    let done = false;
    while (!done) {
      try {
        let value;
        ({ value, done } = await stream2.next());
        process.send(`${taskId2}
PUSH
${value}`);
      } catch (err) {
        process.send(`${taskId2}
THROW
${err.stack}`);
      }
    }
    if (!this.activeStreams.has(taskId2)) {
      return;
    }
    process.send(`${taskId2}
END`);
    this.activeStreams.delete(taskId2);
  }
  listen() {
    if (this.isParentProcess)
      return;
    process.on("message", (message) => {
      let nlLoc = message.indexOf("\n");
      if (nlLoc <= 0)
        throw new Error(`Invalid request ${message}`);
      const taskId = message.slice(0, nlLoc);
      const stream = this.activeStreams.get(taskId);
      message = message.slice(nlLoc + 1);
      nlLoc = message.indexOf("\n");
      if (nlLoc < 0)
        nlLoc = message.length;
      const messageType = message.slice(0, nlLoc);
      message = message.slice(nlLoc + 1);
      if (taskId.startsWith("EVAL")) {
        process.send(`${taskId}
` + eval(message));
        return;
      }
      if (messageType === "NEW") {
        if (stream)
          throw new Error(`NEW: taskId ${taskId} already exists`);
        const newStream = this._createStream();
        this.activeStreams.set(taskId, newStream);
        void this.pipeStream(taskId, newStream);
      } else if (messageType === "DESTROY") {
        if (!stream)
          throw new Error(`DESTROY: Invalid taskId ${taskId}`);
        void stream.destroy();
        this.activeStreams.delete(taskId);
      } else if (messageType === "WRITE") {
        if (!stream)
          throw new Error(`WRITE: Invalid taskId ${taskId}`);
        void stream.write(message);
      } else if (messageType === "WRITEEND") {
        if (!stream)
          throw new Error(`WRITEEND: Invalid taskId ${taskId}`);
        void stream.writeEnd();
      } else {
        throw new Error(`Unrecognized messageType ${messageType}`);
      }
    });
    process.on("disconnect", () => {
      process.exit();
    });
  }
}
class RawProcessManager extends ProcessManager {
  constructor(options) {
    super(options.module);
    /** full list of processes - parent process only */
    this.workers = [];
    /** if spawning 0 worker processes, the worker is instead stored here in the parent process */
    this.masterWorker = null;
    /** stream used only in the child process */
    this.activeStream = null;
    this.spawnSubscription = null;
    this.unspawnSubscription = null;
    /** worker ID of cluster worker - cluster child process only (0 otherwise) */
    this.workerid = cluster.worker?.id || 0;
    this.isCluster = !!options.isCluster;
    this._setupChild = options.setupChild;
    this.env = options.env;
    if (this.isCluster && this.isParentProcess) {
      cluster.setupMaster({
        exec: this.filename,
        cwd: import_fs.FS.ROOT_PATH
      });
    }
    processManagers.push(this);
  }
  subscribeSpawn(callback) {
    this.spawnSubscription = callback;
  }
  subscribeUnspawn(callback) {
    this.unspawnSubscription = callback;
  }
  spawn(count) {
    super.spawn(count);
    if (!this.workers.length) {
      this.masterWorker = new StreamWorker(this._setupChild());
      this.workers.push(this.masterWorker);
      this.spawnSubscription?.(this.masterWorker);
    }
  }
  createProcess() {
    const process2 = new RawProcessWrapper(this.filename, this.isCluster, this.env);
    this.workers.push(process2);
    this.spawnSubscription?.(process2);
    return process2;
  }
  destroyProcess(process2) {
    const index = this.workers.indexOf(process2);
    if (index >= 0)
      this.workers.splice(index, 1);
    this.unspawnSubscription?.(process2);
  }
  async pipeStream(stream2) {
    let done = false;
    while (!done) {
      try {
        let value;
        ({ value, done } = await stream2.next());
        process.send(value);
      } catch (err) {
        process.send(`THROW
${err.stack}`);
      }
    }
  }
  listen() {
    if (this.isParentProcess)
      return;
    setImmediate(() => {
      this.activeStream = this._setupChild();
      void this.pipeStream(this.activeStream);
    });
    process.on("message", (message2) => {
      void this.activeStream.write(message2);
    });
    process.on("disconnect", () => {
      process.exit();
    });
  }
}
//# sourceMappingURL=process-manager.js.map