-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: initial transport implementation
- Loading branch information
1 parent
991ef68
commit e5cb09f
Showing
12 changed files
with
353 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import RequestManager from "./RequestManager"; | ||
import EventEmitterTransport from "./transports/EventEmitterTransport"; | ||
|
||
describe("client-js", () => { | ||
it("can be constructed", () => { | ||
const transport = new EventEmitterTransport("foo://unique-uri"); | ||
const c = new RequestManager([transport]); | ||
expect(!!c).toEqual(true); | ||
}); | ||
|
||
it("has a request method that returns a promise", () => { | ||
const transport = new EventEmitterTransport("foo://unique-uri"); | ||
const c = new RequestManager([transport]); | ||
expect(typeof c.request).toEqual("function"); | ||
expect(typeof c.request("my_method", null).then).toEqual("function"); | ||
}); | ||
|
||
it("can connect", () => { | ||
const transport = new EventEmitterTransport("foo://unique-uri"); | ||
const c = new RequestManager([transport]); | ||
return c.connect(); | ||
}); | ||
|
||
it("can send a request", (done) => { | ||
const transport = new EventEmitterTransport("foo://unique-uri"); | ||
const c = new RequestManager([transport]); | ||
c.connect(); | ||
transport.onData((data: any) => { | ||
const d = JSON.parse(data); | ||
expect(d.method).toEqual("foo"); | ||
done(); | ||
}); | ||
c.request("foo", []); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import ITransport from "./transports/Transport"; | ||
let id = 1; | ||
|
||
/* | ||
** Naive Request Manager, only use 1st transport. | ||
* A more complex request manager could try each transport. | ||
* If a transport fails, or times out, move on to the next. | ||
*/ | ||
class RequestManager { | ||
public transports: ITransport[]; | ||
private requests: any; | ||
private connectPromise: Promise<any>; | ||
constructor(transports: ITransport[]) { | ||
this.transports = transports; | ||
this.requests = {}; | ||
this.connectPromise = this.connect(); | ||
} | ||
public connect() { | ||
const promises = this.transports.map((transport) => { | ||
return new Promise(async (resolve, reject) => { | ||
await transport.connect(); | ||
transport.onData((data: any) => { | ||
this.onData(data); | ||
}); | ||
resolve(); | ||
}); | ||
}); | ||
return Promise.all(promises); | ||
} | ||
public onData(data: string) { | ||
const parsedData = JSON.parse(data); | ||
if (typeof parsedData.result === "undefined") { | ||
return; | ||
} | ||
// call request callback for id | ||
this.requests[parsedData.id](parsedData); | ||
delete this.requests[parsedData.id]; | ||
} | ||
public async request(method: string, params: any): Promise<any> { | ||
await this.connectPromise; | ||
return new Promise((resolve, reject) => { | ||
const i = id++; | ||
// naively grab first transport and use it | ||
const transport = this.transports[0]; | ||
this.requests[i] = resolve; | ||
transport.sendData(JSON.stringify({ | ||
jsonrpc: "2.0", | ||
id: i, | ||
method, | ||
params, | ||
})); | ||
}); | ||
} | ||
public close() { | ||
this.transports.forEach((transport) => { | ||
transport.close(); | ||
}); | ||
} | ||
} | ||
|
||
export default RequestManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Client from "."; | ||
import RequestManager from "./RequestManager"; | ||
import Transport from "./transports/HTTPTransport"; | ||
|
||
const t = new Transport("http://localhost:8545"); | ||
|
||
const c = new Client(new RequestManager([t])); | ||
|
||
// make request for eth_blockNumber | ||
c.request("eth_blockNumber", []).then((b: any) => { | ||
console.log('in then', b); //tslint:disable-line | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
import Client from "."; | ||
import RequestManager from "./RequestManager"; | ||
import EventEmitterTransport from "./transports/EventEmitterTransport"; | ||
|
||
describe("client-js", () => { | ||
it("can be constructed", () => { | ||
const c = new Client(); | ||
const c = new Client(new RequestManager([new EventEmitterTransport("foo://unique")])); | ||
expect(!!c).toEqual(true); | ||
}); | ||
|
||
it("has a request method that returns a promise", () => { | ||
const c = new Client(); | ||
const c = new Client(new RequestManager([new EventEmitterTransport("foo://unique")])); | ||
console.log("test city before"); // tslint:disable-line | ||
expect(typeof c.request).toEqual("function"); | ||
expect(typeof c.request("my_method", null).then).toEqual("function"); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import EventEmitterTransport from "./EventEmitterTransport"; | ||
|
||
describe("EventEmitterTransport", () => { | ||
it("can connect", () => { | ||
const eventEmitterTransport = new EventEmitterTransport("foo://bar"); | ||
eventEmitterTransport.connect(); | ||
}); | ||
it("can close", () => { | ||
const eventEmitterTransport = new EventEmitterTransport("foo://bar"); | ||
eventEmitterTransport.close(); | ||
}); | ||
it("can send and receive data", (done) => { | ||
const eventEmitterTransport = new EventEmitterTransport("foo://bar"); | ||
eventEmitterTransport.onData((data: any) => { | ||
const d = JSON.parse(data); | ||
expect(d.foo).toEqual("bar"); | ||
done(); | ||
}); | ||
eventEmitterTransport.sendData(JSON.stringify({foo: "bar"})); | ||
}); | ||
}); |
Oops, something went wrong.