Repository navigation
Expand file tree
/
Copy patharf.ts
More file actions
232 lines (221 loc) · 8.17 KB
/
Copy patharf.ts
File metadata and controls
232 lines (221 loc) · 8.17 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import * as http from 'http';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { object, MAX_MESSAGE_BYTES } from './protocol';
export interface ArfSession {
pid: number;
socket_path: string;
cwd?: string;
r_version?: string;
started_at?: string;
session_type?: string;
}
// 0.7.2-rc.1 keeps forked R workers from crashing on macOS, reports an R error
// that follows a caught interrupt as an error rather than as interrupted, and
// bounds console reads that cannot be answered.
// TODO: Require arf 0.7.2 once it is released.
export const MIN_ARF_VERSION = '0.7.2-rc.1';
/** arf JSON-RPC error codes that the Interactive client acts on. */
export const ARF_METHOD_NOT_FOUND = -32601;
export const ARF_TIMEOUT = -32007;
/** Errors of the `call` method. */
export const ARF_HANDLER_NOT_FOUND = -32010;
export const ARF_HANDLER_QUEUE_FULL = -32011;
export const ARF_HANDLER_QUEUE_TIMEOUT = -32012;
export const ARF_HANDLER_R_ERROR = -32014;
export const ARF_HANDLER_INTERRUPTED = -32016;
export const ARF_HANDLER_EXECUTION_TIMEOUT = -32019;
/** A JSON-RPC error response from arf, keeping its code and data. */
export class ArfRpcError extends Error {
constructor(
readonly code: number,
readonly rpcMessage: string,
readonly data: unknown,
raw: unknown,
) {
super(JSON.stringify(raw));
this.name = 'ArfRpcError';
}
}
/** The client stopped waiting for arf; the request may still be running. */
export class ArfTransportTimeoutError extends Error {
constructor() {
super('arf request timed out; evaluation may still be running');
this.name = 'ArfTransportTimeoutError';
}
}
export function rpcError(error: unknown): ArfRpcError {
const value =
error && typeof error === 'object' && !Array.isArray(error)
? (error as Record<string, unknown>)
: {};
return new ArfRpcError(
typeof value.code === 'number' ? value.code : NaN,
typeof value.message === 'string' ? value.message : '',
value.data,
error,
);
}
/**
* Whether `version` has at least the precedence of `minimum` under SemVer 2.0.0:
* a prerelease is older than its release, and prerelease identifiers compare
* field by field (numbers numerically and below text). Build metadata is ignored.
*/
export function arfVersionAtLeast(version: unknown, minimum: string): boolean {
const parse = (text: string) => {
const match =
/^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z.-]+)?$/.exec(
text,
);
return match
? {
core: [Number(match[1]), Number(match[2]), Number(match[3])],
pre: match[4]?.split('.') ?? [],
}
: undefined;
};
const actual = typeof version === 'string' ? parse(version) : undefined;
const required = parse(minimum);
if (!actual || !required) {
return false;
}
for (let i = 0; i < 3; i++) {
if (actual.core[i] !== required.core[i]) {
return actual.core[i] > required.core[i];
}
}
// A release is newer than any of its prereleases.
if (!actual.pre.length || !required.pre.length) {
return !actual.pre.length;
}
const numeric = /^\d+$/;
for (let i = 0; i < Math.min(actual.pre.length, required.pre.length); i++) {
const [a, b] = [actual.pre[i], required.pre[i]];
if (a === b) {
continue;
}
if (numeric.test(a) && numeric.test(b)) {
return Number(a) > Number(b);
}
if (numeric.test(a) !== numeric.test(b)) {
return !numeric.test(a);
}
return a > b;
}
// With equal leading identifiers, the longer prerelease is newer.
return actual.pre.length >= required.pre.length;
}
/** Rejects sessions older than MIN_ARF_VERSION, or without the deadline used by inspection. */
export function checkArfSession(session: Record<string, unknown>): void {
if (!arfVersionAtLeast(session.arf_version, MIN_ARF_VERSION)) {
const actual =
typeof session.arf_version === 'string' ? session.arf_version : 'an unknown version';
throw new Error(
`R Interactive requires arf ${MIN_ARF_VERSION} or later, but the session runs ${actual}. Update arf and start a new session.`,
);
}
const capabilities = Array.isArray(session.capabilities) ? session.capabilities : [];
if (!capabilities.includes('call_execution_timeout')) {
throw new Error(
'This arf session does not support execution deadlines for handler calls. Update arf and start a new session.',
);
}
}
/** arf speaks HTTP JSON-RPC, not sess's JSON Lines framing. */
export function arfRequest(
endpoint: string,
method: string,
params: Record<string, unknown> = {},
timeout = 30000,
signal?: AbortSignal,
): Promise<unknown> {
return new Promise((resolve, reject) => {
const body = JSON.stringify({ jsonrpc: '2.0', id: 1, method, params });
const request = http.request(
{
socketPath: endpoint,
path: '/',
method: 'POST',
agent: false,
signal,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
Connection: 'close',
},
},
(response) => {
const chunks: Buffer[] = [];
let size = 0;
response.on('data', (chunk: Buffer) => {
size += chunk.length;
if (size > MAX_MESSAGE_BYTES) {
request.destroy(new Error('arf response exceeds size limit'));
} else {
chunks.push(chunk);
}
});
response.on('error', reject);
response.on('end', () => {
try {
const result = object(JSON.parse(Buffer.concat(chunks).toString('utf8')));
if (result.error) {
reject(rpcError(result.error));
} else {
resolve(result.result);
}
} catch (error) {
reject(error);
}
});
},
);
if (timeout > 0) {
request.setTimeout(timeout, () => request.destroy(new ArfTransportTimeoutError()));
}
request.on('error', reject);
request.end(body);
});
}
export function discoverArf(): ArfSession[] {
const root =
process.platform === 'darwin'
? path.join(os.homedir(), 'Library', 'Caches')
: (process.env.XDG_CACHE_HOME ?? path.join(os.homedir(), '.cache'));
const directory = path.join(root, 'arf', 'sessions');
if (!fs.existsSync(directory)) {
return [];
}
const sessions: ArfSession[] = [];
for (const name of fs.readdirSync(directory)) {
if (!/^\d+\.json$/.test(name)) {
continue;
}
try {
const file = path.join(directory, name);
const stat = fs.lstatSync(file);
if (stat.isSymbolicLink() || (process.getuid && stat.uid !== process.getuid())) {
continue;
}
const value = object(JSON.parse(fs.readFileSync(file, 'utf8')));
if (typeof value.pid !== 'number' || typeof value.socket_path !== 'string') {
continue;
}
process.kill(value.pid, 0);
sessions.push(value as unknown as ArfSession);
} catch {
/* A stale discovery file is not a running session. */
}
}
return sessions;
}
/** Reading frontend metadata does not evaluate R or request execution permission. */
export async function probeArfSession(session: ArfSession): Promise<ArfSession | undefined> {
try {
await arfRequest(session.socket_path, 'session', {}, 1000);
return session;
} catch {
return undefined;
}
}