@miso.ai/server-commons 0.6.0-beta.0 → 0.6.0

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
@@ -17,7 +17,8 @@
17
17
  "js-yaml": "^4.1.0",
18
18
  "log-update": "^5.0.1",
19
19
  "toml": "^3.0.0",
20
- "uuid": "^9.0.0"
20
+ "uuid": "^9.0.0",
21
+ "yargs": "^17.5.1"
21
22
  },
22
- "version": "0.6.0-beta.0"
23
+ "version": "0.6.0"
23
24
  }
package/src/index.js CHANGED
@@ -9,6 +9,7 @@ export * as sink from './sink/index.js';
9
9
  export * as buffer from './buffer/index.js';
10
10
  export * as stream from './stream/index.js';
11
11
  export * as log from './log/index.js';
12
+ export * as yargs from './yargs.js';
12
13
 
13
14
  export { default as Resolution } from './resolution.js';
14
15
  export { default as TaskQueue } from './task-queue.js';
@@ -61,12 +61,8 @@ export default class RateLimitingQueue {
61
61
  }, this._interval);
62
62
  }
63
63
 
64
- async _exec(fn, res) {
65
- try {
66
- res.resolve(await fn());
67
- } catch(error) {
68
- res.reject(error);
69
- }
64
+ _exec(fn, res) {
65
+ res.resolveWith(fn);
70
66
  }
71
67
 
72
- }
68
+ }
package/src/resolution.js CHANGED
@@ -9,7 +9,7 @@ export default class Resolution {
9
9
  Object.freeze(this);
10
10
  }
11
11
 
12
- async execute(fn) {
12
+ async resolveWith(fn) {
13
13
  try {
14
14
  this.resolve(await fn());
15
15
  } catch(error) {
@@ -60,3 +60,19 @@ export function transform(fn, { transform: _, ...options } = {}) {
60
60
  },
61
61
  });
62
62
  }
63
+
64
+ export function take(n, { transform: _, ...options } = {}) {
65
+ let count = 0;
66
+ return new Transform({
67
+ ...options,
68
+ transform (chunk, _, next) {
69
+ if (count > n) {
70
+ next(undefined);
71
+ this.end();
72
+ } else {
73
+ next(undefined, chunk);
74
+ }
75
+ count++;
76
+ }
77
+ });
78
+ }
@@ -17,6 +17,7 @@ export default class OutputStream extends Writable {
17
17
  }
18
18
 
19
19
  _write(record, _, next) {
20
+ //console.error(record);
20
21
  this._out.write(this._format(record) + '\n');
21
22
  next();
22
23
  }
@@ -24,7 +25,7 @@ export default class OutputStream extends Writable {
24
25
  }
25
26
 
26
27
  function defaultObjectModeFormat(v) {
27
- typeof v === 'object' ? JSON.stringify(v) : `${v}`;
28
+ return typeof v === 'object' ? JSON.stringify(v) : `${v}`;
28
29
  }
29
30
 
30
31
  function defaultNonObjectModeFormat(v) {
package/src/string.js CHANGED
@@ -30,3 +30,11 @@ export function padLeft(val, num, str) {
30
30
  export function padRight(val, num, str) {
31
31
  return pad(false, val, num, str);
32
32
  }
33
+
34
+ export function encodeURIIfNecessary(str) {
35
+ return typeof str === 'string' && !str.includes('%') ? encodeURI(str) : str;
36
+ }
37
+
38
+ export function encodeURIComponentIfNecessary(str) {
39
+ return typeof str === 'string' && !str.includes('%') ? encodeURIComponent(str) : str;
40
+ }
package/src/yargs.js ADDED
@@ -0,0 +1,81 @@
1
+ import 'dotenv/config';
2
+ import _yargs from 'yargs/yargs';
3
+ import { hideBin } from 'yargs/helpers';
4
+
5
+ function hasDefaultCommand(args) {
6
+ let args0 = args[0];
7
+ const command = typeof args0 === 'object' ? args0.command : typeof args0 === 'string' ? args0 : undefined;
8
+ if (!command) {
9
+ return false; // this should not happen?
10
+ }
11
+ for (const c of Array.isArray(command) ? command : [command]) {
12
+ if (c.startsWith('*') || c.startsWith('$0')) {
13
+ return true;
14
+ }
15
+ }
16
+ return false;
17
+ }
18
+
19
+ class Tracker {
20
+
21
+ constructor() {}
22
+
23
+ get hasDefaultCommand() {
24
+ return !!this._hasDefaultCommand;
25
+ }
26
+
27
+ shim(yargs) {
28
+ const _command = yargs.command;
29
+ yargs.command = (...args) => {
30
+ if (hasDefaultCommand(args)) {
31
+ this._hasDefaultCommand = true;
32
+ }
33
+ return _command.apply(yargs, args);
34
+ };
35
+ return yargs;
36
+ }
37
+
38
+ }
39
+
40
+ export function build(fn) {
41
+ handleEpipe();
42
+ const yargs = _yargs(hideBin(process.argv));
43
+
44
+ const tracker = new Tracker();
45
+ tracker.shim(yargs);
46
+
47
+ fn(yargs);
48
+
49
+ if (!tracker.hasDefaultCommand) {
50
+ yargs.command('*', '', () => {}, () => yargs.showHelp())
51
+ }
52
+
53
+ return yargs
54
+ .help()
55
+ .fail(handleFail)
56
+ .parse();
57
+ }
58
+
59
+ let _epipeHandled = false;
60
+
61
+ export function handleEpipe() {
62
+ if (_epipeHandled) {
63
+ return;
64
+ }
65
+ _epipeHandled = true;
66
+ process.stdout.on('error', err => err.code == 'EPIPE' && process.exit(0));
67
+ }
68
+
69
+ export function handleFail(msg, err) {
70
+ if (err) {
71
+ throw err;
72
+ }
73
+ console.error(msg);
74
+ process.exit(1);
75
+ }
76
+
77
+ export function coerceToArray(arg) {
78
+ return Array.isArray(arg) ? arg :
79
+ typeof arg === 'string' ? arg.split(',') :
80
+ arg === undefined || arg === null ? [] : [arg];
81
+ }