Skip to content

Commit 231028f

Browse files
committed
feat: Change request/response types to numbers, and support gotham.activated hook
1 parent b0a3656 commit 231028f

File tree

8 files changed

+77
-75
lines changed

8 files changed

+77
-75
lines changed

src/connection/socket-connection.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ export default class SocketConnection extends BaseConnection {
1414
return new Promise(resolve => {
1515
this.client = net.createConnection(this.sockPath);
1616
this.client.on('data', (data) => {
17-
this.onData(data);
17+
const dataLines = data.toString().split(/\r?\n/);
18+
dataLines.map((data) => {
19+
if (data) {
20+
this.onData(Buffer.from(data))
21+
}
22+
});
1823
});
1924
this.client.on('connect', () => {
2025
resolve();

src/gotham-node.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import SocketConnection from './connection/socket-connection';
1313
export default class GothamModule {
1414

1515
private protocol: BaseProtocol;
16+
private moduleId?: string;
1617
private connection: BaseConnection;
1718
private requests: { [type: string]: Function } = {};
1819
private functions: { [type: string]: Function } = {};
1920
private hookListeners: { [type: string]: Function[] } = {};
21+
private messagBuffer?: Buffer;
2022
private registered = false;
2123

2224
constructor(connection: BaseConnection, protocol: BaseProtocol) {
@@ -34,6 +36,7 @@ export default class GothamModule {
3436
version: string,
3537
deps: { [type: string]: string } = {}
3638
) {
39+
this.moduleId = moduleId;
3740
// Setup Connection only when initialize called?
3841
await this.connection.setupConnection();
3942
this.connection.setOnDataListener((data) => {
@@ -85,17 +88,22 @@ export default class GothamModule {
8588
}
8689

8790
private async sendRequest(request: GothamMessage) {
88-
if (request.type !== RequestTypes.ModuleRegistration && !this.registered) {
89-
throw new Error('Can\'t send request before module has been registered');
90-
}
91-
92-
if (request.type === 'moduleRegistration' && this.registered) {
91+
if (request.type === RequestTypes.ModuleRegistration && this.registered) {
9392
throw new Error('Module already registered');
9493
}
9594

96-
await this.connection.send(
97-
this.protocol.encode(request)
98-
);
95+
const encoded = this.protocol.encode(request);
96+
if (this.registered || request.type === RequestTypes.ModuleRegistration) {
97+
await this.connection.send(
98+
encoded
99+
);
100+
} else {
101+
if (this.messagBuffer) {
102+
this.messagBuffer = Buffer.concat([this.messagBuffer, encoded])
103+
} else {
104+
this.messagBuffer = encoded;
105+
}
106+
}
99107

100108
return new Promise((resolve, reject) => {
101109
this.requests[request.requestId] = (response: any) => {
@@ -113,7 +121,6 @@ export default class GothamModule {
113121
let value;
114122
switch (response.type) {
115123
case ResponseTypes.ModuleRegistered: {
116-
this.registered = true;
117124
value = true;
118125
break;
119126
}
@@ -159,7 +166,7 @@ export default class GothamModule {
159166
}
160167
this.sendRequest({
161168
requestId: request.requestId,
162-
type: 'functionResponse',
169+
type: ResponseTypes.FunctionResponse,
163170
data: res || {}
164171
});
165172
return true;
@@ -172,7 +179,12 @@ export default class GothamModule {
172179
private async executeHookTriggered(request: TriggerHookRequest) {
173180
if (request.hook) {
174181
// Hook triggered by another module.
175-
if (this.hookListeners[request.hook]) {
182+
if (request.hook === `gotham.activated`) {
183+
this.registered = true;
184+
if (this.messagBuffer) {
185+
this.connection.send(this.messagBuffer);
186+
}
187+
} else if (this.hookListeners[request.hook]) {
176188
for (const listener of this.hookListeners[request.hook]) {
177189
listener();
178190
}

src/models/messages.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1-
import {
2-
ModuleRegistration,
3-
DeclareFunction,
4-
FunctionCall,
5-
RegisterHook,
6-
TriggerHook,
7-
ModuleRegistered,
8-
FunctionResponse,
9-
HookRegistered,
10-
HookTriggered,
11-
FunctionDeclared
12-
} from '../utils/constants';
13-
141
interface BaseMessage {
152
requestId: string;
16-
type: string;
3+
type: number;
174
}
185

196
export interface RegisterModuleRequest extends BaseMessage {
20-
type: ModuleRegistration;
217
moduleId: string;
228
version: string;
239
dependencies: {
@@ -26,50 +12,41 @@ export interface RegisterModuleRequest extends BaseMessage {
2612
}
2713

2814
export interface DeclareFunctionRequest extends BaseMessage {
29-
type: DeclareFunction;
3015
function: string;
3116
}
3217

3318
export interface FunctionCallRequest extends BaseMessage {
34-
type: FunctionCall;
3519
function: string;
3620
arguments: {
3721
[type: string]: any
3822
};
3923
}
4024

4125
export interface RegisterHookRequest extends BaseMessage {
42-
type: RegisterHook;
4326
hook: string;
4427
}
4528

4629
export interface TriggerHookRequest extends BaseMessage {
47-
type: TriggerHook;
4830
hook: string;
4931
}
5032

5133
export interface RegisterModuleResponse extends BaseMessage {
52-
type: ModuleRegistered;
5334
test: string;
5435
}
5536

5637
export interface FunctionCallResponse extends BaseMessage {
57-
type: FunctionResponse;
5838
data: any;
5939
}
6040

6141
export interface ListenHookResponse extends BaseMessage {
62-
type: HookRegistered;
6342
}
6443

6544
export interface TriggerHookResponse extends BaseMessage {
66-
type: HookTriggered;
6745
hook: string;
6846
data?: any;
6947
}
7048

7149
export interface DeclareFunctionResponse extends BaseMessage {
72-
type: FunctionDeclared;
7350
function: string;
7451
}
7552

src/protocol/base-protocol.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { RequestTypes } from '../utils/constants';
21
import {
32
RegisterHookRequest,
43
TriggerHookRequest,
@@ -7,6 +6,7 @@ import {
76
RegisterModuleRequest,
87
GothamMessage
98
} from '../models/messages';
9+
import { RequestTypes } from '../utils/constants';
1010

1111
export abstract class BaseProtocol {
1212
private moduleId = 'undefined';
@@ -27,7 +27,7 @@ export abstract class BaseProtocol {
2727
this.moduleId = moduleId;
2828
return {
2929
requestId: this.generateRequestId(),
30-
type: 'moduleRegistration',
30+
type: RequestTypes.ModuleRegistration,
3131
moduleId: moduleId,
3232
version: version,
3333
dependencies: deps
@@ -37,23 +37,23 @@ export abstract class BaseProtocol {
3737
public registerHook(hook: string): RegisterHookRequest {
3838
return {
3939
requestId: this.generateRequestId(),
40-
type: 'registerHook',
40+
type: RequestTypes.RegisterHook,
4141
hook
4242
};
4343
}
4444

4545
public triggerHook(hook: string): TriggerHookRequest {
4646
return {
4747
requestId: this.generateRequestId(),
48-
type: 'triggerHook',
48+
type: RequestTypes.TriggerHook,
4949
hook
5050
};
5151
}
5252

5353
public declareFunction(functionName: string): DeclareFunctionRequest {
5454
return {
5555
requestId: this.generateRequestId(),
56-
type: 'declareFunction',
56+
type: RequestTypes.DeclareFunction,
5757
function: functionName
5858
};
5959
}
@@ -64,7 +64,7 @@ export abstract class BaseProtocol {
6464
): FunctionCallRequest {
6565
return {
6666
requestId: this.generateRequestId(),
67-
type: 'functionCall',
67+
type: RequestTypes.FunctionCall,
6868
function: functionName,
6969
arguments: args
7070
};

src/utils/constants.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
export type ModuleRegistration = 'moduleRegistration';
2-
export type FunctionCall = 'functionCall';
3-
export type RegisterHook = 'registerHook';
4-
export type TriggerHook = 'triggerHook';
5-
export type DeclareFunction = 'declareFunction';
1+
export type ModuleRegistration = 1;
2+
export type FunctionCall = 3;
3+
export type RegisterHook = 5;
4+
export type TriggerHook = 7;
5+
export type DeclareFunction = 9;
66

7-
export type ModuleRegistered = 'moduleRegistered';
8-
export type FunctionResponse = 'functionResponse';
9-
export type HookRegistered = 'hookRegistered';
10-
export type HookTriggered = 'hookTriggered';
11-
export type FunctionDeclared = 'functionDeclared';
7+
export type ModuleRegistered = 2;
8+
export type FunctionResponse = 4;
9+
export type HookRegistered = 6;
10+
export type HookTriggered = 8;
11+
export type FunctionDeclared = 10;
1212

1313
export const RequestTypes = {
14-
ModuleRegistration: 'moduleRegistration',
15-
FunctionCall: 'functionCall',
16-
RegisterHook: 'registerHook',
17-
TriggerHook: 'triggerHook',
18-
DeclareFunction: 'declareFunction'
14+
ModuleRegistration: 1,
15+
FunctionCall: 3,
16+
RegisterHook: 5,
17+
TriggerHook: 7,
18+
DeclareFunction: 9
1919
};
2020

2121
export const ResponseTypes = {
22-
ModuleRegistered: 'moduleRegistered',
23-
FunctionResponse: 'functionResponse',
24-
HookRegistered: 'hookRegistered',
25-
HookTriggered: 'hookTriggered',
26-
FunctionDeclared: 'functionDeclared'
22+
ModuleRegistered: 2,
23+
FunctionResponse: 4,
24+
HookRegistered: 6,
25+
HookTriggered: 8,
26+
FunctionDeclared: 10
2727
};

test/gotham-node-test.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { GothamConnection } from "../src/GothamConnection";
1+
import BaseConnection from "../src/connection/base-connection";
22
import GothamModule from "../src/gotham-node";
33
import { SinonSpy } from "sinon";
44
import { DummyGothamConnection } from "./helpers";
55

66
interface GothamTest {
7-
connection: GothamConnection;
7+
connection: BaseConnection;
88
}
99

1010
declare module 'mocha' {

test/gotham-node.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ makeConnectionTests('Initalize Tests', function () {
4545
this.test.module.declareFunction('test', () => { })
4646
).to.be.rejectedWith(Error);
4747
await expect(
48-
this.test.module.functionCall('test', {})
48+
this.test.module.callFunction('test', {})
4949
).to.be.rejectedWith(Error);
5050
await expect(
5151
this.test.module.registerHook('test', () => { })
@@ -82,7 +82,7 @@ makeConnectionTests('Test if requests constructed correctly', function () {
8282

8383

8484
it('functionCall with empty args', function () {
85-
this.test.module.functionCall('module.test_fn');
85+
this.test.module.callFunction('module.test_fn');
8686
const message = this.test.getLatestSent();
8787
expect(message).excluding('requestId').to.deep.equal({
8888
function: "module.test_fn",
@@ -92,7 +92,7 @@ makeConnectionTests('Test if requests constructed correctly', function () {
9292
});
9393

9494
it('functionCall with args', function () {
95-
this.test.module.functionCall('module.test_fn', {
95+
this.test.module.callFunction('module.test_fn', {
9696
a: 1,
9797
b: 2
9898
});
@@ -188,7 +188,7 @@ makeConnectionTests('Test if responses from gotham parsed correctly', async func
188188
});
189189

190190
it('functionResponse', async function() {
191-
const p = this.test.module.functionCall('module.test_fn');
191+
const p = this.test.module.callFunction('module.test_fn');
192192
await sleep(0);
193193
const requestId = this.test.getLatestSent().requestId;
194194

test/helpers.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
import { GothamConnection } from "../src/GothamConnection";
1+
import BaseConnection, { OnDataHandler } from "../src/connection/base-connection";
22

3-
import { dataListenerFn } from "../src/types/protocol";
43

54
import sinon from "sinon";
65

76
import GothamModule from "../src/gotham-node";
7+
import { JsonProtocol } from "../src/protocol/json-protocol";
88

99
export const sleep = (t: number) => new Promise((r) => setTimeout(r, t));
1010

11-
export class DummyGothamConnection extends GothamConnection {
12-
dataListener: dataListenerFn;
13-
setupDataListener(fn: dataListenerFn) {
14-
this.dataListener = fn;
11+
export class DummyGothamConnection extends BaseConnection {
12+
dataListener: OnDataHandler;
13+
async send(request: Buffer) {
14+
}
15+
16+
async setupConnection() {
17+
18+
}
19+
20+
async closeConnection() {
21+
1522
}
1623

1724
sendResponse(message: any) {
@@ -33,7 +40,8 @@ export function makeConnectionTests(name: string, tests: Function, initalizeModu
3340
}
3441
sinon.replace(this.currentTest.conn, 'send', this.currentTest.sendFunc);
3542
this.currentTest.module = new GothamModule(
36-
this.currentTest.conn
43+
this.currentTest.conn,
44+
new JsonProtocol(),
3745
);
3846

3947
if (initalizeModule) {

0 commit comments

Comments
 (0)