@cero-base/core 1.19.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 +9 -6
- package/src/blobs/index.js +15 -15
- package/src/blobs/server.js +0 -2
- package/src/database/changes.js +4 -8
- package/src/database/dispatch.js +296 -227
- package/src/database/encryption.js +22 -52
- package/src/database/envelope.js +1 -3
- package/src/database/index.js +406 -476
- package/src/database/rotation.js +56 -70
- package/src/identity/index.js +5 -9
- package/src/index.js +5 -9
- package/src/lib/constants.js +3 -12
- package/src/lib/errors.js +6 -10
- package/src/lib/schema.js +2 -5
- package/src/lib/utils.js +9 -24
- package/src/network/index.js +144 -167
- package/src/network/presence.js +131 -0
- package/src/pairing/candidate.js +2 -4
- package/src/pairing/index.js +41 -55
- package/src/pairing/invite.js +3 -4
- package/src/pairing/request.js +3 -9
- package/src/rpc/index.js +4 -14
- package/src/rpc/peer.js +3 -7
- package/src/storage/index.js +29 -50
- package/types/blobs/index.d.ts +5 -5
- package/types/blobs/server.d.ts +0 -2
- package/types/database/changes.d.ts +2 -5
- package/types/database/dispatch.d.ts +11 -7
- package/types/database/encryption.d.ts +8 -20
- package/types/database/index.d.ts +164 -141
- package/types/database/rotation.d.ts +14 -20
- package/types/identity/index.d.ts +5 -9
- package/types/index.d.ts +4 -3
- package/types/lib/errors.d.ts +6 -10
- package/types/lib/schema.d.ts +2 -5
- package/types/lib/utils.d.ts +3 -7
- package/types/network/index.d.ts +28 -27
- package/types/network/presence.d.ts +50 -0
- package/types/pairing/candidate.d.ts +1 -2
- package/types/pairing/index.d.ts +22 -30
- package/types/pairing/invite.d.ts +3 -3
- package/types/pairing/request.d.ts +1 -3
- package/types/rpc/index.d.ts +1 -4
- package/types/rpc/peer.d.ts +2 -4
- package/types/storage/index.d.ts +17 -30
package/src/database/rotation.js
CHANGED
|
@@ -10,10 +10,8 @@ import { Identity } from '../identity/index.js'
|
|
|
10
10
|
import { CeroError } from '../lib/errors.js'
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Epoch key rotation for a database: mint a secret, seal it to every member,
|
|
14
|
-
*
|
|
15
|
-
* to it. A debounced audit re-keys when the current epoch's recipients drift
|
|
16
|
-
* from the member set.
|
|
13
|
+
* Epoch key rotation for a database: mint a secret, seal it to every member, announce it
|
|
14
|
+
* through the log, and on every peer learn the secrets addressed to it.
|
|
17
15
|
*/
|
|
18
16
|
export class Rotation {
|
|
19
17
|
/** @param {import('./index.js').Database} db */
|
|
@@ -34,6 +32,56 @@ export class Rotation {
|
|
|
34
32
|
return this._rotate(false)
|
|
35
33
|
}
|
|
36
34
|
|
|
35
|
+
/**
|
|
36
|
+
* An applied rotation announcement: open our envelope, learn the secret, persist it.
|
|
37
|
+
*
|
|
38
|
+
* @param {{ epoch: number, stamp: number, wrapped: Uint8Array, commit: Uint8Array }} row
|
|
39
|
+
*/
|
|
40
|
+
async learn(row) {
|
|
41
|
+
const { keyring } = this.db
|
|
42
|
+
const known = keyring.entropy(row.stamp)
|
|
43
|
+
const entropy = known || this._unseal(row)
|
|
44
|
+
if (known) {
|
|
45
|
+
keyring.add(row.stamp, known, row.epoch) // re-applied, the sequence may be news
|
|
46
|
+
} else if (this.current && entropy && b4a.equals(entropy, this.current.entropy)) {
|
|
47
|
+
this.current.epoch = row.epoch
|
|
48
|
+
} else if (entropy) {
|
|
49
|
+
keyring.add(row.stamp, entropy, row.epoch)
|
|
50
|
+
try {
|
|
51
|
+
await this._save()
|
|
52
|
+
} catch (err) {
|
|
53
|
+
// an epoch we cannot reload after a restart must not be used for writes
|
|
54
|
+
keyring.remove(row.stamp)
|
|
55
|
+
this.db._onerror(err)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
this.heal()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Safety net after boot: an epoch applied but never saved, e.g. a crash between a
|
|
63
|
+
* rotation's append and its save, is learned from the epochs collection.
|
|
64
|
+
*/
|
|
65
|
+
async hydrate() {
|
|
66
|
+
try {
|
|
67
|
+
for (const row of await this._epochs()) {
|
|
68
|
+
if (!this.db.keyring.entropy(row.stamp)) await this.learn(row)
|
|
69
|
+
}
|
|
70
|
+
} catch {
|
|
71
|
+
// rows past our newest epoch hydrate via apply
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Debounced audit, run after every update. */
|
|
76
|
+
heal() {
|
|
77
|
+
const db = this.db
|
|
78
|
+
if (this._timer || db.closing || db.closed) return
|
|
79
|
+
this._timer = setTimeout(() => {
|
|
80
|
+
this._timer = null
|
|
81
|
+
this._audit().catch(db._onerror)
|
|
82
|
+
}, 500)
|
|
83
|
+
}
|
|
84
|
+
|
|
37
85
|
async _rotate(retried) {
|
|
38
86
|
const db = this.db
|
|
39
87
|
db.guard()
|
|
@@ -43,9 +91,7 @@ export class Rotation {
|
|
|
43
91
|
if (db.bee.local.manifest?.version < 2) {
|
|
44
92
|
throw CeroError.INVALID('rotation requires manifest v2 cores')
|
|
45
93
|
}
|
|
46
|
-
// claimed synchronously
|
|
47
|
-
// keyring learns the secret only after the announcement flushed: blocks are
|
|
48
|
-
// encrypted at drain time, so advancing it now seals the announcement to itself
|
|
94
|
+
// claimed synchronously; the keyring learns the secret only after the announcement flushed
|
|
49
95
|
const entropy = Identity.randomBytes(32)
|
|
50
96
|
const stamp = this._mint()
|
|
51
97
|
this.current = { entropy, stamp, epoch: 0 }
|
|
@@ -90,7 +136,6 @@ export class Rotation {
|
|
|
90
136
|
})
|
|
91
137
|
}
|
|
92
138
|
|
|
93
|
-
// append the announcement under the current epoch and wait for its apply;
|
|
94
139
|
// resolves the sequence apply assigned, 0 if it never applied
|
|
95
140
|
async _announce(stamp, entropy, wrapped) {
|
|
96
141
|
const { bee } = this.db
|
|
@@ -128,37 +173,7 @@ export class Rotation {
|
|
|
128
173
|
return this.db.view.find(`@${this.db.ns}/epochs`, {}).toArray()
|
|
129
174
|
}
|
|
130
175
|
|
|
131
|
-
|
|
132
|
-
* An applied rotation announcement: open our envelope, learn the secret,
|
|
133
|
-
* persist it. Removed members have no envelope and stop here, every block
|
|
134
|
-
* after the announcement is noise to them. Our own in-flight rotation is left
|
|
135
|
-
* to `rotate()`, which adds the secret once the announcement flushed.
|
|
136
|
-
*
|
|
137
|
-
* @param {{ epoch: number, stamp: number, wrapped: Uint8Array, commit: Uint8Array }} row
|
|
138
|
-
*/
|
|
139
|
-
async learn(row) {
|
|
140
|
-
const { keyring } = this.db
|
|
141
|
-
const known = keyring.entropy(row.stamp)
|
|
142
|
-
const entropy = known || this._unseal(row)
|
|
143
|
-
if (known) {
|
|
144
|
-
keyring.add(row.stamp, known, row.epoch) // re-applied, the sequence may be news
|
|
145
|
-
} else if (this.current && entropy && b4a.equals(entropy, this.current.entropy)) {
|
|
146
|
-
this.current.epoch = row.epoch
|
|
147
|
-
} else if (entropy) {
|
|
148
|
-
keyring.add(row.stamp, entropy, row.epoch)
|
|
149
|
-
try {
|
|
150
|
-
await this._save()
|
|
151
|
-
} catch (err) {
|
|
152
|
-
// an epoch we cannot reload after a restart must not be used for writes
|
|
153
|
-
keyring.remove(row.stamp)
|
|
154
|
-
this.db._onerror(err)
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
this.heal()
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// our envelope's secret, or null. A bad envelope ahead of a good one must not
|
|
161
|
-
// lock us out, so every one addressed to us is tried
|
|
176
|
+
// every envelope addressed to us is tried, a bad one must not lock us out
|
|
162
177
|
_unseal(row) {
|
|
163
178
|
const { identity } = this.db
|
|
164
179
|
for (const w of c.decode(wraps, row.wrapped)) {
|
|
@@ -172,43 +187,14 @@ export class Rotation {
|
|
|
172
187
|
return null
|
|
173
188
|
}
|
|
174
189
|
|
|
175
|
-
/**
|
|
176
|
-
* Safety net after boot: an epoch applied but never saved, e.g. a crash
|
|
177
|
-
* between a rotation's append and its save, is learned from the epochs
|
|
178
|
-
* collection.
|
|
179
|
-
*/
|
|
180
|
-
async hydrate() {
|
|
181
|
-
try {
|
|
182
|
-
for (const row of await this._epochs()) {
|
|
183
|
-
if (!this.db.keyring.entropy(row.stamp)) await this.learn(row)
|
|
184
|
-
}
|
|
185
|
-
} catch {
|
|
186
|
-
// pre-rotation spec, or rows past our newest epoch, which hydrate via apply
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
190
|
// known epoch secrets to local userData (device-only, never replicates)
|
|
191
191
|
_save() {
|
|
192
192
|
const entries = c.encode(epochEntries, this.db.keyring.all())
|
|
193
193
|
return this.db.bee.local.setUserData('cero/epochs', entries)
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const db = this.db
|
|
199
|
-
if (this._timer || db.closing || db.closed) return
|
|
200
|
-
this._timer = setTimeout(() => {
|
|
201
|
-
this._timer = null
|
|
202
|
-
this._audit().catch(db._onerror)
|
|
203
|
-
}, 500)
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// Convergence rotation: a rotation seals for the rotator's possibly stale
|
|
207
|
-
// view of the members, so after linearization the current epoch may include
|
|
208
|
-
// a since-removed member (who still holds the key) or miss a since-added one
|
|
209
|
-
// (who is locked out). Every REMOVE-capable writable device compares the
|
|
210
|
-
// current epoch's recipients with the members and re-keys on mismatch. Only
|
|
211
|
-
// databases that rotated at least once are audited, rotation stays opt-in
|
|
196
|
+
// a rotation seals for the rotator's view of the members; REMOVE-capable devices re-key
|
|
197
|
+
// when the current epoch's recipients drift from the member list
|
|
212
198
|
async _audit() {
|
|
213
199
|
const db = this.db
|
|
214
200
|
if (db.closing || db.closed || !db.writable || this.current || !db.keyring.current) return
|
package/src/identity/index.js
CHANGED
|
@@ -14,9 +14,7 @@ const DERIVE_PATH = ['SLIP-0021', 'cero', 'v1', 'identity']
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* Long-lived user identity derived from a BIP-39 seed phrase.
|
|
18
|
-
* sign keypair, a symmetric encryption key and the canonical id/topic used
|
|
19
|
-
* by every other resource to identify this user.
|
|
17
|
+
* Long-lived user identity derived from a BIP-39 seed phrase.
|
|
20
18
|
*/
|
|
21
19
|
export class Identity {
|
|
22
20
|
/** @param {IdentityFields} keys */
|
|
@@ -90,9 +88,8 @@ export class Identity {
|
|
|
90
88
|
}
|
|
91
89
|
|
|
92
90
|
/**
|
|
93
|
-
* Open a sealed box addressed to this identity (any device holding the
|
|
94
|
-
*
|
|
95
|
-
* or is corrupt — callers treat that as "not addressed to me".
|
|
91
|
+
* Open a sealed box addressed to this identity (any device holding the seed-derived
|
|
92
|
+
* keypair can open it).
|
|
96
93
|
*
|
|
97
94
|
* @param {Uint8Array} sealed
|
|
98
95
|
* @returns {Uint8Array | null}
|
|
@@ -223,9 +220,8 @@ export class Identity {
|
|
|
223
220
|
}
|
|
224
221
|
|
|
225
222
|
/**
|
|
226
|
-
* Seal a message to an identity's Ed25519 public key (sealed box over the
|
|
227
|
-
*
|
|
228
|
-
* i.e. any of that identity's devices — can open it.
|
|
223
|
+
* Seal a message to an identity's Ed25519 public key (sealed box over the curve25519
|
|
224
|
+
* conversion).
|
|
229
225
|
*
|
|
230
226
|
* @param {Uint8Array} publicKey
|
|
231
227
|
* @param {Uint8Array} message
|
package/src/index.js
CHANGED
|
@@ -8,17 +8,13 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
export * from './identity/index.js'
|
|
11
|
-
//
|
|
12
|
-
// result/option typedefs (`Ref`, `SingleResult`, `ListResult`,
|
|
13
|
-
// `CreateInviteOpts`) that also exist on `storage`/`pairing`. Re-export the
|
|
14
|
-
// lower-level modules' colliding names under namespaced aliases so the barrel
|
|
15
|
-
// stays unambiguous while the canonical (`database`/`pairing`) versions own the
|
|
16
|
-
// bare names.
|
|
11
|
+
// colliding typedef names from storage and pairing are re-exported under aliases
|
|
17
12
|
export { Storage } from './storage/index.js'
|
|
18
13
|
/**
|
|
19
|
-
* `Ref` / `SingleResult` / `ListResult` are JSDoc typedefs (types, not runtime exports),
|
|
20
|
-
* are re-exported as namespaced type aliases — `export { Ref as StorageRef }`
|
|
21
|
-
*
|
|
14
|
+
* `Ref` / `SingleResult` / `ListResult` are JSDoc typedefs (types, not runtime exports),
|
|
15
|
+
* so they are re-exported as namespaced type aliases — `export { Ref as StorageRef }`
|
|
16
|
+
* fails at import because the.
|
|
17
|
+
*
|
|
22
18
|
* @typedef {import('./storage/index.js').Ref} StorageRef
|
|
23
19
|
* @typedef {import('./storage/index.js').SingleResult} StorageSingleResult
|
|
24
20
|
* @typedef {import('./storage/index.js').ListResult} StorageListResult
|
package/src/lib/constants.js
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
|
-
// Schema kinds
|
|
2
1
|
export const SINGLE = 'single'
|
|
3
2
|
export const COLLECTION = 'collection'
|
|
4
3
|
export const HANDLE = 'handle'
|
|
5
4
|
export const ACTION = 'action'
|
|
6
5
|
|
|
7
|
-
// Network topic modes
|
|
8
6
|
export const ACTIVE = 'active'
|
|
9
7
|
export const PASSIVE = 'passive'
|
|
10
8
|
|
|
11
|
-
// Storage backends
|
|
12
9
|
export const ROCKS = 'rocks'
|
|
13
10
|
export const BEE = 'bee'
|
|
14
11
|
|
|
15
|
-
// Roles
|
|
16
12
|
export const OWNER = 'owner'
|
|
17
13
|
export const ADMIN = 'admin'
|
|
18
14
|
export const MEMBER = 'member'
|
|
19
15
|
export const READER = 'reader'
|
|
20
16
|
|
|
21
|
-
// Permissions
|
|
22
17
|
export const READ = 'read'
|
|
23
18
|
export const WRITE = 'write'
|
|
24
19
|
export const DELETE = 'delete'
|
|
@@ -26,7 +21,6 @@ export const INVITE = 'invite'
|
|
|
26
21
|
export const REMOVE = 'remove'
|
|
27
22
|
export const ASSIGN = 'assign'
|
|
28
23
|
|
|
29
|
-
// Default role → permissions
|
|
30
24
|
export const ROLE_PERMS = {
|
|
31
25
|
[OWNER]: ['*'],
|
|
32
26
|
[ADMIN]: [READ, WRITE, DELETE, INVITE, REMOVE, ASSIGN],
|
|
@@ -34,14 +28,11 @@ export const ROLE_PERMS = {
|
|
|
34
28
|
[READER]: [READ]
|
|
35
29
|
}
|
|
36
30
|
|
|
37
|
-
// Role ordering (higher = more authority)
|
|
38
31
|
export const RANK = { [OWNER]: 3, [ADMIN]: 2, [MEMBER]: 1, [READER]: 0 }
|
|
39
32
|
|
|
40
|
-
// Identity / hypercore namespace
|
|
41
33
|
export const NAMESPACE = 'cero'
|
|
42
34
|
|
|
43
|
-
//
|
|
44
|
-
// One set shared by both backends (Database and Storage) so they cannot drift.
|
|
35
|
+
// everything else in a query is an equality field; shared so both backends agree
|
|
45
36
|
export const QUERY_RESERVED = new Set([
|
|
46
37
|
'gt',
|
|
47
38
|
'gte',
|
|
@@ -54,7 +45,7 @@ export const QUERY_RESERVED = new Set([
|
|
|
54
45
|
'total'
|
|
55
46
|
])
|
|
56
47
|
|
|
57
|
-
//
|
|
48
|
+
// monotonic index per collection
|
|
58
49
|
export const COUNTERS = 'counters'
|
|
59
|
-
//
|
|
50
|
+
// rotation announcements
|
|
60
51
|
export const EPOCHS = 'epochs'
|
package/src/lib/errors.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Canonical error class. Every instance carries a stable `code` field which
|
|
3
|
-
*
|
|
4
|
-
* factories produce errors with the right code + a sensible message.
|
|
2
|
+
* Canonical error class. Every instance carries a stable `code` field which is also
|
|
3
|
+
* prefixed to the message, so logs stay self-identifying.
|
|
5
4
|
*/
|
|
6
5
|
export class CeroError extends Error {
|
|
7
6
|
/**
|
|
@@ -56,9 +55,7 @@ export class CeroError extends Error {
|
|
|
56
55
|
return new CeroError('CLOSED', `${resource} is closed`)
|
|
57
56
|
}
|
|
58
57
|
/**
|
|
59
|
-
* A channel-stamped storage was opened under a different channel.
|
|
60
|
-
* does NOT name the stored channel — that label gates the network, so leaking it
|
|
61
|
-
* in an error would hint at a realm the caller isn't on.
|
|
58
|
+
* A channel-stamped storage was opened under a different channel.
|
|
62
59
|
*/
|
|
63
60
|
static CHANNEL_MISMATCH() {
|
|
64
61
|
return new CeroError('CHANNEL_MISMATCH', 'storage belongs to a different channel')
|
|
@@ -148,10 +145,9 @@ export class CeroError extends Error {
|
|
|
148
145
|
return new CeroError('DENIED', msg, { reason })
|
|
149
146
|
}
|
|
150
147
|
/**
|
|
151
|
-
* An apply-time rule refused the op. Every peer reaches this verdict from the
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
* anything that could differ between peers must not abort, or they fork.
|
|
148
|
+
* An apply-time rule refused the op. Every peer reaches this verdict from the same node
|
|
149
|
+
* and the same view, so it aborts the writer's whole batch — which is what makes a
|
|
150
|
+
* transaction atomic.
|
|
155
151
|
*
|
|
156
152
|
* @param {string} rule Which rule refused, e.g. 'write' or 'own'.
|
|
157
153
|
* @param {string} [msg]
|
package/src/lib/schema.js
CHANGED
|
@@ -46,11 +46,8 @@ export const t = {
|
|
|
46
46
|
},
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
|
-
* Multi-row table keyed by `id`.
|
|
50
|
-
*
|
|
51
|
-
* `own` restricts a row to the member who wrote it — anyone may add rows, but
|
|
52
|
-
* only the author changes or deletes theirs. Members holding REMOVE can still
|
|
53
|
-
* moderate. Without it a writer may edit any row in the collection.
|
|
49
|
+
* Multi-row table keyed by `id`. `own` restricts a row to the member who wrote it — anyone
|
|
50
|
+
* may add rows, but only the author changes or deletes theirs.
|
|
54
51
|
*
|
|
55
52
|
* @param {Record<string, Prim>} fields
|
|
56
53
|
* @param {{ indexes?: Record<string, string[]>, own?: boolean }} [opts]
|
package/src/lib/utils.js
CHANGED
|
@@ -5,8 +5,7 @@ import z32 from 'z32'
|
|
|
5
5
|
|
|
6
6
|
import { ROLE_PERMS, RANK, QUERY_RESERVED } from './constants.js'
|
|
7
7
|
|
|
8
|
-
// the capability names
|
|
9
|
-
// without them, and constants.js is not a public subpath
|
|
8
|
+
// can() callers need the capability names, and constants.js is not public
|
|
10
9
|
export { WRITE, INVITE, ASSIGN, REMOVE } from './constants.js'
|
|
11
10
|
|
|
12
11
|
/**
|
|
@@ -18,9 +17,7 @@ export function genId() {
|
|
|
18
17
|
return z32.encode(crypto.randomBytes(16))
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
//
|
|
22
|
-
// key makes an admission unreplayable across rooms; the tag pins the format.
|
|
23
|
-
// v2 — pre-1.2 signatures (writer‖appender / bare writer) no longer verify.
|
|
20
|
+
// binding the db key makes an admission unreplayable across rooms
|
|
24
21
|
const ADD_WRITER_TAG = b4a.from('cero/add-writer/v2')
|
|
25
22
|
const CLAIM_WRITER_TAG = b4a.from('cero/claim-writer/v2')
|
|
26
23
|
|
|
@@ -46,9 +43,7 @@ export function can(role, perm) {
|
|
|
46
43
|
return !!perms && (perms.includes('*') || perms.includes(perm))
|
|
47
44
|
}
|
|
48
45
|
|
|
49
|
-
//
|
|
50
|
-
// nothing, so every gate built on `grants` silently refuses it. Callers that
|
|
51
|
-
// accept a role from an app or a peer must reject it loudly instead.
|
|
46
|
+
// an unknown role grants nothing; callers taking a role from an app must check it
|
|
52
47
|
export function isRank(role) {
|
|
53
48
|
return RANK[role] != null
|
|
54
49
|
}
|
|
@@ -61,9 +56,8 @@ export function outranks(a, b) {
|
|
|
61
56
|
}
|
|
62
57
|
|
|
63
58
|
/**
|
|
64
|
-
* Stream-of-snapshots primitive. Couples a `get` (returns the latest value)
|
|
65
|
-
*
|
|
66
|
-
* time `watch` ticks.
|
|
59
|
+
* Stream-of-snapshots primitive. Couples a `get` (returns the latest value) with a `watch`
|
|
60
|
+
* (re-fires on change) and emits the newest snapshot every time `watch` ticks.
|
|
67
61
|
*
|
|
68
62
|
* @template T
|
|
69
63
|
* @param {object} args
|
|
@@ -87,10 +81,7 @@ export function subscribe({ get, watch }) {
|
|
|
87
81
|
stream.push(data)
|
|
88
82
|
}
|
|
89
83
|
|
|
90
|
-
//
|
|
91
|
-
// dirty and fold into a single trailing re-read — a write burst costs one
|
|
92
|
-
// extra snapshot instead of one concurrent scan per tick, and only the
|
|
93
|
-
// latest state is emitted.
|
|
84
|
+
// one get() in flight; ticks mid-read fold into a single trailing re-read
|
|
94
85
|
let running = false
|
|
95
86
|
let dirty = false
|
|
96
87
|
const push = async () => {
|
|
@@ -119,7 +110,6 @@ export function subscribe({ get, watch }) {
|
|
|
119
110
|
return stream
|
|
120
111
|
}
|
|
121
112
|
|
|
122
|
-
// Lowercase, fold diacritics ('jose' → 'José'), drop whitespace.
|
|
123
113
|
const fold = (s) =>
|
|
124
114
|
s
|
|
125
115
|
.normalize('NFD')
|
|
@@ -127,12 +117,10 @@ const fold = (s) =>
|
|
|
127
117
|
.toLowerCase()
|
|
128
118
|
.replace(/\s/g, '')
|
|
129
119
|
|
|
130
|
-
//
|
|
131
|
-
// every string field). 'smith' → 'John Smith', 'jo sm' → both must appear.
|
|
120
|
+
// every term must be a substring of a searched field
|
|
132
121
|
export function searchHit(row, term, fields) {
|
|
133
122
|
const terms = String(term).split(/\s+/).map(fold).filter(Boolean)
|
|
134
|
-
//
|
|
135
|
-
// otherwise produce spurious, non-deterministic search hits (user-set id/code stay searchable)
|
|
123
|
+
// memberId is a random z32, it would produce spurious hits
|
|
136
124
|
const keys = fields && fields.length ? fields : Object.keys(row).filter((k) => k !== 'memberId')
|
|
137
125
|
const hay = keys.map((k) => (typeof row[k] === 'string' ? fold(row[k]) : '')).join(' ')
|
|
138
126
|
return terms.every((t) => hay.includes(t))
|
|
@@ -169,10 +157,7 @@ function valueEq(a, b) {
|
|
|
169
157
|
}
|
|
170
158
|
|
|
171
159
|
/**
|
|
172
|
-
* Run `cb` when `signal` aborts — or immediately if it already has.
|
|
173
|
-
* without a signal. The listener removes itself on fire. Returns a disposer that
|
|
174
|
-
* detaches the listener early, so a manual unsubscribe doesn't leave it lingering
|
|
175
|
-
* on a long-lived signal.
|
|
160
|
+
* Run `cb` when `signal` aborts — or immediately if it already has.
|
|
176
161
|
*
|
|
177
162
|
* @param {AbortSignal | undefined} signal
|
|
178
163
|
* @param {() => void} cb
|