@atcute/uint8array 1.0.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/LICENSE ADDED
@@ -0,0 +1,17 @@
1
+ Permission is hereby granted, free of charge, to any person obtaining a copy
2
+ of this software and associated documentation files (the "Software"), to deal
3
+ in the Software without restriction, including without limitation the rights
4
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
5
+ copies of the Software, and to permit persons to whom the Software is
6
+ furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all
9
+ copies or substantial portions of the Software.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @atcute/uint8array
2
+
3
+ uint8array utilities
@@ -0,0 +1,9 @@
1
+ export declare const alloc: (size: number) => Uint8Array;
2
+ export declare const allocUnsafe: (size: number) => Uint8Array;
3
+ export declare const compare: (a: Uint8Array, b: Uint8Array) => number;
4
+ export declare const equals: (a: Uint8Array, b: Uint8Array) => boolean;
5
+ export declare const timingSafeEquals: (a: Uint8Array, b: Uint8Array) => boolean;
6
+ export declare const concat: (arrays: Uint8Array[], size?: number) => Uint8Array;
7
+ export declare const encodeUtf8Into: (to: Uint8Array, str: string, offset?: number, length?: number) => number;
8
+ export declare const decodeUtf8From: (from: Uint8Array, offset?: number, length?: number) => string;
9
+ export declare const toSha256: (buffer: Uint8Array) => Promise<Uint8Array>;
@@ -0,0 +1,39 @@
1
+ import { allocUnsafe as _allocUnsafe, concatArrayBuffers as _concat } from 'bun';
2
+ import { Buffer as NodeBuffer } from 'node:buffer';
3
+ import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto';
4
+ const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
5
+ const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
6
+ const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
7
+ const _utf8Write = /*#__PURE__*/ NodeBuffer.prototype.utf8Write;
8
+ const toUint8Array = (buffer) => {
9
+ return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
10
+ };
11
+ export const alloc = (size) => {
12
+ return new Uint8Array(size);
13
+ };
14
+ export const allocUnsafe = (size) => {
15
+ return _allocUnsafe(size);
16
+ };
17
+ export const compare = (a, b) => {
18
+ return _compare.call(a, b);
19
+ };
20
+ export const equals = (a, b) => {
21
+ return _equals.call(a, b);
22
+ };
23
+ export const timingSafeEquals = (a, b) => {
24
+ return _timingSafeEqual(a, b);
25
+ };
26
+ export const concat = (arrays, size) => {
27
+ // Bun's typings is slightly wrong, *you can* pass `size: undefined` with `asUint8Array: true`
28
+ return _concat(arrays, size, true);
29
+ };
30
+ export const encodeUtf8Into = (to, str, offset, length) => {
31
+ return _utf8Write.call(to, str, offset, length);
32
+ };
33
+ export const decodeUtf8From = (from, offset = 0, length = from.length) => {
34
+ return _utf8Slice.call(from, offset, offset + length);
35
+ };
36
+ export const toSha256 = async (buffer) => {
37
+ return toUint8Array(_hash('SHA-256', buffer, 'buffer'));
38
+ };
39
+ //# sourceMappingURL=index.bun.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.bun.js","sourceRoot":"","sources":["../lib/index.bun.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,IAAI,YAAY,EAAE,kBAAkB,IA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M,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAa,EAAE,CAAa,EAAW,EAAE;IACzE,OAAO,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAC/B,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,MAAoB,EAAE,IAAa,EAAc,EAAE;IACzE,8FAA8F;IAC9F,OAAO,OAAO,CAAC,MAAM,EAAE,IAAc,EAAE,IAAI,CAAC,CAAC;AAC9C,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,EAAc,EAAE,GAAW,EAAE,MAAe,EAAE,MAAe,EAAU,EAAE;IACvG,OAAO,UAAU,CAAC,IAAI,CAAC,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AACjD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,CAC7B,IAAgB,EAChB,SAAiB,CAAC,EAClB,SAAiB,IAAI,CAAC,MAAM,EACnB,EAAE;IACX,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;AACvD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAAE,MAAkB,EAAuB,EAAE;IACzE,OAAO,YAAY,CAAC,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;AACzD,CAAC,CAAC"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * creates an Uint8Array of the requested size, with the contents zeroed
3
+ */
4
+ export declare const alloc: (size: number) => Uint8Array;
5
+ /**
6
+ * creates an Uint8Array of the requested size, where the contents may not be
7
+ * zeroed out. only use if you're certain that the contents will be overwritten
8
+ */
9
+ export declare const allocUnsafe: (size: number) => Uint8Array;
10
+ /**
11
+ * compares two Uint8Array buffers
12
+ */
13
+ export declare const compare: (a: Uint8Array, b: Uint8Array) => number;
14
+ /**
15
+ * checks if the two Uint8Array buffers are equal
16
+ */
17
+ export declare const equals: (a: Uint8Array, b: Uint8Array) => boolean;
18
+ /**
19
+ * checks if the two Uint8Array buffers are equal, timing-safe version
20
+ */
21
+ export declare const timingSafeEquals: (a: Uint8Array, b: Uint8Array) => boolean;
22
+ /**
23
+ * concatenates multiple Uint8Array buffers into one
24
+ */
25
+ export declare const concat: (arrays: Uint8Array[], size?: number) => Uint8Array;
26
+ /**
27
+ * encodes a UTF-8 string into the buffer
28
+ */
29
+ export declare const encodeUtf8Into: (to: Uint8Array, str: string, offset?: number, length?: number) => number;
30
+ /**
31
+ * decodes a UTF-8 string from a buffer
32
+ */
33
+ export declare const decodeUtf8From: (from: Uint8Array, offset?: number, length?: number) => string;
34
+ /**
35
+ * get a SHA-256 digest of this buffer
36
+ */
37
+ export declare const toSha256: (buffer: Uint8Array) => Promise<Uint8Array>;
package/dist/index.js ADDED
@@ -0,0 +1,130 @@
1
+ const textEncoder = new TextEncoder();
2
+ const textDecoder = new TextDecoder();
3
+ const subtle = /*#__PURE__*/ crypto.subtle;
4
+ /**
5
+ * creates an Uint8Array of the requested size, with the contents zeroed
6
+ */
7
+ export const alloc = (size) => {
8
+ return new Uint8Array(size);
9
+ };
10
+ /**
11
+ * creates an Uint8Array of the requested size, where the contents may not be
12
+ * zeroed out. only use if you're certain that the contents will be overwritten
13
+ */
14
+ export const allocUnsafe = alloc;
15
+ /**
16
+ * compares two Uint8Array buffers
17
+ */
18
+ export const compare = (a, b) => {
19
+ const alen = a.length;
20
+ const blen = b.length;
21
+ if (alen > blen) {
22
+ return 1;
23
+ }
24
+ if (alen < blen) {
25
+ return -1;
26
+ }
27
+ for (let i = 0; i < alen; i++) {
28
+ const ax = a[i];
29
+ const bx = b[i];
30
+ if (ax < bx) {
31
+ return -1;
32
+ }
33
+ if (ax > bx) {
34
+ return 1;
35
+ }
36
+ }
37
+ return 0;
38
+ };
39
+ /**
40
+ * checks if the two Uint8Array buffers are equal
41
+ */
42
+ export const equals = (a, b) => {
43
+ if (a === b) {
44
+ return true;
45
+ }
46
+ let len;
47
+ if ((len = a.length) === b.length) {
48
+ while (len--) {
49
+ if (a[len] !== b[len]) {
50
+ return false;
51
+ }
52
+ }
53
+ }
54
+ return len === -1;
55
+ };
56
+ /**
57
+ * checks if the two Uint8Array buffers are equal, timing-safe version
58
+ */
59
+ export const timingSafeEquals = (a, b) => {
60
+ let len;
61
+ let out = 0;
62
+ if ((len = a.length) === b.length) {
63
+ while (len--) {
64
+ out |= a[len] ^ b[len];
65
+ }
66
+ }
67
+ return len === -1 && out === 0;
68
+ };
69
+ /**
70
+ * concatenates multiple Uint8Array buffers into one
71
+ */
72
+ export const concat = (arrays, size) => {
73
+ let written = 0;
74
+ let len = arrays.length;
75
+ let idx;
76
+ if (size === undefined) {
77
+ for (idx = size = 0; idx < len; idx++) {
78
+ const chunk = arrays[idx];
79
+ size += chunk.length;
80
+ }
81
+ }
82
+ const buffer = new Uint8Array(size);
83
+ for (idx = 0; idx < len; idx++) {
84
+ const chunk = arrays[idx];
85
+ buffer.set(chunk, written);
86
+ written += chunk.length;
87
+ }
88
+ return buffer;
89
+ };
90
+ /**
91
+ * encodes a UTF-8 string into the buffer
92
+ */
93
+ export const encodeUtf8Into = (to, str, offset, length) => {
94
+ let buffer;
95
+ if (offset === undefined) {
96
+ buffer = to;
97
+ }
98
+ else if (length === undefined) {
99
+ buffer = to.subarray(offset);
100
+ }
101
+ else {
102
+ buffer = to.subarray(offset, offset + length);
103
+ }
104
+ const result = textEncoder.encodeInto(str, buffer);
105
+ return result.written;
106
+ };
107
+ /**
108
+ * decodes a UTF-8 string from a buffer
109
+ */
110
+ export const decodeUtf8From = (from, offset, length) => {
111
+ let buffer;
112
+ if (offset === undefined) {
113
+ buffer = from;
114
+ }
115
+ else if (length === undefined) {
116
+ buffer = from.subarray(offset);
117
+ }
118
+ else {
119
+ buffer = from.subarray(offset, offset + length);
120
+ }
121
+ const result = textDecoder.decode(buffer);
122
+ return result;
123
+ };
124
+ /**
125
+ * get a SHA-256 digest of this buffer
126
+ */
127
+ export const toSha256 = async (buffer) => {
128
+ return new Uint8Array(await subtle.digest('SHA-256', buffer));
129
+ };
130
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AACtC,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AAEtC,MAAM,MAAM,GAAG,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC;AAE3C;;GAEG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,IAAY,EAAc,EAAE;IACjD,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;AAC7B,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,CAAC;AAEjC;;GAEG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,CAAa,EAAE,CAAa,EAAU,EAAE;IAC/D,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC;IACtB,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC;IAEtB,IAAI,IAAI,GAAG,IAAI,EAAE,CAAC;QACjB,OAAO,CAAC,CAAC;IACV,CAAC;IACD,IAAI,IAAI,GAAG,IAAI,EAAE,CAAC;QACjB,OAAO,CAAC,CAAC,CAAC;IACX,CAAC;IAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;QAC/B,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAChB,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAEhB,IAAI,EAAE,GAAG,EAAE,EAAE,CAAC;YACb,OAAO,CAAC,CAAC,CAAC;QACX,CAAC;QAED,IAAI,EAAE,GAAG,EAAE,EAAE,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AC,MAAM,cAAc,GAAG,CAAC,IAAgB,EAAE,MAAe,EAAE,MAAe,EAAU,EAAE;IAC5F,IAAI,MAAkB,CAAC;IAEvB,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QAC1B,MAAM,GAAG,IAAI,CAAC;IACf,CAAC;SAAM,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACjC,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IAChC,CAAC;SAAM,CAAC;QACP,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;IACjD,CAAC;IAED,MAAM,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAE1C,OAAO,MAAM,CAAC;AACf,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAAE,MAAkB,EAAuB,EAAE;IACzE,OAAO,IAAI,UAAU,CAAC,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;AAC/D,CAAC,CAAC"}
@@ -0,0 +1,9 @@
1
+ export declare const alloc: (size: number) => Uint8Array;
2
+ export declare const allocUnsafe: (size: number) => Uint8Array;
3
+ export declare const compare: (a: Uint8Array, b: Uint8Array) => number;
4
+ export declare const equals: (a: Uint8Array, b: Uint8Array) => boolean;
5
+ export declare const timingSafeEquals: (a: Uint8Array, b: Uint8Array) => boolean;
6
+ export declare const concat: (arrays: Uint8Array[], size?: number) => Uint8Array;
7
+ export declare const encodeUtf8Into: (to: Uint8Array, str: string, offset?: number, length?: number) => number;
8
+ export declare const decodeUtf8From: (from: Uint8Array, offset?: number, length?: number) => string;
9
+ export declare const toSha256: (buffer: Uint8Array) => Promise<Uint8Array>;
@@ -0,0 +1,40 @@
1
+ import { Buffer as NodeBuffer } from 'node:buffer';
2
+ import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto';
3
+ const _alloc = /*#__PURE__*/ NodeBuffer.alloc;
4
+ const _allocUnsafe = /*#__PURE__*/ NodeBuffer.allocUnsafe;
5
+ const _concat = /*#__PURE__*/ NodeBuffer.concat;
6
+ const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
7
+ const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
8
+ const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
9
+ const _utf8Write = /*#__PURE__*/ NodeBuffer.prototype.utf8Write;
10
+ const toUint8Array = (buffer) => {
11
+ return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
12
+ };
13
+ export const alloc = (size) => {
14
+ return toUint8Array(_alloc(size));
15
+ };
16
+ export const allocUnsafe = (size) => {
17
+ return toUint8Array(_allocUnsafe(size));
18
+ };
19
+ export const compare = (a, b) => {
20
+ return _compare.call(a, b);
21
+ };
22
+ export const equals = (a, b) => {
23
+ return _equals.call(a, b);
24
+ };
25
+ export const timingSafeEquals = (a, b) => {
26
+ return _timingSafeEqual(a, b);
27
+ };
28
+ export const concat = (arrays, size) => {
29
+ return toUint8Array(_concat(arrays, size));
30
+ };
31
+ export const encodeUtf8Into = (to, str, offset, length) => {
32
+ return _utf8Write.call(to, str, offset, length);
33
+ };
34
+ export const decodeUtf8From = (from, offset = 0, length = from.length) => {
35
+ return _utf8Slice.call(from, offset, offset + length);
36
+ };
37
+ export const toSha256 = async (buffer) => {
38
+ return toUint8Array(_hash('SHA-256', buffer, 'buffer'));
39
+ };
40
+ //# sourceMappingURL=index.node.js.map
@@ -0,0 +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,EAAE,IAAI,IAAI,KAAK,EAAE,eAAe,IAAI,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEjF,MAAM,MAAM,GA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AEF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAa,EAAE,CAAa,EAAW,EAAE;IACzE,OAAO,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAC/B,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,MAAoB,EAAE,IAAa,EAAc,EAAE;IACzE,OAAO,YAAY,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;AAC5C,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,EAAc,EAAE,GAAW,EAAE,MAAe,EAAE,MAAe,EAAU,EAAE;IACvG,OAAO,UAAU,CAAC,IAAI,CAAC,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AACjD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,CAC7B,IAAgB,EAChB,SAAiB,CAAC,EAClB,SAAiB,IAAI,CAAC,MAAM,EACnB,EAAE;IACX,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;AACvD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAAE,MAAkB,EAAuB,EAAE;IACzE,OAAO,YAAY,CAAC,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;AACzD,CAAC,CAAC"}
@@ -0,0 +1,54 @@
1
+ import { allocUnsafe as _allocUnsafe, concatArrayBuffers as _concat } from 'bun';
2
+
3
+ import { Buffer as NodeBuffer } from 'node:buffer';
4
+ import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto';
5
+
6
+ const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
7
+ const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
8
+ const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
9
+ const _utf8Write = /*#__PURE__*/ NodeBuffer.prototype.utf8Write;
10
+
11
+ const toUint8Array = (buffer: NodeBuffer) => {
12
+ return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
13
+ };
14
+
15
+ export const alloc = (size: number): Uint8Array => {
16
+ return new Uint8Array(size);
17
+ };
18
+
19
+ export const allocUnsafe = (size: number): Uint8Array => {
20
+ return _allocUnsafe(size);
21
+ };
22
+
23
+ export const compare = (a: Uint8Array, b: Uint8Array): number => {
24
+ return _compare.call(a, b);
25
+ };
26
+
27
+ export const equals = (a: Uint8Array, b: Uint8Array): boolean => {
28
+ return _equals.call(a, b);
29
+ };
30
+
31
+ export const timingSafeEquals = (a: Uint8Array, b: Uint8Array): boolean => {
32
+ return _timingSafeEqual(a, b);
33
+ };
34
+
35
+ export const concat = (arrays: Uint8Array[], size?: number): Uint8Array => {
36
+ // Bun's typings is slightly wrong, *you can* pass `size: undefined` with `asUint8Array: true`
37
+ return _concat(arrays, size as number, true);
38
+ };
39
+
40
+ export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, length?: number): number => {
41
+ return _utf8Write.call(to, str, offset, length);
42
+ };
43
+
44
+ export const decodeUtf8From = (
45
+ from: Uint8Array,
46
+ offset: number = 0,
47
+ length: number = from.length,
48
+ ): string => {
49
+ return _utf8Slice.call(from, offset, offset + length);
50
+ };
51
+
52
+ export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array> => {
53
+ return toUint8Array(_hash('SHA-256', buffer, 'buffer'));
54
+ };
@@ -0,0 +1,55 @@
1
+ import { Buffer as NodeBuffer } from 'node:buffer';
2
+ import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto';
3
+
4
+ const _alloc = /*#__PURE__*/ NodeBuffer.alloc;
5
+ const _allocUnsafe = /*#__PURE__*/ NodeBuffer.allocUnsafe;
6
+ const _concat = /*#__PURE__*/ NodeBuffer.concat;
7
+
8
+ const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
9
+ const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
10
+ const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
11
+ const _utf8Write = /*#__PURE__*/ NodeBuffer.prototype.utf8Write;
12
+
13
+ const toUint8Array = (buffer: NodeBuffer) => {
14
+ return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
15
+ };
16
+
17
+ export const alloc = (size: number): Uint8Array => {
18
+ return toUint8Array(_alloc(size));
19
+ };
20
+
21
+ export const allocUnsafe = (size: number): Uint8Array => {
22
+ return toUint8Array(_allocUnsafe(size));
23
+ };
24
+
25
+ export const compare = (a: Uint8Array, b: Uint8Array): number => {
26
+ return _compare.call(a, b);
27
+ };
28
+
29
+ export const equals = (a: Uint8Array, b: Uint8Array): boolean => {
30
+ return _equals.call(a, b);
31
+ };
32
+
33
+ export const timingSafeEquals = (a: Uint8Array, b: Uint8Array): boolean => {
34
+ return _timingSafeEqual(a, b);
35
+ };
36
+
37
+ export const concat = (arrays: Uint8Array[], size?: number): Uint8Array => {
38
+ return toUint8Array(_concat(arrays, size));
39
+ };
40
+
41
+ export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, length?: number): number => {
42
+ return _utf8Write.call(to, str, offset, length);
43
+ };
44
+
45
+ export const decodeUtf8From = (
46
+ from: Uint8Array,
47
+ offset: number = 0,
48
+ length: number = from.length,
49
+ ): string => {
50
+ return _utf8Slice.call(from, offset, offset + length);
51
+ };
52
+
53
+ export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array> => {
54
+ return toUint8Array(_hash('SHA-256', buffer, 'buffer'));
55
+ };
package/lib/index.ts ADDED
@@ -0,0 +1,155 @@
1
+ const textEncoder = new TextEncoder();
2
+ const textDecoder = new TextDecoder();
3
+
4
+ const subtle = /*#__PURE__*/ crypto.subtle;
5
+
6
+ /**
7
+ * creates an Uint8Array of the requested size, with the contents zeroed
8
+ */
9
+ export const alloc = (size: number): Uint8Array => {
10
+ return new Uint8Array(size);
11
+ };
12
+
13
+ /**
14
+ * creates an Uint8Array of the requested size, where the contents may not be
15
+ * zeroed out. only use if you're certain that the contents will be overwritten
16
+ */
17
+ export const allocUnsafe = alloc;
18
+
19
+ /**
20
+ * compares two Uint8Array buffers
21
+ */
22
+ export const compare = (a: Uint8Array, b: Uint8Array): number => {
23
+ const alen = a.length;
24
+ const blen = b.length;
25
+
26
+ if (alen > blen) {
27
+ return 1;
28
+ }
29
+ if (alen < blen) {
30
+ return -1;
31
+ }
32
+
33
+ for (let i = 0; i < alen; i++) {
34
+ const ax = a[i];
35
+ const bx = b[i];
36
+
37
+ if (ax < bx) {
38
+ return -1;
39
+ }
40
+
41
+ if (ax > bx) {
42
+ return 1;
43
+ }
44
+ }
45
+
46
+ return 0;
47
+ };
48
+
49
+ /**
50
+ * checks if the two Uint8Array buffers are equal
51
+ */
52
+ export const equals = (a: Uint8Array, b: Uint8Array): boolean => {
53
+ if (a === b) {
54
+ return true;
55
+ }
56
+
57
+ let len: number;
58
+ if ((len = a.length) === b.length) {
59
+ while (len--) {
60
+ if (a[len] !== b[len]) {
61
+ return false;
62
+ }
63
+ }
64
+ }
65
+
66
+ return len === -1;
67
+ };
68
+
69
+ /**
70
+ * checks if the two Uint8Array buffers are equal, timing-safe version
71
+ */
72
+ export const timingSafeEquals = (a: Uint8Array, b: Uint8Array): boolean => {
73
+ let len: number;
74
+ let out = 0;
75
+ if ((len = a.length) === b.length) {
76
+ while (len--) {
77
+ out |= a[len] ^ b[len];
78
+ }
79
+ }
80
+
81
+ return len === -1 && out === 0;
82
+ };
83
+
84
+ /**
85
+ * concatenates multiple Uint8Array buffers into one
86
+ */
87
+ export const concat = (arrays: Uint8Array[], size?: number): Uint8Array => {
88
+ let written = 0;
89
+
90
+ let len = arrays.length;
91
+ let idx: number;
92
+
93
+ if (size === undefined) {
94
+ for (idx = size = 0; idx < len; idx++) {
95
+ const chunk = arrays[idx];
96
+ size += chunk.length;
97
+ }
98
+ }
99
+
100
+ const buffer = new Uint8Array(size);
101
+
102
+ for (idx = 0; idx < len; idx++) {
103
+ const chunk = arrays[idx];
104
+
105
+ buffer.set(chunk, written);
106
+ written += chunk.length;
107
+ }
108
+
109
+ return buffer;
110
+ };
111
+
112
+ /**
113
+ * encodes a UTF-8 string into the buffer
114
+ */
115
+ export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, length?: number): number => {
116
+ let buffer: Uint8Array;
117
+
118
+ if (offset === undefined) {
119
+ buffer = to;
120
+ } else if (length === undefined) {
121
+ buffer = to.subarray(offset);
122
+ } else {
123
+ buffer = to.subarray(offset, offset + length);
124
+ }
125
+
126
+ const result = textEncoder.encodeInto(str, buffer);
127
+
128
+ return result.written;
129
+ };
130
+
131
+ /**
132
+ * decodes a UTF-8 string from a buffer
133
+ */
134
+ export const decodeUtf8From = (from: Uint8Array, offset?: number, length?: number): string => {
135
+ let buffer: Uint8Array;
136
+
137
+ if (offset === undefined) {
138
+ buffer = from;
139
+ } else if (length === undefined) {
140
+ buffer = from.subarray(offset);
141
+ } else {
142
+ buffer = from.subarray(offset, offset + length);
143
+ }
144
+
145
+ const result = textDecoder.decode(buffer);
146
+
147
+ return result;
148
+ };
149
+
150
+ /**
151
+ * get a SHA-256 digest of this buffer
152
+ */
153
+ export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array> => {
154
+ return new Uint8Array(await subtle.digest('SHA-256', buffer));
155
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "type": "module",
3
+ "name": "@atcute/uint8array",
4
+ "version": "1.0.0",
5
+ "description": "uint8array utilities",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "url": "https://github.com/mary-ext/atcute",
9
+ "directory": "packages/utilities/uint8array"
10
+ },
11
+ "files": [
12
+ "dist/",
13
+ "lib/",
14
+ "!lib/**/*.bench.ts",
15
+ "!lib/**/*.test.ts"
16
+ ],
17
+ "exports": {
18
+ ".": {
19
+ "bun": "./dist/index.bun.js",
20
+ "node": "./dist/index.node.js",
21
+ "default": "./dist/index.js"
22
+ }
23
+ },
24
+ "sideEffects": false,
25
+ "devDependencies": {
26
+ "@types/bun": "^1.1.14"
27
+ },
28
+ "scripts": {
29
+ "build": "tsc --project tsconfig.build.json",
30
+ "test": "bun test --coverage",
31
+ "prepublish": "rm -rf dist; pnpm run build"
32
+ }
33
+ }