@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.
Files changed (55) hide show
  1. package/README.md +32 -212
  2. package/package.json +18 -9
  3. package/src/blobs/index.js +9 -0
  4. package/src/blobs/server.js +2 -0
  5. package/src/database/changes.js +1 -1
  6. package/src/database/dispatch.js +341 -209
  7. package/src/database/encryption.js +83 -33
  8. package/src/database/index.js +184 -83
  9. package/src/database/rotation.js +43 -30
  10. package/src/identity/index.js +20 -78
  11. package/src/index.js +3 -10
  12. package/src/lib/constants.js +2 -0
  13. package/src/lib/errors.js +3 -11
  14. package/src/lib/spec/index.js +136 -132
  15. package/src/lib/spec/schema.json +84 -99
  16. package/src/lib/utils.js +57 -3
  17. package/src/mailbox/inbox.js +103 -0
  18. package/src/mailbox/index.js +146 -0
  19. package/src/mailbox/post.js +69 -0
  20. package/src/network/discovery.js +26 -1
  21. package/src/network/index.js +105 -64
  22. package/src/network/presence.js +6 -2
  23. package/src/pairing/index.js +330 -254
  24. package/src/pairing/invite.js +83 -144
  25. package/src/pairing/request.js +54 -56
  26. package/src/rpc/index.js +18 -93
  27. package/src/rpc/peer.js +4 -1
  28. package/src/storage/index.js +78 -39
  29. package/types/blobs/index.d.ts +22 -9
  30. package/types/blobs/server.d.ts +4 -2
  31. package/types/database/changes.d.ts +2 -2
  32. package/types/database/dispatch.d.ts +19 -13
  33. package/types/database/encryption.d.ts +67 -29
  34. package/types/database/index.d.ts +144 -119
  35. package/types/database/rotation.d.ts +32 -13
  36. package/types/identity/index.d.ts +6 -47
  37. package/types/index.d.ts +3 -16
  38. package/types/lib/constants.d.ts +1 -0
  39. package/types/lib/errors.d.ts +3 -9
  40. package/types/lib/spec/index.d.ts +52 -16
  41. package/types/lib/utils.d.ts +40 -8
  42. package/types/mailbox/inbox.d.ts +52 -0
  43. package/types/mailbox/index.d.ts +111 -0
  44. package/types/mailbox/post.d.ts +37 -0
  45. package/types/network/discovery.d.ts +21 -5
  46. package/types/network/index.d.ts +64 -48
  47. package/types/network/presence.d.ts +17 -4
  48. package/types/pairing/index.d.ts +143 -134
  49. package/types/pairing/invite.d.ts +70 -114
  50. package/types/pairing/request.d.ts +58 -98
  51. package/types/rpc/index.d.ts +26 -44
  52. package/types/rpc/peer.d.ts +10 -7
  53. package/types/storage/index.d.ts +64 -54
  54. package/src/pairing/candidate.js +0 -123
  55. package/types/pairing/candidate.d.ts +0 -36
@@ -1,133 +1,93 @@
1
1
  export declare const STATUS_ACCEPTED = 0;
2
2
  export declare const STATUS_DENIED = 1;
3
- export declare const Response: {
4
- preencode(state: any, m: any): void;
5
- encode(state: any, m: any): void;
6
- decode(state: any): {
7
- status: any;
8
- reason: any;
9
- key: any;
10
- encryptionKey: any;
11
- extra: any;
12
- };
13
- } | {
14
- preencode(state: any, m: any): void;
15
- encode(state: any, m: any): void;
16
- decode(state: any): {
17
- data: any;
18
- };
19
- } | {
20
- preencode(state: any, m: any): void;
21
- encode(state: any, m: any): void;
22
- decode(state: any): {
23
- id: any;
24
- name: any;
25
- role: any;
26
- noAccept: boolean;
27
- };
28
- } | {
29
- preencode(state: any, m: any): void;
30
- encode(state: any, m: any): void;
31
- decode(state: any): {
32
- coreKey: any;
33
- blockOffset: any;
34
- blockLength: any;
35
- byteOffset: any;
36
- byteLength: any;
37
- type: any;
38
- };
39
- } | {
40
- preencode(state: any, m: any): void;
41
- encode(state: any, m: any): void;
42
- decode(state: any): {
43
- prev: any;
44
- next: any;
45
- };
46
- };
47
- export type ConfirmOpts = {
48
- /**
49
- * 32-byte resource key delivered to the joiner; required at runtime.
50
- */
51
- key?: Uint8Array;
52
- /**
53
- * Optional 32-byte symmetric key.
54
- */
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;
55
8
  encryptionKey?: Uint8Array | null;
56
- /**
57
- * Extra opaque bytes piggybacked on the response.
58
- */
59
- additional?: Uint8Array | null;
60
- };
9
+ epochs?: Array<{
10
+ epoch: number;
11
+ stamp: number;
12
+ entropy: Uint8Array;
13
+ }> | null;
14
+ }>;
61
15
  export type RequestOpts = {
62
16
  /**
63
17
  * Owning Pairing instance.
64
18
  */
65
19
  pairing: import('./index.js').Pairing;
66
20
  /**
67
- * blind-pairing candidate request being answered.
21
+ * The record of the invite the joiner used.
68
22
  */
69
- req: any;
70
- /**
71
- * Invite the candidate paired against.
72
- */
73
- invite: import('./invite.js').Invite;
74
- /**
75
- * Per-invite seed used to sign the response.
76
- */
77
- seed: Uint8Array;
23
+ invite: {
24
+ id: string;
25
+ role: string;
26
+ expires?: number;
27
+ };
78
28
  /**
79
- * Decoded joiner payload (raw bytes when no encoding).
29
+ * The waiting join, as the database keeps it.
80
30
  */
81
- userData: any;
31
+ row: {
32
+ id: string;
33
+ identity: Uint8Array;
34
+ reply: Uint8Array;
35
+ };
36
+ };
37
+ export type AcceptOpts = {
82
38
  /**
83
- * Called once when the candidate is confirmed or denied.
39
+ * Role granted: the invite's by default, at most the invite's.
84
40
  */
85
- onsettle: () => void;
41
+ role?: string;
86
42
  };
87
43
  /**
88
- * @typedef {object} ConfirmOpts
89
- * @property {Uint8Array} [key] 32-byte resource key delivered to the joiner; required at runtime.
90
- * @property {Uint8Array | null} [encryptionKey] Optional 32-byte symmetric key.
91
- * @property {Uint8Array | null} [additional] Extra opaque bytes piggybacked on the response.
92
- *
93
44
  * @typedef {object} RequestOpts
94
45
  * @property {import('./index.js').Pairing} pairing Owning Pairing instance.
95
- * @property {any} req blind-pairing candidate request being answered.
96
- * @property {import('./invite.js').Invite} invite Invite the candidate paired against.
97
- * @property {Uint8Array} seed Per-invite seed used to sign the response.
98
- * @property {any} userData Decoded joiner payload (raw bytes when no encoding).
99
- * @property {() => void} onsettle Called once when the candidate is confirmed or denied.
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.
100
51
  */
101
52
  /**
102
- * Internal — a pairing request from an incoming candidate, awaiting the host's
103
- * accept/deny.
104
- *
105
- * @property {boolean} _settled Whether confirm/deny has already run.
53
+ * A join on a `confirm` invite, waiting in the database for a member to accept or deny it.
106
54
  */
107
55
  export declare class Request {
108
56
  pairing: import("./index.js").Pairing;
109
- _req: any;
110
- _seed: Uint8Array<ArrayBufferLike>;
111
- _settled: boolean;
112
- _onsettle: () => void;
113
- invite: import("./invite.js").Invite;
114
- userData: any;
115
- publicKey: any;
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;
116
72
  /** @param {RequestOpts} opts */
117
- constructor(opts: RequestOpts);
73
+ constructor({ pairing, invite, row }: RequestOpts);
118
74
  /**
119
- * Accept the candidate and reveal the resource key. Idempotent.
75
+ * Admit the joiner. Every device of an inviter then replies with the keys. Idempotent.
120
76
  *
121
- * @param {ConfirmOpts} opts
77
+ * @param {AcceptOpts} [opts]
122
78
  * @returns {Promise<void>}
123
79
  */
124
- confirm({ key, encryptionKey, additional }?: ConfirmOpts): Promise<void>;
80
+ accept({ role }?: AcceptOpts): Promise<void>;
125
81
  /**
126
- * Reject the candidate with an optional reason. Idempotent.
82
+ * Refuse the joiner, with an optional reason. Idempotent.
127
83
  *
128
84
  * @param {string} [reason]
129
85
  * @returns {Promise<void>}
130
86
  */
131
87
  deny(reason?: string): Promise<void>;
132
- _respond(envelope: any): void;
88
+ /**
89
+ * @private
90
+ * @param {Promise<void>} answer
91
+ */
92
+ private _answering;
133
93
  }
@@ -5,65 +5,47 @@ export type Spec = {
5
5
  * hyperschema-shaped module.
6
6
  */
7
7
  schema: {
8
- encode: (type: string, value: any) => Uint8Array;
9
- decode: (type: string, buf: Uint8Array) => any;
10
- getEncoding?: (name: string) => any;
11
- };
12
- /**
13
- * Optional metadata; `ns` controls envelope type fqns.
14
- */
15
- meta?: {
16
- ns?: string;
8
+ encode: (type: string, value: unknown) => Uint8Array;
9
+ decode: (type: string, buf: Uint8Array) => unknown;
17
10
  };
18
11
  /**
19
12
  * hrpc constructor used by RPCServer/RPCClient.
20
13
  */
21
- rpc?: any;
14
+ rpc?: new (stream: import('streamx').Duplex) => object;
22
15
  /**
23
16
  * Filled in by `bindCodec`.
24
17
  */
25
18
  codec?: Codec;
26
19
  };
27
20
  export type Codec = {
28
- encodeRow: (type: string, row: any) => Uint8Array;
29
- decodeRow: (type: string, buf: Uint8Array) => any;
30
- encodeRows: (type: string, rows: any[]) => Uint8Array;
31
- encodeChanges: (type: string, changes: Array<{
32
- prev: any;
33
- next: any;
34
- }>) => Uint8Array;
35
- decodeChanges: (type: string, buf: Uint8Array) => Array<{
36
- prev: any;
37
- next: any;
38
- }>;
39
- decodeRows: (type: string, buf: Uint8Array) => any[];
40
- encodeQuery: (q: any) => Uint8Array;
41
- decodeQuery: (buf: Uint8Array) => any;
42
- encodeCreate: (row: any) => Uint8Array;
43
- decodeCreate: (buf: Uint8Array) => any;
44
- encodeAction: (handle: any, op: string, data: any) => Uint8Array;
45
- decodeAction: (handle: any, op: string, buf: Uint8Array) => any;
21
+ encodeRow: (type: string, row: Record<string, unknown>) => Uint8Array;
22
+ decodeRow: (type: string, buf: Uint8Array) => Record<string, unknown>;
23
+ encodeRows: (type: string, rows: Record<string, unknown>[]) => Uint8Array;
24
+ decodeRows: (type: string, buf: Uint8Array) => Record<string, unknown>[];
25
+ encodeCreate: (row: Record<string, unknown>) => Uint8Array;
26
+ decodeCreate: (buf: Uint8Array) => Record<string, unknown>;
27
+ encodeAction: (handle: Record<string, {
28
+ schema?: string;
29
+ }>, op: string, data: unknown) => Uint8Array;
30
+ decodeAction: (handle: Record<string, {
31
+ schema?: string;
32
+ }>, op: string, buf: Uint8Array) => unknown;
46
33
  };
47
34
  /**
48
35
  * @typedef {object} Spec
49
- * @property {{ encode: (type: string, value: any) => Uint8Array, decode: (type: string, buf: Uint8Array) => any, getEncoding?: (name: string) => any }} schema hyperschema-shaped module.
50
- * @property {{ ns?: string }} [meta] Optional metadata; `ns` controls envelope type fqns.
51
- * @property {any} [rpc] hrpc constructor used by RPCServer/RPCClient.
52
- * @property {Codec} [codec] Filled in by `bindCodec`.
36
+ * @property {{ encode: (type: string, value: unknown) => Uint8Array, decode: (type: string, buf: Uint8Array) => unknown }} schema hyperschema-shaped module.
37
+ * @property {new (stream: import('streamx').Duplex) => object} [rpc] hrpc constructor used by RPCServer/RPCClient.
38
+ * @property {Codec} [codec] Filled in by `bindCodec`.
53
39
  *
54
40
  * @typedef {object} Codec
55
- * @property {(type: string, row: any) => Uint8Array} encodeRow
56
- * @property {(type: string, buf: Uint8Array) => any} decodeRow
57
- * @property {(type: string, rows: any[]) => Uint8Array} encodeRows
58
- * @property {(type: string, changes: Array<{ prev: any, next: any }>) => Uint8Array} encodeChanges
59
- * @property {(type: string, buf: Uint8Array) => Array<{ prev: any, next: any }>} decodeChanges
60
- * @property {(type: string, buf: Uint8Array) => any[]} decodeRows
61
- * @property {(q: any) => Uint8Array} encodeQuery
62
- * @property {(buf: Uint8Array) => any} decodeQuery
63
- * @property {(row: any) => Uint8Array} encodeCreate
64
- * @property {(buf: Uint8Array) => any} decodeCreate
65
- * @property {(handle: any, op: string, data: any) => Uint8Array} encodeAction
66
- * @property {(handle: any, op: string, buf: Uint8Array) => any} decodeAction
41
+ * @property {(type: string, row: Record<string, unknown>) => Uint8Array} encodeRow
42
+ * @property {(type: string, buf: Uint8Array) => Record<string, unknown>} decodeRow
43
+ * @property {(type: string, rows: Record<string, unknown>[]) => Uint8Array} encodeRows
44
+ * @property {(type: string, buf: Uint8Array) => Record<string, unknown>[]} decodeRows
45
+ * @property {(row: Record<string, unknown>) => Uint8Array} encodeCreate
46
+ * @property {(buf: Uint8Array) => Record<string, unknown>} decodeCreate
47
+ * @property {(handle: Record<string, { schema?: string }>, op: string, data: unknown) => Uint8Array} encodeAction
48
+ * @property {(handle: Record<string, { schema?: string }>, op: string, buf: Uint8Array) => unknown} decodeAction
67
49
  */
68
50
  /**
69
51
  * Attach a `codec` namespace to a hyperschema-shaped `spec`.
@@ -4,15 +4,18 @@ import ReadyResource from 'ready-resource';
4
4
  * hrpc binding.
5
5
  */
6
6
  export declare class RPCPeer extends ReadyResource {
7
- ipc: any;
7
+ ipc: import("streamx").Duplex<import("streamx").DuplexEvents>;
8
8
  spec: import("./index.js").Spec;
9
- framed: any;
10
- rpc: any;
9
+ /** @type {import('framed-stream')} */
10
+ framed: import('framed-stream');
11
+ rpc: object;
11
12
  /**
12
- * @param {any} ipc Duplex IPC stream (e.g. a socket or pipe).
13
+ * @param {import('streamx').Duplex} ipc Duplex IPC stream (e.g. a socket or pipe).
13
14
  * @param {import('./index.js').Spec} spec Spec object exposing `rpc` and `schema`.
14
15
  */
15
- constructor(ipc: any, spec: import('./index.js').Spec);
16
- _open(): Promise<void>;
17
- _close(): Promise<void>;
16
+ constructor(ipc: import('streamx').Duplex, spec: import('./index.js').Spec);
17
+ /** @private */
18
+ private _open;
19
+ /** @private */
20
+ private _close;
18
21
  }
@@ -1,7 +1,7 @@
1
1
  import ReadyResource from 'ready-resource';
2
2
  export type StorageOpts = {
3
3
  spec: {
4
- database: any;
4
+ database: object;
5
5
  meta?: {
6
6
  ns?: string;
7
7
  refs?: Record<string, {
@@ -13,11 +13,11 @@ export type StorageOpts = {
13
13
  /**
14
14
  * Pre-existing HypercoreStorage to reuse.
15
15
  */
16
- root?: any;
16
+ root?: import('hypercore-storage');
17
17
  /**
18
18
  * Pre-existing Corestore to reuse.
19
19
  */
20
- store?: any;
20
+ store?: import('corestore');
21
21
  /**
22
22
  * 32-byte key encrypting the backing core at rest (bee backend only).
23
23
  */
@@ -27,36 +27,21 @@ export type Ref = {
27
27
  name: string;
28
28
  kind: string;
29
29
  };
30
- export type StoredRow = {
31
- id?: string;
32
- createdAt: number;
33
- updatedAt: number;
34
- [k: string]: any;
35
- };
36
- export type SingleResult = {
37
- data: any;
38
- };
39
- export type ListResult = {
40
- data: any[];
41
- total: number;
42
- size: number;
43
- };
44
- export type GetByIdResult = {
45
- data: any | null;
46
- };
30
+ export type Row = import('../database/index.js').Row;
31
+ export type SingleResult = import('../database/index.js').SingleResult;
32
+ export type ListResult = import('../database/index.js').ListResult;
47
33
  /**
48
34
  * @typedef {object} StorageOpts
49
- * @property {{ database: any, meta?: { ns?: string, refs?: Record<string, { kind?: string }> } }} spec
35
+ * @property {{ database: object, meta?: { ns?: string, refs?: Record<string, { kind?: string }> } }} spec
50
36
  * @property {'rocks' | 'bee'} backend
51
- * @property {any} [root] Pre-existing HypercoreStorage to reuse.
52
- * @property {any} [store] Pre-existing Corestore to reuse.
37
+ * @property {import('hypercore-storage')} [root] Pre-existing HypercoreStorage to reuse.
38
+ * @property {import('corestore')} [store] Pre-existing Corestore to reuse.
53
39
  * @property {Uint8Array} [storageKey] 32-byte key encrypting the backing core at rest (bee backend only).
54
40
  *
55
41
  * @typedef {{ name: string, kind: string }} Ref
56
- * @typedef {{ id?: string, createdAt: number, updatedAt: number, [k: string]: any }} StoredRow
57
- * @typedef {{ data: any }} SingleResult
58
- * @typedef {{ data: any[], total: number, size: number }} ListResult
59
- * @typedef {{ data: any | null }} GetByIdResult
42
+ * @typedef {import('../database/index.js').Row} Row
43
+ * @typedef {import('../database/index.js').SingleResult} SingleResult
44
+ * @typedef {import('../database/index.js').ListResult} ListResult
60
45
  */
61
46
  /**
62
47
  * Local, single-writer storage. Backed by either RocksDB (`rocks`) or a Hyperbee on top of
@@ -65,7 +50,7 @@ export type GetByIdResult = {
65
50
  export declare class Storage extends ReadyResource {
66
51
  dir: string;
67
52
  spec: {
68
- database: any;
53
+ database: object;
69
54
  meta?: {
70
55
  ns?: string;
71
56
  refs?: Record<string, {
@@ -79,36 +64,46 @@ export declare class Storage extends ReadyResource {
79
64
  refs: Record<string, {
80
65
  kind?: string;
81
66
  }>;
82
- _cf: string;
83
- _ownsRoot: boolean;
84
- _ownsStore: boolean;
85
- root: any;
86
- store: any;
87
- db: any;
67
+ /** @private */
68
+ _cf;
69
+ /** @private */
70
+ _ownsRoot;
71
+ /** @private */
72
+ _ownsStore;
73
+ /** @type {import('hypercore-storage') | null} */
74
+ root: import('hypercore-storage') | null;
75
+ /** @type {import('corestore') | null} */
76
+ store: import('corestore') | null;
77
+ /** @type {import('hyperdb') | null} */
78
+ db: import('hyperdb') | null;
79
+ /** @private */
80
+ _writing;
88
81
  /**
89
82
  * @param {string} dir
90
83
  * @param {StorageOpts} [opts]
91
84
  */
92
85
  constructor(dir: string, { spec, backend, root, store, storageKey }?: StorageOpts);
93
- _open(): Promise<void>;
94
- _close(): Promise<void>;
86
+ /** @private */
87
+ private _open;
88
+ /** @private */
89
+ private _close;
95
90
  /**
96
91
  * Insert (or overwrite by id) a row, stamping `id`/`createdAt`/`updatedAt`.
97
92
  *
98
93
  * @param {string} name
99
- * @param {Record<string, any>} row
94
+ * @param {Record<string, unknown>} row
100
95
  * @returns {Promise<SingleResult>}
101
96
  */
102
- put(name: string, row: Record<string, any>): Promise<SingleResult>;
97
+ put(name: string, row: Record<string, unknown>): Promise<SingleResult>;
103
98
  /**
104
99
  * Upsert by merging with the existing row, preserving `createdAt`.
105
100
  *
106
101
  * @param {string} name
107
- * @param {Record<string, any>} row
102
+ * @param {Record<string, unknown>} row
108
103
  * @param {{ upsert?: boolean }} [opts]
109
104
  * @returns {Promise<SingleResult | null>}
110
105
  */
111
- set(name: string, row: Record<string, any>, { upsert }?: {
106
+ set(name: string, row: Record<string, unknown>, { upsert }?: {
112
107
  upsert?: boolean;
113
108
  }): Promise<SingleResult | null>;
114
109
  /**
@@ -124,28 +119,43 @@ export declare class Storage extends ReadyResource {
124
119
  * record (single). With a string id: fetch that specific row.
125
120
  *
126
121
  * @param {string} name
127
- * @param {string | Record<string, any>} [query]
128
- * @returns {Promise<SingleResult | ListResult | GetByIdResult>}
122
+ * @param {string | Record<string, unknown>} [query]
123
+ * @returns {Promise<SingleResult | ListResult>}
129
124
  */
130
- get(name: string, query?: string | Record<string, any>): Promise<SingleResult | ListResult | GetByIdResult>;
125
+ get(name: string, query?: string | Record<string, unknown>): Promise<SingleResult | ListResult>;
131
126
  /**
132
127
  * Live snapshot stream — re-emits the latest `get()` result on every
133
128
  * underlying mutation. Destroy the stream to stop watching.
134
129
  *
135
130
  * @param {string} name
136
- * @param {Record<string, any>} [query]
131
+ * @param {Record<string, unknown>} [query]
137
132
  * @returns {import('streamx').Readable}
138
133
  */
139
- watch(name: string, query?: Record<string, any>): import('streamx').Readable;
140
- _guard(): void;
141
- /** @param {string} name @returns {Ref} */
142
- _ref(name: string): Ref;
143
- /** @param {Ref} ref @returns {string} */
144
- _col(ref: Ref): string;
145
- /** @param {Ref} ref @param {string} [id] @returns {Promise<any | null>} */
146
- _read(ref: Ref, id?: string): Promise<any | null>;
147
- /** @param {Ref} ref @param {Record<string, any>} row @returns {Promise<void>} */
148
- _write(ref: Ref, row: Record<string, any>): Promise<void>;
134
+ watch(name: string, query?: Record<string, unknown>): import('streamx').Readable;
135
+ /** @private */
136
+ private _guard;
137
+ /**
138
+ * @param {string} name @returns {Ref}
139
+ * @private
140
+ */
141
+ private _ref;
142
+ /**
143
+ * @param {Ref} ref @returns {string}
144
+ * @private
145
+ */
146
+ private _col;
147
+ /**
148
+ * @param {Ref} ref @param {string} [id] @returns {Promise<Row | null>}
149
+ * @private
150
+ */
151
+ private _read;
152
+ /**
153
+ * @param {Ref} ref @param {Row} row @returns {Promise<void>}
154
+ * @private
155
+ */
156
+ private _write;
157
+ /** @private */
158
+ private _serial;
149
159
  /**
150
160
  * Construct a RocksDB-backed Storage.
151
161
  *
@@ -1,123 +0,0 @@
1
- import safetyCatch from 'safety-catch'
2
- import c from 'compact-encoding'
3
-
4
- import { channelTopic } from '../network/index.js'
5
- import { CeroError } from '../lib/errors.js'
6
- import { Response, STATUS_DENIED } from './request.js'
7
-
8
- /**
9
- * Internal — the joiner side: a candidate's handshake state machine (this *is* the
10
- * blind-pairing `addCandidate`).
11
- *
12
- * @property {any} _candidate blind-pairing addCandidate handle (null until started).
13
- * @property {ReturnType<typeof setTimeout> | null} _timer Pending timeout, if any.
14
- * @property {((result: any) => void) | null} _resolve Promise resolver, set in `start`.
15
- * @property {((err: Error) => void) | null} _reject Promise rejecter, set in `start`.
16
- * @property {boolean} _settled Whether the handshake has already settled.
17
- */
18
- export class Candidate {
19
- /**
20
- * @param {import('./index.js').Pairing} pairing
21
- * @param {import('./invite.js').Invite} invite
22
- * @param {Uint8Array | null} userData
23
- * @param {number} timeout
24
- */
25
- constructor(pairing, invite, userData, timeout) {
26
- this.pairing = pairing
27
- this.invite = invite
28
- this.userData = userData
29
- this.timeout = timeout
30
- this._candidate = null
31
- this._timer = null
32
- this._resolve = null
33
- this._reject = null
34
- this._settled = false
35
- }
36
-
37
- /**
38
- * Kick off the handshake; returns a promise that settles with the host response.
39
- *
40
- * @returns {Promise<import('./index.js').JoinResult>}
41
- */
42
- start() {
43
- this.pairing._candidates.add(this)
44
-
45
- return new Promise((resolve, reject) => {
46
- this._resolve = resolve
47
- this._reject = reject
48
-
49
- if (this.timeout > 0) {
50
- this._timer = setTimeout(() => this._fail(CeroError.TIMEOUT()), this.timeout)
51
- }
52
-
53
- try {
54
- this._candidate = this.pairing._blind.addCandidate({
55
- invite: this.invite.blind,
56
- userData: this.userData,
57
- // must hash exactly like the member side — see Pairing._open
58
- discoveryKey: channelTopic(this.invite.discoveryKey, this.pairing.network.channel),
59
- onadd: (result) => this._done(result)
60
- })
61
- this._candidate.request.on('rejected', (err) => this._fail(fromBlindError(err)))
62
- this.pairing.network.refreshInjected().catch(this._onerror ?? (() => {}))
63
- } catch (err) {
64
- this._fail(err instanceof CeroError ? err : CeroError.NETWORK_ERROR(err.message))
65
- }
66
- })
67
- }
68
-
69
- _done(result) {
70
- if (this._settled) return
71
-
72
- let envelope = null
73
- try {
74
- if (result?.data) envelope = c.decode(Response, result.data)
75
- } catch {}
76
-
77
- if (envelope && envelope.status === STATUS_DENIED) {
78
- this._fail(CeroError.DENIED(envelope.reason || null))
79
- return
80
- }
81
-
82
- if (!envelope) {
83
- this._fail(CeroError.NETWORK_ERROR('malformed pairing response'))
84
- return
85
- }
86
-
87
- this._settled = true
88
- this.pairing._candidates.delete(this)
89
- clearTimeout(this._timer)
90
-
91
- this._resolve({
92
- key: envelope.key,
93
- encryptionKey: envelope.encryptionKey,
94
- additional: envelope.extra
95
- })
96
-
97
- this._candidate.close().catch(safetyCatch)
98
- }
99
-
100
- _fail(err) {
101
- if (this._settled) return
102
- this._settled = true
103
- this.pairing._candidates.delete(this)
104
- clearTimeout(this._timer)
105
- if (this._candidate) this._candidate.close().catch(safetyCatch)
106
- this._reject(err)
107
- }
108
- }
109
-
110
- function fromBlindError(err) {
111
- // a code-less rejection is a protocol failure, never a host decision
112
- if (!err || !err.code) return CeroError.NETWORK_ERROR(err?.message || 'pairing failed')
113
- switch (err.code) {
114
- case 'INVITE_EXPIRED':
115
- return CeroError.EXPIRED(err.message)
116
- case 'INVITE_USED':
117
- return CeroError.DENIED('used', err.message)
118
- case 'PAIRING_REJECTED':
119
- return CeroError.DENIED(null, err.message)
120
- default:
121
- return CeroError.DENIED(err.message)
122
- }
123
- }
@@ -1,36 +0,0 @@
1
- /**
2
- * Internal — the joiner side: a candidate's handshake state machine (this *is* the
3
- * blind-pairing `addCandidate`).
4
- *
5
- * @property {any} _candidate blind-pairing addCandidate handle (null until started).
6
- * @property {ReturnType<typeof setTimeout> | null} _timer Pending timeout, if any.
7
- * @property {((result: any) => void) | null} _resolve Promise resolver, set in `start`.
8
- * @property {((err: Error) => void) | null} _reject Promise rejecter, set in `start`.
9
- * @property {boolean} _settled Whether the handshake has already settled.
10
- */
11
- export declare class Candidate {
12
- pairing: import("./index.js").Pairing;
13
- invite: import("./invite.js").Invite;
14
- userData: Uint8Array<ArrayBufferLike>;
15
- timeout: number;
16
- _candidate: any;
17
- _timer: number;
18
- _resolve: (value: import("./index.js").JoinResult | PromiseLike<import("./index.js").JoinResult>) => void;
19
- _reject: (reason?: any) => void;
20
- _settled: boolean;
21
- /**
22
- * @param {import('./index.js').Pairing} pairing
23
- * @param {import('./invite.js').Invite} invite
24
- * @param {Uint8Array | null} userData
25
- * @param {number} timeout
26
- */
27
- constructor(pairing: import('./index.js').Pairing, invite: import('./invite.js').Invite, userData: Uint8Array | null, timeout: number);
28
- /**
29
- * Kick off the handshake; returns a promise that settles with the host response.
30
- *
31
- * @returns {Promise<import('./index.js').JoinResult>}
32
- */
33
- start(): Promise<import('./index.js').JoinResult>;
34
- _done(result: any): void;
35
- _fail(err: any): void;
36
- }