@cero-base/core 2.0.0 → 2.1.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 +1 -1
- package/package.json +2 -2
- package/src/database/changes.js +1 -1
- package/src/database/dispatch.js +180 -36
- package/src/database/index.js +123 -142
- package/src/network/index.js +6 -1
- package/src/network/presence.js +131 -0
- package/src/storage/index.js +5 -20
- package/types/database/dispatch.d.ts +9 -1
- package/types/database/index.d.ts +101 -61
- package/types/network/index.d.ts +12 -1
- package/types/network/presence.d.ts +50 -0
- package/types/storage/index.d.ts +0 -10
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import safetyCatch from 'safety-catch'
|
|
2
|
+
import b4a from 'b4a'
|
|
3
|
+
|
|
4
|
+
import { ACTIVE, PASSIVE } from '../lib/constants.js'
|
|
5
|
+
|
|
6
|
+
const rank = (mode) => (mode === ACTIVE ? 2 : mode === PASSIVE ? 1 : 0)
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Which handles swarm. Ranked by last touch: the latest `active` search and announce, the
|
|
10
|
+
* next `announced` announce only, the rest leave their topic. A handle moves up at once and
|
|
11
|
+
* down only after `idle` ms. A pinned handle always searches, outside the budget.
|
|
12
|
+
*
|
|
13
|
+
* @typedef {{ mode: 'active' | 'passive' | null, touch: () => void, off: () => void, remove: () => void }} Slot
|
|
14
|
+
*/
|
|
15
|
+
export class Presence {
|
|
16
|
+
/**
|
|
17
|
+
* @param {import('./index.js').Network} network
|
|
18
|
+
* @param {{ active?: number, announced?: number, idle?: number }} [opts]
|
|
19
|
+
*/
|
|
20
|
+
constructor(network, { active = 8, announced = 8, idle = 30_000 } = {}) {
|
|
21
|
+
this.network = network
|
|
22
|
+
this.active = active
|
|
23
|
+
this.announced = announced
|
|
24
|
+
this.idle = idle
|
|
25
|
+
this.entries = new Map()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Register a topic. One entry per topic, shared by every slot on it until all are removed;
|
|
30
|
+
* a new entry starts untouched, at the bottom.
|
|
31
|
+
*
|
|
32
|
+
* @param {Uint8Array} topic
|
|
33
|
+
* @param {{ pinned?: boolean }} [opts]
|
|
34
|
+
* @returns {Slot}
|
|
35
|
+
*/
|
|
36
|
+
add(topic, { pinned = false } = {}) {
|
|
37
|
+
const id = b4a.toString(topic, 'hex')
|
|
38
|
+
let e = this.entries.get(id)
|
|
39
|
+
if (!e) {
|
|
40
|
+
e = {
|
|
41
|
+
topic,
|
|
42
|
+
pinned,
|
|
43
|
+
refs: 0,
|
|
44
|
+
touched: 0,
|
|
45
|
+
off: false,
|
|
46
|
+
discovery: null,
|
|
47
|
+
timer: null,
|
|
48
|
+
pending: null
|
|
49
|
+
}
|
|
50
|
+
this.entries.set(id, e)
|
|
51
|
+
}
|
|
52
|
+
e.refs++
|
|
53
|
+
this.refresh()
|
|
54
|
+
const presence = this
|
|
55
|
+
return {
|
|
56
|
+
get mode() {
|
|
57
|
+
return e.discovery ? e.discovery.mode : null
|
|
58
|
+
},
|
|
59
|
+
touch() {
|
|
60
|
+
e.touched = Date.now()
|
|
61
|
+
e.off = false
|
|
62
|
+
presence.refresh()
|
|
63
|
+
},
|
|
64
|
+
off() {
|
|
65
|
+
e.off = true
|
|
66
|
+
presence.refresh()
|
|
67
|
+
},
|
|
68
|
+
remove() {
|
|
69
|
+
if (--e.refs > 0) return
|
|
70
|
+
presence.entries.delete(id)
|
|
71
|
+
presence._leave(e)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {Uint8Array} topic
|
|
78
|
+
* @returns {'active' | 'passive' | null}
|
|
79
|
+
*/
|
|
80
|
+
mode(topic) {
|
|
81
|
+
const e = this.entries.get(b4a.toString(topic, 'hex'))
|
|
82
|
+
return e?.discovery ? e.discovery.mode : null
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
refresh() {
|
|
86
|
+
if (this.network.closing || this.network.closed) return
|
|
87
|
+
const ranked = []
|
|
88
|
+
for (const e of this.entries.values()) {
|
|
89
|
+
if (e.pinned) this._want(e, ACTIVE)
|
|
90
|
+
else if (e.off) this._want(e, null)
|
|
91
|
+
else ranked.push(e)
|
|
92
|
+
}
|
|
93
|
+
ranked.sort((a, b) => b.touched - a.touched)
|
|
94
|
+
ranked.forEach((e, i) => {
|
|
95
|
+
this._want(e, i < this.active ? ACTIVE : i < this.active + this.announced ? PASSIVE : null)
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
close() {
|
|
100
|
+
for (const e of this.entries.values()) clearTimeout(e.timer)
|
|
101
|
+
this.entries.clear()
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// up at once, down after idle: a room at the edge of the budget must not churn the DHT
|
|
105
|
+
_want(e, mode) {
|
|
106
|
+
const current = e.discovery ? e.discovery.mode : null
|
|
107
|
+
if (rank(mode) < rank(current)) {
|
|
108
|
+
e.pending = mode
|
|
109
|
+
if (!e.timer) e.timer = setTimeout(() => this._settle(e), this.idle)
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
clearTimeout(e.timer)
|
|
113
|
+
e.timer = null
|
|
114
|
+
if (mode === current) return
|
|
115
|
+
if (!e.discovery) e.discovery = this.network.join(e.topic, { mode })
|
|
116
|
+
else e.discovery.activate().catch(safetyCatch)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
_settle(e) {
|
|
120
|
+
e.timer = null
|
|
121
|
+
if (e.pending === PASSIVE) e.discovery.deactivate().catch(safetyCatch)
|
|
122
|
+
else this._leave(e)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
_leave(e) {
|
|
126
|
+
clearTimeout(e.timer)
|
|
127
|
+
e.timer = null
|
|
128
|
+
e.discovery?.destroy().catch(safetyCatch)
|
|
129
|
+
e.discovery = null
|
|
130
|
+
}
|
|
131
|
+
}
|
package/src/storage/index.js
CHANGED
|
@@ -193,29 +193,14 @@ export class Storage extends ReadyResource {
|
|
|
193
193
|
}
|
|
194
194
|
|
|
195
195
|
const data = await this.db.find(col, range(query)).toArray()
|
|
196
|
-
// a limit caps data, so total needs
|
|
197
|
-
const
|
|
198
|
-
|
|
196
|
+
// a limit caps data, so total needs the unlimited range
|
|
197
|
+
const total =
|
|
198
|
+
query?.limit != null
|
|
199
|
+
? (await this.db.find(col, range({ ...query, limit: undefined })).toArray()).length
|
|
200
|
+
: data.length
|
|
199
201
|
return { data, total, size: data.length }
|
|
200
202
|
}
|
|
201
203
|
|
|
202
|
-
/**
|
|
203
|
-
* Number of rows that match `query` (or total if omitted).
|
|
204
|
-
*
|
|
205
|
-
* @param {string} name
|
|
206
|
-
* @param {Record<string, any>} [query]
|
|
207
|
-
* @returns {Promise<{ data: number }>}
|
|
208
|
-
*/
|
|
209
|
-
async count(name, query) {
|
|
210
|
-
this._guard()
|
|
211
|
-
const ref = this._ref(name)
|
|
212
|
-
const col = this._col(ref)
|
|
213
|
-
const eq = Object.keys(query || {}).filter((k) => !QUERY_RESERVED.has(k))
|
|
214
|
-
const filtered = eq.length || query?.search
|
|
215
|
-
const rows = await this.db.find(col, filtered ? {} : range(query)).toArray()
|
|
216
|
-
return { data: (filtered ? filter(rows, query) : rows).length }
|
|
217
|
-
}
|
|
218
|
-
|
|
219
204
|
/**
|
|
220
205
|
* Live snapshot stream — re-emits the latest `get()` result on every
|
|
221
206
|
* underlying mutation. Destroy the stream to stop watching.
|
|
@@ -9,9 +9,13 @@
|
|
|
9
9
|
* @param {(err: Error) => void} opts.onerror Called when a malformed node is skipped.
|
|
10
10
|
* @param {() => Uint8Array | null} opts.key The database key, null until the bee booted.
|
|
11
11
|
* @param {(row: { epoch: number, stamp: number, wrapped: Uint8Array, commit: Uint8Array }) => Promise<void>} opts.onepoch Per-peer side of an applied rotation (skipped in dry runs).
|
|
12
|
+
* @param {(phase: 'before' | 'after', op: string) => Function[]} [opts.hooks] Registered hooks for an op, run inside its transaction.
|
|
13
|
+
* @param {(name: string) => void} [opts.touch] Marks a ref written by a hook, so its watchers tick.
|
|
14
|
+
* @param {(view: object, name: string, query?: any) => Promise<any>} [opts.read] Planned read against a given view, for a hook's `ctx.get`.
|
|
15
|
+
* @param {(fn: Function) => Function} [opts.inHook] Wraps a route so operators called inside it throw, like a hook.
|
|
12
16
|
* @returns {{ dispatch: (value: Buffer, ctx: object) => Promise<void>, apply: (nodes: Array<{ value: Buffer, key: Buffer }>, view: object, host: object) => Promise<void> }}
|
|
13
17
|
*/
|
|
14
|
-
export declare function makeDispatcher({ spec, ns, routes, onerror, key, onepoch }: {
|
|
18
|
+
export declare function makeDispatcher({ spec, ns, routes, onerror, key, onepoch, hooks, touch, read, inHook }: {
|
|
15
19
|
spec: {
|
|
16
20
|
dispatch: {
|
|
17
21
|
Router: Function;
|
|
@@ -34,6 +38,10 @@ export declare function makeDispatcher({ spec, ns, routes, onerror, key, onepoch
|
|
|
34
38
|
wrapped: Uint8Array;
|
|
35
39
|
commit: Uint8Array;
|
|
36
40
|
}) => Promise<void>;
|
|
41
|
+
hooks?: (phase: 'before' | 'after', op: string) => Function[];
|
|
42
|
+
touch?: (name: string) => void;
|
|
43
|
+
read?: (view: object, name: string, query?: any) => Promise<any>;
|
|
44
|
+
inHook?: (fn: Function) => Function;
|
|
37
45
|
}): {
|
|
38
46
|
dispatch: (value: Buffer, ctx: object) => Promise<void>;
|
|
39
47
|
apply: (nodes: Array<{
|
|
@@ -53,15 +53,19 @@ export type DatabaseOpts = {
|
|
|
53
53
|
*/
|
|
54
54
|
key?: Uint8Array | null;
|
|
55
55
|
/**
|
|
56
|
-
*
|
|
56
|
+
* Always search and announce, outside the network's presence budget. The root.
|
|
57
57
|
*/
|
|
58
|
-
|
|
58
|
+
pinned?: boolean;
|
|
59
59
|
/**
|
|
60
60
|
* This device's writer keypair; a fresh random one by default. Never the identity's, never the database key.
|
|
61
61
|
*/
|
|
62
62
|
keyPair?: import('../identity/index.js').KeyPair;
|
|
63
63
|
/**
|
|
64
|
-
* Called when a
|
|
64
|
+
* Called when a node is skipped or refused, or the bee errors.
|
|
65
|
+
*
|
|
66
|
+
* Events: `update` (touched refs, a Set, after each applied batch), `apply` (one per applied
|
|
67
|
+
* op: `{ op, name, row, writerKey, seq }`, local and replicated), `writable`, `unwritable`,
|
|
68
|
+
* `behind` (an op from a newer app version was skipped).
|
|
65
69
|
*/
|
|
66
70
|
onerror?: (err: Error) => void;
|
|
67
71
|
};
|
|
@@ -89,7 +93,51 @@ export type Ref = {
|
|
|
89
93
|
verb: string;
|
|
90
94
|
name: string;
|
|
91
95
|
};
|
|
92
|
-
export type
|
|
96
|
+
export type HookContext = {
|
|
97
|
+
/**
|
|
98
|
+
* One of `put`, `set`, `del`, or an action name.
|
|
99
|
+
*/
|
|
100
|
+
op: string;
|
|
101
|
+
/**
|
|
102
|
+
* The ref the op targets.
|
|
103
|
+
*/
|
|
104
|
+
name: string;
|
|
105
|
+
/**
|
|
106
|
+
* The incoming row; mutate it in `before` to change what lands.
|
|
107
|
+
*/
|
|
108
|
+
row: Record<string, unknown> | null;
|
|
109
|
+
/**
|
|
110
|
+
* The stored row, or null.
|
|
111
|
+
*/
|
|
112
|
+
existing: Record<string, unknown> | null;
|
|
113
|
+
/**
|
|
114
|
+
* The row id for a `del`.
|
|
115
|
+
*/
|
|
116
|
+
id: string | null;
|
|
117
|
+
/**
|
|
118
|
+
* The writer's member id.
|
|
119
|
+
*/
|
|
120
|
+
memberId: string;
|
|
121
|
+
/**
|
|
122
|
+
* The writer's role.
|
|
123
|
+
*/
|
|
124
|
+
role: string;
|
|
125
|
+
get: (ref: string | {
|
|
126
|
+
name: string;
|
|
127
|
+
}, query?: string | Record<string, unknown>) => Promise<{
|
|
128
|
+
data: unknown;
|
|
129
|
+
}>;
|
|
130
|
+
put: (ref: string | {
|
|
131
|
+
name: string;
|
|
132
|
+
}, row: Record<string, unknown>) => Promise<void>;
|
|
133
|
+
set: (ref: string | {
|
|
134
|
+
name: string;
|
|
135
|
+
}, row: Record<string, unknown>) => Promise<void>;
|
|
136
|
+
del: (ref: string | {
|
|
137
|
+
name: string;
|
|
138
|
+
}, id?: string) => Promise<void>;
|
|
139
|
+
};
|
|
140
|
+
export type HookFn = (ctx: HookContext) => unknown;
|
|
93
141
|
/**
|
|
94
142
|
* @typedef {object} DatabaseOpts
|
|
95
143
|
* @property {any} store Corestore (or compatible) used to materialize the autobee.
|
|
@@ -101,15 +149,31 @@ export type HookFn = (ctx: any) => any | Promise<any>;
|
|
|
101
149
|
* @property {Uint8Array | null} [encryptionKey] Optional encryption key; falls back to identity's key.
|
|
102
150
|
* @property {Array<{ epoch: number, entropy: Uint8Array }> | null} [epochs] Rotation epochs to prime the keyring with (delivered at join).
|
|
103
151
|
* @property {Uint8Array | null} [key] Existing autobee key to reopen.
|
|
104
|
-
* @property {boolean} [
|
|
152
|
+
* @property {boolean} [pinned] Always search and announce, outside the network's presence budget. The root.
|
|
105
153
|
* @property {import('../identity/index.js').KeyPair} [keyPair] This device's writer keypair; a fresh random one by default. Never the identity's, never the database key.
|
|
106
|
-
* @property {(err: Error) => void} [onerror] Called when a
|
|
154
|
+
* @property {(err: Error) => void} [onerror] Called when a node is skipped or refused, or the bee errors.
|
|
155
|
+
*
|
|
156
|
+
* Events: `update` (touched refs, a Set, after each applied batch), `apply` (one per applied
|
|
157
|
+
* op: `{ op, name, row, writerKey, seq }`, local and replicated), `writable`, `unwritable`,
|
|
158
|
+
* `behind` (an op from a newer app version was skipped).
|
|
107
159
|
*
|
|
108
160
|
* @typedef {{ data: any | null }} SingleResult
|
|
109
161
|
* @typedef {{ data: any[], total: number | null, size: number }} ListResult `total` is null when a limited read skipped the full count — pass `{ total: true }` to force it.
|
|
110
162
|
* @typedef {{ gt?: string, gte?: string, lt?: string, lte?: string, reverse?: boolean, limit?: number, search?: string, fields?: string[], total?: boolean }} Query
|
|
111
163
|
* @typedef {{ kind: string, verb: string, name: string }} Ref
|
|
112
|
-
* @typedef {
|
|
164
|
+
* @typedef {object} HookContext
|
|
165
|
+
* @property {string} op One of `put`, `set`, `del`, or an action name.
|
|
166
|
+
* @property {string} name The ref the op targets.
|
|
167
|
+
* @property {Record<string, unknown> | null} row The incoming row; mutate it in `before` to change what lands.
|
|
168
|
+
* @property {Record<string, unknown> | null} existing The stored row, or null.
|
|
169
|
+
* @property {string | null} id The row id for a `del`.
|
|
170
|
+
* @property {string} memberId The writer's member id.
|
|
171
|
+
* @property {string} role The writer's role.
|
|
172
|
+
* @property {(ref: string | { name: string }, query?: string | Record<string, unknown>) => Promise<{ data: unknown }>} get
|
|
173
|
+
* @property {(ref: string | { name: string }, row: Record<string, unknown>) => Promise<void>} put
|
|
174
|
+
* @property {(ref: string | { name: string }, row: Record<string, unknown>) => Promise<void>} set
|
|
175
|
+
* @property {(ref: string | { name: string }, id?: string) => Promise<void>} del
|
|
176
|
+
* @typedef {(ctx: HookContext) => unknown} HookFn
|
|
113
177
|
*/
|
|
114
178
|
/**
|
|
115
179
|
* Multi-writer database built on Autobee + HyperDB.
|
|
@@ -149,7 +213,7 @@ export declare class Database extends ReadyResource {
|
|
|
149
213
|
keyring: Keyring;
|
|
150
214
|
rotation: Rotation;
|
|
151
215
|
key: Uint8Array<ArrayBufferLike>;
|
|
152
|
-
|
|
216
|
+
pinned: boolean;
|
|
153
217
|
keyPair: import("../index.js").KeyPair;
|
|
154
218
|
_onerror: any;
|
|
155
219
|
bee: EpochAutobee;
|
|
@@ -160,12 +224,11 @@ export declare class Database extends ReadyResource {
|
|
|
160
224
|
key: Buffer;
|
|
161
225
|
}>, view: object, host: object) => Promise<void>;
|
|
162
226
|
};
|
|
163
|
-
|
|
227
|
+
_presence: import("../network/presence.js").Slot;
|
|
164
228
|
_txChain: any;
|
|
165
229
|
_before: Map<any, any>;
|
|
166
230
|
_after: Map<any, any>;
|
|
167
|
-
|
|
168
|
-
_observers: Set<any>;
|
|
231
|
+
_hooking: number;
|
|
169
232
|
_verbs: Map<any, any>;
|
|
170
233
|
_touched: Set<any>;
|
|
171
234
|
_seq: number;
|
|
@@ -185,15 +248,15 @@ export declare class Database extends ReadyResource {
|
|
|
185
248
|
_open(): Promise<void>;
|
|
186
249
|
_close(): Promise<void>;
|
|
187
250
|
/**
|
|
188
|
-
*
|
|
189
|
-
*
|
|
251
|
+
* `true` ranks this database as just touched, `false` takes it out of the swarm until the
|
|
252
|
+
* next update lands in it.
|
|
190
253
|
*
|
|
191
254
|
* @param {boolean} active
|
|
192
|
-
* @returns {Promise<void>}
|
|
193
255
|
*/
|
|
194
|
-
setActive(active: boolean):
|
|
256
|
+
setActive(active: boolean): void;
|
|
195
257
|
/**
|
|
196
|
-
* Register a pre-op hook.
|
|
258
|
+
* Register a pre-op hook. It runs at apply on every peer, inside the op's transaction;
|
|
259
|
+
* returning `false` (or throwing) refuses the op everywhere.
|
|
197
260
|
*
|
|
198
261
|
* @param {string} op
|
|
199
262
|
* @param {HookFn} fn
|
|
@@ -201,35 +264,14 @@ export declare class Database extends ReadyResource {
|
|
|
201
264
|
*/
|
|
202
265
|
before(op: string, fn: HookFn): () => void;
|
|
203
266
|
/**
|
|
204
|
-
* Register a post-op hook,
|
|
267
|
+
* Register a post-op hook. It runs at apply on every peer, in the op's transaction, so it
|
|
268
|
+
* may write derived rows through `ctx.put` / `ctx.set` / `ctx.del`.
|
|
205
269
|
*
|
|
206
270
|
* @param {string} op
|
|
207
271
|
* @param {HookFn} fn
|
|
208
272
|
* @returns {() => void} disposer
|
|
209
273
|
*/
|
|
210
274
|
after(op: string, fn: HookFn): () => void;
|
|
211
|
-
/**
|
|
212
|
-
* Subscribe to local apply notifications. Fires whenever the view updates;
|
|
213
|
-
* pass `scope` (a ref name) to fire only when that ref was touched.
|
|
214
|
-
*
|
|
215
|
-
* @param {() => void} fn
|
|
216
|
-
* @param {string} [scope]
|
|
217
|
-
* @returns {() => void} disposer
|
|
218
|
-
*/
|
|
219
|
-
onUpdate(fn: () => void, scope?: string): () => void;
|
|
220
|
-
/**
|
|
221
|
-
* Observe every applied op — local AND replicated (apply processes the merged log).
|
|
222
|
-
*
|
|
223
|
-
* @param {(event: { op: string, name: string, row: any, writerKey: any, seq: number }) => void} fn
|
|
224
|
-
* @returns {() => void} disposer
|
|
225
|
-
*/
|
|
226
|
-
onApply(fn: (event: {
|
|
227
|
-
op: string;
|
|
228
|
-
name: string;
|
|
229
|
-
row: any;
|
|
230
|
-
writerKey: any;
|
|
231
|
-
seq: number;
|
|
232
|
-
}) => void): () => void;
|
|
233
275
|
/**
|
|
234
276
|
* Insert (or overwrite by id) a row, stamping `id`/`createdAt`/`updatedAt`.
|
|
235
277
|
*
|
|
@@ -300,15 +342,14 @@ export declare class Database extends ReadyResource {
|
|
|
300
342
|
* @returns {Promise<SingleResult | ListResult>}
|
|
301
343
|
*/
|
|
302
344
|
get(name: string, query?: string | Query): Promise<SingleResult | ListResult>;
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
data: number;
|
|
345
|
+
_read(view: any, name: any, query: any): Promise<{
|
|
346
|
+
data: any;
|
|
347
|
+
total?: undefined;
|
|
348
|
+
size?: undefined;
|
|
349
|
+
} | {
|
|
350
|
+
data: any[];
|
|
351
|
+
total: any;
|
|
352
|
+
size: number;
|
|
312
353
|
}>;
|
|
313
354
|
/**
|
|
314
355
|
* Live snapshot stream — re-emits the latest `get()` result on every
|
|
@@ -360,12 +401,14 @@ export declare class Database extends ReadyResource {
|
|
|
360
401
|
timeout?: number;
|
|
361
402
|
}): Promise<void>;
|
|
362
403
|
/**
|
|
363
|
-
*
|
|
404
|
+
* Admit a device as a writer for `memberId`, an existing member. Omit it to
|
|
405
|
+
* admit another device of this identity.
|
|
364
406
|
*
|
|
365
407
|
* @param {Uint8Array} publicKey
|
|
408
|
+
* @param {string} [memberId]
|
|
366
409
|
* @returns {Promise<void>}
|
|
367
410
|
*/
|
|
368
|
-
addWriter(publicKey: Uint8Array): Promise<void>;
|
|
411
|
+
addWriter(publicKey: Uint8Array, memberId?: string): Promise<void>;
|
|
369
412
|
/**
|
|
370
413
|
* Remove a peer's writer key from the indexer set.
|
|
371
414
|
*
|
|
@@ -400,8 +443,9 @@ export declare class Database extends ReadyResource {
|
|
|
400
443
|
_apply(nodes: any, view: any, host: any): Promise<void>;
|
|
401
444
|
_update(db: any): Promise<void>;
|
|
402
445
|
_joinSwarm(bee: any, discoveryKey: any): void;
|
|
403
|
-
|
|
404
|
-
|
|
446
|
+
_hooks(phase: any, op: any): any;
|
|
447
|
+
_inHook(fn: any): (ctx: any) => Promise<any>;
|
|
448
|
+
onUpdate(name: any, fn: any): () => this;
|
|
405
449
|
_opOf(node: any): {
|
|
406
450
|
op: any;
|
|
407
451
|
name: any;
|
|
@@ -411,19 +455,15 @@ export declare class Database extends ReadyResource {
|
|
|
411
455
|
_merge(name: any, row: any, { upsert }?: {
|
|
412
456
|
upsert?: boolean;
|
|
413
457
|
}): Promise<{
|
|
414
|
-
op: any;
|
|
415
|
-
name: any;
|
|
416
458
|
row: any;
|
|
417
459
|
}>;
|
|
418
460
|
_prepare(name: any, row: any): Ref;
|
|
419
|
-
_append(
|
|
420
|
-
op: any;
|
|
421
|
-
name: any;
|
|
461
|
+
_append(stored: any, verb: any): Promise<{
|
|
422
462
|
row: any;
|
|
423
463
|
}>;
|
|
424
|
-
_done(
|
|
464
|
+
_done(ctx: any): {
|
|
425
465
|
data: any;
|
|
426
|
-
}
|
|
466
|
+
};
|
|
427
467
|
_onfuture(version: any): void;
|
|
428
468
|
_dryRun(encoded: any): Promise<any>;
|
|
429
469
|
_plan(name: any, query?: {}): {
|
|
@@ -440,7 +480,7 @@ export declare class Database extends ReadyResource {
|
|
|
440
480
|
rest: {};
|
|
441
481
|
sorted: boolean;
|
|
442
482
|
};
|
|
443
|
-
_total(path: any, range: any,
|
|
483
|
+
_total(view: any, path: any, range: any, matched: any, query: any): Promise<any>;
|
|
444
484
|
_optimistic(op: any, opts: any): Promise<void>;
|
|
445
485
|
_backfilled(timeout: any): Promise<void>;
|
|
446
486
|
_admission(writer: any, ts?: number): {
|
|
@@ -450,5 +490,5 @@ export declare class Database extends ReadyResource {
|
|
|
450
490
|
ts: number;
|
|
451
491
|
};
|
|
452
492
|
_checkFields(name: any, row: any): void;
|
|
453
|
-
_admit(verb: any,
|
|
493
|
+
_admit(verb: any, publicKey: any, memberId: any): Promise<void>;
|
|
454
494
|
}
|
package/types/network/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import ReadyResource from 'ready-resource';
|
|
2
2
|
import { Discovery } from './discovery.js';
|
|
3
|
+
import { Presence } from './presence.js';
|
|
3
4
|
export declare function channelTopic(topic: any, channel: any): any;
|
|
4
5
|
export type NetworkOpts = {
|
|
5
6
|
/**
|
|
@@ -37,6 +38,14 @@ export type NetworkOpts = {
|
|
|
37
38
|
* Blind-peer public keys; each attached room/blob core is mirrored through them for offline sync.
|
|
38
39
|
*/
|
|
39
40
|
mirrors?: Array<string | Uint8Array>;
|
|
41
|
+
/**
|
|
42
|
+
* Swarm budget for attached databases: how many search, how many only announce, and the idle ms before the rest leave.
|
|
43
|
+
*/
|
|
44
|
+
presence?: {
|
|
45
|
+
active?: number;
|
|
46
|
+
announced?: number;
|
|
47
|
+
idle?: number;
|
|
48
|
+
};
|
|
40
49
|
};
|
|
41
50
|
export type Replicable = {
|
|
42
51
|
replicate: (stream: any) => any;
|
|
@@ -51,6 +60,7 @@ export type Replicable = {
|
|
|
51
60
|
* @property {string} [channel] Optional network-isolation label; only same-channel peers meet.
|
|
52
61
|
* @property {any} [store] Corestore; required for mirrors (blind peers replicate its cores).
|
|
53
62
|
* @property {Array<string | Uint8Array>} [mirrors] Blind-peer public keys; each attached room/blob core is mirrored through them for offline sync.
|
|
63
|
+
* @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.
|
|
54
64
|
*
|
|
55
65
|
* @typedef {{ replicate: (stream: any) => any }} Replicable
|
|
56
66
|
*/
|
|
@@ -72,6 +82,7 @@ export declare class Network extends ReadyResource {
|
|
|
72
82
|
mirrors: any[];
|
|
73
83
|
_swarm: any;
|
|
74
84
|
wakeup: any;
|
|
85
|
+
presence: Presence;
|
|
75
86
|
info: object;
|
|
76
87
|
_peerInfo: Map<any, any>;
|
|
77
88
|
_infoSenders: Set<any>;
|
|
@@ -81,7 +92,7 @@ export declare class Network extends ReadyResource {
|
|
|
81
92
|
_blind: any;
|
|
82
93
|
_blindPeering: any;
|
|
83
94
|
/** @param {NetworkOpts} [opts] */
|
|
84
|
-
constructor({ identity, bootstrap, firewall, relayThrough, backoffs, channel, store, mirrors }?: NetworkOpts);
|
|
95
|
+
constructor({ identity, bootstrap, firewall, relayThrough, backoffs, channel, store, mirrors, presence }?: NetworkOpts);
|
|
85
96
|
/** @returns {any} The underlying hyperswarm, or null before ready / after close. */
|
|
86
97
|
get swarm(): any;
|
|
87
98
|
/** @returns {Map<string, any>} Known peers keyed by public-key string. */
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type Slot = {
|
|
2
|
+
mode: 'active' | 'passive' | null;
|
|
3
|
+
touch: () => void;
|
|
4
|
+
off: () => void;
|
|
5
|
+
remove: () => void;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Which handles swarm. Ranked by last touch: the latest `active` search and announce, the
|
|
9
|
+
* next `announced` announce only, the rest leave their topic. A handle moves up at once and
|
|
10
|
+
* down only after `idle` ms. A pinned handle always searches, outside the budget.
|
|
11
|
+
*
|
|
12
|
+
* @typedef {{ mode: 'active' | 'passive' | null, touch: () => void, off: () => void, remove: () => void }} Slot
|
|
13
|
+
*/
|
|
14
|
+
export declare class Presence {
|
|
15
|
+
network: import("./index.js").Network;
|
|
16
|
+
active: number;
|
|
17
|
+
announced: number;
|
|
18
|
+
idle: number;
|
|
19
|
+
entries: Map<any, any>;
|
|
20
|
+
/**
|
|
21
|
+
* @param {import('./index.js').Network} network
|
|
22
|
+
* @param {{ active?: number, announced?: number, idle?: number }} [opts]
|
|
23
|
+
*/
|
|
24
|
+
constructor(network: import('./index.js').Network, { active, announced, idle }?: {
|
|
25
|
+
active?: number;
|
|
26
|
+
announced?: number;
|
|
27
|
+
idle?: number;
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* Register a topic. One entry per topic, shared by every slot on it until all are removed;
|
|
31
|
+
* a new entry starts untouched, at the bottom.
|
|
32
|
+
*
|
|
33
|
+
* @param {Uint8Array} topic
|
|
34
|
+
* @param {{ pinned?: boolean }} [opts]
|
|
35
|
+
* @returns {Slot}
|
|
36
|
+
*/
|
|
37
|
+
add(topic: Uint8Array, { pinned }?: {
|
|
38
|
+
pinned?: boolean;
|
|
39
|
+
}): Slot;
|
|
40
|
+
/**
|
|
41
|
+
* @param {Uint8Array} topic
|
|
42
|
+
* @returns {'active' | 'passive' | null}
|
|
43
|
+
*/
|
|
44
|
+
mode(topic: Uint8Array): 'active' | 'passive' | null;
|
|
45
|
+
refresh(): void;
|
|
46
|
+
close(): void;
|
|
47
|
+
_want(e: any, mode: any): void;
|
|
48
|
+
_settle(e: any): void;
|
|
49
|
+
_leave(e: any): void;
|
|
50
|
+
}
|
package/types/storage/index.d.ts
CHANGED
|
@@ -128,16 +128,6 @@ export declare class Storage extends ReadyResource {
|
|
|
128
128
|
* @returns {Promise<SingleResult | ListResult | GetByIdResult>}
|
|
129
129
|
*/
|
|
130
130
|
get(name: string, query?: string | Record<string, any>): Promise<SingleResult | ListResult | GetByIdResult>;
|
|
131
|
-
/**
|
|
132
|
-
* Number of rows that match `query` (or total if omitted).
|
|
133
|
-
*
|
|
134
|
-
* @param {string} name
|
|
135
|
-
* @param {Record<string, any>} [query]
|
|
136
|
-
* @returns {Promise<{ data: number }>}
|
|
137
|
-
*/
|
|
138
|
-
count(name: string, query?: Record<string, any>): Promise<{
|
|
139
|
-
data: number;
|
|
140
|
-
}>;
|
|
141
131
|
/**
|
|
142
132
|
* Live snapshot stream — re-emits the latest `get()` result on every
|
|
143
133
|
* underlying mutation. Destroy the stream to stop watching.
|