Skip to content

Commit 0b1c8a4

Browse files
committed
feat: Implement socket connection logic
1 parent 615630d commit 0b1c8a4

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

src/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Protocol, Dependency, FnArgs } from "./protocol/protocol";
2+
import { GothamConnection } from './protocol/GothamConnection';
23

34
export class Gotham {
45
moduleId: string;
56
protocol: Protocol
6-
constructor() {
7-
this.protocol = new Protocol();
7+
constructor(connection: GothamConnection) {
8+
this.protocol = new Protocol(connection);
89
}
910
async initialize(moduleId: string, version: string, deps: Dependency) {
1011
return await this.protocol.sendRequest(

src/protocol/GothamConnection.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
1-
class GothamConnection {
1+
import net from 'net';
2+
3+
type dataListenerFn = (data: any) => any;
4+
export abstract class GothamConnection {
5+
dataListener: dataListenerFn;
26
constructor() {
3-
// Connect to socket
7+
this.setupConnection();
8+
}
9+
10+
abstract setupConnection();
11+
abstract setupDataListener(dataListener: dataListenerFn);
12+
abstract async send(message: object): Promise<void>;
13+
}
14+
15+
16+
17+
export class SocketConnection extends GothamConnection {
18+
client: net.Socket;
19+
sockPath: string;
20+
constructor(sockPath: string, dataListener: dataListenerFn) {
21+
super();
22+
this.sockPath = sockPath;
23+
}
24+
25+
setupConnection() {
26+
this.client = net.createConnection(this.sockPath);
27+
this.client.on('connect', () => {
28+
console.log(`Connected to ${this.sockPath}!`);
29+
});
430
}
531

6-
async send(_message: object) {
7-
// Send message over socket
32+
setupDataListener(dataListener: dataListenerFn) {
33+
this.dataListener = dataListener;
34+
this.client.on('data', dataListener);
835
}
936

10-
async receive() {
11-
// Receive a message from the socket
37+
async send(message: object) {
38+
this.client.write(JSON.stringify(message) + '\n');
1239
}
13-
}
40+
}

src/protocol/protocol.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { constants } from "../constants";
2-
import { ConnectOpts } from 'net';
3-
import { ConnectionOptions } from 'tls';
2+
import { GothamConnection } from './GothamConnection';
43

54
export interface Dependency {
65
[type: string]: string;
@@ -87,6 +86,13 @@ export class Protocol {
8786

8887
private connection: GothamConnection;
8988

89+
constructor(connection: GothamConnection) {
90+
this.connection = connection;
91+
this.connection.setupDataListener((data) => {
92+
this.parseResponse(data);
93+
});
94+
}
95+
9096
async sendRequest(obj: any) {
9197
await this.connection.send(obj);
9298
return new Promise((resolve, reject) => {
@@ -184,7 +190,7 @@ export class Protocol {
184190
res = false;
185191
}
186192

187-
// Call the callback proviced while sending the request
193+
// Call the callback provided while sending the request
188194
if (this.requests[obj.requestId]) {
189195
this.requests[obj.requestId](res);
190196
delete this.requests[obj.requestId];

tsconfig.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
"forceConsistentCasingInFileNames": true,
1313
"noFallthroughCasesInSwitch": true,
1414
"noImplicitReturns": true,
15-
"noUnusedLocals": true,
16-
"noUnusedParameters": true,
1715
"noImplicitAny": false,
1816
"noImplicitThis": false,
1917
"strictNullChecks": false

0 commit comments

Comments
 (0)