Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove buffer-equals, remove safe-buffer #16

Merged
merged 4 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
language: node_js
node_js:
- '4.0'
- '6.0'
- '8.0'
- 'node'
- lts/*
10 changes: 4 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var isIP = require('net').isIP
var dns = require('dns')
var util = require('util')
var events = require('events')
var Buffer = require('safe-buffer').Buffer
var equals = require('buffer-equals')

var ETIMEDOUT = new Error('Query timed out')
ETIMEDOUT.code = 'ETIMEDOUT'
Expand Down Expand Up @@ -108,7 +106,7 @@ function RPC (opts) {
}

var rid = message.r && message.r.id
if (req.peer && req.peer.id && rid && !equals(req.peer.id, rid)) {
if (req.peer && req.peer.id && rid && !req.peer.id.equals(rid)) {
req.callback(EUNEXPECTEDNODE, null, rinfo)
self.emit('update')
self.emit('postupdate')
Expand All @@ -134,11 +132,11 @@ RPC.prototype.address = function () {
}

RPC.prototype.response = function (peer, req, res, cb) {
this.send(peer, {t: req.t, y: 'r', r: res}, cb)
this.send(peer, { t: req.t, y: 'r', r: res }, cb)
}

RPC.prototype.error = function (peer, req, error, cb) {
this.send(peer, {t: req.t, y: 'e', e: [].concat(error.message || error)}, cb)
this.send(peer, { t: req.t, y: 'e', e: [].concat(error.message || error) }, cb)
}

RPC.prototype.send = function (peer, message, cb) {
Expand Down Expand Up @@ -215,7 +213,7 @@ RPC.prototype._resolveAndQuery = function (peer, query, cb) {
dns.lookup(peer.host, function (err, ip) {
if (err) return cb(err)
if (self.destroyed) return cb(new Error('k-rpc-socket is destroyed'))
self.query({host: ip, port: peer.port}, query, cb)
self.query({ host: ip, port: peer.port }, query, cb)
})
}

Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"description": "Low level implementation of the k-rpc network layer that the BitTorrent DHT uses",
"main": "index.js",
"dependencies": {
"bencode": "^2.0.0",
"buffer-equals": "^1.0.4",
"safe-buffer": "^5.1.1"
"bencode": "^2.0.0"
},
"devDependencies": {
"standard": "*",
Expand Down
26 changes: 13 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ tape('query + response', function (t) {
queried = true
t.same(peer.address, '127.0.0.1')
t.same(query.q.toString(), 'hello_world')
t.same(query.a, {hej: 10})
server.response(peer, query, {hello: 42})
t.same(query.a, { hej: 10 })
server.response(peer, query, { hello: 42 })
})

server.bind(0, function () {
var port = server.address().port
var client = rpc()
t.same(client.inflight, 0)
client.query({host: '127.0.0.1', port: port}, {q: 'hello_world', a: {hej: 10}}, function (err, res) {
client.query({ host: '127.0.0.1', port: port }, { q: 'hello_world', a: { hej: 10 } }, function (err, res) {
t.same(client.inflight, 0)
server.destroy()
client.destroy()
t.error(err)
t.ok(queried)
t.same(res.r, {hello: 42})
t.same(res.r, { hello: 42 })
t.end()
})
t.same(client.inflight, 1)
Expand All @@ -35,20 +35,20 @@ tape('parallel query', function (t) {
var server = rpc()

server.on('query', function (query, peer) {
server.response(peer, query, {echo: query.a})
server.response(peer, query, { echo: query.a })
})

server.bind(0, function () {
var port = server.address().port
var client = rpc()
var peer = {host: '127.0.0.1', port: port}
var peer = { host: '127.0.0.1', port: port }

client.query(peer, {q: 'echo', a: 1}, function (_, res) {
t.same(res.r, {echo: 1})
client.query(peer, { q: 'echo', a: 1 }, function (_, res) {
t.same(res.r, { echo: 1 })
done()
})
client.query(peer, {q: 'echo', a: 2}, function (_, res) {
t.same(res.r, {echo: 2})
client.query(peer, { q: 'echo', a: 2 }, function (_, res) {
t.same(res.r, { echo: 2 })
done()
})

Expand All @@ -73,7 +73,7 @@ tape('query + error', function (t) {
server.bind(0, function () {
var port = server.address().port
var client = rpc()
client.query({host: '127.0.0.1', port: port}, {q: 'hello_world', a: {hej: 10}}, function (err) {
client.query({ host: '127.0.0.1', port: port }, { q: 'hello_world', a: { hej: 10 } }, function (err) {
client.destroy()
server.destroy()
t.ok(err)
Expand All @@ -84,9 +84,9 @@ tape('query + error', function (t) {
})

tape('timeout', function (t) {
var socket = rpc({timeout: 100})
var socket = rpc({ timeout: 100 })

socket.query({host: 'example.com', port: 12345}, {q: 'timeout'}, function (err) {
socket.query({ host: 'example.com', port: 12345 }, { q: 'timeout' }, function (err) {
socket.destroy()
t.ok(err)
t.same(err.message, 'Query timed out')
Expand Down