ibm-blockchain-js
Advanced tools
Comparing version 1.1.2 to 1.2.0
231
index.js
@@ -9,6 +9,3 @@ 'use strict'; | ||
*******************************************************************************/ | ||
/* | ||
Updated: 03/15/2016 | ||
*/ | ||
//Load modules | ||
@@ -22,6 +19,11 @@ var fs = require('fs'); | ||
var AdmZip = require('adm-zip'); | ||
var logger = {log: console.log, error: console.error, debug: console.log, warn: console.log}; | ||
function ibc() {} | ||
ibc.chaincode = { //init it all | ||
function ibc(log_outputs) { | ||
if(log_outputs && log_outputs.info) logger.log = log_outputs.info; //send normal logs here | ||
if(log_outputs && log_outputs.error) logger.error = log_outputs.error; //send error logs here | ||
if(log_outputs && log_outputs.warn) logger.warn = log_outputs.warn; //send warn logs here | ||
if(log_outputs && log_outputs.debug) logger.debug = log_outputs.debug; //send debug logs here | ||
} | ||
ibc.chaincode = { | ||
query: {}, | ||
@@ -67,3 +69,3 @@ invoke: {}, | ||
if(errors.length > 0){ //check for input errors | ||
console.log('! [ibc-js] Input Error - ibc.load()', errors); | ||
logger.error('! [ibc-js] Input Error - ibc.load()', errors); | ||
if(cb) cb(helper.eFmt('load() input error', 400, errors)); | ||
@@ -123,3 +125,3 @@ return; //get out of dodge | ||
ibc.chaincode.details.users = []; | ||
console.log('[ibc-js] No membership users found after filtering, assuming this is a network w/o membership'); | ||
logger.log('[ibc-js] No membership users found after filtering, assuming this is a network w/o membership'); | ||
load_cc(); | ||
@@ -156,3 +158,3 @@ } | ||
if(errors.length > 0){ //check for input errors | ||
console.log('! [ibc-js] Input Error - ibc.load_chaincode()', errors); | ||
logger.error('! [ibc-js] Input Error - ibc.load_chaincode()', errors); | ||
if(cb) cb(helper.eFmt('load_chaincode() input error', 400, errors)); | ||
@@ -189,3 +191,3 @@ return; //get out of dodge | ||
else{ | ||
console.log('[ibc-js] Found chaincode in local file system'); | ||
logger.log('[ibc-js] Found chaincode in local file system'); | ||
fs.readdir(unzip_cc_dest, cb_got_names); //yeppers, go use it | ||
@@ -198,3 +200,3 @@ } | ||
function download_it(download_url){ | ||
console.log('[ibc-js] Downloading zip'); | ||
logger.log('[ibc-js] Downloading zip'); | ||
var file = fs.createWriteStream(zip_dest); | ||
@@ -205,3 +207,3 @@ https.get(download_url, function(response) { | ||
if(response.headers.status === '302 Found'){ | ||
console.log('redirect...', response.headers.location); | ||
logger.log('redirect...', response.headers.location); | ||
file.close(); | ||
@@ -215,3 +217,3 @@ download_it(response.headers.location); | ||
}).on('error', function(err) { | ||
console.log('! [ibc-js] Download error'); | ||
logger.error('! [ibc-js] Download error'); | ||
fs.unlink(zip_dest); //delete the file async | ||
@@ -224,6 +226,11 @@ if (cb) cb(helper.eFmt('doad_chaincode() download error', 500, err.message), ibc.chaincode); | ||
function cb_downloaded(){ | ||
console.log('[ibc-js] Unzipping zip'); | ||
var zip = new AdmZip(zip_dest); | ||
zip.extractAllTo(unzip_dest, /*overwrite*/true); | ||
console.log('[ibc-js] Unzip done'); | ||
logger.log('[ibc-js] Unzipping zip'); | ||
try{ | ||
var zip = new AdmZip(zip_dest); | ||
zip.extractAllTo(unzip_dest, /*overwrite*/true); | ||
} | ||
catch (err){ | ||
return cb(helper.eFmt('download repo error', 400, err), null); | ||
} | ||
logger.log('[ibc-js] Unzip done'); | ||
fs.readdir(unzip_cc_dest, cb_got_names); | ||
@@ -235,5 +242,5 @@ fs.unlink(zip_dest, function(err) {}); //remove zip file, never used again | ||
function cb_got_names(err, obj){ | ||
console.log('[ibc-js] Scanning files', obj); | ||
logger.log('[ibc-js] Scanning files', obj); | ||
var foundGo = false; | ||
if(err != null) console.log('! [ibc-js] fs readdir Error', err); | ||
if(err != null) logger.log('! [ibc-js] fs readdir Error', err); | ||
else{ | ||
@@ -262,3 +269,3 @@ for(var i in obj){ | ||
msg = 'did not find any *.go files, cannot continue'; | ||
console.log('! [ibc-js] Error - ', msg); | ||
logger.error('! [ibc-js] Error - ', msg); | ||
if(cb) return cb(helper.eFmt('load_chaincode() no chaincode', 400, msg), null); | ||
@@ -269,3 +276,3 @@ } | ||
if(!found_invoke){ //warning no run/invoke functions | ||
console.log('! [ibc-js] Warning - did not find any invoke functions in chaincode\'s "Invoke()", building a generic "invoke"'); | ||
logger.wartn('! [ibc-js] Warning - did not find any invoke functions in chaincode\'s "Invoke()", building a generic "invoke"'); | ||
build_invoke_func('invoke'); //this will make chaincode.invoke.invokce(args) | ||
@@ -275,3 +282,3 @@ } | ||
if(!found_query){ //warning no query functions | ||
console.log('! [ibc-js] Warning - did not find any query functions in chaincode\'s "Query()", building a generic "query"'); | ||
logger.warn('! [ibc-js] Warning - did not find any query functions in chaincode\'s "Query()", building a generic "query"'); | ||
build_query_func('query'); //this will make chaincode.query.query(args) | ||
@@ -281,3 +288,3 @@ } | ||
// Step 3. success! | ||
console.log('[ibc-js] load_chaincode() finished'); | ||
logger.log('[ibc-js] load_chaincode() finished'); | ||
ibc.chaincode.details.timestamp = Date.now(); | ||
@@ -292,5 +299,5 @@ ibc.chaincode.deploy = deploy; | ||
var ret = ''; | ||
if(file == null) console.log('! [ibc-js] fs readfile Error'); | ||
if(file == null) logger.error('! [ibc-js] fs readfile Error'); | ||
else{ | ||
console.log('[ibc-js] Parsing file for shim version'); | ||
logger.log('[ibc-js] Parsing file for shim version'); | ||
@@ -300,3 +307,3 @@ var shim_regex = /github.com\/\S+\/shim/g; //find chaincode's shim version | ||
if(result[0]){ | ||
console.log('[ibc-js] Found shim version:', result[0]); | ||
logger.log('[ibc-js] Found shim version:', result[0]); | ||
ret = result[0]; | ||
@@ -310,5 +317,5 @@ } | ||
function parse_for_invoke(name, str){ | ||
if(str == null) console.log('! [ibc-js] fs readfile Error'); | ||
if(str == null) logger.error('! [ibc-js] fs readfile Error'); | ||
else{ | ||
console.log('[ibc-js] Parsing file for invoke functions -', name); | ||
logger.log('[ibc-js] Parsing file for invoke functions -', name); | ||
@@ -361,5 +368,5 @@ // Step 2a. | ||
function parse_for_query(name, str){ | ||
if(str == null) console.log('! [ibc-js] fs readfile Error'); | ||
if(str == null) logger.error('! [ibc-js] fs readfile Error'); | ||
else{ | ||
console.log('[ibc-js] Parsing file for query functions -', name); | ||
logger.log('[ibc-js] Parsing file for query functions -', name); | ||
@@ -401,5 +408,5 @@ // Step 2e. | ||
function parse_for_init(name, str){ | ||
if(str == null) console.log('! [ibc-js] fs readfile Error'); | ||
if(str == null) logger.error('! [ibc-js] fs readfile Error'); | ||
else{ | ||
//console.log('[ibc-js] Parsing file for init functions -', name); | ||
//logger.log('[ibc-js] Parsing file for init functions -', name); | ||
@@ -443,9 +450,10 @@ // Step 2h. | ||
var errors = []; | ||
var quiet = true; | ||
var timeout = 60000; | ||
ibc.chaincode.details.options = {quiet: true, timeout: 60000, tls: true}; //defaults | ||
if(!arrayPeers || arrayPeers.constructor !== Array) errors.push('network input arg should be array of peer objects'); | ||
if(options){ | ||
if(options.quiet === true || options.quiet === false) quiet = options.quiet; //optional fields | ||
if(!isNaN(options.timeout)) timeout = Number(options.timeout); | ||
if(options.quiet === true || options.quiet === false) ibc.chaincode.details.options.quiet = options.quiet; //optional fields | ||
if(!isNaN(options.timeout)) ibc.chaincode.details.options.timeout = Number(options.timeout); | ||
if(options.tls === true || options.tls === false) ibc.chaincode.details.options.tls = options.tls; | ||
} | ||
@@ -465,3 +473,3 @@ | ||
if(errors.length > 0){ //check for input errors | ||
console.log('! [ibc-js] Input Error - ibc.network()', errors); | ||
logger.error('! [ibc-js] Input Error - ibc.network()', errors); | ||
} | ||
@@ -478,10 +486,9 @@ else{ | ||
id: arrayPeers[i].id, | ||
tls: true //default | ||
tls: ibc.chaincode.details.options.tls | ||
}; | ||
if(options && options.tls === false){ //if not tls rebuild a few things | ||
temp.tls = false; | ||
temp.name = arrayPeers[i].id.substring(pos) + '-' + arrayPeers[i].id.substring(0, 12) + '...:' + arrayPeers[i].api_port; | ||
} | ||
console.log('[ibc-js] Peer: ', temp.name); //print the friendly name | ||
logger.log('[ibc-js] Peer: ', temp.name); //print the friendly name | ||
ibc.chaincode.details.peers.push(temp); | ||
@@ -498,5 +505,5 @@ } | ||
ssl: ibc.chaincode.details.peers[0].tls, | ||
timeout: timeout, | ||
quiet: quiet | ||
}); | ||
timeout: ibc.chaincode.details.options.timeout, | ||
quiet: ibc.chaincode.details.options.quiet | ||
}, logger); | ||
} | ||
@@ -526,4 +533,4 @@ }; | ||
ssl: ibc.chaincode.details.peers[index].tls, | ||
timeout: 60000, | ||
quiet: true | ||
timeout: ibc.chaincode.details.options.timeout, | ||
quiet: ibc.chaincode.details.options.quiet | ||
}); | ||
@@ -544,3 +551,3 @@ ibc.selectedPeer = index; | ||
if(errors.length > 0){ //check for input errors | ||
console.log('[ibc-js] Input Error - ibc.save()', errors); | ||
logger.error('[ibc-js] Input Error - ibc.save()', errors); | ||
if(cb) cb(helper.eFmt('save() input error', 400, errors)); | ||
@@ -554,7 +561,7 @@ } | ||
if(e != null){ | ||
console.log('[ibc-js] ibc.save() error', e); | ||
logger.error('[ibc-js] ibc.save() error', e); | ||
if(cb) cb(helper.eFmt('save() fs write error', 500, e), null); | ||
} | ||
else { | ||
//console.log(' - saved ', dest); | ||
//logger.log(' - saved ', dest); | ||
if(cb) cb(null, null); | ||
@@ -570,3 +577,3 @@ } | ||
ibc.prototype.clear = function(cb){ | ||
console.log('[ibc-js] removing temp dir'); | ||
logger.log('[ibc-js] removing temp dir'); | ||
helper.removeThing(tempDirectory, cb); //remove everything in this directory | ||
@@ -582,7 +589,7 @@ }; | ||
options.success = function(statusCode, data){ | ||
console.log('[ibc-js] Chain Stats - success'); | ||
logger.log('[ibc-js] Chain Stats - success'); | ||
if(cb) cb(null, data); | ||
}; | ||
options.failure = function(statusCode, e){ | ||
console.log('[ibc-js] Chain Stats - failure:', statusCode, e); | ||
logger.error('[ibc-js] Chain Stats - failure:', statusCode, e); | ||
if(cb) cb(helper.eFmt('chain_stats() error', statusCode, e), null); | ||
@@ -599,7 +606,7 @@ }; | ||
options.success = function(statusCode, data){ | ||
console.log('[ibc-js] Block Stats - success'); | ||
logger.log('[ibc-js] Block Stats - success'); | ||
if(cb) cb(null, data); | ||
}; | ||
options.failure = function(statusCode, e){ | ||
console.log('[ibc-js] Block Stats - failure:', statusCode); | ||
logger.error('[ibc-js] Block Stats - failure:', statusCode); | ||
if(cb) cb(helper.eFmt('block_stats() error', statusCode, e), null); | ||
@@ -638,9 +645,9 @@ }; | ||
}; | ||
//console.log('body', body); | ||
//logger.log('body', body); | ||
options.success = function(statusCode, data){ | ||
console.log('[ibc-js] Read - success:', data); | ||
logger.log('[ibc-js] Read - success:', data); | ||
if(cb) cb(null, data.OK); | ||
}; | ||
options.failure = function(statusCode, e){ | ||
console.log('[ibc-js] Read - failure:', statusCode); | ||
logger.error('[ibc-js] Read - failure:', statusCode); | ||
if(cb) cb(helper.eFmt('read() error', statusCode, e), null); | ||
@@ -659,3 +666,3 @@ }; | ||
function register(index, enrollID, enrollSecret, maxRetry, attempt, cb){ | ||
console.log('[ibc-js] Registering ', ibc.chaincode.details.peers[index].name, ' w/enrollID - ' + enrollID); | ||
logger.log('[ibc-js] Registering ', ibc.chaincode.details.peers[index].name, ' w/enrollID - ' + enrollID); | ||
var options = { | ||
@@ -674,3 +681,3 @@ path: '/registrar', | ||
options.success = function(statusCode, data){ | ||
console.log('[ibc-js] Registration success x' + attempt + ' :', enrollID); | ||
logger.log('[ibc-js] Registration success x' + attempt + ' :', enrollID); | ||
ibc.chaincode.details.peers[index].enrollID = enrollID; //remember a valid enrollID for this peer | ||
@@ -680,5 +687,5 @@ if(cb) cb(null, data); | ||
options.failure = function(statusCode, e){ | ||
console.log('[ibc-js] Register - failure x' + attempt + ' :', enrollID, statusCode); | ||
logger.error('[ibc-js] Register - failure x' + attempt + ' :', enrollID, statusCode); | ||
if(attempt <= maxRetry){ //lets try again after a short delay, maybe the peer is still starting | ||
console.log('[ibc-js] \tgoing to try to register again in 30 secs'); | ||
logger.log('[ibc-js] \tgoing to try to register again in 30 secs'); | ||
setTimeout(function(){register(index, enrollID, enrollSecret, maxRetry, ++attempt, cb);}, 30000); | ||
@@ -697,3 +704,3 @@ } | ||
ibc.prototype.unregister = function(index, enrollID, cb) { | ||
console.log('[ibc-js] Unregistering ', ibc.chaincode.details.peers[index].name, ' w/enrollID - ' + enrollID); | ||
logger.log('[ibc-js] Unregistering ', ibc.chaincode.details.peers[index].name, ' w/enrollID - ' + enrollID); | ||
var options = { | ||
@@ -707,3 +714,3 @@ path: '/registrar/' + enrollID, | ||
options.success = function(statusCode, data){ | ||
console.log('[ibc-js] Unregistering success:', enrollID); | ||
logger.log('[ibc-js] Unregistering success:', enrollID); | ||
ibc.chaincode.details.peers[index].enrollID = null; //unremember a valid enrollID for this peer | ||
@@ -713,3 +720,3 @@ if(cb) cb(null, data); | ||
options.failure = function(statusCode, e){ | ||
console.log('[ibc-js] Unregistering - failure:', enrollID, statusCode); | ||
logger.log('[ibc-js] Unregistering - failure:', enrollID, statusCode); | ||
if(cb) cb(helper.eFmt('unregister() error', statusCode, e), null); | ||
@@ -724,3 +731,3 @@ }; | ||
ibc.prototype.check_register = function(index, enrollID, cb) { | ||
console.log('[ibc-js] Checking ', ibc.chaincode.details.peers[index].name, ' w/enrollID - ' + enrollID); | ||
logger.log('[ibc-js] Checking ', ibc.chaincode.details.peers[index].name, ' w/enrollID - ' + enrollID); | ||
var options = { | ||
@@ -734,7 +741,7 @@ path: '/registrar/' + enrollID, | ||
options.success = function(statusCode, data){ | ||
console.log('[ibc-js] Check Register success:', enrollID); | ||
logger.log('[ibc-js] Check Register success:', enrollID); | ||
if(cb) cb(null, data); | ||
}; | ||
options.failure = function(statusCode, e){ | ||
console.log('[ibc-js] Check Register - failure:', enrollID, statusCode); | ||
logger.error('[ibc-js] Check Register - failure:', enrollID, statusCode); | ||
if(cb) cb(helper.eFmt('check_register() error', statusCode, e), null); | ||
@@ -757,8 +764,8 @@ }; | ||
console.log('[ibc-js] Deploy Chaincode - Starting'); | ||
console.log('[ibc-js] \tfunction:', func, ', arg:', args); | ||
console.log('\n\n\t Waiting...'); //this can take awhile | ||
logger.log('[ibc-js] Deploy Chaincode - Starting'); | ||
logger.log('[ibc-js] \tfunction:', func, ', arg:', args); | ||
logger.log('\n\n\t Waiting...'); //this can take awhile | ||
var options = {}, body = {}; | ||
if(ibc.chaincode.details.version.indexOf('hyperledger/fabric/core/chaincode/shim') >= 0){ | ||
if(ibc.chaincode.details.version.indexOf('hyperledger/fabric/core/chaincode/shim') >= 0){ //hyperledger body format | ||
options = {path: '/chaincode'}; | ||
@@ -782,3 +789,3 @@ body = { | ||
} | ||
else{ | ||
else{ //obc-peer body format | ||
options = {path: '/devops/deploy'}; | ||
@@ -797,22 +804,37 @@ body = { | ||
} | ||
//console.log('!body', body); | ||
// ---- Success ---- // | ||
options.success = function(statusCode, data){ | ||
if(data.result && ibc.chaincode.details.version.indexOf('hyperledger/fabric/core/chaincode/shim') >= 0) ibc.chaincode.details.deployed_name = data.result.message; | ||
else ibc.chaincode.details.deployed_name = data.message; | ||
ibc.prototype.save(tempDirectory); //save it so we remember we have deployed | ||
if(deploy_options && deploy_options.save_path != null) ibc.prototype.save(deploy_options.save_path); | ||
if(cb){ | ||
var wait_ms = 40000; //default wait after deploy, peer may still be starting | ||
if(deploy_options && deploy_options.delay_ms && Number(deploy_options.delay_ms)) wait_ms = deploy_options.delay_ms; | ||
console.log('\n\n\t deploy success [waiting another', (wait_ms / 1000) ,'seconds]'); | ||
console.log('\t', ibc.chaincode.details.deployed_name, '\n'); | ||
if(data.result && ibc.chaincode.details.version.indexOf('hyperledger/fabric/core/chaincode/shim') >= 0){//hyperledger response | ||
ibc.chaincode.details.deployed_name = data.result.message; | ||
} | ||
else ibc.chaincode.details.deployed_name = data.message; //obc-peer response | ||
if(ibc.chaincode.details.deployed_name === ''){ | ||
logger.error('\n\n\t deploy resp error - there is no chaincode hash name in response:', data); | ||
if(cb) cb(helper.eFmt('deploy() error no cc name', 502, data), null); | ||
} | ||
else{ | ||
ibc.prototype.save(tempDirectory); //save it to known place so we remember the cc name | ||
if(deploy_options && deploy_options.save_path != null) { //save it to custom route | ||
ibc.prototype.save(deploy_options.save_path); | ||
} | ||
setTimeout(function(){ | ||
console.log('[ibc-js] Deploy Chaincode - Complete'); | ||
cb(null, data); | ||
}, wait_ms); //wait extra long, not always ready yet | ||
if(cb){ | ||
var wait_ms = 45000; //default wait after deploy, peer may still be starting | ||
if(deploy_options && deploy_options.delay_ms && Number(deploy_options.delay_ms)) wait_ms = deploy_options.delay_ms; | ||
logger.log('\n\n\t deploy success [waiting another', (wait_ms / 1000) ,'seconds]'); | ||
logger.log('\t', ibc.chaincode.details.deployed_name, '\n'); | ||
setTimeout(function(){ | ||
logger.log('[ibc-js] Deploy Chaincode - Complete'); | ||
cb(null, data); | ||
}, wait_ms); //wait extra long, not always ready yet | ||
} | ||
} | ||
}; | ||
// ---- Failure ---- /// | ||
options.failure = function(statusCode, e){ | ||
console.log('[ibc-js] deploy - failure:', statusCode); | ||
logger.error('[ibc-js] deploy - failure:', statusCode); | ||
if(cb) cb(helper.eFmt('deploy() error', statusCode, e), null); | ||
@@ -830,3 +852,3 @@ }; | ||
if(ibc.lastPoll + slow_mode < Date.now()){ //slow mode poll | ||
//console.log('[ibc-js] Its been awhile, time to poll'); | ||
//logger.log('[ibc-js] Its been awhile, time to poll'); | ||
ibc.lastPoll = Date.now(); | ||
@@ -839,3 +861,3 @@ ibc.prototype.chain_stats(cb_got_stats); | ||
if(elasped <= 3000){ //fresh unresolved action, fast mode! | ||
console.log('[ibc-js] Unresolved action, must poll'); | ||
logger.log('[ibc-js] Unresolved action, must poll'); | ||
ibc.lastPoll = Date.now(); | ||
@@ -845,3 +867,3 @@ ibc.prototype.chain_stats(cb_got_stats); | ||
else{ | ||
//console.log('[ibc-js] Expired, removing'); | ||
//logger.log('[ibc-js] Expired, removing'); | ||
ibc.q.pop(); //expired action, remove it | ||
@@ -857,3 +879,3 @@ } | ||
if(ibc.lastBlock != stats.height) { //this is a new block! | ||
console.log('[ibc-js] New block!', stats.height); | ||
logger.log('[ibc-js] New block!', stats.height); | ||
ibc.lastBlock = stats.height; | ||
@@ -884,7 +906,7 @@ ibc.q.pop(); //action is resolved, remove | ||
options.success = function(statusCode, data){ | ||
console.log('[ibc-js] Get Transaction - success:', data); | ||
logger.log('[ibc-js] Get Transaction - success:', data); | ||
if(cb) cb(null, data); | ||
}; | ||
options.failure = function(statusCode, e){ | ||
console.log('[ibc-js] Get Transaction - failure:', statusCode); | ||
logger.error('[ibc-js] Get Transaction - failure:', statusCode); | ||
if(cb) cb(helper.eFmt('read() error', statusCode, e), null); | ||
@@ -902,6 +924,6 @@ }; | ||
if(ibc.chaincode.invoke[name] != null){ //skip if already exists | ||
//console.log('[ibc-js] \t skip, func', name, 'already exists'); | ||
//logger.log('[ibc-js] \t skip, func', name, 'already exists'); | ||
} | ||
else { | ||
console.log('[ibc-js] Found cc invoke function: ', name); | ||
logger.log('[ibc-js] Found cc invoke function: ', name); | ||
ibc.chaincode.details.func.invoke.push(name); | ||
@@ -955,3 +977,3 @@ ibc.chaincode.invoke[name] = function(args, enrollId, cb){ //create the function in the chaincode obj | ||
options.success = function(statusCode, data){ | ||
console.log('[ibc-js]', name, ' - success:', data); | ||
logger.log('[ibc-js]', name, ' - success:', data); | ||
ibc.q.push(Date.now()); //new action, add it to queue | ||
@@ -961,3 +983,3 @@ if(cb) cb(null, data); | ||
options.failure = function(statusCode, e){ | ||
console.log('[ibc-js]', name, ' - failure:', statusCode, e); | ||
logger.error('[ibc-js]', name, ' - failure:', statusCode, e); | ||
if(cb) cb(helper.eFmt('invoke() error', statusCode, e), null); | ||
@@ -975,6 +997,6 @@ }; | ||
if(ibc.chaincode.query[name] != null && name !== 'read'){ //skip if already exists | ||
//console.log('[ibc-js] \t skip, func', name, 'already exists'); | ||
//logger.log('[ibc-js] \t skip, func', name, 'already exists'); | ||
} | ||
else { | ||
console.log('[ibc-js] Found cc query function: ', name); | ||
logger.log('[ibc-js] Found cc query function: ', name); | ||
ibc.chaincode.details.func.query.push(name); | ||
@@ -1029,10 +1051,13 @@ ibc.chaincode.query[name] = function(args, enrollId, cb){ //create the function in the chaincode obj | ||
options.success = function(statusCode, data){ | ||
console.log('[ibc-js]', name, ' - success:', data); | ||
logger.log('[ibc-js]', name, ' - success:', data); | ||
if(cb){ | ||
if(data && data.result) cb(null, data.result.message); | ||
else cb(null, data.OK); | ||
if(data){ | ||
if(data.result) cb(null, data.result.message); | ||
else cb(null, data.OK); | ||
} | ||
else cb(helper.eFmt('query() resp error', 502, data), null); //something is wrong, response is not what we expect | ||
} | ||
}; | ||
options.failure = function(statusCode, e){ | ||
console.log('[ibc-js]', name, ' - failure:', statusCode, e); | ||
logger.error('[ibc-js]', name, ' - failure:', statusCode, e); | ||
if(cb) cb(helper.eFmt('query() error', statusCode, e), null); | ||
@@ -1039,0 +1064,0 @@ }; |
@@ -26,6 +26,6 @@ 'use strict'; | ||
options.success = function(statusCode, data){ | ||
console.log("Get - success"); | ||
logger.log("Get - success"); | ||
} | ||
options.failure = function(statusCode, e){ | ||
console.log("Get - failure", e); | ||
logger.log("Get - failure", e); | ||
} | ||
@@ -55,3 +55,3 @@ rest.get(options, {'user':'david'}); | ||
----------------------------------------------------------------- | ||
quiet: If true will not print to console. Defaults false. | ||
quiet: If true will not print to logger. Defaults false. | ||
ssl: Iff false will use http instead of https. Defaults true. | ||
@@ -73,2 +73,3 @@ timeout: Integer in milliseconds to time out requests. Defaults 20,000 | ||
}; | ||
var logger = {log: console.log, error: console.error, debug: console.log, warn: console.log}; | ||
@@ -88,6 +89,6 @@ //is the obj empty or not | ||
if(a[i] === undefined) { | ||
//console.log(' - copying'); | ||
//logger.log(' - copying'); | ||
a[i] = JSON.parse(JSON.stringify(b[i])); | ||
} | ||
//else console.log(' - not copying'); | ||
//else logger.log(' - not copying'); | ||
} | ||
@@ -125,3 +126,3 @@ return a; | ||
} | ||
if(!options.quiet) console.log(http_txt + ' ' + options.path); | ||
if(!options.quiet) logger.debug(http_txt + ' ' + options.path); | ||
@@ -155,3 +156,3 @@ //// Sanitize Inputs //// | ||
if(!options.quiet && options.method.toLowerCase() !== 'get') { | ||
console.log(' body:', body); | ||
logger.debug(' body:', body); | ||
} | ||
@@ -163,3 +164,3 @@ | ||
var str = '', temp, chunks = 0; | ||
if(!options.quiet) console.log(http_txt + ' Status code: ' + resp.statusCode); | ||
if(!options.quiet) logger.debug(http_txt + ' Status code: ' + resp.statusCode); | ||
@@ -173,3 +174,3 @@ resp.setEncoding('utf8'); | ||
if(resp.statusCode == 204){ //empty response, don't parse body | ||
if(!options.quiet) console.log(http_txt + ' Data: No Content'); | ||
if(!options.quiet) logger.debug(http_txt + ' Data: No Content'); | ||
success(resp.statusCode, resp.headers, str); | ||
@@ -185,7 +186,7 @@ } | ||
goodJSON = false; | ||
if(!options.quiet) console.log(http_txt + ' Error - response is not JSON: ', str); | ||
if(!options.quiet) logger.error(http_txt + ' Error - response is not JSON: ', str); | ||
failure(500, resp.headers, 'Invalid JSON response: ' + str); | ||
} | ||
if(goodJSON){ | ||
//if(!options.quiet) console.log(http_txt + ' Data:', str); //all good [json resp] | ||
//if(!options.quiet) logger.debug(http_txt + ' Data:', str); //all good [json resp] | ||
success(resp.statusCode, resp.headers, temp); | ||
@@ -195,3 +196,3 @@ } | ||
else { //all good [not json resp] | ||
if(!options.quiet) console.log(http_txt + ' Data:', str); | ||
if(!options.quiet) logger.debug(http_txt + ' Data:', str); | ||
success(resp.statusCode, resp.headers, str); | ||
@@ -201,3 +202,3 @@ } | ||
else { | ||
if(!options.quiet) console.log(http_txt + ' Error - status code: ' + resp.statusCode, str); | ||
if(!options.quiet) logger.error(http_txt + ' Error - status code: ' + resp.statusCode, str); | ||
if(acceptJson){ | ||
@@ -215,3 +216,3 @@ try{ | ||
request.on('error', function(e) { //handle error event | ||
if(!options.quiet) console.log(http_txt + ' Error - unknown issue with request: ', e); //catch failed request (failed DNS lookup and such) | ||
if(!options.quiet) logger.error(http_txt + ' Error - unknown issue with request: ', e); //catch failed request (failed DNS lookup and such) | ||
failure(500, null, e); | ||
@@ -222,3 +223,3 @@ }); | ||
request.on('timeout', function(){ //handle time out event | ||
if(!options.quiet) console.log(http_txt + ' Error - request timed out'); | ||
if(!options.quiet) logger.error(http_txt + ' Error - request timed out'); | ||
failure(408, null, 'Request timed out'); | ||
@@ -235,6 +236,11 @@ request.destroy(); | ||
//load new default option values | ||
module.exports.init = function(opt){ | ||
module.exports.init = function(opt, log_outputs){ | ||
for(var i in opt){ | ||
default_options[i] = JSON.parse(JSON.stringify(opt[i])); | ||
} | ||
if(log_outputs && log_outputs.info) logger.log = log_outputs.info; //send normal logs here | ||
if(log_outputs && log_outputs.error) logger.error = log_outputs.error; //send error logs here | ||
if(log_outputs && log_outputs.warn) logger.warn = log_outputs.warn; //send warn logs here | ||
if(log_outputs && log_outputs.debug) logger.debug = log_outputs.debug; //send debug logs here | ||
}; | ||
@@ -241,0 +247,0 @@ |
{ | ||
"name": "ibm-blockchain-js", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "A library for easily interacting with IBM Blockchain.", | ||
@@ -31,3 +31,3 @@ "main": "index.js", | ||
}, | ||
"license": "IPL-1.0" | ||
"license": "Apache-2.0" | ||
} |
@@ -48,3 +48,3 @@ *Are you looking for the Marbles app demo? That’s not here, head to the [marbles example](https://github.com/IBM-Blockchain/marbles)* | ||
var Ibc1 = require('ibm-blockchain-js'); | ||
var ibc = new Ibc1(); | ||
var ibc = new Ibc1(/*logger*/); //you can pass a logger such as winston here - optional | ||
var chaincode = {}; | ||
@@ -157,2 +157,23 @@ | ||
## <a name="ibcjs"></a>IBM-Blockchain-JS Documentation | ||
### Usage | ||
Example with standard console logging: | ||
```js | ||
var Ibc1 = require('ibm-blockchain-js'); | ||
var ibc = new Ibc1(); | ||
``` | ||
Example with [Winston](https://www.npmjs.com/package/winston) logging: | ||
```js | ||
var winston = require('winston'); | ||
var logger = new (winston.Logger)({ | ||
transports: [ | ||
new (winston.transports.Console)(), | ||
new (winston.transports.File)({ filename: 'somefile.log' }) | ||
] | ||
}); | ||
var Ibc1 = require('ibm-blockchain-js'); | ||
var ibc = new Ibc1(logger); //you can pass a logger such as winston here - optional | ||
``` | ||
### ibc.load(options, [callback]) | ||
@@ -159,0 +180,0 @@ This is a function that wraps a typical startup using a standard Bluemix IBM Blockchain network. |
// Function testing the SDK | ||
// This file contains the most recent tests still being developed. | ||
console.log("Now starting SDKFunctiontest.js"); | ||
// Starting out by requiring all dependancies | ||
var test = require('tape'); | ||
var Ibc1 = require('ibm-blockchain-js'); | ||
var Ibc1 = require('..'); | ||
@@ -19,7 +21,8 @@ // Then define new instances that will be needed | ||
network:{ peers: [{ | ||
"api_host": "169.44.63.210", | ||
"api_port": "45937", | ||
"id": "cd750ebe-60cf-493d-907f-9ddf6202d6bd_vp1", | ||
"api_url": "http://169.44.63.210:45937" | ||
network:{ | ||
peers: [{ | ||
"api_host": "3f3fa6c3-a8b4-48b2-95bc-63b5058fa333_vp1-api.blockchain.ibm.com", | ||
"api_port": "80", | ||
"id": "3f3fa6c3-a8b4-48b2-95bc-63b5058fa333_vp1", | ||
"api_url": "http://3f3fa6c3-a8b4-48b2-95bc-63b5058fa333_vp1-api.blockchain.ibm.com:80" | ||
}], | ||
@@ -29,4 +32,4 @@ | ||
users: [{ | ||
"username": "user_type0_2a590b0f89", | ||
"secret": "c008a51fbc" | ||
"username": "user_type0_52737ec3c6", | ||
"secret": "4841d68d27" | ||
}] }, | ||
@@ -42,30 +45,27 @@ | ||
test('Was the load_chaincode sucessful', function (t) { | ||
// Load the Marbles2 chaincode, with defined options, and return call-back-when-ready function. | ||
ibc.load(options, cb_ready); | ||
ibc.load(options, cb_ready); | ||
// Define the call-back-when-ready function returned above | ||
// call-back-when-ready function has err | ||
function cb_ready(err, cc){ | ||
// Define the call-back-when-ready function returned above | ||
// call-back-when-ready function has err | ||
function cb_ready(err, cc){ | ||
//response has chaincode functions | ||
t.error(err, 'There were no errors'); | ||
// if the deployed name is blank, then chaincode has not been deployed | ||
if(cc.details.deployed_name === ""){ | ||
if(cc.details.deployed_name === ""){ | ||
cc.deploy('init', ['99'], './cc_summaries', cb_deployed); | ||
function cb_deployed(err){ | ||
t.error(err, 'There were no errors'); | ||
console.log('sdk has deployed code and waited'); | ||
t.end(); | ||
}; //end test | ||
} | ||
} | ||
} | ||
else{ | ||
console.log('chaincode summary file indicates chaincode has been previously deployed'); | ||
t.end(); | ||
}; | ||
} | ||
}; | ||
ibc.chain_stats([stats_callback]); | ||
function stats_callback(e, stats){ | ||
console.log('got some stats', stats); | ||
} | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
89482
12
0
100
1398
556