@guanghechen/byte 1.0.3 → 2.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present guanghechen (https://github.com/guanghechen)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <header>
2
2
  <h1 align="center">
3
- <a href="https://github.com/guanghechen/sora/tree/@guanghechen/byte@1.0.3/packages/byte#readme">@guanghechen/byte</a>
3
+ <a href="https://github.com/guanghechen/sora/tree/@guanghechen/byte@2.0.0/packages/byte#readme">@guanghechen/byte</a>
4
4
  </h1>
5
5
  <div align="center">
6
6
  <a href="https://www.npmjs.com/package/@guanghechen/byte">
@@ -49,7 +49,8 @@
49
49
  </header>
50
50
  <br/>
51
51
 
52
- Utility functions for bytes (Uint8Array).
52
+ Utility functions for bytes (Uint8Array). Provides encoding/decoding between bytes and various text
53
+ formats (base64, hex, utf8), along with common byte manipulation utilities.
53
54
 
54
55
  ## Install
55
56
 
@@ -67,5 +68,70 @@ Utility functions for bytes (Uint8Array).
67
68
 
68
69
  ## Usage
69
70
 
71
+ ### Encoding / Decoding
72
+
73
+ ```typescript
74
+ import {
75
+ text2bytes,
76
+ bytes2text,
77
+ base64Text2bytes,
78
+ bytes2base64Text,
79
+ hexText2bytes,
80
+ bytes2hexText,
81
+ utf8Text2bytes,
82
+ bytes2utf8Text,
83
+ } from '@guanghechen/byte'
84
+
85
+ // Generic encoding/decoding with encoding type
86
+ const bytes = text2bytes('Hello World', 'utf8')
87
+ const text = bytes2text(bytes, 'utf8') // 'Hello World'
88
+
89
+ // Base64
90
+ const base64Bytes = base64Text2bytes('SGVsbG8gV29ybGQ=')
91
+ const base64Text = bytes2base64Text(new Uint8Array([72, 101, 108, 108, 111])) // 'SGVsbG8='
92
+
93
+ // Hex
94
+ const hexBytes = hexText2bytes('48656c6c6f')
95
+ const hexText = bytes2hexText(new Uint8Array([72, 101, 108, 108, 111])) // '48656c6c6f'
96
+
97
+ // UTF-8
98
+ const utf8Bytes = utf8Text2bytes('Hello')
99
+ const utf8Text = bytes2utf8Text(utf8Bytes) // 'Hello'
100
+ ```
101
+
102
+ ### Byte Manipulation
103
+
104
+ ```typescript
105
+ import {
106
+ mergeBytes,
107
+ areSameBytes,
108
+ randomBytes,
109
+ destroyBytes,
110
+ } from '@guanghechen/byte'
111
+
112
+ // Merge multiple Uint8Arrays into one
113
+ const merged = mergeBytes([
114
+ new Uint8Array([1, 2]),
115
+ new Uint8Array([3, 4]),
116
+ ]) // Uint8Array [1, 2, 3, 4]
117
+
118
+ // Compare two Uint8Arrays
119
+ const isEqual = areSameBytes(
120
+ new Uint8Array([1, 2, 3]),
121
+ new Uint8Array([1, 2, 3]),
122
+ ) // true
123
+
124
+ // Generate cryptographically random bytes
125
+ const random = randomBytes(16) // 16 random bytes
126
+
127
+ // Securely destroy sensitive byte data
128
+ const sensitiveData = new Uint8Array([1, 2, 3, 4])
129
+ destroyBytes(sensitiveData) // Overwrites with zeros, ones, then random values
130
+ ```
131
+
132
+ ## Reference
133
+
134
+ - [homepage][homepage]
135
+
70
136
  [homepage]:
71
- https://github.com/guanghechen/sora/tree/@guanghechen/byte@1.0.3/packages/byte#readme
137
+ https://github.com/guanghechen/sora/tree/@guanghechen/byte@2.0.0/packages/byte#readme
package/lib/cjs/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var crypto = require('crypto');
3
+ var node_crypto = require('node:crypto');
4
4
 
5
5
  const CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
6
6
  const CODE_PADDING = '=';
@@ -225,7 +225,7 @@ function mergeBytes(bytesList) {
225
225
  }
226
226
 
227
227
  function randomBytes(size) {
228
- const bytes = crypto.randomBytes(size);
228
+ const bytes = node_crypto.randomBytes(size);
229
229
  return Uint8Array.from(bytes);
230
230
  }
231
231
 
package/lib/esm/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { randomBytes as randomBytes$1 } from 'crypto';
1
+ import { randomBytes as randomBytes$1 } from 'node:crypto';
2
2
 
3
3
  const CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
4
4
  const CODE_PADDING = '=';
@@ -43,11 +43,12 @@ declare function mergeBytes(bytesList: ReadonlyArray<Uint8Array>): Uint8Array;
43
43
  /**
44
44
  * Generate a random bytes with the given size.
45
45
  *
46
- * Use randomBytes from 'crypto' cause it returns true random numbers.
46
+ * Use randomBytes from 'node:crypto' cause it returns true random numbers.
47
47
  *
48
48
  * @param size
49
49
  * @returns
50
50
  */
51
51
  declare function randomBytes(size: number): Uint8Array;
52
52
 
53
- export { type IByteEncoding, areSameBytes, base64Text2bytes, bytes2base64Text, bytes2hexText, bytes2text, bytes2utf8Text, destroyBytes, destroyBytesList, hexText2bytes, mergeBytes, randomBytes, text2bytes, utf8Text2bytes, validateBase64Text };
53
+ export { areSameBytes, base64Text2bytes, bytes2base64Text, bytes2hexText, bytes2text, bytes2utf8Text, destroyBytes, destroyBytesList, hexText2bytes, mergeBytes, randomBytes, text2bytes, utf8Text2bytes, validateBase64Text };
54
+ export type { IByteEncoding };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guanghechen/byte",
3
- "version": "1.0.3",
3
+ "version": "2.0.1",
4
4
  "description": "Utility functions for bytes (Uint8Array).",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -8,20 +8,20 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/guanghechen/sora/tree/@guanghechen/byte@1.0.2",
11
+ "url": "https://github.com/guanghechen/sora/tree/@guanghechen/byte@2.0.0",
12
12
  "directory": "packages/byte"
13
13
  },
14
- "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/byte@1.0.2/packages/byte#readme",
14
+ "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/byte@2.0.0/packages/byte#readme",
15
15
  "keywords": [
16
16
  "Uint8Array"
17
17
  ],
18
18
  "type": "module",
19
19
  "exports": {
20
20
  ".": {
21
+ "types": "./lib/types/index.d.ts",
21
22
  "source": "./src/index.ts",
22
23
  "import": "./lib/esm/index.mjs",
23
- "require": "./lib/cjs/index.cjs",
24
- "types": "./lib/types/index.d.ts"
24
+ "require": "./lib/cjs/index.cjs"
25
25
  }
26
26
  },
27
27
  "source": "./src/index.ts",
@@ -37,5 +37,11 @@
37
37
  "LICENSE",
38
38
  "README.md"
39
39
  ],
40
- "gitHead": "c46c7cc64c02c61047fbc7dff2ce8f6b21d4da2c"
41
- }
40
+ "scripts": {
41
+ "build": "rollup -c ../../rollup.config.mjs",
42
+ "clean": "rimraf lib",
43
+ "test": "vitest run --config ../../vitest.config.ts",
44
+ "test:coverage": "vitest run --config ../../vitest.config.ts --coverage",
45
+ "test:update": "vitest run --config ../../vitest.config.ts -u"
46
+ }
47
+ }