Skip to content

Commit f21680e

Browse files
committed
test(unit): egress handler
1 parent 96551c1 commit f21680e

File tree

3 files changed

+407
-72
lines changed

3 files changed

+407
-72
lines changed

src/handlers/egress-tracker.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { CID } from 'multiformats'
21
import { 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
*/
6968
function 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
}

test/integration/egress-tracker.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/* eslint-disable no-unused-expressions
2+
---
3+
`no-unused-expressions` doesn't understand that several of Chai's assertions
4+
are implemented as getters rather than explicit function calls; it thinks
5+
the assertions are unused expressions. */
16
import { expect } from 'chai'
27
import { describe, it } from 'mocha'
38
import fetch from 'node-fetch'

0 commit comments

Comments
 (0)