Skip to content

Commit

Permalink
feat: implement car file support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed May 5, 2021
1 parent 9a4b393 commit 054063e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
34 changes: 34 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 054063e

Please sign in to comment.