Comments (6)
I think this really comes down to what let arrayBuffer = await files[0].arrayBuffer();
is doing and is it giving you the complete bytes? How is files
populated, and what is .arrayBuffer()
doing? I'm not sure where this API is from, are you using Node.js? Is this coming out of a core API or something else?
from js-car.
So I believe the web3.storage API is returning the full CAR contents in its singular response but the files()
API is a special reader that decodes the CAR for you and returns UnixFS files within that CAR. So what I think your code is trying to do is pass back in a file that's within the CAR you provided as if those bytes were a CAR. So unless you're wrapping CARs in a CAR then this is probably wrong.
If found, the method returns a Web3Response object, which extends the Fetch API response object to add two iterator methods unique to the Web3.Storage client library: files() and unixFsIterator().
If you want to take care of decoding the CAR yourself, then I think you could pass res.body
in a stream to CarReader.fromIterable()
; which is probably part of what the web3.storage JS library is doing for you, although it'd also be adding on the additional layer of decoding UnixFS files from the blocks within the CAR (i.e. the blocks aren't necessarily your files, the files may be spread across blocks, UnixFS provides a file-like view over those blocks).
Does that make sense?
from js-car.
Cool, so it sounds like you got it sorted. I'm going to pass on some feedback to web3.storage re docs for folks that are working with non-unixfs CAR data.
from js-car.
Hi @rvagg , thanks for the response. The file[0]
is coming from web3.storage
This is a simple code I am using to read it
import { Web3Storage } from 'web3.storage'
// Construct with token and endpoint
const client = new Web3Storage({ token: API_TOKEN })
// Fetch and verify files from web3.storage
const res = await client.get('bafyreidkwmlavwkguze5urgxytucj2zay2me33ewm3y5hdx6hlxc3sfghu')
const files = await res.files() // Promise<Web3File[]>
for (const file of files) {
console.log(`${file.cid} ${file.name} ${file.size}`)
}
I have checked Web3File
class and it is using regular JavaScript file interface under the hood. I also tried to use FileReader
methods and they result the same thing.
I am using the code in the browser. The interesting part is that when I download file and read it from the local drive it reads it with no problems
//This works with no issues.
const inStream = fs.createReadStream('test.car')
const reader = await CarReader.fromIterable(inStream)
from js-car.
Hi @rvagg ,
Thanks for your help, I appreciate it. I just tried to pass res.body
to the CarReader.fromIterable()
but it throws this error Uncaught (in promise) TypeError: fromIterable() requires an async iterable
which I had to use the solution here (https://jakearchibald.com/2017/async-iterators-and-generators/#making-streams-iterate) to read it and it worked perfect.
although it'd also be adding on the additional layer of decoding UnixFS files from the blocks within the CAR (i.e. the blocks aren't necessarily your files, the files may be spread across blocks, UnixFS provides a file-like view over those blocks).
Actually the CAR
I am trying to read is a Cbor
object which is an RTree
graph. I am trying to read the RTree
and use it as an index to be able to search for other object Hashes.
from js-car.
@rvagg thanks, I appreciated your help!
from js-car.
Related Issues (17)
- Can't resolve import when using @ipld/car from jest HOT 2
- Premature exit of reader when consuming an iterable stream with empty blocks HOT 2
- CarWriter.fromIterable(roots, blockIterator) HOT 1
- Propsoal: New API to create a writer with unknown root HOT 2
- Proposal: Sync Writer API HOT 2
- Proposal: make CarReader API sync HOT 5
- CarReader.fromIterable for large files HOT 1
- Add support for WebStreams HOT 6
- Option to validate CID matches block HOT 4
- `CarBufferWriter` should be exported by index-browser HOT 3
- No "exports" main defined HOT 4
- What is the proper way to get CAR content without the `carHeader` HOT 2
- Very strange issue with `writer.close` (silently closes entire node process) HOT 7
- dag-json support for CarReader.fromBytes HOT 1
- tsize is too large? HOT 1
- Export a version from the writer HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from js-car.