Skip to content

Commit

Permalink
chore: formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
alanshaw committed Nov 5, 2024
1 parent cad14ad commit f4f5f5a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 64 deletions.
50 changes: 27 additions & 23 deletions packages/upload-api/test/external-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
83 changes: 53 additions & 30 deletions packages/upload-api/test/external-service/storage-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>
* get: (digest: API.MultihashDigest) => Promise<Uint8Array|undefined>
* set: (digest: API.MultihashDigest, bytes: Uint8Array) => Promise<boolean>
* }} ContentStore
* @typedef {{
* has: (digest: API.MultihashDigest) => Promise<boolean>
* get: (digest: API.MultihashDigest) => Promise<Uint8Array|undefined>
* set: (digest: API.MultihashDigest, bytes: Uint8Array) => Promise<boolean>
* }} ContentStore
* @typedef {{
* has: (digest: API.MultihashDigest) => Promise<boolean>
* add: (digest: API.MultihashDigest) => Promise<void>
* }} AllocationStore
* @typedef {import('../../src/types/blob.js').BlobService} BlobService
*/
* has: (digest: API.MultihashDigest) => Promise<boolean>
* add: (digest: API.MultihashDigest) => Promise<void>
* }} AllocationStore
* @typedef {import('../../src/types/blob.js').BlobService} BlobService
*/

export const MaxUploadSize = 127 * (1 << 25)

Expand All @@ -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 {{
Expand All @@ -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,
Expand All @@ -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({
Expand All @@ -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
}
Expand Down Expand Up @@ -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: {} }),
Expand Down Expand Up @@ -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 */
Expand All @@ -179,15 +198,19 @@ 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()
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({
Expand All @@ -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)
Expand Down
23 changes: 13 additions & 10 deletions packages/upload-api/test/handlers/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 3 additions & 1 deletion packages/w3up-client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
)
}

Expand Down

0 comments on commit f4f5f5a

Please sign in to comment.