Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_net/lens/js/test/adapter/make_lens_adapter.mjs

6.9 KiB, 1 run

created by r1870400018:65612, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1// make_lens_adapter.mjs -- builds `lens_daimond.mjs`: what Daimond's `dev/lens.mjs` becomes when it
2// adopts the extracted reader core.
3//
4// node test/adapter/make_lens_adapter.mjs <path to daimond/dev/lens.mjs>
5// LENS_UNDER_TEST=$PWD/test/adapter/lens_daimond.mjs node <daimond>/dev/lens.test.mjs
6//
7// The views (`turnRecords`, `deviceStates`, `errorRecords`, `consoleRecords`, the `cmd*` commands
8// and their formatting) are Daimond's own text, cut from its own file; they are what its test
9// pins. What the core now owns is deleted by name and imported or bound instead: the argument and
10// time helpers, the file and range helpers, the archive paths and loaders, the ingest (state,
11// dedupe, chunk assembly, scrub on the way in), and the redaction block, which is `redact.js`.
12// `cmdPull` keeps Daimond's report and calls the core's `pull`.
13//
14// The result is committed so the test needs no file of Daimond's but the two it runs. This script
15// is how it was made; it refuses to run if a name it deletes is not found exactly once, so a
16// change in Daimond's file is noticed rather than half-applied.
17import { readFileSync, writeFileSync } from 'node:fs';
18import { dirname, join } from 'node:path';
19import { fileURLToPath } from 'node:url';
20
21const HERE = dirname(fileURLToPath(import.meta.url));
22let lines = readFileSync(process.argv[2], 'utf8').split('\n');
23
24// The top-level function `name`: its line range, its leading comment block included. Daimond's
25// style closes a multi-line function with a lone `}` at the margin, which is what is looked for.
26function span(name) {
27 const re = new RegExp('^(?:async )?function ' + name + '\\(');
28 const at = lines.map((l, i) => re.test(l) ? i : -1).filter((i) => i >= 0);
29 if (at.length !== 1) throw new Error('expected exactly one top-level function ' + name + ', found ' + at.length);
30 let a = at[0], b = a;
31 const one = lines[a].trimEnd().endsWith('}') && (lines[a].match(/{/g) || []).length === (lines[a].match(/}/g) || []).length;
32 if (!one) {
33 b = lines.findIndex((l, i) => i > a && l === '}');
34 if (b < 0) throw new Error('no closing brace for ' + name);
35 }
36 while (a > 0 && /^\/\/\/?( |$)/.test(lines[a - 1])) a--;
37 return [a, b];
38}
39
40function drop(names) {
41 const spans = names.map((n) => span(n)).sort((x, y) => y[0] - x[0]);
42 for (const [a, b] of spans) lines.splice(a, b - a + 1);
43}
44
45function dropLine(prefix) {
46 const at = lines.map((l, i) => l.startsWith(prefix) ? i : -1).filter((i) => i >= 0);
47 if (at.length !== 1) throw new Error('expected exactly one line starting ' + prefix + ', found ' + at.length);
48 lines.splice(at[0], 1);
49}
50
51function indexOfLine(prefix) {
52 const at = lines.map((l, i) => l.startsWith(prefix) ? i : -1).filter((i) => i >= 0);
53 if (at.length !== 1) throw new Error('expected exactly one line starting ' + prefix + ', found ' + at.length);
54 return at[0];
55}
56
57// The redaction block (names, content shapes, scrubber): from its section heading to its end marker.
58{
59 const a = indexOfLine('// ── Redaction ──');
60 const b = indexOfLine('// ── END OF THE SHARED SCRUBBER BLOCK');
61 if (b <= a) throw new Error('the redaction block is not where it was');
62 lines.splice(a, b - a + 1);
63}
64
65// What the core owns, by name.
66const PURE = ['parseArgs', 'sinceMs', 'ensureDir', 'safeName', 'readJson', 'writeJson', 'readNdjson',
67 'rangeHas', 'rangeAdd', 'rangeCount', 'rangeMissing', 'num', 'hm', 'sinceLabel', 'utcLabel', 'covLabel',
68 'windowLabel', 'ago', 'dev7', 'idsMatch', 'emit'];
69const BOUND = ['appendLine', 'loadState', 'eventsFile', 'telFile', 'snapIndexFile', 'snapBodyFile',
70 'archiveDevices', 'wantDevice', 'coverage', 'loadTelemetry', 'loadEvents', 'loadSnapshotIndex', 'pendingSets'];
71const INGEST = ['eachBlock', 'traceFiles', 'devState', 'blockSeen', 'blockMark', 'partsFile', 'materialise',
72 'expire', 'closeSet', 'ingestRow', 'cmdPull'];
73drop([...PURE, ...BOUND, ...INGEST]);
74for (const re of ['const HEADER_RE', 'const CHUNK_RE', 'const EV_RE']) dropLine(re);
75
76// The text the core's pull replaces: Daimond's report, over the core's counts.
77const pull = `function cmdPull(args) {
78 const stat = R.pull({ noPull: !!args.bool['no-rsync'] });
79 stat.rsync = stat.pull;
80 if (JSONOUT) { console.log(JSON.stringify(stat)); return 0; }
81 console.log(\`pull: rsync \${stat.rsync}; \${stat.blocks} new block(s), \${stat.skipped} seen\`);
82 console.log(\` rows \${stat.rows} events \${stat.events} (+\${stat.dupEvents} dup)\`
83 + \` chunks \${stat.chunks} (+\${stat.dupChunks} dup)\`);
84 console.log(\` telemetry sets \${stat.telemetry} snapshots \${stat.snapshots}\`
85 + \` gapped \${stat.gapped} broken \${stat.broken}\`);
86 return 0;
87}
88`;
89{
90 const at = indexOfLine('// ── Reading the archive');
91 lines.splice(at, 0, ...pull.split('\n'));
92}
93
94// Imports, and the reader built from Daimond's own constants.
95{
96 const at = indexOfLine("const RELOAD_GRACE_MS");
97 const glue = `
98// ── The core's reader, handed Daimond's seams ────────────────────────
99import { createRedactor, createScrubber } from '../../redact.js';
100import {
101 ago, covLabel, createReader, dev7, emit, ensureDir, idsMatch, num, parseArgs, rangeAdd, rangeCount,
102 rangeHas, rangeMissing, readJson, readNdjson, safeName, sinceLabel, sinceMs, utcLabel, windowLabel, writeJson, hm,
103} from '../../reader.mjs';
104
105const R = createReader({
106 home: HOME,
107 mirror: 'traces',
108 pull: REMOTE ? [['rsync', '-az', REMOTE, TRACES + '/']] : null,
109 // The gateway's own session cookie, and the fields whose values are ids this reader navigates by.
110 scrub: createScrubber({
111 pairs: [{ k: 'cookie', g: 2, re: /(daimond_gw_sess=)([^;,\\s"'\\[\\]]{4,})/g }],
112 idKeys: { holder: 1, self: 1, nominated: 1, body: 1 },
113 }),
114 prepare: (b) => { if (b.config) b.config = createRedactor({ loose: true }).value(b.config); return b; },
115 tick: (b) => ({ iso: b.iso || null, stats: b.stats || null, ledger: b.ledger || [], trail: b.trail || [], diag: b.diag || [] }),
116 index: (b) => summariseSnapshot(b),
117 nameFor: (d) => nameFor(d),
118 recentMs: RECENT_MS, pendingTtlMs: PENDING_TTL_MS,
119});
120const { appendLine, archiveDevices, coverage, eventsFile, loadEvents, loadSnapshotIndex, loadState, loadTelemetry,
121 pendingSets, snapBodyFile, snapIndexFile, telFile, wantDevice } = R;
122`;
123 // After the last of Daimond's constants, which the reader is built from. Import declarations are
124 // hoisted, so they may stand here.
125 lines.splice(at + 1, 0, ...glue.split('\n'));
126}
127
128const body = lines.join('\n');
129const out = `/* lens_daimond.mjs -- GENERATED by make_lens_adapter.mjs. Daimond's dev/lens.mjs as it would
130 * stand after adopting fe2o3_net/lens/js/reader.mjs: its own views over the extracted reader core.
131 * Run Daimond's test against it with
132 * LENS_UNDER_TEST=<this file> node <daimond>/dev/lens.test.mjs
133 */
134${body}`;
135writeFileSync(join(HERE, 'lens_daimond.mjs'), out);
136console.log('wrote lens_daimond.mjs, ' + out.split('\n').length + ' lines (from ' + readFileSync(process.argv[2], 'utf8').split('\n').length + ')');