Comparing version 0.5.0 to 0.5.1
var UDPServer = require('./udp.js') | ||
var nacl = require('tweetnacl') | ||
var NB_BLOCKS = 10 | ||
var NB_BLOCKS = 100 | ||
var BLOCK_LENGTH = 1024 | ||
@@ -11,2 +11,3 @@ | ||
var source = Buffer(NB_BLOCKS * BLOCK_LENGTH) | ||
var sourceServer = Buffer(NB_BLOCKS * BLOCK_LENGTH) | ||
@@ -18,6 +19,14 @@ for (var i = 0; i < NB_BLOCKS; i++) { | ||
for (i = 0; i < NB_BLOCKS; i++) { | ||
buffer = new Buffer(nacl.randomBytes(BLOCK_LENGTH)) | ||
buffer.copy(sourceServer, i * BLOCK_LENGTH) | ||
} | ||
var currentBlock = 0 | ||
var currentBlockServer = 0 | ||
var messageStream | ||
var messageStreamServer | ||
var destination = Buffer(0) | ||
var destinationClient = Buffer(0) | ||
@@ -27,2 +36,9 @@ server.on('connect', function (rinfo, messageStream) { | ||
console.log(rinfo) | ||
/* messageStream.on('connect', function () { | ||
writeServer() | ||
}) | ||
messageStream.on('drain', function () { | ||
writeServer() | ||
}) | ||
*/ | ||
messageStream.on('data', function (data) { | ||
@@ -34,2 +50,4 @@ console.log('DATA RECEIVED ON SERVER') | ||
console.log('END RECEIVED ON SERVER') | ||
console.log(nacl.util.encodeBase64(source)) | ||
console.log(nacl.util.encodeBase64(destination)) | ||
if (!Buffer.compare(source, destination)) { | ||
@@ -44,2 +62,4 @@ console.log('IDENTICAL BUFFER MATCH FOUND') | ||
}) | ||
messageStreamServer = messageStream | ||
writeServer() | ||
}) | ||
@@ -68,2 +88,23 @@ | ||
var writeServer = function () { | ||
var canContinue = currentBlockServer < NB_BLOCKS | ||
while (canContinue) { | ||
// console.log(currentBlock) | ||
var buffer = new Buffer(BLOCK_LENGTH) | ||
sourceServer.copy(buffer, 0, currentBlockServer * BLOCK_LENGTH, (currentBlockServer + 1) * BLOCK_LENGTH) | ||
var result = messageStreamServer.write(buffer, 'buffer', function (err) { | ||
if (err) { | ||
console.log('ERROR SENDING BLOCK: ' + err) | ||
} else { | ||
// console.log('SUCCESS SENDING BLOCK') | ||
} | ||
}) | ||
currentBlockServer += 1 | ||
canContinue = result && currentBlockServer < NB_BLOCKS | ||
if (currentBlockServer === NB_BLOCKS) { | ||
messageStreamServer.end() | ||
} | ||
} | ||
} | ||
setTimeout(function () { | ||
@@ -73,14 +114,28 @@ messageStream = client.connect({address: '127.0.0.1', port: server.getPort()}, server.keypair.publicKey) | ||
console.log('drain event') | ||
write() | ||
// write() | ||
}) | ||
messageStream.on('connect', function () { | ||
console.log('connect event') | ||
write() | ||
messageStream.write(new Buffer('test')) | ||
// write() | ||
}) | ||
messageStream.on('finish', function () { | ||
console.log('FINISH RECEIVED ON CLIENT') | ||
}) | ||
messageStream.on('data', function (data) { | ||
console.log('DATA RECEIVED ON CLIENT') | ||
destinationClient = Buffer.concat([destinationClient, data]) | ||
}) | ||
messageStream.on('end', function () { | ||
console.log('END RECEIVED ON CLIENT') | ||
console.log(sourceServer.length) | ||
console.log(destinationClient.length) | ||
console.log(nacl.util.encodeBase64(sourceServer)) | ||
console.log(nacl.util.encodeBase64(destinationClient)) | ||
if (!Buffer.compare(sourceServer, destinationClient)) { | ||
console.log('IDENTICAL BUFFER MATCH FOUND ON CLIENT') | ||
} else { | ||
console.log('BUFFERS DO NOT MATCH ON CLIENT') | ||
} | ||
}) | ||
messageStream.on('finish', function () { | ||
console.log('FINISH RECEIVED ON CLIENT') | ||
}) | ||
}, 1000) |
{ | ||
"name": "curvecp", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "Pure javascript CurveCP library", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -0,0 +0,0 @@ module.exports = { |
@@ -12,3 +12,3 @@ var Chicago = require('./chicago.js') | ||
var MessageStream = function (curveCPStream) { | ||
var MessageStream = function (options) { | ||
debug('initialize') | ||
@@ -22,3 +22,3 @@ var opts = {} | ||
this._maxBlockLength = 640 - constants.HEADER_SIZE - constants.MINIMAL_PADDING | ||
this._stream = curveCPStream | ||
this._stream = options.stream | ||
this.__streamReady = false | ||
@@ -32,3 +32,2 @@ var self = this | ||
self.push(null) | ||
self.emit('finish') | ||
_.forEach(self._writeRequests, function (request) { | ||
@@ -42,5 +41,2 @@ this.callback(new Error('Underlying stream closed')) | ||
}) | ||
this._stream.on('finish', function () { | ||
self.emit('finish') | ||
}) | ||
this._stream.on('connect', function () { | ||
@@ -50,2 +46,8 @@ self.__streamReady = true | ||
}) | ||
this._stream.on('lookup', function (err, address, family) { | ||
self.emit('lookup', err, address, family) | ||
}) | ||
this._stream.on('timeout', function () { | ||
self.emit('timeout') | ||
}) | ||
if (this._stream.is_server) { | ||
@@ -97,6 +99,10 @@ this._maxBlockLength = constants.MESSAGE_BODY | ||
MessageStream.prototype.connect = function () { | ||
this._stream.connect() | ||
MessageStream.prototype.connect = function (publicKey, connectionInfo) { | ||
this._stream.connect(publicKey, connectionInfo) | ||
} | ||
MessageStream.prototype.isConnected = function () { | ||
return this._stream.isConnected() | ||
} | ||
MessageStream.prototype.destroy = function () { | ||
@@ -261,2 +267,3 @@ this._stream.destroy() | ||
this._receivedBytes += data.length | ||
// debug(data.toString()) | ||
this.push(data) | ||
@@ -282,2 +289,8 @@ if (message.success || message.failure) { | ||
Object.defineProperty(MessageStream.prototype, 'remoteAddress', { | ||
get: function () { | ||
return this._stream.remoteAddress | ||
} | ||
}) | ||
module.exports = MessageStream |
@@ -9,2 +9,3 @@ 'use strict' | ||
var crypto = require('crypto') | ||
nacl.util = require('tweetnacl-util') | ||
@@ -92,7 +93,3 @@ var HELLO_MSG = nacl.util.decodeUTF8('QvnQ5XlH') | ||
close: function () { | ||
stream.removeListener('data', functions.data) | ||
stream.removeListener('error', functions.error) | ||
stream.removeListener('close', functions.close) | ||
stream.removeListener('end', functions.end) | ||
stream.removeListener('finish', functions.finish) | ||
stream.removeAllListeners() | ||
curveStream.emit('close') | ||
@@ -103,4 +100,10 @@ }, | ||
}, | ||
finish: function () { | ||
curveStream.emit('finish') | ||
drain: function () { | ||
curveStream.emit('drain') | ||
}, | ||
lookup: function (err, address, family) { | ||
curveStream.emit('lookup', err, address, family) | ||
}, | ||
timeout: function () { | ||
curveStream.emit('timeout') | ||
} | ||
@@ -111,4 +114,6 @@ } | ||
stream.on('close', functions.close) | ||
stream.on('finish', functions.finish) | ||
stream.on('end', functions.end) | ||
stream.on('drain', functions.drain) | ||
stream.on('lookup', functions.lookup) | ||
stream.on('timeout', functions.timeout) | ||
} | ||
@@ -170,9 +175,24 @@ | ||
PacketStream.prototype.connect = function () { | ||
PacketStream.prototype.connect = function (publicKey, connectionInfo) { | ||
debug('connect') | ||
var self = this | ||
if (!this.isServer) { | ||
this._sendHello() | ||
this.serverPublicKey = nacl.util.decodeBase64(publicKey) | ||
} | ||
if (this._stream.isConnected()) { | ||
if (!this.isServer) { | ||
this._sendHello() | ||
} | ||
} else { | ||
this._stream.once('connect', function () { | ||
self.connect(publicKey, connectionInfo) | ||
}) | ||
this._stream.connect(connectionInfo) | ||
} | ||
} | ||
PacketStream.prototype.isConnected = function () { | ||
return this.__canSend | ||
} | ||
PacketStream.prototype.destroy = function () { | ||
@@ -237,3 +257,3 @@ this.stream.destroy() | ||
} catch (err) { | ||
this.connectionFail('Decrypt failed with error ' + err) | ||
this.emit('error', new Error('Decrypt failed with error ' + err)) | ||
} | ||
@@ -504,2 +524,12 @@ return result | ||
Object.defineProperty(PacketStream.prototype, 'remoteAddress', { | ||
get: function () { | ||
if (this.isServer) { | ||
return nacl.util.encodeBase64(this.clientPublicKey) | ||
} else { | ||
return nacl.util.encodeBase64(this.serverPublicKey) | ||
} | ||
} | ||
}) | ||
module.exports = PacketStream |
57161
1539