Comparing version 0.1.3 to 0.1.4
@@ -12,3 +12,3 @@ // LICENSE_CODE ZON ISC | ||
'es6_shim', 'escape', 'etask', 'file', 'lang', 'list', 'match', 'rand', | ||
'rate_limit', 'sprintf', 'string', 'typedarray_shim', 'url', 'util', | ||
'rate_limit', 'sprintf', 'string', 'url', 'util', | ||
'version', 'version_util', 'zdot', 'zerr']; | ||
@@ -15,0 +15,0 @@ define(modules.map(function(name){ return '/util/'+name+'.js'; } ), function(){ |
{ | ||
"name": "hutil", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Hola Utilities", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,3 +19,2 @@ // LICENSE_CODE ZON ISC | ||
// XXX arik: need test | ||
// XXX mikhail: should GET be the default method? | ||
function ajax(opt){ | ||
@@ -26,3 +25,3 @@ var timeout = opt.timeout||20*date.ms.SEC, slow = opt.slow||2*date.ms.SEC; | ||
// opt.type is deprecated | ||
var method = opt.method||opt.type||'POST'; | ||
var method = opt.method||opt.type||'GET'; | ||
var data_type = opt.json ? 'json' : 'text'; | ||
@@ -29,0 +28,0 @@ var t0 = Date.now(); |
@@ -1,2 +0,2 @@ | ||
// LICENSE CODE ZON | ||
// LICENSE_CODE ZON | ||
'use strict'; /*jslint browser:true*/ | ||
@@ -8,2 +8,4 @@ define(['angular_1_4_8', '/util/escape.js', '/util/date.js'], | ||
return function(d, format){ return date.strftime(format||'%v', d); }; | ||
}).filter('ms_to_dur', function(){ | ||
return date.ms_to_dur.bind(date); | ||
}).filter('nl2br', ['$sce', function($sce){ | ||
@@ -14,3 +16,5 @@ return function(input){ | ||
return zescape.uri_comp.bind(zescape); | ||
}).filter('mailto', function(){ | ||
return zescape.mailto_url.bind(zescape); | ||
}); | ||
}); |
@@ -13,2 +13,16 @@ // LICENSE_CODE ZON ISC | ||
var proto_slice = Array.prototype.slice; | ||
E.copy = function(a){ | ||
switch (a.length) | ||
{ | ||
case 0: return []; | ||
case 1: return [a[0]]; | ||
case 2: return [a[0], a[1]]; | ||
case 3: return [a[0], a[1], a[2]]; | ||
case 4: return [a[0], a[1], a[2], a[3]]; | ||
case 5: return [a[0], a[1], a[2], a[3], a[4]]; | ||
default: return proto_slice.call(a); | ||
} | ||
}; | ||
E.push = function(a){ | ||
@@ -15,0 +29,0 @@ for (var i=1; i<arguments.length; i++) |
@@ -15,2 +15,3 @@ // LICENSE_CODE ZON ISC | ||
const etask = require('./etask.js'); | ||
const assign = Object.assign, ef = etask.ef; | ||
var readline; | ||
@@ -101,10 +102,6 @@ try { readline = require('readline-sync'); } catch(e){} | ||
}; | ||
E.exec = function(cmd, opt){ | ||
E.verbose(Array.isArray(cmd) ? cmd.join(' ') : cmd); | ||
if (E.dry_run) | ||
return; | ||
var base_opt = Array.isArray(cmd) ? {} : {shell: true, stdio: 'inherit'}; | ||
opt = Object.assign(base_opt, opt||{}); | ||
return exec.sys_sync(cmd, opt); | ||
}; | ||
let get_exec_opt = opt=>assign({log: E.opt.verbose, dry_run: E.dry_run}, opt); | ||
E.exec = (cmd, opt)=>exec.get(cmd, get_exec_opt(opt)); | ||
E.exec_get_lines = cmd=>exec.get_lines(cmd, get_exec_opt()); | ||
E.exec_get_line = cmd=>exec.get_line(cmd, get_exec_opt()); | ||
E.exec_e = function(cmd, opt){ | ||
@@ -115,6 +112,2 @@ var ret; | ||
}; | ||
E.exec_get_lines = function(cmd){ | ||
return string.split_crlf(E.exec(cmd, {out: 'stdout', stdio: 'pipe'})); | ||
}; | ||
E.exec_get_line = function(cmd){ return E.exec_get_lines(cmd)[0]; }; | ||
E.geteuid = function(){ | ||
@@ -219,7 +212,7 @@ if (process.geteuid) | ||
msg = msg||''; | ||
opt = opt||{}; | ||
this.opt = opt||{}; | ||
this.name = name; | ||
this.message = msg.message||msg; | ||
this.stack = msg.stack||(new Error(this)).stack; | ||
this.output = opt.output||''; | ||
this.output = this.opt.output||''; | ||
} | ||
@@ -243,3 +236,3 @@ err.prototype = Object.create(Error.prototype); | ||
yield promise; | ||
} catch(e){ | ||
} catch(e){ ef(e); | ||
console.error(opt.skip_stack ? ''+e : (e.stack||e)); | ||
@@ -246,0 +239,0 @@ exit_with_code(opt, 1); |
@@ -193,3 +193,3 @@ // LICENSE_CODE ZON ISC | ||
var str = num.toFixed(num<1 ? 3 : num<10 ? 2 : num<100 ? 1 : 0); | ||
return sign+str.replace(/\.0*$/, '')+scale.s+_per(); | ||
return sign+str.replace(/\.0*$/, '')+(opt.space ? ' ' : '')+scale.s+_per(); | ||
}; | ||
@@ -255,4 +255,6 @@ | ||
prev_re = re_class.prototype.toJSON; | ||
re_class.prototype.toJSON = function(){ | ||
return {__RegExp__: this.toString()}; }; | ||
Object.defineProperty(re_class.prototype, 'toJSON', { | ||
value: function(){ return {__RegExp__: this.toString()}; }, | ||
writable: true, | ||
}); | ||
} | ||
@@ -273,10 +275,11 @@ try { s = JSON.stringify(obj, opt.replacer, opt.spaces); } | ||
function parse_leaf(v){ | ||
function parse_leaf(v, opt){ | ||
opt = Object.assign({date: 1, re: 1, func: 1}, opt); | ||
if (!v || typeof v!='object' || Object.keys(v).length!=1) | ||
return v; | ||
if (v.__ISODate__) | ||
if (v.__ISODate__ && opt.date) | ||
return new Date(v.__ISODate__); | ||
if (v.__Function__) | ||
if (v.__Function__ && opt.func) | ||
return E.build_function(E.parse_function(v.__Function__)); | ||
if (v.__RegExp__) | ||
if (v.__RegExp__ && opt.re) | ||
{ | ||
@@ -291,6 +294,6 @@ var parsed = /^\/(.*)\/(\w*)$/.exec(v.__RegExp__); | ||
E.JSON_parse = function(s){ | ||
return JSON.parse(s, function(k, v){ return parse_leaf(v); }); }; | ||
E.JSON_parse = function(s, opt){ | ||
return JSON.parse(s, function(k, v){ return parse_leaf(v, opt); }); }; | ||
E.JSON_parse_obj = function(v){ | ||
E.JSON_parse_obj = function(v, opt){ | ||
if (!v || typeof v!='object') | ||
@@ -301,10 +304,10 @@ return v; | ||
for (var i = 0; i<v.length; i++) | ||
v[i] = E.JSON_parse_obj(v[i]); | ||
v[i] = E.JSON_parse_obj(v[i], opt); | ||
return v; | ||
} | ||
var v2 = parse_leaf(v); | ||
var v2 = parse_leaf(v, opt); | ||
if (v2!==v) | ||
return v2; | ||
for (var key in v) | ||
v[key] = E.JSON_parse_obj(v[key]); | ||
v[key] = E.JSON_parse_obj(v[key], opt); | ||
return v; | ||
@@ -311,0 +314,0 @@ }; |
// LICENSE_CODE ZON ISC | ||
'use strict'; /*jslint browser:true*/ | ||
define(['jquery', '/util/url.js', 'jquery_cookie'], | ||
function($, zurl){ | ||
define(['jquery', '/util/url.js', 'cookie'], | ||
function($, zurl, cookie){ | ||
var E = {}; | ||
E.get = function(){ return $.cookie('XSRF-TOKEN'); }; | ||
E.get = function(){ return cookie.get('XSRF-TOKEN'); }; | ||
@@ -9,0 +9,0 @@ E.add = function(form){ |
@@ -254,15 +254,15 @@ // LICENSE_CODE ZON ISC | ||
var _ms = E.get(until_date)-E.get(d); | ||
if (_ms < 2*ms.SEC) | ||
if (_ms<2*ms.SEC) | ||
return 'right now'; | ||
if (_ms < 2*ms.MIN) | ||
if (_ms<2*ms.MIN) | ||
return Math.round(_ms/ms.SEC)+' sec ago'; | ||
if (_ms < 2*ms.HOUR) | ||
if (_ms<2*ms.HOUR) | ||
return Math.round(_ms/ms.MIN)+' min ago'; | ||
if (_ms < 2*ms.DAY) | ||
if (_ms<2*ms.DAY) | ||
return Math.round(_ms/ms.HOUR)+' hour ago'; | ||
if (_ms < 2*ms.WEEK) | ||
if (_ms<2*ms.WEEK) | ||
return Math.round(_ms/ms.DAY)+' days ago'; | ||
if (_ms < 2*ms.MONTH) | ||
if (_ms<2*ms.MONTH) | ||
return Math.round(_ms/ms.WEEK)+' weeks ago'; | ||
if (_ms < 2*ms.YEAR) | ||
if (_ms<2*ms.YEAR) | ||
return Math.round(_ms/ms.MONTH)+' month ago'; | ||
@@ -434,3 +434,3 @@ return Math.round(_ms/ms.YEAR)+' years ago'; | ||
var i = n % 10, ii = n % 100; | ||
if ((ii >= 11 && ii <= 13) || i==0 || i>=4) | ||
if ((ii>=11 && ii<=13) || i==0 || i>=4) | ||
return 'th'; | ||
@@ -488,3 +488,3 @@ switch (i) | ||
{ | ||
var sign = tz[0] == '-' ? -1 : 1; | ||
var sign = tz[0]=='-' ? -1 : 1; | ||
var hours = parseInt(tz.slice(1, 3), 10); | ||
@@ -491,0 +491,0 @@ var mins = parseInt(tz.slice(3, 5), 10); |
// LICENSE_CODE ZON ISC | ||
'use strict'; /*jslint node:true, browser:true*/ | ||
'use strict'; /*jslint node:true, browser:true, -W103*/ | ||
(function(){ | ||
@@ -36,2 +36,14 @@ var define; | ||
add_prop(Object, 'assign', E.t.object_assign); | ||
var can_set_proto = {__proto__: []} instanceof Array; | ||
E.t.object_setPrototypeOf = function(obj, proto){ | ||
if (can_set_proto) | ||
obj.__proto__ = proto; | ||
else // IE9/IE10 | ||
{ | ||
for (var prop in proto) | ||
obj[prop] = proto[prop]; | ||
} | ||
return true; | ||
}; | ||
add_prop(Object, 'setPrototypeOf', E.t.object_setPrototypeOf); | ||
@@ -62,2 +74,14 @@ E.t.string_startsWith = function(head){ | ||
add_prop(String.prototype, 'trimRight', E.t.string_trimRight); | ||
E.t.string_padStart = function(len, val){ | ||
val = val||' '; | ||
var l = len-this.length; | ||
return l<=0 ? this : val.repeat(Math.ceil(l/val.length)).slice(0, l)+this; | ||
}; | ||
add_prop(String.prototype, 'padStart', E.t.string_padStart); | ||
E.t.string_padEnd = function(len, val){ | ||
val = val||' '; | ||
var l = len-this.length; | ||
return l<=0 ? this : this+val.repeat(Math.ceil(l/val.length)).slice(0, l); | ||
}; | ||
add_prop(String.prototype, 'padEnd', E.t.string_padEnd); | ||
@@ -137,5 +161,10 @@ E.t.array_includes = function(search, index){ | ||
function ie9_console(){ | ||
// in ie9 console is undefined if no dev console open | ||
if (typeof window!='object' || window.console) | ||
if (typeof window!='object') | ||
return; | ||
if (window.console) | ||
{ | ||
if (!window.console.debug) // ie9 has no debug() in native console | ||
window.console.debug = window.console.info; | ||
return; | ||
} | ||
var console = window.console = {}; | ||
@@ -142,0 +171,0 @@ ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml', |
@@ -152,3 +152,4 @@ // LICENSE_CODE ZON ISC | ||
return qs; | ||
var uri_comp = opt.space_plus ? E.uri_comp : encodeURIComponent; | ||
var uri_comp = opt.space_plus===undefined || opt.space_plus ? E.uri_comp | ||
: encodeURIComponent; | ||
var uri_comp_val = opt.bin ? E.encodeURIComponent_bin : uri_comp; | ||
@@ -178,9 +179,2 @@ for (var i in param) | ||
function clone(src){ | ||
var dst = {}; | ||
for (var prop in src) | ||
dst[prop] = src[prop]; | ||
return dst; | ||
} | ||
// uri(opt) | ||
@@ -194,3 +188,3 @@ // uri(uri, qs, hash) | ||
{ | ||
opt = clone(uri); | ||
opt = Object.assign({}, uri); | ||
opt._qs = opt.qs; | ||
@@ -197,0 +191,0 @@ opt.qs = undefined; |
@@ -54,2 +54,3 @@ // LICENSE_CODE ZON ISC | ||
E.set_zerr = function(_zerr){ zerr = _zerr; }; | ||
E.events = new events(); | ||
function stack_get(){ | ||
@@ -100,3 +101,3 @@ // new Error(): 200K per second | ||
this.cur_state = -1; | ||
this.states = states; | ||
this.states = []; | ||
this._stack = Etask.use_bt ? stack_get() : undefined; | ||
@@ -106,29 +107,10 @@ this.tm_create = Date.now(); | ||
var idx = this.states.idx = {}; | ||
for (var i=0; i<this.states.length; i++) | ||
for (var i=0; i<states.length; i++) | ||
{ | ||
var state = this.states[i], t; | ||
if (typeof state=='function') | ||
{ | ||
t = this._get_func_type(state); | ||
state = this.states[i] = {f: state, label: t.label, | ||
try_catch: t.try_catch, catch: t.catch, ensure: t.ensure, | ||
cancel: t.cancel, sig: undefined}; | ||
} | ||
else if (typeof state=='object') | ||
{ | ||
assert(state.f, 'invalid state type or missing func '+state); | ||
t = this._get_func_type(state.f); | ||
if (t.label && !state.label) | ||
state.label = t.label; | ||
if (t.try_catch) | ||
state.try_catch = t.try_catch; | ||
if (t.catch) | ||
state.catch = t.catch; | ||
if (t.ensure) | ||
state.ensure = t.ensure; | ||
if (t.cancel) | ||
state.cancel = t.cancel; | ||
} | ||
else | ||
var pstate = states[i], t; | ||
if (typeof pstate!='function') | ||
assert(0, 'invalid state type'); | ||
t = this._get_func_type(pstate); | ||
var state = {f: pstate, label: t.label, try_catch: t.try_catch, | ||
catch: t.catch, ensure: t.ensure, cancel: t.cancel, sig: undefined}; | ||
if (i==0 && opt.state0_args) | ||
@@ -155,2 +137,3 @@ { | ||
} | ||
this.states[i] = state; | ||
} | ||
@@ -169,8 +152,8 @@ E.root.push(this); | ||
{ | ||
var wait_retval = this._set_wait_retval(); | ||
var wait_retval = this._set_wait_retval(), _this = this; | ||
E.nextTick(function(){ | ||
if (this.running!==undefined) | ||
if (_this.running!==undefined) | ||
return; | ||
this._got_retval(wait_retval); | ||
}.bind(this)); | ||
_this._got_retval(wait_retval); | ||
}); | ||
} | ||
@@ -236,2 +219,4 @@ else | ||
this.parent_type = this.up ? 'call' : 'spawn'; | ||
if (this.error) | ||
this.emit_safe('uncaught', this.error); | ||
if (this._ensure!==undefined) | ||
@@ -244,2 +229,4 @@ { | ||
this.emit_safe('ensure'); | ||
if (this.error && !this.up && !this.parent && !this.parent_guess) | ||
E.events.emit('uncaught', this); | ||
if (this.parent) | ||
@@ -376,4 +363,3 @@ this.parent.emit('child', this); | ||
E.in_run = []; | ||
E.in_run_top = function(){ | ||
return E.in_run[E.in_run.length-1]; }; | ||
E.in_run_top = function(){ return E.in_run[E.in_run.length-1]; }; | ||
E.prototype._run = function(){ | ||
@@ -741,4 +727,8 @@ var rv = {ret: undefined, err: undefined}; | ||
}; | ||
Etask_wait.prototype.econtinue_fn = function(){ | ||
return this.econtinue.bind(this); }; | ||
Etask_wait.prototype.ethrow = function(err){ | ||
return this.econtinue(E.err(err)); }; | ||
Etask_wait.prototype.ethrow_fn = function(){ | ||
return this.ethrow.bind(this); }; | ||
E.prototype.wait = function(timeout){ | ||
@@ -745,0 +735,0 @@ return new Etask_wait(this, 'wait', timeout); }; |
153
util/file.js
@@ -9,3 +9,4 @@ // LICENSE_CODE ZON ISC | ||
const fs = require('fs'); | ||
const E = exports; | ||
const StringDecoder = require('string_decoder').StringDecoder; | ||
const E = exports, assign = Object.assign; | ||
// file.xxx_e() throw exceptions. file.xxx() return null/false on fail. | ||
@@ -34,7 +35,7 @@ E.errno = 0; // an integer/string error code | ||
}; | ||
E.fread_cb_e = (fd, offset, length, pos, cb)=>{ | ||
E.fread_cb_e = (fd, pos, cb)=>{ | ||
let res, buf = new Buffer(E.read_buf_size); | ||
while (res = fs.readSync(fd, buf, offset, length, pos)) | ||
while (res = fs.readSync(fd, buf, 0, buf.length, pos)) | ||
{ | ||
if (cb(buf, res, pos)) | ||
if (cb(buf.slice(0, res), pos)) | ||
return true; | ||
@@ -45,44 +46,30 @@ pos += res; | ||
}; | ||
E.read_cb_e = (filename, offset, length, pos, cb)=>{ | ||
E.read_cb_e = (filename, pos, cb)=>{ | ||
let fd = fs.openSync(filename, 'r'); | ||
try { return E.fread_cb_e(fd, offset, length, pos, cb); } | ||
try { return E.fread_cb_e(fd, pos, cb); } | ||
finally { fs.closeSync(fd); } | ||
}; | ||
let bytes2str = (bytes, encoding)=>{ | ||
let ret = new Buffer(bytes).toString(encoding||'utf8'); | ||
// strip \r symbols on non-unix endlines | ||
if (ret[ret.length-1]=='\r') | ||
return ret.substr(0, ret.length-1); | ||
return ret; | ||
}; | ||
let gen_read_line_cb = (bytes, cb, opt)=>(buf, read)=>{ | ||
opt = opt||{}; | ||
let nl = '\n'.charCodeAt(), idx, last_idx = 0; | ||
let size = read ? Math.min(buf.length, read) : buf.length; | ||
for (idx=0; idx<size; idx++) | ||
E.fread_line_cb_e = (fd, cb, opt)=>{ | ||
opt = assign({encoding: 'utf8', buf_size: E.read_buf_size}, opt); | ||
cb = cb||(()=>false); | ||
let read, buf = new Buffer(opt.buf_size); | ||
let strbuf = '', lf_idx, decoder = new StringDecoder(opt.encoding); | ||
while (read = fs.readSync(fd, buf, 0, buf.length)) | ||
{ | ||
if (buf[idx]==nl) | ||
strbuf += decoder.write(buf.slice(0, read)); | ||
while ((lf_idx = strbuf.indexOf('\n'))>=0) | ||
{ | ||
bytes.push.apply(bytes, buf.slice(last_idx, idx)); | ||
let line = bytes2str(bytes, opt.encoding); | ||
bytes.length = 0; | ||
if (cb && cb(line)) | ||
if (cb(strbuf.slice(0, lf_idx-(strbuf[lf_idx-1]=='\r' ? 1 : 0)))) | ||
return true; | ||
last_idx = idx+1; | ||
strbuf = strbuf.slice(lf_idx+1); | ||
} | ||
} | ||
bytes.push.apply(bytes, buf.slice(last_idx, idx)); | ||
return opt.buf_size && size<opt.buf_size; | ||
if (strbuf) | ||
cb(strbuf); | ||
return true; | ||
}; | ||
E.read_line_cb_e = (filename, cb, opt)=>{ | ||
opt = opt||{}; | ||
opt.buf_size = opt.buf_size||E.read_buf_size; | ||
// collect bytes in array first, and later translate to utf8 to avoid | ||
// bugs in multi-byte utf8 chars at block boundry | ||
let bytes = []; | ||
E.read_cb_e(filename, 0, E.read_buf_size, 0, | ||
gen_read_line_cb(bytes, cb, opt)); | ||
if (bytes.length) | ||
cb(bytes2str(bytes)); | ||
return true; | ||
let fd = fs.openSync(filename, 'r'); | ||
try { return E.fread_line_cb_e(fd, cb, opt); } | ||
finally { fs.closeSync(fd); } | ||
}; | ||
@@ -96,3 +83,3 @@ E.read_line_e = filename=>{ | ||
let ret = E.read_e(filename).split(/\r?\n/); | ||
if (ret[ret.length-1]==='') | ||
if (ret[ret.length-1]=='') | ||
ret.pop(); | ||
@@ -102,11 +89,10 @@ return ret; | ||
E.fread_e = (fd, start, size)=>{ | ||
let buf, count = 0, ret = ''; | ||
start = start||0; | ||
buf = new Buffer(E.read_buf_size); | ||
E.fread_cb_e(fd, 0, E.read_buf_size, start, (buf, read)=>{ | ||
count += read; | ||
let len = size && size<=count ? Math.min(read, size) : read; | ||
ret += buf.slice(0, len); | ||
if (size && count<=0) | ||
return true; | ||
let decoder = new StringDecoder(), rest = size; | ||
let ret = '', append_ret = buf=>ret += decoder.write(buf); | ||
E.fread_cb_e(fd, start, rest===undefined ? append_ret : buf=>{ | ||
rest -= buf.length; | ||
if (rest<0) | ||
buf = buf.slice(0, rest); | ||
append_ret(buf); | ||
return rest<=0; | ||
}); | ||
@@ -166,3 +152,3 @@ return ret; | ||
mode = 0o777 & ~(process.umask&&process.umask()); | ||
if (typeof mode==='string') | ||
if (typeof mode=='string') | ||
mode = parseInt(mode, 8); | ||
@@ -230,4 +216,3 @@ let made = null; | ||
try { | ||
E.read_cb_e(src, 0, E.read_buf_size, 0, (buf, read)=>{ | ||
fs.writeSync(fdw, buf, 0, read); }); | ||
E.read_cb_e(src, 0, buf=>void(fs.writeSync(fdw, buf, 0, buf.length))); | ||
let owner = get_owner(stat, opt); | ||
@@ -240,2 +225,4 @@ if (owner) | ||
} finally { fs.closeSync(fdw); } | ||
if (opt.verbose) | ||
console.log(`Copy: ${src}->${dst}`); | ||
return true; | ||
@@ -245,5 +232,5 @@ } | ||
let files = E.readdir_e(src); | ||
for (let f=0; f<files.length; f++) | ||
for (let f of files) | ||
{ | ||
if (!E.copy_e(src+'/'+files[f], dst+'/'+files[f], opt)) | ||
if (!E.copy_e(src+'/'+f, dst+'/'+f, opt)) | ||
return false; | ||
@@ -254,2 +241,8 @@ } | ||
E.copy_e = (src, dst, opt)=>{ | ||
if (opt && opt.exclude && opt.exclude.test(src, dst)) | ||
{ | ||
if (opt.verbose) | ||
console.log(`Skipped coping: ${src}->${dst}`); | ||
return true; | ||
} | ||
src = E.normalize(src); | ||
@@ -305,4 +298,3 @@ dst = E.normalize(dst); | ||
let hash = crypto.createHash(type||'md5'); | ||
E.read_cb_e(filename, 0, E.read_buf_size, 0, (buf, read)=>{ | ||
hash.update(buf.slice(0, read)); }); | ||
E.read_cb_e(filename, 0, buf=>void(hash.update(buf))); | ||
return hash.digest('hex'); | ||
@@ -367,3 +359,3 @@ }; | ||
let err_retval = { | ||
read: null, read_line: null, read_lines: null, fread: null, find: null, | ||
read: null, read_line: null, read_lines: null, fread: null, | ||
tail: null, head: null, size: null, mkdirp: null, mkdirp_file: null, | ||
@@ -374,6 +366,6 @@ hashsum: null, stat: null, lstat: null, realpath: null, readlink: null, | ||
rmdir: undefined, rm_rf: false, touch: false, readdir: [], copy: false, | ||
link: false, link_r: false, symlink: false, mtime: -1, | ||
link: false, link_r: false, symlink: false, mtime: -1, find: null | ||
}; | ||
Object.keys(err_retval).forEach(method=> | ||
E[method] = errno_wrapper.bind(null, E[method+'_e'], err_retval[method])); | ||
for (let method in err_retval) | ||
E[method] = errno_wrapper.bind(null, E[method+'_e'], err_retval[method]); | ||
@@ -420,49 +412,2 @@ E.exists = path=>{ | ||
}; | ||
function is_binary(filename){ | ||
filename = E.normalize(filename); | ||
if (!E.is_file(filename)) | ||
throw new Error('Not a file'); | ||
let fd = fs.openSync(filename, 'r'); | ||
let buf = new Buffer(E.read_buf_size); | ||
let size = fs.readSync(fd, buf, 0, E.read_buf_size, 0); | ||
if (!size) | ||
throw new Error('Empty file'); | ||
// UTF-8 BOM | ||
if (size >= 3 && buf[0]==0xEF && buf[1]==0xBB && buf[2]==0xBF) | ||
return false; | ||
let bytes = 0; | ||
for (let i=0; i<size; i++) | ||
{ | ||
if (!buf[i]) | ||
return true; | ||
if ((buf[i]<7 || buf[i]>14) && (buf[i]<32 || buf[i]>127)) | ||
{ | ||
// UTF-8 detection | ||
if (buf[i]>193 && buf[i]<224 && i+1<size) | ||
{ | ||
i++; | ||
if (buf[i]>127 && buf[i]<192) | ||
continue; | ||
} | ||
else if (buf[i]>223 && buf[i]<240 && i+2<size) | ||
{ | ||
i++; | ||
if (buf[i]>127 && buf[i]<192 && buf[i+1]>127 && buf[i+1]<192) | ||
{ | ||
i++; | ||
continue; | ||
} | ||
} | ||
bytes++; | ||
// Read at least 32 bytes before making a decision | ||
if (i>32 && bytes*100/size > 10) | ||
return true; | ||
} | ||
} | ||
return bytes*100/size > 10; | ||
} | ||
E.is_binary = filename=>{ | ||
try { return is_binary(filename); } | ||
catch(e){ return false; } | ||
}; | ||
E.which = bin=>{ | ||
@@ -469,0 +414,0 @@ bin = E.normalize(bin); |
@@ -129,3 +129,3 @@ // LICENSE_CODE ZON ISC | ||
var handle_resp = function(){ | ||
if (xhr.readyState != 3 || xhr.status != 200) | ||
if (xhr.readyState!=3 || xhr.status!=200) | ||
return; | ||
@@ -187,3 +187,3 @@ // in konqueror xhr.responseText is sometimes null here | ||
return; | ||
if (prev_data_len != xhr.responseText.length) | ||
if (prev_data_len!=xhr.responseText.length) | ||
{ | ||
@@ -190,0 +190,0 @@ instance.ready(xhr.responseText); |
@@ -18,3 +18,3 @@ // LICENSE_CODE ZON ISC | ||
const E = exports; | ||
const assign = Object.assign, SEC = date.ms.SEC; | ||
const assign = Object.assign, SEC = date.ms.SEC, ef = etask.ef; | ||
let mongo_mutex = {}; | ||
@@ -105,3 +105,3 @@ let open_conns = {}; | ||
[selector, opt]); } | ||
catch(e){ handle_error(zmongo, 'findOne', e, selector); } | ||
catch(e){ ef(e); handle_error(zmongo, 'findOne', e, selector); } | ||
log_query('findOne', zmongo.name, selector, null, item, this); | ||
@@ -127,3 +127,3 @@ return item; | ||
try { items = yield etask.nfn_apply(cursor, '.toArray', []); } | ||
catch(e){ handle_error(zmongo, 'find.toArray', e, selector); } | ||
catch(e){ ef(e); handle_error(zmongo, 'find.toArray', e, selector); } | ||
log_query('toArray', zmongo.name, selector, null, items, this); | ||
@@ -144,3 +144,3 @@ return items; | ||
[selector, sort, update, opt]); } | ||
catch(e){ handle_error(zmongo, 'findAndModify', e, selector); } | ||
catch(e){ ef(e); handle_error(zmongo, 'findAndModify', e, selector); } | ||
log_query('findAndModify', zmongo.name, selector, update, res, this); | ||
@@ -157,3 +157,5 @@ return res.value; | ||
try { res = yield etask.nfn_apply(cursor, '.nextObject', []); } | ||
catch(e){ zexit('failed mongodb nextObject: %s %O', e, cursor.selector); } | ||
catch(e){ ef(e); | ||
zexit('failed mongodb nextObject: %s %O', e, cursor.selector); | ||
} | ||
log_query('nextObject', cursor.zmongo.name, cursor.selector, null, res, | ||
@@ -188,3 +190,3 @@ this); | ||
[selector, update, opt]); } | ||
catch(e){ handle_error(zmongo, 'update', e, selector, update); } | ||
catch(e){ ef(e); handle_error(zmongo, 'update', e, selector, update); } | ||
log_query('update', zmongo.name, selector, update, res, this); | ||
@@ -225,4 +227,6 @@ return res; | ||
try { res = yield etask.nfn_apply(zmongo.collection, '.update', | ||
[selector, modifier, opt]); } | ||
catch(e){ handle_error(zmongo, 'update_part', e, selector, update); } | ||
[selector, modifier, opt]); | ||
} catch(e){ ef(e); | ||
handle_error(zmongo, 'update_part', e, selector, update); | ||
} | ||
log_query('update_part', zmongo.name, selector, modifier, res, this); | ||
@@ -241,3 +245,3 @@ }); | ||
try { yield etask.nfn_apply(zmongo.db, '.eval', [f, params]); } | ||
catch(e){ handle_error(zmongo, 'eval', e, selector); throw e; } | ||
catch(e){ ef(e); handle_error(zmongo, 'eval', e, selector); throw e; } | ||
}); }; | ||
@@ -283,3 +287,3 @@ | ||
try { res = yield etask.nfn_apply(zmongo.collection, '.save', [obj]); } | ||
catch(e){ handle_error(zmongo, 'save', e, obj); } | ||
catch(e){ ef(e); handle_error(zmongo, 'save', e, obj); } | ||
log_query('save', zmongo.name, obj, null, res, this); | ||
@@ -297,3 +301,3 @@ return res._id; | ||
try { res = yield etask.nfn_apply(zmongo.collection, '.insert', [obj]); } | ||
catch(e){ handle_error(zmongo, 'insert', e, obj); } | ||
catch(e){ ef(e); handle_error(zmongo, 'insert', e, obj); } | ||
log_query('insert', zmongo.name, obj, null, res, this); | ||
@@ -313,4 +317,7 @@ return res; | ||
[selector]); } | ||
catch(e){ handle_error(zmongo, 'remove', e, selector); } | ||
catch(e){ ef(e); handle_error(zmongo, 'remove', e, selector); } | ||
log_query('remove', zmongo.name, selector, null, res, this); | ||
// XXX vladimir: verify and make default | ||
if (opt.retval) | ||
return res; | ||
}); | ||
@@ -328,3 +335,3 @@ | ||
[selector]); } | ||
catch(e){ handle_error(zmongo, 'count', e, selector); } | ||
catch(e){ ef(e); handle_error(zmongo, 'count', e, selector); } | ||
log_query('count', zmongo.name, selector, null, count, this); | ||
@@ -344,3 +351,3 @@ return count; | ||
[pipeline]); } | ||
catch(e){ handle_error(zmongo, 'aggregate', e, pipeline); } | ||
catch(e){ ef(e); handle_error(zmongo, 'aggregate', e, pipeline); } | ||
log_query('aggregate', zmongo.name, pipeline, null, agg, this); | ||
@@ -370,3 +377,3 @@ return agg; | ||
try { return yield etask.nfn_apply(bulk, '.execute', [opt]); } | ||
catch(e){ zexit('failed mongodb bulk execute: %s', e); } | ||
catch(e){ ef(e); zexit('failed mongodb bulk execute: %s', e); } | ||
}); | ||
@@ -381,3 +388,3 @@ | ||
try { options = yield etask.nfn_apply(zmongo.collection, '.options', []); } | ||
catch(e){ handle_error(zmongo, 'options', e); } | ||
catch(e){ ef(e); handle_error(zmongo, 'options', e); } | ||
log_query('options', zmongo.name, {}, null, options, this); | ||
@@ -449,3 +456,3 @@ return options; | ||
[url, config]); } | ||
catch(e){ | ||
catch(e){ ef(e); | ||
if (opt.no_zexit) | ||
@@ -466,3 +473,3 @@ { | ||
[collection]); } | ||
catch(e){ | ||
catch(e){ ef(e); | ||
if (opt.no_zexit) | ||
@@ -506,3 +513,3 @@ { | ||
try { return yield etask.nfn_apply(zmongo.collection, '.drop', []); } | ||
catch(e){ handle_error(zmongo, 'drop', e); } | ||
catch(e){ ef(e); handle_error(zmongo, 'drop', e); } | ||
}); | ||
@@ -525,3 +532,3 @@ | ||
try { return yield etask.nfn_apply(zmongo.db, '.command', args); } | ||
catch(e){ handle_error(zmongo, 'command', e); } | ||
catch(e){ ef(e); handle_error(zmongo, 'command', e); } | ||
}); | ||
@@ -533,3 +540,3 @@ E.use_power_of_2_sizes = (zmongo, collection, val)=>etask( | ||
{collMod: collection, usePowerOf2Sizes: val!==false}); } | ||
catch(e){ handle_error(zmongo, 'usePowerOf2Sizes', e); } | ||
catch(e){ ef(e); handle_error(zmongo, 'usePowerOf2Sizes', e); } | ||
}); | ||
@@ -635,3 +642,3 @@ | ||
changeset.$push = changeset.$push||{}; | ||
if (values.length == 1) | ||
if (values.length==1) | ||
changeset.$push[path] = values[0]; | ||
@@ -638,0 +645,0 @@ else |
@@ -9,8 +9,9 @@ // LICENSE_CODE ZON ISC | ||
const array = require('./array.js'); | ||
const zutil = require('./util.js'); | ||
const assert = require('assert'); | ||
const cli = require('./cli.js'); | ||
const exec = require('./exec.js'); | ||
const os = require('os'); | ||
const E = exports; | ||
const env = process.env; | ||
const qw = string.qw; | ||
const env = process.env, qw = string.qw, KB = 1024, MB = KB*KB; | ||
@@ -54,6 +55,6 @@ var distro_release; | ||
{ | ||
case 'MemTotal': mem.memtotal = m[2]*1024; break; | ||
case 'MemFree': mem.memfree = m[2]*1024; break; | ||
case 'Buffers': mem.buffers = m[2]*1024; break; | ||
case 'Cached': mem.cached = m[2]*1024; break; | ||
case 'MemTotal': mem.memtotal = m[2]*KB; break; | ||
case 'MemFree': mem.memfree = m[2]*KB; break; | ||
case 'Buffers': mem.buffers = m[2]*KB; break; | ||
case 'Cached': mem.cached = m[2]*KB; break; | ||
} | ||
@@ -84,3 +85,3 @@ } | ||
{ | ||
var info = cli.exec_get_lines(['lsb_release', '-i', '-r', '-c', '-s']); | ||
var info = exec.get_lines(['lsb_release', '-i', '-r', '-c', '-s']); | ||
distro_release = { | ||
@@ -111,3 +112,3 @@ id: info[0].toLowerCase(), | ||
E.swapon = function(){ | ||
if (!file.is_file(swapfile) || file.size(swapfile)<512*1024*1024) | ||
if (!file.is_file(swapfile) || file.size(swapfile)<512*MB) | ||
{ | ||
@@ -125,3 +126,3 @@ cli.exec_rt_e('rm -f '+swapfile); | ||
E.check_space = function(req){ | ||
return +cli.exec_get_line('df --output=avail -k / | grep -iv avail')>req; | ||
return +exec.get_line('df --output=avail -k / | grep -iv avail')>req; | ||
}; | ||
@@ -144,5 +145,3 @@ | ||
for (var i = 0; i<curr.length; i++) | ||
{ | ||
diff.push(cpu_diff(prev[i], curr[i])); | ||
} | ||
diff.all = cpu_diff(prev.all, curr.all); | ||
@@ -156,3 +155,3 @@ return diff; | ||
if (file.is_win) | ||
ll = cli.exec_get_lines('cat /proc/stat'); | ||
ll = exec.get_lines('cat /proc/stat'); | ||
else | ||
@@ -311,27 +310,21 @@ ll = file.read_lines_e('/proc/stat'); | ||
}; | ||
E.sockets_count = function(proto){ | ||
// XXX: read_lines_e will fail on high socket count | ||
var conns = file.read_lines_e('/proc/net/'+proto); | ||
var i, v = {total: 0, lo: 0, ext: 0, err: 0}; | ||
for (i in E.TCP) | ||
v[E.TCP[i]] = 0; | ||
for (i=1; i<conns.length; i++) | ||
{ | ||
var conn = conns[i], start; | ||
if (!conn) | ||
continue; | ||
if ((start = conn.indexOf(':'))==-1) | ||
{ | ||
v.err++; | ||
continue; | ||
} | ||
v.total++; | ||
if (conn.substr(start+2, 8)=='0100007F') | ||
v.lo++; | ||
else | ||
v.ext++; | ||
var state = E.TCP[+('0x'+conn.substr(start+30, 2))]; | ||
if (state) | ||
v[state]++; | ||
} | ||
E.sockets_count = proto=>{ | ||
let line_idx = -1, v = {total: 0, lo: 0, ext: 0, err: 0}; | ||
zutil.forEach(E.TCP, id=>v[id] = 0); | ||
file.read_line_cb_e('/proc/net/'+proto, conn=>{ | ||
line_idx++; | ||
if (!conn || !line_idx) | ||
return; | ||
let start; | ||
if ((start = conn.indexOf(':'))==-1) | ||
return void(v.err++); | ||
v.total++; | ||
if (conn.substr(start+2, 8)=='0100007F') | ||
v.lo++; | ||
else | ||
v.ext++; | ||
let state = E.TCP[+('0x'+conn.substr(start+30, 2))]; | ||
if (state) | ||
v[state]++; | ||
}); | ||
return v; | ||
@@ -356,3 +349,3 @@ }; | ||
// pgpgin/pgpgout are reported in KB | ||
return {read: vmstat.pgpgin*1024, write: vmstat.pgpgout*1024}; | ||
return {read: vmstat.pgpgin*KB, write: vmstat.pgpgout*KB}; | ||
}; | ||
@@ -359,0 +352,0 @@ |
@@ -15,3 +15,3 @@ // LICENSE_CODE ZON ISC | ||
var now = Date.now(); | ||
if (!rl.count || rl.ts+ms < now) | ||
if (!rl.count || rl.ts+ms<now) | ||
{ | ||
@@ -23,3 +23,3 @@ rl.count = 1; | ||
rl.count++; | ||
return rl.count <= n; | ||
return rl.count<=n; | ||
} | ||
@@ -43,3 +43,3 @@ | ||
var new_level = this.level + inc; | ||
if (new_level > this.size) | ||
if (new_level>this.size) | ||
return false; | ||
@@ -46,0 +46,0 @@ this.level = new_level; |
// LICENSE_CODE ZON ISC | ||
'use strict'; /*jslint node:true, es6:false*/ | ||
// this file is used both by node and by ff_addon. | ||
'use strict'; /*jslint node:true, es6:false*/ // ios | ||
// this file is used both by node, ff_addon and ios. | ||
var is_node = typeof module=='object' && module.exports && module.children; | ||
@@ -5,0 +5,0 @@ var is_ff_addon = typeof module=='object' && module.uri |
// LICENSE_CODE ZON ISC | ||
'use strict'; /*jslint node:true, es6:false*/ | ||
'use strict'; /*jslint node:true*/ | ||
require('./config.js'); | ||
@@ -4,0 +4,0 @@ var zutil = require('./util.js'); |
@@ -14,6 +14,5 @@ // LICENSE_CODE ZON ISC | ||
function sprintf(fmt /* args... */){ | ||
var _fmt = ':'+fmt; // protect against __proto__ | ||
if (!E.cache[_fmt]) | ||
if (!Object.prototype.hasOwnProperty.call(E.cache, fmt)) | ||
{ | ||
E.cache[_fmt] = E.parse(fmt); | ||
E.cache[fmt] = E.parse(fmt); | ||
E.cache_n++; | ||
@@ -23,3 +22,3 @@ if (E.cache_cb) | ||
} | ||
return E.cache[_fmt](arguments); | ||
return E.cache[fmt](arguments); | ||
} | ||
@@ -26,0 +25,0 @@ E.cache = {}; |
// LICENSE_CODE ZON ISC | ||
'use strict'; /*jslint browser:true*/ | ||
define(['jquery', 'jquery_cookie'], function($, jquery_cookie){ | ||
define(['cookie'], function(cookie){ | ||
var E = {}; | ||
@@ -18,5 +18,5 @@ var storage; | ||
var cookie_opt = {domain: '.'+domain, path: '/', expires: 30}; | ||
storage = {getItem: $.cookie, | ||
setItem: function(key, val){ $.cookie(key, val, cookie_opt); }, | ||
removeItem: function(key){ $.removeCookie(key, cookie_opt); }, | ||
storage = {getItem: cookie.get, | ||
setItem: function(key, val){ cookie.set(key, val, cookie_opt); }, | ||
removeItem: function(key){ cookie.remove(key, cookie_opt); }, | ||
}; | ||
@@ -23,0 +23,0 @@ } |
@@ -89,2 +89,4 @@ // LICENSE_CODE ZON ISC | ||
}; | ||
// merge lines | ||
E.nl2sp = function(){ return E.es6_str(arguments).replace(/\n\s*/g, ' '); }; | ||
return E; }); }()); |
@@ -185,8 +185,8 @@ // LICENSE_CODE ZON ISC | ||
}; | ||
for (var i = 0; i < len; ++i) | ||
for (var i = 0; i<len; ++i) | ||
{ | ||
var x = q[i]; | ||
var idx = x.indexOf('='); | ||
var kstr = idx >= 0 ? x.substr(0, idx) : x; | ||
var vstr = idx >= 0 ? x.substr(idx + 1): ''; | ||
var kstr = idx>=0 ? x.substr(0, idx) : x; | ||
var vstr = idx>=0 ? x.substr(idx + 1): ''; | ||
var k = unescape_val(kstr); | ||
@@ -276,2 +276,14 @@ var v = unescape_val(vstr); | ||
E.add_params = function(url, params){ | ||
var hash_pos = url.indexOf('#'), href = '', fragment = ''; | ||
if (hash_pos==-1) | ||
href = url; | ||
else | ||
{ | ||
href = url.substr(0, hash_pos); | ||
fragment = url.substr(hash_pos); | ||
} | ||
return href+(href.includes('?') ? '&' : '?')+params+fragment; | ||
}; | ||
return E; }); }()); |
@@ -23,8 +23,6 @@ // LICENSE_CODE ZON ISC | ||
if (E._is_mocha!==undefined) | ||
return E._is_mocha; | ||
if (typeof global!='undefined' && global.mocha_running) | ||
return E._is_mocha = true; | ||
if (typeof module=='undefined' || !module.exports) | ||
return E._is_mocha = false; | ||
return E._is_mocha = /\/_mocha/.test(process.argv[1]||''); | ||
return E._is_mocha; | ||
if (typeof process!='undefined') | ||
return E._is_mocha = process.env.IS_MOCHA||false; | ||
return E._is_mocha = false; | ||
}; | ||
@@ -107,2 +105,3 @@ | ||
// prefer to normally Object.assign() instead of extend() | ||
E.extend = function(obj){ // like _.extend | ||
@@ -262,2 +261,4 @@ for (var i=1; i<arguments.length; i++) | ||
E.has = function(o, path){ return E.get(o, path, has_unique)!==has_unique; }; | ||
E.own = function(o, prop){ | ||
return Object.prototype.hasOwnProperty.call(o, prop); }; | ||
@@ -308,2 +309,12 @@ E.bool_lookup = function(a, split){ | ||
// ctor must only have one prototype level | ||
// XXX vladislav: ES6 class is not supported for ctor | ||
E.inherit_init = function(obj, ctor, params){ | ||
var orig_proto = Object.getPrototypeOf(obj); | ||
var ctor_proto = Object.assign({}, ctor.prototype); | ||
Object.setPrototypeOf(ctor_proto, orig_proto); | ||
Object.setPrototypeOf(obj, ctor_proto); | ||
return ctor.apply(obj, params); | ||
}; | ||
return E; }); }()); |
// LICENSE_CODE ZON ISC | ||
'use strict'; /*jslint node:true, es6:false*/ | ||
'use strict'; /*jslint node:true, es6:false*/ // ios | ||
require('./config.js'); | ||
@@ -4,0 +4,0 @@ var fs = require('fs'); |
@@ -95,3 +95,3 @@ // LICENSE_CODE ZON ISC | ||
a.push({stmt: | ||
'var out = "", tmp, def, helpers;\n' | ||
'\'use strict\'; var out = "", tmp, def, helpers, g;\n' | ||
+'if ('+it+' && '+it+'.helpers)\n' | ||
@@ -98,0 +98,0 @@ +'{ helpers = '+it+'.helpers; g = helpers; }\n' |
181
util/zerr.js
@@ -6,6 +6,5 @@ // LICENSE_CODE ZON ISC | ||
var is_node = typeof module=='object' && module.exports && module.children; | ||
var is_node_ff = typeof module=='object' && module.exports; | ||
var is_ff_addon = typeof module=='object' && module.uri | ||
&& !module.uri.indexOf('resource://'); | ||
if (!is_node_ff) | ||
if (!is_node && !is_ff_addon) | ||
{ | ||
@@ -15,27 +14,19 @@ define = self.define; | ||
} | ||
else if (is_ff_addon) | ||
{ | ||
// XXX romank: tmp hack, find a better way to remove jquery | ||
define = function(deps, fn){ | ||
deps.pop(); | ||
return require('./require_node.js').define(module, '../')(deps, fn); | ||
}; | ||
process = {env: {}}; | ||
} | ||
else | ||
{ | ||
// XXX romank: tmp hack, find a better way to remove jquery | ||
define = function(deps, fn){ | ||
deps.pop(); | ||
return require('./require_node.js').define(module, '../')(deps, fn); | ||
}; | ||
process = global.process||require('_process'); | ||
require('./config.js'); | ||
var cluster = require('cluster'); | ||
var fs = require('fs'); | ||
var version = require('./version.js').version; | ||
define = require('./require_node.js').define(module, '../'); | ||
if (is_ff_addon) | ||
process = {env: {}}; | ||
else if (is_node) | ||
{ | ||
process = global.process||require('_process'); | ||
require('./config.js'); | ||
var cluster = require('cluster'); | ||
var fs = require('fs'); | ||
var version = require('./version.js').version; | ||
} | ||
} | ||
define(['/util/array.js', '/util/date.js', '/util/util.js', | ||
'/util/sprintf.js', '/util/rate_limit.js', '/util/escape.js', 'jquery'], | ||
function(array, date, zutil, sprintf, rate_limit, zescape, $){ | ||
'/util/sprintf.js', '/util/rate_limit.js', '/util/escape.js'], | ||
function(array, date, zutil, sprintf, rate_limit, zescape){ | ||
var E, _zerr; | ||
@@ -70,3 +61,3 @@ var env = process.env; | ||
if (!exp) | ||
zerr.crit(msg); | ||
zerr.crit(msg); | ||
}; | ||
@@ -89,9 +80,33 @@ | ||
E.perr = function(id, info, opt){ | ||
E.zerr('perr '+id+' '+JSON.stringify(info)); | ||
E.zerr('perr '+id+' '+E.json(info)); | ||
if (perr_pending && perr_pending.length<100) | ||
perr_pending.push(Array.from(arguments)); | ||
perr_pending.push(Array.from(arguments)); | ||
}; | ||
var perr_orig = E.perr; | ||
function wrap_perr(perr_fn){ | ||
var send = perr_fn, pre_send; | ||
if (typeof perr_fn!='function') | ||
{ | ||
send = perr_fn.send; | ||
pre_send = perr_fn.pre_send; | ||
} | ||
return function(id, info, opt){ | ||
opt = opt||{}; | ||
var ms = (opt.rate_limit && opt.rate_limit.ms)||date.ms.HOUR; | ||
var count = (opt.rate_limit && opt.rate_limit.count)||10; | ||
var rl_hash = perr_orig.rl_hash = perr_orig.rl_hash||{}; | ||
var rl = rl_hash[id] = rl_hash[id]||{}; | ||
if (pre_send) | ||
pre_send(id, info, opt); | ||
if (opt.rate_limit===false || rate_limit(rl, ms, count)) | ||
return send(id, info, opt); | ||
if (info && typeof info!='string') | ||
info = zerr.json(info); | ||
zerr('perr %s %s rate too high %s %d %d', id, info, zerr.json(rl), ms, | ||
count); | ||
// XXX mikhail: add dropped perrs reporting | ||
}; | ||
} | ||
E.perr_install = function(install_fn){ | ||
E.perr = install_fn(perr_orig, perr_pending||[]); | ||
E.perr = wrap_perr(install_fn(perr_orig, perr_pending||[])); | ||
perr_pending = null; | ||
@@ -112,2 +127,3 @@ }; | ||
E.on_exception = undefined; | ||
var in_exception; | ||
E.set_exception_handler = function(prefix, err_func){ | ||
@@ -120,2 +136,5 @@ E.on_exception = function(err){ | ||
} | ||
if (in_exception) | ||
return; | ||
in_exception = 1; | ||
err.sent_perr = true; | ||
@@ -125,2 +144,3 @@ // XXX amir: better not to get a prefix arg, it can be added by the | ||
err_func((prefix ? prefix+'_' : '')+'etask_typeerror', null, err); | ||
in_exception = 0; | ||
}; | ||
@@ -148,6 +168,6 @@ }; | ||
if (!level) | ||
return prev; | ||
return prev; | ||
var val = L[level] || L[level.replace(/^L/, '')]; | ||
if (val!==undefined) | ||
E.level = val; | ||
E.level = val; | ||
return prev; | ||
@@ -204,3 +224,3 @@ }; | ||
if (level>E.level) | ||
return; | ||
return; | ||
__zerr(level, args); | ||
@@ -214,11 +234,11 @@ }; | ||
{ | ||
stack = args.stack; | ||
__zerr(L.CRIT, [E.e2s(args)]); | ||
stack = args.stack; | ||
__zerr(L.CRIT, [E.e2s(args)]); | ||
} | ||
else | ||
{ | ||
var e = new Error(); | ||
stack = e.stack; | ||
__zerr(L.CRIT, arguments); | ||
console.error(stack); | ||
var e = new Error(); | ||
stack = e.stack; | ||
__zerr(L.CRIT, arguments); | ||
console.error(stack); | ||
} | ||
@@ -228,5 +248,5 @@ E.flush(); | ||
{ | ||
/*jslint -W087*/ | ||
debugger; | ||
process.exit(1); | ||
/*jslint -W087*/ | ||
debugger; | ||
process.exit(1); | ||
} | ||
@@ -237,3 +257,3 @@ var zcounter_file = require('./zcounter_file.js'); | ||
write_zexit_log({id: 'server_zexit', info: ''+args, ts: date.to_sql(), | ||
backtrace: stack, version: version, cid: server_cid}); | ||
backtrace: stack, version: version}); | ||
E.flush(); | ||
@@ -244,5 +264,2 @@ debugger; | ||
var server_cid = 0; | ||
E.set_server_cid = function(cid){ server_cid = cid; }; | ||
var write_zexit_log = function(json){ | ||
@@ -252,3 +269,3 @@ try { | ||
file.write_e(E.ZEXIT_LOG_DIR+'/'+date.to_log_file()+'_zexit_'+ | ||
process.pid+'.log', JSON.stringify(json), {mkdirp: 1}); | ||
process.pid+'.log', E.json(json), {mkdirp: 1}); | ||
} catch(e){ E.zerr(E.e2s(e)); } | ||
@@ -276,42 +293,46 @@ }; | ||
try { | ||
var fmt = ''+args[0]; | ||
var fmt_args = Array.prototype.slice.call(args, 1); | ||
/* XXX arik/bahaa HACK: use sprintf (note, console % options are | ||
* differnt than sprintf % options) */ | ||
s = (fmt+(fmt_args.length ? ' '+E.json(fmt_args) : '')) | ||
.substr(0, 1024); | ||
var prefix = date.to_sql_ms()+' '+L_STR[l]+': '; | ||
E.log.push(prefix+s); | ||
if (E.is(l)) | ||
{ | ||
var fmt = ''+args[0]; | ||
var fmt_args = Array.prototype.slice.call(args, 1); | ||
/* XXX arik/bahaa HACK: use sprintf (note, console % options are | ||
* differnt than sprintf % options) */ | ||
s = (fmt+(fmt_args.length ? ' '+E.json(fmt_args) : '')) | ||
.substr(0, 1024); | ||
var prefix = date.to_sql_ms()+' '+L_STR[l]+': '; | ||
E.log.push(prefix+s); | ||
if (E.is(l)) | ||
{ | ||
Function.prototype.apply.bind(console[console_method(l)], | ||
console)([prefix+fmt].concat(fmt_args)); | ||
} | ||
if (E.log.length>E.log.max_size) | ||
E.log.splice(0, E.log.length - E.log.max_size/2); | ||
} | ||
if (E.log.length>E.log.max_size) | ||
E.log.splice(0, E.log.length - E.log.max_size/2); | ||
} catch(err){ | ||
try { | ||
/* XXX arik: console may fail (or be null) during loading of new | ||
* version */ | ||
console.error('ERROR in zerr '+(err.stack||err)+' '+ | ||
E.json(arguments)); | ||
} catch(_err){} | ||
try { console.error('ERROR in zerr '+(err.stack||err), arguments); } | ||
catch(e){} | ||
} | ||
if (l<=L.CRIT) | ||
throw new Error(s); | ||
throw new Error(s); | ||
}; | ||
E._zerr = _zerr; | ||
var post = function(url, data){ | ||
var use_xdr = typeof XDomainRequest=='function' && | ||
!('withCredentials' in XMLHttpRequest.prototype); | ||
var req = use_xdr ? new XDomainRequest() : new XMLHttpRequest(); | ||
req.open('POST', url); | ||
if (req.setRequestHeader) | ||
{ | ||
req.setRequestHeader('Content-Type', | ||
'application/x-www-form-urlencoded; charset=UTF-8'); | ||
} | ||
req.send(zescape.qs(data)); | ||
}; | ||
var perr_transport = function(id, info, opt){ | ||
opt = zutil.clone(opt||{}); | ||
var qs = opt.qs||{}, data = opt.data||{}; | ||
var ms = (opt.rate_limit && opt.rate_limit.ms)||date.ms.HOUR; | ||
var count = (opt.rate_limit && opt.rate_limit.count)||10; | ||
var rl_hash = perr.rl_hash = perr.rl_hash||{}; | ||
var rl = rl_hash[id] = rl_hash[id]||{}; | ||
data.is_json = 1; | ||
if (info && typeof info!='string') | ||
info = zerr.json(info); | ||
info = zerr.json(info); | ||
if (opt.err && !info) | ||
info = ''+(opt.err.message||zerr.json(opt.err)); | ||
info = ''+(opt.err.message||zerr.json(opt.err)); | ||
data.info = info; | ||
@@ -322,17 +343,5 @@ qs.id = id; | ||
zerr._zerr(opt.level, ['perr '+id+(info ? ' info: '+info : '')+ | ||
(opt.bt ? '\n'+opt.bt : '')]); | ||
(opt.bt ? '\n'+opt.bt : '')]); | ||
} | ||
if (rate_limit(rl, ms, count)) | ||
{ | ||
return $.ajax(zescape.uri(E.conf.url_perr+'/perr', qs), { | ||
type: 'POST', data: data, dataType: 'json'}); | ||
} | ||
if (id=='be_perr_rate_limit') | ||
return; | ||
zerr('perr %s %s rate too high %s %d %d', id, info, zerr.json(rl), | ||
ms, count); | ||
opt.rate_limit = {ms: date.ms.HOUR, count: 1}; | ||
// Don't include 'info' in be_perr_rate_limit because it makes people | ||
// confuse it with the original perr | ||
return E.perr('be_perr_rate_limit', {id: id}, opt); | ||
return post(zescape.uri(E.conf.url_perr+'/perr', qs), data); | ||
}; | ||
@@ -342,4 +351,4 @@ | ||
while (pending.length) | ||
perr_transport.apply(null, pending.shift()); | ||
// set the zerr.perr stub to send to the clog serve | ||
perr_transport.apply(null, pending.shift()); | ||
// set the zerr.perr stub to send to the clog server | ||
return perr_transport; | ||
@@ -346,0 +355,0 @@ }; |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
261770
44
8398
18