|
| 1 | +import type { CID } from 'multiformats/cid' |
| 2 | +import type { Logger } from 'pino' |
| 3 | +import type { ProgressEvent, ProgressEventHandler } from './types.js' |
| 4 | + |
| 5 | +export type ValidateIPNIProgressEvents = |
| 6 | + | ProgressEvent<'ipniAdvertisement.retryUpdate', { retryCount: number }> |
| 7 | + | ProgressEvent<'ipniAdvertisement.complete', { result: boolean; retryCount: number }> |
| 8 | + | ProgressEvent<'ipniAdvertisement.failed', { error: Error }> |
| 9 | + |
| 10 | +export interface ValidateIPNIAdvertisementOptions { |
| 11 | + /** |
| 12 | + * maximum number of attempts |
| 13 | + * |
| 14 | + * @default: 10 |
| 15 | + */ |
| 16 | + maxAttempts?: number | undefined |
| 17 | + |
| 18 | + /** |
| 19 | + * delay between attempts in milliseconds |
| 20 | + * |
| 21 | + * @default: 5000 |
| 22 | + */ |
| 23 | + delayMs?: number | undefined |
| 24 | + |
| 25 | + /** |
| 26 | + * Abort signal |
| 27 | + * |
| 28 | + * @default: undefined |
| 29 | + */ |
| 30 | + signal?: AbortSignal | undefined |
| 31 | + |
| 32 | + /** |
| 33 | + * Logger instance |
| 34 | + * |
| 35 | + * @default: undefined |
| 36 | + */ |
| 37 | + logger?: Logger | undefined |
| 38 | + |
| 39 | + /** |
| 40 | + * Callback for progress updates |
| 41 | + * |
| 42 | + * @default: undefined |
| 43 | + */ |
| 44 | + onProgress?: ProgressEventHandler<ValidateIPNIProgressEvents> |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Check if the SP has announced the IPFS root CID to IPNI. |
| 49 | + * |
| 50 | + * This should not be called until you receive confirmation from the SP that the piece has been parked, i.e. `onPieceAdded` in the `synapse.storage.upload` callbacks. |
| 51 | + * |
| 52 | + * @param ipfsRootCid - The IPFS root CID to check |
| 53 | + * @param options - Options for the check |
| 54 | + * @returns True if the IPNI announce succeeded, false otherwise |
| 55 | + */ |
| 56 | +export async function validateIPNIAdvertisement( |
| 57 | + ipfsRootCid: CID, |
| 58 | + options?: ValidateIPNIAdvertisementOptions |
| 59 | +): Promise<boolean> { |
| 60 | + const delayMs = options?.delayMs ?? 5000 |
| 61 | + const maxAttempts = options?.maxAttempts ?? 10 |
| 62 | + |
| 63 | + return new Promise<boolean>((resolve, reject) => { |
| 64 | + let retryCount = 0 |
| 65 | + const check = async (): Promise<void> => { |
| 66 | + if (options?.signal?.aborted) { |
| 67 | + throw new Error('Check IPNI announce aborted', { cause: options?.signal }) |
| 68 | + } |
| 69 | + options?.logger?.info( |
| 70 | + { |
| 71 | + event: 'check-ipni-announce', |
| 72 | + ipfsRootCid: ipfsRootCid.toString(), |
| 73 | + }, |
| 74 | + 'Checking IPNI for announcement of IPFS Root CID "%s"', |
| 75 | + ipfsRootCid.toString() |
| 76 | + ) |
| 77 | + const fetchOptions: RequestInit = {} |
| 78 | + if (options?.signal) { |
| 79 | + fetchOptions.signal = options?.signal |
| 80 | + } |
| 81 | + try { |
| 82 | + options?.onProgress?.({ type: 'ipniAdvertisement.retryUpdate', data: { retryCount } }) |
| 83 | + } catch (error) { |
| 84 | + options?.logger?.error({ error }, 'Error in consumer onProgress callback for retryUpdate event') |
| 85 | + } |
| 86 | + |
| 87 | + const response = await fetch(`https://filecoinpin.contact/cid/${ipfsRootCid}`, fetchOptions) |
| 88 | + if (response.ok) { |
| 89 | + try { |
| 90 | + options?.onProgress?.({ type: 'ipniAdvertisement.complete', data: { result: true, retryCount } }) |
| 91 | + } catch (error) { |
| 92 | + options?.logger?.error({ error }, 'Error in consumer onProgress callback for complete event') |
| 93 | + } |
| 94 | + resolve(true) |
| 95 | + return |
| 96 | + } |
| 97 | + if (++retryCount < maxAttempts) { |
| 98 | + options?.logger?.info( |
| 99 | + { retryCount, maxAttempts }, |
| 100 | + 'IPFS Root CID "%s" not announced to IPNI yet (%d/%d). Retrying in %dms...', |
| 101 | + ipfsRootCid.toString(), |
| 102 | + retryCount, |
| 103 | + maxAttempts, |
| 104 | + delayMs |
| 105 | + ) |
| 106 | + await new Promise((resolve) => setTimeout(resolve, delayMs)) |
| 107 | + await check() |
| 108 | + } else { |
| 109 | + const msg = `IPFS root CID "${ipfsRootCid.toString()}" not announced to IPNI after ${maxAttempts} attempt${maxAttempts === 1 ? '' : 's'}` |
| 110 | + const error = new Error(msg) |
| 111 | + options?.logger?.error({ error }, msg) |
| 112 | + try { |
| 113 | + options?.onProgress?.({ type: 'ipniAdvertisement.complete', data: { result: false, retryCount } }) |
| 114 | + } catch (error) { |
| 115 | + options?.logger?.error({ error }, 'Error in consumer onProgress callback for complete event') |
| 116 | + } |
| 117 | + throw error |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + check().catch((error) => { |
| 122 | + options?.onProgress?.({ type: 'ipniAdvertisement.failed', data: { error } }) |
| 123 | + reject(error) |
| 124 | + }) |
| 125 | + }) |
| 126 | +} |
0 commit comments