This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cdd4f39
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_moudles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |