@pkcprotocol/pkc-js 0.0.141 → 0.0.143
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/README.md +1 -0
- package/dist/browser/clients/base-client-manager.js +6 -1
- package/dist/browser/clients/base-client-manager.js.map +1 -1
- package/dist/browser/clients/rpc-client/pkc-rpc-client.js +15 -8
- package/dist/browser/clients/rpc-client/pkc-rpc-client.js.map +1 -1
- package/dist/browser/clients/rpc-client/rpc-schema-util.d.ts +2 -0
- package/dist/browser/clients/rpc-client/schema.d.ts +2 -0
- package/dist/browser/community/rpc-local-community.d.ts +12 -1
- package/dist/browser/community/rpc-local-community.js +133 -10
- package/dist/browser/community/rpc-local-community.js.map +1 -1
- package/dist/browser/community/schema.d.ts +19 -0
- package/dist/browser/community/schema.js +7 -3
- package/dist/browser/community/schema.js.map +1 -1
- package/dist/browser/generated-version.d.ts +1 -1
- package/dist/browser/generated-version.js +1 -1
- package/dist/browser/helia/helia-for-pkc.js +18 -1
- package/dist/browser/helia/helia-for-pkc.js.map +1 -1
- package/dist/browser/index-with-compile-cache.d.ts +2 -0
- package/dist/browser/index.d.ts +4 -0
- package/dist/browser/object-key-util.js +5 -1
- package/dist/browser/object-key-util.js.map +1 -1
- package/dist/browser/pages/pages-client-manager.d.ts +1 -0
- package/dist/browser/pages/pages-client-manager.js +22 -5
- package/dist/browser/pages/pages-client-manager.js.map +1 -1
- package/dist/browser/pages/pkc-js-page-sorts/top.js +2 -1
- package/dist/browser/pages/pkc-js-page-sorts/top.js.map +1 -1
- package/dist/browser/pages/schema.js +3 -1
- package/dist/browser/pages/schema.js.map +1 -1
- package/dist/browser/pkc/pkc-with-rpc-client.js +9 -5
- package/dist/browser/pkc/pkc-with-rpc-client.js.map +1 -1
- package/dist/browser/pkc/pkc.d.ts +4 -1
- package/dist/browser/pkc/pkc.js +6 -5
- package/dist/browser/pkc/pkc.js.map +1 -1
- package/dist/browser/publications/comment/comment-client-manager.js +11 -11
- package/dist/browser/publications/comment/comment-client-manager.js.map +1 -1
- package/dist/browser/publications/comment/comment.js +2 -0
- package/dist/browser/publications/comment/comment.js.map +1 -1
- package/dist/browser/publications/community-edit/schema.d.ts +8 -0
- package/dist/browser/publications/publication-client-manager.js +7 -1
- package/dist/browser/publications/publication-client-manager.js.map +1 -1
- package/dist/browser/publications/publication.d.ts +7 -0
- package/dist/browser/publications/publication.js +88 -24
- package/dist/browser/publications/publication.js.map +1 -1
- package/dist/browser/pubsub-messages/schema.d.ts +4 -0
- package/dist/browser/rpc/src/lib/pkc-js/index.d.ts +2 -0
- package/dist/browser/rpc/src/schema.d.ts +12 -0
- package/dist/browser/runtime/node/community/db-handler.d.ts +3 -0
- package/dist/browser/runtime/node/community/db-handler.js +22 -4
- package/dist/browser/runtime/node/community/db-handler.js.map +1 -1
- package/dist/browser/runtime/node/community/local-community/comment-updates.d.ts +1 -0
- package/dist/browser/runtime/node/community/local-community/comment-updates.js +70 -19
- package/dist/browser/runtime/node/community/local-community/comment-updates.js.map +1 -1
- package/dist/browser/runtime/node/community/local-community/post-updates-bucket.d.ts +14 -0
- package/dist/browser/runtime/node/community/local-community/post-updates-bucket.js +206 -0
- package/dist/browser/runtime/node/community/local-community/post-updates-bucket.js.map +1 -0
- package/dist/browser/runtime/node/community/local-community.d.ts +1 -0
- package/dist/browser/runtime/node/community/local-community.js +4 -0
- package/dist/browser/runtime/node/community/local-community.js.map +1 -1
- package/dist/browser/runtime/node/community/page-sorts/index.js +10 -10
- package/dist/browser/runtime/node/community/page-sorts/index.js.map +1 -1
- package/dist/browser/schema/schema-util.d.ts +8 -0
- package/dist/browser/schema.d.ts +16 -0
- package/dist/browser/test/mock-ipfs-client.js +10 -4
- package/dist/browser/test/mock-ipfs-client.js.map +1 -1
- package/dist/browser/util.d.ts +5 -0
- package/dist/browser/util.js +52 -5
- package/dist/browser/util.js.map +1 -1
- package/dist/bundled/bundle-manifest.json +165 -123
- package/dist/bundled/challenges.js +1 -1
- package/dist/bundled/chunks/{helia-for-pkc-C3zShnXN.js → helia-for-pkc-DpsMZzv8.js} +41 -34
- package/dist/bundled/chunks/{local-community-PFaUvMFo.js → local-community-BglT8AUy.js} +284 -47
- package/dist/bundled/chunks/{local-community-DMklqSLp.js → local-community-DEaDtekb.js} +1 -1
- package/dist/bundled/chunks/{node-CGmAA0az.js → node-Tkx_TCqE.js} +84 -34
- package/dist/bundled/chunks/{page-sort-scoring-Bz65Z28_.js → page-sort-scoring-DY7SvqJA.js} +1 -1
- package/dist/bundled/chunks/{rpc-local-community-Dq_6nnU1.js → rpc-local-community-Br_xomgn.js} +128 -20
- package/dist/bundled/chunks/src-ABfmT8J2.js +2323 -0
- package/dist/bundled/chunks/src-BC60-Q7v.js +2 -0
- package/dist/bundled/chunks/src-BHqirG63.js +2 -0
- package/dist/bundled/chunks/{src-BggJ_InF.js → src-Bj5iE3qB.js} +2 -1999
- package/dist/bundled/chunks/src-CNab3O7M.js +2001 -0
- package/dist/bundled/chunks/{src-BsiPSkhv.js → src-ltPxiZKf.js} +1312 -3632
- package/dist/bundled/chunks/{util-C6YaxADn.js → util-C5RiQF_G.js} +59 -10
- package/dist/bundled/chunks/{util-CzoLr9RY.js → util-DIkZirPy.js} +1 -1
- package/dist/bundled/chunks/{validate-package-factories-DMzx5r6y.js → validate-package-factories-DnW2JiL5.js} +2 -2
- package/dist/bundled/index.js +1 -1
- package/dist/bundled/page-sorts.js +1 -1
- package/dist/bundled/rpc/src/index.js +4 -4
- package/dist/node/clients/base-client-manager.js +6 -1
- package/dist/node/clients/base-client-manager.js.map +1 -1
- package/dist/node/clients/rpc-client/pkc-rpc-client.js +15 -8
- package/dist/node/clients/rpc-client/pkc-rpc-client.js.map +1 -1
- package/dist/node/clients/rpc-client/rpc-schema-util.d.ts +2 -0
- package/dist/node/clients/rpc-client/schema.d.ts +2 -0
- package/dist/node/community/rpc-local-community.d.ts +12 -1
- package/dist/node/community/rpc-local-community.js +133 -10
- package/dist/node/community/rpc-local-community.js.map +1 -1
- package/dist/node/community/schema.d.ts +19 -0
- package/dist/node/community/schema.js +7 -3
- package/dist/node/community/schema.js.map +1 -1
- package/dist/node/generated-version.d.ts +1 -1
- package/dist/node/generated-version.js +1 -1
- package/dist/node/helia/helia-for-pkc.js +18 -1
- package/dist/node/helia/helia-for-pkc.js.map +1 -1
- package/dist/node/index-with-compile-cache.d.ts +2 -0
- package/dist/node/index.d.ts +4 -0
- package/dist/node/object-key-util.js +5 -1
- package/dist/node/object-key-util.js.map +1 -1
- package/dist/node/pages/pages-client-manager.d.ts +1 -0
- package/dist/node/pages/pages-client-manager.js +22 -5
- package/dist/node/pages/pages-client-manager.js.map +1 -1
- package/dist/node/pages/pkc-js-page-sorts/top.js +2 -1
- package/dist/node/pages/pkc-js-page-sorts/top.js.map +1 -1
- package/dist/node/pages/schema.js +3 -1
- package/dist/node/pages/schema.js.map +1 -1
- package/dist/node/pkc/pkc-with-rpc-client.js +9 -5
- package/dist/node/pkc/pkc-with-rpc-client.js.map +1 -1
- package/dist/node/pkc/pkc.d.ts +4 -1
- package/dist/node/pkc/pkc.js +6 -5
- package/dist/node/pkc/pkc.js.map +1 -1
- package/dist/node/publications/comment/comment-client-manager.js +11 -11
- package/dist/node/publications/comment/comment-client-manager.js.map +1 -1
- package/dist/node/publications/comment/comment.js +2 -0
- package/dist/node/publications/comment/comment.js.map +1 -1
- package/dist/node/publications/community-edit/schema.d.ts +8 -0
- package/dist/node/publications/publication-client-manager.js +7 -1
- package/dist/node/publications/publication-client-manager.js.map +1 -1
- package/dist/node/publications/publication.d.ts +7 -0
- package/dist/node/publications/publication.js +88 -24
- package/dist/node/publications/publication.js.map +1 -1
- package/dist/node/pubsub-messages/schema.d.ts +4 -0
- package/dist/node/rpc/src/lib/pkc-js/index.d.ts +2 -0
- package/dist/node/rpc/src/schema.d.ts +12 -0
- package/dist/node/runtime/node/community/db-handler.d.ts +3 -0
- package/dist/node/runtime/node/community/db-handler.js +22 -4
- package/dist/node/runtime/node/community/db-handler.js.map +1 -1
- package/dist/node/runtime/node/community/local-community/comment-updates.d.ts +1 -0
- package/dist/node/runtime/node/community/local-community/comment-updates.js +70 -19
- package/dist/node/runtime/node/community/local-community/comment-updates.js.map +1 -1
- package/dist/node/runtime/node/community/local-community/post-updates-bucket.d.ts +14 -0
- package/dist/node/runtime/node/community/local-community/post-updates-bucket.js +206 -0
- package/dist/node/runtime/node/community/local-community/post-updates-bucket.js.map +1 -0
- package/dist/node/runtime/node/community/local-community.d.ts +1 -0
- package/dist/node/runtime/node/community/local-community.js +4 -0
- package/dist/node/runtime/node/community/local-community.js.map +1 -1
- package/dist/node/runtime/node/community/page-sorts/index.js +10 -10
- package/dist/node/runtime/node/community/page-sorts/index.js.map +1 -1
- package/dist/node/schema/schema-util.d.ts +8 -0
- package/dist/node/schema.d.ts +16 -0
- package/dist/node/test/mock-ipfs-client.js +10 -4
- package/dist/node/test/mock-ipfs-client.js.map +1 -1
- package/dist/node/util.d.ts +5 -0
- package/dist/node/util.js +52 -5
- package/dist/node/util.js.map +1 -1
- package/package.json +4 -1
|
@@ -0,0 +1,2323 @@
|
|
|
1
|
+
import { CID } from "multiformats/cid";
|
|
2
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/is.js
|
|
3
|
+
const objectTypeNames = [
|
|
4
|
+
"Object",
|
|
5
|
+
"RegExp",
|
|
6
|
+
"Date",
|
|
7
|
+
"Error",
|
|
8
|
+
"Map",
|
|
9
|
+
"Set",
|
|
10
|
+
"WeakMap",
|
|
11
|
+
"WeakSet",
|
|
12
|
+
"ArrayBuffer",
|
|
13
|
+
"SharedArrayBuffer",
|
|
14
|
+
"DataView",
|
|
15
|
+
"Promise",
|
|
16
|
+
"URL",
|
|
17
|
+
"HTMLElement",
|
|
18
|
+
"Int8Array",
|
|
19
|
+
"Uint8ClampedArray",
|
|
20
|
+
"Int16Array",
|
|
21
|
+
"Uint16Array",
|
|
22
|
+
"Int32Array",
|
|
23
|
+
"Uint32Array",
|
|
24
|
+
"Float32Array",
|
|
25
|
+
"Float64Array",
|
|
26
|
+
"BigInt64Array",
|
|
27
|
+
"BigUint64Array",
|
|
28
|
+
"Tagged"
|
|
29
|
+
];
|
|
30
|
+
/**
|
|
31
|
+
* @param {any} value
|
|
32
|
+
* @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
function is(value) {
|
|
35
|
+
if (value === null) return "null";
|
|
36
|
+
if (value === void 0) return "undefined";
|
|
37
|
+
if (value === true || value === false) return "boolean";
|
|
38
|
+
const typeOf = typeof value;
|
|
39
|
+
if (typeOf === "string" || typeOf === "number" || typeOf === "bigint" || typeOf === "symbol") return typeOf;
|
|
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
|
+
return "Object";
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @param {any} value
|
|
50
|
+
* @returns {string|undefined}
|
|
51
|
+
*/
|
|
52
|
+
function getObjectType(value) {
|
|
53
|
+
const objectTypeName = Object.prototype.toString.call(value).slice(8, -1);
|
|
54
|
+
if (objectTypeNames.includes(objectTypeName)) return objectTypeName;
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/token.js
|
|
58
|
+
var Type = class {
|
|
59
|
+
/**
|
|
60
|
+
* @param {number} major
|
|
61
|
+
* @param {string} name
|
|
62
|
+
* @param {boolean} terminal
|
|
63
|
+
*/
|
|
64
|
+
constructor(major, name, terminal) {
|
|
65
|
+
this.major = major;
|
|
66
|
+
this.majorEncoded = major << 5;
|
|
67
|
+
this.name = name;
|
|
68
|
+
this.terminal = terminal;
|
|
69
|
+
}
|
|
70
|
+
toString() {
|
|
71
|
+
return `Type[${this.major}].${this.name}`;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @param {Type} typ
|
|
75
|
+
* @returns {number}
|
|
76
|
+
*/
|
|
77
|
+
compare(typ) {
|
|
78
|
+
return this.major < typ.major ? -1 : this.major > typ.major ? 1 : 0;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Check equality between two Type instances. Safe to use across different
|
|
82
|
+
* copies of the Type class (e.g., when bundlers duplicate the module).
|
|
83
|
+
* (major, name) uniquely identifies a Type; terminal is implied by these.
|
|
84
|
+
* @param {Type} a
|
|
85
|
+
* @param {Type} b
|
|
86
|
+
* @returns {boolean}
|
|
87
|
+
*/
|
|
88
|
+
static equals(a, b) {
|
|
89
|
+
return a === b || a.major === b.major && a.name === b.name;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
Type.uint = new Type(0, "uint", true);
|
|
93
|
+
Type.negint = new Type(1, "negint", true);
|
|
94
|
+
Type.bytes = new Type(2, "bytes", true);
|
|
95
|
+
Type.string = new Type(3, "string", true);
|
|
96
|
+
Type.array = new Type(4, "array", false);
|
|
97
|
+
Type.map = new Type(5, "map", false);
|
|
98
|
+
Type.tag = new Type(6, "tag", false);
|
|
99
|
+
Type.float = new Type(7, "float", true);
|
|
100
|
+
Type.false = new Type(7, "false", true);
|
|
101
|
+
Type.true = new Type(7, "true", true);
|
|
102
|
+
Type.null = new Type(7, "null", true);
|
|
103
|
+
Type.undefined = new Type(7, "undefined", true);
|
|
104
|
+
Type.break = new Type(7, "break", true);
|
|
105
|
+
var Token = class {
|
|
106
|
+
/**
|
|
107
|
+
* @param {Type} type
|
|
108
|
+
* @param {any} [value]
|
|
109
|
+
* @param {number} [encodedLength]
|
|
110
|
+
*/
|
|
111
|
+
constructor(type, value, encodedLength) {
|
|
112
|
+
this.type = type;
|
|
113
|
+
this.value = value;
|
|
114
|
+
this.encodedLength = encodedLength;
|
|
115
|
+
/** @type {Uint8Array|undefined} */
|
|
116
|
+
this.encodedBytes = void 0;
|
|
117
|
+
/** @type {Uint8Array|undefined} */
|
|
118
|
+
this.byteValue = void 0;
|
|
119
|
+
}
|
|
120
|
+
toString() {
|
|
121
|
+
return `Token[${this.type}].${this.value}`;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/byte-utils.js
|
|
126
|
+
const useBuffer = globalThis.process && !globalThis.process.browser && globalThis.Buffer && typeof globalThis.Buffer.isBuffer === "function";
|
|
127
|
+
const textEncoder = new TextEncoder();
|
|
128
|
+
/**
|
|
129
|
+
* @typedef {import('../interface.js').ByteView} ByteView
|
|
130
|
+
* @typedef {import('../interface.js').AllocatedByteView} AllocatedByteView
|
|
131
|
+
*/
|
|
132
|
+
/**
|
|
133
|
+
* @param {Uint8Array} buf
|
|
134
|
+
* @returns {boolean}
|
|
135
|
+
*/
|
|
136
|
+
function isBuffer(buf) {
|
|
137
|
+
return useBuffer && globalThis.Buffer.isBuffer(buf);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* @param {ByteView|number[]} buf
|
|
141
|
+
* @returns {ByteView}
|
|
142
|
+
*/
|
|
143
|
+
function asU8A(buf) {
|
|
144
|
+
if (!(buf instanceof Uint8Array)) return Uint8Array.from(buf);
|
|
145
|
+
return isBuffer(buf) ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : buf;
|
|
146
|
+
}
|
|
147
|
+
const FROM_STRING_THRESHOLD_BUFFER = 24;
|
|
148
|
+
const FROM_STRING_THRESHOLD_TEXTENCODER = 200;
|
|
149
|
+
const fromString = useBuffer ? (string) => {
|
|
150
|
+
return string.length >= FROM_STRING_THRESHOLD_BUFFER ? globalThis.Buffer.from(string) : utf8ToBytes(string);
|
|
151
|
+
} :
|
|
152
|
+
/**
|
|
153
|
+
* @param {string} string
|
|
154
|
+
*/
|
|
155
|
+
(string) => {
|
|
156
|
+
return string.length >= FROM_STRING_THRESHOLD_TEXTENCODER ? textEncoder.encode(string) : utf8ToBytes(string);
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Buffer variant not fast enough for what we need
|
|
160
|
+
* @param {number[]} arr
|
|
161
|
+
* @returns {AllocatedByteView}
|
|
162
|
+
*/
|
|
163
|
+
const fromArray = (arr) => {
|
|
164
|
+
return Uint8Array.from(arr);
|
|
165
|
+
};
|
|
166
|
+
const slice = useBuffer ? (bytes, start, end) => {
|
|
167
|
+
if (isBuffer(bytes)) return new Uint8Array(bytes.subarray(start, end));
|
|
168
|
+
return bytes.slice(start, end);
|
|
169
|
+
} :
|
|
170
|
+
/**
|
|
171
|
+
* @param {Uint8Array} bytes
|
|
172
|
+
* @param {number} start
|
|
173
|
+
* @param {number} end
|
|
174
|
+
*/
|
|
175
|
+
(bytes, start, end) => {
|
|
176
|
+
return bytes.slice(start, end);
|
|
177
|
+
};
|
|
178
|
+
const concat = useBuffer ? (chunks, length) => {
|
|
179
|
+
chunks = chunks.map((c) => c instanceof Uint8Array ? c : globalThis.Buffer.from(c));
|
|
180
|
+
return asU8A(globalThis.Buffer.concat(chunks, length));
|
|
181
|
+
} :
|
|
182
|
+
/**
|
|
183
|
+
* @param {ByteView[]} chunks
|
|
184
|
+
* @param {number} length
|
|
185
|
+
* @returns {AllocatedByteView}
|
|
186
|
+
*/
|
|
187
|
+
(chunks, length) => {
|
|
188
|
+
const out = new Uint8Array(length);
|
|
189
|
+
let off = 0;
|
|
190
|
+
for (let b of chunks) {
|
|
191
|
+
if (off + b.length > out.length) b = b.subarray(0, out.length - off);
|
|
192
|
+
out.set(b, off);
|
|
193
|
+
off += b.length;
|
|
194
|
+
}
|
|
195
|
+
return out;
|
|
196
|
+
};
|
|
197
|
+
const alloc = useBuffer ? (size) => {
|
|
198
|
+
return globalThis.Buffer.allocUnsafe(size);
|
|
199
|
+
} :
|
|
200
|
+
/**
|
|
201
|
+
* @param {number} size
|
|
202
|
+
* @returns {AllocatedByteView}
|
|
203
|
+
*/
|
|
204
|
+
(size) => {
|
|
205
|
+
return new Uint8Array(size);
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
* @param {Uint8Array} b1
|
|
209
|
+
* @param {Uint8Array} b2
|
|
210
|
+
* @returns {number}
|
|
211
|
+
*/
|
|
212
|
+
function compare(b1, b2) {
|
|
213
|
+
if (isBuffer(b1) && isBuffer(b2)) return b1.compare(b2);
|
|
214
|
+
for (let i = 0; i < b1.length; i++) {
|
|
215
|
+
if (b1[i] === b2[i]) continue;
|
|
216
|
+
return b1[i] < b2[i] ? -1 : 1;
|
|
217
|
+
}
|
|
218
|
+
return 0;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* @param {string} str
|
|
222
|
+
* @returns {number[]}
|
|
223
|
+
*/
|
|
224
|
+
function utf8ToBytes(str) {
|
|
225
|
+
const out = [];
|
|
226
|
+
let p = 0;
|
|
227
|
+
for (let i = 0; i < str.length; i++) {
|
|
228
|
+
let c = str.charCodeAt(i);
|
|
229
|
+
if (c < 128) out[p++] = c;
|
|
230
|
+
else if (c < 2048) {
|
|
231
|
+
out[p++] = c >> 6 | 192;
|
|
232
|
+
out[p++] = c & 63 | 128;
|
|
233
|
+
} else if ((c & 64512) === 55296 && i + 1 < str.length && (str.charCodeAt(i + 1) & 64512) === 56320) {
|
|
234
|
+
c = 65536 + ((c & 1023) << 10) + (str.charCodeAt(++i) & 1023);
|
|
235
|
+
out[p++] = c >> 18 | 240;
|
|
236
|
+
out[p++] = c >> 12 & 63 | 128;
|
|
237
|
+
out[p++] = c >> 6 & 63 | 128;
|
|
238
|
+
out[p++] = c & 63 | 128;
|
|
239
|
+
} else {
|
|
240
|
+
if (c >= 55296 && c <= 57343) c = 65533;
|
|
241
|
+
out[p++] = c >> 12 | 224;
|
|
242
|
+
out[p++] = c >> 6 & 63 | 128;
|
|
243
|
+
out[p++] = c & 63 | 128;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
//#endregion
|
|
249
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/bl.js
|
|
250
|
+
/**
|
|
251
|
+
* Bl is a list of byte chunks, similar to https://github.com/rvagg/bl but for
|
|
252
|
+
* writing rather than reading.
|
|
253
|
+
* A Bl object accepts set() operations for individual bytes and copyTo() for
|
|
254
|
+
* inserting byte arrays. These write operations don't automatically increment
|
|
255
|
+
* the internal cursor so its "length" won't be changed. Instead, increment()
|
|
256
|
+
* must be called to extend its length to cover the inserted data.
|
|
257
|
+
* The toBytes() call will convert all internal memory to a single Uint8Array of
|
|
258
|
+
* the correct length, truncating any data that is stored but hasn't been
|
|
259
|
+
* included by an increment().
|
|
260
|
+
* get() can retrieve a single byte.
|
|
261
|
+
* All operations (except toBytes()) take an "offset" argument that will perform
|
|
262
|
+
* the write at the offset _from the current cursor_. For most operations this
|
|
263
|
+
* will be `0` to write at the current cursor position but it can be ahead of
|
|
264
|
+
* the current cursor. Negative offsets probably work but are untested.
|
|
265
|
+
*/
|
|
266
|
+
/**
|
|
267
|
+
* @typedef {import('../interface.js').ByteView} ByteView
|
|
268
|
+
* @typedef {import('../interface.js').AllocatedByteView} AllocatedByteView
|
|
269
|
+
*/
|
|
270
|
+
const defaultChunkSize = 256;
|
|
271
|
+
var Bl = class {
|
|
272
|
+
/**
|
|
273
|
+
* @param {number} [chunkSize]
|
|
274
|
+
*/
|
|
275
|
+
constructor(chunkSize = defaultChunkSize) {
|
|
276
|
+
this.chunkSize = chunkSize;
|
|
277
|
+
/** @type {number} */
|
|
278
|
+
this.cursor = 0;
|
|
279
|
+
/** @type {number} */
|
|
280
|
+
this.maxCursor = -1;
|
|
281
|
+
/** @type {(ByteView|number[])[]} */
|
|
282
|
+
this.chunks = [];
|
|
283
|
+
/** @type {ByteView|number[]|null} */
|
|
284
|
+
this._initReuseChunk = null;
|
|
285
|
+
}
|
|
286
|
+
reset() {
|
|
287
|
+
this.cursor = 0;
|
|
288
|
+
this.maxCursor = -1;
|
|
289
|
+
if (this.chunks.length) this.chunks = [];
|
|
290
|
+
if (this._initReuseChunk !== null) {
|
|
291
|
+
this.chunks.push(this._initReuseChunk);
|
|
292
|
+
this.maxCursor = this._initReuseChunk.length - 1;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* @param {ByteView|number[]} bytes
|
|
297
|
+
*/
|
|
298
|
+
push(bytes) {
|
|
299
|
+
let topChunk = this.chunks[this.chunks.length - 1];
|
|
300
|
+
if (this.cursor + bytes.length <= this.maxCursor + 1) {
|
|
301
|
+
const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
|
|
302
|
+
topChunk.set(bytes, chunkPos);
|
|
303
|
+
} else {
|
|
304
|
+
if (topChunk) {
|
|
305
|
+
const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
|
|
306
|
+
if (chunkPos < topChunk.length) {
|
|
307
|
+
this.chunks[this.chunks.length - 1] = topChunk.subarray(0, chunkPos);
|
|
308
|
+
this.maxCursor = this.cursor - 1;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (bytes.length < 64 && bytes.length < this.chunkSize) {
|
|
312
|
+
topChunk = alloc(this.chunkSize);
|
|
313
|
+
this.chunks.push(topChunk);
|
|
314
|
+
this.maxCursor += topChunk.length;
|
|
315
|
+
if (this._initReuseChunk === null) this._initReuseChunk = topChunk;
|
|
316
|
+
topChunk.set(bytes, 0);
|
|
317
|
+
} else {
|
|
318
|
+
this.chunks.push(bytes);
|
|
319
|
+
this.maxCursor += bytes.length;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
this.cursor += bytes.length;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* @param {boolean} [reset]
|
|
326
|
+
* @returns {AllocatedByteView}
|
|
327
|
+
*/
|
|
328
|
+
toBytes(reset = false) {
|
|
329
|
+
let byts;
|
|
330
|
+
if (this.chunks.length === 1) {
|
|
331
|
+
const chunk = this.chunks[0];
|
|
332
|
+
if (reset && this.cursor > chunk.length / 2) {
|
|
333
|
+
byts = this.cursor === chunk.length ? chunk : chunk.subarray(0, this.cursor);
|
|
334
|
+
this._initReuseChunk = null;
|
|
335
|
+
this.chunks = [];
|
|
336
|
+
} else byts = slice(chunk, 0, this.cursor);
|
|
337
|
+
} else byts = concat(this.chunks, this.cursor);
|
|
338
|
+
if (reset) this.reset();
|
|
339
|
+
return byts;
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
/**
|
|
343
|
+
* U8Bl is a buffer list that writes directly to a user-provided Uint8Array.
|
|
344
|
+
* It provides the same interface as Bl but writes to a fixed destination.
|
|
345
|
+
* @template {ArrayBufferLike} T
|
|
346
|
+
*/
|
|
347
|
+
var U8Bl = class {
|
|
348
|
+
/**
|
|
349
|
+
* @param {Uint8Array<T>} dest
|
|
350
|
+
*/
|
|
351
|
+
constructor(dest) {
|
|
352
|
+
this.dest = dest;
|
|
353
|
+
/** @type {number} */
|
|
354
|
+
this.cursor = 0;
|
|
355
|
+
/** @type {Array<Uint8Array<T>>} */
|
|
356
|
+
this.chunks = [dest];
|
|
357
|
+
}
|
|
358
|
+
reset() {
|
|
359
|
+
this.cursor = 0;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* @param {ByteView|number[]} bytes
|
|
363
|
+
*/
|
|
364
|
+
push(bytes) {
|
|
365
|
+
if (this.cursor + bytes.length > this.dest.length) throw new Error("write out of bounds, destination buffer is too small");
|
|
366
|
+
this.dest.set(bytes, this.cursor);
|
|
367
|
+
this.cursor += bytes.length;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* @param {boolean} [reset]
|
|
371
|
+
* @returns {Uint8Array<T>}
|
|
372
|
+
*/
|
|
373
|
+
toBytes(reset = false) {
|
|
374
|
+
const byts = this.dest.subarray(0, this.cursor);
|
|
375
|
+
if (reset) this.reset();
|
|
376
|
+
return byts;
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
//#endregion
|
|
380
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/common.js
|
|
381
|
+
const decodeErrPrefix = "CBOR decode error:";
|
|
382
|
+
const encodeErrPrefix = "CBOR encode error:";
|
|
383
|
+
const uintMinorPrefixBytes = [];
|
|
384
|
+
uintMinorPrefixBytes[23] = 1;
|
|
385
|
+
uintMinorPrefixBytes[24] = 2;
|
|
386
|
+
uintMinorPrefixBytes[25] = 3;
|
|
387
|
+
uintMinorPrefixBytes[26] = 5;
|
|
388
|
+
uintMinorPrefixBytes[27] = 9;
|
|
389
|
+
/**
|
|
390
|
+
* @param {Uint8Array} data
|
|
391
|
+
* @param {number} pos
|
|
392
|
+
* @param {number} need
|
|
393
|
+
*/
|
|
394
|
+
function assertEnoughData(data, pos, need) {
|
|
395
|
+
if (data.length - pos < need) throw new Error(`${decodeErrPrefix} not enough data for type`);
|
|
396
|
+
}
|
|
397
|
+
//#endregion
|
|
398
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/0uint.js
|
|
399
|
+
const uintBoundaries = [
|
|
400
|
+
24,
|
|
401
|
+
256,
|
|
402
|
+
65536,
|
|
403
|
+
4294967296,
|
|
404
|
+
BigInt("18446744073709551616")
|
|
405
|
+
];
|
|
406
|
+
/**
|
|
407
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
408
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
409
|
+
*/
|
|
410
|
+
/**
|
|
411
|
+
* @param {Uint8Array} data
|
|
412
|
+
* @param {number} offset
|
|
413
|
+
* @param {DecodeOptions} options
|
|
414
|
+
* @returns {number}
|
|
415
|
+
*/
|
|
416
|
+
function readUint8(data, offset, options) {
|
|
417
|
+
assertEnoughData(data, offset, 1);
|
|
418
|
+
const value = data[offset];
|
|
419
|
+
if (options.strict === true && value < uintBoundaries[0]) throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
420
|
+
return value;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* @param {Uint8Array} data
|
|
424
|
+
* @param {number} offset
|
|
425
|
+
* @param {DecodeOptions} options
|
|
426
|
+
* @returns {number}
|
|
427
|
+
*/
|
|
428
|
+
function readUint16(data, offset, options) {
|
|
429
|
+
assertEnoughData(data, offset, 2);
|
|
430
|
+
const value = data[offset] << 8 | data[offset + 1];
|
|
431
|
+
if (options.strict === true && value < uintBoundaries[1]) throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
432
|
+
return value;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* @param {Uint8Array} data
|
|
436
|
+
* @param {number} offset
|
|
437
|
+
* @param {DecodeOptions} options
|
|
438
|
+
* @returns {number}
|
|
439
|
+
*/
|
|
440
|
+
function readUint32(data, offset, options) {
|
|
441
|
+
assertEnoughData(data, offset, 4);
|
|
442
|
+
const value = data[offset] * 16777216 + (data[offset + 1] << 16) + (data[offset + 2] << 8) + data[offset + 3];
|
|
443
|
+
if (options.strict === true && value < uintBoundaries[2]) throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
444
|
+
return value;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* @param {Uint8Array} data
|
|
448
|
+
* @param {number} offset
|
|
449
|
+
* @param {DecodeOptions} options
|
|
450
|
+
* @returns {number|bigint}
|
|
451
|
+
*/
|
|
452
|
+
function readUint64(data, offset, options) {
|
|
453
|
+
assertEnoughData(data, offset, 8);
|
|
454
|
+
const hi = data[offset] * 16777216 + (data[offset + 1] << 16) + (data[offset + 2] << 8) + data[offset + 3];
|
|
455
|
+
const lo = data[offset + 4] * 16777216 + (data[offset + 5] << 16) + (data[offset + 6] << 8) + data[offset + 7];
|
|
456
|
+
const value = (BigInt(hi) << BigInt(32)) + BigInt(lo);
|
|
457
|
+
if (options.strict === true && value < uintBoundaries[3]) throw new Error(`${decodeErrPrefix} integer encoded in more bytes than necessary (strict decode)`);
|
|
458
|
+
if (value <= Number.MAX_SAFE_INTEGER) return Number(value);
|
|
459
|
+
if (options.allowBigInt === true) return value;
|
|
460
|
+
throw new Error(`${decodeErrPrefix} integers outside of the safe integer range are not supported`);
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* @param {Uint8Array} data
|
|
464
|
+
* @param {number} pos
|
|
465
|
+
* @param {number} _minor
|
|
466
|
+
* @param {DecodeOptions} options
|
|
467
|
+
* @returns {Token}
|
|
468
|
+
*/
|
|
469
|
+
function decodeUint8(data, pos, _minor, options) {
|
|
470
|
+
return new Token(Type.uint, readUint8(data, pos + 1, options), 2);
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* @param {Uint8Array} data
|
|
474
|
+
* @param {number} pos
|
|
475
|
+
* @param {number} _minor
|
|
476
|
+
* @param {DecodeOptions} options
|
|
477
|
+
* @returns {Token}
|
|
478
|
+
*/
|
|
479
|
+
function decodeUint16(data, pos, _minor, options) {
|
|
480
|
+
return new Token(Type.uint, readUint16(data, pos + 1, options), 3);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* @param {Uint8Array} data
|
|
484
|
+
* @param {number} pos
|
|
485
|
+
* @param {number} _minor
|
|
486
|
+
* @param {DecodeOptions} options
|
|
487
|
+
* @returns {Token}
|
|
488
|
+
*/
|
|
489
|
+
function decodeUint32(data, pos, _minor, options) {
|
|
490
|
+
return new Token(Type.uint, readUint32(data, pos + 1, options), 5);
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* @param {Uint8Array} data
|
|
494
|
+
* @param {number} pos
|
|
495
|
+
* @param {number} _minor
|
|
496
|
+
* @param {DecodeOptions} options
|
|
497
|
+
* @returns {Token}
|
|
498
|
+
*/
|
|
499
|
+
function decodeUint64(data, pos, _minor, options) {
|
|
500
|
+
return new Token(Type.uint, readUint64(data, pos + 1, options), 9);
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* @param {ByteWriter} writer
|
|
504
|
+
* @param {Token} token
|
|
505
|
+
*/
|
|
506
|
+
function encodeUint(writer, token) {
|
|
507
|
+
return encodeUintValue(writer, 0, token.value);
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* @param {ByteWriter} writer
|
|
511
|
+
* @param {number} major
|
|
512
|
+
* @param {number|bigint} uint
|
|
513
|
+
*/
|
|
514
|
+
function encodeUintValue(writer, major, uint) {
|
|
515
|
+
if (uint < uintBoundaries[0]) {
|
|
516
|
+
const nuint = Number(uint);
|
|
517
|
+
writer.push([major | nuint]);
|
|
518
|
+
} else if (uint < uintBoundaries[1]) {
|
|
519
|
+
const nuint = Number(uint);
|
|
520
|
+
writer.push([major | 24, nuint]);
|
|
521
|
+
} else if (uint < uintBoundaries[2]) {
|
|
522
|
+
const nuint = Number(uint);
|
|
523
|
+
writer.push([
|
|
524
|
+
major | 25,
|
|
525
|
+
nuint >>> 8,
|
|
526
|
+
nuint & 255
|
|
527
|
+
]);
|
|
528
|
+
} else if (uint < uintBoundaries[3]) {
|
|
529
|
+
const nuint = Number(uint);
|
|
530
|
+
writer.push([
|
|
531
|
+
major | 26,
|
|
532
|
+
nuint >>> 24 & 255,
|
|
533
|
+
nuint >>> 16 & 255,
|
|
534
|
+
nuint >>> 8 & 255,
|
|
535
|
+
nuint & 255
|
|
536
|
+
]);
|
|
537
|
+
} else {
|
|
538
|
+
const buint = BigInt(uint);
|
|
539
|
+
if (buint < uintBoundaries[4]) {
|
|
540
|
+
const set = [
|
|
541
|
+
major | 27,
|
|
542
|
+
0,
|
|
543
|
+
0,
|
|
544
|
+
0,
|
|
545
|
+
0,
|
|
546
|
+
0,
|
|
547
|
+
0,
|
|
548
|
+
0
|
|
549
|
+
];
|
|
550
|
+
let lo = Number(buint & BigInt(4294967295));
|
|
551
|
+
let hi = Number(buint >> BigInt(32) & BigInt(4294967295));
|
|
552
|
+
set[8] = lo & 255;
|
|
553
|
+
lo = lo >> 8;
|
|
554
|
+
set[7] = lo & 255;
|
|
555
|
+
lo = lo >> 8;
|
|
556
|
+
set[6] = lo & 255;
|
|
557
|
+
lo = lo >> 8;
|
|
558
|
+
set[5] = lo & 255;
|
|
559
|
+
set[4] = hi & 255;
|
|
560
|
+
hi = hi >> 8;
|
|
561
|
+
set[3] = hi & 255;
|
|
562
|
+
hi = hi >> 8;
|
|
563
|
+
set[2] = hi & 255;
|
|
564
|
+
hi = hi >> 8;
|
|
565
|
+
set[1] = hi & 255;
|
|
566
|
+
writer.push(set);
|
|
567
|
+
} else throw new Error(`${decodeErrPrefix} encountered BigInt larger than allowable range`);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* @param {Token} token
|
|
572
|
+
* @returns {number}
|
|
573
|
+
*/
|
|
574
|
+
encodeUint.encodedSize = function encodedSize(token) {
|
|
575
|
+
return encodeUintValue.encodedSize(token.value);
|
|
576
|
+
};
|
|
577
|
+
/**
|
|
578
|
+
* @param {number} uint
|
|
579
|
+
* @returns {number}
|
|
580
|
+
*/
|
|
581
|
+
encodeUintValue.encodedSize = function encodedSize(uint) {
|
|
582
|
+
if (uint < uintBoundaries[0]) return 1;
|
|
583
|
+
if (uint < uintBoundaries[1]) return 2;
|
|
584
|
+
if (uint < uintBoundaries[2]) return 3;
|
|
585
|
+
if (uint < uintBoundaries[3]) return 5;
|
|
586
|
+
return 9;
|
|
587
|
+
};
|
|
588
|
+
/**
|
|
589
|
+
* @param {Token} tok1
|
|
590
|
+
* @param {Token} tok2
|
|
591
|
+
* @returns {number}
|
|
592
|
+
*/
|
|
593
|
+
encodeUint.compareTokens = function compareTokens(tok1, tok2) {
|
|
594
|
+
return tok1.value < tok2.value ? -1 : tok1.value > tok2.value ? 1 : 0;
|
|
595
|
+
};
|
|
596
|
+
//#endregion
|
|
597
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/1negint.js
|
|
598
|
+
/**
|
|
599
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
600
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
601
|
+
*/
|
|
602
|
+
/**
|
|
603
|
+
* @param {Uint8Array} data
|
|
604
|
+
* @param {number} pos
|
|
605
|
+
* @param {number} _minor
|
|
606
|
+
* @param {DecodeOptions} options
|
|
607
|
+
* @returns {Token}
|
|
608
|
+
*/
|
|
609
|
+
function decodeNegint8(data, pos, _minor, options) {
|
|
610
|
+
return new Token(Type.negint, -1 - readUint8(data, pos + 1, options), 2);
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* @param {Uint8Array} data
|
|
614
|
+
* @param {number} pos
|
|
615
|
+
* @param {number} _minor
|
|
616
|
+
* @param {DecodeOptions} options
|
|
617
|
+
* @returns {Token}
|
|
618
|
+
*/
|
|
619
|
+
function decodeNegint16(data, pos, _minor, options) {
|
|
620
|
+
return new Token(Type.negint, -1 - readUint16(data, pos + 1, options), 3);
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* @param {Uint8Array} data
|
|
624
|
+
* @param {number} pos
|
|
625
|
+
* @param {number} _minor
|
|
626
|
+
* @param {DecodeOptions} options
|
|
627
|
+
* @returns {Token}
|
|
628
|
+
*/
|
|
629
|
+
function decodeNegint32(data, pos, _minor, options) {
|
|
630
|
+
return new Token(Type.negint, -1 - readUint32(data, pos + 1, options), 5);
|
|
631
|
+
}
|
|
632
|
+
const neg1b$1 = BigInt(-1);
|
|
633
|
+
const pos1b$1 = BigInt(1);
|
|
634
|
+
/**
|
|
635
|
+
* @param {Uint8Array} data
|
|
636
|
+
* @param {number} pos
|
|
637
|
+
* @param {number} _minor
|
|
638
|
+
* @param {DecodeOptions} options
|
|
639
|
+
* @returns {Token}
|
|
640
|
+
*/
|
|
641
|
+
function decodeNegint64(data, pos, _minor, options) {
|
|
642
|
+
const int = readUint64(data, pos + 1, options);
|
|
643
|
+
if (typeof int !== "bigint") {
|
|
644
|
+
const value = -1 - int;
|
|
645
|
+
if (value >= Number.MIN_SAFE_INTEGER) return new Token(Type.negint, value, 9);
|
|
646
|
+
}
|
|
647
|
+
if (options.allowBigInt !== true) throw new Error(`${decodeErrPrefix} integers outside of the safe integer range are not supported`);
|
|
648
|
+
return new Token(Type.negint, neg1b$1 - BigInt(int), 9);
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* @param {ByteWriter} writer
|
|
652
|
+
* @param {Token} token
|
|
653
|
+
*/
|
|
654
|
+
function encodeNegint(writer, token) {
|
|
655
|
+
const negint = token.value;
|
|
656
|
+
const unsigned = typeof negint === "bigint" ? negint * neg1b$1 - pos1b$1 : negint * -1 - 1;
|
|
657
|
+
encodeUintValue(writer, token.type.majorEncoded, unsigned);
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* @param {Token} token
|
|
661
|
+
* @returns {number}
|
|
662
|
+
*/
|
|
663
|
+
encodeNegint.encodedSize = function encodedSize(token) {
|
|
664
|
+
const negint = token.value;
|
|
665
|
+
const unsigned = typeof negint === "bigint" ? negint * neg1b$1 - pos1b$1 : negint * -1 - 1;
|
|
666
|
+
if (unsigned < uintBoundaries[0]) return 1;
|
|
667
|
+
if (unsigned < uintBoundaries[1]) return 2;
|
|
668
|
+
if (unsigned < uintBoundaries[2]) return 3;
|
|
669
|
+
if (unsigned < uintBoundaries[3]) return 5;
|
|
670
|
+
return 9;
|
|
671
|
+
};
|
|
672
|
+
/**
|
|
673
|
+
* @param {Token} tok1
|
|
674
|
+
* @param {Token} tok2
|
|
675
|
+
* @returns {number}
|
|
676
|
+
*/
|
|
677
|
+
encodeNegint.compareTokens = function compareTokens(tok1, tok2) {
|
|
678
|
+
return tok1.value < tok2.value ? 1 : tok1.value > tok2.value ? -1 : 0;
|
|
679
|
+
};
|
|
680
|
+
//#endregion
|
|
681
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/2bytes.js
|
|
682
|
+
/**
|
|
683
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
684
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
685
|
+
*/
|
|
686
|
+
/**
|
|
687
|
+
* @param {Uint8Array} data
|
|
688
|
+
* @param {number} pos
|
|
689
|
+
* @param {number} prefix
|
|
690
|
+
* @param {number} length
|
|
691
|
+
* @returns {Token}
|
|
692
|
+
*/
|
|
693
|
+
function toToken$3(data, pos, prefix, length) {
|
|
694
|
+
assertEnoughData(data, pos, prefix + length);
|
|
695
|
+
const buf = data.slice(pos + prefix, pos + prefix + length);
|
|
696
|
+
return new Token(Type.bytes, buf, prefix + length);
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* @param {Uint8Array} data
|
|
700
|
+
* @param {number} pos
|
|
701
|
+
* @param {number} minor
|
|
702
|
+
* @param {DecodeOptions} _options
|
|
703
|
+
* @returns {Token}
|
|
704
|
+
*/
|
|
705
|
+
function decodeBytesCompact(data, pos, minor, _options) {
|
|
706
|
+
return toToken$3(data, pos, 1, minor);
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* @param {Uint8Array} data
|
|
710
|
+
* @param {number} pos
|
|
711
|
+
* @param {number} _minor
|
|
712
|
+
* @param {DecodeOptions} options
|
|
713
|
+
* @returns {Token}
|
|
714
|
+
*/
|
|
715
|
+
function decodeBytes8(data, pos, _minor, options) {
|
|
716
|
+
return toToken$3(data, pos, 2, readUint8(data, pos + 1, options));
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
* @param {Uint8Array} data
|
|
720
|
+
* @param {number} pos
|
|
721
|
+
* @param {number} _minor
|
|
722
|
+
* @param {DecodeOptions} options
|
|
723
|
+
* @returns {Token}
|
|
724
|
+
*/
|
|
725
|
+
function decodeBytes16(data, pos, _minor, options) {
|
|
726
|
+
return toToken$3(data, pos, 3, readUint16(data, pos + 1, options));
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* @param {Uint8Array} data
|
|
730
|
+
* @param {number} pos
|
|
731
|
+
* @param {number} _minor
|
|
732
|
+
* @param {DecodeOptions} options
|
|
733
|
+
* @returns {Token}
|
|
734
|
+
*/
|
|
735
|
+
function decodeBytes32(data, pos, _minor, options) {
|
|
736
|
+
return toToken$3(data, pos, 5, readUint32(data, pos + 1, options));
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* @param {Uint8Array} data
|
|
740
|
+
* @param {number} pos
|
|
741
|
+
* @param {number} _minor
|
|
742
|
+
* @param {DecodeOptions} options
|
|
743
|
+
* @returns {Token}
|
|
744
|
+
*/
|
|
745
|
+
function decodeBytes64(data, pos, _minor, options) {
|
|
746
|
+
const l = readUint64(data, pos + 1, options);
|
|
747
|
+
if (typeof l === "bigint") throw new Error(`${decodeErrPrefix} 64-bit integer bytes lengths not supported`);
|
|
748
|
+
return toToken$3(data, pos, 9, l);
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* `encodedBytes` allows for caching when we do a byte version of a string
|
|
752
|
+
* for key sorting purposes
|
|
753
|
+
* @param {Token} token
|
|
754
|
+
* @returns {Uint8Array}
|
|
755
|
+
*/
|
|
756
|
+
function tokenBytes(token) {
|
|
757
|
+
if (token.encodedBytes === void 0) token.encodedBytes = Type.equals(token.type, Type.string) ? fromString(token.value) : token.value;
|
|
758
|
+
return token.encodedBytes;
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* @param {ByteWriter} writer
|
|
762
|
+
* @param {Token} token
|
|
763
|
+
*/
|
|
764
|
+
function encodeBytes(writer, token) {
|
|
765
|
+
const bytes = tokenBytes(token);
|
|
766
|
+
encodeUintValue(writer, token.type.majorEncoded, bytes.length);
|
|
767
|
+
writer.push(bytes);
|
|
768
|
+
}
|
|
769
|
+
/**
|
|
770
|
+
* @param {Token} token
|
|
771
|
+
* @returns {number}
|
|
772
|
+
*/
|
|
773
|
+
encodeBytes.encodedSize = function encodedSize(token) {
|
|
774
|
+
const bytes = tokenBytes(token);
|
|
775
|
+
return encodeUintValue.encodedSize(bytes.length) + bytes.length;
|
|
776
|
+
};
|
|
777
|
+
/**
|
|
778
|
+
* @param {Token} tok1
|
|
779
|
+
* @param {Token} tok2
|
|
780
|
+
* @returns {number}
|
|
781
|
+
*/
|
|
782
|
+
encodeBytes.compareTokens = function compareTokens(tok1, tok2) {
|
|
783
|
+
return compareBytes(tokenBytes(tok1), tokenBytes(tok2));
|
|
784
|
+
};
|
|
785
|
+
/**
|
|
786
|
+
* @param {Uint8Array} b1
|
|
787
|
+
* @param {Uint8Array} b2
|
|
788
|
+
* @returns {number}
|
|
789
|
+
*/
|
|
790
|
+
function compareBytes(b1, b2) {
|
|
791
|
+
return b1.length < b2.length ? -1 : b1.length > b2.length ? 1 : compare(b1, b2);
|
|
792
|
+
}
|
|
793
|
+
//#endregion
|
|
794
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/3string.js
|
|
795
|
+
const textDecoder = new TextDecoder();
|
|
796
|
+
const ASCII_THRESHOLD = 32;
|
|
797
|
+
/**
|
|
798
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
799
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
800
|
+
*/
|
|
801
|
+
/**
|
|
802
|
+
* Decode UTF-8 bytes to string. For short ASCII strings (common case for map keys),
|
|
803
|
+
* a simple loop is faster than TextDecoder.
|
|
804
|
+
* @param {Uint8Array} bytes
|
|
805
|
+
* @param {number} start
|
|
806
|
+
* @param {number} end
|
|
807
|
+
* @returns {string}
|
|
808
|
+
*/
|
|
809
|
+
function toStr(bytes, start, end) {
|
|
810
|
+
if (end - start < ASCII_THRESHOLD) {
|
|
811
|
+
let str = "";
|
|
812
|
+
for (let i = start; i < end; i++) {
|
|
813
|
+
const c = bytes[i];
|
|
814
|
+
if (c & 128) return textDecoder.decode(bytes.subarray(start, end));
|
|
815
|
+
str += String.fromCharCode(c);
|
|
816
|
+
}
|
|
817
|
+
return str;
|
|
818
|
+
}
|
|
819
|
+
return textDecoder.decode(bytes.subarray(start, end));
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* @param {Uint8Array} data
|
|
823
|
+
* @param {number} pos
|
|
824
|
+
* @param {number} prefix
|
|
825
|
+
* @param {number} length
|
|
826
|
+
* @param {DecodeOptions} options
|
|
827
|
+
* @returns {Token}
|
|
828
|
+
*/
|
|
829
|
+
function toToken$2(data, pos, prefix, length, options) {
|
|
830
|
+
const totLength = prefix + length;
|
|
831
|
+
assertEnoughData(data, pos, totLength);
|
|
832
|
+
const tok = new Token(Type.string, toStr(data, pos + prefix, pos + totLength), totLength);
|
|
833
|
+
if (options.retainStringBytes === true) tok.byteValue = data.slice(pos + prefix, pos + totLength);
|
|
834
|
+
return tok;
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* @param {Uint8Array} data
|
|
838
|
+
* @param {number} pos
|
|
839
|
+
* @param {number} minor
|
|
840
|
+
* @param {DecodeOptions} options
|
|
841
|
+
* @returns {Token}
|
|
842
|
+
*/
|
|
843
|
+
function decodeStringCompact(data, pos, minor, options) {
|
|
844
|
+
return toToken$2(data, pos, 1, minor, options);
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* @param {Uint8Array} data
|
|
848
|
+
* @param {number} pos
|
|
849
|
+
* @param {number} _minor
|
|
850
|
+
* @param {DecodeOptions} options
|
|
851
|
+
* @returns {Token}
|
|
852
|
+
*/
|
|
853
|
+
function decodeString8(data, pos, _minor, options) {
|
|
854
|
+
return toToken$2(data, pos, 2, readUint8(data, pos + 1, options), options);
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* @param {Uint8Array} data
|
|
858
|
+
* @param {number} pos
|
|
859
|
+
* @param {number} _minor
|
|
860
|
+
* @param {DecodeOptions} options
|
|
861
|
+
* @returns {Token}
|
|
862
|
+
*/
|
|
863
|
+
function decodeString16(data, pos, _minor, options) {
|
|
864
|
+
return toToken$2(data, pos, 3, readUint16(data, pos + 1, options), options);
|
|
865
|
+
}
|
|
866
|
+
/**
|
|
867
|
+
* @param {Uint8Array} data
|
|
868
|
+
* @param {number} pos
|
|
869
|
+
* @param {number} _minor
|
|
870
|
+
* @param {DecodeOptions} options
|
|
871
|
+
* @returns {Token}
|
|
872
|
+
*/
|
|
873
|
+
function decodeString32(data, pos, _minor, options) {
|
|
874
|
+
return toToken$2(data, pos, 5, readUint32(data, pos + 1, options), options);
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* @param {Uint8Array} data
|
|
878
|
+
* @param {number} pos
|
|
879
|
+
* @param {number} _minor
|
|
880
|
+
* @param {DecodeOptions} options
|
|
881
|
+
* @returns {Token}
|
|
882
|
+
*/
|
|
883
|
+
function decodeString64(data, pos, _minor, options) {
|
|
884
|
+
const l = readUint64(data, pos + 1, options);
|
|
885
|
+
if (typeof l === "bigint") throw new Error(`${decodeErrPrefix} 64-bit integer string lengths not supported`);
|
|
886
|
+
return toToken$2(data, pos, 9, l, options);
|
|
887
|
+
}
|
|
888
|
+
const encodeString = encodeBytes;
|
|
889
|
+
//#endregion
|
|
890
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/4array.js
|
|
891
|
+
/**
|
|
892
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
893
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
894
|
+
*/
|
|
895
|
+
/**
|
|
896
|
+
* @param {Uint8Array} _data
|
|
897
|
+
* @param {number} _pos
|
|
898
|
+
* @param {number} prefix
|
|
899
|
+
* @param {number} length
|
|
900
|
+
* @returns {Token}
|
|
901
|
+
*/
|
|
902
|
+
function toToken$1(_data, _pos, prefix, length) {
|
|
903
|
+
return new Token(Type.array, length, prefix);
|
|
904
|
+
}
|
|
905
|
+
/**
|
|
906
|
+
* @param {Uint8Array} data
|
|
907
|
+
* @param {number} pos
|
|
908
|
+
* @param {number} minor
|
|
909
|
+
* @param {DecodeOptions} _options
|
|
910
|
+
* @returns {Token}
|
|
911
|
+
*/
|
|
912
|
+
function decodeArrayCompact(data, pos, minor, _options) {
|
|
913
|
+
return toToken$1(data, pos, 1, minor);
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* @param {Uint8Array} data
|
|
917
|
+
* @param {number} pos
|
|
918
|
+
* @param {number} _minor
|
|
919
|
+
* @param {DecodeOptions} options
|
|
920
|
+
* @returns {Token}
|
|
921
|
+
*/
|
|
922
|
+
function decodeArray8(data, pos, _minor, options) {
|
|
923
|
+
return toToken$1(data, pos, 2, readUint8(data, pos + 1, options));
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* @param {Uint8Array} data
|
|
927
|
+
* @param {number} pos
|
|
928
|
+
* @param {number} _minor
|
|
929
|
+
* @param {DecodeOptions} options
|
|
930
|
+
* @returns {Token}
|
|
931
|
+
*/
|
|
932
|
+
function decodeArray16(data, pos, _minor, options) {
|
|
933
|
+
return toToken$1(data, pos, 3, readUint16(data, pos + 1, options));
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* @param {Uint8Array} data
|
|
937
|
+
* @param {number} pos
|
|
938
|
+
* @param {number} _minor
|
|
939
|
+
* @param {DecodeOptions} options
|
|
940
|
+
* @returns {Token}
|
|
941
|
+
*/
|
|
942
|
+
function decodeArray32(data, pos, _minor, options) {
|
|
943
|
+
return toToken$1(data, pos, 5, readUint32(data, pos + 1, options));
|
|
944
|
+
}
|
|
945
|
+
/**
|
|
946
|
+
* @param {Uint8Array} data
|
|
947
|
+
* @param {number} pos
|
|
948
|
+
* @param {number} _minor
|
|
949
|
+
* @param {DecodeOptions} options
|
|
950
|
+
* @returns {Token}
|
|
951
|
+
*/
|
|
952
|
+
function decodeArray64(data, pos, _minor, options) {
|
|
953
|
+
const l = readUint64(data, pos + 1, options);
|
|
954
|
+
if (typeof l === "bigint") throw new Error(`${decodeErrPrefix} 64-bit integer array lengths not supported`);
|
|
955
|
+
return toToken$1(data, pos, 9, l);
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* @param {Uint8Array} data
|
|
959
|
+
* @param {number} pos
|
|
960
|
+
* @param {number} _minor
|
|
961
|
+
* @param {DecodeOptions} options
|
|
962
|
+
* @returns {Token}
|
|
963
|
+
*/
|
|
964
|
+
function decodeArrayIndefinite(data, pos, _minor, options) {
|
|
965
|
+
if (options.allowIndefinite === false) throw new Error(`${decodeErrPrefix} indefinite length items not allowed`);
|
|
966
|
+
return toToken$1(data, pos, 1, Infinity);
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* @param {ByteWriter} writer
|
|
970
|
+
* @param {Token} token
|
|
971
|
+
*/
|
|
972
|
+
function encodeArray(writer, token) {
|
|
973
|
+
encodeUintValue(writer, Type.array.majorEncoded, token.value);
|
|
974
|
+
}
|
|
975
|
+
encodeArray.compareTokens = encodeUint.compareTokens;
|
|
976
|
+
/**
|
|
977
|
+
* @param {Token} token
|
|
978
|
+
* @returns {number}
|
|
979
|
+
*/
|
|
980
|
+
encodeArray.encodedSize = function encodedSize(token) {
|
|
981
|
+
return encodeUintValue.encodedSize(token.value);
|
|
982
|
+
};
|
|
983
|
+
//#endregion
|
|
984
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/5map.js
|
|
985
|
+
/**
|
|
986
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
987
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
988
|
+
*/
|
|
989
|
+
/**
|
|
990
|
+
* @param {Uint8Array} _data
|
|
991
|
+
* @param {number} _pos
|
|
992
|
+
* @param {number} prefix
|
|
993
|
+
* @param {number} length
|
|
994
|
+
* @returns {Token}
|
|
995
|
+
*/
|
|
996
|
+
function toToken(_data, _pos, prefix, length) {
|
|
997
|
+
return new Token(Type.map, length, prefix);
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* @param {Uint8Array} data
|
|
1001
|
+
* @param {number} pos
|
|
1002
|
+
* @param {number} minor
|
|
1003
|
+
* @param {DecodeOptions} _options
|
|
1004
|
+
* @returns {Token}
|
|
1005
|
+
*/
|
|
1006
|
+
function decodeMapCompact(data, pos, minor, _options) {
|
|
1007
|
+
return toToken(data, pos, 1, minor);
|
|
1008
|
+
}
|
|
1009
|
+
/**
|
|
1010
|
+
* @param {Uint8Array} data
|
|
1011
|
+
* @param {number} pos
|
|
1012
|
+
* @param {number} _minor
|
|
1013
|
+
* @param {DecodeOptions} options
|
|
1014
|
+
* @returns {Token}
|
|
1015
|
+
*/
|
|
1016
|
+
function decodeMap8(data, pos, _minor, options) {
|
|
1017
|
+
return toToken(data, pos, 2, readUint8(data, pos + 1, options));
|
|
1018
|
+
}
|
|
1019
|
+
/**
|
|
1020
|
+
* @param {Uint8Array} data
|
|
1021
|
+
* @param {number} pos
|
|
1022
|
+
* @param {number} _minor
|
|
1023
|
+
* @param {DecodeOptions} options
|
|
1024
|
+
* @returns {Token}
|
|
1025
|
+
*/
|
|
1026
|
+
function decodeMap16(data, pos, _minor, options) {
|
|
1027
|
+
return toToken(data, pos, 3, readUint16(data, pos + 1, options));
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
1030
|
+
* @param {Uint8Array} data
|
|
1031
|
+
* @param {number} pos
|
|
1032
|
+
* @param {number} _minor
|
|
1033
|
+
* @param {DecodeOptions} options
|
|
1034
|
+
* @returns {Token}
|
|
1035
|
+
*/
|
|
1036
|
+
function decodeMap32(data, pos, _minor, options) {
|
|
1037
|
+
return toToken(data, pos, 5, readUint32(data, pos + 1, options));
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* @param {Uint8Array} data
|
|
1041
|
+
* @param {number} pos
|
|
1042
|
+
* @param {number} _minor
|
|
1043
|
+
* @param {DecodeOptions} options
|
|
1044
|
+
* @returns {Token}
|
|
1045
|
+
*/
|
|
1046
|
+
function decodeMap64(data, pos, _minor, options) {
|
|
1047
|
+
const l = readUint64(data, pos + 1, options);
|
|
1048
|
+
if (typeof l === "bigint") throw new Error(`${decodeErrPrefix} 64-bit integer map lengths not supported`);
|
|
1049
|
+
return toToken(data, pos, 9, l);
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* @param {Uint8Array} data
|
|
1053
|
+
* @param {number} pos
|
|
1054
|
+
* @param {number} _minor
|
|
1055
|
+
* @param {DecodeOptions} options
|
|
1056
|
+
* @returns {Token}
|
|
1057
|
+
*/
|
|
1058
|
+
function decodeMapIndefinite(data, pos, _minor, options) {
|
|
1059
|
+
if (options.allowIndefinite === false) throw new Error(`${decodeErrPrefix} indefinite length items not allowed`);
|
|
1060
|
+
return toToken(data, pos, 1, Infinity);
|
|
1061
|
+
}
|
|
1062
|
+
/**
|
|
1063
|
+
* @param {ByteWriter} writer
|
|
1064
|
+
* @param {Token} token
|
|
1065
|
+
*/
|
|
1066
|
+
function encodeMap(writer, token) {
|
|
1067
|
+
encodeUintValue(writer, Type.map.majorEncoded, token.value);
|
|
1068
|
+
}
|
|
1069
|
+
encodeMap.compareTokens = encodeUint.compareTokens;
|
|
1070
|
+
/**
|
|
1071
|
+
* @param {Token} token
|
|
1072
|
+
* @returns {number}
|
|
1073
|
+
*/
|
|
1074
|
+
encodeMap.encodedSize = function encodedSize(token) {
|
|
1075
|
+
return encodeUintValue.encodedSize(token.value);
|
|
1076
|
+
};
|
|
1077
|
+
//#endregion
|
|
1078
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/6tag.js
|
|
1079
|
+
/**
|
|
1080
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
1081
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
1082
|
+
*/
|
|
1083
|
+
/**
|
|
1084
|
+
* @param {Uint8Array} _data
|
|
1085
|
+
* @param {number} _pos
|
|
1086
|
+
* @param {number} minor
|
|
1087
|
+
* @param {DecodeOptions} _options
|
|
1088
|
+
* @returns {Token}
|
|
1089
|
+
*/
|
|
1090
|
+
function decodeTagCompact(_data, _pos, minor, _options) {
|
|
1091
|
+
return new Token(Type.tag, minor, 1);
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* @param {Uint8Array} data
|
|
1095
|
+
* @param {number} pos
|
|
1096
|
+
* @param {number} _minor
|
|
1097
|
+
* @param {DecodeOptions} options
|
|
1098
|
+
* @returns {Token}
|
|
1099
|
+
*/
|
|
1100
|
+
function decodeTag8(data, pos, _minor, options) {
|
|
1101
|
+
return new Token(Type.tag, readUint8(data, pos + 1, options), 2);
|
|
1102
|
+
}
|
|
1103
|
+
/**
|
|
1104
|
+
* @param {Uint8Array} data
|
|
1105
|
+
* @param {number} pos
|
|
1106
|
+
* @param {number} _minor
|
|
1107
|
+
* @param {DecodeOptions} options
|
|
1108
|
+
* @returns {Token}
|
|
1109
|
+
*/
|
|
1110
|
+
function decodeTag16(data, pos, _minor, options) {
|
|
1111
|
+
return new Token(Type.tag, readUint16(data, pos + 1, options), 3);
|
|
1112
|
+
}
|
|
1113
|
+
/**
|
|
1114
|
+
* @param {Uint8Array} data
|
|
1115
|
+
* @param {number} pos
|
|
1116
|
+
* @param {number} _minor
|
|
1117
|
+
* @param {DecodeOptions} options
|
|
1118
|
+
* @returns {Token}
|
|
1119
|
+
*/
|
|
1120
|
+
function decodeTag32(data, pos, _minor, options) {
|
|
1121
|
+
return new Token(Type.tag, readUint32(data, pos + 1, options), 5);
|
|
1122
|
+
}
|
|
1123
|
+
/**
|
|
1124
|
+
* @param {Uint8Array} data
|
|
1125
|
+
* @param {number} pos
|
|
1126
|
+
* @param {number} _minor
|
|
1127
|
+
* @param {DecodeOptions} options
|
|
1128
|
+
* @returns {Token}
|
|
1129
|
+
*/
|
|
1130
|
+
function decodeTag64(data, pos, _minor, options) {
|
|
1131
|
+
return new Token(Type.tag, readUint64(data, pos + 1, options), 9);
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* @param {ByteWriter} writer
|
|
1135
|
+
* @param {Token} token
|
|
1136
|
+
*/
|
|
1137
|
+
function encodeTag(writer, token) {
|
|
1138
|
+
encodeUintValue(writer, Type.tag.majorEncoded, token.value);
|
|
1139
|
+
}
|
|
1140
|
+
encodeTag.compareTokens = encodeUint.compareTokens;
|
|
1141
|
+
/**
|
|
1142
|
+
* @param {Token} token
|
|
1143
|
+
* @returns {number}
|
|
1144
|
+
*/
|
|
1145
|
+
encodeTag.encodedSize = function encodedSize(token) {
|
|
1146
|
+
return encodeUintValue.encodedSize(token.value);
|
|
1147
|
+
};
|
|
1148
|
+
/**
|
|
1149
|
+
* @param {Uint8Array} _data
|
|
1150
|
+
* @param {number} _pos
|
|
1151
|
+
* @param {number} _minor
|
|
1152
|
+
* @param {DecodeOptions} options
|
|
1153
|
+
* @returns {Token}
|
|
1154
|
+
*/
|
|
1155
|
+
function decodeUndefined(_data, _pos, _minor, options) {
|
|
1156
|
+
if (options.allowUndefined === false) throw new Error(`${decodeErrPrefix} undefined values are not supported`);
|
|
1157
|
+
else if (options.coerceUndefinedToNull === true) return new Token(Type.null, null, 1);
|
|
1158
|
+
return new Token(Type.undefined, void 0, 1);
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
* @param {Uint8Array} _data
|
|
1162
|
+
* @param {number} _pos
|
|
1163
|
+
* @param {number} _minor
|
|
1164
|
+
* @param {DecodeOptions} options
|
|
1165
|
+
* @returns {Token}
|
|
1166
|
+
*/
|
|
1167
|
+
function decodeBreak(_data, _pos, _minor, options) {
|
|
1168
|
+
if (options.allowIndefinite === false) throw new Error(`${decodeErrPrefix} indefinite length items not allowed`);
|
|
1169
|
+
return new Token(Type.break, void 0, 1);
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* @param {number} value
|
|
1173
|
+
* @param {number} bytes
|
|
1174
|
+
* @param {DecodeOptions} options
|
|
1175
|
+
* @returns {Token}
|
|
1176
|
+
*/
|
|
1177
|
+
function createToken(value, bytes, options) {
|
|
1178
|
+
if (options) {
|
|
1179
|
+
if (options.allowNaN === false && Number.isNaN(value)) throw new Error(`${decodeErrPrefix} NaN values are not supported`);
|
|
1180
|
+
if (options.allowInfinity === false && (value === Infinity || value === -Infinity)) throw new Error(`${decodeErrPrefix} Infinity values are not supported`);
|
|
1181
|
+
}
|
|
1182
|
+
return new Token(Type.float, value, bytes);
|
|
1183
|
+
}
|
|
1184
|
+
/**
|
|
1185
|
+
* @param {Uint8Array} data
|
|
1186
|
+
* @param {number} pos
|
|
1187
|
+
* @param {number} _minor
|
|
1188
|
+
* @param {DecodeOptions} options
|
|
1189
|
+
* @returns {Token}
|
|
1190
|
+
*/
|
|
1191
|
+
function decodeFloat16(data, pos, _minor, options) {
|
|
1192
|
+
return createToken(readFloat16(data, pos + 1), 3, options);
|
|
1193
|
+
}
|
|
1194
|
+
/**
|
|
1195
|
+
* @param {Uint8Array} data
|
|
1196
|
+
* @param {number} pos
|
|
1197
|
+
* @param {number} _minor
|
|
1198
|
+
* @param {DecodeOptions} options
|
|
1199
|
+
* @returns {Token}
|
|
1200
|
+
*/
|
|
1201
|
+
function decodeFloat32(data, pos, _minor, options) {
|
|
1202
|
+
return createToken(readFloat32(data, pos + 1), 5, options);
|
|
1203
|
+
}
|
|
1204
|
+
/**
|
|
1205
|
+
* @param {Uint8Array} data
|
|
1206
|
+
* @param {number} pos
|
|
1207
|
+
* @param {number} _minor
|
|
1208
|
+
* @param {DecodeOptions} options
|
|
1209
|
+
* @returns {Token}
|
|
1210
|
+
*/
|
|
1211
|
+
function decodeFloat64(data, pos, _minor, options) {
|
|
1212
|
+
return createToken(readFloat64(data, pos + 1), 9, options);
|
|
1213
|
+
}
|
|
1214
|
+
/**
|
|
1215
|
+
* @param {ByteWriter} writer
|
|
1216
|
+
* @param {Token} token
|
|
1217
|
+
* @param {EncodeOptions} options
|
|
1218
|
+
*/
|
|
1219
|
+
function encodeFloat(writer, token, options) {
|
|
1220
|
+
const float = token.value;
|
|
1221
|
+
if (float === false) writer.push([Type.float.majorEncoded | 20]);
|
|
1222
|
+
else if (float === true) writer.push([Type.float.majorEncoded | 21]);
|
|
1223
|
+
else if (float === null) writer.push([Type.float.majorEncoded | 22]);
|
|
1224
|
+
else if (float === void 0) writer.push([Type.float.majorEncoded | 23]);
|
|
1225
|
+
else {
|
|
1226
|
+
let decoded;
|
|
1227
|
+
let success = false;
|
|
1228
|
+
if (!options || options.float64 !== true) {
|
|
1229
|
+
encodeFloat16(float);
|
|
1230
|
+
decoded = readFloat16(ui8a, 1);
|
|
1231
|
+
if (float === decoded || Number.isNaN(float)) {
|
|
1232
|
+
ui8a[0] = 249;
|
|
1233
|
+
writer.push(ui8a.slice(0, 3));
|
|
1234
|
+
success = true;
|
|
1235
|
+
} else {
|
|
1236
|
+
encodeFloat32(float);
|
|
1237
|
+
decoded = readFloat32(ui8a, 1);
|
|
1238
|
+
if (float === decoded) {
|
|
1239
|
+
ui8a[0] = 250;
|
|
1240
|
+
writer.push(ui8a.slice(0, 5));
|
|
1241
|
+
success = true;
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
if (!success) {
|
|
1246
|
+
encodeFloat64(float);
|
|
1247
|
+
decoded = readFloat64(ui8a, 1);
|
|
1248
|
+
ui8a[0] = 251;
|
|
1249
|
+
writer.push(ui8a.slice(0, 9));
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
/**
|
|
1254
|
+
* @param {Token} token
|
|
1255
|
+
* @param {EncodeOptions} options
|
|
1256
|
+
* @returns {number}
|
|
1257
|
+
*/
|
|
1258
|
+
encodeFloat.encodedSize = function encodedSize(token, options) {
|
|
1259
|
+
const float = token.value;
|
|
1260
|
+
if (float === false || float === true || float === null || float === void 0) return 1;
|
|
1261
|
+
if (!options || options.float64 !== true) {
|
|
1262
|
+
encodeFloat16(float);
|
|
1263
|
+
let decoded = readFloat16(ui8a, 1);
|
|
1264
|
+
if (float === decoded || Number.isNaN(float)) return 3;
|
|
1265
|
+
encodeFloat32(float);
|
|
1266
|
+
decoded = readFloat32(ui8a, 1);
|
|
1267
|
+
if (float === decoded) return 5;
|
|
1268
|
+
}
|
|
1269
|
+
return 9;
|
|
1270
|
+
};
|
|
1271
|
+
const buffer = /* @__PURE__ */ new ArrayBuffer(9);
|
|
1272
|
+
const dataView = new DataView(buffer, 1);
|
|
1273
|
+
const ui8a = new Uint8Array(buffer, 0);
|
|
1274
|
+
/**
|
|
1275
|
+
* @param {number} inp
|
|
1276
|
+
*/
|
|
1277
|
+
function encodeFloat16(inp) {
|
|
1278
|
+
if (inp === Infinity) dataView.setUint16(0, 31744, false);
|
|
1279
|
+
else if (inp === -Infinity) dataView.setUint16(0, 64512, false);
|
|
1280
|
+
else if (Number.isNaN(inp)) dataView.setUint16(0, 32256, false);
|
|
1281
|
+
else {
|
|
1282
|
+
dataView.setFloat32(0, inp);
|
|
1283
|
+
const valu32 = dataView.getUint32(0);
|
|
1284
|
+
const exponent = (valu32 & 2139095040) >> 23;
|
|
1285
|
+
const mantissa = valu32 & 8388607;
|
|
1286
|
+
if (exponent === 255) dataView.setUint16(0, 31744, false);
|
|
1287
|
+
else if (exponent === 0) dataView.setUint16(0, (valu32 & 2147483648) >> 16 | mantissa >> 13, false);
|
|
1288
|
+
else {
|
|
1289
|
+
const logicalExponent = exponent - 127;
|
|
1290
|
+
if (logicalExponent < -24) dataView.setUint16(0, 0);
|
|
1291
|
+
else if (logicalExponent < -14) dataView.setUint16(0, (valu32 & 2147483648) >> 16 | 1 << 24 + logicalExponent, false);
|
|
1292
|
+
else dataView.setUint16(0, (valu32 & 2147483648) >> 16 | logicalExponent + 15 << 10 | mantissa >> 13, false);
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* @param {Uint8Array} ui8a
|
|
1298
|
+
* @param {number} pos
|
|
1299
|
+
* @returns {number}
|
|
1300
|
+
*/
|
|
1301
|
+
function readFloat16(ui8a, pos) {
|
|
1302
|
+
if (ui8a.length - pos < 2) throw new Error(`${decodeErrPrefix} not enough data for float16`);
|
|
1303
|
+
const half = (ui8a[pos] << 8) + ui8a[pos + 1];
|
|
1304
|
+
if (half === 31744) return Infinity;
|
|
1305
|
+
if (half === 64512) return -Infinity;
|
|
1306
|
+
if (half === 32256) return NaN;
|
|
1307
|
+
const exp = half >> 10 & 31;
|
|
1308
|
+
const mant = half & 1023;
|
|
1309
|
+
let val;
|
|
1310
|
+
if (exp === 0) val = mant * 2 ** -24;
|
|
1311
|
+
else if (exp !== 31) val = (mant + 1024) * 2 ** (exp - 25);
|
|
1312
|
+
else val = mant === 0 ? Infinity : NaN;
|
|
1313
|
+
return half & 32768 ? -val : val;
|
|
1314
|
+
}
|
|
1315
|
+
/**
|
|
1316
|
+
* @param {number} inp
|
|
1317
|
+
*/
|
|
1318
|
+
function encodeFloat32(inp) {
|
|
1319
|
+
dataView.setFloat32(0, inp, false);
|
|
1320
|
+
}
|
|
1321
|
+
/**
|
|
1322
|
+
* @param {Uint8Array} ui8a
|
|
1323
|
+
* @param {number} pos
|
|
1324
|
+
* @returns {number}
|
|
1325
|
+
*/
|
|
1326
|
+
function readFloat32(ui8a, pos) {
|
|
1327
|
+
if (ui8a.length - pos < 4) throw new Error(`${decodeErrPrefix} not enough data for float32`);
|
|
1328
|
+
const offset = (ui8a.byteOffset || 0) + pos;
|
|
1329
|
+
return new DataView(ui8a.buffer, offset, 4).getFloat32(0, false);
|
|
1330
|
+
}
|
|
1331
|
+
/**
|
|
1332
|
+
* @param {number} inp
|
|
1333
|
+
*/
|
|
1334
|
+
function encodeFloat64(inp) {
|
|
1335
|
+
dataView.setFloat64(0, inp, false);
|
|
1336
|
+
}
|
|
1337
|
+
/**
|
|
1338
|
+
* @param {Uint8Array} ui8a
|
|
1339
|
+
* @param {number} pos
|
|
1340
|
+
* @returns {number}
|
|
1341
|
+
*/
|
|
1342
|
+
function readFloat64(ui8a, pos) {
|
|
1343
|
+
if (ui8a.length - pos < 8) throw new Error(`${decodeErrPrefix} not enough data for float64`);
|
|
1344
|
+
const offset = (ui8a.byteOffset || 0) + pos;
|
|
1345
|
+
return new DataView(ui8a.buffer, offset, 8).getFloat64(0, false);
|
|
1346
|
+
}
|
|
1347
|
+
/**
|
|
1348
|
+
* @param {Token} _tok1
|
|
1349
|
+
* @param {Token} _tok2
|
|
1350
|
+
* @returns {number}
|
|
1351
|
+
*/
|
|
1352
|
+
encodeFloat.compareTokens = encodeUint.compareTokens;
|
|
1353
|
+
//#endregion
|
|
1354
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/jump.js
|
|
1355
|
+
/**
|
|
1356
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
1357
|
+
* @typedef {import('../interface.js').AllocatedByteView} AllocatedByteView
|
|
1358
|
+
*/
|
|
1359
|
+
/**
|
|
1360
|
+
* @param {Uint8Array} data
|
|
1361
|
+
* @param {number} pos
|
|
1362
|
+
* @param {number} minor
|
|
1363
|
+
*/
|
|
1364
|
+
function invalidMinor(data, pos, minor) {
|
|
1365
|
+
throw new Error(`${decodeErrPrefix} encountered invalid minor (${minor}) for major ${data[pos] >>> 5}`);
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* @param {string} msg
|
|
1369
|
+
* @returns {()=>any}
|
|
1370
|
+
*/
|
|
1371
|
+
function errorer(msg) {
|
|
1372
|
+
return () => {
|
|
1373
|
+
throw new Error(`${decodeErrPrefix} ${msg}`);
|
|
1374
|
+
};
|
|
1375
|
+
}
|
|
1376
|
+
/** @type {((data:Uint8Array, pos:number, minor:number, options?:DecodeOptions) => any)[]} */
|
|
1377
|
+
const jump = [];
|
|
1378
|
+
for (let i = 0; i <= 23; i++) jump[i] = invalidMinor;
|
|
1379
|
+
jump[24] = decodeUint8;
|
|
1380
|
+
jump[25] = decodeUint16;
|
|
1381
|
+
jump[26] = decodeUint32;
|
|
1382
|
+
jump[27] = decodeUint64;
|
|
1383
|
+
jump[28] = invalidMinor;
|
|
1384
|
+
jump[29] = invalidMinor;
|
|
1385
|
+
jump[30] = invalidMinor;
|
|
1386
|
+
jump[31] = invalidMinor;
|
|
1387
|
+
for (let i = 32; i <= 55; i++) jump[i] = invalidMinor;
|
|
1388
|
+
jump[56] = decodeNegint8;
|
|
1389
|
+
jump[57] = decodeNegint16;
|
|
1390
|
+
jump[58] = decodeNegint32;
|
|
1391
|
+
jump[59] = decodeNegint64;
|
|
1392
|
+
jump[60] = invalidMinor;
|
|
1393
|
+
jump[61] = invalidMinor;
|
|
1394
|
+
jump[62] = invalidMinor;
|
|
1395
|
+
jump[63] = invalidMinor;
|
|
1396
|
+
for (let i = 64; i <= 87; i++) jump[i] = decodeBytesCompact;
|
|
1397
|
+
jump[88] = decodeBytes8;
|
|
1398
|
+
jump[89] = decodeBytes16;
|
|
1399
|
+
jump[90] = decodeBytes32;
|
|
1400
|
+
jump[91] = decodeBytes64;
|
|
1401
|
+
jump[92] = invalidMinor;
|
|
1402
|
+
jump[93] = invalidMinor;
|
|
1403
|
+
jump[94] = invalidMinor;
|
|
1404
|
+
jump[95] = errorer("indefinite length bytes/strings are not supported");
|
|
1405
|
+
for (let i = 96; i <= 119; i++) jump[i] = decodeStringCompact;
|
|
1406
|
+
jump[120] = decodeString8;
|
|
1407
|
+
jump[121] = decodeString16;
|
|
1408
|
+
jump[122] = decodeString32;
|
|
1409
|
+
jump[123] = decodeString64;
|
|
1410
|
+
jump[124] = invalidMinor;
|
|
1411
|
+
jump[125] = invalidMinor;
|
|
1412
|
+
jump[126] = invalidMinor;
|
|
1413
|
+
jump[127] = errorer("indefinite length bytes/strings are not supported");
|
|
1414
|
+
for (let i = 128; i <= 151; i++) jump[i] = decodeArrayCompact;
|
|
1415
|
+
jump[152] = decodeArray8;
|
|
1416
|
+
jump[153] = decodeArray16;
|
|
1417
|
+
jump[154] = decodeArray32;
|
|
1418
|
+
jump[155] = decodeArray64;
|
|
1419
|
+
jump[156] = invalidMinor;
|
|
1420
|
+
jump[157] = invalidMinor;
|
|
1421
|
+
jump[158] = invalidMinor;
|
|
1422
|
+
jump[159] = decodeArrayIndefinite;
|
|
1423
|
+
for (let i = 160; i <= 183; i++) jump[i] = decodeMapCompact;
|
|
1424
|
+
jump[184] = decodeMap8;
|
|
1425
|
+
jump[185] = decodeMap16;
|
|
1426
|
+
jump[186] = decodeMap32;
|
|
1427
|
+
jump[187] = decodeMap64;
|
|
1428
|
+
jump[188] = invalidMinor;
|
|
1429
|
+
jump[189] = invalidMinor;
|
|
1430
|
+
jump[190] = invalidMinor;
|
|
1431
|
+
jump[191] = decodeMapIndefinite;
|
|
1432
|
+
for (let i = 192; i <= 215; i++) jump[i] = decodeTagCompact;
|
|
1433
|
+
jump[216] = decodeTag8;
|
|
1434
|
+
jump[217] = decodeTag16;
|
|
1435
|
+
jump[218] = decodeTag32;
|
|
1436
|
+
jump[219] = decodeTag64;
|
|
1437
|
+
jump[220] = invalidMinor;
|
|
1438
|
+
jump[221] = invalidMinor;
|
|
1439
|
+
jump[222] = invalidMinor;
|
|
1440
|
+
jump[223] = invalidMinor;
|
|
1441
|
+
for (let i = 224; i <= 243; i++) jump[i] = errorer("simple values are not supported");
|
|
1442
|
+
jump[244] = invalidMinor;
|
|
1443
|
+
jump[245] = invalidMinor;
|
|
1444
|
+
jump[246] = invalidMinor;
|
|
1445
|
+
jump[247] = decodeUndefined;
|
|
1446
|
+
jump[248] = errorer("simple values are not supported");
|
|
1447
|
+
jump[249] = decodeFloat16;
|
|
1448
|
+
jump[250] = decodeFloat32;
|
|
1449
|
+
jump[251] = decodeFloat64;
|
|
1450
|
+
jump[252] = invalidMinor;
|
|
1451
|
+
jump[253] = invalidMinor;
|
|
1452
|
+
jump[254] = invalidMinor;
|
|
1453
|
+
jump[255] = decodeBreak;
|
|
1454
|
+
/** @type {Token[]} */
|
|
1455
|
+
const quick = [];
|
|
1456
|
+
for (let i = 0; i < 24; i++) quick[i] = new Token(Type.uint, i, 1);
|
|
1457
|
+
for (let i = -1; i >= -24; i--) quick[31 - i] = new Token(Type.negint, i, 1);
|
|
1458
|
+
quick[64] = new Token(Type.bytes, new Uint8Array(0), 1);
|
|
1459
|
+
quick[96] = new Token(Type.string, "", 1);
|
|
1460
|
+
quick[128] = new Token(Type.array, 0, 1);
|
|
1461
|
+
quick[160] = new Token(Type.map, 0, 1);
|
|
1462
|
+
quick[244] = new Token(Type.false, false, 1);
|
|
1463
|
+
quick[245] = new Token(Type.true, true, 1);
|
|
1464
|
+
quick[246] = new Token(Type.null, null, 1);
|
|
1465
|
+
/**
|
|
1466
|
+
* @param {Token} token
|
|
1467
|
+
* @returns {AllocatedByteView|undefined}
|
|
1468
|
+
*/
|
|
1469
|
+
function quickEncodeToken(token) {
|
|
1470
|
+
switch (token.type) {
|
|
1471
|
+
case Type.false: return fromArray([244]);
|
|
1472
|
+
case Type.true: return fromArray([245]);
|
|
1473
|
+
case Type.null: return fromArray([246]);
|
|
1474
|
+
case Type.bytes:
|
|
1475
|
+
if (!token.value.length) return fromArray([64]);
|
|
1476
|
+
return;
|
|
1477
|
+
case Type.string:
|
|
1478
|
+
if (token.value === "") return fromArray([96]);
|
|
1479
|
+
return;
|
|
1480
|
+
case Type.array:
|
|
1481
|
+
if (token.value === 0) return fromArray([128]);
|
|
1482
|
+
return;
|
|
1483
|
+
case Type.map:
|
|
1484
|
+
if (token.value === 0) return fromArray([160]);
|
|
1485
|
+
return;
|
|
1486
|
+
case Type.uint:
|
|
1487
|
+
if (token.value < 24) return fromArray([Number(token.value)]);
|
|
1488
|
+
return;
|
|
1489
|
+
case Type.negint: if (token.value >= -24) return fromArray([31 - Number(token.value)]);
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
//#endregion
|
|
1493
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/encode.js
|
|
1494
|
+
/**
|
|
1495
|
+
* @typedef {import('../interface.js').EncodeOptions} EncodeOptions
|
|
1496
|
+
* @typedef {import('../interface.js').OptionalTypeEncoder} OptionalTypeEncoder
|
|
1497
|
+
* @typedef {import('../interface.js').Reference} Reference
|
|
1498
|
+
* @typedef {import('../interface.js').StrictTypeEncoder} StrictTypeEncoder
|
|
1499
|
+
* @typedef {import('../interface.js').TokenTypeEncoder} TokenTypeEncoder
|
|
1500
|
+
* @typedef {import('../interface.js').TokenOrNestedTokens} TokenOrNestedTokens
|
|
1501
|
+
* @typedef {import('../interface.js').ByteWriter} ByteWriter
|
|
1502
|
+
* @typedef {import('../interface.js').ByteView} ByteView
|
|
1503
|
+
* @typedef {import('../interface.js').AllocatedByteView} AllocatedByteView
|
|
1504
|
+
*/
|
|
1505
|
+
/** @type {EncodeOptions} */
|
|
1506
|
+
const defaultEncodeOptions = {
|
|
1507
|
+
float64: false,
|
|
1508
|
+
mapSorter,
|
|
1509
|
+
quickEncodeToken
|
|
1510
|
+
};
|
|
1511
|
+
/** @type {EncodeOptions} */
|
|
1512
|
+
const rfc8949EncodeOptions = Object.freeze({
|
|
1513
|
+
mapSorter: rfc8949MapSorter,
|
|
1514
|
+
quickEncodeToken
|
|
1515
|
+
});
|
|
1516
|
+
/** @returns {TokenTypeEncoder[]} */
|
|
1517
|
+
function makeCborEncoders() {
|
|
1518
|
+
const encoders = [];
|
|
1519
|
+
encoders[Type.uint.major] = encodeUint;
|
|
1520
|
+
encoders[Type.negint.major] = encodeNegint;
|
|
1521
|
+
encoders[Type.bytes.major] = encodeBytes;
|
|
1522
|
+
encoders[Type.string.major] = encodeString;
|
|
1523
|
+
encoders[Type.array.major] = encodeArray;
|
|
1524
|
+
encoders[Type.map.major] = encodeMap;
|
|
1525
|
+
encoders[Type.tag.major] = encodeTag;
|
|
1526
|
+
encoders[Type.float.major] = encodeFloat;
|
|
1527
|
+
return encoders;
|
|
1528
|
+
}
|
|
1529
|
+
const cborEncoders = makeCborEncoders();
|
|
1530
|
+
const defaultWriter = new Bl();
|
|
1531
|
+
/** @implements {Reference} */
|
|
1532
|
+
var Ref = class Ref {
|
|
1533
|
+
/**
|
|
1534
|
+
* @param {object|any[]} obj
|
|
1535
|
+
* @param {Reference|undefined} parent
|
|
1536
|
+
*/
|
|
1537
|
+
constructor(obj, parent) {
|
|
1538
|
+
this.obj = obj;
|
|
1539
|
+
this.parent = parent;
|
|
1540
|
+
}
|
|
1541
|
+
/**
|
|
1542
|
+
* @param {object|any[]} obj
|
|
1543
|
+
* @returns {boolean}
|
|
1544
|
+
*/
|
|
1545
|
+
includes(obj) {
|
|
1546
|
+
/** @type {Reference|undefined} */
|
|
1547
|
+
let p = this;
|
|
1548
|
+
do
|
|
1549
|
+
if (p.obj === obj) return true;
|
|
1550
|
+
while (p = p.parent);
|
|
1551
|
+
return false;
|
|
1552
|
+
}
|
|
1553
|
+
/**
|
|
1554
|
+
* @param {Reference|undefined} stack
|
|
1555
|
+
* @param {object|any[]} obj
|
|
1556
|
+
* @returns {Reference}
|
|
1557
|
+
*/
|
|
1558
|
+
static createCheck(stack, obj) {
|
|
1559
|
+
if (stack && stack.includes(obj)) throw new Error(`${encodeErrPrefix} object contains circular references`);
|
|
1560
|
+
return new Ref(obj, stack);
|
|
1561
|
+
}
|
|
1562
|
+
};
|
|
1563
|
+
const simpleTokens = {
|
|
1564
|
+
null: new Token(Type.null, null),
|
|
1565
|
+
undefined: new Token(Type.undefined, void 0),
|
|
1566
|
+
true: new Token(Type.true, true),
|
|
1567
|
+
false: new Token(Type.false, false),
|
|
1568
|
+
emptyArray: new Token(Type.array, 0),
|
|
1569
|
+
emptyMap: new Token(Type.map, 0)
|
|
1570
|
+
};
|
|
1571
|
+
/** @type {{[typeName: string]: StrictTypeEncoder}} */
|
|
1572
|
+
const typeEncoders = {
|
|
1573
|
+
/**
|
|
1574
|
+
* @param {any} obj
|
|
1575
|
+
* @param {string} _typ
|
|
1576
|
+
* @param {EncodeOptions} _options
|
|
1577
|
+
* @param {Reference} [_refStack]
|
|
1578
|
+
* @returns {TokenOrNestedTokens}
|
|
1579
|
+
*/
|
|
1580
|
+
number(obj, _typ, _options, _refStack) {
|
|
1581
|
+
if (!Number.isInteger(obj) || !Number.isSafeInteger(obj)) return new Token(Type.float, obj);
|
|
1582
|
+
else if (obj >= 0) return new Token(Type.uint, obj);
|
|
1583
|
+
else return new Token(Type.negint, obj);
|
|
1584
|
+
},
|
|
1585
|
+
/**
|
|
1586
|
+
* @param {any} obj
|
|
1587
|
+
* @param {string} _typ
|
|
1588
|
+
* @param {EncodeOptions} _options
|
|
1589
|
+
* @param {Reference} [_refStack]
|
|
1590
|
+
* @returns {TokenOrNestedTokens}
|
|
1591
|
+
*/
|
|
1592
|
+
bigint(obj, _typ, _options, _refStack) {
|
|
1593
|
+
if (obj >= BigInt(0)) return new Token(Type.uint, obj);
|
|
1594
|
+
else return new Token(Type.negint, obj);
|
|
1595
|
+
},
|
|
1596
|
+
/**
|
|
1597
|
+
* @param {any} obj
|
|
1598
|
+
* @param {string} _typ
|
|
1599
|
+
* @param {EncodeOptions} _options
|
|
1600
|
+
* @param {Reference} [_refStack]
|
|
1601
|
+
* @returns {TokenOrNestedTokens}
|
|
1602
|
+
*/
|
|
1603
|
+
Uint8Array(obj, _typ, _options, _refStack) {
|
|
1604
|
+
return new Token(Type.bytes, obj);
|
|
1605
|
+
},
|
|
1606
|
+
/**
|
|
1607
|
+
* @param {any} obj
|
|
1608
|
+
* @param {string} _typ
|
|
1609
|
+
* @param {EncodeOptions} _options
|
|
1610
|
+
* @param {Reference} [_refStack]
|
|
1611
|
+
* @returns {TokenOrNestedTokens}
|
|
1612
|
+
*/
|
|
1613
|
+
string(obj, _typ, _options, _refStack) {
|
|
1614
|
+
return new Token(Type.string, obj);
|
|
1615
|
+
},
|
|
1616
|
+
/**
|
|
1617
|
+
* @param {any} obj
|
|
1618
|
+
* @param {string} _typ
|
|
1619
|
+
* @param {EncodeOptions} _options
|
|
1620
|
+
* @param {Reference} [_refStack]
|
|
1621
|
+
* @returns {TokenOrNestedTokens}
|
|
1622
|
+
*/
|
|
1623
|
+
boolean(obj, _typ, _options, _refStack) {
|
|
1624
|
+
return obj ? simpleTokens.true : simpleTokens.false;
|
|
1625
|
+
},
|
|
1626
|
+
/**
|
|
1627
|
+
* @param {any} _obj
|
|
1628
|
+
* @param {string} _typ
|
|
1629
|
+
* @param {EncodeOptions} _options
|
|
1630
|
+
* @param {Reference} [_refStack]
|
|
1631
|
+
* @returns {TokenOrNestedTokens}
|
|
1632
|
+
*/
|
|
1633
|
+
null(_obj, _typ, _options, _refStack) {
|
|
1634
|
+
return simpleTokens.null;
|
|
1635
|
+
},
|
|
1636
|
+
/**
|
|
1637
|
+
* @param {any} _obj
|
|
1638
|
+
* @param {string} _typ
|
|
1639
|
+
* @param {EncodeOptions} _options
|
|
1640
|
+
* @param {Reference} [_refStack]
|
|
1641
|
+
* @returns {TokenOrNestedTokens}
|
|
1642
|
+
*/
|
|
1643
|
+
undefined(_obj, _typ, _options, _refStack) {
|
|
1644
|
+
return simpleTokens.undefined;
|
|
1645
|
+
},
|
|
1646
|
+
/**
|
|
1647
|
+
* @param {any} obj
|
|
1648
|
+
* @param {string} _typ
|
|
1649
|
+
* @param {EncodeOptions} _options
|
|
1650
|
+
* @param {Reference} [_refStack]
|
|
1651
|
+
* @returns {TokenOrNestedTokens}
|
|
1652
|
+
*/
|
|
1653
|
+
ArrayBuffer(obj, _typ, _options, _refStack) {
|
|
1654
|
+
return new Token(Type.bytes, new Uint8Array(obj));
|
|
1655
|
+
},
|
|
1656
|
+
/**
|
|
1657
|
+
* @param {any} obj
|
|
1658
|
+
* @param {string} _typ
|
|
1659
|
+
* @param {EncodeOptions} _options
|
|
1660
|
+
* @param {Reference} [_refStack]
|
|
1661
|
+
* @returns {TokenOrNestedTokens}
|
|
1662
|
+
*/
|
|
1663
|
+
DataView(obj, _typ, _options, _refStack) {
|
|
1664
|
+
return new Token(Type.bytes, new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength));
|
|
1665
|
+
},
|
|
1666
|
+
/**
|
|
1667
|
+
* @param {any} obj
|
|
1668
|
+
* @param {string} _typ
|
|
1669
|
+
* @param {EncodeOptions} options
|
|
1670
|
+
* @param {Reference} [refStack]
|
|
1671
|
+
* @returns {TokenOrNestedTokens}
|
|
1672
|
+
*/
|
|
1673
|
+
Array(obj, _typ, options, refStack) {
|
|
1674
|
+
if (!obj.length) {
|
|
1675
|
+
if (options.addBreakTokens === true) return [simpleTokens.emptyArray, new Token(Type.break)];
|
|
1676
|
+
return simpleTokens.emptyArray;
|
|
1677
|
+
}
|
|
1678
|
+
refStack = Ref.createCheck(refStack, obj);
|
|
1679
|
+
const entries = [];
|
|
1680
|
+
let i = 0;
|
|
1681
|
+
for (const e of obj) entries[i++] = objectToTokens(e, options, refStack);
|
|
1682
|
+
if (options.addBreakTokens) return [
|
|
1683
|
+
new Token(Type.array, obj.length),
|
|
1684
|
+
entries,
|
|
1685
|
+
new Token(Type.break)
|
|
1686
|
+
];
|
|
1687
|
+
return [new Token(Type.array, obj.length), entries];
|
|
1688
|
+
},
|
|
1689
|
+
/**
|
|
1690
|
+
* @param {any} obj
|
|
1691
|
+
* @param {string} typ
|
|
1692
|
+
* @param {EncodeOptions} options
|
|
1693
|
+
* @param {Reference} [refStack]
|
|
1694
|
+
* @returns {TokenOrNestedTokens}
|
|
1695
|
+
*/
|
|
1696
|
+
Object(obj, typ, options, refStack) {
|
|
1697
|
+
const isMap = typ !== "Object";
|
|
1698
|
+
const keys = isMap ? obj.keys() : Object.keys(obj);
|
|
1699
|
+
const maxLength = isMap ? obj.size : keys.length;
|
|
1700
|
+
/** @type {undefined | [TokenOrNestedTokens, TokenOrNestedTokens][]} */
|
|
1701
|
+
let entries;
|
|
1702
|
+
if (maxLength) {
|
|
1703
|
+
entries = new Array(maxLength);
|
|
1704
|
+
refStack = Ref.createCheck(refStack, obj);
|
|
1705
|
+
const skipUndefined = !isMap && options.ignoreUndefinedProperties;
|
|
1706
|
+
let i = 0;
|
|
1707
|
+
for (const key of keys) {
|
|
1708
|
+
const value = isMap ? obj.get(key) : obj[key];
|
|
1709
|
+
if (skipUndefined && value === void 0) continue;
|
|
1710
|
+
entries[i++] = [objectToTokens(key, options, refStack), objectToTokens(value, options, refStack)];
|
|
1711
|
+
}
|
|
1712
|
+
if (i < maxLength) entries.length = i;
|
|
1713
|
+
}
|
|
1714
|
+
if (!entries?.length) {
|
|
1715
|
+
if (options.addBreakTokens === true) return [simpleTokens.emptyMap, new Token(Type.break)];
|
|
1716
|
+
return simpleTokens.emptyMap;
|
|
1717
|
+
}
|
|
1718
|
+
sortMapEntries(entries, options);
|
|
1719
|
+
if (options.addBreakTokens) return [
|
|
1720
|
+
new Token(Type.map, entries.length),
|
|
1721
|
+
entries,
|
|
1722
|
+
new Token(Type.break)
|
|
1723
|
+
];
|
|
1724
|
+
return [new Token(Type.map, entries.length), entries];
|
|
1725
|
+
},
|
|
1726
|
+
/**
|
|
1727
|
+
* Encode a `Tagged` wrapper as a CBOR tag header followed by the encoded
|
|
1728
|
+
* form of the wrapped value. The value is recursively tokenised through
|
|
1729
|
+
* `objectToTokens()` so any registered `typeEncoders` apply to it.
|
|
1730
|
+
*
|
|
1731
|
+
* @param {any} obj
|
|
1732
|
+
* @param {string} _typ
|
|
1733
|
+
* @param {EncodeOptions} options
|
|
1734
|
+
* @param {Reference} [refStack]
|
|
1735
|
+
* @returns {TokenOrNestedTokens}
|
|
1736
|
+
*/
|
|
1737
|
+
Tagged(obj, _typ, options, refStack) {
|
|
1738
|
+
return [new Token(Type.tag, obj.tag), objectToTokens(obj.value, options, refStack)];
|
|
1739
|
+
}
|
|
1740
|
+
};
|
|
1741
|
+
typeEncoders.Map = typeEncoders.Object;
|
|
1742
|
+
typeEncoders.Buffer = typeEncoders.Uint8Array;
|
|
1743
|
+
for (const typ of "Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt64 Float32 Float64".split(" ")) typeEncoders[`${typ}Array`] = typeEncoders.DataView;
|
|
1744
|
+
/**
|
|
1745
|
+
* @param {any} obj
|
|
1746
|
+
* @param {EncodeOptions} [options]
|
|
1747
|
+
* @param {Reference} [refStack]
|
|
1748
|
+
* @returns {TokenOrNestedTokens}
|
|
1749
|
+
*/
|
|
1750
|
+
function objectToTokens(obj, options = {}, refStack) {
|
|
1751
|
+
const typ = is(obj);
|
|
1752
|
+
const customTypeEncoder = options && options.typeEncoders && options.typeEncoders[typ] || typeEncoders[typ];
|
|
1753
|
+
if (typeof customTypeEncoder === "function") {
|
|
1754
|
+
const tokens = customTypeEncoder(obj, typ, options, refStack);
|
|
1755
|
+
if (tokens != null) return tokens;
|
|
1756
|
+
}
|
|
1757
|
+
const typeEncoder = typeEncoders[typ];
|
|
1758
|
+
if (!typeEncoder) throw new Error(`${encodeErrPrefix} unsupported type: ${typ}`);
|
|
1759
|
+
return typeEncoder(obj, typ, options, refStack);
|
|
1760
|
+
}
|
|
1761
|
+
/**
|
|
1762
|
+
* @param {TokenOrNestedTokens[]} entries
|
|
1763
|
+
* @param {EncodeOptions} options
|
|
1764
|
+
*/
|
|
1765
|
+
function sortMapEntries(entries, options) {
|
|
1766
|
+
if (options.mapSorter) entries.sort(options.mapSorter);
|
|
1767
|
+
}
|
|
1768
|
+
/**
|
|
1769
|
+
* @param {(Token|Token[])[]} e1
|
|
1770
|
+
* @param {(Token|Token[])[]} e2
|
|
1771
|
+
* @returns {number}
|
|
1772
|
+
*/
|
|
1773
|
+
function mapSorter(e1, e2) {
|
|
1774
|
+
const keyToken1 = Array.isArray(e1[0]) ? e1[0][0] : e1[0];
|
|
1775
|
+
const keyToken2 = Array.isArray(e2[0]) ? e2[0][0] : e2[0];
|
|
1776
|
+
if (keyToken1.type !== keyToken2.type) return keyToken1.type.compare(keyToken2.type);
|
|
1777
|
+
const tcmp = cborEncoders[keyToken1.type.major].compareTokens(keyToken1, keyToken2);
|
|
1778
|
+
if (tcmp === 0) console.warn("WARNING: complex key types used, CBOR key sorting guarantees are gone");
|
|
1779
|
+
return tcmp;
|
|
1780
|
+
}
|
|
1781
|
+
/**
|
|
1782
|
+
* @typedef {Token & { _keyBytes?: AllocatedByteView }} TokenEx
|
|
1783
|
+
*
|
|
1784
|
+
* @param {(Token|Token[])[]} e1
|
|
1785
|
+
* @param {(Token|Token[])[]} e2
|
|
1786
|
+
* @returns {number}
|
|
1787
|
+
*/
|
|
1788
|
+
function rfc8949MapSorter(e1, e2) {
|
|
1789
|
+
if (e1[0] instanceof Token && e2[0] instanceof Token) {
|
|
1790
|
+
const t1 = e1[0];
|
|
1791
|
+
const t2 = e2[0];
|
|
1792
|
+
if (!t1._keyBytes) t1._keyBytes = encodeRfc8949(t1.value);
|
|
1793
|
+
if (!t2._keyBytes) t2._keyBytes = encodeRfc8949(t2.value);
|
|
1794
|
+
return compare(t1._keyBytes, t2._keyBytes);
|
|
1795
|
+
}
|
|
1796
|
+
throw new Error("rfc8949MapSorter: complex key types are not supported yet");
|
|
1797
|
+
}
|
|
1798
|
+
/**
|
|
1799
|
+
* @param {any} data
|
|
1800
|
+
* @returns {AllocatedByteView}
|
|
1801
|
+
*/
|
|
1802
|
+
function encodeRfc8949(data) {
|
|
1803
|
+
return encodeCustom(data, cborEncoders, rfc8949EncodeOptions);
|
|
1804
|
+
}
|
|
1805
|
+
/**
|
|
1806
|
+
* @param {ByteWriter} writer
|
|
1807
|
+
* @param {TokenOrNestedTokens} tokens
|
|
1808
|
+
* @param {TokenTypeEncoder[]} encoders
|
|
1809
|
+
* @param {EncodeOptions} options
|
|
1810
|
+
*/
|
|
1811
|
+
function tokensToEncoded(writer, tokens, encoders, options) {
|
|
1812
|
+
if (Array.isArray(tokens)) for (const token of tokens) tokensToEncoded(writer, token, encoders, options);
|
|
1813
|
+
else encoders[tokens.type.major](writer, tokens, options);
|
|
1814
|
+
}
|
|
1815
|
+
const MAJOR_UINT = Type.uint.majorEncoded;
|
|
1816
|
+
const MAJOR_NEGINT = Type.negint.majorEncoded;
|
|
1817
|
+
const MAJOR_BYTES = Type.bytes.majorEncoded;
|
|
1818
|
+
const MAJOR_STRING = Type.string.majorEncoded;
|
|
1819
|
+
const MAJOR_ARRAY = Type.array.majorEncoded;
|
|
1820
|
+
const SIMPLE_FALSE = Type.float.majorEncoded | 20;
|
|
1821
|
+
const SIMPLE_TRUE = Type.float.majorEncoded | 21;
|
|
1822
|
+
const SIMPLE_NULL = Type.float.majorEncoded | 22;
|
|
1823
|
+
const SIMPLE_UNDEFINED = Type.float.majorEncoded | 23;
|
|
1824
|
+
const neg1b = BigInt(-1);
|
|
1825
|
+
const pos1b = BigInt(1);
|
|
1826
|
+
/**
|
|
1827
|
+
* Check if direct encoding can be used for the given options.
|
|
1828
|
+
* Direct encoding bypasses token creation for most values.
|
|
1829
|
+
* @param {EncodeOptions} options
|
|
1830
|
+
* @returns {boolean}
|
|
1831
|
+
*/
|
|
1832
|
+
function canDirectEncode(options) {
|
|
1833
|
+
return options.addBreakTokens !== true;
|
|
1834
|
+
}
|
|
1835
|
+
/**
|
|
1836
|
+
* Direct encode a value to the writer, bypassing token creation for most types.
|
|
1837
|
+
* Falls back to token-based encoding for custom type encoders.
|
|
1838
|
+
* @param {ByteWriter} writer
|
|
1839
|
+
* @param {any} data
|
|
1840
|
+
* @param {EncodeOptions} options
|
|
1841
|
+
* @param {Reference|undefined} refStack
|
|
1842
|
+
*/
|
|
1843
|
+
function directEncode(writer, data, options, refStack) {
|
|
1844
|
+
const typ = is(data);
|
|
1845
|
+
const customEncoder = options.typeEncoders && options.typeEncoders[typ];
|
|
1846
|
+
if (customEncoder) {
|
|
1847
|
+
const tokens = customEncoder(data, typ, options, refStack);
|
|
1848
|
+
if (tokens != null) {
|
|
1849
|
+
tokensToEncoded(writer, tokens, cborEncoders, options);
|
|
1850
|
+
return;
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
switch (typ) {
|
|
1854
|
+
case "null":
|
|
1855
|
+
writer.push([SIMPLE_NULL]);
|
|
1856
|
+
return;
|
|
1857
|
+
case "undefined":
|
|
1858
|
+
writer.push([SIMPLE_UNDEFINED]);
|
|
1859
|
+
return;
|
|
1860
|
+
case "boolean":
|
|
1861
|
+
writer.push([data ? SIMPLE_TRUE : SIMPLE_FALSE]);
|
|
1862
|
+
return;
|
|
1863
|
+
case "number":
|
|
1864
|
+
if (!Number.isInteger(data) || !Number.isSafeInteger(data)) encodeFloat(writer, new Token(Type.float, data), options);
|
|
1865
|
+
else if (data >= 0) encodeUintValue(writer, MAJOR_UINT, data);
|
|
1866
|
+
else encodeUintValue(writer, MAJOR_NEGINT, data * -1 - 1);
|
|
1867
|
+
return;
|
|
1868
|
+
case "bigint":
|
|
1869
|
+
if (data >= BigInt(0)) encodeUintValue(writer, MAJOR_UINT, data);
|
|
1870
|
+
else encodeUintValue(writer, MAJOR_NEGINT, data * neg1b - pos1b);
|
|
1871
|
+
return;
|
|
1872
|
+
case "string": {
|
|
1873
|
+
const bytes = fromString(data);
|
|
1874
|
+
encodeUintValue(writer, MAJOR_STRING, bytes.length);
|
|
1875
|
+
writer.push(bytes);
|
|
1876
|
+
return;
|
|
1877
|
+
}
|
|
1878
|
+
case "Uint8Array":
|
|
1879
|
+
encodeUintValue(writer, MAJOR_BYTES, data.length);
|
|
1880
|
+
writer.push(data);
|
|
1881
|
+
return;
|
|
1882
|
+
case "Array":
|
|
1883
|
+
if (!data.length) {
|
|
1884
|
+
writer.push([MAJOR_ARRAY]);
|
|
1885
|
+
return;
|
|
1886
|
+
}
|
|
1887
|
+
refStack = Ref.createCheck(refStack, data);
|
|
1888
|
+
encodeUintValue(writer, MAJOR_ARRAY, data.length);
|
|
1889
|
+
for (const elem of data) directEncode(writer, elem, options, refStack);
|
|
1890
|
+
return;
|
|
1891
|
+
case "Object":
|
|
1892
|
+
case "Map":
|
|
1893
|
+
tokensToEncoded(writer, typeEncoders.Object(data, typ, options, refStack), cborEncoders, options);
|
|
1894
|
+
return;
|
|
1895
|
+
default: {
|
|
1896
|
+
const typeEncoder = typeEncoders[typ];
|
|
1897
|
+
if (!typeEncoder) throw new Error(`${encodeErrPrefix} unsupported type: ${typ}`);
|
|
1898
|
+
tokensToEncoded(writer, typeEncoder(data, typ, options, refStack), cborEncoders, options);
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
/**
|
|
1903
|
+
* @template {ArrayBufferLike} T
|
|
1904
|
+
* @overload
|
|
1905
|
+
* @param {any} data
|
|
1906
|
+
* @param {TokenTypeEncoder[]} encoders
|
|
1907
|
+
* @param {EncodeOptions} options
|
|
1908
|
+
* @param {Uint8Array<T>} destination
|
|
1909
|
+
* @returns {Uint8Array<T>}
|
|
1910
|
+
*/
|
|
1911
|
+
/**
|
|
1912
|
+
* @overload
|
|
1913
|
+
* @param {any} data
|
|
1914
|
+
* @param {TokenTypeEncoder[]} encoders
|
|
1915
|
+
* @param {EncodeOptions} options
|
|
1916
|
+
* @returns {AllocatedByteView}
|
|
1917
|
+
*/
|
|
1918
|
+
/**
|
|
1919
|
+
* @param {any} data
|
|
1920
|
+
* @param {TokenTypeEncoder[]} encoders
|
|
1921
|
+
* @param {EncodeOptions} options
|
|
1922
|
+
* @param {ByteView} [destination]
|
|
1923
|
+
* @returns {ByteView}
|
|
1924
|
+
*/
|
|
1925
|
+
function encodeCustom(data, encoders, options, destination) {
|
|
1926
|
+
const hasDest = destination instanceof Uint8Array;
|
|
1927
|
+
let writeTo = hasDest ? new U8Bl(destination) : defaultWriter;
|
|
1928
|
+
const tokens = objectToTokens(data, options);
|
|
1929
|
+
if (!Array.isArray(tokens) && options.quickEncodeToken) {
|
|
1930
|
+
const quickBytes = options.quickEncodeToken(tokens);
|
|
1931
|
+
if (quickBytes) {
|
|
1932
|
+
if (hasDest) {
|
|
1933
|
+
writeTo.push(quickBytes);
|
|
1934
|
+
return writeTo.toBytes();
|
|
1935
|
+
}
|
|
1936
|
+
return quickBytes;
|
|
1937
|
+
}
|
|
1938
|
+
const encoder = encoders[tokens.type.major];
|
|
1939
|
+
if (encoder.encodedSize) {
|
|
1940
|
+
const size = encoder.encodedSize(tokens, options);
|
|
1941
|
+
if (!hasDest) writeTo = new Bl(size);
|
|
1942
|
+
encoder(writeTo, tokens, options);
|
|
1943
|
+
if (writeTo.chunks.length !== 1) throw new Error(`Unexpected error: pre-calculated length for ${tokens} was wrong`);
|
|
1944
|
+
return hasDest ? writeTo.toBytes() : asU8A(writeTo.chunks[0]);
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
writeTo.reset();
|
|
1948
|
+
tokensToEncoded(writeTo, tokens, encoders, options);
|
|
1949
|
+
return writeTo.toBytes(true);
|
|
1950
|
+
}
|
|
1951
|
+
/**
|
|
1952
|
+
* @param {any} data
|
|
1953
|
+
* @param {EncodeOptions} [options]
|
|
1954
|
+
* @returns {AllocatedByteView}
|
|
1955
|
+
*/
|
|
1956
|
+
function encode$1(data, options) {
|
|
1957
|
+
options = Object.assign({}, defaultEncodeOptions, options);
|
|
1958
|
+
if (canDirectEncode(options)) {
|
|
1959
|
+
defaultWriter.reset();
|
|
1960
|
+
directEncode(defaultWriter, data, options, void 0);
|
|
1961
|
+
return defaultWriter.toBytes(true);
|
|
1962
|
+
}
|
|
1963
|
+
return encodeCustom(data, cborEncoders, options);
|
|
1964
|
+
}
|
|
1965
|
+
//#endregion
|
|
1966
|
+
//#region node_modules/@ipld/dag-cbor/node_modules/cborg/lib/decode.js
|
|
1967
|
+
/**
|
|
1968
|
+
* @typedef {import('./token.js').Token} Token
|
|
1969
|
+
* @typedef {import('../interface.js').DecodeOptions} DecodeOptions
|
|
1970
|
+
* @typedef {import('../interface.js').DecodeTokenizer} DecodeTokenizer
|
|
1971
|
+
* @typedef {import('../interface.js').TagDecodeControl} TagDecodeControl
|
|
1972
|
+
*/
|
|
1973
|
+
const defaultDecodeOptions = {
|
|
1974
|
+
strict: false,
|
|
1975
|
+
allowIndefinite: true,
|
|
1976
|
+
allowUndefined: true,
|
|
1977
|
+
allowBigInt: true
|
|
1978
|
+
};
|
|
1979
|
+
/**
|
|
1980
|
+
* @implements {DecodeTokenizer}
|
|
1981
|
+
*/
|
|
1982
|
+
var Tokeniser = class {
|
|
1983
|
+
/**
|
|
1984
|
+
* @param {Uint8Array} data
|
|
1985
|
+
* @param {DecodeOptions} options
|
|
1986
|
+
*/
|
|
1987
|
+
constructor(data, options = {}) {
|
|
1988
|
+
this._pos = 0;
|
|
1989
|
+
this.data = data;
|
|
1990
|
+
this.options = options;
|
|
1991
|
+
}
|
|
1992
|
+
pos() {
|
|
1993
|
+
return this._pos;
|
|
1994
|
+
}
|
|
1995
|
+
done() {
|
|
1996
|
+
return this._pos >= this.data.length;
|
|
1997
|
+
}
|
|
1998
|
+
next() {
|
|
1999
|
+
const byt = this.data[this._pos];
|
|
2000
|
+
let token = quick[byt];
|
|
2001
|
+
if (token === void 0) {
|
|
2002
|
+
const decoder = jump[byt];
|
|
2003
|
+
if (!decoder) throw new Error(`${decodeErrPrefix} no decoder for major type ${byt >>> 5} (byte 0x${byt.toString(16).padStart(2, "0")})`);
|
|
2004
|
+
const minor = byt & 31;
|
|
2005
|
+
token = decoder(this.data, this._pos, minor, this.options);
|
|
2006
|
+
}
|
|
2007
|
+
this._pos += token.encodedLength;
|
|
2008
|
+
return token;
|
|
2009
|
+
}
|
|
2010
|
+
};
|
|
2011
|
+
const DONE = Symbol.for("DONE");
|
|
2012
|
+
const BREAK = Symbol.for("BREAK");
|
|
2013
|
+
/**
|
|
2014
|
+
* @param {Token} token
|
|
2015
|
+
* @param {DecodeTokenizer} tokeniser
|
|
2016
|
+
* @param {DecodeOptions} options
|
|
2017
|
+
* @returns {any|typeof BREAK|typeof DONE}
|
|
2018
|
+
*/
|
|
2019
|
+
function tokenToArray(token, tokeniser, options) {
|
|
2020
|
+
const arr = [];
|
|
2021
|
+
for (let i = 0; i < token.value; i++) {
|
|
2022
|
+
const value = tokensToObject(tokeniser, options);
|
|
2023
|
+
if (value === BREAK) {
|
|
2024
|
+
if (token.value === Infinity) break;
|
|
2025
|
+
throw new Error(`${decodeErrPrefix} got unexpected break to lengthed array`);
|
|
2026
|
+
}
|
|
2027
|
+
if (value === DONE) throw new Error(`${decodeErrPrefix} found array but not enough entries (got ${i}, expected ${token.value})`);
|
|
2028
|
+
arr[i] = value;
|
|
2029
|
+
}
|
|
2030
|
+
return arr;
|
|
2031
|
+
}
|
|
2032
|
+
/**
|
|
2033
|
+
* @param {Token} token
|
|
2034
|
+
* @param {DecodeTokenizer} tokeniser
|
|
2035
|
+
* @param {DecodeOptions} options
|
|
2036
|
+
* @returns {any|typeof BREAK|typeof DONE}
|
|
2037
|
+
*/
|
|
2038
|
+
function tokenToMap(token, tokeniser, options) {
|
|
2039
|
+
const useMaps = options.useMaps === true;
|
|
2040
|
+
const rejectDuplicateMapKeys = options.rejectDuplicateMapKeys === true;
|
|
2041
|
+
const obj = useMaps ? void 0 : {};
|
|
2042
|
+
const m = useMaps ? /* @__PURE__ */ new Map() : void 0;
|
|
2043
|
+
for (let i = 0; i < token.value; i++) {
|
|
2044
|
+
const key = tokensToObject(tokeniser, options);
|
|
2045
|
+
if (key === BREAK) {
|
|
2046
|
+
if (token.value === Infinity) break;
|
|
2047
|
+
throw new Error(`${decodeErrPrefix} got unexpected break to lengthed map`);
|
|
2048
|
+
}
|
|
2049
|
+
if (key === DONE) throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no key], expected ${token.value})`);
|
|
2050
|
+
if (!useMaps && typeof key !== "string") throw new Error(`${decodeErrPrefix} non-string keys not supported (got ${typeof key})`);
|
|
2051
|
+
if (rejectDuplicateMapKeys) {
|
|
2052
|
+
if (useMaps && m.has(key) || !useMaps && Object.hasOwn(obj, key)) throw new Error(`${decodeErrPrefix} found repeat map key "${key}"`);
|
|
2053
|
+
}
|
|
2054
|
+
const value = tokensToObject(tokeniser, options);
|
|
2055
|
+
if (value === DONE) throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no value], expected ${token.value})`);
|
|
2056
|
+
if (useMaps) m.set(key, value);
|
|
2057
|
+
else obj[key] = value;
|
|
2058
|
+
}
|
|
2059
|
+
return useMaps ? m : obj;
|
|
2060
|
+
}
|
|
2061
|
+
/**
|
|
2062
|
+
* Generator that yields [key, value] entries from a CBOR map token.
|
|
2063
|
+
* Used by tag decoders that need to preserve key types (e.g., Tag 259 Map).
|
|
2064
|
+
* @param {Token} token - The map token
|
|
2065
|
+
* @param {DecodeTokenizer} tokeniser
|
|
2066
|
+
* @param {DecodeOptions} options
|
|
2067
|
+
* @returns {Generator<[any, any], void, unknown>}
|
|
2068
|
+
*/
|
|
2069
|
+
function* tokenToMapEntries(token, tokeniser, options) {
|
|
2070
|
+
for (let i = 0; i < token.value; i++) {
|
|
2071
|
+
const key = tokensToObject(tokeniser, options);
|
|
2072
|
+
if (key === BREAK) {
|
|
2073
|
+
if (token.value === Infinity) break;
|
|
2074
|
+
throw new Error(`${decodeErrPrefix} got unexpected break to lengthed map`);
|
|
2075
|
+
}
|
|
2076
|
+
if (key === DONE) throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no key], expected ${token.value})`);
|
|
2077
|
+
const value = tokensToObject(tokeniser, options);
|
|
2078
|
+
if (value === DONE) throw new Error(`${decodeErrPrefix} found map but not enough entries (got ${i} [no value], expected ${token.value})`);
|
|
2079
|
+
yield [key, value];
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
/**
|
|
2083
|
+
* Creates a TagDecodeControl object for tag decoders.
|
|
2084
|
+
* @param {DecodeTokenizer} tokeniser
|
|
2085
|
+
* @param {DecodeOptions} options
|
|
2086
|
+
* @returns {TagDecodeControl}
|
|
2087
|
+
*/
|
|
2088
|
+
function createTagDecodeControl(tokeniser, options) {
|
|
2089
|
+
let called = false;
|
|
2090
|
+
/**
|
|
2091
|
+
* @type {TagDecodeControl}
|
|
2092
|
+
*/
|
|
2093
|
+
const decode = function() {
|
|
2094
|
+
if (called) throw new Error(`${decodeErrPrefix} tag decode() may only be called once`);
|
|
2095
|
+
called = true;
|
|
2096
|
+
const value = tokensToObject(tokeniser, options);
|
|
2097
|
+
if (value === DONE) throw new Error(`${decodeErrPrefix} tag content missing`);
|
|
2098
|
+
if (value === BREAK) throw new Error(`${decodeErrPrefix} got unexpected break in tag content`);
|
|
2099
|
+
return value;
|
|
2100
|
+
};
|
|
2101
|
+
decode.entries = function() {
|
|
2102
|
+
if (called) throw new Error(`${decodeErrPrefix} tag decode() may only be called once`);
|
|
2103
|
+
called = true;
|
|
2104
|
+
const token = tokeniser.next();
|
|
2105
|
+
if (!Type.equals(token.type, Type.map)) throw new Error(`${decodeErrPrefix} entries() requires map content, got ${token.type.name}`);
|
|
2106
|
+
const entries = [];
|
|
2107
|
+
for (const entry of tokenToMapEntries(token, tokeniser, options)) entries.push(entry);
|
|
2108
|
+
return entries;
|
|
2109
|
+
};
|
|
2110
|
+
Object.defineProperty(decode, "_called", {
|
|
2111
|
+
get() {
|
|
2112
|
+
return called;
|
|
2113
|
+
},
|
|
2114
|
+
enumerable: false
|
|
2115
|
+
});
|
|
2116
|
+
return decode;
|
|
2117
|
+
}
|
|
2118
|
+
/**
|
|
2119
|
+
* @param {DecodeTokenizer} tokeniser
|
|
2120
|
+
* @param {DecodeOptions} options
|
|
2121
|
+
* @returns {any|typeof BREAK|typeof DONE}
|
|
2122
|
+
*/
|
|
2123
|
+
function tokensToObject(tokeniser, options) {
|
|
2124
|
+
if (tokeniser.done()) return DONE;
|
|
2125
|
+
const token = tokeniser.next();
|
|
2126
|
+
if (Type.equals(token.type, Type.break)) return BREAK;
|
|
2127
|
+
if (token.type.terminal) return token.value;
|
|
2128
|
+
if (Type.equals(token.type, Type.array)) return tokenToArray(token, tokeniser, options);
|
|
2129
|
+
if (Type.equals(token.type, Type.map)) return tokenToMap(token, tokeniser, options);
|
|
2130
|
+
if (Type.equals(token.type, Type.tag)) {
|
|
2131
|
+
if (options.tags && typeof options.tags[token.value] === "function") {
|
|
2132
|
+
const decodeControl = createTagDecodeControl(tokeniser, options);
|
|
2133
|
+
const result = options.tags[token.value](decodeControl);
|
|
2134
|
+
if (!decodeControl._called) throw new Error(`${decodeErrPrefix} tag decoder must call decode() or entries()`);
|
|
2135
|
+
return result;
|
|
2136
|
+
}
|
|
2137
|
+
throw new Error(`${decodeErrPrefix} tag not supported (${token.value})`);
|
|
2138
|
+
}
|
|
2139
|
+
throw new Error("unsupported");
|
|
2140
|
+
}
|
|
2141
|
+
/**
|
|
2142
|
+
* @param {Uint8Array} data
|
|
2143
|
+
* @param {DecodeOptions} [options]
|
|
2144
|
+
* @returns {[any, Uint8Array]}
|
|
2145
|
+
*/
|
|
2146
|
+
function decodeFirst(data, options) {
|
|
2147
|
+
if (!(data instanceof Uint8Array)) throw new Error(`${decodeErrPrefix} data to decode must be a Uint8Array`);
|
|
2148
|
+
options = Object.assign({}, defaultDecodeOptions, options);
|
|
2149
|
+
const u8aData = asU8A(data);
|
|
2150
|
+
const tokeniser = options.tokenizer || new Tokeniser(u8aData, options);
|
|
2151
|
+
const decoded = tokensToObject(tokeniser, options);
|
|
2152
|
+
if (decoded === DONE) throw new Error(`${decodeErrPrefix} did not find any content to decode`);
|
|
2153
|
+
if (decoded === BREAK) throw new Error(`${decodeErrPrefix} got unexpected break`);
|
|
2154
|
+
return [decoded, data.subarray(tokeniser.pos())];
|
|
2155
|
+
}
|
|
2156
|
+
/**
|
|
2157
|
+
* @param {Uint8Array} data
|
|
2158
|
+
* @param {DecodeOptions} [options]
|
|
2159
|
+
* @returns {any}
|
|
2160
|
+
*/
|
|
2161
|
+
function decode$1(data, options) {
|
|
2162
|
+
const [decoded, remainder] = decodeFirst(data, options);
|
|
2163
|
+
if (remainder.length > 0) throw new Error(`${decodeErrPrefix} too many terminals, data makes no sense`);
|
|
2164
|
+
return decoded;
|
|
2165
|
+
}
|
|
2166
|
+
Object.defineProperty(class Tagged {
|
|
2167
|
+
/**
|
|
2168
|
+
* @param {number} tag - CBOR tag number, a non-negative integer
|
|
2169
|
+
* @param {any} value - The value to be tagged; encoded recursively
|
|
2170
|
+
*/
|
|
2171
|
+
constructor(tag, value) {
|
|
2172
|
+
if (typeof tag !== "number" || !Number.isInteger(tag) || tag < 0) throw new TypeError("Tagged: tag must be a non-negative integer");
|
|
2173
|
+
this.tag = tag;
|
|
2174
|
+
this.value = value;
|
|
2175
|
+
}
|
|
2176
|
+
/**
|
|
2177
|
+
* Build a tag decoder for use in `decode()`'s `tags` option that returns the
|
|
2178
|
+
* decoded content wrapped in a `Tagged` instance, preserving the tag number
|
|
2179
|
+
* for the caller to inspect.
|
|
2180
|
+
*
|
|
2181
|
+
* @param {number} tag - The CBOR tag number this decoder will be registered for
|
|
2182
|
+
* @returns {TaggedTagDecoder}
|
|
2183
|
+
*
|
|
2184
|
+
* @example
|
|
2185
|
+
* import { decode, Tagged } from 'cborg'
|
|
2186
|
+
* const value = decode(bytes, { tags: { 16: Tagged.decoder(16) } })
|
|
2187
|
+
* // value instanceof Tagged; value.tag === 16
|
|
2188
|
+
*/
|
|
2189
|
+
static decoder(tag) {
|
|
2190
|
+
return (decode) => new Tagged(tag, decode());
|
|
2191
|
+
}
|
|
2192
|
+
/**
|
|
2193
|
+
* Build a `tags` option for `decode()` that wraps each listed tag number in
|
|
2194
|
+
* a `Tagged` instance, preserving those tags through decode without
|
|
2195
|
+
* registering a dedicated decoder per tag.
|
|
2196
|
+
*
|
|
2197
|
+
* @param {...number} tagNumbers - One or more CBOR tag numbers to preserve
|
|
2198
|
+
* @returns {{[tagNumber: number]: TaggedTagDecoder}}
|
|
2199
|
+
*
|
|
2200
|
+
* @example
|
|
2201
|
+
* import { decode, Tagged } from 'cborg'
|
|
2202
|
+
* const value = decode(bytes, { tags: Tagged.preserve(16, 96) })
|
|
2203
|
+
*/
|
|
2204
|
+
static preserve(...tagNumbers) {
|
|
2205
|
+
/** @type {{[tagNumber: number]: TaggedTagDecoder}} */
|
|
2206
|
+
const tags = {};
|
|
2207
|
+
for (const tag of tagNumbers) tags[tag] = Tagged.decoder(tag);
|
|
2208
|
+
return tags;
|
|
2209
|
+
}
|
|
2210
|
+
}.prototype, Symbol.toStringTag, { value: "Tagged" });
|
|
2211
|
+
//#endregion
|
|
2212
|
+
//#region node_modules/@ipld/dag-cbor/src/index.js
|
|
2213
|
+
const CID_CBOR_TAG = 42;
|
|
2214
|
+
/**
|
|
2215
|
+
* @template T
|
|
2216
|
+
* @typedef {import('multiformats/codecs/interface').ByteView<T>} ByteView
|
|
2217
|
+
*/
|
|
2218
|
+
/**
|
|
2219
|
+
* @template T
|
|
2220
|
+
* @typedef {import('multiformats/codecs/interface').ArrayBufferView<T>} ArrayBufferView
|
|
2221
|
+
*/
|
|
2222
|
+
/**
|
|
2223
|
+
* @template T
|
|
2224
|
+
* @param {ByteView<T> | ArrayBufferView<T>} buf
|
|
2225
|
+
* @returns {ByteView<T>}
|
|
2226
|
+
*/
|
|
2227
|
+
function toByteView(buf) {
|
|
2228
|
+
if (buf instanceof ArrayBuffer) return new Uint8Array(buf, 0, buf.byteLength);
|
|
2229
|
+
return buf;
|
|
2230
|
+
}
|
|
2231
|
+
/**
|
|
2232
|
+
* cidEncoder will receive all Objects during encode, it needs to filter out
|
|
2233
|
+
* anything that's not a CID and return `null` for that so it's encoded as
|
|
2234
|
+
* normal.
|
|
2235
|
+
*
|
|
2236
|
+
* @param {any} obj
|
|
2237
|
+
* @returns {cborg.Token[]|null}
|
|
2238
|
+
*/
|
|
2239
|
+
function cidEncoder(obj) {
|
|
2240
|
+
if (obj.asCID !== obj && obj["/"] !== obj.bytes) return null;
|
|
2241
|
+
const cid = CID.asCID(obj);
|
|
2242
|
+
/* c8 ignore next 4 */
|
|
2243
|
+
if (!cid) return null;
|
|
2244
|
+
const bytes = new Uint8Array(cid.bytes.byteLength + 1);
|
|
2245
|
+
bytes.set(cid.bytes, 1);
|
|
2246
|
+
return [new Token(Type.tag, CID_CBOR_TAG), new Token(Type.bytes, bytes)];
|
|
2247
|
+
}
|
|
2248
|
+
/**
|
|
2249
|
+
* Intercept all `undefined` values from an object walk and reject the entire
|
|
2250
|
+
* object if we find one.
|
|
2251
|
+
*
|
|
2252
|
+
* @returns {null}
|
|
2253
|
+
*/
|
|
2254
|
+
function undefinedEncoder() {
|
|
2255
|
+
throw new Error("`undefined` is not supported by the IPLD Data Model and cannot be encoded");
|
|
2256
|
+
}
|
|
2257
|
+
/**
|
|
2258
|
+
* Intercept all `number` values from an object walk and reject the entire
|
|
2259
|
+
* object if we find something that doesn't fit the IPLD data model (NaN &
|
|
2260
|
+
* Infinity).
|
|
2261
|
+
*
|
|
2262
|
+
* @param {number} num
|
|
2263
|
+
* @returns {null}
|
|
2264
|
+
*/
|
|
2265
|
+
function numberEncoder(num) {
|
|
2266
|
+
if (Number.isNaN(num)) throw new Error("`NaN` is not supported by the IPLD Data Model and cannot be encoded");
|
|
2267
|
+
if (num === Infinity || num === -Infinity) throw new Error("`Infinity` and `-Infinity` is not supported by the IPLD Data Model and cannot be encoded");
|
|
2268
|
+
return null;
|
|
2269
|
+
}
|
|
2270
|
+
/**
|
|
2271
|
+
* @param {Map<any, any>} map
|
|
2272
|
+
* @returns {null}
|
|
2273
|
+
*/
|
|
2274
|
+
function mapEncoder(map) {
|
|
2275
|
+
for (const key of map.keys()) if (typeof key !== "string" || key.length === 0) throw new Error("Non-string Map keys are not supported by the IPLD Data Model and cannot be encoded");
|
|
2276
|
+
return null;
|
|
2277
|
+
}
|
|
2278
|
+
const _encodeOptions = {
|
|
2279
|
+
float64: true,
|
|
2280
|
+
typeEncoders: {
|
|
2281
|
+
Map: mapEncoder,
|
|
2282
|
+
Object: cidEncoder,
|
|
2283
|
+
undefined: undefinedEncoder,
|
|
2284
|
+
number: numberEncoder
|
|
2285
|
+
}
|
|
2286
|
+
};
|
|
2287
|
+
({ ..._encodeOptions }), { ..._encodeOptions.typeEncoders };
|
|
2288
|
+
/**
|
|
2289
|
+
* @param {import('cborg').TagDecodeControl} decode
|
|
2290
|
+
* @returns {CID}
|
|
2291
|
+
*/
|
|
2292
|
+
function cidDecoder(decode) {
|
|
2293
|
+
const bytes = decode();
|
|
2294
|
+
if (bytes[0] !== 0) throw new Error("Invalid CID for CBOR tag 42; expected leading 0x00");
|
|
2295
|
+
return CID.decode(bytes.subarray(1));
|
|
2296
|
+
}
|
|
2297
|
+
const _decodeOptions = {
|
|
2298
|
+
allowIndefinite: false,
|
|
2299
|
+
coerceUndefinedToNull: true,
|
|
2300
|
+
allowNaN: false,
|
|
2301
|
+
allowInfinity: false,
|
|
2302
|
+
allowBigInt: true,
|
|
2303
|
+
strict: true,
|
|
2304
|
+
useMaps: false,
|
|
2305
|
+
rejectDuplicateMapKeys: true,
|
|
2306
|
+
/** @type {{ [tagNumber: number]: import('cborg').TagDecoder }} */
|
|
2307
|
+
tags: { [CID_CBOR_TAG]: cidDecoder }
|
|
2308
|
+
};
|
|
2309
|
+
({ ..._decodeOptions }), { ..._decodeOptions.tags };
|
|
2310
|
+
/**
|
|
2311
|
+
* @template T
|
|
2312
|
+
* @param {T} node
|
|
2313
|
+
* @returns {ByteView<T>}
|
|
2314
|
+
*/
|
|
2315
|
+
const encode = (node) => encode$1(node, _encodeOptions);
|
|
2316
|
+
/**
|
|
2317
|
+
* @template T
|
|
2318
|
+
* @param {ByteView<T> | ArrayBufferView<T>} data
|
|
2319
|
+
* @returns {T}
|
|
2320
|
+
*/
|
|
2321
|
+
const decode = (data) => decode$1(toByteView(data), _decodeOptions);
|
|
2322
|
+
//#endregion
|
|
2323
|
+
export { encode as n, toByteView as r, decode as t };
|