Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 39f4733

Browse files
daviddiasAlan Shaw
authored and
Alan Shaw
committedNov 13, 2018
refactor: updated files API (#878)
BREAKING CHANGE: Files API methods `add*`, `cat*`, `get*` have moved from `files` to the root namespace. Specifically, the following changes have been made: * `ipfs.files.add` => `ipfs.add` * `ipfs.files.addPullStream` => `ipfs.addPullStream` * `ipfs.files.addReadableStream` => `ipfs.addReadableStream` * `ipfs.files.cat` => `ipfs.cat` * `ipfs.files.catPullStream` => `ipfs.catPullStream` * `ipfs.files.catReadableStream` => `ipfs.catReadableStream` * `ipfs.files.get` => `ipfs.get` * `ipfs.files.getPullStream` => `ipfs.getPullStream` * `ipfs.files.getReadableStream` => `ipfs.getReadableStream` Additionally, `addFromFs`, `addFromUrl`, `addFromStream` have moved from `util` to the root namespace: * `ipfs.util.addFromFs` => `ipfs.addFromFs` * `ipfs.util.addFromUrl` => `ipfs.addFromUrl` * `ipfs.util.addFromStream` => `ipfs.addFromStream` License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent ac30a82 commit 39f4733

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+234
-762
lines changed
 

Diff for: ‎README.md

+38-107
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,27 @@ bitswap.unwant(key, (err) => {
100100
})
101101
```
102102

103-
### In a web browser through Browserify
103+
### In a web browser
104+
105+
**through Browserify**
104106

105107
Same as in Node.js, you just have to [browserify](http://browserify.org) the code before serving it. See the browserify repo for how to do that.
106108

107109
See the example in the [examples folder](/examples/bundle-browserify) to get a boilerplate.
108110

109-
### In a web browser through webpack
111+
**through webpack**
110112

111113
See the example in the [examples folder](/examples/bundle-webpack) to get an idea on how to use js-ipfs-api with webpack.
112114

113-
### In a web browser from CDN
115+
**from CDN**
114116

115-
Instead of a local installation (and browserification) you may request a remote copy of IPFS API from [unpkg CDN](https://unpkg.com/).
117+
Instead of a local installation (and browserification) you may request a remote copy of IPFS API from [unpkg CDN](https://unpkg.com/).
116118

117119
To always request the latest version, use the following:
118120

119121
```html
120122
<!-- loading the minified version -->
121123
<script src="https://unpkg.com/ipfs-api/dist/index.min.js"></script>
122-
123124
<!-- loading the human-readable (not minified) version -->
124125
<script src="https://unpkg.com/ipfs-api/dist/index.js"></script>
125126
```
@@ -141,13 +142,13 @@ crossorigin="anonymous"></script>
141142
CDN-based IPFS API provides the `IpfsApi` constructor as a method of the global `window` object. Example:
142143

143144
```js
144-
var ipfs = window.IpfsApi('localhost', '5001')
145+
const ipfs = window.IpfsApi('localhost', '5001')
145146
```
146147

147148
If you omit the host and port, the API will parse `window.host`, and use this information. This also works, and can be useful if you want to write apps that can be run from multiple different gateways:
148149

149150
```js
150-
var ipfs = window.IpfsApi()
151+
const ipfs = window.IpfsApi()
151152
```
152153

153154
### CORS
@@ -185,31 +186,34 @@ const ipfs = IpfsApi({
185186
186187
#### Files
187188

188-
- [files](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md)
189-
- [`ipfs.files.add(data, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesadd). Alias to `ipfs.add`.
190-
- [`ipfs.files.addPullStream([options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesaddpullstream)
191-
- [`ipfs.files.addReadableStream([options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesaddreadablestream)
192-
- [`ipfs.files.cat(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filescat). Alias to `ipfs.cat`.
193-
- [`ipfs.files.catPullStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filescatpullstream)
194-
- [`ipfs.files.catReadableStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filescatreadablestream)
195-
- [`ipfs.files.get(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesget). Alias to `ipfs.get`.
196-
- [`ipfs.files.getPullStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesgetpullstream)
197-
- [`ipfs.files.getReadableStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesgetreadablestream)
189+
- [Regular Files API](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md)
190+
- [`ipfs.add(data, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#add)
191+
- [`ipfs.addPullStream([options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addpullstream)
192+
- [`ipfs.addReadableStream([options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addreadablestream)
193+
- [`ipfs.addFromStream(stream, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addfromstream)
194+
- [`ipfs.addFromFs(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addfromfs)
195+
- [`ipfs.addFromUrl(url, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addfromurl)
196+
- [`ipfs.cat(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#cat)
197+
- [`ipfs.catPullStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#catpullstream)
198+
- [`ipfs.catReadableStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#catreadablestream)
199+
- [`ipfs.get(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#get)
200+
- [`ipfs.getPullStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#getpullstream)
201+
- [`ipfs.getReadableStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#getreadablestream)
198202
- [`ipfs.ls(ipfsPath, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#ls)
199203
- [`ipfs.lsPullStream(ipfsPath)`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#lspullstream)
200204
- [`ipfs.lsReadableStream(ipfsPath)`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#lsreadablestream)
201-
- [MFS (mutable file system) specific](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#mutable-file-system)
202-
- [`ipfs.files.cp([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filescp)
203-
- [`ipfs.files.flush([path], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesflush)
204-
- [`ipfs.files.ls([path], [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesls)
205-
- [`ipfs.files.mkdir(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesmkdir)
206-
- [`ipfs.files.mv([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesmv)
207-
- [`ipfs.files.read(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesread)
208-
- [`ipfs.files.readPullStream(path, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesreadpullstream)
209-
- [`ipfs.files.readReadableStream(path, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesreadreadablestream)
210-
- [`ipfs.files.rm(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesrm)
211-
- [`ipfs.files.stat(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesstat)
212-
- [`ipfs.files.write(path, content, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#fileswrite)
205+
- [MFS (mutable file system) specific](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#mutable-file-system)
206+
- [`ipfs.files.cp([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filescp)
207+
- [`ipfs.files.flush([path], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesflush)
208+
- [`ipfs.files.ls([path], [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesls)
209+
- [`ipfs.files.mkdir(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesmkdir)
210+
- [`ipfs.files.mv([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesmv)
211+
- [`ipfs.files.read(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesread)
212+
- [`ipfs.files.readPullStream(path, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesreadpullstream)
213+
- [`ipfs.files.readReadableStream(path, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesreadreadablestream)
214+
- [`ipfs.files.rm(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesrm)
215+
- [`ipfs.files.stat(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesstat)
216+
- [`ipfs.files.write(path, content, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#fileswrite)
213217

214218
- [block](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BLOCK.md)
215219
- [`ipfs.block.get(cid, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BLOCK.md#blockget)
@@ -359,69 +363,12 @@ A set of data types are exposed directly from the IPFS instance under `ipfs.type
359363
- [`ipfs.types.dagPB`](https://github.com/ipld/js-ipld-dag-pb)
360364
- [`ipfs.types.dagCBOR`](https://github.com/ipld/js-ipld-dag-cbor)
361365

362-
#### Utility functions
366+
#### Extra (util) functions
363367

364368
Adding to the methods defined by [`interface-ipfs-core`](https://github.com/ipfs/interface-ipfs-core), `js-ipfs-api` exposes a set of extra utility methods. These utility functions are scoped behind the `ipfs.util`.
365369

366370
Complete documentation for these methods is coming with: https://github.com/ipfs/js-ipfs-api/pull/305
367371

368-
##### Add files or entire directories from the FileSystem to IPFS
369-
370-
> `ipfs.util.addFromFs(path, option, callback)`
371-
372-
Reads a file or folder from `path` on the filesystem and adds it to IPFS. Options:
373-
- **recursive**: If `path` is a directory, use option `{ recursive: true }` to add the directory and all its sub-directories.
374-
- **ignore**: To exclude fileglobs from the directory, use option `{ ignore: ['ignore/this/folder/**', 'and/this/file'] }`.
375-
- **hidden**: hidden/dot files (files or folders starting with a `.`, for example, `.git/`) are not included by default. To add them, use the option `{ hidden: true }`.
376-
377-
```JavaScript
378-
ipfs.util.addFromFs('path/to/a/folder', { recursive: true , ignore: ['subfolder/to/ignore/**']}, (err, result) => {
379-
if (err) { throw err }
380-
console.log(result)
381-
})
382-
```
383-
384-
`result` is an array of objects describing the files that were added, such as:
385-
386-
```js
387-
[
388-
{
389-
path: 'test-folder',
390-
hash: 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6',
391-
size: 2278
392-
},
393-
// ...
394-
]
395-
```
396-
397-
##### Add a file from a URL to IPFS
398-
399-
> `ipfs.util.addFromURL(url, callback)`
400-
401-
```JavaScript
402-
ipfs.util.addFromURL('http://example.com/', (err, result) => {
403-
if (err) {
404-
throw err
405-
}
406-
console.log(result)
407-
})
408-
```
409-
410-
##### Add a file from a stream to IPFS
411-
412-
> `ipfs.util.addFromStream(stream, callback)`
413-
414-
This is very similar to `ipfs.files.add({path:'', content: stream})`. It is like the reverse of cat
415-
416-
```JavaScript
417-
ipfs.util.addFromStream(<readable-stream>, (err, result) => {
418-
if (err) {
419-
throw err
420-
}
421-
console.log(result)
422-
})
423-
```
424-
425372
##### Get endpoint configuration (host and port)
426373

427374
> `ipfs.util.getEndpointConfig()`
@@ -440,22 +387,6 @@ This contains an object with the crypto primitives
440387
441388
This contains an object with the is-ipfs utilities to help identifying IPFS resources
442389

443-
### Callbacks and Promises
444-
445-
If you do not pass in a callback all API functions will return a `Promise`. For example:
446-
447-
```js
448-
ipfs.id()
449-
.then((id) => {
450-
console.log('my id is: ', id)
451-
})
452-
.catch((err) => {
453-
console.log('Fail: ', err)
454-
})
455-
```
456-
457-
This relies on a global `Promise` object. If you are in an environment where that is not yet available you need to bring your own polyfill.
458-
459390
## Development
460391

461392
### Testing
@@ -466,10 +397,10 @@ We run tests by executing `npm test` in a terminal window. This will run both No
466397

467398
The js-ipfs-api is a work in progress. As such, there's a few things you can do right now to help out:
468399

469-
* **[Check out the existing issues](https://github.com/ipfs/js-ipfs-api/issues)**!
470-
* **Perform code reviews**. More eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.
471-
* **Add tests**. There can never be enough tests. Note that interface tests exist inside [`interface-ipfs-core`](https://github.com/ipfs/interface-ipfs-core/tree/master/js/src).
472-
* **Contribute to the [FAQ repository](https://github.com/ipfs/faq/issues)** with any questions you have about IPFS or any of the relevant technology. A good example would be asking, 'What is a merkledag tree?'. If you don't know a term, odds are, someone else doesn't either. Eventually, we should have a good understanding of where we need to improve communications and teaching together to make IPFS and IPN better.
400+
- **[Check out the existing issues](https://github.com/ipfs/js-ipfs-api/issues)**!
401+
- **Perform code reviews**. More eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.
402+
- **Add tests**. There can never be enough tests. Note that interface tests exist inside [`interface-ipfs-core`](https://github.com/ipfs/interface-ipfs-core/tree/master/js/src).
403+
- **Contribute to the [FAQ repository](https://github.com/ipfs/faq/issues)** with any questions you have about IPFS or any of the relevant technology. A good example would be asking, 'What is a merkledag tree?'. If you don't know a term, odds are, someone else doesn't either. Eventually, we should have a good understanding of where we need to improve communications and teaching together to make IPFS and IPN better.
473404

474405
**Want to hack on IPFS?**
475406

Diff for: ‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"eslint-plugin-react": "^7.11.1",
8686
"go-ipfs-dep": "~0.4.18",
8787
"gulp": "^3.9.1",
88-
"interface-ipfs-core": "~0.85.0",
88+
"interface-ipfs-core": "~0.86.0",
8989
"ipfsd-ctl": "~0.40.0",
9090
"pull-stream": "^3.6.9",
9191
"stream-equal": "^1.1.1"

0 commit comments

Comments
 (0)