@cero-base/core 2.3.3 → 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.
@@ -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
- }