-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathrun.node.ts
180 lines (142 loc) · 4.24 KB
/
run.node.ts
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
namespace $ {
export type $mol_run_error_context = {
pid?: number
stdout: Buffer | string
stderr: Buffer | string
}
export class $mol_run_error extends $mol_error_mix<{
timeout_kill?: boolean
pid?: number
signal?: NodeJS.Signals | null
status?: number | null
command: string
dir: string
}> {}
export const $mol_run_spawn = (
...args: Parameters<typeof $node['child_process']['spawn']>
) => $node['child_process'].spawn(...args)
export const $mol_run_spawn_sync = (
...args: Parameters<typeof $node['child_process']['spawnSync']>
) => $node['child_process'].spawnSync(...args)
export type $mol_run_options = {
command : readonly string[] | string
dir : string
timeout?: number
env?: Record<string, string | undefined>
}
export class $mol_run extends $mol_object {
static async_enabled() {
return Boolean(this.$.$mol_env()['MOL_RUN_ASYNC'])
}
static spawn(options: $mol_run_options) {
const sync = ! this.async_enabled() || ! Boolean($mol_wire_auto())
const env = options.env ?? this.$.$mol_env()
return $mol_wire_sync(this).spawn_async( { ...options, sync, env } )
}
static spawn_async(
{ dir, sync, timeout, command, env }: $mol_run_options & { sync?: boolean }
) {
const args_raw = typeof command === 'string' ? command.split( ' ' ) : command
const [ app, ...args ] = args_raw
const opts = { shell: true, cwd: dir, env }
const log_object = {
place: `${this}.spawn()`,
message: 'Run',
command: args_raw.join(' ') ,
dir: $node.path.relative( '' , dir ) ,
}
if (sync) {
this.$.$mol_log3_come({
hint: 'Run inside fiber',
...log_object
})
let error: Error | undefined
let res
try {
res = this.$.$mol_run_spawn_sync(app, args, opts)
error = res.error
} catch (err) {
error = err as Error
}
if (! res || error || res.status) {
throw new $mol_run_error(
this.error_message(res),
{ ...log_object, status: res?.status, signal: res?.signal },
...(error ? [error] : [])
)
}
return res
}
let sub
try {
sub = this.$.$mol_run_spawn(app, args, {
...opts,
stdio: [ 'pipe', 'inherit', 'inherit' ],
})
} catch (error) {
throw new $mol_run_error(
this.error_message(undefined),
log_object,
error as Error
)
}
const pid = sub.pid ?? 0
this.$.$mol_log3_come({
...log_object,
pid,
})
let timeout_kill = false
let timer: undefined | ReturnType<typeof setTimeout>
const std_data = [] as Buffer[]
const error_data = [] as Buffer[]
const add = (std_chunk?: Buffer, error_chunk?: Buffer) => {
if (std_chunk) std_data.push(std_chunk)
if (error_chunk) error_data.push(error_chunk)
if (! timeout) return
clearTimeout(timer)
timer = setTimeout(() => {
const signal = timeout_kill ? 'SIGKILL' : 'SIGTERM'
timeout_kill = true
add()
sub.kill(signal)
}, timeout)
}
add()
sub.stdout?.on('data', data => add(data) )
sub.stderr?.on('data', data => add(undefined, data) )
const result_promise = new Promise<$mol_run_error_context>((done, fail) => {
const close = (error: Error | null, status: number | null = null, signal: NodeJS.Signals | null = null) => {
if (! timer && timeout) return
clearTimeout(timer)
timer = undefined
const res = {
pid,
signal,
get stdout() { return Buffer.concat(std_data) },
get stderr() { return Buffer.concat(error_data) }
}
if (error || status || timeout_kill) return fail( new $mol_run_error(
this.error_message(res) + (timeout_kill ? ', timeout' : ''),
{ ...log_object, pid, status, signal, timeout_kill },
...error ? [ error ] : []
) )
this.$.$mol_log3_done({
...log_object,
pid,
})
done(res)
}
sub.on('disconnect', () => close(new Error('Disconnected')) )
sub.on('error', err => close(err) )
sub.on('exit', (status, signal) => close(null, status, signal) )
})
return Object.assign(result_promise, { destructor: () => {
clearTimeout(timer)
sub.kill('SIGKILL')
} })
}
static error_message(res?: $mol_run_error_context) {
return res?.stderr.toString() || res?.stdout.toString() || 'Run error'
}
}
}