Skip to content

Commit 887d3ed

Browse files
Merge pull request #14 from JavaScriptSolidServer/issue-13-indexer-cli
Indexer: CLI flags for the firehose switches (override env)
2 parents dc21537 + 1da15db commit 887d3ed

4 files changed

Lines changed: 97 additions & 4 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ npm start # indexer + read API
2727
# or run separately:
2828
npm run index # indexer only
2929
npm run serve # read API only
30+
31+
# indexer flags (override the matching env var; see --help):
32+
node index.js --hoses profiles --no-legacy # run a single hose, no legacy fallback
33+
node src/indexer.js --help
3034
```
3135

3236
## Read API

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Entry point: run the read API and the relay indexer in one process.
22
import { startServer } from './src/server.js';
3-
import { runIndexer } from './src/indexer.js';
3+
import { runIndexer, parseArgs, USAGE } from './src/indexer.js';
4+
5+
if (parseArgs().help) { console.log(USAGE); process.exit(0); }
46

57
await startServer();
6-
await runIndexer();
8+
await runIndexer(); // honours --hoses / --legacy / --no-legacy (override env)

src/indexer.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,38 @@ export function planIngest(allHoses = ALL_HOSES, env = process.env, legacyKindsA
4242
const SUB_ID = 'beacon';
4343
const RECONNECT_MS = 3000;
4444

45+
export const USAGE = `beacon indexer — firehose into MongoDB
46+
47+
Usage: node index.js [flags] (or: node src/indexer.js [flags])
48+
49+
Flags (override the matching env var):
50+
--hoses <list> comma list of hose names to run (env HOSES; default: all)
51+
--no-legacy disable the raw-upsert fallback for un-migrated kinds (INDEX_LEGACY_KINDS=0)
52+
--legacy force the legacy fallback on
53+
-h, --help show this help
54+
55+
Env: MONGODB_URI, MONGO_DB, RELAYS, PORT — see .env.example`;
56+
57+
/**
58+
* Parse indexer CLI flags into an env-overlay (only keys the user passed).
59+
* Merged over process.env by runIndexer so flags win and env is the fallback.
60+
*/
61+
export function parseArgs(argv = process.argv.slice(2)) {
62+
const out = {};
63+
for (let i = 0; i < argv.length; i++) {
64+
const a = argv[i];
65+
if (a === '-h' || a === '--help') out.help = true;
66+
else if (a === '--no-legacy') out.INDEX_LEGACY_KINDS = '0';
67+
else if (a === '--legacy') out.INDEX_LEGACY_KINDS = '1';
68+
else if (a === '--hoses' || a.startsWith('--hoses=')) {
69+
const next = argv[i + 1];
70+
out.HOSES = a.includes('=') ? a.slice(a.indexOf('=') + 1)
71+
: (next && !next.startsWith('--') ? argv[++i] : '');
72+
}
73+
}
74+
return out;
75+
}
76+
4577
function connectRelay(url, kinds, onEvent) {
4678
let ws, closed = false, timer;
4779
const open = () => {
@@ -63,7 +95,9 @@ function connectRelay(url, kinds, onEvent) {
6395

6496
export async function runIndexer() {
6597
const db = await connect();
66-
const { hoses, hoseFor, kinds, legacy, unknown } = planIngest();
98+
// CLI flags override env; env (process.env / .env) is the fallback.
99+
const env = { ...process.env, ...parseArgs() };
100+
const { hoses, hoseFor, kinds, legacy, unknown } = planIngest(ALL_HOSES, env);
67101
if (unknown.length) console.warn(`[beacon] unknown hose(s) in HOSES, ignored: ${unknown.join(', ')}`);
68102
if (!kinds.length) { console.warn('[beacon] no hoses enabled and no legacy kinds — nothing to index'); return { stop() {} }; }
69103
for (const h of hoses) await h.ensureIndexes(db);
@@ -87,4 +121,7 @@ export async function runIndexer() {
87121
return { stop };
88122
}
89123

90-
if (import.meta.url === `file://${process.argv[1]}`) runIndexer();
124+
if (import.meta.url === `file://${process.argv[1]}`) {
125+
if (parseArgs().help) { console.log(USAGE); process.exit(0); }
126+
runIndexer();
127+
}

test/indexer-args.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// CLI flag parsing for the indexer. parseArgs produces an env-overlay that
2+
// runIndexer merges over process.env (flags win, env is the fallback).
3+
import test from 'node:test';
4+
import assert from 'node:assert/strict';
5+
import { parseArgs, planIngest } from '../src/indexer.js';
6+
7+
const profiles = { name: 'profiles', kinds: [0] };
8+
const follows = { name: 'follows', kinds: [3] };
9+
const ALL = [profiles, follows];
10+
11+
test('no flags -> empty overlay', () => {
12+
assert.deepEqual(parseArgs([]), {});
13+
});
14+
15+
test('--hoses with a space-separated value', () => {
16+
assert.deepEqual(parseArgs(['--hoses', 'profiles,follows']), { HOSES: 'profiles,follows' });
17+
});
18+
19+
test('--hoses=value form', () => {
20+
assert.deepEqual(parseArgs(['--hoses=profiles']), { HOSES: 'profiles' });
21+
});
22+
23+
test('--hoses with no value -> empty string (planIngest treats as default)', () => {
24+
assert.deepEqual(parseArgs(['--hoses']), { HOSES: '' });
25+
assert.deepEqual(parseArgs(['--hoses', '--no-legacy']), { HOSES: '', INDEX_LEGACY_KINDS: '0' });
26+
});
27+
28+
test('--no-legacy and --legacy map to INDEX_LEGACY_KINDS', () => {
29+
assert.deepEqual(parseArgs(['--no-legacy']), { INDEX_LEGACY_KINDS: '0' });
30+
assert.deepEqual(parseArgs(['--legacy']), { INDEX_LEGACY_KINDS: '1' });
31+
});
32+
33+
test('--help / -h set help', () => {
34+
assert.equal(parseArgs(['--help']).help, true);
35+
assert.equal(parseArgs(['-h']).help, true);
36+
});
37+
38+
test('flags compose', () => {
39+
assert.deepEqual(parseArgs(['--hoses', 'profiles', '--no-legacy']),
40+
{ HOSES: 'profiles', INDEX_LEGACY_KINDS: '0' });
41+
});
42+
43+
test('flags override env when merged (the runIndexer contract)', () => {
44+
const env = { HOSES: 'profiles,follows', INDEX_LEGACY_KINDS: '1' };
45+
const merged = { ...env, ...parseArgs(['--hoses', 'profiles', '--no-legacy']) };
46+
const p = planIngest(ALL, merged, []);
47+
assert.deepEqual(p.hoses.map((h) => h.name), ['profiles']); // flag won over env
48+
assert.equal(p.legacy, false);
49+
assert.deepEqual(p.kinds, [0]);
50+
});

0 commit comments

Comments
 (0)