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

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Sep 14, 2014
0 parents commit cdd4f39
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_moudles
55 changes: 55 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var net = require('net')
var multiaddr = require('multiaddr')

function request(address, command, args, opts, cb) {
if(typeof args === 'function') {
cb = args;
args = null;
} else if(typeof opts === 'function') {
cb = opts;
opts = null;
} else if(!cb) {
cb = function(){}
}

if(!address)
return cb(new Error('Must specify an address'))
if(!command)
return cb('Must specify a command')
if(!typeof command === 'string')
return cb('Command must be a string')
if(args != null && !Array.isArray(args))
return cb('Args must be an array')
if(opts != null && typeof opts !== 'object')
return cb('Opts must be an object')
if(!typeof cb === 'function')
return cb('Callback must be a function')

var req = new Buffer(JSON.stringify({
Command: command,
Args: args,
Opts: opts
}))

var socket = net.connect(address.nodeAddress(), function() {
var data = [], length = 0

socket.on('error', cb)

socket.on('data', function(chunk) {
data.push(chunk)
length += chunk.length
})

socket.on('end', function() {
var res = Buffer.concat(data, length)
cb(null, res)
})

socket.write(req)
})
}

module.exports = {
request: request
}
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "ipfs-rpc",
"version": "0.0.0",
"description": "An client library for IPFS RPC services",
"main": "index.js",
"dependencies": {
"multiaddr": "~0.1.2"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"ipfs"
],
"author": "Matt Bell <mappum@gmail.com>",
"license": "GPL"
}

0 comments on commit cdd4f39

Please sign in to comment.