@cto.af/chex-cli 2.0.1 → 2.1.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/bin/chex.js +12 -20
- package/lib/stream.js +77 -0
- package/lib/version.js +4 -0
- package/package.json +3 -3
package/bin/chex.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
/* eslint-disable no-console */
|
|
3
2
|
|
|
4
3
|
import {Command, Option} from 'commander';
|
|
5
|
-
import {
|
|
6
|
-
import fs from 'node:fs
|
|
7
|
-
import {
|
|
4
|
+
import {HexDumpTransform} from '../lib/stream.js';
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import {version as libVersion} from '@cto.af/chex/lib/version.js';
|
|
7
|
+
import {version} from '../lib/version.js';
|
|
8
8
|
|
|
9
9
|
const program = new Command();
|
|
10
10
|
program
|
|
@@ -15,6 +15,7 @@ program
|
|
|
15
15
|
'-s,--strings <encoding>',
|
|
16
16
|
'try to decode strings in this encoding'
|
|
17
17
|
).choices(['utf8', 'utf-8']))
|
|
18
|
+
.version(`${version}, lib:${libVersion}`)
|
|
18
19
|
.parse();
|
|
19
20
|
|
|
20
21
|
const opts = program.opts();
|
|
@@ -23,29 +24,20 @@ if (args.length === 0) {
|
|
|
23
24
|
args.push('-');
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
function
|
|
27
|
-
return new Promise((resolve, reject) => {
|
|
28
|
-
const bufs = [];
|
|
29
|
-
process.stdin.on('data', d => bufs.push(d));
|
|
30
|
-
process.stdin.on('end', () => resolve(new Uint8Array(Buffer.concat(bufs))));
|
|
31
|
-
process.stdin.on('error', reject);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async function main(argv) {
|
|
27
|
+
function main(argv) {
|
|
36
28
|
for (const fn of argv) {
|
|
37
29
|
if (argv.length > 1) {
|
|
38
30
|
console.log(fn);
|
|
39
31
|
}
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
32
|
+
const str = (fn === '-') ?
|
|
33
|
+
process.stdin :
|
|
34
|
+
fs.createReadStream(fn);
|
|
35
|
+
str.pipe(new HexDumpTransform({
|
|
44
36
|
colors: opts.colors || process.stdout.isTTY,
|
|
45
37
|
dots: opts.dots,
|
|
46
38
|
decode: opts.strings,
|
|
47
|
-
}));
|
|
39
|
+
})).pipe(process.stdout);
|
|
48
40
|
}
|
|
49
41
|
}
|
|
50
42
|
|
|
51
|
-
main(args)
|
|
43
|
+
main(args);
|
package/lib/stream.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {Transform} from 'node:stream';
|
|
2
|
+
import {hexDump} from '@cto.af/chex';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {Partial<import('@cto.af/chex').HexOptions> &
|
|
6
|
+
* import('node:stream').DuplexOptions} HexDumpStreamOptions
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export class HexDumpTransform extends Transform {
|
|
10
|
+
/** @type {HexDumpStreamOptions} */
|
|
11
|
+
#opts;
|
|
12
|
+
#buf = new Uint8Array(16);
|
|
13
|
+
#bufLen = 0;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* @param {HexDumpStreamOptions} options
|
|
18
|
+
*/
|
|
19
|
+
constructor(options = {}) {
|
|
20
|
+
super(options);
|
|
21
|
+
this.#opts = {
|
|
22
|
+
firstByte: 0,
|
|
23
|
+
...options,
|
|
24
|
+
printLength: false,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Transform a chunk of bytes.
|
|
30
|
+
* @param {Uint8Array} chunk
|
|
31
|
+
* @param {BufferEncoding} _encoding Should be null
|
|
32
|
+
* @param {import('node:stream').TransformCallback} callback
|
|
33
|
+
*/
|
|
34
|
+
_transform(chunk, _encoding, callback) {
|
|
35
|
+
let res = '';
|
|
36
|
+
if (this.#bufLen > 0) {
|
|
37
|
+
const front = Math.min(chunk.length, 16 - this.#bufLen);
|
|
38
|
+
if (front > 0) {
|
|
39
|
+
this.#buf.set(chunk.subarray(0, front), this.#bufLen);
|
|
40
|
+
this.#bufLen += front;
|
|
41
|
+
chunk = chunk.subarray(front);
|
|
42
|
+
}
|
|
43
|
+
if (this.#bufLen === 16) {
|
|
44
|
+
res += hexDump(this.#buf, this.#opts);
|
|
45
|
+
this.#opts.firstByte += 16;
|
|
46
|
+
this.#bufLen = 0;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (chunk.length > 0) {
|
|
50
|
+
const tail = chunk.length % 16;
|
|
51
|
+
if (tail > 0) {
|
|
52
|
+
this.#buf.set(chunk.subarray(-tail), this.#bufLen);
|
|
53
|
+
this.#bufLen += tail;
|
|
54
|
+
chunk = chunk.subarray(0, -tail);
|
|
55
|
+
}
|
|
56
|
+
// Should be multiple of 16 now
|
|
57
|
+
if (chunk.length > 0) {
|
|
58
|
+
res += hexDump(chunk, this.#opts);
|
|
59
|
+
this.#opts.firstByte += chunk.length;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
callback(null, res);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param {import('node:stream').TransformCallback} callback
|
|
67
|
+
*/
|
|
68
|
+
_flush(callback) {
|
|
69
|
+
callback(null, hexDump(this.#buf.subarray(0, this.#bufLen), {
|
|
70
|
+
...this.#opts,
|
|
71
|
+
printLength: true,
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
//
|
|
77
|
+
// process.stdin.pipe(new HexDumpTransform()).pipe(process.stdout);
|
package/lib/version.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cto.af/chex-cli",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "hexdump -C as a small JS library",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"author": "Joe Hildebrand <joe-github@cursive.net>",
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@cto.af/chex": "2.
|
|
25
|
-
"commander": "
|
|
24
|
+
"@cto.af/chex": "2.1.1",
|
|
25
|
+
"commander": "14.0.0"
|
|
26
26
|
},
|
|
27
27
|
"engines": {
|
|
28
28
|
"node": ">=20"
|