1- import type { Synapse , UploadCallbacks } from '@filoz/synapse-sdk'
1+ import type { Synapse } from '@filoz/synapse-sdk'
22import type { CID } from 'multiformats/cid'
33import type { Logger } from 'pino'
44import {
@@ -11,24 +11,26 @@ import {
1111 validatePaymentRequirements ,
1212} from '../payments/index.js'
1313import { isSessionKeyMode , type SynapseService } from '../synapse/index.js'
14+ import type { ProgressEvent , ProgressEventHandler } from '../utils/types.js'
1415import {
1516 type ValidateIPNIAdvertisementOptions ,
17+ type ValidateIPNIProgressEvents ,
1618 validateIPNIAdvertisement ,
1719} from '../utils/validate-ipni-advertisement.js'
18- import { type SynapseUploadResult , uploadToSynapse } from './synapse.js'
20+ import { type SynapseUploadResult , type UploadProgressEvents , uploadToSynapse } from './synapse.js'
1921
20- export type { SynapseUploadOptions , SynapseUploadResult } from './synapse.js'
22+ export type { SynapseUploadOptions , SynapseUploadResult , UploadProgressEvents } from './synapse.js'
2123export { getDownloadURL , getServiceURL , uploadToSynapse } from './synapse.js'
2224
2325/**
2426 * Options for evaluating whether an upload can proceed.
2527 */
26- export type UploadReadinessProgressEvent =
27- | { type : 'checking-balances' }
28- | { type : 'checking-allowances' }
29- | { type : 'configuring-allowances' }
30- | { type : 'allowances-configured' ; transactionHash ?: string }
31- | { type : 'validating-capacity' }
28+ export type UploadReadinessProgressEvents =
29+ | ProgressEvent < 'checking-balances' >
30+ | ProgressEvent < 'checking-allowances' >
31+ | ProgressEvent < 'configuring-allowances' >
32+ | ProgressEvent < 'allowances-configured' , { transactionHash ?: string } >
33+ | ProgressEvent < 'validating-capacity' >
3234
3335export interface UploadReadinessOptions {
3436 /** Initialized Synapse instance. */
@@ -41,7 +43,7 @@ export interface UploadReadinessOptions {
4143 */
4244 autoConfigureAllowances ?: boolean
4345 /** Optional callback for progress updates. */
44- onProgress ?: ( event : UploadReadinessProgressEvent ) => void
46+ onProgress ?: ProgressEventHandler < UploadReadinessProgressEvents >
4547}
4648
4749/**
@@ -129,7 +131,7 @@ export async function checkUploadReadiness(options: UploadReadinessOptions): Pro
129131 const setResult = await setMaxAllowances ( synapse )
130132 allowancesUpdated = true
131133 allowanceTxHash = setResult . transactionHash
132- onProgress ?.( { type : 'allowances-configured' , transactionHash : allowanceTxHash } )
134+ onProgress ?.( { type : 'allowances-configured' , data : { transactionHash : allowanceTxHash } } )
133135 }
134136
135137 onProgress ?.( { type : 'validating-capacity' } )
@@ -179,8 +181,8 @@ export interface UploadExecutionOptions {
179181 logger : Logger
180182 /** Optional identifier to help correlate logs. */
181183 contextId ?: string
182- /** Optional callbacks mirroring Synapse SDK upload callbacks . */
183- callbacks ?: UploadCallbacks
184+ /** Optional umbrella onProgress receiving child progress events . */
185+ onProgress ?: ProgressEventHandler < ( UploadProgressEvents | ValidateIPNIProgressEvents ) & { } >
184186 /** Optional metadata to associate with the upload. */
185187 metadata ?: Record < string , string >
186188 /**
@@ -193,7 +195,7 @@ export interface UploadExecutionOptions {
193195 * @default : true
194196 */
195197 enabled ?: boolean
196- } & ValidateIPNIAdvertisementOptions
198+ } & Omit < ValidateIPNIAdvertisementOptions , 'onProgress' >
197199}
198200
199201export interface UploadExecutionResult extends SynapseUploadResult {
@@ -219,40 +221,40 @@ export async function executeUpload(
219221 rootCid : CID ,
220222 options : UploadExecutionOptions
221223) : Promise < UploadExecutionResult > {
222- const { logger, contextId, callbacks } = options
224+ const { logger, contextId } = options
223225 let transactionHash : string | undefined
224226 let ipniValidationPromise : Promise < boolean > | undefined
225227
226- const mergedCallbacks : UploadCallbacks = {
227- onUploadComplete : ( pieceCid ) => {
228- callbacks ?. onUploadComplete ?.( pieceCid )
229- // Begin IPNI validation as soon as the upload completes
230- if ( options . ipniValidation ?. enabled !== false && ipniValidationPromise == null ) {
231- try {
232- const { enabled : _enabled , ...rest } = options . ipniValidation ?? { }
233- ipniValidationPromise = validateIPNIAdvertisement ( rootCid , {
234- ...rest ,
235- logger,
236- } )
237- } catch ( error ) {
238- logger . error ( { error } , 'Could not begin IPNI advertisement validation' )
239- ipniValidationPromise = Promise . resolve ( false )
228+ const onProgress : ProgressEventHandler < UploadProgressEvents | ValidateIPNIProgressEvents > = ( event ) => {
229+ switch ( event . type ) {
230+ case 'onPieceAdded' : {
231+ // Begin IPNI validation as soon as the piece is added and parked in the data set
232+ if ( options . ipniValidation ?. enabled !== false && ipniValidationPromise == null ) {
233+ try {
234+ const { enabled : _enabled , ...rest } = options . ipniValidation ?? { }
235+ ipniValidationPromise = validateIPNIAdvertisement ( rootCid , {
236+ ...rest ,
237+ logger,
238+ } )
239+ } catch ( error ) {
240+ logger . error ( { error } , 'Could not begin IPNI advertisement validation' )
241+ ipniValidationPromise = Promise . resolve ( false )
242+ }
240243 }
244+ if ( event . data . txHash != null ) {
245+ transactionHash = event . data . txHash
246+ }
247+ break
241248 }
242- } ,
243- onPieceAdded : ( txHash ) => {
244- if ( txHash ) {
245- transactionHash = txHash
249+ default : {
250+ break
246251 }
247- callbacks ?. onPieceAdded ?.( txHash )
248- } ,
249- onPieceConfirmed : ( pieceIds ) => {
250- callbacks ?. onPieceConfirmed ?.( pieceIds )
251- } ,
252+ }
253+ options . onProgress ?.( event )
252254 }
253255
254256 const uploadOptions : Parameters < typeof uploadToSynapse > [ 4 ] = {
255- callbacks : mergedCallbacks ,
257+ onProgress ,
256258 }
257259 if ( contextId ) {
258260 uploadOptions . contextId = contextId
0 commit comments