@steve02081504/fount-p2p 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/discovery/bt.mjs +31 -2
- package/link/handshake.mjs +2 -2
- package/overlay/index.mjs +1 -1
- package/package.json +7 -7
- package/transport/link_registry.mjs +6 -6
package/discovery/bt.mjs
CHANGED
|
@@ -38,6 +38,35 @@ async function loadBleno() {
|
|
|
38
38
|
return mod.default ?? mod
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
/**
|
|
42
|
+
* 等待 BLE 运行时进入 poweredOn(兼容 noble v1/v2 与 bleno)。
|
|
43
|
+
* @param {*} runtime noble 或 bleno 实例
|
|
44
|
+
* @param {number} [timeout] 超时毫秒
|
|
45
|
+
* @returns {Promise<void>}
|
|
46
|
+
*/
|
|
47
|
+
export async function waitPoweredOn(runtime, timeout) {
|
|
48
|
+
const wait = runtime.waitForPoweredOnAsync ?? runtime.waitForPoweredOn
|
|
49
|
+
if (typeof wait !== 'function')
|
|
50
|
+
throw new Error('p2p: bluetooth runtime missing waitForPoweredOn(Async)')
|
|
51
|
+
return wait.call(runtime, timeout)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 探测本机 noble 运行时是否具备 BT 扫描所需 API。
|
|
56
|
+
* @returns {Promise<boolean>}
|
|
57
|
+
*/
|
|
58
|
+
export async function canUseBluetoothDiscovery() {
|
|
59
|
+
try {
|
|
60
|
+
const noble = await loadNoble()
|
|
61
|
+
if (typeof noble.startScanningAsync !== 'function') return false
|
|
62
|
+
const wait = noble.waitForPoweredOnAsync ?? noble.waitForPoweredOn
|
|
63
|
+
return typeof wait === 'function'
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
41
70
|
/**
|
|
42
71
|
* 将 advert 映射序列化为可读 characteristic blob。
|
|
43
72
|
* @param {Map<string, Uint8Array>} adverts topic → payload 映射
|
|
@@ -144,7 +173,7 @@ export function createBluetoothDiscoveryProvider() {
|
|
|
144
173
|
}
|
|
145
174
|
},
|
|
146
175
|
})
|
|
147
|
-
await bleno
|
|
176
|
+
await waitPoweredOn(bleno, 5_000)
|
|
148
177
|
await bleno.setServicesAsync([
|
|
149
178
|
new bleno.PrimaryService({
|
|
150
179
|
uuid: BT_SERVICE_UUID,
|
|
@@ -218,7 +247,7 @@ export function createBluetoothDiscoveryProvider() {
|
|
|
218
247
|
async function ensureScanRuntime() {
|
|
219
248
|
if (scanningStarted) return
|
|
220
249
|
const noble = await loadNoble()
|
|
221
|
-
await noble
|
|
250
|
+
await waitPoweredOn(noble, 5_000)
|
|
222
251
|
noble.on('discover', peripheral => {
|
|
223
252
|
void inspectPeripheral(peripheral).catch(() => { })
|
|
224
253
|
})
|
package/link/handshake.mjs
CHANGED
|
@@ -10,7 +10,7 @@ import { normalizeDtlsFingerprint } from './sdp_fingerprint.mjs'
|
|
|
10
10
|
/**
|
|
11
11
|
* Link 握手签名域标识符。
|
|
12
12
|
*/
|
|
13
|
-
export const LINK_HANDSHAKE_DOMAIN = 'fount-link
|
|
13
|
+
export const LINK_HANDSHAKE_DOMAIN = 'fount-link'
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* 构造 link auth 待签名字节串。
|
|
@@ -126,7 +126,7 @@ export async function verifyAuth(hello, auth, expectedNonce, remoteFingerprintFr
|
|
|
126
126
|
* @returns {Uint8Array} 待签名消息字节
|
|
127
127
|
*/
|
|
128
128
|
export function buildAdvertMessage(topic, ts, nodeHash) {
|
|
129
|
-
return Buffer.from(`fount-advert
|
|
129
|
+
return Buffer.from(`fount-advert\0${String(topic)}\0${String(ts)}\0${normalizeHex64(nodeHash)}`, 'utf8')
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
/**
|
package/overlay/index.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import { pubKeyHash, sign, verify } from '../crypto/crypto.mjs'
|
|
|
4
4
|
import { randomMsgIdHex } from '../link/frame.mjs'
|
|
5
5
|
import { createLruMap } from '../utils/lru.mjs'
|
|
6
6
|
|
|
7
|
-
const ROUTE_DOMAIN = 'fount-route
|
|
7
|
+
const ROUTE_DOMAIN = 'fount-route'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* 构造 overlay 路由签名用的字节序列。
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@steve02081504/fount-p2p",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1",
|
|
4
4
|
"description": "fount federation P2P layer — link, trust graph, mailbox, DAG, EVFS.",
|
|
5
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
6
|
"network",
|
|
7
7
|
"p2p",
|
|
8
8
|
"fount",
|
|
@@ -60,14 +60,14 @@
|
|
|
60
60
|
"./utils/*": "./utils/*.mjs"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@noble/curves": "
|
|
64
|
-
"node-datachannel": "
|
|
63
|
+
"@noble/curves": "latest",
|
|
64
|
+
"node-datachannel": "latest"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
|
-
"@steve02081504/exec": "
|
|
67
|
+
"@steve02081504/exec": "latest"
|
|
68
68
|
},
|
|
69
69
|
"optionalDependencies": {
|
|
70
|
-
"@stoprocent/bleno": "
|
|
71
|
-
"@stoprocent/noble": "
|
|
70
|
+
"@stoprocent/bleno": "latest",
|
|
71
|
+
"@stoprocent/noble": "latest"
|
|
72
72
|
}
|
|
73
73
|
}
|
|
@@ -15,7 +15,7 @@ import { createLruMap } from '../utils/lru.mjs'
|
|
|
15
15
|
|
|
16
16
|
import { DEFAULT_ICE_SERVERS } from './ice_servers.mjs'
|
|
17
17
|
|
|
18
|
-
const SIGNAL_DOMAIN = 'fount-signal
|
|
18
|
+
const SIGNAL_DOMAIN = 'fount-signal'
|
|
19
19
|
const NODE_TOPIC_DOMAIN = 'fount-rdv-node:'
|
|
20
20
|
const GROUP_TOPIC_DOMAIN = 'fount-rdv-group:'
|
|
21
21
|
|
|
@@ -235,11 +235,11 @@ export function createLinkRegistry(opts = {}) {
|
|
|
235
235
|
const providerIds = new Set(listDiscoveryProviders().map(provider => provider.id))
|
|
236
236
|
if (!providerIds.has('mdns'))
|
|
237
237
|
registerDiscoveryProvider(createMdnsDiscoveryProvider())
|
|
238
|
-
if (!providerIds.has('bt'))
|
|
239
|
-
await import('
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
238
|
+
if (!providerIds.has('bt')) {
|
|
239
|
+
const bt = await import('../discovery/bt.mjs').catch(() => null)
|
|
240
|
+
if (await bt?.canUseBluetoothDiscovery?.())
|
|
241
|
+
registerDiscoveryProvider(bt.createBluetoothDiscoveryProvider())
|
|
242
|
+
}
|
|
243
243
|
if (!providerIds.has('nostr'))
|
|
244
244
|
// 测试通过 initNode({ signaling: { relayOverride } }) 注入共享 loopback relay;生产则回落到用户 relay + 默认公网 relay。
|
|
245
245
|
// 新 discovery 栈也必须尊重这层 runtime override,否则 live 双节点测试会各打各的公网 relay。
|