Skip to content

Commit c7b5e42

Browse files
Merge pull request #10 from sauravhiremath/master
TCP sockets for connection
2 parents d300354 + f50d4db commit c7b5e42

14 files changed

+2601
-2295
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Gotham Node
1+
# Juno Node
22

3-
This is a library that provides you with helper methods for interfacing with the microservices framework, [gotham](https://github.com/bytesonus/gotham).
3+
This is a library that provides you with helper methods for interfacing with the microservices framework, [juno](https://github.com/bytesonus/juno).
44

55
## How to use:
66

@@ -13,10 +13,10 @@ For all other basic needs, you can get away without worrying about any of that.
1313
### A piece of code is worth a thousand words
1414

1515
```js
16-
import GothamModule from "./src/gotham-node";
16+
import JunoModule from "juno-node";
1717

1818
async function main() {
19-
let module = GothamModule.default("./path/to/gotham.sock");
19+
let module = JunoModule.default("./path/to/juno.sock");
2020
await module.initialize("module-name", "1.0.0");
2121
console.log("Initialized!");
2222

@@ -29,4 +29,4 @@ async function main() {
2929
}
3030

3131
main()
32-
```
32+
```

package-lock.json

Lines changed: 2446 additions & 2216 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"name": "gotham-node",
2+
"name": "juno-node",
33
"version": "0.0.0",
44
"description": "",
55
"keywords": [],
6-
"main": "dist/gotham-node.umd.js",
7-
"module": "dist/gotham-node.es5.js",
8-
"typings": "dist/types/gotham-node.d.ts",
6+
"main": "dist/juno-node.umd.js",
7+
"module": "dist/juno-node.es5.js",
8+
"typings": "dist/types/juno-node.d.ts",
99
"files": [
1010
"dist"
1111
],
12-
"author": "thebongy <rishit.[email protected]>",
12+
"author": "thebongy <rishit.[email protected]>",
1313
"repository": {
1414
"type": "git",
1515
"url": ""
@@ -55,48 +55,48 @@
5555
]
5656
},
5757
"devDependencies": {
58-
"@commitlint/cli": "^7.1.2",
59-
"@commitlint/config-conventional": "^7.1.2",
60-
"@types/chai": "^4.2.7",
58+
"@commitlint/cli": "^8.3.5",
59+
"@commitlint/config-conventional": "^8.3.4",
60+
"@types/chai": "^4.2.11",
6161
"@types/chai-as-promised": "^7.1.2",
6262
"@types/expect": "^24.3.0",
63-
"@types/mocha": "^5.2.7",
64-
"@types/node": "^10.11.0",
65-
"@types/sinon": "^7.5.1",
66-
"@types/sinon-chai": "^3.2.3",
63+
"@types/mocha": "^7.0.2",
64+
"@types/node": "^13.13.4",
65+
"@types/sinon": "^9.0.0",
66+
"@types/sinon-chai": "^3.2.4",
6767
"chai": "^4.2.0",
6868
"chai-as-promised": "^7.1.1",
6969
"chai-exclude": "^2.0.2",
70-
"colors": "^1.3.2",
71-
"commitizen": "^3.0.0",
72-
"coveralls": "^3.0.2",
73-
"cross-env": "^5.2.0",
74-
"cz-conventional-changelog": "^2.1.0",
75-
"lint-staged": "^8.0.0",
70+
"colors": "^1.4.0",
71+
"commitizen": "^4.0.4",
72+
"coveralls": "^3.1.0",
73+
"cross-env": "^7.0.2",
74+
"cz-conventional-changelog": "^3.1.0",
75+
"lint-staged": "^10.1.7",
7676
"lodash.camelcase": "^4.3.0",
77-
"mocha": "^7.0.0",
78-
"prettier": "^1.14.3",
77+
"mocha": "^7.1.2",
78+
"prettier": "^2.0.5",
7979
"prompt": "^1.0.0",
80-
"replace-in-file": "^3.4.2",
81-
"rimraf": "^2.6.2",
82-
"rollup": "^0.67.0",
83-
"rollup-plugin-commonjs": "^9.1.8",
84-
"rollup-plugin-json": "^3.1.0",
85-
"rollup-plugin-node-resolve": "^3.4.0",
86-
"rollup-plugin-sourcemaps": "^0.4.2",
87-
"rollup-plugin-typescript2": "^0.18.0",
88-
"semantic-release": "^15.9.16",
89-
"shelljs": "^0.8.3",
90-
"sinon": "^8.0.2",
91-
"sinon-chai": "^3.4.0",
92-
"travis-deploy-once": "^5.0.9",
93-
"ts-mocha": "^6.0.0",
94-
"ts-node": "^7.0.1",
95-
"tslint": "^5.11.0",
96-
"tslint-config-prettier": "^1.15.0",
97-
"tslint-config-standard": "^8.0.1",
98-
"typedoc": "^0.12.0",
99-
"typescript": "^3.0.3"
80+
"replace-in-file": "^6.0.0",
81+
"rimraf": "^3.0.2",
82+
"rollup": "^2.7.2",
83+
"rollup-plugin-commonjs": "^10.1.0",
84+
"rollup-plugin-json": "^4.0.0",
85+
"rollup-plugin-node-resolve": "^5.2.0",
86+
"rollup-plugin-sourcemaps": "^0.5.0",
87+
"rollup-plugin-typescript2": "^0.27.0",
88+
"semantic-release": "^17.0.7",
89+
"shelljs": "^0.8.4",
90+
"sinon": "^9.0.2",
91+
"sinon-chai": "^3.5.0",
92+
"travis-deploy-once": "^5.0.11",
93+
"ts-mocha": "^7.0.0",
94+
"ts-node": "^8.9.1",
95+
"tslint": "^6.1.1",
96+
"tslint-config-prettier": "^1.18.0",
97+
"tslint-config-standard": "^9.0.0",
98+
"typedoc": "^0.17.5",
99+
"typescript": "^3.8.3"
100100
},
101101
"dependencies": {}
102102
}

rollup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import json from 'rollup-plugin-json'
77

88
const pkg = require('./package.json')
99

10-
const libraryName = 'gotham-node'
10+
const libraryName = 'juno-node'
1111

1212
export default {
1313
input: `src/${libraryName}.ts`,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Socket, createConnection } from 'net';
2+
import BaseConnection from './base-connection';
3+
4+
export default class InetSocketConnection extends BaseConnection {
5+
client?: Socket;
6+
host: string;
7+
port: number;
8+
9+
constructor(host: string, port: number) {
10+
super();
11+
this.host = host;
12+
this.port = port;
13+
}
14+
15+
setupConnection(): Promise<void> {
16+
return new Promise(resolve => {
17+
this.client = createConnection(this.port, this.host);
18+
this.client.on('data', (data) => {
19+
const dataLines = data.toString().split(/\r?\n/);
20+
dataLines.map((data) => {
21+
if (data) {
22+
this.onData(Buffer.from(data))
23+
}
24+
});
25+
});
26+
this.client.on('connect', () => {
27+
resolve();
28+
});
29+
});
30+
}
31+
32+
async closeConnection() {
33+
this.client?.destroy();
34+
}
35+
36+
send(message: Buffer): Promise<void> {
37+
return new Promise(resolve => {
38+
this.client?.write(message, () => {
39+
resolve();
40+
});
41+
});
42+
}
43+
}

src/connection/unix-socket-connection.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as net from 'net';
1+
import { Socket, createConnection } from 'net';
22
import BaseConnection from './base-connection';
33

4-
export default class SocketConnection extends BaseConnection {
5-
client?: net.Socket;
4+
export default class UnixSocketConnection extends BaseConnection {
5+
client?: Socket;
66
sockPath: string;
77

88
constructor(sockPath: string) {
@@ -12,7 +12,7 @@ export default class SocketConnection extends BaseConnection {
1212

1313
setupConnection(): Promise<void> {
1414
return new Promise(resolve => {
15-
this.client = net.createConnection(this.sockPath);
15+
this.client = createConnection(this.sockPath);
1616
this.client.on('data', (data) => {
1717
const dataLines = data.toString().split(/\r?\n/);
1818
dataLines.map((data) => {

src/gotham-node.ts renamed to src/juno-node.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isIP } from 'net';
2+
import { promises as fsPromises } from 'fs';
13
import { BaseProtocol } from './protocol/base-protocol';
24
import BaseConnection from './connection/base-connection';
35
import { JsonProtocol } from './protocol/json-protocol';
@@ -6,11 +8,12 @@ import {
68
FunctionCallRequest,
79
FunctionCallResponse,
810
TriggerHookRequest,
9-
GothamMessage
11+
JunoMessage
1012
} from './models/messages';
11-
import SocketConnection from './connection/unix-socket-connection';
13+
import UnixSocketConnection from './connection/unix-socket-connection';
14+
import InetSocketConnection from './connection/inet-socket-connection';
1215

13-
export default class GothamModule {
16+
export default class JunoModule {
1417

1518
private protocol: BaseProtocol;
1619
private moduleId?: string;
@@ -27,8 +30,38 @@ export default class GothamModule {
2730
// this.connection.setOnDataListener(this.onDataHandler);
2831
}
2932

30-
public static default(socketPath: string): GothamModule {
31-
return new GothamModule(new SocketConnection(socketPath), new JsonProtocol());
33+
public static async default(socketPath: string) {
34+
const [ host, port ] = socketPath.split(':');
35+
36+
if (isIP(host) && !isNaN(Number(port))) {
37+
return this.fromInetSocket(host, Number(port));
38+
}
39+
if ( (await fsPromises.lstat(socketPath)).isSocket() ) {
40+
return this.fromUnixSocket(socketPath);
41+
}
42+
43+
throw new Error('Invalid socket object. Only unix domain sockets and Inet sockets are allowed');
44+
45+
}
46+
47+
public static async fromUnixSocket(path: string) {
48+
// Return Error if invoked from windows
49+
if (process.platform == 'win32') {
50+
throw new Error('Unix sockets are not supported on windows');
51+
}
52+
if ( (await fsPromises.lstat(path)).isSocket() ) {
53+
return new JunoModule(new UnixSocketConnection(path), new JsonProtocol());
54+
}
55+
56+
throw new Error('Invalid unix socket path');
57+
}
58+
59+
public static async fromInetSocket(host: string, port: number) {
60+
if (isIP(host) && !isNaN(Number(port))) {
61+
return new JunoModule(new InetSocketConnection(host, port), new JsonProtocol());
62+
}
63+
64+
throw new Error('Invalid Inet socket address. Use the format `{host}:{port}`')
3265
}
3366

3467
public async initialize(
@@ -87,7 +120,7 @@ export default class GothamModule {
87120
return this.connection.closeConnection();
88121
}
89122

90-
private async sendRequest(request: GothamMessage) {
123+
private async sendRequest(request: JunoMessage) {
91124
if (request.type === RequestTypes.ModuleRegistration && this.registered) {
92125
throw new Error('Module already registered');
93126
}
@@ -179,7 +212,7 @@ export default class GothamModule {
179212
private async executeHookTriggered(request: TriggerHookRequest) {
180213
if (request.hook) {
181214
// Hook triggered by another module.
182-
if (request.hook === `gotham.activated`) {
215+
if (request.hook === `juno.activated`) {
183216
this.registered = true;
184217
if (this.messagBuffer) {
185218
this.connection.send(this.messagBuffer);

src/models/messages.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ export interface DeclareFunctionResponse extends BaseMessage {
5050
function: string;
5151
}
5252

53-
export type GothamResponse =
53+
export type JunoResponse =
5454
RegisterModuleResponse |
5555
ListenHookResponse |
5656
TriggerHookResponse |
5757
DeclareFunctionResponse |
5858
FunctionCallResponse;
59-
export type GothamRequest =
59+
export type JunoRequest =
6060
RegisterModuleRequest |
6161
DeclareFunctionRequest |
6262
FunctionCallRequest |
6363
RegisterHookRequest |
6464
TriggerHookRequest;
65-
export type GothamMessage =
65+
export type JunoMessage =
6666
RegisterModuleResponse |
6767
ListenHookResponse |
6868
TriggerHookResponse |

src/protocol/base-protocol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
DeclareFunctionRequest,
55
FunctionCallRequest,
66
RegisterModuleRequest,
7-
GothamMessage
7+
JunoMessage
88
} from '../models/messages';
99
import { RequestTypes } from '../utils/constants';
1010

@@ -70,6 +70,6 @@ export abstract class BaseProtocol {
7070
};
7171
}
7272

73-
public abstract encode(req: GothamMessage): Buffer;
74-
public abstract decode(data: Buffer): GothamMessage;
73+
public abstract encode(req: JunoMessage): Buffer;
74+
public abstract decode(data: Buffer): JunoMessage;
7575
}

src/protocol/json-protocol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { BaseProtocol } from './base-protocol';
2-
import { GothamRequest, GothamResponse } from '../models/messages';
2+
import { JunoRequest, JunoResponse } from '../models/messages';
33

44
export class JsonProtocol extends BaseProtocol {
5-
public encode(req: GothamRequest): Buffer {
5+
public encode(req: JunoRequest): Buffer {
66
return Buffer.from(JSON.stringify(req) + '\n');
77
}
88

9-
public decode(data: Buffer): GothamResponse {
9+
public decode(data: Buffer): JunoResponse {
1010
return JSON.parse(data.toString());
1111
}
1212
}

0 commit comments

Comments
 (0)