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

Awesome Endeavour: API Documentation #651

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ build
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

lib
dist
docs
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ build
node_modules

test
docs
3 changes: 3 additions & 0 deletions documentation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
toc:
- name: Intro
file: intro.md
54 changes: 54 additions & 0 deletions intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## Install

### npm

This project is available through [npm](https://www.npmjs.com/). To install:

```bash
$ npm install ipfs --save
```

Requires npm@3 and node >= 4, tested on OSX & Linux, expected to work on Windows.

### Use in Node.js

To include this project programmatically:

```js
var IPFS = require('ipfs')

var node = new IPFS()
```

### Through command line tool

In order to use js-ipfs as a CLI, you must install it with the `global` flag. Run the following (even if you have ipfs installed locally):

```bash
$ npm install ipfs --global
```

The CLI is available by using the command `jsipfs` in your terminal. This is aliased, instead of using `ipfs`, to make sure it does not conflict with the Go implementation.

### Use in the browser with browserify, webpack or any bundler

The code published to npm that gets loaded on require is in fact a ES5 transpiled version with the right shims added. This means that you can require it and use with your favourite bundler without having to adjust the asset management process.

```js
var ipfs = require('ipfs');
```

### Use in a browser using a script tag

Loading this module in a browser (using a `<script>` tag) makes the `Ipfs` object available in the global namespace.

The last published version of the package become [available for download](https://unpkg.com/ipfs/dist/) from [unpkg](https://unpkg.com/) and thus you may use it as the source:


```html
<!-- loading the minified version -->
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script>

<!-- loading the human-readable (not minified) version -->
<script src="https://unpkg.com/ipfs/dist/index.js"></script>
```
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
"scripts": {
"lint": "aegir-lint",
"coverage": "gulp coverage",
"docs": "aegir-docs",
"test": "gulp test",
"test:node": "gulp test:node",
"test:node:core": "TEST=core npm run test:node",
"test:node:http": "TEST=http npm run test:node",
"test:node:cli": "TEST=cli npm run test:node",
"test:browser": "gulp test:browser",
"build": "gulp build",
"release": "gulp release",
"release-minor": "gulp release --type minor",
"release-major": "gulp release --type major",
"release": "gulp release --docs",
"release-minor": "gulp release --type minor --docs",
"release-major": "gulp release --type major --docs",
"coverage-publish": "aegir-coverage publish"
},
"pre-commit": [
Expand Down Expand Up @@ -150,4 +151,4 @@
"nginnever <ginneversource@gmail.com>",
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>"
]
}
}
20 changes: 20 additions & 0 deletions src/core/components/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ function formatWantlist (list) {

module.exports = function bitswap (self) {
return {
/**
* @alias bitswap.wantlist
* @memberof IPFS#
* @method
* @returns {Array}
*/
wantlist: () => {
if (!self.isOnline()) {
throw OFFLINE_ERROR
Expand All @@ -16,6 +22,12 @@ module.exports = function bitswap (self) {
const list = self._bitswap.getWantlist()
return formatWantlist(list)
},
/**
* @alias bitswap.stat
* @memberof IPFS#
* @method
* @returns {Object}
*/
stat: () => {
if (!self.isOnline()) {
throw OFFLINE_ERROR
Expand All @@ -27,6 +39,14 @@ module.exports = function bitswap (self) {

return stats
},
/**
* NOT IMPLEMENTED
* @alias bitswap.unwant
* @memberof IPFS#
* @method
* @param {*} key
* @returns {undefined}
*/
unwant: (key) => {
if (!self.isOnline()) {
throw OFFLINE_ERROR
Expand Down
41 changes: 41 additions & 0 deletions src/core/components/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,31 @@ const waterfall = require('async/waterfall')

module.exports = function block (self) {
return {
/**
* @alias block.get
* @memberof IPFS#
* @method
* @param {CID} cid
* @param {function(Error)} callback
* @returns {undefined}
*
* @see https://github.com/ipfs/interface-ipfs-core/tree/master/API/block#get
*/
get: (cid, callback) => {
cid = cleanCid(cid)
self._blockService.get(cid, callback)
},
/**
* @alias block.put
* @memberof IPFS#
* @method
* @param {Block} block
* @param {CID} cid
* @param {function(Error)} callback
* @returns {undefined}
*
* @see https://github.com/ipfs/interface-ipfs-core/tree/master/API/block#put
*/
put: (block, cid, callback) => {
if (typeof cid === 'function') {
// legacy (without CID)
Expand Down Expand Up @@ -49,10 +70,30 @@ module.exports = function block (self) {
callback(null, block)
})
},
/**
* @alias block.rm
* @memberof IPFS#
* @method
* @param {CID} cid
* @param {function(Error)} callback
* @returns {undefined}
*
* @see https://github.com/ipfs/interface-ipfs-core/tree/master/API/block#rm
*/
rm: (cid, callback) => {
cid = cleanCid(cid)
self._blockService.delete(cid, callback)
},
/**
* @alias block.stat
* @memberof IPFS#
* @method
* @param {CID} cid
* @param {function(Error)} callback
* @returns {undefined}
*
* @see https://github.com/ipfs/interface-ipfs-core/tree/master/API/block#stat
*/
stat: (cid, callback) => {
cid = cleanCid(cid)

Expand Down
22 changes: 22 additions & 0 deletions src/core/components/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

module.exports = function bootstrap (self) {
return {
/**
* @alias bootstrap.list
* @memberof IPFS#
* @param {function(Error, Array<string>)} callback
* @returns {Promise<Array<string>>|undefined}
*/
list: (callback) => {
self._repo.config.get((err, config) => {
if (err) {
Expand All @@ -10,6 +16,14 @@ module.exports = function bootstrap (self) {
callback(null, config.Bootstrap)
})
},

/**
* @alias bootstrap.add
* @memberof IPFS#
* @param {string} multiaddr
* @param {function(Error)} callback
* @returns {Promise<undefined>|undefined}
*/
add: (multiaddr, callback) => {
self._repo.config.get((err, config) => {
if (err) {
Expand All @@ -19,6 +33,14 @@ module.exports = function bootstrap (self) {
self._repo.config.set(config, callback)
})
},

/**
* @alias bootstrap.rm
* @memberof IPFS#
* @param {string} multiaddr
* @param {function(Error)} callback
* @returns {Promise<undefiend>|undefined}
*/
rm: (multiaddr, callback) => {
self._repo.config.get((err, config) => {
if (err) {
Expand Down
21 changes: 21 additions & 0 deletions src/core/components/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ const _set = require('lodash.set')

module.exports = function config (self) {
return {
/**
* @alias config.get
* @memberof IPFS#
* @method
* @param {function(Error)} callback
* @returns {Promise<undefined>|undefined}
*/
get: promisify((key, callback) => {
if (typeof key === 'function') {
callback = key
Expand All @@ -33,6 +40,13 @@ module.exports = function config (self) {
}
})
}),
/**
* @alias config.set
* @memberof IPFS#
* @method
* @param {function(Error)} callback
* @returns {Promise<undefined>|undefined}
*/
set: promisify((key, value, callback) => {
if (!key || typeof key !== 'string') {
return callback(new Error('Invalid key type'))
Expand All @@ -50,6 +64,13 @@ module.exports = function config (self) {
self.config.replace(config, callback)
})
}),
/**
* @alias config.replace
* @memberof IPFS#
* @method
* @param {function(Error)} callback
* @returns {Promise<undefined>|undefined}
*/
replace: promisify((config, callback) => {
self._repo.config.set(config, callback)
})
Expand Down
51 changes: 46 additions & 5 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,32 @@ module.exports = function files (self) {
}

return {
/**
* @alias files.createAddStream
* @memberof IPFS#
* @method
* @param {function(Error, DuplexStream)} callback
* @returns {Promise<DuplexStream>|undefined}
*/
createAddStream: (callback) => {
callback(null, toStream(createAddPullStream()))
},

/**
* @alias files.createAddPullStream
* @memberof IPFS#
* @method
* @param {function(Error, Pullstream)} callback
* @returns {Promise<PullStream>|undefined}
*/
createAddPullStream: createAddPullStream,

/**
* @alias files.add
* @memberof IPFS#
* @method
* @param {*} data
* @param {function(Error *)} callback
* @returns {Promise<*>|undefined}
*/
add: promisify((data, callback) => {
if (!callback || typeof callback !== 'function') {
callback = noop
Expand All @@ -48,7 +68,14 @@ module.exports = function files (self) {
pull.collect(callback)
)
}),

/**
* @alias files.cat
* @memberof IPFS#
* @method
* @param {string} hash
* @param {function(Error, ReadableStream)} callback
* @returns {Promise<ReadableStream>|undefined}
*/
cat: promisify((hash, callback) => {
if (typeof hash === 'function') {
return callback(new Error('You must supply a multihash'))
Expand Down Expand Up @@ -77,7 +104,14 @@ module.exports = function files (self) {
)
})
}),

/**
* @alias files.get
* @memberof IPFS#
* @method
* @param {string} hash
* @param {function(Error, *)} callback
* @returns {Promise<*>|undefined}
*/
get: promisify((hash, callback) => {
callback(null, toStream.source(pull(
exporter(hash, self._ipldResolver),
Expand All @@ -91,7 +125,14 @@ module.exports = function files (self) {
})
)))
}),

/**
* @alias files.getPull
* @memberof IPFS#
* @method
* @param {string} hash
* @param {function(Error, *)} callback
* @returns {Promise<*>|undefined}
*/
getPull: promisify((hash, callback) => {
callback(null, exporter(hash, self._ipldResolver))
})
Expand Down
7 changes: 7 additions & 0 deletions src/core/components/go-offline.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
'use strict'

module.exports = function goOffline (self) {
/**
* @alias goOffline
* @memberof IPFS#
* @method
* @param {function(Error)} cb
* @returns {undefined}
*/
return (cb) => {
self._blockService.goOffline()
self._bitswap.stop()
Expand Down
Loading