Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6a8a925
feat #4: TCP sockets support added
sauravhiremath Apr 28, 2020
f5d6ca3
feat #4: Error handling for socket path
sauravhiremath Apr 28, 2020
9d6eb53
fix #7: Import destructuring and minor fixes
sauravhiremath Apr 28, 2020
f50d4db
fix #7: fs renamed to fsPromises
sauravhiremath Apr 28, 2020
c7b5e42
Merge pull request #10 from sauravhiremath/master
rakshith-ravi May 1, 2020
367af19
Update github repo link
thebongy May 1, 2020
cb29ff5
Merge branch 'master' into develop
thebongy May 1, 2020
54d96ad
Merge branch 'master' into develop
rakshith-ravi May 1, 2020
d6c89dd
Merge branch 'develop' of github.com:bytesonus/juno-node into develop
thebongy May 1, 2020
23fc531
fix: Don't fail build if just version check fails if not publishing r…
thebongy May 1, 2020
0e1a72a
0.1.0 beta test release (#11)
thebongy May 1, 2020
9bdf552
fix: Correct downloaded atrifact name
thebongy May 1, 2020
b300ba9
Test staging release
thebongy May 1, 2020
9fc7bc2
Update build.yml
rakshith-ravi May 1, 2020
4573f82
fix: Bump version in package.json on staging
thebongy May 1, 2020
4649665
fix: bump version
thebongy May 1, 2020
621fc54
Merge branch 'staging' into develop
thebongy May 1, 2020
59080a4
fix: rename tar file
thebongy May 1, 2020
9584b1b
Merge branch 'develop' of github.com:bytesonus/juno-node into develop
thebongy May 1, 2020
83cf757
fix: Rename tar only if doesn't exist
thebongy May 1, 2020
2463c3c
fix
thebongy May 1, 2020
3ed615a
Removed unnecessarry await
rakshith-ravi May 7, 2020
10c2d58
Merge branch 'develop' of github.com:bytesonus/gotham-node into develop
rakshith-ravi May 7, 2020
79f85d5
fix: Support passing data along with hook
thebongy May 7, 2020
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
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 <[email protected]>",
"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