Comparing version 2.5.1 to 2.6.0
{ | ||
"name": "ipfs-api", | ||
"version": "2.5.1", | ||
"version": "2.6.0", | ||
"description": "A client library for the IPFS API", | ||
@@ -11,2 +11,3 @@ "main": "src/index.js", | ||
"multipart-stream": "^2.0.0", | ||
"request": "^2.65.0", | ||
"vinyl": "^0.5.1", | ||
@@ -29,6 +30,7 @@ "vinyl-fs-browser": "^2.1.1-1", | ||
"devDependencies": { | ||
"brfs": "^1.4.1", | ||
"babel-eslint": "^4.1.3", | ||
"browserify": "^11.0.0", | ||
"concurrently": "^0.1.1", | ||
"eslint-config-standard": "^4.4.0", | ||
"eslint-plugin-standard": "^1.3.1", | ||
"gulp": "^3.9.0", | ||
@@ -38,3 +40,3 @@ "gulp-eslint": "^1.0.0", | ||
"gulp-mocha": "^2.1.3", | ||
"ipfsd-ctl": "^0.5.5", | ||
"ipfsd-ctl": "^0.6.1", | ||
"karma": "^0.13.11", | ||
@@ -60,3 +62,4 @@ "karma-browserify": "^4.4.0", | ||
"pre-commit": [ | ||
"lint" | ||
"lint", | ||
"test:node" | ||
], | ||
@@ -63,0 +66,0 @@ "keywords": [ |
IPFS API wrapper library in JavaScript | ||
====================================== | ||
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) [![](https://img.shields.io/badge/freejs-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![Dependency Status](https://david-dm.org/ipfs/js-ipfs-api.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfs-api) | ||
[![Circle CI](https://circleci.com/gh/ipfs/js-ipfs-api.svg?style=svg)](https://circleci.com/gh/ipfs/js-ipfs-api) | ||
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) [![](https://img.shields.io/badge/freejs-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![Dependency Status](https://david-dm.org/ipfs/js-ipfs-api.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfs-api) [![Circle CI](https://circleci.com/gh/ipfs/js-ipfs-api.svg?style=svg)](https://circleci.com/gh/ipfs/js-ipfs-api) | ||
@@ -11,4 +10,2 @@ > A client library for the IPFS API. | ||
### NOTE - Current version only supports IPFS 0.3.7 | ||
## Installing the module | ||
@@ -15,0 +12,0 @@ |
@@ -11,3 +11,2 @@ var File = require('vinyl') | ||
if (!files) return null | ||
if (!Array.isArray(files)) files = [files] | ||
@@ -35,3 +34,3 @@ // merge all inputs into one stream | ||
// if recursive, glob the contents | ||
if (opts.r || opts.recursive) { | ||
if (opts.recursive) { | ||
adder.add(vinylfs.src(file + '/**/*', srcOpts)) | ||
@@ -38,0 +37,0 @@ } |
@@ -1,108 +0,123 @@ | ||
var http = require('http') | ||
var qs = require('querystring') | ||
var getFilesStream = require('./get-files-stream') | ||
'use strict' | ||
exports = module.exports = function getRequestAPI (config) { | ||
return requestAPI | ||
const request = require('request') | ||
const getFilesStream = require('./get-files-stream') | ||
function requestAPI (path, args, opts, files, buffer, cb) { | ||
var query, stream, contentType | ||
contentType = 'application/json' | ||
const isNode = !global.window | ||
if (Array.isArray(path)) path = path.join('/') | ||
// -- Internal | ||
opts = opts || {} | ||
function onEnd (buffer, result, cb) { | ||
return (err, res, body) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
if (args && !Array.isArray(args)) args = [args] | ||
if (args) opts.arg = args | ||
if (res.statusCode >= 400 || !res.statusCode) { | ||
return cb(new Error(`Server responded with ${res.statusCode}: ${body}`)) | ||
} | ||
if (files) { | ||
stream = getFilesStream(files, opts) | ||
if (!stream.boundary) { | ||
throw new Error('no boundary in multipart stream') | ||
} | ||
contentType = 'multipart/form-data; boundary=' + stream.boundary | ||
if ((result.stream && !buffer) || | ||
(result.chunkedObjects && buffer)) { | ||
return cb(null, body) | ||
} | ||
if (typeof buffer === 'function') { | ||
cb = buffer | ||
buffer = false | ||
if (result.chunkedObjects) return cb(null, result.objects) | ||
try { | ||
const parsedBody = JSON.parse(body) | ||
cb(null, parsedBody) | ||
} catch (e) { | ||
cb(null, body) | ||
} | ||
} | ||
} | ||
// this option is only used internally, not passed to daemon | ||
delete opts.followSymlinks | ||
function onData (result) { | ||
return chunk => { | ||
if (!result.chunkedObjects) return | ||
opts['stream-channels'] = true | ||
query = qs.stringify(opts) | ||
var reqo = { | ||
method: files ? 'POST' : 'GET', | ||
host: config.host, | ||
port: config.port, | ||
path: config['api-path'] + path + '?' + query, | ||
headers: { | ||
'User-Agent': config['user-agent'], | ||
'Content-Type': contentType | ||
}, | ||
withCredentials: false | ||
try { | ||
const obj = JSON.parse(chunk.toString()) | ||
result.objects.push(obj) | ||
} catch (e) { | ||
result.chunkedObjects = false | ||
} | ||
} | ||
} | ||
var req = http.request(reqo, function (res) { | ||
var data = '' | ||
var objects = [] | ||
var stream = !!res.headers && !!res.headers['x-stream-output'] | ||
var chunkedObjects = !!res.headers && !!res.headers['x-chunked-output'] | ||
function onResponse (result) { | ||
return res => { | ||
result.stream = !!res.headers['x-stream-output'] | ||
result.chunkedObjects = !!res.headers['x-chunked-output'] | ||
} | ||
} | ||
if (stream && !buffer) return cb(null, res) | ||
if (chunkedObjects && buffer) return cb(null, res) | ||
function makeRequest (opts, buffer, cb) { | ||
// this option is only used internally, not passed to daemon | ||
delete opts.qs.followSymlinks | ||
res.on('data', function (chunk) { | ||
if (!chunkedObjects) { | ||
data += chunk | ||
return data | ||
} | ||
const result = { | ||
stream: false, | ||
chunkedObjects: false, | ||
objects: [] | ||
} | ||
try { | ||
var obj = JSON.parse(chunk.toString()) | ||
objects.push(obj) | ||
} catch (e) { | ||
chunkedObjects = false | ||
data += chunk | ||
} | ||
}) | ||
res.on('end', function () { | ||
var parsed | ||
return request(opts, onEnd(buffer, result, cb)) | ||
.on('data', onData(result)) | ||
.on('response', onResponse(result)) | ||
} | ||
if (!chunkedObjects) { | ||
try { | ||
parsed = JSON.parse(data) | ||
data = parsed | ||
} catch (e) {} | ||
} else { | ||
data = objects | ||
} | ||
function requestAPI (config, path, args, qs, files, buffer, cb) { | ||
qs = qs || {} | ||
if (Array.isArray(path)) path = path.join('/') | ||
if (args && !Array.isArray(args)) args = [args] | ||
if (args) qs.arg = args | ||
if (files && !Array.isArray(files)) files = [files] | ||
if (res.statusCode >= 400 || !res.statusCode) { | ||
if (!data) data = new Error() | ||
return cb(data, null) | ||
} | ||
return cb(null, data) | ||
}) | ||
res.on('error', function (err) { | ||
return cb(err, null) | ||
}) | ||
}) | ||
if (typeof buffer === 'function') { | ||
cb = buffer | ||
buffer = false | ||
} | ||
req.on('error', function (err) { | ||
return cb(err, null) | ||
}) | ||
if (qs.r) qs.recursive = qs.r | ||
if (stream) { | ||
stream.pipe(req) | ||
} else { | ||
req.end() | ||
if (!isNode && qs.recursive && path === 'add') { | ||
return cb(new Error('Recursive uploads are not supported in the browser')) | ||
} | ||
qs['stream-channels'] = true | ||
const opts = { | ||
method: files ? 'POST' : 'GET', | ||
uri: `http://${config.host}:${config.port}${config['api-path']}${path}`, | ||
qs: qs, | ||
useQuerystring: true, | ||
headers: {}, | ||
withCredentials: false, | ||
gzip: true | ||
} | ||
if (isNode) { | ||
// Browsers do not allow you to modify the user agent | ||
opts.headers['User-Agent'] = config['user-agent'] | ||
} | ||
if (files) { | ||
const stream = getFilesStream(files, qs) | ||
if (!stream.boundary) { | ||
return cb(new Error('No boundary in multipart stream')) | ||
} | ||
return req | ||
opts.headers['Content-Type'] = `multipart/form-data; boundary=${stream.boundary}` | ||
stream.pipe(makeRequest(opts, buffer, cb)) | ||
} else { | ||
makeRequest(opts, buffer, cb) | ||
} | ||
} | ||
// -- Interface | ||
exports = module.exports = function getRequestAPI (config) { | ||
return requestAPI.bind(null, config) | ||
} |
@@ -133,14 +133,15 @@ /* global describe it before */ | ||
it('add a nested dir', function (done) { | ||
if (!isNode) { | ||
return done() | ||
} | ||
this.timeout(10000) | ||
apiClients['a'].add(__dirname + '/test-folder', { recursive: true }, function (err, res) { | ||
if (err) { | ||
throw err | ||
if (isNode) { | ||
if (err) throw err | ||
var added = res[res.length - 1] | ||
assert.equal(added.Hash, 'QmZdsefMGMeG6bL719gX44XSVQrL6psEgRZdw1SGadFaK2') | ||
done() | ||
} else { | ||
assert.equal(err.message, 'Recursive uploads are not supported in the browser') | ||
done() | ||
} | ||
var added = res[res.length - 1] | ||
assert.equal(added.Hash, 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q') | ||
done() | ||
}) | ||
@@ -290,2 +291,4 @@ }) | ||
assert(res) | ||
assert(res.Version) | ||
console.log(' - running against version', res.Version) | ||
done() | ||
@@ -478,2 +481,3 @@ }) | ||
} | ||
apiClients['a'].ping(id.ID, function (err, res) { | ||
@@ -509,3 +513,3 @@ if (err) { | ||
apiClients['b'].pin.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', function (err, res) { | ||
apiClients['b'].pin.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, function (err, res) { | ||
if (err) { | ||
@@ -534,3 +538,3 @@ throw err | ||
apiClients['b'].pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: true}, function (err, res) { | ||
apiClients['b'].pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, function (err, res) { | ||
if (err) { | ||
@@ -540,3 +544,3 @@ throw err | ||
assert(res) | ||
apiClients['b'].pin.list(function (err, res) { | ||
apiClients['b'].pin.list('direct', function (err, res) { | ||
if (err) { | ||
@@ -608,13 +612,13 @@ throw err | ||
var result = [{ | ||
Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmaeuuKLHzirbVoTjb3659fyyV381amjaGrU2pecHEWPrN add.js\n', | ||
Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmaeuuKLHzirbVoTjb3659fyyV381amjaGrU2pecHEWPrN add.js', | ||
Err: '' }, | ||
{ Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTQhTtDWeaaP9pttDd1CuoVTLQm1w51ABfjgmGUbCUF6i cat.js\n', | ||
{ Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTQhTtDWeaaP9pttDd1CuoVTLQm1w51ABfjgmGUbCUF6i cat.js', | ||
Err: '' }, | ||
{ Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTYFLz5vsdMpq4XXw1a1pSxujJc9Z5V3Aw1Qg64d849Zy files\n', | ||
{ Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTYFLz5vsdMpq4XXw1a1pSxujJc9Z5V3Aw1Qg64d849Zy files', | ||
Err: '' }, | ||
{ Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTjXxUemcuMAZ2KNN3iJGWHwrkMsW8SWEwkYVSBi1nFD9 ipfs-add.js\n', | ||
{ Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmTjXxUemcuMAZ2KNN3iJGWHwrkMsW8SWEwkYVSBi1nFD9 ipfs-add.js', | ||
Err: '' }, | ||
{ Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmXYUXDFNNh1wgwtX5QDG7MsuhAAcE9NzDYnz8SjnhvQrK ls.js\n', | ||
{ Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmXYUXDFNNh1wgwtX5QDG7MsuhAAcE9NzDYnz8SjnhvQrK ls.js', | ||
Err: '' }, | ||
{ Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmUmDmH4hZgN5THnVP1VjJ1YWh5kWuhLGUihch8nFiD9iy version.js\n', | ||
{ Ref: 'QmaMTzaGBmdLrispnPRTESta4yDQdK4uKSVcQez2No4h6q QmUmDmH4hZgN5THnVP1VjJ1YWh5kWuhLGUihch8nFiD9iy version.js', | ||
Err: '' } ] | ||
@@ -643,16 +647,19 @@ | ||
apiClients['a'].dht.put('scope', 'interplanetary', function (err, cb) { | ||
assert(!err) | ||
apiClients['a'].dht.put('scope', 'interplanetary', function (err, res) { | ||
if (err) { | ||
done() | ||
return | ||
throw err | ||
} | ||
apiClients['a'].dht.get('scope', function (err, value) { | ||
if (err) { | ||
throw err | ||
} | ||
assert.equal(value, 'interplanetary') | ||
done() | ||
}) | ||
return done() | ||
// non ipns or pk hashes fail to fetch, known bug | ||
// bug: https://github.com/ipfs/go-ipfs/issues/1923#issuecomment-152932234 | ||
// apiClients['a'].dht.get('scope', function (err, value) { | ||
// console.log('->>', err, value) | ||
// if (err) { | ||
// throw err | ||
// } | ||
// assert.equal(value, 'interplanetary') | ||
// done() | ||
// }) | ||
}) | ||
@@ -659,0 +666,0 @@ }) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
888425
20427
1
8
20
217
+ Addedrequest@^2.65.0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.15.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)