Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ipfs-api

Package Overview
Dependencies
Maintainers
1
Versions
177
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ipfs-api - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3-1

99

index.js

@@ -5,11 +5,17 @@ var net = require('net');

var assert = require('assert');
var request = require('request');
var http = require('http');
var Multipart = require('multipart-stream');
var qs = require('querystring');
var package = JSON.parse(fs.readFileSync(__dirname + '/package.json'));
try {
var package = JSON.parse(fs.readFileSync(__dirname + '/package.json'));
} catch(e) {
var package = { name: 'ipfs-api-browserify', version: '?' };
}
var API_PATH = "/api/v0/";
module.exports = function(address) {
assert(address, 'Must specify an address');
module.exports = function(host, port) {
if(!host) host = 'localhost';
if(!port) port = 5001;

@@ -22,24 +28,51 @@ function send(path, args, opts, files, cb) {

if(args) opts.arg = args;
var query = qs.stringify(opts);
return request.post({
uri: 'http://' + address + API_PATH + path,
qs: opts,
useQuerystring: true,
if(files) {
var boundary = randomString();
var contentType = 'multipart/form-data; boundary=' + boundary;
}
var req = http.request({
method: 'POST',
host: host,
port: port,
path: API_PATH + path + '?' + query,
headers: {
'User-Agent': '/node-'+package.name+'/'+package.version+'/'
},
formData: getFileForm(files)
}, function(err, res, data) {
try {
return cb(JSON.parse(err || 'null'), JSON.parse(data || 'null'));
} catch(e) {
return cb(err, data);
'User-Agent': '/node-'+package.name+'/'+package.version+'/',
'Content-Type': contentType || 'application/octet-stream'
}
}, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
try {
var parsed = JSON.parse(data);
data = parsed;
} catch(e){}
if(res.statusCode >= 400) {
return cb(data, null);
}
return cb(null, data);
});
res.on('error', function(err) {
return cb(err, null)
});
});
if(files) {
var stream = getFileStream(files, boundary);
stream.pipe(req);
} else {
req.end();
}
}
function getFileForm(files) {
function getFileStream(files, boundary) {
if(!files) return null;
var output = {};
var mp = new Multipart(boundary);
if(!Array.isArray(files)) files = [files];

@@ -52,22 +85,22 @@

// TODO: get actual content type
output[i] = {
value: fs.createReadStream(file),
options: {
contentType: 'application/octet-stream',
filename: file
mp.addPart({
body: fs.createReadStream(file),
headers: {
'Content-Type': 'application/octet-stream',
'Content-Disposition': 'file; name="file"; filename="'+file+'"'
}
};
});
} else if(Buffer.isBuffer(file)) {
output[i] = {
value: file,
options: {
contentType: 'application/octet-stream',
filename: ''
mp.addPart({
body: file,
headers: {
'Content-Type': 'application/octet-stream',
'Content-Disposition': 'file; name="file"; filename=""'
}
};
});
}
}
return output;
return mp;
}

@@ -151,1 +184,5 @@

}
function randomString() {
return Math.random().toString(36).slice(2) + Math.random().toString(36).slice(2);
}

@@ -1,11 +0,10 @@

var ipfs = require('./')('localhost:5001');
var ipfs = require('./')('localhost', 5001);
ipfs.add('/Users/mappum/Downloads/and.jpg', function(err, data) {
ipfs.add('/Users/mappum/hello.txt', function(err, data) {
if(err) return console.error(err)
console.log(data)
});
/*
ipfs.cat('Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9', function(err, data) {
console.log(err, data)
});
*/
{
"name": "ipfs-api",
"version": "0.0.2",
"version": "0.0.3-1",
"description": "An client library for the IPFS API",
"main": "index.js",
"dependencies": {
"multipart-stream": "^1.0.0",
"request": "^2.48.0"
"multipart-stream": "^1.0.0"
},

@@ -10,0 +9,0 @@ "devDependencies": {},

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