|
| 1 | +import { Accounting } from '../services/accounting.js' |
| 2 | + |
| 3 | +/** |
| 4 | + * @import { Context, IpfsUrlContext, Middleware } from '@web3-storage/gateway-lib' |
| 5 | + * @import { Environment } from './withEgressTracker.types.js' |
| 6 | + * @import { AccountingService } from '../bindings.js' |
| 7 | + * @typedef {IpfsUrlContext & { ACCOUNTING_SERVICE?: AccountingService }} EgressTrackerContext |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * The egress tracking handler must be enabled after the rate limiting handler, |
| 12 | + * and before any handler that serves the response body. It uses the CID of the |
| 13 | + * served content to record the egress in the accounting service, and it counts |
| 14 | + * the bytes served with a TransformStream to determine the egress amount. |
| 15 | + * |
| 16 | + * @type {Middleware<EgressTrackerContext, EgressTrackerContext, Environment>} |
| 17 | + */ |
| 18 | +export function withEgressTracker (handler) { |
| 19 | + return async (req, env, ctx) => { |
| 20 | + if (env.FF_EGRESS_TRACKER_ENABLED !== 'true') { |
| 21 | + return handler(req, env, ctx) |
| 22 | + } |
| 23 | + |
| 24 | + const response = await handler(req, env, ctx) |
| 25 | + if (!response.ok || !response.body) { |
| 26 | + return response |
| 27 | + } |
| 28 | + |
| 29 | + const { dataCid } = ctx |
| 30 | + const accounting = ctx.ACCOUNTING_SERVICE ?? Accounting.create({ |
| 31 | + serviceURL: env.ACCOUNTING_SERVICE_URL |
| 32 | + }) |
| 33 | + |
| 34 | + const responseBody = response.body.pipeThrough( |
| 35 | + createByteCountStream((totalBytesServed) => { |
| 36 | + // Non-blocking call to the accounting service to record egress |
| 37 | + if (totalBytesServed > 0) { |
| 38 | + ctx.waitUntil( |
| 39 | + accounting.record(dataCid, totalBytesServed, new Date().toISOString()) |
| 40 | + ) |
| 41 | + } |
| 42 | + }) |
| 43 | + ) |
| 44 | + |
| 45 | + return new Response(responseBody, { |
| 46 | + status: response.status, |
| 47 | + statusText: response.statusText, |
| 48 | + headers: response.headers |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Creates a TransformStream to count bytes served to the client. |
| 55 | + * It records egress when the stream is finalized without an error. |
| 56 | + * |
| 57 | + * @param {(totalBytesServed: number) => void} onClose |
| 58 | + * @template {Uint8Array} T |
| 59 | + * @returns {TransformStream<T, T>} - The created TransformStream. |
| 60 | + */ |
| 61 | +function createByteCountStream (onClose) { |
| 62 | + let totalBytesServed = 0 |
| 63 | + |
| 64 | + return new TransformStream({ |
| 65 | + /** |
| 66 | + * The transform function is called for each chunk of the response body. |
| 67 | + * It enqueues the chunk and updates the total bytes served. |
| 68 | + * If an error occurs, it signals an error to the controller and logs it. |
| 69 | + * The bytes are not counted in case of enqueuing an error. |
| 70 | + */ |
| 71 | + async transform (chunk, controller) { |
| 72 | + try { |
| 73 | + controller.enqueue(chunk) |
| 74 | + totalBytesServed += chunk.byteLength |
| 75 | + } catch (error) { |
| 76 | + console.error('Error while counting bytes:', error) |
| 77 | + controller.error(error) |
| 78 | + } |
| 79 | + }, |
| 80 | + |
| 81 | + /** |
| 82 | + * The flush function is called when the stream is being finalized, |
| 83 | + * which is when the response is being sent to the client. |
| 84 | + * So before the response is sent, we record the egress using the callback. |
| 85 | + * If an error occurs, the egress is not recorded. |
| 86 | + * NOTE: The flush function is NOT called in case of a stream error. |
| 87 | + */ |
| 88 | + async flush () { |
| 89 | + onClose(totalBytesServed) |
| 90 | + } |
| 91 | + }) |
| 92 | +} |
0 commit comments