Skip to content

Commit

Permalink
Fix bug and use the public gateway car response (#13)
Browse files Browse the repository at this point in the history
* Fix bug and use the public gateway car response

Fixes #12

* Upgrade ipld/car dep

* Update test matrix to node 16 and 18

Co-authored-by: Daniel N <2color@users.noreply.github.com>
  • Loading branch information
2color and 2color authored Jan 8, 2023
1 parent 042aa5a commit 0256280
Show file tree
Hide file tree
Showing 5 changed files with 699 additions and 302 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [14.x, 16.x]
node-version: [16.x, 18.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fetch from 'isomorphic-unfetch'
import { bytes } from 'multiformats'
import { sha256 } from 'multiformats/hashes/sha2'
import { CarReader } from '@ipld/car'
import exporter from 'ipfs-unixfs-exporter'
import { recursive } from 'ipfs-unixfs-exporter'
import toIterable from 'stream-to-it'
import { pipe } from 'it-pipe'

Expand All @@ -22,7 +22,7 @@ export async function ipfsGet ({ ipfsPath, gateway, output, quiet }) {
cid = ipfsPath.substring('/ipfs/'.length)
}
if (cid.match('/')) {
// looks pathish! resolve!
// looks pathish, i.e CID/path! resolve to get the CID for the file in the path
console.log(`📡 Resolving CID from ${gatewayUrl}`)
cid = await resolveIpfsAddress(ipfsPath, gatewayUrl)
console.log(`🎯 ${cid}`)
Expand All @@ -46,13 +46,13 @@ export async function extractCar ({ cid, carReader, output }) {
throw new Error(`Bad block. Hash does not match CID ${cid}`)
}
blockCount++
return res
}
return res.bytes
},
}
// magic extracted from js-ipfs:
// https://github.com/ipfs/js-ipfs/blob/46618c795bf5363ba3186645640fb81349231db7/packages/ipfs-core/src/components/get.js#L20
// https://github.com/ipfs/js-ipfs/blob/46618c795bf5363ba3186645640fb81349231db7/packages/ipfs-cli/src/commands/get.js#L56-L66
for await (const file of exporter.recursive(cid, verifyingBlockService, { /* options */ })) {
for await (const file of recursive(cid, verifyingBlockService, { /* options */ })) {
let filePath = file.path
// output overrides the first part of the path.
if (output) {
Expand All @@ -74,9 +74,14 @@ export async function extractCar ({ cid, carReader, output }) {
return { blockCount, filename: output || cid }
}

async function fetchCar (cid, gateway) {
const url = `${gateway}api/v0/dag/export?arg=${cid}`
const res = await fetch(url, { method: 'POST' })
async function fetchCar(cid, gateway) {
const url = `${gateway}/ipfs/${cid}`
const res = await fetch(url, {
method: 'GET',
headers: {
Accept: 'application/vnd.ipld.car',
},
})
if (res.status > 400) {
throw new Error(`${res.status} ${res.statusText} ${url}`)
}
Expand Down
Loading

0 comments on commit 0256280

Please sign in to comment.