decentralized-internet
Version:
An NPM library of programs to create decentralized web and distributed computing projects
1 lines • 7.74 kB
JavaScript
;const Assert=require("assert"),Crypto=require("crypto"),Path=require("path"),DeepEqual=require("./deep-equal"),Escape=require("./escape"),internals={};exports.deepEqual=DeepEqual,exports.clone=function(e,t={},r=null){if("object"!=typeof e||null===e)return e;const n=r||new Map,o=n.get(e);if(o)return o;let s,a=!1;const i=Array.isArray(e);if(i)s=[],a=!0;else if(Buffer.isBuffer(e))s=Buffer.from(e);else if(e instanceof Date)s=new Date(e.getTime());else if(e instanceof RegExp)s=new RegExp(e);else if(e instanceof Set){s=new Set,a=!0;for(const t of e)s.add(exports.clone(t))}else if(e instanceof Map){s=new Map,a=!0;for(let[t,r]of e)r=exports.clone(r),s.set(t,r)}else if(!1!==t.prototype){const t=Object.getPrototypeOf(e);t&&t.isImmutable?s=e:(s=Object.create(t),a=!0)}else s={},a=!0;if(n.set(e,s),a){const r=internals.keys(e,t);for(let o=0;o<r.length;++o){const a=r[o];if(i&&"length"===a)continue;const c=Object.getOwnPropertyDescriptor(e,a);c&&(c.get||c.set)?Object.defineProperty(s,a,c):Object.defineProperty(s,a,{enumerable:!c||c.enumerable,writable:!0,configurable:!0,value:exports.clone(e[a],t,n)})}i&&(s.length=e.length)}return s},internals.keys=function(e,t={}){return t.symbols?Reflect.ownKeys(e):Object.getOwnPropertyNames(e)},exports.merge=function(e,t,r=!0,n=!0){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"),n||(e.length=0);for(let r=0;r<t.length;++r)e.push(exports.clone(t[r]));return e}const o=internals.keys(t);for(let s=0;s<o.length;++s){const a=o[s];if("__proto__"===a||!Object.prototype.propertyIsEnumerable.call(t,a))continue;const 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:r&&(e[a]=i)}return e},exports.applyToDefaults=function(e,t,r=!1){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,r,!1)},exports.cloneWithShallow=function(e,t,r){if(!e||"object"!=typeof e)return e;const n=internals.store(e,t),o=exports.clone(e,r);return internals.restore(o,e,n),o},internals.store=function(e,t){const r=new Map;for(let n=0;n<t.length;++n){const o=t[n],s=exports.reach(e,o);"object"!=typeof s&&"function"!=typeof s||(r.set(o,s),internals.reachSet(e,o,void 0))}return r},internals.restore=function(e,t,r){for(const[n,o]of r)internals.reachSet(e,n,o),internals.reachSet(t,n,o)},internals.reachSet=function(e,t,r){const n=Array.isArray(t)?t: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.intersect=function(e,t,r=!1){if(!e||!t)return r?null:[];const n=[],o=Array.isArray(e)?new Set(e):e,s=new Set;for(const e of t)if(internals.has(o,e)&&!s.has(e)){if(r)return e;n.push(e),s.add(e)}return r?null:n},internals.has=function(e,t){return"function"==typeof e.has?e.has(t):void 0!==e[t]},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);else{s=t;const e=Object.getOwnPropertySymbols(t).filter(Object.prototype.propertyIsEnumerable.bind(t));t=[...Object.keys(t),...e]}if(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=void 0!==r.only,t=void 0!==r.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)){const s=!(!r.only||!r.once);if(s&&e.length!==t.length)return!1;for(let r=0;r<e.length;++r){let c=!1;for(let a=0;a<t.length&&!1===c;++a)s&&0!==i[a]||(c=n(t[a],e[r],o)&&a);!1!==c?++i[c]:a=!0}}else{const c=internals.keys(e,r);for(let r=0;r<c.length;++r){const l=c[r],p=t.indexOf(l);if(-1!==p){if(s&&!n(s[l],e[l],o))return!1;++i[p]}else a=!0}}if(r.only&&(a||!r.once))return!a;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 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=Array.isArray(t);exports.assert(!n||!r.separator,"Separator option no valid for array-based chain");const o=n?t:t.split(r.separator||".");let s=e;for(let e=0;e<o.length;++e){let n=o[e];if(Array.isArray(s)){const e=Number(n);Number.isInteger(e)&&e<0&&(n=s.length+e)}if(!s||"object"!=typeof s&&"function"!=typeof s||!(n in s)||"object"!=typeof s&&!1===r.functions){exports.assert(!r.strict||e+1===o.length,"Missing segment",n,"in reach path ",t),exports.assert("object"==typeof s||!0===r.functions||"function"!=typeof s,"Invalid segment",n,"in reach path ",t),s=r.default;break}s=s[n]}return s},exports.reachTemplate=function(e,t,r){return t.replace(/{([^}]+)}/g,(t,n)=>{const o=exports.reach(e,n,r);return null==o?"":o})},exports.assert=function(e,...t){if(e)return;if(1===t.length&&t[0]instanceof Error)throw t[0];const r=t.filter(e=>""!==e).map(e=>"string"==typeof e?e:e instanceof Error?e.message:exports.stringify(e));throw new Assert.AssertionError({message:r.join(" ")||"Unknown error",actual:!1,expected:!0,operator:"==",stackStartFunction:exports.assert})},exports.Bench=class{constructor(){this.ts=0,this.reset()}reset(){this.ts=exports.Bench.now()}elapsed(){return exports.Bench.now()-this.ts}static now(){const e=process.hrtime();return 1e3*e[0]+e[1]/1e6}},exports.escapeRegex=function(e){return e.replace(/[\^\$\.\*\+\-\?\=\!\:\|\\\/\(\)\[\]\{\}\,]/g,"\\$&")},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.escapeJson=function(e){return Escape.escapeJson(e)},exports.once=function(e){if(e._hoekOnce)return e;let t=!1;const r=function(...r){t||(t=!0,e(...r))};return r._hoekOnce=!0,r},exports.ignore=function(){},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(...e){try{return JSON.stringify.apply(null,e)}catch(e){return"[Cannot display object: "+e.message+"]"}},exports.wait=function(e){return new Promise(t=>setTimeout(t,e))},exports.block=function(){return new Promise(exports.ignore)};