@cero-base/core 2.2.1 → 2.3.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/README.md +32 -212
- package/package.json +18 -9
- package/src/blobs/index.js +9 -0
- package/src/blobs/server.js +2 -0
- package/src/database/changes.js +1 -1
- package/src/database/dispatch.js +341 -209
- package/src/database/encryption.js +83 -33
- package/src/database/index.js +184 -83
- package/src/database/rotation.js +43 -30
- package/src/identity/index.js +20 -78
- package/src/index.js +3 -10
- package/src/lib/constants.js +2 -0
- package/src/lib/errors.js +3 -11
- package/src/lib/spec/index.js +136 -132
- package/src/lib/spec/schema.json +84 -99
- package/src/lib/utils.js +57 -3
- package/src/mailbox/inbox.js +103 -0
- package/src/mailbox/index.js +146 -0
- package/src/mailbox/post.js +69 -0
- package/src/network/discovery.js +26 -1
- package/src/network/index.js +105 -64
- package/src/network/presence.js +6 -2
- package/src/pairing/index.js +330 -254
- package/src/pairing/invite.js +83 -144
- package/src/pairing/request.js +54 -56
- package/src/rpc/index.js +18 -93
- package/src/rpc/peer.js +4 -1
- package/src/storage/index.js +78 -39
- package/types/blobs/index.d.ts +22 -9
- package/types/blobs/server.d.ts +4 -2
- package/types/database/changes.d.ts +2 -2
- package/types/database/dispatch.d.ts +19 -13
- package/types/database/encryption.d.ts +67 -29
- package/types/database/index.d.ts +144 -119
- package/types/database/rotation.d.ts +32 -13
- package/types/identity/index.d.ts +6 -47
- package/types/index.d.ts +3 -16
- package/types/lib/constants.d.ts +1 -0
- package/types/lib/errors.d.ts +3 -9
- package/types/lib/spec/index.d.ts +52 -16
- package/types/lib/utils.d.ts +40 -8
- package/types/mailbox/inbox.d.ts +52 -0
- package/types/mailbox/index.d.ts +111 -0
- package/types/mailbox/post.d.ts +37 -0
- package/types/network/discovery.d.ts +21 -5
- package/types/network/index.d.ts +64 -48
- package/types/network/presence.d.ts +17 -4
- package/types/pairing/index.d.ts +143 -134
- package/types/pairing/invite.d.ts +70 -114
- package/types/pairing/request.d.ts +58 -98
- package/types/rpc/index.d.ts +26 -44
- package/types/rpc/peer.d.ts +10 -7
- package/types/storage/index.d.ts +64 -54
- package/src/pairing/candidate.js +0 -123
- package/types/pairing/candidate.d.ts +0 -36
package/src/lib/spec/schema.json
CHANGED
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version":
|
|
2
|
+
"version": 1,
|
|
3
3
|
"schema": [
|
|
4
4
|
{
|
|
5
|
-
"name": "
|
|
5
|
+
"name": "link",
|
|
6
|
+
"namespace": "cero",
|
|
7
|
+
"compact": true,
|
|
8
|
+
"flagsPosition": -1,
|
|
9
|
+
"fields": [
|
|
10
|
+
{
|
|
11
|
+
"name": "key",
|
|
12
|
+
"required": true,
|
|
13
|
+
"type": "fixed32",
|
|
14
|
+
"version": 1
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"name": "length",
|
|
18
|
+
"required": true,
|
|
19
|
+
"type": "uint",
|
|
20
|
+
"version": 1
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"name": "invite",
|
|
6
26
|
"namespace": "cero",
|
|
7
27
|
"compact": false,
|
|
8
|
-
"flagsPosition":
|
|
28
|
+
"flagsPosition": 6,
|
|
9
29
|
"fields": [
|
|
10
30
|
{
|
|
11
31
|
"name": "version",
|
|
@@ -14,83 +34,109 @@
|
|
|
14
34
|
"version": 1
|
|
15
35
|
},
|
|
16
36
|
{
|
|
17
|
-
"name": "
|
|
37
|
+
"name": "expires",
|
|
18
38
|
"required": true,
|
|
19
|
-
"type": "
|
|
39
|
+
"type": "uint",
|
|
20
40
|
"version": 1
|
|
21
41
|
},
|
|
22
42
|
{
|
|
23
|
-
"name": "
|
|
43
|
+
"name": "key",
|
|
24
44
|
"required": true,
|
|
25
|
-
"type": "
|
|
45
|
+
"type": "fixed32",
|
|
26
46
|
"version": 1
|
|
27
47
|
},
|
|
28
48
|
{
|
|
29
|
-
"name": "
|
|
49
|
+
"name": "address",
|
|
30
50
|
"required": true,
|
|
31
|
-
"type": "
|
|
51
|
+
"type": "fixed32",
|
|
32
52
|
"version": 1
|
|
33
53
|
},
|
|
34
54
|
{
|
|
35
|
-
"name": "
|
|
36
|
-
"required":
|
|
37
|
-
"type": "
|
|
55
|
+
"name": "seed",
|
|
56
|
+
"required": true,
|
|
57
|
+
"type": "fixed32",
|
|
38
58
|
"version": 1
|
|
39
59
|
},
|
|
40
60
|
{
|
|
41
|
-
"name": "
|
|
61
|
+
"name": "link",
|
|
42
62
|
"required": true,
|
|
63
|
+
"type": "@cero/link",
|
|
64
|
+
"version": 1
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"name": "data",
|
|
68
|
+
"required": false,
|
|
43
69
|
"type": "buffer",
|
|
44
70
|
"version": 1
|
|
45
71
|
}
|
|
46
72
|
]
|
|
47
73
|
},
|
|
48
74
|
{
|
|
49
|
-
"name": "
|
|
75
|
+
"name": "join",
|
|
50
76
|
"namespace": "cero",
|
|
51
77
|
"compact": false,
|
|
52
|
-
"flagsPosition":
|
|
78
|
+
"flagsPosition": 5,
|
|
53
79
|
"fields": [
|
|
54
80
|
{
|
|
55
|
-
"name": "
|
|
81
|
+
"name": "invite",
|
|
56
82
|
"required": true,
|
|
57
|
-
"type": "
|
|
83
|
+
"type": "fixed32",
|
|
58
84
|
"version": 1
|
|
59
85
|
},
|
|
60
86
|
{
|
|
61
|
-
"name": "
|
|
87
|
+
"name": "reply",
|
|
62
88
|
"required": true,
|
|
63
89
|
"type": "fixed32",
|
|
64
90
|
"version": 1
|
|
65
91
|
},
|
|
66
92
|
{
|
|
67
|
-
"name": "
|
|
93
|
+
"name": "proof",
|
|
68
94
|
"required": true,
|
|
69
|
-
"type": "
|
|
95
|
+
"type": "fixed64",
|
|
70
96
|
"version": 1
|
|
71
97
|
},
|
|
72
98
|
{
|
|
73
|
-
"name": "
|
|
99
|
+
"name": "identity",
|
|
74
100
|
"required": true,
|
|
75
|
-
"type": "
|
|
101
|
+
"type": "fixed32",
|
|
76
102
|
"version": 1
|
|
77
103
|
},
|
|
78
104
|
{
|
|
79
|
-
"name": "
|
|
105
|
+
"name": "signature",
|
|
106
|
+
"required": true,
|
|
107
|
+
"type": "fixed64",
|
|
108
|
+
"version": 1
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
"name": "ts",
|
|
80
112
|
"required": false,
|
|
81
|
-
"type": "
|
|
113
|
+
"type": "uint",
|
|
114
|
+
"version": 1
|
|
115
|
+
}
|
|
116
|
+
]
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
"name": "epoch",
|
|
120
|
+
"namespace": "cero",
|
|
121
|
+
"compact": true,
|
|
122
|
+
"flagsPosition": -1,
|
|
123
|
+
"fields": [
|
|
124
|
+
{
|
|
125
|
+
"name": "epoch",
|
|
126
|
+
"required": true,
|
|
127
|
+
"type": "uint",
|
|
82
128
|
"version": 1
|
|
83
129
|
},
|
|
84
130
|
{
|
|
85
|
-
"name": "
|
|
131
|
+
"name": "stamp",
|
|
86
132
|
"required": true,
|
|
87
|
-
"type": "
|
|
133
|
+
"type": "uint",
|
|
88
134
|
"version": 1
|
|
89
135
|
},
|
|
90
136
|
{
|
|
91
|
-
"name": "
|
|
137
|
+
"name": "entropy",
|
|
92
138
|
"required": true,
|
|
93
|
-
"type": "
|
|
139
|
+
"type": "fixed32",
|
|
94
140
|
"version": 1
|
|
95
141
|
}
|
|
96
142
|
]
|
|
@@ -126,9 +172,10 @@
|
|
|
126
172
|
"version": 1
|
|
127
173
|
},
|
|
128
174
|
{
|
|
129
|
-
"name": "
|
|
175
|
+
"name": "epochs",
|
|
130
176
|
"required": false,
|
|
131
|
-
"
|
|
177
|
+
"array": true,
|
|
178
|
+
"type": "@cero/epoch",
|
|
132
179
|
"version": 1
|
|
133
180
|
}
|
|
134
181
|
]
|
|
@@ -149,51 +196,23 @@
|
|
|
149
196
|
]
|
|
150
197
|
},
|
|
151
198
|
{
|
|
152
|
-
"name": "
|
|
199
|
+
"name": "changes",
|
|
153
200
|
"namespace": "cero",
|
|
154
201
|
"compact": false,
|
|
155
202
|
"flagsPosition": 0,
|
|
156
203
|
"fields": [
|
|
157
204
|
{
|
|
158
|
-
"name": "
|
|
159
|
-
"required": false,
|
|
160
|
-
"type": "string",
|
|
161
|
-
"version": 1
|
|
162
|
-
},
|
|
163
|
-
{
|
|
164
|
-
"name": "gte",
|
|
165
|
-
"required": false,
|
|
166
|
-
"type": "string",
|
|
167
|
-
"version": 1
|
|
168
|
-
},
|
|
169
|
-
{
|
|
170
|
-
"name": "lt",
|
|
171
|
-
"required": false,
|
|
172
|
-
"type": "string",
|
|
173
|
-
"version": 1
|
|
174
|
-
},
|
|
175
|
-
{
|
|
176
|
-
"name": "lte",
|
|
177
|
-
"required": false,
|
|
178
|
-
"type": "string",
|
|
179
|
-
"version": 1
|
|
180
|
-
},
|
|
181
|
-
{
|
|
182
|
-
"name": "limit",
|
|
183
|
-
"required": false,
|
|
184
|
-
"type": "uint",
|
|
185
|
-
"version": 1
|
|
186
|
-
},
|
|
187
|
-
{
|
|
188
|
-
"name": "reverse",
|
|
205
|
+
"name": "prev",
|
|
189
206
|
"required": false,
|
|
190
|
-
"
|
|
207
|
+
"array": true,
|
|
208
|
+
"type": "buffer",
|
|
191
209
|
"version": 1
|
|
192
210
|
},
|
|
193
211
|
{
|
|
194
|
-
"name": "
|
|
212
|
+
"name": "next",
|
|
195
213
|
"required": false,
|
|
196
|
-
"
|
|
214
|
+
"array": true,
|
|
215
|
+
"type": "buffer",
|
|
197
216
|
"version": 1
|
|
198
217
|
}
|
|
199
218
|
]
|
|
@@ -215,18 +234,6 @@
|
|
|
215
234
|
"required": false,
|
|
216
235
|
"type": "string",
|
|
217
236
|
"version": 1
|
|
218
|
-
},
|
|
219
|
-
{
|
|
220
|
-
"name": "role",
|
|
221
|
-
"required": false,
|
|
222
|
-
"type": "string",
|
|
223
|
-
"version": 1
|
|
224
|
-
},
|
|
225
|
-
{
|
|
226
|
-
"name": "noAccept",
|
|
227
|
-
"required": false,
|
|
228
|
-
"type": "bool",
|
|
229
|
-
"version": 1
|
|
230
237
|
}
|
|
231
238
|
]
|
|
232
239
|
},
|
|
@@ -273,28 +280,6 @@
|
|
|
273
280
|
"version": 1
|
|
274
281
|
}
|
|
275
282
|
]
|
|
276
|
-
},
|
|
277
|
-
{
|
|
278
|
-
"name": "changes",
|
|
279
|
-
"namespace": "cero",
|
|
280
|
-
"compact": false,
|
|
281
|
-
"flagsPosition": 0,
|
|
282
|
-
"fields": [
|
|
283
|
-
{
|
|
284
|
-
"name": "prev",
|
|
285
|
-
"required": false,
|
|
286
|
-
"array": true,
|
|
287
|
-
"type": "buffer",
|
|
288
|
-
"version": 2
|
|
289
|
-
},
|
|
290
|
-
{
|
|
291
|
-
"name": "next",
|
|
292
|
-
"required": false,
|
|
293
|
-
"array": true,
|
|
294
|
-
"type": "buffer",
|
|
295
|
-
"version": 2
|
|
296
|
-
}
|
|
297
|
-
]
|
|
298
283
|
}
|
|
299
284
|
]
|
|
300
285
|
}
|
package/src/lib/utils.js
CHANGED
|
@@ -2,6 +2,7 @@ import b4a from 'b4a'
|
|
|
2
2
|
import { Readable } from 'streamx'
|
|
3
3
|
|
|
4
4
|
import { ROLE_PERMS, RANK, QUERY_RESERVED } from './constants.js'
|
|
5
|
+
import { CeroError } from './errors.js'
|
|
5
6
|
|
|
6
7
|
// can() callers need the capability names, and constants.js is not public
|
|
7
8
|
export { WRITE, INVITE, ASSIGN, REMOVE } from './constants.js'
|
|
@@ -9,6 +10,10 @@ export { WRITE, INVITE, ASSIGN, REMOVE } from './constants.js'
|
|
|
9
10
|
// binding the db key makes an admission unreplayable across rooms
|
|
10
11
|
const ADD_WRITER_TAG = b4a.from('cero/add-writer')
|
|
11
12
|
const CLAIM_WRITER_TAG = b4a.from('cero/claim-writer')
|
|
13
|
+
const JOIN_TAG = b4a.from('cero/join')
|
|
14
|
+
|
|
15
|
+
// fields the write path stamps itself, always allowed
|
|
16
|
+
const SYSTEM_FIELDS = new Set(['id', 'memberId', 'index', 'createdAt', 'updatedAt'])
|
|
12
17
|
|
|
13
18
|
/** @type {(dbKey: Uint8Array, writer: Uint8Array, appender: Uint8Array) => Uint8Array} */
|
|
14
19
|
export function admission(dbKey, writer, appender) {
|
|
@@ -20,6 +25,12 @@ export function ownership(dbKey, writer) {
|
|
|
20
25
|
return b4a.concat([CLAIM_WRITER_TAG, dbKey, writer])
|
|
21
26
|
}
|
|
22
27
|
|
|
28
|
+
// what a joiner's identity signs: this room, this invite, this writer, where the keys go
|
|
29
|
+
/** @type {(dbKey: Uint8Array, invite: Uint8Array, writer: Uint8Array, reply: Uint8Array) => Uint8Array} */
|
|
30
|
+
export function joining(dbKey, invite, writer, reply) {
|
|
31
|
+
return b4a.concat([JOIN_TAG, dbKey, invite, writer, reply])
|
|
32
|
+
}
|
|
33
|
+
|
|
23
34
|
/**
|
|
24
35
|
* Whether `role` is granted `perm` under the default policy.
|
|
25
36
|
*
|
|
@@ -33,13 +44,16 @@ export function can(role, perm) {
|
|
|
33
44
|
}
|
|
34
45
|
|
|
35
46
|
// an unknown role grants nothing; callers taking a role from an app must check it
|
|
47
|
+
/** @type {(role: string) => boolean} */
|
|
36
48
|
export function isRank(role) {
|
|
37
49
|
return RANK[role] != null
|
|
38
50
|
}
|
|
39
51
|
|
|
52
|
+
/** @type {(a: string, b: string) => boolean} */
|
|
40
53
|
export function grants(a, b) {
|
|
41
54
|
return RANK[a] != null && RANK[b] != null && RANK[a] >= RANK[b]
|
|
42
55
|
}
|
|
56
|
+
/** @type {(a: string, b: string) => boolean} */
|
|
43
57
|
export function outranks(a, b) {
|
|
44
58
|
return RANK[a] != null && RANK[b] != null && RANK[a] > RANK[b]
|
|
45
59
|
}
|
|
@@ -107,6 +121,7 @@ const fold = (s) =>
|
|
|
107
121
|
.replace(/\s/g, '')
|
|
108
122
|
|
|
109
123
|
// every term must be a substring of a searched field
|
|
124
|
+
/** @param {Record<string, unknown>} row @param {string} term @param {string[]} [fields] @returns {boolean} */
|
|
110
125
|
export function searchHit(row, term, fields) {
|
|
111
126
|
const terms = String(term).split(/\s+/).map(fold).filter(Boolean)
|
|
112
127
|
// memberId is a random z32, it would produce spurious hits
|
|
@@ -119,9 +134,10 @@ export function searchHit(row, term, fields) {
|
|
|
119
134
|
* The in-memory query grammar over rows: equality on any non-reserved field,
|
|
120
135
|
* id ranges, `search`, then `reverse` and `limit`.
|
|
121
136
|
*
|
|
122
|
-
* @
|
|
123
|
-
* @param {
|
|
124
|
-
* @
|
|
137
|
+
* @template {Record<string, unknown>} T
|
|
138
|
+
* @param {T[]} rows
|
|
139
|
+
* @param {Record<string, unknown>} [query]
|
|
140
|
+
* @returns {T[]}
|
|
125
141
|
*/
|
|
126
142
|
export function filter(rows, query) {
|
|
127
143
|
if (!query) return rows
|
|
@@ -158,3 +174,41 @@ export function onAbort(signal, cb) {
|
|
|
158
174
|
signal.addEventListener('abort', cb, { once: true })
|
|
159
175
|
return () => signal.removeEventListener('abort', cb)
|
|
160
176
|
}
|
|
177
|
+
|
|
178
|
+
/** @type {<T>(row: T, createdAt?: number | null, ts?: number) => T & { createdAt: number, updatedAt: number }} */
|
|
179
|
+
export function stamp(row, createdAt, ts = Date.now()) {
|
|
180
|
+
return { ...row, createdAt: createdAt ?? ts, updatedAt: ts }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Refuse a field the ref does not declare: the encoder would drop it and the returned row would lie.
|
|
185
|
+
*
|
|
186
|
+
* @param {string} name
|
|
187
|
+
* @param {{ fields?: string[] } | undefined} ref
|
|
188
|
+
* @param {Record<string, unknown> | null | undefined} row
|
|
189
|
+
*/
|
|
190
|
+
export function checkFields(name, ref, row) {
|
|
191
|
+
const declared = ref?.fields
|
|
192
|
+
if (!declared || !row) return
|
|
193
|
+
for (const key of Object.keys(row)) {
|
|
194
|
+
if (declared.includes(key) || SYSTEM_FIELDS.has(key)) continue
|
|
195
|
+
throw CeroError.INVALID(
|
|
196
|
+
`unknown field '${key}' on '${name}' — declared: ${declared.join(', ') || '(none)'}`
|
|
197
|
+
)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Refuse a whole row that lacks a required field: the encoder would fail on it with no code.
|
|
203
|
+
*
|
|
204
|
+
* @param {string} name
|
|
205
|
+
* @param {{ required?: Record<string, string> } | undefined} ref
|
|
206
|
+
* @param {Record<string, unknown>} row
|
|
207
|
+
*/
|
|
208
|
+
export function checkRequired(name, ref, row) {
|
|
209
|
+
for (const key in ref?.required) {
|
|
210
|
+
if (row[key] == null && !SYSTEM_FIELDS.has(key)) {
|
|
211
|
+
throw CeroError.INVALID(`'${key}' is required on '${name}'`)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import ReadyResource from 'ready-resource'
|
|
2
|
+
import safetyCatch from 'safety-catch'
|
|
3
|
+
import crypto from 'hypercore-crypto'
|
|
4
|
+
import b4a from 'b4a'
|
|
5
|
+
|
|
6
|
+
const NS = b4a.from('cero/mailbox')
|
|
7
|
+
const READ_TIMEOUT = 30000
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The keypair behind the address a secret owns.
|
|
11
|
+
*
|
|
12
|
+
* @param {Uint8Array} secret
|
|
13
|
+
* @returns {{ publicKey: Uint8Array, secretKey: Uint8Array }}
|
|
14
|
+
*/
|
|
15
|
+
export function keyPair(secret) {
|
|
16
|
+
return crypto.encryptionKeyPair(crypto.hash([NS, secret]))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Receives at the address a secret owns, direct from the sender or from a mirror. A message
|
|
21
|
+
* that does not open is dropped; one that opens is kept in `box` until `onmessage` resolves.
|
|
22
|
+
*/
|
|
23
|
+
export class Inbox extends ReadyResource {
|
|
24
|
+
/**
|
|
25
|
+
* @param {import('../network/index.js').Network} network
|
|
26
|
+
* @param {Uint8Array} secret
|
|
27
|
+
* @param {{ box: import('./index.js').Box, onmessage: (message: Uint8Array) => unknown, onerror: (err: Error) => void }} opts
|
|
28
|
+
*/
|
|
29
|
+
constructor(network, secret, { box, onmessage, onerror }) {
|
|
30
|
+
super()
|
|
31
|
+
this.network = network
|
|
32
|
+
this.keyPair = keyPair(secret)
|
|
33
|
+
this.address = this.keyPair.publicKey
|
|
34
|
+
this.box = box
|
|
35
|
+
this.onmessage = onmessage
|
|
36
|
+
this.onerror = onerror
|
|
37
|
+
/** @private */
|
|
38
|
+
this._seen = new Set()
|
|
39
|
+
/** @private */
|
|
40
|
+
this._session = null
|
|
41
|
+
/** @private */
|
|
42
|
+
this._discovery = null
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @private */
|
|
46
|
+
async _open() {
|
|
47
|
+
for (const mail of await this.box.list()) {
|
|
48
|
+
if (!b4a.equals(mail.address, this.address)) continue
|
|
49
|
+
this._seen.add(mail.id)
|
|
50
|
+
this._handle(mail)
|
|
51
|
+
}
|
|
52
|
+
this._session = this.network.wakeup.session(this.address, {
|
|
53
|
+
discoveryKey: crypto.discoveryKey(this.address),
|
|
54
|
+
onannounce: (entries) => {
|
|
55
|
+
for (const { key } of entries) this._onannounce(key)
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
// active on both ends: whichever side comes second is the one that looks the other up
|
|
59
|
+
this._discovery = this.network.join(crypto.discoveryKey(this.address))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** @private */
|
|
63
|
+
async _close() {
|
|
64
|
+
this._session?.destroy()
|
|
65
|
+
// the unannounce is a DHT round trip, never waited on
|
|
66
|
+
this._discovery?.destroy().catch(safetyCatch)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** @private */
|
|
70
|
+
_onannounce(key) {
|
|
71
|
+
const id = b4a.toHex(key)
|
|
72
|
+
if (this._seen.has(id)) return
|
|
73
|
+
this._seen.add(id)
|
|
74
|
+
// unread, so the next announce (a mirror's, a reconnect's) retries it
|
|
75
|
+
this._read(id, key).catch(() => this._seen.delete(id))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** @private */
|
|
79
|
+
async _read(id, key) {
|
|
80
|
+
const core = this.network.store.get({ key })
|
|
81
|
+
let message
|
|
82
|
+
try {
|
|
83
|
+
message = crypto.decrypt(await core.get(0, { timeout: READ_TIMEOUT }), this.keyPair)
|
|
84
|
+
} finally {
|
|
85
|
+
await core.close()
|
|
86
|
+
}
|
|
87
|
+
if (!message) return
|
|
88
|
+
const mail = { id, address: this.address, message }
|
|
89
|
+
await this.box.put(mail)
|
|
90
|
+
this._handle(mail)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// dropped once handled; one that throws stays for the next time this address is received
|
|
94
|
+
/** @private */
|
|
95
|
+
async _handle({ id, message }) {
|
|
96
|
+
try {
|
|
97
|
+
await this.onmessage(message)
|
|
98
|
+
await this.box.del(id)
|
|
99
|
+
} catch (err) {
|
|
100
|
+
this.onerror(err)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import ReadyResource from 'ready-resource'
|
|
2
|
+
import safetyCatch from 'safety-catch'
|
|
3
|
+
import crypto from 'hypercore-crypto'
|
|
4
|
+
import b4a from 'b4a'
|
|
5
|
+
|
|
6
|
+
import { Inbox, keyPair } from './inbox.js'
|
|
7
|
+
import { Post } from './post.js'
|
|
8
|
+
import { CeroError } from '../lib/errors.js'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {object} Mail
|
|
12
|
+
* @property {string} id Unique per message, hex.
|
|
13
|
+
* @property {Uint8Array} address Where it goes, or where it arrived.
|
|
14
|
+
* @property {Uint8Array} message
|
|
15
|
+
* @property {Uint8Array[]} [mirrors] Where a sent message waits for an owner who is offline.
|
|
16
|
+
*
|
|
17
|
+
* @typedef {object} Box Where mail is kept; memory unless given a persistent one.
|
|
18
|
+
* @property {() => Promise<Mail[]>} list
|
|
19
|
+
* @property {(mail: Mail) => Promise<unknown>} put
|
|
20
|
+
* @property {(id: string) => Promise<unknown>} del
|
|
21
|
+
*
|
|
22
|
+
* @typedef {object} MailboxOpts
|
|
23
|
+
* @property {Box} [inbox] Keeps a received message until it was handled.
|
|
24
|
+
* @property {Box} [outbox] Keeps a sent message until someone read it.
|
|
25
|
+
* @property {(err: Error) => void} [onerror] Called when a message fails to be handled or sent.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A device's mailbox. It receives at the addresses it holds the secret of, and sends to any
|
|
30
|
+
* address. Both ends keep their mail until it is done with: the outbox until someone read it,
|
|
31
|
+
* the inbox until it was handled. Give it persistent boxes and that survives a restart: it
|
|
32
|
+
* sends again what was not read and hands over again what was not handled, so a message may
|
|
33
|
+
* arrive twice, never not at all.
|
|
34
|
+
*/
|
|
35
|
+
export class Mailbox extends ReadyResource {
|
|
36
|
+
/**
|
|
37
|
+
* @param {import('../network/index.js').Network} network Needs a store: messages travel as cores in it.
|
|
38
|
+
* @param {MailboxOpts} [opts]
|
|
39
|
+
*/
|
|
40
|
+
constructor(
|
|
41
|
+
network,
|
|
42
|
+
{ inbox = memory(), outbox = memory(), onerror = (err) => console.error(err) } = {}
|
|
43
|
+
) {
|
|
44
|
+
super()
|
|
45
|
+
if (!network?.store) throw CeroError.REQUIRED('store')
|
|
46
|
+
this.network = network
|
|
47
|
+
this.inbox = inbox
|
|
48
|
+
this.outbox = outbox
|
|
49
|
+
this.onerror = onerror
|
|
50
|
+
/** @private */
|
|
51
|
+
this._resources = new Set() // inboxes, and posts not read yet
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The address a secret owns: share it, anyone can send to it, only the secret opens.
|
|
56
|
+
*
|
|
57
|
+
* @param {Uint8Array} secret
|
|
58
|
+
* @returns {Uint8Array}
|
|
59
|
+
*/
|
|
60
|
+
static getAddress(secret) {
|
|
61
|
+
return keyPair(secret).publicKey
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** @private */
|
|
65
|
+
async _open() {
|
|
66
|
+
await this.network.ready()
|
|
67
|
+
for (const mail of await this.outbox.list()) this._deliver(mail)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @private */
|
|
71
|
+
async _close() {
|
|
72
|
+
const open = [...this._resources]
|
|
73
|
+
this._resources.clear()
|
|
74
|
+
await Promise.allSettled(open.map((resource) => resource.close()))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Receive the messages sent to the address `secret` owns, until the returned inbox closes.
|
|
79
|
+
* A message stays in the inbox until `onmessage` resolves; one it did not finish is handed
|
|
80
|
+
* over again the next time this address is received.
|
|
81
|
+
*
|
|
82
|
+
* @param {Uint8Array} secret
|
|
83
|
+
* @param {(message: Uint8Array) => unknown} onmessage
|
|
84
|
+
* @returns {{ close: () => Promise<void> }}
|
|
85
|
+
*/
|
|
86
|
+
receive(secret, onmessage) {
|
|
87
|
+
const inbox = new Inbox(this.network, secret, {
|
|
88
|
+
box: this.inbox,
|
|
89
|
+
onmessage,
|
|
90
|
+
onerror: this.onerror
|
|
91
|
+
})
|
|
92
|
+
this._track(inbox)
|
|
93
|
+
inbox.ready().catch(this.onerror)
|
|
94
|
+
return inbox
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Send a message to an address. Resolves once the outbox keeps it; it is delivered in the
|
|
99
|
+
* background, and dropped from the outbox once someone read it.
|
|
100
|
+
*
|
|
101
|
+
* @param {Uint8Array} address
|
|
102
|
+
* @param {Uint8Array} message
|
|
103
|
+
* @param {{ mirrors?: Uint8Array[] }} [opts] Where it waits for an owner who is offline; the network's by default.
|
|
104
|
+
* @returns {Promise<void>}
|
|
105
|
+
*/
|
|
106
|
+
async send(address, message, { mirrors = this.network.mirrors } = {}) {
|
|
107
|
+
if (!this.opened) await this.ready()
|
|
108
|
+
const mail = { id: b4a.toHex(crypto.randomBytes(16)), address, message, mirrors }
|
|
109
|
+
await this.outbox.put(mail)
|
|
110
|
+
this._deliver(mail)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** @private */
|
|
114
|
+
_deliver({ id, address, message, mirrors }) {
|
|
115
|
+
const post = new Post(this.network, address, message, { mirrors })
|
|
116
|
+
this._track(post)
|
|
117
|
+
deliver(post, () => this.outbox.del(id)).catch(safetyCatch)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** @private */
|
|
121
|
+
_track(resource) {
|
|
122
|
+
this._resources.add(resource)
|
|
123
|
+
resource.once('close', () => this._resources.delete(resource))
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// wait until a post is read once, then close it
|
|
128
|
+
async function deliver(post, ondelivered) {
|
|
129
|
+
try {
|
|
130
|
+
await post.ready()
|
|
131
|
+
await post.delivered
|
|
132
|
+
await ondelivered()
|
|
133
|
+
} finally {
|
|
134
|
+
await post.close()
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** @returns {Box} */
|
|
139
|
+
function memory() {
|
|
140
|
+
const kept = new Map()
|
|
141
|
+
return {
|
|
142
|
+
list: async () => [...kept.values()],
|
|
143
|
+
put: async (mail) => kept.set(mail.id, mail),
|
|
144
|
+
del: async (id) => kept.delete(id)
|
|
145
|
+
}
|
|
146
|
+
}
|