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

Commit cdd4f39

Browse files
committed
Initial commit
0 parents  commit cdd4f39

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_moudles

index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var net = require('net')
2+
var multiaddr = require('multiaddr')
3+
4+
function request(address, command, args, opts, cb) {
5+
if(typeof args === 'function') {
6+
cb = args;
7+
args = null;
8+
} else if(typeof opts === 'function') {
9+
cb = opts;
10+
opts = null;
11+
} else if(!cb) {
12+
cb = function(){}
13+
}
14+
15+
if(!address)
16+
return cb(new Error('Must specify an address'))
17+
if(!command)
18+
return cb('Must specify a command')
19+
if(!typeof command === 'string')
20+
return cb('Command must be a string')
21+
if(args != null && !Array.isArray(args))
22+
return cb('Args must be an array')
23+
if(opts != null && typeof opts !== 'object')
24+
return cb('Opts must be an object')
25+
if(!typeof cb === 'function')
26+
return cb('Callback must be a function')
27+
28+
var req = new Buffer(JSON.stringify({
29+
Command: command,
30+
Args: args,
31+
Opts: opts
32+
}))
33+
34+
var socket = net.connect(address.nodeAddress(), function() {
35+
var data = [], length = 0
36+
37+
socket.on('error', cb)
38+
39+
socket.on('data', function(chunk) {
40+
data.push(chunk)
41+
length += chunk.length
42+
})
43+
44+
socket.on('end', function() {
45+
var res = Buffer.concat(data, length)
46+
cb(null, res)
47+
})
48+
49+
socket.write(req)
50+
})
51+
}
52+
53+
module.exports = {
54+
request: request
55+
}

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "ipfs-rpc",
3+
"version": "0.0.0",
4+
"description": "An client library for IPFS RPC services",
5+
"main": "index.js",
6+
"dependencies": {
7+
"multiaddr": "~0.1.2"
8+
},
9+
"devDependencies": {},
10+
"scripts": {
11+
"test": "echo \"Error: no test specified\" && exit 1"
12+
},
13+
"keywords": [
14+
"ipfs"
15+
],
16+
"author": "Matt Bell <mappum@gmail.com>",
17+
"license": "GPL"
18+
}

0 commit comments

Comments
 (0)