1
1
import * as UCantoClient from '@ucanto/client'
2
2
import * as CAR from '@ucanto/transport/car'
3
3
import { SpaceDID } from '@web3-storage/capabilities/utils'
4
- import { ed25519 } from '@ucanto/principal'
4
+ import { Verifier , Signer } from '@ucanto/principal/ed25519 '
5
5
import { HTTP } from '@ucanto/transport'
6
6
import { Usage } from './withUcantoClient.capabilities.js'
7
7
@@ -25,40 +25,28 @@ export function withUcantoClient (handler) {
25
25
}
26
26
27
27
/**
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 .
29
29
*
30
30
* @param {Environment } env
31
31
* @returns {Promise<import('./withUcantoClient.types.ts').UCantoClient> }
32
32
*/
33
33
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 )
38
37
39
38
return {
40
39
/**
40
+ * Records the egress bytes for the given resource.
41
+ *
41
42
* @param {import('@ucanto/principal/ed25519').DIDKey } space - The Space DID where the content was served
42
43
* @param {import('@ucanto/principal/ed25519').UnknownLink } resource - The link to the resource that was served
43
44
* @param {number } bytes - The number of bytes served
44
45
* @param {Date } servedAt - The timestamp of when the content was served
46
+ * @returns {Promise<void> }
45
47
*/
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 ) ,
62
50
63
51
/**
64
52
* TODO: implement this function
@@ -81,7 +69,7 @@ async function create (env) {
81
69
* @param {import('@ucanto/principal/ed25519').EdSigner } principal
82
70
*
83
71
*/
84
- async function connectUcantoClient ( serverUrl , principal ) {
72
+ async function connect ( serverUrl , principal ) {
85
73
const connection = await UCantoClient . connect ( {
86
74
id : principal ,
87
75
codec : CAR . outbound ,
@@ -90,3 +78,32 @@ async function connectUcantoClient (serverUrl, principal) {
90
78
91
79
return { connection }
92
80
}
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