Skip to content

Commit

Permalink
Add JSON receive buffers
Browse files Browse the repository at this point in the history
- Added a receive buffer so that JSON messages are no longer required to arrive all at once, which is not possible with large amounts of data (i.e. block list)
- Upgrade mint-socket ^1.1.0 and use JsonBuffer
- Fixed comment
  • Loading branch information
JCThePants committed May 21, 2020
1 parent 7af02f4 commit bcc3337
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
20 changes: 15 additions & 5 deletions libs/class.BeamExplorerClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const
http = require('http'),
https = require('https'),
precon = require('@mintpond/mint-precon'),
JsonBuffer = require('@mintpond/mint-socket').JsonBuffer,
pu = require('@mintpond/mint-utils').prototypes;


Expand Down Expand Up @@ -403,12 +404,21 @@ class BeamExplorerClient extends EventEmitter {
return;
}

res.setEncoding('utf8');
res.on('data', chunk => {
const json = chunk.toString();
const parsed = JSON.parse(json);
callback && callback(null, parsed);
const buffer = new JsonBuffer();
const messagesArr = [];
const timeout = setTimeout(() => {
callback && callback(new Error(`Timed out: ${path}`));
callback = null;
}, 20000);

res.on('data', chunk => {
buffer.append(chunk, messagesArr);

if (messagesArr.length > 0) {
clearTimeout(timeout);
callback && callback(null, messagesArr[0]);
callback = null;
}
});
});

Expand Down
33 changes: 29 additions & 4 deletions libs/class.BeamWalletClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const
http = require('http'),
https = require('https'),
precon = require('@mintpond/mint-precon'),
JsonBuffer = require('@mintpond/mint-socket').JsonBuffer,
mu = require('@mintpond/mint-utils'),
pu = require('@mintpond/mint-utils').prototypes,
BeamAddressExpire = require('./const.BeamAddressExpire'),
Expand Down Expand Up @@ -345,8 +346,17 @@ class BeamWalletClient extends EventEmitter {
* @param args
* @param args.txId {string}
* @param args.callback {function(err:*, {
* comment: string,
* create_time: number,
* fee: number,
* income: boolean,
* kernel: string,
* receiver: string,
* sender: string,
* status: number,
* status_string: string,
* txId: string,
* asset_id: number,
* value: number
* })}
*/
txStatus(args) {
Expand Down Expand Up @@ -699,10 +709,24 @@ class BeamWalletClient extends EventEmitter {
return;
}

res.setEncoding('utf8');
const buffer = new JsonBuffer();
const messagesArr = [];
const timeout = setTimeout(() => {
callback && callback(new Error(`Timed out.`));
callback = null;
}, 20000);

res.on('data', chunk => {
const json = chunk.toString();
const parsed = JSON.parse(json);

buffer.append(chunk, messagesArr);

if (!messagesArr.length)
return;

clearTimeout(timeout);

const parsed = messagesArr[0];

callback && callback(parsed.error ? {
...parsed.error,
toString() { return `ERROR: ${parsed.error.code}: ${parsed.error.message} ${parsed.error.data}`},
Expand All @@ -714,6 +738,7 @@ class BeamWalletClient extends EventEmitter {
};
}
} : null, parsed.result);

callback = null;
});
});
Expand Down
12 changes: 1 addition & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"dependencies": {
"@mintpond/mint-precon": "^1.0.0",
"@mintpond/mint-socket": "^1.0.0",
"@mintpond/mint-socket": "^1.1.0",
"@mintpond/mint-utils": "^2.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit bcc3337

Please sign in to comment.