Skip to content

Commit

Permalink
Add hardhat_closeJsonRpcServer method
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Oct 14, 2020
1 parent 3a9ece5 commit 7bbcfa0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {
// tslint:disable only-hardhat-error

export default class JsonRpcHandler {
constructor(private readonly _provider: EIP1193Provider) {}
constructor(
private readonly _provider: EIP1193Provider,
private readonly _onServerClose: () => Promise<void>
) {}

public handleHttp = async (req: IncomingMessage, res: ServerResponse) => {
this._setCorsHeaders(res);
Expand Down Expand Up @@ -48,9 +51,18 @@ export default class JsonRpcHandler {
return;
}

const rpcResp = await this._handleSingleRequest(jsonHttpRequest);

this._sendResponse(res, rpcResp);
// hardhat_closeJsonRpcRequest is a special method that we have to handle carefully
if (jsonHttpRequest.method === "hardhat_closeJsonRpcRequest") {
this._sendResponse(res, {
jsonrpc: "2.0",
id: jsonHttpRequest.id,
result: true,
});
await this._onServerClose();
} else {
const rpcResp = await this._handleSingleRequest(jsonHttpRequest);
this._sendResponse(res, rpcResp);
}
};

public handleWs = async (ws: WebSocket) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class JsonRpcServer implements IJsonRpcServer {
constructor(config: JsonRpcServerConfig) {
this._config = config;

const handler = new JsonRpcHandler(config.provider);
const handler = new JsonRpcHandler(config.provider, () => this.close());

this._httpServer = http.createServer();
this._wsServer = new WSServer({
Expand Down

0 comments on commit 7bbcfa0

Please sign in to comment.