@cero-base/core 2.3.4 → 2.4.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.
@@ -61,8 +61,10 @@ export declare class Keyring {
61
61
  current: number;
62
62
  /** highest adopted apply-order sequence (0 = base era) */
63
63
  seq: number;
64
- /** bumped on every add/remove — cheap change detection for retries */
64
+ /** bumped on every add/remove — cheap change detection */
65
65
  version: number;
66
+ /** @type {(() => void) | null} called when an epoch is learned or dropped */
67
+ onchange: (() => void) | null;
66
68
  constructor();
67
69
  /** @returns {Array<{ epoch: number, stamp: number, entropy: Uint8Array }>} ascending by seq */
68
70
  all(): Array<{
@@ -99,11 +101,9 @@ export declare class EpochAutobee extends Autobee {
99
101
  /** @private */
100
102
  _epochStalled;
101
103
  /** @private */
102
- _epochRetry;
104
+ _epochParked;
103
105
  /** @private */
104
- _epochRetryDelay;
105
- /** @private */
106
- _epochRetrySeen;
106
+ _epochWake;
107
107
  /**
108
108
  * @param {import('corestore')} store
109
109
  * @param {Uint8Array | null} key
@@ -119,11 +119,9 @@ export declare class EpochAutobee extends Autobee {
119
119
  key?: Uint8Array;
120
120
  }): Promise<Uint8Array>;
121
121
  /** @private */
122
- private _close;
123
- /** @private */
124
122
  private _bumpPendingWriters;
125
123
  /** @private */
126
124
  private _applyWakeupHints;
127
125
  /** @private */
128
- private _scheduleEpochRetry;
126
+ private _scheduleEpochWake;
129
127
  }
@@ -7,13 +7,12 @@
7
7
  */
8
8
  export declare function wrap(version: number, body: Uint8Array): Uint8Array;
9
9
  /**
10
- * Split an op into contract version and payload. Ops written before the
11
- * envelope existed carry no sentinel and read as version 0.
10
+ * Split an op into contract version and payload, or `null` for bytes with no envelope or a cut one.
12
11
  *
13
12
  * @param {Uint8Array} buf
14
- * @returns {{ version: number, body: Uint8Array }}
13
+ * @returns {{ version: number, body: Uint8Array } | null}
15
14
  */
16
15
  export declare function unwrap(buf: Uint8Array): {
17
16
  version: number;
18
17
  body: Uint8Array;
19
- };
18
+ } | null;
@@ -12,7 +12,7 @@ export type DatabaseOpts = {
12
12
  */
13
13
  identity: import('../identity/index.js').Identity;
14
14
  /**
15
- * Optional swarm; required for multi-writer replication.
15
+ * Optional swarm; required for multi-writer replication; `store` is its store or a session of it.
16
16
  */
17
17
  network?: import('../network/index.js').Network;
18
18
  /**
@@ -42,9 +42,9 @@ export type DatabaseOpts = {
42
42
  */
43
43
  namespace?: string;
44
44
  /**
45
- * Optional encryption key; falls back to identity's key.
45
+ * Encrypts it; a join hands it out, so never the identity's on a database you pair.
46
46
  */
47
- encryptionKey?: Uint8Array | null;
47
+ encryptionKey: Uint8Array;
48
48
  /**
49
49
  * Rotation epochs to prime the keyring with (delivered at join).
50
50
  */
@@ -147,10 +147,10 @@ export type HookFn = (ctx: HookContext) => unknown;
147
147
  * @typedef {object} DatabaseOpts
148
148
  * @property {import('corestore')} store Corestore (or compatible) used to materialize the autobee.
149
149
  * @property {import('../identity/index.js').Identity} identity Long-lived member identity used to sign writer changes.
150
- * @property {import('../network/index.js').Network} [network] Optional swarm; required for multi-writer replication.
150
+ * @property {import('../network/index.js').Network} [network] Optional swarm; required for multi-writer replication; `store` is its store or a session of it.
151
151
  * @property {{ database: object, dispatch: { Router: new () => object, encode: (name: string, value: unknown) => Uint8Array, decode: (buf: Uint8Array) => { name: string, value: unknown } }, meta?: { ns?: string, version?: number, refs?: Record<string, { kind?: string, verb?: string }> } }} spec Generated hyperdb + hyperdispatch spec.
152
152
  * @property {string} [namespace] Corestore namespace; defaults to `cero`.
153
- * @property {Uint8Array | null} [encryptionKey] Optional encryption key; falls back to identity's key.
153
+ * @property {Uint8Array} encryptionKey Encrypts it; a join hands it out, so never the identity's on a database you pair.
154
154
  * @property {Array<{ epoch: number, entropy: Uint8Array }> | null} [epochs] Rotation epochs to prime the keyring with (delivered at join).
155
155
  * @property {Uint8Array | null} [key] Existing autobee key to reopen.
156
156
  * @property {boolean} [pinned] Always search and announce, outside the network's presence budget. The root.
@@ -417,12 +417,16 @@ export declare class Database extends ReadyResource {
417
417
  writer: import('../identity/index.js').KeyPair;
418
418
  }>;
419
419
  /**
420
- * Claim writership on an existing room by signing our writer key with the member identity;
421
- * the claim rides in the device core as an optimistic node.
420
+ * Seat this device's writer where its identity is a member: wait for the member row, append
421
+ * the add-writer the identity signs from this device's own core, and resolve once it writes.
422
+ * A member whose role cannot write takes no seat.
422
423
  *
424
+ * @param {{ timeout?: number }} [opts]
423
425
  * @returns {Promise<void>}
424
426
  */
425
- claim(): Promise<void>;
427
+ claim({ timeout }?: {
428
+ timeout?: number;
429
+ }): Promise<void>;
426
430
  /**
427
431
  * Resolve once the bee becomes writable, or reject after `timeout` ms.
428
432
  *
@@ -511,9 +515,7 @@ export declare class Database extends ReadyResource {
511
515
  /** @private */
512
516
  private _total;
513
517
  /** @private */
514
- private _optimistic;
515
- /** @private */
516
- private _backfilled;
518
+ private _member;
517
519
  /** @private */
518
520
  private _admission;
519
521
  /** @private */
@@ -10,9 +10,7 @@ export declare const OWNER = "owner";
10
10
  export declare const ADMIN = "admin";
11
11
  export declare const MEMBER = "member";
12
12
  export declare const READER = "reader";
13
- export declare const READ = "read";
14
13
  export declare const WRITE = "write";
15
- export declare const DELETE = "delete";
16
14
  export declare const INVITE = "invite";
17
15
  export declare const REMOVE = "remove";
18
16
  export declare const ASSIGN = "assign";
@@ -20,7 +18,7 @@ export declare const ROLE_PERMS: {
20
18
  owner: string[];
21
19
  admin: string[];
22
20
  member: string[];
23
- reader: string[];
21
+ reader: any[];
24
22
  };
25
23
  export declare const RANK: {
26
24
  owner: number;
@@ -1,8 +1,6 @@
1
1
  export { WRITE, INVITE, ASSIGN, REMOVE } from './constants.js';
2
2
  /** @type {(dbKey: Uint8Array, writer: Uint8Array, appender: Uint8Array) => Uint8Array} */
3
3
  export declare function admission(dbKey: Uint8Array<ArrayBufferLike>, writer: Uint8Array<ArrayBufferLike>, appender: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>;
4
- /** @type {(dbKey: Uint8Array, writer: Uint8Array) => Uint8Array} */
5
- export declare function ownership(dbKey: Uint8Array<ArrayBufferLike>, writer: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>;
6
4
  /** @type {(dbKey: Uint8Array, invite: Uint8Array, writer: Uint8Array, reply: Uint8Array) => Uint8Array} */
7
5
  export declare function joining(dbKey: Uint8Array<ArrayBufferLike>, invite: Uint8Array<ArrayBufferLike>, writer: Uint8Array<ArrayBufferLike>, reply: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>;
8
6
  /**
@@ -65,7 +65,7 @@ export declare class Mailbox extends ReadyResource {
65
65
  /** @private */
66
66
  _resources;
67
67
  /**
68
- * @param {import('../network/index.js').Network} network Needs a store: messages travel as cores in it.
68
+ * @param {import('../network/index.js').Network} network Messages travel as cores in its store.
69
69
  * @param {MailboxOpts} [opts]
70
70
  */
71
71
  constructor(network: import('../network/index.js').Network, { inbox, outbox, onerror }?: MailboxOpts);
@@ -32,11 +32,11 @@ export type NetworkOpts = {
32
32
  */
33
33
  channel?: string;
34
34
  /**
35
- * Corestore; required for mirrors (blind peers replicate its cores).
35
+ * Corestore; every connection replicates it, so every core it holds rides every peer.
36
36
  */
37
- store?: import('corestore');
37
+ store: import('corestore');
38
38
  /**
39
- * Blind-peer public keys; each attached room/blob core is mirrored through them for offline sync.
39
+ * Blind-peer public keys; each attached database/blob core is mirrored through them for offline sync.
40
40
  */
41
41
  mirrors?: Array<string | Uint8Array>;
42
42
  /**
@@ -52,9 +52,6 @@ export type NetworkOpts = {
52
52
  idle?: number;
53
53
  };
54
54
  };
55
- export type Replicable = {
56
- replicate: (stream: import('@hyperswarm/secret-stream')) => unknown;
57
- };
58
55
  /**
59
56
  * @typedef {object} NetworkOpts
60
57
  * @property {import('../identity/index.js').Identity} [identity] Long-lived keypair used as the swarm identity.
@@ -63,16 +60,14 @@ export type Replicable = {
63
60
  * @property {Uint8Array[]} [relayThrough] Relay public keys to tunnel through.
64
61
  * @property {number[]} [backoffs] Reconnect backoff tiers in ms; the default escalates to ~10min, far too slow for local nets.
65
62
  * @property {string} [channel] Optional network-isolation label; only same-channel peers meet.
66
- * @property {import('corestore')} [store] Corestore; required for mirrors (blind peers replicate its cores).
67
- * @property {Array<string | Uint8Array>} [mirrors] Blind-peer public keys; each attached room/blob core is mirrored through them for offline sync.
63
+ * @property {import('corestore')} store Corestore; every connection replicates it, so every core it holds rides every peer.
64
+ * @property {Array<string | Uint8Array>} [mirrors] Blind-peer public keys; each attached database/blob core is mirrored through them for offline sync.
68
65
  * @property {(err: Error) => void} [onerror] Background-task error handler.
69
66
  * @property {{ active?: number, announced?: number, idle?: number }} [presence] Swarm budget for attached databases: how many search, how many only announce, and the idle ms before the rest leave.
70
- *
71
- * @typedef {{ replicate: (stream: import('@hyperswarm/secret-stream')) => unknown }} Replicable
72
67
  */
73
68
  /**
74
- * Hyperswarm peer-discovery + replication multiplexer. Wraps a swarm and a
75
- * shared wakeup channel, replicating any attached resource onto every peer.
69
+ * Hyperswarm peer discovery plus a shared wakeup channel. Every connection, swarm or injected,
70
+ * replicates the store.
76
71
  */
77
72
  export declare class Network extends ReadyResource {
78
73
  identity: import("../index.js").Identity;
@@ -84,8 +79,8 @@ export declare class Network extends ReadyResource {
84
79
  relayThrough: Uint8Array<ArrayBufferLike>[];
85
80
  backoffs: number[];
86
81
  channel: string;
87
- /** @type {import('corestore') | null} */
88
- store: import('corestore') | null;
82
+ /** @type {import('corestore')} */
83
+ store: import('corestore');
89
84
  /** @type {Uint8Array[]} */
90
85
  mirrors: Uint8Array[];
91
86
  /** @private */
@@ -93,13 +88,6 @@ export declare class Network extends ReadyResource {
93
88
  /** @type {import('protomux-wakeup')} */
94
89
  wakeup: import('protomux-wakeup');
95
90
  presence: Presence;
96
- info: object;
97
- /** @private */
98
- _peerInfo;
99
- /** @private */
100
- _infoSenders;
101
- /** @private */
102
- _replicateables;
103
91
  /** @private */
104
92
  _discoveries;
105
93
  /** @private */
@@ -112,7 +100,7 @@ export declare class Network extends ReadyResource {
112
100
  _relookup;
113
101
  /** @private */
114
102
  _onerror;
115
- /** @param {NetworkOpts} [opts] */
103
+ /** @param {NetworkOpts} opts */
116
104
  constructor({ identity, bootstrap, firewall, relayThrough, backoffs, channel, store, mirrors, presence, onerror }?: NetworkOpts);
117
105
  /** @returns {import('hyperswarm') | null} The underlying hyperswarm, or null before ready / after close. */
118
106
  get swarm(): import('hyperswarm') | null;
@@ -137,6 +125,8 @@ export declare class Network extends ReadyResource {
137
125
  inject(stream: import('streamx').Duplex, { isInitiator }?: {
138
126
  isInitiator?: boolean;
139
127
  }): import('@hyperswarm/secret-stream');
128
+ /** @private */
129
+ private _add;
140
130
  /**
141
131
  * The blind-peering client, built on first use: a mailbox post names mirrors this network
142
132
  * may not have been given.
@@ -144,19 +134,6 @@ export declare class Network extends ReadyResource {
144
134
  * @returns {import('blind-peering')}
145
135
  */
146
136
  peering(): import('blind-peering');
147
- /**
148
- * Declare this peer's self-reported info ({ name, ... }) to the peers of injected streams.
149
- *
150
- * @param {object | null} info
151
- */
152
- setInfo(info: object | null): void;
153
- /**
154
- * Info a connected peer declared about itself, or null.
155
- *
156
- * @param {Uint8Array | string} key Peer public key (bytes or hex).
157
- * @returns {object | null}
158
- */
159
- getInfo(key: Uint8Array | string): object | null;
160
137
  /**
161
138
  * Wait for pending DHT announces and lookups to settle, bounded by timeout.
162
139
  *
@@ -189,35 +166,24 @@ export declare class Network extends ReadyResource {
189
166
  mode?: 'active' | 'passive';
190
167
  }): Discovery;
191
168
  /**
192
- * Register a replicable resource (hypercore, autobee, hyperdb).
193
- * It is replicated on every current and future swarm connection.
169
+ * Whether `store` replicates on this network's connections: the network's store or a session of it.
194
170
  *
195
- * @param {Replicable} core
196
- * @returns {void}
197
- */
198
- attach(core: Replicable): void;
199
- /** @private */
200
- private _mirror;
201
- /**
202
- * Unregister a previously attached resource. New connections will no
203
- * longer replicate it (existing replication streams continue).
204
- *
205
- * @param {Replicable} core
206
- * @returns {void}
171
+ * @param {import('corestore')} store
172
+ * @returns {boolean}
207
173
  */
208
- detach(core: Replicable): void;
174
+ replicates(store: import('corestore')): boolean;
209
175
  /**
210
- * Replicate a one-off resource onto every current swarm connection
211
- * without registering it as a long-lived attachment.
176
+ * Keep a core or an autobee on the mirrors, so it stays reachable while its writers are
177
+ * offline. It rides the connections already, from the store; a mirror drops it once it closes.
212
178
  *
213
- * @param {Replicable} target
179
+ * @param {import('hypercore') | import('autobee')} core
214
180
  * @returns {void}
215
181
  */
216
- replicate(target: Replicable): void;
182
+ attach(core: import('hypercore') | import('autobee')): void;
183
+ /** @private */
184
+ private _mirror;
217
185
  /** @private */
218
186
  private _lose;
219
187
  /** @private */
220
188
  private _relook;
221
- /** @private */
222
- private _attachInfo;
223
189
  }
@@ -1,6 +1,5 @@
1
1
  import ReadyResource from 'ready-resource';
2
2
  import { Invite } from './invite.js';
3
- import { Request } from './request.js';
4
3
  import { Mailbox } from '../mailbox/index.js';
5
4
  export type KeyPair = {
6
5
  publicKey: Uint8Array;
@@ -38,6 +37,12 @@ export type InviteOpts = {
38
37
  */
39
38
  data?: Uint8Array | null;
40
39
  };
40
+ export type AcceptOpts = {
41
+ /**
42
+ * Role granted: the invite's by default, at most the invite's.
43
+ */
44
+ role?: string;
45
+ };
41
46
  export type JoinOpts = {
42
47
  /**
43
48
  * Who joins: the member they become, and who signs the join.
@@ -67,6 +72,10 @@ export type JoinOpts = {
67
72
  * Stops the join, rejecting it with `CLOSED`, as closing the mailbox does.
68
73
  */
69
74
  signal?: AbortSignal;
75
+ /**
76
+ * Saves the answer, the keys or the `DENIED`, before the join settles: the reply stays in the mailbox until it resolves, so a join resumed with the same writer after a crash gets it again.
77
+ */
78
+ keep?: (answer: JoinResult | Error) => Promise<unknown>;
70
79
  };
71
80
  export type JoinResult = {
72
81
  key: Uint8Array;
@@ -95,12 +104,16 @@ export type JoinResult = {
95
104
  * @property {boolean} [confirm] Its joins wait for a member to accept them, as requests.
96
105
  * @property {Uint8Array | null} [data] The app's payload in the invite, readable before joining: `Invite.parse(invite).data`.
97
106
  *
107
+ * @typedef {object} AcceptOpts
108
+ * @property {string} [role] Role granted: the invite's by default, at most the invite's.
109
+ *
98
110
  * @typedef {object} JoinOpts
99
111
  * @property {import('../identity/index.js').Identity} identity Who joins: the member they become, and who signs the join.
100
112
  * @property {{ dispatch: { encode: Function }, meta?: { ns?: string, version?: number } }} spec The database's spec: the join is one of its ops.
101
113
  * @property {KeyPair} [writer] Their writer keypair in the database, a fresh one by default. The join is its first block and its secret key owns the reply address: pass the same one to resume a join after a restart.
102
114
  * @property {number} [timeout] Deadline for the reply, in ms; `0` waits until the invite expires, or for good. Defaults to 30000.
103
115
  * @property {AbortSignal} [signal] Stops the join, rejecting it with `CLOSED`, as closing the mailbox does.
116
+ * @property {(answer: JoinResult | Error) => Promise<unknown>} [keep] Saves the answer, the keys or the `DENIED`, before the join settles: the reply stays in the mailbox until it resolves, so a join resumed with the same writer after a crash gets it again.
104
117
  *
105
118
  * @typedef {object} JoinResult
106
119
  * @property {Uint8Array} key
@@ -115,19 +128,15 @@ export type JoinResult = {
115
128
  * that may invite offers them while online, and the first one read settles it for all. A join
116
129
  * turned away with an invite the database knows, spent, revoked or older than the joiner's
117
130
  * removal, is answered the same way with `DENIED` and the reason. A `confirm` invite's joins wait
118
- * as requests until a member accepts or denies them; `'request'` fires for each new one.
131
+ * as rows of `requests` until a member accepts or denies one by its id.
119
132
  * `Pairing.join` is the other side: write the join, wait for the reply.
120
133
  */
121
134
  export declare class Pairing extends ReadyResource {
122
135
  mailbox: Mailbox;
123
136
  db: import("../index.js").Database;
124
- /** @type {Set<Request>} requests not answered yet: whoever attaches after one fired goes through these first */
125
- pending: Set<Request>;
126
137
  /** Whether this device answers the database's joins: it may invite, and invites or joiners owed an answer exist. */
127
138
  serving: boolean;
128
139
  /** @private */
129
- _requests;
130
- /** @private */
131
140
  _replies;
132
141
  /** @private */
133
142
  _denials;
@@ -156,19 +165,29 @@ export declare class Pairing extends ReadyResource {
156
165
  */
157
166
  revoke(invite: string): Promise<boolean>;
158
167
  /**
159
- * A join waiting on a `confirm` invite, by its id in the `requests` collection.
168
+ * Admit a join waiting on a `confirm` invite, by its id in `requests`. Every device of an
169
+ * inviter then replies with the keys.
160
170
  *
161
171
  * @param {string} id
162
- * @returns {Promise<Request>}
172
+ * @param {AcceptOpts} [opts]
173
+ * @returns {Promise<void>}
163
174
  */
164
- request(id: string): Promise<Request>;
175
+ accept(id: string, { role }?: AcceptOpts): Promise<void>;
176
+ /**
177
+ * Refuse a join waiting on a `confirm` invite, by its id in `requests`, with an optional reason.
178
+ *
179
+ * @param {string} id
180
+ * @param {string} [reason]
181
+ * @returns {Promise<void>}
182
+ */
183
+ deny(id: string, reason?: string): Promise<void>;
184
+ /** @private */
185
+ private _waiting;
165
186
  /** @private */
166
187
  private _sync;
167
188
  /** @private */
168
189
  private _arm;
169
190
  /** @private */
170
- private _pending;
171
- /** @private */
172
191
  private _answer;
173
192
  /** @private */
174
193
  private _offer;
@@ -189,7 +208,7 @@ export declare class Pairing extends ReadyResource {
189
208
  * @param {JoinOpts} opts
190
209
  * @returns {Promise<JoinResult>}
191
210
  */
192
- static join(mailbox: Mailbox, invite: string, { identity, spec, writer, timeout, signal }?: JoinOpts): Promise<JoinResult>;
211
+ static join(mailbox: Mailbox, invite: string, { identity, spec, writer, timeout, signal, keep }?: JoinOpts): Promise<JoinResult>;
193
212
  }
194
213
  /**
195
214
  * A join's payload: sealed to the database's address, so only its members read who joins; proven
@@ -0,0 +1,14 @@
1
+ export declare const STATUS_ACCEPTED = 0;
2
+ export declare const STATUS_DENIED = 1;
3
+ /** @type {import('compact-encoding').Encoder<{ status: number, reason?: string, key?: Uint8Array | null, encryptionKey?: Uint8Array | null, epochs?: Array<{ epoch: number, stamp: number, entropy: Uint8Array }> | null }>} */
4
+ export declare const Response: import('compact-encoding').Encoder<{
5
+ status: number;
6
+ reason?: string;
7
+ key?: Uint8Array | null;
8
+ encryptionKey?: Uint8Array | null;
9
+ epochs?: Array<{
10
+ epoch: number;
11
+ stamp: number;
12
+ entropy: Uint8Array;
13
+ }> | null;
14
+ }>;
@@ -1,94 +0,0 @@
1
- import hid from 'hypercore-id-encoding'
2
- import c from 'compact-encoding'
3
-
4
- import { getEncoding } from '../lib/spec/index.js'
5
- import { CeroError } from '../lib/errors.js'
6
- import { grants, isRank } from '../lib/utils.js'
7
-
8
- export const STATUS_ACCEPTED = 0
9
- export const STATUS_DENIED = 1
10
-
11
- // an accept and a deny both ride this response
12
- /** @type {import('compact-encoding').Encoder<{ status: number, reason?: string, key?: Uint8Array | null, encryptionKey?: Uint8Array | null, epochs?: Array<{ epoch: number, stamp: number, entropy: Uint8Array }> | null }>} */
13
- export const Response = getEncoding('@cero/confirm')
14
-
15
- /**
16
- * @typedef {object} RequestOpts
17
- * @property {import('./index.js').Pairing} pairing Owning Pairing instance.
18
- * @property {{ id: string, role: string, expires?: number }} invite The record of the invite the joiner used.
19
- * @property {{ id: string, identity: Uint8Array, reply: Uint8Array }} row The waiting join, as the database keeps it.
20
- *
21
- * @typedef {object} AcceptOpts
22
- * @property {string} [role] Role granted: the invite's by default, at most the invite's.
23
- */
24
-
25
- /**
26
- * A join on a `confirm` invite, waiting in the database for a member to accept or deny it.
27
- */
28
- export class Request {
29
- /** @param {RequestOpts} opts */
30
- constructor({ pairing, invite, row }) {
31
- this.pairing = pairing
32
- this.invite = invite
33
- this.id = row.id
34
- this.identity = row.identity
35
- /** The role the join asks for: its invite's. */
36
- this.role = invite.role
37
- /** @type {Uint8Array} */
38
- this.writer = hid.decode(row.id)
39
- /** @private */
40
- this._reply = row.reply
41
- /** @private */
42
- this._answer = null
43
- }
44
-
45
- /**
46
- * Admit the joiner. Every device of an inviter then replies with the keys. Idempotent.
47
- *
48
- * @param {AcceptOpts} [opts]
49
- * @returns {Promise<void>}
50
- */
51
- async accept({ role } = {}) {
52
- if (this._answer) return this._answer
53
- const { invite } = this
54
- if (invite.expires > 0 && Date.now() > invite.expires) throw CeroError.EXPIRED()
55
- role = role || invite.role
56
- if (!isRank(role)) {
57
- throw CeroError.INVALID(`role '${role}' is not a rank (owner, admin, member, reader)`)
58
- }
59
- if (!grants(invite.role, role)) {
60
- throw CeroError.INVALID(`role '${role}' exceeds the invite role '${invite.role}'`)
61
- }
62
- return this._answering(this.pairing.db.call('accept', { id: this.id, role }))
63
- }
64
-
65
- /**
66
- * Refuse the joiner, with an optional reason. Idempotent.
67
- *
68
- * @param {string} [reason]
69
- * @returns {Promise<void>}
70
- */
71
- async deny(reason = '') {
72
- if (this._answer) return this._answer
73
- const { db, mailbox } = this.pairing
74
- const denied = { status: STATUS_DENIED, reason, key: null, encryptionKey: null, epochs: null }
75
- const denying = async () => {
76
- await db.call('del-request', { id: this.id })
77
- await mailbox.send(this._reply, c.encode(Response, denied))
78
- }
79
- return this._answering(denying())
80
- }
81
-
82
- // the first answer holds while it is written; refused, the request is answerable again
83
- /**
84
- * @private
85
- * @param {Promise<void>} answer
86
- */
87
- _answering(answer) {
88
- this._answer = answer
89
- answer.catch(() => {
90
- this._answer = null
91
- })
92
- return answer
93
- }
94
- }
@@ -1,93 +0,0 @@
1
- export declare const STATUS_ACCEPTED = 0;
2
- export declare const STATUS_DENIED = 1;
3
- /** @type {import('compact-encoding').Encoder<{ status: number, reason?: string, key?: Uint8Array | null, encryptionKey?: Uint8Array | null, epochs?: Array<{ epoch: number, stamp: number, entropy: Uint8Array }> | null }>} */
4
- export declare const Response: import('compact-encoding').Encoder<{
5
- status: number;
6
- reason?: string;
7
- key?: Uint8Array | null;
8
- encryptionKey?: Uint8Array | null;
9
- epochs?: Array<{
10
- epoch: number;
11
- stamp: number;
12
- entropy: Uint8Array;
13
- }> | null;
14
- }>;
15
- export type RequestOpts = {
16
- /**
17
- * Owning Pairing instance.
18
- */
19
- pairing: import('./index.js').Pairing;
20
- /**
21
- * The record of the invite the joiner used.
22
- */
23
- invite: {
24
- id: string;
25
- role: string;
26
- expires?: number;
27
- };
28
- /**
29
- * The waiting join, as the database keeps it.
30
- */
31
- row: {
32
- id: string;
33
- identity: Uint8Array;
34
- reply: Uint8Array;
35
- };
36
- };
37
- export type AcceptOpts = {
38
- /**
39
- * Role granted: the invite's by default, at most the invite's.
40
- */
41
- role?: string;
42
- };
43
- /**
44
- * @typedef {object} RequestOpts
45
- * @property {import('./index.js').Pairing} pairing Owning Pairing instance.
46
- * @property {{ id: string, role: string, expires?: number }} invite The record of the invite the joiner used.
47
- * @property {{ id: string, identity: Uint8Array, reply: Uint8Array }} row The waiting join, as the database keeps it.
48
- *
49
- * @typedef {object} AcceptOpts
50
- * @property {string} [role] Role granted: the invite's by default, at most the invite's.
51
- */
52
- /**
53
- * A join on a `confirm` invite, waiting in the database for a member to accept or deny it.
54
- */
55
- export declare class Request {
56
- pairing: import("./index.js").Pairing;
57
- invite: {
58
- id: string;
59
- role: string;
60
- expires?: number;
61
- };
62
- id: string;
63
- identity: Uint8Array<ArrayBufferLike>;
64
- /** The role the join asks for: its invite's. */
65
- role: string;
66
- /** @type {Uint8Array} */
67
- writer: Uint8Array;
68
- /** @private */
69
- _reply;
70
- /** @private */
71
- _answer;
72
- /** @param {RequestOpts} opts */
73
- constructor({ pairing, invite, row }: RequestOpts);
74
- /**
75
- * Admit the joiner. Every device of an inviter then replies with the keys. Idempotent.
76
- *
77
- * @param {AcceptOpts} [opts]
78
- * @returns {Promise<void>}
79
- */
80
- accept({ role }?: AcceptOpts): Promise<void>;
81
- /**
82
- * Refuse the joiner, with an optional reason. Idempotent.
83
- *
84
- * @param {string} [reason]
85
- * @returns {Promise<void>}
86
- */
87
- deny(reason?: string): Promise<void>;
88
- /**
89
- * @private
90
- * @param {Promise<void>} answer
91
- */
92
- private _answering;
93
- }