@cto.af/chex-cli 2.0.1 → 2.1.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/bin/chex.js CHANGED
@@ -2,9 +2,10 @@
2
2
  /* eslint-disable no-console */
3
3
 
4
4
  import {Command, Option} from 'commander';
5
- import {Buffer} from 'node:buffer';
6
- import fs from 'node:fs/promises';
7
- import {hexDump} from '@cto.af/chex';
5
+ import {HexDumpTransform} from '../lib/stream.js';
6
+ import fs from 'node:fs';
7
+ import {version as libVersion} from '@cto.af/chex/lib/version.js';
8
+ import {version} from '../lib/version.js';
8
9
 
9
10
  const program = new Command();
10
11
  program
@@ -15,6 +16,7 @@ program
15
16
  '-s,--strings <encoding>',
16
17
  'try to decode strings in this encoding'
17
18
  ).choices(['utf8', 'utf-8']))
19
+ .version(`${version}, lib:${libVersion}`)
18
20
  .parse();
19
21
 
20
22
  const opts = program.opts();
@@ -23,29 +25,20 @@ if (args.length === 0) {
23
25
  args.push('-');
24
26
  }
25
27
 
26
- function readStdin() {
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) {
28
+ function main(argv) {
36
29
  for (const fn of argv) {
37
30
  if (argv.length > 1) {
38
31
  console.log(fn);
39
32
  }
40
- const buf = fn === '-' ?
41
- await readStdin() :
42
- new Uint8Array(await fs.readFile(fn));
43
- console.log(hexDump(buf, {
33
+ const str = (fn === '-') ?
34
+ process.stdin :
35
+ fs.createReadStream(fn);
36
+ str.pipe(new HexDumpTransform({
44
37
  colors: opts.colors || process.stdout.isTTY,
45
38
  dots: opts.dots,
46
39
  decode: opts.strings,
47
- }));
40
+ })).pipe(process.stdout);
48
41
  }
49
42
  }
50
43
 
51
- main(args).catch(console.error);
44
+ 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
@@ -0,0 +1,4 @@
1
+ // Generated by package-extract v3.1.0
2
+ // Do not modify by hand.
3
+
4
+ export const version = '2.1.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cto.af/chex-cli",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "hexdump -C as a small JS library",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",
@@ -21,7 +21,7 @@
21
21
  "author": "Joe Hildebrand <joe-github@cursive.net>",
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
- "@cto.af/chex": "2.0.1",
24
+ "@cto.af/chex": "2.1.0",
25
25
  "commander": "13.1.0"
26
26
  },
27
27
  "engines": {