1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function __export(m) {
|
19 | for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
20 | }
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | const child_process_1 = require("child_process");
|
23 | const _ = require("lodash");
|
24 | const crypto = require("crypto");
|
25 | const path = require("path");
|
26 | const moment = require("moment-timezone");
|
27 | const util = require("util");
|
28 | const UUID = require("uuid");
|
29 | const UUID_v5 = require("uuid/v5");
|
30 | const yargs = require("yargs-parser");
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function applyFuncFor(func, thisArg) {
|
40 | return function () {
|
41 | return func.apply(thisArg, arguments);
|
42 | };
|
43 | }
|
44 | exports.applyFuncFor = applyFuncFor;
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function asArray(val, noEmpty = true) {
|
54 | if (!Array.isArray(val)) {
|
55 | val = [val];
|
56 | }
|
57 | return val.filter(x => {
|
58 | if (noEmpty) {
|
59 | return !_.isNil(x);
|
60 | }
|
61 | return true;
|
62 | });
|
63 | }
|
64 | exports.asArray = asArray;
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function asLocal(time) {
|
73 | let result = asMoment(time);
|
74 | if (!_.isNil(result)) {
|
75 | if (result.isValid()) {
|
76 | if (!result.isLocal()) {
|
77 | result = result.local();
|
78 | }
|
79 | }
|
80 | }
|
81 | return result;
|
82 | }
|
83 | exports.asLocal = asLocal;
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function asMoment(val) {
|
92 | if (_.isNil(val)) {
|
93 | return val;
|
94 | }
|
95 | let result;
|
96 | if (moment.isMoment(val)) {
|
97 | result = val;
|
98 | }
|
99 | else if (moment.isDate(val)) {
|
100 | result = moment(val);
|
101 | }
|
102 | else {
|
103 | let unix = parseInt(toStringSafe(val).trim());
|
104 | if (isNaN(unix)) {
|
105 | result = moment(toStringSafe(val));
|
106 | }
|
107 | else {
|
108 | result = moment(unix);
|
109 | }
|
110 | }
|
111 | return result;
|
112 | }
|
113 | exports.asMoment = asMoment;
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | function asUTC(time) {
|
122 | let result = asMoment(time);
|
123 | if (!_.isNil(result)) {
|
124 | if (result.isValid()) {
|
125 | if (!result.isUTC()) {
|
126 | result = result.utc();
|
127 | }
|
128 | }
|
129 | }
|
130 | return result;
|
131 | }
|
132 | exports.asUTC = asUTC;
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | function cloneObj(obj) {
|
141 | if (!obj) {
|
142 | return obj;
|
143 | }
|
144 | return JSON.parse(JSON.stringify(obj));
|
145 | }
|
146 | exports.cloneObj = cloneObj;
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | function compareValues(x, y) {
|
156 | return compareValuesBy(x, y, i => i);
|
157 | }
|
158 | exports.compareValues = compareValues;
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | function compareValuesBy(x, y, selector) {
|
169 | const VAL_X = selector(x);
|
170 | const VAL_Y = selector(y);
|
171 | if (VAL_X !== VAL_Y) {
|
172 | if (VAL_X < VAL_Y) {
|
173 | return -1;
|
174 | }
|
175 | if (VAL_X > VAL_Y) {
|
176 | return 1;
|
177 | }
|
178 | }
|
179 | return 0;
|
180 | }
|
181 | exports.compareValuesBy = compareValuesBy;
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | function createCompletedAction(resolve, reject) {
|
191 | let completedInvoked = false;
|
192 | return (err, result) => {
|
193 | if (completedInvoked) {
|
194 | return;
|
195 | }
|
196 | completedInvoked = true;
|
197 | if (err) {
|
198 | if (reject) {
|
199 | reject(err);
|
200 | }
|
201 | }
|
202 | else {
|
203 | if (resolve) {
|
204 | resolve(result);
|
205 | }
|
206 | }
|
207 | };
|
208 | }
|
209 | exports.createCompletedAction = createCompletedAction;
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | async function exec(command) {
|
218 | command = toStringSafe(command);
|
219 | return new Promise((resolve, reject) => {
|
220 | try {
|
221 | child_process_1.exec(command, (err, stdout, stderr) => {
|
222 | if (err) {
|
223 | reject(err);
|
224 | }
|
225 | else {
|
226 | resolve({
|
227 | stderr: stderr,
|
228 | stdout: stdout,
|
229 | });
|
230 | }
|
231 | });
|
232 | }
|
233 | catch (e) {
|
234 | reject(e);
|
235 | }
|
236 | });
|
237 | }
|
238 | exports.exec = exec;
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | async function forEachAsync(seq, action) {
|
246 | let i = -1;
|
247 | for (const ITEM of seq) {
|
248 | ++i;
|
249 | await Promise.resolve(action(ITEM, i));
|
250 | }
|
251 | }
|
252 | exports.forEachAsync = forEachAsync;
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | function getAppVersionSync(opts) {
|
261 | if (!opts) {
|
262 | opts = {};
|
263 | }
|
264 |
|
265 | let cwd = toStringSafe(opts.cwd);
|
266 | if (isEmptyString(cwd)) {
|
267 | cwd = process.cwd();
|
268 | }
|
269 | if (!path.isAbsolute(cwd)) {
|
270 | cwd = path.join(process.cwd(), cwd);
|
271 | }
|
272 | cwd = path.resolve(cwd);
|
273 | const VERSION = {};
|
274 | try {
|
275 | VERSION.date = asUTC(moment(child_process_1.execSync('git log -n1 --pretty=%cI HEAD', {
|
276 | cwd: cwd,
|
277 | }).toString('utf8')
|
278 | .trim()));
|
279 | }
|
280 | catch (_a) {
|
281 | VERSION.date = false;
|
282 | }
|
283 | try {
|
284 | VERSION.hash = normalizeString(child_process_1.execSync('git log --pretty="%H" -n1 HEAD', {
|
285 | cwd: cwd,
|
286 | }).toString('utf8'));
|
287 | }
|
288 | catch (_b) {
|
289 | VERSION.hash = false;
|
290 | }
|
291 | return VERSION;
|
292 | }
|
293 | exports.getAppVersionSync = getAppVersionSync;
|
294 |
|
295 |
|
296 |
|
297 | function guid(version) {
|
298 | return uuid.apply(null, arguments);
|
299 | }
|
300 | exports.guid = guid;
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 | function isEmptyString(val) {
|
309 | return '' === toStringSafe(val)
|
310 | .trim();
|
311 | }
|
312 | exports.isEmptyString = isEmptyString;
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 | function normalizeString(val) {
|
321 | return toStringSafe(val).toLowerCase()
|
322 | .trim();
|
323 | }
|
324 | exports.normalizeString = normalizeString;
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 | function now(timezone) {
|
333 | timezone = toStringSafe(timezone).trim();
|
334 | const NOW = moment();
|
335 | return '' === timezone ? NOW
|
336 | : NOW.tz(timezone);
|
337 | }
|
338 | exports.now = now;
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | function parseCommandLine(cmd) {
|
347 | cmd = toStringSafe(cmd).trim();
|
348 | const ARGS = yargs(cmd);
|
349 | cmd = normalizeString(ARGS._[0]);
|
350 | ARGS._ = ARGS._.filter((a, i) => i > 0).map((a) => {
|
351 | if (_.isString(a)) {
|
352 | if (a.startsWith('"') && a.endsWith('"')) {
|
353 | a = a.substr(1, a.length - 2);
|
354 | a = a.split('\\"')
|
355 | .join('"');
|
356 | }
|
357 | }
|
358 | return a;
|
359 | });
|
360 | if ('' === cmd) {
|
361 | cmd = undefined;
|
362 | }
|
363 | return {
|
364 | arguments: ARGS,
|
365 | command: cmd,
|
366 | };
|
367 | }
|
368 | exports.parseCommandLine = parseCommandLine;
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 | async function randChars(size, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
|
378 | return randCharsInner(await util.promisify(crypto.randomBytes)(size), chars);
|
379 | }
|
380 | exports.randChars = randChars;
|
381 | function randCharsInner(randBlob, chars) {
|
382 | chars = toStringSafe(chars);
|
383 | let str = '';
|
384 | for (let i = 0; i < randBlob.length; i++) {
|
385 | str += chars.substr(randBlob.readInt8(i) % chars.length, 1);
|
386 | }
|
387 | return str;
|
388 | }
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 | function randCharsSync(size, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
|
398 | return randCharsInner(crypto.randomBytes(size), chars);
|
399 | }
|
400 | exports.randCharsSync = randCharsSync;
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 | function toBooleanSafe(val, defaultValue = false) {
|
410 | if (_.isBoolean(val)) {
|
411 | return val;
|
412 | }
|
413 | if (_.isNil(val)) {
|
414 | return !!defaultValue;
|
415 | }
|
416 | return !!val;
|
417 | }
|
418 | exports.toBooleanSafe = toBooleanSafe;
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 | function toStringSafe(val, defaultValue = '') {
|
428 | if (_.isString(val)) {
|
429 | return val;
|
430 | }
|
431 | if (_.isNil(val)) {
|
432 | return '' + defaultValue;
|
433 | }
|
434 | if (val instanceof Error) {
|
435 | return `[${val.name}] ${val.message}${_.isNil(val.stack) ? '' : ("\n\n" + val.stack)}`;
|
436 | }
|
437 | if (_.isFunction(val['toString'])) {
|
438 | return '' + val.toString();
|
439 | }
|
440 | if (Array.isArray(val) || _.isObjectLike(val)) {
|
441 | return JSON.stringify(val);
|
442 | }
|
443 | return '' + val;
|
444 | }
|
445 | exports.toStringSafe = toStringSafe;
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 | function utc() {
|
452 | return moment.utc();
|
453 | }
|
454 | exports.utc = utc;
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 | function uuid(version, ...args) {
|
464 | version = normalizeString(version);
|
465 | switch (version) {
|
466 | case '':
|
467 | case '4':
|
468 | case 'v4':
|
469 | return UUID.v4
|
470 | .apply(null, args);
|
471 | case '1':
|
472 | case 'v1':
|
473 | return UUID.v1
|
474 | .apply(null, args);
|
475 | case '5':
|
476 | case 'v5':
|
477 | return UUID_v5.apply(null, args);
|
478 | }
|
479 | throw new Error(`Version '${version}' is not supported`);
|
480 | }
|
481 | exports.uuid = uuid;
|
482 | __export(require("./apis/host"));
|
483 | __export(require("./apis/index"));
|
484 | __export(require("./apis/statistics"));
|
485 | __export(require("./apis/validation"));
|
486 | __export(require("./azure/storage"));
|
487 | __export(require("./cache"));
|
488 | __export(require("./cache/redis"));
|
489 | __export(require("./dev"));
|
490 | __export(require("./diagnostics/logger"));
|
491 | __export(require("./diagnostics/stopwatch"));
|
492 | __export(require("./env"));
|
493 | __export(require("./events"));
|
494 | __export(require("./fs"));
|
495 | __export(require("./geo"));
|
496 | __export(require("./http"));
|
497 | __export(require("./http/websockets"));
|
498 | __export(require("./mail"));
|
499 | __export(require("./mongo/index"));
|
500 | __export(require("./mongo/statistics"));
|
501 | __export(require("./oauth/microsoft"));
|
502 | __export(require("./queues/index"));
|
503 | __export(require("./schemas"));
|
504 | __export(require("./statistics"));
|
505 | __export(require("./streams"));
|
506 | __export(require("./strings"));
|
507 | __export(require("./system"));
|
508 | __export(require("./zip/builder"));
|
509 | var node_enumerable_1 = require("node-enumerable");
|
510 | exports.asEnumerable = node_enumerable_1.asEnumerable;
|
511 | exports.from = node_enumerable_1.from;
|
512 | exports.isEnumerable = node_enumerable_1.isEnumerable;
|
513 | exports.isSequence = node_enumerable_1.isSequence;
|
514 | exports.popFrom = node_enumerable_1.popFrom;
|
515 | exports.random = node_enumerable_1.random;
|
516 | exports.range = node_enumerable_1.range;
|
517 | exports.repeat = node_enumerable_1.repeat;
|
518 | exports.shiftFrom = node_enumerable_1.shiftFrom;
|
519 | var swagger_jsdoc_express_1 = require("swagger-jsdoc-express");
|
520 | exports.setupSwaggerUIFromSourceFiles = swagger_jsdoc_express_1.setupSwaggerUIFromSourceFiles;
|
521 |
|
\ | No newline at end of file |