@steve02081504/fount-p2p 0.0.22 → 0.0.24
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/README.md +8 -6
- package/core/entity_id.mjs +5 -5
- package/core/logical_entity.mjs +1 -1
- package/core/object.mjs +9 -0
- package/core/partpath.mjs +10 -0
- package/core/random_id.mjs +4 -2
- package/crypto/crypto.mjs +26 -93
- package/dag/storage.mjs +0 -5
- package/dag/strip_extensions.mjs +1 -3
- package/{wire/part_query_cache.mjs → federation/part_query/cache.mjs} +7 -7
- package/{wire/part_query.mjs → federation/part_query/runtime.mjs} +36 -77
- package/files/assemble.mjs +5 -5
- package/files/assemble_stream.mjs +2 -2
- package/files/chunk/fetch.mjs +101 -0
- package/files/chunk/pending.mjs +59 -0
- package/files/{chunk_provider_registry.mjs → chunk/provider_registry.mjs} +4 -5
- package/files/chunk/responder.mjs +104 -0
- package/files/{chunk_store.mjs → chunk/store.mjs} +2 -2
- package/files/{chunk_fetch_verify.mjs → chunk/verify.mjs} +2 -2
- package/files/evfs.mjs +18 -19
- package/files/fed/fetch_shared.mjs +39 -0
- package/files/fed/responder.mjs +47 -0
- package/files/{acl.mjs → manifest/acl.mjs} +4 -4
- package/files/{manifest_acl_registry.mjs → manifest/acl_registry.mjs} +14 -17
- package/files/{manifest_fetch.mjs → manifest/fetch.mjs} +47 -52
- package/files/{manifest.mjs → manifest/normalize.mjs} +2 -2
- package/files/manifest/pending.mjs +53 -0
- package/files/{public_manifest.mjs → manifest/public.mjs} +12 -12
- package/files/transfer_key.mjs +2 -2
- package/files/transfer_key_registry.mjs +7 -7
- package/index.mjs +4 -4
- package/infra/service.mjs +1 -1
- package/link/handshake.mjs +0 -3
- package/mailbox/deliver_or_store.mjs +0 -2
- package/mailbox/parse.mjs +1 -1
- package/mailbox/store.mjs +0 -9
- package/mailbox/wire.mjs +24 -16
- package/node/reputation_sync.mjs +33 -29
- package/package.json +9 -8
- package/registries/inbound.mjs +19 -9
- package/registries/part_path.mjs +3 -8
- package/schemas/discovery.mjs +1 -1
- package/schemas/federation_pull.mjs +1 -1
- package/schemas/part_query.mjs +6 -5
- package/transport/group_link_set.mjs +2 -2
- package/transport/link_registry.mjs +2 -2
- package/transport/node_scope/features.mjs +166 -0
- package/transport/node_scope/wire.mjs +124 -0
- package/transport/remote_user_room.mjs +0 -1
- package/{rooms → transport}/scoped_link.mjs +1 -1
- package/transport/user_room.mjs +15 -28
- package/utils/fetch_wait.mjs +98 -0
- package/wire/adapter.mjs +13 -0
- package/wire/ingress.mjs +1 -8
- package/wire/part/fanout.mjs +99 -0
- package/wire/part/group.mjs +45 -0
- package/wire/{part_ingress.mjs → part/ingress.mjs} +58 -67
- package/wire/{part_invoke.mjs → part/invoke.mjs} +1 -22
- package/wire/part/pending.mjs +32 -0
- package/wire/part/query.mjs +91 -0
- package/wire/subscribe.mjs +13 -0
- package/federation/chunk_fetch_pending.mjs +0 -124
- package/federation/manifest_fetch_pending.mjs +0 -88
- package/files/chunk_fetch.mjs +0 -99
- package/files/chunk_responder.mjs +0 -142
- package/transport/advert_ingest.mjs +0 -17
- package/transport/node_scope.mjs +0 -289
- package/wire/group_part.mjs +0 -43
- package/wire/part_common.mjs +0 -55
- package/wire/part_fanout.mjs +0 -52
- /package/{wire → schemas}/part_query.tunables.json +0 -0
|
@@ -8,7 +8,7 @@ import { encryptSliceToPart } from './assemble.mjs'
|
|
|
8
8
|
import { decryptPart } from './transfer_key.mjs'
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* @typedef {import('./manifest.mjs').CeMode} CeMode
|
|
11
|
+
* @typedef {import('./manifest/normalize.mjs').CeMode} CeMode
|
|
12
12
|
* @typedef {{ hash: string, size: number, raw: Buffer, contentHash?: string }} EncryptedPart
|
|
13
13
|
*/
|
|
14
14
|
|
|
@@ -113,7 +113,7 @@ function readStreamChunk(stream) {
|
|
|
113
113
|
|
|
114
114
|
/**
|
|
115
115
|
* 将 manifest 各密文块按 part.size 拼齐后解密,串联为明文可读流。
|
|
116
|
-
* @param {import('./manifest.mjs').FileManifest} manifest 清单
|
|
116
|
+
* @param {import('./manifest/normalize.mjs').FileManifest} manifest 清单
|
|
117
117
|
* @param {import('node:stream').Readable[]} partStreams 按序密文流
|
|
118
118
|
* @param {Buffer | null} contentKey 随机密钥
|
|
119
119
|
* @returns {Readable} 明文流
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { bytesToBase64 } from '../../core/bytes_codec.mjs'
|
|
2
|
+
import { assertHex64 } from '../../core/hexIds.mjs'
|
|
3
|
+
import { ms } from '../../utils/duration.mjs'
|
|
4
|
+
import { createInflightTable } from '../../utils/inflight_table.mjs'
|
|
5
|
+
import { beginFedFanoutFetch } from '../fed/fetch_shared.mjs'
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
MAX_PENDING_CHUNK_FETCHES,
|
|
9
|
+
registerChunkFetchWait,
|
|
10
|
+
} from './pending.mjs'
|
|
11
|
+
import { fetchFederationChunk } from './provider_registry.mjs'
|
|
12
|
+
import { getChunk, hasChunk, putChunk } from './store.mjs'
|
|
13
|
+
import { verifiedChunkBytes } from './verify.mjs'
|
|
14
|
+
|
|
15
|
+
const DEFAULT_CHUNK_FETCH_TIMEOUT_MS = ms('8s')
|
|
16
|
+
|
|
17
|
+
/** 同 username+hash 共享一次 fanout;队满只丢已超基础超时的队首。 */
|
|
18
|
+
const chunkInflight = createInflightTable({
|
|
19
|
+
maxSize: MAX_PENDING_CHUNK_FETCHES,
|
|
20
|
+
baseTimeoutMs: DEFAULT_CHUNK_FETCH_TIMEOUT_MS,
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {{
|
|
25
|
+
* username: string,
|
|
26
|
+
* ciphertextHash: string,
|
|
27
|
+
* ownerEntityHash?: string,
|
|
28
|
+
* groupId?: string,
|
|
29
|
+
* }} FetchChunkContext
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {FetchChunkContext} context 上下文
|
|
34
|
+
* @returns {Promise<Uint8Array | null>} 密文块
|
|
35
|
+
*/
|
|
36
|
+
export async function fetchChunk(context) {
|
|
37
|
+
const hash = String(context.ciphertextHash || '').trim().toLowerCase()
|
|
38
|
+
const { username } = context
|
|
39
|
+
if (!hash || !username) return null
|
|
40
|
+
|
|
41
|
+
if (await hasChunk(hash))
|
|
42
|
+
return new Uint8Array(await getChunk(hash))
|
|
43
|
+
|
|
44
|
+
if (context.groupId) {
|
|
45
|
+
const verified = verifiedChunkBytes(hash, await fetchFederationChunk(username, context.groupId, hash))
|
|
46
|
+
if (verified) {
|
|
47
|
+
await putChunk(hash, verified)
|
|
48
|
+
return verified
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const shared = beginFedFanoutFetch({
|
|
53
|
+
inflight: chunkInflight,
|
|
54
|
+
inflightKey: `${username}\0${hash}`,
|
|
55
|
+
username,
|
|
56
|
+
action: 'fed_chunk_get',
|
|
57
|
+
/**
|
|
58
|
+
* @param {string} requestId 请求 id
|
|
59
|
+
* @returns {{ done: Promise<Uint8Array | null>, cancel: () => void }} 等待句柄
|
|
60
|
+
*/
|
|
61
|
+
registerWait: requestId => registerChunkFetchWait(requestId, hash, DEFAULT_CHUNK_FETCH_TIMEOUT_MS),
|
|
62
|
+
/**
|
|
63
|
+
* @param {string} requestId 请求 id
|
|
64
|
+
* @param {string} nodeHash 目标节点 hash
|
|
65
|
+
* @returns {object} fanout 载荷
|
|
66
|
+
*/
|
|
67
|
+
buildPayload: (requestId, nodeHash) => ({
|
|
68
|
+
requestId,
|
|
69
|
+
nodeHash,
|
|
70
|
+
chunkHash: hash,
|
|
71
|
+
ownerEntityHash: context.ownerEntityHash,
|
|
72
|
+
}),
|
|
73
|
+
})
|
|
74
|
+
if (!shared) return null
|
|
75
|
+
|
|
76
|
+
const verified = verifiedChunkBytes(hash, await shared)
|
|
77
|
+
if (verified) {
|
|
78
|
+
await putChunk(hash, verified)
|
|
79
|
+
return verified
|
|
80
|
+
}
|
|
81
|
+
return null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 若本机有 chunk 则响应 fed_chunk_get。
|
|
86
|
+
* @param {object} payload 请求
|
|
87
|
+
* @param {(response: object, peerId: string) => void} sendResponse 发送
|
|
88
|
+
* @param {string} peerId 对端
|
|
89
|
+
* @returns {Promise<void>}
|
|
90
|
+
*/
|
|
91
|
+
export async function handleIncomingChunkGet(payload, sendResponse, peerId) {
|
|
92
|
+
let hash
|
|
93
|
+
try {
|
|
94
|
+
hash = assertHex64(payload?.chunkHash, 'chunkHash')
|
|
95
|
+
}
|
|
96
|
+
catch { return }
|
|
97
|
+
if (!await hasChunk(hash)) return
|
|
98
|
+
const chunkBytes = await getChunk(hash)
|
|
99
|
+
if (!chunkBytes?.length) return
|
|
100
|
+
sendResponse({ requestId: payload.requestId, dataBase64: bytesToBase64(chunkBytes) }, peerId)
|
|
101
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { base64ToBytes } from '../../core/bytes_codec.mjs'
|
|
2
|
+
import { createFetchWaitTable } from '../../utils/fetch_wait.mjs'
|
|
3
|
+
|
|
4
|
+
import { verifiedChunkBytes } from './verify.mjs'
|
|
5
|
+
|
|
6
|
+
/** 并发 pending chunk fetch 上限。 */
|
|
7
|
+
export const MAX_PENDING_CHUNK_FETCHES = 2048
|
|
8
|
+
|
|
9
|
+
const table = createFetchWaitTable({ maxSize: MAX_PENDING_CHUNK_FETCHES })
|
|
10
|
+
|
|
11
|
+
/** @type {typeof table.pending} */
|
|
12
|
+
export const pendingChunkFetches = table.pending
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 注册 chunk 拉取等待槽(requestId 或 compositeKey)。
|
|
16
|
+
* @param {string} key 唯一等待键
|
|
17
|
+
* @param {string} expectedHash 期望 64 hex 密文哈希
|
|
18
|
+
* @param {number} timeoutMs 超时毫秒
|
|
19
|
+
* @param {{ rejectOnTimeout?: boolean }} [options] rejectOnTimeout 时 Promise 以 Error 拒绝
|
|
20
|
+
* @returns {{ done: Promise<Uint8Array | null>, cancel: () => void }} 等待 Promise 与取消函数
|
|
21
|
+
*/
|
|
22
|
+
export function registerChunkFetchWait(key, expectedHash, timeoutMs, options = {}) {
|
|
23
|
+
return table.register(key, expectedHash, timeoutMs, options)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 按等待键解析入站 chunk 响应(校验哈希后 resolve)。
|
|
28
|
+
* @param {string} key 等待键
|
|
29
|
+
* @param {string} expectedHash 期望哈希
|
|
30
|
+
* @param {Uint8Array | null} bytes 密文块
|
|
31
|
+
* @returns {boolean} 是否命中并完成等待
|
|
32
|
+
*/
|
|
33
|
+
export function resolveChunkFetchWait(key, expectedHash, bytes) {
|
|
34
|
+
const entry = table.peek(key)
|
|
35
|
+
if (!entry || entry.expectedKey !== expectedHash) return false
|
|
36
|
+
const verified = bytes ? verifiedChunkBytes(expectedHash, bytes) : null
|
|
37
|
+
if (bytes && !verified) return false
|
|
38
|
+
return table.settle(key, verified)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 处理 fed_chunk_data / 带 requestId 的响应载荷。
|
|
43
|
+
* @param {object} payload 入站载荷
|
|
44
|
+
* @returns {boolean} 是否命中 pending
|
|
45
|
+
*/
|
|
46
|
+
export function resolvePendingChunkFetch(payload) {
|
|
47
|
+
const requestId = typeof payload?.requestId === 'string' ? payload.requestId : ''
|
|
48
|
+
if (!requestId) return false
|
|
49
|
+
const entry = table.peek(requestId)
|
|
50
|
+
if (!entry) return false
|
|
51
|
+
if (typeof payload?.dataBase64 === 'string') {
|
|
52
|
+
try {
|
|
53
|
+
return resolveChunkFetchWait(requestId, entry.expectedKey, base64ToBytes(payload.dataBase64))
|
|
54
|
+
}
|
|
55
|
+
catch { /* keep waiting */ }
|
|
56
|
+
return false
|
|
57
|
+
}
|
|
58
|
+
return table.settle(requestId, null)
|
|
59
|
+
}
|
|
@@ -10,7 +10,7 @@ const nodeHashProvidersByOwner = new Map()
|
|
|
10
10
|
* @returns {void}
|
|
11
11
|
*/
|
|
12
12
|
export function registerFederationChunkFetcher(ownerId, fetcher) {
|
|
13
|
-
federationFetchersByOwner.set(
|
|
13
|
+
federationFetchersByOwner.set(ownerId, fetcher)
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -19,7 +19,7 @@ export function registerFederationChunkFetcher(ownerId, fetcher) {
|
|
|
19
19
|
* @returns {void}
|
|
20
20
|
*/
|
|
21
21
|
export function registerNodeHashProvider(ownerId, provider) {
|
|
22
|
-
nodeHashProvidersByOwner.set(
|
|
22
|
+
nodeHashProvidersByOwner.set(ownerId, provider)
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
@@ -27,9 +27,8 @@ export function registerNodeHashProvider(ownerId, provider) {
|
|
|
27
27
|
* @returns {void}
|
|
28
28
|
*/
|
|
29
29
|
export function unregisterChunkProviders(ownerId) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
nodeHashProvidersByOwner.delete(key)
|
|
30
|
+
federationFetchersByOwner.delete(ownerId)
|
|
31
|
+
nodeHashProvidersByOwner.delete(ownerId)
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
/**
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { handleIncomingManifestGet } from '../manifest/fetch.mjs'
|
|
2
|
+
import { resolvePendingManifestFetch } from '../manifest/pending.mjs'
|
|
3
|
+
|
|
4
|
+
import { handleIncomingChunkGet } from './fetch.mjs'
|
|
5
|
+
import { resolvePendingChunkFetch } from './pending.mjs'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 群联邦 / TrustGraph 路径:响应带 requestId 的 fed_chunk_get。
|
|
9
|
+
* @param {object} data 入站 fed_chunk_get
|
|
10
|
+
* @param {string} peerId 对端 id
|
|
11
|
+
* @param {(resp: object, peerId: string) => void} sendChunkData 发送 fed_chunk_data
|
|
12
|
+
* @returns {Promise<void>}
|
|
13
|
+
*/
|
|
14
|
+
export async function handleFedChunkGetIngress(data, peerId, sendChunkData) {
|
|
15
|
+
await handleIncomingChunkGet(data, sendChunkData, peerId)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {object} data 入站 fed_chunk_data(含 requestId 时 resolve pending fetch)
|
|
20
|
+
* @returns {boolean} 是否命中 pending
|
|
21
|
+
*/
|
|
22
|
+
export function handleFedChunkDataIngress(data) {
|
|
23
|
+
return resolvePendingChunkFetch(data)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {object} data 入站 fed_manifest_get
|
|
28
|
+
* @param {string} peerId 对端 id
|
|
29
|
+
* @param {(resp: object, peerId: string) => void} sendManifestData 发送 fed_manifest_data
|
|
30
|
+
* @returns {Promise<void>}
|
|
31
|
+
*/
|
|
32
|
+
export async function handleFedManifestGetIngress(data, peerId, sendManifestData) {
|
|
33
|
+
await handleIncomingManifestGet(data, sendManifestData, peerId)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {object} data 入站 fed_manifest_data
|
|
38
|
+
* @returns {Promise<boolean>} 是否命中 pending
|
|
39
|
+
*/
|
|
40
|
+
export function handleFedManifestDataIngress(data) {
|
|
41
|
+
return resolvePendingManifestFetch(data)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {(resp: object, peerId: string) => void} sendData 发送
|
|
46
|
+
* @param {{ enqueue: (prio: number, cleanup: () => void) => void }} [fedOut] 出站限速队列
|
|
47
|
+
* @returns {(resp: object, peerId: string) => void} 可入队的发送
|
|
48
|
+
*/
|
|
49
|
+
function wrapSend(sendData, fedOut) {
|
|
50
|
+
if (!fedOut) return sendData
|
|
51
|
+
return (resp, peerId) => {
|
|
52
|
+
fedOut.enqueue(6, () => {
|
|
53
|
+
try { sendData(resp, peerId) }
|
|
54
|
+
catch { /* peer gone */ }
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Trystero room:注册带 requestId 的 fed_chunk_* + fed_manifest_*(TrustGraph / 群全局 miss)。
|
|
61
|
+
* @param {object} room Trystero room
|
|
62
|
+
* @param {{ enqueue: (prio: number, cleanup: () => void) => void }} [fedOut] 出站队列
|
|
63
|
+
* @param {(roomKey: string, action: string, rtcLimits: object) => boolean} [guardGet] RTC 负载守卫
|
|
64
|
+
* @param {object} [rtcLimits] RTC 限额
|
|
65
|
+
* @param {string} [roomKey] 房间键
|
|
66
|
+
* @returns {void}
|
|
67
|
+
*/
|
|
68
|
+
export function attachTrustGraphFedChunkResponder(room, fedOut, guardGet, rtcLimits = {}, roomKey = '') {
|
|
69
|
+
const [sendChunkData, getChunkData] = room.makeAction('fed_chunk_data')
|
|
70
|
+
const [, getChunkGet] = room.makeAction('fed_chunk_get')
|
|
71
|
+
const [sendManifestData, getManifestData] = room.makeAction('fed_manifest_data')
|
|
72
|
+
const [, getManifestGet] = room.makeAction('fed_manifest_get')
|
|
73
|
+
|
|
74
|
+
const sendChunk = wrapSend((resp, peerId) => {
|
|
75
|
+
try { sendChunkData(resp, peerId) }
|
|
76
|
+
catch { /* peer gone */ }
|
|
77
|
+
}, fedOut)
|
|
78
|
+
const sendManifest = wrapSend((resp, peerId) => {
|
|
79
|
+
try { sendManifestData(resp, peerId) }
|
|
80
|
+
catch { /* peer gone */ }
|
|
81
|
+
}, fedOut)
|
|
82
|
+
|
|
83
|
+
getChunkGet((data, peerId) => {
|
|
84
|
+
if (guardGet && !guardGet(roomKey, 'fed_chunk_get', rtcLimits)) return
|
|
85
|
+
if (!data?.requestId) return
|
|
86
|
+
void handleFedChunkGetIngress(data, peerId, sendChunk)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
getChunkData(data => {
|
|
90
|
+
if (!data?.requestId) return
|
|
91
|
+
handleFedChunkDataIngress(data)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
getManifestGet((data, peerId) => {
|
|
95
|
+
if (guardGet && !guardGet(roomKey, 'fed_manifest_get', rtcLimits)) return
|
|
96
|
+
if (!data?.requestId) return
|
|
97
|
+
void handleFedManifestGetIngress(data, peerId, sendManifest)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
getManifestData(data => {
|
|
101
|
+
if (!data?.requestId) return
|
|
102
|
+
handleFedManifestDataIngress(data)
|
|
103
|
+
})
|
|
104
|
+
}
|
|
@@ -4,8 +4,8 @@ import fsp from 'node:fs/promises'
|
|
|
4
4
|
import { dirname, join } from 'node:path'
|
|
5
5
|
import { pipeline } from 'node:stream/promises'
|
|
6
6
|
|
|
7
|
-
import { isHex64 } from '
|
|
8
|
-
import { getNodeDir } from '
|
|
7
|
+
import { isHex64 } from '../../core/hexIds.mjs'
|
|
8
|
+
import { getNodeDir } from '../../node/instance.mjs'
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* @returns {string} `{nodeDir}/chunks`
|
package/files/evfs.mjs
CHANGED
|
@@ -6,15 +6,15 @@ import { getEntityStore } from '../node/instance.mjs'
|
|
|
6
6
|
|
|
7
7
|
import { buildFileManifestFromEnc, encryptPlaintextToMultiPartsAsync, encryptPlaintextToParts, manifestPartsForPersist } from './assemble.mjs'
|
|
8
8
|
import { createManifestPlaintextStream, encryptReadableToParts } from './assemble_stream.mjs'
|
|
9
|
-
import { fetchChunk } from './
|
|
10
|
-
import { createChunkReadStream, getChunk, hasChunk, putChunk } from './
|
|
11
|
-
import { normalizeFileManifest, publicTransferKeyDescriptor } from './manifest.mjs'
|
|
9
|
+
import { fetchChunk } from './chunk/fetch.mjs'
|
|
10
|
+
import { createChunkReadStream, getChunk, hasChunk, putChunk } from './chunk/store.mjs'
|
|
11
|
+
import { normalizeFileManifest, publicTransferKeyDescriptor } from './manifest/normalize.mjs'
|
|
12
12
|
import { assembleManifestPlaintext, resolveContentKey } from './transfer_key.mjs'
|
|
13
13
|
import { readDagManifestPlaintext, resolveTransferKeyDependencies } from './transfer_key_registry.mjs'
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* @param {string} replicaUsername 副本用户名
|
|
17
|
-
* @param {import('./manifest.mjs').FileManifest} manifest 清单
|
|
17
|
+
* @param {import('./manifest/normalize.mjs').FileManifest} manifest 清单
|
|
18
18
|
* @returns {{ getGroupFileMasterKey?: Function, getVaultMasterKey?: Function }} 密钥依赖
|
|
19
19
|
*/
|
|
20
20
|
function transferKeyDependenciesForReplica(replicaUsername, manifest) {
|
|
@@ -31,7 +31,7 @@ function transferKeyDependenciesForReplica(replicaUsername, manifest) {
|
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* @param {string} username 拉取身份
|
|
34
|
-
* @param {import('./manifest.mjs').FileManifest} manifest 清单
|
|
34
|
+
* @param {import('./manifest/normalize.mjs').FileManifest} manifest 清单
|
|
35
35
|
* @param {{ fetchChunk?: Function }} [options] miss 拉取
|
|
36
36
|
* @returns {Promise<boolean>} 全部 part 是否已就位
|
|
37
37
|
*/
|
|
@@ -53,15 +53,14 @@ async function ensureManifestPartsLocal(username, manifest, options = {}) {
|
|
|
53
53
|
/**
|
|
54
54
|
* @param {string} ownerEntityHash 所有者
|
|
55
55
|
* @param {string} logicalPath 路径
|
|
56
|
-
* @returns {Promise<import('./manifest.mjs').FileManifest | null>}
|
|
56
|
+
* @returns {Promise<import('./manifest/normalize.mjs').FileManifest | null>} 本机已写盘的 manifest
|
|
57
57
|
*/
|
|
58
58
|
export async function loadFileManifest(ownerEntityHash, logicalPath) {
|
|
59
|
-
|
|
60
|
-
return manifest ? normalizeFileManifest(manifest) : null
|
|
59
|
+
return getEntityStore().readManifest(ownerEntityHash, logicalPath)
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
/**
|
|
64
|
-
* @param {import('./manifest.mjs').FileManifest} manifest 清单
|
|
63
|
+
* @param {import('./manifest/normalize.mjs').FileManifest} manifest 清单
|
|
65
64
|
* @returns {Promise<void>}
|
|
66
65
|
*/
|
|
67
66
|
export async function saveFileManifest(manifest) {
|
|
@@ -69,7 +68,7 @@ export async function saveFileManifest(manifest) {
|
|
|
69
68
|
}
|
|
70
69
|
|
|
71
70
|
/**
|
|
72
|
-
* @param {import('./manifest.mjs').FileManifest} manifest 清单
|
|
71
|
+
* @param {import('./manifest/normalize.mjs').FileManifest} manifest 清单
|
|
73
72
|
* @param {Array<Buffer | Uint8Array>} partBytes 密文块
|
|
74
73
|
* @returns {Promise<void>}
|
|
75
74
|
*/
|
|
@@ -80,7 +79,7 @@ export async function storeManifestParts(manifest, partBytes) {
|
|
|
80
79
|
|
|
81
80
|
/**
|
|
82
81
|
* @param {string} replicaUsername 副本用户名
|
|
83
|
-
* @param {import('./manifest.mjs').FileManifest} manifest 清单
|
|
82
|
+
* @param {import('./manifest/normalize.mjs').FileManifest} manifest 清单
|
|
84
83
|
* @param {{ username?: string, fetchChunk?: Function }} [options] miss 拉取
|
|
85
84
|
* @returns {Promise<Buffer | null>} 明文内容
|
|
86
85
|
*/
|
|
@@ -104,7 +103,7 @@ export async function readManifestPlaintext(replicaUsername, manifest, options =
|
|
|
104
103
|
|
|
105
104
|
/**
|
|
106
105
|
* @param {string} replicaUsername 副本用户名
|
|
107
|
-
* @param {import('./manifest.mjs').FileManifest} manifest 清单
|
|
106
|
+
* @param {import('./manifest/normalize.mjs').FileManifest} manifest 清单
|
|
108
107
|
* @param {{ username?: string, fetchChunk?: Function }} [options] miss 拉取
|
|
109
108
|
* @returns {Promise<import('node:stream').Readable | null>} 明文流
|
|
110
109
|
*/
|
|
@@ -134,10 +133,10 @@ export async function readManifestPlaintextStream(replicaUsername, manifest, opt
|
|
|
134
133
|
* @param {Buffer | Uint8Array} parameters.plaintext 明文
|
|
135
134
|
* @param {string} [parameters.name] 文件名
|
|
136
135
|
* @param {string} [parameters.mimeType] MIME
|
|
137
|
-
* @param {import('./manifest.mjs').CeMode} [parameters.ceMode] 模式
|
|
138
|
-
* @param {import('./manifest.mjs').TransferKeyDescriptor} [parameters.transferKeyDescriptor] 传递密钥
|
|
136
|
+
* @param {import('./manifest/normalize.mjs').CeMode} [parameters.ceMode] 模式
|
|
137
|
+
* @param {import('./manifest/normalize.mjs').TransferKeyDescriptor} [parameters.transferKeyDescriptor] 传递密钥
|
|
139
138
|
* @param {object} [parameters.meta] meta
|
|
140
|
-
* @returns {Promise<import('./manifest.mjs').FileManifest>} 写入后的 manifest
|
|
139
|
+
* @returns {Promise<import('./manifest/normalize.mjs').FileManifest>} 写入后的 manifest
|
|
141
140
|
*/
|
|
142
141
|
export async function putFileManifest(parameters) {
|
|
143
142
|
const {
|
|
@@ -178,10 +177,10 @@ export async function putFileManifest(parameters) {
|
|
|
178
177
|
* @param {number} parameters.plainSize 明文字节数
|
|
179
178
|
* @param {string} [parameters.name] 文件名
|
|
180
179
|
* @param {string} [parameters.mimeType] MIME
|
|
181
|
-
* @param {import('./manifest.mjs').CeMode} [parameters.ceMode] 模式
|
|
182
|
-
* @param {import('./manifest.mjs').TransferKeyDescriptor} [parameters.transferKeyDescriptor] 传递密钥
|
|
180
|
+
* @param {import('./manifest/normalize.mjs').CeMode} [parameters.ceMode] 模式
|
|
181
|
+
* @param {import('./manifest/normalize.mjs').TransferKeyDescriptor} [parameters.transferKeyDescriptor] 传递密钥
|
|
183
182
|
* @param {object} [parameters.meta] meta
|
|
184
|
-
* @returns {Promise<import('./manifest.mjs').FileManifest>} 写入后的 manifest
|
|
183
|
+
* @returns {Promise<import('./manifest/normalize.mjs').FileManifest>} 写入后的 manifest
|
|
185
184
|
*/
|
|
186
185
|
export async function putFileManifestFromStream(parameters) {
|
|
187
186
|
const {
|
|
@@ -223,7 +222,7 @@ export async function putFileManifestFromStream(parameters) {
|
|
|
223
222
|
* @returns {Promise<Buffer | null>} 明文或 null
|
|
224
223
|
*/
|
|
225
224
|
export async function readPublicFile(replicaUsername, entityHash, logicalPath, options = {}) {
|
|
226
|
-
const { fetchPublicManifest } = await import('./
|
|
225
|
+
const { fetchPublicManifest } = await import('./manifest/fetch.mjs')
|
|
227
226
|
const manifest = await fetchPublicManifest({
|
|
228
227
|
username: options.username || replicaUsername,
|
|
229
228
|
ownerEntityHash: entityHash,
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
import { resolveNodeHash } from '../chunk/provider_registry.mjs'
|
|
4
|
+
import { fanoutFedFetch } from '../fetch_fanout.mjs'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* inflight + pending wait + fanout 共用骨架。
|
|
8
|
+
* @template T
|
|
9
|
+
* @param {{
|
|
10
|
+
* inflight: { acquire: (key: string, start: () => { done: Promise<T | null>, cancel: () => void }) => Promise<T | null> | null },
|
|
11
|
+
* inflightKey: string,
|
|
12
|
+
* username: string,
|
|
13
|
+
* action: string,
|
|
14
|
+
* registerWait: (requestId: string) => { done: Promise<T | null>, cancel: () => void },
|
|
15
|
+
* buildPayload: (requestId: string, nodeHash: string) => object,
|
|
16
|
+
* }} options 选项
|
|
17
|
+
* @returns {Promise<T | null> | null} 共享 Promise;队满为 null
|
|
18
|
+
*/
|
|
19
|
+
export function beginFedFanoutFetch({
|
|
20
|
+
inflight,
|
|
21
|
+
inflightKey,
|
|
22
|
+
username,
|
|
23
|
+
action,
|
|
24
|
+
registerWait,
|
|
25
|
+
buildPayload,
|
|
26
|
+
}) {
|
|
27
|
+
return inflight.acquire(inflightKey, () => {
|
|
28
|
+
const requestId = randomUUID()
|
|
29
|
+
const wait = registerWait(requestId)
|
|
30
|
+
void (async () => {
|
|
31
|
+
try {
|
|
32
|
+
const { nodeHash } = await resolveNodeHash(username)
|
|
33
|
+
await fanoutFedFetch(username, action, buildPayload(requestId, nodeHash))
|
|
34
|
+
}
|
|
35
|
+
catch { /* pending wait 超时/cancel 负责 settle */ }
|
|
36
|
+
})()
|
|
37
|
+
return { done: wait.done, cancel: wait.cancel }
|
|
38
|
+
})
|
|
39
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { subscribeWire } from '../../wire/subscribe.mjs'
|
|
2
|
+
import { handleIncomingChunkGet } from '../chunk/fetch.mjs'
|
|
3
|
+
import { resolvePendingChunkFetch } from '../chunk/pending.mjs'
|
|
4
|
+
import { handleIncomingManifestGet } from '../manifest/fetch.mjs'
|
|
5
|
+
import { resolvePendingManifestFetch } from '../manifest/pending.mjs'
|
|
6
|
+
|
|
7
|
+
/** @typedef {import('../../wire/adapter.mjs').WireAdapter} WireAdapter */
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* node scope wire:注册 fed_chunk_* + fed_manifest_*。
|
|
11
|
+
* @param {WireAdapter} wire action 表
|
|
12
|
+
* @returns {() => void} 取消挂载的 dispose
|
|
13
|
+
*/
|
|
14
|
+
export function attachNodeScopeFedResponder(wire) {
|
|
15
|
+
return subscribeWire(wire, {
|
|
16
|
+
/**
|
|
17
|
+
* @param {unknown} data chunk 请求载荷
|
|
18
|
+
* @param {string} peerId 对端 nodeHash
|
|
19
|
+
*/
|
|
20
|
+
fed_chunk_get(data, peerId) {
|
|
21
|
+
void handleIncomingChunkGet(data, (resp, pid) => {
|
|
22
|
+
try { wire.send('fed_chunk_data', resp, pid) }
|
|
23
|
+
catch { /* disconnected */ }
|
|
24
|
+
}, peerId)
|
|
25
|
+
},
|
|
26
|
+
/**
|
|
27
|
+
* @param {unknown} data chunk 响应载荷
|
|
28
|
+
*/
|
|
29
|
+
fed_chunk_data(data) { resolvePendingChunkFetch(data) },
|
|
30
|
+
/**
|
|
31
|
+
* @param {unknown} data manifest 请求载荷
|
|
32
|
+
* @param {string} peerId 对端 nodeHash
|
|
33
|
+
*/
|
|
34
|
+
fed_manifest_get(data, peerId) {
|
|
35
|
+
void handleIncomingManifestGet(data, (resp, pid) => {
|
|
36
|
+
try { wire.send('fed_manifest_data', resp, pid) }
|
|
37
|
+
catch { /* disconnected */ }
|
|
38
|
+
}, peerId)
|
|
39
|
+
},
|
|
40
|
+
/**
|
|
41
|
+
* @param {unknown} data manifest 响应载荷
|
|
42
|
+
*/
|
|
43
|
+
fed_manifest_data(data) {
|
|
44
|
+
void resolvePendingManifestFetch(data)
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { isLogicalEntityHash } from '
|
|
2
|
-
import { isWritableLocalEntity } from '
|
|
1
|
+
import { isLogicalEntityHash } from '../../core/logical_entity.mjs'
|
|
2
|
+
import { isWritableLocalEntity } from '../../node/identity.mjs'
|
|
3
3
|
|
|
4
|
-
import { checkManifestAcl, resolveManifestAclType } from './
|
|
4
|
+
import { checkManifestAcl, resolveManifestAclType } from './acl_registry.mjs'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @param {string} replicaUsername 请求 replica
|
|
8
8
|
* @param {string} ownerEntityHash 文件 owner
|
|
9
|
-
* @param {import('./
|
|
9
|
+
* @param {import('./normalize.mjs').FileManifest} manifest 清单
|
|
10
10
|
* @returns {Promise<boolean>} 是否允许读
|
|
11
11
|
*/
|
|
12
12
|
export async function canReadManifest(replicaUsername, ownerEntityHash, manifest) {
|
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
/** @type {Map<string, { ownerId: string, handler: (context: ManifestAclContext, logicalPath?: string) => Promise<boolean> }>} */
|
|
6
6
|
const handlersByType = new Map()
|
|
7
7
|
|
|
8
|
-
/** @type {Map<string, { ownerId: string, match: (manifest: import('./
|
|
8
|
+
/** @type {Map<string, { ownerId: string, match: (manifest: import('./normalize.mjs').FileManifest | null | undefined, ownerEntityHash: string) => string | null }>} */
|
|
9
9
|
const matchersByOwner = new Map()
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* @typedef {{
|
|
13
13
|
* replicaUsername: string,
|
|
14
14
|
* ownerEntityHash: string,
|
|
15
|
-
* manifest: import('./
|
|
15
|
+
* manifest: import('./normalize.mjs').FileManifest,
|
|
16
16
|
* }} ManifestAclContext
|
|
17
17
|
*/
|
|
18
18
|
|
|
@@ -23,12 +23,10 @@ const matchersByOwner = new Map()
|
|
|
23
23
|
* @returns {void}
|
|
24
24
|
*/
|
|
25
25
|
export function registerManifestAcl(type, ownerId, handler) {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
throw new Error(`manifest acl handler for '${key}' already registered by '${existing.ownerId}'`)
|
|
31
|
-
handlersByType.set(key, { ownerId: owner, handler })
|
|
26
|
+
const existing = handlersByType.get(type)
|
|
27
|
+
if (existing && existing.ownerId !== ownerId)
|
|
28
|
+
throw new Error(`manifest acl handler for '${type}' already registered by '${existing.ownerId}'`)
|
|
29
|
+
handlersByType.set(type, { ownerId, handler })
|
|
32
30
|
}
|
|
33
31
|
|
|
34
32
|
/**
|
|
@@ -37,20 +35,19 @@ export function registerManifestAcl(type, ownerId, handler) {
|
|
|
37
35
|
* @returns {void}
|
|
38
36
|
*/
|
|
39
37
|
export function unregisterManifestAcl(type, ownerId) {
|
|
40
|
-
const
|
|
41
|
-
const existing = handlersByType.get(key)
|
|
38
|
+
const existing = handlersByType.get(type)
|
|
42
39
|
if (!existing) return
|
|
43
|
-
if (existing.ownerId ===
|
|
44
|
-
handlersByType.delete(
|
|
40
|
+
if (existing.ownerId === ownerId)
|
|
41
|
+
handlersByType.delete(type)
|
|
45
42
|
}
|
|
46
43
|
|
|
47
44
|
/**
|
|
48
45
|
* @param {string} ownerId 注册方
|
|
49
|
-
* @param {(manifest: import('./
|
|
46
|
+
* @param {(manifest: import('./normalize.mjs').FileManifest | null | undefined, ownerEntityHash: string) => string | null} match ACL 类型匹配
|
|
50
47
|
* @returns {void}
|
|
51
48
|
*/
|
|
52
49
|
export function registerManifestAclMatcher(ownerId, match) {
|
|
53
|
-
matchersByOwner.set(
|
|
50
|
+
matchersByOwner.set(ownerId, { ownerId, match })
|
|
54
51
|
}
|
|
55
52
|
|
|
56
53
|
/**
|
|
@@ -58,11 +55,11 @@ export function registerManifestAclMatcher(ownerId, match) {
|
|
|
58
55
|
* @returns {void}
|
|
59
56
|
*/
|
|
60
57
|
export function unregisterManifestAclMatcher(ownerId) {
|
|
61
|
-
matchersByOwner.delete(
|
|
58
|
+
matchersByOwner.delete(ownerId)
|
|
62
59
|
}
|
|
63
60
|
|
|
64
61
|
/**
|
|
65
|
-
* @param {import('./
|
|
62
|
+
* @param {import('./normalize.mjs').FileManifest | null | undefined} manifest manifest
|
|
66
63
|
* @param {string} ownerEntityHash 所有者
|
|
67
64
|
* @returns {string | null} ACL 类型
|
|
68
65
|
*/
|
|
@@ -81,7 +78,7 @@ export function resolveManifestAclType(manifest, ownerEntityHash) {
|
|
|
81
78
|
* @returns {Promise<boolean>} 是否允许(未注册为 false)
|
|
82
79
|
*/
|
|
83
80
|
export async function checkManifestAcl(type, context, logicalPath) {
|
|
84
|
-
const row = handlersByType.get(
|
|
81
|
+
const row = handlersByType.get(type)
|
|
85
82
|
if (!row?.handler) return false
|
|
86
83
|
return row.handler(context, logicalPath)
|
|
87
84
|
}
|