diff --git a/README.md b/README.md index 212f45f86c..6741f12557 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,9 @@ This project is available through [npm](https://www.npmjs.com/). To install run > npm install ipfs --save ``` -Requires npm@3 and node@6 or above, tested on OSX & Linux, expected to work on Windows. +We support both the Current and Active LTS versions of Node.js. Please see [nodejs.org](https://nodejs.org/) for what these currently are. + +This project is tested on OSX & Linux, expected to work on Windows. ### Use in Node.js diff --git a/examples/ipfs-101/1.js b/examples/ipfs-101/1.js index 9f206d543a..664af3d90c 100644 --- a/examples/ipfs-101/1.js +++ b/examples/ipfs-101/1.js @@ -1,32 +1,22 @@ 'use strict' -const series = require('async/series') const IPFS = require('ipfs') const node = new IPFS() -let fileMultihash - -series([ - (cb) => node.on('ready', cb), - (cb) => node.version((err, version) => { - if (err) { return cb(err) } - console.log('Version:', version.version) - cb() - }), - (cb) => node.files.add({ + +node.on('ready', async () => { + const version = await node.version() + + console.log('Version:', version.version) + + const filesAdded = await node.files.add({ path: 'hello.txt', content: Buffer.from('Hello World 101') - }, (err, filesAdded) => { - if (err) { return cb(err) } - - console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash) - fileMultihash = filesAdded[0].hash - cb() - }), - (cb) => node.files.cat(fileMultihash, (err, data) => { - if (err) { return cb(err) } - - console.log('\nFile content:') - process.stdout.write(data) }) -]) + + console.log('Added file:', filesAdded[0].path, filesAdded[0].hash) + + const fileBuffer = await node.files.cat(filesAdded[0].hash) + + console.log('Added file contents:', fileBuffer.toString()) +}) diff --git a/examples/ipfs-101/README.md b/examples/ipfs-101/README.md index df44debe7e..98e969f534 100644 --- a/examples/ipfs-101/README.md +++ b/examples/ipfs-101/README.md @@ -1,80 +1,91 @@ # IPFS 101, spawn a node and add a file to the IPFS network -In this tutorial, we go through spawning an IPFS node, adding a file and cat'ing the file multihash locally and throught the gateway. +In this tutorial, we go through spawning an IPFS node, adding a file and cat'ing the file multihash locally and through the gateway. -You can find a complete version of this tutorial in [1.js](./1.js). For this tutorial, you need to install the following dependencies: `ipfs` and `async` using `npm install ipfs async`. +You can find a complete version of this tutorial in [1.js](./1.js). For this tutorial, you need to install `ipfs` using `npm install ipfs`. Creating an IPFS instance can be done in one line, after requiring the module, you simply have to: -```JavaScript +```js const IPFS = require('ipfs') const node = new IPFS() ``` -We can listen for the `ready` event to learn when the node is ready to be used. In this part, we start using `async/series` to help us manage the async flow. As a test, we are going to check the version of the node. +We can listen for the `ready` event to learn when the node is ready to be used. Within the ready event, we'll use `async`/`await` to help us manage the async flow. -```JavaScript +As a test, we are going to check the version of the node. + +```js const IPFS = require('ipfs') const node = new IPFS() -series([ - (cb) => node.on('ready', cb), - (cb) => node.version((err, version) => { - if (err) { return cb(err) } - console.log('Version:', version.version) - cb() - }) -]) +node.on('ready', async () => { + const version = await node.version() + + console.log('Version:', version.version) +}) ``` +(If you prefer not to use `async`/`await`, you can instead use `.then()` as you would with any promise, +or pass an [error-first callback](https://nodejs.org/api/errors.html#errors_error_first_callbacks), e.g. `node.version((err, version) => { ... })`) + Running the code above gets you: ```bash > node 1.js -IPFS Version: 0.25.0 +Version: 0.31.2 ``` -Now lets make it more interesting and add a file to IPFS. We can do it by adding another async call to the series that uses the `node.files.add` call. You can learn about IPFS API for files at [interface-ipfs-core](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md). - -```JavaScript -// Create the File to add, a file consists of a path + content. More details on -// https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md -(cb) => node.files.add({ - path: 'hello.txt', - content: Buffer.from('Hello World') -}, (err, filesAdded) => { - if (err) { return cb(err) } - - // Once the file is added, we get back an object containing the path, the - // multihash and the sie of the file - console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash) - fileMultihash = filesAdded[0].hash - cb() +Now let's make it more interesting and add a file to IPFS using `node.files.add`. A file consists of a path and content. + +You can learn about the IPFS File API at [interface-ipfs-core](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md). + +```js +node.on('ready', async () => { + const version = await node.version() + + console.log('Version:', version.version) + + const filesAdded = await node.files.add({ + path: 'hello.txt', + content: Buffer.from('Hello World 101') + }) + + console.log('Added file:', filesAdded[0].path, filesAdded[0].hash) }) ``` -If you avoid calling that last `cb()`, the program won't exit enabling you to go to an IPFS Gateway and load the printed hash from a gateway. Go ahead and try it! +You can now go to an IPFS Gateway and load the printed hash from a gateway. Go ahead and try it! ```bash > node 1.js -Version: 0.25.0 +Version: 0.31.2 Added file: hello.txt QmXgZAUWd8yo4tvjBETqzUy3wLx5YRzuDwUQnBwRGrAmAo # Copy that hash and load it on the gateway, here is a prefiled url: # https://ipfs.io/ipfs/QmXgZAUWd8yo4tvjBETqzUy3wLx5YRzuDwUQnBwRGrAmAo ``` -The last step of this tutorial is retrieving the file back using the `cat` 😺 call. Add another step on the series chain that does the following: +The last step of this tutorial is retrieving the file back using the `cat` 😺 call. + +```js +node.on('ready', async () => { + const version = await node.version() + + console.log('Version:', version.version) + + const filesAdded = await node.files.add({ + path: 'hello.txt', + content: Buffer.from('Hello World 101') + }) + + console.log('Added file:', filesAdded[0].path, filesAdded[0].hash) -```JavaScript -(cb) => node.files.cat(fileMultihash, (err, data) => { - if (err) { return cb(err) } + const fileBuffer = await node.files.cat(filesAdded[0].hash) - console.log('\nFile content:') - // print the file to the terminal and then exit the program - process.stdout.write(data) + console.log('Added file contents:', fileBuffer.toString()) }) ``` diff --git a/examples/ipfs-101/package.json b/examples/ipfs-101/package.json index 4973b2c618..7b77539945 100644 --- a/examples/ipfs-101/package.json +++ b/examples/ipfs-101/package.json @@ -9,7 +9,6 @@ "author": "David Dias ", "license": "MIT", "dependencies": { - "async": "^2.6.0", "ipfs": "file:../../" } }