@miso.ai/server-commons 0.5.4-beta.2 → 0.5.4-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -16,5 +16,5 @@
16
16
  "js-yaml": "^4.1.0",
17
17
  "toml": "^3.0.0"
18
18
  },
19
- "version": "0.5.4-beta.2"
19
+ "version": "0.5.4-beta.4"
20
20
  }
package/src/async.js ADDED
@@ -0,0 +1,4 @@
1
+ export async function delay(duration) {
2
+ // TODO: can accept abort singal
3
+ return new Promise((resolve) => setTimeout(resolve, duration));
4
+ }
package/src/index.js CHANGED
@@ -1,6 +1,12 @@
1
1
  export * from './object.js';
2
- export * as stream from './stream.js';
2
+ export * from './string.js';
3
3
  export * from './date.js';
4
4
  export * from './file.js';
5
5
  export * from './config.js';
6
+ export * from './async.js';
7
+
8
+ export * as stream from './stream.js';
9
+ export * as log from './log.js';
10
+
6
11
  export { default as TaskQueue } from './task-queue.js';
12
+ export { default as Resolution } from './resolution.js';
package/src/log.js ADDED
@@ -0,0 +1,96 @@
1
+ import { padLeft, padRight } from './string.js';
2
+
3
+ export const FATAL = 'fatal';
4
+ export const ERROR = 'error';
5
+ export const WARNING = 'warning';
6
+ export const INFO = 'info';
7
+ export const DEBUG = 'debug';
8
+
9
+ const LEVEL_VALUES = {
10
+ [FATAL]: 2,
11
+ [ERROR]: 4,
12
+ [WARNING]: 6,
13
+ [INFO]: 8,
14
+ [DEBUG]: 50,
15
+ };
16
+ const FALLBACK_LEVEL_VALUE = 40;
17
+
18
+ export const LEVELS = Object.keys(LEVEL_VALUES);
19
+
20
+ LEVELS.sort(compareLevel);
21
+
22
+ export function reachesThreshold(level, threshold) {
23
+ if (!LEVEL_VALUES[threshold]) {
24
+ throw new Error(`Unrecognized log level: ${threshold}`);
25
+ }
26
+ if (!level) {
27
+ throw new Error(`Level is absent: ${level}`);
28
+ }
29
+ return compareLevel(level, threshold) <= 0;
30
+ }
31
+
32
+ export function isError(level) {
33
+ return reachesThreshold(level, ERROR);
34
+ }
35
+
36
+ function compareLevel(a, b) {
37
+ return (LEVEL_VALUES[a] || FALLBACK_LEVEL_VALUE) - (LEVEL_VALUES[b] || FALLBACK_LEVEL_VALUE);
38
+ }
39
+
40
+ export function formatDuration(value) {
41
+ if (typeof value !== 'number') {
42
+ throw new Error(`Value must be a number: ${value}`);
43
+ }
44
+ value = Math.floor(value);
45
+ const ms = padLeft(`${value % 60000}`, 5, '0');
46
+ let min = '00';
47
+ let hour = '00';
48
+ if (value >= 60000) {
49
+ value = Math.floor(value / 60000);
50
+ min = padLeft(`${value % 60}`, 2, '0');
51
+ if (value >= 60) {
52
+ hour = padLeft(Math.floor(value / 60), 2, '0');
53
+ }
54
+ }
55
+ return `${hour}:${min}:${ms.substring(0, 2)}.${ms.substring(2, 5)}`;
56
+ }
57
+
58
+ export function formatBytes(value) {
59
+ let i = 0;
60
+ for (; i < 4; i++) {
61
+ if (value < 1024) {
62
+ break;
63
+ }
64
+ value /= 1024;
65
+ }
66
+ return `${Math.floor(value * 100) / 100} ${UNITS[i]}`;
67
+ }
68
+
69
+ const UNITS = ['B', 'KB', 'MB', 'GB', 'TB'];
70
+
71
+ export function formatTable(rows, { columnPadding: columnSpacing = 3 } = {}) {
72
+ const maxLens = [];
73
+ for (const cells of rows) {
74
+ for (let j = 0, len = cells.length; j < len; j++) {
75
+ const len = `${cells[j]}`.length;
76
+ if (!maxLens[j] || maxLens[j] < len) {
77
+ maxLens[j] = len;
78
+ }
79
+ }
80
+ }
81
+ const colSpc = ' '.repeat(columnSpacing);
82
+ let str = '';
83
+ for (let i = 0, len = rows.length; i < len; i++) {
84
+ if (i > 0) {
85
+ str += '\n';
86
+ }
87
+ const cells = rows[i];
88
+ for (let j = 0, len = cells.length; j < len; j++) {
89
+ if (j > 0) {
90
+ str += colSpc;
91
+ }
92
+ str += padRight(`${cells[j]}`, maxLens[j], ' ');
93
+ }
94
+ }
95
+ return str;
96
+ }
@@ -0,0 +1,12 @@
1
+ export default class Resolution {
2
+
3
+ constructor() {
4
+ const self = this;
5
+ self.promise = new Promise((resolve, reject) => {
6
+ self.resolve = resolve;
7
+ self.reject = reject;
8
+ });
9
+ Object.freeze(this);
10
+ }
11
+
12
+ }
package/src/stream.js CHANGED
@@ -12,12 +12,22 @@ export function parse() {
12
12
  export function stringify() {
13
13
  return new Transform({
14
14
  transform(chunk, _, callback) {
15
- callback(null, JSON.stringify(chunk) + '\n');
15
+ try {
16
+ callback(null, JSON.stringify(chunk) + '\n');
17
+ } catch(e) {
18
+ callback(null, `${chunk}` + '\n');
19
+ }
16
20
  },
17
21
  writableObjectMode: true,
18
22
  });
19
23
  }
20
24
 
25
+ export async function pipeline(...streams) {
26
+ return new Promise((resolve, reject) =>
27
+ _pipeline(...streams, err => err ? reject(err) : resolve())
28
+ );
29
+ }
30
+
21
31
  export async function pipelineToStdout(...streams) {
22
32
  return new Promise((resolve, reject) =>
23
33
  _pipeline(...streams, err => err ? reject(err) : resolve())
package/src/string.js ADDED
@@ -0,0 +1,32 @@
1
+ // From:
2
+ // https://github.com/jonschlinkert/pad-right
3
+
4
+ const DEFAULT_PAD = ' ';
5
+ const DEFAULT_PAD_5 = DEFAULT_PAD.repeat(5);
6
+ const DEFAULT_PAD_25 = DEFAULT_PAD.repeat(25);
7
+
8
+ function pad(left, val, num, str) {
9
+ const diff = num - val.length;
10
+ if (diff <= 0) {
11
+ return val;
12
+ }
13
+ const padding = buildPadding(diff, str);
14
+ return left ? padding + val : val + padding;
15
+ }
16
+
17
+ function buildPadding(count, str) {
18
+ // Breakpoints based on benchmarks to use the fastest approach
19
+ // for the given number of zeros
20
+ return str ? str.repeat(count) :
21
+ count <= 5 ? DEFAULT_PAD_5.slice(0, count) :
22
+ count <= 25 ? DEFAULT_PAD_25.slice(0, count) :
23
+ DEFAULT_PAD.repeat(count);
24
+ }
25
+
26
+ export function padLeft(val, num, str) {
27
+ return pad(true, val, num, str);
28
+ }
29
+
30
+ export function padRight(val, num, str) {
31
+ return pad(false, val, num, str);
32
+ }