-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
121 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @import { Middleware } from '@web3-storage/gateway-lib' | ||
* @typedef {import('./withAccountingService.types.ts').AccountingServiceContext} AccountingServiceContext | ||
* @typedef {import('./withAccountingService.types.ts').Environment} Environment | ||
*/ | ||
|
||
/** | ||
* The accounting service handler exposes the method `record` to record the egress bytes for a given SpaceDID, Content CID, and servedAt timestamp. | ||
* | ||
* @type {Middleware<AccountingServiceContext, AccountingServiceContext, Environment>} | ||
*/ | ||
export function withAccountingService (handler) { | ||
return async (req, env, ctx) => { | ||
const accountingService = create(env, ctx) | ||
|
||
return handler(req, env, { ...ctx, accountingService }) | ||
} | ||
} | ||
|
||
/** | ||
* @param {Environment} env | ||
* @param {AccountingServiceContext} ctx | ||
* @returns {import('./withAccountingService.types.ts').AccountingService} | ||
*/ | ||
function create (env, ctx) { | ||
return { | ||
/** | ||
* @param {import('@ucanto/principal/ed25519').DIDKey} space - The Space DID where the content was served | ||
* @param {import('@ucanto/principal/ed25519').UnknownLink} resource - The link to the resource that was served | ||
* @param {number} bytes - The number of bytes served | ||
* @param {string} servedAt - The timestamp of when the content was served | ||
*/ | ||
record: async (space, resource, bytes, servedAt) => { | ||
console.log(`Record egress: ${space}, ${resource}, ${bytes}, ${servedAt}`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Environment as MiddlewareEnvironment, Context as MiddlewareContext } from '@web3-storage/gateway-lib' | ||
import { DIDKey, UnknownLink } from '@ucanto/principal/ed25519' | ||
|
||
export interface Environment extends MiddlewareEnvironment { | ||
//TODO: ucanto signer principal key | ||
} | ||
|
||
export interface AccountingServiceContext extends MiddlewareContext { | ||
accountingService?: AccountingService | ||
} | ||
|
||
export interface TokenMetadata { | ||
locationClaim?: unknown // TODO: figure out the right type to use for this - we probably need it for the private data case to verify auth | ||
invalid?: boolean | ||
} | ||
|
||
export interface AccountingService { | ||
record: (space: DIDKey, resource: UnknownLink, bytes: number, servedAt: string) => Promise<void> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import { Environment as MiddlewareEnvironment } from '@web3-storage/gateway-lib' | ||
import { IpfsUrlContext, Environment as MiddlewareEnvironment } from '@web3-storage/gateway-lib' | ||
import { AccountingService } from './withAccountingService.types.js' | ||
import { DIDKey, UnknownLink } from '@ucanto/client' | ||
|
||
export interface Environment extends MiddlewareEnvironment { | ||
ACCOUNTING_SERVICE_URL: string | ||
FF_EGRESS_TRACKER_ENABLED: string | ||
} | ||
|
||
export interface Context extends IpfsUrlContext { | ||
space: DIDKey | ||
accountingService: AccountingService | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters