@cero-base/cero 2.4.1 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +25 -11
- package/src/build/internal.js +4 -2
- package/src/build/schemas.js +15 -0
- package/src/handle/index.js +68 -3
- package/src/lib/operators.js +13 -0
- package/src/push/firebase.js +23 -0
- package/src/push/index.js +67 -0
- package/src/rpc/client.js +16 -1
- package/src/rpc/push.js +57 -0
- package/src/rpc/server.js +4 -2
- package/types/build/internal.d.ts +3 -0
- package/types/build/schemas.d.ts +13 -0
- package/types/extensions/index.d.ts +6 -0
- package/types/handle/index.d.ts +10 -1
- package/types/lib/operators.d.ts +14 -0
- package/types/push/firebase.d.ts +12 -0
- package/types/push/index.d.ts +28 -0
- package/types/rpc/client.d.ts +12 -1
- package/types/rpc/push.d.ts +20 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/cero",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "The ideal p2p API — everything is a handle, handles contain refs, refs contain rows.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -44,6 +44,14 @@
|
|
|
44
44
|
"./extensions": {
|
|
45
45
|
"types": "./types/extensions/index.d.ts",
|
|
46
46
|
"default": "./src/extensions/index.js"
|
|
47
|
+
},
|
|
48
|
+
"./push": {
|
|
49
|
+
"types": "./types/push/index.d.ts",
|
|
50
|
+
"default": "./src/push/index.js"
|
|
51
|
+
},
|
|
52
|
+
"./push/firebase": {
|
|
53
|
+
"types": "./types/push/firebase.d.ts",
|
|
54
|
+
"default": "./src/push/firebase.js"
|
|
47
55
|
}
|
|
48
56
|
},
|
|
49
57
|
"typesVersions": {
|
|
@@ -59,6 +67,12 @@
|
|
|
59
67
|
],
|
|
60
68
|
"extensions": [
|
|
61
69
|
"types/extensions/index.d.ts"
|
|
70
|
+
],
|
|
71
|
+
"push": [
|
|
72
|
+
"types/push/index.d.ts"
|
|
73
|
+
],
|
|
74
|
+
"push/firebase": [
|
|
75
|
+
"types/push/firebase.d.ts"
|
|
62
76
|
]
|
|
63
77
|
}
|
|
64
78
|
},
|
|
@@ -99,24 +113,24 @@
|
|
|
99
113
|
"test:node": "find test -name '*.test.js' | sort | xargs -P4 -n1 brittle-node"
|
|
100
114
|
},
|
|
101
115
|
"dependencies": {
|
|
102
|
-
"@cero-base/core": "^2.
|
|
116
|
+
"@cero-base/core": "^2.5.0",
|
|
103
117
|
"b4a": "^1.9.0",
|
|
104
118
|
"bare-abort-controller": "^1.1.2",
|
|
105
119
|
"bare-crypto": "^1.15.4",
|
|
106
|
-
"bare-fs": "^4.8.
|
|
120
|
+
"bare-fs": "^4.8.4",
|
|
107
121
|
"bare-path": "^3.1.2",
|
|
108
122
|
"ble-swarm": "^2.3.4",
|
|
109
123
|
"compact-encoding": "^3.5.2",
|
|
110
|
-
"corestore": "^7.
|
|
124
|
+
"corestore": "^7.16.1",
|
|
111
125
|
"hrpc": "^4.3.1",
|
|
112
|
-
"hypercore": "^11.
|
|
126
|
+
"hypercore": "^11.39.3",
|
|
113
127
|
"hypercore-crypto": "^3.7.0",
|
|
114
128
|
"hypercore-id-encoding": "^1.3.0",
|
|
115
129
|
"hypercore-storage": "^3.3.1",
|
|
116
|
-
"hyperdb": "^6.
|
|
130
|
+
"hyperdb": "^6.11.0",
|
|
117
131
|
"hyperdispatch": "^1.6.0",
|
|
118
132
|
"hyperschema": "^1.26.2",
|
|
119
|
-
"protomux": "^3.
|
|
133
|
+
"protomux": "^3.13.0",
|
|
120
134
|
"ready-resource": "^1.2.0",
|
|
121
135
|
"safety-catch": "^1.0.3",
|
|
122
136
|
"streamx": "^2.28.1",
|
|
@@ -126,11 +140,11 @@
|
|
|
126
140
|
"devDependencies": {
|
|
127
141
|
"@hyperswarm/testnet": "^3.1.4",
|
|
128
142
|
"bare-events": "^2.9.2",
|
|
129
|
-
"bare-fetch": "^3.4.
|
|
143
|
+
"bare-fetch": "^3.4.1",
|
|
130
144
|
"bare-process": "^4.5.1",
|
|
131
|
-
"bare-url": "^2.5.
|
|
132
|
-
"blind-peer": "^3.
|
|
133
|
-
"brittle": "^4.
|
|
145
|
+
"bare-url": "^2.5.5",
|
|
146
|
+
"blind-peer": "^3.16.0",
|
|
147
|
+
"brittle": "^4.5.0",
|
|
134
148
|
"typescript": "^7.0.2"
|
|
135
149
|
},
|
|
136
150
|
"license": "Apache-2.0"
|
package/src/build/internal.js
CHANGED
|
@@ -20,7 +20,8 @@ export const defs = {
|
|
|
20
20
|
inbox: { type: 'mail' },
|
|
21
21
|
outbox: { type: 'mail' },
|
|
22
22
|
environment: { type: 'environment', kind: 'single' },
|
|
23
|
-
serving: { type: 'serving' }
|
|
23
|
+
serving: { type: 'serving' },
|
|
24
|
+
topics: { type: 'subscription' }
|
|
24
25
|
}
|
|
25
26
|
}
|
|
26
27
|
|
|
@@ -28,7 +29,8 @@ export const defs = {
|
|
|
28
29
|
const LIVE = {
|
|
29
30
|
status: { type: 'status', kind: 'single' },
|
|
30
31
|
joins: { type: 'joining', root: true },
|
|
31
|
-
nearby: { type: 'peer', root: true }
|
|
32
|
+
nearby: { type: 'peer', root: true },
|
|
33
|
+
topics: { type: 'topic', root: true }
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
/**
|
package/src/build/schemas.js
CHANGED
|
@@ -145,6 +145,13 @@ export const main = {
|
|
|
145
145
|
deny: {
|
|
146
146
|
id: required(string),
|
|
147
147
|
reason: string
|
|
148
|
+
},
|
|
149
|
+
// a handle's push topic: what a device subscribes to for pushes about it
|
|
150
|
+
topic: {
|
|
151
|
+
id: required(string),
|
|
152
|
+
handle: required(string),
|
|
153
|
+
type: required(string),
|
|
154
|
+
name: string
|
|
148
155
|
}
|
|
149
156
|
}
|
|
150
157
|
|
|
@@ -192,6 +199,14 @@ export const local = {
|
|
|
192
199
|
},
|
|
193
200
|
environment: {
|
|
194
201
|
channel: required(string)
|
|
202
|
+
},
|
|
203
|
+
// the handles rows a cold push process can read without the root: topic → handle and its key
|
|
204
|
+
subscription: {
|
|
205
|
+
id: required(string),
|
|
206
|
+
handle: required(string),
|
|
207
|
+
type: required(string),
|
|
208
|
+
key: required(bytes),
|
|
209
|
+
name: string
|
|
195
210
|
}
|
|
196
211
|
}
|
|
197
212
|
|
package/src/handle/index.js
CHANGED
|
@@ -10,6 +10,7 @@ import { Identity } from '@cero-base/core/identity'
|
|
|
10
10
|
import { Database } from '@cero-base/core/database'
|
|
11
11
|
import { blobEpochKey } from '@cero-base/core/database/encryption'
|
|
12
12
|
import { Pairing } from '@cero-base/core/pairing'
|
|
13
|
+
import { receive } from '@cero-base/core/push'
|
|
13
14
|
import { Invite } from '@cero-base/core/invite'
|
|
14
15
|
import { Mailbox } from '@cero-base/core/mailbox'
|
|
15
16
|
import hid from 'hypercore-id-encoding'
|
|
@@ -156,6 +157,8 @@ export class Handle extends ReadyResource {
|
|
|
156
157
|
namespace: opts.namespace,
|
|
157
158
|
keyPair: opts.keyPair,
|
|
158
159
|
pinned: !parent,
|
|
160
|
+
// the root has no topic to push to
|
|
161
|
+
push: !!parent,
|
|
159
162
|
onerror: this._onerror
|
|
160
163
|
})
|
|
161
164
|
/** @private */
|
|
@@ -170,8 +173,9 @@ export class Handle extends ReadyResource {
|
|
|
170
173
|
this._live = {
|
|
171
174
|
status: { get: () => this._status(), watch: (fn) => this._onStatus(fn) },
|
|
172
175
|
...(!this.parent && {
|
|
173
|
-
joins: { get: (q) => this._joins(q), watch: (fn) => this.
|
|
174
|
-
nearby: { get: () => this._peers(), watch: (fn) => this._onRadio(fn) }
|
|
176
|
+
joins: { get: (q) => this._joins(q), watch: (fn) => this._onLocal(fn) },
|
|
177
|
+
nearby: { get: () => this._peers(), watch: (fn) => this._onRadio(fn) },
|
|
178
|
+
topics: { get: (q) => this._topics(q), watch: (fn) => this._onLocal(fn) }
|
|
175
179
|
})
|
|
176
180
|
}
|
|
177
181
|
}
|
|
@@ -278,6 +282,10 @@ export class Handle extends ReadyResource {
|
|
|
278
282
|
}
|
|
279
283
|
await this._carryOn().catch(this._onerror)
|
|
280
284
|
this._reserve().catch(this._onerror)
|
|
285
|
+
// the handles rows, keyed by push topic, for a cold push process that cannot read the root
|
|
286
|
+
const off = this.store.onUpdate('handles', () => this._index().catch(this._onerror))
|
|
287
|
+
this.once('close', off)
|
|
288
|
+
await this._index()
|
|
281
289
|
}
|
|
282
290
|
}
|
|
283
291
|
|
|
@@ -445,6 +453,7 @@ export class Handle extends ReadyResource {
|
|
|
445
453
|
behind: this.store.behind || 0,
|
|
446
454
|
nearby: this.root._bluetooth?.state ?? null
|
|
447
455
|
}
|
|
456
|
+
if (this.parent) data.topic = b4a.toHex(this.store.discoveryKey)
|
|
448
457
|
return { data }
|
|
449
458
|
}
|
|
450
459
|
|
|
@@ -521,11 +530,56 @@ export class Handle extends ReadyResource {
|
|
|
521
530
|
}
|
|
522
531
|
|
|
523
532
|
/** @private */
|
|
524
|
-
|
|
533
|
+
_onLocal(fn) {
|
|
525
534
|
this.local.store.db.watch(fn)
|
|
526
535
|
return () => this.local.store.db.unwatch(fn)
|
|
527
536
|
}
|
|
528
537
|
|
|
538
|
+
/** @private */
|
|
539
|
+
async _topics(query = {}) {
|
|
540
|
+
const rows = (await this.local.store.get('topics')).data
|
|
541
|
+
const data = filter(
|
|
542
|
+
rows.map(({ id, handle, type, name }) => ({ id, handle, type, name })),
|
|
543
|
+
query
|
|
544
|
+
)
|
|
545
|
+
return { data, total: data.length, size: data.length }
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// one pass at a time: two overlapping could put back a row the later one deleted
|
|
549
|
+
/** @private */
|
|
550
|
+
_index() {
|
|
551
|
+
this._indexing = (this._indexing ?? Promise.resolve()).then(() => this._reindex())
|
|
552
|
+
return this._indexing
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/** @private */
|
|
556
|
+
async _reindex() {
|
|
557
|
+
if (this.closing || this.closed) return
|
|
558
|
+
const { data: handles } = await this.store.get('handles')
|
|
559
|
+
const { data: rows } = await this.local.store.get('topics')
|
|
560
|
+
const wanted = new Map(handles.map((h) => [b4a.toHex(discoveryKey(h.key)), h]))
|
|
561
|
+
const have = new Map(rows.map((row) => [row.id, row]))
|
|
562
|
+
for (const row of rows) {
|
|
563
|
+
if (!wanted.has(row.id)) await this.local.store.del('topics', row.id)
|
|
564
|
+
}
|
|
565
|
+
for (const [id, h] of wanted) {
|
|
566
|
+
const row = { id, handle: h.id, type: h.type, key: h.key, name: h.name ?? null }
|
|
567
|
+
if (same(have.get(id), row)) continue
|
|
568
|
+
await this.local.store.put('topics', row)
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// a push about one of this device's handles, verified against its own cores
|
|
573
|
+
/** @private */
|
|
574
|
+
async _receive(payload) {
|
|
575
|
+
// a suspended storage answers no read: the cold entry serves a push while the app sleeps
|
|
576
|
+
if (this._sus.suspended) throw CeroError.INVALID('receive: the app is suspended')
|
|
577
|
+
const { data: rows } = await this.local.store.get('topics')
|
|
578
|
+
const handles = rows.map(({ handle, key, type, name }) => ({ id: handle, key, type, name }))
|
|
579
|
+
const hit = await receive(this.store.store.storage, payload, handles)
|
|
580
|
+
return hit && { id: hit.id, type: hit.type, name: hit.name, newer: hit.newer }
|
|
581
|
+
}
|
|
582
|
+
|
|
529
583
|
/** @private */
|
|
530
584
|
async _phrase() {
|
|
531
585
|
return this.identity.toPhrase()
|
|
@@ -1072,6 +1126,17 @@ function randomNs() {
|
|
|
1072
1126
|
return z32.encode(Identity.randomBytes(8))
|
|
1073
1127
|
}
|
|
1074
1128
|
|
|
1129
|
+
// a topics row still describes its handle
|
|
1130
|
+
function same(row, next) {
|
|
1131
|
+
return (
|
|
1132
|
+
!!row &&
|
|
1133
|
+
row.handle === next.handle &&
|
|
1134
|
+
row.type === next.type &&
|
|
1135
|
+
(row.name ?? null) === next.name &&
|
|
1136
|
+
b4a.equals(row.key, next.key)
|
|
1137
|
+
)
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1075
1140
|
// run every teardown step, then surface the first failure: a bug must not leak the storage lock
|
|
1076
1141
|
async function settle(steps) {
|
|
1077
1142
|
let failed = null
|
package/src/lib/operators.js
CHANGED
|
@@ -484,6 +484,19 @@ export async function deactivate(ctx) {
|
|
|
484
484
|
return ctx._active(false)
|
|
485
485
|
}
|
|
486
486
|
|
|
487
|
+
/**
|
|
488
|
+
* A push the platform delivered, as bytes: the handle it is about, verified against this device's
|
|
489
|
+
* own data, or `null` for a forged one or one about no handle here.
|
|
490
|
+
*
|
|
491
|
+
* @param {Context} me
|
|
492
|
+
* @param {Uint8Array} payload
|
|
493
|
+
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean } | null>}
|
|
494
|
+
*/
|
|
495
|
+
export async function receive(me, payload) {
|
|
496
|
+
if (me.parent) throw CeroError.INVALID("receive is the app's, on me")
|
|
497
|
+
return me._receive(payload)
|
|
498
|
+
}
|
|
499
|
+
|
|
487
500
|
/**
|
|
488
501
|
* Reveal the recovery phrase.
|
|
489
502
|
*
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import b4a from 'b4a'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `@react-native-firebase/messaging` as a push backend: pass `messaging()`. Pushes to the app in
|
|
5
|
+
* front; the push worklet answers the ones the OS delivers while it is not.
|
|
6
|
+
*
|
|
7
|
+
* @param {{ subscribeToTopic: (topic: string) => Promise<void>, unsubscribeFromTopic: (topic: string) => Promise<void>, onMessage: (fn: (message: object) => void) => () => void }} messaging
|
|
8
|
+
* @returns {import('../rpc/push.js').Backend}
|
|
9
|
+
*/
|
|
10
|
+
export function firebase(messaging) {
|
|
11
|
+
const payload = (message) => message?.data?.payload && b4a.from(message.data.payload, 'base64')
|
|
12
|
+
return {
|
|
13
|
+
subscribe: (topic) => messaging.subscribeToTopic(topic),
|
|
14
|
+
unsubscribe: (topic) => messaging.unsubscribeFromTopic(topic),
|
|
15
|
+
on(event, fn) {
|
|
16
|
+
if (event !== 'push') return () => {}
|
|
17
|
+
return messaging.onMessage((message) => {
|
|
18
|
+
const bytes = payload(message)
|
|
19
|
+
if (bytes) fn(bytes)
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import HypercoreStorage from 'hypercore-storage'
|
|
2
|
+
import Corestore from 'corestore'
|
|
3
|
+
import b4a from 'b4a'
|
|
4
|
+
import safetyCatch from 'safety-catch'
|
|
5
|
+
|
|
6
|
+
import { receive as verify } from '@cero-base/core/push'
|
|
7
|
+
|
|
8
|
+
import { Local } from '../local/index.js'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A push the platform delivered while the app is dead, verified from its storage opened
|
|
12
|
+
* read-only. `null` while the live app holds the storage, which shows the activity itself, and
|
|
13
|
+
* for a push that is forged or about no handle here.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} dir The directory `cero()` opened.
|
|
16
|
+
* @param {import('../lib/spec.js').Spec} spec
|
|
17
|
+
* @param {Uint8Array} payload
|
|
18
|
+
* @param {{ storageKey?: Uint8Array }} [opts] The key `cero()` encrypts the local store with, if any.
|
|
19
|
+
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean } | null>}
|
|
20
|
+
*/
|
|
21
|
+
export async function receive(dir, spec, payload, { storageKey } = {}) {
|
|
22
|
+
const storage = new HypercoreStorage(`${dir}/main`, { readOnly: true })
|
|
23
|
+
let store = null
|
|
24
|
+
let local = null
|
|
25
|
+
try {
|
|
26
|
+
await storage.ready()
|
|
27
|
+
store = new Corestore(storage, { manifestVersion: 2 })
|
|
28
|
+
local = new Local(null, spec, { store, storageKey })
|
|
29
|
+
await local.ready()
|
|
30
|
+
const { data: rows } = await local.store.get('topics')
|
|
31
|
+
const handles = rows.map(({ handle, key, type, name }) => ({ id: handle, key, type, name }))
|
|
32
|
+
const hit = await verify(storage, payload, handles)
|
|
33
|
+
return hit && { id: hit.id, type: hit.type, name: hit.name, newer: hit.newer }
|
|
34
|
+
} catch (err) {
|
|
35
|
+
safetyCatch(err)
|
|
36
|
+
return null
|
|
37
|
+
} finally {
|
|
38
|
+
await local?.close().catch(safetyCatch)
|
|
39
|
+
await store?.close().catch(safetyCatch)
|
|
40
|
+
await storage.close().catch(safetyCatch)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The bare-kit push entry: on each push the OS hands this worklet, verify it and answer with the
|
|
46
|
+
* notification to show, or nothing. The request carries `storage`, `payload` (base64) and, when
|
|
47
|
+
* the app encrypts its local store, `storageKey` (base64).
|
|
48
|
+
*
|
|
49
|
+
* @param {import('../lib/spec.js').Spec} spec
|
|
50
|
+
* @returns {void}
|
|
51
|
+
*/
|
|
52
|
+
export function push(spec) {
|
|
53
|
+
globalThis.BareKit.on('push', async (json, reply) => {
|
|
54
|
+
let hit = null
|
|
55
|
+
try {
|
|
56
|
+
const { storage, storageKey, payload } = JSON.parse(json)
|
|
57
|
+
hit = await receive(storage, spec, b4a.from(payload, 'base64'), {
|
|
58
|
+
storageKey: storageKey ? b4a.from(storageKey, 'base64') : undefined
|
|
59
|
+
})
|
|
60
|
+
} catch {
|
|
61
|
+
// the OS waits for an answer: whatever went wrong, no notification
|
|
62
|
+
hit = null
|
|
63
|
+
}
|
|
64
|
+
const shown = hit?.newer ? { title: hit.name || hit.type, body: 'New activity' } : null
|
|
65
|
+
reply(null, shown && JSON.stringify(shown))
|
|
66
|
+
})
|
|
67
|
+
}
|
package/src/rpc/client.js
CHANGED
|
@@ -8,6 +8,7 @@ import z32 from 'z32'
|
|
|
8
8
|
import { decodeId } from '@cero-base/core/blobs/codec'
|
|
9
9
|
|
|
10
10
|
import { Ref } from '../lib/refs.js'
|
|
11
|
+
import { follow } from './push.js'
|
|
11
12
|
import * as verbs from '../lib/operators.js'
|
|
12
13
|
import { t, schema } from '../lib/spec.js'
|
|
13
14
|
|
|
@@ -389,6 +390,11 @@ const operators = {
|
|
|
389
390
|
return this._verb('phrase')
|
|
390
391
|
},
|
|
391
392
|
|
|
393
|
+
/** @private */
|
|
394
|
+
_receive(payload) {
|
|
395
|
+
return this._verb('receive', payload)
|
|
396
|
+
},
|
|
397
|
+
|
|
392
398
|
/** @private */
|
|
393
399
|
_nearby(on, opts) {
|
|
394
400
|
return this._verb('nearby', on, opts)
|
|
@@ -461,8 +467,10 @@ export class Client extends RPCClient {
|
|
|
461
467
|
* @param {Spec} spec Compiled cero spec (schema + rpc + handles).
|
|
462
468
|
* @param {{ onerror?: (err: Error) => void }} [opts] Where the worker's background errors go; the console without one.
|
|
463
469
|
*/
|
|
464
|
-
constructor(ipc, spec, { onerror } = {}) {
|
|
470
|
+
constructor(ipc, spec, { onerror, push } = {}) {
|
|
465
471
|
super(ipc, spec)
|
|
472
|
+
/** @private */
|
|
473
|
+
this._push = push?.backend || null
|
|
466
474
|
if (spec.local?.schema && !spec.local.codec) bindCodec(spec.local)
|
|
467
475
|
Object.assign(this, operators)
|
|
468
476
|
/** @private */
|
|
@@ -503,6 +511,13 @@ export class Client extends RPCClient {
|
|
|
503
511
|
Ref.attach(this, /** @type {Spec} */ (this.spec).meta.refs)
|
|
504
512
|
if (/** @type {Spec} */ (this.spec).meta.local?.refs) this.local = new LocalRefs(this)
|
|
505
513
|
this._pumpErrors()
|
|
514
|
+
if (this._push) this._unfollow = follow(this, this._push)
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** @private */
|
|
518
|
+
async _close() {
|
|
519
|
+
if (this._unfollow) this._unfollow()
|
|
520
|
+
await super._close()
|
|
506
521
|
}
|
|
507
522
|
// the worker's answer to init and to restore: who this UI is, and where its files are served
|
|
508
523
|
/** @private */
|
package/src/rpc/push.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { watch, get, open, activate, receive } from '../lib/operators.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {object} Backend The platform's push service, as `connect` takes it.
|
|
5
|
+
* @property {(topic: string) => Promise<void>} subscribe
|
|
6
|
+
* @property {(topic: string) => Promise<void>} unsubscribe
|
|
7
|
+
* @property {(event: 'push', fn: (payload: Uint8Array) => void) => (() => void) | void} on
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Keep the device on the push topics of its handles, one subscription per handle, dropped when it
|
|
12
|
+
* leaves; and act on a push: the handle it is about ranks first on the swarm.
|
|
13
|
+
*
|
|
14
|
+
* @param {import('./client.js').Client} me
|
|
15
|
+
* @param {Backend} backend
|
|
16
|
+
* @returns {() => void}
|
|
17
|
+
*/
|
|
18
|
+
export function follow(me, backend) {
|
|
19
|
+
const topics = new Set()
|
|
20
|
+
const stream = watch(me.topics)
|
|
21
|
+
let closed = false
|
|
22
|
+
const sync = async () => {
|
|
23
|
+
for await (const { data } of stream) {
|
|
24
|
+
const wanted = new Set(data.map((row) => row.id))
|
|
25
|
+
// a subscribe that fails is left out, so the next change tries it again
|
|
26
|
+
for (const topic of wanted) {
|
|
27
|
+
if (topics.has(topic)) continue
|
|
28
|
+
try {
|
|
29
|
+
await backend.subscribe(topic)
|
|
30
|
+
topics.add(topic)
|
|
31
|
+
} catch (err) {
|
|
32
|
+
me._onerror(err)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
for (const topic of topics) {
|
|
36
|
+
if (wanted.has(topic)) continue
|
|
37
|
+
topics.delete(topic)
|
|
38
|
+
await backend.unsubscribe(topic).catch(me._onerror)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
sync().catch((err) => closed || me._onerror(err))
|
|
43
|
+
const off = backend.on('push', (payload) => wake(me, payload).catch(me._onerror))
|
|
44
|
+
return () => {
|
|
45
|
+
closed = true
|
|
46
|
+
stream.destroy()
|
|
47
|
+
if (off) off()
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// while the app sleeps the OS shows the push; the handle syncs first once it wakes on its own
|
|
52
|
+
async function wake(me, payload) {
|
|
53
|
+
if ((await get(me.status)).data.suspended) return
|
|
54
|
+
const hit = await receive(me, payload)
|
|
55
|
+
if (!hit?.newer) return
|
|
56
|
+
await activate(await open(me[hit.type], { id: hit.id }))
|
|
57
|
+
}
|
package/src/rpc/server.js
CHANGED
|
@@ -24,7 +24,8 @@ import {
|
|
|
24
24
|
activate,
|
|
25
25
|
deactivate,
|
|
26
26
|
phrase,
|
|
27
|
-
nearby
|
|
27
|
+
nearby,
|
|
28
|
+
receive
|
|
28
29
|
} from '../lib/operators.js'
|
|
29
30
|
|
|
30
31
|
// what a UI reaches by name: these operators, never close or a handle's privates
|
|
@@ -41,7 +42,8 @@ const VERBS = new Map(
|
|
|
41
42
|
activate,
|
|
42
43
|
deactivate,
|
|
43
44
|
phrase,
|
|
44
|
-
nearby
|
|
45
|
+
nearby,
|
|
46
|
+
receive
|
|
45
47
|
})
|
|
46
48
|
)
|
|
47
49
|
|
package/types/build/schemas.d.ts
CHANGED
|
@@ -125,6 +125,12 @@ export declare const main: {
|
|
|
125
125
|
id: import("@cero-base/core").Prim;
|
|
126
126
|
reason: import("@cero-base/core").Prim;
|
|
127
127
|
};
|
|
128
|
+
topic: {
|
|
129
|
+
id: import("@cero-base/core").Prim;
|
|
130
|
+
handle: import("@cero-base/core").Prim;
|
|
131
|
+
type: import("@cero-base/core").Prim;
|
|
132
|
+
name: import("@cero-base/core").Prim;
|
|
133
|
+
};
|
|
128
134
|
};
|
|
129
135
|
export declare const local: {
|
|
130
136
|
master: {
|
|
@@ -164,6 +170,13 @@ export declare const local: {
|
|
|
164
170
|
environment: {
|
|
165
171
|
channel: import("@cero-base/core").Prim;
|
|
166
172
|
};
|
|
173
|
+
subscription: {
|
|
174
|
+
id: import("@cero-base/core").Prim;
|
|
175
|
+
handle: import("@cero-base/core").Prim;
|
|
176
|
+
type: import("@cero-base/core").Prim;
|
|
177
|
+
key: import("@cero-base/core").Prim;
|
|
178
|
+
name: import("@cero-base/core").Prim;
|
|
179
|
+
};
|
|
167
180
|
};
|
|
168
181
|
export declare const rpc: {
|
|
169
182
|
'req-empty': {
|
|
@@ -49,6 +49,12 @@ export declare const cero: {
|
|
|
49
49
|
resume(me: operators.Context): Promise<void>;
|
|
50
50
|
activate(ctx: operators.Context): Promise<void>;
|
|
51
51
|
deactivate(ctx: operators.Context): Promise<void>;
|
|
52
|
+
receive(me: operators.Context, payload: Uint8Array): Promise<{
|
|
53
|
+
id: string;
|
|
54
|
+
type: string;
|
|
55
|
+
name: string | null;
|
|
56
|
+
newer: boolean;
|
|
57
|
+
} | null>;
|
|
52
58
|
phrase(me: operators.Context): Promise<string>;
|
|
53
59
|
nearby(ctx: operators.Context, on: boolean, opts?: {
|
|
54
60
|
topic?: string;
|
package/types/handle/index.d.ts
CHANGED
|
@@ -209,6 +209,7 @@ export declare class Handle extends ReadyResource {
|
|
|
209
209
|
_live;
|
|
210
210
|
/** @private */
|
|
211
211
|
_ac;
|
|
212
|
+
_indexing: any;
|
|
212
213
|
/** @param {HandleOpts} [opts] */
|
|
213
214
|
constructor(opts?: HandleOpts);
|
|
214
215
|
/**
|
|
@@ -307,7 +308,15 @@ export declare class Handle extends ReadyResource {
|
|
|
307
308
|
/** @private */
|
|
308
309
|
private _joins;
|
|
309
310
|
/** @private */
|
|
310
|
-
private
|
|
311
|
+
private _onLocal;
|
|
312
|
+
/** @private */
|
|
313
|
+
private _topics;
|
|
314
|
+
/** @private */
|
|
315
|
+
private _index;
|
|
316
|
+
/** @private */
|
|
317
|
+
private _reindex;
|
|
318
|
+
/** @private */
|
|
319
|
+
private _receive;
|
|
311
320
|
/** @private */
|
|
312
321
|
private _phrase;
|
|
313
322
|
/**
|
package/types/lib/operators.d.ts
CHANGED
|
@@ -223,6 +223,20 @@ export declare function activate(ctx: Context): Promise<void>;
|
|
|
223
223
|
* @returns {Promise<void>}
|
|
224
224
|
*/
|
|
225
225
|
export declare function deactivate(ctx: Context): Promise<void>;
|
|
226
|
+
/**
|
|
227
|
+
* A push the platform delivered, as bytes: the handle it is about, verified against this device's
|
|
228
|
+
* own data, or `null` for a forged one or one about no handle here.
|
|
229
|
+
*
|
|
230
|
+
* @param {Context} me
|
|
231
|
+
* @param {Uint8Array} payload
|
|
232
|
+
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean } | null>}
|
|
233
|
+
*/
|
|
234
|
+
export declare function receive(me: Context, payload: Uint8Array): Promise<{
|
|
235
|
+
id: string;
|
|
236
|
+
type: string;
|
|
237
|
+
name: string | null;
|
|
238
|
+
newer: boolean;
|
|
239
|
+
} | null>;
|
|
226
240
|
/**
|
|
227
241
|
* Reveal the recovery phrase.
|
|
228
242
|
*
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@react-native-firebase/messaging` as a push backend: pass `messaging()`. Pushes to the app in
|
|
3
|
+
* front; the push worklet answers the ones the OS delivers while it is not.
|
|
4
|
+
*
|
|
5
|
+
* @param {{ subscribeToTopic: (topic: string) => Promise<void>, unsubscribeFromTopic: (topic: string) => Promise<void>, onMessage: (fn: (message: object) => void) => () => void }} messaging
|
|
6
|
+
* @returns {import('../rpc/push.js').Backend}
|
|
7
|
+
*/
|
|
8
|
+
export declare function firebase(messaging: {
|
|
9
|
+
subscribeToTopic: (topic: string) => Promise<void>;
|
|
10
|
+
unsubscribeFromTopic: (topic: string) => Promise<void>;
|
|
11
|
+
onMessage: (fn: (message: object) => void) => () => void;
|
|
12
|
+
}): import('../rpc/push.js').Backend;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A push the platform delivered while the app is dead, verified from its storage opened
|
|
3
|
+
* read-only. `null` while the live app holds the storage, which shows the activity itself, and
|
|
4
|
+
* for a push that is forged or about no handle here.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} dir The directory `cero()` opened.
|
|
7
|
+
* @param {import('../lib/spec.js').Spec} spec
|
|
8
|
+
* @param {Uint8Array} payload
|
|
9
|
+
* @param {{ storageKey?: Uint8Array }} [opts] The key `cero()` encrypts the local store with, if any.
|
|
10
|
+
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean } | null>}
|
|
11
|
+
*/
|
|
12
|
+
export declare function receive(dir: string, spec: import('../lib/spec.js').Spec, payload: Uint8Array, { storageKey }?: {
|
|
13
|
+
storageKey?: Uint8Array;
|
|
14
|
+
}): Promise<{
|
|
15
|
+
id: string;
|
|
16
|
+
type: string;
|
|
17
|
+
name: string | null;
|
|
18
|
+
newer: boolean;
|
|
19
|
+
} | null>;
|
|
20
|
+
/**
|
|
21
|
+
* The bare-kit push entry: on each push the OS hands this worklet, verify it and answer with the
|
|
22
|
+
* notification to show, or nothing. The request carries `storage`, `payload` (base64) and, when
|
|
23
|
+
* the app encrypts its local store, `storageKey` (base64).
|
|
24
|
+
*
|
|
25
|
+
* @param {import('../lib/spec.js').Spec} spec
|
|
26
|
+
* @returns {void}
|
|
27
|
+
*/
|
|
28
|
+
export declare function push(spec: import('../lib/spec.js').Spec): void;
|
package/types/rpc/client.d.ts
CHANGED
|
@@ -41,6 +41,12 @@ declare const remote: {
|
|
|
41
41
|
resume(me: verbs.Context): Promise<void>;
|
|
42
42
|
activate(ctx: verbs.Context): Promise<void>;
|
|
43
43
|
deactivate(ctx: verbs.Context): Promise<void>;
|
|
44
|
+
receive(me: verbs.Context, payload: Uint8Array): Promise<{
|
|
45
|
+
id: string;
|
|
46
|
+
type: string;
|
|
47
|
+
name: string | null;
|
|
48
|
+
newer: boolean;
|
|
49
|
+
} | null>;
|
|
44
50
|
phrase(me: verbs.Context): Promise<string>;
|
|
45
51
|
nearby(ctx: verbs.Context, on: boolean, opts?: {
|
|
46
52
|
topic?: string;
|
|
@@ -95,6 +101,8 @@ declare class LocalRefs {
|
|
|
95
101
|
* across the wire.
|
|
96
102
|
*/
|
|
97
103
|
export declare class Client extends RPCClient {
|
|
104
|
+
/** @private */
|
|
105
|
+
_push;
|
|
98
106
|
/** @private */
|
|
99
107
|
_onerror;
|
|
100
108
|
/** @type {string | null} */
|
|
@@ -106,6 +114,7 @@ export declare class Client extends RPCClient {
|
|
|
106
114
|
} | null;
|
|
107
115
|
store: this;
|
|
108
116
|
local: LocalRefs;
|
|
117
|
+
_unfollow: () => void;
|
|
109
118
|
/** @private */
|
|
110
119
|
_fileBase;
|
|
111
120
|
/** @private */
|
|
@@ -115,7 +124,7 @@ export declare class Client extends RPCClient {
|
|
|
115
124
|
* @param {Spec} spec Compiled cero spec (schema + rpc + handles).
|
|
116
125
|
* @param {{ onerror?: (err: Error) => void }} [opts] Where the worker's background errors go; the console without one.
|
|
117
126
|
*/
|
|
118
|
-
constructor(ipc: import('streamx').Duplex, spec: Spec, { onerror }?: {
|
|
127
|
+
constructor(ipc: import('streamx').Duplex, spec: Spec, { onerror, push }?: {
|
|
119
128
|
onerror?: (err: Error) => void;
|
|
120
129
|
});
|
|
121
130
|
/** @private */
|
|
@@ -123,6 +132,8 @@ export declare class Client extends RPCClient {
|
|
|
123
132
|
/** @private */
|
|
124
133
|
private _open;
|
|
125
134
|
/** @private */
|
|
135
|
+
private _close;
|
|
136
|
+
/** @private */
|
|
126
137
|
private _identify;
|
|
127
138
|
/**
|
|
128
139
|
* Create a new child handle of the given type.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type Backend = {
|
|
2
|
+
subscribe: (topic: string) => Promise<void>;
|
|
3
|
+
unsubscribe: (topic: string) => Promise<void>;
|
|
4
|
+
on: (event: 'push', fn: (payload: Uint8Array) => void) => (() => void) | void;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {object} Backend The platform's push service, as `connect` takes it.
|
|
8
|
+
* @property {(topic: string) => Promise<void>} subscribe
|
|
9
|
+
* @property {(topic: string) => Promise<void>} unsubscribe
|
|
10
|
+
* @property {(event: 'push', fn: (payload: Uint8Array) => void) => (() => void) | void} on
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Keep the device on the push topics of its handles, one subscription per handle, dropped when it
|
|
14
|
+
* leaves; and act on a push: the handle it is about ranks first on the swarm.
|
|
15
|
+
*
|
|
16
|
+
* @param {import('./client.js').Client} me
|
|
17
|
+
* @param {Backend} backend
|
|
18
|
+
* @returns {() => void}
|
|
19
|
+
*/
|
|
20
|
+
export declare function follow(me: import('./client.js').Client, backend: Backend): () => void;
|