@atcute/uint8array 1.0.5 → 1.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/README.md +7 -1
- package/dist/index.bun.d.ts +31 -7
- package/dist/index.bun.d.ts.map +1 -1
- package/dist/index.bun.js +134 -38
- package/dist/index.bun.js.map +1 -1
- package/dist/index.d.ts +31 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +197 -33
- package/dist/index.js.map +1 -1
- package/dist/index.node.d.ts +34 -7
- package/dist/index.node.d.ts.map +1 -1
- package/dist/index.node.js +136 -1
- package/dist/index.node.js.map +1 -1
- package/lib/index.bun.ts +114 -45
- package/lib/index.node.ts +119 -1
- package/lib/index.ts +178 -33
- package/package.json +9 -7
package/dist/index.node.d.ts
CHANGED
|
@@ -1,11 +1,38 @@
|
|
|
1
1
|
export declare const alloc: (size: number) => Uint8Array<ArrayBuffer>;
|
|
2
2
|
export declare const allocUnsafe: (size: number) => Uint8Array<ArrayBuffer>;
|
|
3
|
-
export declare const compare: (a: Uint8Array
|
|
4
|
-
export declare const equals: (a: Uint8Array
|
|
5
|
-
export declare const timingSafeEquals: (a: Uint8Array
|
|
6
|
-
export declare const concat: (arrays: Uint8Array[], size?: number) => Uint8Array<ArrayBuffer>;
|
|
3
|
+
export declare const compare: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => number;
|
|
4
|
+
export declare const equals: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => boolean;
|
|
5
|
+
export declare const timingSafeEquals: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => boolean;
|
|
6
|
+
export declare const concat: (arrays: Uint8Array<ArrayBufferLike>[], size?: number | undefined) => Uint8Array<ArrayBuffer>;
|
|
7
7
|
export declare const encodeUtf8: (str: string) => Uint8Array<ArrayBuffer>;
|
|
8
|
-
export declare const encodeUtf8Into: (to: Uint8Array
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
export declare const encodeUtf8Into: (to: Uint8Array<ArrayBufferLike>, str: string, offset?: number | undefined, length?: number | undefined) => number;
|
|
9
|
+
/**
|
|
10
|
+
* decodes a UTF-8 string from a given buffer
|
|
11
|
+
* @param from source buffer
|
|
12
|
+
* @param offset byte offset to start reading from
|
|
13
|
+
* @param length number of bytes to read
|
|
14
|
+
* @returns decoded string
|
|
15
|
+
*/
|
|
16
|
+
export declare const decodeUtf8From: (from: Uint8Array<ArrayBufferLike>, offset?: number, length?: number) => string;
|
|
17
|
+
/**
|
|
18
|
+
* calculates the UTF-8 byte length of a string
|
|
19
|
+
* @param str string to measure
|
|
20
|
+
* @returns byte length when encoded as UTF-8
|
|
21
|
+
*/
|
|
22
|
+
export declare const getUtf8Length: (str: string) => number;
|
|
23
|
+
/**
|
|
24
|
+
* checks if a string's UTF-8 byte length is within a given range
|
|
25
|
+
* @param str string to measure
|
|
26
|
+
* @param min minimum byte length (inclusive)
|
|
27
|
+
* @param max maximum byte length (inclusive)
|
|
28
|
+
* @returns true if byte length is within [min, max]
|
|
29
|
+
*/
|
|
30
|
+
export declare const isUtf8LengthInRange: (str: string, min: number, max: number) => boolean;
|
|
31
|
+
export declare const toSha256: (buffer: Uint8Array<ArrayBufferLike>) => Promise<Uint8Array<ArrayBuffer>>;
|
|
32
|
+
/**
|
|
33
|
+
* generates cryptographically secure random bytes
|
|
34
|
+
* @param size number of bytes to generate
|
|
35
|
+
* @returns buffer filled with random bytes
|
|
36
|
+
*/
|
|
37
|
+
export declare const randomBytes: (size: number) => Uint8Array<ArrayBuffer>;
|
|
11
38
|
//# sourceMappingURL=index.node.d.ts.map
|
package/dist/index.node.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.node.d.ts","sourceRoot":"","sources":["../lib/index.node.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.node.d.ts","sourceRoot":"","sources":["../lib/index.node.ts"],"names":[],"mappings":"AAuBA,eAAO,MAAM,KAAK,2CAEjB,CAAC;AAEF,eAAO,MAAM,WAAW,2CAEvB,CAAC;AAEF,eAAO,MAAM,OAAO,4EAEnB,CAAC;AAEF,eAAO,MAAM,MAAM,6EAElB,CAAC;AAEF,eAAO,MAAM,gBAAgB,6EAE5B,CAAC;AAEF,eAAO,MAAM,MAAM,+FAElB,CAAC;AAEF,eAAO,MAAM,UAAU,0CAEtB,CAAC;AAEF,eAAO,MAAM,cAAc,oHAE1B,CAAC;AAyEF;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,iFAU1B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,yBAEzB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,oDAG/B,CAAC;AAEF,eAAO,MAAM,QAAQ,2EAEpB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,WAAW,2CAEvB,CAAC"}
|
package/dist/index.node.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Buffer as NodeBuffer } from 'node:buffer';
|
|
2
|
-
import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { hash as _hash, randomFillSync as _randomFillSync, timingSafeEqual as _timingSafeEqual, } from 'node:crypto';
|
|
3
3
|
const _alloc = /*#__PURE__*/ NodeBuffer.alloc;
|
|
4
4
|
const _allocUnsafe = /*#__PURE__*/ NodeBuffer.allocUnsafe;
|
|
5
5
|
const _concat = /*#__PURE__*/ NodeBuffer.concat;
|
|
6
6
|
const _from = /*#__PURE__*/ NodeBuffer.from;
|
|
7
|
+
const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength;
|
|
7
8
|
const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
|
|
8
9
|
const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
|
|
9
10
|
const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
|
|
@@ -35,10 +36,144 @@ export const encodeUtf8 = (str) => {
|
|
|
35
36
|
export const encodeUtf8Into = (to, str, offset, length) => {
|
|
36
37
|
return _utf8Write.call(to, str, offset, length);
|
|
37
38
|
};
|
|
39
|
+
const _fromCharCode = String.fromCharCode;
|
|
40
|
+
// fully unrolled short string decoder, inspired by cbor-x
|
|
41
|
+
// returns null if non-ASCII byte encountered, signaling fallback to utf8Slice
|
|
42
|
+
const _shortString = (from, p, length) => {
|
|
43
|
+
if (length < 4) {
|
|
44
|
+
if (length < 2) {
|
|
45
|
+
if (length === 0)
|
|
46
|
+
return '';
|
|
47
|
+
const a = from[p];
|
|
48
|
+
if (a & 0x80)
|
|
49
|
+
return null;
|
|
50
|
+
return _fromCharCode(a);
|
|
51
|
+
}
|
|
52
|
+
const a = from[p];
|
|
53
|
+
const b = from[p + 1];
|
|
54
|
+
if ((a | b) & 0x80)
|
|
55
|
+
return null;
|
|
56
|
+
if (length === 2)
|
|
57
|
+
return _fromCharCode(a, b);
|
|
58
|
+
const c = from[p + 2];
|
|
59
|
+
if (c & 0x80)
|
|
60
|
+
return null;
|
|
61
|
+
return _fromCharCode(a, b, c);
|
|
62
|
+
}
|
|
63
|
+
const a = from[p];
|
|
64
|
+
const b = from[p + 1];
|
|
65
|
+
const c = from[p + 2];
|
|
66
|
+
const d = from[p + 3];
|
|
67
|
+
if ((a | b | c | d) & 0x80)
|
|
68
|
+
return null;
|
|
69
|
+
if (length < 8) {
|
|
70
|
+
if (length === 4)
|
|
71
|
+
return _fromCharCode(a, b, c, d);
|
|
72
|
+
const e = from[p + 4];
|
|
73
|
+
if (e & 0x80)
|
|
74
|
+
return null;
|
|
75
|
+
if (length === 5)
|
|
76
|
+
return _fromCharCode(a, b, c, d, e);
|
|
77
|
+
const f = from[p + 5];
|
|
78
|
+
if (f & 0x80)
|
|
79
|
+
return null;
|
|
80
|
+
if (length === 6)
|
|
81
|
+
return _fromCharCode(a, b, c, d, e, f);
|
|
82
|
+
const g = from[p + 6];
|
|
83
|
+
if (g & 0x80)
|
|
84
|
+
return null;
|
|
85
|
+
return _fromCharCode(a, b, c, d, e, f, g);
|
|
86
|
+
}
|
|
87
|
+
const e = from[p + 4];
|
|
88
|
+
const f = from[p + 5];
|
|
89
|
+
const g = from[p + 6];
|
|
90
|
+
const h = from[p + 7];
|
|
91
|
+
if ((e | f | g | h) & 0x80)
|
|
92
|
+
return null;
|
|
93
|
+
if (length < 12) {
|
|
94
|
+
if (length === 8)
|
|
95
|
+
return _fromCharCode(a, b, c, d, e, f, g, h);
|
|
96
|
+
const i = from[p + 8];
|
|
97
|
+
if (i & 0x80)
|
|
98
|
+
return null;
|
|
99
|
+
if (length === 9)
|
|
100
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
101
|
+
const j = from[p + 9];
|
|
102
|
+
if (j & 0x80)
|
|
103
|
+
return null;
|
|
104
|
+
if (length === 10)
|
|
105
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
106
|
+
const k = from[p + 10];
|
|
107
|
+
if (k & 0x80)
|
|
108
|
+
return null;
|
|
109
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
110
|
+
}
|
|
111
|
+
const i = from[p + 8];
|
|
112
|
+
const j = from[p + 9];
|
|
113
|
+
const k = from[p + 10];
|
|
114
|
+
const l = from[p + 11];
|
|
115
|
+
if ((i | j | k | l) & 0x80)
|
|
116
|
+
return null;
|
|
117
|
+
if (length === 12)
|
|
118
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
119
|
+
const m = from[p + 12];
|
|
120
|
+
if (m & 0x80)
|
|
121
|
+
return null;
|
|
122
|
+
if (length === 13)
|
|
123
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
124
|
+
const n = from[p + 13];
|
|
125
|
+
if (n & 0x80)
|
|
126
|
+
return null;
|
|
127
|
+
if (length === 14)
|
|
128
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
129
|
+
const o = from[p + 14];
|
|
130
|
+
if (o & 0x80)
|
|
131
|
+
return null;
|
|
132
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* decodes a UTF-8 string from a given buffer
|
|
136
|
+
* @param from source buffer
|
|
137
|
+
* @param offset byte offset to start reading from
|
|
138
|
+
* @param length number of bytes to read
|
|
139
|
+
* @returns decoded string
|
|
140
|
+
*/
|
|
38
141
|
export const decodeUtf8From = (from, offset = 0, length = from.length) => {
|
|
142
|
+
if (length <= 15) {
|
|
143
|
+
const result = _shortString(from, offset, length);
|
|
144
|
+
if (result !== null)
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
39
147
|
return _utf8Slice.call(from, offset, offset + length);
|
|
40
148
|
};
|
|
149
|
+
/**
|
|
150
|
+
* calculates the UTF-8 byte length of a string
|
|
151
|
+
* @param str string to measure
|
|
152
|
+
* @returns byte length when encoded as UTF-8
|
|
153
|
+
*/
|
|
154
|
+
export const getUtf8Length = (str) => {
|
|
155
|
+
return _byteLength(str, 'utf8');
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* checks if a string's UTF-8 byte length is within a given range
|
|
159
|
+
* @param str string to measure
|
|
160
|
+
* @param min minimum byte length (inclusive)
|
|
161
|
+
* @param max maximum byte length (inclusive)
|
|
162
|
+
* @returns true if byte length is within [min, max]
|
|
163
|
+
*/
|
|
164
|
+
export const isUtf8LengthInRange = (str, min, max) => {
|
|
165
|
+
const len = _byteLength(str, 'utf8');
|
|
166
|
+
return len >= min && len <= max;
|
|
167
|
+
};
|
|
41
168
|
export const toSha256 = async (buffer) => {
|
|
42
169
|
return toUint8Array(_hash('sha256', buffer, 'buffer'));
|
|
43
170
|
};
|
|
171
|
+
/**
|
|
172
|
+
* generates cryptographically secure random bytes
|
|
173
|
+
* @param size number of bytes to generate
|
|
174
|
+
* @returns buffer filled with random bytes
|
|
175
|
+
*/
|
|
176
|
+
export const randomBytes = (size) => {
|
|
177
|
+
return _randomFillSync(toUint8Array(_allocUnsafe(size)));
|
|
178
|
+
};
|
|
44
179
|
//# sourceMappingURL=index.node.js.map
|
package/dist/index.node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.node.js","sourceRoot":"","sources":["../lib/index.node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,
|
|
1
|
+
{"version":3,"file":"index.node.js","sourceRoot":"","sources":["../lib/index.node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EACN,IAAI,IAAI,KAAK,EACb,cAAc,IAAI,eAAe,EACjC,eAAe,IAAI,gBAAgB,GACnC,MAAM,aAAa,CAAC;AAErB,MAAM,MAAM,GAAG,aAAa,CAAC,UAAU,CAAC,KAAK,CAAC;AAC9C,MAAM,YAAY,GAAG,aAAa,CAAC,UAAU,CAAC,WAAW,CAAC;AAC1D,MAAM,OAAO,GAAG,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC;AAChD,MAAM,KAAK,GAAG,aAAa,CAAC,UAAU,CAAC,IAAI,CAAC;AAE5C,MAAM,WAAW,GAAG,aAAa,CAAC,UAAU,CAAC,UAAU,CAAC;AAExD,MAAM,QAAQ,GAAG,aAAa,CAAC,UAAU,CAAC,SAAS,CAAC,OAAO,CAAC;AAC5D,MAAM,OAAO,GAAG,aAAa,CAAC,UAAU,CAAC,SAAS,CAAC,MAAM,CAAC;AAC1D,MAAM,UAAU,GAAG,aAAa,CAAC,UAAU,CAAC,SAAS,CAAC,SAAS,CAAC;AAChE,MAAM,UAAU,GAAG,aAAa,CAAC,UAAU,CAAC,SAAS,CAAC,SAAS,CAAC;AAEhE,MAAM,YAAY,GAAG,CAAC,MAAkB,EAAE,EAAE,CAAC;IAC5C,OAAO,IA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AE,CAAC;IACtF,MAAM,GAAG,GAAG,WAAW,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IACrC,OAAO,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,CAAC;AAAA,CAChC,CAAC;AAEF,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAAE,MAAkB,EAAoC,EAAE,CAAC;IACvF,OAAO,YAAY,CAAC,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,CAA4B,CAAC;AAAA,CAClF,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,IAAY,EAA2B,EAAE,CAAC;IACrE,OAAO,eAAe,CAAC,YAAY,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAA4B,CAAC;AAAA,CACpF,CAAC"}
|
package/lib/index.bun.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { allocUnsafe as _allocUnsafe, concatArrayBuffers as _concat } from 'bun';
|
|
2
|
-
|
|
3
2
|
import { Buffer as NodeBuffer } from 'node:buffer';
|
|
4
3
|
import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto';
|
|
5
4
|
|
|
5
|
+
const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength;
|
|
6
|
+
|
|
6
7
|
const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
|
|
7
8
|
const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
|
|
9
|
+
const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
|
|
8
10
|
|
|
9
11
|
const textEncoder = new TextEncoder();
|
|
10
|
-
const textDecoder = new TextDecoder();
|
|
11
12
|
|
|
12
13
|
const toUint8Array = (buffer: NodeBuffer) => {
|
|
13
14
|
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
@@ -54,58 +55,126 @@ export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, len
|
|
|
54
55
|
return result.written;
|
|
55
56
|
};
|
|
56
57
|
|
|
57
|
-
const
|
|
58
|
+
const _fromCharCode = String.fromCharCode;
|
|
59
|
+
|
|
60
|
+
// fully unrolled short string decoder, inspired by cbor-x
|
|
61
|
+
// returns null if non-ASCII byte encountered, signaling fallback to utf8Slice
|
|
62
|
+
const _shortString = (from: Uint8Array, p: number, length: number): string | null => {
|
|
63
|
+
if (length < 4) {
|
|
64
|
+
if (length < 2) {
|
|
65
|
+
if (length === 0) return '';
|
|
66
|
+
const a = from[p];
|
|
67
|
+
if (a & 0x80) return null;
|
|
68
|
+
return _fromCharCode(a);
|
|
69
|
+
}
|
|
70
|
+
const a = from[p];
|
|
71
|
+
const b = from[p + 1];
|
|
72
|
+
if ((a | b) & 0x80) return null;
|
|
73
|
+
if (length === 2) return _fromCharCode(a, b);
|
|
74
|
+
const c = from[p + 2];
|
|
75
|
+
if (c & 0x80) return null;
|
|
76
|
+
return _fromCharCode(a, b, c);
|
|
77
|
+
}
|
|
78
|
+
const a = from[p];
|
|
79
|
+
const b = from[p + 1];
|
|
80
|
+
const c = from[p + 2];
|
|
81
|
+
const d = from[p + 3];
|
|
82
|
+
if ((a | b | c | d) & 0x80) return null;
|
|
83
|
+
if (length < 8) {
|
|
84
|
+
if (length === 4) return _fromCharCode(a, b, c, d);
|
|
85
|
+
const e = from[p + 4];
|
|
86
|
+
if (e & 0x80) return null;
|
|
87
|
+
if (length === 5) return _fromCharCode(a, b, c, d, e);
|
|
88
|
+
const f = from[p + 5];
|
|
89
|
+
if (f & 0x80) return null;
|
|
90
|
+
if (length === 6) return _fromCharCode(a, b, c, d, e, f);
|
|
91
|
+
const g = from[p + 6];
|
|
92
|
+
if (g & 0x80) return null;
|
|
93
|
+
return _fromCharCode(a, b, c, d, e, f, g);
|
|
94
|
+
}
|
|
95
|
+
const e = from[p + 4];
|
|
96
|
+
const f = from[p + 5];
|
|
97
|
+
const g = from[p + 6];
|
|
98
|
+
const h = from[p + 7];
|
|
99
|
+
if ((e | f | g | h) & 0x80) return null;
|
|
100
|
+
if (length < 12) {
|
|
101
|
+
if (length === 8) return _fromCharCode(a, b, c, d, e, f, g, h);
|
|
102
|
+
const i = from[p + 8];
|
|
103
|
+
if (i & 0x80) return null;
|
|
104
|
+
if (length === 9) return _fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
105
|
+
const j = from[p + 9];
|
|
106
|
+
if (j & 0x80) return null;
|
|
107
|
+
if (length === 10) return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
108
|
+
const k = from[p + 10];
|
|
109
|
+
if (k & 0x80) return null;
|
|
110
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
111
|
+
}
|
|
112
|
+
const i = from[p + 8];
|
|
113
|
+
const j = from[p + 9];
|
|
114
|
+
const k = from[p + 10];
|
|
115
|
+
const l = from[p + 11];
|
|
116
|
+
if ((i | j | k | l) & 0x80) return null;
|
|
117
|
+
if (length === 12) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
118
|
+
const m = from[p + 12];
|
|
119
|
+
if (m & 0x80) return null;
|
|
120
|
+
if (length === 13) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
121
|
+
const n = from[p + 13];
|
|
122
|
+
if (n & 0x80) return null;
|
|
123
|
+
if (length === 14) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
124
|
+
const o = from[p + 14];
|
|
125
|
+
if (o & 0x80) return null;
|
|
126
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
127
|
+
};
|
|
58
128
|
|
|
59
129
|
/**
|
|
60
130
|
* decodes a UTF-8 string from a given buffer
|
|
131
|
+
* @param from source buffer
|
|
132
|
+
* @param offset byte offset to start reading from
|
|
133
|
+
* @param length number of bytes to read
|
|
134
|
+
* @returns decoded string
|
|
61
135
|
*/
|
|
62
|
-
export const decodeUtf8From = (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
buffer = from.subarray(offset, offset + length);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const end = buffer.length;
|
|
74
|
-
if (end > 24) {
|
|
75
|
-
return textDecoder.decode(buffer);
|
|
136
|
+
export const decodeUtf8From = (
|
|
137
|
+
from: Uint8Array,
|
|
138
|
+
offset: number = 0,
|
|
139
|
+
length: number = from.length,
|
|
140
|
+
): string => {
|
|
141
|
+
if (length <= 15) {
|
|
142
|
+
const result = _shortString(from, offset, length);
|
|
143
|
+
if (result !== null) return result;
|
|
76
144
|
}
|
|
145
|
+
return _utf8Slice.call(from, offset, offset + length);
|
|
146
|
+
};
|
|
77
147
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const d = buffer[idx + 3];
|
|
87
|
-
|
|
88
|
-
if ((a | b | c | d) & 0x80) {
|
|
89
|
-
return str + textDecoder.decode(buffer.subarray(idx));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
str += fromCharCode(a, b, c, d);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
for (; idx < end; idx++) {
|
|
96
|
-
const x = buffer[idx];
|
|
97
|
-
|
|
98
|
-
if (x & 0x80) {
|
|
99
|
-
return str + textDecoder.decode(buffer.subarray(idx));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
str += fromCharCode(x);
|
|
103
|
-
}
|
|
148
|
+
/**
|
|
149
|
+
* calculates the UTF-8 byte length of a string
|
|
150
|
+
* @param str string to measure
|
|
151
|
+
* @returns byte length when encoded as UTF-8
|
|
152
|
+
*/
|
|
153
|
+
export const getUtf8Length = (str: string): number => {
|
|
154
|
+
return _byteLength(str, 'utf8');
|
|
155
|
+
};
|
|
104
156
|
|
|
105
|
-
|
|
106
|
-
|
|
157
|
+
/**
|
|
158
|
+
* checks if a string's UTF-8 byte length is within a given range
|
|
159
|
+
* @param str string to measure
|
|
160
|
+
* @param min minimum byte length (inclusive)
|
|
161
|
+
* @param max maximum byte length (inclusive)
|
|
162
|
+
* @returns true if byte length is within [min, max]
|
|
163
|
+
*/
|
|
164
|
+
export const isUtf8LengthInRange = (str: string, min: number, max: number): boolean => {
|
|
165
|
+
const len = _byteLength(str, 'utf8');
|
|
166
|
+
return len >= min && len <= max;
|
|
107
167
|
};
|
|
108
168
|
|
|
109
169
|
export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => {
|
|
110
170
|
return toUint8Array(_hash('sha256', buffer, 'buffer')) as Uint8Array<ArrayBuffer>;
|
|
111
171
|
};
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* generates cryptographically secure random bytes
|
|
175
|
+
* @param size number of bytes to generate
|
|
176
|
+
* @returns buffer filled with random bytes
|
|
177
|
+
*/
|
|
178
|
+
export const randomBytes = (size: number): Uint8Array<ArrayBuffer> => {
|
|
179
|
+
return crypto.getRandomValues(new Uint8Array(size));
|
|
180
|
+
};
|
package/lib/index.node.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { Buffer as NodeBuffer } from 'node:buffer';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
hash as _hash,
|
|
4
|
+
randomFillSync as _randomFillSync,
|
|
5
|
+
timingSafeEqual as _timingSafeEqual,
|
|
6
|
+
} from 'node:crypto';
|
|
3
7
|
|
|
4
8
|
const _alloc = /*#__PURE__*/ NodeBuffer.alloc;
|
|
5
9
|
const _allocUnsafe = /*#__PURE__*/ NodeBuffer.allocUnsafe;
|
|
6
10
|
const _concat = /*#__PURE__*/ NodeBuffer.concat;
|
|
7
11
|
const _from = /*#__PURE__*/ NodeBuffer.from;
|
|
8
12
|
|
|
13
|
+
const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength;
|
|
14
|
+
|
|
9
15
|
const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
|
|
10
16
|
const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
|
|
11
17
|
const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
|
|
@@ -47,14 +53,126 @@ export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, len
|
|
|
47
53
|
return _utf8Write.call(to, str, offset, length);
|
|
48
54
|
};
|
|
49
55
|
|
|
56
|
+
const _fromCharCode = String.fromCharCode;
|
|
57
|
+
|
|
58
|
+
// fully unrolled short string decoder, inspired by cbor-x
|
|
59
|
+
// returns null if non-ASCII byte encountered, signaling fallback to utf8Slice
|
|
60
|
+
const _shortString = (from: Uint8Array, p: number, length: number): string | null => {
|
|
61
|
+
if (length < 4) {
|
|
62
|
+
if (length < 2) {
|
|
63
|
+
if (length === 0) return '';
|
|
64
|
+
const a = from[p];
|
|
65
|
+
if (a & 0x80) return null;
|
|
66
|
+
return _fromCharCode(a);
|
|
67
|
+
}
|
|
68
|
+
const a = from[p];
|
|
69
|
+
const b = from[p + 1];
|
|
70
|
+
if ((a | b) & 0x80) return null;
|
|
71
|
+
if (length === 2) return _fromCharCode(a, b);
|
|
72
|
+
const c = from[p + 2];
|
|
73
|
+
if (c & 0x80) return null;
|
|
74
|
+
return _fromCharCode(a, b, c);
|
|
75
|
+
}
|
|
76
|
+
const a = from[p];
|
|
77
|
+
const b = from[p + 1];
|
|
78
|
+
const c = from[p + 2];
|
|
79
|
+
const d = from[p + 3];
|
|
80
|
+
if ((a | b | c | d) & 0x80) return null;
|
|
81
|
+
if (length < 8) {
|
|
82
|
+
if (length === 4) return _fromCharCode(a, b, c, d);
|
|
83
|
+
const e = from[p + 4];
|
|
84
|
+
if (e & 0x80) return null;
|
|
85
|
+
if (length === 5) return _fromCharCode(a, b, c, d, e);
|
|
86
|
+
const f = from[p + 5];
|
|
87
|
+
if (f & 0x80) return null;
|
|
88
|
+
if (length === 6) return _fromCharCode(a, b, c, d, e, f);
|
|
89
|
+
const g = from[p + 6];
|
|
90
|
+
if (g & 0x80) return null;
|
|
91
|
+
return _fromCharCode(a, b, c, d, e, f, g);
|
|
92
|
+
}
|
|
93
|
+
const e = from[p + 4];
|
|
94
|
+
const f = from[p + 5];
|
|
95
|
+
const g = from[p + 6];
|
|
96
|
+
const h = from[p + 7];
|
|
97
|
+
if ((e | f | g | h) & 0x80) return null;
|
|
98
|
+
if (length < 12) {
|
|
99
|
+
if (length === 8) return _fromCharCode(a, b, c, d, e, f, g, h);
|
|
100
|
+
const i = from[p + 8];
|
|
101
|
+
if (i & 0x80) return null;
|
|
102
|
+
if (length === 9) return _fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
103
|
+
const j = from[p + 9];
|
|
104
|
+
if (j & 0x80) return null;
|
|
105
|
+
if (length === 10) return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
106
|
+
const k = from[p + 10];
|
|
107
|
+
if (k & 0x80) return null;
|
|
108
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
109
|
+
}
|
|
110
|
+
const i = from[p + 8];
|
|
111
|
+
const j = from[p + 9];
|
|
112
|
+
const k = from[p + 10];
|
|
113
|
+
const l = from[p + 11];
|
|
114
|
+
if ((i | j | k | l) & 0x80) return null;
|
|
115
|
+
if (length === 12) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
116
|
+
const m = from[p + 12];
|
|
117
|
+
if (m & 0x80) return null;
|
|
118
|
+
if (length === 13) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
119
|
+
const n = from[p + 13];
|
|
120
|
+
if (n & 0x80) return null;
|
|
121
|
+
if (length === 14) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
122
|
+
const o = from[p + 14];
|
|
123
|
+
if (o & 0x80) return null;
|
|
124
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* decodes a UTF-8 string from a given buffer
|
|
129
|
+
* @param from source buffer
|
|
130
|
+
* @param offset byte offset to start reading from
|
|
131
|
+
* @param length number of bytes to read
|
|
132
|
+
* @returns decoded string
|
|
133
|
+
*/
|
|
50
134
|
export const decodeUtf8From = (
|
|
51
135
|
from: Uint8Array,
|
|
52
136
|
offset: number = 0,
|
|
53
137
|
length: number = from.length,
|
|
54
138
|
): string => {
|
|
139
|
+
if (length <= 15) {
|
|
140
|
+
const result = _shortString(from, offset, length);
|
|
141
|
+
if (result !== null) return result;
|
|
142
|
+
}
|
|
55
143
|
return _utf8Slice.call(from, offset, offset + length);
|
|
56
144
|
};
|
|
57
145
|
|
|
146
|
+
/**
|
|
147
|
+
* calculates the UTF-8 byte length of a string
|
|
148
|
+
* @param str string to measure
|
|
149
|
+
* @returns byte length when encoded as UTF-8
|
|
150
|
+
*/
|
|
151
|
+
export const getUtf8Length = (str: string): number => {
|
|
152
|
+
return _byteLength(str, 'utf8');
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* checks if a string's UTF-8 byte length is within a given range
|
|
157
|
+
* @param str string to measure
|
|
158
|
+
* @param min minimum byte length (inclusive)
|
|
159
|
+
* @param max maximum byte length (inclusive)
|
|
160
|
+
* @returns true if byte length is within [min, max]
|
|
161
|
+
*/
|
|
162
|
+
export const isUtf8LengthInRange = (str: string, min: number, max: number): boolean => {
|
|
163
|
+
const len = _byteLength(str, 'utf8');
|
|
164
|
+
return len >= min && len <= max;
|
|
165
|
+
};
|
|
166
|
+
|
|
58
167
|
export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => {
|
|
59
168
|
return toUint8Array(_hash('sha256', buffer, 'buffer')) as Uint8Array<ArrayBuffer>;
|
|
60
169
|
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* generates cryptographically secure random bytes
|
|
173
|
+
* @param size number of bytes to generate
|
|
174
|
+
* @returns buffer filled with random bytes
|
|
175
|
+
*/
|
|
176
|
+
export const randomBytes = (size: number): Uint8Array<ArrayBuffer> => {
|
|
177
|
+
return _randomFillSync(toUint8Array(_allocUnsafe(size))) as Uint8Array<ArrayBuffer>;
|
|
178
|
+
};
|