This repository was archived by the owner on Apr 29, 2020. It is now read-only.
This repository was archived by the owner on Apr 29, 2020. It is now read-only.
API review #7
Closed
Description
Proposal
To change the return type and codify the behaviour of this module.
Current API
const exporter = require('ipfs-unixfs-exporter')
const pull = require('pull-stream')
const through = require('pull-stream/throughs/through')
const onEnd = require('pull-stream/sinks/on-end')
pull(
exporter('QmFoo.../bar/baz.txt', ipld)
through(file => {
console.info(file)
// {
// depth: 2,
// name: 'baz.txt',
// path: 'QmFoo.../bar/baz.txt',
// size: ...
// hash: Buffer
// content: <Pull stream>
// type: 'file'
// }
}),
onEnd(...)
)
Observations
- If the user wants any extra information about the node, they must turn the hash into a CID then pass it to an IPLD resolver, but we already had the CID and DAGNodes during the graph traversal so it's just unnecessary work
- Using external libraries dictates how the user should structure their code and their choice of dependencies. They came here for the files, just give them the files
Proposal
- Emit entries with object versions of CIDs and nodes
- Use core JS types, e.g. async iterators
const exporter = require('ipfs-unixfs-exporter')
for await (const entry of exporter('QmFoo.../bar/baz.txt', ipld)) {
console.info(entry)
// {
// depth: 2,
// name: 'baz.txt',
// path: 'QmFoo.../bar/baz.txt',
// cid: CID
// node: DAGNode (cid.codec === 'dag-pb') || Object (cid.codec === 'dag-cbor') || Buffer (cid.codec === 'raw')
// content: Function // returns an async iterable of file or directory contents
// type: 'file' // or 'directory'
// }
for await (const content of entry.content()) {
// content is either a buffer (when entry.type === 'file') or an entry (when entry.type === 'directory')
}
}
Options
{
offset: Number, // If a file is at the end of the path `.content()` will start at this offset
length: Number, // If a file is at the end of the path `.content()` will only return this many bytes
fullPath: Boolean, // Emit entries for every path component
maxDepth: Boolean // Only descend this number of components into the path
}
These would remain the same.
Sharding
Sharded directories are treated like normal directories. That is, they have a type of directory
.
Behaviour
When requesting a path: /ipfs/Qmfoo/bar/baz
, if baz
resolves to a directory, the contents of the directory will be emitted, otherwise baz
itself will be emitted.
If maxDepth
is specified and matches the zero-indexed number of path components (excluding /ipfs
), baz
will be emitted even if it is a directory.
If fullPath
is specified, each path component encountered will be emitted (excluding /ipfs
).
Examples
When baz
is a directory
exporter('/ipfs/Qmfoo/bar/baz')
// emits the files contained in `baz`
exporter('/ipfs/Qmfoo/bar/baz', {
fullPath: true
})
// emits `Qmfoo`, `bar`, `baz`, then the files contained in `baz`
exporter('/ipfs/Qmfoo/bar/baz', {
maxDepth: 2
})
// emits`baz`
exporter('/ipfs/Qmfoo/bar/baz', {
fullPath: true,
maxDepth: 2
})
// emits `Qmfoo`, `bar`, `baz`
When baz
is a file
exporter('/ipfs/Qmfoo/bar/baz')
// emits `baz`
exporter('/ipfs/Qmfoo/bar/baz', {
fullPath: true
})
// emits `Qmfoo`, `bar`, `baz`
exporter('/ipfs/Qmfoo/bar/baz', {
maxDepth: 2
})
// emits`baz`
exporter('/ipfs/Qmfoo/bar/baz', {
fullPath: true,
maxDepth: 2
})
// emits `Qmfoo`, `bar`, `baz`
Metadata
Metadata
Assignees
Labels
No labels