123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- const npm = {
- path: require('path'),
- util: require('util'),
- patterns: require('../patterns')
- };
- function isNull(value) {
- return value === null || value === undefined;
- }
- function isText(txt) {
- return txt && typeof txt === 'string' && /\S/.test(txt);
- }
- function isDev() {
- const env = global.process.env.NODE_ENV || '';
- return env.toLowerCase().indexOf('dev') !== -1;
- }
- function addReadProperties(target, source) {
- for (const p in source) {
- addReadProp(target, p, source[p]);
- }
- }
- function addReadProp(obj, name, value, hidden) {
- Object.defineProperty(obj, name, {
- value,
- configurable: false,
- enumerable: !hidden,
- writable: false
- });
- }
- function getSafeConnection(cn) {
- const maskPassword = cs => cs.replace(/:(?![/])([^@]+)/, (_, m) => ':' + new Array(m.length + 1).join('#'));
- if (typeof cn === 'object') {
- const copy = Object.assign({}, cn);
- if (typeof copy.password === 'string') {
- copy.password = copy.password.replace(/./g, '#');
- }
- if (typeof copy.connectionString === 'string') {
- copy.connectionString = maskPassword(copy.connectionString);
- }
- return copy;
- }
- return maskPassword(cn);
- }
- function messageGap(level) {
- return ' '.repeat(level * 4);
- }
- function inherits(child, parent) {
- child.prototype.__proto__ = parent.prototype;
- }
- function getLocalStack(startIdx, maxLines) {
-
-
- startIdx = startIdx || 0;
- const endIdx = maxLines > 0 ? startIdx + maxLines : undefined;
- return new Error().stack
- .split('\n')
- .filter(line => line.match(/\(.+\)/))
- .slice(startIdx, endIdx)
- .join('\n');
- }
- function InternalError(error) {
- this.error = error;
- }
- function getIfHas(obj, prop) {
- const result = {valid: true};
- if (prop.indexOf('.') === -1) {
- result.has = prop in obj;
- result.target = obj;
- if (result.has) {
- result.value = obj[prop];
- }
- } else {
- const names = prop.split('.');
- let missing, target;
- for (let i = 0; i < names.length; i++) {
- const n = names[i];
- if (!n) {
- result.valid = false;
- return result;
- }
- if (!missing && hasProperty(obj, n)) {
- target = obj;
- obj = obj[n];
- } else {
- missing = true;
- }
- }
- result.has = !missing;
- if (result.has) {
- result.target = target;
- result.value = obj;
- }
- }
- return result;
- }
- function hasProperty(value, prop) {
- return (value && typeof value === 'object' && prop in value) ||
- value !== null && value !== undefined && value[prop] !== undefined;
- }
- function addInspection(type, cb) {
- type.prototype[npm.util.inspect.custom] = cb;
- }
- function isConnectivityError(err) {
- const code = err && typeof err.code === 'string' && err.code;
- const cls = code && code.substring(0, 2);
-
- return code === 'ECONNRESET' || (cls === '08' && code !== '08P01') || (cls === '57' && code !== '57014');
-
-
-
-
-
- }
- function toJson(data) {
- if (data !== undefined) {
- return JSON.stringify(data, (_, v) => typeof v === 'bigint' ? `${v}#bigint` : v)
- .replace(/"(-?\d+)#bigint"/g, (_, a) => a);
- }
- }
- const exp = {
- toJson,
- getIfHas,
- addInspection,
- InternalError,
- getLocalStack,
- isText,
- isNull,
- isDev,
- addReadProp,
- addReadProperties,
- getSafeConnection,
- messageGap,
- inherits,
- isConnectivityError
- };
- const mainFile = process.argv[1];
- exp.startDir = mainFile ? npm.path.dirname(mainFile) : process.cwd();
- module.exports = exp;
|