@cero-base/core 2.2.1 → 2.3.1
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 +188 -84
- 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/types/lib/errors.d.ts
CHANGED
|
@@ -15,10 +15,10 @@ export declare class CeroError extends Error {
|
|
|
15
15
|
/**
|
|
16
16
|
* Type-guard for `err instanceof CeroError` that survives realm boundaries.
|
|
17
17
|
*
|
|
18
|
-
* @param {
|
|
18
|
+
* @param {unknown} err
|
|
19
19
|
* @returns {err is CeroError}
|
|
20
20
|
*/
|
|
21
|
-
static isCeroError(err:
|
|
21
|
+
static isCeroError(err: unknown): err is CeroError;
|
|
22
22
|
/**
|
|
23
23
|
* Missing required argument.
|
|
24
24
|
*
|
|
@@ -78,7 +78,7 @@ export declare class CeroError extends Error {
|
|
|
78
78
|
*
|
|
79
79
|
* @param {string} what
|
|
80
80
|
*/
|
|
81
|
-
static
|
|
81
|
+
static TIMEOUT(what: string): CeroError;
|
|
82
82
|
/**
|
|
83
83
|
* Feature is not yet implemented.
|
|
84
84
|
*
|
|
@@ -113,12 +113,6 @@ export declare class CeroError extends Error {
|
|
|
113
113
|
* @param {string} [msg]
|
|
114
114
|
*/
|
|
115
115
|
static REFUSED(rule: string, msg?: string): CeroError;
|
|
116
|
-
/**
|
|
117
|
-
* Pairing handshake did not complete in time.
|
|
118
|
-
*
|
|
119
|
-
* @param {string} [msg]
|
|
120
|
-
*/
|
|
121
|
-
static TIMEOUT(msg?: string): CeroError;
|
|
122
116
|
/**
|
|
123
117
|
* Underlying swarm/transport failure.
|
|
124
118
|
*
|
|
@@ -4,6 +4,32 @@ declare function encode(name: any, value: any, v?: number): any;
|
|
|
4
4
|
declare function decode(name: any, buffer: any, v?: number): any;
|
|
5
5
|
declare function getEnum(name: any): void;
|
|
6
6
|
declare function getEncoding(name: any): {
|
|
7
|
+
preencode(state: any, m: any): void;
|
|
8
|
+
encode(state: any, m: any): void;
|
|
9
|
+
decode(state: any): {
|
|
10
|
+
key: any;
|
|
11
|
+
length: any;
|
|
12
|
+
};
|
|
13
|
+
} | {
|
|
14
|
+
preencode(state: any, m: any): void;
|
|
15
|
+
encode(state: any, m: any): void;
|
|
16
|
+
decode(state: any): {
|
|
17
|
+
invite: any;
|
|
18
|
+
reply: any;
|
|
19
|
+
proof: any;
|
|
20
|
+
identity: any;
|
|
21
|
+
signature: any;
|
|
22
|
+
ts: any;
|
|
23
|
+
};
|
|
24
|
+
} | {
|
|
25
|
+
preencode(state: any, m: any): void;
|
|
26
|
+
encode(state: any, m: any): void;
|
|
27
|
+
decode(state: any): {
|
|
28
|
+
epoch: any;
|
|
29
|
+
stamp: any;
|
|
30
|
+
entropy: any;
|
|
31
|
+
};
|
|
32
|
+
} | {
|
|
7
33
|
preencode(state: any, m: any): void;
|
|
8
34
|
encode(state: any, m: any): void;
|
|
9
35
|
decode(state: any): {
|
|
@@ -11,7 +37,7 @@ declare function getEncoding(name: any): {
|
|
|
11
37
|
reason: any;
|
|
12
38
|
key: any;
|
|
13
39
|
encryptionKey: any;
|
|
14
|
-
|
|
40
|
+
epochs: any;
|
|
15
41
|
};
|
|
16
42
|
} | {
|
|
17
43
|
preencode(state: any, m: any): void;
|
|
@@ -19,14 +45,19 @@ declare function getEncoding(name: any): {
|
|
|
19
45
|
decode(state: any): {
|
|
20
46
|
data: any;
|
|
21
47
|
};
|
|
48
|
+
} | {
|
|
49
|
+
preencode(state: any, m: any): void;
|
|
50
|
+
encode(state: any, m: any): void;
|
|
51
|
+
decode(state: any): {
|
|
52
|
+
prev: any;
|
|
53
|
+
next: any;
|
|
54
|
+
};
|
|
22
55
|
} | {
|
|
23
56
|
preencode(state: any, m: any): void;
|
|
24
57
|
encode(state: any, m: any): void;
|
|
25
58
|
decode(state: any): {
|
|
26
59
|
id: any;
|
|
27
60
|
name: any;
|
|
28
|
-
role: any;
|
|
29
|
-
noAccept: boolean;
|
|
30
61
|
};
|
|
31
62
|
} | {
|
|
32
63
|
preencode(state: any, m: any): void;
|
|
@@ -39,30 +70,38 @@ declare function getEncoding(name: any): {
|
|
|
39
70
|
byteLength: any;
|
|
40
71
|
type: any;
|
|
41
72
|
};
|
|
42
|
-
} | {
|
|
43
|
-
preencode(state: any, m: any): void;
|
|
44
|
-
encode(state: any, m: any): void;
|
|
45
|
-
decode(state: any): {
|
|
46
|
-
prev: any;
|
|
47
|
-
next: any;
|
|
48
|
-
};
|
|
49
73
|
};
|
|
50
74
|
declare function getStruct(name: any, v?: number): {
|
|
51
75
|
preencode(state: any, m: any): void;
|
|
52
76
|
encode(state: any, m: any): void;
|
|
53
77
|
decode(state: any): {
|
|
78
|
+
key: any;
|
|
79
|
+
length: any;
|
|
80
|
+
} | {
|
|
81
|
+
invite: any;
|
|
82
|
+
reply: any;
|
|
83
|
+
proof: any;
|
|
84
|
+
identity: any;
|
|
85
|
+
signature: any;
|
|
86
|
+
ts: any;
|
|
87
|
+
} | {
|
|
88
|
+
epoch: any;
|
|
89
|
+
stamp: any;
|
|
90
|
+
entropy: any;
|
|
91
|
+
} | {
|
|
54
92
|
status: any;
|
|
55
93
|
reason: any;
|
|
56
94
|
key: any;
|
|
57
95
|
encryptionKey: any;
|
|
58
|
-
|
|
96
|
+
epochs: any;
|
|
59
97
|
} | {
|
|
60
98
|
data: any;
|
|
99
|
+
} | {
|
|
100
|
+
prev: any;
|
|
101
|
+
next: any;
|
|
61
102
|
} | {
|
|
62
103
|
id: any;
|
|
63
104
|
name: any;
|
|
64
|
-
role: any;
|
|
65
|
-
noAccept: boolean;
|
|
66
105
|
} | {
|
|
67
106
|
coreKey: any;
|
|
68
107
|
blockOffset: any;
|
|
@@ -70,9 +109,6 @@ declare function getStruct(name: any, v?: number): {
|
|
|
70
109
|
byteOffset: any;
|
|
71
110
|
byteLength: any;
|
|
72
111
|
type: any;
|
|
73
|
-
} | {
|
|
74
|
-
prev: any;
|
|
75
|
-
next: any;
|
|
76
112
|
};
|
|
77
113
|
};
|
|
78
114
|
declare const resolveStruct: typeof getStruct;
|
package/types/lib/utils.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export { WRITE, INVITE, ASSIGN, REMOVE } from './constants.js';
|
|
|
3
3
|
export declare function admission(dbKey: Uint8Array<ArrayBufferLike>, writer: Uint8Array<ArrayBufferLike>, appender: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>;
|
|
4
4
|
/** @type {(dbKey: Uint8Array, writer: Uint8Array) => Uint8Array} */
|
|
5
5
|
export declare function ownership(dbKey: Uint8Array<ArrayBufferLike>, writer: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>;
|
|
6
|
+
/** @type {(dbKey: Uint8Array, invite: Uint8Array, writer: Uint8Array, reply: Uint8Array) => Uint8Array} */
|
|
7
|
+
export declare function joining(dbKey: Uint8Array<ArrayBufferLike>, invite: Uint8Array<ArrayBufferLike>, writer: Uint8Array<ArrayBufferLike>, reply: Uint8Array<ArrayBufferLike>): Uint8Array<ArrayBufferLike>;
|
|
6
8
|
/**
|
|
7
9
|
* Whether `role` is granted `perm` under the default policy.
|
|
8
10
|
*
|
|
@@ -11,9 +13,12 @@ export declare function ownership(dbKey: Uint8Array<ArrayBufferLike>, writer: Ui
|
|
|
11
13
|
* @returns {boolean}
|
|
12
14
|
*/
|
|
13
15
|
export declare function can(role: string, perm: string): boolean;
|
|
14
|
-
|
|
15
|
-
export declare function
|
|
16
|
-
|
|
16
|
+
/** @type {(role: string) => boolean} */
|
|
17
|
+
export declare function isRank(role: string): boolean;
|
|
18
|
+
/** @type {(a: string, b: string) => boolean} */
|
|
19
|
+
export declare function grants(a: string, b: string): boolean;
|
|
20
|
+
/** @type {(a: string, b: string) => boolean} */
|
|
21
|
+
export declare function outranks(a: string, b: string): boolean;
|
|
17
22
|
/**
|
|
18
23
|
* Stream-of-snapshots primitive. Couples a `get` (returns the latest value) with a `watch`
|
|
19
24
|
* (re-fires on change) and emits the newest snapshot every time `watch` ticks.
|
|
@@ -28,16 +33,18 @@ export declare function subscribe<T>({ get, watch }: {
|
|
|
28
33
|
get: () => Promise<T> | T;
|
|
29
34
|
watch: (fn: () => void) => (() => void) | void;
|
|
30
35
|
}): import('streamx').Readable<T>;
|
|
31
|
-
|
|
36
|
+
/** @param {Record<string, unknown>} row @param {string} term @param {string[]} [fields] @returns {boolean} */
|
|
37
|
+
export declare function searchHit(row: Record<string, unknown>, term: string, fields?: string[]): boolean;
|
|
32
38
|
/**
|
|
33
39
|
* The in-memory query grammar over rows: equality on any non-reserved field,
|
|
34
40
|
* id ranges, `search`, then `reverse` and `limit`.
|
|
35
41
|
*
|
|
36
|
-
* @
|
|
37
|
-
* @param {
|
|
38
|
-
* @
|
|
42
|
+
* @template {Record<string, unknown>} T
|
|
43
|
+
* @param {T[]} rows
|
|
44
|
+
* @param {Record<string, unknown>} [query]
|
|
45
|
+
* @returns {T[]}
|
|
39
46
|
*/
|
|
40
|
-
export declare function filter(rows:
|
|
47
|
+
export declare function filter<T extends Record<string, unknown>>(rows: T[], query?: Record<string, unknown>): T[];
|
|
41
48
|
/**
|
|
42
49
|
* Run `cb` when `signal` aborts — or immediately if it already has.
|
|
43
50
|
*
|
|
@@ -46,3 +53,28 @@ export declare function filter(rows: any[], query?: Record<string, any>): any[];
|
|
|
46
53
|
* @returns {(() => void) | undefined}
|
|
47
54
|
*/
|
|
48
55
|
export declare function onAbort(signal: AbortSignal | undefined, cb: () => void): (() => void) | undefined;
|
|
56
|
+
/** @type {<T>(row: T, createdAt?: number | null, ts?: number) => T & { createdAt: number, updatedAt: number }} */
|
|
57
|
+
export declare function stamp<T>(row: T, createdAt: number, ts?: number): T & {
|
|
58
|
+
createdAt: number;
|
|
59
|
+
updatedAt: number;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Refuse a field the ref does not declare: the encoder would drop it and the returned row would lie.
|
|
63
|
+
*
|
|
64
|
+
* @param {string} name
|
|
65
|
+
* @param {{ fields?: string[] } | undefined} ref
|
|
66
|
+
* @param {Record<string, unknown> | null | undefined} row
|
|
67
|
+
*/
|
|
68
|
+
export declare function checkFields(name: string, ref: {
|
|
69
|
+
fields?: string[];
|
|
70
|
+
} | undefined, row: Record<string, unknown> | null | undefined): void;
|
|
71
|
+
/**
|
|
72
|
+
* Refuse a whole row that lacks a required field: the encoder would fail on it with no code.
|
|
73
|
+
*
|
|
74
|
+
* @param {string} name
|
|
75
|
+
* @param {{ required?: Record<string, string> } | undefined} ref
|
|
76
|
+
* @param {Record<string, unknown>} row
|
|
77
|
+
*/
|
|
78
|
+
export declare function checkRequired(name: string, ref: {
|
|
79
|
+
required?: Record<string, string>;
|
|
80
|
+
} | undefined, row: Record<string, unknown>): void;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import ReadyResource from 'ready-resource';
|
|
2
|
+
/**
|
|
3
|
+
* The keypair behind the address a secret owns.
|
|
4
|
+
*
|
|
5
|
+
* @param {Uint8Array} secret
|
|
6
|
+
* @returns {{ publicKey: Uint8Array, secretKey: Uint8Array }}
|
|
7
|
+
*/
|
|
8
|
+
export declare function keyPair(secret: Uint8Array): {
|
|
9
|
+
publicKey: Uint8Array;
|
|
10
|
+
secretKey: Uint8Array;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Receives at the address a secret owns, direct from the sender or from a mirror. A message
|
|
14
|
+
* that does not open is dropped; one that opens is kept in `box` until `onmessage` resolves.
|
|
15
|
+
*/
|
|
16
|
+
export declare class Inbox extends ReadyResource {
|
|
17
|
+
network: import("../index.js").Network;
|
|
18
|
+
keyPair: {
|
|
19
|
+
publicKey: Uint8Array;
|
|
20
|
+
secretKey: Uint8Array;
|
|
21
|
+
};
|
|
22
|
+
address: Uint8Array<ArrayBufferLike>;
|
|
23
|
+
box: import("./index.js").Box;
|
|
24
|
+
onmessage: (message: Uint8Array) => unknown;
|
|
25
|
+
onerror: (err: Error) => void;
|
|
26
|
+
/** @private */
|
|
27
|
+
_seen;
|
|
28
|
+
/** @private */
|
|
29
|
+
_session;
|
|
30
|
+
/** @private */
|
|
31
|
+
_discovery;
|
|
32
|
+
/**
|
|
33
|
+
* @param {import('../network/index.js').Network} network
|
|
34
|
+
* @param {Uint8Array} secret
|
|
35
|
+
* @param {{ box: import('./index.js').Box, onmessage: (message: Uint8Array) => unknown, onerror: (err: Error) => void }} opts
|
|
36
|
+
*/
|
|
37
|
+
constructor(network: import('../network/index.js').Network, secret: Uint8Array, { box, onmessage, onerror }: {
|
|
38
|
+
box: import('./index.js').Box;
|
|
39
|
+
onmessage: (message: Uint8Array) => unknown;
|
|
40
|
+
onerror: (err: Error) => void;
|
|
41
|
+
});
|
|
42
|
+
/** @private */
|
|
43
|
+
private _open;
|
|
44
|
+
/** @private */
|
|
45
|
+
private _close;
|
|
46
|
+
/** @private */
|
|
47
|
+
private _onannounce;
|
|
48
|
+
/** @private */
|
|
49
|
+
private _read;
|
|
50
|
+
/** @private */
|
|
51
|
+
private _handle;
|
|
52
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import ReadyResource from 'ready-resource';
|
|
2
|
+
export type Mail = {
|
|
3
|
+
/**
|
|
4
|
+
* Unique per message, hex.
|
|
5
|
+
*/
|
|
6
|
+
id: string;
|
|
7
|
+
/**
|
|
8
|
+
* Where it goes, or where it arrived.
|
|
9
|
+
*/
|
|
10
|
+
address: Uint8Array;
|
|
11
|
+
message: Uint8Array;
|
|
12
|
+
/**
|
|
13
|
+
* Where a sent message waits for an owner who is offline.
|
|
14
|
+
*/
|
|
15
|
+
mirrors?: Uint8Array[];
|
|
16
|
+
};
|
|
17
|
+
export type Box = {
|
|
18
|
+
list: () => Promise<Mail[]>;
|
|
19
|
+
put: (mail: Mail) => Promise<unknown>;
|
|
20
|
+
del: (id: string) => Promise<unknown>;
|
|
21
|
+
};
|
|
22
|
+
export type MailboxOpts = {
|
|
23
|
+
/**
|
|
24
|
+
* Keeps a received message until it was handled.
|
|
25
|
+
*/
|
|
26
|
+
inbox?: Box;
|
|
27
|
+
/**
|
|
28
|
+
* Keeps a sent message until someone read it.
|
|
29
|
+
*/
|
|
30
|
+
outbox?: Box;
|
|
31
|
+
/**
|
|
32
|
+
* Called when a message fails to be handled or sent.
|
|
33
|
+
*/
|
|
34
|
+
onerror?: (err: Error) => void;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* @typedef {object} Mail
|
|
38
|
+
* @property {string} id Unique per message, hex.
|
|
39
|
+
* @property {Uint8Array} address Where it goes, or where it arrived.
|
|
40
|
+
* @property {Uint8Array} message
|
|
41
|
+
* @property {Uint8Array[]} [mirrors] Where a sent message waits for an owner who is offline.
|
|
42
|
+
*
|
|
43
|
+
* @typedef {object} Box Where mail is kept; memory unless given a persistent one.
|
|
44
|
+
* @property {() => Promise<Mail[]>} list
|
|
45
|
+
* @property {(mail: Mail) => Promise<unknown>} put
|
|
46
|
+
* @property {(id: string) => Promise<unknown>} del
|
|
47
|
+
*
|
|
48
|
+
* @typedef {object} MailboxOpts
|
|
49
|
+
* @property {Box} [inbox] Keeps a received message until it was handled.
|
|
50
|
+
* @property {Box} [outbox] Keeps a sent message until someone read it.
|
|
51
|
+
* @property {(err: Error) => void} [onerror] Called when a message fails to be handled or sent.
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* A device's mailbox. It receives at the addresses it holds the secret of, and sends to any
|
|
55
|
+
* address. Both ends keep their mail until it is done with: the outbox until someone read it,
|
|
56
|
+
* the inbox until it was handled. Give it persistent boxes and that survives a restart: it
|
|
57
|
+
* sends again what was not read and hands over again what was not handled, so a message may
|
|
58
|
+
* arrive twice, never not at all.
|
|
59
|
+
*/
|
|
60
|
+
export declare class Mailbox extends ReadyResource {
|
|
61
|
+
network: import("../index.js").Network;
|
|
62
|
+
inbox: Box;
|
|
63
|
+
outbox: Box;
|
|
64
|
+
onerror: (err: Error) => void;
|
|
65
|
+
/** @private */
|
|
66
|
+
_resources;
|
|
67
|
+
/**
|
|
68
|
+
* @param {import('../network/index.js').Network} network Needs a store: messages travel as cores in it.
|
|
69
|
+
* @param {MailboxOpts} [opts]
|
|
70
|
+
*/
|
|
71
|
+
constructor(network: import('../network/index.js').Network, { inbox, outbox, onerror }?: MailboxOpts);
|
|
72
|
+
/**
|
|
73
|
+
* The address a secret owns: share it, anyone can send to it, only the secret opens.
|
|
74
|
+
*
|
|
75
|
+
* @param {Uint8Array} secret
|
|
76
|
+
* @returns {Uint8Array}
|
|
77
|
+
*/
|
|
78
|
+
static getAddress(secret: Uint8Array): Uint8Array;
|
|
79
|
+
/** @private */
|
|
80
|
+
private _open;
|
|
81
|
+
/** @private */
|
|
82
|
+
private _close;
|
|
83
|
+
/**
|
|
84
|
+
* Receive the messages sent to the address `secret` owns, until the returned inbox closes.
|
|
85
|
+
* A message stays in the inbox until `onmessage` resolves; one it did not finish is handed
|
|
86
|
+
* over again the next time this address is received.
|
|
87
|
+
*
|
|
88
|
+
* @param {Uint8Array} secret
|
|
89
|
+
* @param {(message: Uint8Array) => unknown} onmessage
|
|
90
|
+
* @returns {{ close: () => Promise<void> }}
|
|
91
|
+
*/
|
|
92
|
+
receive(secret: Uint8Array, onmessage: (message: Uint8Array) => unknown): {
|
|
93
|
+
close: () => Promise<void>;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Send a message to an address. Resolves once the outbox keeps it; it is delivered in the
|
|
97
|
+
* background, and dropped from the outbox once someone read it.
|
|
98
|
+
*
|
|
99
|
+
* @param {Uint8Array} address
|
|
100
|
+
* @param {Uint8Array} message
|
|
101
|
+
* @param {{ mirrors?: Uint8Array[] }} [opts] Where it waits for an owner who is offline; the network's by default.
|
|
102
|
+
* @returns {Promise<void>}
|
|
103
|
+
*/
|
|
104
|
+
send(address: Uint8Array, message: Uint8Array, { mirrors }?: {
|
|
105
|
+
mirrors?: Uint8Array[];
|
|
106
|
+
}): Promise<void>;
|
|
107
|
+
/** @private */
|
|
108
|
+
private _deliver;
|
|
109
|
+
/** @private */
|
|
110
|
+
private _track;
|
|
111
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import ReadyResource from 'ready-resource';
|
|
2
|
+
/**
|
|
3
|
+
* One sealed message on its way to an address: written to a fresh core, announced to peers
|
|
4
|
+
* receiving at the address, and deposited on `mirrors` for an owner who is offline.
|
|
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.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Post extends ReadyResource {
|
|
9
|
+
network: import("../index.js").Network;
|
|
10
|
+
address: Uint8Array<ArrayBufferLike>;
|
|
11
|
+
message: Uint8Array<ArrayBufferLike>;
|
|
12
|
+
mirrors: Uint8Array<ArrayBufferLike>[];
|
|
13
|
+
/** @type {Promise<void>} */
|
|
14
|
+
delivered: Promise<void>;
|
|
15
|
+
/** @private */
|
|
16
|
+
_ondelivered;
|
|
17
|
+
/** @private */
|
|
18
|
+
_core;
|
|
19
|
+
/** @private */
|
|
20
|
+
_session;
|
|
21
|
+
/** @private */
|
|
22
|
+
_discovery;
|
|
23
|
+
/**
|
|
24
|
+
* @param {import('../network/index.js').Network} network
|
|
25
|
+
* @param {Uint8Array} address
|
|
26
|
+
* @param {Uint8Array | null} message
|
|
27
|
+
* @param {{ mirrors?: Uint8Array[], core?: import('hypercore') }} [opts]
|
|
28
|
+
*/
|
|
29
|
+
constructor(network: import('../network/index.js').Network, address: Uint8Array, message: Uint8Array | null, { mirrors, core }?: {
|
|
30
|
+
mirrors?: Uint8Array[];
|
|
31
|
+
core?: import('hypercore');
|
|
32
|
+
});
|
|
33
|
+
/** @private */
|
|
34
|
+
private _open;
|
|
35
|
+
/** @private */
|
|
36
|
+
private _close;
|
|
37
|
+
}
|
|
@@ -1,18 +1,32 @@
|
|
|
1
|
+
export type Session = {
|
|
2
|
+
refresh(opts?: object): Promise<void>;
|
|
3
|
+
flushed(): Promise<boolean>;
|
|
4
|
+
destroy(): Promise<void>;
|
|
5
|
+
};
|
|
1
6
|
/**
|
|
2
7
|
* Handle for a single topic membership on a {@link Network}. Wraps a hyperswarm
|
|
3
8
|
* PeerDiscovery session and lets it switch between active/passive announce/lookup.
|
|
4
9
|
*/
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {{ refresh(opts?: object): Promise<void>, flushed(): Promise<boolean>, destroy(): Promise<void> }} Session
|
|
12
|
+
* The hyperswarm session `swarm.join()` returns, as far as this file uses it.
|
|
13
|
+
*/
|
|
5
14
|
export declare class Discovery {
|
|
6
15
|
network: import("./index.js").Network;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
16
|
+
/** @type {Session} */
|
|
17
|
+
session: Session;
|
|
18
|
+
/** @private */
|
|
19
|
+
_mode;
|
|
20
|
+
/** @private */
|
|
21
|
+
_destroyed;
|
|
22
|
+
/** @private */
|
|
23
|
+
_timer;
|
|
10
24
|
/**
|
|
11
25
|
* @param {import('./index.js').Network} network Owning network.
|
|
12
|
-
* @param {
|
|
26
|
+
* @param {Session} session Hyperswarm PeerDiscovery session from `swarm.join()`.
|
|
13
27
|
* @param {'active' | 'passive'} mode
|
|
14
28
|
*/
|
|
15
|
-
constructor(network: import('./index.js').Network, session:
|
|
29
|
+
constructor(network: import('./index.js').Network, session: Session, mode: 'active' | 'passive');
|
|
16
30
|
/** @returns {'active' | 'passive'} */
|
|
17
31
|
get mode(): 'active' | 'passive';
|
|
18
32
|
/** @returns {boolean} */
|
|
@@ -35,6 +49,8 @@ export declare class Discovery {
|
|
|
35
49
|
* @returns {Promise<void>}
|
|
36
50
|
*/
|
|
37
51
|
flush(): Promise<void>;
|
|
52
|
+
/** @private */
|
|
53
|
+
private _requery;
|
|
38
54
|
/**
|
|
39
55
|
* Leave the topic and tear down the session. Idempotent.
|
|
40
56
|
*
|
package/types/network/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import ReadyResource from 'ready-resource';
|
|
2
2
|
import { Discovery } from './discovery.js';
|
|
3
3
|
import { Presence } from './presence.js';
|
|
4
|
-
|
|
4
|
+
/** @type {(topic: Uint8Array, channel: string | null) => Uint8Array} */
|
|
5
|
+
export declare function channelTopic(topic: Uint8Array<ArrayBufferLike>, channel: string): Uint8Array<ArrayBufferLike>;
|
|
5
6
|
export type NetworkOpts = {
|
|
6
7
|
/**
|
|
7
8
|
* Long-lived keypair used as the swarm identity.
|
|
@@ -17,7 +18,7 @@ export type NetworkOpts = {
|
|
|
17
18
|
/**
|
|
18
19
|
* Incoming-connection filter.
|
|
19
20
|
*/
|
|
20
|
-
firewall?: (remotePublicKey: Uint8Array, payload:
|
|
21
|
+
firewall?: (remotePublicKey: Uint8Array, payload: unknown) => boolean;
|
|
21
22
|
/**
|
|
22
23
|
* Relay public keys to tunnel through.
|
|
23
24
|
*/
|
|
@@ -33,7 +34,7 @@ export type NetworkOpts = {
|
|
|
33
34
|
/**
|
|
34
35
|
* Corestore; required for mirrors (blind peers replicate its cores).
|
|
35
36
|
*/
|
|
36
|
-
store?:
|
|
37
|
+
store?: import('corestore');
|
|
37
38
|
/**
|
|
38
39
|
* Blind-peer public keys; each attached room/blob core is mirrored through them for offline sync.
|
|
39
40
|
*/
|
|
@@ -41,7 +42,7 @@ export type NetworkOpts = {
|
|
|
41
42
|
/**
|
|
42
43
|
* Background-task error handler.
|
|
43
44
|
*/
|
|
44
|
-
onerror?: (err:
|
|
45
|
+
onerror?: (err: Error) => void;
|
|
45
46
|
/**
|
|
46
47
|
* Swarm budget for attached databases: how many search, how many only announce, and the idle ms before the rest leave.
|
|
47
48
|
*/
|
|
@@ -52,22 +53,22 @@ export type NetworkOpts = {
|
|
|
52
53
|
};
|
|
53
54
|
};
|
|
54
55
|
export type Replicable = {
|
|
55
|
-
replicate: (stream:
|
|
56
|
+
replicate: (stream: import('@hyperswarm/secret-stream')) => unknown;
|
|
56
57
|
};
|
|
57
58
|
/**
|
|
58
59
|
* @typedef {object} NetworkOpts
|
|
59
60
|
* @property {import('../identity/index.js').Identity} [identity] Long-lived keypair used as the swarm identity.
|
|
60
61
|
* @property {Array<{ host: string, port: number }>} [bootstrap] Custom DHT bootstrap nodes.
|
|
61
|
-
* @property {(remotePublicKey: Uint8Array, payload:
|
|
62
|
+
* @property {(remotePublicKey: Uint8Array, payload: unknown) => boolean} [firewall] Incoming-connection filter.
|
|
62
63
|
* @property {Uint8Array[]} [relayThrough] Relay public keys to tunnel through.
|
|
63
64
|
* @property {number[]} [backoffs] Reconnect backoff tiers in ms; the default escalates to ~10min, far too slow for local nets.
|
|
64
65
|
* @property {string} [channel] Optional network-isolation label; only same-channel peers meet.
|
|
65
|
-
* @property {
|
|
66
|
+
* @property {import('corestore')} [store] Corestore; required for mirrors (blind peers replicate its cores).
|
|
66
67
|
* @property {Array<string | Uint8Array>} [mirrors] Blind-peer public keys; each attached room/blob core is mirrored through them for offline sync.
|
|
67
|
-
* @property {(err:
|
|
68
|
+
* @property {(err: Error) => void} [onerror] Background-task error handler.
|
|
68
69
|
* @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.
|
|
69
70
|
*
|
|
70
|
-
* @typedef {{ replicate: (stream:
|
|
71
|
+
* @typedef {{ replicate: (stream: import('@hyperswarm/secret-stream')) => unknown }} Replicable
|
|
71
72
|
*/
|
|
72
73
|
/**
|
|
73
74
|
* Hyperswarm peer-discovery + replication multiplexer. Wraps a swarm and a
|
|
@@ -79,63 +80,72 @@ export declare class Network extends ReadyResource {
|
|
|
79
80
|
host: string;
|
|
80
81
|
port: number;
|
|
81
82
|
}[];
|
|
82
|
-
firewall: (remotePublicKey: Uint8Array, payload:
|
|
83
|
+
firewall: (remotePublicKey: Uint8Array, payload: unknown) => boolean;
|
|
83
84
|
relayThrough: Uint8Array<ArrayBufferLike>[];
|
|
84
85
|
backoffs: number[];
|
|
85
86
|
channel: string;
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
/** @type {import('corestore') | null} */
|
|
88
|
+
store: import('corestore') | null;
|
|
89
|
+
/** @type {Uint8Array[]} */
|
|
90
|
+
mirrors: Uint8Array[];
|
|
91
|
+
/** @private */
|
|
92
|
+
_swarm;
|
|
93
|
+
/** @type {import('protomux-wakeup')} */
|
|
94
|
+
wakeup: import('protomux-wakeup');
|
|
90
95
|
presence: Presence;
|
|
91
96
|
info: object;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
/** @private */
|
|
98
|
+
_peerInfo;
|
|
99
|
+
/** @private */
|
|
100
|
+
_infoSenders;
|
|
101
|
+
/** @private */
|
|
102
|
+
_replicateables;
|
|
103
|
+
/** @private */
|
|
104
|
+
_discoveries;
|
|
105
|
+
/** @private */
|
|
106
|
+
_injected;
|
|
107
|
+
/** @private */
|
|
108
|
+
_blindPeering;
|
|
109
|
+
/** @private */
|
|
110
|
+
_lost;
|
|
111
|
+
/** @private */
|
|
112
|
+
_relookup;
|
|
113
|
+
/** @private */
|
|
114
|
+
_onerror;
|
|
100
115
|
/** @param {NetworkOpts} [opts] */
|
|
101
116
|
constructor({ identity, bootstrap, firewall, relayThrough, backoffs, channel, store, mirrors, presence, onerror }?: NetworkOpts);
|
|
102
|
-
/** @returns {
|
|
103
|
-
get swarm():
|
|
104
|
-
/** @returns {Map<string,
|
|
105
|
-
get peers(): Map<string,
|
|
106
|
-
/** @returns {Set<
|
|
107
|
-
get connections(): Set<
|
|
117
|
+
/** @returns {import('hyperswarm') | null} The underlying hyperswarm, or null before ready / after close. */
|
|
118
|
+
get swarm(): import('hyperswarm') | null;
|
|
119
|
+
/** @returns {Map<string, object>} Known peers keyed by public-key string. */
|
|
120
|
+
get peers(): Map<string, object>;
|
|
121
|
+
/** @returns {Set<import('@hyperswarm/secret-stream')>} Live connection streams — swarm and injected. */
|
|
122
|
+
get connections(): Set<import('@hyperswarm/secret-stream')>;
|
|
108
123
|
/** @returns {boolean} */
|
|
109
124
|
get suspended(): boolean;
|
|
110
|
-
|
|
111
|
-
|
|
125
|
+
/** @private */
|
|
126
|
+
private _open;
|
|
127
|
+
/** @private */
|
|
128
|
+
private _close;
|
|
112
129
|
/**
|
|
113
130
|
* Feed an externally-established connection — a Bluetooth L2CAP channel, a serial link, an
|
|
114
131
|
* in-process pair, any duplex — into the network.
|
|
115
132
|
*
|
|
116
|
-
* @param {
|
|
133
|
+
* @param {import('streamx').Duplex} stream Duplex transport, or a ready NoiseSecretStream.
|
|
117
134
|
* @param {{ isInitiator?: boolean }} [opts] Which side initiates the noise handshake (raw duplexes only).
|
|
118
|
-
* @returns {
|
|
135
|
+
* @returns {import('@hyperswarm/secret-stream')} The encrypted connection stream.
|
|
119
136
|
*/
|
|
120
|
-
inject(stream:
|
|
137
|
+
inject(stream: import('streamx').Duplex, { isInitiator }?: {
|
|
121
138
|
isInitiator?: boolean;
|
|
122
|
-
}):
|
|
139
|
+
}): import('@hyperswarm/secret-stream');
|
|
123
140
|
/**
|
|
124
|
-
*
|
|
141
|
+
* The blind-peering client, built on first use: a mailbox post names mirrors this network
|
|
142
|
+
* may not have been given.
|
|
125
143
|
*
|
|
126
|
-
* @returns {
|
|
144
|
+
* @returns {import('blind-peering')}
|
|
127
145
|
*/
|
|
128
|
-
|
|
146
|
+
peering(): import('blind-peering');
|
|
129
147
|
/**
|
|
130
|
-
*
|
|
131
|
-
* refs that existed when a connection arrived — swarm peers meet again over topic joins,
|
|
132
|
-
* injected links (Bluetooth,.
|
|
133
|
-
*
|
|
134
|
-
* @returns {Promise<void>}
|
|
135
|
-
*/
|
|
136
|
-
refreshInjected(): Promise<void>;
|
|
137
|
-
/**
|
|
138
|
-
* Declare this peer's self-reported info ({ name, ... }).
|
|
148
|
+
* Declare this peer's self-reported info ({ name, ... }) to the peers of injected streams.
|
|
139
149
|
*
|
|
140
150
|
* @param {object | null} info
|
|
141
151
|
*/
|
|
@@ -186,7 +196,8 @@ export declare class Network extends ReadyResource {
|
|
|
186
196
|
* @returns {void}
|
|
187
197
|
*/
|
|
188
198
|
attach(core: Replicable): void;
|
|
189
|
-
|
|
199
|
+
/** @private */
|
|
200
|
+
private _mirror;
|
|
190
201
|
/**
|
|
191
202
|
* Unregister a previously attached resource. New connections will no
|
|
192
203
|
* longer replicate it (existing replication streams continue).
|
|
@@ -203,5 +214,10 @@ export declare class Network extends ReadyResource {
|
|
|
203
214
|
* @returns {void}
|
|
204
215
|
*/
|
|
205
216
|
replicate(target: Replicable): void;
|
|
206
|
-
|
|
217
|
+
/** @private */
|
|
218
|
+
private _lose;
|
|
219
|
+
/** @private */
|
|
220
|
+
private _relook;
|
|
221
|
+
/** @private */
|
|
222
|
+
private _attachInfo;
|
|
207
223
|
}
|