forked from storacha/w3up
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: compatibility with legacy w3up client (#59)
This PR allows `upload-service` to work with the legacy client (`@web3-storage/w3up-client`). The new service uses `blob/allocate` and `blob/accept` capabilities when talking to storage nodes. For some reason I thought these were not referenced by the client but they are....except via their old names `web3.storage/blob/allocate` and `web3.storage/blob/accept` 🤦 This PR adds additional effects to the `blob/add` receipt, essentially "fake" `web3.storage/blob/allocate` and `web3.storage/blob/accept` delegations that contain the same information as their `blob/allocate` and `blob/accept` counterparts. It allows old clients to use the old capability names and the new clients to use the new ones. We can remove these additional effects when we are satisfied enough clients have moved to `@storacha/client`.
- Loading branch information
Showing
9 changed files
with
364 additions
and
11 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import http from 'node:http' | ||
import { Client } from '@web3-storage/w3up-client' | ||
import { AgentData } from '@web3-storage/access' | ||
import * as Link from 'multiformats/link' | ||
import { Message } from '@ucanto/core' | ||
import * as CAR from '@ucanto/transport/car' | ||
import * as Test from './test.js' | ||
import { randomBytes } from './helpers/random.js' | ||
|
||
/** @param {import('@storacha/upload-api').AgentStore} agentStore */ | ||
const createReceiptsServer = (agentStore) => | ||
http.createServer(async (req, res) => { | ||
const task = Link.parse(req.url?.split('/').pop() ?? '') | ||
const receiptGet = await agentStore.receipts.get(task) | ||
if (receiptGet.error) { | ||
res.writeHead(404) | ||
return res.end() | ||
} | ||
const message = await Message.build({ receipts: [receiptGet.ok] }) | ||
const request = CAR.request.encode(message) | ||
res.writeHead(200) | ||
res.end(request.body) | ||
}) | ||
|
||
/** @type {Test.Suite} */ | ||
export const testLegacyCompatibility = { | ||
uploadFile: Test.withContext({ | ||
'should upload a file to the service via legacy client': async ( | ||
assert, | ||
{ connection, provisionsStorage, agentStore } | ||
) => { | ||
const receiptsServer = createReceiptsServer(agentStore) | ||
const receiptsEndpoint = await new Promise((resolve) => { | ||
receiptsServer.listen(() => { | ||
// @ts-expect-error | ||
resolve(new URL(`http://127.0.0.1:${receiptsServer.address().port}`)) | ||
}) | ||
}) | ||
|
||
try { | ||
const bytes = await randomBytes(128) | ||
const file = new Blob([bytes]) | ||
const alice = new Client(await AgentData.create(), { | ||
// @ts-expect-error service no longer implements `store/*` | ||
serviceConf: { access: connection, upload: connection }, | ||
receiptsEndpoint, | ||
}) | ||
|
||
const space = await alice.createSpace('upload-test') | ||
const auth = await space.createAuthorization(alice) | ||
await alice.addSpace(auth) | ||
await alice.setCurrentSpace(space.did()) | ||
|
||
await provisionsStorage.put({ | ||
// @ts-expect-error | ||
provider: connection.id.did(), | ||
account: alice.agent.did(), | ||
consumer: space.did(), | ||
}) | ||
|
||
await assert.doesNotReject(alice.uploadFile(file)) | ||
} finally { | ||
receiptsServer.close() | ||
} | ||
}, | ||
}), | ||
} | ||
|
||
Test.test({ LegacyCompatibility: testLegacyCompatibility }) |
Oops, something went wrong.