starfold 0.0.0-stage → 0.3.2
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/LICENSE +22 -0
- package/README.md +102 -2
- package/bin/starfold.js +36 -0
- package/dist/act.d.ts +47 -0
- package/dist/act.js +49 -0
- package/dist/client.d.ts +62 -0
- package/dist/client.js +379 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +5 -0
- package/dist/native.d.ts +76 -0
- package/dist/native.js +419 -0
- package/dist/types.d.ts +78 -0
- package/dist/types.js +53 -0
- package/dist/zip.d.ts +2 -0
- package/dist/zip.js +53 -0
- package/package.json +42 -4
package/dist/native.js
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
+
import { accessSync, chmodSync, constants, readdirSync, statSync, } from 'node:fs';
|
|
3
|
+
import { Agent, request } from 'node:http';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import { createServer } from 'node:net';
|
|
6
|
+
import { dirname, join } from 'node:path';
|
|
7
|
+
import { Starfold } from './client.js';
|
|
8
|
+
import { StarfoldError } from './types.js';
|
|
9
|
+
/** The apt line for the shared libraries the Linux binary links. */
|
|
10
|
+
export const LINUX_LIBRARIES_HINT = 'sudo apt install libyuv0 libopus0 libpulse0 (glibc 2.39+, Ubuntu 24.04 or newer)';
|
|
11
|
+
const PACKAGES = {
|
|
12
|
+
'linux-x64-gnu': '@starfold/server-linux-x64-gnu',
|
|
13
|
+
'linux-arm64-gnu': '@starfold/server-linux-arm64-gnu',
|
|
14
|
+
'darwin-arm64': '@starfold/server-darwin-arm64',
|
|
15
|
+
'win32-x64': '@starfold/server-win32-x64',
|
|
16
|
+
};
|
|
17
|
+
const SUPPORTED = 'Linux x64 and arm64 with glibc, macOS arm64, Windows x64';
|
|
18
|
+
/** No server binary: $STARFOLD_BIN isn't set (or isn't a file) and no platform package has one. */
|
|
19
|
+
export class BinaryNotFoundError extends StarfoldError {
|
|
20
|
+
}
|
|
21
|
+
/** The shared libraries the server binary needs don't all resolve on this Linux system. */
|
|
22
|
+
export class MissingLibrariesError extends StarfoldError {
|
|
23
|
+
missing;
|
|
24
|
+
constructor(missing) {
|
|
25
|
+
super(`the starfold server can't start: these shared libraries are missing: ${missing.join(', ')}. ` +
|
|
26
|
+
`It needs glibc 2.39+ (Ubuntu 24.04 or newer) and: ${LINUX_LIBRARIES_HINT}`);
|
|
27
|
+
this.missing = missing;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** startServer failed: the server exited, didn't answer in time, or its port was taken. */
|
|
31
|
+
export class ServerStartError extends StarfoldError {
|
|
32
|
+
/** The last of what the server printed. */
|
|
33
|
+
output;
|
|
34
|
+
constructor(message, output) {
|
|
35
|
+
super(`${message}:\n${output}`);
|
|
36
|
+
this.output = output;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function glibc() {
|
|
40
|
+
const report = process.report;
|
|
41
|
+
if (typeof report?.getReport === 'function') {
|
|
42
|
+
try {
|
|
43
|
+
return Boolean(report.getReport().header?.glibcVersionRuntime);
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// fall through to the loader check
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
return !readdirSync('/lib').some((n) => n.startsWith('ld-musl-'));
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** The platform's key in the server packages' names, such as linux-x64-gnu. */
|
|
57
|
+
export function platformKey() {
|
|
58
|
+
const base = `${process.platform}-${process.arch}`;
|
|
59
|
+
if (process.platform !== 'linux') {
|
|
60
|
+
return base;
|
|
61
|
+
}
|
|
62
|
+
return `${base}-${glibc() ? 'gnu' : 'musl'}`;
|
|
63
|
+
}
|
|
64
|
+
/** The server package for this platform, or null if there is none. */
|
|
65
|
+
export function serverPackage(key = platformKey()) {
|
|
66
|
+
return PACKAGES[key] ?? null;
|
|
67
|
+
}
|
|
68
|
+
function isFile(path) {
|
|
69
|
+
try {
|
|
70
|
+
return statSync(path).isFile();
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function executable(path) {
|
|
77
|
+
if (process.platform === 'win32') {
|
|
78
|
+
return path;
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
accessSync(path, constants.X_OK);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// A tarball that lost the mode bits: make it runnable again.
|
|
85
|
+
chmodSync(path, 0o755);
|
|
86
|
+
}
|
|
87
|
+
return path;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The server binary: $STARFOLD_BIN if set, else the one in this platform's
|
|
91
|
+
* @starfold/server-* package. Throws a BinaryNotFoundError if neither is there; the client
|
|
92
|
+
* still works against a remote server without it.
|
|
93
|
+
*/
|
|
94
|
+
export function binaryPath() {
|
|
95
|
+
const env = process.env.STARFOLD_BIN;
|
|
96
|
+
if (env) {
|
|
97
|
+
if (!isFile(env)) {
|
|
98
|
+
throw new BinaryNotFoundError(`STARFOLD_BIN is ${env}, which isn't a file`);
|
|
99
|
+
}
|
|
100
|
+
return executable(env);
|
|
101
|
+
}
|
|
102
|
+
const key = platformKey();
|
|
103
|
+
const pkg = serverPackage(key);
|
|
104
|
+
const remote = 'The client still works against a remote server: new Starfold(url).';
|
|
105
|
+
if (pkg === null) {
|
|
106
|
+
throw new BinaryNotFoundError(`starfold has no server binary for ${key} (there are packages for ${SUPPORTED}); ` +
|
|
107
|
+
`set STARFOLD_BIN to a starfold binary built for it. ${remote}`);
|
|
108
|
+
}
|
|
109
|
+
let dir;
|
|
110
|
+
try {
|
|
111
|
+
dir = dirname(createRequire(import.meta.url).resolve(`${pkg}/package.json`));
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
throw new BinaryNotFoundError(`the server binary for ${key} comes in ${pkg}, which is not installed ` +
|
|
115
|
+
`(were optional dependencies left out?); install it, or set STARFOLD_BIN. ${remote}`);
|
|
116
|
+
}
|
|
117
|
+
const bin = join(dir, 'bin', process.platform === 'win32' ? 'starfold.exe' : 'starfold');
|
|
118
|
+
if (!isFile(bin)) {
|
|
119
|
+
throw new BinaryNotFoundError(`${pkg} has no ${bin}; reinstall it, or set STARFOLD_BIN`);
|
|
120
|
+
}
|
|
121
|
+
return executable(bin);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* On Linux, what `binary` needs and the system lacks, by ldd: the shared libraries that don't
|
|
125
|
+
* resolve and the glibc versions that are too new (GLIBC_2.39, say); elsewhere nothing.
|
|
126
|
+
*/
|
|
127
|
+
export function missingLibraries(binary) {
|
|
128
|
+
if (process.platform !== 'linux') {
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
const r = spawnSync('ldd', ['--', binary], { encoding: 'utf8' });
|
|
132
|
+
if (r.error) {
|
|
133
|
+
return [];
|
|
134
|
+
}
|
|
135
|
+
const missing = [];
|
|
136
|
+
for (const line of `${r.stdout ?? ''}\n${r.stderr ?? ''}`.split('\n')) {
|
|
137
|
+
const lib = /^\s*(\S+)\s+=>\s+not found/.exec(line);
|
|
138
|
+
const version = /version `([^']+)' not found/.exec(line);
|
|
139
|
+
const name = lib?.[1] ?? version?.[1];
|
|
140
|
+
if (name && !missing.includes(name)) {
|
|
141
|
+
missing.push(name);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return missing;
|
|
145
|
+
}
|
|
146
|
+
/** Throws a MissingLibrariesError naming what `binary` lacks (Linux only). */
|
|
147
|
+
export function checkLibraries(binary) {
|
|
148
|
+
const missing = missingLibraries(binary);
|
|
149
|
+
if (missing.length > 0) {
|
|
150
|
+
throw new MissingLibrariesError(missing);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// The Python SDK's guard, the same text: run as the leader of its own process group (detached),
|
|
154
|
+
// it starts the server and a watcher holding the guard's stdin, a pipe from this process. When
|
|
155
|
+
// that pipe closes (this process ended, however it ended) the watcher sends TERM to the group,
|
|
156
|
+
// which stops the server (the guard and the watcher ignore it); once the server is reaped the
|
|
157
|
+
// guard sends USR1 to the group, which ends the watcher, and exits with the server's status.
|
|
158
|
+
// Only the group is ever signalled, never a pid that may have been reaped.
|
|
159
|
+
const GUARD = `exec 3<&0 0</dev/null
|
|
160
|
+
"$0" "$@" 3<&- &
|
|
161
|
+
p=$!
|
|
162
|
+
trap '' TERM
|
|
163
|
+
{ while read -r _ <&3; do :; done; kill -TERM 0; } >/dev/null 2>&1 &
|
|
164
|
+
exec 3<&-
|
|
165
|
+
wait "$p"
|
|
166
|
+
rc=$?
|
|
167
|
+
trap '' USR1
|
|
168
|
+
kill -USR1 0 2>/dev/null
|
|
169
|
+
exit "$rc"`;
|
|
170
|
+
const TAIL_BYTES = 16 * 1024;
|
|
171
|
+
const PORT_TRIES = 3;
|
|
172
|
+
function freePort(addr) {
|
|
173
|
+
return new Promise((resolve, reject) => {
|
|
174
|
+
const s = createServer();
|
|
175
|
+
s.once('error', reject);
|
|
176
|
+
s.listen(0, addr, () => {
|
|
177
|
+
const a = s.address();
|
|
178
|
+
s.close(() => typeof a === 'object' && a
|
|
179
|
+
? resolve(a.port)
|
|
180
|
+
: reject(new Error('no port')));
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function generatedPassword() {
|
|
185
|
+
const b = new Uint8Array(16);
|
|
186
|
+
crypto.getRandomValues(b);
|
|
187
|
+
return Array.from(b, (x) => x.toString(16).padStart(2, '0')).join('');
|
|
188
|
+
}
|
|
189
|
+
function urlHost(addr) {
|
|
190
|
+
if (addr === '0.0.0.0' || addr === '')
|
|
191
|
+
return '127.0.0.1';
|
|
192
|
+
if (addr === '::')
|
|
193
|
+
return '[::1]';
|
|
194
|
+
return addr.includes(':') ? `[${addr}]` : addr;
|
|
195
|
+
}
|
|
196
|
+
/** The --base-path among the server flags, read as the server reads it, without a trailing /. */
|
|
197
|
+
function basePath(args) {
|
|
198
|
+
let found = '';
|
|
199
|
+
for (let i = 0; i < args.length; i++) {
|
|
200
|
+
const a = args[i];
|
|
201
|
+
const m = /^--base[-_]path(=(.*))?$/.exec(a);
|
|
202
|
+
if (!m)
|
|
203
|
+
continue;
|
|
204
|
+
if (m[1] !== undefined) {
|
|
205
|
+
found = m[2];
|
|
206
|
+
}
|
|
207
|
+
else if (i + 1 < args.length && !args[i + 1].startsWith('--')) {
|
|
208
|
+
found = args[++i];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return found.replace(/\/$/, '');
|
|
212
|
+
}
|
|
213
|
+
// Straight to the server: node:http with an agent of its own, since the global one takes the
|
|
214
|
+
// environment's proxy under NODE_USE_ENV_PROXY=1.
|
|
215
|
+
const direct = new Agent();
|
|
216
|
+
// Settles on every outcome: the whole answer (true for a 200), or false once the request or
|
|
217
|
+
// the answer closes, errs or stalls for a second, headers sent or not.
|
|
218
|
+
function answers(url) {
|
|
219
|
+
return new Promise((resolve) => {
|
|
220
|
+
const req = request(url, { timeout: 1000, agent: direct }, (res) => {
|
|
221
|
+
res.resume();
|
|
222
|
+
res.once('end', () => resolve(res.statusCode === 200));
|
|
223
|
+
res.once('close', () => resolve(false));
|
|
224
|
+
res.once('error', () => resolve(false));
|
|
225
|
+
});
|
|
226
|
+
req.once('timeout', () => req.destroy(new Error('no answer in time')));
|
|
227
|
+
req.once('error', () => resolve(false));
|
|
228
|
+
req.once('close', () => resolve(false));
|
|
229
|
+
req.end();
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
function unref(x) {
|
|
233
|
+
const u = x?.unref;
|
|
234
|
+
if (typeof u === 'function')
|
|
235
|
+
u.call(x);
|
|
236
|
+
}
|
|
237
|
+
/** A server this process started: `client()` is signed in; `close()` stops it. */
|
|
238
|
+
export class LocalServer {
|
|
239
|
+
url;
|
|
240
|
+
password;
|
|
241
|
+
/** The child process (on Linux and macOS a small sh guard whose child is the server). */
|
|
242
|
+
process;
|
|
243
|
+
#exited;
|
|
244
|
+
#output;
|
|
245
|
+
constructor(url, password, child, exited, output) {
|
|
246
|
+
this.url = url;
|
|
247
|
+
this.password = password;
|
|
248
|
+
this.process = child;
|
|
249
|
+
this.#exited = exited;
|
|
250
|
+
this.#output = output;
|
|
251
|
+
}
|
|
252
|
+
/** A client for this server, already holding its password. */
|
|
253
|
+
client(options = {}) {
|
|
254
|
+
return new Starfold(this.url, { ...options, password: this.password });
|
|
255
|
+
}
|
|
256
|
+
/** The last of what the server printed. */
|
|
257
|
+
output() {
|
|
258
|
+
return this.#output();
|
|
259
|
+
}
|
|
260
|
+
/** Stops the server and waits for it to exit. */
|
|
261
|
+
async close() {
|
|
262
|
+
const child = this.process;
|
|
263
|
+
if (child.exitCode !== null || child.signalCode !== null) {
|
|
264
|
+
// The guard is gone; its stdin's end tells a watcher that may outlive it to stop.
|
|
265
|
+
child.stdin?.destroy();
|
|
266
|
+
await this.#exited;
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (process.platform === 'win32') {
|
|
270
|
+
child.kill();
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
child.stdin?.end();
|
|
274
|
+
}
|
|
275
|
+
let timer;
|
|
276
|
+
const late = new Promise((r) => {
|
|
277
|
+
timer = setTimeout(() => r(true), 5000);
|
|
278
|
+
});
|
|
279
|
+
const forced = await Promise.race([this.#exited.then(() => false), late]);
|
|
280
|
+
clearTimeout(timer);
|
|
281
|
+
// The group only while its leader, the guard, is unreaped: Node reaps it when it sets
|
|
282
|
+
// exitCode or signalCode, after which the group's id could name another group.
|
|
283
|
+
const unreaped = child.exitCode === null && child.signalCode === null;
|
|
284
|
+
if (forced && unreaped) {
|
|
285
|
+
try {
|
|
286
|
+
if (process.platform === 'win32' || child.pid === undefined) {
|
|
287
|
+
child.kill('SIGKILL');
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
// already gone
|
|
295
|
+
}
|
|
296
|
+
await this.#exited;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
async [Symbol.asyncDispose]() {
|
|
300
|
+
await this.close();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
class PortTaken extends Error {
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Starts the server and resolves once it answers. It binds to loopback with a generated
|
|
307
|
+
* password unless told otherwise, and stops when this process exits. On Linux it needs an
|
|
308
|
+
* X display ($DISPLAY, Xvfb for one); it does not start one. Every failure to start is a
|
|
309
|
+
* ServerStartError carrying what the server printed.
|
|
310
|
+
*/
|
|
311
|
+
export async function startServer(options = {}) {
|
|
312
|
+
const binary = options.binary ?? binaryPath();
|
|
313
|
+
checkLibraries(binary);
|
|
314
|
+
const addr = options.addr ?? '127.0.0.1';
|
|
315
|
+
const password = options.password || generatedPassword();
|
|
316
|
+
const deadline = Date.now() + (options.timeoutMs ?? 30_000);
|
|
317
|
+
for (let attempt = 1;; attempt++) {
|
|
318
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
319
|
+
const port = options.port ?? (await freePort(addr));
|
|
320
|
+
try {
|
|
321
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
322
|
+
return await launch(binary, addr, port, password, options, deadline);
|
|
323
|
+
}
|
|
324
|
+
catch (e) {
|
|
325
|
+
if (!(e instanceof PortTaken))
|
|
326
|
+
throw e;
|
|
327
|
+
if (options.port !== undefined || attempt >= PORT_TRIES) {
|
|
328
|
+
throw new ServerStartError(`port ${port} is taken`, e.message);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
async function launch(binary, addr, port, password, options, deadline) {
|
|
334
|
+
const env = {
|
|
335
|
+
...process.env,
|
|
336
|
+
...options.env,
|
|
337
|
+
STARFOLD_PASSWORD: password,
|
|
338
|
+
};
|
|
339
|
+
delete env.VOXL_PASSWORD;
|
|
340
|
+
const args = [`--addr=${addr}`, `--port=${port}`, ...(options.args ?? [])];
|
|
341
|
+
const windows = process.platform === 'win32';
|
|
342
|
+
const child = windows
|
|
343
|
+
? spawn(binary, args, {
|
|
344
|
+
env,
|
|
345
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
346
|
+
windowsHide: true,
|
|
347
|
+
})
|
|
348
|
+
: spawn('/bin/sh', ['-c', GUARD, binary, ...args], {
|
|
349
|
+
env,
|
|
350
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
351
|
+
detached: true,
|
|
352
|
+
});
|
|
353
|
+
let tail = '';
|
|
354
|
+
const keep = (chunk) => {
|
|
355
|
+
tail = (tail + chunk.toString()).slice(-TAIL_BYTES);
|
|
356
|
+
};
|
|
357
|
+
child.stdout?.on('data', keep);
|
|
358
|
+
child.stderr?.on('data', keep);
|
|
359
|
+
let exitedWith = null;
|
|
360
|
+
// 'close', after the output pipes have drained, so the tail holds the server's last words.
|
|
361
|
+
const exited = new Promise((resolve) => {
|
|
362
|
+
child.once('close', (code, signal) => {
|
|
363
|
+
exitedWith = signal ? `signal ${signal}` : `code ${code}`;
|
|
364
|
+
resolve();
|
|
365
|
+
});
|
|
366
|
+
child.once('error', (e) => {
|
|
367
|
+
exitedWith = `error ${e.message}`;
|
|
368
|
+
resolve();
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
if (windows) {
|
|
372
|
+
const stop = () => child.kill();
|
|
373
|
+
process.once('exit', stop);
|
|
374
|
+
void exited.then(() => process.off('exit', stop));
|
|
375
|
+
}
|
|
376
|
+
const base = basePath(options.args ?? []);
|
|
377
|
+
const url = `http://${urlHost(addr)}:${port}${base}`;
|
|
378
|
+
const server = new LocalServer(url, password, child, exited, () => tail);
|
|
379
|
+
if ((base !== '' && !base.startsWith('/')) ||
|
|
380
|
+
!URL.canParse(`${url}/healthz`)) {
|
|
381
|
+
await server.close();
|
|
382
|
+
throw new ServerStartError(base !== ''
|
|
383
|
+
? `--base-path ${base} makes no URL to reach the server at (it starts with /)`
|
|
384
|
+
: `the address ${addr} with port ${port} makes no URL to reach the server at`, tail);
|
|
385
|
+
}
|
|
386
|
+
const failed = () => {
|
|
387
|
+
// The guard is gone; its stdin's end tells a watcher that may outlive it to stop.
|
|
388
|
+
child.stdin?.destroy();
|
|
389
|
+
if (/address (already )?in use/i.test(tail))
|
|
390
|
+
return new PortTaken(tail);
|
|
391
|
+
return new ServerStartError(`the starfold server exited (${exitedWith}) before it answered`, tail);
|
|
392
|
+
};
|
|
393
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
394
|
+
while (!(await answers(`${url}/healthz`))) {
|
|
395
|
+
if (exitedWith !== null) {
|
|
396
|
+
throw failed();
|
|
397
|
+
}
|
|
398
|
+
if (child.exitCode !== null || child.signalCode !== null) {
|
|
399
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
400
|
+
await exited;
|
|
401
|
+
throw failed();
|
|
402
|
+
}
|
|
403
|
+
if (Date.now() > deadline) {
|
|
404
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
405
|
+
await server.close();
|
|
406
|
+
throw new ServerStartError(`the starfold server did not answer within ${options.timeoutMs ?? 30_000} ms`, tail);
|
|
407
|
+
}
|
|
408
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
409
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
410
|
+
}
|
|
411
|
+
if (exitedWith !== null) {
|
|
412
|
+
throw failed();
|
|
413
|
+
}
|
|
414
|
+
unref(child);
|
|
415
|
+
unref(child.stdin);
|
|
416
|
+
unref(child.stdout);
|
|
417
|
+
unref(child.stderr);
|
|
418
|
+
return server;
|
|
419
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** A request the server refused or could not answer. */
|
|
2
|
+
export declare class StarfoldError extends Error {
|
|
3
|
+
readonly status: number | null;
|
|
4
|
+
constructor(message: string, status?: number | null);
|
|
5
|
+
}
|
|
6
|
+
/** Missing or wrong password (401). */
|
|
7
|
+
export declare class AuthError extends StarfoldError {
|
|
8
|
+
}
|
|
9
|
+
/** The server refuses this (403), e.g. file transfer is turned off. */
|
|
10
|
+
export declare class ForbiddenError extends StarfoldError {
|
|
11
|
+
}
|
|
12
|
+
/** The server runs with --read-only and takes no input. */
|
|
13
|
+
export declare class ReadOnlyError extends ForbiddenError {
|
|
14
|
+
}
|
|
15
|
+
/** The server runs without --enable-recording, so it doesn't record. */
|
|
16
|
+
export declare class RecordingOffError extends ForbiddenError {
|
|
17
|
+
}
|
|
18
|
+
/** No such file, folder or recording (404). */
|
|
19
|
+
export declare class NotFoundError extends StarfoldError {
|
|
20
|
+
}
|
|
21
|
+
/** The server did not answer in time. */
|
|
22
|
+
export declare class StarfoldTimeoutError extends StarfoldError {
|
|
23
|
+
}
|
|
24
|
+
/** The server could not be reached. */
|
|
25
|
+
export declare class StarfoldConnectionError extends StarfoldError {
|
|
26
|
+
}
|
|
27
|
+
export declare class Screenshot {
|
|
28
|
+
readonly png: Uint8Array;
|
|
29
|
+
/** Pass as `since` to `wait({ change: true })` for changes after this screenshot. */
|
|
30
|
+
readonly seq: number;
|
|
31
|
+
constructor(png: Uint8Array, seq: number);
|
|
32
|
+
/** [width, height], from the PNG header. */
|
|
33
|
+
get size(): [number, number];
|
|
34
|
+
/** Writes the PNG to `path` and returns the path. */
|
|
35
|
+
save(path: string): Promise<string>;
|
|
36
|
+
}
|
|
37
|
+
export interface WaitResult {
|
|
38
|
+
changed: boolean;
|
|
39
|
+
timedOut: boolean;
|
|
40
|
+
seq: number;
|
|
41
|
+
waitedMs: number;
|
|
42
|
+
}
|
|
43
|
+
export interface InputResult {
|
|
44
|
+
/** The screen's change count before the input: `wait({ change: true, since: seq })` returns once it changed. */
|
|
45
|
+
seq: number;
|
|
46
|
+
events: number;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A started recording. `id` is the string the server returned once, to this caller; any
|
|
50
|
+
* holder of it can stop the recording and obtain its clip, so treat it like a secret.
|
|
51
|
+
*/
|
|
52
|
+
export interface Recording {
|
|
53
|
+
id: string;
|
|
54
|
+
maxSeconds: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A recording's MP4s, in order: one, unless another encoder took over mid-recording.
|
|
58
|
+
* `endReason` says why it ended: stopped, time_limit, resized, segment_cap,
|
|
59
|
+
* bad_parameter_sets, segment_lost or closed (null from a server that sends one MP4).
|
|
60
|
+
*/
|
|
61
|
+
export type Segments = Uint8Array[] & {
|
|
62
|
+
endReason: string | null;
|
|
63
|
+
};
|
|
64
|
+
export declare function segments(parts: Uint8Array[], endReason?: string | null): Segments;
|
|
65
|
+
export interface FileEntry {
|
|
66
|
+
name: string;
|
|
67
|
+
dir: boolean;
|
|
68
|
+
size: number;
|
|
69
|
+
mtimeMs: number;
|
|
70
|
+
}
|
|
71
|
+
export interface FileList {
|
|
72
|
+
path: string;
|
|
73
|
+
entries: FileEntry[];
|
|
74
|
+
}
|
|
75
|
+
export interface Upload {
|
|
76
|
+
path: string;
|
|
77
|
+
size: number;
|
|
78
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** A request the server refused or could not answer. */
|
|
2
|
+
export class StarfoldError extends Error {
|
|
3
|
+
status;
|
|
4
|
+
constructor(message, status = null) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = new.target.name;
|
|
7
|
+
this.status = status;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/** Missing or wrong password (401). */
|
|
11
|
+
export class AuthError extends StarfoldError {
|
|
12
|
+
}
|
|
13
|
+
/** The server refuses this (403), e.g. file transfer is turned off. */
|
|
14
|
+
export class ForbiddenError extends StarfoldError {
|
|
15
|
+
}
|
|
16
|
+
/** The server runs with --read-only and takes no input. */
|
|
17
|
+
export class ReadOnlyError extends ForbiddenError {
|
|
18
|
+
}
|
|
19
|
+
/** The server runs without --enable-recording, so it doesn't record. */
|
|
20
|
+
export class RecordingOffError extends ForbiddenError {
|
|
21
|
+
}
|
|
22
|
+
/** No such file, folder or recording (404). */
|
|
23
|
+
export class NotFoundError extends StarfoldError {
|
|
24
|
+
}
|
|
25
|
+
/** The server did not answer in time. */
|
|
26
|
+
export class StarfoldTimeoutError extends StarfoldError {
|
|
27
|
+
}
|
|
28
|
+
/** The server could not be reached. */
|
|
29
|
+
export class StarfoldConnectionError extends StarfoldError {
|
|
30
|
+
}
|
|
31
|
+
export class Screenshot {
|
|
32
|
+
png;
|
|
33
|
+
/** Pass as `since` to `wait({ change: true })` for changes after this screenshot. */
|
|
34
|
+
seq;
|
|
35
|
+
constructor(png, seq) {
|
|
36
|
+
this.png = png;
|
|
37
|
+
this.seq = seq;
|
|
38
|
+
}
|
|
39
|
+
/** [width, height], from the PNG header. */
|
|
40
|
+
get size() {
|
|
41
|
+
const v = new DataView(this.png.buffer, this.png.byteOffset, this.png.byteLength);
|
|
42
|
+
return [v.getUint32(16), v.getUint32(20)];
|
|
43
|
+
}
|
|
44
|
+
/** Writes the PNG to `path` and returns the path. */
|
|
45
|
+
async save(path) {
|
|
46
|
+
const { writeFile } = await import('node:fs/promises');
|
|
47
|
+
await writeFile(path, this.png);
|
|
48
|
+
return path;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export function segments(parts, endReason = null) {
|
|
52
|
+
return Object.assign(parts, { endReason });
|
|
53
|
+
}
|
package/dist/zip.d.ts
ADDED
package/dist/zip.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** Reads the members of a zip archive (stored or deflated), enough for a recording's segments. */
|
|
2
|
+
export async function unzip(data) {
|
|
3
|
+
const v = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
4
|
+
let end = -1;
|
|
5
|
+
for (let i = data.length - 22; i >= Math.max(0, data.length - 22 - 0xffff); i--) {
|
|
6
|
+
if (v.getUint32(i, true) === 0x06054b50) {
|
|
7
|
+
end = i;
|
|
8
|
+
break;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
if (end < 0) {
|
|
12
|
+
throw new Error('not a zip archive');
|
|
13
|
+
}
|
|
14
|
+
const count = v.getUint16(end + 10, true);
|
|
15
|
+
let p = v.getUint32(end + 16, true);
|
|
16
|
+
const names = new TextDecoder();
|
|
17
|
+
const out = new Map();
|
|
18
|
+
for (let n = 0; n < count; n++) {
|
|
19
|
+
if (v.getUint32(p, true) !== 0x02014b50) {
|
|
20
|
+
throw new Error('a broken zip central directory');
|
|
21
|
+
}
|
|
22
|
+
const method = v.getUint16(p + 10, true);
|
|
23
|
+
const size = v.getUint32(p + 20, true);
|
|
24
|
+
const nameLen = v.getUint16(p + 28, true);
|
|
25
|
+
const extraLen = v.getUint16(p + 30, true);
|
|
26
|
+
const commentLen = v.getUint16(p + 32, true);
|
|
27
|
+
const local = v.getUint32(p + 42, true);
|
|
28
|
+
const name = names.decode(data.subarray(p + 46, p + 46 + nameLen));
|
|
29
|
+
p += 46 + nameLen + extraLen + commentLen;
|
|
30
|
+
if (v.getUint32(local, true) !== 0x04034b50) {
|
|
31
|
+
throw new Error(`a broken zip entry: ${name}`);
|
|
32
|
+
}
|
|
33
|
+
const start = local +
|
|
34
|
+
30 +
|
|
35
|
+
v.getUint16(local + 26, true) +
|
|
36
|
+
v.getUint16(local + 28, true);
|
|
37
|
+
const raw = data.subarray(start, start + size);
|
|
38
|
+
if (method === 0) {
|
|
39
|
+
out.set(name, raw);
|
|
40
|
+
}
|
|
41
|
+
else if (method === 8) {
|
|
42
|
+
const stream = new Blob([raw])
|
|
43
|
+
.stream()
|
|
44
|
+
.pipeThrough(new DecompressionStream('deflate-raw'));
|
|
45
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
46
|
+
out.set(name, new Uint8Array(await new Response(stream).arrayBuffer()));
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
throw new Error(`zip entry ${name} uses compression method ${method}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "starfold",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.2",
|
|
4
|
+
"description": "TypeScript SDK for starfold: screenshots, waiting for the screen, mouse and keyboard, and a local server",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/kyora-sh/starfold.git",
|
|
9
|
+
"directory": "packages/starfold"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"starfold": "bin/starfold.js"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"bin",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
31
|
+
"typecheck": "tsc -p .",
|
|
32
|
+
"test": "bun run build && bun test test/",
|
|
33
|
+
"test:integration": "node test/integration.mjs"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=20"
|
|
37
|
+
},
|
|
38
|
+
"optionalDependencies": {
|
|
39
|
+
"@starfold/server-linux-x64-gnu": "0.3.2",
|
|
40
|
+
"@starfold/server-linux-arm64-gnu": "0.3.2",
|
|
41
|
+
"@starfold/server-darwin-arm64": "0.3.2",
|
|
42
|
+
"@starfold/server-win32-x64": "0.3.2"
|
|
43
|
+
}
|
|
44
|
+
}
|