Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

planktos

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

planktos - npm Package Compare versions

Comparing version 0.0.7 to 0.0.8

bin/peer.js

13

build/lib/channel/index.js

@@ -7,7 +7,9 @@ "use strict";

* socketioUrl - the url of the socketio server to get peers from
* useWebrtc - the webrtc module to use. If undefined the browser one
* will be used or no webrtc will be used
* wrtc - the webrtc module to use. If undefined the browser one
* will be used or no webrtc will be used. If null, channels
* will be relayed through the signaling server instead of over
* webrtc.
* cb - callback called when the channel manager is ready
*/
function startManager(socketioUrl, useWebRtc, cb) {
function startManager(socketioUrl, wrtc, cb) {
var server = new socketio_1.SocketioChannel(socketioUrl);

@@ -18,4 +20,5 @@ server.once('connect', function () {

var manager = new relay_1.RelayChannelManager(localId, server);
if (useWebRtc)
manager = new webrtc_1.WrtcChannelManager(manager);
// It's okay for it to be undefined, the browser one will be used in that case
if (wrtc !== null)
manager = new webrtc_1.WrtcChannelManager(manager, wrtc);
var onServerMessage = function (msg) {

@@ -22,0 +25,0 @@ if (msg.type === 'newPeers') {

@@ -19,5 +19,5 @@ "use strict";

*/
function WrtcChannel(signaler, remoteId, offer) {
function WrtcChannel(signaler, remoteId, wrtc, offer) {
_super.call(this);
if (!SimplePeer.WEBRTC_SUPPORT)
if (!SimplePeer.WEBRTC_SUPPORT && wrtc === undefined)
throw new Error("WebRTC is not supported!");

@@ -27,3 +27,5 @@ this.remoteId = remoteId;

initiator: typeof (offer) === 'undefined',
trickle: false
trickle: false,
wrtc: wrtc,
offerConstraints: {}
});

@@ -93,3 +95,3 @@ if (typeof (offer) !== 'undefined')

__extends(WrtcChannelManager, _super);
function WrtcChannelManager(relay) {
function WrtcChannelManager(relay, wrtcModule) {
var _this = this;

@@ -100,2 +102,3 @@ _super.call(this);

this.relay = relay;
this.wrtcModule = wrtcModule;
relay.on('channel-connect', function (c) {

@@ -134,15 +137,15 @@ _this.handleNewRelayChannel(c);

var remoteId = relay.getRemoteId();
var wrtc = new WrtcChannel(relay, remoteId, offer);
wrtc.once('connect', function () {
_this.emit('channel-connect', wrtc);
var c = new WrtcChannel(relay, remoteId, this.wrtcModule, offer);
c.once('connect', function () {
_this.emit('channel-connect', c);
relay.disconnect();
relay = null;
});
// TODO Maybe a memory leak for `wrtc` and/or `relay`?
wrtc.once('disconnect', function () {
// TODO Maybe a memory leak for `c` and/or `relay`?
c.once('disconnect', function () {
delete _this.channels[remoteId];
_this.emit('channel-disconnect', wrtc);
_this.emit('channel-disconnect', c);
});
this.channels[remoteId] = wrtc;
return wrtc;
this.channels[remoteId] = c;
return c;
};

@@ -149,0 +152,0 @@ return WrtcChannelManager;

@@ -56,7 +56,6 @@ "use strict";

* socketiourl - the url of the socketio server to find peers from
* useWebRtc - indicates if webrtc should be used
* wrtc - The webrtc module to use. See npm for 'wrtc' and 'electron-wrtc'
*/
function Client(socketioUrl, useWebRtc) {
function Client(socketioUrl, wrtc) {
var _this = this;
if (useWebRtc === void 0) { useWebRtc = true; }
_super.call(this);

@@ -66,3 +65,3 @@ this.blockMap = {}; // Index by block id

this.peers = {};
channel_1.startManager(socketioUrl, useWebRtc, function (cm) {
channel_1.startManager(socketioUrl, wrtc, function (cm) {
_this.channelManager = cm;

@@ -146,6 +145,8 @@ _this.channelManager.on('channel-connect', function (c) { return _this.handleNewConnections(c); });

exports.Client = Client;
/* Helper function which simplifies the bootstrapping process */
function bootstrap(socketioUrl, useWebRtc) {
if (useWebRtc === void 0) { useWebRtc = true; }
return new Client(socketioUrl, useWebRtc);
/* Bootstraps the peer onto the planktos network
* socketioUrl - the url of the socketio server to find peers from
* wrtc - The webrtc module to use. See npm for 'wrtc' and 'electron-wrtc'
*/
function bootstrap(socketioUrl, wrtc) {
return new Client(socketioUrl, wrtc);
}

@@ -152,0 +153,0 @@ exports.bootstrap = bootstrap;

"use strict";
var assert_1 = require('assert');
var client_1 = require('../lib/client');
var utils_1 = require('./utils');
describe('peer tests', function () {
var peerCount = 2;
it('all peers connect to everyone else', utils_1.setupNet(function (testnet, done) {
for (var _i = 0, _a = testnet.peers; _i < _a.length; _i++) {
var p = _a[_i];
assert_1.equal(peerCount - 1, Object.keys(p.peers).length);
}
done();
}, peerCount));
it('new peer automatically connects to net', utils_1.setupNet(function (testnet, done) {
var count = 0;
var onPeerConnect = function () {
count++;
assert_1.ok(count <= 2 * peerCount);
if (count == 2 * peerCount)
done();
};
for (var _i = 0, _a = testnet.peers; _i < _a.length; _i++) {
var p = _a[_i];
p.on('connected-peer', onPeerConnect);
}
var newPeer = utils_1.startPeer(testnet);
newPeer.on('connected-peer', onPeerConnect);
}, peerCount));
});
describe('utility tests', function () {

@@ -31,0 +5,0 @@ it('normalize path test', function () {

@@ -40,5 +40,5 @@ "use strict";

var url = "http://localhost:" + testnet.server.address().port;
// don't use webrtc. set to false for testing because webrtc
// don't use webrtc. set to null for testing because webrtc
// support in nodejs is... tricky...
var c = client_1.bootstrap(url, false);
var c = client_1.bootstrap(url, null);
testnet.peers.push(c);

@@ -45,0 +45,0 @@ return c;

@@ -49,7 +49,9 @@ import { EventEmitter } from 'events';

* socketioUrl - the url of the socketio server to get peers from
* useWebrtc - the webrtc module to use. If undefined the browser one
* will be used or no webrtc will be used
* wrtc - the webrtc module to use. If undefined the browser one
* will be used or no webrtc will be used. If null, channels
* will be relayed through the signaling server instead of over
* webrtc.
* cb - callback called when the channel manager is ready
*/
export function startManager(socketioUrl?: string, useWebRtc?: boolean, cb?: (cm: ChannelManager)=>void) {
export function startManager(socketioUrl?: string, wrtc?: Object, cb?: (cm: ChannelManager)=>void) {

@@ -63,6 +65,8 @@ const server = new SocketioChannel(socketioUrl);

let manager: ChannelManager = new RelayChannelManager(localId, server);
if (useWebRtc)
manager = new WrtcChannelManager(manager);
// It's okay for it to be undefined, the browser one will be used in that case
if (wrtc !== null)
manager = new WrtcChannelManager(manager, wrtc);
const onServerMessage = (msg: Message) => {

@@ -69,0 +73,0 @@ if (msg.type === 'newPeers') {

@@ -17,5 +17,5 @@ const SimplePeer = require('simple-peer');

*/
constructor(signaler: Channel, remoteId: string, offer?: any) {
constructor(signaler: Channel, remoteId: string, wrtc?: Object, offer?: any) {
super();
if (!SimplePeer.WEBRTC_SUPPORT)
if (!SimplePeer.WEBRTC_SUPPORT && wrtc === undefined)
throw new Error("WebRTC is not supported!");

@@ -25,3 +25,5 @@ this.remoteId = remoteId;

initiator: typeof(offer) === 'undefined',
trickle: false
trickle: false,
wrtc: wrtc,
offerConstraints: {}
});

@@ -101,2 +103,3 @@

private relay: ChannelManager;
private wrtcModule: Object;

@@ -106,6 +109,7 @@ // The remote IDs of connected/initiated channels

constructor(relay: ChannelManager) {
constructor(relay: ChannelManager, wrtcModule?: Object) {
super();
this.relay = relay;
this.wrtcModule = wrtcModule;

@@ -148,6 +152,6 @@ relay.on('channel-connect', (c: Channel) => {

const remoteId = relay.getRemoteId();
const wrtc = new WrtcChannel(relay, remoteId, offer);
const c = new WrtcChannel(relay, remoteId, this.wrtcModule, offer);
wrtc.once('connect', () => {
this.emit('channel-connect', wrtc);
c.once('connect', () => {
this.emit('channel-connect', c);
relay.disconnect();

@@ -157,13 +161,13 @@ relay = null;

// TODO Maybe a memory leak for `wrtc` and/or `relay`?
// TODO Maybe a memory leak for `c` and/or `relay`?
wrtc.once('disconnect', () => {
c.once('disconnect', () => {
delete this.channels[remoteId];
this.emit('channel-disconnect', wrtc);
this.emit('channel-disconnect', c);
});
this.channels[remoteId] = wrtc;
this.channels[remoteId] = c;
return wrtc;
return c;
}
}

@@ -58,8 +58,8 @@ import { ChannelManager, Channel, Message, startManager } from './channel';

* socketiourl - the url of the socketio server to find peers from
* useWebRtc - indicates if webrtc should be used
* wrtc - The webrtc module to use. See npm for 'wrtc' and 'electron-wrtc'
*/
constructor(socketioUrl?: string, useWebRtc = true) {
constructor(socketioUrl?: string, wrtc?: Object ) {
super();
startManager(socketioUrl, useWebRtc, cm => {
startManager(socketioUrl, wrtc, cm => {
this.channelManager = cm;

@@ -150,5 +150,8 @@ this.channelManager.on('channel-connect', (c: Channel) => this.handleNewConnections(c));

/* Helper function which simplifies the bootstrapping process */
export function bootstrap(socketioUrl?: string, useWebRtc = true) {
return new Client(socketioUrl, useWebRtc);
/* Bootstraps the peer onto the planktos network
* socketioUrl - the url of the socketio server to find peers from
* wrtc - The webrtc module to use. See npm for 'wrtc' and 'electron-wrtc'
*/
export function bootstrap(socketioUrl?: string, wrtc?: Object) {
return new Client(socketioUrl, wrtc);
}

@@ -155,0 +158,0 @@

{
"name": "planktos",
"description": "A library for making p2p web apps",
"version": "0.0.7",
"version": "0.0.8",
"homepage": "http://www.planktos.xyz",
"main": "./build/lib/client.js",
"bin": "./bin/server.js",
"bin": {
"planktos": "./bin/server.js",
"planktos-peer": "./bin/peer.js"
},
"license": "MIT",

@@ -28,3 +31,3 @@ "engines": {

"test": "mocha build/test --use-strict",
"build": "rm -r build; tsc && browserify -r ./build/lib/client.js:planktos -o ./build/bundle.js",
"build": "rm -r build; tsc && browserify -s planktos -r ./build/lib/client.js:planktos -o ./build/bundle.js",
"prepublish": "typings install && npm run build && npm test",

@@ -31,0 +34,0 @@ "browserify": "browserify",

@@ -1,33 +0,4 @@

import { ok, equal } from 'assert';
import { Peer, Client, normalizePath } from '../lib/client';
import { Server } from 'http';
import { startNet, stopNet, startPeer, setupNet } from './utils';
import { equal } from 'assert';
import { normalizePath } from '../lib/client';
describe('peer tests', function() {
const peerCount = 2;
it('all peers connect to everyone else', setupNet((testnet, done) => {
for (let p of testnet.peers) {
equal(peerCount - 1, Object.keys(p.peers).length);
}
done();
}, peerCount));
it('new peer automatically connects to net', setupNet((testnet, done) => {
let count = 0;
const onPeerConnect = () => {
count++;
ok(count <= 2 * peerCount);
if (count == 2 * peerCount)
done();
};
for (let p of testnet.peers) {
p.on('connected-peer', onPeerConnect);
}
const newPeer = startPeer(testnet);
newPeer.on('connected-peer', onPeerConnect);
}, peerCount));
});
describe('utility tests', function() {

@@ -34,0 +5,0 @@ it('normalize path test', function() {

@@ -50,5 +50,5 @@ import { Client, bootstrap } from '../lib/client';

const url = "http://localhost:" + testnet.server.address().port;
// don't use webrtc. set to false for testing because webrtc
// don't use webrtc. set to null for testing because webrtc
// support in nodejs is... tricky...
const c = bootstrap(url, false);
const c = bootstrap(url, null);
testnet.peers.push(c);

@@ -55,0 +55,0 @@ return c;

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc