ataraxia-local
Advanced tools
Comparing version 0.5.2 to 0.6.0
@@ -8,3 +8,2 @@ 'use strict'; | ||
net.addTransport(new LocalTransport()); | ||
net.start(); | ||
@@ -19,1 +18,5 @@ net.on('node:available', node => { | ||
}); | ||
net.start() | ||
.then(() => console.log('Network has started')) | ||
.catch(err => console.error(err)); |
84
index.js
@@ -6,4 +6,4 @@ 'use strict'; | ||
const leader = require('unix-socket-leader'); | ||
const { AbstractTransport, Peer } = require('ataraxia/transport'); | ||
const { LowLevelNetwork } = require('local-machine-network'); | ||
const { AbstractTransport, StreamingPeer, addPeer, events } = require('ataraxia/transport'); | ||
@@ -19,31 +19,19 @@ const eos = require('end-of-stream'); | ||
super('local'); | ||
this.leader = false; | ||
} | ||
start(options) { | ||
super.start(options); | ||
return super.start(options) | ||
.then(started => { | ||
if(! started) return false; | ||
const id = path.join(os.tmpdir(), options.name + ''); | ||
const id = path.join(os.tmpdir(), options.name + ''); | ||
const connect = () => { | ||
this.leader = false; | ||
this.net = leader(id); | ||
this.net.on('leader', () => { | ||
// Emit an event when this node becomes the leader | ||
this.debug('This node is now the leader of the machine local network'); | ||
this.leader = true; | ||
this.events.emit('leader'); | ||
}); | ||
const handlePeer = sock => { | ||
const peer = new LocalPeer(this); | ||
peer.setSocket(sock); | ||
peer.negotiate(); | ||
this.addPeer(peer); | ||
}; | ||
this.net.on('connection', handlePeer); | ||
this.net.on('client', sock => { | ||
if(this.leader) { | ||
eos(sock, err => { | ||
this.net = new LowLevelNetwork({ | ||
path: id | ||
}); | ||
this.net.on('leader', serverSocket => { | ||
// Emit an event when this node becomes the leader | ||
eos(serverSocket, err => { | ||
if(err) { | ||
@@ -53,30 +41,30 @@ this.debug('Closed with error;', err); | ||
}); | ||
} else { | ||
handlePeer(sock); | ||
} | ||
}); | ||
this.net.on('error', err => { | ||
this.debug('Trouble connecting to machine local network;', err); | ||
try { | ||
this.net.close(); | ||
} catch(ex) { | ||
// Do nothing | ||
} | ||
this.debug('This node is now the leader of the machine local network'); | ||
this[events].emit('leader'); | ||
}); | ||
if(this.started) { | ||
const delay = Math.floor(50 * Math.random() + 100); | ||
this.debug('Retrying connection in ' + delay + 'ms'); | ||
setTimeout(connect, delay); | ||
} | ||
}); | ||
}; | ||
const handlePeer = sock => { | ||
const peer = new LocalPeer(this); | ||
peer.setSocket(sock); | ||
peer.negotiate(); | ||
this[addPeer](peer); | ||
}; | ||
connect(); | ||
this.net.on('connection', handlePeer); | ||
this.net.on('connected', handlePeer); | ||
return this.net.connect(); | ||
}) | ||
.then(() => true); | ||
} | ||
stop() { | ||
this.net.close(); | ||
return super.stop() | ||
.then(stopped => { | ||
if(! stopped) return false; | ||
super.stop(); | ||
return this.net.disconnect(); | ||
}) | ||
.then(() => true); | ||
} | ||
@@ -86,3 +74,3 @@ }; | ||
class LocalPeer extends Peer { | ||
class LocalPeer extends StreamingPeer { | ||
disconnect() { | ||
@@ -89,0 +77,0 @@ // Disconnect does nothing for local transport |
{ | ||
"name": "ataraxia-local", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "Machine-local transport for Ataraxia P2P messaging", | ||
@@ -9,6 +9,6 @@ "main": "index.js", | ||
"dependencies": { | ||
"ataraxia": "^0.5.2", | ||
"ataraxia": "^0.6.0", | ||
"debug": "^3.1.0", | ||
"end-of-stream": "^1.4.0", | ||
"unix-socket-leader": "^0.1.2" | ||
"end-of-stream": "^1.4.1", | ||
"local-machine-network": "^0.1.0" | ||
}, | ||
@@ -15,0 +15,0 @@ "devDependencies": { |
# ataraxia-local | ||
[![npm version](https://badge.fury.io/js/ataraxia-local.svg)](https://badge.fury.io/js/ataraxia-local) | ||
[![Dependencies](https://david-dm.org/aholstenson/ataraxia-local.svg)](https://david-dm.org/aholstenson/ataraxia-local) | ||
Machine-local transport for [Ataraxia](https://github.com/aholstenson/ataraxia). | ||
@@ -21,3 +24,6 @@ This transport connects together instances on the same machine via a Unix | ||
net.addTransport(new LocalTransport()); | ||
net.start(); | ||
net.start() | ||
.then(...) | ||
.catch(...); | ||
``` | ||
@@ -45,3 +51,6 @@ | ||
net.addTransport(local); | ||
net.start(); | ||
net.start() | ||
.then(...) | ||
.catch(...); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55
4077
4
75
+ Addedlocal-machine-network@^0.1.0
+ Addedataraxia@0.6.1(transitive)
+ Addedlocal-machine-network@0.1.0(transitive)
- Removedunix-socket-leader@^0.1.2
- Removedataraxia@0.5.2(transitive)
- Removedunix-socket-leader@0.1.2(transitive)
Updatedataraxia@^0.6.0
Updatedend-of-stream@^1.4.1