@steve02081504/fount-p2p 0.0.17 → 0.0.18

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.
@@ -15,9 +15,12 @@ import { fanoutFedFetch } from './fetch_fanout.mjs'
15
15
  import { normalizeFileManifest } from './manifest.mjs'
16
16
  import { shouldPreferIncomingPublicManifest } from './public_manifest.mjs'
17
17
 
18
+ const DEFAULT_MANIFEST_FETCH_TIMEOUT_MS = 8000
19
+
18
20
  /**
19
21
  * 拉取公开 manifest;默认不写盘。`cache: true` 或 `cachePublicManifest` 才缓存。
20
- * @param {{ username: string, ownerEntityHash: string, logicalPath: string, cache?: boolean }} context - 拉取上下文
22
+ * 本地已有 publicSig 时仍会 fanout 再校验,按 publishedAt 择新;超时则回退本地。
23
+ * @param {{ username: string, ownerEntityHash: string, logicalPath: string, cache?: boolean, timeoutMs?: number }} context - 拉取上下文
21
24
  * @returns {Promise<import('./manifest.mjs').FileManifest | null>} 验签后的 manifest,失败为 null
22
25
  */
23
26
  export async function fetchPublicManifest(context) {
@@ -27,16 +30,19 @@ export async function fetchPublicManifest(context) {
27
30
  if (!ownerEntityHash || !logicalPath || !username) return null
28
31
 
29
32
  const local = await loadFileManifest(ownerEntityHash, logicalPath)
30
- if (local?.transferKeyDescriptor?.type === 'public' && local?.meta?.publicSig)
31
- return local
33
+ const hasLocalPublic = local?.transferKeyDescriptor?.type === 'public' && !!local?.meta?.publicSig
32
34
 
33
- if (pendingManifestFetches.size >= MAX_PENDING_MANIFEST_FETCHES) return null
35
+ if (pendingManifestFetches.size >= MAX_PENDING_MANIFEST_FETCHES)
36
+ return hasLocalPublic ? local : null
34
37
 
38
+ const timeoutMs = Number(context.timeoutMs) > 0
39
+ ? Number(context.timeoutMs)
40
+ : DEFAULT_MANIFEST_FETCH_TIMEOUT_MS
35
41
  const requestId = randomUUID()
36
42
  const { done } = registerManifestFetchWait(
37
43
  requestId,
38
44
  manifestFetchExpectedKey(ownerEntityHash, logicalPath),
39
- 8000,
45
+ timeoutMs,
40
46
  )
41
47
  const { nodeHash } = await resolveNodeHash(username)
42
48
  const payload = {
@@ -47,11 +53,14 @@ export async function fetchPublicManifest(context) {
47
53
  }
48
54
  await fanoutFedFetch(username, 'fed_manifest_get', payload)
49
55
  const result = await done
50
- if (!result) return null
51
56
 
52
- if (context.cache === true)
53
- await cachePublicManifest(ownerEntityHash, logicalPath, result)
54
- return result
57
+ if (result && (!hasLocalPublic || shouldPreferIncomingPublicManifest(local, result))) {
58
+ if (context.cache === true)
59
+ await cachePublicManifest(ownerEntityHash, logicalPath, result)
60
+ return result
61
+ }
62
+ if (hasLocalPublic) return local
63
+ return null
55
64
  }
56
65
 
57
66
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve02081504/fount-p2p",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "fount federation P2P layer — link, trust graph, mailbox, DAG, EVFS.",
5
5
  "keywords": [
6
6
  "network",