nostr-bray 2.1.0 → 2.2.0

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.
@@ -1,30 +1,28 @@
1
- import { assertEventSemanticallyValid, handlePublishEvent, handlePublishRaw } from '../../exports.js';
1
+ import { assertEventSemanticallyValid, handlePublishEvent, handlePublishRaw, resolveKind } from '../../exports.js';
2
+ import { nip19 } from 'nostr-tools';
2
3
  export async function dispatch(cmd, _cmdArgs, h, ctx, pool) {
3
- const { flag, flags, hasFlag, out } = h;
4
+ const { flag, flags, hasFlag, flagAny, flagsAny, out } = h;
4
5
  switch (cmd) {
5
6
  case 'event': {
6
- const kind = parseInt(flag('kind') ?? '', 10);
7
- if (!kind || isNaN(kind))
8
- throw new Error('Usage: event --kind <N> [--tag k=v] [--content s] [--relay url] [--no-publish]');
9
- const tagValues = flags('tag').map(t => {
10
- const eq = t.indexOf('=');
11
- if (eq === -1)
12
- return [t];
13
- return [t.slice(0, eq), t.slice(eq + 1)];
7
+ const kindArg = flagAny('kind', 'k');
8
+ if (!kindArg) {
9
+ throw new Error('Usage: event -k <number|name> [-c content] [-t k=v] [-p pubkey] [-e id] [-d ident] [--relay url]');
10
+ }
11
+ // Accepts a number or a registry kind name, e.g. -k "short text note"
12
+ const kind = resolveKind(kindArg);
13
+ const tagValues = collectTags(flagsAny('tag', 't'), {
14
+ p: flagsAny('p'), e: flagsAny('e'), d: flagsAny('d'), h: flagsAny('h'),
14
15
  });
15
- const content = flag('content') ?? '';
16
+ const content = flagAny('content', 'c') ?? '';
16
17
  const validationMode = parseValidationMode(flag('validation'));
17
- const relayOverrides = flags('relay');
18
+ const relayOverrides = flagsAny('relay', 'r');
18
19
  const pow = flag('pow') ? parseInt(flag('pow'), 10) : undefined;
19
20
  const powTimeoutMs = flag('pow-timeout') ? parseInt(flag('pow-timeout'), 10) : undefined;
21
+ const createdAtArg = flagAny('created-at', 'ts', 'time');
22
+ const createdAt = createdAtArg ? parseTimestamp(createdAtArg) : Math.floor(Date.now() / 1000);
20
23
  if (hasFlag('no-publish')) {
21
24
  const { minePow } = await import('../../event/pow.js');
22
- let template = {
23
- kind,
24
- created_at: Math.floor(Date.now() / 1000),
25
- tags: tagValues,
26
- content,
27
- };
25
+ let template = { kind, created_at: createdAt, tags: tagValues, content };
28
26
  if (pow !== undefined) {
29
27
  template = minePow(template, { difficulty: pow, pubkey: ctx.activePublicKeyHex, timeoutMs: powTimeoutMs }).template;
30
28
  }
@@ -32,18 +30,27 @@ export async function dispatch(cmd, _cmdArgs, h, ctx, pool) {
32
30
  reportWarnings(validation.issues);
33
31
  const sign = ctx.getSigningFunction();
34
32
  const event = await sign(template);
35
- console.log(JSON.stringify(event, null, 2));
33
+ // --envelope emits the relay wire form, ready to paste into a socket
34
+ console.log(hasFlag('envelope')
35
+ ? JSON.stringify(['EVENT', event])
36
+ : JSON.stringify(event, null, 2));
37
+ if (hasFlag('nevent'))
38
+ printNevent(event, relayOverrides);
36
39
  break;
37
40
  }
38
- out(await handlePublishEvent(ctx, pool, {
41
+ const result = await handlePublishEvent(ctx, pool, {
39
42
  kind,
40
43
  content,
41
44
  tags: tagValues,
45
+ createdAt,
42
46
  relays: relayOverrides.length ? relayOverrides : undefined,
43
47
  validationMode,
44
48
  pow,
45
49
  powTimeoutMs,
46
- }));
50
+ });
51
+ out(result);
52
+ if (hasFlag('nevent'))
53
+ printNevent(result.event, relayOverrides);
47
54
  break;
48
55
  }
49
56
  case 'publish-raw': {
@@ -102,3 +109,45 @@ function reportWarnings(issues) {
102
109
  console.error(`validation warning [${entry.code}]: ${entry.message}`);
103
110
  }
104
111
  }
112
+ /**
113
+ * Merge `--tag k=v` entries with the single-letter shortcuts.
114
+ *
115
+ * Shortcuts come after the explicit tags so that `-t p=<x> -p <y>` yields both,
116
+ * in the order written, rather than one silently replacing the other.
117
+ */
118
+ function collectTags(tagArgs, shortcuts) {
119
+ const tags = tagArgs.map(t => {
120
+ const eq = t.indexOf('=');
121
+ if (eq === -1)
122
+ return [t];
123
+ // A value may itself contain '=', so only split on the first one
124
+ return [t.slice(0, eq), t.slice(eq + 1)];
125
+ });
126
+ for (const [name, values] of Object.entries(shortcuts)) {
127
+ for (const v of values)
128
+ tags.push([name, v]);
129
+ }
130
+ return tags;
131
+ }
132
+ /** Parse a created_at: a unix timestamp, an ISO date, or `now`. */
133
+ function parseTimestamp(value) {
134
+ if (value === 'now')
135
+ return Math.floor(Date.now() / 1000);
136
+ if (/^\d+$/.test(value))
137
+ return Number(value);
138
+ const parsed = Date.parse(value);
139
+ if (Number.isNaN(parsed)) {
140
+ throw new Error(`Could not read "${value}" as a timestamp. Use a unix time, an ISO date, or "now".`);
141
+ }
142
+ return Math.floor(parsed / 1000);
143
+ }
144
+ /** Print the nevent code to stderr, so stdout stays parseable. */
145
+ function printNevent(event, relays) {
146
+ const code = nip19.neventEncode({
147
+ id: event.id,
148
+ author: event.pubkey,
149
+ kind: event.kind,
150
+ ...(relays.length ? { relays } : {}),
151
+ });
152
+ console.error(code);
153
+ }
@@ -29,12 +29,20 @@ export async function dispatch(cmd, cmdArgs, h, ctx, pool, _activeNpub) {
29
29
  out(await handleRelayInfo(req(1, 'relay-info <wss://url>')));
30
30
  break;
31
31
  case 'req': {
32
- // Support JSON filter on stdin when stdin is a pipe
32
+ // Support a JSON filter on stdin when stdin is a pipe. Not being a TTY
33
+ // does not guarantee stdin is readable — under a test runner, or a
34
+ // service manager that hands over a closed descriptor, the read throws
35
+ // EAGAIN and there is no filter to find anyway.
33
36
  let stdinFilter;
34
- const isTTY = process.stdin.isTTY;
35
- if (!isTTY) {
37
+ if (!process.stdin.isTTY) {
36
38
  const { readFileSync } = await import('node:fs');
37
- const raw = readFileSync(0, 'utf-8').trim();
39
+ let raw = '';
40
+ try {
41
+ raw = readFileSync(0, 'utf-8').trim();
42
+ }
43
+ catch {
44
+ raw = '';
45
+ }
38
46
  if (raw)
39
47
  stdinFilter = JSON.parse(raw);
40
48
  }
@@ -60,6 +68,59 @@ export async function dispatch(cmd, cmdArgs, h, ctx, pool, _activeNpub) {
60
68
  const paginate = hasFlag('paginate');
61
69
  const paginateIntervalMs = flag('paginate-interval') ? parseInt(flag('paginate-interval'), 10) : undefined;
62
70
  const maxPages = flag('max-pages') ? parseInt(flag('max-pages'), 10) : undefined;
71
+ // NIP-77 paths run before the normal REQ: both reconcile IDs with one
72
+ // relay rather than transferring events, which is the whole point.
73
+ if (hasFlag('ids-only') || hasFlag('only-missing')) {
74
+ const target = relayOverrides[0] ?? pool.getRelays(ctx.activeNpub).read[0];
75
+ if (!target)
76
+ throw new Error('--ids-only and --only-missing need a relay: pass --relay <url>');
77
+ const negFilter = {
78
+ ...(kindsRaw ? { kinds: kindsRaw.split(',').map(Number) } : {}),
79
+ ...(authorsRaw ? { authors: authorsRaw.split(',') } : {}),
80
+ ...(since !== undefined ? { since } : {}),
81
+ ...(until !== undefined ? { until } : {}),
82
+ };
83
+ // Reconcile against what we already hold: nothing for --ids-only, the
84
+ // supplied jsonl for --only-missing.
85
+ let local = [];
86
+ if (hasFlag('only-missing')) {
87
+ const { readFileSync } = await import('node:fs');
88
+ const { validateInputPath } = await import('../../validation.js');
89
+ const path = flag('only-missing');
90
+ if (!path)
91
+ throw new Error('Usage: req --only-missing <events.jsonl> [--relay url]');
92
+ local = readFileSync(validateInputPath(path), 'utf8')
93
+ .split('\n')
94
+ .filter(l => l.trim())
95
+ .map(l => {
96
+ const e = JSON.parse(l);
97
+ return { id: e.id, createdAt: e.created_at };
98
+ });
99
+ }
100
+ const recon = await pool.reconcileDirect(target, local, negFilter, {
101
+ timeoutMs: flag('timeout') ? parseInt(flag('timeout'), 10) : undefined,
102
+ maxIds: limit,
103
+ });
104
+ if (recon.truncated) {
105
+ console.error(`note: result truncated at ${recon.remoteOnlyIds.length} ids; raise --limit to see more`);
106
+ }
107
+ if (hasFlag('ids-only')) {
108
+ for (const id of recon.remoteOnlyIds)
109
+ console.log(id);
110
+ break;
111
+ }
112
+ // --only-missing: fetch just the events we do not already have
113
+ if (recon.remoteOnlyIds.length === 0)
114
+ break;
115
+ const missing = await handleRelayQuery(pool, ctx.activeNpub, {
116
+ ids: recon.remoteOnlyIds,
117
+ relays: [target],
118
+ limit: recon.remoteOnlyIds.length,
119
+ });
120
+ for (const ev of missing)
121
+ console.log(JSON.stringify(ev));
122
+ break;
123
+ }
63
124
  const queryArgs = {
64
125
  ...(stdinFilter ?? {
65
126
  ...(kindsRaw ? { kinds: kindsRaw.split(',').map(Number) } : {}),
@@ -8,6 +8,10 @@ export interface Helpers {
8
8
  flag(name: string, fallback?: string): string | undefined;
9
9
  flags(name: string): string[];
10
10
  hasFlag(name: string): boolean;
11
+ /** First value of whichever alias is present. Single-character names take one dash. */
12
+ flagAny(...names: string[]): string | undefined;
13
+ /** Every value across all the given aliases, in command-line order. */
14
+ flagsAny(...names: string[]): string[];
11
15
  out(data: unknown, humanFormatter?: (d: any) => string): void;
12
16
  }
13
17
  export type OutputMode = 'json' | 'human' | 'jsonl' | 'csv' | 'tsv';
@@ -123,6 +123,26 @@ export function makeHelpers(cmdArgs, outputMode) {
123
123
  }
124
124
  return result;
125
125
  }
126
+ // `-k` and `--kind` should behave identically, so aliases are resolved by
127
+ // dash count rather than requiring every callsite to spell out both forms.
128
+ const asSwitch = (name) => (name.length === 1 ? `-${name}` : `--${name}`);
129
+ function flagAny(...names) {
130
+ const wanted = names.map(asSwitch);
131
+ for (let i = 0; i < cmdArgs.length - 1; i++) {
132
+ if (wanted.includes(cmdArgs[i]))
133
+ return cmdArgs[i + 1];
134
+ }
135
+ return undefined;
136
+ }
137
+ function flagsAny(...names) {
138
+ const wanted = names.map(asSwitch);
139
+ const result = [];
140
+ for (let i = 0; i < cmdArgs.length - 1; i++) {
141
+ if (wanted.includes(cmdArgs[i]))
142
+ result.push(cmdArgs[i + 1]);
143
+ }
144
+ return result;
145
+ }
126
146
  function out(data, humanFormatter) {
127
147
  switch (outputMode) {
128
148
  case 'jsonl':
@@ -152,7 +172,7 @@ export function makeHelpers(cmdArgs, outputMode) {
152
172
  }
153
173
  }
154
174
  }
155
- return { req, flag, flags, hasFlag, out };
175
+ return { req, flag, flags, hasFlag, flagAny, flagsAny, out };
156
176
  }
157
177
  /** Parse a shell line into args, respecting quotes */
158
178
  export function parseShellLine(line) {
package/dist/cli/index.js CHANGED
@@ -203,7 +203,16 @@ Safety:
203
203
 
204
204
  Utility:
205
205
  req [--kinds N,N] [--authors hex,hex] [--since ts] [--limit N] [--relay url] [--min-trust level] Query events
206
- event --kind N [--tag k=v] [--content s] [--relay url] [--validation strict-known|off] Validate, sign and publish an arbitrary event
206
+ [--paginate] Walk 'until' backwards until --limit is met
207
+ (--max-pages N caps round-trips, default 20;
208
+ --paginate-interval ms waits between pages)
209
+ [--ids-only] Fetch only event IDs, via NIP-77 negentropy
210
+ [--only-missing <events.jsonl>] Fetch only events absent from that file (NIP-77)
211
+ event -k <number|name> [-c content] [-t k=v] [-p pk] [-e id] [-d ident] [-h group]
212
+ [--relay url] [--pow N] [--created-at ts|ISO|now] [--envelope] [--nevent]
213
+ [--validation strict-known|off] [--no-publish]
214
+ Validate, sign and publish an arbitrary event.
215
+ -k takes a kind number or a registry name, e.g. -k "short text note"
207
216
  validate-event [event-json|--file path] [--validation strict-known|off] Semantic protocol validation (no signature check)
208
217
  publish-raw [--file path] [--report] [--timeout ms] [--quorum n] [--validation strict-known|off] Validate, sign+broadcast event
209
218
  subscribe [--kinds N,N] [--authors hex] [--relay url] Live-tail events to stdout (JSONL) until SIGINT
@@ -46,3 +46,14 @@ export declare function lookupKind(kind: number): KindInfo;
46
46
  * dozens of kinds whose descriptions merely contain "note".
47
47
  */
48
48
  export declare function searchKinds(query: string, limit?: number): KindInfo[];
49
+ /**
50
+ * Resolve a kind given either a number or a description.
51
+ *
52
+ * `-k 1` and `-k "short text note"` should both work, which is what nak does
53
+ * and what makes the CLI usable without memorising kind numbers. An ambiguous
54
+ * name is an error rather than a guess: silently picking one of several
55
+ * matching kinds would publish the wrong thing.
56
+ *
57
+ * @throws if the name matches no kind, or more than one.
58
+ */
59
+ export declare function resolveKind(input: string): number;
@@ -84,3 +84,44 @@ export function searchKinds(query, limit = 20) {
84
84
  scored.sort((a, b) => a.score - b.score || a.kind - b.kind);
85
85
  return scored.slice(0, limit).map(s => lookupKind(s.kind));
86
86
  }
87
+ /**
88
+ * Resolve a kind given either a number or a description.
89
+ *
90
+ * `-k 1` and `-k "short text note"` should both work, which is what nak does
91
+ * and what makes the CLI usable without memorising kind numbers. An ambiguous
92
+ * name is an error rather than a guess: silently picking one of several
93
+ * matching kinds would publish the wrong thing.
94
+ *
95
+ * @throws if the name matches no kind, or more than one.
96
+ */
97
+ export function resolveKind(input) {
98
+ const trimmed = input.trim();
99
+ if (/^\d+$/.test(trimmed))
100
+ return Number(trimmed);
101
+ const q = trimmed.toLowerCase();
102
+ const exact = [];
103
+ const partial = [];
104
+ for (const [k, schema] of Object.entries(PINNED_KIND_REGISTRY.kinds)) {
105
+ const desc = (schema.description ?? '').toLowerCase();
106
+ if (!desc)
107
+ continue;
108
+ if (desc === q)
109
+ exact.push(Number(k));
110
+ else if (desc.includes(q))
111
+ partial.push(Number(k));
112
+ }
113
+ if (exact.length === 1)
114
+ return exact[0];
115
+ if (exact.length > 1) {
116
+ throw new Error(`Kind name "${input}" matches several kinds: ${exact.join(', ')}. Use the number.`);
117
+ }
118
+ if (partial.length === 1)
119
+ return partial[0];
120
+ if (partial.length > 1) {
121
+ const shown = partial.slice(0, 8)
122
+ .map(k => `${k} (${PINNED_KIND_REGISTRY.kinds[String(k)].description})`)
123
+ .join(', ');
124
+ throw new Error(`Kind name "${input}" is ambiguous — matches ${partial.length}: ${shown}${partial.length > 8 ? ', …' : ''}. Use the number.`);
125
+ }
126
+ throw new Error(`No kind matches "${input}". Try: nostr-bray kind "${input}"`);
127
+ }
@@ -274,6 +274,8 @@ export declare function handlePublishEvent(ctx: SigningContext, pool: RelayPool,
274
274
  pow?: number;
275
275
  /** Wall-clock budget for mining, in milliseconds. */
276
276
  powTimeoutMs?: number;
277
+ /** Unix timestamp for created_at. Defaults to now. */
278
+ createdAt?: number;
277
279
  }): Promise<ValidatedPostResult & {
278
280
  pow?: Omit<MinePowResult, 'template'>;
279
281
  }>;
@@ -480,7 +480,7 @@ export async function handleContactsUnfollow(ctx, pool, args) {
480
480
  export async function handlePublishEvent(ctx, pool, args) {
481
481
  let template = {
482
482
  kind: args.kind,
483
- created_at: Math.floor(Date.now() / 1000),
483
+ created_at: args.createdAt ?? Math.floor(Date.now() / 1000),
484
484
  tags: args.tags ?? [],
485
485
  content: args.content,
486
486
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nostr-bray",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "mcpName": "dev.forgesworn/bray",
5
5
  "description": "Trust-aware Nostr for AI and humans. Three dimensions of trust -- verification, proximity, and access -- woven into every interaction.",
6
6
  "type": "module",