Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #4: TCP sockets for connection #7

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions src/connection/inet-socket-connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as net from 'net';
sauravhiremath marked this conversation as resolved.
Show resolved Hide resolved
import BaseConnection from './base-connection';

export default class InetSocketConnection extends BaseConnection {
client?: net.Socket;
host: string;
port: number;

constructor(host: string, port: number) {
super();
this.host = host;
this.port = port;
}

setupConnection(): Promise<void> {
return new Promise(resolve => {
this.client = net.createConnection(this.port, this.host);
this.client.on('data', (data) => {
const dataLines = data.toString().split(/\r?\n/);
dataLines.map((data) => {
if (data) {
this.onData(Buffer.from(data))
}
});
});
this.client.on('connect', () => {
resolve();
});
});
}

async closeConnection() {
this.client?.destroy();
}

send(message: Buffer): Promise<void> {
return new Promise(resolve => {
this.client?.write(message, () => {
resolve();
});
});
}
}
2 changes: 1 addition & 1 deletion src/connection/unix-socket-connection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as net from 'net';
sauravhiremath marked this conversation as resolved.
Show resolved Hide resolved
import BaseConnection from './base-connection';

export default class SocketConnection extends BaseConnection {
export default class UnixSocketConnection extends BaseConnection {
client?: net.Socket;
sockPath: string;

Expand Down
31 changes: 28 additions & 3 deletions src/juno-node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as net from 'net';
sauravhiremath marked this conversation as resolved.
Show resolved Hide resolved
import * as fs from 'fs';
sauravhiremath marked this conversation as resolved.
Show resolved Hide resolved
import { BaseProtocol } from './protocol/base-protocol';
import BaseConnection from './connection/base-connection';
import { JsonProtocol } from './protocol/json-protocol';
Expand All @@ -8,7 +10,8 @@ import {
TriggerHookRequest,
JunoMessage
} from './models/messages';
import SocketConnection from './connection/unix-socket-connection';
import UnixSocketConnection from './connection/unix-socket-connection';
import InetSocketConnection from './connection/inet-socket-connection';

export default class JunoModule {

Expand All @@ -27,8 +30,30 @@ export default class JunoModule {
// this.connection.setOnDataListener(this.onDataHandler);
}

public static default(socketPath: string): JunoModule {
return new JunoModule(new SocketConnection(socketPath), new JsonProtocol());
public static async default(socketPath: string) {
const [ host, port ] = socketPath.split(':');

if (net.isIP(host) && typeof Number(port) === 'number') {
return this.fromInetSocket(host, Number(port));
}
if ( (await fs.promises.lstat(socketPath)).isSocket() ) {
sauravhiremath marked this conversation as resolved.
Show resolved Hide resolved
return this.fromUnixSocket(socketPath);
}

throw new Error('Invalid socket object');
sauravhiremath marked this conversation as resolved.
Show resolved Hide resolved

}

public static fromUnixSocket(path: string) {
// Return Error if invoked from windows
if (process.platform == 'win32') {
throw new Error('Unix sockets are not supported on windows');
}
return new JunoModule(new UnixSocketConnection(path), new JsonProtocol());
}

public static fromInetSocket(host: string, port: number) {
return new JunoModule(new InetSocketConnection(host, port), new JsonProtocol());
}

public async initialize(
Expand Down