| ... | ... | @@ -45,23 +45,29 @@ export interface ProcessRunOutput { |
| 45 | 45 | |
| 46 | 46 | const ProcessInvocationFields = { |
| 47 | 47 | command: Schema.String, |
| 48 | | args: Schema.Array(Schema.String), |
| 48 | argumentCount: Schema.Number, |
| 49 | 49 | cwd: Schema.optional(Schema.String), |
| 50 | spawnCwd: Schema.optional(Schema.String), |
| 50 | 51 | }; |
| 51 | 52 | |
| 52 | 53 | const formatProcessInvocation = (input: { |
| 53 | 54 | readonly command: string; |
| 54 | | readonly args: ReadonlyArray<string>; |
| 55 | 55 | readonly cwd?: string | undefined; |
| 56 | readonly spawnCwd?: string | undefined; |
| 56 | 57 | }): string => { |
| 57 | | const command = [input.command, ...input.args].join(" "); |
| 58 | | return input.cwd === undefined ? `'${command}'` : `'${command}' in '${input.cwd}'`; |
| 58 | const executionCwd = input.spawnCwd ?? input.cwd; |
| 59 | return executionCwd === undefined |
| 60 | ? `'${input.command}'` |
| 61 | : `'${input.command}' in '${executionCwd}'`; |
| 59 | 62 | }; |
| 60 | 63 | |
| 61 | 64 | export class ProcessSpawnError extends Schema.TaggedErrorClass<ProcessSpawnError>()( |
| 62 | 65 | "ProcessSpawnError", |
| 63 | 66 | { |
| 64 | 67 | ...ProcessInvocationFields, |
| 68 | resolvedCommand: Schema.optional(Schema.String), |
| 69 | resolvedArgumentCount: Schema.optional(Schema.Number), |
| 70 | shell: Schema.optional(Schema.Boolean), |
| 65 | 71 | cause: Schema.Defect(), |
| 66 | 72 | }, |
| 67 | 73 | ) { |
| ... | ... | @@ -74,6 +80,7 @@ export class ProcessStdinError extends Schema.TaggedErrorClass<ProcessStdinError |
| 74 | 80 | "ProcessStdinError", |
| 75 | 81 | { |
| 76 | 82 | ...ProcessInvocationFields, |
| 83 | stdinBytes: Schema.Number, |
| 77 | 84 | cause: Schema.Defect(), |
| 78 | 85 | }, |
| 79 | 86 | ) { |
| ... | ... | @@ -88,10 +95,11 @@ export class ProcessOutputLimitError extends Schema.TaggedErrorClass<ProcessOutp |
| 88 | 95 | ...ProcessInvocationFields, |
| 89 | 96 | stream: Schema.Literals(["stdout", "stderr"]), |
| 90 | 97 | maxBytes: Schema.Number, |
| 98 | observedBytes: Schema.Number, |
| 91 | 99 | }, |
| 92 | 100 | ) { |
| 93 | 101 | override get message(): string { |
| 94 | | return `Process ${formatProcessInvocation(this)} ${this.stream} exceeded ${this.maxBytes} bytes`; |
| 102 | return `Process ${formatProcessInvocation(this)} ${this.stream} produced ${this.observedBytes} bytes, exceeding the ${this.maxBytes} byte limit`; |
| 95 | 103 | } |
| 96 | 104 | } |
| 97 | 105 | |
| ... | ... | @@ -120,12 +128,14 @@ export class ProcessTimeoutError extends Schema.TaggedErrorClass<ProcessTimeoutE |
| 120 | 128 | } |
| 121 | 129 | } |
| 122 | 130 | |
| 123 | | export type ProcessRunError = |
| 124 | | | ProcessSpawnError |
| 125 | | | ProcessStdinError |
| 126 | | | ProcessOutputLimitError |
| 127 | | | ProcessReadError |
| 128 | | | ProcessTimeoutError; |
| 131 | export const ProcessRunError = Schema.Union([ |
| 132 | ProcessSpawnError, |
| 133 | ProcessStdinError, |
| 134 | ProcessOutputLimitError, |
| 135 | ProcessReadError, |
| 136 | ProcessTimeoutError, |
| 137 | ]); |
| 138 | export type ProcessRunError = typeof ProcessRunError.Type; |
| 129 | 139 | |
| 130 | 140 | export class ProcessRunner extends Context.Service< |
| 131 | 141 | ProcessRunner, |
| ... | ... | @@ -163,6 +173,7 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: { |
| 163 | 173 | readonly command: string; |
| 164 | 174 | readonly args: ReadonlyArray<string>; |
| 165 | 175 | readonly cwd?: string | undefined; |
| 176 | readonly spawnCwd?: string | undefined; |
| 166 | 177 | readonly streamName: "stdout" | "stderr"; |
| 167 | 178 | readonly stream: Stream.Stream<Uint8Array, PlatformError.PlatformError>; |
| 168 | 179 | readonly maxOutputBytes: number; |
| ... | ... | @@ -174,8 +185,9 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: { |
| 174 | 185 | (cause) => |
| 175 | 186 | new ProcessReadError({ |
| 176 | 187 | command: input.command, |
| 177 | | args: input.args, |
| 188 | argumentCount: input.args.length, |
| 178 | 189 | cwd: input.cwd, |
| 190 | spawnCwd: input.spawnCwd, |
| 179 | 191 | stream: input.streamName, |
| 180 | 192 | cause, |
| 181 | 193 | }), |
| ... | ... | @@ -203,14 +215,16 @@ const collectText = Effect.fn("processRunner.collectText")(function* (input: { |
| 203 | 215 | () => ({ chunks: [], bytes: 0 }), |
| 204 | 216 | (state, chunk) => { |
| 205 | 217 | const remainingBytes = input.maxOutputBytes - state.bytes; |
| 206 | | if (remainingBytes <= 0 || chunk.byteLength > remainingBytes) { |
| 218 | if (chunk.byteLength > remainingBytes) { |
| 207 | 219 | return Effect.fail( |
| 208 | 220 | new ProcessOutputLimitError({ |
| 209 | 221 | command: input.command, |
| 210 | | args: input.args, |
| 222 | argumentCount: input.args.length, |
| 211 | 223 | cwd: input.cwd, |
| 224 | spawnCwd: input.spawnCwd, |
| 212 | 225 | stream: input.streamName, |
| 213 | 226 | maxBytes: input.maxOutputBytes, |
| 227 | observedBytes: state.bytes + chunk.byteLength, |
| 214 | 228 | }), |
| 215 | 229 | ); |
| 216 | 230 | } |
| ... | ... | @@ -259,8 +273,9 @@ function finalizeRunProcess<R>( |
| 259 | 273 | return Effect.fail( |
| 260 | 274 | new ProcessTimeoutError({ |
| 261 | 275 | command: input.command, |
| 262 | | args: input.args, |
| 276 | argumentCount: input.args.length, |
| 263 | 277 | cwd: input.cwd, |
| 278 | spawnCwd: input.spawnCwd, |
| 264 | 279 | timeoutMs: Duration.toMillis(timeout), |
| 265 | 280 | }), |
| 266 | 281 | ); |
| ... | ... | @@ -300,23 +315,30 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( |
| 300 | 315 | (cause) => |
| 301 | 316 | new ProcessSpawnError({ |
| 302 | 317 | command: input.command, |
| 303 | | args: input.args, |
| 318 | argumentCount: input.args.length, |
| 304 | 319 | cwd: input.cwd, |
| 320 | spawnCwd: input.spawnCwd, |
| 321 | resolvedCommand: spawnCommand.command, |
| 322 | resolvedArgumentCount: spawnCommand.args.length, |
| 323 | shell: spawnCommand.shell, |
| 305 | 324 | cause, |
| 306 | 325 | }), |
| 307 | 326 | ), |
| 308 | 327 | ); |
| 309 | 328 | |
| 329 | const stdin = input.stdin; |
| 310 | 330 | const writeStdin = |
| 311 | | input.stdin === undefined |
| 331 | stdin === undefined |
| 312 | 332 | ? Effect.void |
| 313 | | : Stream.run(Stream.encodeText(Stream.make(input.stdin)), child.stdin).pipe( |
| 333 | : Stream.run(Stream.encodeText(Stream.make(stdin)), child.stdin).pipe( |
| 314 | 334 | Effect.mapError( |
| 315 | 335 | (cause) => |
| 316 | 336 | new ProcessStdinError({ |
| 317 | 337 | command: input.command, |
| 318 | | args: input.args, |
| 338 | argumentCount: input.args.length, |
| 319 | 339 | cwd: input.cwd, |
| 340 | spawnCwd: input.spawnCwd, |
| 341 | stdinBytes: Buffer.byteLength(stdin), |
| 320 | 342 | cause, |
| 321 | 343 | }), |
| 322 | 344 | ), |
| ... | ... | @@ -328,6 +350,7 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( |
| 328 | 350 | command: input.command, |
| 329 | 351 | args: input.args, |
| 330 | 352 | cwd: input.cwd, |
| 353 | spawnCwd: input.spawnCwd, |
| 331 | 354 | streamName: "stdout", |
| 332 | 355 | stream: child.stdout, |
| 333 | 356 | maxOutputBytes, |
| ... | ... | @@ -338,6 +361,7 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( |
| 338 | 361 | command: input.command, |
| 339 | 362 | args: input.args, |
| 340 | 363 | cwd: input.cwd, |
| 364 | spawnCwd: input.spawnCwd, |
| 341 | 365 | streamName: "stderr", |
| 342 | 366 | stream: child.stderr, |
| 343 | 367 | maxOutputBytes, |
| ... | ... | @@ -354,8 +378,9 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( |
| 354 | 378 | (cause) => |
| 355 | 379 | new ProcessReadError({ |
| 356 | 380 | command: input.command, |
| 357 | | args: input.args, |
| 381 | argumentCount: input.args.length, |
| 358 | 382 | cwd: input.cwd, |
| 383 | spawnCwd: input.spawnCwd, |
| 359 | 384 | stream: "exitCode", |
| 360 | 385 | cause, |
| 361 | 386 | }), |