|
| 1 | +import {Auth} from './auth' |
| 2 | +import {GraphSync} from './proto' |
| 3 | +import {normal64Bytes, webSafe64FromBytes} from './utils' |
| 4 | +import {pamGetLeafsMessage, pamMultiSyncMessage, pamSyncMessage} from './restMessages' |
| 5 | + |
| 6 | +export type PamDagSyncRequest = { |
| 7 | + streamId: string |
| 8 | + syncPoint?: number |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Syncs the PAM link DAG for a single record UID. |
| 13 | + * Resolves the config root via get_leafs, then syncs that stream. |
| 14 | + * Use this for on-demand per-record syncing. |
| 15 | + */ |
| 16 | +export async function syncDagForPamRecord( |
| 17 | + auth: Auth, |
| 18 | + recordUid: string, |
| 19 | + origin?: Uint8Array |
| 20 | +): Promise<GraphSync.IGraphSyncMultiResult> { |
| 21 | + const recordUidBytes = normal64Bytes(recordUid) |
| 22 | + const leafsResult = await auth.executeRouterRest(pamGetLeafsMessage({ vertices: [recordUidBytes] })) |
| 23 | + const refs = leafsResult.refs ?? [] |
| 24 | + if (refs.length === 0) { |
| 25 | + return { results: [] } |
| 26 | + } |
| 27 | + const queries: GraphSync.IGraphSyncQuery[] = refs |
| 28 | + .filter(ref => ref.value && ref.value.length > 0) |
| 29 | + .map(ref => ({ |
| 30 | + streamId: ref.value!, |
| 31 | + origin, |
| 32 | + syncPoint: 0 |
| 33 | + })) |
| 34 | + if (queries.length === 0) { |
| 35 | + return { results: [] } |
| 36 | + } |
| 37 | + return auth.executeRouterRest(pamMultiSyncMessage({ queries })) |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Syncs the PAM link DAG for a known config root UID. |
| 42 | + * Use this when the config/network UID is already known. |
| 43 | + */ |
| 44 | +export async function syncPamLinkDagForConfigRoot( |
| 45 | + auth: Auth, |
| 46 | + configUid: string, |
| 47 | + syncPoint: number = 0, |
| 48 | + origin?: Uint8Array |
| 49 | +): Promise<GraphSync.IGraphSyncResult> { |
| 50 | + const streamId = normal64Bytes(configUid) |
| 51 | + return auth.executeRouterRest(pamSyncMessage({ streamId, origin, syncPoint })) |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Bulk-loads the PAM link DAG for multiple record UIDs and/or config UIDs. |
| 56 | + * Resolves config roots for all record UIDs via get_leafs, unions with explicit |
| 57 | + * configUids, then multi-syncs all streams. Use this for startup bulk loading. |
| 58 | + */ |
| 59 | +export async function loadPamLinkDag( |
| 60 | + auth: Auth, |
| 61 | + recordUids: string[], |
| 62 | + configUids: string[] = [], |
| 63 | + origin?: Uint8Array |
| 64 | +): Promise<GraphSync.IGraphSyncMultiResult> { |
| 65 | + const uniqueConfigUidBytes = new Map<string, Uint8Array>() |
| 66 | + |
| 67 | + if (recordUids.length > 0) { |
| 68 | + const vertices = recordUids.map(uid => normal64Bytes(uid)) |
| 69 | + const leafsResult = await auth.executeRouterRest(pamGetLeafsMessage({ vertices })) |
| 70 | + for (const ref of leafsResult.refs ?? []) { |
| 71 | + if (ref.value && ref.value.length > 0) { |
| 72 | + const key = webSafe64FromBytes(ref.value) |
| 73 | + uniqueConfigUidBytes.set(key, ref.value) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + for (const uid of configUids) { |
| 79 | + const bytes = normal64Bytes(uid) |
| 80 | + uniqueConfigUidBytes.set(uid, bytes) |
| 81 | + } |
| 82 | + |
| 83 | + if (uniqueConfigUidBytes.size === 0) { |
| 84 | + return { results: [] } |
| 85 | + } |
| 86 | + |
| 87 | + const queries: GraphSync.IGraphSyncQuery[] = Array.from(uniqueConfigUidBytes.values()).map(streamId => ({ |
| 88 | + streamId, |
| 89 | + origin, |
| 90 | + syncPoint: 0, |
| 91 | + maxCount: 500 |
| 92 | + })) |
| 93 | + |
| 94 | + return auth.executeRouterRest(pamMultiSyncMessage({ queries })) |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Fetches the config root UIDs (stream IDs) for the given leaf (resource) UIDs. |
| 99 | + * Returns the raw refs from the router response. |
| 100 | + */ |
| 101 | +export async function getConfigRootsForRecordUids( |
| 102 | + auth: Auth, |
| 103 | + recordUids: string[] |
| 104 | +): Promise<GraphSync.IGraphSyncRef[]> { |
| 105 | + if (recordUids.length === 0) return [] |
| 106 | + const vertices = recordUids.map(uid => normal64Bytes(uid)) |
| 107 | + const result = await auth.executeRouterRest(pamGetLeafsMessage({ vertices })) |
| 108 | + return result.refs ?? [] |
| 109 | +} |
0 commit comments