xshell 0.0.10 → 0.0.14

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/ufs.js ADDED
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.set_ufs = exports.ufs = exports.UFS = void 0;
4
+ const lists_js_1 = require("fs-monkey/lib/util/lists.js");
5
+ require("./prototype.js");
6
+ class UFS {
7
+ constructor(fss) {
8
+ this.fss = fss || [];
9
+ const overriden_methods = Object.getOwnPropertyNames(UFS.prototype);
10
+ const filter_out_overriden_methods = (method) => !overriden_methods.includes(method);
11
+ lists_js_1.fsSyncMethods.filter(filter_out_overriden_methods).forEach(method => {
12
+ this[method] = UFS.sync_method_wrapper.bind(this, method);
13
+ }, this);
14
+ lists_js_1.fsAsyncMethods.filter(filter_out_overriden_methods).forEach(method => {
15
+ this[method] = UFS.async_method_wrapper.bind(this, method);
16
+ }, this);
17
+ }
18
+ use(fs) {
19
+ this.fss = [fs, ...this.fss];
20
+ return this;
21
+ }
22
+ existsSync(path) {
23
+ for (let fs of this.fss)
24
+ if (fs.existsSync(path))
25
+ return true;
26
+ return false;
27
+ }
28
+ readdir(...args) {
29
+ const method = 'readdir';
30
+ let callback = args.last;
31
+ if (typeof callback !== 'function')
32
+ callback = null;
33
+ else
34
+ args.pop();
35
+ let files = new Set();
36
+ const iterate = (i, error) => {
37
+ if (i >= this.fss.length)
38
+ return callback === null || callback === void 0 ? void 0 : callback(error, [...files].sort());
39
+ const fs = this.fss[i];
40
+ if (!fs[method])
41
+ return iterate(i + 1, new Error(`fs no method: ${method}, args: ${args}`));
42
+ fs[method](...args, (fsError, _files) => {
43
+ if (!fsError) {
44
+ files = new Set([...files, ..._files]);
45
+ return iterate(i + 1, null);
46
+ }
47
+ fsError.prev = error;
48
+ return iterate(i + 1, fsError);
49
+ });
50
+ };
51
+ return iterate(0, null);
52
+ }
53
+ readdirSync(...args) {
54
+ const method = 'readdirSync';
55
+ let last_error = null;
56
+ let files = new Set();
57
+ this.fss.forEach(fs => {
58
+ try {
59
+ if (!fs[method])
60
+ throw new Error(`fs no method: ${method}, args: ${args}`);
61
+ files = new Set([...files, ...fs[method].apply(fs, args)]);
62
+ }
63
+ catch (error) {
64
+ error.prev = last_error;
65
+ last_error = error;
66
+ }
67
+ });
68
+ if (last_error)
69
+ throw last_error;
70
+ return [...files].sort();
71
+ }
72
+ createReadStream(path, options) {
73
+ let last_error = null;
74
+ for (let fs of this.fss)
75
+ try {
76
+ if (!fs.createReadStream)
77
+ throw new Error('method not supported: "createReadStream"');
78
+ if (!fs.existsSync)
79
+ throw new Error('method not supported: "existsSync"');
80
+ if (!fs.existsSync(path))
81
+ throw new Error(`文件不存在:${path}`);
82
+ const read_stream = fs.createReadStream.apply(fs, arguments);
83
+ if (!read_stream)
84
+ throw new Error('no valid read stream');
85
+ return read_stream;
86
+ }
87
+ catch (error) {
88
+ error.prev = last_error;
89
+ last_error = error;
90
+ }
91
+ throw last_error;
92
+ }
93
+ createWriteStream(path, options) {
94
+ let last_error = null;
95
+ for (let fs of this.fss)
96
+ try {
97
+ if (!fs.createWriteStream)
98
+ throw new Error('Method not supported: "createWriteStream"');
99
+ fs.statSync(path);
100
+ const write_stream = fs.createWriteStream.apply(fs, arguments);
101
+ if (!write_stream)
102
+ throw new Error('no valid write stream');
103
+ return write_stream;
104
+ }
105
+ catch (error) {
106
+ error.prev = last_error;
107
+ last_error = error;
108
+ }
109
+ throw last_error;
110
+ }
111
+ static sync_method_wrapper(method, ...args) {
112
+ let last_error = null;
113
+ for (let fs of this.fss)
114
+ try {
115
+ if (!fs[method])
116
+ throw new Error(`fs no method: ${method}, args: ${args}`);
117
+ return fs[method].apply(fs, args);
118
+ }
119
+ catch (error) {
120
+ error.prev = last_error;
121
+ last_error = error;
122
+ }
123
+ throw last_error;
124
+ }
125
+ static async_method_wrapper(method, ...args) {
126
+ let callback = args.last;
127
+ if (typeof callback !== 'function')
128
+ callback = null;
129
+ else
130
+ args.pop();
131
+ const iterate = (i, error) => {
132
+ if (i >= this.fss.length)
133
+ return callback === null || callback === void 0 ? void 0 : callback(error);
134
+ const fs = this.fss[i];
135
+ if (!fs[method])
136
+ return iterate(i + 1, new Error(`fs no method: ${method}, args: ${args}`));
137
+ return fs[method](...args, (fs_error, ...results) => {
138
+ if (!fs_error)
139
+ return callback === null || callback === void 0 ? void 0 : callback.call(fs, null, ...results);
140
+ fs_error.prev = error;
141
+ return iterate(i + 1, fs_error);
142
+ });
143
+ };
144
+ return iterate(0, null);
145
+ }
146
+ }
147
+ exports.UFS = UFS;
148
+ function set_ufs(_ufs) {
149
+ exports.ufs = _ufs;
150
+ }
151
+ exports.set_ufs = set_ufs;
152
+ exports.default = UFS;
153
+ //# sourceMappingURL=ufs.js.map
package/ufs.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ufs.js","sourceRoot":"","sources":["ufs.ts"],"names":[],"mappings":";;;AAAA,0DAAkH;AAKlH,0BAAuB;AAQvB,MAAa,GAAG;IAGZ,YAAa,GAAU;QACnB,IAAI,CAAC,GAAG,GAAG,GAAG,IAAI,EAAE,CAAA;QAEpB,MAAM,iBAAiB,GAAG,MAAM,CAAC,mBAAmB,CAAC,GAAG,CAAC,SAAS,CAAC,CAAA;QAEnE,MAAM,4BAA4B,GAAG,CAAC,MAAc,EAAE,EAAE,CACpD,CAAC,iBAAiB,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;QAEvC,wBAAe,CAAC,MAAM,CAAC,4BAA4B,CAAC,CAAC,OAAO,CAAE,MAAM,CAAC,EAAE;YACnE,IAAI,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC,mBAAmB,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;QAC7D,CAAC,EAAE,IAAI,CAAC,CAAA;QAER,yBAAgB,CAAC,MAAM,CAAC,4BAA4B,CAAC,CAAC,OAAO,CAAE,MAAM,CAAC,EAAE;YAC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I,KAAK,CAAC,iBAAiB,MAAM,WAAW,IAAI,EAAE,CAAC,CAAC,CAAA;YAEzF,OAAO,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,QAAiC,EAAE,GAAG,OAAc,EAAE,EAAE;gBAChF,IAAI,CAAC,QAAQ;oBAAE,OAAO,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAE,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,CAAA;gBAC1D,QAAQ,CAAC,IAAI,GAAG,KAAK,CAAA;gBACrB,OAAO,OAAO,CAAC,CAAC,GAAC,CAAC,EAAE,QAAQ,CAAC,CAAA;YACjC,CAAC,CAAC,CAAA;QACN,CAAC,CAAA;QAED,OAAO,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,CAAA;IAC3B,CAAC;CACJ;AAxKD,kBAwKC;AAID,SAAgB,OAAO,CAAE,IAAS;IAC9B,WAAG,GAAG,IAAI,CAAA;AACd,CAAC;AAFD,0BAEC;AAED,kBAAe,GAAG,CAAA","sourcesContent":["import { fsAsyncMethods as fs_async_methods, fsSyncMethods as fs_sync_methods } from 'fs-monkey/lib/util/lists.js'\n\nimport type fs from 'fs'\n\n\nimport './prototype.js'\n\n\ntype FS = typeof fs\n\n// @ts-ignore\nexport interface UFS extends FS { }\n\nexport class UFS {\n fss: FS[]\n \n constructor (fss: any[]) {\n this.fss = fss || []\n \n const overriden_methods = Object.getOwnPropertyNames(UFS.prototype)\n \n const filter_out_overriden_methods = (method: string) => \n !overriden_methods.includes(method)\n \n fs_sync_methods.filter(filter_out_overriden_methods).forEach( method => {\n this[method] = UFS.sync_method_wrapper.bind(this, method)\n }, this)\n \n fs_async_methods.filter(filter_out_overriden_methods).forEach( method => {\n this[method] = UFS.async_method_wrapper.bind(this, method)\n }, this)\n }\n \n \n use (fs: any) {\n this.fss = [fs, ...this.fss]\n return this\n }\n \n \n existsSync (path: string) {\n for (let fs of this.fss)\n if (fs.existsSync(path)) return true\n return false\n }\n \n \n readdir (...args) {\n const method = 'readdir'\n \n let callback = args.last\n \n if (typeof callback !== 'function')\n callback = null\n else\n args.pop()\n \n let files = new Set()\n \n const iterate = (i: number, error: Error) => {\n if (i >= this.fss.length) return callback?.(error, [...files].sort())\n \n const fs = this.fss[i]\n \n if (!fs[method]) return iterate(i+1, new Error(`fs no method: ${method}, args: ${args}`))\n \n fs[method as string](...args, (fsError: Error & { prev: Error }, _files: string[]) => {\n if (!fsError) {\n files = new Set([...files, ..._files])\n return iterate(i+1, null)\n }\n fsError.prev = error\n return iterate(i+1, fsError)\n })\n }\n \n return iterate(0, null)\n }\n \n \n readdirSync (...args) {\n const method = 'readdirSync'\n \n let last_error = null\n let files = new Set()\n \n this.fss.forEach( fs => {\n try {\n if (!fs[method]) throw new Error(`fs no method: ${method}, args: ${args}`)\n \n files = new Set([...files, ...fs[method].apply(fs, args)])\n } catch (error) {\n error.prev = last_error\n last_error = error\n }\n })\n \n if (last_error) throw last_error\n \n return [...files].sort()\n }\n \n \n createReadStream (path: string, options?: any) {\n let last_error = null\n \n for (let fs of this.fss)\n try {\n if (!fs.createReadStream) throw new Error('method not supported: \"createReadStream\"')\n if (!fs.existsSync) throw new Error('method not supported: \"existsSync\"')\n if (!fs.existsSync(path)) throw new Error(`文件不存在:${path}`)\n const read_stream = fs.createReadStream.apply(fs, arguments)\n if (!read_stream) throw new Error('no valid read stream')\n return read_stream\n } catch (error) {\n error.prev = last_error\n last_error = error\n }\n \n throw last_error\n }\n \n \n createWriteStream (path: string, options?: any) {\n let last_error = null\n \n for (let fs of this.fss)\n try {\n if (!fs.createWriteStream) throw new Error('Method not supported: \"createWriteStream\"')\n fs.statSync(path)\n const write_stream = fs.createWriteStream.apply(fs, arguments)\n if (!write_stream) throw new Error('no valid write stream')\n return write_stream\n } catch (error) {\n error.prev = last_error\n last_error = error\n }\n \n \n throw last_error\n }\n \n \n static sync_method_wrapper (this: UFS, method: string, ...args: any[]) {\n let last_error: Error = null\n for (let fs of this.fss)\n try {\n if (!fs[method]) throw new Error(`fs no method: ${method}, args: ${args}`)\n return fs[method].apply(fs, args)\n } catch (error) {\n error.prev = last_error\n last_error = error\n }\n throw last_error\n }\n \n \n static async_method_wrapper (this: UFS, method: string, ...args: any[]) {\n let callback = args.last\n \n if (typeof callback !== 'function')\n callback = null\n else\n args.pop()\n \n const iterate = (i: number, error: Error) => {\n if (i >= this.fss.length) return callback?.(error)\n \n const fs = this.fss[i]\n \n if (!fs[method]) return iterate(i+1, new Error(`fs no method: ${method}, args: ${args}`))\n \n return fs[method](...args, (fs_error: Error & { prev: Error }, ...results: any[]) => {\n if (!fs_error) return callback?.call(fs, null, ...results)\n fs_error.prev = error\n return iterate(i+1, fs_error)\n })\n }\n \n return iterate(0, null)\n }\n}\n\nexport let ufs: UFS\n\nexport function set_ufs (_ufs: UFS) {\n ufs = _ufs\n}\n\nexport default UFS\n"]}
package/utils.d.ts CHANGED
@@ -1,20 +1,26 @@
1
1
  /// <reference types="node" />
2
2
  import { Readable } from 'stream';
3
3
  import util from 'util';
4
- export declare function assert(shoud_be_true_expr: any): never | void;
4
+ import './prototype';
5
+ export declare const output_width = 230;
5
6
  export declare function dedent(templ: TemplateStringsArray | string, ...values: any[]): string;
6
- export declare function unique(iterable: any[] | Set<any>): any[];
7
+ /** unique iterable or array (by selector)
8
+ - selector?: 可以是 key (string) 或 (obj: any) => any
9
+ */
10
+ export declare function unique<T>(iterable: T[] | Iterable<T>, selector?: string | ((obj: T) => any)): any[];
11
+ /** sort keys in object and returns new object */
7
12
  export declare function sort_keys<T>(obj: T): T;
8
- export declare function log_module_loaded(id: string): void;
9
- export declare function log_section(message: string, { timestamp, time, color, left_width, full_width }?: {
10
- timestamp?: boolean;
11
- time?: boolean | Date;
13
+ /** string compare in lexicographic order */
14
+ export declare function strcmp(l: string, r: string): 0 | 1 | -1;
15
+ /** 拼接 TypedArrays 生成一个完整的 Uint8Array */
16
+ export declare function concat(views: ArrayBufferView[]): Uint8Array;
17
+ export declare function typed_array_to_buffer(view: ArrayBufferView): Buffer;
18
+ export declare function log_section(message: string, { time, timestamp, color, }?: {
19
+ time?: boolean;
20
+ timestamp?: boolean | Date;
12
21
  color?: 'green' | 'red' | 'yellow';
13
- left_width?: number;
14
- full_width?: number;
15
22
  }): void;
16
- /** '─' === '\u2500' */
17
- export declare function log_line(width?: number): void;
23
+ export declare function log_line(): void;
18
24
  export declare function delay(milliseconds: number): Promise<unknown>;
19
25
  export declare function has_chinese(str: string): boolean;
20
26
  export declare function escape_line_feed(str: string): string;
@@ -30,3 +36,4 @@ export declare namespace inspect {
30
36
  const custom: typeof util.inspect.custom;
31
37
  }
32
38
  export declare function stream_to_buffer(stream: Readable): Promise<Buffer>;
39
+ export declare function stream_to_lines(stream: Readable): AsyncGenerator<string, void, unknown>;
package/utils.js CHANGED
@@ -1,38 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stream_to_buffer = exports.inspect = exports.escape_line_feed = exports.has_chinese = exports.delay = exports.log_line = exports.log_section = exports.log_module_loaded = exports.sort_keys = exports.unique = exports.dedent = exports.assert = void 0;
3
+ exports.stream_to_lines = exports.stream_to_buffer = exports.inspect = exports.escape_line_feed = exports.has_chinese = exports.delay = exports.log_line = exports.log_section = exports.typed_array_to_buffer = exports.concat = exports.strcmp = exports.sort_keys = exports.unique = exports.dedent = exports.output_width = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const upath_1 = (0, tslib_1.__importDefault)(require("upath"));
6
5
  const util_1 = (0, tslib_1.__importDefault)(require("util"));
7
6
  const omit_1 = (0, tslib_1.__importDefault)(require("lodash/omit"));
8
- const sortBy_1 = (0, tslib_1.__importDefault)(require("lodash/sortBy"));
9
- const prototype_1 = require("./prototype");
10
- function assert(shoud_be_true_expr) {
11
- if (shoud_be_true_expr)
12
- return;
13
- debugger;
14
- throw new Error(`Assertion Failed: ${inspect(shoud_be_true_expr)}`);
15
- }
16
- exports.assert = assert;
7
+ require("./prototype");
8
+ exports.output_width = 230;
17
9
  function dedent(templ, ...values) {
18
10
  let strings = Array.from(typeof templ === 'string' ? [templ] : templ.raw);
19
- // 1. Remove trailing whitespace.
11
+ // 1. remove trailing whitespace
20
12
  strings[strings.length - 1] = strings[strings.length - 1].replace(/\r?\n([\t ]*)$/, '');
21
- // 2. Find all line breaks to determine the highest common indentation level.
22
- const indentLengths = strings.reduce((arr, str) => {
13
+ // 2. find all line breaks to determine the highest common indentation level
14
+ const indent_lengths = strings.reduce((arr, str) => {
23
15
  const matches = str.match(/\n[\t ]+/g);
24
16
  if (matches)
25
17
  return arr.concat(matches.map(match => match.length - 1));
26
18
  return arr;
27
19
  }, []);
28
- // 3. Remove the common indentation from all strings.
29
- if (indentLengths.length) {
30
- const pattern = new RegExp(`\n[\t ]{${Math.min(...indentLengths)}}`, 'g');
20
+ // 3. remove the common indentation from all strings
21
+ if (indent_lengths.length) {
22
+ const pattern = new RegExp(`\n[\t ]{${Math.min(...indent_lengths)}}`, 'g');
31
23
  strings = strings.map(str => str.replace(pattern, '\n'));
32
24
  }
33
- // 4. Remove leading whitespace.
25
+ // 4. remove leading whitespace
34
26
  strings[0] = strings[0].replace(/^\r?\n/, '');
35
- // 5. Perform interpolation.
27
+ // 5. perform interpolation
36
28
  let string = strings[0];
37
29
  values.forEach((value, i) => {
38
30
  string += value + strings[i + 1];
@@ -41,40 +33,76 @@ function dedent(templ, ...values) {
41
33
  return string;
42
34
  }
43
35
  exports.dedent = dedent;
44
- function unique(iterable) {
45
- return [...new Set(iterable)];
36
+ /** unique iterable or array (by selector)
37
+ - selector?: 可以是 key (string) 或 (obj: any) => any
38
+ */
39
+ function unique(iterable, selector) {
40
+ if (!selector)
41
+ return [...new Set(iterable)];
42
+ let map = new Map();
43
+ if (typeof selector === 'string')
44
+ for (const x of iterable)
45
+ map.set(x[selector], x);
46
+ else
47
+ for (const x of iterable)
48
+ map.set(selector(x), x);
49
+ return [...map.values()];
46
50
  }
47
51
  exports.unique = unique;
52
+ /** sort keys in object and returns new object */
48
53
  function sort_keys(obj) {
49
- return Object.fromEntries((0, sortBy_1.default)(Object.entries(obj), ([key,]) => key));
54
+ return Object.fromEntries(Object.entries(obj)
55
+ .sort(([key_l], [key_r]) => strcmp(key_l, key_r)));
50
56
  }
51
57
  exports.sort_keys = sort_keys;
52
- // ------------------------------------ Log: module loaded, section, line
53
- function log_module_loaded(id) {
54
- const fname = upath_1.default.basename(id).replace(/\.(coffee|ts)$/, '');
55
- console.log(`${fname}${' '.repeat(20 - fname.length)}loaded`);
58
+ /** string compare in lexicographic order */
59
+ function strcmp(l, r) {
60
+ if (l === r)
61
+ return 0;
62
+ if (l < r)
63
+ return -1;
64
+ return 1;
65
+ }
66
+ exports.strcmp = strcmp;
67
+ /** 拼接 TypedArrays 生成一个完整的 Uint8Array */
68
+ function concat(views) {
69
+ let length = 0;
70
+ for (const v of views)
71
+ length += v.byteLength;
72
+ let buf = new Uint8Array(length);
73
+ let offset = 0;
74
+ for (const v of views) {
75
+ const uint8view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength);
76
+ buf.set(uint8view, offset);
77
+ offset += uint8view.byteLength;
78
+ }
79
+ return buf;
56
80
  }
57
- exports.log_module_loaded = log_module_loaded;
58
- function log_section(message, { timestamp = false, time = false, color = undefined, left_width = 30, full_width = 110 } = {}) {
81
+ exports.concat = concat;
82
+ function typed_array_to_buffer(view) {
83
+ return Buffer.from(view.buffer, view.byteOffset, view.byteLength);
84
+ }
85
+ exports.typed_array_to_buffer = typed_array_to_buffer;
86
+ // ------------------------------------ log: module loaded, section, line
87
+ function log_section(message, { time = false, timestamp = false, color = undefined, } = {}) {
59
88
  const stime = (() => {
60
- if (timestamp)
61
- return ` [${String(new Date().getTime() - global.started_at.getTime()).pad(4, { position: 'left' })} ms]`;
62
89
  if (time)
63
- if (typeof time === 'object')
64
- return ` [${time.to_str()}]`;
90
+ return `[${String(new Date().getTime() - global.started_at.getTime()).pad(4, { position: 'left' })} ms]`;
91
+ if (timestamp)
92
+ if (typeof timestamp === 'object')
93
+ return `[${timestamp.to_str()}]`;
65
94
  else
66
- return ` [${new Date().to_str()}]`;
95
+ return `[${new Date().to_str()}]`;
67
96
  return '';
68
97
  })();
69
- message = `${'-'.repeat(left_width)}${stime} ${message} `.pad(full_width, { character: '-' });
98
+ message = `${stime.pad(20)}${message}`;
70
99
  if (color)
71
- message = prototype_1.colors[color](message);
100
+ message = message[color];
72
101
  console.log(message);
73
102
  }
74
103
  exports.log_section = log_section;
75
- /** '─' === '\u2500' */
76
- function log_line(width = global.WIDTH || 240) {
77
- console.log('─'.repeat(width / 2));
104
+ function log_line() {
105
+ console.log('---');
78
106
  }
79
107
  exports.log_line = log_line;
80
108
  async function delay(milliseconds) {
@@ -83,7 +111,7 @@ async function delay(milliseconds) {
83
111
  });
84
112
  }
85
113
  exports.delay = delay;
86
- // ------------ Text
114
+ // ------------ text
87
115
  function has_chinese(str) {
88
116
  return /[\u4E00-\u9FA5]/.test(str);
89
117
  }
@@ -100,8 +128,6 @@ function inspect(obj, options = {}) {
100
128
  if (options.omit)
101
129
  obj = (0, omit_1.default)(obj, [inspect.custom, ...(options.omit || [])]);
102
130
  let text = util_1.default.inspect(obj, options);
103
- if (obj && typeof obj === 'object')
104
- text = text.split_lines().indent2to4().join_lines();
105
131
  if (!('limit' in options))
106
132
  options.limit = 10000;
107
133
  if (options.limit && text.length > options.limit)
@@ -121,4 +147,21 @@ async function stream_to_buffer(stream) {
121
147
  return Buffer.concat(chunks);
122
148
  }
123
149
  exports.stream_to_buffer = stream_to_buffer;
150
+ async function* stream_to_lines(stream) {
151
+ let buf = '';
152
+ for await (const chunk of stream) {
153
+ let i = 0, j = 0;
154
+ for (; (i = chunk.indexOf('\n', j)) >= 0;) {
155
+ let line = chunk.slice(j, i);
156
+ if (buf) {
157
+ line = buf + line;
158
+ buf = '';
159
+ }
160
+ j = i + 1;
161
+ yield line;
162
+ }
163
+ buf = chunk.slice(j);
164
+ }
165
+ }
166
+ exports.stream_to_lines = stream_to_lines;
124
167
  //# sourceMappingURL=utils.js.map
package/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":";;;;AAEA,+DAAwB;AACxB,6DAAuB;AACvB,oEAA8B;AAC9B,wEAAmC;AAEnC,2CAAoC;AAGpC,SAAgB,MAAM,CAAE,kBAAuB;IAC3C,IAAI,kBAAkB;QAAE,OAAM;IAC9B,QAAQ,CAAA;IACR,MAAM,IAAI,KAAK,CAAC,qBAAqB,OAAO,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAA;AACvE,CAAC;AAJD,wBAIC;AAGD,SAAgB,MAAM,CAClB,KAAoC,EACpC,GAAG,MAAa;IAEhB,IAAI,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAEzE,iCAAiC;IACjC,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,OAAO,CAC7D,gBAAgB,EAChB,EAAE,CACL,CAAA;IAED,6EAA6E;IAC7E,MAAM,aAAa,GAAG,OAAO,CAAC,MAAM,CAChC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QACT,MAAM,OAAO,GAAG,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,CAAA;QACtC,IAAI,OAAO;YACP,OAAO,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAA;QAE7D,OAAO,GAAG,CAAA;IACd,CAAC,EACD,EAAE,CACL,CAAA;IAED,qDAAqD;IACrD,IAAI,aAAa,CAAC,MAAM,EAAE;QACtB,M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AACnB,CAAC;AArBD,0BAqBC;AAED,WAAiB,OAAO;IACP,cAAM,GAA+B,cAAI,CAAC,OAAO,CAAC,MAAM,CAAA;AACzE,CAAC,EAFgB,OAAO,GAAP,eAAO,KAAP,eAAO,QAEvB;AAGD,6CAA6C;AACtC,KAAK,UAAU,gBAAgB,CAAE,MAAgB;IACpD,IAAI,MAAM,GAAG,EAAG,CAAA;IAChB,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM;QAC5B,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IACtB,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;AAChC,CAAC;AALD,4CAKC","sourcesContent":["import { Readable } from 'stream'\n\nimport path from 'upath'\nimport util from 'util'\nimport omit from 'lodash/omit'\nimport sort_by from 'lodash/sortBy'\n\nimport { colors } from './prototype'\n\n\nexport function assert (shoud_be_true_expr: any): never | void {\n if (shoud_be_true_expr) return\n debugger\n throw new Error(`Assertion Failed: ${inspect(shoud_be_true_expr)}`)\n}\n\n\nexport function dedent (\n templ: TemplateStringsArray | string,\n ...values: any[]\n): string {\n let strings = Array.from(typeof templ === 'string' ? [templ] : templ.raw)\n \n // 1. Remove trailing whitespace.\n strings[strings.length - 1] = strings[strings.length - 1].replace(\n /\\r?\\n([\\t ]*)$/,\n '',\n )\n \n // 2. Find all line breaks to determine the highest common indentation level.\n const indentLengths = strings.reduce<number[]>(\n (arr, str) => {\n const matches = str.match(/\\n[\\t ]+/g)\n if (matches) \n return arr.concat(matches.map(match => match.length - 1))\n \n return arr\n },\n [],\n )\n \n // 3. Remove the common indentation from all strings.\n if (indentLengths.length) {\n const pattern = new RegExp(`\\n[\\t ]{${Math.min(...indentLengths)}}`, 'g')\n \n strings = strings.map(str => str.replace(pattern, '\\n'))\n }\n \n // 4. Remove leading whitespace.\n strings[0] = strings[0].replace(/^\\r?\\n/, '')\n \n // 5. Perform interpolation.\n let string = strings[0]\n \n values.forEach((value, i) => {\n string += value + strings[i + 1]\n })\n \n string += '\\n'\n \n return string\n}\n\n\nexport function unique (iterable: any[] | Set<any>) {\n return [... new Set(iterable)]\n}\n\n\nexport function sort_keys <T> (obj: T) {\n return Object.fromEntries(\n sort_by(\n Object.entries(obj),\n ([key, ]) => key\n )\n ) as T\n}\n\n\n// ------------------------------------ Log: module loaded, section, line\nexport function log_module_loaded (id: string) {\n const fname = path.basename(id).replace(/\\.(coffee|ts)$/, '')\n console.log(`${ fname }${ ' '.repeat(20 - fname.length) }loaded`)\n}\n\n\nexport function log_section (\n message: string, \n {\n timestamp = false,\n time = false,\n color = undefined,\n left_width = 30,\n full_width = 110\n }: {\n timestamp?: boolean\n time?: boolean | Date\n color?: 'green' | 'red' | 'yellow'\n left_width?: number\n full_width?: number\n } = { }\n) {\n const stime = (() => {\n if (timestamp)\n return ` [${String(new Date().getTime() - global.started_at.getTime()).pad(4, { position: 'left' })} ms]`\n if (time)\n if (typeof time === 'object')\n return ` [${time.to_str()}]`\n else\n return ` [${new Date().to_str()}]`\n return ''\n })()\n \n message = `${'-'.repeat(left_width)}${stime} ${message} `.pad(full_width, { character: '-' })\n \n if (color)\n message = colors[color](message)\n \n console.log(message)\n}\n\n\n/** '─' === '\\u2500' */\nexport function log_line (width: number = global.WIDTH || 240) {\n console.log('─'.repeat(width / 2))\n}\n\n\nexport async function delay (milliseconds: number) {\n return new Promise( resolve => {\n setTimeout(resolve, milliseconds)\n })\n}\n\n\n// ------------ Text\nexport function has_chinese (str: string) {\n return /[\\u4E00-\\u9FA5]/.test(str)\n}\n\n\nexport function escape_line_feed (str: string) {\n return str.replace(/\\n/g, '\\\\n')\n}\n\n/** util.inspect(obj) \n - options\n - limit?: `10000`\n*/\nexport function inspect (\n obj: any, \n options: util.InspectOptions & {\n limit?: number\n omit?: string[]\n } = { }\n) {\n if (options.omit)\n obj = omit(obj, [inspect.custom, ...(options.omit || [])])\n \n let text = util.inspect(obj, options)\n \n if (obj && typeof obj === 'object')\n text = text.split_lines().indent2to4().join_lines()\n\n if (!('limit' in options))\n options.limit = 10000\n if (options.limit && text.length > options.limit)\n return `${text.slice(0, options.limit)}……'\\u001b[39m\\n`\n else\n return text\n}\n\nexport namespace inspect {\n export const custom: typeof util.inspect.custom = util.inspect.custom\n}\n\n\n// ------------------------------------ Steam\nexport async function stream_to_buffer (stream: Readable) {\n let chunks = [ ]\n for await (const chunk of stream)\n chunks.push(chunk)\n return Buffer.concat(chunks)\n}\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":";;;;AAEA,6DAAuB;AACvB,oEAA8B;AAE9B,uBAAoB;AAEP,QAAA,YAAY,GAAG,GAAG,CAAA;AAG/B,SAAgB,MAAM,CAClB,KAAoC,EACpC,GAAG,MAAa;IAEhB,IAAI,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAEzE,gCAAgC;IAChC,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,OAAO,CAC7D,gBAAgB,EAChB,EAAE,CACL,CAAA;IAED,4EAA4E;IAC5E,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CACjC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QACT,MAAM,OAAO,GAAG,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,CAAA;QACtC,IAAI,OAAO;YACP,OAAO,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAA;QAE7D,OAAO,GAAG,CAAA;IACd,CAAC,EACD,EAAE,CACL,CAAA;IAED,oDAAoD;IACpD,IAAI,cAAc,CAAC,MAAM,EAAE;QAC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AAC,CAAC,eAAe,CAAE,MAAgB;IACrD,IAAI,GAAG,GAAG,EAAE,CAAA;IACZ,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAA+B,EAAE;QACvD,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAA;QAChB,OAAQ,CAAC,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAK;YAC1C,IAAI,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;YAC5B,IAAI,GAAG,EAAE;gBACL,IAAI,GAAG,GAAG,GAAG,IAAI,CAAA;gBACjB,GAAG,GAAG,EAAE,CAAA;aACX;YACD,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;YACT,MAAM,IAAI,CAAA;SACb;QACD,GAAG,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;KACvB;AACL,CAAC;AAfD,0CAeC","sourcesContent":["import { Readable } from 'stream'\n\nimport util from 'util'\nimport omit from 'lodash/omit'\n\nimport './prototype'\n\nexport const output_width = 230\n\n\nexport function dedent (\n templ: TemplateStringsArray | string,\n ...values: any[]\n): string {\n let strings = Array.from(typeof templ === 'string' ? [templ] : templ.raw)\n \n // 1. remove trailing whitespace\n strings[strings.length - 1] = strings[strings.length - 1].replace(\n /\\r?\\n([\\t ]*)$/,\n '',\n )\n \n // 2. find all line breaks to determine the highest common indentation level\n const indent_lengths = strings.reduce<number[]>(\n (arr, str) => {\n const matches = str.match(/\\n[\\t ]+/g)\n if (matches) \n return arr.concat(matches.map(match => match.length - 1))\n \n return arr\n },\n [],\n )\n \n // 3. remove the common indentation from all strings\n if (indent_lengths.length) {\n const pattern = new RegExp(`\\n[\\t ]{${Math.min(...indent_lengths)}}`, 'g')\n \n strings = strings.map(str => str.replace(pattern, '\\n'))\n }\n \n // 4. remove leading whitespace\n strings[0] = strings[0].replace(/^\\r?\\n/, '')\n \n // 5. perform interpolation\n let string = strings[0]\n \n values.forEach((value, i) => {\n string += value + strings[i + 1]\n })\n \n string += '\\n'\n \n return string\n}\n\n\n/** unique iterable or array (by selector) \n - selector?: 可以是 key (string) 或 (obj: any) => any\n*/\nexport function unique <T> (iterable: T[] | Iterable<T>, selector?: string | ((obj: T) => any)) {\n if (!selector)\n return [...new Set(iterable)]\n \n let map = new Map()\n if (typeof selector === 'string')\n for (const x of iterable)\n map.set(x[selector], x)\n else\n for (const x of iterable)\n map.set(\n selector(x),\n x\n )\n \n return [...map.values()]\n}\n\n\n/** sort keys in object and returns new object */\nexport function sort_keys <T> (obj: T) {\n return Object.fromEntries(\n Object.entries(obj)\n .sort(([key_l], [key_r]) => \n strcmp(key_l, key_r))\n ) as T\n}\n\n\n/** string compare in lexicographic order */\nexport function strcmp (l: string, r: string) {\n if (l === r) return 0\n if (l < r) return -1\n return 1\n}\n\n\n/** 拼接 TypedArrays 生成一个完整的 Uint8Array */\nexport function concat (views: ArrayBufferView[]) {\n let length = 0\n for (const v of views)\n length += v.byteLength\n \n let buf = new Uint8Array(length)\n let offset = 0\n for (const v of views) {\n const uint8view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength)\n buf.set(uint8view, offset)\n offset += uint8view.byteLength\n }\n \n return buf\n}\n\n\nexport function typed_array_to_buffer (view: ArrayBufferView) {\n return Buffer.from(\n view.buffer,\n view.byteOffset,\n view.byteLength\n )\n}\n\n\n// ------------------------------------ log: module loaded, section, line\nexport function log_section (\n message: string, \n {\n time = false,\n timestamp = false,\n color = undefined,\n }: {\n time?: boolean\n timestamp?: boolean | Date\n color?: 'green' | 'red' | 'yellow'\n } = { }\n) {\n const stime = (() => {\n if (time)\n return `[${String(new Date().getTime() - global.started_at.getTime()).pad(4, { position: 'left' })} ms]`\n if (timestamp)\n if (typeof timestamp === 'object')\n return `[${timestamp.to_str()}]`\n else\n return `[${new Date().to_str()}]`\n return ''\n })()\n \n message = `${stime.pad(20)}${message}`\n \n if (color)\n message = message[color]\n \n console.log(message)\n}\n\n\nexport function log_line () {\n console.log('---')\n}\n\n\nexport async function delay (milliseconds: number) {\n return new Promise( resolve => {\n setTimeout(resolve, milliseconds)\n })\n}\n\n\n// ------------ text\nexport function has_chinese (str: string) {\n return /[\\u4E00-\\u9FA5]/.test(str)\n}\n\n\nexport function escape_line_feed (str: string) {\n return str.replace(/\\n/g, '\\\\n')\n}\n\n/** util.inspect(obj) \n - options\n - limit?: `10000`\n*/\nexport function inspect (\n obj: any, \n options: util.InspectOptions & {\n limit?: number\n omit?: string[]\n } = { }\n) {\n if (options.omit)\n obj = omit(obj, [inspect.custom, ...(options.omit || [])])\n \n let text = util.inspect(obj, options)\n \n if (!('limit' in options))\n options.limit = 10000\n if (options.limit && text.length > options.limit)\n return `${text.slice(0, options.limit)}……'\\u001b[39m\\n`\n else\n return text\n}\n\nexport namespace inspect {\n export const custom: typeof util.inspect.custom = util.inspect.custom\n}\n\n\n// ------------------------------------ Steam\nexport async function stream_to_buffer (stream: Readable) {\n let chunks = [ ]\n for await (const chunk of stream as AsyncIterable<Buffer>)\n chunks.push(chunk)\n return Buffer.concat(chunks)\n}\n\n\nexport async function * stream_to_lines (stream: Readable) {\n let buf = ''\n for await (const chunk of stream as AsyncIterable<string>) {\n let i = 0, j = 0\n for (; (i = chunk.indexOf('\\n', j)) >= 0; ) {\n let line = chunk.slice(j, i)\n if (buf) {\n line = buf + line\n buf = ''\n }\n j = i + 1\n yield line\n }\n buf = chunk.slice(j)\n }\n}\n\n"]}