decentralized-internet
Version:
An NPM library of programs to create decentralized web and distributed computing projects
1 lines • 10.8 kB
JavaScript
;const Crypto=require("crypto"),Path=require("path"),Util=require("util"),Escape=require("./escape"),internals={};exports.clone=function(e,t){if("object"!=typeof e||null===e)return e;const r=(t=t||new Map).get(e);if(r)return r;let n,o=!1;if(Array.isArray(e))n=[],o=!0;else if(Buffer.isBuffer(e))n=new Buffer(e);else if(e instanceof Date)n=new Date(e.getTime());else if(e instanceof RegExp)n=new RegExp(e);else{const t=Object.getPrototypeOf(e);t&&t.isImmutable?n=e:(n=Object.create(t),o=!0)}if(t.set(e,n),o){const r=Object.getOwnPropertyNames(e);for(let o=0;o<r.length;++o){const s=r[o],a=Object.getOwnPropertyDescriptor(e,s);a&&(a.get||a.set)?Object.defineProperty(n,s,a):n[s]=exports.clone(e[s],t)}}return n},exports.merge=function(e,t,r,n){if(exports.assert(e&&"object"==typeof e,"Invalid target value: must be an object"),exports.assert(null==t||"object"==typeof t,"Invalid source value: must be null, undefined, or an object"),!t)return e;if(Array.isArray(t)){exports.assert(Array.isArray(e),"Cannot merge array onto an object"),!1===n&&(e.length=0);for(let r=0;r<t.length;++r)e.push(exports.clone(t[r]));return e}const o=Object.keys(t);for(let s=0;s<o.length;++s){const a=o[s],i=t[a];i&&"object"==typeof i?!e[a]||"object"!=typeof e[a]||Array.isArray(e[a])!==Array.isArray(i)||i instanceof Date||Buffer.isBuffer(i)||i instanceof RegExp?e[a]=exports.clone(i):exports.merge(e[a],i,r,n):null!=i?e[a]=i:!1!==r&&(e[a]=i)}return e},exports.applyToDefaults=function(e,t,r){if(exports.assert(e&&"object"==typeof e,"Invalid defaults value: must be an object"),exports.assert(!t||!0===t||"object"==typeof t,"Invalid options value: must be true, falsy or an object"),!t)return null;const n=exports.clone(e);return!0===t?n:exports.merge(n,t,!0===r,!1)},exports.cloneWithShallow=function(e,t){if(!e||"object"!=typeof e)return e;const r=internals.store(e,t),n=exports.clone(e);return internals.restore(n,e,r),n},internals.store=function(e,t){const r={};for(let n=0;n<t.length;++n){const o=t[n],s=exports.reach(e,o);void 0!==s&&(r[o]=s,internals.reachSet(e,o,void 0))}return r},internals.restore=function(e,t,r){const n=Object.keys(r);for(let o=0;o<n.length;++o){const s=n[o];internals.reachSet(e,s,r[s]),internals.reachSet(t,s,r[s])}},internals.reachSet=function(e,t,r){const n=t.split(".");let o=e;for(let e=0;e<n.length;++e){const t=n[e];e+1===n.length&&(o[t]=r),o=o[t]}},exports.applyToDefaultsWithShallow=function(e,t,r){if(exports.assert(e&&"object"==typeof e,"Invalid defaults value: must be an object"),exports.assert(!t||!0===t||"object"==typeof t,"Invalid options value: must be true, falsy or an object"),exports.assert(r&&Array.isArray(r),"Invalid keys"),!t)return null;const n=exports.cloneWithShallow(e,r);if(!0===t)return n;const o=internals.store(t,r);return exports.merge(n,t,!1,!1),internals.restore(n,t,o),n},exports.deepEqual=function(e,t,r,n){r=r||{prototype:!0};const o=typeof e;if(o!==typeof t)return!1;if("object"!==o||null===e||null===t)return e===t?0!==e||1/e==1/t:e!=e&&t!=t;if(-1!==(n=n||[]).indexOf(e))return!0;if(n.push(e),Array.isArray(e)){if(!Array.isArray(t))return!1;if(!r.part&&e.length!==t.length)return!1;for(let n=0;n<e.length;++n){if(r.part){let o=!1;for(let s=0;s<t.length;++s)if(exports.deepEqual(e[n],t[s],r)){o=!0;break}return o}if(!exports.deepEqual(e[n],t[n],r))return!1}return!0}if(Buffer.isBuffer(e)){if(!Buffer.isBuffer(t))return!1;if(e.length!==t.length)return!1;for(let r=0;r<e.length;++r)if(e[r]!==t[r])return!1;return!0}if(e instanceof Date)return t instanceof Date&&e.getTime()===t.getTime();if(e instanceof RegExp)return t instanceof RegExp&&e.toString()===t.toString();if(r.prototype&&Object.getPrototypeOf(e)!==Object.getPrototypeOf(t))return!1;const s=Object.getOwnPropertyNames(e);if(!r.part&&s.length!==Object.getOwnPropertyNames(t).length)return!1;for(let o=0;o<s.length;++o){const a=s[o],i=Object.getOwnPropertyDescriptor(e,a);if(i.get){if(!exports.deepEqual(i,Object.getOwnPropertyDescriptor(t,a),r,n))return!1}else if(!exports.deepEqual(e[a],t[a],r,n))return!1}return!0},exports.unique=((e,t)=>{let r;if(t){r=[];const n=new Set;e.forEach(e=>{const o=e[t];n.has(o)||(n.add(o),r.push(e))})}else r=Array.from(new Set(e));return r}),exports.mapToObject=function(e,t){if(!e)return null;const r={};for(let n=0;n<e.length;++n)t?e[n][t]&&(r[e[n][t]]=!0):r[e[n]]=!0;return r},exports.intersect=function(e,t,r){if(!e||!t)return[];const n=[],o=Array.isArray(e)?exports.mapToObject(e):e,s={};for(let e=0;e<t.length;++e)if(o[t[e]]&&!s[t[e]]){if(r)return t[e];n.push(t[e]),s[t[e]]=!0}return r?null:n},exports.contain=function(e,t,r){let n,o,s=null;if("object"!=typeof e||"object"!=typeof t||Array.isArray(e)||Array.isArray(t)?t=[].concat(t):(s=t,t=Object.keys(t)),r=r||{},exports.assert(arguments.length>=2,"Insufficient arguments"),exports.assert("string"==typeof e||"object"==typeof e,"Reference must be string or an object"),exports.assert(t.length,"Values array cannot be empty"),r.deep){n=exports.deepEqual;const e=r.hasOwnProperty("only"),t=r.hasOwnProperty("part");o={prototype:e?r.only:!!t&&!r.part,part:e?!r.only:!t||r.part}}else n=((e,t)=>e===t);let a=!1;const i=new Array(t.length);for(let e=0;e<i.length;++e)i[e]=0;if("string"==typeof e){let r="(";for(let e=0;e<t.length;++e){const n=t[e];exports.assert("string"==typeof n,"Cannot compare string reference to non-string value"),r+=(e?"|":"")+exports.escapeRegex(n)}const n=new RegExp(r+")","g");a=!!e.replace(n,(e,r)=>{const n=t.indexOf(r);return++i[n],""})}else if(Array.isArray(e))for(let r=0;r<e.length;++r){let s=!1;for(let a=0;a<t.length&&!1===s;++a)s=n(t[a],e[r],o)&&a;!1!==s?++i[s]:a=!0}else{const r=Object.getOwnPropertyNames(e);for(let c=0;c<r.length;++c){const p=r[c],l=t.indexOf(p);if(-1!==l){if(s&&!n(s[p],e[p],o))return!1;++i[l]}else a=!0}}let c=!1;for(let e=0;e<i.length;++e)if(c=c||!!i[e],r.once&&i[e]>1||!r.part&&!i[e])return!1;return(!r.only||!a)&&c},exports.flatten=function(e,t){const r=t||[];for(let t=0;t<e.length;++t)Array.isArray(e[t])?exports.flatten(e[t],r):r.push(e[t]);return r},exports.reach=function(e,t,r){if(!1===t||null==t)return e;"string"==typeof(r=r||{})&&(r={separator:r});const n=t.split(r.separator||".");let o=e;for(let e=0;e<n.length;++e){let s=n[e];if("-"===s[0]&&Array.isArray(o)&&(s=s.slice(1,s.length),s=o.length-s),!o||"object"!=typeof o&&"function"!=typeof o||!(s in o)||"object"!=typeof o&&!1===r.functions){exports.assert(!r.strict||e+1===n.length,"Missing segment",s,"in reach path ",t),exports.assert("object"==typeof o||!0===r.functions||"function"!=typeof o,"Invalid segment",s,"in reach path ",t),o=r.default;break}o=o[s]}return o},exports.reachTemplate=function(e,t,r){return t.replace(/{([^}]+)}/g,(t,n)=>{const o=exports.reach(e,n,r);return null==o?"":o})},exports.formatStack=function(e){const t=[];for(let r=0;r<e.length;++r){const n=e[r];t.push([n.getFileName(),n.getLineNumber(),n.getColumnNumber(),n.getFunctionName(),n.isConstructor()])}return t},exports.formatTrace=function(e){const t=[];for(let r=0;r<e.length;++r){const n=e[r];t.push((n[4]?"new ":"")+n[3]+" ("+n[0]+":"+n[1]+":"+n[2]+")")}return t},exports.callStack=function(e){const t=Error.prepareStackTrace;Error.prepareStackTrace=function(e,t){return t};const r={};Error.captureStackTrace(r,this);const n=r.stack;return Error.prepareStackTrace=t,exports.formatStack(n).slice(1+e)},exports.displayStack=function(e){const t=exports.callStack(void 0===e?1:e+1);return exports.formatTrace(t)},exports.abortThrow=!1,exports.abort=function(e,t){if("test"===process.env.NODE_ENV||!0===exports.abortThrow)throw new Error(e||"Unknown error");let r="";t||(r=exports.displayStack(1).join("\n\t")),console.log("ABORT: "+e+"\n\t"+r),process.exit(1)},exports.assert=function(e){if(e)return;if(2===arguments.length&&arguments[1]instanceof Error)throw arguments[1];let t=[];for(let e=1;e<arguments.length;++e)""!==arguments[e]&&t.push(arguments[e]);throw t=t.map(e=>"string"==typeof e?e:e instanceof Error?e.message:exports.stringify(e)),new Error(t.join(" ")||"Unknown error")},exports.Timer=function(){this.ts=0,this.reset()},exports.Timer.prototype.reset=function(){this.ts=Date.now()},exports.Timer.prototype.elapsed=function(){return Date.now()-this.ts},exports.Bench=function(){this.ts=0,this.reset()},exports.Bench.prototype.reset=function(){this.ts=exports.Bench.now()},exports.Bench.prototype.elapsed=function(){return exports.Bench.now()-this.ts},exports.Bench.now=function(){const e=process.hrtime();return 1e3*e[0]+e[1]/1e6},exports.escapeRegex=function(e){return e.replace(/[\^\$\.\*\+\-\?\=\!\:\|\\\/\(\)\[\]\{\}\,]/g,"\\$&")},exports.base64urlEncode=function(e,t){return exports.assert("string"==typeof e||Buffer.isBuffer(e),"value must be string or buffer"),(Buffer.isBuffer(e)?e:new Buffer(e,t||"binary")).toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/\=/g,"")},exports.base64urlDecode=function(e,t){if("string"!=typeof e)return new Error("Value not a string");if(!/^[\w\-]*$/.test(e))return new Error("Invalid character");const r=new Buffer(e,"base64");return"buffer"===t?r:r.toString(t||"binary")},exports.escapeHeaderAttribute=function(e){return exports.assert(/^[ \w\!#\$%&'\(\)\*\+,\-\.\/\:;<\=>\?@\[\]\^`\{\|\}~\"\\]*$/.test(e),"Bad attribute value ("+e+")"),e.replace(/\\/g,"\\\\").replace(/\"/g,'\\"')},exports.escapeHtml=function(e){return Escape.escapeHtml(e)},exports.escapeJavaScript=function(e){return Escape.escapeJavaScript(e)},exports.nextTick=function(e){return function(){const t=arguments;process.nextTick(()=>{e.apply(null,t)})}},exports.once=function(e){if(e._hoekOnce)return e;let t=!1;const r=function(){t||(t=!0,e.apply(null,arguments))};return r._hoekOnce=!0,r},exports.isInteger=Number.isSafeInteger,exports.ignore=function(){},exports.inherits=Util.inherits,exports.format=Util.format,exports.transform=function(e,t,r){exports.assert(null==e||"object"==typeof e||Array.isArray(e),"Invalid source object: must be null, undefined, an object, or an array");const n="object"==typeof r&&null!==r&&r.separator||".";if(Array.isArray(e)){const n=[];for(let o=0;o<e.length;++o)n.push(exports.transform(e[o],t,r));return n}const o={},s=Object.keys(t);for(let a=0;a<s.length;++a){const i=s[a],c=i.split(n),p=t[i];let l;exports.assert("string"==typeof p,'All mappings must be "." delineated strings');let f=o;for(;c.length>1;)f[l=c.shift()]||(f[l]={}),f=f[l];f[l=c.shift()]=exports.reach(e,p,r)}return o},exports.uniqueFilename=function(e,t){t=t?"."!==t[0]?"."+t:t:"",e=Path.resolve(e);const r=[Date.now(),process.pid,Crypto.randomBytes(8).toString("hex")].join("-")+t;return Path.join(e,r)},exports.stringify=function(){try{return JSON.stringify.apply(null,arguments)}catch(e){return"[Cannot display object: "+e.message+"]"}},exports.shallow=function(e){const t={},r=Object.keys(e);for(let n=0;n<r.length;++n){const o=r[n];t[o]=e[o]}return t};