New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@xyo-network/p2p

Package Overview
Dependencies
Maintainers
5
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xyo-network/p2p - npm Package Compare versions

Comparing version 0.23.0 to 0.24.0

20

dist/@types/index.d.ts

@@ -1,3 +0,3 @@

/// <reference types="node" />
import { Server } from "net";
import { unsubscribeFn } from '@xyo-network/utils';
export interface IXyoP2PService {

@@ -40,2 +40,11 @@ /**

subscribe(topic: string, cb: (senderPublicKey: string, message: Buffer) => void): unsubscribeFn;
/**
* Same as `Subscribe` but stops listening after the first time the subscription is fulfilled
*
* @param {string} topic
* @param {(senderPublicKey: string, message: Buffer) => void} cb
* @returns {unsubscribeFn}
* @memberof IXyoP2PService
*/
subscribeOnce(topic: string, cb: (senderPublicKey: string, message: Buffer) => void): unsubscribeFn;
}

@@ -63,2 +72,3 @@ export interface IXyoPeer {

*/
initialize(address: string): void;
start(): Promise<Server>;

@@ -98,3 +108,8 @@ /**

}
export interface IXyoPeerDiscoveryConfig {
publicKey: string;
address: string;
}
export interface IXyoPeerDiscoveryService {
initialize(config: IXyoPeerDiscoveryConfig): void;
start(): Promise<undefined>;

@@ -121,5 +136,2 @@ stop(): void;

}
/** A function to unsubscribe from a topic */
export declare type unsubscribeFn = () => void;
export declare type Callback = (...args: any[]) => void;
//# sourceMappingURL=index.d.ts.map

2

dist/index.d.ts

@@ -1,2 +0,2 @@

export { IXyoP2PService, IXyoPeer, unsubscribeFn, IXyoPeerDiscoveryService } from './@types';
export { IXyoP2PService, IXyoPeer, IXyoPeerDiscoveryService, IXyoPeerTransport } from './@types';
export { XyoPeerTransport } from './xyo-peer-transport';

@@ -3,0 +3,0 @@ export { XyoPeerDiscoveryService } from './xyo-peer-discovery';

@@ -8,3 +8,3 @@ "use strict";

* @Last modified by: ryanxyo
* @Last modified time: Tuesday, 29th January 2019 10:45:53 am
* @Last modified time: Wednesday, 13th February 2019 1:11:18 pm
* @License: All Rights Reserved

@@ -11,0 +11,0 @@ * @Copyright: Copyright XY | The Findables Company

@@ -1,5 +0,5 @@

/// <reference types="node" />
import { IXyoP2PService, unsubscribeFn, IXyoPeerDiscoveryService } from './@types';
import { IXyoP2PService, IXyoPeerDiscoveryService } from './@types';
import { XyoBase } from '@xyo-network/base';
declare type Callback = (senderPublicKey: string, message: Buffer) => void;
import { unsubscribeFn } from '@xyo-network/utils';
declare type p2pCallback = (senderPublicKey: string, message: Buffer) => void;
export declare class XyoP2PService extends XyoBase implements IXyoP2PService {

@@ -12,5 +12,6 @@ private discoveryService;

publish(topic: string, message: Buffer): Promise<void>;
subscribe(topic: string, cb: Callback): unsubscribeFn;
subscribe(topic: string, cb: p2pCallback): unsubscribeFn;
subscribeOnce(topic: string, cb: (senderPublicKey: string, message: Buffer) => void): unsubscribeFn;
}
export {};
//# sourceMappingURL=xyo-p2p-service.d.ts.map

@@ -23,6 +23,6 @@ "use strict";

publishMessageToPeer(topic, message, publicKey) {
this.logInfo(`Sending message to peer with topic ${topic}`);
const connection = this.discoveryService.getPeerConnection(publicKey);
if (connection) {
connection.write(xyo_topic_buffer_1.encodeXyoTopicBuffer(topic, message));
const bufferedMessage = xyo_topic_buffer_1.encodeXyoTopicBuffer(topic, message);
connection.write(bufferedMessage);
}

@@ -42,4 +42,14 @@ return Promise.resolve();

}
subscribeOnce(topic, cb) {
let unsubscribe = this.subscribe(topic, (senderPublicKey, message) => {
if (unsubscribe) {
unsubscribe();
unsubscribe = undefined;
}
cb(senderPublicKey, message);
});
return unsubscribe;
}
}
exports.XyoP2PService = XyoP2PService;
//# sourceMappingURL=xyo-p2p-service.js.map
/// <reference path="../src/@types/multiaddr.d.ts" />
/// <reference types="node" />
import { IXyoPeerConnection, Callback } from './@types';
import { IXyoPeerConnection } from './@types';
import { Callback } from '@xyo-network/utils';
import { Socket } from 'net';

@@ -5,0 +5,0 @@ export declare class XyoPeerConnection implements IXyoPeerConnection {

@@ -1,3 +0,4 @@

import { IXyoPeerTransport, IXyoPeerConnection, IXyoPeerDiscoveryService, Callback } from "./@types";
import { IXyoPeerTransport, IXyoPeerConnection, IXyoPeerDiscoveryService, IXyoPeerDiscoveryConfig } from "./@types";
import { XyoBase } from "@xyo-network/base";
import { Callback } from "@xyo-network/utils";
declare enum Attrs {

@@ -8,4 +9,2 @@ address = "address",

export declare class XyoPeerDiscoveryService extends XyoBase implements IXyoPeerDiscoveryService {
private publicKey;
private address;
private transport;

@@ -17,3 +16,6 @@ private running;

private starting;
constructor(publicKey: string, address: string, transport: IXyoPeerTransport);
private publicKey;
private address;
constructor(transport: IXyoPeerTransport);
initialize(config: IXyoPeerDiscoveryConfig): void;
getListOfPeersAttributes: (attr: Attrs) => string[];

@@ -20,0 +22,0 @@ getListOfKnownAddresses(): string[];

@@ -21,6 +21,4 @@ "use strict";

class XyoPeerDiscoveryService extends base_1.XyoBase {
constructor(publicKey, address, transport) {
constructor(transport) {
super();
this.publicKey = publicKey;
this.address = address;
this.transport = transport;

@@ -43,2 +41,6 @@ this.running = false;

};
}
initialize(config) {
this.publicKey = config.publicKey;
this.address = config.address;
this.onDiscovery((connection) => {

@@ -57,2 +59,3 @@ this.logInfo(`Discovered Peer`);

});
return;
}

@@ -72,2 +75,5 @@ getListOfKnownAddresses() {

addBootstrapNodes(addresses) {
if (addresses.length === 0) {
return;
}
this.logInfo(`Adding bootstrap nodes ${addresses.map(a => `\n${a}`)}`);

@@ -126,3 +132,3 @@ this.bootstrapAddresses.push(...addresses.filter(this.isValidBootstrapAddress));

if (topic === 'discovery') {
const { publicKey, address, peers } = JSON.parse(message); // TODO sanitize / validate input
const { publicKey, address, peers } = JSON.parse(message.toString()); // TODO sanitize / validate input
this.logInfo(`Discovery topic received for publicKey ${publicKey}`);

@@ -141,3 +147,3 @@ connection.setPublicKey(publicKey);

if (topic === 'discovery') {
const { publicKey, address, peers } = JSON.parse(message);
const { publicKey, address, peers } = JSON.parse(message.toString());
connection.setPublicKey(publicKey);

@@ -144,0 +150,0 @@ connection.setMultiAddress(address);

/// <reference path="../src/@types/multiaddr.d.ts" />
/// <reference types="node" />
import { IXyoPeerTransport, IXyoPeerConnection } from './@types';

@@ -11,3 +10,3 @@ import net from 'net';

private host;
constructor(address: string);
initialize(address: string): void;
start(): Promise<net.Server>;

@@ -14,0 +13,0 @@ stop(): void;

@@ -13,6 +13,8 @@ "use strict";

class XyoPeerTransport {
constructor(address) {
constructor() {
this.listener = new xyo_pub_sub_1.XyoPubSub();
}
initialize(address) {
this.address = address;
this.listener = new xyo_pub_sub_1.XyoPubSub();
const { port, address: host } = multiaddr_1.default(address).nodeAddress();
const { port, address: host } = multiaddr_1.default(this.address).nodeAddress();
this.port = port;

@@ -19,0 +21,0 @@ this.host = host;

@@ -1,2 +0,2 @@

import { Callback } from './@types';
import { Callback } from '@xyo-network/utils';
export declare class XyoPubSub {

@@ -3,0 +3,0 @@ private listeners;

@@ -1,5 +0,4 @@

/// <reference types="node" />
interface IXyoTopic {
topic: string;
message: string;
message: Buffer;
offset: number;

@@ -6,0 +5,0 @@ }

@@ -21,3 +21,3 @@ "use strict";

const topic = buffer.slice(SIZE_OF_SIZE + offset, offset + topicLength).toString();
const message = buffer.slice(SIZE_OF_SIZE + offset + topicLength, offset + topicLength + messageLength).toString();
const message = buffer.slice(SIZE_OF_SIZE + offset + topicLength, offset + topicLength + messageLength);
return { topic, message, offset: offset + topicLength + messageLength };

@@ -24,0 +24,0 @@ };

{
"name": "@xyo-network/p2p",
"version": "0.23.0",
"version": "0.24.0",
"description": "A peer to peer network module",

@@ -10,3 +10,4 @@ "main": "dist/index.js",

"dependencies": {
"@xyo-network/base": "^0.23.0",
"@xyo-network/base": "^0.24.0",
"@xyo-network/utils": "^0.24.0",
"multiaddr": "^6.0.3"

@@ -23,3 +24,3 @@ },

},
"gitHead": "451c89a026f13eed645b676f7073b3cffce955c5"
"gitHead": "629618ae9a38cc374b662561d1786827b618c2fd"
}
import { Server } from "net"
import { unsubscribeFn } from '@xyo-network/utils'

@@ -9,3 +10,3 @@ /*

* @Last modified by: ryanxyo
* @Last modified time: Tuesday, 29th January 2019 2:55:29 pm
* @Last modified time: Wednesday, 13th February 2019 1:07:32 pm
* @License: All Rights Reserved

@@ -56,2 +57,12 @@ * @Copyright: Copyright XY | The Findables Company

subscribe(topic: string, cb: (senderPublicKey: string, message: Buffer) => void): unsubscribeFn
/**
* Same as `Subscribe` but stops listening after the first time the subscription is fulfilled
*
* @param {string} topic
* @param {(senderPublicKey: string, message: Buffer) => void} cb
* @returns {unsubscribeFn}
* @memberof IXyoP2PService
*/
subscribeOnce(topic: string, cb: (senderPublicKey: string, message: Buffer) => void): unsubscribeFn
}

@@ -83,2 +94,4 @@

*/
initialize(address: string): void
start(): Promise<Server>

@@ -132,3 +145,11 @@

export interface IXyoPeerDiscoveryConfig {
publicKey: string
address: string
}
export interface IXyoPeerDiscoveryService {
initialize(config: IXyoPeerDiscoveryConfig): void
start(): Promise<undefined>

@@ -172,6 +193,1 @@

}
/** A function to unsubscribe from a topic */
export type unsubscribeFn = () => void
export type Callback = (...args: any[]) => void

@@ -7,3 +7,3 @@ /*

* @Last modified by: ryanxyo
* @Last modified time: Tuesday, 29th January 2019 10:45:53 am
* @Last modified time: Wednesday, 13th February 2019 1:11:18 pm
* @License: All Rights Reserved

@@ -13,5 +13,5 @@ * @Copyright: Copyright XY | The Findables Company

export { IXyoP2PService, IXyoPeer, unsubscribeFn, IXyoPeerDiscoveryService } from './@types'
export { IXyoP2PService, IXyoPeer, IXyoPeerDiscoveryService, IXyoPeerTransport } from './@types'
export { XyoPeerTransport } from './xyo-peer-transport'
export { XyoPeerDiscoveryService } from './xyo-peer-discovery'
export { XyoP2PService } from './xyo-p2p-service'

@@ -19,4 +19,6 @@

const createP2PService = (peer: IPeer) => {
const transport = new XyoPeerTransport(peer.address)
const discovery = new XyoPeerDiscoveryService(peer.publicKey, peer.address, transport)
const transport = new XyoPeerTransport()
transport.initialize(peer.address)
const discovery = new XyoPeerDiscoveryService(transport)
discovery.initialize({ publicKey: peer.publicKey, address: peer.address })
const node = new XyoP2PService(discovery)

@@ -89,3 +91,3 @@ return { transport, discovery, node }

topic: 'topic1',
message: 'foo',
message: Buffer.from('foo'),
offset: 17

@@ -130,3 +132,5 @@ })

it(`Should create a transport`, () => {
expect(new XyoPeerTransport(peer1.address)).toBeInstanceOf(XyoPeerTransport)
const transport = new XyoPeerTransport()
transport.initialize(peer1.address)
expect(transport).toBeInstanceOf(XyoPeerTransport)
})

@@ -140,3 +144,4 @@

it(`Should communicate`, async (done) => {
const transport = new XyoPeerTransport(peer1.address)
const transport = new XyoPeerTransport()
transport.initialize(peer1.address)

@@ -188,8 +193,13 @@ await transport.start()

it(`Should discover peers`, async (done) => {
const transport1 = new XyoPeerTransport(peer1.address)
const discovery1 = new XyoPeerDiscoveryService(peer1.publicKey, peer1.address, transport1)
const transport1 = new XyoPeerTransport()
transport1.initialize(peer1.address)
const discovery1 = new XyoPeerDiscoveryService(transport1)
discovery1.initialize({ publicKey: peer1.publicKey, address: peer1.address })
const transport2 = new XyoPeerTransport(peer2.address)
const discovery2 = new XyoPeerDiscoveryService(peer2.publicKey, peer2.address, transport2)
const transport2 = new XyoPeerTransport()
transport2.initialize(peer2.address)
const discovery2 = new XyoPeerDiscoveryService(transport2)
discovery2.initialize({ publicKey: peer2.publicKey, address: peer2.address })
const discovered = jest.fn()

@@ -216,12 +226,20 @@ discovery2.addBootstrapNodes([peer1.address])

it(`Should create a new P2P Service`, async (done) => {
const transport1 = new XyoPeerTransport(peer1.address)
const discovery1 = new XyoPeerDiscoveryService(peer1.publicKey, peer1.address, transport1)
const transport1 = new XyoPeerTransport()
transport1.initialize(peer1.address)
const discovery1 = new XyoPeerDiscoveryService(transport1)
discovery1.initialize({ publicKey: peer1.publicKey, address: peer1.address, })
const node1 = new XyoP2PService(discovery1)
const transport2 = new XyoPeerTransport(peer2.address)
const discovery2 = new XyoPeerDiscoveryService(peer2.publicKey, peer2.address, transport2)
const transport2 = new XyoPeerTransport()
transport2.initialize(peer2.address)
const discovery2 = new XyoPeerDiscoveryService(transport2)
discovery2.initialize({ publicKey: peer2.publicKey, address: peer2.address, })
const node2 = new XyoP2PService(discovery2)
const transport3 = new XyoPeerTransport(peer3.address)
const discovery3 = new XyoPeerDiscoveryService(peer3.publicKey, peer3.address, transport3)
const transport3 = new XyoPeerTransport()
transport3.initialize(peer3.address)
const discovery3 = new XyoPeerDiscoveryService(transport3)
discovery3.initialize({ publicKey: peer3.publicKey, address: peer3.address, })
const node3 = new XyoP2PService(discovery3)

@@ -257,12 +275,22 @@

it('Should recursively discover peers of peers', async (done) => {
const transport1 = new XyoPeerTransport(peer1.address)
const discovery1 = new XyoPeerDiscoveryService(peer1.publicKey, peer1.address, transport1)
const transport1 = new XyoPeerTransport()
transport1.initialize(peer1.address)
const discovery1 = new XyoPeerDiscoveryService(transport1)
discovery1.initialize({ publicKey: peer1.publicKey, address: peer1.address, })
const node1 = new XyoP2PService(discovery1)
const transport2 = new XyoPeerTransport(peer2.address)
const discovery2 = new XyoPeerDiscoveryService(peer2.publicKey, peer2.address, transport2)
const transport2 = new XyoPeerTransport()
transport2.initialize(peer2.address)
const discovery2 = new XyoPeerDiscoveryService(transport2)
discovery2.initialize({ publicKey: peer2.publicKey, address: peer2.address })
const node2 = new XyoP2PService(discovery2)
const transport3 = new XyoPeerTransport(peer3.address)
const discovery3 = new XyoPeerDiscoveryService(peer3.publicKey, peer3.address, transport3)
const transport3 = new XyoPeerTransport()
transport3.initialize(peer3.address)
const discovery3 = new XyoPeerDiscoveryService(transport3)
discovery3.initialize({ publicKey: peer3.publicKey, address: peer3.address, })
const node3 = new XyoP2PService(discovery3)

@@ -269,0 +297,0 @@

@@ -1,7 +0,8 @@

import { IXyoP2PService, unsubscribeFn, IXyoPeerDiscoveryService } from './@types'
import { IXyoP2PService, IXyoPeerDiscoveryService } from './@types'
import { XyoBase } from '@xyo-network/base'
import { XyoPubSub } from './xyo-pub-sub'
import { decodeXyoTopicBuffer, encodeXyoTopicBuffer } from './xyo-topic-buffer'
import { unsubscribeFn } from '@xyo-network/utils'
type Callback = (senderPublicKey: string, message: Buffer) => void
type p2pCallback = (senderPublicKey: string, message: Buffer) => void

@@ -30,6 +31,6 @@ export class XyoP2PService extends XyoBase implements IXyoP2PService {

public publishMessageToPeer(topic: string, message: Buffer, publicKey: string) {
this.logInfo(`Sending message to peer with topic ${topic}`)
const connection = this.discoveryService.getPeerConnection(publicKey)
if (connection) {
connection.write(encodeXyoTopicBuffer(topic, message))
const bufferedMessage = encodeXyoTopicBuffer(topic, message)
connection.write(bufferedMessage)
}

@@ -47,3 +48,3 @@ return Promise.resolve()

public subscribe(topic: string, cb: Callback): unsubscribeFn {
public subscribe(topic: string, cb: p2pCallback): unsubscribeFn {
return this.listener.subscribe(topic, (senderPublicKey, message) => {

@@ -54,2 +55,15 @@ this.logInfo(`Message subscription with ${topic} received`)

}
public subscribeOnce(topic: string, cb: (senderPublicKey: string, message: Buffer) => void): unsubscribeFn {
let unsubscribe: unsubscribeFn | undefined = this.subscribe(topic, (senderPublicKey, message) => {
if (unsubscribe) {
unsubscribe()
unsubscribe = undefined
}
cb(senderPublicKey, message)
})
return unsubscribe
}
}
// tslint:disable-next-line:no-reference
/// <reference path="./@types/multiaddr.d.ts" />
import multiaddr from 'multiaddr'
import { IXyoPeerConnection, Callback } from './@types'
import { IXyoPeerConnection } from './@types'
import { Callback } from '@xyo-network/utils'
import { XyoPubSub } from './xyo-pub-sub'

@@ -6,0 +7,0 @@ import { encodeXyoBuffer, accumulateChunks } from './xyo-topic-buffer'

@@ -1,7 +0,7 @@

import { IXyoP2PService, IXyoPeer, IXyoPeerTransport, unsubscribeFn, IXyoPeerConnection, IXyoPeerDiscoveryService, Callback } from "./@types"
import { IXyoPeerTransport, IXyoPeerConnection, IXyoPeerDiscoveryService, IXyoPeerDiscoveryConfig } from "./@types"
import { XyoPubSub } from './xyo-pub-sub'
import { XyoPeerTransport } from './xyo-peer-transport'
import { encodeXyoTopicBuffer, decodeXyoTopicBuffer } from "./xyo-topic-buffer"
import { XyoPeerConnectionPool } from "./xyo-peer-connection-pool"
import { XyoBase } from "@xyo-network/base"
import { Callback } from "@xyo-network/utils"

@@ -19,9 +19,13 @@ enum Attrs {

private starting: Promise<undefined> = Promise.resolve(undefined)
private publicKey!: string
private address!: string
constructor(
private publicKey: string,
private address: string,
private transport: IXyoPeerTransport,
) {
constructor(private transport: IXyoPeerTransport) {
super()
}
public initialize(config: IXyoPeerDiscoveryConfig) {
this.publicKey = config.publicKey
this.address = config.address
this.onDiscovery((connection) => {

@@ -40,2 +44,4 @@ this.logInfo(`Discovered Peer`)

})
return
}

@@ -66,2 +72,5 @@

public addBootstrapNodes(addresses: string[]) {
if (addresses.length === 0) {
return
}
this.logInfo(`Adding bootstrap nodes ${addresses.map(a => `\n${a}`)}`)

@@ -129,3 +138,3 @@ this.bootstrapAddresses.push(...addresses.filter(this.isValidBootstrapAddress))

if (topic === 'discovery') {
const { publicKey, address, peers } = JSON.parse(message) // TODO sanitize / validate input
const { publicKey, address, peers } = JSON.parse(message.toString()) // TODO sanitize / validate input
this.logInfo(`Discovery topic received for publicKey ${publicKey}`)

@@ -145,3 +154,3 @@ connection.setPublicKey(publicKey)

if (topic === 'discovery') {
const { publicKey, address, peers } = JSON.parse(message)
const { publicKey, address, peers } = JSON.parse(message.toString())
connection.setPublicKey(publicKey)

@@ -148,0 +157,0 @@ connection.setMultiAddress(address)

@@ -10,9 +10,11 @@ // tslint:disable-next-line:no-reference

export class XyoPeerTransport implements IXyoPeerTransport {
public address!: string
private listener: XyoPubSub = new XyoPubSub()
private server: undefined | Server
private port: undefined | number
private host: string
private host!: string
constructor(public address: string) {
const { port, address: host } = multiaddr(address).nodeAddress()
public initialize(address: string) {
this.address = address
const { port, address: host } = multiaddr(this.address).nodeAddress()
this.port = port as number

@@ -19,0 +21,0 @@ this.host = host as string

@@ -1,2 +0,2 @@

import { Callback } from './@types'
import { Callback } from '@xyo-network/utils'

@@ -3,0 +3,0 @@ export class XyoPubSub {

interface IXyoTopic {
topic: string
message: string
message: Buffer
offset: number

@@ -28,3 +28,3 @@ }

const topic = buffer.slice(SIZE_OF_SIZE + offset, offset + topicLength).toString()
const message = buffer.slice(SIZE_OF_SIZE + offset + topicLength, offset + topicLength + messageLength).toString()
const message = buffer.slice(SIZE_OF_SIZE + offset + topicLength, offset + topicLength + messageLength)
return { topic, message, offset: offset + topicLength + messageLength }

@@ -31,0 +31,0 @@ }

@@ -24,5 +24,6 @@ {

"include": ["src/**/*"],
"references": [{
"path": "../base"
}]
"references": [
{"path": "../base"},
{"path": "../utils"}
]
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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