Skip to content

Commit

Permalink
Fixed long-response bug in IpcProvider (#384).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jan 21, 2019
1 parent eac0805 commit 5f01321
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src.ts/providers/ipc-provider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use strict";

import net from 'net';

Expand All @@ -10,12 +11,15 @@ import { Networkish } from '../utils/networks';

import * as errors from '../errors';


export class IpcProvider extends JsonRpcProvider {
readonly path: string;

constructor(path: string, network?: Networkish) {
if (path == null) {
errors.throwError('missing path', errors.MISSING_ARGUMENT, { arg: 'path' });
errors.throwError('missing path', errors.MISSING_ARGUMENT, {
argument: 'path'
});
}

super('ipc://' + path, network);
Expand All @@ -32,18 +36,25 @@ export class IpcProvider extends JsonRpcProvider {
// advantage we are aiming for now is security. This simplifies
// multiplexing requests (since we do not need to multiplex).

var payload = JSON.stringify({
let payload = JSON.stringify({
method: method,
params: params,
id: 42,
jsonrpc: "2.0"
});

return new Promise((resolve, reject) => {
var stream = net.connect(this.path);
stream.on('data', function(data) {
let response = Buffer.alloc(0);

let stream = net.connect(this.path);

stream.on('data', (data) => {
response = Buffer.concat([ response, data ]);
});

stream.on("end", () => {
try {
resolve(JSON.parse(data.toString('utf8')).result);
resolve(JSON.parse(response.toString('utf8')).result);
// @TODO: Better pull apart the error
stream.destroy();
} catch (error) {
Expand All @@ -52,14 +63,11 @@ export class IpcProvider extends JsonRpcProvider {
}
});

stream.on('end', function() {
stream.destroy();
});

stream.on('error', function(error) {
stream.on('error', (error) => {
reject(error);
stream.destroy();
});

stream.write(payload);
stream.end();
});
Expand Down

0 comments on commit 5f01321

Please sign in to comment.