Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,46 +196,54 @@ The HTTP-API exposed by the js-ipfs daemon follows the [`http-api-spec`](https:/

#### Create a IPFS node instance

The basic startup flow involves (optionally) creating a Repo, creating an IPFS node, `init`-ing it so it can generate its keys, `load`-ing its configuration, and putting it online with `goOnline`. Here is a structural example:
Creating an IPFS instance couldn't be easier, all you have to do is:

```JavaScript
// Create the IPFS node instance
const node = new IPFS()

node.on('start', () => {
// Your now is ready to use \o/

// stopping a node
node.stop(() => {
// node is now 'offline'
})
})
```
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to make people happier :)


#### Advanced options when creating an IPFS node.

When starting a node, you can:

```JavaScript
// IPFS will need a repo, it can create one for you or you can pass
// it a repo instance of the type IPFS Repo
// https://github.com/ipfs/js-ipfs-repo
const repo = <IPFS Repo instance or repo path>

// Create the IPFS node instance
const node = new IPFS({
repo: repo,
EXPERIMENTAL: {
pubsub: false
init: true, // default
// init: false,
// init: {
// bits: 1024 // size of the RSA key generated
// },
start: true,
// start: false,
EXPERIMENTAL: { // enable experimental features
pubsub: true
},
config: { // overload the default config
Addresses: {
Swarm: [
'/ip4/127.0.0.1/tcp/1337'
]
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No more get/set config madness :)

}
})

// We need to init our repo, in this case the repo was empty
// We are picking 2048 bits for the RSA key that will be our PeerId
node.init({ emptyRepo: true, bits: 2048 }, (err) => {
if (err) { throw err }

// Once the repo is initiated, we have to load it so that the IPFS
// instance has its config values. This is useful when you have
// previous created repos and you don't need to generate a new one
node.load((err) => {
if (err) { throw err }

// Last but not the least, we want our IPFS node to use its peer
// connections to fetch and serve blocks from.
node.goOnline((err) => {
if (err) { throw err }
// Here you should be good to go and call any IPFS function
})
})
```

> We are working on making this init process better, see https://github.com/ipfs/js-ipfs/issues/556 for the discussion.

More examples can be found in the [examples folder](./examples)

### [Tutorials and Examples](/examples)

You can find some examples and tutorials in the [examples](/examples) folder, these exist to help you get started using `js-ipfs`.
Expand Down
8 changes: 3 additions & 5 deletions examples/basics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const IPFS = require('../../src/core')
*/
const node = new IPFS({
repo: path.join(os.tmpdir() + '/' + new Date().toString()),
init: false,
start: false,
EXPERIMENTAL: {
pubsub: false
}
Expand Down Expand Up @@ -42,14 +44,10 @@ series([
* Initialize the repo for this node
*/
(cb) => node.init({ emptyRepo: true, bits: 2048 }, cb),
/*
* Load the repo config into the IPFS node
*/
(cb) => node.load(cb),
/*
* Take the node online (bitswap, network and so on)
*/
(cb) => node.goOnline(cb),
(cb) => node.start(cb),
/*
* Add a file to IPFS - Complete Files API on:
* https://github.com/ipfs/interface-ipfs-core/tree/master/API/files
Expand Down
31 changes: 4 additions & 27 deletions examples/browser-script-tag/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,28 @@
<title>IPFS in the Browser</title>
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
<script type="text/javascript">
// We provide a hosted signalling endpoint that you can use to discover
// and dial to other nodes. It is hosted at `star-signal.cloud.ipfs.team`
// If you run your own signalling, you can change this multiaddr.
const SIGNALING_SERVER = '/libp2p-webrtc-star/dns4/star-signal.cloud.ipfs.team/wss/ipfs/'

const repoPath = 'ipfs-' + Math.random()

// Create an IPFS node
const node = new Ipfs({
init: false,
start: false
repo: repoPath
})

// Init the node
node.init(handleInit)

function handleInit (err) {
if (!err) { // The repo was initialized for the first time, we need to configure it
addWebRTCMultiaddr()
} else if (err && err.message !== 'repo already exists') { // The repo already existed, let's just load it
loadRepo()
} else {
if (err) {
throw err
}
}

function addWebRTCMultiaddr() {
// Addj the WebrTCStar Multiaddr to your node
node.config.get(function (err, config) {
if (err) {
throw err
}

const starAddr = (SIGNALING_SERVER + config.Identity.PeerID)

node.config.set('Addresses.Swarm[1]', starAddr, loadRepo)
})
}

function loadRepo() {
node.load(() => node.goOnline(() => {
node.start(() => {
console.log('Online status: ', node.isOnline() ? 'online' : 'offline')

document.getElementById("status").innerHTML= 'Node status: ' + (node.isOnline() ? 'online' : 'offline')

// \o/ Now you have an IPFS node using WebRTC to find other nodes!
// You can write more code here to use it. Use methods like
// node.files.add, node.files.get. See the API docs here:
// https://github.com/ipfs/interface-ipfs-core/tree/master/API
Expand Down
13 changes: 5 additions & 8 deletions examples/bundle-browserify/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const repoPath = String(Math.random())

const node = new IPFS({
repo: repoPath,
init: false,
start: false,
EXPERIMENTAL: {
pubsub: false
}
Expand All @@ -23,17 +25,12 @@ node.init({ emptyRepo: true, bits: 2048 }, function (err) {
if (err) {
throw err
}
node.load(function (err) {

node.start(function (err) {
if (err) {
throw err
}

node.goOnline(function (err) {
if (err) {
throw err
}
console.log('IPFS node is ready')
})
console.log('IPFS node is ready')
})
})

Expand Down
16 changes: 7 additions & 9 deletions examples/bundle-webpack/src/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ class App extends React.Component {
// for simplicity, we create a new repo everytime the node
// is created, because you can't init already existing repos
const repoPath = String(Math.random())

node = new IPFS({
repo: repoPath,
init: false,
start: false,
EXPERIMENTAL: {
pubsub: false
}
Expand All @@ -40,18 +43,13 @@ class App extends React.Component {
if (err) {
throw err
}
node.load(function (err) {

node.start(function (err) {
if (err) {
throw err
}

node.goOnline(function (err) {
if (err) {
throw err
}
console.log('IPFS node is ready')
ops()
})
console.log('IPFS node is ready')
ops()
})
})
}
Expand Down
8 changes: 1 addition & 7 deletions examples/dag/create-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const series = require('async/series')

const IPFS = require('../../src/core')
// In your project, replace by the following line and install IPFS as a dep
// const IPFS = require('ipfs')
Expand All @@ -18,11 +16,7 @@ function createNode (options, callback) {
repo: options.path
})

series([
(cb) => node.init({ emptyRepo: true, bits: 2048 }, cb),
(cb) => node.load(cb),
(cb) => node.goOnline(cb)
], (err) => callback(err, node))
node.on('start', () => callback(null, node))
}

module.exports = createNode
57 changes: 26 additions & 31 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,37 @@ let nodes = []
function spawnDaemon (num, callback) {
num = leftPad(num, 3, 0)

const repo = createTempRepo()

const node = new IPFS({
repo: repo,
repo: createTempRepo(),
init: {
bits: 1024
},
start: false,
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
`/ip4/127.0.0.1/tcp/10${num}`,
`/ip4/127.0.0.1/tcp/20${num}/ws`
],
API: `/ip4/127.0.0.1/tcp/31${num}`,
Gateway: `/ip4/127.0.0.1/tcp/32${num}`
},
Discovery: {
MDNS: {
Enabled: false
}
}
}
})

series([
(cb) => node.init({ emptyRepo: true, bits: 1024 }, cb),
(cb) => {
repo.config.get((err, config) => {
if (err) { return callback(err) }

config.Addresses = {
Swarm: [
`/ip4/127.0.0.1/tcp/10${num}`,
`/ip4/127.0.0.1/tcp/20${num}/ws`
],
API: `/ip4/127.0.0.1/tcp/31${num}`,
Gateway: `/ip4/127.0.0.1/tcp/32${num}`
}

config.Discovery.MDNS.Enabled = false

repo.config.set(config, cb)
})
},
(cb) => node.load(cb),
(cb) => {
const daemon = new HTTPAPI(node.repo.path())
nodes.push(daemon)
daemon.start(cb)
}
], callback)
setTimeout(() => {
const daemon = new HTTPAPI(node.repo.path())
nodes.push(daemon)
daemon.start(callback)
}, 400)
}

gulp.task('libnode:start', (done) => {
Expand All @@ -66,7 +61,7 @@ gulp.task('libnode:start', (done) => {

gulp.task('libnode:stop', (done) => {
series(nodes.map((node) => (cb) => {
setTimeout(() => node.stop(cb), 200)
setTimeout(() => node.stop(cb), 100)
}), done)
})

Expand Down
15 changes: 5 additions & 10 deletions src/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ module.exports = {
default: '2048',
describe: 'Number of bits to use in the generated RSA private key (defaults to 2048)'
},
force: {
alias: 'f',
type: 'boolean',
describe: 'Overwrite existing config (if it exists)'
},
emptyRepo: {
alias: 'e',
type: 'boolean',
Expand All @@ -39,16 +34,16 @@ module.exports = {
stores: Store
})

const ipfs = new IPFS({
const node = new IPFS({
repo: repo,
EXPERIMENTAL: {}
init: false,
start: false
})

ipfs.init({
node.init({
bits: argv.bits,
force: argv.force,
emptyRepo: argv.emptyRepo,
log
log: log
}, (err) => {
if (err) {
console.error(err.toString())
Expand Down
22 changes: 12 additions & 10 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ function getAPICtl () {
}

exports.getIPFS = (callback) => {
if (!isDaemonOn()) {
const ipfs = new IPFS({
repo: exports.getRepoPath(),
EXPERIMENTAL: {
pubsub: true
}
})
ipfs.load(() => callback(null, ipfs))
return
if (isDaemonOn()) {
return callback(null, getAPICtl())
}

callback(null, getAPICtl())
const node = new IPFS({
repo: exports.getRepoPath(),
init: false,
start: false,
EXPERIMENTAL: {
pubsub: true
}
})

node.preStart(() => callback(null, node))
}

exports.getRepoPath = () => {
Expand Down
Loading