@miso.ai/server-commons 0.5.2 → 0.5.4-beta.1

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
@@ -12,6 +12,9 @@
12
12
  "contributors": [
13
13
  "simonpai <simon.pai@askmiso.com>"
14
14
  ],
15
- "dependencies": {},
16
- "version": "0.5.2"
15
+ "dependencies": {
16
+ "js-yaml": "^4.1.0",
17
+ "toml": "^3.0.0"
18
+ },
19
+ "version": "0.5.4-beta.1"
17
20
  }
package/src/config.js ADDED
@@ -0,0 +1,70 @@
1
+ import { dirname } from 'path';
2
+ import { access, readFile, mkdir, writeFile } from 'fs/promises';
3
+ import { accessSync, readFileSync, mkdirSync, writeFileSync, constants } from 'fs';
4
+ import yaml from 'js-yaml';
5
+ import toml from 'toml';
6
+
7
+ export async function loadConfig(file) {
8
+ await access(file, constants.R_OK);
9
+ const content = await readFile(file, { encoding: 'utf8' });
10
+ return parseConfig(file, content);
11
+ }
12
+
13
+ export function loadConfigSync(file) {
14
+ accessSync(file, constants.R_OK);
15
+ const content = readFileSync(file, { encoding: 'utf8' });
16
+ return parseConfig(file, content);
17
+ }
18
+
19
+ function parseConfig(file, content) {
20
+ const ext = getFileExtension(file);
21
+ switch (ext) {
22
+ case 'yml':
23
+ case 'yaml':
24
+ return yaml.load(content);
25
+ case 'toml':
26
+ return toml.parse(content);
27
+ case 'json':
28
+ return JSON.parse(content);
29
+ default:
30
+ throw new Error(`Unrecognized file format: ${file}`);
31
+ }
32
+ }
33
+
34
+ export async function saveConfig(file, config) {
35
+ // TODO: check if valid file URL
36
+ await mkdir(dirname(file), { recursive: true });
37
+ const ext = getFileExtension(file);
38
+ const content = formatConfig(ext, config);
39
+ await writeFile(file, content, { encoding: 'utf8' });
40
+ }
41
+
42
+ export function saveConfigSync(file, config) {
43
+ // TODO: check if valid file URL
44
+ mkdirSync(dirname(file), { recursive: true });
45
+ const ext = getFileExtension(file);
46
+ const content = formatConfig(ext, config);
47
+ writeFileSync(file, content, { encoding: 'utf8' });
48
+ }
49
+
50
+ function formatConfig(ext, config) {
51
+ switch (ext) {
52
+ case 'yml':
53
+ case 'yaml':
54
+ throw new Error(`Unimplemented yet.`);
55
+ case 'toml':
56
+ throw new Error(`Unimplemented yet.`);
57
+ case 'json':
58
+ return JSON.stringify(config, undefined, 2);
59
+ default:
60
+ throw new Error(`Unrecognized file format: ${ext}`);
61
+ }
62
+ }
63
+
64
+ function getFileExtension(file) {
65
+ const i = file.lastIndexOf('.');
66
+ if (i < 0) {
67
+ throw new Error(`Unknown file type: ${file}`);
68
+ }
69
+ return file.substring(i + 1);
70
+ }
package/src/file.js ADDED
@@ -0,0 +1,20 @@
1
+ import { access } from 'fs/promises';
2
+ import { accessSync, constants } from 'fs';
3
+
4
+ export async function fileExists(file, mode = constants.F_OK) {
5
+ try {
6
+ await access(file, mode);
7
+ return true;
8
+ } catch(_) {
9
+ return false;
10
+ }
11
+ }
12
+
13
+ export function fileExistsSync(file, mode = constants.F_OK) {
14
+ try {
15
+ accessSync(file, mode);
16
+ return true;
17
+ } catch(_) {
18
+ return false;
19
+ }
20
+ }
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './object.js';
2
- export * from './functions.js';
3
- export * from './stream.js';
2
+ export * as stream from './stream.js';
4
3
  export * from './date.js';
4
+ export * from './file.js';
5
+ export * from './config.js';
5
6
  export { default as TaskQueue } from './task-queue.js';
package/src/object.js CHANGED
@@ -13,6 +13,17 @@ export function trimObj(obj) {
13
13
  return obj;
14
14
  }
15
15
 
16
+ export function splitObj(source, propNames) {
17
+ const propSet = new Set(propNames);
18
+ const a = {}, b = {};
19
+ for (const name in source) {
20
+ if (source.hasOwnProperty(name)) {
21
+ (propSet.has(name) ? a : b)[name] = source[name];
22
+ }
23
+ }
24
+ return [a, b];
25
+ }
26
+
16
27
  export function asArray(value) {
17
28
  return Array.isArray(value) ? value : value === undefined ? [] : [value];
18
29
  }
@@ -23,3 +34,31 @@ export function asMap(objects, {key = 'id', target = {}} = {}) {
23
34
  return acc;
24
35
  }, target);
25
36
  }
37
+
38
+ export function asNumber(value) {
39
+ if (value === undefined || value === null || value === '') {
40
+ return undefined;
41
+ }
42
+ value = Number(value);
43
+ return isNaN(value) ? undefined : value;
44
+ }
45
+
46
+ /**
47
+ * Assign values on target object with Object.defineProperties() from source object.
48
+ */
49
+ export function defineValues(target, source) {
50
+ for (const name in source) {
51
+ if (source.hasOwnProperty(name)) {
52
+ Object.defineProperty(target, name, { value: source[name] });
53
+ }
54
+ }
55
+ }
56
+
57
+ export function copyValues(target, source, propNames) {
58
+ for (const name of propNames || Object.keys(source)) {
59
+ if (source.hasOwnProperty(name)) {
60
+ target[name] = source[name];
61
+ }
62
+ }
63
+ return target;
64
+ }
package/src/stream.js CHANGED
@@ -1,5 +1,14 @@
1
1
  import { Transform, pipeline as _pipeline } from 'stream';
2
2
 
3
+ export function parse() {
4
+ return new Transform({
5
+ transform(chunk, _, callback) {
6
+ callback(null, JSON.parse(chunk));
7
+ },
8
+ readableObjectMode: true,
9
+ });
10
+ }
11
+
3
12
  export function stringify() {
4
13
  return new Transform({
5
14
  transform(chunk, _, callback) {
@@ -16,7 +25,7 @@ export async function pipelineToStdout(...streams) {
16
25
  );
17
26
  }
18
27
 
19
- export async function collectStream(stream) {
28
+ export async function collect(stream) {
20
29
  const records = [];
21
30
  for await (const record of stream) {
22
31
  records.push(record);
@@ -25,16 +34,16 @@ export async function collectStream(stream) {
25
34
  }
26
35
 
27
36
  // from https://github.com/maxogden/concat-stream/issues/66
28
- export async function * concatStreams(...streams) {
37
+ export async function * concat(...streams) {
29
38
  for (const stream of streams) yield * stream
30
39
  }
31
40
 
32
41
  export function transform(fn, { transform: _, ...options } = {}) {
33
42
  return new Transform({
34
43
  ...options,
35
- transform(chunk, encoding, next) {
44
+ async transform(chunk, encoding, next) {
36
45
  try {
37
- next(undefined, fn(chunk, encoding));
46
+ next(undefined, await fn(chunk, encoding));
38
47
  } catch (error) {
39
48
  next(error);
40
49
  }
package/src/functions.js DELETED
@@ -1,3 +0,0 @@
1
- export function concatFns(...fns) {
2
- return v => fns.reduce((v, fn) => fn(v), v);
3
- }