@ocap/message 1.30.2 → 1.30.4
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/esm/provider.mjs +4 -2
- 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/lib/provider.cjs +4 -2
- package/package.json +14 -3
|
@@ -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/5map.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.map, 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 decodeMapCompact(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 decodeMap8(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 decodeMap16(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 decodeMap32(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 decodeMap64(data, pos, _minor, options) {
|
|
68
|
+
const l = readUint64(data, pos + 1, options);
|
|
69
|
+
if (typeof l === "bigint") throw new Error(`${decodeErrPrefix} 64-bit integer map 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 decodeMapIndefinite(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 encodeMap(writer, token) {
|
|
88
|
+
encodeUintValue(writer, Type.map.majorEncoded, token.value);
|
|
89
|
+
}
|
|
90
|
+
encodeMap.compareTokens = encodeUint.compareTokens;
|
|
91
|
+
/**
|
|
92
|
+
* @param {Token} token
|
|
93
|
+
* @returns {number}
|
|
94
|
+
*/
|
|
95
|
+
encodeMap.encodedSize = function encodedSize(token) {
|
|
96
|
+
return encodeUintValue.encodedSize(token.value);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
export { decodeMap16, decodeMap32, decodeMap64, decodeMap8, decodeMapCompact, decodeMapIndefinite, encodeMap };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Token, Type } from "./token.mjs";
|
|
2
|
+
import { encodeUint, encodeUintValue, readUint16, readUint32, readUint64, readUint8 } from "./0uint.mjs";
|
|
3
|
+
|
|
4
|
+
//#region ../../node_modules/cborg/lib/6tag.js
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
7
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* @param {Uint8Array} _data
|
|
11
|
+
* @param {number} _pos
|
|
12
|
+
* @param {number} minor
|
|
13
|
+
* @param {DecodeOptions} _options
|
|
14
|
+
* @returns {Token}
|
|
15
|
+
*/
|
|
16
|
+
function decodeTagCompact(_data, _pos, minor, _options) {
|
|
17
|
+
return new Token(Type.tag, minor, 1);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @param {Uint8Array} data
|
|
21
|
+
* @param {number} pos
|
|
22
|
+
* @param {number} _minor
|
|
23
|
+
* @param {DecodeOptions} options
|
|
24
|
+
* @returns {Token}
|
|
25
|
+
*/
|
|
26
|
+
function decodeTag8(data, pos, _minor, options) {
|
|
27
|
+
return new Token(Type.tag, readUint8(data, pos + 1, options), 2);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @param {Uint8Array} data
|
|
31
|
+
* @param {number} pos
|
|
32
|
+
* @param {number} _minor
|
|
33
|
+
* @param {DecodeOptions} options
|
|
34
|
+
* @returns {Token}
|
|
35
|
+
*/
|
|
36
|
+
function decodeTag16(data, pos, _minor, options) {
|
|
37
|
+
return new Token(Type.tag, readUint16(data, pos + 1, options), 3);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @param {Uint8Array} data
|
|
41
|
+
* @param {number} pos
|
|
42
|
+
* @param {number} _minor
|
|
43
|
+
* @param {DecodeOptions} options
|
|
44
|
+
* @returns {Token}
|
|
45
|
+
*/
|
|
46
|
+
function decodeTag32(data, pos, _minor, options) {
|
|
47
|
+
return new Token(Type.tag, readUint32(data, pos + 1, options), 5);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* @param {Uint8Array} data
|
|
51
|
+
* @param {number} pos
|
|
52
|
+
* @param {number} _minor
|
|
53
|
+
* @param {DecodeOptions} options
|
|
54
|
+
* @returns {Token}
|
|
55
|
+
*/
|
|
56
|
+
function decodeTag64(data, pos, _minor, options) {
|
|
57
|
+
return new Token(Type.tag, readUint64(data, pos + 1, options), 9);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* @param {ByteWriter} writer
|
|
61
|
+
* @param {Token} token
|
|
62
|
+
*/
|
|
63
|
+
function encodeTag(writer, token) {
|
|
64
|
+
encodeUintValue(writer, Type.tag.majorEncoded, token.value);
|
|
65
|
+
}
|
|
66
|
+
encodeTag.compareTokens = encodeUint.compareTokens;
|
|
67
|
+
/**
|
|
68
|
+
* @param {Token} token
|
|
69
|
+
* @returns {number}
|
|
70
|
+
*/
|
|
71
|
+
encodeTag.encodedSize = function encodedSize(token) {
|
|
72
|
+
return encodeUintValue.encodedSize(token.value);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
export { decodeTag16, decodeTag32, decodeTag64, decodeTag8, decodeTagCompact, encodeTag };
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { Token, Type } from "./token.mjs";
|
|
2
|
+
import { decodeErrPrefix } from "./common.mjs";
|
|
3
|
+
import { encodeUint } from "./0uint.mjs";
|
|
4
|
+
|
|
5
|
+
//#region ../../node_modules/cborg/lib/7float.js
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
8
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
9
|
+
* @typedef {import('../interface.js').EncodeOptions} EncodeOptions
|
|
10
|
+
*/
|
|
11
|
+
const MINOR_FALSE = 20;
|
|
12
|
+
const MINOR_TRUE = 21;
|
|
13
|
+
const MINOR_NULL = 22;
|
|
14
|
+
const MINOR_UNDEFINED = 23;
|
|
15
|
+
/**
|
|
16
|
+
* @param {Uint8Array} _data
|
|
17
|
+
* @param {number} _pos
|
|
18
|
+
* @param {number} _minor
|
|
19
|
+
* @param {DecodeOptions} options
|
|
20
|
+
* @returns {Token}
|
|
21
|
+
*/
|
|
22
|
+
function decodeUndefined(_data, _pos, _minor, options) {
|
|
23
|
+
if (options.allowUndefined === false) throw new Error(`${decodeErrPrefix} undefined values are not supported`);
|
|
24
|
+
else if (options.coerceUndefinedToNull === true) return new Token(Type.null, null, 1);
|
|
25
|
+
return new Token(Type.undefined, void 0, 1);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @param {Uint8Array} _data
|
|
29
|
+
* @param {number} _pos
|
|
30
|
+
* @param {number} _minor
|
|
31
|
+
* @param {DecodeOptions} options
|
|
32
|
+
* @returns {Token}
|
|
33
|
+
*/
|
|
34
|
+
function decodeBreak(_data, _pos, _minor, options) {
|
|
35
|
+
if (options.allowIndefinite === false) throw new Error(`${decodeErrPrefix} indefinite length items not allowed`);
|
|
36
|
+
return new Token(Type.break, void 0, 1);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @param {number} value
|
|
40
|
+
* @param {number} bytes
|
|
41
|
+
* @param {DecodeOptions} options
|
|
42
|
+
* @returns {Token}
|
|
43
|
+
*/
|
|
44
|
+
function createToken(value, bytes, options) {
|
|
45
|
+
if (options) {
|
|
46
|
+
if (options.allowNaN === false && Number.isNaN(value)) throw new Error(`${decodeErrPrefix} NaN values are not supported`);
|
|
47
|
+
if (options.allowInfinity === false && (value === Infinity || value === -Infinity)) throw new Error(`${decodeErrPrefix} Infinity values are not supported`);
|
|
48
|
+
}
|
|
49
|
+
return new Token(Type.float, value, bytes);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* @param {Uint8Array} data
|
|
53
|
+
* @param {number} pos
|
|
54
|
+
* @param {number} _minor
|
|
55
|
+
* @param {DecodeOptions} options
|
|
56
|
+
* @returns {Token}
|
|
57
|
+
*/
|
|
58
|
+
function decodeFloat16(data, pos, _minor, options) {
|
|
59
|
+
return createToken(readFloat16(data, pos + 1), 3, options);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @param {Uint8Array} data
|
|
63
|
+
* @param {number} pos
|
|
64
|
+
* @param {number} _minor
|
|
65
|
+
* @param {DecodeOptions} options
|
|
66
|
+
* @returns {Token}
|
|
67
|
+
*/
|
|
68
|
+
function decodeFloat32(data, pos, _minor, options) {
|
|
69
|
+
return createToken(readFloat32(data, pos + 1), 5, options);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @param {Uint8Array} data
|
|
73
|
+
* @param {number} pos
|
|
74
|
+
* @param {number} _minor
|
|
75
|
+
* @param {DecodeOptions} options
|
|
76
|
+
* @returns {Token}
|
|
77
|
+
*/
|
|
78
|
+
function decodeFloat64(data, pos, _minor, options) {
|
|
79
|
+
return createToken(readFloat64(data, pos + 1), 9, options);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* @param {ByteWriter} writer
|
|
83
|
+
* @param {Token} token
|
|
84
|
+
* @param {EncodeOptions} options
|
|
85
|
+
*/
|
|
86
|
+
function encodeFloat(writer, token, options) {
|
|
87
|
+
const float = token.value;
|
|
88
|
+
if (float === false) writer.push([Type.float.majorEncoded | MINOR_FALSE]);
|
|
89
|
+
else if (float === true) writer.push([Type.float.majorEncoded | MINOR_TRUE]);
|
|
90
|
+
else if (float === null) writer.push([Type.float.majorEncoded | MINOR_NULL]);
|
|
91
|
+
else if (float === void 0) writer.push([Type.float.majorEncoded | MINOR_UNDEFINED]);
|
|
92
|
+
else {
|
|
93
|
+
let decoded;
|
|
94
|
+
let success = false;
|
|
95
|
+
if (!options || options.float64 !== true) {
|
|
96
|
+
encodeFloat16(float);
|
|
97
|
+
decoded = readFloat16(ui8a, 1);
|
|
98
|
+
if (float === decoded || Number.isNaN(float)) {
|
|
99
|
+
ui8a[0] = 249;
|
|
100
|
+
writer.push(ui8a.slice(0, 3));
|
|
101
|
+
success = true;
|
|
102
|
+
} else {
|
|
103
|
+
encodeFloat32(float);
|
|
104
|
+
decoded = readFloat32(ui8a, 1);
|
|
105
|
+
if (float === decoded) {
|
|
106
|
+
ui8a[0] = 250;
|
|
107
|
+
writer.push(ui8a.slice(0, 5));
|
|
108
|
+
success = true;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (!success) {
|
|
113
|
+
encodeFloat64(float);
|
|
114
|
+
decoded = readFloat64(ui8a, 1);
|
|
115
|
+
ui8a[0] = 251;
|
|
116
|
+
writer.push(ui8a.slice(0, 9));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* @param {Token} token
|
|
122
|
+
* @param {EncodeOptions} options
|
|
123
|
+
* @returns {number}
|
|
124
|
+
*/
|
|
125
|
+
encodeFloat.encodedSize = function encodedSize(token, options) {
|
|
126
|
+
const float = token.value;
|
|
127
|
+
if (float === false || float === true || float === null || float === void 0) return 1;
|
|
128
|
+
if (!options || options.float64 !== true) {
|
|
129
|
+
encodeFloat16(float);
|
|
130
|
+
let decoded = readFloat16(ui8a, 1);
|
|
131
|
+
if (float === decoded || Number.isNaN(float)) return 3;
|
|
132
|
+
encodeFloat32(float);
|
|
133
|
+
decoded = readFloat32(ui8a, 1);
|
|
134
|
+
if (float === decoded) return 5;
|
|
135
|
+
}
|
|
136
|
+
return 9;
|
|
137
|
+
};
|
|
138
|
+
const buffer = /* @__PURE__ */ new ArrayBuffer(9);
|
|
139
|
+
const dataView = new DataView(buffer, 1);
|
|
140
|
+
const ui8a = new Uint8Array(buffer, 0);
|
|
141
|
+
/**
|
|
142
|
+
* @param {number} inp
|
|
143
|
+
*/
|
|
144
|
+
function encodeFloat16(inp) {
|
|
145
|
+
if (inp === Infinity) dataView.setUint16(0, 31744, false);
|
|
146
|
+
else if (inp === -Infinity) dataView.setUint16(0, 64512, false);
|
|
147
|
+
else if (Number.isNaN(inp)) dataView.setUint16(0, 32256, false);
|
|
148
|
+
else {
|
|
149
|
+
dataView.setFloat32(0, inp);
|
|
150
|
+
const valu32 = dataView.getUint32(0);
|
|
151
|
+
const exponent = (valu32 & 2139095040) >> 23;
|
|
152
|
+
const mantissa = valu32 & 8388607;
|
|
153
|
+
/* c8 ignore next 6 */
|
|
154
|
+
if (exponent === 255) dataView.setUint16(0, 31744, false);
|
|
155
|
+
else if (exponent === 0) dataView.setUint16(0, (valu32 & 2147483648) >> 16 | mantissa >> 13, false);
|
|
156
|
+
else {
|
|
157
|
+
const logicalExponent = exponent - 127;
|
|
158
|
+
/* c8 ignore next 6 */
|
|
159
|
+
if (logicalExponent < -24) dataView.setUint16(0, 0);
|
|
160
|
+
else if (logicalExponent < -14) dataView.setUint16(0, (valu32 & 2147483648) >> 16 | 1 << 24 + logicalExponent, false);
|
|
161
|
+
else dataView.setUint16(0, (valu32 & 2147483648) >> 16 | logicalExponent + 15 << 10 | mantissa >> 13, false);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* @param {Uint8Array} ui8a
|
|
167
|
+
* @param {number} pos
|
|
168
|
+
* @returns {number}
|
|
169
|
+
*/
|
|
170
|
+
function readFloat16(ui8a$1, pos) {
|
|
171
|
+
if (ui8a$1.length - pos < 2) throw new Error(`${decodeErrPrefix} not enough data for float16`);
|
|
172
|
+
const half = (ui8a$1[pos] << 8) + ui8a$1[pos + 1];
|
|
173
|
+
if (half === 31744) return Infinity;
|
|
174
|
+
if (half === 64512) return -Infinity;
|
|
175
|
+
if (half === 32256) return NaN;
|
|
176
|
+
const exp = half >> 10 & 31;
|
|
177
|
+
const mant = half & 1023;
|
|
178
|
+
let val;
|
|
179
|
+
if (exp === 0) val = mant * 2 ** -24;
|
|
180
|
+
else if (exp !== 31) val = (mant + 1024) * 2 ** (exp - 25);
|
|
181
|
+
else val = mant === 0 ? Infinity : NaN;
|
|
182
|
+
return half & 32768 ? -val : val;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* @param {number} inp
|
|
186
|
+
*/
|
|
187
|
+
function encodeFloat32(inp) {
|
|
188
|
+
dataView.setFloat32(0, inp, false);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* @param {Uint8Array} ui8a
|
|
192
|
+
* @param {number} pos
|
|
193
|
+
* @returns {number}
|
|
194
|
+
*/
|
|
195
|
+
function readFloat32(ui8a$1, pos) {
|
|
196
|
+
if (ui8a$1.length - pos < 4) throw new Error(`${decodeErrPrefix} not enough data for float32`);
|
|
197
|
+
const offset = (ui8a$1.byteOffset || 0) + pos;
|
|
198
|
+
return new DataView(ui8a$1.buffer, offset, 4).getFloat32(0, false);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* @param {number} inp
|
|
202
|
+
*/
|
|
203
|
+
function encodeFloat64(inp) {
|
|
204
|
+
dataView.setFloat64(0, inp, false);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* @param {Uint8Array} ui8a
|
|
208
|
+
* @param {number} pos
|
|
209
|
+
* @returns {number}
|
|
210
|
+
*/
|
|
211
|
+
function readFloat64(ui8a$1, pos) {
|
|
212
|
+
if (ui8a$1.length - pos < 8) throw new Error(`${decodeErrPrefix} not enough data for float64`);
|
|
213
|
+
const offset = (ui8a$1.byteOffset || 0) + pos;
|
|
214
|
+
return new DataView(ui8a$1.buffer, offset, 8).getFloat64(0, false);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* @param {Token} _tok1
|
|
218
|
+
* @param {Token} _tok2
|
|
219
|
+
* @returns {number}
|
|
220
|
+
*/
|
|
221
|
+
encodeFloat.compareTokens = encodeUint.compareTokens;
|
|
222
|
+
|
|
223
|
+
//#endregion
|
|
224
|
+
export { MINOR_FALSE, MINOR_NULL, MINOR_TRUE, MINOR_UNDEFINED, decodeBreak, decodeFloat16, decodeFloat32, decodeFloat64, decodeUndefined, encodeFloat };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { alloc, concat, slice } from "./byte-utils.mjs";
|
|
2
|
+
|
|
3
|
+
//#region ../../node_modules/cborg/lib/bl.js
|
|
4
|
+
/**
|
|
5
|
+
* Bl is a list of byte chunks, similar to https://github.com/rvagg/bl but for
|
|
6
|
+
* writing rather than reading.
|
|
7
|
+
* A Bl object accepts set() operations for individual bytes and copyTo() for
|
|
8
|
+
* inserting byte arrays. These write operations don't automatically increment
|
|
9
|
+
* the internal cursor so its "length" won't be changed. Instead, increment()
|
|
10
|
+
* must be called to extend its length to cover the inserted data.
|
|
11
|
+
* The toBytes() call will convert all internal memory to a single Uint8Array of
|
|
12
|
+
* the correct length, truncating any data that is stored but hasn't been
|
|
13
|
+
* included by an increment().
|
|
14
|
+
* get() can retrieve a single byte.
|
|
15
|
+
* All operations (except toBytes()) take an "offset" argument that will perform
|
|
16
|
+
* the write at the offset _from the current cursor_. For most operations this
|
|
17
|
+
* will be `0` to write at the current cursor position but it can be ahead of
|
|
18
|
+
* the current cursor. Negative offsets probably work but are untested.
|
|
19
|
+
*/
|
|
20
|
+
const defaultChunkSize = 256;
|
|
21
|
+
var Bl = class {
|
|
22
|
+
/**
|
|
23
|
+
* @param {number} [chunkSize]
|
|
24
|
+
*/
|
|
25
|
+
constructor(chunkSize = defaultChunkSize) {
|
|
26
|
+
this.chunkSize = chunkSize;
|
|
27
|
+
/** @type {number} */
|
|
28
|
+
this.cursor = 0;
|
|
29
|
+
/** @type {number} */
|
|
30
|
+
this.maxCursor = -1;
|
|
31
|
+
/** @type {(Uint8Array|number[])[]} */
|
|
32
|
+
this.chunks = [];
|
|
33
|
+
/** @type {Uint8Array|number[]|null} */
|
|
34
|
+
this._initReuseChunk = null;
|
|
35
|
+
}
|
|
36
|
+
reset() {
|
|
37
|
+
this.cursor = 0;
|
|
38
|
+
this.maxCursor = -1;
|
|
39
|
+
if (this.chunks.length) this.chunks = [];
|
|
40
|
+
if (this._initReuseChunk !== null) {
|
|
41
|
+
this.chunks.push(this._initReuseChunk);
|
|
42
|
+
this.maxCursor = this._initReuseChunk.length - 1;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* @param {Uint8Array|number[]} bytes
|
|
47
|
+
*/
|
|
48
|
+
push(bytes) {
|
|
49
|
+
let topChunk = this.chunks[this.chunks.length - 1];
|
|
50
|
+
if (this.cursor + bytes.length <= this.maxCursor + 1) {
|
|
51
|
+
const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
|
|
52
|
+
topChunk.set(bytes, chunkPos);
|
|
53
|
+
} else {
|
|
54
|
+
if (topChunk) {
|
|
55
|
+
const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
|
|
56
|
+
if (chunkPos < topChunk.length) {
|
|
57
|
+
this.chunks[this.chunks.length - 1] = topChunk.subarray(0, chunkPos);
|
|
58
|
+
this.maxCursor = this.cursor - 1;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (bytes.length < 64 && bytes.length < this.chunkSize) {
|
|
62
|
+
topChunk = alloc(this.chunkSize);
|
|
63
|
+
this.chunks.push(topChunk);
|
|
64
|
+
this.maxCursor += topChunk.length;
|
|
65
|
+
if (this._initReuseChunk === null) this._initReuseChunk = topChunk;
|
|
66
|
+
topChunk.set(bytes, 0);
|
|
67
|
+
} else {
|
|
68
|
+
this.chunks.push(bytes);
|
|
69
|
+
this.maxCursor += bytes.length;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
this.cursor += bytes.length;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* @param {boolean} [reset]
|
|
76
|
+
* @returns {Uint8Array}
|
|
77
|
+
*/
|
|
78
|
+
toBytes(reset = false) {
|
|
79
|
+
let byts;
|
|
80
|
+
if (this.chunks.length === 1) {
|
|
81
|
+
const chunk = this.chunks[0];
|
|
82
|
+
if (reset && this.cursor > chunk.length / 2) {
|
|
83
|
+
/* c8 ignore next 2 */
|
|
84
|
+
byts = this.cursor === chunk.length ? chunk : chunk.subarray(0, this.cursor);
|
|
85
|
+
this._initReuseChunk = null;
|
|
86
|
+
this.chunks = [];
|
|
87
|
+
} else byts = slice(chunk, 0, this.cursor);
|
|
88
|
+
} else byts = concat(this.chunks, this.cursor);
|
|
89
|
+
if (reset) this.reset();
|
|
90
|
+
return byts;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* U8Bl is a buffer list that writes directly to a user-provided Uint8Array.
|
|
95
|
+
* It provides the same interface as Bl but writes to a fixed destination.
|
|
96
|
+
*/
|
|
97
|
+
var U8Bl = class {
|
|
98
|
+
/**
|
|
99
|
+
* @param {Uint8Array} dest
|
|
100
|
+
*/
|
|
101
|
+
constructor(dest) {
|
|
102
|
+
this.dest = dest;
|
|
103
|
+
/** @type {number} */
|
|
104
|
+
this.cursor = 0;
|
|
105
|
+
/** @type {Uint8Array[]} */
|
|
106
|
+
this.chunks = [dest];
|
|
107
|
+
}
|
|
108
|
+
reset() {
|
|
109
|
+
this.cursor = 0;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* @param {Uint8Array|number[]} bytes
|
|
113
|
+
*/
|
|
114
|
+
push(bytes) {
|
|
115
|
+
if (this.cursor + bytes.length > this.dest.length) throw new Error("write out of bounds, destination buffer is too small");
|
|
116
|
+
this.dest.set(bytes, this.cursor);
|
|
117
|
+
this.cursor += bytes.length;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @param {boolean} [reset]
|
|
121
|
+
* @returns {Uint8Array}
|
|
122
|
+
*/
|
|
123
|
+
toBytes(reset = false) {
|
|
124
|
+
const byts = this.dest.subarray(0, this.cursor);
|
|
125
|
+
if (reset) this.reset();
|
|
126
|
+
return byts;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
//#endregion
|
|
131
|
+
export { Bl, U8Bl };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
//#region ../../node_modules/cborg/lib/byte-utils.js
|
|
2
|
+
const useBuffer = globalThis.process && !globalThis.process.browser && globalThis.Buffer && typeof globalThis.Buffer.isBuffer === "function";
|
|
3
|
+
const textEncoder = new TextEncoder();
|
|
4
|
+
/**
|
|
5
|
+
* @param {Uint8Array} buf
|
|
6
|
+
* @returns {boolean}
|
|
7
|
+
*/
|
|
8
|
+
function isBuffer(buf) {
|
|
9
|
+
return useBuffer && globalThis.Buffer.isBuffer(buf);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* @param {Uint8Array|number[]} buf
|
|
13
|
+
* @returns {Uint8Array}
|
|
14
|
+
*/
|
|
15
|
+
function asU8A(buf) {
|
|
16
|
+
/* c8 ignore next */
|
|
17
|
+
if (!(buf instanceof Uint8Array)) return Uint8Array.from(buf);
|
|
18
|
+
return isBuffer(buf) ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : buf;
|
|
19
|
+
}
|
|
20
|
+
const FROM_STRING_THRESHOLD_BUFFER = 24;
|
|
21
|
+
const FROM_STRING_THRESHOLD_TEXTENCODER = 200;
|
|
22
|
+
const fromString = useBuffer ? (string) => {
|
|
23
|
+
return string.length >= FROM_STRING_THRESHOLD_BUFFER ? globalThis.Buffer.from(string) : utf8ToBytes(string);
|
|
24
|
+
} : (string) => {
|
|
25
|
+
return string.length >= FROM_STRING_THRESHOLD_TEXTENCODER ? textEncoder.encode(string) : utf8ToBytes(string);
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Buffer variant not fast enough for what we need
|
|
29
|
+
* @param {number[]} arr
|
|
30
|
+
* @returns {Uint8Array}
|
|
31
|
+
*/
|
|
32
|
+
const fromArray = (arr) => {
|
|
33
|
+
return Uint8Array.from(arr);
|
|
34
|
+
};
|
|
35
|
+
const slice = useBuffer ? (bytes, start, end) => {
|
|
36
|
+
if (isBuffer(bytes)) return new Uint8Array(bytes.subarray(start, end));
|
|
37
|
+
return bytes.slice(start, end);
|
|
38
|
+
} : (bytes, start, end) => {
|
|
39
|
+
return bytes.slice(start, end);
|
|
40
|
+
};
|
|
41
|
+
const concat = useBuffer ? (chunks, length) => {
|
|
42
|
+
/* c8 ignore next 1 */
|
|
43
|
+
chunks = chunks.map((c) => c instanceof Uint8Array ? c : globalThis.Buffer.from(c));
|
|
44
|
+
return asU8A(globalThis.Buffer.concat(chunks, length));
|
|
45
|
+
} : (chunks, length) => {
|
|
46
|
+
const out = new Uint8Array(length);
|
|
47
|
+
let off = 0;
|
|
48
|
+
for (let b of chunks) {
|
|
49
|
+
if (off + b.length > out.length) b = b.subarray(0, out.length - off);
|
|
50
|
+
out.set(b, off);
|
|
51
|
+
off += b.length;
|
|
52
|
+
}
|
|
53
|
+
return out;
|
|
54
|
+
};
|
|
55
|
+
const alloc = useBuffer ? (size) => {
|
|
56
|
+
return globalThis.Buffer.allocUnsafe(size);
|
|
57
|
+
} : (size) => {
|
|
58
|
+
return new Uint8Array(size);
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* @param {Uint8Array} b1
|
|
62
|
+
* @param {Uint8Array} b2
|
|
63
|
+
* @returns {number}
|
|
64
|
+
*/
|
|
65
|
+
function compare(b1, b2) {
|
|
66
|
+
/* c8 ignore next 5 */
|
|
67
|
+
if (isBuffer(b1) && isBuffer(b2)) return b1.compare(b2);
|
|
68
|
+
for (let i = 0; i < b1.length; i++) {
|
|
69
|
+
if (b1[i] === b2[i]) continue;
|
|
70
|
+
return b1[i] < b2[i] ? -1 : 1;
|
|
71
|
+
}
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* @param {string} str
|
|
76
|
+
* @returns {number[]}
|
|
77
|
+
*/
|
|
78
|
+
function utf8ToBytes(str) {
|
|
79
|
+
const out = [];
|
|
80
|
+
let p = 0;
|
|
81
|
+
for (let i = 0; i < str.length; i++) {
|
|
82
|
+
let c = str.charCodeAt(i);
|
|
83
|
+
if (c < 128) out[p++] = c;
|
|
84
|
+
else if (c < 2048) {
|
|
85
|
+
out[p++] = c >> 6 | 192;
|
|
86
|
+
out[p++] = c & 63 | 128;
|
|
87
|
+
} else if ((c & 64512) === 55296 && i + 1 < str.length && (str.charCodeAt(i + 1) & 64512) === 56320) {
|
|
88
|
+
c = 65536 + ((c & 1023) << 10) + (str.charCodeAt(++i) & 1023);
|
|
89
|
+
out[p++] = c >> 18 | 240;
|
|
90
|
+
out[p++] = c >> 12 & 63 | 128;
|
|
91
|
+
out[p++] = c >> 6 & 63 | 128;
|
|
92
|
+
out[p++] = c & 63 | 128;
|
|
93
|
+
} else {
|
|
94
|
+
if (c >= 55296 && c <= 57343) c = 65533;
|
|
95
|
+
out[p++] = c >> 12 | 224;
|
|
96
|
+
out[p++] = c >> 6 & 63 | 128;
|
|
97
|
+
out[p++] = c & 63 | 128;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
export { alloc, asU8A, compare, concat, fromArray, fromString, slice };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region ../../node_modules/cborg/lib/common.js
|
|
2
|
+
const decodeErrPrefix = "CBOR decode error:";
|
|
3
|
+
const encodeErrPrefix = "CBOR encode error:";
|
|
4
|
+
const uintMinorPrefixBytes = [];
|
|
5
|
+
uintMinorPrefixBytes[23] = 1;
|
|
6
|
+
uintMinorPrefixBytes[24] = 2;
|
|
7
|
+
uintMinorPrefixBytes[25] = 3;
|
|
8
|
+
uintMinorPrefixBytes[26] = 5;
|
|
9
|
+
uintMinorPrefixBytes[27] = 9;
|
|
10
|
+
/**
|
|
11
|
+
* @param {Uint8Array} data
|
|
12
|
+
* @param {number} pos
|
|
13
|
+
* @param {number} need
|
|
14
|
+
*/
|
|
15
|
+
function assertEnoughData(data, pos, need) {
|
|
16
|
+
if (data.length - pos < need) throw new Error(`${decodeErrPrefix} not enough data for type`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { assertEnoughData, decodeErrPrefix, encodeErrPrefix };
|