Repository navigation
Expand file tree
/
Copy patharf.ts
More file actions
172 lines (171 loc) · 6.92 KB
/
Copy patharf.ts
File metadata and controls
172 lines (171 loc) · 6.92 KB
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
import { arfRequest } from '../arf';
import { BackendEvent } from '../backend';
import { object, Submission } from '../protocol';
import { RuntimeProcess } from './process';
import { rString } from './rCode';
import { ArfOutput } from './arfOutput';
export class Arf {
readonly process: RuntimeProcess;
ready = false;
private abort = new AbortController();
private started = false;
constructor(
private command: string,
private directory: string,
private endpoint: string | undefined,
adopted: boolean,
private emit: (event: BackendEvent) => void,
private token: string,
private event: (message: Record<string, unknown>, channel: string) => void,
) {
this.process = new RuntimeProcess(adopted, emit);
}
markStarted(): void {
this.started = true;
}
async start(bootstrap: string, env: NodeJS.ProcessEnv): Promise<void> {
if (!this.process.adopted) {
const output = new Map(
['stdout', 'stderr'].map((channel) => [
channel,
new ArfOutput(
this.token,
(text) => {
if (text) {
this.emit({ type: 'stream', text, channel });
}
},
(message) => this.event(message, channel),
),
]),
);
this.endpoint = await new Promise<string>((resolve, reject) => {
let readiness = '';
const timer = setTimeout(
() => reject(new Error('arf did not report its IPC endpoint')),
30000,
);
const child = this.process.launch(
this.command,
['headless', '--json'],
this.directory,
env,
(text, channel) => {
if (channel === 'stdout' && !this.endpoint) {
readiness += text;
let newline: number;
while (!this.endpoint && (newline = readiness.indexOf('\n')) >= 0) {
const line = readiness.slice(0, newline);
readiness = readiness.slice(newline + 1);
let info: Record<string, unknown> | undefined;
try {
info = object(JSON.parse(line));
} catch {
/* Profiles can print before readiness. */
}
if (typeof info?.socket_path === 'string') {
this.endpoint = info.socket_path;
clearTimeout(timer);
resolve(this.endpoint);
} else {
this.emit({ type: 'stream', text: line + '\n', channel });
}
}
if (this.endpoint || readiness.length > 65536) {
if (readiness) {
output.get(channel)!.push(readiness);
}
readiness = '';
}
} else {
try {
output.get(channel)!.push(text);
} catch (error) {
this.emit({ type: 'unavailable', message: String(error) });
}
}
},
);
child.once('error', (error) => {
clearTimeout(timer);
reject(error);
});
child.once('exit', () => {
clearTimeout(timer);
reject(new Error('arf exited during startup'));
});
});
}
if (!this.endpoint) {
throw new Error('Missing arf endpoint');
}
const metadata = object(
await arfRequest(this.endpoint, 'session', {}, 30000, this.abort.signal),
);
this.emit({
type: 'provider',
data: {
provider: this.process.adopted ? 'arf-existing' : 'arf',
policy: metadata.ipc_policy,
},
});
// Visible evaluation respects arf's advertised IPC policy, including bootstrap.
const result = object(
await arfRequest(
this.endpoint,
'evaluate',
{ code: bootstrap, visible: true },
30000,
this.abort.signal,
),
);
if (result.error) {
throw new Error(
typeof result.error === 'string' ? result.error : JSON.stringify(result.error),
);
}
}
dispose(): void {
this.abort.abort();
this.process.dispose();
}
async dispatch(submission: Submission): Promise<void> {
const code = `sess:::interactive_execute(${rString(submission.id)}, ${rString(submission.code)}, jsonlite::fromJSON(${rString(JSON.stringify(submission.source ?? null))}, simplifyVector=FALSE))`;
this.started = false;
// Managed sessions use send to avoid arf's unbounded evaluation capture.
// R's ordered events establish the outcome even if arf's reply times out.
let result: Record<string, unknown>;
try {
result = object(
await arfRequest(
this.endpoint!,
this.process.adopted ? 'evaluate' : 'user_input',
{ code, visible: true, timeout_ms: 86400000 },
0,
this.abort.signal,
),
);
} catch (error) {
// A server timeout does not cancel R. Keep accepting its event stream.
// Other transport failures remain ambiguous and must block dispatch.
if (!this.process.adopted && this.started && /timed out|timeout/i.test(String(error))) {
return;
}
throw error;
}
if (this.process.adopted) {
for (const channel of ['stdout', 'stderr']) {
if (typeof result[channel] === 'string' && result[channel]) {
this.emit({ type: 'stream', text: result[channel], channel });
}
}
}
if (result.error || result.accepted === false) {
throw new Error(
typeof result.error === 'string'
? result.error
: 'arf rejected Interactive execution',
);
}
}
}