@atcute/uint8array 1.1.0 → 1.1.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/dist/index.bun.d.ts +7 -7
- package/dist/index.bun.d.ts.map +1 -1
- package/dist/index.bun.js +70 -33
- package/dist/index.bun.js.map +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +57 -27
- package/dist/index.js.map +1 -1
- package/dist/index.node.d.ts +7 -7
- package/dist/index.node.d.ts.map +1 -1
- package/dist/index.node.js +75 -38
- package/dist/index.node.js.map +1 -1
- package/lib/index.bun.ts +101 -33
- package/lib/index.node.ts +106 -38
- package/lib/index.ts +84 -27
- package/package.json +5 -4
package/dist/index.node.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
+
// oxlint-disable no-underscore-dangle
|
|
1
2
|
import { Buffer as NodeBuffer } from 'node:buffer';
|
|
2
3
|
import { hash as _hash, randomFillSync as _randomFillSync, 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 _from = /*#__PURE__*/ NodeBuffer.from;
|
|
7
|
-
const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength;
|
|
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;
|
|
4
|
+
const _alloc = /*#__PURE__*/ (() => NodeBuffer.alloc)();
|
|
5
|
+
const _allocUnsafe = /*#__PURE__*/ (() => NodeBuffer.allocUnsafe)();
|
|
6
|
+
const _concat = /*#__PURE__*/ (() => NodeBuffer.concat)();
|
|
7
|
+
const _from = /*#__PURE__*/ (() => NodeBuffer.from)();
|
|
8
|
+
const _byteLength = /*#__PURE__*/ (() => NodeBuffer.byteLength)();
|
|
9
|
+
const _compare = /*#__PURE__*/ (() => NodeBuffer.prototype.compare)();
|
|
10
|
+
const _equals = /*#__PURE__*/ (() => NodeBuffer.prototype.equals)();
|
|
11
|
+
const _utf8Slice = /*#__PURE__*/ (() => NodeBuffer.prototype.utf8Slice)();
|
|
12
|
+
const _utf8Write = /*#__PURE__*/ (() => NodeBuffer.prototype.utf8Write)();
|
|
12
13
|
const toUint8Array = (buffer) => {
|
|
13
14
|
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
14
15
|
};
|
|
@@ -42,93 +43,119 @@ const _fromCharCode = String.fromCharCode;
|
|
|
42
43
|
const _shortString = (from, p, length) => {
|
|
43
44
|
if (length < 4) {
|
|
44
45
|
if (length < 2) {
|
|
45
|
-
if (length === 0)
|
|
46
|
+
if (length === 0) {
|
|
46
47
|
return '';
|
|
48
|
+
}
|
|
47
49
|
const a = from[p];
|
|
48
|
-
if (a & 0x80)
|
|
50
|
+
if (a & 0x80) {
|
|
49
51
|
return null;
|
|
52
|
+
}
|
|
50
53
|
return _fromCharCode(a);
|
|
51
54
|
}
|
|
52
55
|
const a = from[p];
|
|
53
56
|
const b = from[p + 1];
|
|
54
|
-
if ((a | b) & 0x80)
|
|
57
|
+
if ((a | b) & 0x80) {
|
|
55
58
|
return null;
|
|
56
|
-
|
|
59
|
+
}
|
|
60
|
+
if (length === 2) {
|
|
57
61
|
return _fromCharCode(a, b);
|
|
62
|
+
}
|
|
58
63
|
const c = from[p + 2];
|
|
59
|
-
if (c & 0x80)
|
|
64
|
+
if (c & 0x80) {
|
|
60
65
|
return null;
|
|
66
|
+
}
|
|
61
67
|
return _fromCharCode(a, b, c);
|
|
62
68
|
}
|
|
63
69
|
const a = from[p];
|
|
64
70
|
const b = from[p + 1];
|
|
65
71
|
const c = from[p + 2];
|
|
66
72
|
const d = from[p + 3];
|
|
67
|
-
if ((a | b | c | d) & 0x80)
|
|
73
|
+
if ((a | b | c | d) & 0x80) {
|
|
68
74
|
return null;
|
|
75
|
+
}
|
|
69
76
|
if (length < 8) {
|
|
70
|
-
if (length === 4)
|
|
77
|
+
if (length === 4) {
|
|
71
78
|
return _fromCharCode(a, b, c, d);
|
|
79
|
+
}
|
|
72
80
|
const e = from[p + 4];
|
|
73
|
-
if (e & 0x80)
|
|
81
|
+
if (e & 0x80) {
|
|
74
82
|
return null;
|
|
75
|
-
|
|
83
|
+
}
|
|
84
|
+
if (length === 5) {
|
|
76
85
|
return _fromCharCode(a, b, c, d, e);
|
|
86
|
+
}
|
|
77
87
|
const f = from[p + 5];
|
|
78
|
-
if (f & 0x80)
|
|
88
|
+
if (f & 0x80) {
|
|
79
89
|
return null;
|
|
80
|
-
|
|
90
|
+
}
|
|
91
|
+
if (length === 6) {
|
|
81
92
|
return _fromCharCode(a, b, c, d, e, f);
|
|
93
|
+
}
|
|
82
94
|
const g = from[p + 6];
|
|
83
|
-
if (g & 0x80)
|
|
95
|
+
if (g & 0x80) {
|
|
84
96
|
return null;
|
|
97
|
+
}
|
|
85
98
|
return _fromCharCode(a, b, c, d, e, f, g);
|
|
86
99
|
}
|
|
87
100
|
const e = from[p + 4];
|
|
88
101
|
const f = from[p + 5];
|
|
89
102
|
const g = from[p + 6];
|
|
90
103
|
const h = from[p + 7];
|
|
91
|
-
if ((e | f | g | h) & 0x80)
|
|
104
|
+
if ((e | f | g | h) & 0x80) {
|
|
92
105
|
return null;
|
|
106
|
+
}
|
|
93
107
|
if (length < 12) {
|
|
94
|
-
if (length === 8)
|
|
108
|
+
if (length === 8) {
|
|
95
109
|
return _fromCharCode(a, b, c, d, e, f, g, h);
|
|
110
|
+
}
|
|
96
111
|
const i = from[p + 8];
|
|
97
|
-
if (i & 0x80)
|
|
112
|
+
if (i & 0x80) {
|
|
98
113
|
return null;
|
|
99
|
-
|
|
114
|
+
}
|
|
115
|
+
if (length === 9) {
|
|
100
116
|
return _fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
117
|
+
}
|
|
101
118
|
const j = from[p + 9];
|
|
102
|
-
if (j & 0x80)
|
|
119
|
+
if (j & 0x80) {
|
|
103
120
|
return null;
|
|
104
|
-
|
|
121
|
+
}
|
|
122
|
+
if (length === 10) {
|
|
105
123
|
return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
124
|
+
}
|
|
106
125
|
const k = from[p + 10];
|
|
107
|
-
if (k & 0x80)
|
|
126
|
+
if (k & 0x80) {
|
|
108
127
|
return null;
|
|
128
|
+
}
|
|
109
129
|
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
110
130
|
}
|
|
111
131
|
const i = from[p + 8];
|
|
112
132
|
const j = from[p + 9];
|
|
113
133
|
const k = from[p + 10];
|
|
114
134
|
const l = from[p + 11];
|
|
115
|
-
if ((i | j | k | l) & 0x80)
|
|
135
|
+
if ((i | j | k | l) & 0x80) {
|
|
116
136
|
return null;
|
|
117
|
-
|
|
137
|
+
}
|
|
138
|
+
if (length === 12) {
|
|
118
139
|
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
140
|
+
}
|
|
119
141
|
const m = from[p + 12];
|
|
120
|
-
if (m & 0x80)
|
|
142
|
+
if (m & 0x80) {
|
|
121
143
|
return null;
|
|
122
|
-
|
|
144
|
+
}
|
|
145
|
+
if (length === 13) {
|
|
123
146
|
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
147
|
+
}
|
|
124
148
|
const n = from[p + 13];
|
|
125
|
-
if (n & 0x80)
|
|
149
|
+
if (n & 0x80) {
|
|
126
150
|
return null;
|
|
127
|
-
|
|
151
|
+
}
|
|
152
|
+
if (length === 14) {
|
|
128
153
|
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
154
|
+
}
|
|
129
155
|
const o = from[p + 14];
|
|
130
|
-
if (o & 0x80)
|
|
156
|
+
if (o & 0x80) {
|
|
131
157
|
return null;
|
|
158
|
+
}
|
|
132
159
|
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
133
160
|
};
|
|
134
161
|
/**
|
|
@@ -141,8 +168,9 @@ const _shortString = (from, p, length) => {
|
|
|
141
168
|
export const decodeUtf8From = (from, offset = 0, length = from.length) => {
|
|
142
169
|
if (length <= 15) {
|
|
143
170
|
const result = _shortString(from, offset, length);
|
|
144
|
-
if (result !== null)
|
|
171
|
+
if (result !== null) {
|
|
145
172
|
return result;
|
|
173
|
+
}
|
|
146
174
|
}
|
|
147
175
|
return _utf8Slice.call(from, offset, offset + length);
|
|
148
176
|
};
|
|
@@ -162,8 +190,17 @@ export const getUtf8Length = (str) => {
|
|
|
162
190
|
* @returns true if byte length is within [min, max]
|
|
163
191
|
*/
|
|
164
192
|
export const isUtf8LengthInRange = (str, min, max) => {
|
|
165
|
-
const len =
|
|
166
|
-
|
|
193
|
+
const len = str.length;
|
|
194
|
+
// fast path: if max possible UTF-8 length is below min, fail
|
|
195
|
+
if (len * 3 < min) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
// fast path: if UTF-16 length satisfies min and max possible satisfies max
|
|
199
|
+
if (len >= min && len * 3 <= max) {
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
const utf8len = _byteLength(str, 'utf8');
|
|
203
|
+
return utf8len >= min && utf8len <= max;
|
|
167
204
|
};
|
|
168
205
|
export const toSha256 = async (buffer) => {
|
|
169
206
|
return toUint8Array(_hash('sha256', buffer, 'buffer'));
|
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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AAE,MAAM,EAAE,QAAQ,CAAC,CAA4B,CAAC;AACnF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,IAAY,EAA2B,EAAE;IACpE,OAAO,eAAe,CAAC,YAAY,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAA4B,CAAC;AACrF,CAAC,CAAC"}
|
package/lib/index.bun.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
// oxlint-disable no-underscore-dangle
|
|
2
|
+
|
|
1
3
|
import { allocUnsafe as _allocUnsafe, concatArrayBuffers as _concat } from 'bun';
|
|
2
4
|
import { Buffer as NodeBuffer } from 'node:buffer';
|
|
3
5
|
import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto';
|
|
4
6
|
|
|
5
|
-
const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength;
|
|
7
|
+
const _byteLength = /*#__PURE__*/ (() => NodeBuffer.byteLength)();
|
|
6
8
|
|
|
7
|
-
const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
|
|
8
|
-
const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
|
|
9
|
-
const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
|
|
9
|
+
const _compare = /*#__PURE__*/ (() => NodeBuffer.prototype.compare)();
|
|
10
|
+
const _equals = /*#__PURE__*/ (() => NodeBuffer.prototype.equals)();
|
|
11
|
+
const _utf8Slice = /*#__PURE__*/ (() => NodeBuffer.prototype.utf8Slice)();
|
|
10
12
|
|
|
11
13
|
const textEncoder = new TextEncoder();
|
|
12
14
|
|
|
@@ -62,67 +64,119 @@ const _fromCharCode = String.fromCharCode;
|
|
|
62
64
|
const _shortString = (from: Uint8Array, p: number, length: number): string | null => {
|
|
63
65
|
if (length < 4) {
|
|
64
66
|
if (length < 2) {
|
|
65
|
-
if (length === 0)
|
|
67
|
+
if (length === 0) {
|
|
68
|
+
return '';
|
|
69
|
+
}
|
|
66
70
|
const a = from[p];
|
|
67
|
-
if (a & 0x80)
|
|
71
|
+
if (a & 0x80) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
68
74
|
return _fromCharCode(a);
|
|
69
75
|
}
|
|
70
76
|
const a = from[p];
|
|
71
77
|
const b = from[p + 1];
|
|
72
|
-
if ((a | b) & 0x80)
|
|
73
|
-
|
|
78
|
+
if ((a | b) & 0x80) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
if (length === 2) {
|
|
82
|
+
return _fromCharCode(a, b);
|
|
83
|
+
}
|
|
74
84
|
const c = from[p + 2];
|
|
75
|
-
if (c & 0x80)
|
|
85
|
+
if (c & 0x80) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
76
88
|
return _fromCharCode(a, b, c);
|
|
77
89
|
}
|
|
78
90
|
const a = from[p];
|
|
79
91
|
const b = from[p + 1];
|
|
80
92
|
const c = from[p + 2];
|
|
81
93
|
const d = from[p + 3];
|
|
82
|
-
if ((a | b | c | d) & 0x80)
|
|
94
|
+
if ((a | b | c | d) & 0x80) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
83
97
|
if (length < 8) {
|
|
84
|
-
if (length === 4)
|
|
98
|
+
if (length === 4) {
|
|
99
|
+
return _fromCharCode(a, b, c, d);
|
|
100
|
+
}
|
|
85
101
|
const e = from[p + 4];
|
|
86
|
-
if (e & 0x80)
|
|
87
|
-
|
|
102
|
+
if (e & 0x80) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
if (length === 5) {
|
|
106
|
+
return _fromCharCode(a, b, c, d, e);
|
|
107
|
+
}
|
|
88
108
|
const f = from[p + 5];
|
|
89
|
-
if (f & 0x80)
|
|
90
|
-
|
|
109
|
+
if (f & 0x80) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
if (length === 6) {
|
|
113
|
+
return _fromCharCode(a, b, c, d, e, f);
|
|
114
|
+
}
|
|
91
115
|
const g = from[p + 6];
|
|
92
|
-
if (g & 0x80)
|
|
116
|
+
if (g & 0x80) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
93
119
|
return _fromCharCode(a, b, c, d, e, f, g);
|
|
94
120
|
}
|
|
95
121
|
const e = from[p + 4];
|
|
96
122
|
const f = from[p + 5];
|
|
97
123
|
const g = from[p + 6];
|
|
98
124
|
const h = from[p + 7];
|
|
99
|
-
if ((e | f | g | h) & 0x80)
|
|
125
|
+
if ((e | f | g | h) & 0x80) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
100
128
|
if (length < 12) {
|
|
101
|
-
if (length === 8)
|
|
129
|
+
if (length === 8) {
|
|
130
|
+
return _fromCharCode(a, b, c, d, e, f, g, h);
|
|
131
|
+
}
|
|
102
132
|
const i = from[p + 8];
|
|
103
|
-
if (i & 0x80)
|
|
104
|
-
|
|
133
|
+
if (i & 0x80) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
if (length === 9) {
|
|
137
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
138
|
+
}
|
|
105
139
|
const j = from[p + 9];
|
|
106
|
-
if (j & 0x80)
|
|
107
|
-
|
|
140
|
+
if (j & 0x80) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
if (length === 10) {
|
|
144
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
145
|
+
}
|
|
108
146
|
const k = from[p + 10];
|
|
109
|
-
if (k & 0x80)
|
|
147
|
+
if (k & 0x80) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
110
150
|
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
111
151
|
}
|
|
112
152
|
const i = from[p + 8];
|
|
113
153
|
const j = from[p + 9];
|
|
114
154
|
const k = from[p + 10];
|
|
115
155
|
const l = from[p + 11];
|
|
116
|
-
if ((i | j | k | l) & 0x80)
|
|
117
|
-
|
|
156
|
+
if ((i | j | k | l) & 0x80) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
if (length === 12) {
|
|
160
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
161
|
+
}
|
|
118
162
|
const m = from[p + 12];
|
|
119
|
-
if (m & 0x80)
|
|
120
|
-
|
|
163
|
+
if (m & 0x80) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
if (length === 13) {
|
|
167
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
168
|
+
}
|
|
121
169
|
const n = from[p + 13];
|
|
122
|
-
if (n & 0x80)
|
|
123
|
-
|
|
170
|
+
if (n & 0x80) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
if (length === 14) {
|
|
174
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
175
|
+
}
|
|
124
176
|
const o = from[p + 14];
|
|
125
|
-
if (o & 0x80)
|
|
177
|
+
if (o & 0x80) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
126
180
|
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
127
181
|
};
|
|
128
182
|
|
|
@@ -140,7 +194,9 @@ export const decodeUtf8From = (
|
|
|
140
194
|
): string => {
|
|
141
195
|
if (length <= 15) {
|
|
142
196
|
const result = _shortString(from, offset, length);
|
|
143
|
-
if (result !== null)
|
|
197
|
+
if (result !== null) {
|
|
198
|
+
return result;
|
|
199
|
+
}
|
|
144
200
|
}
|
|
145
201
|
return _utf8Slice.call(from, offset, offset + length);
|
|
146
202
|
};
|
|
@@ -162,8 +218,20 @@ export const getUtf8Length = (str: string): number => {
|
|
|
162
218
|
* @returns true if byte length is within [min, max]
|
|
163
219
|
*/
|
|
164
220
|
export const isUtf8LengthInRange = (str: string, min: number, max: number): boolean => {
|
|
165
|
-
const len =
|
|
166
|
-
|
|
221
|
+
const len = str.length;
|
|
222
|
+
|
|
223
|
+
// fast path: if max possible UTF-8 length is below min, fail
|
|
224
|
+
if (len * 3 < min) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// fast path: if UTF-16 length satisfies min and max possible satisfies max
|
|
229
|
+
if (len >= min && len * 3 <= max) {
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const utf8len = _byteLength(str, 'utf8');
|
|
234
|
+
return utf8len >= min && utf8len <= max;
|
|
167
235
|
};
|
|
168
236
|
|
|
169
237
|
export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => {
|
package/lib/index.node.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// oxlint-disable no-underscore-dangle
|
|
2
|
+
|
|
1
3
|
import { Buffer as NodeBuffer } from 'node:buffer';
|
|
2
4
|
import {
|
|
3
5
|
hash as _hash,
|
|
@@ -5,17 +7,17 @@ import {
|
|
|
5
7
|
timingSafeEqual as _timingSafeEqual,
|
|
6
8
|
} from 'node:crypto';
|
|
7
9
|
|
|
8
|
-
const _alloc = /*#__PURE__*/ NodeBuffer.alloc;
|
|
9
|
-
const _allocUnsafe = /*#__PURE__*/ NodeBuffer.allocUnsafe;
|
|
10
|
-
const _concat = /*#__PURE__*/ NodeBuffer.concat;
|
|
11
|
-
const _from = /*#__PURE__*/ NodeBuffer.from;
|
|
10
|
+
const _alloc = /*#__PURE__*/ (() => NodeBuffer.alloc)();
|
|
11
|
+
const _allocUnsafe = /*#__PURE__*/ (() => NodeBuffer.allocUnsafe)();
|
|
12
|
+
const _concat = /*#__PURE__*/ (() => NodeBuffer.concat)();
|
|
13
|
+
const _from = /*#__PURE__*/ (() => NodeBuffer.from)();
|
|
12
14
|
|
|
13
|
-
const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength;
|
|
15
|
+
const _byteLength = /*#__PURE__*/ (() => NodeBuffer.byteLength)();
|
|
14
16
|
|
|
15
|
-
const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
|
|
16
|
-
const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
|
|
17
|
-
const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
|
|
18
|
-
const _utf8Write = /*#__PURE__*/ NodeBuffer.prototype.utf8Write;
|
|
17
|
+
const _compare = /*#__PURE__*/ (() => NodeBuffer.prototype.compare)();
|
|
18
|
+
const _equals = /*#__PURE__*/ (() => NodeBuffer.prototype.equals)();
|
|
19
|
+
const _utf8Slice = /*#__PURE__*/ (() => NodeBuffer.prototype.utf8Slice)();
|
|
20
|
+
const _utf8Write = /*#__PURE__*/ (() => NodeBuffer.prototype.utf8Write)();
|
|
19
21
|
|
|
20
22
|
const toUint8Array = (buffer: NodeBuffer) => {
|
|
21
23
|
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
@@ -60,67 +62,119 @@ const _fromCharCode = String.fromCharCode;
|
|
|
60
62
|
const _shortString = (from: Uint8Array, p: number, length: number): string | null => {
|
|
61
63
|
if (length < 4) {
|
|
62
64
|
if (length < 2) {
|
|
63
|
-
if (length === 0)
|
|
65
|
+
if (length === 0) {
|
|
66
|
+
return '';
|
|
67
|
+
}
|
|
64
68
|
const a = from[p];
|
|
65
|
-
if (a & 0x80)
|
|
69
|
+
if (a & 0x80) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
66
72
|
return _fromCharCode(a);
|
|
67
73
|
}
|
|
68
74
|
const a = from[p];
|
|
69
75
|
const b = from[p + 1];
|
|
70
|
-
if ((a | b) & 0x80)
|
|
71
|
-
|
|
76
|
+
if ((a | b) & 0x80) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
if (length === 2) {
|
|
80
|
+
return _fromCharCode(a, b);
|
|
81
|
+
}
|
|
72
82
|
const c = from[p + 2];
|
|
73
|
-
if (c & 0x80)
|
|
83
|
+
if (c & 0x80) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
74
86
|
return _fromCharCode(a, b, c);
|
|
75
87
|
}
|
|
76
88
|
const a = from[p];
|
|
77
89
|
const b = from[p + 1];
|
|
78
90
|
const c = from[p + 2];
|
|
79
91
|
const d = from[p + 3];
|
|
80
|
-
if ((a | b | c | d) & 0x80)
|
|
92
|
+
if ((a | b | c | d) & 0x80) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
81
95
|
if (length < 8) {
|
|
82
|
-
if (length === 4)
|
|
96
|
+
if (length === 4) {
|
|
97
|
+
return _fromCharCode(a, b, c, d);
|
|
98
|
+
}
|
|
83
99
|
const e = from[p + 4];
|
|
84
|
-
if (e & 0x80)
|
|
85
|
-
|
|
100
|
+
if (e & 0x80) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
if (length === 5) {
|
|
104
|
+
return _fromCharCode(a, b, c, d, e);
|
|
105
|
+
}
|
|
86
106
|
const f = from[p + 5];
|
|
87
|
-
if (f & 0x80)
|
|
88
|
-
|
|
107
|
+
if (f & 0x80) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
if (length === 6) {
|
|
111
|
+
return _fromCharCode(a, b, c, d, e, f);
|
|
112
|
+
}
|
|
89
113
|
const g = from[p + 6];
|
|
90
|
-
if (g & 0x80)
|
|
114
|
+
if (g & 0x80) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
91
117
|
return _fromCharCode(a, b, c, d, e, f, g);
|
|
92
118
|
}
|
|
93
119
|
const e = from[p + 4];
|
|
94
120
|
const f = from[p + 5];
|
|
95
121
|
const g = from[p + 6];
|
|
96
122
|
const h = from[p + 7];
|
|
97
|
-
if ((e | f | g | h) & 0x80)
|
|
123
|
+
if ((e | f | g | h) & 0x80) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
98
126
|
if (length < 12) {
|
|
99
|
-
if (length === 8)
|
|
127
|
+
if (length === 8) {
|
|
128
|
+
return _fromCharCode(a, b, c, d, e, f, g, h);
|
|
129
|
+
}
|
|
100
130
|
const i = from[p + 8];
|
|
101
|
-
if (i & 0x80)
|
|
102
|
-
|
|
131
|
+
if (i & 0x80) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
if (length === 9) {
|
|
135
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i);
|
|
136
|
+
}
|
|
103
137
|
const j = from[p + 9];
|
|
104
|
-
if (j & 0x80)
|
|
105
|
-
|
|
138
|
+
if (j & 0x80) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
if (length === 10) {
|
|
142
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
|
|
143
|
+
}
|
|
106
144
|
const k = from[p + 10];
|
|
107
|
-
if (k & 0x80)
|
|
145
|
+
if (k & 0x80) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
108
148
|
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
|
|
109
149
|
}
|
|
110
150
|
const i = from[p + 8];
|
|
111
151
|
const j = from[p + 9];
|
|
112
152
|
const k = from[p + 10];
|
|
113
153
|
const l = from[p + 11];
|
|
114
|
-
if ((i | j | k | l) & 0x80)
|
|
115
|
-
|
|
154
|
+
if ((i | j | k | l) & 0x80) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
if (length === 12) {
|
|
158
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
|
|
159
|
+
}
|
|
116
160
|
const m = from[p + 12];
|
|
117
|
-
if (m & 0x80)
|
|
118
|
-
|
|
161
|
+
if (m & 0x80) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
if (length === 13) {
|
|
165
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
|
|
166
|
+
}
|
|
119
167
|
const n = from[p + 13];
|
|
120
|
-
if (n & 0x80)
|
|
121
|
-
|
|
168
|
+
if (n & 0x80) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
if (length === 14) {
|
|
172
|
+
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
|
|
173
|
+
}
|
|
122
174
|
const o = from[p + 14];
|
|
123
|
-
if (o & 0x80)
|
|
175
|
+
if (o & 0x80) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
124
178
|
return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
|
|
125
179
|
};
|
|
126
180
|
|
|
@@ -138,7 +192,9 @@ export const decodeUtf8From = (
|
|
|
138
192
|
): string => {
|
|
139
193
|
if (length <= 15) {
|
|
140
194
|
const result = _shortString(from, offset, length);
|
|
141
|
-
if (result !== null)
|
|
195
|
+
if (result !== null) {
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
142
198
|
}
|
|
143
199
|
return _utf8Slice.call(from, offset, offset + length);
|
|
144
200
|
};
|
|
@@ -160,8 +216,20 @@ export const getUtf8Length = (str: string): number => {
|
|
|
160
216
|
* @returns true if byte length is within [min, max]
|
|
161
217
|
*/
|
|
162
218
|
export const isUtf8LengthInRange = (str: string, min: number, max: number): boolean => {
|
|
163
|
-
const len =
|
|
164
|
-
|
|
219
|
+
const len = str.length;
|
|
220
|
+
|
|
221
|
+
// fast path: if max possible UTF-8 length is below min, fail
|
|
222
|
+
if (len * 3 < min) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// fast path: if UTF-16 length satisfies min and max possible satisfies max
|
|
227
|
+
if (len >= min && len * 3 <= max) {
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const utf8len = _byteLength(str, 'utf8');
|
|
232
|
+
return utf8len >= min && utf8len <= max;
|
|
165
233
|
};
|
|
166
234
|
|
|
167
235
|
export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => {
|