Skip to content

Commit

Permalink
Merge branch 'master' into pin-api
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamStone committed Feb 21, 2018
2 parents 222a428 + e4d2a15 commit f5a05fe
Show file tree
Hide file tree
Showing 22 changed files with 275 additions and 134 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ language: node_js

matrix:
include:
- node_js: 6
env: CXX=g++-4.8
- node_js: 8
env: CXX=g++-4.8

script:
- npm run lint
- npm run test
- make test

before_script:
- export DISPLAY=:99.0
Expand Down
3 changes: 3 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ machine:
version: stable

test:
pre:
- npm run lint
post:
- make test
- npm run coverage -- --upload --providers coveralls

dependencies:
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Let us know if you find any issue or if you want to contribute and add a new tut
- [js-ipfs in the browser with WebPack](./browser-webpack)
- [js-ipfs in the browser with a `<script>` tag](./browser-script-tag)
- [js-ipfs in electron](./run-in-electron)
- [Using streams to add a directory of files to ipfs](./browser-add-readable-stream)

## Understanding the IPFS Stack

Expand Down
7 changes: 7 additions & 0 deletions examples/browser-add-readable-stream/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Using duplex streams to add files to IPFS in the browser

If you have a number of files that you'd like to add to IPFS and end up with a hash representing the directory containing your files, you can invoke [`ipfs.files.add`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#add) with an array of objects.

But what if you don't know how many there will be in advance? You can add multiple files to a directory in IPFS over time by using [`ipfs.files.addReadableStream`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md#addreadablestream).

See `index.js` for a working example and open `index.html` in your browser to see it run.
7 changes: 7 additions & 0 deletions examples/browser-add-readable-stream/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<pre id="output"></pre>
<script src="https://unpkg.com/ipfs/dist/index.js"></script>
<script src="index.js"></script>
</body>
</html>
75 changes: 75 additions & 0 deletions examples/browser-add-readable-stream/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict'

/* global Ipfs */
/* eslint-env browser */

const repoPath = 'ipfs-' + Math.random()
const ipfs = new Ipfs({ repo: repoPath })

ipfs.on('ready', () => {
const directory = 'directory'

// Our list of files
const files = createFiles(directory)

streamFiles(directory, files, (err, directoryHash) => {
if (err) {
return log(`There was an error adding the files ${err}`)
}

ipfs.ls(directoryHash, (err, files) => {
if (err) {
return log(`There was an error listing the files ${err}`)
}

log(`
--
Directory contents:
${directory}/ ${directoryHash}`)

files.forEach((file, index) => {
log(` ${index < files.length - 1 ? '\u251C' : '\u2514'}\u2500 ${file.name} ${file.path} ${file.hash}`)
})
})
})
})

const createFiles = (directory) => {
return [{
path: `${directory}/file1.txt`,

// content could be a stream, a url etc
content: ipfs.types.Buffer.from('one', 'utf8')
}, {
path: `${directory}/file2.txt`,
content: ipfs.types.Buffer.from('two', 'utf8')
}, {
path: `${directory}/file3.txt`,
content: ipfs.types.Buffer.from('three', 'utf8')
}]
}

const streamFiles = (directory, files, cb) => {
// Create a stream to write files to
const stream = ipfs.files.addReadableStream()
stream.on('data', function (data) {
log(`Added ${data.path} hash: ${data.hash}`)

// The last data event will contain the directory hash
if (data.path === directory) {
cb(null, data.hash)
}
})

// Add the files one by one
files.forEach(file => stream.write(file))

// When we have no more files to add, close the stream
stream.end()
}

const log = (line) => {
document.getElementById('output').appendChild(document.createTextNode(`${line}\r\n`))
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
"is-ipfs": "^0.3.2",
"is-stream": "^1.1.0",
"joi": "^13.1.2",
"libp2p": "~0.16.5",
"libp2p": "~0.17.0",
"libp2p-circuit": "~0.1.4",
"libp2p-floodsub": "~0.14.1",
"libp2p-kad-dht": "~0.8.0",
Expand Down
35 changes: 29 additions & 6 deletions src/cli/commands/version.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const os = require('os')
const print = require('../utils').print

module.exports = {
Expand All @@ -11,26 +12,48 @@ module.exports = {
number: {
alias: 'n',
type: 'boolean',
default: false
default: false,
describe: 'Print only the version number'
},
commit: {
type: 'boolean',
default: false
default: false,
describe: `Include the version's commit hash`
},
repo: {
type: 'boolean',
default: false
default: false,
describe: `Print only the repo's version number`
},
all: {
type: 'boolean',
default: false,
describe: 'Print everything we have'
}
},

handler (argv) {
// TODO: handle argv.{repo|commit|number}
argv.ipfs.version((err, version) => {
argv.ipfs.version((err, data) => {
if (err) {
throw err
}

print(`js-ipfs version: ${version.version}`)
const withCommit = argv.all || argv.commit
const parsedVersion = `${data.version}${withCommit ? `-${data.commit}` : ''}`

if (argv.repo) {
// go-ipfs prints only the number, even without the --number flag.
print(data.repo)
} else if (argv.number) {
print(parsedVersion)
} else if (argv.all) {
print(`js-ipfs version: ${parsedVersion}`)
print(`Repo version: ${data.repo}`)
print(`System version: ${os.arch()}/${os.platform()}`)
print(`Node.js version: ${process.version}`)
} else {
print(`js-ipfs version: ${parsedVersion}`)
}
})
}
}
35 changes: 21 additions & 14 deletions src/core/components/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
'use strict'

const defaultNodes = require('../runtime/config-nodejs.json').Bootstrap
const MultiAddr = require('multiaddr')
const isMultiaddr = require('mafmt').IPFS.matches
const promisify = require('promisify-es6')

function isValidMultiaddr (ma) {
try {
return isMultiaddr(ma)
} catch (err) {
return false
}
}

function invalidMultiaddrError (ma) {
return new Error(`${ma} is not a valid Multiaddr`)
}

module.exports = function bootstrap (self) {
return {
list: promisify((callback) => {
Expand All @@ -17,15 +29,13 @@ module.exports = function bootstrap (self) {
add: promisify((multiaddr, args, callback) => {
if (typeof args === 'function') {
callback = args
args = {default: false}
args = { default: false }
}
try {
if (multiaddr)
new MultiAddr(multiaddr)
}
catch (err) {
return setImmediate(() => callback(err))

if (multiaddr && !isValidMultiaddr(multiaddr)) {
return setImmediate(() => callback(invalidMultiaddrError(multiaddr)))
}

self._repo.config.get((err, config) => {
if (err) {
return callback(err)
Expand All @@ -51,13 +61,10 @@ module.exports = function bootstrap (self) {
callback = args
args = {all: false}
}
try {
if (multiaddr)
new MultiAddr(multiaddr)
}
catch (err) {
return setImmediate(() => callback(err))
if (multiaddr && !isValidMultiaddr(multiaddr)) {
return setImmediate(() => callback(invalidMultiaddrError(multiaddr)))
}

self._repo.config.get((err, config) => {
if (err) {
return callback(err)
Expand Down
5 changes: 2 additions & 3 deletions src/core/components/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = function libp2p (self) {
bootstrap: get(config, 'Bootstrap'),
modules: self._libp2pModules,
// EXPERIMENTAL
pubsub: get(self._options, 'EXPERIMENTAL.pubsub', false),
dht: get(self._options, 'EXPERIMENTAL.dht', false),
relay: {
enabled: get(config, 'EXPERIMENTAL.relay.enabled', false),
Expand Down Expand Up @@ -50,9 +51,7 @@ module.exports = function libp2p (self) {
})

self._libp2pNode.start((err) => {
if (err) {
return callback(err)
}
if (err) { return callback(err) }

self._libp2pNode.peerInfo.multiaddrs.forEach((ma) => {
console.log('Swarm listening on', ma.toString())
Expand Down
24 changes: 0 additions & 24 deletions src/core/components/no-floodsub.js

This file was deleted.

Loading

0 comments on commit f5a05fe

Please sign in to comment.