Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 2x 6x 6x 23x 23x 3x 3x 4x 4x 3x 3x 4x 4x 3x 3x 3x 3x 3x 11x 1x 5x 2x 5x 3x 3x 3x 3x 3x 3x 6x 6x 2x 1x 1x 1x 4x 1x 1x 3x 3x 1x 2x 2x 2x | /**
* `bun run src/cli.ts --input dump.rdb --d1-name … --kv-name … [--dry-run]`
*
* Glue layer: reads an RDB file from disk, parses it, extracts rooms,
* and applies them to either a real {@link WranglerTarget} or a
* dry-run sink that prints every intended write without calling out.
*
* Logic is split for testability:
* - `parseArgs` — pure argv → config transform (lives in cli-args.ts).
* - `runMigrate` — pure (modulo injected deps).
* - `main` — thin real-process wrapper at the bottom.
*
* `parseArgs` is imported via the module namespace (not a named import)
* so tests can `vi.spyOn` it to cover the non-CliArgError branch in
* {@link main}. Direct-named imports are baked in at bundle time and
* wouldn't be rebindable.
*/
import type { Exec } from './targets/wrangler.ts';
import { parseRdb } from './parse-rdb.ts';
import { extractRooms } from './extract-rooms.ts';
import { applyRooms, type MigrationTarget, type ApplyStats } from './apply.ts';
import { WranglerTarget } from './targets/wrangler.ts';
import * as args from './cli-args.ts';
export { CliArgError, parseArgs } from './cli-args.ts';
export type { CliArgs } from './cli-args.ts';
import { CliArgError, type CliArgs } from './cli-args.ts';
export const USAGE: string = [
'ethercalc-migrate — import a legacy Redis dump into Cloudflare',
'',
'USAGE',
' bun run src/cli.ts --input <dump.rdb> --d1-name <NAME> --kv-name <NAME> [--dry-run]',
'',
'OPTIONS',
' --input <path> Path to an RDB file (produced by `redis-cli --rdb`).',
' --d1-name <name> D1 database binding name (e.g. ethercalc-rooms).',
' --kv-name <name> KV namespace binding name (e.g. ROOMS_INDEX).',
' --dry-run Print what would be written without calling wrangler.',
' -h, --help Show this help and exit.',
].join('\n');
/**
* DryRun target prints each intended write and counts them. Matches the
* real wrangler target's public interface.
*/
export class DryRunTarget implements MigrationTarget {
public readonly log: string[] = [];
private readonly write: (s: string) => void;
constructor(write: (s: string) => void) {
this.write = write;
}
private emit(action: string): void {
this.log.push(action);
this.write(`${action}\n`);
}
putSnapshot(room: string, snapshot: string): Promise<void> {
this.emit(`DO[${room}] put snapshot (${snapshot.length} bytes)`);
return Promise.resolve();
}
putLog(room: string, seq: number, cmd: string): Promise<void> {
this.emit(`DO[${room}] put log#${seq} = ${abbrev(cmd)}`);
return Promise.resolve();
}
putAudit(room: string, seq: number, cmd: string): Promise<void> {
this.emit(`DO[${room}] put audit#${seq} = ${abbrev(cmd)}`);
return Promise.resolve();
}
putChat(room: string, seq: number, msg: string): Promise<void> {
this.emit(`DO[${room}] put chat#${seq} = ${abbrev(msg)}`);
return Promise.resolve();
}
putEcell(room: string, user: string, cell: string): Promise<void> {
this.emit(`DO[${room}] put ecell:${user} = ${cell}`);
return Promise.resolve();
}
setRoomIndex(room: string, updatedAt: number): Promise<void> {
this.emit(`D1 rooms INSERT ${room} updated_at=${updatedAt}`);
this.emit(`KV rooms:exists:${room} = 1`);
return Promise.resolve();
}
}
function abbrev(s: string): string {
if (s.length <= 40) return JSON.stringify(s);
return `${JSON.stringify(s.slice(0, 37))}... (${s.length})`;
}
export interface RunDeps {
readFile: (path: string) => Buffer;
exec: Exec;
stdout: (s: string) => void;
stderr: (s: string) => void;
}
/**
* Produce a target for the given args + deps. Exported so tests can
* verify the dry-run/non-dry-run branches individually.
*/
export function buildTarget(args: CliArgs, deps: RunDeps): MigrationTarget {
if (args.dryRun) return new DryRunTarget(deps.stdout);
return new WranglerTarget({
d1Name: args.d1Name,
kvName: args.kvName,
exec: deps.exec,
});
}
/** End-to-end: read dump, apply to target, return stats. */
export async function runMigrate(
args: CliArgs,
deps: RunDeps,
): Promise<ApplyStats> {
const buf = deps.readFile(args.input);
const dump = parseRdb(buf);
const rooms = extractRooms(dump);
const target = buildTarget(args, deps);
const stats = await applyRooms(rooms, target);
deps.stdout(
`migrated ${stats.rooms} rooms ` +
`(${stats.snapshots} snapshots, ${stats.logEntries} log, ` +
`${stats.auditEntries} audit, ${stats.chatEntries} chat, ` +
`${stats.ecellEntries} ecell)\n`,
);
return stats;
}
/**
* CLI entry. Returns the exit code the caller should propagate.
*
* Behavior:
* - `--help`: write USAGE to stdout, return 0.
* - parse error: write to stderr, return 2.
* - success: return 0 (and stats printed to stdout).
* - runtime err: write message to stderr, return 1.
*/
export async function main(
argv: readonly string[],
deps: RunDeps,
): Promise<number> {
let parsed: CliArgs;
try {
parsed = args.parseArgs(argv);
} catch (err) {
if (!(err instanceof CliArgError)) throw err;
deps.stderr(`${err.message}\n`);
deps.stderr('Run with --help for usage.\n');
return 2;
}
if (parsed.help) {
deps.stdout(`${USAGE}\n`);
return 0;
}
try {
await runMigrate(parsed, deps);
return 0;
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
deps.stderr(`migrate failed: ${msg}\n`);
return 1;
}
}
|