Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "juno-node",
"version": "0.1.1",
"version": "0.1.2",
"description": "",
"keywords": [],
"main": "dist/juno-node.cjs.js",
Expand Down
65 changes: 40 additions & 25 deletions src/juno-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { promises as fsPromises } from 'fs';
import { BaseProtocol } from './protocol/base-protocol';
import BaseConnection from './connection/base-connection';
import { JsonProtocol } from './protocol/json-protocol';
import { ResponseTypes, RequestTypes } from './utils/constants';
import { ResponseTypes, RequestTypes, ErrorTypes } from './utils/constants';
import {
FunctionCallRequest,
FunctionCallResponse,
Expand All @@ -12,6 +12,7 @@ import {
} from './models/messages';
import UnixSocketConnection from './connection/unix-socket-connection';
import InetSocketConnection from './connection/inet-socket-connection';
import {JunoError} from './models/errors';

export default class JunoModule {

Expand Down Expand Up @@ -110,9 +111,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 All @@ -139,54 +140,73 @@ export default class JunoModule {
}

return new Promise((resolve, reject) => {
this.requests[request.requestId] = (response: any) => {
if (response) {
resolve(response);
this.requests[request.requestId] = (err: boolean | Error, response: any) => {
if (err) {
reject(err);
} else {
reject(response);
resolve(response);
}
};
});
}

private async onDataHandler(data: Buffer) {
const response = this.protocol.decode(data);
let value;
let value: any = true;
let err: boolean | Error = false;
switch (response.type) {
case ResponseTypes.ModuleRegistered: {
value = true;
err = false;
break;
}
case ResponseTypes.FunctionResponse: {
value = await (response as FunctionCallResponse).data;
err = false;
value = (response as FunctionCallResponse).data;
break;
}
case ResponseTypes.FunctionDeclared: {
value = true;
err = false;
break;
}
case ResponseTypes.HookRegistered: {
value = true;
err = false;
break;
}
case ResponseTypes.HookTriggered: {
value = await this.executeHookTriggered(response as TriggerHookRequest);
try {
await this.executeHookTriggered(response as TriggerHookRequest);
err = false;
} catch (e) {
err = e;
}
break;
}

case RequestTypes.FunctionCall: {
this.executeFunctionCall(response as FunctionCallRequest);
try {
await this.executeFunctionCall(response as FunctionCallRequest);
} catch (e) {
err = e;
}
break;
}

default: {
value = false;
break;
err = TypeError(`Error message/Invalid message received from juno:
${JSON.stringify(response)}`);
}
}

if (err instanceof JunoError) {
this.sendRequest({
requestId: response.requestId,
type: ResponseTypes.Error,
error: err.errCode,
});
}

if (this.requests[response.requestId]) {
this.requests[response.requestId](value);
this.requests[response.requestId](err, value);
delete this.requests[response.requestId];
}
}
Expand All @@ -202,10 +222,9 @@ export default class JunoModule {
type: ResponseTypes.FunctionResponse,
data: res || {}
});
return true;
} else {
// Function wasn't found in the module.
return false;
throw new JunoError(ErrorTypes.UnkownFunction);
Comment thread
thebongy marked this conversation as resolved.
Resolved
}
}

Expand All @@ -219,13 +238,9 @@ export default class JunoModule {
}
} else if (this.hookListeners[request.hook]) {
for (const listener of this.hookListeners[request.hook]) {
listener();
listener(request.data || {});
}
}
return true;
} else {
// This moddule triggered the hook.
return true;
}
}
}
}
11 changes: 11 additions & 0 deletions src/models/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class JunoError extends Error {
errCode: number;
constructor(errCode: number) {
super(`Juno Error: ${errCode}`);
this.errCode = errCode;

// Set the prototype explicitly.
Object.setPrototypeOf(this, JunoError.prototype);
}
}

14 changes: 11 additions & 3 deletions 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,19 +46,23 @@ export interface ListenHookResponse extends BaseMessage {

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

export interface DeclareFunctionResponse extends BaseMessage {
function: string;
}

export interface ErrorResponse extends BaseMessage {
error: number;
}

export type JunoResponse =
RegisterModuleResponse |
ListenHookResponse |
TriggerHookResponse |
DeclareFunctionResponse |
FunctionCallResponse;
FunctionCallResponse |
ErrorResponse;
export type JunoRequest =
RegisterModuleRequest |
DeclareFunctionRequest |
Expand All @@ -72,4 +79,5 @@ export type JunoMessage =
DeclareFunctionRequest |
FunctionCallRequest |
RegisterHookRequest |
TriggerHookRequest;
TriggerHookRequest |
ErrorResponse;
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
12 changes: 12 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,21 @@ export const RequestTypes = {
};

export const ResponseTypes = {
Error: 0,
ModuleRegistered: 2,
FunctionResponse: 4,
HookRegistered: 6,
HookTriggered: 8,
FunctionDeclared: 10
};

export const ErrorTypes = {
MalformedRequest: 0,
InvalidRequestId: 1,
UnkownRequest: 2,
UnregisteredModule: 3,
UnkownModule: 4,
UnkownFunction: 5,
InvalidModuleId: 6,
DuplicateModule: 7,
};
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