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": {}, |
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
Network access
Supply chain riskThis module accesses the network.
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
5152
1
166
0
2
- Removedrequest@^2.48.0
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.15.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)