Added jsonl output to stream in order to better delimit on client.

This commit is contained in:
k 2017-04-17 19:01:33 -04:00
parent 230f3681bd
commit 9a7b39230f
3 changed files with 27 additions and 1 deletions

View File

@ -609,7 +609,7 @@ WalletService.prototype._endpointGetTransactions = function() {
var rs = new Readable;
transactions.forEach(function(transaction) {
rs.push(JSON.stringify(self._formatTransaction(transaction)));
rs.push(utils.toJSONL(self._formatTransaction(transaction)));
});
rs.push(null);
@ -626,6 +626,7 @@ WalletService.prototype._formatTransactions = function(txs) {
WalletService.prototype._formatTransaction = function(tx) {
var obj = tx.toObject();
//jsonl parser will not allow newline characters here
for(var i = 0; i < tx.inputs.length; i++) {
obj.inputs[i].inputSatoshis = tx.__inputValues[i];
}

View File

@ -752,4 +752,14 @@ exports.hasRequiredArgsForEncoding = function(args) {
return true;
};
exports.toJSONL = function(obj) {
//this should be a standard obj that JSON.stringify will handle
//general newlines within key values or data values are not permitted
//this is intended to be used for bitcoin tx's that don't have newlines
//within keys or values themselves
var str = JSON.stringify(obj);
str = str.replace(/\n/g, '');
return str + '\n';
};
module.exports = exports;

View File

@ -0,0 +1,15 @@
'use strict';
var should = require('chai').should();
var utils = require('../../../lib/services/wallet-api/utils');
describe('Wallet-Api service utils', function() {
it('should create jsonl from obj', function() {
var obj = {
1: 'test',
'foo bar': 'test1',
array: [1,2,'test']
};
utils.toJSONL(obj).should.equal('{"1":"test","foo bar":"test1","array":[1,2,"test"]}\n');
});
});