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
package/dist/errors.js
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
export class SignerError extends Error {
|
|
3
3
|
operation;
|
|
4
4
|
code;
|
|
5
|
-
constructor(operation, code =
|
|
5
|
+
constructor(operation, code = "SIGNER_FAILED") {
|
|
6
6
|
super(`Signer ${operation} failed (${code})`);
|
|
7
7
|
this.operation = operation;
|
|
8
8
|
this.code = code;
|
|
9
|
-
this.name =
|
|
9
|
+
this.name = "SignerError";
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
/** Compatibility never silently replaces a known unrelated account encryption declaration. */
|
|
@@ -15,20 +15,46 @@ export class Nip4eCompatibilityError extends Error {
|
|
|
15
15
|
constructor(code) {
|
|
16
16
|
super(`Cannot use the configured encryption key: existing NIP-4E declaration conflicts or is invalid (${code}). Keep the declared key, use a separate identity, or explicitly migrate a locally held key with announce({replaceEncryptionKey}).`);
|
|
17
17
|
this.code = code;
|
|
18
|
-
this.name =
|
|
18
|
+
this.name = "Nip4eCompatibilityError";
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
/** Never reinterpret existing ciphertext as plaintext or silently change an account's storage mode. */
|
|
22
22
|
export class StorageEncryptionError extends Error {
|
|
23
23
|
code;
|
|
24
24
|
constructor(code) {
|
|
25
|
-
super(code ===
|
|
25
|
+
super(code === "STORAGE_KEY_REQUIRED"
|
|
26
|
+
? "Existing encrypted data requires its original storageKey."
|
|
27
|
+
: "Existing plaintext data cannot be opened as encrypted; use its original mode or explicitly migrate to a separate database.");
|
|
26
28
|
this.code = code;
|
|
27
|
-
this.name =
|
|
29
|
+
this.name = "StorageEncryptionError";
|
|
28
30
|
}
|
|
29
31
|
}
|
|
30
|
-
const networkCodes = new Set([
|
|
31
|
-
|
|
32
|
+
const networkCodes = new Set([
|
|
33
|
+
"ECONNREFUSED",
|
|
34
|
+
"ECONNRESET",
|
|
35
|
+
"ETIMEDOUT",
|
|
36
|
+
"ENOTFOUND",
|
|
37
|
+
"EAI_AGAIN",
|
|
38
|
+
"ENETUNREACH",
|
|
39
|
+
"EHOSTUNREACH",
|
|
40
|
+
"ECONNABORTED",
|
|
41
|
+
"EPROTO",
|
|
42
|
+
"CERT_HAS_EXPIRED",
|
|
43
|
+
"DEPTH_ZERO_SELF_SIGNED_CERT",
|
|
44
|
+
"UNABLE_TO_VERIFY_LEAF_SIGNATURE",
|
|
45
|
+
"ERR_TLS_CERT_ALTNAME_INVALID",
|
|
46
|
+
]);
|
|
47
|
+
const reasonCodes = new Set([
|
|
48
|
+
"auth-required",
|
|
49
|
+
"restricted",
|
|
50
|
+
"blocked",
|
|
51
|
+
"rate-limited",
|
|
52
|
+
"pow",
|
|
53
|
+
"payment-required",
|
|
54
|
+
"duplicate",
|
|
55
|
+
"invalid",
|
|
56
|
+
"error",
|
|
57
|
+
]);
|
|
32
58
|
/** Diagnostic metadata only: no query/path/credentials, raw relay reason, frame, or plaintext. */
|
|
33
59
|
export class RelayError extends Error {
|
|
34
60
|
relay;
|
|
@@ -41,33 +67,41 @@ export class RelayError extends Error {
|
|
|
41
67
|
reason;
|
|
42
68
|
historyReason;
|
|
43
69
|
constructor(message, details) {
|
|
44
|
-
let relay =
|
|
70
|
+
let relay = "(invalid relay URL)";
|
|
45
71
|
try {
|
|
46
72
|
const url = new URL(details.relay);
|
|
47
|
-
if ([
|
|
73
|
+
if (["ws:", "wss:"].includes(url.protocol)) {
|
|
48
74
|
relay = url.origin;
|
|
49
75
|
}
|
|
50
76
|
}
|
|
51
|
-
catch {
|
|
52
|
-
}
|
|
77
|
+
catch { }
|
|
53
78
|
super(`${message} [${relay}] (${details.code})`);
|
|
54
|
-
this.name =
|
|
79
|
+
this.name = "RelayError";
|
|
55
80
|
this.relay = relay;
|
|
56
81
|
this.operation = details.operation;
|
|
57
82
|
this.code = details.code;
|
|
58
83
|
if (details.networkCode && networkCodes.has(details.networkCode)) {
|
|
59
84
|
this.networkCode = details.networkCode;
|
|
60
85
|
}
|
|
61
|
-
if (
|
|
86
|
+
if (typeof details.statusCode === "number" &&
|
|
87
|
+
Number.isInteger(details.statusCode) &&
|
|
88
|
+
details.statusCode >= 100 &&
|
|
89
|
+
details.statusCode <= 599) {
|
|
62
90
|
this.statusCode = details.statusCode;
|
|
63
91
|
}
|
|
64
|
-
if (
|
|
92
|
+
if (typeof details.closeCode === "number" &&
|
|
93
|
+
Number.isInteger(details.closeCode) &&
|
|
94
|
+
details.closeCode >= 1000 &&
|
|
95
|
+
details.closeCode <= 4999) {
|
|
65
96
|
this.closeCode = details.closeCode;
|
|
66
97
|
}
|
|
67
|
-
if (
|
|
98
|
+
if (typeof details.eventKind === "number" &&
|
|
99
|
+
Number.isInteger(details.eventKind) &&
|
|
100
|
+
details.eventKind >= 0 &&
|
|
101
|
+
details.eventKind <= 65535) {
|
|
68
102
|
this.eventKind = details.eventKind;
|
|
69
103
|
}
|
|
70
|
-
const reason = details.reason?.split(
|
|
104
|
+
const reason = details.reason?.split(":")[0];
|
|
71
105
|
if (reason && reasonCodes.has(reason)) {
|
|
72
106
|
this.reason = reason;
|
|
73
107
|
}
|
|
@@ -83,7 +117,10 @@ export class InvalidArgsError extends Error {
|
|
|
83
117
|
super(`Invalid arguments for /${command}`);
|
|
84
118
|
this.command = command;
|
|
85
119
|
this.category = category;
|
|
86
|
-
this.name =
|
|
87
|
-
this.issues = Object.freeze(issues.map(issue => Object.freeze({
|
|
120
|
+
this.name = "InvalidArgsError";
|
|
121
|
+
this.issues = Object.freeze(issues.map((issue) => Object.freeze({
|
|
122
|
+
code: issue.code,
|
|
123
|
+
path: Object.freeze([...issue.path]),
|
|
124
|
+
})));
|
|
88
125
|
}
|
|
89
126
|
}
|
package/dist/inbox.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { type Event, type Filter } from
|
|
2
|
-
import { type Subscription, type Transport } from
|
|
3
|
-
import type { Store } from
|
|
1
|
+
import { type Event, type Filter } from "nostr-tools";
|
|
2
|
+
import { type Subscription, type Transport } from "./relay.js";
|
|
3
|
+
import type { Store } from "./store.js";
|
|
4
4
|
export interface ReconnectOptions {
|
|
5
5
|
initialDelayMs?: number;
|
|
6
6
|
maxDelayMs?: number;
|
|
@@ -14,12 +14,12 @@ export interface RecoveryOptions {
|
|
|
14
14
|
}
|
|
15
15
|
export interface HistorySyncResult {
|
|
16
16
|
relay: string;
|
|
17
|
-
coverage:
|
|
17
|
+
coverage: "window";
|
|
18
18
|
since: number;
|
|
19
19
|
until: number;
|
|
20
20
|
pages: number;
|
|
21
|
-
status:
|
|
22
|
-
reason?:
|
|
21
|
+
status: "window-complete" | "incomplete" | "aborted" | "disabled";
|
|
22
|
+
reason?: "page-limit" | "boundary-overflow" | "query-failed";
|
|
23
23
|
}
|
|
24
24
|
/** Headless per-relay demand, generation cancellation and fixed-window catch-up. */
|
|
25
25
|
export declare class InboxSubscription implements Subscription {
|
|
@@ -36,8 +36,10 @@ export declare class InboxSubscription implements Subscription {
|
|
|
36
36
|
filter: Filter;
|
|
37
37
|
reconnect?: false | ReconnectOptions;
|
|
38
38
|
recovery?: false | RecoveryOptions;
|
|
39
|
+
/** Activation date in seconds is the baseline until this relay completes a pass. */
|
|
40
|
+
recoveryBaseline?: number;
|
|
39
41
|
localReadWindow?: boolean;
|
|
40
|
-
receive(event: Event, source:
|
|
42
|
+
receive(event: Event, source: "live" | "history", relay: string): void | Promise<void>;
|
|
41
43
|
error(error: unknown): void;
|
|
42
44
|
reconnected(): Promise<void>;
|
|
43
45
|
});
|
package/dist/inbox.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { RelayError } from
|
|
2
|
-
import { verifyEvent } from
|
|
3
|
-
import { physicalConnectionState, subscribeInbox } from
|
|
4
|
-
import { validTimestamp } from
|
|
1
|
+
import { RelayError } from "./errors.js";
|
|
2
|
+
import { verifyEvent } from "nostr-tools";
|
|
3
|
+
import { physicalConnectionState, subscribeInbox, } from "./relay.js";
|
|
4
|
+
import { validTimestamp } from "./protocol.js";
|
|
5
5
|
function integer(value, name, min, max) {
|
|
6
6
|
if (!Number.isSafeInteger(value) || value < min || value > max) {
|
|
7
7
|
throw new TypeError(`${name} must be an integer in ${min}..${max}`);
|
|
@@ -19,25 +19,25 @@ export class InboxSubscription {
|
|
|
19
19
|
constructor(options) {
|
|
20
20
|
this.options = options;
|
|
21
21
|
if (options.reconnect !== false) {
|
|
22
|
-
const initial = integer(options.reconnect?.initialDelayMs ?? 1000,
|
|
23
|
-
const max = integer(options.reconnect?.maxDelayMs ?? 60000,
|
|
22
|
+
const initial = integer(options.reconnect?.initialDelayMs ?? 1000, "initialDelayMs", 1, 60000);
|
|
23
|
+
const max = integer(options.reconnect?.maxDelayMs ?? 60000, "maxDelayMs", initial, 3600000);
|
|
24
24
|
const jitter = options.reconnect?.jitter ?? 0.2;
|
|
25
25
|
if (!Number.isFinite(jitter) || jitter < 0 || jitter > 1) {
|
|
26
|
-
throw new TypeError(
|
|
26
|
+
throw new TypeError("jitter must be in 0..1");
|
|
27
27
|
}
|
|
28
28
|
this.retry = {
|
|
29
29
|
initial,
|
|
30
30
|
max,
|
|
31
31
|
jitter,
|
|
32
32
|
stepped: options.reconnect?.initialDelayMs === undefined,
|
|
33
|
-
stableReset: integer(options.reconnect?.stableResetMs ?? 30000,
|
|
33
|
+
stableReset: integer(options.reconnect?.stableResetMs ?? 30000, "stableResetMs", 1, 3600000),
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
36
|
if (options.recovery !== false) {
|
|
37
37
|
this.recovery = {
|
|
38
|
-
pageSize: integer(options.recovery?.pageSize ?? 500,
|
|
39
|
-
maxPages: integer(options.recovery?.maxPages ?? 20,
|
|
40
|
-
overlap: integer(options.recovery?.overlapSeconds ?? 176400,
|
|
38
|
+
pageSize: integer(options.recovery?.pageSize ?? 500, "pageSize", 1, 1000),
|
|
39
|
+
maxPages: integer(options.recovery?.maxPages ?? 20, "maxPages", 1, 1000),
|
|
40
|
+
overlap: integer(options.recovery?.overlapSeconds ?? 176400, "overlapSeconds", 172800, 31536000),
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
43
|
for (const url of new Set(options.relays)) {
|
|
@@ -52,18 +52,22 @@ export class InboxSubscription {
|
|
|
52
52
|
connectedBefore: false,
|
|
53
53
|
initial,
|
|
54
54
|
resolve,
|
|
55
|
-
reject
|
|
55
|
+
reject,
|
|
56
56
|
});
|
|
57
57
|
}
|
|
58
|
-
this.ready = (this.replicas.length === 1
|
|
59
|
-
|
|
58
|
+
this.ready = (this.replicas.length === 1
|
|
59
|
+
? this.replicas[0].initial
|
|
60
|
+
: Promise.any(this.replicas.map((replica) => replica.initial))).then(() => { });
|
|
60
61
|
for (const replica of this.replicas) {
|
|
61
62
|
this.connect(replica);
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
/** An unfailed owned subscription, not physical connection success or historical EOSE. */
|
|
65
66
|
hasUsableSubscription() {
|
|
66
|
-
return !this.closed &&
|
|
67
|
+
return (!this.closed &&
|
|
68
|
+
this.replicas.some((replica) => replica.attempt?.subscription !== undefined &&
|
|
69
|
+
physicalConnectionState(this.options.transport, replica.url) !==
|
|
70
|
+
false));
|
|
67
71
|
}
|
|
68
72
|
current(replica, attempt) {
|
|
69
73
|
return !this.closed && replica.attempt === attempt;
|
|
@@ -74,9 +78,10 @@ export class InboxSubscription {
|
|
|
74
78
|
}
|
|
75
79
|
let cancel;
|
|
76
80
|
const attempt = {
|
|
77
|
-
canceled: new Promise(resolve => {
|
|
81
|
+
canceled: new Promise((resolve) => {
|
|
78
82
|
cancel = resolve;
|
|
79
|
-
}),
|
|
83
|
+
}),
|
|
84
|
+
cancel: () => cancel(),
|
|
80
85
|
};
|
|
81
86
|
replica.attempt = attempt;
|
|
82
87
|
const reconnecting = replica.connectedBefore || replica.failures > 0;
|
|
@@ -90,14 +95,24 @@ export class InboxSubscription {
|
|
|
90
95
|
attempt.cancel();
|
|
91
96
|
attempt.subscription?.close();
|
|
92
97
|
attempt.query?.close();
|
|
93
|
-
const diagnostic = error instanceof RelayError
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
+
const diagnostic = error instanceof RelayError
|
|
99
|
+
? error
|
|
100
|
+
: new RelayError("Relay subscription failed", {
|
|
101
|
+
relay: replica.url,
|
|
102
|
+
operation: "subscribe",
|
|
103
|
+
code: "RELAY_SUBSCRIPTION_FAILED",
|
|
104
|
+
});
|
|
98
105
|
// Established connections get a grace period for automatic network recovery.
|
|
99
|
-
const transient = new Set([
|
|
100
|
-
|
|
106
|
+
const transient = new Set([
|
|
107
|
+
"RELAY_CONNECTION_FAILED",
|
|
108
|
+
"RELAY_CONNECT_TIMEOUT",
|
|
109
|
+
"RELAY_EOSE_TIMEOUT",
|
|
110
|
+
"RELAY_CLOSED",
|
|
111
|
+
"RELAY_SUBSCRIPTION_FAILED",
|
|
112
|
+
]);
|
|
113
|
+
if (this.retry &&
|
|
114
|
+
replica.connectedBefore &&
|
|
115
|
+
transient.has(diagnostic.code)) {
|
|
101
116
|
replica.outageError = diagnostic;
|
|
102
117
|
if (!replica.outageTimer && !replica.outageReported) {
|
|
103
118
|
replica.outageTimer = setTimeout(() => {
|
|
@@ -121,17 +136,22 @@ export class InboxSubscription {
|
|
|
121
136
|
}
|
|
122
137
|
const steps = [1000, 2000, 5000, 10000, 20000, 30000, 60000];
|
|
123
138
|
const attemptIndex = replica.failures++;
|
|
124
|
-
const base = Math.min(this.retry.max, this.retry.stepped
|
|
125
|
-
|
|
139
|
+
const base = Math.min(this.retry.max, this.retry.stepped
|
|
140
|
+
? steps[Math.min(attemptIndex, steps.length - 1)]
|
|
141
|
+
: this.retry.initial * 2 ** Math.min(attemptIndex, 30));
|
|
142
|
+
const delay = Math.min(this.retry.max, Math.max(1, Math.round(base *
|
|
143
|
+
(1 - this.retry.jitter + Math.random() * this.retry.jitter * 2))));
|
|
126
144
|
replica.retry = setTimeout(() => {
|
|
127
145
|
replica.retry = undefined;
|
|
128
146
|
this.connect(replica);
|
|
129
147
|
}, delay);
|
|
130
148
|
};
|
|
131
149
|
try {
|
|
132
|
-
const subscription = (this.options.localReadWindow
|
|
150
|
+
const subscription = (this.options.localReadWindow
|
|
151
|
+
? subscribeInbox.bind(undefined, this.options.transport)
|
|
152
|
+
: this.options.transport.subscribe.bind(this.options.transport))([replica.url], this.options.filter, (event, source) => {
|
|
133
153
|
if (this.current(replica, attempt)) {
|
|
134
|
-
void Promise.resolve(this.options.receive(event, source, replica.url)).catch(error => this.options.error(error));
|
|
154
|
+
void Promise.resolve(this.options.receive(event, source, replica.url)).catch((error) => this.options.error(error));
|
|
135
155
|
}
|
|
136
156
|
}, fail);
|
|
137
157
|
attempt.subscription = subscription;
|
|
@@ -139,7 +159,8 @@ export class InboxSubscription {
|
|
|
139
159
|
subscription.close();
|
|
140
160
|
return;
|
|
141
161
|
}
|
|
142
|
-
void subscription.ready
|
|
162
|
+
void subscription.ready
|
|
163
|
+
.then(async () => {
|
|
143
164
|
if (!this.current(replica, attempt)) {
|
|
144
165
|
return;
|
|
145
166
|
}
|
|
@@ -163,9 +184,12 @@ export class InboxSubscription {
|
|
|
163
184
|
replica.outageReported = false;
|
|
164
185
|
replica.resolve();
|
|
165
186
|
if (reconnecting) {
|
|
166
|
-
void this.options
|
|
187
|
+
void this.options
|
|
188
|
+
.reconnected()
|
|
189
|
+
.catch((error) => this.options.error(error));
|
|
167
190
|
}
|
|
168
|
-
})
|
|
191
|
+
})
|
|
192
|
+
.catch(fail);
|
|
169
193
|
}
|
|
170
194
|
catch (error) {
|
|
171
195
|
fail(error);
|
|
@@ -178,7 +202,7 @@ export class InboxSubscription {
|
|
|
178
202
|
if (replica.recovering) {
|
|
179
203
|
return replica.recovering.then(() => {
|
|
180
204
|
if (!this.current(replica, attempt)) {
|
|
181
|
-
return this.result(replica.url,
|
|
205
|
+
return this.result(replica.url, "aborted");
|
|
182
206
|
}
|
|
183
207
|
return this.recover(replica, attempt);
|
|
184
208
|
});
|
|
@@ -195,36 +219,38 @@ export class InboxSubscription {
|
|
|
195
219
|
result(relay, status) {
|
|
196
220
|
return {
|
|
197
221
|
relay,
|
|
198
|
-
coverage:
|
|
222
|
+
coverage: "window",
|
|
199
223
|
since: 0,
|
|
200
224
|
until: 0,
|
|
201
225
|
pages: 0,
|
|
202
|
-
status
|
|
226
|
+
status,
|
|
203
227
|
};
|
|
204
228
|
}
|
|
205
229
|
async scan(replica, attempt) {
|
|
206
230
|
const config = this.recovery;
|
|
207
231
|
if (!config) {
|
|
208
|
-
return this.result(replica.url,
|
|
232
|
+
return this.result(replica.url, "disabled");
|
|
209
233
|
}
|
|
210
234
|
const through = Math.floor(Date.now() / 1000);
|
|
211
|
-
const since = Math.max(0, Math.min(this.options.store.checkpoint(replica.url) ??
|
|
235
|
+
const since = Math.max(0, Math.min(this.options.store.checkpoint(replica.url) ??
|
|
236
|
+
this.options.recoveryBaseline ??
|
|
237
|
+
through, through) - config.overlap);
|
|
212
238
|
let until = through;
|
|
213
239
|
const result = {
|
|
214
240
|
relay: replica.url,
|
|
215
|
-
coverage:
|
|
241
|
+
coverage: "window",
|
|
216
242
|
since,
|
|
217
243
|
until: through,
|
|
218
244
|
pages: 0,
|
|
219
|
-
status:
|
|
245
|
+
status: "incomplete",
|
|
220
246
|
};
|
|
221
247
|
const incomplete = (reason) => {
|
|
222
248
|
result.reason = reason;
|
|
223
249
|
this.options.error(new RelayError(`Inbox history recovery incomplete (${reason}); checkpoint unchanged`, {
|
|
224
250
|
relay: replica.url,
|
|
225
|
-
operation:
|
|
226
|
-
code:
|
|
227
|
-
historyReason: reason
|
|
251
|
+
operation: "history",
|
|
252
|
+
code: "RELAY_HISTORY_INCOMPLETE",
|
|
253
|
+
historyReason: reason,
|
|
228
254
|
}));
|
|
229
255
|
return result;
|
|
230
256
|
};
|
|
@@ -235,9 +261,9 @@ export class InboxSubscription {
|
|
|
235
261
|
...this.options.filter,
|
|
236
262
|
since,
|
|
237
263
|
until,
|
|
238
|
-
limit: config.pageSize
|
|
264
|
+
limit: config.pageSize,
|
|
239
265
|
}, (event, source) => {
|
|
240
|
-
if (source ===
|
|
266
|
+
if (source === "history" && this.current(replica, attempt)) {
|
|
241
267
|
events.push(event);
|
|
242
268
|
}
|
|
243
269
|
});
|
|
@@ -253,62 +279,81 @@ export class InboxSubscription {
|
|
|
253
279
|
}
|
|
254
280
|
if (!this.current(replica, attempt)) {
|
|
255
281
|
return {
|
|
256
|
-
...result,
|
|
282
|
+
...result,
|
|
283
|
+
status: "aborted",
|
|
257
284
|
};
|
|
258
285
|
}
|
|
259
|
-
const page = [
|
|
286
|
+
const page = [
|
|
287
|
+
...new Map(events
|
|
288
|
+
.filter((event) => {
|
|
260
289
|
try {
|
|
261
|
-
return event.kind === 1059 &&
|
|
262
|
-
event.
|
|
290
|
+
return (event.kind === 1059 &&
|
|
291
|
+
validTimestamp(event.created_at) &&
|
|
292
|
+
event.created_at >= since &&
|
|
293
|
+
event.created_at <= until &&
|
|
294
|
+
event.tags.some((tag) => tag[0] === "p" &&
|
|
295
|
+
tag[1] === this.options.filter["#p"]?.[0]) &&
|
|
296
|
+
verifyEvent(JSON.parse(JSON.stringify(event))));
|
|
263
297
|
}
|
|
264
298
|
catch {
|
|
265
299
|
return false;
|
|
266
300
|
}
|
|
267
|
-
})
|
|
301
|
+
})
|
|
302
|
+
.map((event) => [event.id, event])).values(),
|
|
303
|
+
].sort((a, b) => b.created_at - a.created_at || a.id.localeCompare(b.id));
|
|
268
304
|
result.pages++;
|
|
269
305
|
for (const event of page) {
|
|
270
306
|
if (!this.current(replica, attempt)) {
|
|
271
307
|
return {
|
|
272
|
-
...result,
|
|
308
|
+
...result,
|
|
309
|
+
status: "aborted",
|
|
273
310
|
};
|
|
274
311
|
}
|
|
275
|
-
await this.options.receive(event,
|
|
312
|
+
await this.options.receive(event, "history", replica.url);
|
|
276
313
|
}
|
|
277
314
|
if (!this.current(replica, attempt)) {
|
|
278
315
|
return {
|
|
279
|
-
...result,
|
|
316
|
+
...result,
|
|
317
|
+
status: "aborted",
|
|
280
318
|
};
|
|
281
319
|
}
|
|
282
320
|
if (page.length < config.pageSize) {
|
|
283
321
|
this.options.store.saveCheckpoint(replica.url, through);
|
|
284
322
|
return {
|
|
285
|
-
...result,
|
|
323
|
+
...result,
|
|
324
|
+
status: "window-complete",
|
|
286
325
|
};
|
|
287
326
|
}
|
|
288
|
-
const oldest = page.at(-1)
|
|
289
|
-
if (oldest >= until) {
|
|
290
|
-
return incomplete(
|
|
327
|
+
const oldest = page.at(-1)?.created_at;
|
|
328
|
+
if (oldest === undefined || oldest >= until) {
|
|
329
|
+
return incomplete("boundary-overflow");
|
|
291
330
|
}
|
|
292
331
|
until = oldest; // Inclusive boundary: re-read it before moving to an earlier second.
|
|
293
|
-
await new Promise(resolve => setTimeout(resolve, 0)); // Yield between bounded crypto/SQLite pages.
|
|
332
|
+
await new Promise((resolve) => setTimeout(resolve, 0)); // Yield between bounded crypto/SQLite pages.
|
|
294
333
|
}
|
|
295
|
-
return this.current(replica, attempt)
|
|
296
|
-
|
|
297
|
-
|
|
334
|
+
return this.current(replica, attempt)
|
|
335
|
+
? incomplete("page-limit")
|
|
336
|
+
: {
|
|
337
|
+
...result,
|
|
338
|
+
status: "aborted",
|
|
339
|
+
};
|
|
298
340
|
}
|
|
299
341
|
catch (error) {
|
|
300
342
|
if (!this.current(replica, attempt)) {
|
|
301
343
|
return {
|
|
302
|
-
...result,
|
|
344
|
+
...result,
|
|
345
|
+
status: "aborted",
|
|
303
346
|
};
|
|
304
347
|
}
|
|
305
348
|
this.options.error(error);
|
|
306
|
-
return incomplete(
|
|
349
|
+
return incomplete("query-failed");
|
|
307
350
|
}
|
|
308
351
|
}
|
|
309
352
|
async sync() {
|
|
310
353
|
await this.ready;
|
|
311
|
-
return Promise.all(this.replicas.map(replica => replica.attempt
|
|
354
|
+
return Promise.all(this.replicas.map((replica) => replica.attempt
|
|
355
|
+
? this.recover(replica, replica.attempt)
|
|
356
|
+
: this.result(replica.url, "aborted")));
|
|
312
357
|
}
|
|
313
358
|
close() {
|
|
314
359
|
if (this.closed) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
export type {
|
|
3
|
-
export
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export type
|
|
7
|
-
export type {
|
|
8
|
-
export type {
|
|
9
|
-
export {
|
|
10
|
-
export {
|
|
11
|
-
export {
|
|
12
|
-
export type
|
|
13
|
-
export type {
|
|
14
|
-
export {
|
|
15
|
-
export
|
|
16
|
-
export {
|
|
17
|
-
export
|
|
18
|
-
export type {
|
|
19
|
-
export type {
|
|
20
|
-
export {
|
|
21
|
-
export {
|
|
22
|
-
export
|
|
23
|
-
export type {
|
|
24
|
-
export {
|
|
1
|
+
export type { FileAttachment, FileDownloadError, FileDownloadErrorCode, FileDownloadResult, } from "./attachments.js";
|
|
2
|
+
export type { ReplyFileOptions, SendFileOptions, UploadFileOptions, UploadFileResult, } from "./blossom.js";
|
|
3
|
+
export type { MessageObserver, ErrorHandler, IntakeMetadata, SendOptions, SendResult, } from "./client.js";
|
|
4
|
+
export { Bot, type Context, type CommandContext, type MessageHandler, type Middleware, type Next, type BotCommandOptions, type CommandHandler, type BotOptions, type Session, type SessionStore, } from "./bot.js";
|
|
5
|
+
export { generateSecretKey, getPublicKey, type Rumor } from "./protocol.js";
|
|
6
|
+
export { WebSocketTransport, DEFAULT_RELAY_TIMEOUTS, type RelayTimeouts, type WebSocketTransportOptions, type Transport, type Subscription, type RelayTiming, type RelayTimingObserver, } from "./relay.js";
|
|
7
|
+
export type { Message, HistoryPage, MessageRecord, MessageList, MessageListOptions, } from "./store.js";
|
|
8
|
+
export type { ProtocolMode } from "./modes.js";
|
|
9
|
+
export type { ReconnectOptions, RecoveryOptions, HistorySyncResult, } from "./inbox.js";
|
|
10
|
+
export { nsecSigner, bunkerSigner, type Signer, type BunkerOptions, } from "./signers.js";
|
|
11
|
+
export { SignerError, Nip4eCompatibilityError, StorageEncryptionError, RelayError, type RelayOperation, type RelayErrorCode, type RelayErrorDetails, } from "./errors.js";
|
|
12
|
+
export { COMMAND_DEFINITION_KIND, validateCommandDefinition, encodeCommandDefinition, decodeCommandDefinition, encodeCommandRevocation, selectCommandDefinitions, commandAddress, matchesCommandScopes, parseCommand, type CommandDefinition, type BotCommandDefinition, type CommandArgument, type CommandArgs, type CommandArgumentType, type CommandAddress, type CommandValue, type CommandEvent, type CommandMatchContext, type CommandInvocation, type CommandParseResult, type PublishedCommand, type CommandDiscoveryResult, type DiscoverCommandsOptions, } from "./nipcd.js";
|
|
13
|
+
export type { KeyRotationOptions, AnnounceOptions, EncryptionKeyStatus, } from "./keys.js";
|
|
14
|
+
export type { MessageState } from "./storage.js";
|
|
15
|
+
export { sqliteMessageStorage, type SQLiteMessageStorage, } from "./message-storage-sqlite.js";
|
|
16
|
+
export type { MessageStorage, MessageStorageEntry, MessageStorageQuery, } from "./message-storage.js";
|
|
17
|
+
export { DEFAULT_DM_RELAYS, DEFAULT_READ_RELAYS, DEFAULT_WRITE_RELAYS, DEFAULT_DISCOVERY_RELAYS, DEFAULT_BLOSSOM_SERVERS, } from "./defaults.js";
|
|
18
|
+
export type { MetadataCacheOptions } from "./messaging-metadata.js";
|
|
19
|
+
export type { KeySyncOptions } from "./key-sync.js";
|
|
20
|
+
export type { CommandSyncResult } from "./commands.js";
|
|
21
|
+
export { allowlist } from "./allowlist.js";
|
|
22
|
+
export { requestTiming, type RequestTiming, type RequestTimingObserver, type RequestTimingOptions, } from "./request-timing.js";
|
|
23
|
+
export type { Input, ThreadResult, ThreadOptions, PublicThreadOptions, } from "./public-thread.js";
|
|
24
|
+
export type { PublicInboxOptions } from "./public-inbox.js";
|
|
25
|
+
export { InvalidArgsError } from "./errors.js";
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export { Bot } from
|
|
2
|
-
export { generateSecretKey, getPublicKey } from
|
|
3
|
-
export { WebSocketTransport, DEFAULT_RELAY_TIMEOUTS } from
|
|
4
|
-
export { nsecSigner, bunkerSigner } from
|
|
5
|
-
export { SignerError, Nip4eCompatibilityError, StorageEncryptionError, RelayError } from
|
|
6
|
-
export { COMMAND_DEFINITION_KIND, validateCommandDefinition, encodeCommandDefinition, decodeCommandDefinition, encodeCommandRevocation, selectCommandDefinitions, commandAddress, matchesCommandScopes, parseCommand, } from
|
|
7
|
-
export { sqliteMessageStorage } from
|
|
8
|
-
export { DEFAULT_DM_RELAYS, DEFAULT_READ_RELAYS, DEFAULT_WRITE_RELAYS, DEFAULT_DISCOVERY_RELAYS, DEFAULT_BLOSSOM_SERVERS } from
|
|
9
|
-
export { allowlist } from
|
|
10
|
-
export { requestTiming } from
|
|
11
|
-
export { InvalidArgsError } from
|
|
1
|
+
export { Bot, } from "./bot.js";
|
|
2
|
+
export { generateSecretKey, getPublicKey } from "./protocol.js";
|
|
3
|
+
export { WebSocketTransport, DEFAULT_RELAY_TIMEOUTS, } from "./relay.js";
|
|
4
|
+
export { nsecSigner, bunkerSigner, } from "./signers.js";
|
|
5
|
+
export { SignerError, Nip4eCompatibilityError, StorageEncryptionError, RelayError, } from "./errors.js";
|
|
6
|
+
export { COMMAND_DEFINITION_KIND, validateCommandDefinition, encodeCommandDefinition, decodeCommandDefinition, encodeCommandRevocation, selectCommandDefinitions, commandAddress, matchesCommandScopes, parseCommand, } from "./nipcd.js";
|
|
7
|
+
export { sqliteMessageStorage, } from "./message-storage-sqlite.js";
|
|
8
|
+
export { DEFAULT_DM_RELAYS, DEFAULT_READ_RELAYS, DEFAULT_WRITE_RELAYS, DEFAULT_DISCOVERY_RELAYS, DEFAULT_BLOSSOM_SERVERS, } from "./defaults.js";
|
|
9
|
+
export { allowlist } from "./allowlist.js";
|
|
10
|
+
export { requestTiming, } from "./request-timing.js";
|
|
11
|
+
export { InvalidArgsError } from "./errors.js";
|
package/dist/key-sync.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { type Event } from
|
|
2
|
-
import { type Identity } from
|
|
3
|
-
import type { Store } from
|
|
4
|
-
import type { Transport } from
|
|
1
|
+
import { type Event } from "nostr-tools";
|
|
2
|
+
import { type Identity } from "./protocol.js";
|
|
3
|
+
import type { Store } from "./store.js";
|
|
4
|
+
import type { Transport } from "./relay.js";
|
|
5
5
|
export interface KeySyncOptions {
|
|
6
6
|
timeoutMs?: number;
|
|
7
7
|
}
|