From 0ac31651001ca21d0d3fb391909548adc7fb4c27 Mon Sep 17 00:00:00 2001 From: you21979 Date: Mon, 10 Jul 2017 20:25:34 +0900 Subject: [PATCH] mod --- example/simple.js | 4 ++++ lib/util.js | 36 ++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/example/simple.js b/example/simple.js index fd36004..034378c 100644 --- a/example/simple.js +++ b/example/simple.js @@ -6,6 +6,10 @@ const main = async () => { try{ const ver = await ecl.server_version("2.7.11", "1.0") console.log(ver) + const balance = await ecl.blockchainAddress_getBalance("12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX") + console.log(balance) + const unspent = await ecl.blockchainAddress_listunspent("12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX") + console.log(unspent) }catch(e){ console.log(e) } diff --git a/lib/util.js b/lib/util.js index c035cb5..ea90476 100644 --- a/lib/util.js +++ b/lib/util.js @@ -9,24 +9,27 @@ const makeRequest = exports.makeRequest = (method, params, id) => { }) } -const recursiveParser = exports.recursiveParser = (n, buffer, callback) => { - const MAX_DEPTH = 20; - if(buffer.length === 0) { - return {code:0, buffer:buffer} - } - if(n > MAX_DEPTH) { - return {code:1, buffer:buffer} - } - const xs = buffer.split('\n') - if(xs.length === 1){ - return {code:0, buffer:buffer} +const createRecuesiveParser = exports.createRecuesiveParser = (max_depth, delimiter) => { + const MAX_DEPTH = max_depth + const DELIMITER = delimiter + const recursiveParser = (n, buffer, callback) => { + if(buffer.length === 0) { + return {code:0, buffer:buffer} + } + if(n > MAX_DEPTH) { + return {code:1, buffer:buffer} + } + const xs = buffer.split(DELIMITER) + if(xs.length === 1){ + return {code:0, buffer:buffer} + } + callback(xs.shift(), n) + return recursiveParser(n + 1, xs.join(DELIMITER), callback) } - const content = xs.shift() - - callback(content, n) - return recursiveParser(n + 1, xs.join('\n'), callback) + return recursiveParser } + const createPromiseResult = exports.createPromiseResult = (resolve, reject) => { return (err, result) => { if(err) reject(err) @@ -38,11 +41,12 @@ class MessageParser{ constructor(callback){ this.buffer = '' this.callback = callback + this.recursiveParser = createRecuesiveParser(20, '\n') } run(chunk){ this.buffer += chunk while(true){ - const res = recursiveParser(0, this.buffer, this.callback) + const res = this.recursiveParser(0, this.buffer, this.callback) this.buffer = res.buffer if(res.code === 0){ break;