@ocap/message 1.30.3 → 1.30.5
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/esm/canonical-cbor.d.mts +6 -0
- package/esm/canonical-cbor.mjs +401 -0
- package/esm/cbor.d.mts +8 -0
- package/esm/cbor.mjs +13 -0
- package/esm/{package.mjs → core/message/package.mjs} +12 -1
- package/esm/index.mjs +2 -6
- package/esm/message.mjs +30 -5
- package/esm/node_modules/cborg/cborg.mjs +6 -0
- package/esm/node_modules/cborg/lib/0uint.mjs +204 -0
- package/esm/node_modules/cborg/lib/1negint.mjs +91 -0
- package/esm/node_modules/cborg/lib/2bytes.mjs +120 -0
- package/esm/node_modules/cborg/lib/3string.mjs +103 -0
- package/esm/node_modules/cborg/lib/4array.mjs +100 -0
- package/esm/node_modules/cborg/lib/5map.mjs +100 -0
- package/esm/node_modules/cborg/lib/6tag.mjs +76 -0
- package/esm/node_modules/cborg/lib/7float.mjs +224 -0
- package/esm/node_modules/cborg/lib/bl.mjs +131 -0
- package/esm/node_modules/cborg/lib/byte-utils.mjs +104 -0
- package/esm/node_modules/cborg/lib/common.mjs +20 -0
- package/esm/node_modules/cborg/lib/decode.mjs +210 -0
- package/esm/node_modules/cborg/lib/encode.mjs +388 -0
- package/esm/node_modules/cborg/lib/is.mjs +59 -0
- package/esm/node_modules/cborg/lib/jump.mjs +154 -0
- package/esm/node_modules/cborg/lib/tagged.mjs +70 -0
- package/esm/node_modules/cborg/lib/token.mjs +73 -0
- package/esm/patch.mjs +1 -1
- package/esm/protobuf.d.mts +6 -0
- package/esm/protobuf.mjs +17 -0
- package/esm/provider-init-schema.d.mts +1 -0
- package/esm/provider-init-schema.mjs +22 -0
- package/esm/provider-init.d.mts +1 -0
- package/esm/provider-init.mjs +18 -0
- package/lib/canonical-cbor.cjs +404 -0
- package/lib/canonical-cbor.d.cts +6 -0
- package/lib/cbor.cjs +17 -0
- package/lib/cbor.d.cts +8 -0
- package/lib/{package.cjs → core/message/package.cjs} +12 -1
- package/lib/index.cjs +2 -8
- package/lib/message.cjs +30 -5
- package/lib/node_modules/cborg/cborg.cjs +4 -0
- package/lib/node_modules/cborg/lib/0uint.cjs +214 -0
- package/lib/node_modules/cborg/lib/1negint.cjs +95 -0
- package/lib/node_modules/cborg/lib/2bytes.cjs +125 -0
- package/lib/node_modules/cborg/lib/3string.cjs +108 -0
- package/lib/node_modules/cborg/lib/4array.cjs +106 -0
- package/lib/node_modules/cborg/lib/5map.cjs +106 -0
- package/lib/node_modules/cborg/lib/6tag.cjs +81 -0
- package/lib/node_modules/cborg/lib/7float.cjs +233 -0
- package/lib/node_modules/cborg/lib/bl.cjs +132 -0
- package/lib/node_modules/cborg/lib/byte-utils.cjs +111 -0
- package/lib/node_modules/cborg/lib/common.cjs +23 -0
- package/lib/node_modules/cborg/lib/decode.cjs +213 -0
- package/lib/node_modules/cborg/lib/encode.cjs +390 -0
- package/lib/node_modules/cborg/lib/is.cjs +60 -0
- package/lib/node_modules/cborg/lib/jump.cjs +156 -0
- package/lib/node_modules/cborg/lib/tagged.cjs +71 -0
- package/lib/node_modules/cborg/lib/token.cjs +75 -0
- package/lib/patch.cjs +1 -1
- package/lib/protobuf.cjs +18 -0
- package/lib/protobuf.d.cts +6 -0
- package/lib/provider-init-schema.cjs +23 -0
- package/lib/provider-init-schema.d.cts +1 -0
- package/lib/provider-init.cjs +19 -0
- package/lib/provider-init.d.cts +1 -0
- package/package.json +14 -3
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { Token, Type } from "./token.mjs";
|
|
2
|
+
import { assertEnoughData, decodeErrPrefix } from "./common.mjs";
|
|
3
|
+
|
|
4
|
+
//#region ../../node_modules/cborg/lib/0uint.js
|
|
5
|
+
const uintBoundaries = [
|
|
6
|
+
24,
|
|
7
|
+
256,
|
|
8
|
+
65536,
|
|
9
|
+
4294967296,
|
|
10
|
+
BigInt("18446744073709551616")
|
|
11
|
+
];
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
14
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* @param {Uint8Array} data
|
|
18
|
+
* @param {number} offset
|
|
19
|
+
* @param {DecodeOptions} options
|
|
20
|
+
* @returns {number}
|
|
21
|
+
*/
|
|
22
|
+
function readUint8(data, offset, options) {
|
|
23
|
+
assertEnoughData(data, offset, 1);
|
|
24
|
+
const value = data[offset];
|
|
25
|
+
if (options.strict === true && value < uintBoundaries[0]) throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* @param {Uint8Array} data
|
|
30
|
+
* @param {number} offset
|
|
31
|
+
* @param {DecodeOptions} options
|
|
32
|
+
* @returns {number}
|
|
33
|
+
*/
|
|
34
|
+
function readUint16(data, offset, options) {
|
|
35
|
+
assertEnoughData(data, offset, 2);
|
|
36
|
+
const value = data[offset] << 8 | data[offset + 1];
|
|
37
|
+
if (options.strict === true && value < uintBoundaries[1]) throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @param {Uint8Array} data
|
|
42
|
+
* @param {number} offset
|
|
43
|
+
* @param {DecodeOptions} options
|
|
44
|
+
* @returns {number}
|
|
45
|
+
*/
|
|
46
|
+
function readUint32(data, offset, options) {
|
|
47
|
+
assertEnoughData(data, offset, 4);
|
|
48
|
+
const value = data[offset] * 16777216 + (data[offset + 1] << 16) + (data[offset + 2] << 8) + data[offset + 3];
|
|
49
|
+
if (options.strict === true && value < uintBoundaries[2]) throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @param {Uint8Array} data
|
|
54
|
+
* @param {number} offset
|
|
55
|
+
* @param {DecodeOptions} options
|
|
56
|
+
* @returns {number|bigint}
|
|
57
|
+
*/
|
|
58
|
+
function readUint64(data, offset, options) {
|
|
59
|
+
assertEnoughData(data, offset, 8);
|
|
60
|
+
const hi = data[offset] * 16777216 + (data[offset + 1] << 16) + (data[offset + 2] << 8) + data[offset + 3];
|
|
61
|
+
const lo = data[offset + 4] * 16777216 + (data[offset + 5] << 16) + (data[offset + 6] << 8) + data[offset + 7];
|
|
62
|
+
const value = (BigInt(hi) << BigInt(32)) + BigInt(lo);
|
|
63
|
+
if (options.strict === true && value < uintBoundaries[3]) throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
64
|
+
if (value <= Number.MAX_SAFE_INTEGER) return Number(value);
|
|
65
|
+
if (options.allowBigInt === true) return value;
|
|
66
|
+
throw new Error(`${decodeErrPrefix} integers outside of the safe integer range are not supported`);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @param {Uint8Array} data
|
|
70
|
+
* @param {number} pos
|
|
71
|
+
* @param {number} _minor
|
|
72
|
+
* @param {DecodeOptions} options
|
|
73
|
+
* @returns {Token}
|
|
74
|
+
*/
|
|
75
|
+
function decodeUint8(data, pos, _minor, options) {
|
|
76
|
+
return new Token(Type.uint, readUint8(data, pos + 1, options), 2);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @param {Uint8Array} data
|
|
80
|
+
* @param {number} pos
|
|
81
|
+
* @param {number} _minor
|
|
82
|
+
* @param {DecodeOptions} options
|
|
83
|
+
* @returns {Token}
|
|
84
|
+
*/
|
|
85
|
+
function decodeUint16(data, pos, _minor, options) {
|
|
86
|
+
return new Token(Type.uint, readUint16(data, pos + 1, options), 3);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* @param {Uint8Array} data
|
|
90
|
+
* @param {number} pos
|
|
91
|
+
* @param {number} _minor
|
|
92
|
+
* @param {DecodeOptions} options
|
|
93
|
+
* @returns {Token}
|
|
94
|
+
*/
|
|
95
|
+
function decodeUint32(data, pos, _minor, options) {
|
|
96
|
+
return new Token(Type.uint, readUint32(data, pos + 1, options), 5);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* @param {Uint8Array} data
|
|
100
|
+
* @param {number} pos
|
|
101
|
+
* @param {number} _minor
|
|
102
|
+
* @param {DecodeOptions} options
|
|
103
|
+
* @returns {Token}
|
|
104
|
+
*/
|
|
105
|
+
function decodeUint64(data, pos, _minor, options) {
|
|
106
|
+
return new Token(Type.uint, readUint64(data, pos + 1, options), 9);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* @param {ByteWriter} writer
|
|
110
|
+
* @param {Token} token
|
|
111
|
+
*/
|
|
112
|
+
function encodeUint(writer, token) {
|
|
113
|
+
return encodeUintValue(writer, 0, token.value);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* @param {ByteWriter} writer
|
|
117
|
+
* @param {number} major
|
|
118
|
+
* @param {number|bigint} uint
|
|
119
|
+
*/
|
|
120
|
+
function encodeUintValue(writer, major, uint) {
|
|
121
|
+
if (uint < uintBoundaries[0]) {
|
|
122
|
+
const nuint = Number(uint);
|
|
123
|
+
writer.push([major | nuint]);
|
|
124
|
+
} else if (uint < uintBoundaries[1]) {
|
|
125
|
+
const nuint = Number(uint);
|
|
126
|
+
writer.push([major | 24, nuint]);
|
|
127
|
+
} else if (uint < uintBoundaries[2]) {
|
|
128
|
+
const nuint = Number(uint);
|
|
129
|
+
writer.push([
|
|
130
|
+
major | 25,
|
|
131
|
+
nuint >>> 8,
|
|
132
|
+
nuint & 255
|
|
133
|
+
]);
|
|
134
|
+
} else if (uint < uintBoundaries[3]) {
|
|
135
|
+
const nuint = Number(uint);
|
|
136
|
+
writer.push([
|
|
137
|
+
major | 26,
|
|
138
|
+
nuint >>> 24 & 255,
|
|
139
|
+
nuint >>> 16 & 255,
|
|
140
|
+
nuint >>> 8 & 255,
|
|
141
|
+
nuint & 255
|
|
142
|
+
]);
|
|
143
|
+
} else {
|
|
144
|
+
const buint = BigInt(uint);
|
|
145
|
+
if (buint < uintBoundaries[4]) {
|
|
146
|
+
const set = [
|
|
147
|
+
major | 27,
|
|
148
|
+
0,
|
|
149
|
+
0,
|
|
150
|
+
0,
|
|
151
|
+
0,
|
|
152
|
+
0,
|
|
153
|
+
0,
|
|
154
|
+
0
|
|
155
|
+
];
|
|
156
|
+
let lo = Number(buint & BigInt(4294967295));
|
|
157
|
+
let hi = Number(buint >> BigInt(32) & BigInt(4294967295));
|
|
158
|
+
set[8] = lo & 255;
|
|
159
|
+
lo = lo >> 8;
|
|
160
|
+
set[7] = lo & 255;
|
|
161
|
+
lo = lo >> 8;
|
|
162
|
+
set[6] = lo & 255;
|
|
163
|
+
lo = lo >> 8;
|
|
164
|
+
set[5] = lo & 255;
|
|
165
|
+
set[4] = hi & 255;
|
|
166
|
+
hi = hi >> 8;
|
|
167
|
+
set[3] = hi & 255;
|
|
168
|
+
hi = hi >> 8;
|
|
169
|
+
set[2] = hi & 255;
|
|
170
|
+
hi = hi >> 8;
|
|
171
|
+
set[1] = hi & 255;
|
|
172
|
+
writer.push(set);
|
|
173
|
+
} else throw new Error(`${decodeErrPrefix} encountered BigInt larger than allowable range`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* @param {Token} token
|
|
178
|
+
* @returns {number}
|
|
179
|
+
*/
|
|
180
|
+
encodeUint.encodedSize = function encodedSize(token) {
|
|
181
|
+
return encodeUintValue.encodedSize(token.value);
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* @param {number} uint
|
|
185
|
+
* @returns {number}
|
|
186
|
+
*/
|
|
187
|
+
encodeUintValue.encodedSize = function encodedSize(uint) {
|
|
188
|
+
if (uint < uintBoundaries[0]) return 1;
|
|
189
|
+
if (uint < uintBoundaries[1]) return 2;
|
|
190
|
+
if (uint < uintBoundaries[2]) return 3;
|
|
191
|
+
if (uint < uintBoundaries[3]) return 5;
|
|
192
|
+
return 9;
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* @param {Token} tok1
|
|
196
|
+
* @param {Token} tok2
|
|
197
|
+
* @returns {number}
|
|
198
|
+
*/
|
|
199
|
+
encodeUint.compareTokens = function compareTokens(tok1, tok2) {
|
|
200
|
+
return tok1.value < tok2.value ? -1 : tok1.value > tok2.value ? 1 : 0;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
//#endregion
|
|
204
|
+
export { decodeUint16, decodeUint32, decodeUint64, decodeUint8, encodeUint, encodeUintValue, readUint16, readUint32, readUint64, readUint8, uintBoundaries };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Token, Type } from "./token.mjs";
|
|
2
|
+
import { decodeErrPrefix } from "./common.mjs";
|
|
3
|
+
import { encodeUintValue, readUint16, readUint32, readUint64, readUint8, uintBoundaries } from "./0uint.mjs";
|
|
4
|
+
|
|
5
|
+
//#region ../../node_modules/cborg/lib/1negint.js
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
8
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @param {Uint8Array} data
|
|
12
|
+
* @param {number} pos
|
|
13
|
+
* @param {number} _minor
|
|
14
|
+
* @param {DecodeOptions} options
|
|
15
|
+
* @returns {Token}
|
|
16
|
+
*/
|
|
17
|
+
function decodeNegint8(data, pos, _minor, options) {
|
|
18
|
+
return new Token(Type.negint, -1 - readUint8(data, pos + 1, options), 2);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @param {Uint8Array} data
|
|
22
|
+
* @param {number} pos
|
|
23
|
+
* @param {number} _minor
|
|
24
|
+
* @param {DecodeOptions} options
|
|
25
|
+
* @returns {Token}
|
|
26
|
+
*/
|
|
27
|
+
function decodeNegint16(data, pos, _minor, options) {
|
|
28
|
+
return new Token(Type.negint, -1 - readUint16(data, pos + 1, options), 3);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @param {Uint8Array} data
|
|
32
|
+
* @param {number} pos
|
|
33
|
+
* @param {number} _minor
|
|
34
|
+
* @param {DecodeOptions} options
|
|
35
|
+
* @returns {Token}
|
|
36
|
+
*/
|
|
37
|
+
function decodeNegint32(data, pos, _minor, options) {
|
|
38
|
+
return new Token(Type.negint, -1 - readUint32(data, pos + 1, options), 5);
|
|
39
|
+
}
|
|
40
|
+
const neg1b = BigInt(-1);
|
|
41
|
+
const pos1b = BigInt(1);
|
|
42
|
+
/**
|
|
43
|
+
* @param {Uint8Array} data
|
|
44
|
+
* @param {number} pos
|
|
45
|
+
* @param {number} _minor
|
|
46
|
+
* @param {DecodeOptions} options
|
|
47
|
+
* @returns {Token}
|
|
48
|
+
*/
|
|
49
|
+
function decodeNegint64(data, pos, _minor, options) {
|
|
50
|
+
const int = readUint64(data, pos + 1, options);
|
|
51
|
+
if (typeof int !== "bigint") {
|
|
52
|
+
const value = -1 - int;
|
|
53
|
+
if (value >= Number.MIN_SAFE_INTEGER) return new Token(Type.negint, value, 9);
|
|
54
|
+
}
|
|
55
|
+
if (options.allowBigInt !== true) throw new Error(`${decodeErrPrefix} integers outside of the safe integer range are not supported`);
|
|
56
|
+
return new Token(Type.negint, neg1b - BigInt(int), 9);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* @param {ByteWriter} writer
|
|
60
|
+
* @param {Token} token
|
|
61
|
+
*/
|
|
62
|
+
function encodeNegint(writer, token) {
|
|
63
|
+
const negint = token.value;
|
|
64
|
+
const unsigned = typeof negint === "bigint" ? negint * neg1b - pos1b : negint * -1 - 1;
|
|
65
|
+
encodeUintValue(writer, token.type.majorEncoded, unsigned);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* @param {Token} token
|
|
69
|
+
* @returns {number}
|
|
70
|
+
*/
|
|
71
|
+
encodeNegint.encodedSize = function encodedSize(token) {
|
|
72
|
+
const negint = token.value;
|
|
73
|
+
const unsigned = typeof negint === "bigint" ? negint * neg1b - pos1b : negint * -1 - 1;
|
|
74
|
+
/* c8 ignore next 4 */
|
|
75
|
+
if (unsigned < uintBoundaries[0]) return 1;
|
|
76
|
+
if (unsigned < uintBoundaries[1]) return 2;
|
|
77
|
+
if (unsigned < uintBoundaries[2]) return 3;
|
|
78
|
+
if (unsigned < uintBoundaries[3]) return 5;
|
|
79
|
+
return 9;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* @param {Token} tok1
|
|
83
|
+
* @param {Token} tok2
|
|
84
|
+
* @returns {number}
|
|
85
|
+
*/
|
|
86
|
+
encodeNegint.compareTokens = function compareTokens(tok1, tok2) {
|
|
87
|
+
return tok1.value < tok2.value ? 1 : tok1.value > tok2.value ? -1 : 0;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
export { decodeNegint16, decodeNegint32, decodeNegint64, decodeNegint8, encodeNegint };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Token, Type } from "./token.mjs";
|
|
2
|
+
import { compare, fromString } from "./byte-utils.mjs";
|
|
3
|
+
import { assertEnoughData, decodeErrPrefix } from "./common.mjs";
|
|
4
|
+
import { encodeUintValue, readUint16, readUint32, readUint64, readUint8 } from "./0uint.mjs";
|
|
5
|
+
|
|
6
|
+
//#region ../../node_modules/cborg/lib/2bytes.js
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
9
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* @param {Uint8Array} data
|
|
13
|
+
* @param {number} pos
|
|
14
|
+
* @param {number} prefix
|
|
15
|
+
* @param {number} length
|
|
16
|
+
* @returns {Token}
|
|
17
|
+
*/
|
|
18
|
+
function toToken(data, pos, prefix, length) {
|
|
19
|
+
assertEnoughData(data, pos, prefix + length);
|
|
20
|
+
const buf = data.slice(pos + prefix, pos + prefix + length);
|
|
21
|
+
return new Token(Type.bytes, buf, prefix + length);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @param {Uint8Array} data
|
|
25
|
+
* @param {number} pos
|
|
26
|
+
* @param {number} minor
|
|
27
|
+
* @param {DecodeOptions} _options
|
|
28
|
+
* @returns {Token}
|
|
29
|
+
*/
|
|
30
|
+
function decodeBytesCompact(data, pos, minor, _options) {
|
|
31
|
+
return toToken(data, pos, 1, minor);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @param {Uint8Array} data
|
|
35
|
+
* @param {number} pos
|
|
36
|
+
* @param {number} _minor
|
|
37
|
+
* @param {DecodeOptions} options
|
|
38
|
+
* @returns {Token}
|
|
39
|
+
*/
|
|
40
|
+
function decodeBytes8(data, pos, _minor, options) {
|
|
41
|
+
return toToken(data, pos, 2, readUint8(data, pos + 1, options));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* @param {Uint8Array} data
|
|
45
|
+
* @param {number} pos
|
|
46
|
+
* @param {number} _minor
|
|
47
|
+
* @param {DecodeOptions} options
|
|
48
|
+
* @returns {Token}
|
|
49
|
+
*/
|
|
50
|
+
function decodeBytes16(data, pos, _minor, options) {
|
|
51
|
+
return toToken(data, pos, 3, readUint16(data, pos + 1, options));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @param {Uint8Array} data
|
|
55
|
+
* @param {number} pos
|
|
56
|
+
* @param {number} _minor
|
|
57
|
+
* @param {DecodeOptions} options
|
|
58
|
+
* @returns {Token}
|
|
59
|
+
*/
|
|
60
|
+
function decodeBytes32(data, pos, _minor, options) {
|
|
61
|
+
return toToken(data, pos, 5, readUint32(data, pos + 1, options));
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* @param {Uint8Array} data
|
|
65
|
+
* @param {number} pos
|
|
66
|
+
* @param {number} _minor
|
|
67
|
+
* @param {DecodeOptions} options
|
|
68
|
+
* @returns {Token}
|
|
69
|
+
*/
|
|
70
|
+
function decodeBytes64(data, pos, _minor, options) {
|
|
71
|
+
const l = readUint64(data, pos + 1, options);
|
|
72
|
+
if (typeof l === "bigint") throw new Error(`${decodeErrPrefix} 64-bit integer bytes lengths not supported`);
|
|
73
|
+
return toToken(data, pos, 9, l);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* `encodedBytes` allows for caching when we do a byte version of a string
|
|
77
|
+
* for key sorting purposes
|
|
78
|
+
* @param {Token} token
|
|
79
|
+
* @returns {Uint8Array}
|
|
80
|
+
*/
|
|
81
|
+
function tokenBytes(token) {
|
|
82
|
+
if (token.encodedBytes === void 0) token.encodedBytes = Type.equals(token.type, Type.string) ? fromString(token.value) : token.value;
|
|
83
|
+
return token.encodedBytes;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @param {ByteWriter} writer
|
|
87
|
+
* @param {Token} token
|
|
88
|
+
*/
|
|
89
|
+
function encodeBytes(writer, token) {
|
|
90
|
+
const bytes = tokenBytes(token);
|
|
91
|
+
encodeUintValue(writer, token.type.majorEncoded, bytes.length);
|
|
92
|
+
writer.push(bytes);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* @param {Token} token
|
|
96
|
+
* @returns {number}
|
|
97
|
+
*/
|
|
98
|
+
encodeBytes.encodedSize = function encodedSize(token) {
|
|
99
|
+
const bytes = tokenBytes(token);
|
|
100
|
+
return encodeUintValue.encodedSize(bytes.length) + bytes.length;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* @param {Token} tok1
|
|
104
|
+
* @param {Token} tok2
|
|
105
|
+
* @returns {number}
|
|
106
|
+
*/
|
|
107
|
+
encodeBytes.compareTokens = function compareTokens(tok1, tok2) {
|
|
108
|
+
return compareBytes(tokenBytes(tok1), tokenBytes(tok2));
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* @param {Uint8Array} b1
|
|
112
|
+
* @param {Uint8Array} b2
|
|
113
|
+
* @returns {number}
|
|
114
|
+
*/
|
|
115
|
+
function compareBytes(b1, b2) {
|
|
116
|
+
return b1.length < b2.length ? -1 : b1.length > b2.length ? 1 : compare(b1, b2);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
//#endregion
|
|
120
|
+
export { decodeBytes16, decodeBytes32, decodeBytes64, decodeBytes8, decodeBytesCompact, encodeBytes };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Token, Type } from "./token.mjs";
|
|
2
|
+
import { assertEnoughData, decodeErrPrefix } from "./common.mjs";
|
|
3
|
+
import { readUint16, readUint32, readUint64, readUint8 } from "./0uint.mjs";
|
|
4
|
+
import { encodeBytes } from "./2bytes.mjs";
|
|
5
|
+
|
|
6
|
+
//#region ../../node_modules/cborg/lib/3string.js
|
|
7
|
+
const textDecoder = new TextDecoder();
|
|
8
|
+
const ASCII_THRESHOLD = 32;
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
11
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Decode UTF-8 bytes to string. For short ASCII strings (common case for map keys),
|
|
15
|
+
* a simple loop is faster than TextDecoder.
|
|
16
|
+
* @param {Uint8Array} bytes
|
|
17
|
+
* @param {number} start
|
|
18
|
+
* @param {number} end
|
|
19
|
+
* @returns {string}
|
|
20
|
+
*/
|
|
21
|
+
function toStr(bytes, start, end) {
|
|
22
|
+
if (end - start < ASCII_THRESHOLD) {
|
|
23
|
+
let str = "";
|
|
24
|
+
for (let i = start; i < end; i++) {
|
|
25
|
+
const c = bytes[i];
|
|
26
|
+
if (c & 128) return textDecoder.decode(bytes.subarray(start, end));
|
|
27
|
+
str += String.fromCharCode(c);
|
|
28
|
+
}
|
|
29
|
+
return str;
|
|
30
|
+
}
|
|
31
|
+
return textDecoder.decode(bytes.subarray(start, end));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* @param {Uint8Array} data
|
|
35
|
+
* @param {number} pos
|
|
36
|
+
* @param {number} prefix
|
|
37
|
+
* @param {number} length
|
|
38
|
+
* @param {DecodeOptions} options
|
|
39
|
+
* @returns {Token}
|
|
40
|
+
*/
|
|
41
|
+
function toToken(data, pos, prefix, length, options) {
|
|
42
|
+
const totLength = prefix + length;
|
|
43
|
+
assertEnoughData(data, pos, totLength);
|
|
44
|
+
const tok = new Token(Type.string, toStr(data, pos + prefix, pos + totLength), totLength);
|
|
45
|
+
if (options.retainStringBytes === true) tok.byteValue = data.slice(pos + prefix, pos + totLength);
|
|
46
|
+
return tok;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @param {Uint8Array} data
|
|
50
|
+
* @param {number} pos
|
|
51
|
+
* @param {number} minor
|
|
52
|
+
* @param {DecodeOptions} options
|
|
53
|
+
* @returns {Token}
|
|
54
|
+
*/
|
|
55
|
+
function decodeStringCompact(data, pos, minor, options) {
|
|
56
|
+
return toToken(data, pos, 1, minor, options);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* @param {Uint8Array} data
|
|
60
|
+
* @param {number} pos
|
|
61
|
+
* @param {number} _minor
|
|
62
|
+
* @param {DecodeOptions} options
|
|
63
|
+
* @returns {Token}
|
|
64
|
+
*/
|
|
65
|
+
function decodeString8(data, pos, _minor, options) {
|
|
66
|
+
return toToken(data, pos, 2, readUint8(data, pos + 1, options), options);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @param {Uint8Array} data
|
|
70
|
+
* @param {number} pos
|
|
71
|
+
* @param {number} _minor
|
|
72
|
+
* @param {DecodeOptions} options
|
|
73
|
+
* @returns {Token}
|
|
74
|
+
*/
|
|
75
|
+
function decodeString16(data, pos, _minor, options) {
|
|
76
|
+
return toToken(data, pos, 3, readUint16(data, pos + 1, options), options);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @param {Uint8Array} data
|
|
80
|
+
* @param {number} pos
|
|
81
|
+
* @param {number} _minor
|
|
82
|
+
* @param {DecodeOptions} options
|
|
83
|
+
* @returns {Token}
|
|
84
|
+
*/
|
|
85
|
+
function decodeString32(data, pos, _minor, options) {
|
|
86
|
+
return toToken(data, pos, 5, readUint32(data, pos + 1, options), options);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* @param {Uint8Array} data
|
|
90
|
+
* @param {number} pos
|
|
91
|
+
* @param {number} _minor
|
|
92
|
+
* @param {DecodeOptions} options
|
|
93
|
+
* @returns {Token}
|
|
94
|
+
*/
|
|
95
|
+
function decodeString64(data, pos, _minor, options) {
|
|
96
|
+
const l = readUint64(data, pos + 1, options);
|
|
97
|
+
if (typeof l === "bigint") throw new Error(`${decodeErrPrefix} 64-bit integer string lengths not supported`);
|
|
98
|
+
return toToken(data, pos, 9, l, options);
|
|
99
|
+
}
|
|
100
|
+
const encodeString = encodeBytes;
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
export { decodeString16, decodeString32, decodeString64, decodeString8, decodeStringCompact, encodeString };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Token, Type } from "./token.mjs";
|
|
2
|
+
import { decodeErrPrefix } from "./common.mjs";
|
|
3
|
+
import { encodeUint, encodeUintValue, readUint16, readUint32, readUint64, readUint8 } from "./0uint.mjs";
|
|
4
|
+
|
|
5
|
+
//#region ../../node_modules/cborg/lib/4array.js
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
8
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @param {Uint8Array} _data
|
|
12
|
+
* @param {number} _pos
|
|
13
|
+
* @param {number} prefix
|
|
14
|
+
* @param {number} length
|
|
15
|
+
* @returns {Token}
|
|
16
|
+
*/
|
|
17
|
+
function toToken(_data, _pos, prefix, length) {
|
|
18
|
+
return new Token(Type.array, length, prefix);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @param {Uint8Array} data
|
|
22
|
+
* @param {number} pos
|
|
23
|
+
* @param {number} minor
|
|
24
|
+
* @param {DecodeOptions} _options
|
|
25
|
+
* @returns {Token}
|
|
26
|
+
*/
|
|
27
|
+
function decodeArrayCompact(data, pos, minor, _options) {
|
|
28
|
+
return toToken(data, pos, 1, minor);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @param {Uint8Array} data
|
|
32
|
+
* @param {number} pos
|
|
33
|
+
* @param {number} _minor
|
|
34
|
+
* @param {DecodeOptions} options
|
|
35
|
+
* @returns {Token}
|
|
36
|
+
*/
|
|
37
|
+
function decodeArray8(data, pos, _minor, options) {
|
|
38
|
+
return toToken(data, pos, 2, readUint8(data, pos + 1, options));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @param {Uint8Array} data
|
|
42
|
+
* @param {number} pos
|
|
43
|
+
* @param {number} _minor
|
|
44
|
+
* @param {DecodeOptions} options
|
|
45
|
+
* @returns {Token}
|
|
46
|
+
*/
|
|
47
|
+
function decodeArray16(data, pos, _minor, options) {
|
|
48
|
+
return toToken(data, pos, 3, readUint16(data, pos + 1, options));
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @param {Uint8Array} data
|
|
52
|
+
* @param {number} pos
|
|
53
|
+
* @param {number} _minor
|
|
54
|
+
* @param {DecodeOptions} options
|
|
55
|
+
* @returns {Token}
|
|
56
|
+
*/
|
|
57
|
+
function decodeArray32(data, pos, _minor, options) {
|
|
58
|
+
return toToken(data, pos, 5, readUint32(data, pos + 1, options));
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @param {Uint8Array} data
|
|
62
|
+
* @param {number} pos
|
|
63
|
+
* @param {number} _minor
|
|
64
|
+
* @param {DecodeOptions} options
|
|
65
|
+
* @returns {Token}
|
|
66
|
+
*/
|
|
67
|
+
function decodeArray64(data, pos, _minor, options) {
|
|
68
|
+
const l = readUint64(data, pos + 1, options);
|
|
69
|
+
if (typeof l === "bigint") throw new Error(`${decodeErrPrefix} 64-bit integer array lengths not supported`);
|
|
70
|
+
return toToken(data, pos, 9, l);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @param {Uint8Array} data
|
|
74
|
+
* @param {number} pos
|
|
75
|
+
* @param {number} _minor
|
|
76
|
+
* @param {DecodeOptions} options
|
|
77
|
+
* @returns {Token}
|
|
78
|
+
*/
|
|
79
|
+
function decodeArrayIndefinite(data, pos, _minor, options) {
|
|
80
|
+
if (options.allowIndefinite === false) throw new Error(`${decodeErrPrefix} indefinite length items not allowed`);
|
|
81
|
+
return toToken(data, pos, 1, Infinity);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* @param {ByteWriter} writer
|
|
85
|
+
* @param {Token} token
|
|
86
|
+
*/
|
|
87
|
+
function encodeArray(writer, token) {
|
|
88
|
+
encodeUintValue(writer, Type.array.majorEncoded, token.value);
|
|
89
|
+
}
|
|
90
|
+
encodeArray.compareTokens = encodeUint.compareTokens;
|
|
91
|
+
/**
|
|
92
|
+
* @param {Token} token
|
|
93
|
+
* @returns {number}
|
|
94
|
+
*/
|
|
95
|
+
encodeArray.encodedSize = function encodedSize(token) {
|
|
96
|
+
return encodeUintValue.encodedSize(token.value);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
export { decodeArray16, decodeArray32, decodeArray64, decodeArray8, decodeArrayCompact, decodeArrayIndefinite, encodeArray };
|