usegrunt 0.6.4 → 0.6.6
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/README.md +15 -6
- package/dist/local/beatapi-setup.js +83 -0
- package/dist/local/beatapi.js +298 -0
- package/dist/local/clixad-preload.js +177 -0
- package/dist/local/clixad-setup.js +345 -0
- package/dist/local/clixad.js +398 -0
- package/dist/local/data-sharing.js +18 -2
- package/dist/local/fallback-setup.js +74 -22
- package/dist/local/free-api.js +2 -1
- package/dist/local/freebuff.js +5 -1
- package/dist/local/freellmapi.js +3 -5
- package/dist/local/kilo.js +209 -0
- package/dist/local/livekit-setup.js +269 -0
- package/dist/local/livekit.js +219 -0
- package/dist/local/main.js +13 -11
- package/dist/local/parallel.js +2 -0
- package/dist/local/perch-process.js +377 -0
- package/dist/local/perch-setup.js +297 -0
- package/dist/local/perch.js +480 -0
- package/dist/local/pollinations-setup.js +109 -0
- package/dist/local/pollinations.js +324 -0
- package/dist/local/registry.js +4 -4
- package/dist/local/setup.js +340 -62
- package/dist/local/source.js +7 -2
- package/dist/local/wizard.js +24 -21
- package/dist/registry/adapters.json +65 -29
- package/dist/registry.js +4 -2
- package/package.json +1 -1
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import { lstat, open } from 'node:fs/promises';
|
|
4
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
5
|
+
import { dirname, join } from 'node:path';
|
|
6
|
+
import { StringDecoder } from 'node:string_decoder';
|
|
7
|
+
import { SourceError } from './source.js';
|
|
8
|
+
function kill(child) {
|
|
9
|
+
if (!child.pid)
|
|
10
|
+
return;
|
|
11
|
+
try {
|
|
12
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
13
|
+
}
|
|
14
|
+
catch { /* Only our owned process group. */ }
|
|
15
|
+
}
|
|
16
|
+
function unsupported() {
|
|
17
|
+
if (!['linux', 'darwin'].includes(process.platform))
|
|
18
|
+
throw new SourceError('not_installed', 'Perch requires the vetted Linux or macOS CLI');
|
|
19
|
+
}
|
|
20
|
+
/** program and args are executed exactly as supplied, without a shell. Login may use 600s; inference uses 175s. */
|
|
21
|
+
export async function runPerchCommand(program, args, options) {
|
|
22
|
+
unsupported();
|
|
23
|
+
options.signal.throwIfAborted();
|
|
24
|
+
return await new Promise((resolveResult, reject) => {
|
|
25
|
+
const child = spawn(program, args, { cwd: options.cwd, env: options.env, stdio: ['ignore', 'pipe', 'pipe'], detached: true });
|
|
26
|
+
let stdout = '', bytes = 0, failure;
|
|
27
|
+
const decoder = { stdout: new StringDecoder('utf8'), stderr: new StringDecoder('utf8') }, partial = { stdout: '', stderr: '' };
|
|
28
|
+
const fail = (error) => { failure ??= error; kill(child); };
|
|
29
|
+
const abort = () => fail(options.signal.reason);
|
|
30
|
+
const timer = setTimeout(() => fail(new SourceError('timeout', 'Perch command timed out')), Math.min(options.timeoutMs ?? 175000, 600000));
|
|
31
|
+
const consume = (chunk, stream) => {
|
|
32
|
+
bytes += chunk.length;
|
|
33
|
+
if (bytes > (options.maxOutputBytes ?? 65536)) {
|
|
34
|
+
fail(new SourceError('upstream', 'Perch output exceeded the supported size'));
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const text = decoder[stream].write(chunk);
|
|
38
|
+
if (stream === 'stdout')
|
|
39
|
+
stdout += text;
|
|
40
|
+
if (!options.onLine)
|
|
41
|
+
return;
|
|
42
|
+
partial[stream] += text;
|
|
43
|
+
let newline;
|
|
44
|
+
while ((newline = partial[stream].indexOf('\n')) !== -1) {
|
|
45
|
+
const line = partial[stream].slice(0, newline).replace(/\r$/, '');
|
|
46
|
+
partial[stream] = partial[stream].slice(newline + 1);
|
|
47
|
+
try {
|
|
48
|
+
options.onLine(line, stream);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
fail(error);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
options.signal.addEventListener('abort', abort, { once: true });
|
|
57
|
+
child.stdout.on('data', chunk => consume(chunk, 'stdout'));
|
|
58
|
+
child.stderr.on('data', chunk => consume(chunk, 'stderr'));
|
|
59
|
+
child.once('error', () => fail(new SourceError('not_installed', 'Perch CLI could not start; rerun usegrunt setup')));
|
|
60
|
+
child.once('close', code => {
|
|
61
|
+
clearTimeout(timer);
|
|
62
|
+
options.signal.removeEventListener('abort', abort);
|
|
63
|
+
kill(child);
|
|
64
|
+
if (!failure && !options.signal.aborted && options.onLine) {
|
|
65
|
+
for (const stream of ['stdout', 'stderr']) {
|
|
66
|
+
const last = partial[stream] + decoder[stream].end();
|
|
67
|
+
try {
|
|
68
|
+
if (last.trim())
|
|
69
|
+
options.onLine(last, stream);
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
failure = error;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (options.signal.aborted)
|
|
77
|
+
reject(options.signal.reason);
|
|
78
|
+
else if (failure)
|
|
79
|
+
reject(failure);
|
|
80
|
+
else
|
|
81
|
+
resolveResult({ stdout, code });
|
|
82
|
+
});
|
|
83
|
+
if (options.signal.aborted)
|
|
84
|
+
abort();
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/** Supported native plain-terminal settings; fixed slashcommands never invoke a model. */
|
|
88
|
+
export async function runPerchMemorySettings(binary, options, configure = false) {
|
|
89
|
+
unsupported();
|
|
90
|
+
options.signal.throwIfAborted();
|
|
91
|
+
const native = [process.execPath, binary, '--no-local-tools', '--mode', 'ask', '--permission', 'plan', '--cwd', options.cwd];
|
|
92
|
+
// Linux script invokes a shell. Every argument is fixed or an owned path, and
|
|
93
|
+
// receives POSIX quoting; caller prompts never enter this command or stdin.
|
|
94
|
+
const quote = (value) => "'" + value.replaceAll("'", "'\"'\"'") + "'";
|
|
95
|
+
const nonce = randomUUID().replaceAll('-', '');
|
|
96
|
+
// forkpty gives this shell its own process group. Its first exact nonce frame
|
|
97
|
+
// identifies that owned group before exec, before the CLI can produce output.
|
|
98
|
+
const command = `printf '\\036grunt-perch-${nonce}:%s\\037\\n' "$$"; exec ` + native.map(quote).join(' ');
|
|
99
|
+
const args = process.platform === 'darwin' ? ['-q', '/dev/null', '/bin/sh', '-c', command] : ['-q', '-e', '-c', command, '/dev/null'];
|
|
100
|
+
return await new Promise((resolveResult, reject) => {
|
|
101
|
+
const env = Object.fromEntries(Object.entries(options.env).filter(([key]) => !/^(?:SHELL|BASH_ENV|ENV|ZDOTDIR|LD_PRELOAD|DYLD_INSERT_LIBRARIES)$/i.test(key)));
|
|
102
|
+
const child = spawn('/usr/bin/script', args, { cwd: options.cwd, env: { ...env, SHELL: '/bin/sh', PERCH_CLI_PLAIN: '1', TERM: 'dumb' }, stdio: ['pipe', 'pipe', 'pipe'], detached: true });
|
|
103
|
+
const commands = configure ? ['/settings memory off', '/settings learn-memories off', '/settings memory', '/exit'] : ['/settings memory', '/exit'];
|
|
104
|
+
const decoder = new StringDecoder('utf8');
|
|
105
|
+
let output = '', bytes = 0, stage = 0, scan = 0, failure, identity, verified = false, nativeGroup;
|
|
106
|
+
let cleanupTimer;
|
|
107
|
+
const stop = () => {
|
|
108
|
+
if (nativeGroup)
|
|
109
|
+
try {
|
|
110
|
+
process.kill(-nativeGroup, 'SIGKILL');
|
|
111
|
+
}
|
|
112
|
+
catch { /* Only the nonce-bound forkpty child group. */ }
|
|
113
|
+
kill(child);
|
|
114
|
+
};
|
|
115
|
+
const fail = (error) => {
|
|
116
|
+
failure ??= error;
|
|
117
|
+
if (nativeGroup) {
|
|
118
|
+
stop();
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
// Let script forward termination while an early startup frame is pending;
|
|
122
|
+
// keep draining that frame so we can kill its separate native group too.
|
|
123
|
+
try {
|
|
124
|
+
if (child.pid)
|
|
125
|
+
process.kill(-child.pid, 'SIGTERM');
|
|
126
|
+
}
|
|
127
|
+
catch { /* Already closed. */ }
|
|
128
|
+
cleanupTimer ??= setTimeout(stop, 250);
|
|
129
|
+
};
|
|
130
|
+
const abort = () => fail(options.signal.reason);
|
|
131
|
+
const timer = setTimeout(() => fail(new SourceError('timeout', 'Perch memory settings timed out')), Math.min(options.timeoutMs ?? 30000, 30000));
|
|
132
|
+
options.signal.addEventListener('abort', abort, { once: true });
|
|
133
|
+
child.stdin.on('error', () => { });
|
|
134
|
+
child.stdout.on('data', (chunk) => {
|
|
135
|
+
bytes += chunk.length;
|
|
136
|
+
if (bytes > Math.min(options.maxOutputBytes ?? 65536, 65536)) {
|
|
137
|
+
fail(new SourceError('upstream', 'Perch memory settings exceeded the supported size'));
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
output += decoder.write(chunk);
|
|
141
|
+
if (!nativeGroup) {
|
|
142
|
+
const frame = output.match(new RegExp(`^\x1egrunt-perch-${nonce}:(\\d+)\x1f\\r?\\n`));
|
|
143
|
+
if (!frame) {
|
|
144
|
+
if (output.includes('\n'))
|
|
145
|
+
fail(new SourceError('refused', 'Perch terminal ownership could not be verified'));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const group = Number(frame[1]);
|
|
149
|
+
if (!Number.isSafeInteger(group) || group <= 1 || group === process.pid || group === child.pid) {
|
|
150
|
+
fail(new SourceError('refused', 'Perch terminal ownership could not be verified'));
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
nativeGroup = group;
|
|
154
|
+
output = output.slice(frame[0].length);
|
|
155
|
+
if (failure || options.signal.aborted) {
|
|
156
|
+
stop();
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
let marker;
|
|
161
|
+
while ((marker = output.indexOf('you› ', scan)) !== -1) {
|
|
162
|
+
const response = output.slice(scan, marker).replaceAll('\r', '');
|
|
163
|
+
scan = marker + 'you› '.length;
|
|
164
|
+
if (!stage) {
|
|
165
|
+
const clean = output.replaceAll('\r', ''), matches = [...clean.matchAll(/^\s*signed in as ([^\s@]+@[^\s@]+\.[^\s@]+) · saffron · ask · plan\s*$/gm)];
|
|
166
|
+
const versions = [...clean.matchAll(/^perch · field terminal\s+v([^\s]+)\s*$/gm)];
|
|
167
|
+
if (matches.length !== 1 || versions.length !== 1 || versions[0][1] !== '2.4.132') {
|
|
168
|
+
fail(new SourceError('logged_out', 'Perch memory settings identity is unavailable'));
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
identity = { email: matches[0][1].toLowerCase() };
|
|
172
|
+
}
|
|
173
|
+
else if (commands[stage - 1] === '/settings memory') {
|
|
174
|
+
const use = response.split('\n').filter(line => /^\s*Use memory\s+/.test(line)), learn = response.split('\n').filter(line => /^\s*Learn new memories\s+/.test(line));
|
|
175
|
+
if (use.length !== 1 || learn.length !== 1 || !/^\s*Use memory\s+Off\s+memory\s*$/.test(use[0]) || !/^\s*Learn new memories\s+Off\s+learn-memories\s*$/.test(learn[0])) {
|
|
176
|
+
fail(new SourceError('refused', 'Perch account memory must be switched off; rerun usegrunt setup'));
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
verified = true;
|
|
180
|
+
}
|
|
181
|
+
const command = commands[stage++];
|
|
182
|
+
if (!command) {
|
|
183
|
+
fail(new SourceError('upstream', 'Perch memory settings returned an unexpected prompt'));
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
child.stdin.write(command + '\n');
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
child.stderr.on('data', (chunk) => { bytes += chunk.length; if (bytes > Math.min(options.maxOutputBytes ?? 65536, 65536))
|
|
190
|
+
fail(new SourceError('upstream', 'Perch memory settings exceeded the supported size')); });
|
|
191
|
+
child.once('error', () => fail(new SourceError('not_installed', 'Perch requires the native script terminal utility for isolated settings')));
|
|
192
|
+
child.once('close', code => {
|
|
193
|
+
clearTimeout(timer);
|
|
194
|
+
clearTimeout(cleanupTimer);
|
|
195
|
+
options.signal.removeEventListener('abort', abort);
|
|
196
|
+
stop();
|
|
197
|
+
if (options.signal.aborted)
|
|
198
|
+
reject(options.signal.reason);
|
|
199
|
+
else if (failure)
|
|
200
|
+
reject(failure);
|
|
201
|
+
else if (code !== 0 || !identity || !verified || stage !== commands.length)
|
|
202
|
+
reject(new SourceError('refused', 'Perch memory settings could not be verified'));
|
|
203
|
+
else
|
|
204
|
+
resolveResult(identity);
|
|
205
|
+
});
|
|
206
|
+
if (options.signal.aborted)
|
|
207
|
+
abort();
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/** The official CLI implements newline-delimited ACP. Never expose its stderr or raw protocol errors. */
|
|
211
|
+
export async function openPerchACP(binary, options) {
|
|
212
|
+
unsupported();
|
|
213
|
+
options.signal.throwIfAborted();
|
|
214
|
+
const child = spawn(process.execPath, [binary, 'acp'], { cwd: options.cwd, env: options.env, stdio: ['pipe', 'pipe', 'pipe'], detached: true });
|
|
215
|
+
const notifications = [], pending = new Map();
|
|
216
|
+
const decoder = new StringDecoder('utf8');
|
|
217
|
+
let nextId = 1, bytes = 0, partial = '', closed = false, failure;
|
|
218
|
+
let exit;
|
|
219
|
+
const exited = new Promise(resolveExit => { exit = resolveExit; });
|
|
220
|
+
const fail = (error) => {
|
|
221
|
+
failure ??= error;
|
|
222
|
+
for (const operation of pending.values())
|
|
223
|
+
operation.reject(error);
|
|
224
|
+
pending.clear();
|
|
225
|
+
kill(child);
|
|
226
|
+
};
|
|
227
|
+
const abort = () => fail(options.signal.reason);
|
|
228
|
+
const timer = setTimeout(() => fail(new SourceError('timeout', 'Perch ACP timed out')), Math.min(options.timeoutMs ?? 175000, 175000));
|
|
229
|
+
const send = (value) => {
|
|
230
|
+
if (closed || child.stdin.destroyed)
|
|
231
|
+
throw new SourceError('upstream', 'Perch ACP is closed');
|
|
232
|
+
child.stdin.write(JSON.stringify(value) + '\n');
|
|
233
|
+
};
|
|
234
|
+
const receive = (line) => {
|
|
235
|
+
if (!line.trim())
|
|
236
|
+
return;
|
|
237
|
+
let value;
|
|
238
|
+
try {
|
|
239
|
+
const parsed = JSON.parse(line);
|
|
240
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
241
|
+
throw new Error('Invalid frame');
|
|
242
|
+
value = parsed;
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
throw new SourceError('upstream', 'Perch returned an invalid ACP message');
|
|
246
|
+
}
|
|
247
|
+
if (value.jsonrpc !== '2.0')
|
|
248
|
+
throw new SourceError('upstream', 'Perch returned an invalid ACP version');
|
|
249
|
+
if (typeof value.method === 'string') {
|
|
250
|
+
if (value.id !== undefined) {
|
|
251
|
+
// Deny before executing anything. This client exposes no filesystem, terminals or permissions.
|
|
252
|
+
if (value.method === 'session/request_permission')
|
|
253
|
+
send({ jsonrpc: '2.0', id: value.id, result: { outcome: { outcome: 'cancelled' } } });
|
|
254
|
+
else
|
|
255
|
+
send({ jsonrpc: '2.0', id: value.id, error: { code: -32601, message: 'No client tools are available' } });
|
|
256
|
+
throw new SourceError('refused', 'Perch requested an unexpected native action');
|
|
257
|
+
}
|
|
258
|
+
if (value.method !== 'session/update')
|
|
259
|
+
throw new SourceError('refused', 'Perch returned an unexpected ACP notification');
|
|
260
|
+
const update = value.params?.update;
|
|
261
|
+
if (update?.sessionUpdate === 'tool_call' || update?.sessionUpdate === 'tool_call_update')
|
|
262
|
+
throw new SourceError('refused', 'Perch attempted an unexpected native action');
|
|
263
|
+
options.onNotify?.(value);
|
|
264
|
+
notifications.push(value);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (!Number.isSafeInteger(value.id) || !pending.has(value.id))
|
|
268
|
+
throw new SourceError('upstream', 'Perch returned an uncorrelated ACP reply');
|
|
269
|
+
const operation = pending.get(value.id);
|
|
270
|
+
pending.delete(value.id);
|
|
271
|
+
if (value.error) {
|
|
272
|
+
const error = value.error;
|
|
273
|
+
const text = typeof error.message === 'string' ? error.message : '';
|
|
274
|
+
const code = /quota|usage.?limit|rate.?limit|allowance/i.test(text) ? 'quota'
|
|
275
|
+
: error.code === -32000 || /auth|sign.?in|logged.?out/i.test(text) ? 'logged_out'
|
|
276
|
+
: /plan|required.*(?:starter|pro)|forbidden|refus|denied/i.test(text) ? 'refused' : 'upstream';
|
|
277
|
+
operation.reject(new SourceError(code, 'Perch ACP did not complete the request'));
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (!Object.hasOwn(value, 'result'))
|
|
281
|
+
throw new SourceError('upstream', 'Perch ACP returned no result');
|
|
282
|
+
operation.resolve(value.result);
|
|
283
|
+
};
|
|
284
|
+
options.signal.addEventListener('abort', abort, { once: true });
|
|
285
|
+
child.stdin.on('error', () => { });
|
|
286
|
+
child.stdout.on('data', (chunk) => {
|
|
287
|
+
bytes += chunk.length;
|
|
288
|
+
if (bytes > (options.maxOutputBytes ?? 2 * 1024 * 1024)) {
|
|
289
|
+
fail(new SourceError('upstream', 'Perch output exceeded the supported size'));
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
partial += decoder.write(chunk);
|
|
293
|
+
let newline;
|
|
294
|
+
try {
|
|
295
|
+
while ((newline = partial.indexOf('\n')) !== -1) {
|
|
296
|
+
const line = partial.slice(0, newline);
|
|
297
|
+
partial = partial.slice(newline + 1);
|
|
298
|
+
receive(line);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
catch (error) {
|
|
302
|
+
fail(error);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
child.stderr.on('data', (chunk) => {
|
|
306
|
+
// Count but never retain native diagnostics, which can contain pairing URLs or tokens.
|
|
307
|
+
bytes += chunk.length;
|
|
308
|
+
if (bytes > (options.maxOutputBytes ?? 2 * 1024 * 1024))
|
|
309
|
+
fail(new SourceError('upstream', 'Perch output exceeded the supported size'));
|
|
310
|
+
});
|
|
311
|
+
child.once('error', () => fail(new SourceError('not_installed', 'Perch CLI could not start; rerun usegrunt setup')));
|
|
312
|
+
child.once('close', () => {
|
|
313
|
+
closed = true;
|
|
314
|
+
clearTimeout(timer);
|
|
315
|
+
options.signal.removeEventListener('abort', abort);
|
|
316
|
+
fail(failure ?? new SourceError('upstream', 'Perch ACP closed before completing the request'));
|
|
317
|
+
exit();
|
|
318
|
+
});
|
|
319
|
+
if (options.signal.aborted)
|
|
320
|
+
abort();
|
|
321
|
+
return {
|
|
322
|
+
notifications,
|
|
323
|
+
request: async (method, params) => {
|
|
324
|
+
options.signal.throwIfAborted();
|
|
325
|
+
if (failure)
|
|
326
|
+
throw failure;
|
|
327
|
+
if (closed)
|
|
328
|
+
throw new SourceError('upstream', 'Perch ACP is closed');
|
|
329
|
+
return await new Promise((resolveResult, reject) => {
|
|
330
|
+
const id = nextId++;
|
|
331
|
+
pending.set(id, { resolve: resolveResult, reject });
|
|
332
|
+
try {
|
|
333
|
+
send({ jsonrpc: '2.0', id, method, params });
|
|
334
|
+
}
|
|
335
|
+
catch (error) {
|
|
336
|
+
pending.delete(id);
|
|
337
|
+
reject(error);
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
},
|
|
341
|
+
close: async () => { if (!closed) {
|
|
342
|
+
child.stdin.end();
|
|
343
|
+
kill(child);
|
|
344
|
+
} await exited; }
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/** Hash only public executable artifacts, never native authentication. */
|
|
348
|
+
export async function verifyPerchCoreFiles(packageDirectory, expected, signal) {
|
|
349
|
+
for (const [relative, wanted] of Object.entries(expected)) {
|
|
350
|
+
signal.throwIfAborted();
|
|
351
|
+
let directory = dirname(join(packageDirectory, relative));
|
|
352
|
+
while (directory.startsWith(packageDirectory)) {
|
|
353
|
+
const st = await lstat(directory);
|
|
354
|
+
if (!st.isDirectory() || st.isSymbolicLink() || st.uid !== process.getuid?.() || (st.mode & 0o022))
|
|
355
|
+
throw new SourceError('not_installed', 'Perch public installation directory is unsafe');
|
|
356
|
+
if (directory === packageDirectory)
|
|
357
|
+
break;
|
|
358
|
+
directory = dirname(directory);
|
|
359
|
+
}
|
|
360
|
+
const file = await open(join(packageDirectory, relative), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
361
|
+
try {
|
|
362
|
+
const st = await file.stat();
|
|
363
|
+
if (!st.isFile() || st.uid !== process.getuid?.() || (st.mode & 0o022) || st.size > 20 * 1024 * 1024)
|
|
364
|
+
throw new SourceError('not_installed', 'Perch public installation is unsafe');
|
|
365
|
+
const hash = createHash('sha256');
|
|
366
|
+
for await (const chunk of file.createReadStream({ autoClose: false })) {
|
|
367
|
+
signal.throwIfAborted();
|
|
368
|
+
hash.update(chunk);
|
|
369
|
+
}
|
|
370
|
+
if (hash.digest('hex') !== wanted)
|
|
371
|
+
throw new SourceError('not_installed', 'Perch installation does not match the pinned release');
|
|
372
|
+
}
|
|
373
|
+
finally {
|
|
374
|
+
await file.close();
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|