Skip to content

Commit

Permalink
Pass data along with hooks (#18)
Browse files Browse the repository at this point in the history
* feat #4: TCP sockets support added

* feat #4: Error handling for socket path

* fix #7: Import destructuring and minor fixes

* fix #7: fs renamed to fsPromises

* Update github repo link

* fix: Don't fail build if just version check fails if not publishing release

* 0.1.0 beta test release (#11)

* fix: Correct downloaded atrifact name

* Test staging release

* Update build.yml

* fix: Bump version in package.json on staging

* fix: bump version

* fix: rename tar file

* fix: Rename tar only if doesn't exist

* fix

* Removed unnecessarry await

* fix: Support passing data along with hook
Closes #16

Co-authored-by: Saurav M. H <sauravhiremath@gmail.com>
Co-authored-by: Rakshith Ravi <rakshith.ravi@gmx.com>
  • Loading branch information
3 people committed May 7, 2020
1 parent fb81e9b commit 22f8272
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 22 deletions.
21 changes: 16 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ jobs:
uses: thebongy/version-check@v1
with:
file: package.json
failBuild: false
id: version_check

- name: Bump package.json version (staging)
if: github.ref == 'refs/heads/staging'
run: npm -no-git-tag-version version v${{ steps.version_check.outputs.rawVersion }}-beta

- name: Install dependencies
run: npm ci

Expand All @@ -36,12 +41,18 @@ jobs:
run: npm run build

- name: Create tarball
run: npm pack
run: |
npm pack
- name: Rename tar (staging)
if: github.ref == 'refs/heads/staging'
run: mv *.tgz juno-node-${{ steps.version_check.outputs.rawVersion }}.tgz


- name: Upload Artifact
uses: actions/upload-artifact@v2
with:
name: juno-node-${{ steps.version_check.outputs.releaseVersion }}
name: juno-node-${{ steps.version_check.outputs.rawVersion }}
path: ./juno-node-${{ steps.version_check.outputs.rawVersion }}.tgz

# Publish release on push to master
Expand Down Expand Up @@ -71,7 +82,7 @@ jobs:
uses: actions/create-release@latest
with:
tag_name: ${{ steps.version_check.outputs.releaseVersion }}
release_name: juno-node-${{ steps.version_check.outputs.releaseVersion }}
release_name: ${{ steps.version_check.outputs.releaseVersion }}
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -120,14 +131,14 @@ jobs:
- name: Download Artifact
uses: actions/download-artifact@v2
with:
name: juno-node-${{ steps.version_check.outputs.releaseVersion }}
name: juno-node-${{ steps.version_check.outputs.rawVersion }}

- name: Publish Release
id: create_release
uses: actions/create-release@latest
with:
tag_name: ${{ steps.version_check.outputs.releaseVersion }}
release_name: juno-node-${{ steps.version_check.outputs.releaseVersion }}
release_name: ${{ steps.version_check.outputs.releaseVersion }}
prerelease: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "juno-node",
"version": "0.0.3",
"version": "0.1.1",
"description": "",
"keywords": [],
"main": "dist/juno-node.cjs.js",
Expand All @@ -12,7 +12,7 @@
"author": "thebongy <rishit.bansal@vicara.co>",
"repository": {
"type": "git",
"url": ""
"url": "https://github.com/bytesonus/juno-node"
},
"license": "MIT",
"engines": {
Expand Down
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
47 changes: 40 additions & 7 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 Expand Up @@ -77,9 +110,9 @@ export default class JunoModule {
);
}

public async triggerHook(hook: string) {
public async triggerHook(hook: string, data: any = {}) {
return this.sendRequest(
this.protocol.triggerHook(hook)
this.protocol.triggerHook(hook, data)
);
}

Expand Down Expand Up @@ -125,7 +158,7 @@ export default class JunoModule {
break;
}
case ResponseTypes.FunctionResponse: {
value = await (response as FunctionCallResponse).data;
value = (response as FunctionCallResponse).data;
break;
}
case ResponseTypes.FunctionDeclared: {
Expand Down Expand Up @@ -186,7 +219,7 @@ export default class JunoModule {
}
} else if (this.hookListeners[request.hook]) {
for (const listener of this.hookListeners[request.hook]) {
listener();
listener(request.data || {});
}
}
return true;
Expand Down
4 changes: 3 additions & 1 deletion src/models/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export interface RegisterHookRequest extends BaseMessage {

export interface TriggerHookRequest extends BaseMessage {
hook: string;
data: {
[type: string]: any
};
}

export interface RegisterModuleResponse extends BaseMessage {
Expand All @@ -43,7 +46,6 @@ export interface ListenHookResponse extends BaseMessage {

export interface TriggerHookResponse extends BaseMessage {
hook: string;
data?: any;
}

export interface DeclareFunctionResponse extends BaseMessage {
Expand Down
5 changes: 3 additions & 2 deletions src/protocol/base-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ export abstract class BaseProtocol {
};
}

public triggerHook(hook: string): TriggerHookRequest {
public triggerHook(hook: string, data: any): TriggerHookRequest {
return {
requestId: this.generateRequestId(),
type: RequestTypes.TriggerHook,
hook
hook,
data
};
}

Expand Down
19 changes: 18 additions & 1 deletion test/juno-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,29 @@ makeConnectionTests('Test if requests constructed correctly', function () {
});
});

it('triggerHook', function () {
it('triggerHook with no args', function () {
this.test.module.triggerHook('test_hook');
const message = this.test.getLatestSent();
expect(message).excluding('requestId').to.deep.equal({
type: 7,
hook: 'test_hook',
data: {},
});
});

it('triggerHook with args', function () {
this.test.module.triggerHook('test_hook', {
a:1,
b:2,
});
const message = this.test.getLatestSent();
expect(message).excluding('requestId').to.deep.equal({
type: 7,
hook: 'test_hook',
data: {
a: 1,
b: 2,
}
});
});
});
Expand Down

0 comments on commit 22f8272

Please sign in to comment.