1- import { CID } from 'multiformats'
21import { Accounting } from '../services/accounting.js'
32
43/**
@@ -63,7 +62,7 @@ export function withEgressHandler (handler) {
6362 *
6463 * @param {import('@web3-storage/gateway-lib/middleware').Context } ctx - The context object.
6564 * @param {AccountingService } accounting - The accounting service instance to record egress.
66- * @param {CID } dataCid - The CID of the served content.
65+ * @param {import('@web3-storage/gateway-lib/handlers'). CID } dataCid - The CID of the served content.
6766 * @returns {TransformStream } - The created TransformStream.
6867 */
6968function createEgressPassThroughStream ( ctx , accounting , dataCid ) {
@@ -90,6 +89,7 @@ function createEgressPassThroughStream (ctx, accounting, dataCid) {
9089 controller . enqueue ( chunk )
9190 totalBytesServed += chunk . byteLength
9291 } catch ( error ) {
92+ console . error ( 'Error while counting egress bytes:' , error )
9393 controller . error ( error )
9494 }
9595 } ,
@@ -105,8 +105,11 @@ function createEgressPassThroughStream (ctx, accounting, dataCid) {
105105 async flush ( controller ) {
106106 try {
107107 // Non-blocking call to the accounting service to record egress
108- ctx . waitUntil ( accounting . record ( dataCid , totalBytesServed , new Date ( ) . toISOString ( ) ) )
108+ if ( totalBytesServed > 0 ) {
109+ ctx . waitUntil ( accounting . record ( dataCid , totalBytesServed , new Date ( ) . toISOString ( ) ) )
110+ }
109111 } catch ( error ) {
112+ console . error ( 'Error while recording egress:' , error )
110113 controller . error ( error )
111114 }
112115 }
0 commit comments