chirli 0.1.0 → 0.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.
- package/ARCHITECTURE.md +18 -20
- package/CONTRIBUTING.md +7 -3
- package/README.md +63 -55
- package/SECURITY.md +11 -11
- package/dist/allowlist.d.ts +3 -0
- package/dist/allowlist.js +9 -0
- package/dist/announcements.d.ts +4 -4
- package/dist/announcements.js +66 -34
- package/dist/attachments.d.ts +43 -0
- package/dist/attachments.js +340 -0
- package/dist/blossom.d.ts +2 -2
- package/dist/blossom.js +120 -66
- package/dist/bot-handlers.d.ts +9 -18
- package/dist/bot-handlers.js +125 -66
- package/dist/bot.d.ts +17 -20
- package/dist/bot.js +150 -55
- package/dist/client.d.ts +43 -39
- package/dist/client.js +476 -290
- package/dist/command-discovery.d.ts +2 -2
- package/dist/command-discovery.js +16 -7
- package/dist/command-schema.d.ts +2 -2
- package/dist/command-schema.js +62 -31
- package/dist/commands.d.ts +7 -6
- package/dist/commands.js +116 -51
- package/dist/defaults.js +13 -6
- package/dist/errors.d.ts +12 -12
- package/dist/errors.js +55 -18
- package/dist/inbox.d.ts +9 -7
- package/dist/inbox.js +107 -62
- package/dist/index.d.ts +25 -24
- package/dist/index.js +11 -11
- package/dist/key-sync.d.ts +4 -4
- package/dist/key-sync.js +88 -50
- package/dist/keys.d.ts +7 -6
- package/dist/keys.js +187 -93
- package/dist/message-storage-sqlite.d.ts +1 -1
- package/dist/message-storage-sqlite.js +32 -27
- package/dist/message-storage.d.ts +2 -2
- package/dist/messaging-metadata.d.ts +4 -4
- package/dist/messaging-metadata.js +73 -46
- package/dist/metadata.d.ts +3 -3
- package/dist/metadata.js +29 -15
- package/dist/modes.d.ts +7 -7
- package/dist/modes.js +53 -24
- package/dist/nip4e-compat.d.ts +2 -2
- package/dist/nip4e-compat.js +2 -2
- package/dist/nipcd.d.ts +14 -14
- package/dist/nipcd.js +225 -126
- package/dist/protocol.d.ts +7 -6
- package/dist/protocol.js +101 -54
- package/dist/public-inbox.d.ts +17 -15
- package/dist/public-inbox.js +129 -138
- package/dist/public-thread.d.ts +7 -4
- package/dist/public-thread.js +205 -88
- package/dist/relay-pool.d.ts +5 -5
- package/dist/relay-pool.js +172 -67
- package/dist/relay-roles.js +22 -13
- package/dist/relay.d.ts +11 -11
- package/dist/relay.js +194 -100
- package/dist/request-timing.d.ts +2 -2
- package/dist/request-timing.js +19 -7
- package/dist/session.js +7 -5
- package/dist/signer.d.ts +1 -1
- package/dist/signers/bunker-relays.d.ts +2 -2
- package/dist/signers/bunker-relays.js +16 -13
- package/dist/signers/bunker.d.ts +2 -2
- package/dist/signers/bunker.js +57 -37
- package/dist/signers/nsec.d.ts +2 -2
- package/dist/signers/nsec.js +8 -8
- package/dist/signers.d.ts +8 -8
- package/dist/signers.js +25 -18
- package/dist/storage-sqlite.d.ts +3 -1
- package/dist/storage-sqlite.js +110 -58
- package/dist/storage.d.ts +10 -6
- package/dist/store.d.ts +12 -8
- package/dist/store.js +156 -78
- package/docs/ACCESS.md +14 -25
- package/docs/API.md +125 -79
- package/docs/KEYS.md +3 -3
- package/docs/NIP-CD.md +29 -19
- package/docs/PUBLIC.md +33 -23
- package/docs/RECOVERY.md +7 -5
- package/docs/RELAYS.md +6 -6
- package/docs/SESSIONS.md +25 -21
- package/docs/SIGNERS.md +5 -3
- package/docs/STORAGE.md +15 -15
- package/docs/TUTORIAL.md +42 -39
- package/package.json +1 -37
- package/dist/inbound-policy.d.ts +0 -4
- package/dist/inbound-policy.js +0 -9
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Transport } from
|
|
2
|
-
import { type DiscoverCommandsOptions } from
|
|
1
|
+
import type { Transport } from "./relay.js";
|
|
2
|
+
import { type DiscoverCommandsOptions } from "./nipcd.js";
|
|
3
3
|
/** Read-only discovery never changes registrations or publishes account metadata. */
|
|
4
4
|
export declare function discoverCommandCatalog(transport: Transport, options: DiscoverCommandsOptions): Promise<import("./nipcd.js").CommandDiscoveryResult>;
|
|
@@ -1,23 +1,32 @@
|
|
|
1
|
-
import { hexKey } from
|
|
2
|
-
import { commandRelayURL, selectCommandDefinitions, } from
|
|
1
|
+
import { hexKey } from "./protocol.js";
|
|
2
|
+
import { commandRelayURL, selectCommandDefinitions, } from "./nipcd.js";
|
|
3
3
|
function discoveryRelays(relays) {
|
|
4
4
|
if (!Array.isArray(relays) || !relays.length) {
|
|
5
|
-
throw new TypeError(
|
|
5
|
+
throw new TypeError("Explicit command metadata relays are required");
|
|
6
6
|
}
|
|
7
7
|
return [...new Set(relays.map(commandRelayURL))];
|
|
8
8
|
}
|
|
9
9
|
/** Read-only discovery never changes registrations or publishes account metadata. */
|
|
10
10
|
export async function discoverCommandCatalog(transport, options) {
|
|
11
|
-
if (!options.authors?.length ||
|
|
12
|
-
|
|
11
|
+
if (!options.authors?.length ||
|
|
12
|
+
!options.authors.every(hexKey) ||
|
|
13
|
+
(options.ids !== undefined &&
|
|
14
|
+
(!options.ids.length ||
|
|
15
|
+
!options.ids.every((id) => typeof id === "string")))) {
|
|
16
|
+
throw new TypeError("Explicit valid command authors/IDs are required");
|
|
13
17
|
}
|
|
14
18
|
const limit = options.limit ?? 200;
|
|
15
19
|
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 1000) {
|
|
16
|
-
throw new TypeError(
|
|
20
|
+
throw new TypeError("Command discovery limit must be in 1..1000");
|
|
17
21
|
}
|
|
18
22
|
const relays = discoveryRelays(options.relays), authors = [...new Set(options.authors)];
|
|
19
23
|
const [definitions, deletions] = await Promise.all([
|
|
20
|
-
transport.query(relays, {
|
|
24
|
+
transport.query(relays, {
|
|
25
|
+
kinds: [31992],
|
|
26
|
+
authors,
|
|
27
|
+
...(options.ids ? { "#d": options.ids } : {}),
|
|
28
|
+
limit,
|
|
29
|
+
}),
|
|
21
30
|
transport.query(relays, { kinds: [5], authors, limit }),
|
|
22
31
|
]);
|
|
23
32
|
return selectCommandDefinitions([...definitions, ...deletions], authors, options.ids);
|
package/dist/command-schema.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { z } from
|
|
2
|
-
import type { CommandArgument } from
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { CommandArgument } from "./nipcd.js";
|
|
3
3
|
/** Positional NIP-CD schemas deliberately accept a small, predictable Zod subset. */
|
|
4
4
|
export declare function commandArguments(schema: z.ZodObject): CommandArgument[];
|
package/dist/command-schema.js
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
import { z } from
|
|
1
|
+
import { z } from "zod";
|
|
2
2
|
/** Positional NIP-CD schemas deliberately accept a small, predictable Zod subset. */
|
|
3
3
|
export function commandArguments(schema) {
|
|
4
|
-
const fail = (name, reason) => {
|
|
4
|
+
const fail = (name, reason) => {
|
|
5
|
+
throw new TypeError(`Unsupported command args schema at ${name}: ${reason}`);
|
|
6
|
+
};
|
|
5
7
|
const check = (field, name) => {
|
|
6
8
|
const def = field._zod.def;
|
|
7
|
-
if (def.coerce ||
|
|
8
|
-
|
|
9
|
+
if (def.coerce ||
|
|
10
|
+
def.checks?.some((item) => item._zod.def.check === "overwrite"))
|
|
11
|
+
fail(name, "coercions and overwrites/transforms are not supported");
|
|
9
12
|
};
|
|
10
13
|
if (!(schema instanceof z.ZodObject))
|
|
11
|
-
fail(
|
|
12
|
-
check(schema,
|
|
14
|
+
fail("args", "expected a Zod 4 z.object");
|
|
15
|
+
check(schema, "args");
|
|
13
16
|
const catchall = schema._zod.def.catchall;
|
|
14
|
-
if (catchall && catchall._zod.def.type !==
|
|
15
|
-
fail(
|
|
17
|
+
if (catchall && catchall._zod.def.type !== "never")
|
|
18
|
+
fail("args", "catchall/loose objects cannot declare positional arguments");
|
|
16
19
|
return Object.entries(schema.shape).map(([name, outer]) => {
|
|
17
|
-
if (name ===
|
|
18
|
-
fail(name,
|
|
20
|
+
if (name === "__proto__")
|
|
21
|
+
fail(name, "reserved top-level argument name");
|
|
19
22
|
let field = outer, required = true;
|
|
20
23
|
check(field, name);
|
|
21
24
|
const meta = field.meta() ?? {};
|
|
@@ -26,43 +29,71 @@ export function commandArguments(schema) {
|
|
|
26
29
|
const metadata = { ...field.meta(), ...meta };
|
|
27
30
|
check(field, name);
|
|
28
31
|
const type = field._zod.def.type;
|
|
29
|
-
|
|
32
|
+
const inferred = type === "string"
|
|
33
|
+
? "word"
|
|
34
|
+
: type === "bigint"
|
|
35
|
+
? "number"
|
|
36
|
+
: type === "boolean"
|
|
37
|
+
? "bool"
|
|
38
|
+
: type === "enum"
|
|
39
|
+
? "enum"
|
|
40
|
+
: undefined;
|
|
30
41
|
const explicit = metadata.nipcdType;
|
|
31
|
-
if (explicit !== undefined &&
|
|
32
|
-
|
|
42
|
+
if (explicit !== undefined &&
|
|
43
|
+
![
|
|
44
|
+
"word",
|
|
45
|
+
"text",
|
|
46
|
+
"pubkey",
|
|
47
|
+
"event",
|
|
48
|
+
"relay",
|
|
49
|
+
"address",
|
|
50
|
+
"number",
|
|
51
|
+
"bool",
|
|
52
|
+
"enum",
|
|
53
|
+
].includes(explicit))
|
|
54
|
+
fail(name, "unknown nipcdType metadata");
|
|
33
55
|
const wire = (explicit ?? inferred);
|
|
34
|
-
if (wire ===
|
|
56
|
+
if (wire === "address") {
|
|
35
57
|
if (!(field instanceof z.ZodObject))
|
|
36
|
-
fail(name,
|
|
58
|
+
fail(name, "address requires an object matching CommandAddress");
|
|
37
59
|
const shape = field.shape;
|
|
38
|
-
if (Object.keys(shape).sort().join(
|
|
39
|
-
fail(name,
|
|
60
|
+
if (Object.keys(shape).sort().join(",") !== "identifier,kind,pubkey,relays")
|
|
61
|
+
fail(name, "address needs kind, pubkey, identifier and relays");
|
|
40
62
|
for (const [key, child] of Object.entries(shape)) {
|
|
41
63
|
const value = child;
|
|
42
64
|
check(value, `${name}.${key}`);
|
|
43
|
-
const expected = key ===
|
|
65
|
+
const expected = key === "kind" ? "number" : key === "relays" ? "array" : "string";
|
|
44
66
|
if (value._zod.def.type !== expected)
|
|
45
67
|
fail(name, `invalid address ${key} schema`);
|
|
46
|
-
if (key ===
|
|
68
|
+
if (key === "relays") {
|
|
47
69
|
const element = value.element;
|
|
48
70
|
check(element, `${name}.relays`);
|
|
49
|
-
if (element._zod.def.type !==
|
|
50
|
-
fail(name,
|
|
71
|
+
if (element._zod.def.type !== "string")
|
|
72
|
+
fail(name, "address relays must be strings");
|
|
51
73
|
}
|
|
52
74
|
}
|
|
53
75
|
check(field, name);
|
|
54
|
-
if (field._zod.def.catchall &&
|
|
55
|
-
|
|
76
|
+
if (field._zod.def.catchall &&
|
|
77
|
+
field._zod.def.catchall?._zod.def.type !== "never")
|
|
78
|
+
fail(name, "address catchall is unsupported");
|
|
56
79
|
}
|
|
57
|
-
else if (!inferred ||
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (
|
|
80
|
+
else if (!inferred ||
|
|
81
|
+
!(wire === inferred ||
|
|
82
|
+
(inferred === "word" &&
|
|
83
|
+
["text", "pubkey", "event", "relay"].includes(wire))))
|
|
84
|
+
fail(name, "use string, bigint, boolean, string enum, optional(), or an explicitly annotated address object");
|
|
85
|
+
if (metadata.label !== undefined && typeof metadata.label !== "string")
|
|
86
|
+
fail(name, "label metadata must be a string");
|
|
87
|
+
const argument = {
|
|
88
|
+
name,
|
|
89
|
+
type: wire,
|
|
90
|
+
required,
|
|
91
|
+
label: metadata.label ?? name,
|
|
92
|
+
};
|
|
93
|
+
if (wire === "enum") {
|
|
63
94
|
const choices = Object.values(field._zod.def.entries);
|
|
64
|
-
if (!choices.every(value => typeof value ===
|
|
65
|
-
fail(name,
|
|
95
|
+
if (!choices.every((value) => typeof value === "string"))
|
|
96
|
+
fail(name, "only string enum choices are supported");
|
|
66
97
|
argument.choices = choices;
|
|
67
98
|
}
|
|
68
99
|
return argument;
|
package/dist/commands.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { type
|
|
3
|
-
import type
|
|
4
|
-
import type {
|
|
1
|
+
import { type Event } from "nostr-tools";
|
|
2
|
+
import { type Identity } from "./protocol.js";
|
|
3
|
+
import { type CommandDefinition } from "./nipcd.js";
|
|
4
|
+
import type { Store } from "./store.js";
|
|
5
|
+
import type { Transport } from "./relay.js";
|
|
5
6
|
export interface CommandSyncResult {
|
|
6
7
|
added: string[];
|
|
7
8
|
updated: string[];
|
|
@@ -24,8 +25,8 @@ export declare class CommandCatalog {
|
|
|
24
25
|
private deleted;
|
|
25
26
|
sync(definitions: CommandDefinition[], removals?: string[], active?: () => boolean): Promise<CommandSyncResult>;
|
|
26
27
|
private reconcile;
|
|
27
|
-
definition(id: string):
|
|
28
|
-
revocation(id: string):
|
|
28
|
+
definition(id: string): Event | undefined;
|
|
29
|
+
revocation(id: string): Event | undefined;
|
|
29
30
|
hasPending(): boolean;
|
|
30
31
|
idle(): Promise<void>;
|
|
31
32
|
}
|
package/dist/commands.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { verifyEvent } from
|
|
2
|
-
import { signChecked, validTimestamp } from
|
|
3
|
-
import { commandAddress, decodeCommandDefinition, encodeCommandDefinition, encodeCommandRevocation, validateCommandDefinition, } from
|
|
1
|
+
import { verifyEvent } from "nostr-tools";
|
|
2
|
+
import { signChecked, validTimestamp } from "./protocol.js";
|
|
3
|
+
import { commandAddress, decodeCommandDefinition, encodeCommandDefinition, encodeCommandRevocation, validateCommandDefinition, } from "./nipcd.js";
|
|
4
4
|
const PAGE_SIZE = 1000;
|
|
5
5
|
const MAX_PAGES = 100;
|
|
6
|
-
const identifier = (event) => event.tags.find(tag => tag[0] ===
|
|
6
|
+
const identifier = (event) => event.tags.find((tag) => tag[0] === "d")?.[1] ?? "";
|
|
7
7
|
const newer = (a, b) => a.created_at > b.created_at || (a.created_at === b.created_at && a.id < b.id);
|
|
8
|
-
const equal = (a, b) => JSON.stringify(validateCommandDefinition(a)) ===
|
|
8
|
+
const equal = (a, b) => JSON.stringify(validateCommandDefinition(a)) ===
|
|
9
|
+
JSON.stringify(validateCommandDefinition(b));
|
|
9
10
|
/** Reconciles one account's authoritative command registry on its configured write relays. */
|
|
10
11
|
export class CommandCatalog {
|
|
11
12
|
identity;
|
|
@@ -19,35 +20,49 @@ export class CommandCatalog {
|
|
|
19
20
|
this.store = store;
|
|
20
21
|
this.transport = transport;
|
|
21
22
|
this.relays = relays;
|
|
22
|
-
this.state = store.metadata(
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
this.state = store.metadata("command-catalog") ?? {
|
|
24
|
+
version: 1,
|
|
25
|
+
known: [],
|
|
26
|
+
pending: [],
|
|
27
|
+
};
|
|
28
|
+
if (this.state.version !== 1 ||
|
|
29
|
+
!Array.isArray(this.state.known) ||
|
|
30
|
+
!Array.isArray(this.state.pending)) {
|
|
31
|
+
throw new Error("Unsupported command catalog state");
|
|
25
32
|
}
|
|
26
33
|
// Old journals may contain unrelated event kinds. Retain only supported command operations.
|
|
27
|
-
const supported = (events) => events.flatMap(event => {
|
|
28
|
-
const valid = event && (event.kind === 31992 || event.kind === 5)
|
|
34
|
+
const supported = (events) => events.flatMap((event) => {
|
|
35
|
+
const valid = event && (event.kind === 31992 || event.kind === 5)
|
|
36
|
+
? this.verified(event, event.kind)
|
|
37
|
+
: undefined;
|
|
29
38
|
return valid ? [valid] : [];
|
|
30
39
|
});
|
|
31
40
|
const known = supported(this.state.known), pending = supported(this.state.pending);
|
|
32
|
-
if (known.length !== this.state.known.length ||
|
|
41
|
+
if (known.length !== this.state.known.length ||
|
|
42
|
+
pending.length !== this.state.pending.length) {
|
|
33
43
|
this.save({ version: 1, known, pending });
|
|
34
44
|
}
|
|
35
45
|
}
|
|
36
46
|
verified(input, kind) {
|
|
37
47
|
try {
|
|
38
48
|
const event = JSON.parse(JSON.stringify(input));
|
|
39
|
-
if (event.kind === kind &&
|
|
49
|
+
if (event.kind === kind &&
|
|
50
|
+
event.pubkey === this.identity.pubkey &&
|
|
51
|
+
validTimestamp(event.created_at) &&
|
|
52
|
+
verifyEvent(event))
|
|
40
53
|
return event;
|
|
41
54
|
}
|
|
42
|
-
catch {
|
|
55
|
+
catch {
|
|
56
|
+
/* Untrusted frames never become registry or deletion evidence. */
|
|
57
|
+
}
|
|
43
58
|
}
|
|
44
59
|
save(state) {
|
|
45
|
-
this.store.saveMetadata(
|
|
60
|
+
this.store.saveMetadata("command-catalog", state);
|
|
46
61
|
this.state = state;
|
|
47
62
|
}
|
|
48
63
|
check(active) {
|
|
49
64
|
if (!active())
|
|
50
|
-
throw new Error(
|
|
65
|
+
throw new Error("Command synchronization canceled");
|
|
51
66
|
}
|
|
52
67
|
async readKind(relay, kind, active) {
|
|
53
68
|
const events = new Map();
|
|
@@ -55,58 +70,80 @@ export class CommandCatalog {
|
|
|
55
70
|
for (let page = 0; page < MAX_PAGES; page++) {
|
|
56
71
|
this.check(active);
|
|
57
72
|
const frames = await this.transport.query([relay], {
|
|
58
|
-
kinds: [kind],
|
|
73
|
+
kinds: [kind],
|
|
74
|
+
authors: [this.identity.pubkey],
|
|
75
|
+
limit: PAGE_SIZE,
|
|
59
76
|
...(until === undefined ? {} : { until }),
|
|
60
77
|
});
|
|
61
78
|
this.check(active);
|
|
62
|
-
const valid = frames.flatMap(frame => {
|
|
79
|
+
const valid = frames.flatMap((frame) => {
|
|
63
80
|
const event = this.verified(frame, kind);
|
|
64
|
-
return event && (until === undefined || event.created_at <= until)
|
|
81
|
+
return event && (until === undefined || event.created_at <= until)
|
|
82
|
+
? [event]
|
|
83
|
+
: [];
|
|
65
84
|
});
|
|
66
85
|
for (const event of valid)
|
|
67
86
|
events.set(event.id, event);
|
|
68
87
|
if (frames.length < PAGE_SIZE)
|
|
69
88
|
return [...events.values()];
|
|
70
|
-
const oldest = Math.min(...valid.map(event => event.created_at));
|
|
89
|
+
const oldest = Math.min(...valid.map((event) => event.created_at));
|
|
71
90
|
// Never infer deletions from a truncated or saturated timestamp bucket.
|
|
72
91
|
if (!Number.isFinite(oldest) || oldest === until)
|
|
73
|
-
throw new Error(
|
|
92
|
+
throw new Error("Command query incomplete: timestamp boundary saturated");
|
|
74
93
|
until = oldest;
|
|
75
94
|
}
|
|
76
|
-
throw new Error(
|
|
95
|
+
throw new Error("Command query incomplete: page limit reached");
|
|
77
96
|
}
|
|
78
97
|
compact(events) {
|
|
79
98
|
const slots = new Map();
|
|
80
99
|
for (const event of events) {
|
|
81
|
-
const keys = event.kind === 31992
|
|
82
|
-
|
|
100
|
+
const keys = event.kind === 31992
|
|
101
|
+
? [`definition:${identifier(event)}`]
|
|
102
|
+
: event.kind === 5
|
|
103
|
+
? event.tags
|
|
104
|
+
.filter((tag) => tag[0] === "a" || tag[0] === "e")
|
|
105
|
+
.map((tag) => `${tag[0]}:${tag[1]}`)
|
|
106
|
+
: [];
|
|
83
107
|
for (const key of keys) {
|
|
84
108
|
const old = slots.get(key);
|
|
85
109
|
if (!old || newer(event, old))
|
|
86
110
|
slots.set(key, event);
|
|
87
111
|
}
|
|
88
112
|
}
|
|
89
|
-
return [
|
|
113
|
+
return [
|
|
114
|
+
...new Map([...slots.values()].map((event) => [event.id, event])).values(),
|
|
115
|
+
];
|
|
90
116
|
}
|
|
91
117
|
deleted(event, events) {
|
|
92
118
|
const address = commandAddress(this.identity.pubkey, identifier(event));
|
|
93
|
-
return events.some(deletion => deletion.kind === 5 &&
|
|
94
|
-
(tag[0] ===
|
|
119
|
+
return events.some((deletion) => deletion.kind === 5 &&
|
|
120
|
+
deletion.tags.some((tag) => (tag[0] === "a" &&
|
|
121
|
+
tag[1] === address &&
|
|
122
|
+
deletion.created_at >= event.created_at) ||
|
|
123
|
+
(tag[0] === "e" && tag[1] === event.id)));
|
|
95
124
|
}
|
|
96
125
|
sync(definitions, removals = [], active = () => true) {
|
|
97
126
|
const desired = definitions.map(validateCommandDefinition);
|
|
98
|
-
const task = (this.operation ?? Promise.resolve())
|
|
127
|
+
const task = (this.operation ?? Promise.resolve())
|
|
128
|
+
.catch(() => { })
|
|
129
|
+
.then(() => this.reconcile(desired, removals, active));
|
|
99
130
|
this.operation = task;
|
|
100
|
-
void task
|
|
101
|
-
|
|
131
|
+
void task
|
|
132
|
+
.finally(() => {
|
|
133
|
+
if (this.operation === task)
|
|
134
|
+
this.operation = undefined;
|
|
135
|
+
})
|
|
136
|
+
.catch(() => { });
|
|
102
137
|
return task;
|
|
103
138
|
}
|
|
104
139
|
async reconcile(definitions, removals, active) {
|
|
105
140
|
// Query every write relay independently. Partial transport query success is unsafe here.
|
|
106
|
-
const reads = await Promise.all(this.relays.map(relay => this.readKind(relay, 31992, active)));
|
|
141
|
+
const reads = await Promise.all(this.relays.map((relay) => this.readKind(relay, 31992, active)));
|
|
107
142
|
this.check(active);
|
|
108
|
-
const events = [
|
|
109
|
-
|
|
143
|
+
const events = [
|
|
144
|
+
...new Map([...reads.flat(), ...this.state.known, ...this.state.pending].map((event) => [event.id, event])).values(),
|
|
145
|
+
];
|
|
146
|
+
const desired = new Map(definitions.map((definition) => [definition.id, definition]));
|
|
110
147
|
const latest = new Map();
|
|
111
148
|
// Fresh relay presence and unacknowledged definitions are actionable; confirmed cache alone is not.
|
|
112
149
|
for (const event of [...reads.flat(), ...this.state.pending]) {
|
|
@@ -116,21 +153,33 @@ export class CommandCatalog {
|
|
|
116
153
|
if (!old || newer(event, old))
|
|
117
154
|
latest.set(id, event);
|
|
118
155
|
}
|
|
119
|
-
const pending = [], known = new Map(events.map(event => [event.id, event]));
|
|
120
|
-
const result = {
|
|
156
|
+
const pending = [], known = new Map(events.map((event) => [event.id, event]));
|
|
157
|
+
const result = {
|
|
158
|
+
added: [],
|
|
159
|
+
updated: [],
|
|
160
|
+
removed: [],
|
|
161
|
+
};
|
|
121
162
|
const now = Math.floor(Date.now() / 1000);
|
|
122
|
-
const onEveryRelay = (event) => reads.every(read => read.some(copy => copy.id === event.id));
|
|
123
|
-
const enqueue = (event) => {
|
|
124
|
-
|
|
163
|
+
const onEveryRelay = (event) => reads.every((read) => read.some((copy) => copy.id === event.id));
|
|
164
|
+
const enqueue = (event) => {
|
|
165
|
+
known.set(event.id, event);
|
|
166
|
+
if (!onEveryRelay(event))
|
|
167
|
+
pending.push(event);
|
|
168
|
+
};
|
|
125
169
|
const timeFor = (id) => {
|
|
126
170
|
const address = commandAddress(this.identity.pubkey, id);
|
|
127
|
-
return Math.max(now, ...events
|
|
128
|
-
(event
|
|
171
|
+
return Math.max(now, ...events
|
|
172
|
+
.filter((event) => (event.kind === 31992 && identifier(event) === id) ||
|
|
173
|
+
(event.kind === 5 &&
|
|
174
|
+
event.tags.some((tag) => tag[0] === "a" && tag[1] === address)))
|
|
175
|
+
.map((event) => event.created_at + 1));
|
|
129
176
|
};
|
|
130
177
|
for (const [id, definition] of desired) {
|
|
131
178
|
this.check(active);
|
|
132
|
-
const previous = latest.get(id), parsed = previous && !this.deleted(previous, events)
|
|
133
|
-
|
|
179
|
+
const previous = latest.get(id), parsed = previous && !this.deleted(previous, events)
|
|
180
|
+
? decodeCommandDefinition(previous)
|
|
181
|
+
: undefined;
|
|
182
|
+
if (previous && parsed && equal(parsed.definition, definition)) {
|
|
134
183
|
enqueue(previous);
|
|
135
184
|
}
|
|
136
185
|
else {
|
|
@@ -140,15 +189,22 @@ export class CommandCatalog {
|
|
|
140
189
|
}
|
|
141
190
|
}
|
|
142
191
|
const prefix = `31992:${this.identity.pubkey}:`;
|
|
143
|
-
const withdrawn = this.state.pending.flatMap(event => event.kind === 5
|
|
192
|
+
const withdrawn = this.state.pending.flatMap((event) => event.kind === 5
|
|
193
|
+
? event.tags.flatMap((tag) => tag[0] === "a" && tag[1]?.startsWith(prefix)
|
|
194
|
+
? [tag[1].slice(prefix.length)]
|
|
195
|
+
: [])
|
|
196
|
+
: []);
|
|
144
197
|
for (const id of new Set([...latest.keys(), ...withdrawn, ...removals])) {
|
|
145
198
|
if (desired.has(id))
|
|
146
199
|
continue;
|
|
147
200
|
const previous = latest.get(id);
|
|
148
201
|
const address = commandAddress(this.identity.pubkey, id);
|
|
149
|
-
const deletion = events
|
|
202
|
+
const deletion = events
|
|
203
|
+
.filter((event) => event.kind === 5 &&
|
|
204
|
+
event.tags.some((tag) => tag[0] === "a" && tag[1] === address))
|
|
150
205
|
.sort((a, b) => b.created_at - a.created_at || a.id.localeCompare(b.id))[0];
|
|
151
|
-
if (deletion &&
|
|
206
|
+
if (deletion &&
|
|
207
|
+
(!previous || deletion.created_at >= previous.created_at)) {
|
|
152
208
|
// A tombstone staged before a crash is not proof every relay received it.
|
|
153
209
|
if (!onEveryRelay(deletion))
|
|
154
210
|
result.removed.push(id);
|
|
@@ -160,29 +216,38 @@ export class CommandCatalog {
|
|
|
160
216
|
}
|
|
161
217
|
}
|
|
162
218
|
this.check(active);
|
|
163
|
-
this.save({
|
|
219
|
+
this.save({
|
|
220
|
+
version: 1,
|
|
221
|
+
known: this.compact([...known.values()]),
|
|
222
|
+
pending,
|
|
223
|
+
});
|
|
164
224
|
while (this.state.pending.length) {
|
|
165
225
|
this.check(active);
|
|
166
226
|
const event = this.state.pending[0];
|
|
167
|
-
const outcomes = await Promise.allSettled(this.relays.map(relay => this.transport.publish([relay], event)));
|
|
168
|
-
const failures = outcomes.flatMap(outcome => outcome.status ===
|
|
227
|
+
const outcomes = await Promise.allSettled(this.relays.map((relay) => this.transport.publish([relay], event)));
|
|
228
|
+
const failures = outcomes.flatMap((outcome) => outcome.status === "rejected" ? [outcome.reason] : []);
|
|
169
229
|
if (failures.length)
|
|
170
|
-
throw new AggregateError(failures,
|
|
230
|
+
throw new AggregateError(failures, "Command publication failed on write relays");
|
|
171
231
|
this.save({ ...this.state, pending: this.state.pending.slice(1) });
|
|
172
232
|
}
|
|
173
233
|
return result;
|
|
174
234
|
}
|
|
175
235
|
definition(id) {
|
|
176
|
-
return this.state.known
|
|
236
|
+
return this.state.known
|
|
237
|
+
.filter((event) => event.kind === 31992 && identifier(event) === id)
|
|
177
238
|
.sort((a, b) => b.created_at - a.created_at || a.id.localeCompare(b.id))[0];
|
|
178
239
|
}
|
|
179
240
|
revocation(id) {
|
|
180
241
|
const address = commandAddress(this.identity.pubkey, id);
|
|
181
|
-
return this.state.known
|
|
242
|
+
return this.state.known
|
|
243
|
+
.filter((event) => event.kind === 5 &&
|
|
244
|
+
event.tags.some((tag) => tag[0] === "a" && tag[1] === address))
|
|
182
245
|
.sort((a, b) => b.created_at - a.created_at || a.id.localeCompare(b.id))[0];
|
|
183
246
|
}
|
|
184
247
|
hasPending() {
|
|
185
248
|
return this.state.pending.length > 0;
|
|
186
249
|
}
|
|
187
|
-
async idle() {
|
|
250
|
+
async idle() {
|
|
251
|
+
await this.operation?.catch(() => { });
|
|
252
|
+
}
|
|
188
253
|
}
|
package/dist/defaults.js
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
/** PsstPsst-derived DM defaults; nip17.com intentionally excluded. Availability is not guaranteed. */
|
|
2
2
|
export const DEFAULT_DM_RELAYS = Object.freeze([
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
"wss://relay.psstpsst.chat/",
|
|
4
|
+
"wss://nos.lol/",
|
|
5
|
+
"wss://relay.ditto.pub/",
|
|
6
6
|
]);
|
|
7
7
|
/** PsstPsst's loadAccountReadRelays/loadAccountWriteRelays fall back to DISCOVERY_RELAYS. */
|
|
8
8
|
export const DEFAULT_READ_RELAYS = Object.freeze([
|
|
9
|
-
|
|
9
|
+
"wss://nos.lol/",
|
|
10
|
+
"wss://relay.primal.net/",
|
|
11
|
+
"wss://relay.ditto.pub/",
|
|
12
|
+
"wss://offchain.pub/",
|
|
13
|
+
]);
|
|
14
|
+
export const DEFAULT_WRITE_RELAYS = Object.freeze([
|
|
15
|
+
...DEFAULT_READ_RELAYS,
|
|
10
16
|
]);
|
|
11
|
-
export const DEFAULT_WRITE_RELAYS = Object.freeze([...DEFAULT_READ_RELAYS]);
|
|
12
17
|
/** Default Blossom server for encrypted attachment uploads. */
|
|
13
|
-
export const DEFAULT_BLOSSOM_SERVERS = Object.freeze([
|
|
18
|
+
export const DEFAULT_BLOSSOM_SERVERS = Object.freeze([
|
|
19
|
+
"https://blossom.jumble.social/",
|
|
20
|
+
]);
|
|
14
21
|
/** Large relays used to help clients discover public account metadata. */
|
|
15
22
|
export const DEFAULT_DISCOVERY_RELAYS = DEFAULT_READ_RELAYS;
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
/** Sanitized operational failure: remote responses and credential strings are not included. */
|
|
2
2
|
export declare class SignerError extends Error {
|
|
3
3
|
readonly operation: string;
|
|
4
|
-
readonly code:
|
|
5
|
-
constructor(operation: string, code?:
|
|
4
|
+
readonly code: "SIGNER_FAILED" | "SIGNER_TIMEOUT" | "SIGNER_CLOSED";
|
|
5
|
+
constructor(operation: string, code?: "SIGNER_FAILED" | "SIGNER_TIMEOUT" | "SIGNER_CLOSED");
|
|
6
6
|
}
|
|
7
7
|
/** Compatibility never silently replaces a known unrelated account encryption declaration. */
|
|
8
8
|
export declare class Nip4eCompatibilityError extends Error {
|
|
9
|
-
readonly code:
|
|
10
|
-
constructor(code:
|
|
9
|
+
readonly code: "NIP4E_KEY_CONFLICT" | "NIP4E_DECLARATION_INVALID";
|
|
10
|
+
constructor(code: "NIP4E_KEY_CONFLICT" | "NIP4E_DECLARATION_INVALID");
|
|
11
11
|
}
|
|
12
12
|
/** Never reinterpret existing ciphertext as plaintext or silently change an account's storage mode. */
|
|
13
13
|
export declare class StorageEncryptionError extends Error {
|
|
14
|
-
readonly code:
|
|
15
|
-
constructor(code:
|
|
14
|
+
readonly code: "STORAGE_KEY_REQUIRED" | "STORAGE_MODE_MISMATCH";
|
|
15
|
+
constructor(code: "STORAGE_KEY_REQUIRED" | "STORAGE_MODE_MISMATCH");
|
|
16
16
|
}
|
|
17
|
-
export type RelayOperation =
|
|
18
|
-
export type RelayErrorCode =
|
|
17
|
+
export type RelayOperation = "connect" | "subscribe" | "publish" | "auth" | "history";
|
|
18
|
+
export type RelayErrorCode = "RELAY_CONNECTION_FAILED" | "RELAY_CONNECT_TIMEOUT" | "RELAY_HTTP_REJECTED" | "RELAY_EOSE_TIMEOUT" | "RELAY_ACK_TIMEOUT" | "RELAY_CLOSED" | "RELAY_AUTH_REQUIRED" | "RELAY_AUTH_TIMEOUT" | "RELAY_AUTH_REJECTED" | "RELAY_AUTH_SIGNING_FAILED" | "RELAY_SUBSCRIPTION_REJECTED" | "RELAY_SUBSCRIPTION_FAILED" | "RELAY_EVENT_REJECTED" | "RELAY_HISTORY_INCOMPLETE";
|
|
19
19
|
export interface RelayErrorDetails {
|
|
20
20
|
relay: string;
|
|
21
21
|
operation: RelayOperation;
|
|
@@ -25,7 +25,7 @@ export interface RelayErrorDetails {
|
|
|
25
25
|
closeCode?: number;
|
|
26
26
|
eventKind?: number;
|
|
27
27
|
reason?: string;
|
|
28
|
-
historyReason?:
|
|
28
|
+
historyReason?: "page-limit" | "boundary-overflow" | "query-failed";
|
|
29
29
|
}
|
|
30
30
|
/** Diagnostic metadata only: no query/path/credentials, raw relay reason, frame, or plaintext. */
|
|
31
31
|
export declare class RelayError extends Error {
|
|
@@ -37,18 +37,18 @@ export declare class RelayError extends Error {
|
|
|
37
37
|
readonly closeCode?: number;
|
|
38
38
|
readonly eventKind?: number;
|
|
39
39
|
readonly reason?: string;
|
|
40
|
-
readonly historyReason?: RelayErrorDetails[
|
|
40
|
+
readonly historyReason?: RelayErrorDetails["historyReason"];
|
|
41
41
|
constructor(message: string, details: RelayErrorDetails);
|
|
42
42
|
}
|
|
43
43
|
/** Terminal input rejection, with no submitted values, raw Zod messages or credentials. */
|
|
44
44
|
export declare class InvalidArgsError extends Error {
|
|
45
45
|
readonly command: string;
|
|
46
|
-
readonly category:
|
|
46
|
+
readonly category: "decode" | "validation";
|
|
47
47
|
readonly issues: readonly {
|
|
48
48
|
readonly code: string;
|
|
49
49
|
readonly path: readonly string[];
|
|
50
50
|
}[];
|
|
51
|
-
constructor(command: string, category:
|
|
51
|
+
constructor(command: string, category: "decode" | "validation", issues?: readonly {
|
|
52
52
|
code: string;
|
|
53
53
|
path: readonly string[];
|
|
54
54
|
}[]);
|