forked from storacha/w3up
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: compatibility with legacy w3up client #59
Merged
+364
−11
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: Nice. 😄