-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove custom multipart parser
Adds tests to store endpoint and improves upload tests with new mocks for cluster Tweak some types and deps to make ts happy
- Loading branch information
1 parent
00eef65
commit 3cc2515
Showing
19 changed files
with
1,071 additions
and
1,049 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,57 @@ | ||
// @ts-ignore not typed | ||
const Hash = require('ipfs-only-hash') | ||
const { importer } = require('ipfs-unixfs-importer') | ||
const block = { | ||
get: async (/** @type {any} */ cid) => { | ||
throw new Error(`unexpected block API get for ${cid}`) | ||
}, | ||
put: async () => {}, | ||
} | ||
/** | ||
* @param {MultrFile[]} content | ||
* @param {import('ipfs-unixfs-importer').UserImporterOptions} options | ||
*/ | ||
async function add(content, options) { | ||
const opts = { ...options } | ||
const source = content.map((c) => ({ | ||
content: c.buffer, | ||
path: c.originalname, | ||
})) | ||
|
||
const out = [] | ||
// @ts-ignore | ||
for await (const unixfs of importer(source, block, opts)) { | ||
out.push({ | ||
name: unixfs.path, | ||
cid: { | ||
'/': unixfs.cid.toString(), | ||
}, | ||
size: unixfs.size, | ||
}) | ||
} | ||
|
||
return out | ||
} | ||
|
||
/** | ||
* https://github.com/sinedied/smoke#javascript-mocks | ||
* @typedef {{ buffer: Buffer, originalname: string }} MultrFile | ||
* @param {{ query: Record<string, string>, files: MultrFile[] }} request | ||
*/ | ||
module.exports = async ({ query, files }) => { | ||
const result = { | ||
cid: { | ||
'/': await Hash.of(files[0].buffer, { cidVersion: 1, rawLeaves: true }), | ||
}, | ||
name: files[0].originalname, | ||
size: files[0].buffer.length, | ||
} | ||
return { | ||
statusCode: 200, | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: query['stream-channels'] === 'false' ? [result] : result, | ||
const body = await add(files, { | ||
cidVersion: 1, | ||
rawLeaves: true, | ||
onlyHash: true, | ||
wrapWithDirectory: query['wrap-with-directory'] === 'true', | ||
}) | ||
|
||
try { | ||
return { | ||
statusCode: 200, | ||
headers: { 'Content-Type': 'application/json' }, | ||
body, | ||
} | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
} |
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,103 @@ | ||
import assert from 'assert' | ||
import { CID } from 'multiformats' | ||
import { clearStores } from './scripts/helpers.js' | ||
import stores from './scripts/stores.js' | ||
import { signJWT } from '../src/utils/jwt.js' | ||
import { SALT } from './scripts/worker-globals.js' | ||
import * as Token from '../../client/src/token.js' | ||
|
||
/** | ||
* @param {{publicAddress?: string, issuer?: string, name?: string}} userInfo | ||
*/ | ||
async function createTestUser({ | ||
publicAddress = `0x73573r${Date.now()}`, | ||
issuer = `did:eth:${publicAddress}`, | ||
name = 'A Tester', | ||
} = {}) { | ||
const token = await signJWT( | ||
{ | ||
sub: issuer, | ||
iss: 'nft-storage', | ||
iat: Date.now(), | ||
name: 'test', | ||
}, | ||
SALT | ||
) | ||
await stores.users.put( | ||
issuer, | ||
JSON.stringify({ | ||
sub: issuer, | ||
nickname: 'testymctestface', | ||
name, | ||
email: 'a.tester@example.org', | ||
picture: 'http://example.org/avatar.png', | ||
issuer, | ||
publicAddress, | ||
tokens: { test: token }, | ||
}) | ||
) | ||
return { token, issuer } | ||
} | ||
|
||
describe('/store', () => { | ||
beforeEach(clearStores) | ||
|
||
it('should store image', async () => { | ||
const { token, issuer } = await createTestUser() | ||
|
||
const trick = | ||
'ipfs://bafyreiemweb3jxougg7vaovg7wyiohwqszmgwry5xwitw3heepucg6vyd4' | ||
const metadata = { | ||
name: 'name', | ||
description: 'stuff', | ||
image: new File(['fake image'], 'cat.png', { type: 'image/png' }), | ||
properties: { | ||
extra: 'meta', | ||
trick, | ||
src: [ | ||
new File(['hello'], 'hello.txt', { type: 'text/plain' }), | ||
new Blob(['bye']), | ||
], | ||
}, | ||
} | ||
const body = Token.encode(metadata) | ||
|
||
const res = await fetch('/store', { | ||
method: 'POST', | ||
headers: { Authorization: `Bearer ${token}` }, | ||
body, | ||
}) | ||
assert(res, 'Server responded') | ||
assert(res.ok, 'Server response ok') | ||
const { ok, value } = await res.json() | ||
const result = value | ||
const cid = CID.parse(result.ipnft) | ||
assert.equal(cid.version, 1) | ||
|
||
assert.ok(typeof result.url === 'string') | ||
assert.ok(result.url.startsWith('ipfs:')) | ||
|
||
assert.equal(result.data.name, 'name') | ||
assert.equal(result.data.description, 'stuff') | ||
assert.equal( | ||
result.data.image, | ||
'ipfs://bafybeieb43wq6bqbfmyaawfmq6zuycdq4bo77zph33zxx26wvquth3qxau/cat.png' | ||
) | ||
assert.equal(result.data.properties.extra, 'meta') | ||
assert.equal(result.data.properties.trick, trick) | ||
assert.ok(Array.isArray(result.data.properties.src)) | ||
assert.equal(result.data.properties.src.length, 2) | ||
|
||
const nftData = await stores.nfts.get(`${issuer}:${result.ipnft}`) | ||
assert(nftData, 'nft data was stored') | ||
|
||
const pinData = await stores.pins.getWithMetadata(result.ipnft) | ||
assert(pinData.metadata, 'pin metadata was stored') | ||
assert.strictEqual( | ||
// @ts-ignore | ||
pinData.metadata.status, | ||
'pinned', | ||
'pin status is "pinned"' | ||
) | ||
}) | ||
}) |
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
Oops, something went wrong.