js-dev-tool 1.2.22 → 1.2.24
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/README.md +10 -1
- package/extras/algorithms.d.ts +2 -1
- package/extras/algorithms.js +8 -1
- package/extras/gzip-tool.d.ts +22 -1
- package/extras/gzip-tool.js +28 -6
- package/package.json +1 -1
- package/tool-lib/cmt-trick.js +11 -10
- package/utils.d.ts +5 -8
- package/utils.js +7 -18
package/README.md
CHANGED
|
@@ -210,7 +210,9 @@ main().catch(console.error);
|
|
|
210
210
|
Exports:
|
|
211
211
|
|
|
212
212
|
- `gzip(data, useZeroTimestamp?)`
|
|
213
|
+
- `gunzip(data)`
|
|
213
214
|
- `toBase64SourceLiteral(rawData, wrapSize?)`
|
|
215
|
+
- `fromBase64SourceLiteral(sourceLiteral)`
|
|
214
216
|
|
|
215
217
|
Use this module when you want deterministic gzip output and a clean way to embed the compressed bytes into JavaScript source.
|
|
216
218
|
|
|
@@ -218,19 +220,26 @@ Key behaviors:
|
|
|
218
220
|
|
|
219
221
|
- `gzip(...)` compresses with gzip level `9`
|
|
220
222
|
- `gzip(..., true)` zeroes the gzip `mtime` header so repeated builds with the same input stay byte-stable
|
|
223
|
+
- `gunzip(...)` restores raw bytes from gzip-compressed data
|
|
221
224
|
- `toBase64SourceLiteral(...)` wraps Base64 output with trailing `\` line continuations so the result can be pasted into a multi-line JavaScript string literal
|
|
225
|
+
- `fromBase64SourceLiteral(...)` reverses the wrapped source-literal form and accepts both LF and CRLF line continuations
|
|
222
226
|
- `wrapSize` must be a finite integer between `1` and `2048`
|
|
223
227
|
|
|
224
228
|
Example:
|
|
225
229
|
|
|
226
230
|
```js
|
|
227
|
-
const {
|
|
231
|
+
const {
|
|
232
|
+
gzip, gunzip,
|
|
233
|
+
toBase64SourceLiteral, fromBase64SourceLiteral
|
|
234
|
+
} = require("js-dev-tool/extras/gzip-tool");
|
|
228
235
|
|
|
229
236
|
const compressed = gzip("console.log(\"hello\");");
|
|
230
237
|
const sourceLiteral = toBase64SourceLiteral(compressed, 76);
|
|
238
|
+
const restored = gunzip(fromBase64SourceLiteral(sourceLiteral));
|
|
231
239
|
|
|
232
240
|
console.log(compressed.length);
|
|
233
241
|
console.log(sourceLiteral);
|
|
242
|
+
console.log(restored.toString("utf8"));
|
|
234
243
|
```
|
|
235
244
|
|
|
236
245
|
### `js-dev-tool/extras/progress-light`
|
package/extras/algorithms.d.ts
CHANGED
|
@@ -12,4 +12,5 @@
|
|
|
12
12
|
* Array.sort etc...
|
|
13
13
|
*/
|
|
14
14
|
export declare type TBnComparator<T, V> = (a: T, b: V) => number;
|
|
15
|
-
export declare const bnSearch: <T, V>(src: T[], v: V, comparator: TBnComparator<T, V>) => number;
|
|
15
|
+
export declare const bnSearch: <T, V>(src: T[], v: V, comparator: TBnComparator<T, V>) => number;
|
|
16
|
+
export declare const bnFind: <T, V>(src: T[], v: V, comparator: TBnComparator<T, V>) => T | null;
|
package/extras/algorithms.js
CHANGED
|
@@ -34,6 +34,13 @@ const bnSearch = (src, v, comparator) => {
|
|
|
34
34
|
}
|
|
35
35
|
return -1;
|
|
36
36
|
};
|
|
37
|
+
/**
|
|
38
|
+
* @type {algzm["bnFind"]}
|
|
39
|
+
*/
|
|
40
|
+
const bnFind = (src, v, comparator) => {
|
|
41
|
+
const index = bnSearch(src, v, comparator);
|
|
42
|
+
return index >= 0 ? src[index] : null;
|
|
43
|
+
};
|
|
37
44
|
module.exports = {
|
|
38
|
-
bnSearch,
|
|
45
|
+
bnSearch, bnFind
|
|
39
46
|
};
|
package/extras/gzip-tool.d.ts
CHANGED
|
@@ -21,6 +21,16 @@ import type { InputType } from "zlib";
|
|
|
21
21
|
* @returns {Buffer} gzip-compressed binary data
|
|
22
22
|
*/
|
|
23
23
|
export function gzip(data: InputType, useZeroTimestamp?: boolean): Buffer;
|
|
24
|
+
/**
|
|
25
|
+
* Decompresses gzip binary data.
|
|
26
|
+
*
|
|
27
|
+
* This is a small wrapper around `zlib.gunzipSync(...)` so callers can pair it
|
|
28
|
+
* with {@link gzip} from the same helper module.
|
|
29
|
+
*
|
|
30
|
+
* @param {InputType} data gzip-compressed binary data
|
|
31
|
+
* @returns {Buffer} decompressed raw bytes
|
|
32
|
+
*/
|
|
33
|
+
export function gunzip(data: InputType): Buffer;
|
|
24
34
|
/**
|
|
25
35
|
* Encodes binary data as Base64 and formats it as a JavaScript string-literal fragment.
|
|
26
36
|
*
|
|
@@ -31,4 +41,15 @@ export function gzip(data: InputType, useZeroTimestamp?: boolean): Buffer;
|
|
|
31
41
|
* @param {number=} [wrapSize=100] max Base64 characters per physical line. (default __`100`__)
|
|
32
42
|
* @returns {string} Base64 text wrapped with `\\\n` line continuations
|
|
33
43
|
*/
|
|
34
|
-
export function toBase64SourceLiteral(rawData: Uint8Array, wrapSize?: number): string;
|
|
44
|
+
export function toBase64SourceLiteral(rawData: Uint8Array, wrapSize?: number): string;
|
|
45
|
+
/**
|
|
46
|
+
* Decodes a Base64 JavaScript string-literal fragment back into raw bytes.
|
|
47
|
+
*
|
|
48
|
+
* This reverses {@link toBase64SourceLiteral} by removing the trailing
|
|
49
|
+
* line-continuation sequences (`\\\n` or `\\\r\n`) between wrapped lines and then
|
|
50
|
+
* decoding the resulting Base64 text.
|
|
51
|
+
*
|
|
52
|
+
* @param {string} sourceLiteral Base64 source-literal fragment
|
|
53
|
+
* @returns {Buffer} decoded raw bytes
|
|
54
|
+
*/
|
|
55
|
+
export function fromBase64SourceLiteral(sourceLiteral: string): Buffer;
|
package/extras/gzip-tool.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
/**
|
|
10
10
|
* @file extras/gzip-tool.js
|
|
11
11
|
*/
|
|
12
|
-
const { gzipSync, constants } = require("zlib");
|
|
12
|
+
const { gzipSync, constants, gunzipSync } = require("zlib");
|
|
13
13
|
/**
|
|
14
14
|
* @import { InputType } from "zlib";
|
|
15
15
|
*/
|
|
@@ -24,7 +24,7 @@ const { gzipSync, constants } = require("zlib");
|
|
|
24
24
|
* @param {boolean=} [useZeroTimestamp=true] when `true`, zeroes the gzip `mtime` header. (default __`true`__)
|
|
25
25
|
* @returns {Buffer} gzip-compressed binary data
|
|
26
26
|
*/
|
|
27
|
-
|
|
27
|
+
const gzip = (data, useZeroTimestamp = true) => {
|
|
28
28
|
const compressed = gzipSync(data, {
|
|
29
29
|
level: 9, strategy: constants.Z_DEFAULT_STRATEGY
|
|
30
30
|
});
|
|
@@ -32,7 +32,17 @@ function gzip(data, useZeroTimestamp = true) {
|
|
|
32
32
|
compressed.writeUInt32LE(0, 4);
|
|
33
33
|
}
|
|
34
34
|
return compressed;
|
|
35
|
-
}
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Decompresses gzip binary data.
|
|
38
|
+
*
|
|
39
|
+
* This is a small wrapper around `zlib.gunzipSync(...)` so callers can pair it
|
|
40
|
+
* with {@link gzip} from the same helper module.
|
|
41
|
+
*
|
|
42
|
+
* @param {InputType} data gzip-compressed binary data
|
|
43
|
+
* @returns {Buffer} decompressed raw bytes
|
|
44
|
+
*/
|
|
45
|
+
const gunzip = (data) => gunzipSync(data);
|
|
36
46
|
/**
|
|
37
47
|
* Encodes binary data as Base64 and formats it as a JavaScript string-literal fragment.
|
|
38
48
|
*
|
|
@@ -43,7 +53,7 @@ function gzip(data, useZeroTimestamp = true) {
|
|
|
43
53
|
* @param {number=} [wrapSize=100] max Base64 characters per physical line. (default __`100`__)
|
|
44
54
|
* @returns {string} Base64 text wrapped with `\\\n` line continuations
|
|
45
55
|
*/
|
|
46
|
-
|
|
56
|
+
const toBase64SourceLiteral = (rawData, wrapSize = 100) => {
|
|
47
57
|
if (!Number.isFinite(wrapSize)) throw new Error(`Invalid wrapSize: ${wrapSize}`);
|
|
48
58
|
if (!Number.isInteger(wrapSize)) throw new Error(`wrapSize is not a integer - ${wrapSize}`);
|
|
49
59
|
if (wrapSize > 2048) throw new Error(`"wrapSize" is expected to be 2048 or less - ${wrapSize}`);
|
|
@@ -59,7 +69,19 @@ function toBase64SourceLiteral(rawData, wrapSize = 100) {
|
|
|
59
69
|
wrapped += (base64.slice(offset, offset += wrapSize) + lineSuffix);
|
|
60
70
|
}
|
|
61
71
|
return wrapped;
|
|
62
|
-
}
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Decodes a Base64 JavaScript string-literal fragment back into raw bytes.
|
|
75
|
+
*
|
|
76
|
+
* This reverses {@link toBase64SourceLiteral} by removing the trailing
|
|
77
|
+
* line-continuation sequences (`\\\n` or `\\\r\n`) between wrapped lines and then
|
|
78
|
+
* decoding the resulting Base64 text.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} sourceLiteral Base64 source-literal fragment
|
|
81
|
+
* @returns {Buffer} decoded raw bytes
|
|
82
|
+
*/
|
|
83
|
+
const fromBase64SourceLiteral = (sourceLiteral) =>
|
|
84
|
+
Buffer.from(sourceLiteral.replace(/\\\r?\n/g, ""), "base64");
|
|
63
85
|
module.exports = {
|
|
64
|
-
gzip, toBase64SourceLiteral
|
|
86
|
+
gzip, gunzip, toBase64SourceLiteral, fromBase64SourceLiteral
|
|
65
87
|
};
|
package/package.json
CHANGED
package/tool-lib/cmt-trick.js
CHANGED
|
@@ -11,16 +11,16 @@
|
|
|
11
11
|
/**
|
|
12
12
|
* @file comment trick toggle
|
|
13
13
|
*/
|
|
14
|
+
const rmc = require("rm-cstyle-cmts");
|
|
14
15
|
/**
|
|
15
16
|
* @param {string} code
|
|
16
17
|
* @param {NsJsTool.TDevConsole} logger
|
|
17
18
|
*/
|
|
18
19
|
function cleanup(code, logger) {
|
|
19
|
-
const reCleanUp =
|
|
20
|
-
"\s*\/(?:\s*\/+\*\s+(?:ctt|comment-toggle-trick|https:\/\/coderwall)([\s\S]+?)(?:\s+)\/\*\/[\s\S]+|\s*\*\s+(?:ctt|comment-toggle-trick|https:\/\/coderwall)[\s\S]+\/\*\/([\s\S]+?))\s*\/{2,}\*\/", "g"
|
|
21
|
-
);
|
|
20
|
+
const reCleanUp = /(?<=[\x20\t]*?)\/\/?\*\s+(?:ctt)[\s\S]+?\/\/\*\//gm;
|
|
22
21
|
return code.replace(
|
|
23
|
-
reCleanUp,
|
|
22
|
+
reCleanUp,
|
|
23
|
+
$0 => rmc($0),
|
|
24
24
|
);
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
@@ -53,21 +53,22 @@ module.exports = () => {
|
|
|
53
53
|
/** @type {string} */ (this.taskName), (code) => {
|
|
54
54
|
return mode === "clean" ? cleanup(code, logger) : doIt(code, logger);
|
|
55
55
|
}, {
|
|
56
|
-
|
|
56
|
+
// @ts-expect-error force
|
|
57
|
+
base: params.basePath
|
|
57
58
|
},
|
|
58
59
|
);
|
|
59
60
|
},
|
|
60
61
|
get help() {
|
|
61
62
|
const level = 16;
|
|
62
|
-
return `jstool -cmd cmtTrick[:clean] (-
|
|
63
|
+
return `jstool -cmd cmtTrick[:clean] (-base "<source dir>" | -base "<source dir>,<source dir>,...") [-test "r/\\\\.js$/"]
|
|
63
64
|
|
|
64
65
|
${" Example:".red}
|
|
65
|
-
${"jstool".yellow} ${"-cmd".green} ${"cmtTrick:clean".cyan} ${"-
|
|
66
|
+
${"jstool".yellow} ${"-cmd".green} ${"cmtTrick:clean".cyan} ${"-base".green} ${"src,examples".gray(level)} ${"-test".green} ${"re/\\\\.js$/".gray(level)}
|
|
66
67
|
|
|
67
68
|
${" Options:".red}
|
|
68
|
-
${" :clean".cyan}
|
|
69
|
-
${"
|
|
70
|
-
${" test".cyan}
|
|
69
|
+
${" :clean".cyan} ${`: Remove comment tokens and leave the currently enabled code.`.gray(level)}
|
|
70
|
+
${" base".cyan} ${`: Source directory path(s). Comma-separated or array type arg is accepted.`.gray(level)}
|
|
71
|
+
${" test".cyan} ${`: Target file matcher. default is /\\\\.js$/.`.gray(level)}
|
|
71
72
|
|
|
72
73
|
${" Note:".magenta} ${`Recognized markers: ctt | comment-toggle-trick | https://coderwall`.gray(level)}
|
|
73
74
|
`;
|
package/utils.d.ts
CHANGED
|
@@ -12,10 +12,6 @@ import type {
|
|
|
12
12
|
} from "fs";
|
|
13
13
|
export const tinArgs: typeof import("tin-args");
|
|
14
14
|
export type TFsCallback = (err: any, data: string) => void;
|
|
15
|
-
export type TypedRecord<T> = {
|
|
16
|
-
[x: string]: T;
|
|
17
|
-
};
|
|
18
|
-
export type TReadJsonCallback<T> = (err: any, data: Record<string, T>) => void;
|
|
19
15
|
export type TExtraArgsValue = string | boolean | RegExp | string[];
|
|
20
16
|
/**
|
|
21
17
|
* prepend `content` to the beginning of each element of `str_array`
|
|
@@ -83,15 +79,16 @@ export function readText<C extends TBD<TFsCallback>, R extends undefined extends
|
|
|
83
79
|
* @param path
|
|
84
80
|
* @param [callback]
|
|
85
81
|
*/
|
|
86
|
-
export function readJson<T
|
|
82
|
+
export function readJson<T = Record<string, unknown>>(path: string): T;
|
|
83
|
+
export function readJson<T = Record<string, unknown>>(path: string, callback: (err: any, data: T) => void): void;
|
|
87
84
|
/**
|
|
88
85
|
* @template T
|
|
89
|
-
* @param {T} json
|
|
86
|
+
* @param {T} json json object
|
|
90
87
|
* @param {string} path file path.
|
|
91
|
-
* @param {
|
|
88
|
+
* @param {() => void=} [callback]
|
|
92
89
|
* @returns {void} description
|
|
93
90
|
*/
|
|
94
|
-
declare function writeJson<T>(json: T, path: string, callback?:
|
|
91
|
+
declare function writeJson<T>(json: T, path: string, callback?: () => void): void;
|
|
95
92
|
/**
|
|
96
93
|
* @template T
|
|
97
94
|
* @param {T} json
|
package/utils.js
CHANGED
|
@@ -142,26 +142,15 @@ function readText(from, callback) {
|
|
|
142
142
|
}
|
|
143
143
|
return /** @type {R} */ (undefined);
|
|
144
144
|
}
|
|
145
|
-
/**
|
|
146
|
-
* @template T
|
|
147
|
-
* @typedef {Record<string, T>} TypedRecord<T>
|
|
148
|
-
*/
|
|
149
|
-
/**
|
|
150
|
-
* @template T
|
|
151
|
-
* @typedef {TBD<(err: any, data: TypedRecord<T>) => void>} TReadJsonCallback
|
|
152
|
-
*/
|
|
153
|
-
/**
|
|
154
|
-
* @typedef {TBD<() => void>} TWriteJsonCallback
|
|
155
|
-
*/
|
|
156
145
|
/**
|
|
157
146
|
* NOTE: when callback specified, returns undefined
|
|
158
147
|
*
|
|
159
148
|
* @template T
|
|
160
|
-
* @template {
|
|
161
|
-
* @template {
|
|
162
|
-
* @param {string} path
|
|
163
|
-
* @param {C}
|
|
164
|
-
* @returns {R}
|
|
149
|
+
* @template {(err: any, data: T) => void=} C
|
|
150
|
+
* @template {C extends void ? T : void} R
|
|
151
|
+
* @param {string} path
|
|
152
|
+
* @param {C=} callback
|
|
153
|
+
* @returns {R}
|
|
165
154
|
*/
|
|
166
155
|
function readJson(path, callback) {
|
|
167
156
|
if (typeof callback === "function") {
|
|
@@ -176,9 +165,9 @@ function readJson(path, callback) {
|
|
|
176
165
|
}
|
|
177
166
|
/**
|
|
178
167
|
* @template T
|
|
179
|
-
* @param {T} json
|
|
168
|
+
* @param {T} json json object
|
|
180
169
|
* @param {string} path file path.
|
|
181
|
-
* @param {
|
|
170
|
+
* @param {() => void=} [callback]
|
|
182
171
|
* @returns {void} description
|
|
183
172
|
*/
|
|
184
173
|
function writeJson(json, path, callback) {
|