@guanghechen/byte 2.0.0 → 2.0.2
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 +29 -0
- package/LICENSE +21 -0
- package/README.md +67 -1
- package/lib/cjs/index.cjs +2 -2
- package/lib/esm/index.mjs +1 -1
- package/lib/types/index.d.ts +1 -1
- package/package.json +13 -14
package/CHANGELOG.md
CHANGED
|
@@ -1,8 +1,37 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 2.0.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- chore: vitest config auto-load aliases and coverage thresholds; style/doc formatting updates
|
|
8
|
+
|
|
3
9
|
All notable changes to this project will be documented in this file. See
|
|
4
10
|
[Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
11
|
|
|
12
|
+
## 2.0.1 (2025-02-07)
|
|
13
|
+
|
|
14
|
+
### Improvements
|
|
15
|
+
|
|
16
|
+
- Use `node:` protocol for builtin module imports
|
|
17
|
+
- Clean up build configs and standardize package exports
|
|
18
|
+
|
|
19
|
+
### Documentation
|
|
20
|
+
|
|
21
|
+
- Update README.md
|
|
22
|
+
|
|
23
|
+
### Miscellaneous
|
|
24
|
+
|
|
25
|
+
- Add LICENSE file
|
|
26
|
+
- Migrate from lerna to changesets
|
|
27
|
+
|
|
28
|
+
## 2.0.0 (2025-01-15)
|
|
29
|
+
|
|
30
|
+
### Improvements
|
|
31
|
+
|
|
32
|
+
- Upgrade to stable release
|
|
33
|
+
- Switch to recommended ESLint stack
|
|
34
|
+
|
|
6
35
|
## 1.0.0-alpha.7 (2024-09-18)
|
|
7
36
|
|
|
8
37
|
- :art: format codes ([177eb54](https://github.com/guanghechen/sora/commit/177eb54))
|
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
|
@@ -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
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
|
|
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 =
|
|
228
|
+
const bytes = node_crypto.randomBytes(size);
|
|
229
229
|
return Uint8Array.from(bytes);
|
|
230
230
|
}
|
|
231
231
|
|
package/lib/esm/index.mjs
CHANGED
package/lib/types/index.d.ts
CHANGED
|
@@ -43,7 +43,7 @@ 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
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guanghechen/byte",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
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@
|
|
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@
|
|
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",
|
|
@@ -29,13 +29,6 @@
|
|
|
29
29
|
"module": "./lib/esm/index.mjs",
|
|
30
30
|
"types": "./lib/types/index.d.ts",
|
|
31
31
|
"license": "MIT",
|
|
32
|
-
"scripts": {
|
|
33
|
-
"build": "rollup -c ../../rollup.config.mjs",
|
|
34
|
-
"clean": "rimraf lib",
|
|
35
|
-
"test": "vitest run --config ../../vitest.config.ts",
|
|
36
|
-
"test:coverage": "vitest run --config ../../vitest.config.ts --coverage",
|
|
37
|
-
"test:update": "vitest run --config ../../vitest.config.ts -u"
|
|
38
|
-
},
|
|
39
32
|
"files": [
|
|
40
33
|
"lib/",
|
|
41
34
|
"!lib/**/*.map",
|
|
@@ -44,5 +37,11 @@
|
|
|
44
37
|
"LICENSE",
|
|
45
38
|
"README.md"
|
|
46
39
|
],
|
|
47
|
-
"
|
|
48
|
-
|
|
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
|
+
}
|