Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 4b68189

Browse files
committed
docs: major API changes
The whole IPLD APIs get a review to make them more consistent and easier to use. Changes to compared to the current spec: - No more callbacks, everything is Promises. - `get()`: - Is renamed to `resolve()` to indicate that it also takes a mandatory path argument. - Doesn’t have a `cid` in the return value anymore. That use case is covered by returning all objects that were traversed. Their `value` will always be the CID of the next one to traverse. So if you want to know the CID of the current IPLD Node, just look at the `value` of the previously returned IPLD Node. - Doesn’t return a single value, but an iterator. - `localResolve` option is removed. If you want to resolve a single IPLD Node only, just stop the iterator after the first item. - `getStream()` is removed without replacement. Use `resolve()` which uses async iterators instead. - `getMany()` is renamed to `get()`: - takes an iterable of CIDs as parameter. - `put()`: - takes an iterable of CIDs as parameter. - Doesn’t take `cid` as an option anymore. The CID is always calculated (#175). - The options don’t take the `format` (which is a string), but the `codec` (which is a `multicodec`) (#175). - the `cidVersion` option always defaults to `1`. - `.treeStream()` is renamed to `.tree()` and returns an async iterator. - `remove()`: - takes an iterable of CIDs as parameter. - `.support.add()` is renamed to `.addFormat()`. - `.support.rm()` is renamed to `.removeFormat()`. - `options.loadFormat()` is no longer callback based but is using an async function. Almost all open issues in regards to the IPLD API got adressed. One bigger thing is the Patch API, which also isn’t part of the current specification. Here’s an overview of the issues: - #66 - [ ] IPLD Resolver `patch` API: There is no patch API yet - #70 - [x] lazy value lookups: Can be done as IPLD Formats is pure JavaScript - [x] get external / local paths only: @vmx doesn’t know what this is, considers it a “won’t fix” - [x] toJSON + fromJSON - roundtrip: A roundtrip is not a goal anymore => won’t fix - [ ] put + patch api #66: Patch API is still missing - [x] text summary: @vmx doesn’t see a strong use case for this => “won’t fix” - [x] globbing: Out of scope for js-ipld - #175 - [x] Deprecate passing a CID to `ipld.put`?: Current spec doesn’t allow `put()` with a CID - #182 - [x] API Review: Links to many other issues, I list the individual issues below - #183 - [x] getting the merkle proof with resolver.resolve: `resolve()` returns all CIDs along the traversed path - #184 - [ ] Pass down the `options` object to resolver.resolve(): This needs a custom resolver. @vmx isn’t sure if the js-ipld API should make this possible, or of it should just be easy enough to create your own resolver. - https://github.com/ipfs/interface-ipfs-core/issues/81 - [x] The `dag` API - One API to manipulate all the IPLD Format objects: Interesting discussion, but not relevant anymore. - ipfs-inactive/interface-js-ipfs-core#121 - [x] dag api: add {ls, tree}: `tree()` is not part of js-ipld anymore as the IPLD Nodes are just JavaScript objects which you can easily traverse if you like. Though `tree()`-like functionality might be added as an example or separate module. - ipfs-inactive/interface-js-ipfs-core#125 - [x] `dag get --localResolve` vs `dag resolve`: This is solved by the new `resolve()` API - ipfs-inactive/interface-js-ipfs-core#137 - [x] add `level` option to ipfs.dag.tree: `tree()` is not part of js-ipld anymore. It should be considered if `tree()` is implemented (probably as an example or separate module) Closes #70, #175, #182, #183 Closes ipfs-inactive/interface-js-ipfs-core#121 Closes ipfs-inactive/interface-js-ipfs-core#125 Closed ipfs-inactive/interface-js-ipfs-core#137
1 parent 9a12316 commit 4b68189

File tree

1 file changed

+67
-49
lines changed

1 file changed

+67
-49
lines changed

README.md

+67-49
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,19 @@ Want to get started? Check our examples folder. You can check the development st
3838
- [Install](#install)
3939
- [Usage](#usage)
4040
- [API](#api)
41-
- IPLD Resolver
42-
- [Constructor](#ipld-constructor)
43-
- [`.put(node, options, callback)`](#putnode-options-callback)
44-
- [`.get(cid [, path] [, options], callback)`](#getcid--path--options-callback)
45-
- [`.getStream(cid [, path] [, options])`](#getstreamcid--path--options)
46-
- [`.treeStream(cid [, path] [, options])`](#treestreamcid--path--options)
47-
- [`.remove(cid, callback)`](#removecid-callback)
48-
- [`.support.add(multicodec, formatResolver, formatUtil)`](#supportaddmulticodec-formatresolver-formatutil)
49-
- [`.support.rm(multicodec)`](#supportrmmulticodec)
50-
- [Properties](#properties)
51-
- [`defaultOptions`](#defaultoptions)
41+
- [IPLD constructor](#ipld-constructor)
42+
- [`options.blockService`](#optionsblockservice)
43+
- [`options.formats`](#optionsformats)
44+
- [`options.loadFormat(codec)`](#optionsloadformatcodec)
45+
- [`.put(nodes, format, options)`](#putnodes-format-options)
46+
- [`.resolve(cid, path)`](#resolvecid-path)
47+
- [`.get(cids)`](#getcids)
48+
- [`.remove(cids)`](#removecids)
49+
- [`.tree(cid, [path], [options])`](#treecid-path-options)
50+
- [`.addFormat(ipldFormatImplementation)`](#addformatipldformatimplementation)
51+
- [`.removeFormat(codec)`](#removeformatcodec)
52+
- [Properties](#properties)
53+
- [`defaultOptions`](#defaultoptions)
5254
- [Packages](#packages)
5355
- [Contribute](#contribute)
5456
- [License](#license)
@@ -90,6 +92,8 @@ initIpld('/tmp/ifpsrepo', (err, ipld) => {
9092

9193
## API
9294

95+
The IPLD API works strictly with CIDs and deserialized IPLD Nodes. Interacting with the binary data happens on lower levels. To access the binary data directly, use the [Block API](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BLOCK.md).
96+
9397
### IPLD constructor
9498

9599
> Creates and returns an instance of IPLD.
@@ -130,83 +134,97 @@ const ipld = new Ipld({
130134
})
131135
```
132136

133-
##### `options.loadFormat(codec, callback)`
137+
##### `options.loadFormat(codec)`
134138

135139
| Type | Default |
136140
|------|---------|
137-
| `Function` | `null` |
141+
| `async Function` | `null` |
138142

139-
Function to dynamically load an [IPLD Format](https://github.com/ipld/interface-ipld-format). It is passed a string `codec`, the multicodec of the IPLD format to load and a callback function to call when the format has been loaded. e.g.
143+
Function to dynamically load an [IPLD Format](https://github.com/ipld/interface-ipld-format). It is passed a `codec`, the multicodec code of the IPLD format to load and returns an IPLD Format implementation. For example:
140144

141145
```js
146+
const multicodec = require('multicodec')
142147
const ipld = new Ipld({
143-
loadFormat (codec, callback) {
144-
if (codec === 'git-raw') {
145-
callback(null, require('ipld-git'))
148+
async loadFormat (codec) {
149+
if (codec === multicodec.GIT_RAW) {
150+
return require('ipld-git')
146151
} else {
147-
callback(new Error('unable to load format ' + codec))
152+
throw new Error('unable to load format ' + multicodec.print[codec])
148153
}
149154
}
150155
})
151156
```
152157

153-
### `.put(node, options, callback)`
158+
### `.put(nodes, format, options)`
159+
160+
> Stores the given IPLD Nodes of a recognized IPLD Format.
161+
162+
- `nodes` (`Iterable<Object>`): deserialized IPLD nodes that should be inserted.
163+
- `format` (`multicodec`, required): the multicodec of the format that IPLD Node should be encoded in.
164+
- `options` is applied to any of the `nodes` and is an object with the following properties:
165+
- `hashAlg` (`multicodec`, default: hash algorithm of the given multicodec): the hashing algorithm that is used to calculate the CID.
166+
- `cidVersion` (`number`, default: 1): the CID version to use.
167+
- `onlyHash` (`boolean`, default: false): if true the serialized form of the IPLD Node will not be passed to the underlying block store.
168+
169+
Returns an async iterator with the CIDs of the serialized IPLD Nodes.
170+
171+
172+
### `.resolve(cid, path)`
173+
174+
> Retrieves IPLD Nodes along the `path` that is rooted at `cid`.
154175
155-
> Store the given node of a recognized IPLD Format.
176+
- `cid` (`CID`, required): the CID the resolving starts.
177+
- `path` (`IPLD Path`, required): the path that should be resolved.
156178

157-
`options` is an object that must contain one of the following combinations:
158-
- `cid` - the CID of the node
159-
- `[hashAlg]`, `[version]` and `format` - the hashAlg, version and the format that should be used to create the CID of the node. The
160-
`hashAlg` and `version` defaults to the default values for the `format`.
179+
Returns an async iterator of all the IPLD Nodes that were traversed during the path resolving. Every element is an object with these fields:
180+
- `remainderPath` (`string`): the part of the path that wasn’t resolved yet.
181+
- `value` (`*`): the value where the resolved path points to. If further traversing is possible, then the value is a CID object linking to another IPLD Node. If it was possible to fully resolve the path, `value` is the value the `path` points to. So if you need the CID of the IPLD Node you’re currently at, just take the `value` of the previously returned IPLD Node.
161182

162-
It may contain any of the following:
163183

164-
- `onlyHash` - If true the serialized form of the node will not be passed to the underlying block store but the passed callback will be invoked as if it had been
184+
### `.get(cids)`
165185

166-
`callback` is a function that should have the signature as following: `function (err, cid) {}`, where `err` is an Error object in case of error and `cid` is the cid of the stored object.
186+
> Retrieve several IPLD Nodes at once.
167187
168-
### `.get(cid [, path] [, options], callback)`
188+
- `cids` (`Iterable<CID>`): the CIDs of the IPLD Nodes that should be retrieved.
169189

170-
> Retrieve a node by the given `cid` or `cid + path`
190+
Returns an async iterator with the IPLD Nodes that correspond to the given `cids`.
171191

172-
`options` is an optional object containing:
192+
Throws an error if a IPLD Node can’t be retrieved.
173193

174-
- `localResolve: bool` - if true, get will only attempt to resolve the path locally
194+
### `.remove(cids)`
175195

176-
`callback` should be a function with the signature `function (err, result)`, the result being an object with:
196+
> Remove IPLD Nodes by the given `cids`
177197
178-
- `value` - the value that resulted from the get
179-
- `remainderPath` - If it didn't manage to successfully resolve the whole path through or if simply the `localResolve` option was passed.
180-
- `cid` - Where the graph traversal finished - if `remainderPath` has a value, this will be where it has its root
198+
- `cids` (`Iterable<CID>`): the CIDs of the IPLD Nodes that should be removed.
181199

182-
### `.getMany(cids, callback)`
200+
Throws an error if any of the Blocks can’t be removed. This operation is not atomic, some Blocks might have already been removed.
183201

184-
> Retrieve several nodes at once
185202

186-
`callback` should be a function with the signature `function (err, result)`, the result is an array with the nodes corresponding to the CIDs.
203+
### `.tree(cid, [path], [options])`
187204

205+
> Returns all the paths that can be resolved into.
188206
189-
### `.getStream(cid [, path] [, options])`
207+
- `cid` (`CID`, required): the CID to get the paths from.
208+
- `path` (`IPLD Path`, default: ''): the path to start to retrieve the other paths from.
209+
- `options`:
210+
- `recursive` (`bool`, default: false): whether to get the paths recursively or not. `false` resolves only the paths of the given CID.
190211

191-
> Same as get, but returns a source pull-stream that is used to pass the fetched node.
212+
Returns an async iterator of all the paths (as Strings) you could resolve into.
192213

193-
### `.treeStream(cid [, path] [, options])`
194214

195-
> Returns all the paths under a cid + path through a pull-stream. Accepts the following options:
215+
### `.addFormat(ipldFormatImplementation)`
196216

197-
- `recursive` - bool - traverse through links to complete the graph.
217+
> Add support for an IPLD Format
198218
199-
### `.remove(cid, callback)`
219+
- `ipldFormatImplementation` (`IPLD Format`, required): the implementation of an IPLD Format.
200220

201-
> Remove a node by the given `cid`
202221

203-
### `.support.add(multicodec, formatResolver, formatUtil)`
222+
### `.removeFormat(codec)`
204223

205-
> Add support to another IPLD Format
224+
> Remove support for an IPLD Format
206225
207-
### `.support.rm(multicodec)`
226+
- `codec` (`multicodec`, required): the codec of the IPLD Format to remove.
208227

209-
> Removes support of an IPLD Format
210228

211229
### Properties
212230

0 commit comments

Comments
 (0)