@effect/platform-deno 4.0.0-beta.104

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -0
  3. package/dist/DenoChildProcessSpawner.d.ts +39 -0
  4. package/dist/DenoChildProcessSpawner.d.ts.map +1 -0
  5. package/dist/DenoChildProcessSpawner.js +311 -0
  6. package/dist/DenoChildProcessSpawner.js.map +1 -0
  7. package/dist/DenoClusterHttp.d.ts +60 -0
  8. package/dist/DenoClusterHttp.d.ts.map +1 -0
  9. package/dist/DenoClusterHttp.js +78 -0
  10. package/dist/DenoClusterHttp.js.map +1 -0
  11. package/dist/DenoClusterSocket.d.ts +65 -0
  12. package/dist/DenoClusterSocket.d.ts.map +1 -0
  13. package/dist/DenoClusterSocket.js +101 -0
  14. package/dist/DenoClusterSocket.js.map +1 -0
  15. package/dist/DenoCrypto.d.ts +25 -0
  16. package/dist/DenoCrypto.d.ts.map +1 -0
  17. package/dist/DenoCrypto.js +52 -0
  18. package/dist/DenoCrypto.js.map +1 -0
  19. package/dist/DenoFileSystem.d.ts +10 -0
  20. package/dist/DenoFileSystem.d.ts.map +1 -0
  21. package/dist/DenoFileSystem.js +331 -0
  22. package/dist/DenoFileSystem.js.map +1 -0
  23. package/dist/DenoHttpClient.d.ts +9 -0
  24. package/dist/DenoHttpClient.d.ts.map +1 -0
  25. package/dist/DenoHttpClient.js +9 -0
  26. package/dist/DenoHttpClient.js.map +1 -0
  27. package/dist/DenoHttpPlatform.d.ts +34 -0
  28. package/dist/DenoHttpPlatform.d.ts.map +1 -0
  29. package/dist/DenoHttpPlatform.js +88 -0
  30. package/dist/DenoHttpPlatform.js.map +1 -0
  31. package/dist/DenoHttpServer.d.ts +99 -0
  32. package/dist/DenoHttpServer.d.ts.map +1 -0
  33. package/dist/DenoHttpServer.js +353 -0
  34. package/dist/DenoHttpServer.js.map +1 -0
  35. package/dist/DenoHttpServerRequest.d.ts +18 -0
  36. package/dist/DenoHttpServerRequest.d.ts.map +1 -0
  37. package/dist/DenoHttpServerRequest.js +8 -0
  38. package/dist/DenoHttpServerRequest.js.map +1 -0
  39. package/dist/DenoKeyValueStore.d.ts +22 -0
  40. package/dist/DenoKeyValueStore.d.ts.map +1 -0
  41. package/dist/DenoKeyValueStore.js +21 -0
  42. package/dist/DenoKeyValueStore.js.map +1 -0
  43. package/dist/DenoMultipart.d.ts +32 -0
  44. package/dist/DenoMultipart.d.ts.map +1 -0
  45. package/dist/DenoMultipart.js +25 -0
  46. package/dist/DenoMultipart.js.map +1 -0
  47. package/dist/DenoPath.d.ts +43 -0
  48. package/dist/DenoPath.d.ts.map +1 -0
  49. package/dist/DenoPath.js +81 -0
  50. package/dist/DenoPath.js.map +1 -0
  51. package/dist/DenoRedis.d.ts +60 -0
  52. package/dist/DenoRedis.d.ts.map +1 -0
  53. package/dist/DenoRedis.js +76 -0
  54. package/dist/DenoRedis.js.map +1 -0
  55. package/dist/DenoRuntime.d.ts +36 -0
  56. package/dist/DenoRuntime.d.ts.map +1 -0
  57. package/dist/DenoRuntime.js +35 -0
  58. package/dist/DenoRuntime.js.map +1 -0
  59. package/dist/DenoServices.d.ts +34 -0
  60. package/dist/DenoServices.d.ts.map +1 -0
  61. package/dist/DenoServices.js +16 -0
  62. package/dist/DenoServices.js.map +1 -0
  63. package/dist/DenoSocket.d.ts +99 -0
  64. package/dist/DenoSocket.d.ts.map +1 -0
  65. package/dist/DenoSocket.js +192 -0
  66. package/dist/DenoSocket.js.map +1 -0
  67. package/dist/DenoSocketServer.d.ts +70 -0
  68. package/dist/DenoSocketServer.d.ts.map +1 -0
  69. package/dist/DenoSocketServer.js +68 -0
  70. package/dist/DenoSocketServer.js.map +1 -0
  71. package/dist/DenoStdio.d.ts +11 -0
  72. package/dist/DenoStdio.d.ts.map +1 -0
  73. package/dist/DenoStdio.js +39 -0
  74. package/dist/DenoStdio.js.map +1 -0
  75. package/dist/DenoTerminal.d.ts +21 -0
  76. package/dist/DenoTerminal.d.ts.map +1 -0
  77. package/dist/DenoTerminal.js +27 -0
  78. package/dist/DenoTerminal.js.map +1 -0
  79. package/dist/DenoWorker.d.ts +17 -0
  80. package/dist/DenoWorker.d.ts.map +1 -0
  81. package/dist/DenoWorker.js +54 -0
  82. package/dist/DenoWorker.js.map +1 -0
  83. package/dist/DenoWorkerRunner.d.ts +24 -0
  84. package/dist/DenoWorkerRunner.d.ts.map +1 -0
  85. package/dist/DenoWorkerRunner.js +150 -0
  86. package/dist/DenoWorkerRunner.js.map +1 -0
  87. package/dist/index.d.ts +88 -0
  88. package/dist/index.d.ts.map +1 -0
  89. package/dist/index.js +89 -0
  90. package/dist/index.js.map +1 -0
  91. package/dist/internal/denoSocketServer.d.ts +7 -0
  92. package/dist/internal/denoSocketServer.d.ts.map +1 -0
  93. package/dist/internal/denoSocketServer.js +55 -0
  94. package/dist/internal/denoSocketServer.js.map +1 -0
  95. package/dist/internal/error.d.ts +4 -0
  96. package/dist/internal/error.d.ts.map +1 -0
  97. package/dist/internal/error.js +46 -0
  98. package/dist/internal/error.js.map +1 -0
  99. package/package.json +72 -0
  100. package/src/DenoChildProcessSpawner.ts +439 -0
  101. package/src/DenoClusterHttp.ts +156 -0
  102. package/src/DenoClusterSocket.ts +197 -0
  103. package/src/DenoCrypto.ts +63 -0
  104. package/src/DenoFileSystem.ts +497 -0
  105. package/src/DenoHttpClient.ts +9 -0
  106. package/src/DenoHttpPlatform.ts +93 -0
  107. package/src/DenoHttpServer.ts +535 -0
  108. package/src/DenoHttpServerRequest.ts +18 -0
  109. package/src/DenoKeyValueStore.ts +28 -0
  110. package/src/DenoMultipart.ts +53 -0
  111. package/src/DenoPath.ts +102 -0
  112. package/src/DenoRedis.ts +113 -0
  113. package/src/DenoRuntime.ts +73 -0
  114. package/src/DenoServices.ts +49 -0
  115. package/src/DenoSocket.ts +318 -0
  116. package/src/DenoSocketServer.ts +120 -0
  117. package/src/DenoStdio.ts +51 -0
  118. package/src/DenoTerminal.ts +32 -0
  119. package/src/DenoWorker.ts +75 -0
  120. package/src/DenoWorkerRunner.ts +175 -0
  121. package/src/index.ts +110 -0
  122. package/src/internal/denoSocketServer.ts +89 -0
  123. package/src/internal/error.ts +63 -0
@@ -0,0 +1,439 @@
1
+ /**
2
+ * Deno implementation of the child process spawner service.
3
+ *
4
+ * This module uses `Deno.Command` and Web Streams directly. Deno cannot create
5
+ * detached process groups, so killing a handle terminates only its direct child;
6
+ * descendants spawned by that child are left running.
7
+ *
8
+ * @since 4.0.0
9
+ */
10
+ import type * as Arr from "effect/Array"
11
+ import * as Deferred from "effect/Deferred"
12
+ import * as Effect from "effect/Effect"
13
+ import * as Exit from "effect/Exit"
14
+ import * as Layer from "effect/Layer"
15
+ import * as Path from "effect/Path"
16
+ import * as PlatformError from "effect/PlatformError"
17
+ import * as Predicate from "effect/Predicate"
18
+ import type * as Scope from "effect/Scope"
19
+ import * as Sink from "effect/Sink"
20
+ import * as Stream from "effect/Stream"
21
+ import * as ChildProcess from "effect/unstable/process/ChildProcess"
22
+ import type { ChildProcessHandle } from "effect/unstable/process/ChildProcessSpawner"
23
+ import {
24
+ ChildProcessSpawner,
25
+ ExitCode,
26
+ make as makeSpawner,
27
+ makeHandle,
28
+ ProcessId
29
+ } from "effect/unstable/process/ChildProcessSpawner"
30
+
31
+ const commandString = (command: ChildProcess.Command): string => {
32
+ const { commands } = flattenCommand(command)
33
+ return commands.map((command) => `${command.command} ${command.args.join(" ")}`).join(" | ")
34
+ }
35
+
36
+ const toPlatformError = (
37
+ method: string,
38
+ cause: unknown,
39
+ command: ChildProcess.Command
40
+ ): PlatformError.PlatformError => {
41
+ const errorName = Predicate.hasProperty(cause, "name") ? cause.name : undefined
42
+ const tag = errorName === "NotFound" ?
43
+ "NotFound" :
44
+ errorName === "PermissionDenied" ?
45
+ "PermissionDenied" :
46
+ errorName === "TimedOut"
47
+ ? "TimedOut"
48
+ : "Unknown"
49
+ return PlatformError.systemError({
50
+ _tag: tag,
51
+ module: "ChildProcess",
52
+ method,
53
+ pathOrDescriptor: commandString(command),
54
+ syscall: `${method} ${commandString(command).trim()}`,
55
+ cause
56
+ })
57
+ }
58
+
59
+ const unsupported = (option: "additionalFds" | "detached") =>
60
+ PlatformError.badArgument({
61
+ module: "ChildProcessSpawner",
62
+ method: "spawn",
63
+ description: `The ${option} option is unsupported because Deno has no equivalent`
64
+ })
65
+
66
+ const make = Effect.gen(function*() {
67
+ const path = yield* Path.Path
68
+
69
+ const resolveWorkingDirectory = (options: ChildProcess.CommandOptions) =>
70
+ options.cwd === undefined ? undefined : path.resolve(options.cwd)
71
+
72
+ const resolveEnvironment = (options: ChildProcess.CommandOptions) => {
73
+ if (options.env === undefined) return undefined
74
+ const env: Record<string, string> = {}
75
+ for (const [key, value] of Object.entries(options.env)) {
76
+ if (value !== undefined) env[key] = value
77
+ }
78
+ return env
79
+ }
80
+
81
+ const resolveStdinOption = (options: ChildProcess.CommandOptions): ChildProcess.StdinConfig => {
82
+ const defaultConfig: ChildProcess.StdinConfig = { stream: "pipe", encoding: "utf-8", endOnDone: true }
83
+ if (options.stdin === undefined) {
84
+ return defaultConfig
85
+ }
86
+ if (typeof options.stdin === "string") {
87
+ return { ...defaultConfig, stream: options.stdin }
88
+ }
89
+ if (Stream.isStream(options.stdin)) {
90
+ return { ...defaultConfig, stream: options.stdin }
91
+ }
92
+ return {
93
+ stream: options.stdin.stream,
94
+ encoding: options.stdin.encoding ?? defaultConfig.encoding,
95
+ endOnDone: options.stdin.endOnDone ?? defaultConfig.endOnDone
96
+ }
97
+ }
98
+
99
+ const resolveOutputOption = (
100
+ options: ChildProcess.CommandOptions,
101
+ streamName: "stdout" | "stderr"
102
+ ): ChildProcess.StdoutConfig => {
103
+ const option = options[streamName]
104
+ if (option === undefined) return { stream: "pipe" }
105
+ if (typeof option === "string" || Sink.isSink(option)) return { stream: option }
106
+ return { stream: option.stream }
107
+ }
108
+
109
+ const inputToStdioOption = (
110
+ input: ChildProcess.CommandInput
111
+ ): "piped" | "inherit" | "null" =>
112
+ Stream.isStream(input) || input === "pipe" || input === "overlapped" ?
113
+ "piped" :
114
+ input === "ignore"
115
+ ? "null"
116
+ : "inherit"
117
+
118
+ const outputToStdioOption = (
119
+ output: ChildProcess.CommandOutput | undefined
120
+ ): "piped" | "inherit" | "null" =>
121
+ Sink.isSink(output) || output === "pipe" || output === "overlapped" || output === undefined ?
122
+ "piped" :
123
+ output === "ignore"
124
+ ? "null"
125
+ : "inherit"
126
+
127
+ const setupChildStdin = (
128
+ command: ChildProcess.StandardCommand,
129
+ childProcess: Deno.ChildProcess,
130
+ config: ChildProcess.StdinConfig
131
+ ) =>
132
+ Effect.suspend(() => {
133
+ if (inputToStdioOption(config.stream) !== "piped") return Effect.succeed(Sink.drain)
134
+ const sink = Sink.fromWritableStream({
135
+ evaluate: () => childProcess.stdin,
136
+ onError: (cause) => toPlatformError("fromWritable(stdin)", cause, command),
137
+ closeOnDone: config.endOnDone
138
+ })
139
+ return Stream.isStream(config.stream)
140
+ ? Effect.as(Effect.forkScoped(Stream.run(config.stream, sink)), sink)
141
+ : Effect.succeed(sink)
142
+ })
143
+
144
+ const setupChildOutputStreams = (
145
+ command: ChildProcess.StandardCommand,
146
+ childProcess: Deno.ChildProcess,
147
+ stdoutConfig: ChildProcess.StdoutConfig,
148
+ stderrConfig: ChildProcess.StderrConfig
149
+ ) => {
150
+ let stdout: Stream.Stream<Uint8Array, PlatformError.PlatformError> =
151
+ outputToStdioOption(stdoutConfig.stream) === "piped"
152
+ ? Stream.fromReadableStream({
153
+ evaluate: () => childProcess.stdout,
154
+ onError: (cause) => toPlatformError("fromReadable(stdout)", cause, command)
155
+ })
156
+ : Stream.empty
157
+ let stderr: Stream.Stream<Uint8Array, PlatformError.PlatformError> =
158
+ outputToStdioOption(stderrConfig.stream) === "piped"
159
+ ? Stream.fromReadableStream({
160
+ evaluate: () => childProcess.stderr,
161
+ onError: (cause) => toPlatformError("fromReadable(stderr)", cause, command)
162
+ })
163
+ : Stream.empty
164
+
165
+ if (Sink.isSink(stdoutConfig.stream)) stdout = Stream.transduce(stdout, stdoutConfig.stream)
166
+ if (Sink.isSink(stderrConfig.stream)) stderr = Stream.transduce(stderr, stderrConfig.stream)
167
+
168
+ return { stdout, stderr, all: Stream.merge(stdout, stderr) }
169
+ }
170
+
171
+ const spawn = (
172
+ command: ChildProcess.StandardCommand,
173
+ executable: string,
174
+ args: ReadonlyArray<string>,
175
+ options: Deno.CommandOptions
176
+ ) =>
177
+ Effect.try({
178
+ try: () => {
179
+ const childProcess = new Deno.Command(executable, { ...options, args: [...args] }).spawn()
180
+ const exitSignal = Deferred.makeUnsafe<Deno.CommandStatus, PlatformError.PlatformError>()
181
+ childProcess.status.then(
182
+ (status) => Deferred.doneUnsafe(exitSignal, Exit.succeed(status)),
183
+ (cause) => Deferred.doneUnsafe(exitSignal, Exit.fail(toPlatformError("status", cause, command)))
184
+ )
185
+ return [childProcess, exitSignal] as const
186
+ },
187
+ catch: (cause) => toPlatformError("spawn", cause, command)
188
+ })
189
+
190
+ const killProcess = (
191
+ command: ChildProcess.StandardCommand,
192
+ childProcess: Deno.ChildProcess,
193
+ signal: ChildProcess.Signal
194
+ ) =>
195
+ Effect.try({
196
+ try: () => childProcess.kill(signal as Deno.Signal),
197
+ catch: (cause) => toPlatformError("kill", cause, command)
198
+ })
199
+
200
+ const withTimeout = (
201
+ childProcess: Deno.ChildProcess,
202
+ command: ChildProcess.StandardCommand,
203
+ options: ChildProcess.KillOptions | undefined
204
+ ) =>
205
+ <A, E, R>(
206
+ kill: (
207
+ command: ChildProcess.StandardCommand,
208
+ childProcess: Deno.ChildProcess,
209
+ signal: ChildProcess.Signal
210
+ ) => Effect.Effect<A, E, R>
211
+ ) => {
212
+ const killSignal = options?.killSignal ?? "SIGTERM"
213
+ return options?.forceKillAfter === undefined
214
+ ? kill(command, childProcess, killSignal)
215
+ : Effect.timeoutOrElse(kill(command, childProcess, killSignal), {
216
+ duration: options.forceKillAfter,
217
+ orElse: () => kill(command, childProcess, "SIGKILL")
218
+ })
219
+ }
220
+
221
+ const getSourceStream = (
222
+ handle: ChildProcessHandle,
223
+ from: "stdout" | "stderr" | "all"
224
+ ): Stream.Stream<Uint8Array, PlatformError.PlatformError> => {
225
+ switch (from) {
226
+ case "stdout":
227
+ return handle.stdout
228
+ case "stderr":
229
+ return handle.stderr
230
+ case "all":
231
+ return handle.all
232
+ }
233
+ }
234
+
235
+ const spawnCommand: (
236
+ command: ChildProcess.Command
237
+ ) => Effect.Effect<ChildProcessHandle, PlatformError.PlatformError, Scope.Scope> = Effect.fnUntraced(function*(cmd) {
238
+ switch (cmd._tag) {
239
+ case "StandardCommand": {
240
+ if (cmd.options.additionalFds !== undefined) {
241
+ return yield* Effect.fail(unsupported("additionalFds"))
242
+ }
243
+ if (cmd.options.detached !== undefined) {
244
+ return yield* Effect.fail(unsupported("detached"))
245
+ }
246
+
247
+ const stdinConfig = resolveStdinOption(cmd.options)
248
+ const stdoutConfig = resolveOutputOption(cmd.options, "stdout")
249
+ const stderrConfig = resolveOutputOption(cmd.options, "stderr")
250
+ let isReferenced = true
251
+
252
+ let executable = cmd.command
253
+ let args = cmd.args
254
+ let windowsRawArguments = false
255
+ if (cmd.options.shell) {
256
+ const shellCommand = `${cmd.command} ${cmd.args.join(" ")}`
257
+ if (Deno.build.os === "windows") {
258
+ executable = typeof cmd.options.shell === "string" ? cmd.options.shell : "cmd.exe"
259
+ args = ["/d", "/s", "/c", shellCommand]
260
+ windowsRawArguments = true
261
+ } else {
262
+ executable = typeof cmd.options.shell === "string" ? cmd.options.shell : "/bin/sh"
263
+ args = ["-c", shellCommand]
264
+ }
265
+ }
266
+
267
+ const cwd = resolveWorkingDirectory(cmd.options)
268
+ const env = resolveEnvironment(cmd.options)
269
+ const [childProcess, exitSignal] = yield* Effect.acquireRelease(
270
+ spawn(cmd, executable, args, {
271
+ ...(cwd === undefined ? undefined : { cwd }),
272
+ ...(env === undefined ? undefined : { env }),
273
+ clearEnv: cmd.options.env !== undefined && cmd.options.extendEnv !== true,
274
+ stdin: inputToStdioOption(stdinConfig.stream),
275
+ stdout: outputToStdioOption(stdoutConfig.stream),
276
+ stderr: outputToStdioOption(stderrConfig.stream),
277
+ windowsRawArguments
278
+ }),
279
+ Effect.fnUntraced(function*([childProcess, exitSignal]) {
280
+ const exited = yield* Deferred.isDone(exitSignal)
281
+ if (exited || !isReferenced) return
282
+ const killWithTimeout = withTimeout(childProcess, cmd, cmd.options)
283
+ yield* killWithTimeout((command, childProcess, signal) =>
284
+ killProcess(command, childProcess, signal).pipe(
285
+ Effect.andThen(Deferred.await(exitSignal))
286
+ )
287
+ ).pipe(Effect.ignore)
288
+ })
289
+ )
290
+
291
+ const reref = Effect.sync(() => {
292
+ if (!isReferenced) {
293
+ childProcess.ref()
294
+ isReferenced = true
295
+ }
296
+ })
297
+ const unref = Effect.sync(() => {
298
+ if (isReferenced) {
299
+ childProcess.unref()
300
+ isReferenced = false
301
+ }
302
+ return reref
303
+ })
304
+ const stdin = yield* setupChildStdin(cmd, childProcess, stdinConfig)
305
+ const { all, stderr, stdout } = setupChildOutputStreams(cmd, childProcess, stdoutConfig, stderrConfig)
306
+ const isRunning = Effect.map(Deferred.isDone(exitSignal), (done) => !done)
307
+ const exitCode = Effect.flatMap(Deferred.await(exitSignal), (status) =>
308
+ Predicate.isNull(status.signal)
309
+ ? Effect.succeed(ExitCode(status.code))
310
+ : Effect.fail(toPlatformError(
311
+ "exitCode",
312
+ new globalThis.Error(`Process interrupted due to receipt of signal: '${status.signal}'`),
313
+ cmd
314
+ )))
315
+ const kill = (options?: ChildProcess.KillOptions | undefined) => {
316
+ const killWithTimeout = withTimeout(childProcess, cmd, options)
317
+ return killWithTimeout((command, childProcess, signal) =>
318
+ killProcess(command, childProcess, signal).pipe(
319
+ Effect.andThen(Deferred.await(exitSignal))
320
+ )
321
+ ).pipe(Effect.asVoid)
322
+ }
323
+
324
+ return makeHandle({
325
+ pid: ProcessId(childProcess.pid),
326
+ exitCode,
327
+ isRunning,
328
+ kill,
329
+ stdin,
330
+ stdout,
331
+ stderr,
332
+ all,
333
+ getInputFd: () => Sink.drain,
334
+ getOutputFd: () => Stream.empty,
335
+ unref
336
+ })
337
+ }
338
+ case "PipedCommand": {
339
+ const { commands, pipeOptions } = flattenCommand(cmd)
340
+ const [root, ...pipeline] = commands
341
+ const handles = [yield* spawnCommand(root)]
342
+
343
+ for (let i = 0; i < pipeline.length; i++) {
344
+ const command = pipeline[i]
345
+ const options = pipeOptions[i] ?? {}
346
+ const stdinConfig = resolveStdinOption(command.options)
347
+ const from = options.from ?? "stdout"
348
+
349
+ if (
350
+ (from !== "stdout" && from !== "stderr" && from !== "all") ||
351
+ (options.to ?? "stdin") !== "stdin"
352
+ ) {
353
+ return yield* Effect.fail(unsupported("additionalFds"))
354
+ }
355
+ const sourceStream = getSourceStream(handles[handles.length - 1], from)
356
+ handles.push(
357
+ yield* spawnCommand(ChildProcess.make(command.command, command.args, {
358
+ ...command.options,
359
+ stdin: { ...stdinConfig, stream: sourceStream }
360
+ }))
361
+ )
362
+ }
363
+
364
+ const handle = handles[handles.length - 1]
365
+ const kill = (options?: ChildProcess.KillOptions | undefined) =>
366
+ Effect.forEach([...handles].reverse(), (handle) => Effect.ignore(handle.kill(options)), { discard: true })
367
+ const unref = Effect.gen(function*() {
368
+ const rerefs: Array<Effect.Effect<void, PlatformError.PlatformError>> = []
369
+ for (const handle of handles) rerefs.push(yield* handle.unref)
370
+ return Effect.forEach([...rerefs].reverse(), (reref) => reref, { discard: true })
371
+ })
372
+
373
+ return makeHandle({
374
+ pid: handle.pid,
375
+ exitCode: handle.exitCode,
376
+ isRunning: handle.isRunning,
377
+ kill,
378
+ stdin: handle.stdin,
379
+ stdout: handle.stdout,
380
+ stderr: handle.stderr,
381
+ all: handle.all,
382
+ getInputFd: handle.getInputFd,
383
+ getOutputFd: handle.getOutputFd,
384
+ unref
385
+ })
386
+ }
387
+ }
388
+ })
389
+
390
+ return makeSpawner(spawnCommand)
391
+ })
392
+
393
+ /**
394
+ * Layer that provides the Deno child process spawner.
395
+ *
396
+ * @category layers
397
+ * @since 4.0.0
398
+ */
399
+ export const layer: Layer.Layer<ChildProcessSpawner, never, Path.Path> = Layer.effect(ChildProcessSpawner, make)
400
+
401
+ /**
402
+ * Result of flattening a pipeline of commands.
403
+ *
404
+ * @category models
405
+ * @since 4.0.0
406
+ */
407
+ export interface FlattenedPipeline {
408
+ readonly commands: Arr.NonEmptyReadonlyArray<ChildProcess.StandardCommand>
409
+ readonly pipeOptions: ReadonlyArray<ChildProcess.PipeOptions>
410
+ }
411
+
412
+ /**
413
+ * Flattens a command into standard commands and their pipe options.
414
+ *
415
+ * @category transforming
416
+ * @since 4.0.0
417
+ */
418
+ export const flattenCommand = (command: ChildProcess.Command): FlattenedPipeline => {
419
+ const commands: Array<ChildProcess.StandardCommand> = []
420
+ const pipeOptions: Array<ChildProcess.PipeOptions> = []
421
+
422
+ const flatten = (command: ChildProcess.Command): void => {
423
+ switch (command._tag) {
424
+ case "StandardCommand":
425
+ commands.push(command)
426
+ break
427
+ case "PipedCommand":
428
+ flatten(command.left)
429
+ pipeOptions.push(command.options)
430
+ flatten(command.right)
431
+ break
432
+ }
433
+ }
434
+ flatten(command)
435
+
436
+ const [first, ...rest] = commands
437
+ if (first === undefined) throw new Error("flattenCommand produced empty commands array")
438
+ return { commands: [first, ...rest], pipeOptions }
439
+ }
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Native Deno HTTP and WebSocket layers for Effect Cluster runners.
3
+ *
4
+ * `layerHttpServer` provides the Deno HTTP server used by cluster runners. The
5
+ * main `layer` builds a sharding layer for HTTP or WebSocket transport,
6
+ * choosing serialization, runner health checks, runner storage, message
7
+ * storage, and optional client-only mode from the supplied options.
8
+ *
9
+ * @since 4.0.0
10
+ */
11
+ import type * as Config from "effect/Config"
12
+ import * as Effect from "effect/Effect"
13
+ import * as Layer from "effect/Layer"
14
+ import * as Option from "effect/Option"
15
+ import * as HttpRunner from "effect/unstable/cluster/HttpRunner"
16
+ import * as MessageStorage from "effect/unstable/cluster/MessageStorage"
17
+ import * as RunnerHealth from "effect/unstable/cluster/RunnerHealth"
18
+ import * as Runners from "effect/unstable/cluster/Runners"
19
+ import * as RunnerStorage from "effect/unstable/cluster/RunnerStorage"
20
+ import type { Sharding } from "effect/unstable/cluster/Sharding"
21
+ import * as ShardingConfig from "effect/unstable/cluster/ShardingConfig"
22
+ import * as SqlMessageStorage from "effect/unstable/cluster/SqlMessageStorage"
23
+ import * as SqlRunnerStorage from "effect/unstable/cluster/SqlRunnerStorage"
24
+ import type * as Etag from "effect/unstable/http/Etag"
25
+ import type { HttpPlatform } from "effect/unstable/http/HttpPlatform"
26
+ import type { HttpServer } from "effect/unstable/http/HttpServer"
27
+ import type { ServeError } from "effect/unstable/http/HttpServerError"
28
+ import * as RpcSerialization from "effect/unstable/rpc/RpcSerialization"
29
+ import type { SqlClient } from "effect/unstable/sql/SqlClient"
30
+ import { layerK8sHttpClient } from "./DenoClusterSocket.ts"
31
+ import * as DenoCrypto from "./DenoCrypto.ts"
32
+ import * as DenoHttpClient from "./DenoHttpClient.ts"
33
+ import * as DenoHttpServer from "./DenoHttpServer.ts"
34
+ import type { DenoServices } from "./DenoServices.ts"
35
+ import * as DenoSocket from "./DenoSocket.ts"
36
+
37
+ export {
38
+ /**
39
+ * Layer that provides a Kubernetes HTTP client for runner health checks.
40
+ *
41
+ * @category re-exports
42
+ * @since 4.0.0
43
+ */
44
+ layerK8sHttpClient
45
+ }
46
+
47
+ /**
48
+ * Layer that provides a native Deno HTTP server for cluster runners.
49
+ *
50
+ * @category layers
51
+ * @since 4.0.0
52
+ */
53
+ export const layerHttpServer: Layer.Layer<
54
+ | HttpPlatform
55
+ | Etag.Generator
56
+ | DenoServices
57
+ | HttpServer,
58
+ ServeError,
59
+ ShardingConfig.ShardingConfig
60
+ > = Effect.gen(function*() {
61
+ const config = yield* ShardingConfig.ShardingConfig
62
+ const listenAddress = Option.orElse(config.runnerListenAddress, () => config.runnerAddress)
63
+ if (Option.isNone(listenAddress)) {
64
+ return yield* Effect.die("DenoClusterHttp.layerHttpServer: ShardingConfig.runnerAddress is None")
65
+ }
66
+ return DenoHttpServer.layer({
67
+ hostname: listenAddress.value.host,
68
+ port: listenAddress.value.port,
69
+ onListen: () => {}
70
+ })
71
+ }).pipe(Layer.unwrap)
72
+
73
+ /**
74
+ * Creates Deno cluster layers for HTTP or WebSocket transport, configuring
75
+ * serialization, storage, runner health, and optional client-only mode.
76
+ *
77
+ * @category layers
78
+ * @since 4.0.0
79
+ */
80
+ export const layer = <
81
+ const ClientOnly extends boolean = false,
82
+ const Storage extends "local" | "sql" | "byo" = never
83
+ >(options: {
84
+ readonly transport: "http" | "websocket"
85
+ readonly serialization?: "msgpack" | "ndjson" | undefined
86
+ readonly serializationMaxBufferSize?: number | "unbounded" | undefined
87
+ readonly clientOnly?: ClientOnly | undefined
88
+ readonly storage?: Storage | undefined
89
+ readonly runnerHealth?: "ping" | "k8s" | undefined
90
+ readonly runnerHealthK8s?: {
91
+ readonly namespace?: string | undefined
92
+ readonly labelSelector?: string | undefined
93
+ } | undefined
94
+ readonly shardingConfig?: Partial<ShardingConfig.ShardingConfig["Service"]> | undefined
95
+ }): ClientOnly extends true ? Layer.Layer<
96
+ Sharding | Runners.Runners | ("byo" extends Storage ? never : MessageStorage.MessageStorage),
97
+ Config.ConfigError,
98
+ "local" extends Storage ? never
99
+ : "byo" extends Storage ? (MessageStorage.MessageStorage | RunnerStorage.RunnerStorage)
100
+ : SqlClient
101
+ > :
102
+ Layer.Layer<
103
+ Sharding | Runners.Runners | ("byo" extends Storage ? never : MessageStorage.MessageStorage),
104
+ ServeError | Config.ConfigError,
105
+ "local" extends Storage ? never
106
+ : "byo" extends Storage ? (MessageStorage.MessageStorage | RunnerStorage.RunnerStorage)
107
+ : SqlClient
108
+ > =>
109
+ {
110
+ const layer: Layer.Layer<any, any, any> = options.clientOnly
111
+ ? options.transport === "http"
112
+ ? Layer.provide(HttpRunner.layerHttpClientOnly, DenoHttpClient.layer)
113
+ : Layer.provide(HttpRunner.layerWebsocketClientOnly, DenoSocket.layerWebSocketConstructor)
114
+ : options.transport === "http"
115
+ ? Layer.provide(HttpRunner.layerHttp, [layerHttpServer, DenoHttpClient.layer])
116
+ : Layer.provide(HttpRunner.layerWebsocket, [layerHttpServer, DenoSocket.layerWebSocketConstructor])
117
+
118
+ const runnerHealth: Layer.Layer<any, any, any> = options.clientOnly
119
+ ? Layer.empty as any
120
+ : options.runnerHealth === "k8s"
121
+ ? RunnerHealth.layerK8s(options.runnerHealthK8s).pipe(
122
+ Layer.provide(layerK8sHttpClient)
123
+ )
124
+ : RunnerHealth.layerPing.pipe(
125
+ Layer.provide(Runners.layerRpc),
126
+ Layer.provide(
127
+ options.transport === "http"
128
+ ? HttpRunner.layerClientProtocolHttpDefault.pipe(Layer.provide(DenoHttpClient.layer))
129
+ : HttpRunner.layerClientProtocolWebsocketDefault.pipe(Layer.provide(DenoSocket.layerWebSocketConstructor))
130
+ )
131
+ )
132
+
133
+ return layer.pipe(
134
+ Layer.provide(runnerHealth),
135
+ Layer.provideMerge(
136
+ options.storage === "local"
137
+ ? MessageStorage.layerNoop
138
+ : options.storage === "byo"
139
+ ? Layer.empty
140
+ : Layer.orDie(SqlMessageStorage.layer).pipe(Layer.provide(DenoCrypto.layer))
141
+ ),
142
+ Layer.provide(
143
+ options.storage === "local"
144
+ ? RunnerStorage.layerMemory
145
+ : options.storage === "byo"
146
+ ? Layer.empty
147
+ : Layer.orDie(SqlRunnerStorage.layer)
148
+ ),
149
+ Layer.provide(ShardingConfig.layerFromEnv(options.shardingConfig)),
150
+ Layer.provide(
151
+ options.serialization === "ndjson"
152
+ ? RpcSerialization.layerNdjsonWith({ maxBufferSize: options.serializationMaxBufferSize })
153
+ : RpcSerialization.layerMsgPackWith({ maxBufferSize: options.serializationMaxBufferSize })
154
+ )
155
+ ) as any
156
+ }