@guanghechen/byte 1.0.0-alpha.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/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # 1.0.0-alpha.1 (2023-10-22)
7
+
8
+
9
+ ### Features
10
+
11
+ * ✨ add @guanghechen/byte ([23c4579](https://github.com/guanghechen/sora/commit/23c4579b3c82a710c026c3e1ffa5df27e240ef83))
package/README.md ADDED
@@ -0,0 +1,71 @@
1
+ <header>
2
+ <h1 align="center">
3
+ <a href="https://github.com/guanghechen/sora/tree/@guanghechen/byte@1.0.0-alpha.1/packages/byte#readme">@guanghechen/byte</a>
4
+ </h1>
5
+ <div align="center">
6
+ <a href="https://www.npmjs.com/package/@guanghechen/byte">
7
+ <img
8
+ alt="Npm Version"
9
+ src="https://img.shields.io/npm/v/@guanghechen/byte.svg"
10
+ />
11
+ </a>
12
+ <a href="https://www.npmjs.com/package/@guanghechen/byte">
13
+ <img
14
+ alt="Npm Download"
15
+ src="https://img.shields.io/npm/dm/@guanghechen/byte.svg"
16
+ />
17
+ </a>
18
+ <a href="https://www.npmjs.com/package/@guanghechen/byte">
19
+ <img
20
+ alt="Npm License"
21
+ src="https://img.shields.io/npm/l/@guanghechen/byte.svg"
22
+ />
23
+ </a>
24
+ <a href="#install">
25
+ <img
26
+ alt="Module Formats: cjs"
27
+ src="https://img.shields.io/badge/module_formats-cjs-green.svg"
28
+ />
29
+ </a>
30
+ <a href="https://github.com/nodejs/node">
31
+ <img
32
+ alt="Node.js Version"
33
+ src="https://img.shields.io/node/v/@guanghechen/byte"
34
+ />
35
+ </a>
36
+ <a href="https://github.com/facebook/jest">
37
+ <img
38
+ alt="Tested with Jest"
39
+ src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
40
+ />
41
+ </a>
42
+ <a href="https://github.com/prettier/prettier">
43
+ <img
44
+ alt="Code Style: prettier"
45
+ src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
46
+ />
47
+ </a>
48
+ </div>
49
+ </header>
50
+ <br/>
51
+
52
+ Utility functions for bytes (Uint8Array).
53
+
54
+
55
+ ## Install
56
+
57
+ * npm
58
+
59
+ ```bash
60
+ npm install --save @guanghechen/byte
61
+ ```
62
+
63
+ * yarn
64
+
65
+ ```bash
66
+ yarn add @guanghechen/byte
67
+ ```
68
+
69
+ ## Usage
70
+
71
+ [homepage]: https://github.com/guanghechen/sora/tree/@guanghechen/byte@1.0.0-alpha.1/packages/byte#readme
@@ -0,0 +1,147 @@
1
+ 'use strict';
2
+
3
+ const hexCodeMap = {
4
+ 0: 0x00,
5
+ 1: 0x01,
6
+ 2: 0x02,
7
+ 3: 0x03,
8
+ 4: 0x04,
9
+ 5: 0x05,
10
+ 6: 0x06,
11
+ 7: 0x07,
12
+ 8: 0x08,
13
+ 9: 0x09,
14
+ a: 0x0a,
15
+ b: 0x0b,
16
+ c: 0x0c,
17
+ d: 0x0d,
18
+ e: 0x0e,
19
+ f: 0x0f,
20
+ A: 0x0a,
21
+ B: 0x0b,
22
+ C: 0x0c,
23
+ D: 0x0d,
24
+ E: 0x0e,
25
+ F: 0x0f,
26
+ };
27
+ const invHexCodeMap = [
28
+ '0',
29
+ '1',
30
+ '2',
31
+ '3',
32
+ '4',
33
+ '5',
34
+ '6',
35
+ '7',
36
+ '8',
37
+ '9',
38
+ 'a',
39
+ 'b',
40
+ 'c',
41
+ 'd',
42
+ 'e',
43
+ 'f',
44
+ ];
45
+ function hexText2bytes(hexText) {
46
+ if (hexText.length % 2 !== 0) {
47
+ throw new Error('[hexText2bytes] Hex string length must be even.');
48
+ }
49
+ const bytesSize = hexText.length / 2;
50
+ const bytes = new Uint8Array(bytesSize);
51
+ for (let i = 0, j = 0; i < bytesSize; ++i, j += 2) {
52
+ const high = hexCodeMap[hexText[j]];
53
+ const low = hexCodeMap[hexText[j + 1]];
54
+ if (high === undefined) {
55
+ throw new Error(`[hexText2bytes] bad hex string, unknown char (${hexText[j]}).`);
56
+ }
57
+ if (low === undefined) {
58
+ throw new Error(`[hexText2bytes] bad hex string, unknown char (${hexText[j + 1]}).`);
59
+ }
60
+ bytes[i] = (high << 4) | low;
61
+ }
62
+ return bytes;
63
+ }
64
+ function bytes2HexText(bytes) {
65
+ const hexText = new Array(bytes.length * 2);
66
+ for (let i = 0, j = 0; i < bytes.length; ++i, j += 2) {
67
+ const high = bytes[i] >>> 4;
68
+ const low = bytes[i] & 0x0f;
69
+ hexText[j] = invHexCodeMap[high];
70
+ hexText[j + 1] = invHexCodeMap[low];
71
+ }
72
+ return hexText.join('');
73
+ }
74
+
75
+ const textEncoder = new TextEncoder();
76
+ const textDecoder = new TextDecoder('utf8', { fatal: true });
77
+ function utf8Text2bytes(utf8Text) {
78
+ return textEncoder.encode(utf8Text);
79
+ }
80
+ function bytes2Utf8Text(bytes) {
81
+ return textDecoder.decode(bytes);
82
+ }
83
+
84
+ function text2bytes(text, encoding) {
85
+ switch (encoding) {
86
+ case 'utf8':
87
+ case 'utf-8':
88
+ return utf8Text2bytes(text);
89
+ case 'hex':
90
+ return hexText2bytes(text);
91
+ default:
92
+ throw new TypeError(`[text2bytes] Unsupported encoding: ${encoding}.`);
93
+ }
94
+ }
95
+ function bytes2text(bytes, encoding) {
96
+ switch (encoding) {
97
+ case 'utf8':
98
+ case 'utf-8':
99
+ return bytes2Utf8Text(bytes);
100
+ case 'hex':
101
+ return bytes2HexText(bytes);
102
+ default:
103
+ throw new TypeError(`[bytes2text] Unsupported encoding: ${encoding}.`);
104
+ }
105
+ }
106
+
107
+ function destroyBytes(bytes) {
108
+ bytes.fill(0);
109
+ bytes.fill(1);
110
+ bytes.fill(Math.random() * 127);
111
+ }
112
+ function destroyBytesList(bytesList) {
113
+ for (const bytes of bytesList)
114
+ destroyBytes(bytes);
115
+ }
116
+
117
+ function mergeBytes(bytesList) {
118
+ const bytesSize = bytesList.reduce((acc, cur) => acc + cur.length, 0);
119
+ const result = new Uint8Array(bytesSize);
120
+ let k = 0;
121
+ for (const bytes of bytesList) {
122
+ for (let i = 0; i < bytes.length; ++i, ++k) {
123
+ result[k] = bytes[i];
124
+ }
125
+ }
126
+ return result;
127
+ }
128
+
129
+ function randomBytes(size) {
130
+ const bytes = new Uint8Array(size);
131
+ for (let i = 0; i < size; i++) {
132
+ const v = Math.random() * 256;
133
+ bytes[i] = v >>> 0;
134
+ }
135
+ return bytes;
136
+ }
137
+
138
+ exports.bytes2HexText = bytes2HexText;
139
+ exports.bytes2Utf8Text = bytes2Utf8Text;
140
+ exports.bytes2text = bytes2text;
141
+ exports.destroyBytes = destroyBytes;
142
+ exports.destroyBytesList = destroyBytesList;
143
+ exports.hexText2bytes = hexText2bytes;
144
+ exports.mergeBytes = mergeBytes;
145
+ exports.randomBytes = randomBytes;
146
+ exports.text2bytes = text2bytes;
147
+ exports.utf8Text2bytes = utf8Text2bytes;
@@ -0,0 +1,136 @@
1
+ const hexCodeMap = {
2
+ 0: 0x00,
3
+ 1: 0x01,
4
+ 2: 0x02,
5
+ 3: 0x03,
6
+ 4: 0x04,
7
+ 5: 0x05,
8
+ 6: 0x06,
9
+ 7: 0x07,
10
+ 8: 0x08,
11
+ 9: 0x09,
12
+ a: 0x0a,
13
+ b: 0x0b,
14
+ c: 0x0c,
15
+ d: 0x0d,
16
+ e: 0x0e,
17
+ f: 0x0f,
18
+ A: 0x0a,
19
+ B: 0x0b,
20
+ C: 0x0c,
21
+ D: 0x0d,
22
+ E: 0x0e,
23
+ F: 0x0f,
24
+ };
25
+ const invHexCodeMap = [
26
+ '0',
27
+ '1',
28
+ '2',
29
+ '3',
30
+ '4',
31
+ '5',
32
+ '6',
33
+ '7',
34
+ '8',
35
+ '9',
36
+ 'a',
37
+ 'b',
38
+ 'c',
39
+ 'd',
40
+ 'e',
41
+ 'f',
42
+ ];
43
+ function hexText2bytes(hexText) {
44
+ if (hexText.length % 2 !== 0) {
45
+ throw new Error('[hexText2bytes] Hex string length must be even.');
46
+ }
47
+ const bytesSize = hexText.length / 2;
48
+ const bytes = new Uint8Array(bytesSize);
49
+ for (let i = 0, j = 0; i < bytesSize; ++i, j += 2) {
50
+ const high = hexCodeMap[hexText[j]];
51
+ const low = hexCodeMap[hexText[j + 1]];
52
+ if (high === undefined) {
53
+ throw new Error(`[hexText2bytes] bad hex string, unknown char (${hexText[j]}).`);
54
+ }
55
+ if (low === undefined) {
56
+ throw new Error(`[hexText2bytes] bad hex string, unknown char (${hexText[j + 1]}).`);
57
+ }
58
+ bytes[i] = (high << 4) | low;
59
+ }
60
+ return bytes;
61
+ }
62
+ function bytes2HexText(bytes) {
63
+ const hexText = new Array(bytes.length * 2);
64
+ for (let i = 0, j = 0; i < bytes.length; ++i, j += 2) {
65
+ const high = bytes[i] >>> 4;
66
+ const low = bytes[i] & 0x0f;
67
+ hexText[j] = invHexCodeMap[high];
68
+ hexText[j + 1] = invHexCodeMap[low];
69
+ }
70
+ return hexText.join('');
71
+ }
72
+
73
+ const textEncoder = new TextEncoder();
74
+ const textDecoder = new TextDecoder('utf8', { fatal: true });
75
+ function utf8Text2bytes(utf8Text) {
76
+ return textEncoder.encode(utf8Text);
77
+ }
78
+ function bytes2Utf8Text(bytes) {
79
+ return textDecoder.decode(bytes);
80
+ }
81
+
82
+ function text2bytes(text, encoding) {
83
+ switch (encoding) {
84
+ case 'utf8':
85
+ case 'utf-8':
86
+ return utf8Text2bytes(text);
87
+ case 'hex':
88
+ return hexText2bytes(text);
89
+ default:
90
+ throw new TypeError(`[text2bytes] Unsupported encoding: ${encoding}.`);
91
+ }
92
+ }
93
+ function bytes2text(bytes, encoding) {
94
+ switch (encoding) {
95
+ case 'utf8':
96
+ case 'utf-8':
97
+ return bytes2Utf8Text(bytes);
98
+ case 'hex':
99
+ return bytes2HexText(bytes);
100
+ default:
101
+ throw new TypeError(`[bytes2text] Unsupported encoding: ${encoding}.`);
102
+ }
103
+ }
104
+
105
+ function destroyBytes(bytes) {
106
+ bytes.fill(0);
107
+ bytes.fill(1);
108
+ bytes.fill(Math.random() * 127);
109
+ }
110
+ function destroyBytesList(bytesList) {
111
+ for (const bytes of bytesList)
112
+ destroyBytes(bytes);
113
+ }
114
+
115
+ function mergeBytes(bytesList) {
116
+ const bytesSize = bytesList.reduce((acc, cur) => acc + cur.length, 0);
117
+ const result = new Uint8Array(bytesSize);
118
+ let k = 0;
119
+ for (const bytes of bytesList) {
120
+ for (let i = 0; i < bytes.length; ++i, ++k) {
121
+ result[k] = bytes[i];
122
+ }
123
+ }
124
+ return result;
125
+ }
126
+
127
+ function randomBytes(size) {
128
+ const bytes = new Uint8Array(size);
129
+ for (let i = 0; i < size; i++) {
130
+ const v = Math.random() * 256;
131
+ bytes[i] = v >>> 0;
132
+ }
133
+ return bytes;
134
+ }
135
+
136
+ export { bytes2HexText, bytes2Utf8Text, bytes2text, destroyBytes, destroyBytesList, hexText2bytes, mergeBytes, randomBytes, text2bytes, utf8Text2bytes };
@@ -0,0 +1,38 @@
1
+ type IByteEncoding = 'utf8' | 'utf-8' | 'hex';
2
+
3
+ /**
4
+ * Encode the string to Uint8Array with the given encoding.
5
+ * @param data
6
+ * @param encoding
7
+ * @returns
8
+ */
9
+ declare function text2bytes(text: string, encoding: IByteEncoding): Uint8Array;
10
+ declare function bytes2text(bytes: Readonly<Uint8Array>, encoding: IByteEncoding): string;
11
+
12
+ declare function hexText2bytes(hexText: string): Uint8Array;
13
+ declare function bytes2HexText(bytes: Readonly<Uint8Array>): string;
14
+
15
+ declare function utf8Text2bytes(utf8Text: string): Uint8Array;
16
+ declare function bytes2Utf8Text(bytes: Readonly<Uint8Array>): string;
17
+
18
+ /**
19
+ * Fill the bytes with random numbers.
20
+ * @param bytes
21
+ */
22
+ declare function destroyBytes(bytes: Uint8Array): void;
23
+ /**
24
+ * Destroy bytes lists.
25
+ * @param bytesList
26
+ */
27
+ declare function destroyBytesList(bytesList: Uint8Array[]): void;
28
+
29
+ declare function mergeBytes(bytesList: ReadonlyArray<Uint8Array>): Uint8Array;
30
+
31
+ /**
32
+ * Generate a random bytes with the given size.
33
+ * @param size
34
+ * @returns
35
+ */
36
+ declare function randomBytes(size: number): Uint8Array;
37
+
38
+ export { type IByteEncoding, bytes2HexText, bytes2Utf8Text, bytes2text, destroyBytes, destroyBytesList, hexText2bytes, mergeBytes, randomBytes, text2bytes, utf8Text2bytes };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@guanghechen/byte",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "Utility functions for bytes (Uint8Array).",
5
+ "author": {
6
+ "name": "guanghechen",
7
+ "url": "https://github.com/guanghechen/"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/guanghechen/sora/tree/@guanghechen/byte@1.0.0-alpha.0",
12
+ "directory": "packages/byte"
13
+ },
14
+ "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/byte@1.0.0-alpha.0/packages/byte#readme",
15
+ "keywords": [
16
+ "Uint8Array"
17
+ ],
18
+ "type": "module",
19
+ "exports": {
20
+ ".": {
21
+ "source": "./src/index.ts",
22
+ "import": "./lib/esm/index.mjs",
23
+ "require": "./lib/cjs/index.cjs",
24
+ "types": "./lib/types/index.d.ts"
25
+ }
26
+ },
27
+ "source": "./src/index.ts",
28
+ "main": "./lib/cjs/index.cjs",
29
+ "module": "./lib/esm/index.mjs",
30
+ "types": "./lib/types/index.d.ts",
31
+ "license": "MIT",
32
+ "engines": {
33
+ "node": ">= 16.0.0"
34
+ },
35
+ "files": [
36
+ "lib/",
37
+ "!lib/**/*.map",
38
+ "package.json",
39
+ "CHANGELOG.md",
40
+ "LICENSE",
41
+ "README.md"
42
+ ],
43
+ "scripts": {
44
+ "build": "../../node_modules/.bin/rimraf lib/ && ../../node_modules/.bin/cross-env NODE_ENV=production ../../node_modules/.bin/rollup -c ../../rollup.config.mjs",
45
+ "prepublishOnly": "yarn build",
46
+ "test": "node --experimental-vm-modules ../../node_modules/.bin/jest --config ../../jest.config.mjs --rootDir ."
47
+ },
48
+ "gitHead": "c62824a430a456f7a6c0823d7c32002a24fe738d"
49
+ }