@smithy/core 3.25.1 → 3.27.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/dist-cjs/submodules/cbor/index.js +518 -371
- package/dist-cjs/submodules/checksum/index.browser.js +355 -145
- package/dist-cjs/submodules/checksum/index.js +456 -142
- package/dist-cjs/submodules/checksum/index.native.js +355 -145
- package/dist-cjs/submodules/client/index.js +1 -1
- package/dist-cjs/submodules/protocols/index.js +5 -2
- package/dist-cjs/submodules/serde/index.browser.js +30 -36
- package/dist-cjs/submodules/serde/index.js +54 -110
- package/dist-cjs/submodules/serde/index.native.js +30 -36
- package/dist-es/submodules/cbor/cbor-decode.js +326 -269
- package/dist-es/submodules/cbor/cbor-encode.js +192 -103
- package/dist-es/submodules/cbor/cbor.js +4 -2
- package/dist-es/submodules/checksum/crc32/Crc32Js.js +27 -0
- package/dist-es/submodules/checksum/crc32/Crc32Node.js +21 -0
- package/dist-es/submodules/checksum/index.browser.js +7 -1
- package/dist-es/submodules/checksum/index.js +7 -1
- package/dist-es/submodules/checksum/index.native.js +7 -1
- package/dist-es/submodules/checksum/md5/Md5Js.js +92 -0
- package/dist-es/submodules/checksum/md5/Md5Node.js +29 -0
- package/dist-es/submodules/checksum/sha256/Sha256Js.js +179 -0
- package/dist-es/submodules/checksum/sha256/Sha256Node.js +59 -0
- package/dist-es/submodules/checksum/sha256/Sha256WebCrypto.js +71 -0
- package/dist-es/submodules/client/smithy-client/get-value-from-text-node.js +1 -1
- package/dist-es/submodules/protocols/HttpProtocol.js +4 -1
- package/dist-es/submodules/serde/concatBytes.js +15 -0
- package/dist-es/submodules/serde/index.browser.js +2 -0
- package/dist-es/submodules/serde/index.js +2 -0
- package/dist-es/submodules/serde/index.native.js +2 -0
- package/dist-es/submodules/serde/util-stream/checksum/ChecksumStream.js +4 -0
- package/dist-es/submodules/serde/util-stream/headStream.js +2 -1
- package/dist-es/submodules/serde/util-stream/stream-collector.browser.js +12 -38
- package/dist-es/submodules/serde/util-stream/stream-collector.js +18 -33
- package/dist-es/submodules/serde/util-utf8/toUint8Array.browser.js +3 -0
- package/dist-es/submodules/serde/util-utf8/toUint8Array.js +3 -0
- package/dist-types/submodules/cbor/cbor-decode.d.ts +4 -0
- package/dist-types/submodules/cbor/cbor-encode.d.ts +8 -4
- package/dist-types/submodules/checksum/crc32/Crc32Js.d.ts +13 -0
- package/dist-types/submodules/checksum/crc32/Crc32Node.d.ts +13 -0
- package/dist-types/submodules/checksum/index.browser.d.ts +7 -1
- package/dist-types/submodules/checksum/index.d.ts +7 -1
- package/dist-types/submodules/checksum/index.native.d.ts +7 -1
- package/dist-types/submodules/checksum/md5/Md5Js.d.ts +19 -0
- package/dist-types/submodules/checksum/md5/Md5Node.d.ts +17 -0
- package/dist-types/submodules/checksum/sha256/Sha256Js.d.ts +28 -0
- package/dist-types/submodules/checksum/sha256/Sha256Node.d.ts +13 -0
- package/dist-types/submodules/checksum/sha256/Sha256WebCrypto.d.ts +22 -0
- package/dist-types/submodules/serde/concatBytes.d.ts +6 -0
- package/dist-types/submodules/serde/index.browser.d.ts +2 -0
- package/dist-types/submodules/serde/index.d.ts +2 -0
- package/dist-types/submodules/serde/index.native.d.ts +2 -0
- package/dist-types/submodules/serde/util-stream/checksum/ChecksumStream.d.ts +9 -0
- package/dist-types/submodules/serde/util-stream/stream-collector.browser.d.ts +8 -2
- package/dist-types/submodules/serde/util-stream/stream-collector.d.ts +4 -1
- package/package.json +4 -3
- package/dist-es/submodules/checksum/md5-js/constants.js +0 -3
- package/dist-es/submodules/checksum/md5-js/md5.js +0 -168
- package/dist-types/submodules/checksum/md5-js/constants.d.ts +0 -12
- package/dist-types/submodules/checksum/md5-js/md5.d.ts +0 -16
|
@@ -1,83 +1,64 @@
|
|
|
1
|
-
import { NumericValue
|
|
1
|
+
import { NumericValue } from "@smithy/core/serde";
|
|
2
2
|
import { alloc, extendedFloat16, extendedFloat32, extendedFloat64, majorList, majorMap, majorNegativeInt64, majorSpecial, majorTag, majorUint64, majorUnstructuredByteString, majorUtf8String, specialFalse, specialNull, specialTrue, tagSymbol, } from "./cbor-types";
|
|
3
3
|
const USE_BUFFER = typeof Buffer !== "undefined";
|
|
4
|
+
const encodeStringCache = new Map();
|
|
5
|
+
let encodeCacheEpoch = 0;
|
|
6
|
+
let encodeCacheSaturated = false;
|
|
4
7
|
const initialSize = 2048;
|
|
5
8
|
let data = alloc(initialSize);
|
|
6
9
|
let dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
7
10
|
let cursor = 0;
|
|
8
|
-
function ensureSpace(bytes) {
|
|
9
|
-
const remaining = data.byteLength - cursor;
|
|
10
|
-
if (remaining < bytes) {
|
|
11
|
-
if (cursor < 16_000_000) {
|
|
12
|
-
resize(Math.max(data.byteLength * 4, data.byteLength + bytes));
|
|
13
|
-
}
|
|
14
|
-
else {
|
|
15
|
-
resize(data.byteLength + bytes + 16_000_000);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
export function toUint8Array() {
|
|
20
|
-
const out = alloc(cursor);
|
|
21
|
-
out.set(data.subarray(0, cursor), 0);
|
|
22
|
-
cursor = 0;
|
|
23
|
-
return out;
|
|
24
|
-
}
|
|
25
|
-
export function resize(size) {
|
|
26
|
-
const old = data;
|
|
27
|
-
data = alloc(size);
|
|
28
|
-
if (old) {
|
|
29
|
-
if (old.copy) {
|
|
30
|
-
old.copy(data, 0, 0, old.byteLength);
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
data.set(old, 0);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
37
|
-
}
|
|
38
|
-
function encodeHeader(major, value) {
|
|
39
|
-
if (value < 24) {
|
|
40
|
-
data[cursor++] = (major << 5) | value;
|
|
41
|
-
}
|
|
42
|
-
else if (value < 1 << 8) {
|
|
43
|
-
data[cursor++] = (major << 5) | 24;
|
|
44
|
-
data[cursor++] = value;
|
|
45
|
-
}
|
|
46
|
-
else if (value < 1 << 16) {
|
|
47
|
-
data[cursor++] = (major << 5) | extendedFloat16;
|
|
48
|
-
dataView.setUint16(cursor, value);
|
|
49
|
-
cursor += 2;
|
|
50
|
-
}
|
|
51
|
-
else if (value < 2 ** 32) {
|
|
52
|
-
data[cursor++] = (major << 5) | extendedFloat32;
|
|
53
|
-
dataView.setUint32(cursor, value);
|
|
54
|
-
cursor += 4;
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
data[cursor++] = (major << 5) | extendedFloat64;
|
|
58
|
-
dataView.setBigUint64(cursor, typeof value === "bigint" ? value : BigInt(value));
|
|
59
|
-
cursor += 8;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
11
|
export function encode(_input) {
|
|
63
12
|
const encodeStack = [_input];
|
|
64
13
|
while (encodeStack.length) {
|
|
65
14
|
const input = encodeStack.pop();
|
|
66
|
-
ensureSpace(typeof input === "string" ? input.length * 4 : 64);
|
|
67
15
|
if (typeof input === "string") {
|
|
16
|
+
const len = input.length;
|
|
68
17
|
if (USE_BUFFER) {
|
|
69
|
-
|
|
70
|
-
|
|
18
|
+
ensureSpace(len * 3 + 9);
|
|
19
|
+
if (len > 23) {
|
|
20
|
+
encodeHeader(majorUtf8String, Buffer.byteLength(input));
|
|
21
|
+
cursor += data.write(input, cursor);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
encodeStringCached(input);
|
|
25
|
+
}
|
|
71
26
|
}
|
|
72
27
|
else {
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
cursor
|
|
28
|
+
const maxBytes = len * 3;
|
|
29
|
+
ensureSpace(maxBytes + 9);
|
|
30
|
+
const headerPos = cursor;
|
|
31
|
+
const result = new TextEncoder().encodeInto(input, data.subarray(cursor + 9));
|
|
32
|
+
const byteLen = result.written;
|
|
33
|
+
let headerSize;
|
|
34
|
+
if (byteLen < 24) {
|
|
35
|
+
headerSize = 1;
|
|
36
|
+
}
|
|
37
|
+
else if (byteLen < 256) {
|
|
38
|
+
headerSize = 2;
|
|
39
|
+
}
|
|
40
|
+
else if (byteLen < 65536) {
|
|
41
|
+
headerSize = 3;
|
|
42
|
+
}
|
|
43
|
+
else if (byteLen < 4294967296) {
|
|
44
|
+
headerSize = 5;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
headerSize = 9;
|
|
48
|
+
}
|
|
49
|
+
if (headerSize < 9) {
|
|
50
|
+
data.copyWithin(headerPos + headerSize, headerPos + 9, headerPos + 9 + byteLen);
|
|
51
|
+
}
|
|
52
|
+
cursor = headerPos;
|
|
53
|
+
encodeInteger(majorUtf8String, byteLen);
|
|
54
|
+
cursor += byteLen;
|
|
77
55
|
}
|
|
78
56
|
continue;
|
|
79
57
|
}
|
|
80
|
-
|
|
58
|
+
if (data.byteLength - cursor < 9) {
|
|
59
|
+
ensureSpace(64);
|
|
60
|
+
}
|
|
61
|
+
if (typeof input === "number") {
|
|
81
62
|
if (Number.isInteger(input)) {
|
|
82
63
|
const nonNegative = input >= 0;
|
|
83
64
|
const major = nonNegative ? majorUint64 : majorNegativeInt64;
|
|
@@ -92,7 +73,7 @@ export function encode(_input) {
|
|
|
92
73
|
else if (value < 65536) {
|
|
93
74
|
data[cursor++] = (major << 5) | extendedFloat16;
|
|
94
75
|
data[cursor++] = value >> 8;
|
|
95
|
-
data[cursor++] = value;
|
|
76
|
+
data[cursor++] = value & 0xff;
|
|
96
77
|
}
|
|
97
78
|
else if (value < 4294967296) {
|
|
98
79
|
data[cursor++] = (major << 5) | extendedFloat32;
|
|
@@ -101,7 +82,10 @@ export function encode(_input) {
|
|
|
101
82
|
}
|
|
102
83
|
else {
|
|
103
84
|
data[cursor++] = (major << 5) | extendedFloat64;
|
|
104
|
-
|
|
85
|
+
const hi = (value / 4294967296) | 0;
|
|
86
|
+
const lo = (value - hi * 4294967296) | 0;
|
|
87
|
+
dataView.setUint32(cursor, hi);
|
|
88
|
+
dataView.setUint32(cursor + 4, lo);
|
|
105
89
|
cursor += 8;
|
|
106
90
|
}
|
|
107
91
|
continue;
|
|
@@ -115,28 +99,16 @@ export function encode(_input) {
|
|
|
115
99
|
const nonNegative = input >= 0;
|
|
116
100
|
const major = nonNegative ? majorUint64 : majorNegativeInt64;
|
|
117
101
|
const value = nonNegative ? input : -input - BigInt(1);
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
data[cursor++] = n >> 8;
|
|
129
|
-
data[cursor++] = n & 0b1111_1111;
|
|
130
|
-
}
|
|
131
|
-
else if (n < 4294967296) {
|
|
132
|
-
data[cursor++] = (major << 5) | extendedFloat32;
|
|
133
|
-
dataView.setUint32(cursor, n);
|
|
134
|
-
cursor += 4;
|
|
135
|
-
}
|
|
136
|
-
else if (value < BigInt("18446744073709551616")) {
|
|
137
|
-
data[cursor++] = (major << 5) | extendedFloat64;
|
|
138
|
-
dataView.setBigUint64(cursor, value);
|
|
139
|
-
cursor += 8;
|
|
102
|
+
if (value < BigInt("18446744073709551616")) {
|
|
103
|
+
const n = Number(value);
|
|
104
|
+
if (n < 4294967296) {
|
|
105
|
+
encodeInteger(major, n);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
data[cursor++] = (major << 5) | extendedFloat64;
|
|
109
|
+
dataView.setBigUint64(cursor, value);
|
|
110
|
+
cursor += 8;
|
|
111
|
+
}
|
|
140
112
|
}
|
|
141
113
|
else {
|
|
142
114
|
const binaryBigInt = value.toString(2);
|
|
@@ -147,14 +119,9 @@ export function encode(_input) {
|
|
|
147
119
|
bigIntBytes[bigIntBytes.byteLength - i] = Number(b & BigInt(255));
|
|
148
120
|
b >>= BigInt(8);
|
|
149
121
|
}
|
|
150
|
-
ensureSpace(bigIntBytes.byteLength * 2);
|
|
122
|
+
ensureSpace(bigIntBytes.byteLength * 2 + 16);
|
|
151
123
|
data[cursor++] = nonNegative ? 0b110_00010 : 0b110_00011;
|
|
152
|
-
|
|
153
|
-
encodeHeader(majorUnstructuredByteString, Buffer.byteLength(bigIntBytes));
|
|
154
|
-
}
|
|
155
|
-
else {
|
|
156
|
-
encodeHeader(majorUnstructuredByteString, bigIntBytes.byteLength);
|
|
157
|
-
}
|
|
124
|
+
encodeHeader(majorUnstructuredByteString, bigIntBytes.byteLength);
|
|
158
125
|
data.set(bigIntBytes, cursor);
|
|
159
126
|
cursor += bigIntBytes.byteLength;
|
|
160
127
|
}
|
|
@@ -172,15 +139,16 @@ export function encode(_input) {
|
|
|
172
139
|
throw new Error("@smithy/core/cbor: client may not serialize undefined value.");
|
|
173
140
|
}
|
|
174
141
|
else if (Array.isArray(input)) {
|
|
142
|
+
encodeInteger(majorList, input.length);
|
|
143
|
+
ensureSpace(input.length * 9 + 64);
|
|
175
144
|
for (let i = input.length - 1; i >= 0; --i) {
|
|
176
145
|
encodeStack.push(input[i]);
|
|
177
146
|
}
|
|
178
|
-
encodeHeader(majorList, input.length);
|
|
179
147
|
continue;
|
|
180
148
|
}
|
|
181
149
|
else if (typeof input.byteLength === "number") {
|
|
182
|
-
ensureSpace(input.length * 2);
|
|
183
|
-
|
|
150
|
+
ensureSpace(input.length * 2 + 9);
|
|
151
|
+
encodeInteger(majorUnstructuredByteString, input.length);
|
|
184
152
|
data.set(input, cursor);
|
|
185
153
|
cursor += input.byteLength;
|
|
186
154
|
continue;
|
|
@@ -191,9 +159,9 @@ export function encode(_input) {
|
|
|
191
159
|
const exponent = decimalIndex === -1 ? 0 : decimalIndex - input.string.length + 1;
|
|
192
160
|
const mantissa = BigInt(input.string.replace(".", ""));
|
|
193
161
|
data[cursor++] = 0b110_00100;
|
|
162
|
+
encodeInteger(majorList, 2);
|
|
194
163
|
encodeStack.push(mantissa);
|
|
195
164
|
encodeStack.push(exponent);
|
|
196
|
-
encodeHeader(majorList, 2);
|
|
197
165
|
continue;
|
|
198
166
|
}
|
|
199
167
|
if (input[tagSymbol]) {
|
|
@@ -207,14 +175,135 @@ export function encode(_input) {
|
|
|
207
175
|
}
|
|
208
176
|
}
|
|
209
177
|
const keys = Object.keys(input);
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
encodeStack.push(
|
|
178
|
+
const len = keys.length;
|
|
179
|
+
encodeInteger(majorMap, len);
|
|
180
|
+
for (let i = len - 1; i >= 0; --i) {
|
|
181
|
+
encodeStack.push(input[keys[i]]);
|
|
182
|
+
encodeStack.push(keys[i]);
|
|
214
183
|
}
|
|
215
|
-
encodeHeader(majorMap, keys.length);
|
|
216
184
|
continue;
|
|
217
185
|
}
|
|
218
186
|
throw new Error(`data type ${input?.constructor?.name ?? typeof input} not compatible for encoding.`);
|
|
219
187
|
}
|
|
220
188
|
}
|
|
189
|
+
export function advanceEncodingEpoch() {
|
|
190
|
+
encodeCacheEpoch = (encodeCacheEpoch + 1) & 0b1111_1111_1111_1111;
|
|
191
|
+
encodeCacheSaturated = false;
|
|
192
|
+
}
|
|
193
|
+
export function toUint8Array() {
|
|
194
|
+
const out = alloc(cursor);
|
|
195
|
+
out.set(data.subarray(0, cursor), 0);
|
|
196
|
+
cursor = 0;
|
|
197
|
+
return out;
|
|
198
|
+
}
|
|
199
|
+
export function resize(size) {
|
|
200
|
+
const old = data;
|
|
201
|
+
data = alloc(size);
|
|
202
|
+
if (old) {
|
|
203
|
+
if (old.copy) {
|
|
204
|
+
old.copy(data, 0, 0, old.byteLength);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
data.set(old, 0);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
211
|
+
}
|
|
212
|
+
function encodeStringCached(input) {
|
|
213
|
+
const cached = encodeStringCache.get(input);
|
|
214
|
+
if (cached !== undefined) {
|
|
215
|
+
data.set(cached.bytes, cursor);
|
|
216
|
+
cursor += cached.bytes.length;
|
|
217
|
+
cached.epoch = encodeCacheEpoch;
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const start = cursor;
|
|
221
|
+
const byteLen = Buffer.byteLength(input);
|
|
222
|
+
encodeInteger(majorUtf8String, byteLen);
|
|
223
|
+
cursor += data.write(input, cursor);
|
|
224
|
+
const bytes = Uint8Array.prototype.slice.call(data, start, cursor);
|
|
225
|
+
if (encodeStringCache.size >= 2048) {
|
|
226
|
+
if (encodeCacheSaturated) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
let evicted = 0;
|
|
230
|
+
for (const [key, entry] of encodeStringCache) {
|
|
231
|
+
if (evicted >= 1024) {
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
if (entry.epoch !== encodeCacheEpoch) {
|
|
235
|
+
encodeStringCache.delete(key);
|
|
236
|
+
evicted++;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (evicted === 0) {
|
|
240
|
+
encodeCacheSaturated = true;
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (encodeStringCache.size < 2048) {
|
|
245
|
+
encodeStringCache.set(input, { epoch: encodeCacheEpoch, bytes });
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function ensureSpace(bytes) {
|
|
249
|
+
const remaining = data.byteLength - cursor;
|
|
250
|
+
if (remaining < bytes) {
|
|
251
|
+
if (cursor < 16_000_000) {
|
|
252
|
+
resize(Math.max(data.byteLength * 4, data.byteLength + bytes));
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
resize(data.byteLength + bytes + 16_000_000);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function encodeHeader(major, value) {
|
|
260
|
+
if (value < 24) {
|
|
261
|
+
data[cursor++] = (major << 5) | value;
|
|
262
|
+
}
|
|
263
|
+
else if (value < 256) {
|
|
264
|
+
data[cursor++] = (major << 5) | 24;
|
|
265
|
+
data[cursor++] = value;
|
|
266
|
+
}
|
|
267
|
+
else if (value < 65536) {
|
|
268
|
+
data[cursor++] = (major << 5) | extendedFloat16;
|
|
269
|
+
dataView.setUint16(cursor, value);
|
|
270
|
+
cursor += 2;
|
|
271
|
+
}
|
|
272
|
+
else if (value < 4294967296) {
|
|
273
|
+
data[cursor++] = (major << 5) | extendedFloat32;
|
|
274
|
+
dataView.setUint32(cursor, value);
|
|
275
|
+
cursor += 4;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
data[cursor++] = (major << 5) | extendedFloat64;
|
|
279
|
+
dataView.setBigUint64(cursor, typeof value === "bigint" ? value : BigInt(value));
|
|
280
|
+
cursor += 8;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
function encodeInteger(major, value) {
|
|
284
|
+
if (value < 24) {
|
|
285
|
+
data[cursor++] = (major << 5) | value;
|
|
286
|
+
}
|
|
287
|
+
else if (value < 256) {
|
|
288
|
+
data[cursor++] = (major << 5) | 24;
|
|
289
|
+
data[cursor++] = value;
|
|
290
|
+
}
|
|
291
|
+
else if (value < 65536) {
|
|
292
|
+
data[cursor++] = (major << 5) | extendedFloat16;
|
|
293
|
+
data[cursor++] = value >> 8;
|
|
294
|
+
data[cursor++] = value & 0xff;
|
|
295
|
+
}
|
|
296
|
+
else if (value < 4294967296) {
|
|
297
|
+
data[cursor++] = (major << 5) | extendedFloat32;
|
|
298
|
+
dataView.setUint32(cursor, value);
|
|
299
|
+
cursor += 4;
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
data[cursor++] = (major << 5) | extendedFloat64;
|
|
303
|
+
const hi = (value / 4294967296) | 0;
|
|
304
|
+
const lo = (value - hi * 4294967296) | 0;
|
|
305
|
+
dataView.setUint32(cursor, hi);
|
|
306
|
+
dataView.setUint32(cursor + 4, lo);
|
|
307
|
+
cursor += 8;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { decode, setPayload } from "./cbor-decode";
|
|
2
|
-
import { encode, resize, toUint8Array } from "./cbor-encode";
|
|
1
|
+
import { advanceDecodingEpoch, decode, setPayload } from "./cbor-decode";
|
|
2
|
+
import { advanceEncodingEpoch, encode, resize, toUint8Array } from "./cbor-encode";
|
|
3
3
|
export const cbor = {
|
|
4
4
|
deserialize(payload) {
|
|
5
|
+
advanceDecodingEpoch();
|
|
5
6
|
setPayload(payload);
|
|
6
7
|
return decode(0, payload.length);
|
|
7
8
|
},
|
|
8
9
|
serialize(input) {
|
|
10
|
+
advanceEncodingEpoch();
|
|
9
11
|
try {
|
|
10
12
|
encode(input);
|
|
11
13
|
return toUint8Array();
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const CRC32_TABLE = new Uint32Array(256);
|
|
2
|
+
for (let i = 0; i < 256; ++i) {
|
|
3
|
+
let c = i;
|
|
4
|
+
for (let j = 0; j < 8; ++j) {
|
|
5
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
6
|
+
}
|
|
7
|
+
CRC32_TABLE[i] = c >>> 0;
|
|
8
|
+
}
|
|
9
|
+
const ONES = 0xffff_ffff;
|
|
10
|
+
export class Crc32Js {
|
|
11
|
+
digestLength = 4;
|
|
12
|
+
checksum = ONES;
|
|
13
|
+
update(data) {
|
|
14
|
+
for (let i = 0; i < data.length; ++i) {
|
|
15
|
+
this.checksum = (this.checksum >>> 8) ^ CRC32_TABLE[(this.checksum ^ data[i]) & 0xff];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async digest() {
|
|
19
|
+
const value = (this.checksum ^ ONES) >>> 0;
|
|
20
|
+
const out = new Uint8Array(4);
|
|
21
|
+
new DataView(out.buffer).setUint32(0, value, false);
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
reset() {
|
|
25
|
+
this.checksum = ONES;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as zlib from "node:zlib";
|
|
2
|
+
import { Crc32Js } from "./Crc32Js";
|
|
3
|
+
const zlibCrc32 = typeof zlib.crc32 === "function" ? zlib.crc32 : undefined;
|
|
4
|
+
export const Crc32Node = zlibCrc32 ? buildNativeClass(zlibCrc32) : Crc32Js;
|
|
5
|
+
function buildNativeClass(nativeCrc32) {
|
|
6
|
+
return class Crc32Node {
|
|
7
|
+
digestLength = 4;
|
|
8
|
+
value = 0;
|
|
9
|
+
update(data) {
|
|
10
|
+
this.value = nativeCrc32(data, this.value);
|
|
11
|
+
}
|
|
12
|
+
async digest() {
|
|
13
|
+
const out = new Uint8Array(4);
|
|
14
|
+
new DataView(out.buffer).setUint32(0, this.value >>> 0, false);
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
reset() {
|
|
18
|
+
this.value = 0;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -2,5 +2,11 @@ const no = Symbol.for("node-only");
|
|
|
2
2
|
export { blobHasher } from "./hash-blob-browser/blobHasher";
|
|
3
3
|
export const fileStreamHasher = no;
|
|
4
4
|
export const readableStreamHasher = no;
|
|
5
|
-
export { Md5 } from "./md5
|
|
5
|
+
export { Md5Js, Md5Js as Md5 } from "./md5/Md5Js";
|
|
6
|
+
export const Md5Node = no;
|
|
7
|
+
export { Crc32Js, Crc32Js as Crc32 } from "./crc32/Crc32Js";
|
|
8
|
+
export const Crc32Node = no;
|
|
9
|
+
export { Sha256Js } from "./sha256/Sha256Js";
|
|
10
|
+
export { Sha256WebCrypto, Sha256WebCrypto as Sha256 } from "./sha256/Sha256WebCrypto";
|
|
11
|
+
export const Sha256Node = no;
|
|
6
12
|
export { blobReader } from "./chunked-blob-reader/chunked-blob-reader";
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
export { blobHasher } from "./hash-blob-browser/blobHasher";
|
|
2
2
|
export { fileStreamHasher } from "./hash-stream-node/fileStreamHasher";
|
|
3
3
|
export { readableStreamHasher } from "./hash-stream-node/readableStreamHasher";
|
|
4
|
-
export {
|
|
4
|
+
export { Md5Js } from "./md5/Md5Js";
|
|
5
|
+
export { Md5Node, Md5Node as Md5 } from "./md5/Md5Node";
|
|
6
|
+
export { Crc32Js } from "./crc32/Crc32Js";
|
|
7
|
+
export { Crc32Node, Crc32Node as Crc32 } from "./crc32/Crc32Node";
|
|
8
|
+
export { Sha256Js } from "./sha256/Sha256Js";
|
|
9
|
+
export { Sha256Node, Sha256Node as Sha256 } from "./sha256/Sha256Node";
|
|
10
|
+
export { Sha256WebCrypto } from "./sha256/Sha256WebCrypto";
|
|
5
11
|
export { blobReader } from "./chunked-blob-reader/chunked-blob-reader";
|
|
@@ -2,5 +2,11 @@ const no = Symbol.for("node-only");
|
|
|
2
2
|
export { blobHasher } from "./hash-blob-browser/blobHasher";
|
|
3
3
|
export const fileStreamHasher = no;
|
|
4
4
|
export const readableStreamHasher = no;
|
|
5
|
-
export { Md5 } from "./md5
|
|
5
|
+
export { Md5Js, Md5Js as Md5 } from "./md5/Md5Js";
|
|
6
|
+
export const Md5Node = no;
|
|
7
|
+
export { Crc32Js, Crc32Js as Crc32 } from "./crc32/Crc32Js";
|
|
8
|
+
export const Crc32Node = no;
|
|
9
|
+
export { Sha256Js } from "./sha256/Sha256Js";
|
|
10
|
+
export { Sha256WebCrypto, Sha256WebCrypto as Sha256 } from "./sha256/Sha256WebCrypto";
|
|
11
|
+
export const Sha256Node = no;
|
|
6
12
|
export { blobReader } from "./chunked-blob-reader/chunked-blob-reader.native";
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { toUint8Array } from "@smithy/core/serde";
|
|
2
|
+
export class Md5Js {
|
|
3
|
+
digestLength = 16;
|
|
4
|
+
state = Uint32Array.from(INIT);
|
|
5
|
+
writeBuffer = new DataView(new ArrayBuffer(64));
|
|
6
|
+
bufferLength = 0;
|
|
7
|
+
bytesHashed = 0;
|
|
8
|
+
update(sourceData) {
|
|
9
|
+
const data = toUint8Array(sourceData);
|
|
10
|
+
let pos = 0;
|
|
11
|
+
let len = data.byteLength;
|
|
12
|
+
this.bytesHashed += len;
|
|
13
|
+
while (len > 0) {
|
|
14
|
+
this.writeBuffer.setUint8(this.bufferLength++, data[pos++]);
|
|
15
|
+
--len;
|
|
16
|
+
if (this.bufferLength === 64) {
|
|
17
|
+
compress(this.state, this.writeBuffer);
|
|
18
|
+
this.bufferLength = 0;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async digest() {
|
|
23
|
+
const state = Uint32Array.from(this.state);
|
|
24
|
+
const buf = new DataView(this.writeBuffer.buffer.slice(0));
|
|
25
|
+
let bufLen = this.bufferLength;
|
|
26
|
+
const bits = this.bytesHashed * 8;
|
|
27
|
+
buf.setUint8(bufLen++, 0x80);
|
|
28
|
+
if (this.bufferLength % 64 >= 56) {
|
|
29
|
+
for (let i = bufLen; i < 64; ++i) {
|
|
30
|
+
buf.setUint8(i, 0);
|
|
31
|
+
}
|
|
32
|
+
compress(state, buf);
|
|
33
|
+
bufLen = 0;
|
|
34
|
+
}
|
|
35
|
+
for (let i = bufLen; i < 56; ++i) {
|
|
36
|
+
buf.setUint8(i, 0);
|
|
37
|
+
}
|
|
38
|
+
buf.setUint32(56, bits >>> 0, true);
|
|
39
|
+
buf.setUint32(60, Math.floor(bits / 2 ** 32), true);
|
|
40
|
+
compress(state, buf);
|
|
41
|
+
const out = new Uint8Array(16);
|
|
42
|
+
const view = new DataView(out.buffer);
|
|
43
|
+
for (let i = 0; i < 4; ++i) {
|
|
44
|
+
view.setUint32(i * 4, state[i], true);
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
reset() {
|
|
49
|
+
this.state.set(INIT);
|
|
50
|
+
this.writeBuffer = new DataView(new ArrayBuffer(64));
|
|
51
|
+
this.bufferLength = 0;
|
|
52
|
+
this.bytesHashed = 0;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const INIT = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
|
|
56
|
+
const M = 0xffffffff;
|
|
57
|
+
const S = Uint8Array.of(7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21);
|
|
58
|
+
const T = Array.from({ length: 64 }, (_, i) => (Math.abs(Math.sin(i + 1)) * 2 ** 32) >>> 0);
|
|
59
|
+
function compress(state, block) {
|
|
60
|
+
let a = state[0], b = state[1], c = state[2], d = state[3];
|
|
61
|
+
for (let i = 0; i < 64; ++i) {
|
|
62
|
+
let f, g;
|
|
63
|
+
if (i < 16) {
|
|
64
|
+
f = (b & c) | (~b & d);
|
|
65
|
+
g = i;
|
|
66
|
+
}
|
|
67
|
+
else if (i < 32) {
|
|
68
|
+
f = (d & b) | (c & ~d);
|
|
69
|
+
g = (5 * i + 1) % 16;
|
|
70
|
+
}
|
|
71
|
+
else if (i < 48) {
|
|
72
|
+
f = b ^ c ^ d;
|
|
73
|
+
g = (3 * i + 5) % 16;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
f = c ^ (b | ~d);
|
|
77
|
+
g = (7 * i) % 16;
|
|
78
|
+
}
|
|
79
|
+
const x = block.getUint32(g * 4, true);
|
|
80
|
+
const tmp = d;
|
|
81
|
+
d = c;
|
|
82
|
+
c = b;
|
|
83
|
+
const s = S[(i >> 4) * 4 + (i & 3)];
|
|
84
|
+
const sum = (((a + f) & M) + ((x + T[i]) & M)) & M;
|
|
85
|
+
b = (b + (((sum << s) | (sum >>> (32 - s))) >>> 0)) & M;
|
|
86
|
+
a = tmp;
|
|
87
|
+
}
|
|
88
|
+
state[0] = (state[0] + a) & M;
|
|
89
|
+
state[1] = (state[1] + b) & M;
|
|
90
|
+
state[2] = (state[2] + c) & M;
|
|
91
|
+
state[3] = (state[3] + d) & M;
|
|
92
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { toUint8Array } from "@smithy/core/serde";
|
|
3
|
+
import { Md5Js } from "./Md5Js";
|
|
4
|
+
const hasNativeCrypto = (() => {
|
|
5
|
+
try {
|
|
6
|
+
createHash("md5");
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
})();
|
|
13
|
+
export const Md5Node = hasNativeCrypto ? buildNativeClass() : Md5Js;
|
|
14
|
+
function buildNativeClass() {
|
|
15
|
+
return class Md5Node {
|
|
16
|
+
digestLength = 16;
|
|
17
|
+
hash = createHash("md5");
|
|
18
|
+
update(data) {
|
|
19
|
+
this.hash.update(toUint8Array(data));
|
|
20
|
+
}
|
|
21
|
+
async digest() {
|
|
22
|
+
const buf = this.hash.copy().digest();
|
|
23
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
24
|
+
}
|
|
25
|
+
reset() {
|
|
26
|
+
this.hash = createHash("md5");
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|