mirror of
https://github.com/smogon/sprites.git
synced 2026-12-05 15:31:38 -06:00
Every deploy tarred the whole published tree into `smogonctl assets upload`: a few hundred megabytes of stamped files the home already had, and a 512-byte header for each of the thousands of links under __meta/. A served name carries a content hash and the tree is add-only, so a name the home has is a file that needn't travel. The deploy now opens the same stdin with a manifest -- every name with its size, every link -- and reads the reply off the command's stdout: the positions it hasn't got. Only those and the __meta/ files are then tarred into the same stream. The format is binary and length prefixed, so a name is whatever bytes a tar would carry for it. The reply is read strictly from the first byte: anything else on stdout, the command's own refusal or a shell profile printing over ssh, is relayed whole, stdin is closed so the command sees the end of the stream, and the deploy fails with its exit code. The progress bar counts what travels. `run --tar` still writes a bare tar, which the upload also takes. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014b7csvcRmyygDYQun3mE6E
95 lines
2.8 KiB
TypeScript
95 lines
2.8 KiB
TypeScript
// A stand-in for `smogonctl assets upload`: reads the manifest, asks for every
|
|
// other asset, takes the tar, and writes what it saw to the path in argv[2] as
|
|
// JSON. Prints one line of report after the reply, the way the real one does.
|
|
|
|
import * as fs from 'node:fs';
|
|
import tar from 'tar-stream';
|
|
|
|
let MAGIC = Buffer.from('smogonctl assets');
|
|
let out = process.argv[2]!;
|
|
|
|
let buf = Buffer.alloc(0);
|
|
let pending: {n: number, resolve: (b: Buffer) => void} | null = null;
|
|
let ended = false;
|
|
|
|
process.stdin.on('data', (chunk: Buffer) => {
|
|
buf = Buffer.concat([buf, chunk]);
|
|
serve();
|
|
});
|
|
process.stdin.on('end', () => {
|
|
ended = true;
|
|
serve();
|
|
});
|
|
|
|
function serve() {
|
|
if (pending !== null && buf.length >= pending.n) {
|
|
let {n, resolve} = pending;
|
|
pending = null;
|
|
let head = buf.subarray(0, n);
|
|
buf = buf.subarray(n);
|
|
resolve(head);
|
|
} else if (pending !== null && ended) {
|
|
throw new Error('manifest ended early');
|
|
}
|
|
}
|
|
|
|
function take(n: number): Promise<Buffer> {
|
|
return new Promise(resolve => {
|
|
pending = {n, resolve};
|
|
serve();
|
|
});
|
|
}
|
|
|
|
async function name(): Promise<string> {
|
|
let len = (await take(4)).readUInt32BE(0);
|
|
return (await take(len)).toString();
|
|
}
|
|
|
|
let header = await take(24);
|
|
if (!header.subarray(0, 16).equals(MAGIC)) {
|
|
throw new Error('not a manifest');
|
|
}
|
|
let count = header.readUInt32BE(20);
|
|
let manifest: {kind: number, name: string, size?: number, target?: string}[] = [];
|
|
for (let i = 0; i < count; i++) {
|
|
let kind = (await take(1))[0]!;
|
|
if (kind === 3) {
|
|
manifest.push({kind, name: await name(), target: await name()});
|
|
} else {
|
|
let size = Number((await take(8)).readBigUInt64BE(0));
|
|
manifest.push({kind, name: await name(), size});
|
|
}
|
|
}
|
|
let asked = manifest.map((e, i) => i).filter(i => manifest[i]!.kind === 1 && i % 2 === 0);
|
|
let reply = Buffer.alloc(24 + 4 * asked.length);
|
|
MAGIC.copy(reply);
|
|
reply.writeUInt32BE(1, 16);
|
|
reply.writeUInt32BE(asked.length, 20);
|
|
asked.forEach((index, i) => reply.writeUInt32BE(index, 24 + 4 * i));
|
|
process.stdout.write(reply);
|
|
|
|
// Whatever follows is the tar, or nothing at all.
|
|
let received: {name: string, size: number}[] = [];
|
|
let extract = tar.extract();
|
|
extract.on('entry', (header, stream, next) => {
|
|
received.push({name: header.name, size: header.size ?? 0});
|
|
stream.on('end', next);
|
|
stream.resume();
|
|
});
|
|
let done = new Promise<void>((resolve, reject) => {
|
|
extract.on('finish', resolve);
|
|
extract.on('error', reject);
|
|
});
|
|
if (buf.length > 0) {
|
|
extract.write(buf);
|
|
}
|
|
buf = Buffer.alloc(0);
|
|
if (ended) {
|
|
extract.end();
|
|
} else {
|
|
process.stdin.pipe(extract);
|
|
}
|
|
await done;
|
|
fs.writeFileSync(out, JSON.stringify({manifest, asked, received}));
|
|
process.stdout.write(`* ${received.length} received\n`);
|