New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ipfs-cluster-api

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ipfs-cluster-api - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

src/add.js

35

package.json
{
"name": "ipfs-cluster-api",
"version": "0.0.2",
"version": "0.0.3",
"description": "A JS client library for the IPFS Cluster HTTP API.",
"leadMaintainer": "Vaibhav Saini <vasa@towardsblockchain.com>",
"main": "src/index.js",
"scripts": {
"test": "mocha"
},
"license": "MIT",
"dependencies": {
"concat-stream": "^1.6.0",
"async": "^2.6.2",
"concat-stream": "^2.0.0",
"detect-node": "^2.0.4",
"flatmap": "0.0.3",
"is-pull-stream": "0.0.0",
"iso-stream-http": "^0.1.2",
"ndjson": "^1.5.0",
"once": "^1.4.0",
"pump": "^1.0.2",
"pull-to-stream": "^0.1.1",
"pump": "^3.0.0",
"qs": "^6.5.1"

@@ -17,6 +28,18 @@ },

},
"keywords": [
"ipfs",
"ipfs-cluster",
"ipfscloud",
"ipfs-cluster-api",
"js",
"clusterlabs"
],
"contributors": [
"Tom O'Donnell <todonnel91@gmail.com>",
"Vaibhav Saini <vasa@towardsblockchain.com>"
]
"Vaibhav Saini <vasa@towardsblockchain.com>",
"Tom O'Donnell <todonnel91@gmail.com>"
],
"bugs": {
"url": "https://github.com/cluster-labs/ipfs-cluster-api/issues"
},
"homepage": "https://github.com/cluster-labs/ipfs-cluster-api"
}

@@ -0,44 +1,92 @@

<p align="center">
<img src="https://i.pinimg.com/564x/f4/f9/68/f4f968409552de91c2ff09d2a141f2e4.jpg" alt="ipfs-cluster-api" />
</p>
<h3 align="center">A Javascript client library for the IPFS Cluster HTTP API.</h3>
# js-ipfs-cluster-api
[![Made by](https://img.shields.io/badge/made%20by-Cluster%20Labs-blue.svg?style=flat-square)](https://clusterlabs.io) [![Main project](https://img.shields.io/badge/project-ipfscloud-blue.svg?style=flat-square)](http://github.com/cluster-labs/ipfscloud-web) [![npm version](https://badge.fury.io/js/ipfs-cluster-api.svg)](https://badge.fury.io/js/ipfs-cluster-api)
<a href=""><img src="https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square" /></a> <a href=""><img src="https://img.shields.io/badge/Node.js-%3E%3D10.0.0-orange.svg?style=flat-square" /></a> <a href="https://david-dm.org/cluster-labs/ipfs-cluster-api"><img src="https://david-dm.org/cluster-labs/ipfs-cluster-api.svg?style=flat-square" /></a> <a href="https://bundlephobia.com/result?p=ipfs-cluster-api"><img src="https://flat.badgen.net/bundlephobia/minzip/ipfs-cluster-api"></a> <a href="https://github.com/feross/standard"><img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square"></a>
---
> A client library for the IPFS Cluster HTTP API, implemented in JavaScript.
**UNOFFICIAL AND ALPHA**
[![Made by](https://img.shields.io/badge/made%20by-Cluster%20Labs-blue.svg?style=flat-square)](https://clusterlabs.io)
[![Main project](https://img.shields.io/badge/project-ipfscloud-blue.svg?style=flat-square)](http://github.com/cluster-labs/ipfscloud-web)
This is a port of `ipfs/js-ipfs-api` adapted for the API exposed by `ipfs/ipfs-cluster`.
[![npm version](https://badge.fury.io/js/ipfs-cluster-api.svg)](https://badge.fury.io/js/ipfs-cluster-api)
## Maintainer
---
[**Vaibhav Saini**](https://github.com/vasa-develop)
**UNOFFICIAL AND ALPHA**
## Table of Contents
This is a port of `ipfs/js-ipfs-api` adapted for the API exposed by `ipfs/ipfs-cluster`.
- [Install](#install)
- [Running the daemon with the right port](#running-the-daemon-with-the-right-port)
- [Importing the module and usage](#importing-the-module-and-usage)
- [Usage](#usage)
- [API Docs](#api)
- [Callbacks and promises](#callbacks-and-promises)
- [Development](#development)
- [Contribute](#contribute)
- [Historical Context](#historical)
- [License](#license)
## Install
This module can be installed through npm directly from the github repository.
This module uses node.js, and can be installed through npm:
`npm i ipfs-cluster-api`
```bash
npm install --save ipfs-cluster-api
```
### Dependencies
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 module requires `ipfs-cluster` to be running. It is assumed that the IPFS
Cluster API(`ipfs-cluster-service`) is running on "127.0.0.1:9094".
### Running the daemon with the right port
## Usage
**ipfs daemon**
To import the module:
To make `ipfs-cluster-service` work, you need to have a `ipfs` local daemon running. It needs to be open on the right port. `5001` is the default, and is used in the examples below, but it can be set to whatever you need.
```
var ipfsClusterAPI = require('ipfs-cluster-api')
# Show the ipfs config API port to check it is correct
> ipfs config Addresses.API
/ip4/127.0.0.1/tcp/5001
# Set it if it does not match the above output
> ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001
# Restart the daemon after changing the config
# Run the daemon
> ipfs daemon
```
**ipfs-cluster-service daemon**
To interact with the API, you need to have a daemon running. It needs to be open on the right port. `9094` is the default, and is used in the examples below, but it can be set to whatever you need. You can setup `ipfs-cluster-service` by following [**this installation guide**]([https://github.com/ipfs/ipfs-cluster#install](https://github.com/ipfs/ipfs-cluster#install)).
After installing run the daemon.
```
// connect to ipfs-cluster daemon API server (displayed are default values)
var ipfsCluster = ipfsClusterAPI('localhost', 9094, {protocol: 'http'})
# Run the daemon
> ipfs-cluster-service daemon
```
### Importing the module and usage
```javascript
var ipfsCluster = require('ipfs-cluster-api')
// connect to ipfs daemon API server
var cluster = ipfsCluster('localhost', '9094', { protocol: 'http' }) // leaving out the arguments will default to these values
// or connect with multiaddr
var ipfs = ipfsCluster('/ip4/127.0.0.1/tcp/9094')
// or using options
var ipfs = ipfsCluster({ host: 'localhost', port: '9094', protocol: 'http' })
```
## Usage
### API

@@ -49,43 +97,458 @@

See [test](./test) for working examples
### Adding & pinning data to cluster
* **`add`**
> Add and pin data to the cluster
Add allows to add and replicate content to several ipfs daemons, performing a Cluster Pin operation on success. It takes elements from local paths as well as from web URLs (accessed with a GET request).
Cluster Add is equivalent to "ipfs add" in terms of DAG building, and supports the same options for adjusting the chunker, the DAG layout etc. However, it will allocate the content and send it directly to the allocated peers (among which may not necessarily be the local ipfs daemon).
Once the adding process is finished, the content is fully added to all
allocations and pinned in them. This makes cluster add slower than a local
ipfs add, but the result is a fully replicated CID on completion.
If you prefer faster adding, add directly to the local IPFS and trigger a
cluster "pin add".
**`ipfsCluster.add(data, [options], [callback])`**
Where `data` may be:
- a [`Buffer instance`](https://www.npmjs.com/package/buffer)
- a [`Readable Stream`](https://www.npmjs.com/package/readable-stream)
- a [`Pull Stream`](https://www.npmjs.com/package/pull-stream)
- an array of objects, each of the form:
```
ipfsCluster.id([callback])
{
path: '/tmp/myfile.txt', // The file path
content: <data> // A Buffer, Readable Stream or Pull Stream with the contents of the file
}
```
If no `content` is passed, then the path is treated as an empty directory
ipfsCluster.add([fileBuffer OR filePath], [options], [callback])
ipfsCluster.peers.ls([callback])
ipfsCluster.peers.add(addr, [callback]) // e.g. /ip4/1.2.3.4/tcp/1234/<peerid>
ipfsCluster.peers.rm(peerid, [callback])
`options` is an optional object argument that might include the following keys:
* recursive(boolean) Add directory paths recursively
ipfsCluster.pins.ls([options], [callback])
ipfsCluster.pins.add(cid, [options], [callback]) // e.g. { "replication_factor": 2 }
ipfsCluster.pins.rm(cid, [options], [callback])
ipfsCluster.allocations.filter(hash, [options], [callback])
`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. If successful, `res` will be an empty array `[]`, as the Go implementation of `ipfs-cluster` APIs returns `nil` as response.
ipfsCluster.status([cid], [options], [callback])
ipfsCluster.sync([cid], [options], [callback])
ipfsCluster.recover([cid], [options], [callback])
If no `callback` is passed, a promise is returned.
ipfsCluster.health.graph([options], [callback])
ipfsCluster.health.metrics(name, [options], [callback])
### Example
```
cluster.add(Buffer.from("vasa"), (err, result) => {
err ? console.error(err) : console.log(result)
})
```
### Peer management
> Lists, adds & removes peers from the cluster
ipfsCluster.monitor(name, [options], [callback])
* **`peers`**
ipfsCluster.version([callback])
* **`ls`**
> Lists the peers in the cluster
This command tells IPFS Cluster to no longer manage a CID. This will
trigger unpinning operations in all the IPFS nodes holding the content.
When the request has succeeded, the command returns the status of the CID in the cluster. The CID should disappear from the list offered by "pin ls", although unpinning operations in the cluster may take longer or fail.
**`ipfsCluster.peers.ls([callback])`**
`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. If successful, `res` returns a information abount the connected peers in the following form:
```
[ { id: 'QmPq34QAMCFLNTXWtM3pc7qeQ2kneuCgLZjSVywWoEumRn',
addresses:
[ '/p2p-circuit/ipfs/QmPq34QAMCFLNTXWtM3pc7qeQ2kneuCgLZjSVywWoEumRn',
'/ip4/127.0.0.1/tcp/9096/ipfs/QmPq34QAMCFLNTXWtM3pc7qeQ2kneuCgLZjSVywWoEumRn',
'/ip4/10.184.9.134/tcp/9096/ipfs/QmPq34QAMCFLNTXWtM3pc7qeQ2kneuCgLZjSVywWoEumRn',
'/ip4/172.17.0.1/tcp/9096/ipfs/QmPq34QAMCFLNTXWtM3pc7qeQ2kneuCgLZjSVywWoEumRn',
'/ip4/172.18.0.1/tcp/9096/ipfs/QmPq34QAMCFLNTXWtM3pc7qeQ2kneuCgLZjSVywWoEumRn' ],
cluster_peers: [ 'QmPq34QAMCFLNTXWtM3pc7qeQ2kneuCgLZjSVywWoEumRn' ],
cluster_peers_addresses: null,
version: '0.10.1',
commit: '',
rpc_protocol_version: '/ipfscluster/0.10/rpc',
error: '',
ipfs:
{ id: 'QmdKAFhAAnc6U3ik6XfEDVKEsok7TnQ1yeyXmnnvGFmBhx',
addresses: [Array],
error: '' },
peername: 'jarvis' } ]
```
### Example
```
cluster.peers.ls((err, peers) => {
err ? console.error(err) : console.log(peers)
})
```
* **`add`**
> Adds peers to the cluster
**`ipfsCluster.peers.add(addr, [callback])`**
Where `addr` is the [multiaddress](http://multiformats.io/multiaddr/) of the peer to be added.
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
If no `callback` is passed, a promise is returned.
### Example
```
cluster.peers.add("/ip4/1.2.3.4/tcp/1234/QmdKAFhAAnc6U3ik6XfEDVKEsok7TnQ1yeyXmnnvGFmBhx", {}, (err) => {
err ? console.error(err) : console.log("peer added")
}
```
* **`remove`**
> Removes peer from the cluster
This command removes a peer from the cluster. If the peer is online, it will automatically shut down. All other cluster peers should be online for the operation to succeed, otherwise some nodes may be left with an outdated list of cluster peers.
**`ipfsCluster.peers.rm(peerid, [callback])`**
Where `peerid` is the `id` of the peer to be removed.
`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful.
If no `callback` is passed, a promise is returned.
### Example
```
cluster.peers.rm("QmdKAFhAAnc6U3ik6XfEDVKEsok7TnQ1yeyXmnnvGFmBhx", (err) => {
err ? console.error(err) : console.log("peer removed")
}
```
### Pins management
> Lists, adds & removes pins from the pinlist of the cluster
* **`pin`**
* **`ls`**
> Lists the pins in the pinlist
This command will list the CIDs which are tracked by IPFS Cluster and to which peers they are currently allocated. This list does not include any monitoring information about the IPFS status of the CIDs, it merely represents the list of pins which are part of the shared state of the cluster. For IPFS-status information about the pins, use "status".
**`ipfsCluster.pin.ls([options], [callback])`**
`options` is an optional object argument that might include the following keys:
* `filter`: (default: `pin`) The filter only takes effect when listing all pins. The possible values are:
- all
- pin
- meta-pin
- clusterdag-pin
- shard-pin
`callback` must follow `function (err, pins) {}` signature, where `err` is an error if the operation was not successful. If successful, `pins` returns the list of pins.
If no `callback` is passed, a promise is returned.
### Example
```
cluster.pin.ls({filter: 'all'}, (err, pins) => {
err ? console.error(err) : console.log(pins)
}
```
* **`add`**
> Adds a pin to the cluster
This command tells IPFS Cluster to start managing a CID. Depending on
the pinning strategy, this will trigger IPFS pin requests. The CID will
become part of the Cluster's state and will tracked from this point.
When the request has succeeded, the command returns the status of the CID in the cluster and should be part of the list offered by "pin ls".
An optional replication factor can be provided: -1 means "pin everywhere" and 0 means use cluster's default setting. Positive values indicate how many peers should pin this content.
An optional allocations argument can be provided, allocations should be a comma-separated list of peer IDs on which we want to pin. Peers in allocations are prioritized over automatically-determined ones, but replication factors would stil be respected.
**`ipfsCluster.pin.add(cid, [options], [callback])`**
Where `cid` is the [CID](https://docs.ipfs.io/guides/concepts/cid/) of the data to be pinned.
`options` is an optional object argument that might include the following keys:
> TODO: Add options
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
If no `callback` is passed, a promise is returned.
### Example
```
cluster.pin.add(CID, (err) => {
err ? console.error(err) : console.log('pin added')
}
```
* **`remove`**
> Removes a pin from the pinlist
This command tells IPFS Cluster to no longer manage a CID. This will
trigger unpinning operations in all the IPFS nodes holding the content.
When the request has succeeded, the command returns the status of the CID in the cluster. The CID should disappear from the list offered by "pin ls", although unpinning operations in the cluster may take longer or fail.
**`ipfsCluster.pin.rm(cid, [options], [callback])`**
Where `cid` is the [CID](https://docs.ipfs.io/guides/concepts/cid/) of the data to be unpinned.
`options` is an optional object argument that might include the following keys:
> TODO: Add options
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
If no `callback` is passed, a promise is returned.
### Example
```
const CID = "QmU4xZd9Yj7EzRj5ntw6AJ1VkbWNe1jXRM56KoRLkTxKch"
cluster.pin.rm(CID, (err) => {
err ? console.error(err) : console.log(`${CID} unpinned`)
})
```
#### Node management
* **`id`**
> Gets the connected peer's name, address info
This command displays information about the peer that the tool is contacting.
**`ipfsCluster.id([callback])`**
`callback` must follow `function (err, id) {}` signature, where `err` is an error if the operation was not successful. If successful, `id` returns the information about the peer that the tool is contacting.
If no `callback` is passed, a promise is returned.
### Example
```
cluster.id((err, id) => {
err ? console.error(err) : console.log(id)
}
```
## Maintainer
* **`version`**
> Gets the current version of IPFS Cluster version
[**Vaibhav Saini**](https://github.com/vasa-develop)
This command retrieves the IPFS Cluster version and can be used
to check that it matches the CLI version
The code is mostly from `js-ipfs-api` modified to consume the `ipfs-cluster` API.
**`ipfsCluster.version([callback])`**
`callback` must follow `function (err, version) {}` signature, where `err` is an error if the operation was not successful. If successful, `version` will return the IPFS Cluster version.
If no `callback` is passed, a promise is returned.
### Example
```
cluster.version((err, version) => {
err ? console.error(err) : console.log(version)
}
```
* **`health`**
* **`ipfsCluster.health.graph([options], [callback])`**
> Lists the health graph of the cluster
This command queries all connected cluster peers and their ipfs peers to generate a graph of the connections. Output is a dot file encoding the cluster's connection state.
`options` is an optional object argument that might include the following keys:
> TODO: add options
`callback` must follow `function (err, graph) {}` signature, where `err` is an error if the operation was not successful. If successful, `graph` returns the cluster's current state.
If no `callback` is passed, a promise is returned.
### Example
```
cluster.health.graph((err, health) => {
err ? console.error(err) : console.log(health)
}
```
* **`ipfsCluster.health.metrics(name, [options], [callback])`**
> Lists the health metrics of the cluster
This commands displays the latest valid metrics of the given type logged by this peer for all current cluster peers.
`type` is the type of the monitoring desired(`freespace` OR `ping`)
`callback` must follow `function (err, metrics) {}` signature, where `err` is an error if the operation was not successful. If successful, `metrics` returns the desired metrics.
If no `callback` is passed, a promise is returned.
### Example
```
cluster.health.metrics('freespace', (err, metrics) => {
err ? console.error(err) : console.log(metrics)
}
````
* **`status`**
> Retrieves the status of the CIDs tracked by IPFS
Cluster
This command retrieves the status of the CIDs tracked by IPFS
Cluster, including which member is pinning them and any errors.
If a CID is provided, the status will be only fetched for a single
item. Metadata CIDs are included in the status response
The status of a CID may not be accurate. A manual sync can be triggered
with "sync".
When the `local` option is set, it will only fetch the status from the
contacted cluster peer. By default, status will be fetched from all peers.
When the `filter` option is set, it will only fetch the peer information
where status of the pin matches at least one of the filter values.
**`ipfsCluster.status([cid], [options], [callback])`**
Where `cid` is the [CID](https://docs.ipfs.io/guides/concepts/cid/) of the data for which we need the status.
`options` is an optional object argument that might include the following keys:
* `filter`(string): list of filters
- error
- cluster_error
- pin_error
- pin_queued
- pinned
- pinning
- queued
- remote
- unpin_error
- unpin_queued
- unpinned
- unpinning
* `local`(boolean): if set `true`, runs operation only on the contacted peer
`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. If successful `res` returns the status of the passed `cid`
If no `callback` is passed, a promise is returned.
### Example
```
const CID = "QmU4xZd9Yj7EzRj5ntw6AJ1VkbWNe1jXRM56KoRLkTxKch"
cluster.status(CID, { filter: 'pinned', local: true }, (err, res) => {
err ? console.error(err) : console.log(res)
}
```
* **`sync`**
> Syncs the pinset/CID across all the peers in the cluster
This command asks Cluster peers to verify that the current status of tracked
CIDs is accurate by triggering queries to the IPFS daemons that pin them.
If a CID is provided, the sync and recover operations will be limited to
that single item.
Unless providing a specific CID, the command will output only items which
have changed status because of the sync or are in error state in some node,
therefore, the output should be empty if no operations were performed.
CIDs in error state may be manually recovered with "recover".
When the `local` option is passed, it will only trigger sync
operations on the contacted peer. By default, all peers will sync.
**`ipfsCluster.sync([cid], [options], [callback])`**
Where `cid` is the [CID](https://docs.ipfs.io/guides/concepts/cid/) of the data to be synced.
`options` is an optional object argument that might include the following keys:
* `local`(boolean): if set `true`, runs operation only on the contacted peer
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
If no `callback` is passed, a promise is returned.
### Example
```
const CID = "QmU4xZd9Yj7EzRj5ntw6AJ1VkbWNe1jXRM56KoRLkTxKch"
cluster.sync(CID, { local: true }, (err) => {
err ? console.error(err) : console.log(`${CID} synced`)
}
```
* **`recover`**
> re-track or re-forget CIDs in error state
This command asks Cluster peers to re-track or re-forget CIDs in
error state, usually because the IPFS pin or unpin operation has failed.
The command will wait for any operations to succeed and will return the status
of the item upon completion. Note that, when running on the full sets of tracked
CIDs (without argument), it may take a considerably long time.
When the `local` option is set, it will only trigger recover
operations on the contacted peer (as opposed to on every peer).
**`ipfsCluster.recover([cid], [options], [callback])`**
Where `cid` is the [CID](https://docs.ipfs.io/guides/concepts/cid/) of the data to be recovered.
`options` is an optional object argument that might include the following keys:
* `local`(boolean): if set `true` it will only trigger recover
operations on the contacted peer
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
If no `callback` is passed, a promise is returned.
### Example
```
const CID = "QmU4xZd9Yj7EzRj5ntw6AJ1VkbWNe1jXRM56KoRLkTxKch"
cluster.recover(CID, { local: true }, (err) => {
err ? console.error(err) : console.log(`${CID} recovered`)
}
```
## Development
### Testing
We run tests by executing `npm test` in a terminal window. This will run Node.js tests.
## Contribute
PRs Accepted.
The `ipfs-cluster-api` is a work in progress. As such, there's a few things you can do right now to help out:
- **[Check out the existing issues](https://github.com/cluster-labs/ipfs-cluster-api/issues)**!
- **Perform code reviews**. More eyes will help
- speed the project along
- ensure quality and
- reduce possible future bugs.
- **Add tests**. There can never be enough tests.
- **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.
## Historical Context
This module started as a direct mapping from the Go `ipfs-cluster-ctl` to a JavaScript implementation.
## License
MIT
[MIT](LICENSE)

7

src/health/metrics.js

@@ -13,6 +13,9 @@ 'use strict'

var metricsPath = `health/metrics/${arg}`
var monitorPath = 'monitor/metrics';
if (arg) {
monitorPath += '/' + arg;
}
send({
path: metricsPath,
path: monitorPath,
qs: opts

@@ -19,0 +22,0 @@ }, callback)

@@ -5,6 +5,6 @@ 'use strict'

const cmds = {
add: require('../add'),
id: require('../id'),
peers: require('../peers'),
pins: require('../pins'),
allocations: require('../allocations'),
pin: require('../pin'),
status: require('../status'),

@@ -14,4 +14,3 @@ sync: require('../sync'),

version: require('../version'),
health: require('../health'),
monitor: require('../monitor')
health: require('../health')
}

@@ -18,0 +17,0 @@

@@ -8,10 +8,14 @@ 'use strict'

const streamToJsonValue = require('./stream-to-json-value')
const qsDefaultEncoder = require('qs/lib/utils').encode
const isNode = require('detect-node')
const ndjson = require('ndjson')
const pump = require('pump')
const log = require('debug')('ipfs-http-client:request')
function parseError (res, callback) {
function parseError (res, cb) {
const error = new Error(`Server responded with ${res.statusCode}`)
// streamToJsonValue(res, (err, payload) => {
streamToValue(res, (err, payload) => {
streamToJsonValue(res, (err, payload) => {
if (err) {
return callback(err)
return cb(err)
}

@@ -22,21 +26,59 @@

error.message = payload.Message || payload.toString()
error.type = payload.Type
}
callback(error)
cb(error)
})
}
function onRes (buffer, callback) {
function onRes (buffer, cb) {
return (res) => {
const stream = Boolean(res.headers['x-stream-output'])
const chunkedObjects = Boolean(res.headers['x-chunked-output'])
const isJson = res.headers['content-type'] &&
res.headers['content-type'].indexOf('application/json') === 0
if (res.req) {
log(res.req.method, `${res.req.getHeaders().host}${res.req.path}`, res.statusCode, res.statusMessage)
} else {
log(res.url, res.statusCode, res.statusMessage)
}
if (res.statusCode >= 400 || !res.statusCode) {
parseError(res, callback)
return parseError(res, cb)
}
// Return the response stream directly
if (stream && !buffer) {
//console.log("LOG_0: ", res)
return cb(null, res)
}
// Return a stream of JSON objects
if (chunkedObjects && isJson) {
const outputStream = ndjson.parse()
pump(res, outputStream)
res.on('end', () => {
let err = res.trailers['x-stream-error']
if (err) {
// Not all errors are JSON
try {
err = JSON.parse(err)
} catch (e) {
err = { Message: err }
}
outputStream.emit('error', new Error(err.Message))
}
})
//console.log("LOG_1: ", outputStream)
return cb(null, outputStream)
}
// Return a JSON object
if (isJson) {
return streamToJsonValue(res, callback)
//console.log("LOG_2: ", res)
return streamToJsonValue(res, cb)
}
return streamToValue(res, callback)
// Return a value
return streamToValue(res, cb)
}

@@ -49,8 +91,54 @@ }

if (Array.isArray(options.path)) {
options.path = options.path.join('/')
}
if (options.args && !Array.isArray(options.args)) {
options.args = [options.args]
}
if (options.args) {
options.qs.arg = options.args
}
if (options.progress) {
options.qs.progress = true
}
if (options.qs.r) {
options.qs.recursive = options.qs.r
// From IPFS 0.4.0, it throws an error when both r and recursive are passed
delete options.qs.r
}
options.qs['stream-channels'] = true
if (options.stream) {
options.buffer = false
}
// this option is only used internally, not passed to daemon
delete options.qs.followSymlinks
////console.log(options)
const method = options.method || 'GET'
const headers = options.header || {}
var headers = options.header || {}
headers = Object.assign(headers, config.headers)
if (isNode) {
// Browsers do not allow you to modify the user agent
headers['User-Agent'] = config['user-agent']
}
if (options.multipart) {
if (!options.multipartBoundary) {
return callback(new Error('No multipartBoundary'))
}
headers['Content-Type'] = `multipart/form-data; boundary=${options.multipartBoundary}`
}
const qs = Qs.stringify(options.qs, {
arrayFormat: 'repeat',
encoder: (data, qsDefaultEncoder) => {
encoder: data => {
// TODO: future releases of qs will provide the default
// encoder as a 2nd argument to this function; it will
// no longer be necessary to import qsDefaultEncoder
if (Buffer.isBuffer(data)) {

@@ -62,10 +150,10 @@ let uriEncoded = ''

if (
(byte >= 0x41 && byte <= 0x5A) ||
(byte >= 0x61 && byte <= 0x7A) ||
(byte >= 0x30 && byte <= 0x39) ||
(byte === 0x2D) ||
(byte === 0x2E) ||
(byte === 0x5F) ||
(byte === 0x7E)
) {
(byte >= 0x41 && byte <= 0x5A) ||
(byte >= 0x61 && byte <= 0x7A) ||
(byte >= 0x30 && byte <= 0x39) ||
(byte === 0x2D) ||
(byte === 0x2E) ||
(byte === 0x5F) ||
(byte === 0x7E)
) {
uriEncoded += String.fromCharCode(byte)

@@ -97,9 +185,6 @@ } else {

// if (options.files) { stream.pipe(req) }
if (options.data) {
req.write(options.data);
if (!options.stream) {
req.end()
}
req.end()
return req

@@ -106,0 +191,0 @@ }

const cluster = require('./helpers')
const assert = require('chai').assert
/* describe('health.metrics', () => {
it('lists latest metrics logged by this peer (without options)', (done) => {
cluster.health.metrics({}, (err, health) => {
assert.notExists(err, 'throws error while listing latest metrics logged by this peer (without options)')
describe('health.metrics', () => {
it('logs \'freespace\' metrics for a peer', (done) => {
cluster.health.metrics('freespace', (err, metrics) => {
assert.notExists(err, 'throws error while logging metrics for a peer')
done()
})
})
}) */
it('logs \'ping\' metrics for a peer', (done) => {
cluster.health.metrics('ping', (err, metrics) => {
assert.notExists(err, 'throws error while logging metrics for a peer')
done()
})
})
})

@@ -6,3 +6,3 @@ const cluster = require('./helpers')

it('shows cluster peer and ipfs daemon information', (done) => {
cluster.id((err, details) => {
cluster.id((err, id) => {
assert.notExists(err, 'throws error while fetching cluster peer and ipfs daemon information')

@@ -9,0 +9,0 @@ done()

@@ -13,3 +13,3 @@ const cluster = require('./helpers')

cluster.peers.ls({protocol: 'http'}, (err, peers) => {
cluster.peers.ls((err, peers) => {
assert.equal(err, null, 'throws error while fetching the list of cluster peers')

@@ -16,0 +16,0 @@ if (peers.length>=2){

@@ -36,9 +36,2 @@ const cluster = require('./helpers')

it('lists current status of tracked CIDs with \'pinned\' status (local state)', (done) => {
cluster.status({ filter: 'pinned', local: true }, (err, details) => {
assert.notExists(err, 'throws error while listing current status of tracked CIDs with \'pinned\' status (local state)')
done()
})
})
it('lists current status of tracked CIDs with \'pinning\' status (local state)', (done) => {

@@ -45,0 +38,0 @@ cluster.status({ filter: 'pinning', local: true }, (err, details) => {

@@ -7,3 +7,2 @@ const cluster = require('./helpers')

cluster.version((err, version) => {
assert.equal(JSON.stringify(version), JSON.stringify({ version: '0.10.1' }), "outputs invalid version")
assert.notExists(err, 'throws error while fetching cluster peer and ipfs daemon information')

@@ -10,0 +9,0 @@ done()

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc