-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
20 changed files
with
944 additions
and
387 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,41 @@ | ||
import assert from 'assert' | ||
import { endpoint } from './scripts/constants.js' | ||
import * as JWT from '../src/utils/jwt.js' | ||
import { SALT } from './scripts/worker-globals.js' | ||
import { createCar } from './scripts/car.js' | ||
import { JWT_ISSUER } from '../src/constants.js' | ||
|
||
function getTestJWT (sub = 'test', name = 'test') { | ||
return JWT.sign({ sub, iss: JWT_ISSUER, iat: Date.now(), name }, SALT) | ||
} | ||
|
||
describe('POST /car', () => { | ||
it('should add posted CARs to Cluster', async () => { | ||
const name = 'car' | ||
// Create token | ||
const token = await getTestJWT() | ||
|
||
// Create Car | ||
const { root, car: carBody } = await createCar('hello world!') | ||
|
||
// expected CID for the above data | ||
const expectedCid = 'bafkreidvbhs33ighmljlvr7zbv2ywwzcmp5adtf4kqvlly67cy56bdtmve' | ||
assert.strictEqual(root.toString(), expectedCid, 'car file has correct root') | ||
|
||
const res = await fetch(new URL('car', endpoint).toString(), { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
'Content-Type': 'application/car', | ||
'X-Name': name | ||
}, | ||
body: carBody, | ||
}) | ||
|
||
assert(res, 'Server responded') | ||
assert(res.ok, 'Server response ok') | ||
const { cid } = await res.json() | ||
assert(cid, 'Server response payload has `cid` property') | ||
assert.strictEqual(cid, cid, 'Server responded with expected CID') | ||
}) | ||
}) |
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,26 @@ | ||
const { CarReader } = require('@ipld/car') | ||
|
||
/** | ||
* 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 car = await CarReader.fromBytes(files[0].buffer) | ||
const roots = await car.getRoots() | ||
// @ts-ignore | ||
const { cid, bytes } = await car.get(roots[0]) | ||
const result = { | ||
cid: { | ||
'/': cid.toString(), | ||
}, | ||
name: files[0].originalname, | ||
// car uploads may not be unixfs, so get a bytes property instead of `size` https://github.com/ipfs/ipfs-cluster/issues/1362 | ||
bytes: bytes.length, | ||
} | ||
return { | ||
statusCode: 200, | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: query['stream-channels'] === 'false' ? [result] : result, | ||
} | ||
} |
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,10 @@ | ||
import { packToBlob } from 'ipfs-car/pack/blob' | ||
|
||
/** | ||
* @param {string} str Data to encode into CAR file. | ||
*/ | ||
export async function createCar(str) { | ||
return await packToBlob({ | ||
input: new TextEncoder().encode(str) | ||
}) | ||
} |
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
Oops, something went wrong.