Sitelet https://github.com/open-rpc/client-js/commit/ba1e23b
Skip to content

Commit ba1e23b

Browse files
committed
fix: event emitter transport
1 parent 8bcf352 commit ba1e23b

4 files changed

Lines changed: 92 additions & 63 deletions

File tree

‎src/RequestManager.test.ts‎

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,65 @@
11
import RequestManager from "./RequestManager";
22
import EventEmitterTransport from "./transports/EventEmitterTransport";
3+
import { EventEmitter } from "events";
34

45
describe("client-js", () => {
56
it("can be constructed", () => {
6-
const transport = new EventEmitterTransport("foo://unique-uri");
7+
const emitter = new EventEmitter();
8+
const transport = new EventEmitterTransport(emitter, "from1", "to1");
79
const c = new RequestManager([transport]);
810
expect(!!c).toEqual(true);
911
});
1012

1113
it("has a request method that returns a promise", () => {
12-
const transport = new EventEmitterTransport("foo://unique-uri");
14+
const emitter = new EventEmitter();
15+
const transport = new EventEmitterTransport(emitter, "from1", "to1");
1316
const c = new RequestManager([transport]);
1417
expect(typeof c.request).toEqual("function");
1518
expect(typeof c.request("my_method", null).then).toEqual("function");
1619
});
1720

1821
it("can connect", () => {
19-
const transport = new EventEmitterTransport("foo://unique-uri");
22+
const emitter = new EventEmitter();
23+
const transport = new EventEmitterTransport(emitter, "from1", "to1");
2024
const c = new RequestManager([transport]);
2125
return c.connect();
2226
});
2327

2428
it("can close", () => {
25-
const transport = new EventEmitterTransport("foo://unique-uri");
29+
const emitter = new EventEmitter();
30+
const transport = new EventEmitterTransport(emitter, "from1", "to1");
2631
const c = new RequestManager([transport]);
2732
c.close();
2833
});
2934

3035
it("can send a request", (done) => {
31-
const transport = new EventEmitterTransport("foo://unique-uri");
36+
const emitter = new EventEmitter();
37+
const transport = new EventEmitterTransport(emitter, "from1", "to1");
38+
const serverTransport = new EventEmitterTransport(emitter, "to1", "from1");
3239
const c = new RequestManager([transport]);
3340
c.connect();
3441
transport.onData((data: any) => {
3542
const d = JSON.parse(data);
36-
expect(d.method).toEqual("foo");
43+
expect(d.foo).toEqual("bar");
3744
done();
3845
});
3946
c.request("foo", []);
47+
serverTransport.sendData(JSON.stringify({ foo: "bar" }));
4048
});
4149

4250
it("can error on batchng a request", async () => {
43-
const transport = new EventEmitterTransport("foo://unique-uri");
51+
const emitter = new EventEmitter();
52+
const transport = new EventEmitterTransport(emitter, "from1", "to1");
4453
const c = new RequestManager([transport]);
4554
return c.connect().then(() => {
4655
expect(() => c.stopBatch()).toThrow();
4756
});
4857
});
4958

5059
it("can return errors on batch requests", async () => {
51-
const transport = new EventEmitterTransport("foo://unique-uri");
52-
transport.sendData = (data) => {
53-
const result = JSON.stringify([
54-
{
55-
jsonrpc: "2.0",
56-
id: "0",
57-
error: {
58-
code: 509,
59-
message: "too much 509",
60-
data: {
61-
test: "data",
62-
},
63-
},
64-
},
65-
{
66-
jsonrpc: "2.0",
67-
id: "1",
68-
result: "bar",
69-
},
70-
]);
71-
transport.connection.emit("message", result);
72-
};
60+
const emitter = new EventEmitter();
61+
const transport = new EventEmitterTransport(emitter, "from1", "to1");
62+
const serverTransport = new EventEmitterTransport(emitter, "to1", "from1");
7363

7464
const c = new RequestManager([transport]);
7565
await c.connect();
@@ -85,15 +75,49 @@ describe("client-js", () => {
8575
test: "data",
8676
},
8777
});
78+
serverTransport.sendData(JSON.stringify([
79+
{
80+
jsonrpc: "2.0",
81+
id: "0",
82+
error: {
83+
code: 509,
84+
message: "too much 509",
85+
data: {
86+
test: "data",
87+
},
88+
},
89+
},
90+
{
91+
jsonrpc: "2.0",
92+
id: "1",
93+
result: "bar",
94+
},
95+
]));
8896
expect(requests[1]).resolves.toEqual("bar");
8997
c.stopBatch();
9098
c.close();
9199
});
92100

93-
it("can batch a request", async () => {
94-
const transport = new EventEmitterTransport("foo://unique-uri");
95-
transport.sendData = (data) => {
96-
const result = JSON.stringify([
101+
it("can batch a request", (done) => {
102+
const emitter = new EventEmitter();
103+
const transport = new EventEmitterTransport(emitter, "from1", "to1");
104+
const serverTransport = new EventEmitterTransport(emitter, "to1", "from1");
105+
106+
const c = new RequestManager([transport]);
107+
c.connect().then(() => {
108+
c.startBatch();
109+
const requests = [
110+
c.request("foo", []),
111+
c.request("foo", []),
112+
];
113+
c.stopBatch();
114+
Promise.all(requests).then(([a, b]) => {
115+
expect(a).toEqual("foo");
116+
expect(b).toEqual("bar");
117+
c.close();
118+
done();
119+
});
120+
serverTransport.sendData(JSON.stringify([
97121
{
98122
jsonrpc: "2.0",
99123
id: 0,
@@ -104,26 +128,13 @@ describe("client-js", () => {
104128
id: 1,
105129
result: "bar",
106130
},
107-
]);
108-
transport.connection.emit("message", result);
109-
};
110-
111-
const c = new RequestManager([transport]);
112-
await c.connect();
113-
c.startBatch();
114-
const requests = [
115-
c.request("foo", []),
116-
c.request("foo", []),
117-
];
118-
c.stopBatch();
119-
const [a, b] = await Promise.all(requests);
120-
expect(a).toEqual("foo");
121-
expect(b).toEqual("bar");
122-
c.close();
131+
]));
132+
});
123133
});
124134

125135
it("can send a request and error", async () => {
126-
const transport = new EventEmitterTransport("foo://unique-uri");
136+
const emitter = new EventEmitter();
137+
const transport = new EventEmitterTransport(emitter, "from1", "to1");
127138
const c = new RequestManager([transport]);
128139
transport.onData = (fn) => {
129140
transport.connection.on("message", () => {
@@ -152,7 +163,8 @@ describe("client-js", () => {
152163

153164
describe("stopBatch", () => {
154165
it("does nothing if the batch is empty", () => {
155-
const transport = new EventEmitterTransport("foo://unique-uri");
166+
const emitter = new EventEmitter();
167+
const transport = new EventEmitterTransport(emitter, "from1", "to1");
156168
transport.sendData = jest.fn();
157169
const c = new RequestManager([transport]);
158170
c.startBatch();

‎src/index.test.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11
import Client from ".";
22
import RequestManager from "./RequestManager";
33
import EventEmitterTransport from "./transports/EventEmitterTransport";
4+
import { EventEmitter } from "events";
45

56
jest.mock("./RequestManager");
67

78
const mockedRequestManager = RequestManager as jest.Mock<RequestManager>;
89
describe("client-js", () => {
910
it("can be constructed", () => {
10-
const c = new Client(new RequestManager([new EventEmitterTransport("foo://unique")]));
11+
const emitter = new EventEmitter();
12+
const c = new Client(new RequestManager([new EventEmitterTransport(emitter, "from1", "to1")]));
1113
expect(!!c).toEqual(true);
1214
});
1315

1416
it("has a request method that returns a promise", () => {
15-
const c = new Client(new RequestManager([new EventEmitterTransport("foo://unique")]));
17+
const emitter = new EventEmitter();
18+
const c = new Client(new RequestManager([new EventEmitterTransport(emitter, "from1", "to1")]));
1619
expect(typeof c.request).toEqual("function");
1720
expect(typeof c.request("my_method", null).then).toEqual("function");
1821
});
1922

2023
describe("startBatch", () => {
2124
it("calls the requestManager.startBatch", () => {
22-
const rm = new mockedRequestManager([new EventEmitterTransport("foo://unique")]);
25+
const emitter = new EventEmitter();
26+
const rm = new mockedRequestManager([new EventEmitterTransport(emitter, "from1", "to1")]);
2327
const c = new Client(rm);
2428
c.startBatch();
2529
expect(mockedRequestManager.mock.instances[0].startBatch).toHaveBeenCalled();
2630
});
2731
});
2832

2933
describe("stopBatch", () => {
30-
const rm = new RequestManager([new EventEmitterTransport("foo://unique")]);
34+
const emitter = new EventEmitter();
35+
const rm = new mockedRequestManager([new EventEmitterTransport(emitter, "from1", "to1")]);
3136
const c = new Client(rm);
3237
c.startBatch();
3338
c.stopBatch();
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
import EventEmitterTransport from "./EventEmitterTransport";
2+
import { EventEmitter } from "events";
23

34
describe("EventEmitterTransport", () => {
45
it("can connect", () => {
5-
const eventEmitterTransport = new EventEmitterTransport("foo://bar");
6+
const emitter = new EventEmitter();
7+
const eventEmitterTransport = new EventEmitterTransport(emitter, "foo://in", "foo://out");
68
eventEmitterTransport.connect();
79
});
810
it("can close", () => {
9-
const eventEmitterTransport = new EventEmitterTransport("foo://bar");
11+
const emitter = new EventEmitter();
12+
const reqUri = "from";
13+
const resUri = "to";
14+
const eventEmitterTransport = new EventEmitterTransport(emitter, reqUri, resUri);
1015
eventEmitterTransport.close();
1116
});
1217
it("can send and receive data", (done) => {
13-
const eventEmitterTransport = new EventEmitterTransport("foo://bar");
18+
const emitter = new EventEmitter();
19+
const eventEmitterTransport = new EventEmitterTransport(emitter, "from1", "to1");
1420
eventEmitterTransport.onData((data: any) => {
1521
const d = JSON.parse(data);
1622
expect(d.foo).toEqual("bar");
1723
done();
1824
});
19-
eventEmitterTransport.sendData(JSON.stringify({foo: "bar"}));
25+
26+
const eventEmitterServerTransport = new EventEmitterTransport(emitter, "to1", "from1");
27+
eventEmitterServerTransport.sendData(JSON.stringify({foo: "bar"}));
2028
});
2129
});

‎src/transports/EventEmitterTransport.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ import ITransport from "./Transport";
33

44
class EventEmitterTransport implements ITransport {
55
public connection: EventEmitter;
6-
constructor(uri: string) {
7-
this.connection = new EventEmitter();
6+
private reqUri: string;
7+
private resUri: string;
8+
constructor(emitter: EventEmitter, reqUri: string, resUri: string) {
9+
this.connection = emitter;
10+
this.reqUri = reqUri;
11+
this.resUri = resUri;
812
}
913
public connect(): Promise<any> {
1014
return Promise.resolve();
1115
}
1216
public onData(callback: (data: string) => any) {
13-
this.connection.addListener("message", (data: any) => {
17+
this.connection.on(this.reqUri, (data: any) => {
1418
callback(data);
1519
});
1620
}
1721
public sendData(data: string) {
18-
this.connection.emit("message", data);
22+
this.connection.emit(this.resUri, data);
1923
}
2024
public close() {
2125
this.connection.removeAllListeners();

0 commit comments

Comments
 (0)