diff --git a/packages/upload-api/test/external-service/index.js b/packages/upload-api/test/external-service/index.js index 73e767d46..005db99bb 100644 --- a/packages/upload-api/test/external-service/index.js +++ b/packages/upload-api/test/external-service/index.js @@ -22,29 +22,33 @@ export const getExternalServiceImplementations = async (config) => { const claimsService = await ClaimsService.activate(config) const blobRetriever = BlobRetriever.create(claimsService) - const storageProviders = await Promise.all(config.http ? [ - StorageNode.activate({ - http: config.http, - claimsService, - ...principalResolver, - }), - StorageNode.activate({ - http: config.http, - claimsService, - ...principalResolver, - }), - ] : [ - BrowserStorageNode.activate({ - port: 8989, - claimsService, - ...principalResolver, - }), - BrowserStorageNode.activate({ - port: 8990, - claimsService, - ...principalResolver, - }), - ]) + const storageProviders = await Promise.all( + config.http + ? [ + StorageNode.activate({ + http: config.http, + claimsService, + ...principalResolver, + }), + StorageNode.activate({ + http: config.http, + claimsService, + ...principalResolver, + }), + ] + : [ + BrowserStorageNode.activate({ + port: 8989, + claimsService, + ...principalResolver, + }), + BrowserStorageNode.activate({ + port: 8990, + claimsService, + ...principalResolver, + }), + ] + ) const router = Router.create(config.serviceID, storageProviders) return { claimsService, diff --git a/packages/upload-api/test/external-service/storage-node.js b/packages/upload-api/test/external-service/storage-node.js index dc3c44f5b..60be4c642 100644 --- a/packages/upload-api/test/external-service/storage-node.js +++ b/packages/upload-api/test/external-service/storage-node.js @@ -11,18 +11,18 @@ import { CAR, HTTP } from '@ucanto/transport' import * as Server from '@ucanto/server' import { connect } from '@ucanto/client' -/** +/** + * @typedef {{ + * has: (digest: API.MultihashDigest) => Promise + * get: (digest: API.MultihashDigest) => Promise + * set: (digest: API.MultihashDigest, bytes: Uint8Array) => Promise + * }} ContentStore * @typedef {{ -* has: (digest: API.MultihashDigest) => Promise -* get: (digest: API.MultihashDigest) => Promise -* set: (digest: API.MultihashDigest, bytes: Uint8Array) => Promise -* }} ContentStore -* @typedef {{ -* has: (digest: API.MultihashDigest) => Promise -* add: (digest: API.MultihashDigest) => Promise -* }} AllocationStore -* @typedef {import('../../src/types/blob.js').BlobService} BlobService -*/ + * has: (digest: API.MultihashDigest) => Promise + * add: (digest: API.MultihashDigest) => Promise + * }} AllocationStore + * @typedef {import('../../src/types/blob.js').BlobService} BlobService + */ export const MaxUploadSize = 127 * (1 << 25) @@ -34,7 +34,9 @@ const contentKey = (digest) => { /** @param {string} key */ const contentDigest = (key) => - Digest.decode(base58btc.decode(key.split('/').pop()?.replace('.blob', '') ?? '')) + Digest.decode( + base58btc.decode(key.split('/').pop()?.replace('.blob', '') ?? '') + ) /** * @param {{ @@ -45,7 +47,12 @@ const contentDigest = (key) => * }} config * @returns {BlobService} */ -const createService = ({ baseURL, claimsService, contentStore, allocationStore }) => ({ +const createService = ({ + baseURL, + claimsService, + contentStore, + allocationStore, +}) => ({ blob: { allocate: Server.provideAdvanced({ capability: BlobCapabilities.allocate, @@ -59,7 +66,9 @@ const createService = ({ baseURL, claimsService, contentStore, allocationStore } return ok({ size: 0 }) } - const size = await allocationStore.has(digest) ? 0 : capability.nb.blob.size + const size = (await allocationStore.has(digest)) + ? 0 + : capability.nb.blob.size await allocationStore.add(digest) return ok({ @@ -80,13 +89,16 @@ const createService = ({ baseURL, claimsService, contentStore, allocationStore } return error(new AllocatedMemoryNotWrittenError()) } - const receipt = await publishLocationCommitment({ claimsService }, { - space: capability.nb.space, - digest, - location: - /** @type {API.URI} */ - (new URL(contentKey(digest), baseURL()).toString()), - }) + const receipt = await publishLocationCommitment( + { claimsService }, + { + space: capability.nb.space, + digest, + location: + /** @type {API.URI} */ + (new URL(contentKey(digest), baseURL()).toString()), + } + ) if (receipt.out.error) { return receipt.out } @@ -117,21 +129,28 @@ export class BrowserStorageNode { return res.status === 200 ? new Uint8Array(await res.arrayBuffer()) : undefined - } + }, } - + const allocations = new Set() const allocationStore = { /** @param {API.MultihashDigest} digest */ has: async (digest) => allocations.has(contentKey(digest)), /** @param {API.MultihashDigest} digest */ - add: async (digest) => { allocations.add(contentKey(digest)) } + add: async (digest) => { + allocations.add(contentKey(digest)) + }, } const server = Server.create({ id, codec: CAR.inbound, - service: createService({ baseURL: () => baseURL, claimsService, contentStore, allocationStore }), + service: createService({ + baseURL: () => baseURL, + claimsService, + contentStore, + allocationStore, + }), // @ts-expect-error resolveDIDKey, validateAuthorization: () => ({ ok: {} }), @@ -168,7 +187,7 @@ export class StorageNode { const id = await ed25519.generate() /** @type {URL} */ let baseURL - + const content = new Map() const contentStore = { /** @param {API.MultihashDigest} digest */ @@ -179,7 +198,9 @@ export class StorageNode { * @param {API.MultihashDigest} digest * @param {Uint8Array} bytes */ - set: async (digest, bytes) => { content.set(contentKey(digest), bytes) } + set: async (digest, bytes) => { + content.set(contentKey(digest), bytes) + }, } const allocations = new Set() @@ -187,7 +208,9 @@ export class StorageNode { /** @param {API.MultihashDigest} digest */ has: async (digest) => allocations.has(contentKey(digest)), /** @param {API.MultihashDigest} digest */ - add: async (digest) => { allocations.add(contentKey(digest)) } + add: async (digest) => { + allocations.add(contentKey(digest)) + }, } const server = Server.create({ @@ -197,13 +220,13 @@ export class StorageNode { baseURL: () => baseURL, claimsService, contentStore, - allocationStore + allocationStore, }), // @ts-expect-error resolveDIDKey, validateAuthorization: () => ({ ok: {} }), }) - + const httpServer = http.createServer(async (request, response) => { try { const { pathname } = new URL(request.url ?? '/', baseURL) diff --git a/packages/upload-api/test/handlers/usage.js b/packages/upload-api/test/handlers/usage.js index e86cd02d3..ed62e06d4 100644 --- a/packages/upload-api/test/handlers/usage.js +++ b/packages/upload-api/test/handlers/usage.js @@ -18,16 +18,19 @@ export const test = { const link = await CAR.codec.link(data) const size = data.byteLength - await uploadBlob({ - connection, - issuer: alice, - audience: context.id, - with: spaceDid, - proofs: [proof], - }, { - digest: link.multihash, - bytes: data - }) + await uploadBlob( + { + connection, + issuer: alice, + audience: context.id, + with: spaceDid, + proofs: [proof], + }, + { + digest: link.multihash, + bytes: data, + } + ) const usageReportRes = await Usage.report .invoke({ diff --git a/packages/w3up-client/src/client.js b/packages/w3up-client/src/client.js index 5398f002e..7ee1bcde9 100644 --- a/packages/w3up-client/src/client.js +++ b/packages/w3up-client/src/client.js @@ -485,7 +485,9 @@ export class Client extends Base { // Remove shards if (upload.shards?.length) { await Promise.allSettled( - upload.shards.map(shard => this.capability.blob.remove(shard.multihash)) + upload.shards.map((shard) => + this.capability.blob.remove(shard.multihash) + ) ) }