@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 124 lines 3.73 KB
Raw
1 /**
2 * Parent-side Node.js support for Effect workers.
3 *
4 * `layerPlatform` installs the `WorkerPlatform` used by a Node program that
5 * owns workers. It supports both `node:worker_threads` workers and IPC-enabled
6 * child processes, routing messages through Effect's worker protocol. `layer`
7 * combines that platform with a `Spawner` callback, and the platform asks
8 * workers to close on scope finalization before forcefully terminating them on
9 * timeout.
10 *
11 * @stability unstable
12 * @since 4.0.0
13 */
14 import * as Deferred from "effect/Deferred"
15 import * as Effect from "effect/Effect"
16 import * as Exit from "effect/Exit"
17 import * as Layer from "effect/Layer"
18 import * as Scope from "effect/Scope"
19 import * as Worker from "effect/workers/Worker"
20 import { WorkerError, WorkerReceiveError } from "effect/workers/WorkerError"
21 import type * as ChildProcess from "node:child_process"
22 import type * as WorkerThreads from "node:worker_threads"
23
24 /**
25 * Provides the Node `WorkerPlatform` for `worker_threads` workers and child
26 * process workers, wiring messages, errors, and exits into Effect workers and
27 * terminating the worker if graceful shutdown times out.
28 *
29 * @stability unstable
30 * @category layers
31 * @since 4.0.0
32 */
33 export const layerPlatform: Layer.Layer<Worker.WorkerPlatform> = Layer.succeed(Worker.WorkerPlatform)(
34 Worker.makePlatform<WorkerThreads.Worker | ChildProcess.ChildProcess>()({
35 setup({ scope, worker }) {
36 const exitDeferred = Deferred.makeUnsafe<void, WorkerError>()
37 const thing = "postMessage" in worker ?
38 {
39 postMessage(msg: any, t?: any) {
40 worker.postMessage(msg, t)
41 },
42 kill: () => worker.terminate(),
43 worker
44 } :
45 {
46 postMessage(msg: any, _?: any) {
47 worker.send(msg)
48 },
49 kill: () => worker.kill("SIGKILL"),
50 worker
51 }
52 worker.on("exit", () => {
53 Deferred.doneUnsafe(exitDeferred, Exit.void)
54 })
55 return Effect.as(
56 Scope.addFinalizer(
57 scope,
58 Effect.suspend(() => {
59 thing.postMessage([1])
60 return Deferred.await(exitDeferred)
61 }).pipe(
62 Effect.timeout(5000),
63 Effect.catchCause(() => Effect.promise(() => Promise.resolve(thing.kill())))
64 )
65 ),
66 thing
67 )
68 },
69 listen({ deferred, emit, port }) {
70 port.worker.on("message", (message) => {
71 emit(message)
72 })
73 port.worker.on("messageerror", (cause) => {
74 Deferred.doneUnsafe(
75 deferred,
76 new WorkerError({
77 reason: new WorkerReceiveError({
78 message: "A messageerror event was emitted",
79 cause
80 })
81 })
82 )
83 })
84 port.worker.on("error", (cause) => {
85 Deferred.doneUnsafe(
86 deferred,
87 new WorkerError({
88 reason: new WorkerReceiveError({
89 message: "An error event was emitted",
90 cause
91 })
92 })
93 )
94 })
95 port.worker.on("exit", (code) => {
96 Deferred.doneUnsafe(
97 deferred,
98 new WorkerError({
99 reason: new WorkerReceiveError({
100 message: "The worker has exited with code: " + code
101 })
102 })
103 )
104 })
105 return Effect.void
106 }
107 })
108 )
109
110 /**
111 * Provides the Node `WorkerPlatform` together with a `Worker.Spawner` created
112 * from the supplied worker or child-process spawning function.
113 *
114 * @stability unstable
115 * @category layers
116 * @since 4.0.0
117 */
118 export const layer = (
119 spawn: (id: number) => WorkerThreads.Worker | ChildProcess.ChildProcess
120 ): Layer.Layer<Worker.WorkerPlatform | Worker.Spawner> =>
121 Layer.merge(
122 Worker.layerSpawner(spawn),
123 layerPlatform
124 )