@steve02081504/fount-p2p 0.0.34 → 0.0.35

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/core/hexIds.mjs CHANGED
@@ -20,10 +20,11 @@ export function normalizeHex64(value) {
20
20
 
21
21
  /**
22
22
  * @param {unknown} value 待校验值
23
- * @returns {boolean} 是否为 64 位小写 hex
23
+ * @returns {string | null} 合法时返回规范化 64 hex,否则 null
24
24
  */
25
25
  export function isHex64(value) {
26
- return HEX_ID_64.test(normalizeHex64(value))
26
+ const normalized = normalizeHex64(value)
27
+ return HEX_ID_64.test(normalized) ? normalized : null
27
28
  }
28
29
 
29
30
  /**
@@ -53,8 +54,9 @@ export function assertHex64(value, label = 'hex64') {
53
54
 
54
55
  /**
55
56
  * @param {unknown} value 待校验值
56
- * @returns {boolean} 是否为 128 位签名 hex
57
+ * @returns {string | null} 合法时返回 128 位签名 hex,否则 null
57
58
  */
58
59
  export function isSignatureHex128(value) {
59
- return SIGNATURE_HEX_128.test(String(value ?? ''))
60
+ const normalized = String(value ?? '')
61
+ return SIGNATURE_HEX_128.test(normalized) ? normalized : null
60
62
  }
@@ -41,7 +41,7 @@ export async function verifyCheckpointSignature(checkpoint, ownerPublicKey) {
41
41
  * @returns {boolean} 签名格式合法为 true
42
42
  */
43
43
  export function isSignedCheckpoint(checkpoint) {
44
- return isSignatureHex128(String(checkpoint?.checkpoint_signature || ''))
44
+ return !!isSignatureHex128(String(checkpoint?.checkpoint_signature || ''))
45
45
  }
46
46
 
47
47
  /**
package/discovery/lan.mjs CHANGED
@@ -95,7 +95,7 @@ export function createLanDiscoveryProvider(options = {}) {
95
95
  let beaconTimer = null
96
96
  const seededSelf = normalizeHex64(options.localNodeHash)
97
97
  /** @type {string | null} */
98
- let selfNodeHash = isHex64(seededSelf) ? seededSelf : null
98
+ let selfNodeHash = isHex64(seededSelf)
99
99
  /** @type {Set<string>} */
100
100
  const joinedAddresses = new Set()
101
101
 
@@ -763,7 +763,7 @@ export function createNostrDiscoveryProvider(options = {}) {
763
763
  const secretKey = randomBytes(32)
764
764
  const seededSelf = normalizeHex64(options.localNodeHash)
765
765
  /** @type {string | null} */
766
- let selfNodeHash = isHex64(seededSelf) ? seededSelf : null
766
+ let selfNodeHash = isHex64(seededSelf)
767
767
  const NETWORK_SUB_KEY = 'network'
768
768
  /**
769
769
  * @typedef {{ stop: () => void, held: boolean, listeners: Set<(bytes: Uint8Array, meta: object) => void> }} AdvertSubEntry
@@ -22,8 +22,7 @@ export const LINK_HANDSHAKE_DOMAIN = 'fount-link'
22
22
  export function normalizeLinkBinding(value) {
23
23
  const dtls = normalizeDtlsFingerprint(value)
24
24
  if (dtls) return dtls
25
- const hex = normalizeHex64(value)
26
- return isHex64(hex) ? hex : null
25
+ return isHex64(value)
27
26
  }
28
27
 
29
28
  /**
package/node/network.mjs CHANGED
@@ -53,9 +53,7 @@ export function normalizeNetwork(raw) {
53
53
  * @returns {string[]} 去重 nodeHash 列表
54
54
  */
55
55
  const pickIds = key => [...new Set(
56
- (file[key] || [])
57
- .map(id => normalizeHex64(id))
58
- .filter(id => isHex64(id)),
56
+ (file[key] || []).map(isHex64).filter(Boolean),
59
57
  )]
60
58
  const hints = (file.hints || [])
61
59
  .map(hint => ({
@@ -221,9 +219,9 @@ export function promoteExplorePeer(nodeHash) {
221
219
  export function replaceNetworkPeerPools(pools = {}) {
222
220
  const net = loadNetwork()
223
221
  if (Array.isArray(pools.trustedPeers))
224
- net.trustedPeers = pools.trustedPeers.map(id => normalizeHex64(id)).filter(id => isHex64(id))
222
+ net.trustedPeers = pools.trustedPeers.map(isHex64).filter(Boolean)
225
223
  if (Array.isArray(pools.explorePeers))
226
- net.explorePeers = pools.explorePeers.map(id => normalizeHex64(id)).filter(id => isHex64(id))
224
+ net.explorePeers = pools.explorePeers.map(isHex64).filter(Boolean)
227
225
  net.lastRosterAt = Date.now()
228
226
  saveNetwork(net)
229
227
  }
@@ -73,9 +73,7 @@ function loadSyncConfig() {
73
73
  * @returns {string[]} 规范化去重后的 64-hex 列表
74
74
  */
75
75
  function normalizeHashList(list) {
76
- return [...new Set((Array.isArray(list) ? list : [])
77
- .map(id => normalizeHex64(id))
78
- .filter(id => isHex64(id)))]
76
+ return [...new Set((Array.isArray(list) ? list : []).map(isHex64).filter(Boolean))]
79
77
  }
80
78
 
81
79
  /** 将当前 sync 配置写盘 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve02081504/fount-p2p",
3
- "version": "0.0.34",
3
+ "version": "0.0.35",
4
4
  "description": "fount federation P2P layer — link, trust graph, mailbox, DAG, EVFS.",
5
5
  "keywords": [
6
6
  "network",
@@ -19,7 +19,7 @@ function sanitizeDiscoveryAdvertisement(ad) {
19
19
  title: String(ad.title || '').slice(0, 200),
20
20
  blurb: String(ad.blurb || '').slice(0, 500),
21
21
  advertiserPubKeyHash,
22
- advertiserNodeHash: isHex64(advertiserNodeHash) ? advertiserNodeHash : String(ad.advertiserNodeHash || ''),
22
+ advertiserNodeHash: isHex64(advertiserNodeHash) || String(ad.advertiserNodeHash || ''),
23
23
  observedAt: Number(ad.observedAt) || 0,
24
24
  signature,
25
25
  }