Skip to content

Commit

Permalink
mod
Browse files Browse the repository at this point in the history
  • Loading branch information
you21979 committed Jul 10, 2017
1 parent d8e59aa commit 0ac3165
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
4 changes: 4 additions & 0 deletions example/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
36 changes: 20 additions & 16 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down

0 comments on commit 0ac3165

Please sign in to comment.