@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,210 @@
|
|
|
1
|
+
import { Type } from "./token.mjs";
|
|
2
|
+
import { asU8A } from "./byte-utils.mjs";
|
|
3
|
+
import { decodeErrPrefix } from "./common.mjs";
|
|
4
|
+
import { jump, quick } from "./jump.mjs";
|
|
5
|
+
|
|
6
|
+
//#region ../../node_modules/cborg/lib/decode.js
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {import('./token.js').Token} Token
|
|
9
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
10
|
+
* @typedef {import('../interface.js').DecodeTokenizer} DecodeTokenizer
|
|
11
|
+
* @typedef {import('../interface.js').TagDecodeControl} TagDecodeControl
|
|
12
|
+
*/
|
|
13
|
+
const defaultDecodeOptions = {
|
|
14
|
+
strict: false,
|
|
15
|
+
allowIndefinite: true,
|
|
16
|
+
allowUndefined: true,
|
|
17
|
+
allowBigInt: true
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* @implements {DecodeTokenizer}
|
|
21
|
+
*/
|
|
22
|
+
var Tokeniser = class {
|
|
23
|
+
/**
|
|
24
|
+
* @param {Uint8Array} data
|
|
25
|
+
* @param {DecodeOptions} options
|
|
26
|
+
*/
|
|
27
|
+
constructor(data, options = {}) {
|
|
28
|
+
this._pos = 0;
|
|
29
|
+
this.data = data;
|
|
30
|
+
this.options = options;
|
|
31
|
+
}
|
|
32
|
+
pos() {
|
|
33
|
+
return this._pos;
|
|
34
|
+
}
|
|
35
|
+
done() {
|
|
36
|
+
return this._pos >= this.data.length;
|
|
37
|
+
}
|
|
38
|
+
next() {
|
|
39
|
+
const byt = this.data[this._pos];
|
|
40
|
+
let token = quick[byt];
|
|
41
|
+
if (token === void 0) {
|
|
42
|
+
const decoder = jump[byt];
|
|
43
|
+
/* c8 ignore next 4 */
|
|
44
|
+
if (!decoder) throw new Error(`${decodeErrPrefix} no decoder for major type ${byt >>> 5} (byte 0x${byt.toString(16).padStart(2, "0")})`);
|
|
45
|
+
const minor = byt & 31;
|
|
46
|
+
token = decoder(this.data, this._pos, minor, this.options);
|
|
47
|
+
}
|
|
48
|
+
this._pos += token.encodedLength;
|
|
49
|
+
return token;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const DONE = Symbol.for("DONE");
|
|
53
|
+
const BREAK = Symbol.for("BREAK");
|
|
54
|
+
/**
|
|
55
|
+
* @param {Token} token
|
|
56
|
+
* @param {DecodeTokenizer} tokeniser
|
|
57
|
+
* @param {DecodeOptions} options
|
|
58
|
+
* @returns {any|BREAK|DONE}
|
|
59
|
+
*/
|
|
60
|
+
function tokenToArray(token, tokeniser, options) {
|
|
61
|
+
const arr = [];
|
|
62
|
+
for (let i = 0; i < token.value; i++) {
|
|
63
|
+
const value = tokensToObject(tokeniser, options);
|
|
64
|
+
if (value === BREAK) {
|
|
65
|
+
if (token.value === Infinity) break;
|
|
66
|
+
throw new Error(`${decodeErrPrefix} got unexpected break to lengthed array`);
|
|
67
|
+
}
|
|
68
|
+
if (value === DONE) throw new Error(`${decodeErrPrefix} found array but not enough entries (got ${i}, expected ${token.value})`);
|
|
69
|
+
arr[i] = value;
|
|
70
|
+
}
|
|
71
|
+
return arr;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @param {Token} token
|
|
75
|
+
* @param {DecodeTokenizer} tokeniser
|
|
76
|
+
* @param {DecodeOptions} options
|
|
77
|
+
* @returns {any|BREAK|DONE}
|
|
78
|
+
*/
|
|
79
|
+
function tokenToMap(token, tokeniser, options) {
|
|
80
|
+
const useMaps = options.useMaps === true;
|
|
81
|
+
const rejectDuplicateMapKeys = options.rejectDuplicateMapKeys === true;
|
|
82
|
+
const obj = useMaps ? void 0 : {};
|
|
83
|
+
const m = useMaps ? /* @__PURE__ */ new Map() : void 0;
|
|
84
|
+
for (let i = 0; i < token.value; i++) {
|
|
85
|
+
const key = tokensToObject(tokeniser, options);
|
|
86
|
+
if (key === BREAK) {
|
|
87
|
+
if (token.value === Infinity) break;
|
|
88
|
+
throw new Error(`${decodeErrPrefix} got unexpected break to lengthed map`);
|
|
89
|
+
}
|
|
90
|
+
if (key === DONE) throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no key], expected ${token.value})`);
|
|
91
|
+
if (!useMaps && typeof key !== "string") throw new Error(`${decodeErrPrefix} non-string keys not supported (got ${typeof key})`);
|
|
92
|
+
if (rejectDuplicateMapKeys) {
|
|
93
|
+
if (useMaps && m.has(key) || !useMaps && Object.hasOwn(obj, key)) throw new Error(`${decodeErrPrefix} found repeat map key "${key}"`);
|
|
94
|
+
}
|
|
95
|
+
const value = tokensToObject(tokeniser, options);
|
|
96
|
+
if (value === DONE) throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no value], expected ${token.value})`);
|
|
97
|
+
if (useMaps) m.set(key, value);
|
|
98
|
+
else obj[key] = value;
|
|
99
|
+
}
|
|
100
|
+
return useMaps ? m : obj;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Generator that yields [key, value] entries from a CBOR map token.
|
|
104
|
+
* Used by tag decoders that need to preserve key types (e.g., Tag 259 Map).
|
|
105
|
+
* @param {Token} token - The map token
|
|
106
|
+
* @param {DecodeTokenizer} tokeniser
|
|
107
|
+
* @param {DecodeOptions} options
|
|
108
|
+
* @returns {Generator<[any, any], void, unknown>}
|
|
109
|
+
*/
|
|
110
|
+
function* tokenToMapEntries(token, tokeniser, options) {
|
|
111
|
+
for (let i = 0; i < token.value; i++) {
|
|
112
|
+
const key = tokensToObject(tokeniser, options);
|
|
113
|
+
if (key === BREAK) {
|
|
114
|
+
if (token.value === Infinity) break;
|
|
115
|
+
throw new Error(`${decodeErrPrefix} got unexpected break to lengthed map`);
|
|
116
|
+
}
|
|
117
|
+
if (key === DONE) throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no key], expected ${token.value})`);
|
|
118
|
+
const value = tokensToObject(tokeniser, options);
|
|
119
|
+
if (value === DONE) throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no value], expected ${token.value})`);
|
|
120
|
+
yield [key, value];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Creates a TagDecodeControl object for tag decoders.
|
|
125
|
+
* @param {DecodeTokenizer} tokeniser
|
|
126
|
+
* @param {DecodeOptions} options
|
|
127
|
+
* @returns {TagDecodeControl}
|
|
128
|
+
*/
|
|
129
|
+
function createTagDecodeControl(tokeniser, options) {
|
|
130
|
+
let called = false;
|
|
131
|
+
/**
|
|
132
|
+
* @type {TagDecodeControl}
|
|
133
|
+
*/
|
|
134
|
+
const decode$1 = function() {
|
|
135
|
+
if (called) throw new Error(`${decodeErrPrefix} tag decode() may only be called once`);
|
|
136
|
+
called = true;
|
|
137
|
+
const value = tokensToObject(tokeniser, options);
|
|
138
|
+
if (value === DONE) throw new Error(`${decodeErrPrefix} tag content missing`);
|
|
139
|
+
if (value === BREAK) throw new Error(`${decodeErrPrefix} got unexpected break in tag content`);
|
|
140
|
+
return value;
|
|
141
|
+
};
|
|
142
|
+
decode$1.entries = function() {
|
|
143
|
+
if (called) throw new Error(`${decodeErrPrefix} tag decode() may only be called once`);
|
|
144
|
+
called = true;
|
|
145
|
+
const token = tokeniser.next();
|
|
146
|
+
if (!Type.equals(token.type, Type.map)) throw new Error(`${decodeErrPrefix} entries() requires map content, got ${token.type.name}`);
|
|
147
|
+
const entries = [];
|
|
148
|
+
for (const entry of tokenToMapEntries(token, tokeniser, options)) entries.push(entry);
|
|
149
|
+
return entries;
|
|
150
|
+
};
|
|
151
|
+
Object.defineProperty(decode$1, "_called", {
|
|
152
|
+
get() {
|
|
153
|
+
return called;
|
|
154
|
+
},
|
|
155
|
+
enumerable: false
|
|
156
|
+
});
|
|
157
|
+
return decode$1;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* @param {DecodeTokenizer} tokeniser
|
|
161
|
+
* @param {DecodeOptions} options
|
|
162
|
+
* @returns {any|BREAK|DONE}
|
|
163
|
+
*/
|
|
164
|
+
function tokensToObject(tokeniser, options) {
|
|
165
|
+
if (tokeniser.done()) return DONE;
|
|
166
|
+
const token = tokeniser.next();
|
|
167
|
+
if (Type.equals(token.type, Type.break)) return BREAK;
|
|
168
|
+
if (token.type.terminal) return token.value;
|
|
169
|
+
if (Type.equals(token.type, Type.array)) return tokenToArray(token, tokeniser, options);
|
|
170
|
+
if (Type.equals(token.type, Type.map)) return tokenToMap(token, tokeniser, options);
|
|
171
|
+
if (Type.equals(token.type, Type.tag)) {
|
|
172
|
+
if (options.tags && typeof options.tags[token.value] === "function") {
|
|
173
|
+
const decodeControl = createTagDecodeControl(tokeniser, options);
|
|
174
|
+
const result = options.tags[token.value](decodeControl);
|
|
175
|
+
if (!decodeControl._called) throw new Error(`${decodeErrPrefix} tag decoder must call decode() or entries()`);
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
throw new Error(`${decodeErrPrefix} tag not supported (${token.value})`);
|
|
179
|
+
}
|
|
180
|
+
/* c8 ignore next */
|
|
181
|
+
throw new Error("unsupported");
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* @param {Uint8Array} data
|
|
185
|
+
* @param {DecodeOptions} [options]
|
|
186
|
+
* @returns {[any, Uint8Array]}
|
|
187
|
+
*/
|
|
188
|
+
function decodeFirst(data, options) {
|
|
189
|
+
if (!(data instanceof Uint8Array)) throw new Error(`${decodeErrPrefix} data to decode must be a Uint8Array`);
|
|
190
|
+
options = Object.assign({}, defaultDecodeOptions, options);
|
|
191
|
+
const u8aData = asU8A(data);
|
|
192
|
+
const tokeniser = options.tokenizer || new Tokeniser(u8aData, options);
|
|
193
|
+
const decoded = tokensToObject(tokeniser, options);
|
|
194
|
+
if (decoded === DONE) throw new Error(`${decodeErrPrefix} did not find any content to decode`);
|
|
195
|
+
if (decoded === BREAK) throw new Error(`${decodeErrPrefix} got unexpected break`);
|
|
196
|
+
return [decoded, data.subarray(tokeniser.pos())];
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* @param {Uint8Array} data
|
|
200
|
+
* @param {DecodeOptions} [options]
|
|
201
|
+
* @returns {any}
|
|
202
|
+
*/
|
|
203
|
+
function decode(data, options) {
|
|
204
|
+
const [decoded, remainder] = decodeFirst(data, options);
|
|
205
|
+
if (remainder.length > 0) throw new Error(`${decodeErrPrefix} too many terminals, data makes no sense`);
|
|
206
|
+
return decoded;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
//#endregion
|
|
210
|
+
export { Tokeniser, decode, decodeFirst, tokensToObject };
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { is } from "./is.mjs";
|
|
2
|
+
import { Token, Type } from "./token.mjs";
|
|
3
|
+
import { asU8A, compare, fromString } from "./byte-utils.mjs";
|
|
4
|
+
import { Bl, U8Bl } from "./bl.mjs";
|
|
5
|
+
import { encodeErrPrefix } from "./common.mjs";
|
|
6
|
+
import { encodeUint, encodeUintValue } from "./0uint.mjs";
|
|
7
|
+
import { encodeNegint } from "./1negint.mjs";
|
|
8
|
+
import { encodeBytes } from "./2bytes.mjs";
|
|
9
|
+
import { encodeString } from "./3string.mjs";
|
|
10
|
+
import { encodeArray } from "./4array.mjs";
|
|
11
|
+
import { encodeMap } from "./5map.mjs";
|
|
12
|
+
import { encodeTag } from "./6tag.mjs";
|
|
13
|
+
import { MINOR_FALSE, MINOR_NULL, MINOR_TRUE, MINOR_UNDEFINED, encodeFloat } from "./7float.mjs";
|
|
14
|
+
import { quickEncodeToken } from "./jump.mjs";
|
|
15
|
+
|
|
16
|
+
//#region ../../node_modules/cborg/lib/encode.js
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {import('../interface.js').EncodeOptions} EncodeOptions
|
|
19
|
+
* @typedef {import('../interface.js').OptionalTypeEncoder} OptionalTypeEncoder
|
|
20
|
+
* @typedef {import('../interface.js').Reference} Reference
|
|
21
|
+
* @typedef {import('../interface.js').StrictTypeEncoder} StrictTypeEncoder
|
|
22
|
+
* @typedef {import('../interface.js').TokenTypeEncoder} TokenTypeEncoder
|
|
23
|
+
* @typedef {import('../interface.js').TokenOrNestedTokens} TokenOrNestedTokens
|
|
24
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
25
|
+
*/
|
|
26
|
+
/** @type {EncodeOptions} */
|
|
27
|
+
const defaultEncodeOptions = {
|
|
28
|
+
float64: false,
|
|
29
|
+
mapSorter,
|
|
30
|
+
quickEncodeToken
|
|
31
|
+
};
|
|
32
|
+
/** @type {EncodeOptions} */
|
|
33
|
+
const rfc8949EncodeOptions = Object.freeze({
|
|
34
|
+
float64: true,
|
|
35
|
+
mapSorter: rfc8949MapSorter,
|
|
36
|
+
quickEncodeToken
|
|
37
|
+
});
|
|
38
|
+
/** @returns {TokenTypeEncoder[]} */
|
|
39
|
+
function makeCborEncoders() {
|
|
40
|
+
const encoders = [];
|
|
41
|
+
encoders[Type.uint.major] = encodeUint;
|
|
42
|
+
encoders[Type.negint.major] = encodeNegint;
|
|
43
|
+
encoders[Type.bytes.major] = encodeBytes;
|
|
44
|
+
encoders[Type.string.major] = encodeString;
|
|
45
|
+
encoders[Type.array.major] = encodeArray;
|
|
46
|
+
encoders[Type.map.major] = encodeMap;
|
|
47
|
+
encoders[Type.tag.major] = encodeTag;
|
|
48
|
+
encoders[Type.float.major] = encodeFloat;
|
|
49
|
+
return encoders;
|
|
50
|
+
}
|
|
51
|
+
const cborEncoders = makeCborEncoders();
|
|
52
|
+
const defaultWriter = new Bl();
|
|
53
|
+
/** @implements {Reference} */
|
|
54
|
+
var Ref = class Ref {
|
|
55
|
+
/**
|
|
56
|
+
* @param {object|any[]} obj
|
|
57
|
+
* @param {Reference|undefined} parent
|
|
58
|
+
*/
|
|
59
|
+
constructor(obj, parent) {
|
|
60
|
+
this.obj = obj;
|
|
61
|
+
this.parent = parent;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* @param {object|any[]} obj
|
|
65
|
+
* @returns {boolean}
|
|
66
|
+
*/
|
|
67
|
+
includes(obj) {
|
|
68
|
+
/** @type {Reference|undefined} */
|
|
69
|
+
let p = this;
|
|
70
|
+
do
|
|
71
|
+
if (p.obj === obj) return true;
|
|
72
|
+
while (p = p.parent);
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* @param {Reference|undefined} stack
|
|
77
|
+
* @param {object|any[]} obj
|
|
78
|
+
* @returns {Reference}
|
|
79
|
+
*/
|
|
80
|
+
static createCheck(stack, obj) {
|
|
81
|
+
if (stack && stack.includes(obj)) throw new Error(`${encodeErrPrefix} object contains circular references`);
|
|
82
|
+
return new Ref(obj, stack);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const simpleTokens = {
|
|
86
|
+
null: new Token(Type.null, null),
|
|
87
|
+
undefined: new Token(Type.undefined, void 0),
|
|
88
|
+
true: new Token(Type.true, true),
|
|
89
|
+
false: new Token(Type.false, false),
|
|
90
|
+
emptyArray: new Token(Type.array, 0),
|
|
91
|
+
emptyMap: new Token(Type.map, 0)
|
|
92
|
+
};
|
|
93
|
+
/** @type {{[typeName: string]: StrictTypeEncoder}} */
|
|
94
|
+
const typeEncoders = {
|
|
95
|
+
number(obj, _typ, _options, _refStack) {
|
|
96
|
+
if (!Number.isInteger(obj) || !Number.isSafeInteger(obj)) return new Token(Type.float, obj);
|
|
97
|
+
else if (obj >= 0) return new Token(Type.uint, obj);
|
|
98
|
+
else return new Token(Type.negint, obj);
|
|
99
|
+
},
|
|
100
|
+
bigint(obj, _typ, _options, _refStack) {
|
|
101
|
+
if (obj >= BigInt(0)) return new Token(Type.uint, obj);
|
|
102
|
+
else return new Token(Type.negint, obj);
|
|
103
|
+
},
|
|
104
|
+
Uint8Array(obj, _typ, _options, _refStack) {
|
|
105
|
+
return new Token(Type.bytes, obj);
|
|
106
|
+
},
|
|
107
|
+
string(obj, _typ, _options, _refStack) {
|
|
108
|
+
return new Token(Type.string, obj);
|
|
109
|
+
},
|
|
110
|
+
boolean(obj, _typ, _options, _refStack) {
|
|
111
|
+
return obj ? simpleTokens.true : simpleTokens.false;
|
|
112
|
+
},
|
|
113
|
+
null(_obj, _typ, _options, _refStack) {
|
|
114
|
+
return simpleTokens.null;
|
|
115
|
+
},
|
|
116
|
+
undefined(_obj, _typ, _options, _refStack) {
|
|
117
|
+
return simpleTokens.undefined;
|
|
118
|
+
},
|
|
119
|
+
ArrayBuffer(obj, _typ, _options, _refStack) {
|
|
120
|
+
return new Token(Type.bytes, new Uint8Array(obj));
|
|
121
|
+
},
|
|
122
|
+
DataView(obj, _typ, _options, _refStack) {
|
|
123
|
+
return new Token(Type.bytes, new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength));
|
|
124
|
+
},
|
|
125
|
+
Array(obj, _typ, options, refStack) {
|
|
126
|
+
if (!obj.length) {
|
|
127
|
+
if (options.addBreakTokens === true) return [simpleTokens.emptyArray, new Token(Type.break)];
|
|
128
|
+
return simpleTokens.emptyArray;
|
|
129
|
+
}
|
|
130
|
+
refStack = Ref.createCheck(refStack, obj);
|
|
131
|
+
const entries = [];
|
|
132
|
+
let i = 0;
|
|
133
|
+
for (const e of obj) entries[i++] = objectToTokens(e, options, refStack);
|
|
134
|
+
if (options.addBreakTokens) return [
|
|
135
|
+
new Token(Type.array, obj.length),
|
|
136
|
+
entries,
|
|
137
|
+
new Token(Type.break)
|
|
138
|
+
];
|
|
139
|
+
return [new Token(Type.array, obj.length), entries];
|
|
140
|
+
},
|
|
141
|
+
Object(obj, typ, options, refStack) {
|
|
142
|
+
const isMap = typ !== "Object";
|
|
143
|
+
const keys = isMap ? obj.keys() : Object.keys(obj);
|
|
144
|
+
const maxLength = isMap ? obj.size : keys.length;
|
|
145
|
+
/** @type {undefined | [TokenOrNestedTokens, TokenOrNestedTokens][]} */
|
|
146
|
+
let entries;
|
|
147
|
+
if (maxLength) {
|
|
148
|
+
entries = new Array(maxLength);
|
|
149
|
+
refStack = Ref.createCheck(refStack, obj);
|
|
150
|
+
const skipUndefined = !isMap && options.ignoreUndefinedProperties;
|
|
151
|
+
let i = 0;
|
|
152
|
+
for (const key of keys) {
|
|
153
|
+
const value = isMap ? obj.get(key) : obj[key];
|
|
154
|
+
if (skipUndefined && value === void 0) continue;
|
|
155
|
+
entries[i++] = [objectToTokens(key, options, refStack), objectToTokens(value, options, refStack)];
|
|
156
|
+
}
|
|
157
|
+
if (i < maxLength) entries.length = i;
|
|
158
|
+
}
|
|
159
|
+
if (!entries?.length) {
|
|
160
|
+
if (options.addBreakTokens === true) return [simpleTokens.emptyMap, new Token(Type.break)];
|
|
161
|
+
return simpleTokens.emptyMap;
|
|
162
|
+
}
|
|
163
|
+
sortMapEntries(entries, options);
|
|
164
|
+
if (options.addBreakTokens) return [
|
|
165
|
+
new Token(Type.map, entries.length),
|
|
166
|
+
entries,
|
|
167
|
+
new Token(Type.break)
|
|
168
|
+
];
|
|
169
|
+
return [new Token(Type.map, entries.length), entries];
|
|
170
|
+
},
|
|
171
|
+
Tagged(obj, _typ, options, refStack) {
|
|
172
|
+
return [new Token(Type.tag, obj.tag), objectToTokens(obj.value, options, refStack)];
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
typeEncoders.Map = typeEncoders.Object;
|
|
176
|
+
typeEncoders.Buffer = typeEncoders.Uint8Array;
|
|
177
|
+
for (const typ of "Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt64 Float32 Float64".split(" ")) typeEncoders[`${typ}Array`] = typeEncoders.DataView;
|
|
178
|
+
/**
|
|
179
|
+
* @param {any} obj
|
|
180
|
+
* @param {EncodeOptions} [options]
|
|
181
|
+
* @param {Reference} [refStack]
|
|
182
|
+
* @returns {TokenOrNestedTokens}
|
|
183
|
+
*/
|
|
184
|
+
function objectToTokens(obj, options = {}, refStack) {
|
|
185
|
+
const typ = is(obj);
|
|
186
|
+
const customTypeEncoder = options && options.typeEncoders && options.typeEncoders[typ] || typeEncoders[typ];
|
|
187
|
+
if (typeof customTypeEncoder === "function") {
|
|
188
|
+
const tokens = customTypeEncoder(obj, typ, options, refStack);
|
|
189
|
+
if (tokens != null) return tokens;
|
|
190
|
+
}
|
|
191
|
+
const typeEncoder = typeEncoders[typ];
|
|
192
|
+
if (!typeEncoder) throw new Error(`${encodeErrPrefix} unsupported type: ${typ}`);
|
|
193
|
+
return typeEncoder(obj, typ, options, refStack);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* @param {TokenOrNestedTokens[]} entries
|
|
197
|
+
* @param {EncodeOptions} options
|
|
198
|
+
*/
|
|
199
|
+
function sortMapEntries(entries, options) {
|
|
200
|
+
if (options.mapSorter) entries.sort(options.mapSorter);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* @param {(Token|Token[])[]} e1
|
|
204
|
+
* @param {(Token|Token[])[]} e2
|
|
205
|
+
* @returns {number}
|
|
206
|
+
*/
|
|
207
|
+
function mapSorter(e1, e2) {
|
|
208
|
+
/* c8 ignore next 2 */
|
|
209
|
+
const keyToken1 = Array.isArray(e1[0]) ? e1[0][0] : e1[0];
|
|
210
|
+
const keyToken2 = Array.isArray(e2[0]) ? e2[0][0] : e2[0];
|
|
211
|
+
if (keyToken1.type !== keyToken2.type) return keyToken1.type.compare(keyToken2.type);
|
|
212
|
+
const tcmp = cborEncoders[keyToken1.type.major].compareTokens(keyToken1, keyToken2);
|
|
213
|
+
/* c8 ignore next 5 */
|
|
214
|
+
if (tcmp === 0) console.warn("WARNING: complex key types used, CBOR key sorting guarantees are gone");
|
|
215
|
+
return tcmp;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* @typedef {Token & { _keyBytes?: Uint8Array }} TokenEx
|
|
219
|
+
*
|
|
220
|
+
* @param {(Token|Token[])[]} e1
|
|
221
|
+
* @param {(Token|Token[])[]} e2
|
|
222
|
+
* @returns {number}
|
|
223
|
+
*/
|
|
224
|
+
function rfc8949MapSorter(e1, e2) {
|
|
225
|
+
if (e1[0] instanceof Token && e2[0] instanceof Token) {
|
|
226
|
+
const t1 = e1[0];
|
|
227
|
+
const t2 = e2[0];
|
|
228
|
+
if (!t1._keyBytes) t1._keyBytes = encodeRfc8949(t1.value);
|
|
229
|
+
if (!t2._keyBytes) t2._keyBytes = encodeRfc8949(t2.value);
|
|
230
|
+
return compare(t1._keyBytes, t2._keyBytes);
|
|
231
|
+
}
|
|
232
|
+
throw new Error("rfc8949MapSorter: complex key types are not supported yet");
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* @param {any} data
|
|
236
|
+
* @returns {Uint8Array}
|
|
237
|
+
*/
|
|
238
|
+
function encodeRfc8949(data) {
|
|
239
|
+
return encodeCustom(data, cborEncoders, rfc8949EncodeOptions);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* @param {ByteWriter} writer
|
|
243
|
+
* @param {TokenOrNestedTokens} tokens
|
|
244
|
+
* @param {TokenTypeEncoder[]} encoders
|
|
245
|
+
* @param {EncodeOptions} options
|
|
246
|
+
*/
|
|
247
|
+
function tokensToEncoded(writer, tokens, encoders, options) {
|
|
248
|
+
if (Array.isArray(tokens)) for (const token of tokens) tokensToEncoded(writer, token, encoders, options);
|
|
249
|
+
else encoders[tokens.type.major](writer, tokens, options);
|
|
250
|
+
}
|
|
251
|
+
const MAJOR_UINT = Type.uint.majorEncoded;
|
|
252
|
+
const MAJOR_NEGINT = Type.negint.majorEncoded;
|
|
253
|
+
const MAJOR_BYTES = Type.bytes.majorEncoded;
|
|
254
|
+
const MAJOR_STRING = Type.string.majorEncoded;
|
|
255
|
+
const MAJOR_ARRAY = Type.array.majorEncoded;
|
|
256
|
+
const SIMPLE_FALSE = Type.float.majorEncoded | MINOR_FALSE;
|
|
257
|
+
const SIMPLE_TRUE = Type.float.majorEncoded | MINOR_TRUE;
|
|
258
|
+
const SIMPLE_NULL = Type.float.majorEncoded | MINOR_NULL;
|
|
259
|
+
const SIMPLE_UNDEFINED = Type.float.majorEncoded | MINOR_UNDEFINED;
|
|
260
|
+
const neg1b = BigInt(-1);
|
|
261
|
+
const pos1b = BigInt(1);
|
|
262
|
+
/**
|
|
263
|
+
* Check if direct encoding can be used for the given options.
|
|
264
|
+
* Direct encoding bypasses token creation for most values.
|
|
265
|
+
* @param {EncodeOptions} options
|
|
266
|
+
* @returns {boolean}
|
|
267
|
+
*/
|
|
268
|
+
function canDirectEncode(options) {
|
|
269
|
+
return options.addBreakTokens !== true;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Direct encode a value to the writer, bypassing token creation for most types.
|
|
273
|
+
* Falls back to token-based encoding for custom type encoders.
|
|
274
|
+
* @param {ByteWriter} writer
|
|
275
|
+
* @param {any} data
|
|
276
|
+
* @param {EncodeOptions} options
|
|
277
|
+
* @param {Reference|undefined} refStack
|
|
278
|
+
*/
|
|
279
|
+
function directEncode(writer, data, options, refStack) {
|
|
280
|
+
const typ = is(data);
|
|
281
|
+
const customEncoder = options.typeEncoders && options.typeEncoders[typ];
|
|
282
|
+
if (customEncoder) {
|
|
283
|
+
const tokens = customEncoder(data, typ, options, refStack);
|
|
284
|
+
if (tokens != null) {
|
|
285
|
+
tokensToEncoded(writer, tokens, cborEncoders, options);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
switch (typ) {
|
|
290
|
+
case "null":
|
|
291
|
+
writer.push([SIMPLE_NULL]);
|
|
292
|
+
return;
|
|
293
|
+
case "undefined":
|
|
294
|
+
writer.push([SIMPLE_UNDEFINED]);
|
|
295
|
+
return;
|
|
296
|
+
case "boolean":
|
|
297
|
+
writer.push([data ? SIMPLE_TRUE : SIMPLE_FALSE]);
|
|
298
|
+
return;
|
|
299
|
+
case "number":
|
|
300
|
+
if (!Number.isInteger(data) || !Number.isSafeInteger(data)) encodeFloat(writer, new Token(Type.float, data), options);
|
|
301
|
+
else if (data >= 0) encodeUintValue(writer, MAJOR_UINT, data);
|
|
302
|
+
else encodeUintValue(writer, MAJOR_NEGINT, data * -1 - 1);
|
|
303
|
+
return;
|
|
304
|
+
case "bigint":
|
|
305
|
+
if (data >= BigInt(0)) encodeUintValue(writer, MAJOR_UINT, data);
|
|
306
|
+
else encodeUintValue(writer, MAJOR_NEGINT, data * neg1b - pos1b);
|
|
307
|
+
return;
|
|
308
|
+
case "string": {
|
|
309
|
+
const bytes = fromString(data);
|
|
310
|
+
encodeUintValue(writer, MAJOR_STRING, bytes.length);
|
|
311
|
+
writer.push(bytes);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
case "Uint8Array":
|
|
315
|
+
encodeUintValue(writer, MAJOR_BYTES, data.length);
|
|
316
|
+
writer.push(data);
|
|
317
|
+
return;
|
|
318
|
+
case "Array":
|
|
319
|
+
if (!data.length) {
|
|
320
|
+
writer.push([MAJOR_ARRAY]);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
refStack = Ref.createCheck(refStack, data);
|
|
324
|
+
encodeUintValue(writer, MAJOR_ARRAY, data.length);
|
|
325
|
+
for (const elem of data) directEncode(writer, elem, options, refStack);
|
|
326
|
+
return;
|
|
327
|
+
case "Object":
|
|
328
|
+
case "Map":
|
|
329
|
+
tokensToEncoded(writer, typeEncoders.Object(data, typ, options, refStack), cborEncoders, options);
|
|
330
|
+
return;
|
|
331
|
+
default: {
|
|
332
|
+
const typeEncoder = typeEncoders[typ];
|
|
333
|
+
if (!typeEncoder) throw new Error(`${encodeErrPrefix} unsupported type: ${typ}`);
|
|
334
|
+
tokensToEncoded(writer, typeEncoder(data, typ, options, refStack), cborEncoders, options);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* @param {any} data
|
|
340
|
+
* @param {TokenTypeEncoder[]} encoders
|
|
341
|
+
* @param {EncodeOptions} options
|
|
342
|
+
* @param {Uint8Array} [destination]
|
|
343
|
+
* @returns {Uint8Array}
|
|
344
|
+
*/
|
|
345
|
+
function encodeCustom(data, encoders, options, destination) {
|
|
346
|
+
const hasDest = destination instanceof Uint8Array;
|
|
347
|
+
let writeTo = hasDest ? new U8Bl(destination) : defaultWriter;
|
|
348
|
+
const tokens = objectToTokens(data, options);
|
|
349
|
+
if (!Array.isArray(tokens) && options.quickEncodeToken) {
|
|
350
|
+
const quickBytes = options.quickEncodeToken(tokens);
|
|
351
|
+
if (quickBytes) {
|
|
352
|
+
if (hasDest) {
|
|
353
|
+
writeTo.push(quickBytes);
|
|
354
|
+
return writeTo.toBytes();
|
|
355
|
+
}
|
|
356
|
+
return quickBytes;
|
|
357
|
+
}
|
|
358
|
+
const encoder = encoders[tokens.type.major];
|
|
359
|
+
if (encoder.encodedSize) {
|
|
360
|
+
const size = encoder.encodedSize(tokens, options);
|
|
361
|
+
if (!hasDest) writeTo = new Bl(size);
|
|
362
|
+
encoder(writeTo, tokens, options);
|
|
363
|
+
/* c8 ignore next 4 */
|
|
364
|
+
if (writeTo.chunks.length !== 1) throw new Error(`Unexpected error: pre-calculated length for ${tokens} was wrong`);
|
|
365
|
+
return hasDest ? writeTo.toBytes() : asU8A(writeTo.chunks[0]);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
writeTo.reset();
|
|
369
|
+
tokensToEncoded(writeTo, tokens, encoders, options);
|
|
370
|
+
return writeTo.toBytes(true);
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* @param {any} data
|
|
374
|
+
* @param {EncodeOptions} [options]
|
|
375
|
+
* @returns {Uint8Array}
|
|
376
|
+
*/
|
|
377
|
+
function encode(data, options) {
|
|
378
|
+
options = Object.assign({}, defaultEncodeOptions, options);
|
|
379
|
+
if (canDirectEncode(options)) {
|
|
380
|
+
defaultWriter.reset();
|
|
381
|
+
directEncode(defaultWriter, data, options, void 0);
|
|
382
|
+
return defaultWriter.toBytes(true);
|
|
383
|
+
}
|
|
384
|
+
return encodeCustom(data, cborEncoders, options);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
//#endregion
|
|
388
|
+
export { encode, objectToTokens, rfc8949EncodeOptions };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region ../../node_modules/cborg/lib/is.js
|
|
2
|
+
const objectTypeNames = [
|
|
3
|
+
"Object",
|
|
4
|
+
"RegExp",
|
|
5
|
+
"Date",
|
|
6
|
+
"Error",
|
|
7
|
+
"Map",
|
|
8
|
+
"Set",
|
|
9
|
+
"WeakMap",
|
|
10
|
+
"WeakSet",
|
|
11
|
+
"ArrayBuffer",
|
|
12
|
+
"SharedArrayBuffer",
|
|
13
|
+
"DataView",
|
|
14
|
+
"Promise",
|
|
15
|
+
"URL",
|
|
16
|
+
"HTMLElement",
|
|
17
|
+
"Int8Array",
|
|
18
|
+
"Uint8ClampedArray",
|
|
19
|
+
"Int16Array",
|
|
20
|
+
"Uint16Array",
|
|
21
|
+
"Int32Array",
|
|
22
|
+
"Uint32Array",
|
|
23
|
+
"Float32Array",
|
|
24
|
+
"Float64Array",
|
|
25
|
+
"BigInt64Array",
|
|
26
|
+
"BigUint64Array",
|
|
27
|
+
"Tagged"
|
|
28
|
+
];
|
|
29
|
+
/**
|
|
30
|
+
* @param {any} value
|
|
31
|
+
* @returns {string}
|
|
32
|
+
*/
|
|
33
|
+
function is(value) {
|
|
34
|
+
if (value === null) return "null";
|
|
35
|
+
if (value === void 0) return "undefined";
|
|
36
|
+
if (value === true || value === false) return "boolean";
|
|
37
|
+
const typeOf = typeof value;
|
|
38
|
+
if (typeOf === "string" || typeOf === "number" || typeOf === "bigint" || typeOf === "symbol") return typeOf;
|
|
39
|
+
/* c8 ignore next 3 */
|
|
40
|
+
if (typeOf === "function") return "Function";
|
|
41
|
+
if (Array.isArray(value)) return "Array";
|
|
42
|
+
if (value instanceof Uint8Array) return "Uint8Array";
|
|
43
|
+
if (value.constructor === Object) return "Object";
|
|
44
|
+
const objectType = getObjectType(value);
|
|
45
|
+
if (objectType) return objectType;
|
|
46
|
+
/* c8 ignore next */
|
|
47
|
+
return "Object";
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* @param {any} value
|
|
51
|
+
* @returns {string|undefined}
|
|
52
|
+
*/
|
|
53
|
+
function getObjectType(value) {
|
|
54
|
+
const objectTypeName = Object.prototype.toString.call(value).slice(8, -1);
|
|
55
|
+
if (objectTypeNames.includes(objectTypeName)) return objectTypeName;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
export { is };
|