Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 1b71470

Browse files
authored
Merge branch 'master' into fix-webpack-example
2 parents f97fb52 + 179b6a4 commit 1b71470

File tree

12 files changed

+209
-77
lines changed

12 files changed

+209
-77
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,18 @@ The CLI is available by using the command `jsipfs` in your terminal. This is ali
116116
Learn how to bundle with browserify and webpack in the [`examples`](https://github.com/ipfs/js-ipfs/tree/master/examples) folder.
117117

118118

119-
You can also load it using a `<script>` using the [unpkg](https://unpkg.com) CDN. Inserting one of the following lines will make a `Ipfs` object available in the global namespace.
119+
You can also load it using a `<script>` using the [unpkg](https://unpkg.com) CDN or the [jsDelivr](https://www.jsdelivr.com/package/npm/ipfs) CDN. Inserting one of the following lines will make a `Ipfs` object available in the global namespace.
120120

121121
```html
122-
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script> <!-- loading the minified version -->
123-
<script src="https://unpkg.com/ipfs/dist/index.js"></script> <!-- loading the human-readable (not minified) version -->
122+
<!-- loading the minified version -->
123+
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
124+
<script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js"></script>
125+
126+
<!-- loading the human-readable (not minified) version -->
127+
<script src="https://unpkg.com/ipfs/dist/index.js"></script>
128+
<script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.js"></script>
124129
```
130+
Inserting one of the above lines will make an `Ipfs` object available in the global namespace.
125131

126132
## Usage
127133

@@ -309,7 +315,7 @@ Every IPFS instance also exposes the libp2p API at `ipfs.libp2p`. The formal int
309315

310316
##### [object](https://github.com/ipfs/interface-ipfs-core/tree/master/API/object)
311317

312-
> Consider using the [dag API](#dag) API instead.
318+
> Consider using the [dag API](#dag) instead.
313319
314320
- [`ipfs.object.new([template][, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/object#objectnew)
315321
- [`ipfs.object.put(obj, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/object#objectput)
@@ -487,7 +493,7 @@ Then, update your IPFS Daemon config to include the multiaddr for this new trans
487493

488494
### Lint
489495

490-
*Conforming to linting rules is a prerequisite to commit to js-ipfs.*
496+
**Conforming to linting rules is a prerequisite to commit to js-ipfs.**
491497

492498
```sh
493499
> npm run lint
@@ -522,11 +528,11 @@ src # Main source code folder
522528
### Monitoring
523529

524530
The HTTP API exposed with js-ipfs can also be used for exposing metrics about
525-
the running js-ipfs node and other nodejs metrics.
531+
the running js-ipfs node and other Node.js metrics.
526532

527533
To enable it, you need to set the environment variable `IPFS_MONITORING` (any value)
528534

529-
Once environment variable is set and the js-ipfs daemon is running, you can get
535+
Once the environment variable is set and the js-ipfs daemon is running, you can get
530536
the metrics (in prometheus format) by making a GET request to the following endpoint:
531537

532538
```
@@ -541,17 +547,17 @@ What does this image explain?
541547

542548
- IPFS uses `ipfs-repo` which picks `fs` or `indexeddb` as its storage drivers, depending if it is running in Node.js or in the Browser.
543549
- The exchange protocol, `bitswap`, uses the Block Service which in turn uses the Repo, offering a get and put of blocks to the IPFS implementation.
544-
- The DAG api (previously Object) comes from the IPLD Resolver, it can support several IPLD Formats (i.e: dag-pb, dag-cbor, etc).
550+
- The DAG API (previously Object) comes from the IPLD Resolver, it can support several IPLD Formats (i.e: dag-pb, dag-cbor, etc).
545551
- The Files API uses `ipfs-unixfs-engine` to import and export files to and from IPFS.
546-
- Swarm, the component that offers a network API, uses libp2p to dial and listen for connections, use the DHT, discovery mechanisms and more. libp2p-ipfs-nodejs is used in case of running in Node.js and libp2p-ipfs-browser is used in the case of the Browser.
552+
- Swarm, the component that offers a network API, uses libp2p to dial and listen for connections, to use the DHT, for discovery mechanisms, and more. libp2p-ipfs-nodejs is used when running in Node.js and libp2p-ipfs-browser is used when running in the browser.
547553

548554
## Contribute
549555

550556
IPFS implementation in JavaScript is a work in progress. As such, there's a few things you can do right now to help out:
551557

552558
* Go through the modules below and **check out existing issues**. This would be especially useful for modules in active development. Some knowledge of IPFS may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
553-
* **Perform code reviews**. More eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.
554-
* Take a look at go-ipfs and some of the planning repositories or issues: for instance, the libp2p spec [here](https://github.com/ipfs/specs/pull/19). Contributions here that would be most helpful are **top-level comments** about how it should look based on our understanding. Again, the more eyes the better.
559+
* **Perform code reviews**. More eyes will help (a) speed the project along, (b) ensure quality, and (c) reduce possible future bugs.
560+
* Take a look at go-ipfs and some of the planning repositories or issues: for instance, the [libp2p spec](https://github.com/ipfs/specs/pull/19). Contributions here that would be most helpful are **top-level comments** about how it should look based on our understanding. Again, the more eyes the better.
555561
* **Add tests**. There can never be enough tests.
556562
* **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.
557563

examples/browser-webpack/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
"babel-preset-react": "^6.24.1",
1414
"babel-preset-stage-0": "^6.24.1",
1515
"json-loader": "^0.5.4",
16+
"react": "^15.5.4",
17+
"react-dom": "^15.6.1",
1618
"react-hot-loader": "^1.3.1",
1719
"webpack": "^2.5.1",
1820
"webpack-dev-server": "^2.4.5"
1921
},
2022
"dependencies": {
21-
"ipfs": "file:../../",
22-
"react": "^15.6.1",
23-
"react-dom": "^15.6.1"
23+
"ipfs": "file:../../"
2424
}
2525
}

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"expose-loader": "^0.7.3",
7575
"form-data": "^2.3.1",
7676
"gulp": "^3.9.1",
77-
"interface-ipfs-core": "~0.31.19",
77+
"interface-ipfs-core": "~0.32.1",
7878
"ipfsd-ctl": "~0.23.0",
7979
"left-pad": "^1.1.3",
8080
"lodash": "^4.17.4",
@@ -93,16 +93,18 @@
9393
"async": "^2.5.0",
9494
"bl": "^1.2.1",
9595
"boom": "^5.2.0",
96-
"debug": "^3.0.1",
96+
"byteman": "^1.3.5",
9797
"cids": "^0.5.1",
98+
"debug": "^3.0.1",
9899
"file-type": "^6.1.0",
99100
"filesize": "^3.5.10",
100101
"fsm-event": "^2.1.0",
102+
"get-folder-size": "^1.0.0",
101103
"glob": "^7.1.2",
102104
"hapi": "^16.5.2",
103105
"hapi-set-header": "^1.0.2",
104106
"hoek": "^4.2.0",
105-
"ipfs-api": "^14.3.5",
107+
"ipfs-api": "^14.3.7",
106108
"ipfs-bitswap": "~0.17.2",
107109
"ipfs-block": "~0.6.0",
108110
"ipfs-block-service": "~0.12.0",
@@ -138,8 +140,11 @@
138140
"peer-book": "~0.5.1",
139141
"peer-id": "~0.10.1",
140142
"peer-info": "~0.11.0",
143+
"progress": "^2.0.0",
141144
"promisify-es6": "^1.0.3",
145+
"pull-abortable": "^4.1.1",
142146
"pull-file": "^1.0.0",
147+
"pull-ndjson": "^0.1.1",
143148
"pull-paramap": "^1.2.2",
144149
"pull-pushable": "^2.1.1",
145150
"pull-sort": "^1.0.1",
@@ -216,4 +221,4 @@
216221
"Łukasz Magiera <magik6k@users.noreply.github.com>",
217222
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"
218223
]
219-
}
224+
}

src/cli/commands/files/add.js

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ const pull = require('pull-stream')
88
const paramap = require('pull-paramap')
99
const zip = require('pull-zip')
1010
const toPull = require('stream-to-pull-stream')
11+
const getFolderSize = require('get-folder-size')
12+
const byteman = require('byteman')
13+
const waterfall = require('async/waterfall')
1114
const utils = require('../../utils')
1215
const print = require('../../utils').print
16+
const createProgressBar = require('../../utils').createProgressBar
1317

1418
const WRAPPER = 'wrapper/'
1519

@@ -40,14 +44,21 @@ function checkPath (inPath, recursive) {
4044
return inPath
4145
}
4246

47+
function getTotalBytes (path, recursive, cb) {
48+
if (recursive) {
49+
getFolderSize(path, cb)
50+
} else {
51+
fs.stat(path, (err, stat) => cb(err, stat.size))
52+
}
53+
}
54+
4355
function addPipeline (index, addStream, list, argv) {
4456
const {
4557
wrapWithDirectory,
4658
quiet,
4759
quieter,
4860
silent
4961
} = argv
50-
5162
pull(
5263
zip(
5364
pull.values(list),
@@ -102,6 +113,12 @@ module.exports = {
102113
describe: 'Add a file to IPFS using the UnixFS data format',
103114

104115
builder: {
116+
progress: {
117+
alias: 'p',
118+
type: 'boolean',
119+
default: true,
120+
describe: 'Stream progress data'
121+
},
105122
recursive: {
106123
alias: 'r',
107124
type: 'boolean',
@@ -185,34 +202,42 @@ module.exports = {
185202
}
186203
const ipfs = argv.ipfs
187204

188-
// TODO: revist when interface-ipfs-core exposes pull-streams
189-
let createAddStream = (cb) => {
190-
ipfs.files.createAddStream(options, (err, stream) => {
191-
cb(err, err ? null : toPull.transform(stream))
192-
})
193-
}
194-
195-
if (typeof ipfs.files.createAddPullStream === 'function') {
196-
createAddStream = (cb) => {
197-
cb(null, ipfs.files.createAddPullStream(options))
198-
}
199-
}
205+
let list = []
206+
waterfall([
207+
(next) => glob(path.join(inPath, '/**/*'), next),
208+
(globResult, next) => {
209+
list = globResult.length === 0 ? [inPath] : globResult
210+
211+
getTotalBytes(inPath, argv.recursive, next)
212+
},
213+
(totalBytes, next) => {
214+
if (argv.progress) {
215+
const bar = createProgressBar(totalBytes)
216+
options.progress = function (byteLength) {
217+
bar.update(byteLength / totalBytes, {progress: byteman(byteLength, 2, 'MB')})
218+
}
219+
}
200220

201-
createAddStream((err, addStream) => {
202-
if (err) {
203-
throw err
204-
}
221+
// TODO: revist when interface-ipfs-core exposes pull-streams
205222

206-
glob(path.join(inPath, '/**/*'), (err, list) => {
207-
if (err) {
208-
throw err
223+
let createAddStream = (cb) => {
224+
ipfs.files.createAddStream(options, (err, stream) => {
225+
cb(err, err ? null : toPull.transform(stream))
226+
})
209227
}
210-
if (list.length === 0) {
211-
list = [inPath]
228+
229+
if (typeof ipfs.files.createAddPullStream === 'function') {
230+
createAddStream = (cb) => {
231+
cb(null, ipfs.files.createAddPullStream(options))
232+
}
212233
}
213234

214-
addPipeline(index, addStream, list, argv)
215-
})
235+
createAddStream(next)
236+
}
237+
], (err, addStream) => {
238+
if (err) throw err
239+
240+
addPipeline(index, addStream, list, argv)
216241
})
217242
}
218243
}

src/cli/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const path = require('path')
99
const debug = require('debug')
1010
const log = debug('cli')
1111
log.error = debug('cli:error')
12+
const Progress = require('progress')
13+
const byteman = require('byteman')
1214

1315
exports = module.exports
1416

@@ -85,3 +87,16 @@ exports.print = (msg, newline) => {
8587
process.stdout.write(msg)
8688
}
8789
}
90+
91+
exports.createProgressBar = (totalBytes) => {
92+
const total = byteman(totalBytes, 2, 'MB')
93+
const barFormat = `:progress / ${total} [:bar] :percent :etas`
94+
95+
// 16 MB / 34 MB [=========== ] 48% 5.8s //
96+
return new Progress(barFormat, {
97+
incomplete: ' ',
98+
clear: true,
99+
stream: process.stdout,
100+
total: totalBytes
101+
})
102+
}

src/core/components/files.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ module.exports = function files (self) {
2020
shardSplitThreshold: self._options.EXPERIMENTAL.sharding ? 1000 : Infinity
2121
}, options)
2222

23+
let total = 0
24+
let prog = opts.progress || (() => {})
25+
const progress = (bytes) => {
26+
total += bytes
27+
prog(total)
28+
}
29+
30+
opts.progress = progress
2331
return pull(
2432
pull.map(normalizeContent),
2533
pull.flatten(),
@@ -54,7 +62,7 @@ module.exports = function files (self) {
5462
add: promisify((data, options, callback) => {
5563
if (typeof options === 'function') {
5664
callback = options
57-
options = undefined
65+
options = {}
5866
} else if (!callback || typeof callback !== 'function') {
5967
callback = noop
6068
}
@@ -66,9 +74,8 @@ module.exports = function files (self) {
6674
}
6775

6876
pull(
69-
pull.values(normalizeContent(data)),
70-
importer(self._ipldResolver, options),
71-
pull.asyncMap(prepareFile.bind(null, self, options)),
77+
pull.values([data]),
78+
createAddPullStream(options),
7279
sort((a, b) => {
7380
if (a.path < b.path) return 1
7481
if (a.path > b.path) return -1
@@ -80,7 +87,7 @@ module.exports = function files (self) {
8087

8188
cat: promisify((ipfsPath, callback) => {
8289
if (typeof ipfsPath === 'function') {
83-
return callback(new Error('You must supply a ipfsPath'))
90+
return callback(new Error('You must supply an ipfsPath'))
8491
}
8592

8693
pull(
@@ -89,6 +96,7 @@ module.exports = function files (self) {
8996
if (err) {
9097
return callback(err)
9198
}
99+
if (!files || !files.length) return callback(new Error('No such file'))
92100
callback(null, toStream.source(files[files.length - 1].content))
93101
})
94102
)

src/core/components/init-assets.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const glob = require('glob')
66
const importer = require('ipfs-unixfs-engine').importer
77
const pull = require('pull-stream')
88
const file = require('pull-file')
9-
// const mh = require('multihashes')
9+
const CID = require('cids')
1010

1111
// Add the default assets to the repo.
1212
module.exports = function addDefaultAssets (self, log, callback) {
1313
const initDocsPath = path.join(__dirname, '../../init-files/init-docs')
14-
const index = __dirname.lastIndexOf('/')
14+
const index = initDocsPath.lastIndexOf('/')
1515

1616
pull(
1717
pull.values([initDocsPath]),
@@ -20,7 +20,7 @@ module.exports = function addDefaultAssets (self, log, callback) {
2020
}),
2121
pull.flatten(),
2222
pull.map((element) => {
23-
const addPath = element.substring(index + 1, element.length)
23+
const addPath = element.substring(index + 1)
2424
if (fs.statSync(element).isDirectory()) {
2525
return
2626
}
@@ -34,14 +34,10 @@ module.exports = function addDefaultAssets (self, log, callback) {
3434
pull.filter(Boolean),
3535
importer(self._ipldResolver),
3636
pull.through((el) => {
37-
if (el.path === 'files/init-docs/docs') {
38-
log('to get started, enter:')
39-
log()
40-
log(`\t jsipfs files cat /ipfs/QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB`)
41-
// TODO when we support pathing in unixfs-engine
42-
// const hash = mh.toB58String(el.multihash)
43-
// log(`\t jsipfs files cat /ipfs/${hash}/readme`)
44-
log()
37+
if (el.path === 'init-docs') {
38+
const cid = new CID(el.multihash)
39+
log('to get started, enter:\n')
40+
log(`\t jsipfs files cat /ipfs/${cid.toBaseEncodedString()}/readme\n`)
4541
}
4642
}),
4743
pull.collect((err) => {

src/core/runtime/libp2p-browser.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ class Node extends libp2p {
2626
modules.discovery.push(r)
2727
}
2828

29+
if (options.modules && options.modules.transport) {
30+
options.modules.transport.forEach((t) => modules.transport.push(t))
31+
}
32+
33+
if (options.modules && options.modules.discovery) {
34+
options.modules.discovery.forEach((d) => modules.discovery.push(d))
35+
}
36+
2937
super(modules, peerInfo, peerBook, options)
3038
}
3139
}

0 commit comments

Comments
 (0)