From 39f4733cdac5fce2ec99a3eceaaa425095036ac6 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 13 Nov 2018 10:51:21 +0000 Subject: [PATCH] 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 --- README.md | 145 ++------ package.json | 2 +- src/add.js | 9 - src/cat.js | 9 - src/{files => files-mfs}/cp.js | 0 src/{files => files-mfs}/flush.js | 0 src/{files => files-mfs}/index.js | 13 +- src/{files => files-mfs}/ls.js | 0 src/{files => files-mfs}/mkdir.js | 0 src/{files => files-mfs}/mv.js | 0 src/{files => files-mfs}/read-pull-stream.js | 0 .../read-readable-stream.js | 0 src/{files => files-mfs}/read.js | 0 src/{files => files-mfs}/rm.js | 0 src/{files => files-mfs}/stat.js | 0 src/{files => files-mfs}/write.js | 0 .../add-from-fs.js} | 0 .../add-from-url.js} | 0 .../add-pull-stream.js | 0 .../add-readable-stream.js | 0 src/{files => files-regular}/add.js | 0 .../cat-pull-stream.js | 0 .../cat-readable-stream.js | 0 src/{files => files-regular}/cat.js | 0 .../get-pull-stream.js | 0 .../get-readable-stream.js | 0 src/{files => files-regular}/get.js | 0 src/files-regular/index.js | 25 ++ src/{ => files-regular}/ls-pull-stream.js | 2 +- src/{ => files-regular}/ls-readable-stream.js | 2 +- src/{ => files-regular}/ls.js | 2 +- src/get.js | 9 - src/index.js | 1 + src/utils/load-commands.js | 79 ++-- test/{files.spec.js => files-mfs.spec.js} | 87 ++--- test/fixtures/weird name folder [v0]/add | 22 -- test/fixtures/weird name folder [v0]/cat | 18 - .../weird name folder [v0]/files/hello.txt | 1 - .../weird name folder [v0]/files/ipfs.txt | 1 - .../weird name folder [v0]/hello-link | 1 - test/fixtures/weird name folder [v0]/ipfs-add | 14 - test/fixtures/weird name folder [v0]/ls | 18 - test/fixtures/weird name folder [v0]/version | 8 - test/get.spec.js | 12 +- test/interface.spec.js | 69 ++-- test/name.spec.js | 2 +- test/ping.spec.js | 6 +- test/refs.spec.js | 2 +- test/request-api.spec.js | 2 +- test/sub-modules.spec.js | 87 ++--- test/util.spec.js | 348 ------------------ 51 files changed, 234 insertions(+), 762 deletions(-) delete mode 100644 src/add.js delete mode 100644 src/cat.js rename src/{files => files-mfs}/cp.js (100%) rename src/{files => files-mfs}/flush.js (100%) rename src/{files => files-mfs}/index.js (54%) rename src/{files => files-mfs}/ls.js (100%) rename src/{files => files-mfs}/mkdir.js (100%) rename src/{files => files-mfs}/mv.js (100%) rename src/{files => files-mfs}/read-pull-stream.js (100%) rename src/{files => files-mfs}/read-readable-stream.js (100%) rename src/{files => files-mfs}/read.js (100%) rename src/{files => files-mfs}/rm.js (100%) rename src/{files => files-mfs}/stat.js (100%) rename src/{files => files-mfs}/write.js (100%) rename src/{util/fs-add.js => files-regular/add-from-fs.js} (100%) rename src/{util/url-add.js => files-regular/add-from-url.js} (100%) rename src/{files => files-regular}/add-pull-stream.js (100%) rename src/{files => files-regular}/add-readable-stream.js (100%) rename src/{files => files-regular}/add.js (100%) rename src/{files => files-regular}/cat-pull-stream.js (100%) rename src/{files => files-regular}/cat-readable-stream.js (100%) rename src/{files => files-regular}/cat.js (100%) rename src/{files => files-regular}/get-pull-stream.js (100%) rename src/{files => files-regular}/get-readable-stream.js (100%) rename src/{files => files-regular}/get.js (100%) create mode 100644 src/files-regular/index.js rename src/{ => files-regular}/ls-pull-stream.js (95%) rename src/{ => files-regular}/ls-readable-stream.js (95%) rename src/{ => files-regular}/ls.js (95%) delete mode 100644 src/get.js rename test/{files.spec.js => files-mfs.spec.js} (82%) delete mode 100644 test/fixtures/weird name folder [v0]/add delete mode 100644 test/fixtures/weird name folder [v0]/cat delete mode 100644 test/fixtures/weird name folder [v0]/files/hello.txt delete mode 100644 test/fixtures/weird name folder [v0]/files/ipfs.txt delete mode 120000 test/fixtures/weird name folder [v0]/hello-link delete mode 100755 test/fixtures/weird name folder [v0]/ipfs-add delete mode 100644 test/fixtures/weird name folder [v0]/ls delete mode 100644 test/fixtures/weird name folder [v0]/version diff --git a/README.md b/README.md index b502183802..a5fd30e6ff 100644 --- a/README.md +++ b/README.md @@ -100,26 +100,27 @@ bitswap.unwant(key, (err) => { }) ``` -### In a web browser through Browserify +### In a web browser + +**through Browserify** 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. See the example in the [examples folder](/examples/bundle-browserify) to get a boilerplate. -### In a web browser through webpack +**through webpack** See the example in the [examples folder](/examples/bundle-webpack) to get an idea on how to use js-ipfs-api with webpack. -### In a web browser from CDN +**from CDN** -Instead of a local installation (and browserification) you may request a remote copy of IPFS API from [unpkg CDN](https://unpkg.com/). +Instead of a local installation (and browserification) you may request a remote copy of IPFS API from [unpkg CDN](https://unpkg.com/). To always request the latest version, use the following: ```html - ``` @@ -141,13 +142,13 @@ crossorigin="anonymous"> CDN-based IPFS API provides the `IpfsApi` constructor as a method of the global `window` object. Example: ```js -var ipfs = window.IpfsApi('localhost', '5001') +const ipfs = window.IpfsApi('localhost', '5001') ``` 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: ```js -var ipfs = window.IpfsApi() +const ipfs = window.IpfsApi() ``` ### CORS @@ -185,31 +186,34 @@ const ipfs = IpfsApi({ #### Files -- [files](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md) - - [`ipfs.files.add(data, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesadd). Alias to `ipfs.add`. - - [`ipfs.files.addPullStream([options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesaddpullstream) - - [`ipfs.files.addReadableStream([options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesaddreadablestream) - - [`ipfs.files.cat(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filescat). Alias to `ipfs.cat`. - - [`ipfs.files.catPullStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filescatpullstream) - - [`ipfs.files.catReadableStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filescatreadablestream) - - [`ipfs.files.get(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesget). Alias to `ipfs.get`. - - [`ipfs.files.getPullStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesgetpullstream) - - [`ipfs.files.getReadableStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesgetreadablestream) +- [Regular Files API](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md) + - [`ipfs.add(data, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#add) + - [`ipfs.addPullStream([options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addpullstream) + - [`ipfs.addReadableStream([options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addreadablestream) + - [`ipfs.addFromStream(stream, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addfromstream) + - [`ipfs.addFromFs(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addfromfs) + - [`ipfs.addFromUrl(url, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addfromurl) + - [`ipfs.cat(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#cat) + - [`ipfs.catPullStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#catpullstream) + - [`ipfs.catReadableStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#catreadablestream) + - [`ipfs.get(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#get) + - [`ipfs.getPullStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#getpullstream) + - [`ipfs.getReadableStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#getreadablestream) - [`ipfs.ls(ipfsPath, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#ls) - [`ipfs.lsPullStream(ipfsPath)`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#lspullstream) - [`ipfs.lsReadableStream(ipfsPath)`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#lsreadablestream) - - [MFS (mutable file system) specific](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#mutable-file-system) - - [`ipfs.files.cp([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filescp) - - [`ipfs.files.flush([path], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesflush) - - [`ipfs.files.ls([path], [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesls) - - [`ipfs.files.mkdir(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesmkdir) - - [`ipfs.files.mv([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesmv) - - [`ipfs.files.read(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesread) - - [`ipfs.files.readPullStream(path, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesreadpullstream) - - [`ipfs.files.readReadableStream(path, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesreadreadablestream) - - [`ipfs.files.rm(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesrm) - - [`ipfs.files.stat(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesstat) - - [`ipfs.files.write(path, content, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#fileswrite) +- [MFS (mutable file system) specific](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#mutable-file-system) + - [`ipfs.files.cp([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filescp) + - [`ipfs.files.flush([path], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesflush) + - [`ipfs.files.ls([path], [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesls) + - [`ipfs.files.mkdir(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesmkdir) + - [`ipfs.files.mv([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesmv) + - [`ipfs.files.read(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesread) + - [`ipfs.files.readPullStream(path, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesreadpullstream) + - [`ipfs.files.readReadableStream(path, [options])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesreadreadablestream) + - [`ipfs.files.rm(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesrm) + - [`ipfs.files.stat(path, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#filesstat) + - [`ipfs.files.write(path, content, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#fileswrite) - [block](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BLOCK.md) - [`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 - [`ipfs.types.dagPB`](https://github.com/ipld/js-ipld-dag-pb) - [`ipfs.types.dagCBOR`](https://github.com/ipld/js-ipld-dag-cbor) -#### Utility functions +#### Extra (util) functions 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`. Complete documentation for these methods is coming with: https://github.com/ipfs/js-ipfs-api/pull/305 -##### Add files or entire directories from the FileSystem to IPFS - -> `ipfs.util.addFromFs(path, option, callback)` - -Reads a file or folder from `path` on the filesystem and adds it to IPFS. Options: -- **recursive**: If `path` is a directory, use option `{ recursive: true }` to add the directory and all its sub-directories. - - **ignore**: To exclude fileglobs from the directory, use option `{ ignore: ['ignore/this/folder/**', 'and/this/file'] }`. - - **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 }`. - -```JavaScript -ipfs.util.addFromFs('path/to/a/folder', { recursive: true , ignore: ['subfolder/to/ignore/**']}, (err, result) => { - if (err) { throw err } - console.log(result) -}) -``` - -`result` is an array of objects describing the files that were added, such as: - -```js -[ - { - path: 'test-folder', - hash: 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6', - size: 2278 - }, - // ... -] -``` - -##### Add a file from a URL to IPFS - -> `ipfs.util.addFromURL(url, callback)` - -```JavaScript -ipfs.util.addFromURL('http://example.com/', (err, result) => { - if (err) { - throw err - } - console.log(result) -}) -``` - -##### Add a file from a stream to IPFS - -> `ipfs.util.addFromStream(stream, callback)` - -This is very similar to `ipfs.files.add({path:'', content: stream})`. It is like the reverse of cat - -```JavaScript -ipfs.util.addFromStream(, (err, result) => { - if (err) { - throw err - } - console.log(result) -}) -``` - ##### Get endpoint configuration (host and port) > `ipfs.util.getEndpointConfig()` @@ -440,22 +387,6 @@ This contains an object with the crypto primitives This contains an object with the is-ipfs utilities to help identifying IPFS resources -### Callbacks and Promises - -If you do not pass in a callback all API functions will return a `Promise`. For example: - -```js -ipfs.id() - .then((id) => { - console.log('my id is: ', id) - }) - .catch((err) => { - console.log('Fail: ', err) - }) -``` - -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. - ## Development ### Testing @@ -466,10 +397,10 @@ We run tests by executing `npm test` in a terminal window. This will run both No The js-ipfs-api is a work in progress. As such, there's a few things you can do right now to help out: -* **[Check out the existing issues](https://github.com/ipfs/js-ipfs-api/issues)**! -* **Perform code reviews**. More eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs. -* **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). -* **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. +- **[Check out the existing issues](https://github.com/ipfs/js-ipfs-api/issues)**! +- **Perform code reviews**. More eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs. +- **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). +- **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. **Want to hack on IPFS?** diff --git a/package.json b/package.json index 3f2c56d6f9..0fdb0f4a21 100644 --- a/package.json +++ b/package.json @@ -85,7 +85,7 @@ "eslint-plugin-react": "^7.11.1", "go-ipfs-dep": "~0.4.18", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.85.0", + "interface-ipfs-core": "~0.86.0", "ipfsd-ctl": "~0.40.0", "pull-stream": "^3.6.9", "stream-equal": "^1.1.1" diff --git a/src/add.js b/src/add.js deleted file mode 100644 index 07915370e4..0000000000 --- a/src/add.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -const moduleConfig = require('./utils/module-config') - -module.exports = (arg) => { - const send = moduleConfig(arg) - - return require('./files/add')(send) -} diff --git a/src/cat.js b/src/cat.js deleted file mode 100644 index 2d381efbcb..0000000000 --- a/src/cat.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -const moduleConfig = require('./utils/module-config') - -module.exports = (arg) => { - const send = moduleConfig(arg) - - return require('./files/cat')(send) -} diff --git a/src/files/cp.js b/src/files-mfs/cp.js similarity index 100% rename from src/files/cp.js rename to src/files-mfs/cp.js diff --git a/src/files/flush.js b/src/files-mfs/flush.js similarity index 100% rename from src/files/flush.js rename to src/files-mfs/flush.js diff --git a/src/files/index.js b/src/files-mfs/index.js similarity index 54% rename from src/files/index.js rename to src/files-mfs/index.js index 6bcbd722bf..d4c6a8e7f3 100644 --- a/src/files/index.js +++ b/src/files-mfs/index.js @@ -6,20 +6,9 @@ module.exports = (arg) => { const send = moduleConfig(arg) return { - add: require('./add')(send), - addReadableStream: require('./add-readable-stream')(send), - addPullStream: require('./add-pull-stream')(send), - cat: require('./cat')(send), - catReadableStream: require('./cat-readable-stream')(send), - catPullStream: require('./cat-pull-stream')(send), - get: require('./get')(send), - getReadableStream: require('./get-readable-stream')(send), - getPullStream: require('./get-pull-stream')(send), - flush: require('./flush')(send), - - // Specific to MFS (for now) cp: require('./cp')(send), mkdir: require('./mkdir')(send), + flush: require('./flush')(send), stat: require('./stat')(send), rm: require('./rm')(send), ls: require('./ls')(send), diff --git a/src/files/ls.js b/src/files-mfs/ls.js similarity index 100% rename from src/files/ls.js rename to src/files-mfs/ls.js diff --git a/src/files/mkdir.js b/src/files-mfs/mkdir.js similarity index 100% rename from src/files/mkdir.js rename to src/files-mfs/mkdir.js diff --git a/src/files/mv.js b/src/files-mfs/mv.js similarity index 100% rename from src/files/mv.js rename to src/files-mfs/mv.js diff --git a/src/files/read-pull-stream.js b/src/files-mfs/read-pull-stream.js similarity index 100% rename from src/files/read-pull-stream.js rename to src/files-mfs/read-pull-stream.js diff --git a/src/files/read-readable-stream.js b/src/files-mfs/read-readable-stream.js similarity index 100% rename from src/files/read-readable-stream.js rename to src/files-mfs/read-readable-stream.js diff --git a/src/files/read.js b/src/files-mfs/read.js similarity index 100% rename from src/files/read.js rename to src/files-mfs/read.js diff --git a/src/files/rm.js b/src/files-mfs/rm.js similarity index 100% rename from src/files/rm.js rename to src/files-mfs/rm.js diff --git a/src/files/stat.js b/src/files-mfs/stat.js similarity index 100% rename from src/files/stat.js rename to src/files-mfs/stat.js diff --git a/src/files/write.js b/src/files-mfs/write.js similarity index 100% rename from src/files/write.js rename to src/files-mfs/write.js diff --git a/src/util/fs-add.js b/src/files-regular/add-from-fs.js similarity index 100% rename from src/util/fs-add.js rename to src/files-regular/add-from-fs.js diff --git a/src/util/url-add.js b/src/files-regular/add-from-url.js similarity index 100% rename from src/util/url-add.js rename to src/files-regular/add-from-url.js diff --git a/src/files/add-pull-stream.js b/src/files-regular/add-pull-stream.js similarity index 100% rename from src/files/add-pull-stream.js rename to src/files-regular/add-pull-stream.js diff --git a/src/files/add-readable-stream.js b/src/files-regular/add-readable-stream.js similarity index 100% rename from src/files/add-readable-stream.js rename to src/files-regular/add-readable-stream.js diff --git a/src/files/add.js b/src/files-regular/add.js similarity index 100% rename from src/files/add.js rename to src/files-regular/add.js diff --git a/src/files/cat-pull-stream.js b/src/files-regular/cat-pull-stream.js similarity index 100% rename from src/files/cat-pull-stream.js rename to src/files-regular/cat-pull-stream.js diff --git a/src/files/cat-readable-stream.js b/src/files-regular/cat-readable-stream.js similarity index 100% rename from src/files/cat-readable-stream.js rename to src/files-regular/cat-readable-stream.js diff --git a/src/files/cat.js b/src/files-regular/cat.js similarity index 100% rename from src/files/cat.js rename to src/files-regular/cat.js diff --git a/src/files/get-pull-stream.js b/src/files-regular/get-pull-stream.js similarity index 100% rename from src/files/get-pull-stream.js rename to src/files-regular/get-pull-stream.js diff --git a/src/files/get-readable-stream.js b/src/files-regular/get-readable-stream.js similarity index 100% rename from src/files/get-readable-stream.js rename to src/files-regular/get-readable-stream.js diff --git a/src/files/get.js b/src/files-regular/get.js similarity index 100% rename from src/files/get.js rename to src/files-regular/get.js diff --git a/src/files-regular/index.js b/src/files-regular/index.js new file mode 100644 index 0000000000..98effdeb1e --- /dev/null +++ b/src/files-regular/index.js @@ -0,0 +1,25 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + add: require('../files-regular/add')(send), + addReadableStream: require('../files-regular/add-readable-stream')(send), + addPullStream: require('../files-regular/add-pull-stream')(send), + addFromFs: require('../files-regular/add-from-fs')(send), + addFromUrl: require('../files-regular/add-from-url')(send), + addFromStream: require('../files-regular/add')(send), + cat: require('../files-regular/cat')(send), + catReadableStream: require('../files-regular/cat-readable-stream')(send), + catPullStream: require('../files-regular/cat-pull-stream')(send), + get: require('../files-regular/get')(send), + getReadableStream: require('../files-regular/get-readable-stream')(send), + getPullStream: require('../files-regular/get-pull-stream')(send), + ls: require('../files-regular/ls')(send), + lsReadableStream: require('../files-regular/ls-readable-stream')(send), + lsPullStream: require('../files-regular/ls-pull-stream')(send) + } +} diff --git a/src/ls-pull-stream.js b/src/files-regular/ls-pull-stream.js similarity index 95% rename from src/ls-pull-stream.js rename to src/files-regular/ls-pull-stream.js index eacb7e391f..3430d60c20 100644 --- a/src/ls-pull-stream.js +++ b/src/files-regular/ls-pull-stream.js @@ -1,6 +1,6 @@ 'use strict' -const moduleConfig = require('./utils/module-config') +const moduleConfig = require('../utils/module-config') const pull = require('pull-stream') const deferred = require('pull-defer') diff --git a/src/ls-readable-stream.js b/src/files-regular/ls-readable-stream.js similarity index 95% rename from src/ls-readable-stream.js rename to src/files-regular/ls-readable-stream.js index 26005c5ee8..9a15e1060f 100644 --- a/src/ls-readable-stream.js +++ b/src/files-regular/ls-readable-stream.js @@ -1,6 +1,6 @@ 'use strict' -const moduleConfig = require('./utils/module-config') +const moduleConfig = require('../utils/module-config') const Stream = require('readable-stream') module.exports = (arg) => { diff --git a/src/ls.js b/src/files-regular/ls.js similarity index 95% rename from src/ls.js rename to src/files-regular/ls.js index 2ae92483c7..772915e959 100644 --- a/src/ls.js +++ b/src/files-regular/ls.js @@ -1,7 +1,7 @@ 'use strict' const promisify = require('promisify-es6') -const moduleConfig = require('./utils/module-config') +const moduleConfig = require('../utils/module-config') module.exports = (arg) => { const send = moduleConfig(arg) diff --git a/src/get.js b/src/get.js deleted file mode 100644 index a7e04bce33..0000000000 --- a/src/get.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -const moduleConfig = require('./utils/module-config') - -module.exports = (arg) => { - const send = moduleConfig(arg) - - return require('./files/get')(send) -} diff --git a/src/index.js b/src/index.js index f0f73b782d..323cab6535 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ 'use strict' +/* global self */ const multiaddr = require('multiaddr') const loadCommands = require('./utils/load-commands') diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index 55b4570abd..4d203defc6 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -2,72 +2,75 @@ function requireCommands () { const cmds = { - // Files (not MFS) - add: require('../files/add'), - addReadableStream: require('../files/add-readable-stream'), - addPullStream: require('../files/add-pull-stream'), - cat: require('../files/cat'), - catReadableStream: require('../files/cat-readable-stream'), - catPullStream: require('../files/cat-pull-stream'), - get: require('../files/get'), - getReadableStream: require('../files/get-readable-stream'), - getPullStream: require('../files/get-pull-stream'), - ls: require('../ls'), - lsReadableStream: require('../ls-readable-stream'), - lsPullStream: require('../ls-pull-stream'), + // Files Regular (not MFS) + add: require('../files-regular/add'), + addReadableStream: require('../files-regular/add-readable-stream'), + addPullStream: require('../files-regular/add-pull-stream'), + addFromFs: require('../files-regular/add-from-fs'), + addFromURL: require('../files-regular/add-from-url'), + addFromStream: require('../files-regular/add'), + cat: require('../files-regular/cat'), + catReadableStream: require('../files-regular/cat-readable-stream'), + catPullStream: require('../files-regular/cat-pull-stream'), + get: require('../files-regular/get'), + getReadableStream: require('../files-regular/get-readable-stream'), + getPullStream: require('../files-regular/get-pull-stream'), + ls: require('../files-regular/ls'), + lsReadableStream: require('../files-regular/ls-readable-stream'), + lsPullStream: require('../files-regular/ls-pull-stream'), - bitswap: require('../bitswap'), + // Block block: require('../block'), + bitswap: require('../bitswap'), + + // Graph + dag: require('../dag'), + object: require('../object'), + pin: require('../pin'), + + // Network bootstrap: require('../bootstrap'), + dht: require('../dht'), + name: require('../name'), + ping: require('../ping'), + pingReadableStream: require('../ping-readable-stream'), + pingPullStream: require('../ping-pull-stream'), + swarm: require('../swarm'), + pubsub: require('../pubsub'), + dns: require('../dns'), + + // Miscellaneous commands: require('../commands'), config: require('../config'), - dag: require('../dag'), - dht: require('../dht'), diag: require('../diag'), id: require('../id'), key: require('../key'), log: require('../log'), mount: require('../mount'), - name: require('../name'), - object: require('../object'), - pin: require('../pin'), - ping: require('../ping'), - pingReadableStream: require('../ping-readable-stream'), - pingPullStream: require('../ping-pull-stream'), refs: require('../refs'), repo: require('../repo'), stop: require('../stop'), stats: require('../stats'), - swarm: require('../swarm'), - pubsub: require('../pubsub'), update: require('../update'), version: require('../version'), types: require('../types'), - resolve: require('../resolve'), - dns: require('../dns') + resolve: require('../resolve') } // shutdown is an alias for stop cmds.shutdown = cmds.stop - // TODO: crowding the 'files' namespace temporarily for interface-ipfs-core - // compatibility, until 'files vs mfs' naming decision is resolved. - cmds.files = function (send) { - const files = require('../files')(send) - - return files + // Files MFS (Mutable Filesystem) + cmds.files = (send) => { + return require('../files-mfs')(send) } - cmds.util = function (send, config) { - const util = { - addFromFs: require('../util/fs-add')(send), - addFromStream: require('../files/add')(send), - addFromURL: require('../util/url-add')(send), + cmds.util = (send, config) => { + return { getEndpointConfig: require('../util/get-endpoint-config')(config), crypto: require('libp2p-crypto'), isIPFS: require('is-ipfs') } - return util } return cmds diff --git a/test/files.spec.js b/test/files-mfs.spec.js similarity index 82% rename from test/files.spec.js rename to test/files-mfs.spec.js index 05477c41bc..2929bea166 100644 --- a/test/files.spec.js +++ b/test/files-mfs.spec.js @@ -51,8 +51,8 @@ describe('.files (the MFS API part)', function () { ipfsd.stop(done) }) - it('add file for testing', (done) => { - ipfs.files.add(testfile, (err, res) => { + it('.add file for testing', (done) => { + ipfs.add(testfile, (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) @@ -62,13 +62,13 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.add with Buffer module', (done) => { + it('.add with Buffer module', (done) => { let Buffer = require('buffer').Buffer let expectedBufferMultihash = 'QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX' let file = Buffer.from('hello') - ipfs.files.add(file, (err, res) => { + ipfs.add(file, (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) @@ -78,11 +78,11 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.add with empty path and buffer content', (done) => { + it('.add with empty path and buffer content', (done) => { const expectedHash = 'QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX' const content = Buffer.from('hello') - ipfs.files.add([{ path: '', content }], (err, res) => { + ipfs.add([{ path: '', content }], (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) @@ -92,11 +92,11 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.add with cid-version=1 and raw-leaves=false', (done) => { + it('.add with cid-version=1 and raw-leaves=false', (done) => { const expectedCid = 'zdj7Wh9x6gXdg4UAqhRYnjBTw9eJF7hvzUU4HjpnZXHYQz9jK' const options = { 'cid-version': 1, 'raw-leaves': false } - ipfs.files.add(testfile, options, (err, res) => { + ipfs.add(testfile, options, (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) @@ -106,11 +106,11 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.add with only-hash=true', function () { + it('.add with only-hash=true', function () { this.slow(10 * 1000) const content = String(Math.random() + Date.now()) - return ipfs.files.add(Buffer.from(content), { onlyHash: true }) + return ipfs.add(Buffer.from(content), { onlyHash: true }) .then(files => { expect(files).to.have.length(1) @@ -119,8 +119,8 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.add with options', (done) => { - ipfs.files.add(testfile, { pin: false }, (err, res) => { + it('.add with options', (done) => { + ipfs.add(testfile, { pin: false }, (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) @@ -130,13 +130,13 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.add pins by default', (done) => { + it('.add pins by default', (done) => { const newContent = Buffer.from(String(Math.random())) ipfs.pin.ls((err, pins) => { expect(err).to.not.exist() const initialPinCount = pins.length - ipfs.files.add(newContent, (err, res) => { + ipfs.add(newContent, (err, res) => { expect(err).to.not.exist() ipfs.pin.ls((err, pins) => { @@ -148,13 +148,13 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.add with pin=false', (done) => { + it('.add with pin=false', (done) => { const newContent = Buffer.from(String(Math.random())) ipfs.pin.ls((err, pins) => { expect(err).to.not.exist() const initialPinCount = pins.length - ipfs.files.add(newContent, { pin: false }, (err, res) => { + ipfs.add(newContent, { pin: false }, (err, res) => { expect(err).to.not.exist() ipfs.pin.ls((err, pins) => { @@ -167,7 +167,7 @@ describe('.files (the MFS API part)', function () { }) HASH_ALGS.forEach((name) => { - it(`files.add with hash=${name} and raw-leaves=false`, (done) => { + it(`.add with hash=${name} and raw-leaves=false`, (done) => { const content = String(Math.random() + Date.now()) const file = { path: content + '.txt', @@ -175,7 +175,7 @@ describe('.files (the MFS API part)', function () { } const options = { hash: name, 'raw-leaves': false } - ipfs.files.add([file], options, (err, res) => { + ipfs.add([file], options, (err, res) => { if (err) return done(err) expect(res).to.have.length(1) const cid = new CID(res[0].hash) @@ -185,7 +185,7 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.add file with progress option', (done) => { + it('.add file with progress option', (done) => { let progress let progressCount = 0 @@ -194,7 +194,7 @@ describe('.files (the MFS API part)', function () { progress = p } - ipfs.files.add(testfile, { progress: progressHandler }, (err, res) => { + ipfs.add(testfile, { progress: progressHandler }, (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) @@ -205,7 +205,7 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.add big file with progress option', (done) => { + it('.add big file with progress option', (done) => { let progress = 0 let progressCount = 0 @@ -215,7 +215,7 @@ describe('.files (the MFS API part)', function () { } // TODO: needs to be using a big file - ipfs.files.add(testfile, { progress: progressHandler }, (err, res) => { + ipfs.add(testfile, { progress: progressHandler }, (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) @@ -226,7 +226,7 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.add directory with progress option', (done) => { + it('.add directory with progress option', (done) => { let progress = 0 let progressCount = 0 @@ -236,7 +236,7 @@ describe('.files (the MFS API part)', function () { } // TODO: needs to be using a directory - ipfs.files.add(testfile, { progress: progressHandler }, (err, res) => { + ipfs.add(testfile, { progress: progressHandler }, (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) @@ -247,8 +247,8 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.add without progress options', (done) => { - ipfs.files.add(testfile, (err, res) => { + it('.add without progress options', (done) => { + ipfs.add(testfile, (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) @@ -257,7 +257,7 @@ describe('.files (the MFS API part)', function () { }) HASH_ALGS.forEach((name) => { - it(`files.add with hash=${name} and raw-leaves=false`, (done) => { + it(`.add with hash=${name} and raw-leaves=false`, (done) => { const content = String(Math.random() + Date.now()) const file = { path: content + '.txt', @@ -265,8 +265,9 @@ describe('.files (the MFS API part)', function () { } const options = { hash: name, 'raw-leaves': false } - ipfs.files.add([file], options, (err, res) => { - if (err) return done(err) + ipfs.add([file], options, (err, res) => { + expect(err).to.not.exist() + expect(res).to.have.length(1) const cid = new CID(res[0].hash) expect(mh.decode(cid.multihash).name).to.equal(name) @@ -275,14 +276,15 @@ describe('.files (the MFS API part)', function () { }) }) - it('files.addPullStream with object chunks and pull stream content', (done) => { + it('.addPullStream with object chunks and pull stream content', (done) => { const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm' pull( pull.values([{ content: pull.values([Buffer.from('test')]) }]), - ipfs.files.addPullStream(), + ipfs.addPullStream(), pull.collect((err, res) => { - if (err) return done(err) + expect(err).to.not.exist() + expect(res).to.have.length(1) expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 }) done() @@ -290,34 +292,35 @@ describe('.files (the MFS API part)', function () { ) }) - it('files.add with pull stream (callback)', (done) => { + it('.add with pull stream (callback)', (done) => { const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm' - ipfs.files.add(pull.values([Buffer.from('test')]), (err, res) => { - if (err) return done(err) + ipfs.add(pull.values([Buffer.from('test')]), (err, res) => { + expect(err).to.not.exist() + expect(res).to.have.length(1) expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 }) done() }) }) - it('files.add with pull stream (promise)', () => { + it('.add with pull stream (promise)', () => { const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm' - return ipfs.files.add(pull.values([Buffer.from('test')])) + return ipfs.add(pull.values([Buffer.from('test')])) .then((res) => { expect(res).to.have.length(1) expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 }) }) }) - it('files.add with array of objects with pull stream content', () => { + it('.add with array of objects with pull stream content', () => { const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm' - return ipfs.files.add([{ content: pull.values([Buffer.from('test')]) }]) + return ipfs.add([{ content: pull.values([Buffer.from('test')]) }]) .then((res) => { expect(res).to.have.length(1) - expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 }) + expect(res[0]).to.eql({ path: expectedCid, hash: expectedCid, size: 12 }) }) }) @@ -407,7 +410,7 @@ describe('.files (the MFS API part)', function () { it('files.write', (done) => { ipfs.files - .write('/test-folder/test-file-2.txt', Buffer.from('hello world'), {create: true}, (err) => { + .write('/test-folder/test-file-2.txt', Buffer.from('hello world'), { create: true }, (err) => { expect(err).to.not.exist() ipfs.files.read('/test-folder/test-file-2.txt', (err, buf) => { @@ -483,6 +486,6 @@ describe('.files (the MFS API part)', function () { }) it('files.rm', (done) => { - ipfs.files.rm('/test-folder', {recursive: true}, done) + ipfs.files.rm('/test-folder', { recursive: true }, done) }) }) diff --git a/test/fixtures/weird name folder [v0]/add b/test/fixtures/weird name folder [v0]/add deleted file mode 100644 index ce7fb7cc8c..0000000000 --- a/test/fixtures/weird name folder [v0]/add +++ /dev/null @@ -1,22 +0,0 @@ -'use strict' - -const ipfs = require('../src')('localhost', 5001) - -const f1 = 'Hello' -const f2 = 'World' - -ipfs.add([new Buffer(f1), new Buffer(f2)], function (err, res) { - if (err || !res) return console.log(err) - - for (let i = 0; i < res.length; i++) { - console.log(res[i]) - } -}) - -ipfs.add(['./files/hello.txt', './files/ipfs.txt'], function (err, res) { - if (err || !res) return console.log(err) - - for (let i = 0; i < res.length; i++) { - console.log(res[i]) - } -}) diff --git a/test/fixtures/weird name folder [v0]/cat b/test/fixtures/weird name folder [v0]/cat deleted file mode 100644 index 9bfc46638f..0000000000 --- a/test/fixtures/weird name folder [v0]/cat +++ /dev/null @@ -1,18 +0,0 @@ -'use strict' - -const ipfs = require('../src')('localhost', 5001) - -const hash = [ - 'QmdFyxZXsFiP4csgfM5uPu99AvFiKH62CSPDw5TP92nr7w', - 'QmY9cxiHqTFoWamkQVkpmmqzBrY3hCBEL2XNu3NtX74Fuu' -] - -ipfs.cat(hash, function (err, res) { - if (err || !res) return console.log(err) - - if (res.readable) { - res.pipe(process.stdout) - } else { - console.log(res) - } -}) diff --git a/test/fixtures/weird name folder [v0]/files/hello.txt b/test/fixtures/weird name folder [v0]/files/hello.txt deleted file mode 100644 index e965047ad7..0000000000 --- a/test/fixtures/weird name folder [v0]/files/hello.txt +++ /dev/null @@ -1 +0,0 @@ -Hello diff --git a/test/fixtures/weird name folder [v0]/files/ipfs.txt b/test/fixtures/weird name folder [v0]/files/ipfs.txt deleted file mode 100644 index 95a3116521..0000000000 --- a/test/fixtures/weird name folder [v0]/files/ipfs.txt +++ /dev/null @@ -1 +0,0 @@ -IPFS diff --git a/test/fixtures/weird name folder [v0]/hello-link b/test/fixtures/weird name folder [v0]/hello-link deleted file mode 120000 index 50b07e9e71..0000000000 --- a/test/fixtures/weird name folder [v0]/hello-link +++ /dev/null @@ -1 +0,0 @@ -files/hello.txt \ No newline at end of file diff --git a/test/fixtures/weird name folder [v0]/ipfs-add b/test/fixtures/weird name folder [v0]/ipfs-add deleted file mode 100755 index 962aa11fe6..0000000000 --- a/test/fixtures/weird name folder [v0]/ipfs-add +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env node - -'use strict' - -const ipfs = require('../src')('localhost', 5001) -const files = process.argv.slice(2) - -ipfs.add(files, {recursive: true}, function (err, res) { - if (err || !res) return console.log(err) - - for (let i = 0; i < res.length; i++) { - console.log('added', res[i].Hash, res[i].Name) - } -}) diff --git a/test/fixtures/weird name folder [v0]/ls b/test/fixtures/weird name folder [v0]/ls deleted file mode 100644 index c810fbead0..0000000000 --- a/test/fixtures/weird name folder [v0]/ls +++ /dev/null @@ -1,18 +0,0 @@ -'use strict' - -const ipfs = require('../src')('localhost', 5001) - -const hash = ['QmdbHK6gMiecyjjSoPnfJg6iKMF7v6E2NkoBgGpmyCoevh'] - -ipfs.ls(hash, function (err, res) { - if (err || !res) return console.log(err) - - res.Objects.forEach(function (node) { - console.log(node.Hash) - - console.log('Links [%d]', node.Links.length) - node.Links.forEach(function (link, i) { - console.log('[%d]', i, link) - }) - }) -}) diff --git a/test/fixtures/weird name folder [v0]/version b/test/fixtures/weird name folder [v0]/version deleted file mode 100644 index a792af011d..0000000000 --- a/test/fixtures/weird name folder [v0]/version +++ /dev/null @@ -1,8 +0,0 @@ -'use strict' - -const ipfs = require('../src')('localhost', 5001) - -ipfs.commands(function (err, res) { - if (err) throw err - console.log(res) -}) diff --git a/test/get.spec.js b/test/get.spec.js index 91d16f3fe7..29987bce17 100644 --- a/test/get.spec.js +++ b/test/get.spec.js @@ -37,12 +37,12 @@ describe('.get (specific go-ipfs features)', function () { ipfs = IPFSApi(_ipfsd.apiAddr) cb() }), - (cb) => ipfs.files.add(smallFile.data, cb) + (cb) => ipfs.add(smallFile.data, cb) ], done) }) after((done) => { - if (!ipfsd) return done() + if (!ipfsd) { return done() } ipfsd.stop(done) }) @@ -83,9 +83,7 @@ describe('.get (specific go-ipfs features)', function () { }) it('add path containing "+"s (for testing get)', (done) => { - if (!isNode) { - return done() - } + if (!isNode) { return done() } const filename = 'ti,c64x+mega++mod-pic.txt' const subdir = 'tmp/c++files' @@ -101,9 +99,7 @@ describe('.get (specific go-ipfs features)', function () { }) it('get path containing "+"s', (done) => { - if (!isNode) { - return done() - } + if (!isNode) { return done() } const cid = 'QmPkmARcqjo5fqK1V1o8cFsuaXxWYsnwCNLJUYS4KeZyff' let count = 0 diff --git a/test/interface.spec.js b/test/interface.spec.js index f91b8b9d58..d3be72baea 100644 --- a/test/interface.spec.js +++ b/test/interface.spec.js @@ -7,7 +7,7 @@ const CommonFactory = require('./utils/interface-common-factory') const IPFSApi = require('../src') const isWindows = process.platform && process.platform === 'win32' -describe('interface-ipfs-core tests', () => { +describe.only('interface-ipfs-core tests', () => { const defaultCommonFactory = CommonFactory.create() tests.bitswap(defaultCommonFactory, { @@ -91,9 +91,9 @@ describe('interface-ipfs-core tests', () => { ] }) - tests.files(defaultCommonFactory, { + tests.filesRegular(defaultCommonFactory, { skip: [ - // files.add + // .add isNode ? null : { name: 'should add a nested directory as array of tupples', reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' @@ -102,17 +102,32 @@ describe('interface-ipfs-core tests', () => { name: 'should add a nested directory as array of tupples with progress', reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' }, - // files.addPullStream + // .addPullStream isNode ? null : { name: 'should add pull stream of valid files and dirs', reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' }, - // files.addReadableStream + // .addReadableStream isNode ? null : { name: 'should add readable stream of valid files and dirs', reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' }, - // files.catPullStream + // .addFromStream + isNode ? null : { + name: 'addFromStream', + reason: 'Not designed to run in the browser' + }, + // .addFromFs + isNode ? null : { + name: 'addFromFs', + reason: 'Not designed to run in the browser' + }, + // .addFromUrl + isNode ? null : { + name: 'addFromUrl', + reason: 'Not designed to run in the browser' + }, + // .catPullStream { name: 'should export a chunk of a file', reason: 'TODO not implemented in go-ipfs yet' @@ -125,19 +140,31 @@ describe('interface-ipfs-core tests', () => { name: 'should export a chunk of a file in a Readable Stream', reason: 'TODO not implemented in go-ipfs yet' }, - // files.get + // .get isNode ? null : { name: 'should get a directory', reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' }, - // files.write - { - name: 'should write to deeply nested non existent file with create and parents flags', - reason: 'TODO remove when 0.4.18 is released https://github.com/ipfs/go-ipfs/pull/5359' + // .ls + isNode ? null : { + name: 'should ls with a base58 encoded CID', + reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' + }, + // .lsPullStream + isNode ? null : { + name: 'should pull stream ls with a base58 encoded CID', + reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' + }, + // .lsReadableStream + isNode ? null : { + name: 'should readable stream ls with a base58 encoded CID', + reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' } ] }) + tests.filesMFS(defaultCommonFactory) + tests.key(defaultCommonFactory, { skip: [ // key.export @@ -153,26 +180,6 @@ describe('interface-ipfs-core tests', () => { ] }) - tests.ls(defaultCommonFactory, { - skip: [ - // lsPullStream - isNode ? null : { - name: 'should pull stream ls with a base58 encoded CID', - reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' - }, - // lsReadableStream - isNode ? null : { - name: 'should readable stream ls with a base58 encoded CID', - reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' - }, - // ls - isNode ? null : { - name: 'should ls with a base58 encoded CID', - reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' - } - ] - }) - tests.miscellaneous(defaultCommonFactory, { skip: [ // stop diff --git a/test/name.spec.js b/test/name.spec.js index 3e5d0402b0..58de812fe7 100644 --- a/test/name.spec.js +++ b/test/name.spec.js @@ -53,7 +53,7 @@ describe('.name', () => { }) }, (cb) => { - ipfs.files.add(testfile, (err, res) => { + ipfs.add(testfile, (err, res) => { expect(err).to.not.exist() testFileCid = res[0].hash cb() diff --git a/test/ping.spec.js b/test/ping.spec.js index 06848ee78f..184f99b8d6 100644 --- a/test/ping.spec.js +++ b/test/ping.spec.js @@ -21,7 +21,7 @@ function isPong (pingResponse) { } describe('.ping', function () { - this.timeout(10 * 1000) + this.timeout(20 * 1000) let ipfs let ipfsd @@ -131,7 +131,7 @@ describe('.ping', function () { it('.ping fails with count & n', function (done) { this.timeout(20 * 1000) - ipfs.ping(otherId, {count: 2, n: 2}, (err, res) => { + ipfs.ping(otherId, { count: 2, n: 2 }, (err, res) => { expect(err).to.exist() done() }) @@ -189,7 +189,7 @@ describe('.ping', function () { it('message conversion fails if invalid message is received', () => { const messageConverter = new PingMessageStream() expect(() => { - messageConverter.write({some: 'InvalidMessage'}) + messageConverter.write({ some: 'InvalidMessage' }) }).to.throw('Invalid ping message received') }) }) diff --git a/test/refs.spec.js b/test/refs.spec.js index 7ce2454551..4f6cc28645 100644 --- a/test/refs.spec.js +++ b/test/refs.spec.js @@ -39,7 +39,7 @@ describe('.refs', function () { (_ipfsd, cb) => { ipfsd = _ipfsd ipfs = IPFSApi(_ipfsd.apiAddr) - ipfs.util.addFromFs(filesPath, { recursive: true }, cb) + ipfs.addFromFs(filesPath, { recursive: true }, cb) }, (hashes, cb) => { folder = hashes[hashes.length - 1].hash diff --git a/test/request-api.spec.js b/test/request-api.spec.js index 63a2dce246..8f43a3fae5 100644 --- a/test/request-api.spec.js +++ b/test/request-api.spec.js @@ -54,7 +54,7 @@ describe('trailer headers', () => { server.listen(6001, () => { const ipfs = ipfsAPI('/ip4/127.0.0.1/tcp/6001') /* eslint-disable */ - ipfs.files.add(Buffer.from('Hello there!'), (err, res) => { + ipfs.add(Buffer.from('Hello there!'), (err, res) => { // TODO: error's are not being correctly // propagated with Trailer headers yet // expect(err).to.exist() diff --git a/test/sub-modules.spec.js b/test/sub-modules.spec.js index 1921b0228e..0401eccfe9 100644 --- a/test/sub-modules.spec.js +++ b/test/sub-modules.spec.js @@ -165,26 +165,37 @@ describe('submodules', () => { expect(pubsub.peers).to.be.a('function') }) - it('files', () => { - const files = require('../src/files')(config) - - expect(files.add).to.be.a('function') - expect(files.addReadableStream).to.be.a('function') - expect(files.addPullStream).to.be.a('function') - expect(files.get).to.be.a('function') - expect(files.getReadableStream).to.be.a('function') - expect(files.getPullStream).to.be.a('function') - expect(files.cat).to.be.a('function') - expect(files.catReadableStream).to.be.a('function') - expect(files.catPullStream).to.be.a('function') - expect(files.cp).to.be.a('function') - expect(files.ls).to.be.a('function') - expect(files.mkdir).to.be.a('function') - expect(files.stat).to.be.a('function') - expect(files.rm).to.be.a('function') - expect(files.read).to.be.a('function') - expect(files.write).to.be.a('function') - expect(files.mv).to.be.a('function') + it('files regular API', () => { + const filesRegular = require('../src/files-regular')(config) + + expect(filesRegular.add).to.be.a('function') + expect(filesRegular.addReadableStream).to.be.a('function') + expect(filesRegular.addPullStream).to.be.a('function') + expect(filesRegular.addFromStream).to.be.a('function') + expect(filesRegular.addFromFs).to.be.a('function') + expect(filesRegular.addFromUrl).to.be.a('function') + expect(filesRegular.get).to.be.a('function') + expect(filesRegular.getReadableStream).to.be.a('function') + expect(filesRegular.getPullStream).to.be.a('function') + expect(filesRegular.cat).to.be.a('function') + expect(filesRegular.catReadableStream).to.be.a('function') + expect(filesRegular.catPullStream).to.be.a('function') + expect(filesRegular.ls).to.be.a('function') + expect(filesRegular.lsReadableStream).to.be.a('function') + expect(filesRegular.lsPullStream).to.be.a('function') + }) + + it('files MFS API', () => { + const filesMFS = require('../src/files-mfs')(config) + + expect(filesMFS.cp).to.be.a('function') + expect(filesMFS.ls).to.be.a('function') + expect(filesMFS.mkdir).to.be.a('function') + expect(filesMFS.stat).to.be.a('function') + expect(filesMFS.rm).to.be.a('function') + expect(filesMFS.read).to.be.a('function') + expect(filesMFS.write).to.be.a('function') + expect(filesMFS.mv).to.be.a('function') }) it('commands', () => { @@ -193,52 +204,16 @@ describe('submodules', () => { expect(commands).to.be.a('function') }) - it('ls', () => { - const ls = require('../src/ls')(config) - - expect(ls).to.be.a('function') - }) - it('mount', () => { const mount = require('../src/mount')(config) expect(mount).to.be.a('function') }) - it('fs-add', () => { - const fsAdd = require('../src/util/fs-add')(config) - - expect(fsAdd).to.be.a('function') - }) - - it('url-add', () => { - const urlAdd = require('../src/util/url-add')(config) - - expect(urlAdd).to.be.a('function') - }) - it('refs', () => { const refs = require('../src/refs')(config) expect(refs).to.be.a('function') expect(refs.local).to.be.a('function') }) - - it('add', () => { - const add = require('../src/add')(config) - - expect(add).to.be.a('function') - }) - - it('get', () => { - const get = require('../src/get')(config) - - expect(get).to.be.a('function') - }) - - it('cat', () => { - const cat = require('../src/cat')(config) - - expect(cat).to.be.a('function') - }) }) diff --git a/test/util.spec.js b/test/util.spec.js index 6109e5cc9f..559f9abd7d 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -7,18 +7,9 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) const isNode = require('detect-node') -const path = require('path') -const fs = require('fs') -const os = require('os') -const http = require('http') -const https = require('https') -const each = require('async/each') -const waterfall = require('async/waterfall') -const parallel = require('async/parallel') const IPFSApi = require('../src') const f = require('./utils/factory') -const expectTimeout = require('./utils/expect-timeout') describe('.util', () => { if (!isNode) { return } @@ -43,345 +34,6 @@ describe('.util', () => { ipfsd.stop(done) }) - it('.streamAdd', (done) => { - const tfpath = path.join(__dirname, '/fixtures/testfile.txt') - const rs = fs.createReadStream(tfpath) - rs.path = '' // clean the path for testing purposes - - ipfs.util.addFromStream(rs, (err, result) => { - expect(err).to.not.exist() - expect(result.length).to.equal(1) - done() - }) - }) - - describe('.fsAdd should add', () => { - it('a directory', (done) => { - const filesPath = path.join(__dirname, '/fixtures/test-folder') - ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => { - expect(err).to.not.exist() - expect(result.length).to.be.above(8) - done() - }) - }) - - it('a directory with an odd name', (done) => { - const filesPath = path.join(__dirname, '/fixtures/weird name folder [v0]') - ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => { - expect(err).to.not.exist() - expect(result.length).to.be.above(8) - done() - }) - }) - - it('add and ignore a directory', (done) => { - const filesPath = path.join(__dirname, '/fixtures/test-folder') - ipfs.util.addFromFs(filesPath, { recursive: true, ignore: ['files/**'] }, (err, result) => { - expect(err).to.not.exist() - expect(result.length).to.be.below(9) - done() - }) - }) - - it('a file', (done) => { - const filePath = path.join(__dirname, '/fixtures/testfile.txt') - ipfs.util.addFromFs(filePath, (err, result) => { - expect(err).to.not.exist() - expect(result.length).to.be.equal(1) - expect(result[0].path).to.be.equal('testfile.txt') - done() - }) - }) - - it('a hidden file in a directory', (done) => { - const filesPath = path.join(__dirname, '/fixtures/test-folder') - ipfs.util.addFromFs(filesPath, { recursive: true, hidden: true }, (err, result) => { - expect(err).to.not.exist() - expect(result.length).to.be.above(10) - expect(result.map(object => object.path)).to.include('test-folder/.hiddenTest.txt') - expect(result.map(object => object.hash)).to.include('QmdbAjVmLRdpFyi8FFvjPfhTGB2cVXvWLuK7Sbt38HXrtt') - done() - }) - }) - - it('with only-hash=true', function () { - this.slow(10 * 1000) - const content = String(Math.random() + Date.now()) - const filepath = path.join(os.tmpdir(), `${content}.txt`) - fs.writeFileSync(filepath, content) - - return ipfs.util.addFromFs(filepath, { onlyHash: true }) - .then(out => { - fs.unlinkSync(filepath) - return expectTimeout(ipfs.object.get(out[0].hash), 4000) - }) - }) - }) - - describe('.urlAdd', () => { - let testServers = [] - - const sslOpts = { - key: fs.readFileSync(path.join(__dirname, 'fixtures', 'ssl', 'privkey.pem')), - cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'ssl', 'cert.pem')) - } - - const startTestServer = (handler, opts, cb) => { - if (typeof opts === 'function') { - cb = opts - opts = {} - } - - const server = opts.secure - ? https.createServer(sslOpts, handler) - : http.createServer(handler) - - server.listen((err) => { - if (err) return cb(err) - testServers.push(server) - cb(null, server) - }) - } - - beforeEach(() => { - // Instructs node to not reject our snake oil SSL certificate when it - // can't verify the certificate authority - process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 - }) - - afterEach((done) => { - // Reinstate unauthorised SSL cert rejection - process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1 - - each(testServers, (server, cb) => server.close(cb), (err) => { - testServers = [] - done(err) - }) - }) - - it('http', (done) => { - const data = Buffer.from(`TEST${Date.now()}`) - - parallel({ - server: (cb) => { - const handler = (req, res) => { - res.write(data) - res.end() - } - startTestServer(handler, cb) - }, - expectedResult: (cb) => ipfs.add(data, cb) - }, (err, taskResult) => { - expect(err).to.not.exist() - const { server, expectedResult } = taskResult - - const url = `http://127.0.0.1:${server.address().port}/` - ipfs.util.addFromURL(url, (err, result) => { - expect(err).to.not.exist() - expect(result).to.deep.equal(expectedResult) - done() - }) - }) - }) - - it('https', (done) => { - const data = Buffer.from(`TEST${Date.now()}`) - - parallel({ - server: (cb) => { - const handler = (req, res) => { - res.write(data) - res.end() - } - startTestServer(handler, { secure: true }, cb) - }, - expectedResult: (cb) => ipfs.add(data, cb) - }, (err, taskResult) => { - expect(err).to.not.exist() - const { server, expectedResult } = taskResult - - const url = `https://127.0.0.1:${server.address().port}/` - ipfs.util.addFromURL(url, (err, result) => { - expect(err).to.not.exist() - expect(result).to.deep.equal(expectedResult) - done() - }) - }) - }) - - it('http with redirection', (done) => { - const data = Buffer.from(`TEST${Date.now()}`) - - waterfall([ - (cb) => { - const handler = (req, res) => { - res.write(data) - res.end() - } - startTestServer(handler, cb) - }, - (serverA, cb) => { - const url = `http://127.0.0.1:${serverA.address().port}` - const handler = (req, res) => { - res.statusCode = 302 - res.setHeader('Location', url) - res.end() - } - startTestServer(handler, (err, serverB) => { - if (err) return cb(err) - cb(null, { a: serverA, b: serverB }) - }) - } - ], (err, servers) => { - expect(err).to.not.exist() - - ipfs.add(data, (err, res) => { - expect(err).to.not.exist() - - const expectedHash = res[0].hash - const url = `http://127.0.0.1:${servers.b.address().port}` - - ipfs.util.addFromURL(url, (err, result) => { - expect(err).to.not.exist() - expect(result[0].hash).to.equal(expectedHash) - done() - }) - }) - }) - }) - - it('https with redirection', (done) => { - const data = Buffer.from(`TEST${Date.now()}`) - - waterfall([ - (cb) => { - const handler = (req, res) => { - res.write(data) - res.end() - } - startTestServer(handler, { secure: true }, cb) - }, - (serverA, cb) => { - const url = `https://127.0.0.1:${serverA.address().port}` - const handler = (req, res) => { - res.statusCode = 302 - res.setHeader('Location', url) - res.end() - } - startTestServer(handler, { secure: true }, (err, serverB) => { - if (err) return cb(err) - cb(null, { a: serverA, b: serverB }) - }) - } - ], (err, servers) => { - expect(err).to.not.exist() - - ipfs.add(data, (err, res) => { - expect(err).to.not.exist() - - const expectedHash = res[0].hash - const url = `https://127.0.0.1:${servers.b.address().port}` - - ipfs.util.addFromURL(url, (err, result) => { - expect(err).to.not.exist() - expect(result[0].hash).to.equal(expectedHash) - done() - }) - }) - }) - }) - - it('with only-hash=true', (done) => { - const handler = (req, res) => { - res.write(`TEST${Date.now()}`) - res.end() - } - - startTestServer(handler, (err, server) => { - expect(err).to.not.exist() - - const url = `http://127.0.0.1:${server.address().port}/` - - ipfs.util.addFromURL(url, { onlyHash: true }, (err, res) => { - expect(err).to.not.exist() - - // A successful object.get for this size data took my laptop ~14ms - let didTimeout = false - const timeoutId = setTimeout(() => { - didTimeout = true - done() - }, 500) - - ipfs.object.get(res[0].hash, () => { - clearTimeout(timeoutId) - if (didTimeout) return - expect(new Error('did not timeout')).to.not.exist() - }) - }) - }) - }) - - it('with wrap-with-directory=true', (done) => { - const filename = `TEST${Date.now()}.txt` - const data = Buffer.from(`TEST${Date.now()}`) - - parallel({ - server: (cb) => startTestServer((req, res) => { - res.write(data) - res.end() - }, cb), - expectedResult: (cb) => { - ipfs.add([{ path: filename, content: data }], { wrapWithDirectory: true }, cb) - } - }, (err, taskResult) => { - expect(err).to.not.exist() - - const { server, expectedResult } = taskResult - const url = `http://127.0.0.1:${server.address().port}/${filename}?foo=bar#buzz` - - ipfs.util.addFromURL(url, { wrapWithDirectory: true }, (err, result) => { - expect(err).to.not.exist() - expect(result).to.deep.equal(expectedResult) - done() - }) - }) - }) - - it('with wrap-with-directory=true and URL-escaped file name', (done) => { - const filename = '320px-Domažlice,_Jiráskova_43_(9102).jpg' - const data = Buffer.from(`TEST${Date.now()}`) - - parallel({ - server: (cb) => startTestServer((req, res) => { - res.write(data) - res.end() - }, cb), - expectedResult: (cb) => { - ipfs.add([{ path: filename, content: data }], { wrapWithDirectory: true }, cb) - } - }, (err, taskResult) => { - expect(err).to.not.exist() - - const { server, expectedResult } = taskResult - const url = `http://127.0.0.1:${server.address().port}/${encodeURIComponent(filename)}?foo=bar#buzz` - - ipfs.util.addFromURL(url, { wrapWithDirectory: true }, (err, result) => { - expect(err).to.not.exist() - expect(result).to.deep.equal(expectedResult) - done() - }) - }) - }) - - it('with invalid url', (done) => { - ipfs.util.addFromURL('http://invalid', (err, result) => { - expect(err.code).to.equal('ENOTFOUND') - expect(result).to.not.exist() - done() - }) - }) - }) - describe('.getEndpointConfig', () => { it('should return the endpoint configured host and port', function () { const endpoint = ipfs.util.getEndpointConfig()