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 all 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 { Socket, createConnection } from 'net';
import BaseConnection from './base-connection';

export default class InetSocketConnection extends BaseConnection {
client?: 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 = 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();
});
});
}
}
8 changes: 4 additions & 4 deletions src/connection/unix-socket-connection.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as net from 'net';
import { Socket, createConnection } from 'net';
import BaseConnection from './base-connection';

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

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

setupConnection(): Promise<void> {
return new Promise(resolve => {
this.client = net.createConnection(this.sockPath);
this.client = createConnection(this.sockPath);
this.client.on('data', (data) => {
const dataLines = data.toString().split(/\r?\n/);
dataLines.map((data) => {
Expand Down
39 changes: 36 additions & 3 deletions src/juno-node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isIP } from 'net';
import { promises as fsPromises } from 'fs';
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,38 @@ 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 (isIP(host) && !isNaN(Number(port))) {
return this.fromInetSocket(host, Number(port));
}
if ( (await fsPromises.lstat(socketPath)).isSocket() ) {
return this.fromUnixSocket(socketPath);
}

throw new Error('Invalid socket object. Only unix domain sockets and Inet sockets are allowed');

}

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

throw new Error('Invalid unix socket path');
}

public static async fromInetSocket(host: string, port: number) {
if (isIP(host) && !isNaN(Number(port))) {
return new JunoModule(new InetSocketConnection(host, port), new JsonProtocol());
}

throw new Error('Invalid Inet socket address. Use the format `{host}:{port}`')
}

public async initialize(
Expand Down