@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.
- package/files/manifest_fetch.mjs +18 -9
- package/package.json +1 -1
package/files/manifest_fetch.mjs
CHANGED
|
@@ -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
|
-
*
|
|
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
|
-
|
|
31
|
-
return local
|
|
33
|
+
const hasLocalPublic = local?.transferKeyDescriptor?.type === 'public' && !!local?.meta?.publicSig
|
|
32
34
|
|
|
33
|
-
if (pendingManifestFetches.size >= MAX_PENDING_MANIFEST_FETCHES)
|
|
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
|
-
|
|
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 (
|
|
53
|
-
|
|
54
|
-
|
|
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
|
/**
|