|
| 1 | +import { CID } from 'multiformats' |
| 2 | +import { Accounting } from '../services/accounting.js' |
| 3 | +/** |
| 4 | + * @import { Context, IpfsUrlContext, Middleware } from '@web3-storage/gateway-lib' |
| 5 | + * @import { Environment } from './egress-tracker.types.js' |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * The egress tracking handler must be enabled after the rate limiting handler, |
| 10 | + * and before any handler that serves the response body. It uses the CID of the |
| 11 | + * served content to record the egress in the accounting service, and it counts |
| 12 | + * the bytes served with a TransformStream to determine the egress amount. |
| 13 | + * |
| 14 | + * @type {Middleware<IpfsUrlContext, IpfsUrlContext, Environment>} |
| 15 | + */ |
| 16 | +export function withEgressHandler (handler) { |
| 17 | + return async (req, env, ctx) => { |
| 18 | + const egressTrackerEnabled = env.FF_EGRESS_TRACKER_ENABLED === 'true' |
| 19 | + if (!egressTrackerEnabled) { |
| 20 | + return handler(req, env, ctx) |
| 21 | + } |
| 22 | + |
| 23 | + let response |
| 24 | + try { |
| 25 | + response = await handler(req, env, ctx) |
| 26 | + } catch (error) { |
| 27 | + console.error('Error in egress tracker handler:', error) |
| 28 | + throw error |
| 29 | + } |
| 30 | + |
| 31 | + if (!response.ok || !response.body) { |
| 32 | + return response |
| 33 | + } |
| 34 | + |
| 35 | + const { dataCid } = ctx |
| 36 | + const accounting = Accounting.create({ |
| 37 | + serviceURL: env.ACCOUNTING_SERVICE_URL |
| 38 | + }) |
| 39 | + |
| 40 | + const { readable, writable } = createEgressPassThroughStream(ctx, accounting, dataCid) |
| 41 | + |
| 42 | + try { |
| 43 | + ctx.waitUntil(response.body.pipeTo(writable)) |
| 44 | + } catch (error) { |
| 45 | + console.error('Error in egress tracker handler:', error) |
| 46 | + // Original response in case of an error to avoid breaking the chain and serve the content |
| 47 | + return response |
| 48 | + } |
| 49 | + |
| 50 | + return new Response(readable, { |
| 51 | + status: response.status, |
| 52 | + statusText: response.statusText, |
| 53 | + headers: response.headers |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Creates a TransformStream to count bytes served to the client. |
| 60 | + * It records egress when the stream is finalized without an error. |
| 61 | + * |
| 62 | + * @param {Context} ctx - The context object. |
| 63 | + * @param {import('../bindings.js').AccountingService} accounting - The accounting service instance to record egress. |
| 64 | + * @param {CID} dataCid - The CID of the served content. |
| 65 | + * @returns {TransformStream} - The created TransformStream. |
| 66 | + */ |
| 67 | +function createEgressPassThroughStream (ctx, accounting, dataCid) { |
| 68 | + let totalBytesServed = 0 |
| 69 | + |
| 70 | + return new TransformStream({ |
| 71 | + /** |
| 72 | + * The start function is called when the stream is being initialized. |
| 73 | + * It resets the total bytes served to 0. |
| 74 | + */ |
| 75 | + start () { |
| 76 | + totalBytesServed = 0 |
| 77 | + }, |
| 78 | + /** |
| 79 | + * The transform function is called for each chunk of the response body. |
| 80 | + * It enqueues the chunk and updates the total bytes served. |
| 81 | + * If an error occurs, it signals an error to the controller and logs it. |
| 82 | + * The bytes are not counted in case of enqueuing an error. |
| 83 | + * @param {Uint8Array} chunk |
| 84 | + * @param {TransformStreamDefaultController} controller |
| 85 | + */ |
| 86 | + async transform (chunk, controller) { |
| 87 | + try { |
| 88 | + controller.enqueue(chunk) |
| 89 | + totalBytesServed += chunk.byteLength |
| 90 | + } catch (error) { |
| 91 | + controller.error(error) |
| 92 | + } |
| 93 | + }, |
| 94 | + |
| 95 | + /** |
| 96 | + * The flush function is called when the stream is being finalized, |
| 97 | + * which is when the response is being sent to the client. |
| 98 | + * So before the response is sent, we record the egress. |
| 99 | + * It is called only once and it triggers a non-blocking call to the accounting service. |
| 100 | + * If an error occurs, the egress is not recorded. |
| 101 | + * NOTE: The flush function is NOT called in case of an stream error. |
| 102 | + */ |
| 103 | + async flush (controller) { |
| 104 | + try { |
| 105 | + // Non-blocking call to the accounting service to record egress |
| 106 | + ctx.waitUntil(accounting.record(dataCid, totalBytesServed, new Date().toISOString())) |
| 107 | + } catch (error) { |
| 108 | + controller.error(error) |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | +} |
0 commit comments