@kikuchan/hexdump 0.1.0-alpha.6 → 0.1.0-alpha.8
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 +53 -0
- package/index.cjs +2 -0
- package/index.d.cts +44 -0
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# @kikuchan/hexdump
|
|
2
|
+
|
|
3
|
+
Tiny, configurable hex dump utility for Node.js and browsers.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -D @kikuchan/hexdump
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { hexdump } from '@kikuchan/hexdump';
|
|
15
|
+
|
|
16
|
+
const bytes = Uint8Array.from([
|
|
17
|
+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
18
|
+
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
hexdump.log(bytes);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
output:
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
00000000: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f |................|
|
|
28
|
+
00000010: | |
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## API
|
|
32
|
+
|
|
33
|
+
### Basic Functions
|
|
34
|
+
|
|
35
|
+
- `hexdump.log|warn|error(data, options?)`: outputs to `console.log|warn|error` respectively.
|
|
36
|
+
- `hexdump(data, options?)`: returns the full string instead of printing.
|
|
37
|
+
- `hexdump.create(printer)`: returns a hexdump function that prints to `printer` (a function that takes a string).
|
|
38
|
+
|
|
39
|
+
### `data`
|
|
40
|
+
- Binary-like data: `Uint8Array`, `Uint8ClampedArray`, `ArrayBufferLike` or `DataView`;
|
|
41
|
+
|
|
42
|
+
### `options?`
|
|
43
|
+
- `addrOffset: number` — Starting address for the first byte. Default `0`.
|
|
44
|
+
- `addrLength: number` — Hex digits shown for the address. Default `8 - prefix.length` (clamped to `>= 0`).
|
|
45
|
+
- `prefix: string` — Printed before the address (e.g. `"0x"`). Default `""`.
|
|
46
|
+
- `foldSize: number` — Bytes per row. Default `16`.
|
|
47
|
+
- `printChars: boolean` — Show the ASCII gutter at right. Default `true`.
|
|
48
|
+
- `footer: boolean` — Print a trailing line showing the next address. Default `true`.
|
|
49
|
+
- `color: boolean | 'simple' | 'html' | ((s: string, ctx: Context) => ColorizerOperation | undefined)` —
|
|
50
|
+
- `true` or `'simple'`: add ANSI colors in terminals.
|
|
51
|
+
- `'html'`: wrap parts with semantic spans like `hexdump-address`, `hexdump-ascii`, etc., and escape content.
|
|
52
|
+
- `printer?: (line: string) => void | null` — Provide a per-line sink directly in options. Set to `null` to disable printing even if a default printer is configured.
|
|
53
|
+
- `formatter: (s: string, ctx: Context) => string | undefined` — Transform each fragment before it is appended. Return `undefined` to drop that fragment.
|
package/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,`__esModule`,{value:!0});function e(e,t){return Number(e).toString(16).padStart(t,`0`)}const t=e=>e,n=e=>e.replace(/[&<>]/g,e=>({"&":`&`,"<":`<`,">":`>`})[e]),r={simple:{address:{enter:`\x1B[38;5;238m`,leave:`\x1B[m`},separator:{enter:`\x1B[38;5;238m`,leave:`\x1B[m`},control:{enter:`\x1B[38;5;178m`,leave:`\x1B[m`},ascii:{enter:`\x1B[m`,leave:`\x1B[m`},exascii:{enter:`\x1B[38;5;209m`,leave:`\x1B[m`},null:{enter:`\x1B[38;5;244m`,leave:`\x1B[m`},normal:null},html:{address:{enter:`<span class="hexdump-address">`,leave:`</span>`,escape:n},separator:{enter:`<span class="hexdump-separator">`,leave:`</span>`,escape:n},control:{enter:`<span class="hexdump-control">`,leave:`</span>`,escape:n},ascii:{enter:`<span class="hexdump-ascii">`,leave:`</span>`,escape:n},exascii:{enter:`<span class="hexdump-exascii">`,leave:`</span>`,escape:n},null:{enter:`<span class="hexdump-null">`,leave:`</span>`,escape:n},normal:{enter:``,leave:``,escape:n}}},i=e=>{let n;if(!e)return t;if(e===!0&&(e=`simple`),typeof e==`string`){let t=r[e],n=[`address-prefix`,`address-suffix`,`character-prefix`,`character-suffix`];e=function(e,r){if(e.trim()){if(t.address&&r.type===`address`)return t.address;if(t.separator&&n.includes(r.type))return t.separator;if((r.type===`hex-value`||r.type===`character-value`)&&typeof r.value==`number`){if(t.null!==void 0&&r.value===0)return t.null;if(t.control!==void 0&&r.value<32)return t.control;if(t.ascii!==void 0&&32<=r.value&&r.value<127)return t.ascii;if(t.exascii!==void 0&&128<=r.value&&r.value<=255)return t.exascii}return t.normal}}}return(r,i)=>{let a=i.type===`flush`?null:e(r,i);return a!==void 0&&(n?.enter!==a?.enter||n?.leave!==a?.leave||n?.escape!==a?.escape)?(r=(n?.leave||``)+(a?.enter||``)+(a?.escape||t)(r),n=a,r):a&&a.escape?a.escape(r):r}},a=e=>(e||=t,(t,n)=>e(t,n)||``);function o(t){return(n,r,o)=>{let s=ArrayBuffer.isView(n)?new Uint8Array(n.buffer,n.byteOffset,n.byteLength):new Uint8Array(n);typeof r!=`number`&&(o=r,r=s.length),o={...o??{}},(r===void 0||r<0)&&(r=s.length),t=o.printer===null?null:o.printer??t;let c=a(o.formatter),l=i(o.color),u=o.foldSize||16,d=o.printChars!==!1,f=o.addrOffset||0,p=f%u,m=(r?Math.ceil((p%u+r)/u):0)+(o.footer===!1?0:1),h=[],g=``,_=function(e,t){g+=l(c(e,t),t)},v=o?.prefix||``,y=Math.max(o?.addrLength??8-v.length,0);for(let n=0;n<m;n++){let i=f;_(``,{type:`line-prefix`}),_(v,{type:`address-prefix`}),y>=1&&_(e(f,y),{type:`address`,address:f}),_(`: `,{type:`address-suffix`}),_(` `,{type:`hex-dump-prefix`}),_(``,{type:`hex-group-prefix`});for(let t=0;t<u;t++){let i=n*u+t-p;t&&t%8==0?(_(``,{type:`hex-group-suffix`}),_(` `,{type:`hex-group-gap`}),_(``,{type:`hex-group-prefix`})):t&&_(` `,{type:`hex-gap`}),0<=i&&i<r?(_(``,{type:`hex-value-prefix`,address:f,value:s[i]}),_(e(s[i],2),{type:`hex-value`,address:f,value:s[i]}),_(``,{type:`hex-value-suffix`,address:f,value:s[i]}),f++):_(` `,{type:`hex-value-no-data`})}if(_(``,{type:`hex-group-suffix`}),_(` `,{type:`hex-dump-suffix`}),d){let e=i;_(` |`,{type:`character-prefix`});for(let t=0;t<u;t++){let i=n*u+t-p;0<=i&&i<r?(_(s[i]>=32&&s[i]<127?String.fromCharCode(s[i]):`.`,{type:`character-value`,address:e,value:s[i]}),e++):_(` `,{type:`character-value-no-data`})}_(`|`,{type:`character-suffix`})}_(``,{type:`line-suffix`}),_(``,{type:`flush`}),t?.(g),h.push(g),g=``}return h.join(`
|
|
2
|
+
`)}}const s=Object.assign(o(null),{create:o,log:o(e=>console.log(e)),warn:o(e=>console.warn(e)),error:o(e=>console.error(e)),string:o(null)});var c=s;exports.default=c,exports.hexdump=s;
|
package/index.d.cts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
type BinaryLike = Uint8Array | Uint8ClampedArray | ArrayBufferLike | DataView;
|
|
3
|
+
type Context = {
|
|
4
|
+
type: 'hex-value' | 'hex-value-prefix' | 'hex-value-suffix' | 'character-value';
|
|
5
|
+
address: number;
|
|
6
|
+
value: number;
|
|
7
|
+
} | {
|
|
8
|
+
type: 'address';
|
|
9
|
+
address: number;
|
|
10
|
+
} | {
|
|
11
|
+
type: 'line-prefix' | 'address-prefix' | 'address-suffix' | 'hex-dump-prefix' | 'hex-group-prefix' | 'hex-value-no-data' | 'character-value-no-data' | 'hex-gap' | 'hex-group-gap' | 'hex-group-suffix' | 'hex-dump-suffix' | 'character-prefix' | 'character-suffix' | 'line-suffix' | 'flush';
|
|
12
|
+
};
|
|
13
|
+
type ColorizerOperation = {
|
|
14
|
+
enter: string;
|
|
15
|
+
leave: string;
|
|
16
|
+
escape?: (s: string) => string;
|
|
17
|
+
} | null;
|
|
18
|
+
type Colorizer = boolean | undefined | 'simple' | 'html' | ((s: string, ctx: Context) => ColorizerOperation | undefined);
|
|
19
|
+
type Formatter = undefined | ((s: string, ctx: Context) => string | undefined);
|
|
20
|
+
type Options = {
|
|
21
|
+
addrOffset?: number;
|
|
22
|
+
addrLength?: number;
|
|
23
|
+
printer?: null | ((s: string) => void);
|
|
24
|
+
formatter?: Formatter;
|
|
25
|
+
color?: Colorizer;
|
|
26
|
+
prefix?: string;
|
|
27
|
+
printChars?: boolean;
|
|
28
|
+
foldSize?: number;
|
|
29
|
+
footer?: boolean;
|
|
30
|
+
};
|
|
31
|
+
interface Hexdumper {
|
|
32
|
+
(buf: BinaryLike, options?: Options): string;
|
|
33
|
+
(buf: BinaryLike, len: number, options?: Options): string;
|
|
34
|
+
}
|
|
35
|
+
interface Hexdump extends Hexdumper {
|
|
36
|
+
log: Hexdumper;
|
|
37
|
+
warn: Hexdumper;
|
|
38
|
+
error: Hexdumper;
|
|
39
|
+
string: Hexdumper;
|
|
40
|
+
create: (printer: (s: string) => void) => Hexdumper;
|
|
41
|
+
}
|
|
42
|
+
declare const hexdump: Hexdump;
|
|
43
|
+
//#endregion
|
|
44
|
+
export { BinaryLike, hexdump as default, hexdump };
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"keywords": [
|
|
5
5
|
"hexdump"
|
|
6
6
|
],
|
|
7
|
-
"version": "0.1.0-alpha.
|
|
7
|
+
"version": "0.1.0-alpha.8",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"main": "./index.js",
|
|
10
10
|
"author": "kikuchan <kikuchan98@gmail.com>",
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"exports": {
|
|
15
15
|
".": {
|
|
16
16
|
"types": "./index.d.ts",
|
|
17
|
-
"import": "./index.js"
|
|
17
|
+
"import": "./index.js",
|
|
18
|
+
"require": "./index.cjs"
|
|
18
19
|
}
|
|
19
20
|
},
|
|
20
21
|
"repository": {
|