iota-gateway
Advanced tools
Comparing version 0.1.1 to 0.1.3
@@ -27,3 +27,3 @@ "use strict"; | ||
transaction: this._factory.createTransactionFromBytes(transactionBytes), | ||
requestHash: this._factory.createHashFromBytes(hashBytes), | ||
requestHash: this._factory.createHashFromBytes(hashBytes) | ||
}; | ||
@@ -30,0 +30,0 @@ } |
{ | ||
"name": "iota-gateway", | ||
"version": "0.1.1", | ||
"version": "0.1.3", | ||
"description": "Network gateway for IOTA", | ||
@@ -10,2 +10,3 @@ "main": "./dist/index.js", | ||
"build": "tsc --project tsconfig.json", | ||
"lint": "tslint -c tslint.json -p tsconfig.json && echo 'THERE ARE NO ERRORS'", | ||
"test": "mocha test/**/*.spec.ts --require ts-node/register", | ||
@@ -47,4 +48,6 @@ "test:watch": "npm run test -- --watch --watch-extensions=ts" | ||
"ts-node": "^4.1.0", | ||
"tslint": "^5.9.1", | ||
"tslint-config-standard": "^7.0.0", | ||
"typescript": "^2.6.2" | ||
} | ||
} |
@@ -12,10 +12,10 @@ import { Transaction, Hash } from 'iota-tangle' | ||
private _neighborsTransportsMap: Map<Neighbor, Transport> | ||
private _neighborsTransportsMap: Map<Neighbor, Transport> | ||
private _isRunning: boolean = false | ||
private _onTransportReceive: ((data: Data, neighbor: Neighbor) => void)|null = null | ||
private _onTransportError: ((error: any) => void)|null = null | ||
private _onTransportReceive: ((data: Data, neighbor: Neighbor) => void) | null = null | ||
private _onTransportError: ((error: any) => void) | null = null | ||
constructor(params: { | ||
constructor (params: { | ||
neighbors?: Neighbor[] | ||
@@ -26,3 +26,3 @@ transports?: Transport[] | ||
const transports = params.transports || [] | ||
const neighbors = params.neighbors || [] | ||
const neighbors = params.neighbors || [] | ||
@@ -47,3 +47,3 @@ if (!transports.length) { | ||
this._transports = transports | ||
this._neighbors = neighbors | ||
this._neighbors = neighbors | ||
@@ -53,9 +53,7 @@ this._neighborsTransportsMap = neighborsTransportsMap | ||
get isRunning(): boolean { | ||
get isRunning (): boolean { | ||
return this._isRunning | ||
} | ||
getNeighbor(neighborAddress: string): Neighbor|null { | ||
getNeighbor (neighborAddress: string): Neighbor | null { | ||
for (const neighbor of this._neighbors) { | ||
@@ -70,4 +68,3 @@ if (neighbor.match(neighborAddress)) { | ||
async addNeighbor(neighbor: Neighbor): Promise<void> { | ||
async addNeighbor (neighbor: Neighbor): Promise<void> { | ||
if (this._neighborsTransportsMap.has(neighbor)) { | ||
@@ -92,4 +89,3 @@ throw new Error(`Couldn't add a neighbor: the neighbor '${neighbor.address}' already exists!`) | ||
async removeNeighbor(neighbor: Neighbor): Promise<void> { | ||
async removeNeighbor (neighbor: Neighbor): Promise<void> { | ||
if (!this._neighborsTransportsMap.has(neighbor)) { | ||
@@ -107,4 +103,3 @@ throw new Error(`Couldn't remove a neighbor: the neighbor '${neighbor.address}' doesn't exists!`) | ||
async run(): Promise<void> { | ||
async run (): Promise<void> { | ||
if (this._isRunning) { | ||
@@ -129,7 +124,7 @@ throw new Error('The gateway is already running!') | ||
transport.on('receive', onTransportReceive) | ||
transport.on('error', onTransportError) | ||
transport.on('error', onTransportError) | ||
} | ||
this._onTransportReceive = onTransportReceive | ||
this._onTransportError = onTransportError | ||
this._onTransportError = onTransportError | ||
@@ -140,3 +135,3 @@ } catch (error) { | ||
await this._neighborsTransportsMap.get(neighbor).removeNeighbor(neighbor) | ||
} catch (error) {} | ||
} catch (error) { /* ignore any errors */ } | ||
})) | ||
@@ -147,3 +142,3 @@ | ||
await transport.shutdown() | ||
} catch (error) {} | ||
} catch (error) { /* ignore any errors */ } | ||
})) | ||
@@ -158,4 +153,3 @@ | ||
async shutdown(): Promise<void> { | ||
async shutdown (): Promise<void> { | ||
if (!this._isRunning) { | ||
@@ -175,7 +169,7 @@ throw new Error('The gateway is not running!') | ||
transport.removeListener('receive', this._onTransportReceive) | ||
transport.removeListener('error', this._onTransportError) | ||
transport.removeListener('error', this._onTransportError) | ||
} | ||
this._onTransportReceive = null | ||
this._onTransportError = null | ||
this._onTransportError = null | ||
@@ -186,4 +180,3 @@ this.emit('shutdown') | ||
async send(data: Data, neighborAddress: string): Promise<void> { | ||
async send (data: Data, neighborAddress: string): Promise<void> { | ||
if (!this._isRunning) { | ||
@@ -190,0 +183,0 @@ throw new Error("Can't send a data: the gateway is not running!") |
export abstract class Neighbor { | ||
abstract get address(): string | ||
abstract get address (): string | ||
get gatewayCanSendTo(): boolean { | ||
get gatewayCanSendTo (): boolean { | ||
return true | ||
} | ||
get gatewayCanReceiveFrom(): boolean { | ||
get gatewayCanReceiveFrom (): boolean { | ||
return true | ||
} | ||
match(address: string): boolean { | ||
match (address: string): boolean { | ||
return this.address === address | ||
} | ||
} |
@@ -1,9 +0,9 @@ | ||
import { Transaction, Hash, Factory } from 'iota-tangle' | ||
import { Transaction, Factory } from 'iota-tangle' | ||
import { Data } from './gateway' | ||
export const TRANSCTION_OFFSET = 0 | ||
export const TRANSACTION_SIZE = Transaction.BYTES_SIZE | ||
export const TRANSACTION_SIZE = Transaction.BYTES_SIZE | ||
export const REQUEST_HASH_OFFSET = TRANSCTION_OFFSET + TRANSACTION_SIZE | ||
export const REQUEST_HASH_SIZE = 46 | ||
export const REQUEST_HASH_SIZE = 46 | ||
@@ -15,11 +15,11 @@ export const PACKET_SIZE = TRANSACTION_SIZE + REQUEST_HASH_SIZE | ||
constructor(params: { factory: Factory }) { | ||
constructor (params: { factory: Factory }) { | ||
this._factory = params.factory | ||
} | ||
get packetSize(): number { | ||
get packetSize (): number { | ||
return PACKET_SIZE | ||
} | ||
pack(data: Data): Buffer { | ||
pack (data: Data): Buffer { | ||
const buffer = Buffer.alloc(PACKET_SIZE) | ||
@@ -33,3 +33,3 @@ | ||
unpack(packet: Buffer): Data { | ||
unpack (packet: Buffer): Data { | ||
const transactionBytes = packet.slice(TRANSCTION_OFFSET, TRANSCTION_OFFSET + TRANSACTION_SIZE) | ||
@@ -40,5 +40,5 @@ const hashBytes = packet.slice(REQUEST_HASH_OFFSET, REQUEST_HASH_OFFSET + REQUEST_HASH_SIZE) | ||
transaction: this._factory.createTransactionFromBytes(transactionBytes), | ||
requestHash: this._factory.createHashFromBytes(hashBytes), | ||
requestHash: this._factory.createHashFromBytes(hashBytes) | ||
} | ||
} | ||
} |
@@ -1,21 +0,17 @@ | ||
import { Transaction, Hash } from 'iota-tangle' | ||
import { EventEmitter } from 'events' | ||
import { parse as parseUrl } from 'url' | ||
import { Data } from './gateway' | ||
import { Neighbor } from './neighbor' | ||
export abstract class Transport extends EventEmitter { | ||
abstract get isRunning(): boolean | ||
abstract get isRunning (): boolean | ||
abstract supports(neighbor: Neighbor): boolean | ||
abstract supports (neighbor: Neighbor): boolean | ||
abstract async run(): Promise<void> | ||
abstract async shutdown(): Promise<void> | ||
abstract async run (): Promise<void> | ||
abstract async shutdown (): Promise<void> | ||
abstract async addNeighbor(neighbor: Neighbor): Promise<void> | ||
abstract async removeNeighbor(neighbor: Neighbor): Promise<void> | ||
abstract async addNeighbor (neighbor: Neighbor): Promise<void> | ||
abstract async removeNeighbor (neighbor: Neighbor): Promise<void> | ||
abstract async send(data: Data, neighbor: Neighbor): Promise<void> | ||
abstract async send (data: Data, neighbor: Neighbor): Promise<void> | ||
} | ||
@@ -12,5 +12,5 @@ import { expect, use }from 'chai' | ||
import { TransportStub, NeighborStub, generateHash, generateTransaction } from './utils' | ||
import { setTimeout } from 'timers'; | ||
import { setTimeout } from 'timers' | ||
describe("Gateway", () => { | ||
describe('Gateway', () => { | ||
let gateway: Gateway | ||
@@ -23,3 +23,3 @@ let transports: Transport[] | ||
new NeighborStub({ address: 'address1' }), | ||
new NeighborStub({ address: 'address2' }), | ||
new NeighborStub({ address: 'address2' }) | ||
] | ||
@@ -29,3 +29,3 @@ | ||
new TransportStub({ supports: (n) => n === neighbors[0] }), | ||
new TransportStub({ supports: (n) => n === neighbors[1] || n.address.startsWith('1234') }), | ||
new TransportStub({ supports: (n) => n === neighbors[1] || n.address.startsWith('1234') }) | ||
] | ||
@@ -36,4 +36,3 @@ | ||
describe("run()", () => { | ||
describe('run()', () => { | ||
let runEventCallback: SinonSpy | ||
@@ -56,3 +55,3 @@ let receiveEventCallback: SinonSpy | ||
it("should launch all transports", async () => { | ||
it('should launch all transports', async () => { | ||
for (const transport of transports) { | ||
@@ -69,3 +68,3 @@ expect(transport.run).to.not.have.been.called | ||
it("should make isRunning flag return true", async () => { | ||
it('should make isRunning flag return true', async () => { | ||
expect(gateway.isRunning).to.be.false | ||
@@ -76,3 +75,3 @@ await expect(gateway.run()).to.be.fulfilled | ||
it("should add neighbors to transports", async () => { | ||
it('should add neighbors to transports', async () => { | ||
for (const transport of transports) { | ||
@@ -99,3 +98,3 @@ expect(transport.addNeighbor).to.not.have.been.called | ||
it("should start receiving data from transports", async () => { | ||
it('should start receiving data from transports', async () => { | ||
await expect(gateway.run()).to.be.fulfilled | ||
@@ -118,3 +117,3 @@ | ||
it("should start receiving errors from transports", async () => { | ||
it('should start receiving errors from transports', async () => { | ||
await expect(gateway.run()).to.be.fulfilled | ||
@@ -135,3 +134,3 @@ | ||
it("should be rejected if some of the transports run() calls was rejected", async () => { | ||
it('should be rejected if some of the transports run() calls was rejected', async () => { | ||
transports[0].run = stub().resolves() | ||
@@ -144,3 +143,3 @@ transports[1].run = stub().rejects() | ||
it("should shutdown all the gateway's transports " + | ||
"if there were an error while lanching the gatway", async () => { | ||
'if there were an error while lanching the gatway', async () => { | ||
@@ -161,4 +160,4 @@ transports[0].run = stub().rejects() | ||
it("should remove neighbors from all transports " + | ||
"if there were an error while lanching the gatway", async () => { | ||
it('should remove neighbors from all transports ' + | ||
'if there were an error while lanching the gatway', async () => { | ||
@@ -181,3 +180,3 @@ transports[0].addNeighbor = stub().rejects() | ||
it("should be rejected if server is already started", async () => { | ||
it('should be rejected if server is already started', async () => { | ||
await expect(gateway.run()).to.not.be.rejected | ||
@@ -188,4 +187,3 @@ await expect(gateway.run()).to.be.rejected | ||
describe("shutdown()", () => { | ||
describe('shutdown()', () => { | ||
let receiveEventCallback: SinonSpy | ||
@@ -222,3 +220,3 @@ let errorEventCallback: SinonSpy | ||
it("should make isRunning flag return false", async () => { | ||
it('should make isRunning flag return false', async () => { | ||
expect(gateway.isRunning).to.be.true | ||
@@ -229,3 +227,3 @@ await expect(gateway.shutdown()).to.be.fulfilled | ||
it("should remove neighbors from all transports ", async () => { | ||
it('should remove neighbors from all transports ', async () => { | ||
for (let transport of transports) { | ||
@@ -244,3 +242,3 @@ expect(transport.removeNeighbor).to.not.have.been.called | ||
it("should stop receiving data from transports", async () => { | ||
it('should stop receiving data from transports', async () => { | ||
await expect(gateway.shutdown()).to.be.fulfilled | ||
@@ -258,3 +256,3 @@ | ||
it("should stop receiving errors from transports", async () => { | ||
it('should stop receiving errors from transports', async () => { | ||
await expect(gateway.shutdown()).to.be.fulfilled | ||
@@ -280,3 +278,3 @@ | ||
it("should be rejected if some of the transports shutdown() method calls was rejected", async () => { | ||
it('should be rejected if some of the transports shutdown() method calls was rejected', async () => { | ||
transports[0].shutdown = stub().resolves() | ||
@@ -288,3 +286,3 @@ transports[1].shutdown = stub().rejects() | ||
it("should be rejected if the gateway is not running", async () => { | ||
it('should be rejected if the gateway is not running', async () => { | ||
await expect(gateway.shutdown()).to.be.fulfilled | ||
@@ -295,4 +293,3 @@ await expect(gateway.shutdown()).to.be.rejected | ||
describe("send(data, neighbor)", () => { | ||
describe('send(data, neighbor)', () => { | ||
let data: Data | ||
@@ -315,3 +312,3 @@ let receiveEventCallback: SinonSpy | ||
it("should delegate sending of data to the specified neighbor", async () => { | ||
it('should delegate sending of data to the specified neighbor', async () => { | ||
expect(transports[0].send).to.not.have.been.called | ||
@@ -340,4 +337,3 @@ expect(transports[0].send).to.not.have.been.called | ||
describe("addNeighbor(neighbor)", () => { | ||
describe('addNeighbor(neighbor)', () => { | ||
let neighbor: Neighbor | ||
@@ -355,3 +351,3 @@ | ||
it("should add neighbor to the gateway", async () => { | ||
it('should add neighbor to the gateway', async () => { | ||
expect(gateway.getNeighbor(neighbor.address)).to.be.null | ||
@@ -364,3 +360,3 @@ | ||
it("should add specified neighbor to the transport if the gateway is running", async () => { | ||
it('should add specified neighbor to the transport if the gateway is running', async () => { | ||
expect(transports[0].addNeighbor).to.not.have.been.called | ||
@@ -375,3 +371,3 @@ expect(transports[1].addNeighbor).to.not.have.been.called | ||
it("should not add specified neighbor to the transport if the gateway is not running", async () => { | ||
it('should not add specified neighbor to the transport if the gateway is not running', async () => { | ||
await expect(gateway.shutdown()).to.be.fulfilled | ||
@@ -388,3 +384,3 @@ | ||
it("should be rejected if there were an error while adding neighbor to the transport ", async () => { | ||
it('should be rejected if there were an error while adding neighbor to the transport ', async () => { | ||
transports[1].addNeighbor = stub().rejects() | ||
@@ -401,7 +397,7 @@ | ||
it("should be rejected if there is no transport that supports specified neighbor", async () => { | ||
it('should be rejected if there is no transport that supports specified neighbor', async () => { | ||
await expect(gateway.addNeighbor(new NeighborStub({ address: '4321.12.12.12' }))).to.be.rejected | ||
}) | ||
it("should be rejected if there specified neighbor has been already added to the gateway", async () => { | ||
it('should be rejected if there specified neighbor has been already added to the gateway', async () => { | ||
await expect(gateway.addNeighbor(neighbor)).to.be.fulfilled | ||
@@ -412,4 +408,3 @@ await expect(gateway.addNeighbor(neighbor)).to.be.rejected | ||
describe("removeNeighbor(neighbor)", () => { | ||
describe('removeNeighbor(neighbor)', () => { | ||
let neighbor: Neighbor | ||
@@ -427,3 +422,3 @@ | ||
it("should remove the neighbor from the gateway", async () => { | ||
it('should remove the neighbor from the gateway', async () => { | ||
expect(gateway.getNeighbor(neighbor.address)).to.equal(neighbor) | ||
@@ -440,3 +435,3 @@ | ||
it("should remove the neighbor from the transport if the gateway is running", async () => { | ||
it('should remove the neighbor from the transport if the gateway is running', async () => { | ||
await expect(gateway.run()).to.be.fulfilled | ||
@@ -453,3 +448,3 @@ | ||
it("should be rejected if there were an error while removing the neighbor from the transport", async () => { | ||
it('should be rejected if there were an error while removing the neighbor from the transport', async () => { | ||
transports[1].removeNeighbor = stub().rejects() | ||
@@ -468,3 +463,3 @@ | ||
it("should not remove the neighbor from the transport if the gateway is not running", async () => { | ||
it('should not remove the neighbor from the transport if the gateway is not running', async () => { | ||
expect(transports[0].removeNeighbor).to.not.have.been.called | ||
@@ -471,0 +466,0 @@ expect(transports[1].removeNeighbor).to.not.have.been.called |
@@ -14,3 +14,2 @@ import { expect } from 'chai' | ||
beforeEach(() => { | ||
@@ -25,3 +24,2 @@ serializer = new Serializer() | ||
describe('pack(packetData)', () => { | ||
@@ -42,3 +40,2 @@ it('should pack the specified transaction and transaction hash into a packet of bytes', () => { | ||
describe('unpack(packet)', () => { | ||
@@ -45,0 +42,0 @@ it('should unpack the specified packet', () => { |
@@ -9,4 +9,3 @@ import { Transaction, Hash, Factory, Serializer } from 'iota-tangle' | ||
export function generateTransaction(): Transaction { | ||
export function generateTransaction (): Transaction { | ||
const buffer = Buffer.alloc(1604) | ||
@@ -19,4 +18,3 @@ | ||
export function generateHash(): Hash { | ||
export function generateHash (): Hash { | ||
const buffer = Buffer.alloc(49) | ||
@@ -29,7 +27,6 @@ | ||
export class NeighborStub extends Neighbor { | ||
private _address: string | ||
constructor(params: { address: string }) { | ||
constructor (params: { address: string }) { | ||
super() | ||
@@ -39,3 +36,3 @@ this._address = params.address | ||
get address(): string { | ||
get address (): string { | ||
return this._address | ||
@@ -45,3 +42,2 @@ } | ||
export class TransportStub extends Transport { | ||
@@ -51,7 +47,7 @@ private _isRunning = false | ||
get isRunning() { | ||
get isRunning () { | ||
return this._isRunning | ||
} | ||
constructor(params: { supports: Function }) { | ||
constructor (params: { supports: Function }) { | ||
super() | ||
@@ -61,18 +57,18 @@ this._supports = params.supports | ||
supports(neighbor: Neighbor): boolean { | ||
supports (neighbor: Neighbor): boolean { | ||
return this._supports(neighbor) | ||
} | ||
async addNeighbor(neighbor: Neighbor): Promise<void> {} | ||
async removeNeighbor(neighbor: Neighbor): Promise<void> {} | ||
async addNeighbor (neighbor: Neighbor): Promise<void> {} | ||
async removeNeighbor (neighbor: Neighbor): Promise<void> {} | ||
async send(data: Data, neighbor: Neighbor): Promise<void> {} | ||
async send (data: Data, neighbor: Neighbor): Promise<void> {} | ||
async run(): Promise<void> { | ||
async run (): Promise<void> { | ||
this._isRunning = true | ||
} | ||
async shutdown(): Promise<void> { | ||
async shutdown (): Promise<void> { | ||
this._isRunning = false | ||
} | ||
} |
@@ -10,4 +10,4 @@ { | ||
"include": [ | ||
"src/**/*.ts" | ||
"src/**/*.ts", | ||
] | ||
} |
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
65492
37
1333
15