@cero-base/core 2.8.0 → 2.10.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/package.json +7 -7
- package/src/database/index.js +7 -0
- package/src/mailbox/inbox.js +31 -6
- package/src/mailbox/index.js +28 -9
- package/src/mailbox/post.js +12 -5
- package/src/network/index.js +7 -3
- package/types/mailbox/inbox.d.ts +15 -2
- package/types/mailbox/index.d.ts +33 -3
- package/types/mailbox/post.d.ts +8 -3
- package/types/network/index.d.ts +4 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.10.0",
|
|
4
4
|
"description": "cero p2p primitives — identity, storage, network, database, blobs, rpc, pairing.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -152,26 +152,26 @@
|
|
|
152
152
|
},
|
|
153
153
|
"dependencies": {
|
|
154
154
|
"@hyperswarm/secret-stream": "^6.9.2",
|
|
155
|
-
"autobee": "2.
|
|
155
|
+
"autobee": "2.13.0",
|
|
156
156
|
"b4a": "^1.9.0",
|
|
157
157
|
"bare-crypto": "^1.15.4",
|
|
158
158
|
"bare-fs": "^4.8.4",
|
|
159
159
|
"bare-path": "^3.1.2",
|
|
160
160
|
"bip39-mnemonic": "^2.5.0",
|
|
161
|
-
"blind-peering": "^2.12.
|
|
161
|
+
"blind-peering": "^2.12.2",
|
|
162
162
|
"blind-push": "^1.0.2",
|
|
163
163
|
"compact-encoding": "^3.5.2",
|
|
164
164
|
"corestore": "^7.16.1",
|
|
165
165
|
"framed-stream": "^1.0.1",
|
|
166
166
|
"hrpc": "^4.3.1",
|
|
167
167
|
"hyperblobs": "^2.13.0",
|
|
168
|
-
"hypercore": "^11.39.
|
|
168
|
+
"hypercore": "^11.39.6",
|
|
169
169
|
"hypercore-blob-server": "^1.15.1",
|
|
170
170
|
"hypercore-crypto": "^3.7.0",
|
|
171
171
|
"hypercore-id-encoding": "^1.3.0",
|
|
172
|
-
"hypercore-storage": "^3.
|
|
173
|
-
"hyperdb": "^6.11.
|
|
174
|
-
"hyperdht": "^6.
|
|
172
|
+
"hypercore-storage": "^3.4.0",
|
|
173
|
+
"hyperdb": "^6.11.1",
|
|
174
|
+
"hyperdht": "^6.35.0",
|
|
175
175
|
"hyperdispatch": "^1.6.0",
|
|
176
176
|
"hyperschema": "^1.26.2",
|
|
177
177
|
"hyperswarm": "^4.17.2",
|
package/src/database/index.js
CHANGED
|
@@ -36,6 +36,10 @@ import { makeChanges } from './changes.js'
|
|
|
36
36
|
import { makeDispatcher, BESPOKE } from './dispatch.js'
|
|
37
37
|
import { keyPair } from '../mailbox/inbox.js'
|
|
38
38
|
|
|
39
|
+
// autobee remaps an older store once a recovery peer, a mirror, serves it; unset, it closes the store
|
|
40
|
+
const recovery = []
|
|
41
|
+
Hypercore.setRecoveryPeers(recovery)
|
|
42
|
+
|
|
39
43
|
/**
|
|
40
44
|
* @typedef {object} DatabaseOpts
|
|
41
45
|
* @property {import('corestore')} store Corestore (or compatible) used to materialize the autobee.
|
|
@@ -99,6 +103,9 @@ export class Database extends ReadyResource {
|
|
|
99
103
|
this.store = opts.store
|
|
100
104
|
this.identity = opts.identity
|
|
101
105
|
this.network = opts.network || null
|
|
106
|
+
for (const key of this.network?.mirrors || []) {
|
|
107
|
+
if (!recovery.some((k) => b4a.equals(k, key))) recovery.push(key)
|
|
108
|
+
}
|
|
102
109
|
this.spec = opts.spec
|
|
103
110
|
this.meta = opts.spec.meta || { ns: NAMESPACE, refs: {} }
|
|
104
111
|
this.ns = this.meta.ns || NAMESPACE
|
package/src/mailbox/inbox.js
CHANGED
|
@@ -3,6 +3,8 @@ import safetyCatch from 'safety-catch'
|
|
|
3
3
|
import crypto from 'hypercore-crypto'
|
|
4
4
|
import b4a from 'b4a'
|
|
5
5
|
|
|
6
|
+
import { CeroError } from '../lib/errors.js'
|
|
7
|
+
|
|
6
8
|
const NS = b4a.from('cero/mailbox')
|
|
7
9
|
const READ_TIMEOUT = 30000
|
|
8
10
|
|
|
@@ -16,17 +18,32 @@ export function keyPair(secret) {
|
|
|
16
18
|
return crypto.encryptionKeyPair(crypto.hash([NS, secret]))
|
|
17
19
|
}
|
|
18
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Erase the core a message travelled in. Corestore leaves a core that still has a session open,
|
|
23
|
+
* and that is an error here, not silence.
|
|
24
|
+
*
|
|
25
|
+
* @param {import('corestore')} store
|
|
26
|
+
* @param {Uint8Array} key
|
|
27
|
+
* @returns {Promise<void>}
|
|
28
|
+
*/
|
|
29
|
+
export async function erase(store, key) {
|
|
30
|
+
const [gone] = await store.purge(key)
|
|
31
|
+
if (!gone) throw CeroError.CONFLICT(`mailbox: ${b4a.toHex(key)} is still open, not erased`)
|
|
32
|
+
}
|
|
33
|
+
|
|
19
34
|
/**
|
|
20
35
|
* Receives at the address a secret owns, direct from the sender or from a mirror. A message
|
|
21
36
|
* that does not open is dropped; one that opens is kept in `box` until `onmessage` resolves.
|
|
37
|
+
* A core the inbox fetched is erased once dropped, or once handled with `keep` false; one this
|
|
38
|
+
* device held already is left alone.
|
|
22
39
|
*/
|
|
23
40
|
export class Inbox extends ReadyResource {
|
|
24
41
|
/**
|
|
25
42
|
* @param {import('../network/index.js').Network} network
|
|
26
43
|
* @param {Uint8Array} secret
|
|
27
|
-
* @param {{ box: import('./index.js').Box, onmessage: (message: Uint8Array) => unknown, onerror: (err: Error) => void }} opts
|
|
44
|
+
* @param {{ box: import('./index.js').Box, onmessage: (message: Uint8Array) => unknown, onerror: (err: Error) => void, keep?: boolean }} opts
|
|
28
45
|
*/
|
|
29
|
-
constructor(network, secret, { box, onmessage, onerror }) {
|
|
46
|
+
constructor(network, secret, { box, onmessage, onerror, keep = true }) {
|
|
30
47
|
super()
|
|
31
48
|
this.network = network
|
|
32
49
|
this.keyPair = keyPair(secret)
|
|
@@ -34,6 +51,7 @@ export class Inbox extends ReadyResource {
|
|
|
34
51
|
this.box = box
|
|
35
52
|
this.onmessage = onmessage
|
|
36
53
|
this.onerror = onerror
|
|
54
|
+
this.keep = keep
|
|
37
55
|
/** @private */
|
|
38
56
|
this._seen = new Set()
|
|
39
57
|
/** @private */
|
|
@@ -77,25 +95,32 @@ export class Inbox extends ReadyResource {
|
|
|
77
95
|
|
|
78
96
|
/** @private */
|
|
79
97
|
async _read(id, key) {
|
|
80
|
-
const
|
|
98
|
+
const { store } = this.network
|
|
99
|
+
// a core this device already holds is not the inbox's to erase, whatever is announced here
|
|
100
|
+
const fetched = !(await store.storage.hasCore(crypto.discoveryKey(key)))
|
|
101
|
+
const core = store.get({ key })
|
|
81
102
|
let message
|
|
82
103
|
try {
|
|
83
104
|
message = crypto.decrypt(await core.get(0, { timeout: READ_TIMEOUT }), this.keyPair)
|
|
84
105
|
} finally {
|
|
85
106
|
await core.close()
|
|
86
107
|
}
|
|
87
|
-
if (!message)
|
|
88
|
-
|
|
108
|
+
if (!message) {
|
|
109
|
+
if (fetched) await erase(store, key)
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
const mail = { id, address: this.address, message, fetched }
|
|
89
113
|
await this.box.put(mail)
|
|
90
114
|
this._handle(mail)
|
|
91
115
|
}
|
|
92
116
|
|
|
93
117
|
// dropped once handled; one that throws stays for the next time this address is received
|
|
94
118
|
/** @private */
|
|
95
|
-
async _handle({ id, message }) {
|
|
119
|
+
async _handle({ id, message, fetched }) {
|
|
96
120
|
try {
|
|
97
121
|
await this.onmessage(message)
|
|
98
122
|
await this.box.del(id)
|
|
123
|
+
if (!this.keep && fetched) await erase(this.network.store, b4a.from(id, 'hex'))
|
|
99
124
|
} catch (err) {
|
|
100
125
|
this.onerror(err)
|
|
101
126
|
}
|
package/src/mailbox/index.js
CHANGED
|
@@ -3,16 +3,19 @@ import safetyCatch from 'safety-catch'
|
|
|
3
3
|
import crypto from 'hypercore-crypto'
|
|
4
4
|
import b4a from 'b4a'
|
|
5
5
|
|
|
6
|
-
import { Inbox, keyPair } from './inbox.js'
|
|
6
|
+
import { Inbox, keyPair, erase } from './inbox.js'
|
|
7
7
|
import { Post } from './post.js'
|
|
8
8
|
import { CeroError } from '../lib/errors.js'
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* @typedef {object} Mail
|
|
12
12
|
* @property {string} id Unique per message, hex.
|
|
13
|
+
* @property {string} [name] The core it travels in, so a restart sends it on the same core.
|
|
13
14
|
* @property {Uint8Array} address Where it goes, or where it arrived.
|
|
14
15
|
* @property {Uint8Array} message
|
|
15
16
|
* @property {Uint8Array[]} [mirrors] Where a sent message waits for an owner who is offline.
|
|
17
|
+
* @property {boolean} [keep] False erases the sender's copy once someone read it.
|
|
18
|
+
* @property {boolean} [fetched] The inbox fetched the core it arrived in, so it may erase it.
|
|
16
19
|
*
|
|
17
20
|
* @typedef {object} Box Where mail is kept; memory unless given a persistent one.
|
|
18
21
|
* @property {() => Promise<Mail[]>} list
|
|
@@ -61,6 +64,17 @@ export class Mailbox extends ReadyResource {
|
|
|
61
64
|
return keyPair(secret).publicKey
|
|
62
65
|
}
|
|
63
66
|
|
|
67
|
+
/**
|
|
68
|
+
* The key pair behind the address: seal to `publicKey` with `crypto.encrypt`, open with
|
|
69
|
+
* `crypto.decrypt(sealed, pair)`.
|
|
70
|
+
*
|
|
71
|
+
* @param {Uint8Array} secret
|
|
72
|
+
* @returns {{ publicKey: Uint8Array, secretKey: Uint8Array }}
|
|
73
|
+
*/
|
|
74
|
+
static keyPair(secret) {
|
|
75
|
+
return keyPair(secret)
|
|
76
|
+
}
|
|
77
|
+
|
|
64
78
|
/** @private */
|
|
65
79
|
async _open() {
|
|
66
80
|
await this.network.ready()
|
|
@@ -81,13 +95,15 @@ export class Mailbox extends ReadyResource {
|
|
|
81
95
|
*
|
|
82
96
|
* @param {Uint8Array} secret
|
|
83
97
|
* @param {(message: Uint8Array) => unknown} onmessage
|
|
98
|
+
* @param {{ keep?: boolean }} [opts] False erases each message from this device once handled.
|
|
84
99
|
* @returns {{ close: () => Promise<void> }}
|
|
85
100
|
*/
|
|
86
|
-
receive(secret, onmessage) {
|
|
101
|
+
receive(secret, onmessage, { keep = true } = {}) {
|
|
87
102
|
const inbox = new Inbox(this.network, secret, {
|
|
88
103
|
box: this.inbox,
|
|
89
104
|
onmessage,
|
|
90
|
-
onerror: this.onerror
|
|
105
|
+
onerror: this.onerror,
|
|
106
|
+
keep
|
|
91
107
|
})
|
|
92
108
|
this._track(inbox)
|
|
93
109
|
inbox.ready().catch(this.onerror)
|
|
@@ -100,21 +116,24 @@ export class Mailbox extends ReadyResource {
|
|
|
100
116
|
*
|
|
101
117
|
* @param {Uint8Array} address
|
|
102
118
|
* @param {Uint8Array} message
|
|
103
|
-
* @param {{ mirrors?: Uint8Array[] }} [opts]
|
|
119
|
+
* @param {{ mirrors?: Uint8Array[], keep?: boolean }} [opts] `mirrors`: where it waits for an owner who is offline, the network's by default. `keep`: false erases it from this device once someone read it.
|
|
104
120
|
* @returns {Promise<void>}
|
|
105
121
|
*/
|
|
106
|
-
async send(address, message, { mirrors = this.network.mirrors } = {}) {
|
|
122
|
+
async send(address, message, { mirrors = this.network.mirrors, keep = true } = {}) {
|
|
107
123
|
if (!this.opened) await this.ready()
|
|
108
|
-
const
|
|
124
|
+
const name = b4a.toHex(crypto.randomBytes(32))
|
|
125
|
+
const mail = { id: b4a.toHex(crypto.randomBytes(16)), name, address, message, mirrors, keep }
|
|
109
126
|
await this.outbox.put(mail)
|
|
110
127
|
this._deliver(mail)
|
|
111
128
|
}
|
|
112
129
|
|
|
113
130
|
/** @private */
|
|
114
|
-
_deliver({ id, address, message, mirrors }) {
|
|
115
|
-
const post = new Post(this.network, address, message, { mirrors })
|
|
131
|
+
_deliver({ id, name, address, message, mirrors, keep = true }) {
|
|
132
|
+
const post = new Post(this.network, address, message, { mirrors, name })
|
|
116
133
|
this._track(post)
|
|
117
|
-
deliver(post, () => this.outbox.del(id))
|
|
134
|
+
deliver(post, () => this.outbox.del(id))
|
|
135
|
+
.then(() => keep || erase(this.network.store, post.key).catch(this.onerror))
|
|
136
|
+
.catch(safetyCatch)
|
|
118
137
|
}
|
|
119
138
|
|
|
120
139
|
/** @private */
|
package/src/mailbox/post.js
CHANGED
|
@@ -7,21 +7,23 @@ import b4a from 'b4a'
|
|
|
7
7
|
* One sealed message on its way to an address: written to a fresh core, announced to peers
|
|
8
8
|
* receiving at the address, and deposited on `mirrors` for an owner who is offline.
|
|
9
9
|
* `delivered` resolves on the first read, by the owner or by a mirror holding it for them.
|
|
10
|
-
* Given a `core` instead, it announces that core as it is
|
|
10
|
+
* Given a `core` instead, it announces that core as it is; given a `name`, the core is the
|
|
11
|
+
* store's core of that name, and a message it already holds is not written twice.
|
|
11
12
|
*/
|
|
12
13
|
export class Post extends ReadyResource {
|
|
13
14
|
/**
|
|
14
15
|
* @param {import('../network/index.js').Network} network
|
|
15
16
|
* @param {Uint8Array} address
|
|
16
17
|
* @param {Uint8Array | null} message
|
|
17
|
-
* @param {{ mirrors?: Uint8Array[], core?: import('hypercore') }} [opts]
|
|
18
|
+
* @param {{ mirrors?: Uint8Array[], core?: import('hypercore'), name?: string }} [opts]
|
|
18
19
|
*/
|
|
19
|
-
constructor(network, address, message, { mirrors = [], core = null } = {}) {
|
|
20
|
+
constructor(network, address, message, { mirrors = [], core = null, name } = {}) {
|
|
20
21
|
super()
|
|
21
22
|
this.network = network
|
|
22
23
|
this.address = address
|
|
23
24
|
this.message = message
|
|
24
25
|
this.mirrors = mirrors
|
|
26
|
+
this.name = name || b4a.toHex(crypto.randomBytes(32))
|
|
25
27
|
/** @type {Promise<void>} */
|
|
26
28
|
this.delivered = new Promise((resolve) => {
|
|
27
29
|
/** @private */
|
|
@@ -38,11 +40,11 @@ export class Post extends ReadyResource {
|
|
|
38
40
|
/** @private */
|
|
39
41
|
async _open() {
|
|
40
42
|
const { network, address } = this
|
|
41
|
-
const core = this._core || network.store.get({ name:
|
|
43
|
+
const core = this._core || network.store.get({ name: this.name })
|
|
42
44
|
this._core = core
|
|
43
45
|
await core.ready()
|
|
44
46
|
core.once('upload', () => this._ondelivered())
|
|
45
|
-
if (this.message) await core.append(crypto.encrypt(this.message, address))
|
|
47
|
+
if (this.message && core.length === 0) await core.append(crypto.encrypt(this.message, address))
|
|
46
48
|
|
|
47
49
|
const wakeup = [{ key: core.key, length: core.length }]
|
|
48
50
|
const session = network.wakeup.session(address, {
|
|
@@ -59,6 +61,11 @@ export class Post extends ReadyResource {
|
|
|
59
61
|
}
|
|
60
62
|
}
|
|
61
63
|
|
|
64
|
+
/** The key of the core it travels in, once opened. */
|
|
65
|
+
get key() {
|
|
66
|
+
return this._core?.key ?? null
|
|
67
|
+
}
|
|
68
|
+
|
|
62
69
|
/** @private */
|
|
63
70
|
async _close() {
|
|
64
71
|
this._session?.destroy()
|
package/src/network/index.js
CHANGED
|
@@ -19,6 +19,10 @@ import { Presence } from './presence.js'
|
|
|
19
19
|
const SILENT_TIMEOUT = 3000
|
|
20
20
|
const RELOOKUPS = [5000, 20000, 60000]
|
|
21
21
|
|
|
22
|
+
function toKey(k) {
|
|
23
|
+
return typeof k === 'string' ? decodeKey(k) : k
|
|
24
|
+
}
|
|
25
|
+
|
|
22
26
|
/** @type {(topic: Uint8Array, channel: string | null) => Uint8Array} */
|
|
23
27
|
export function channelTopic(topic, channel) {
|
|
24
28
|
return channel ? hash([topic, b4a.from(channel)]) : topic
|
|
@@ -28,7 +32,7 @@ export function channelTopic(topic, channel) {
|
|
|
28
32
|
* @typedef {object} NetworkOpts
|
|
29
33
|
* @property {Array<{ host: string, port: number }>} [bootstrap] Custom DHT bootstrap nodes.
|
|
30
34
|
* @property {(remotePublicKey: Uint8Array, payload: unknown) => boolean} [firewall] Incoming-connection filter.
|
|
31
|
-
* @property {Uint8Array
|
|
35
|
+
* @property {Array<string | Uint8Array>} [relayThrough] Blind relay public keys; a connection goes through one only when a direct one fails or this device sits behind a randomizing NAT.
|
|
32
36
|
* @property {number[]} [backoffs] Reconnect backoff tiers in ms; the default escalates to ~10min, far too slow for local nets.
|
|
33
37
|
* @property {string} [channel] Optional network-isolation label; only same-channel peers meet.
|
|
34
38
|
* @property {import('corestore')} store Corestore; every connection replicates it, so every core it holds rides every peer.
|
|
@@ -58,13 +62,13 @@ export class Network extends ReadyResource {
|
|
|
58
62
|
if (!store) throw CeroError.REQUIRED('store')
|
|
59
63
|
this.bootstrap = bootstrap || null
|
|
60
64
|
this.firewall = firewall || null
|
|
61
|
-
this.relayThrough = relayThrough || null
|
|
65
|
+
this.relayThrough = relayThrough?.map(toKey) || null
|
|
62
66
|
this.backoffs = backoffs || null
|
|
63
67
|
this.channel = channel || null
|
|
64
68
|
/** @type {import('corestore')} */
|
|
65
69
|
this.store = store
|
|
66
70
|
/** @type {Uint8Array[]} */
|
|
67
|
-
this.mirrors = (mirrors || []).map(
|
|
71
|
+
this.mirrors = (mirrors || []).map(toKey)
|
|
68
72
|
|
|
69
73
|
/** @private */
|
|
70
74
|
this._swarm = null
|
package/types/mailbox/inbox.d.ts
CHANGED
|
@@ -9,9 +9,20 @@ export declare function keyPair(secret: Uint8Array): {
|
|
|
9
9
|
publicKey: Uint8Array;
|
|
10
10
|
secretKey: Uint8Array;
|
|
11
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* Erase the core a message travelled in. Corestore leaves a core that still has a session open,
|
|
14
|
+
* and that is an error here, not silence.
|
|
15
|
+
*
|
|
16
|
+
* @param {import('corestore')} store
|
|
17
|
+
* @param {Uint8Array} key
|
|
18
|
+
* @returns {Promise<void>}
|
|
19
|
+
*/
|
|
20
|
+
export declare function erase(store: import('corestore'), key: Uint8Array): Promise<void>;
|
|
12
21
|
/**
|
|
13
22
|
* Receives at the address a secret owns, direct from the sender or from a mirror. A message
|
|
14
23
|
* that does not open is dropped; one that opens is kept in `box` until `onmessage` resolves.
|
|
24
|
+
* A core the inbox fetched is erased once dropped, or once handled with `keep` false; one this
|
|
25
|
+
* device held already is left alone.
|
|
15
26
|
*/
|
|
16
27
|
export declare class Inbox extends ReadyResource {
|
|
17
28
|
network: import("../index.js").Network;
|
|
@@ -23,6 +34,7 @@ export declare class Inbox extends ReadyResource {
|
|
|
23
34
|
box: import("./index.js").Box;
|
|
24
35
|
onmessage: (message: Uint8Array) => unknown;
|
|
25
36
|
onerror: (err: Error) => void;
|
|
37
|
+
keep: boolean;
|
|
26
38
|
/** @private */
|
|
27
39
|
_seen;
|
|
28
40
|
/** @private */
|
|
@@ -32,12 +44,13 @@ export declare class Inbox extends ReadyResource {
|
|
|
32
44
|
/**
|
|
33
45
|
* @param {import('../network/index.js').Network} network
|
|
34
46
|
* @param {Uint8Array} secret
|
|
35
|
-
* @param {{ box: import('./index.js').Box, onmessage: (message: Uint8Array) => unknown, onerror: (err: Error) => void }} opts
|
|
47
|
+
* @param {{ box: import('./index.js').Box, onmessage: (message: Uint8Array) => unknown, onerror: (err: Error) => void, keep?: boolean }} opts
|
|
36
48
|
*/
|
|
37
|
-
constructor(network: import('../network/index.js').Network, secret: Uint8Array, { box, onmessage, onerror }: {
|
|
49
|
+
constructor(network: import('../network/index.js').Network, secret: Uint8Array, { box, onmessage, onerror, keep }: {
|
|
38
50
|
box: import('./index.js').Box;
|
|
39
51
|
onmessage: (message: Uint8Array) => unknown;
|
|
40
52
|
onerror: (err: Error) => void;
|
|
53
|
+
keep?: boolean;
|
|
41
54
|
});
|
|
42
55
|
/** @private */
|
|
43
56
|
private _open;
|
package/types/mailbox/index.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ export type Mail = {
|
|
|
4
4
|
* Unique per message, hex.
|
|
5
5
|
*/
|
|
6
6
|
id: string;
|
|
7
|
+
/**
|
|
8
|
+
* The core it travels in, so a restart sends it on the same core.
|
|
9
|
+
*/
|
|
10
|
+
name?: string;
|
|
7
11
|
/**
|
|
8
12
|
* Where it goes, or where it arrived.
|
|
9
13
|
*/
|
|
@@ -13,6 +17,14 @@ export type Mail = {
|
|
|
13
17
|
* Where a sent message waits for an owner who is offline.
|
|
14
18
|
*/
|
|
15
19
|
mirrors?: Uint8Array[];
|
|
20
|
+
/**
|
|
21
|
+
* False erases the sender's copy once someone read it.
|
|
22
|
+
*/
|
|
23
|
+
keep?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* The inbox fetched the core it arrived in, so it may erase it.
|
|
26
|
+
*/
|
|
27
|
+
fetched?: boolean;
|
|
16
28
|
};
|
|
17
29
|
export type Box = {
|
|
18
30
|
list: () => Promise<Mail[]>;
|
|
@@ -36,9 +48,12 @@ export type MailboxOpts = {
|
|
|
36
48
|
/**
|
|
37
49
|
* @typedef {object} Mail
|
|
38
50
|
* @property {string} id Unique per message, hex.
|
|
51
|
+
* @property {string} [name] The core it travels in, so a restart sends it on the same core.
|
|
39
52
|
* @property {Uint8Array} address Where it goes, or where it arrived.
|
|
40
53
|
* @property {Uint8Array} message
|
|
41
54
|
* @property {Uint8Array[]} [mirrors] Where a sent message waits for an owner who is offline.
|
|
55
|
+
* @property {boolean} [keep] False erases the sender's copy once someone read it.
|
|
56
|
+
* @property {boolean} [fetched] The inbox fetched the core it arrived in, so it may erase it.
|
|
42
57
|
*
|
|
43
58
|
* @typedef {object} Box Where mail is kept; memory unless given a persistent one.
|
|
44
59
|
* @property {() => Promise<Mail[]>} list
|
|
@@ -76,6 +91,17 @@ export declare class Mailbox extends ReadyResource {
|
|
|
76
91
|
* @returns {Uint8Array}
|
|
77
92
|
*/
|
|
78
93
|
static getAddress(secret: Uint8Array): Uint8Array;
|
|
94
|
+
/**
|
|
95
|
+
* The key pair behind the address: seal to `publicKey` with `crypto.encrypt`, open with
|
|
96
|
+
* `crypto.decrypt(sealed, pair)`.
|
|
97
|
+
*
|
|
98
|
+
* @param {Uint8Array} secret
|
|
99
|
+
* @returns {{ publicKey: Uint8Array, secretKey: Uint8Array }}
|
|
100
|
+
*/
|
|
101
|
+
static keyPair(secret: Uint8Array): {
|
|
102
|
+
publicKey: Uint8Array;
|
|
103
|
+
secretKey: Uint8Array;
|
|
104
|
+
};
|
|
79
105
|
/** @private */
|
|
80
106
|
private _open;
|
|
81
107
|
/** @private */
|
|
@@ -87,9 +113,12 @@ export declare class Mailbox extends ReadyResource {
|
|
|
87
113
|
*
|
|
88
114
|
* @param {Uint8Array} secret
|
|
89
115
|
* @param {(message: Uint8Array) => unknown} onmessage
|
|
116
|
+
* @param {{ keep?: boolean }} [opts] False erases each message from this device once handled.
|
|
90
117
|
* @returns {{ close: () => Promise<void> }}
|
|
91
118
|
*/
|
|
92
|
-
receive(secret: Uint8Array, onmessage: (message: Uint8Array) => unknown
|
|
119
|
+
receive(secret: Uint8Array, onmessage: (message: Uint8Array) => unknown, { keep }?: {
|
|
120
|
+
keep?: boolean;
|
|
121
|
+
}): {
|
|
93
122
|
close: () => Promise<void>;
|
|
94
123
|
};
|
|
95
124
|
/**
|
|
@@ -98,11 +127,12 @@ export declare class Mailbox extends ReadyResource {
|
|
|
98
127
|
*
|
|
99
128
|
* @param {Uint8Array} address
|
|
100
129
|
* @param {Uint8Array} message
|
|
101
|
-
* @param {{ mirrors?: Uint8Array[] }} [opts]
|
|
130
|
+
* @param {{ mirrors?: Uint8Array[], keep?: boolean }} [opts] `mirrors`: where it waits for an owner who is offline, the network's by default. `keep`: false erases it from this device once someone read it.
|
|
102
131
|
* @returns {Promise<void>}
|
|
103
132
|
*/
|
|
104
|
-
send(address: Uint8Array, message: Uint8Array, { mirrors }?: {
|
|
133
|
+
send(address: Uint8Array, message: Uint8Array, { mirrors, keep }?: {
|
|
105
134
|
mirrors?: Uint8Array[];
|
|
135
|
+
keep?: boolean;
|
|
106
136
|
}): Promise<void>;
|
|
107
137
|
/** @private */
|
|
108
138
|
private _deliver;
|
package/types/mailbox/post.d.ts
CHANGED
|
@@ -3,13 +3,15 @@ import ReadyResource from 'ready-resource';
|
|
|
3
3
|
* One sealed message on its way to an address: written to a fresh core, announced to peers
|
|
4
4
|
* receiving at the address, and deposited on `mirrors` for an owner who is offline.
|
|
5
5
|
* `delivered` resolves on the first read, by the owner or by a mirror holding it for them.
|
|
6
|
-
* Given a `core` instead, it announces that core as it is
|
|
6
|
+
* Given a `core` instead, it announces that core as it is; given a `name`, the core is the
|
|
7
|
+
* store's core of that name, and a message it already holds is not written twice.
|
|
7
8
|
*/
|
|
8
9
|
export declare class Post extends ReadyResource {
|
|
9
10
|
network: import("../index.js").Network;
|
|
10
11
|
address: Uint8Array<ArrayBufferLike>;
|
|
11
12
|
message: Uint8Array<ArrayBufferLike>;
|
|
12
13
|
mirrors: Uint8Array<ArrayBufferLike>[];
|
|
14
|
+
name: string;
|
|
13
15
|
/** @type {Promise<void>} */
|
|
14
16
|
delivered: Promise<void>;
|
|
15
17
|
/** @private */
|
|
@@ -24,14 +26,17 @@ export declare class Post extends ReadyResource {
|
|
|
24
26
|
* @param {import('../network/index.js').Network} network
|
|
25
27
|
* @param {Uint8Array} address
|
|
26
28
|
* @param {Uint8Array | null} message
|
|
27
|
-
* @param {{ mirrors?: Uint8Array[], core?: import('hypercore') }} [opts]
|
|
29
|
+
* @param {{ mirrors?: Uint8Array[], core?: import('hypercore'), name?: string }} [opts]
|
|
28
30
|
*/
|
|
29
|
-
constructor(network: import('../network/index.js').Network, address: Uint8Array, message: Uint8Array | null, { mirrors, core }?: {
|
|
31
|
+
constructor(network: import('../network/index.js').Network, address: Uint8Array, message: Uint8Array | null, { mirrors, core, name }?: {
|
|
30
32
|
mirrors?: Uint8Array[];
|
|
31
33
|
core?: import('hypercore');
|
|
34
|
+
name?: string;
|
|
32
35
|
});
|
|
33
36
|
/** @private */
|
|
34
37
|
private _open;
|
|
38
|
+
/** The key of the core it travels in, once opened. */
|
|
39
|
+
get key(): any;
|
|
35
40
|
/** @private */
|
|
36
41
|
private _close;
|
|
37
42
|
}
|
package/types/network/index.d.ts
CHANGED
|
@@ -16,9 +16,9 @@ export type NetworkOpts = {
|
|
|
16
16
|
*/
|
|
17
17
|
firewall?: (remotePublicKey: Uint8Array, payload: unknown) => boolean;
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
19
|
+
* Blind relay public keys; a connection goes through one only when a direct one fails or this device sits behind a randomizing NAT.
|
|
20
20
|
*/
|
|
21
|
-
relayThrough?: Uint8Array
|
|
21
|
+
relayThrough?: Array<string | Uint8Array>;
|
|
22
22
|
/**
|
|
23
23
|
* Reconnect backoff tiers in ms; the default escalates to ~10min, far too slow for local nets.
|
|
24
24
|
*/
|
|
@@ -52,7 +52,7 @@ export type NetworkOpts = {
|
|
|
52
52
|
* @typedef {object} NetworkOpts
|
|
53
53
|
* @property {Array<{ host: string, port: number }>} [bootstrap] Custom DHT bootstrap nodes.
|
|
54
54
|
* @property {(remotePublicKey: Uint8Array, payload: unknown) => boolean} [firewall] Incoming-connection filter.
|
|
55
|
-
* @property {Uint8Array
|
|
55
|
+
* @property {Array<string | Uint8Array>} [relayThrough] Blind relay public keys; a connection goes through one only when a direct one fails or this device sits behind a randomizing NAT.
|
|
56
56
|
* @property {number[]} [backoffs] Reconnect backoff tiers in ms; the default escalates to ~10min, far too slow for local nets.
|
|
57
57
|
* @property {string} [channel] Optional network-isolation label; only same-channel peers meet.
|
|
58
58
|
* @property {import('corestore')} store Corestore; every connection replicates it, so every core it holds rides every peer.
|
|
@@ -70,7 +70,7 @@ export declare class Network extends ReadyResource {
|
|
|
70
70
|
port: number;
|
|
71
71
|
}[];
|
|
72
72
|
firewall: (remotePublicKey: Uint8Array, payload: unknown) => boolean;
|
|
73
|
-
relayThrough:
|
|
73
|
+
relayThrough: any[];
|
|
74
74
|
backoffs: number[];
|
|
75
75
|
channel: string;
|
|
76
76
|
/** @type {import('corestore')} */
|