diff --git a/index.js b/index.js index bb77709..094e7e4 100644 --- a/index.js +++ b/index.js @@ -35,6 +35,10 @@ export class Cluster { url.searchParams.set('cid-version', 1) setPinOptions(options, url.searchParams) + if (file.type === 'application/car') { + url.searchParams.set('format', 'car') + } + const headers = this.options.headers const response = await fetch(url.toString(), { method: 'POST', headers, body }) diff --git a/package.json b/package.json index 0f68c5a..ee2339b 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,10 @@ "@web-std/fetch": "^1.0.0", "@web-std/file": "^1.0.1", "@web-std/form-data": "^2.0.0", - "standard": "^16.0.3" + "standard": "^16.0.3", + "@ipld/car": "^1.0.1", + "multiformats": "^7.0.0", + "@ipld/dag-cbor": "^5.0.0" }, "standard": { "ignore": [ diff --git a/test.js b/test.js index 6f247e8..287ceea 100644 --- a/test.js +++ b/test.js @@ -3,6 +3,10 @@ import { FormData } from '@web-std/form-data' import { File, Blob } from '@web-std/file' import assert from 'assert' import { Cluster } from './index.js' +import { CarWriter } from '@ipld/car' +import * as CBOR from '@ipld/dag-cbor' +import { CID } from 'multiformats' +import { sha256 } from 'multiformats/hashes/sha2' Object.assign(global, { fetch, File, Blob, FormData }) @@ -45,6 +49,36 @@ const tests = { assert.strictEqual(dir[2].cid, 'bafybeihdqdewefamqnvu7ih6t7pdam4wisengifqg3fv4jin76ax63hl3i') }, + async 'import dag via car file' () { + const cluster = new Cluster(URL) + const message = CBOR.encode({ hello: 'world' }) + const link = CID.createV1(CBOR.code, await sha256.digest(message)) + + const dag = CBOR.encode({ + to: 'world', + message: link + }) + const cid = CID.createV1(CBOR.code, await sha256.digest(dag)) + + const { writer, out } = CarWriter.create([cid]) + writer.put({ cid, bytes: dag }) + writer.put({ cid: link, bytes: message }) + writer.close() + + const parts = [] + for await (const chunk of out) { + parts.push(chunk) + } + const car = new Blob(parts, { type: 'application/car' }) + + const result = await cluster.add(car) + console.log(result) + + assert.strictEqual(result.name, 'blob') + assert.strictEqual(result.cid, cid.toString()) + assert.strictEqual(result.bytes, message.byteLength + dag.byteLength) + }, + async 'pins a CID' () { const cluster = new Cluster(URL) const file = new File(['foo'], 'foo.txt')