Skip to content

Commit 95f0331

Browse files
committed
extract record egress to a new func
1 parent 000ce54 commit 95f0331

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

src/middleware/withUcantoClient.js

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as UCantoClient from '@ucanto/client'
22
import * as CAR from '@ucanto/transport/car'
33
import { SpaceDID } from '@web3-storage/capabilities/utils'
4-
import { ed25519 } from '@ucanto/principal'
4+
import { Verifier, Signer } from '@ucanto/principal/ed25519'
55
import { HTTP } from '@ucanto/transport'
66
import { Usage } from './withUcantoClient.capabilities.js'
77

@@ -25,40 +25,28 @@ export function withUcantoClient (handler) {
2525
}
2626

2727
/**
28-
* Creates a UCantoClient instance with the given environment.
28+
* Creates a UCantoClient instance with the given environment and establishes a connection to the UCanto Server.
2929
*
3030
* @param {Environment} env
3131
* @returns {Promise<import('./withUcantoClient.types.ts').UCantoClient>}
3232
*/
3333
async function create (env) {
34-
const service = ed25519.Verifier.parse(env.SERVICE_ID)
35-
const principal = ed25519.Signer.parse(env.SIGNER_PRINCIPAL_KEY)
36-
37-
const { connection } = await connectUcantoClient(env.UPLOAD_API_URL, principal)
34+
const service = Verifier.parse(env.SERVICE_ID)
35+
const principal = Signer.parse(env.SIGNER_PRINCIPAL_KEY)
36+
const { connection } = await connect(env.UPLOAD_API_URL, principal)
3837

3938
return {
4039
/**
40+
* Records the egress bytes for the given resource.
41+
*
4142
* @param {import('@ucanto/principal/ed25519').DIDKey} space - The Space DID where the content was served
4243
* @param {import('@ucanto/principal/ed25519').UnknownLink} resource - The link to the resource that was served
4344
* @param {number} bytes - The number of bytes served
4445
* @param {Date} servedAt - The timestamp of when the content was served
46+
* @returns {Promise<void>}
4547
*/
46-
record: async (space, resource, bytes, servedAt) => {
47-
const res = await Usage.record.invoke({
48-
issuer: principal,
49-
audience: service,
50-
with: SpaceDID.from(space),
51-
nb: {
52-
resource,
53-
bytes,
54-
servedAt: Math.floor(servedAt.getTime() / 1000)
55-
}
56-
}).execute(connection)
57-
58-
if (res.out.error) {
59-
console.error('Failed to record egress', res.out.error)
60-
}
61-
},
48+
record: async (space, resource, bytes, servedAt) =>
49+
recordEgress(space, resource, bytes, servedAt, principal, service, connection),
6250

6351
/**
6452
* TODO: implement this function
@@ -81,7 +69,7 @@ async function create (env) {
8169
* @param {import('@ucanto/principal/ed25519').EdSigner} principal
8270
*
8371
*/
84-
async function connectUcantoClient (serverUrl, principal) {
72+
async function connect (serverUrl, principal) {
8573
const connection = await UCantoClient.connect({
8674
id: principal,
8775
codec: CAR.outbound,
@@ -90,3 +78,32 @@ async function connectUcantoClient (serverUrl, principal) {
9078

9179
return { connection }
9280
}
81+
82+
/**
83+
* Records the egress bytes in the UCanto Server by invoking the `Usage.record` capability.
84+
*
85+
* @param {import('@ucanto/principal/ed25519').DIDKey} space - The Space DID where the content was served
86+
* @param {import('@ucanto/principal/ed25519').UnknownLink} resource - The link to the resource that was served
87+
* @param {number} bytes - The number of bytes served
88+
* @param {Date} servedAt - The timestamp of when the content was served
89+
* @param {import('@ucanto/principal/ed25519').EdSigner} principal - The principal signer
90+
* @param {Signer.Verifier} service - The service verifier
91+
* @param {any} connection - The connection to execute the command
92+
* @returns {Promise<void>}
93+
*/
94+
async function recordEgress (space, resource, bytes, servedAt, principal, service, connection) {
95+
const res = await Usage.record.invoke({
96+
issuer: principal,
97+
audience: service,
98+
with: SpaceDID.from(space),
99+
nb: {
100+
resource,
101+
bytes,
102+
servedAt: Math.floor(servedAt.getTime() / 1000)
103+
}
104+
}).execute(connection)
105+
106+
if (res.out.error) {
107+
console.error('Failed to record egress', res.out.error)
108+
}
109+
}

0 commit comments

Comments
 (0)