@zyro-sec/util 1.0.2 → 1.0.3
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.
- package/docs/layer-node.md +155 -155
- package/package.json +1 -1
- package/script/build.ts +9 -9
- package/script/pack.ts +5 -5
- package/src/abort.ts +9 -9
- package/src/activity-calendar.ts +50 -50
- package/src/artifact.ts +95 -95
- package/src/base64.ts +29 -29
- package/src/binary.ts +30 -30
- package/src/bom.ts +46 -46
- package/src/cross-spawn-spawner.ts +694 -694
- package/src/duration.ts +22 -22
- package/src/effect/app-node-platform.ts +40 -40
- package/src/effect/app-node.ts +14 -14
- package/src/effect/layer-node.ts +360 -360
- package/src/effect-flock.ts +313 -313
- package/src/encode.ts +39 -39
- package/src/execute-output.ts +23 -23
- package/src/flock.ts +346 -346
- package/src/fs-util.ts +271 -271
- package/src/glob.ts +38 -38
- package/src/global-roots.ts +19 -19
- package/src/global-roots.workerd.ts +16 -16
- package/src/global.ts +88 -88
- package/src/hash.ts +11 -11
- package/src/heap-snapshot.ts +44 -44
- package/src/no-timeout-fetch.ts +5 -5
- package/src/npm-config.ts +41 -41
- package/src/npm.ts +557 -557
- package/src/observability/logging.ts +179 -179
- package/src/observability/otlp.ts +109 -109
- package/src/observability/shared.ts +8 -8
- package/src/observability.ts +60 -60
- package/src/open.ts +12 -12
- package/src/patch.ts +382 -382
- package/src/path.ts +149 -149
- package/src/process-signal.ts +23 -23
- package/src/process.ts +270 -270
- package/src/qr.ts +323 -323
- package/src/record.ts +3 -3
- package/src/retry.ts +51 -51
- package/src/runtime/import.bun.ts +10 -10
- package/src/runtime/import.node.ts +61 -61
- package/src/runtime/import.workerd.ts +9 -9
- package/src/runtime-import.ts +1 -1
- package/src/session-mention.ts +24 -24
- package/src/session-title-fallback.ts +36 -36
- package/src/session-turn.ts +82 -82
- package/src/teardown.ts +19 -19
- package/src/text-boundary.ts +36 -36
- package/src/uuid.ts +15 -15
- package/test/fixtures/qr.json +625 -625
- package/tsconfig.build.json +12 -12
- package/tsconfig.dist.json +10 -10
- package/tsconfig.json +14 -14
- package/src/abort.test.ts +0 -16
- package/src/activity-calendar.test.ts +0 -136
- package/src/artifact.test.ts +0 -29
- package/src/base64.test.ts +0 -12
- package/src/binary.test.ts +0 -19
- package/src/bom.test.ts +0 -21
- package/src/encode.test.ts +0 -32
- package/src/execute-output.test.ts +0 -36
- package/src/no-timeout-fetch.test.ts +0 -33
- package/src/open.test.ts +0 -16
- package/src/path.test.ts +0 -89
- package/src/process-signal.test.ts +0 -38
- package/src/retry.test.ts +0 -87
- package/src/session-mention.test.ts +0 -19
- package/src/session-turn.test.ts +0 -131
- package/src/text-boundary.test.ts +0 -39
- package/src/uuid.test.ts +0 -80
- package/test/glob.test.ts +0 -11
- package/test/global-roots.test.ts +0 -62
- package/test/global.test.ts +0 -99
- package/test/observability-otlp.test.ts +0 -45
- package/test/qr.test.ts +0 -113
- package/test/session-title-fallback.test.ts +0 -49
package/src/process.ts
CHANGED
|
@@ -1,270 +1,270 @@
|
|
|
1
|
-
import { Context, Duration, Effect, Fiber, Layer, Schema, Stream } from "effect"
|
|
2
|
-
import type { PlatformError } from "effect/PlatformError"
|
|
3
|
-
import { ChildProcess } from "effect/process"
|
|
4
|
-
import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"
|
|
5
|
-
import { CrossSpawnSpawner } from "./cross-spawn-spawner.js"
|
|
6
|
-
import { makeGlobalNode } from "./effect/app-node.js"
|
|
7
|
-
|
|
8
|
-
export class AppProcessError extends Schema.TaggedError<AppProcessError>()("AppProcessError", {
|
|
9
|
-
command: Schema.String,
|
|
10
|
-
exitCode: Schema.optional(Schema.Number),
|
|
11
|
-
stderr: Schema.optional(Schema.String),
|
|
12
|
-
cause: Schema.optional(Schema.Defect()),
|
|
13
|
-
}) {
|
|
14
|
-
override get message() {
|
|
15
|
-
const detail =
|
|
16
|
-
this.stderr?.trim() || (this.cause instanceof Error ? this.cause.message : this.cause && String(this.cause))
|
|
17
|
-
const status = this.exitCode === undefined ? "" : ` (exit ${this.exitCode})`
|
|
18
|
-
return `Command failed${status}: ${this.command}${detail ? `: ${detail}` : ""}`
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface RunOptions {
|
|
23
|
-
readonly combineOutput?: boolean
|
|
24
|
-
readonly maxOutputBytes?: number
|
|
25
|
-
readonly maxErrorBytes?: number
|
|
26
|
-
readonly signal?: AbortSignal
|
|
27
|
-
readonly timeout?: Duration.Input
|
|
28
|
-
readonly stdin?: string | Uint8Array | Stream.Stream<Uint8Array, PlatformError>
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface RunStreamOptions {
|
|
32
|
-
readonly signal?: AbortSignal
|
|
33
|
-
readonly includeStderr?: boolean
|
|
34
|
-
readonly okExitCodes?: ReadonlyArray<number>
|
|
35
|
-
readonly maxErrorBytes?: number
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface RunResult {
|
|
39
|
-
readonly command: string
|
|
40
|
-
readonly exitCode: number
|
|
41
|
-
readonly output?: Buffer
|
|
42
|
-
readonly stdout: Buffer
|
|
43
|
-
readonly stderr: Buffer
|
|
44
|
-
readonly outputTruncated?: boolean
|
|
45
|
-
readonly stdoutTruncated: boolean
|
|
46
|
-
readonly stderrTruncated: boolean
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export type Interface = ChildProcessSpawner["Service"] & {
|
|
50
|
-
readonly run: (command: ChildProcess.Command, options?: RunOptions) => Effect.Effect<RunResult, AppProcessError>
|
|
51
|
-
readonly runStream: (
|
|
52
|
-
command: ChildProcess.Command,
|
|
53
|
-
options?: RunStreamOptions,
|
|
54
|
-
) => Stream.Stream<string, AppProcessError>
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export class Service extends Context.Service<Service, Interface>()("@zyro-sec/AppProcess") {}
|
|
58
|
-
|
|
59
|
-
export const requireSuccess = (result: RunResult): Effect.Effect<RunResult, AppProcessError> =>
|
|
60
|
-
result.exitCode === 0
|
|
61
|
-
? Effect.succeed(result)
|
|
62
|
-
: Effect.fail(
|
|
63
|
-
new AppProcessError({
|
|
64
|
-
command: result.command,
|
|
65
|
-
exitCode: result.exitCode,
|
|
66
|
-
stderr: result.stderr.toString("utf8"),
|
|
67
|
-
}),
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
export const requireExitIn =
|
|
71
|
-
(codes: ReadonlyArray<number>) =>
|
|
72
|
-
(result: RunResult): Effect.Effect<RunResult, AppProcessError> =>
|
|
73
|
-
codes.includes(result.exitCode)
|
|
74
|
-
? Effect.succeed(result)
|
|
75
|
-
: Effect.fail(
|
|
76
|
-
new AppProcessError({
|
|
77
|
-
command: result.command,
|
|
78
|
-
exitCode: result.exitCode,
|
|
79
|
-
stderr: result.stderr.toString("utf8"),
|
|
80
|
-
}),
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
const describeCommand = (command: ChildProcess.Command): string => {
|
|
84
|
-
if (command._tag === "StandardCommand") {
|
|
85
|
-
return command.args.length ? `${command.command} ${command.args.join(" ")}` : command.command
|
|
86
|
-
}
|
|
87
|
-
return `${describeCommand(command.left)} | ${describeCommand(command.right)}`
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const wrapError = (description: string, cause: unknown): AppProcessError =>
|
|
91
|
-
cause instanceof AppProcessError ? cause : new AppProcessError({ command: description, cause })
|
|
92
|
-
|
|
93
|
-
export const abortError = (signal: AbortSignal): Error => {
|
|
94
|
-
const reason = signal.reason
|
|
95
|
-
if (reason instanceof Error) return reason
|
|
96
|
-
const err = new Error("Aborted")
|
|
97
|
-
err.name = "AbortError"
|
|
98
|
-
return err
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export const waitForAbort = (signal: AbortSignal) =>
|
|
102
|
-
Effect.callback<never, Error>((resume) => {
|
|
103
|
-
if (signal.aborted) {
|
|
104
|
-
resume(Effect.fail(abortError(signal)))
|
|
105
|
-
return
|
|
106
|
-
}
|
|
107
|
-
const onabort = () => resume(Effect.fail(abortError(signal)))
|
|
108
|
-
signal.addEventListener("abort", onabort, { once: true })
|
|
109
|
-
return Effect.sync(() => signal.removeEventListener("abort", onabort))
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
const normalizeStdin = (
|
|
113
|
-
input: string | Uint8Array | Stream.Stream<Uint8Array, PlatformError>,
|
|
114
|
-
): Stream.Stream<Uint8Array, PlatformError> =>
|
|
115
|
-
typeof input === "string"
|
|
116
|
-
? Stream.make(new TextEncoder().encode(input))
|
|
117
|
-
: input instanceof Uint8Array
|
|
118
|
-
? Stream.make(input)
|
|
119
|
-
: input
|
|
120
|
-
|
|
121
|
-
export const collectStream = (stream: Stream.Stream<Uint8Array, PlatformError>, maxOutputBytes: number | undefined) =>
|
|
122
|
-
Stream.runFold(
|
|
123
|
-
stream,
|
|
124
|
-
() => ({ chunks: [] as Uint8Array[], bytes: 0, truncated: false }),
|
|
125
|
-
(acc, chunk) => {
|
|
126
|
-
if (maxOutputBytes === undefined) {
|
|
127
|
-
acc.chunks.push(chunk)
|
|
128
|
-
acc.bytes += chunk.length
|
|
129
|
-
return acc
|
|
130
|
-
}
|
|
131
|
-
const remaining = maxOutputBytes - acc.bytes
|
|
132
|
-
if (remaining > 0) acc.chunks.push(remaining >= chunk.length ? chunk : chunk.slice(0, remaining))
|
|
133
|
-
acc.bytes += chunk.length
|
|
134
|
-
acc.truncated = acc.truncated || acc.bytes > maxOutputBytes
|
|
135
|
-
return acc
|
|
136
|
-
},
|
|
137
|
-
).pipe(Effect.map((x) => ({ buffer: Buffer.concat(x.chunks), truncated: x.truncated })))
|
|
138
|
-
|
|
139
|
-
const layer = Layer.effect(
|
|
140
|
-
Service,
|
|
141
|
-
Effect.gen(function* () {
|
|
142
|
-
const spawner = yield* ChildProcessSpawner
|
|
143
|
-
|
|
144
|
-
const runCommand = (command: ChildProcess.Command, options?: RunOptions) => {
|
|
145
|
-
const description = describeCommand(command)
|
|
146
|
-
const collect = Effect.scoped(
|
|
147
|
-
Effect.gen(function* () {
|
|
148
|
-
const handle = yield* spawner.spawn(command)
|
|
149
|
-
if (options?.combineOutput) {
|
|
150
|
-
const [output, exitCode] = yield* Effect.all(
|
|
151
|
-
[collectStream(handle.all, options.maxOutputBytes), handle.exitCode],
|
|
152
|
-
{ concurrency: "unbounded" },
|
|
153
|
-
)
|
|
154
|
-
return {
|
|
155
|
-
command: description,
|
|
156
|
-
exitCode,
|
|
157
|
-
output: output.buffer,
|
|
158
|
-
stdout: Buffer.alloc(0),
|
|
159
|
-
stderr: Buffer.alloc(0),
|
|
160
|
-
outputTruncated: output.truncated,
|
|
161
|
-
stdoutTruncated: false,
|
|
162
|
-
stderrTruncated: false,
|
|
163
|
-
} satisfies RunResult
|
|
164
|
-
}
|
|
165
|
-
const [stdout, stderr, exitCode] = yield* Effect.all(
|
|
166
|
-
[
|
|
167
|
-
collectStream(handle.stdout, options?.maxOutputBytes),
|
|
168
|
-
collectStream(handle.stderr, options?.maxErrorBytes),
|
|
169
|
-
handle.exitCode,
|
|
170
|
-
],
|
|
171
|
-
{ concurrency: "unbounded" },
|
|
172
|
-
)
|
|
173
|
-
return {
|
|
174
|
-
command: description,
|
|
175
|
-
exitCode,
|
|
176
|
-
stdout: stdout.buffer,
|
|
177
|
-
stderr: stderr.buffer,
|
|
178
|
-
stdoutTruncated: stdout.truncated,
|
|
179
|
-
stderrTruncated: stderr.truncated,
|
|
180
|
-
} satisfies RunResult
|
|
181
|
-
}),
|
|
182
|
-
)
|
|
183
|
-
const timed = options?.timeout
|
|
184
|
-
? Effect.timeoutOrElse(collect, {
|
|
185
|
-
duration: options.timeout,
|
|
186
|
-
orElse: () => Effect.fail(new AppProcessError({ command: description, cause: new Error("Timed out") })),
|
|
187
|
-
})
|
|
188
|
-
: collect
|
|
189
|
-
const aborted = options?.signal
|
|
190
|
-
? timed.pipe(
|
|
191
|
-
Effect.raceFirst(
|
|
192
|
-
waitForAbort(options.signal).pipe(Effect.mapError((cause) => wrapError(description, cause))),
|
|
193
|
-
),
|
|
194
|
-
)
|
|
195
|
-
: timed
|
|
196
|
-
return aborted.pipe(Effect.mapError((cause) => wrapError(description, cause)))
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const run = Effect.fn("AppProcess.run")(function* (command: ChildProcess.Command, options?: RunOptions) {
|
|
200
|
-
if (options?.stdin === undefined) return yield* runCommand(command, options)
|
|
201
|
-
if (command._tag !== "StandardCommand") {
|
|
202
|
-
return yield* new AppProcessError({
|
|
203
|
-
command: describeCommand(command),
|
|
204
|
-
cause: new Error("stdin option only supports StandardCommand; received PipedCommand"),
|
|
205
|
-
})
|
|
206
|
-
}
|
|
207
|
-
const next = ChildProcess.make(command.command, command.args, {
|
|
208
|
-
...command.options,
|
|
209
|
-
stdin: normalizeStdin(options.stdin),
|
|
210
|
-
})
|
|
211
|
-
return yield* runCommand(next, options)
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
const runStream = (
|
|
215
|
-
command: ChildProcess.Command,
|
|
216
|
-
options?: RunStreamOptions,
|
|
217
|
-
): Stream.Stream<string, AppProcessError> => {
|
|
218
|
-
const description = describeCommand(command)
|
|
219
|
-
const okExitCodes = options?.okExitCodes
|
|
220
|
-
const built: Stream.Stream<string, AppProcessError | PlatformError> = Stream.unwrap(
|
|
221
|
-
Effect.gen(function* () {
|
|
222
|
-
const handle = yield* spawner.spawn(command)
|
|
223
|
-
const streams =
|
|
224
|
-
options?.includeStderr === true
|
|
225
|
-
? yield* handle.stderr.pipe(
|
|
226
|
-
Stream.broadcastN({ n: 2, capacity: 16 }),
|
|
227
|
-
Effect.map((copies) => ({
|
|
228
|
-
source: Stream.merge(handle.stdout, copies[0]),
|
|
229
|
-
diagnostics: copies[1],
|
|
230
|
-
})),
|
|
231
|
-
)
|
|
232
|
-
: { source: handle.stdout, diagnostics: handle.stderr }
|
|
233
|
-
const stderrFiber = yield* Effect.forkScoped(
|
|
234
|
-
collectStream(streams.diagnostics, options?.maxErrorBytes).pipe(
|
|
235
|
-
Effect.map((x) => x.buffer.toString("utf8")),
|
|
236
|
-
),
|
|
237
|
-
)
|
|
238
|
-
const lines = streams.source.pipe(
|
|
239
|
-
Stream.decodeText,
|
|
240
|
-
Stream.splitLines,
|
|
241
|
-
Stream.filter((line) => line.length > 0),
|
|
242
|
-
)
|
|
243
|
-
const tail = Stream.unwrap(
|
|
244
|
-
Effect.gen(function* () {
|
|
245
|
-
const code = yield* handle.exitCode
|
|
246
|
-
if (okExitCodes && okExitCodes.length > 0 && !okExitCodes.includes(code)) {
|
|
247
|
-
const stderr = yield* Fiber.join(stderrFiber)
|
|
248
|
-
return Stream.fail(new AppProcessError({ command: description, exitCode: code, stderr }))
|
|
249
|
-
}
|
|
250
|
-
return Stream.empty
|
|
251
|
-
}),
|
|
252
|
-
)
|
|
253
|
-
return Stream.concat(lines, tail) as Stream.Stream<string, AppProcessError | PlatformError>
|
|
254
|
-
}),
|
|
255
|
-
)
|
|
256
|
-
const mapped = built.pipe(Stream.mapError((cause) => wrapError(description, cause)))
|
|
257
|
-
if (!options?.signal) return mapped
|
|
258
|
-
const signal = options.signal
|
|
259
|
-
return mapped.pipe(
|
|
260
|
-
Stream.interruptWhen(waitForAbort(signal).pipe(Effect.mapError((cause) => wrapError(description, cause)))),
|
|
261
|
-
)
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return Service.of({ ...spawner, run, runStream })
|
|
265
|
-
}),
|
|
266
|
-
)
|
|
267
|
-
|
|
268
|
-
export const node = makeGlobalNode({ service: Service, layer: layer, deps: [CrossSpawnSpawner.node] })
|
|
269
|
-
|
|
270
|
-
export * as AppProcess from "./process.js"
|
|
1
|
+
import { Context, Duration, Effect, Fiber, Layer, Schema, Stream } from "effect"
|
|
2
|
+
import type { PlatformError } from "effect/PlatformError"
|
|
3
|
+
import { ChildProcess } from "effect/process"
|
|
4
|
+
import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"
|
|
5
|
+
import { CrossSpawnSpawner } from "./cross-spawn-spawner.js"
|
|
6
|
+
import { makeGlobalNode } from "./effect/app-node.js"
|
|
7
|
+
|
|
8
|
+
export class AppProcessError extends Schema.TaggedError<AppProcessError>()("AppProcessError", {
|
|
9
|
+
command: Schema.String,
|
|
10
|
+
exitCode: Schema.optional(Schema.Number),
|
|
11
|
+
stderr: Schema.optional(Schema.String),
|
|
12
|
+
cause: Schema.optional(Schema.Defect()),
|
|
13
|
+
}) {
|
|
14
|
+
override get message() {
|
|
15
|
+
const detail =
|
|
16
|
+
this.stderr?.trim() || (this.cause instanceof Error ? this.cause.message : this.cause && String(this.cause))
|
|
17
|
+
const status = this.exitCode === undefined ? "" : ` (exit ${this.exitCode})`
|
|
18
|
+
return `Command failed${status}: ${this.command}${detail ? `: ${detail}` : ""}`
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RunOptions {
|
|
23
|
+
readonly combineOutput?: boolean
|
|
24
|
+
readonly maxOutputBytes?: number
|
|
25
|
+
readonly maxErrorBytes?: number
|
|
26
|
+
readonly signal?: AbortSignal
|
|
27
|
+
readonly timeout?: Duration.Input
|
|
28
|
+
readonly stdin?: string | Uint8Array | Stream.Stream<Uint8Array, PlatformError>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface RunStreamOptions {
|
|
32
|
+
readonly signal?: AbortSignal
|
|
33
|
+
readonly includeStderr?: boolean
|
|
34
|
+
readonly okExitCodes?: ReadonlyArray<number>
|
|
35
|
+
readonly maxErrorBytes?: number
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface RunResult {
|
|
39
|
+
readonly command: string
|
|
40
|
+
readonly exitCode: number
|
|
41
|
+
readonly output?: Buffer
|
|
42
|
+
readonly stdout: Buffer
|
|
43
|
+
readonly stderr: Buffer
|
|
44
|
+
readonly outputTruncated?: boolean
|
|
45
|
+
readonly stdoutTruncated: boolean
|
|
46
|
+
readonly stderrTruncated: boolean
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type Interface = ChildProcessSpawner["Service"] & {
|
|
50
|
+
readonly run: (command: ChildProcess.Command, options?: RunOptions) => Effect.Effect<RunResult, AppProcessError>
|
|
51
|
+
readonly runStream: (
|
|
52
|
+
command: ChildProcess.Command,
|
|
53
|
+
options?: RunStreamOptions,
|
|
54
|
+
) => Stream.Stream<string, AppProcessError>
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class Service extends Context.Service<Service, Interface>()("@zyro-sec/AppProcess") {}
|
|
58
|
+
|
|
59
|
+
export const requireSuccess = (result: RunResult): Effect.Effect<RunResult, AppProcessError> =>
|
|
60
|
+
result.exitCode === 0
|
|
61
|
+
? Effect.succeed(result)
|
|
62
|
+
: Effect.fail(
|
|
63
|
+
new AppProcessError({
|
|
64
|
+
command: result.command,
|
|
65
|
+
exitCode: result.exitCode,
|
|
66
|
+
stderr: result.stderr.toString("utf8"),
|
|
67
|
+
}),
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
export const requireExitIn =
|
|
71
|
+
(codes: ReadonlyArray<number>) =>
|
|
72
|
+
(result: RunResult): Effect.Effect<RunResult, AppProcessError> =>
|
|
73
|
+
codes.includes(result.exitCode)
|
|
74
|
+
? Effect.succeed(result)
|
|
75
|
+
: Effect.fail(
|
|
76
|
+
new AppProcessError({
|
|
77
|
+
command: result.command,
|
|
78
|
+
exitCode: result.exitCode,
|
|
79
|
+
stderr: result.stderr.toString("utf8"),
|
|
80
|
+
}),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
const describeCommand = (command: ChildProcess.Command): string => {
|
|
84
|
+
if (command._tag === "StandardCommand") {
|
|
85
|
+
return command.args.length ? `${command.command} ${command.args.join(" ")}` : command.command
|
|
86
|
+
}
|
|
87
|
+
return `${describeCommand(command.left)} | ${describeCommand(command.right)}`
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const wrapError = (description: string, cause: unknown): AppProcessError =>
|
|
91
|
+
cause instanceof AppProcessError ? cause : new AppProcessError({ command: description, cause })
|
|
92
|
+
|
|
93
|
+
export const abortError = (signal: AbortSignal): Error => {
|
|
94
|
+
const reason = signal.reason
|
|
95
|
+
if (reason instanceof Error) return reason
|
|
96
|
+
const err = new Error("Aborted")
|
|
97
|
+
err.name = "AbortError"
|
|
98
|
+
return err
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export const waitForAbort = (signal: AbortSignal) =>
|
|
102
|
+
Effect.callback<never, Error>((resume) => {
|
|
103
|
+
if (signal.aborted) {
|
|
104
|
+
resume(Effect.fail(abortError(signal)))
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
const onabort = () => resume(Effect.fail(abortError(signal)))
|
|
108
|
+
signal.addEventListener("abort", onabort, { once: true })
|
|
109
|
+
return Effect.sync(() => signal.removeEventListener("abort", onabort))
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
const normalizeStdin = (
|
|
113
|
+
input: string | Uint8Array | Stream.Stream<Uint8Array, PlatformError>,
|
|
114
|
+
): Stream.Stream<Uint8Array, PlatformError> =>
|
|
115
|
+
typeof input === "string"
|
|
116
|
+
? Stream.make(new TextEncoder().encode(input))
|
|
117
|
+
: input instanceof Uint8Array
|
|
118
|
+
? Stream.make(input)
|
|
119
|
+
: input
|
|
120
|
+
|
|
121
|
+
export const collectStream = (stream: Stream.Stream<Uint8Array, PlatformError>, maxOutputBytes: number | undefined) =>
|
|
122
|
+
Stream.runFold(
|
|
123
|
+
stream,
|
|
124
|
+
() => ({ chunks: [] as Uint8Array[], bytes: 0, truncated: false }),
|
|
125
|
+
(acc, chunk) => {
|
|
126
|
+
if (maxOutputBytes === undefined) {
|
|
127
|
+
acc.chunks.push(chunk)
|
|
128
|
+
acc.bytes += chunk.length
|
|
129
|
+
return acc
|
|
130
|
+
}
|
|
131
|
+
const remaining = maxOutputBytes - acc.bytes
|
|
132
|
+
if (remaining > 0) acc.chunks.push(remaining >= chunk.length ? chunk : chunk.slice(0, remaining))
|
|
133
|
+
acc.bytes += chunk.length
|
|
134
|
+
acc.truncated = acc.truncated || acc.bytes > maxOutputBytes
|
|
135
|
+
return acc
|
|
136
|
+
},
|
|
137
|
+
).pipe(Effect.map((x) => ({ buffer: Buffer.concat(x.chunks), truncated: x.truncated })))
|
|
138
|
+
|
|
139
|
+
const layer = Layer.effect(
|
|
140
|
+
Service,
|
|
141
|
+
Effect.gen(function* () {
|
|
142
|
+
const spawner = yield* ChildProcessSpawner
|
|
143
|
+
|
|
144
|
+
const runCommand = (command: ChildProcess.Command, options?: RunOptions) => {
|
|
145
|
+
const description = describeCommand(command)
|
|
146
|
+
const collect = Effect.scoped(
|
|
147
|
+
Effect.gen(function* () {
|
|
148
|
+
const handle = yield* spawner.spawn(command)
|
|
149
|
+
if (options?.combineOutput) {
|
|
150
|
+
const [output, exitCode] = yield* Effect.all(
|
|
151
|
+
[collectStream(handle.all, options.maxOutputBytes), handle.exitCode],
|
|
152
|
+
{ concurrency: "unbounded" },
|
|
153
|
+
)
|
|
154
|
+
return {
|
|
155
|
+
command: description,
|
|
156
|
+
exitCode,
|
|
157
|
+
output: output.buffer,
|
|
158
|
+
stdout: Buffer.alloc(0),
|
|
159
|
+
stderr: Buffer.alloc(0),
|
|
160
|
+
outputTruncated: output.truncated,
|
|
161
|
+
stdoutTruncated: false,
|
|
162
|
+
stderrTruncated: false,
|
|
163
|
+
} satisfies RunResult
|
|
164
|
+
}
|
|
165
|
+
const [stdout, stderr, exitCode] = yield* Effect.all(
|
|
166
|
+
[
|
|
167
|
+
collectStream(handle.stdout, options?.maxOutputBytes),
|
|
168
|
+
collectStream(handle.stderr, options?.maxErrorBytes),
|
|
169
|
+
handle.exitCode,
|
|
170
|
+
],
|
|
171
|
+
{ concurrency: "unbounded" },
|
|
172
|
+
)
|
|
173
|
+
return {
|
|
174
|
+
command: description,
|
|
175
|
+
exitCode,
|
|
176
|
+
stdout: stdout.buffer,
|
|
177
|
+
stderr: stderr.buffer,
|
|
178
|
+
stdoutTruncated: stdout.truncated,
|
|
179
|
+
stderrTruncated: stderr.truncated,
|
|
180
|
+
} satisfies RunResult
|
|
181
|
+
}),
|
|
182
|
+
)
|
|
183
|
+
const timed = options?.timeout
|
|
184
|
+
? Effect.timeoutOrElse(collect, {
|
|
185
|
+
duration: options.timeout,
|
|
186
|
+
orElse: () => Effect.fail(new AppProcessError({ command: description, cause: new Error("Timed out") })),
|
|
187
|
+
})
|
|
188
|
+
: collect
|
|
189
|
+
const aborted = options?.signal
|
|
190
|
+
? timed.pipe(
|
|
191
|
+
Effect.raceFirst(
|
|
192
|
+
waitForAbort(options.signal).pipe(Effect.mapError((cause) => wrapError(description, cause))),
|
|
193
|
+
),
|
|
194
|
+
)
|
|
195
|
+
: timed
|
|
196
|
+
return aborted.pipe(Effect.mapError((cause) => wrapError(description, cause)))
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const run = Effect.fn("AppProcess.run")(function* (command: ChildProcess.Command, options?: RunOptions) {
|
|
200
|
+
if (options?.stdin === undefined) return yield* runCommand(command, options)
|
|
201
|
+
if (command._tag !== "StandardCommand") {
|
|
202
|
+
return yield* new AppProcessError({
|
|
203
|
+
command: describeCommand(command),
|
|
204
|
+
cause: new Error("stdin option only supports StandardCommand; received PipedCommand"),
|
|
205
|
+
})
|
|
206
|
+
}
|
|
207
|
+
const next = ChildProcess.make(command.command, command.args, {
|
|
208
|
+
...command.options,
|
|
209
|
+
stdin: normalizeStdin(options.stdin),
|
|
210
|
+
})
|
|
211
|
+
return yield* runCommand(next, options)
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
const runStream = (
|
|
215
|
+
command: ChildProcess.Command,
|
|
216
|
+
options?: RunStreamOptions,
|
|
217
|
+
): Stream.Stream<string, AppProcessError> => {
|
|
218
|
+
const description = describeCommand(command)
|
|
219
|
+
const okExitCodes = options?.okExitCodes
|
|
220
|
+
const built: Stream.Stream<string, AppProcessError | PlatformError> = Stream.unwrap(
|
|
221
|
+
Effect.gen(function* () {
|
|
222
|
+
const handle = yield* spawner.spawn(command)
|
|
223
|
+
const streams =
|
|
224
|
+
options?.includeStderr === true
|
|
225
|
+
? yield* handle.stderr.pipe(
|
|
226
|
+
Stream.broadcastN({ n: 2, capacity: 16 }),
|
|
227
|
+
Effect.map((copies) => ({
|
|
228
|
+
source: Stream.merge(handle.stdout, copies[0]),
|
|
229
|
+
diagnostics: copies[1],
|
|
230
|
+
})),
|
|
231
|
+
)
|
|
232
|
+
: { source: handle.stdout, diagnostics: handle.stderr }
|
|
233
|
+
const stderrFiber = yield* Effect.forkScoped(
|
|
234
|
+
collectStream(streams.diagnostics, options?.maxErrorBytes).pipe(
|
|
235
|
+
Effect.map((x) => x.buffer.toString("utf8")),
|
|
236
|
+
),
|
|
237
|
+
)
|
|
238
|
+
const lines = streams.source.pipe(
|
|
239
|
+
Stream.decodeText,
|
|
240
|
+
Stream.splitLines,
|
|
241
|
+
Stream.filter((line) => line.length > 0),
|
|
242
|
+
)
|
|
243
|
+
const tail = Stream.unwrap(
|
|
244
|
+
Effect.gen(function* () {
|
|
245
|
+
const code = yield* handle.exitCode
|
|
246
|
+
if (okExitCodes && okExitCodes.length > 0 && !okExitCodes.includes(code)) {
|
|
247
|
+
const stderr = yield* Fiber.join(stderrFiber)
|
|
248
|
+
return Stream.fail(new AppProcessError({ command: description, exitCode: code, stderr }))
|
|
249
|
+
}
|
|
250
|
+
return Stream.empty
|
|
251
|
+
}),
|
|
252
|
+
)
|
|
253
|
+
return Stream.concat(lines, tail) as Stream.Stream<string, AppProcessError | PlatformError>
|
|
254
|
+
}),
|
|
255
|
+
)
|
|
256
|
+
const mapped = built.pipe(Stream.mapError((cause) => wrapError(description, cause)))
|
|
257
|
+
if (!options?.signal) return mapped
|
|
258
|
+
const signal = options.signal
|
|
259
|
+
return mapped.pipe(
|
|
260
|
+
Stream.interruptWhen(waitForAbort(signal).pipe(Effect.mapError((cause) => wrapError(description, cause)))),
|
|
261
|
+
)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return Service.of({ ...spawner, run, runStream })
|
|
265
|
+
}),
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
export const node = makeGlobalNode({ service: Service, layer: layer, deps: [CrossSpawnSpawner.node] })
|
|
269
|
+
|
|
270
|
+
export * as AppProcess from "./process.js"
|