cborg 1.10.2 → 2.0.0-alpha.1

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.
Files changed (140) hide show
  1. package/.github/dependabot.yml +16 -0
  2. package/.github/workflows/test-and-release.yml +63 -0
  3. package/CHANGELOG.md +332 -0
  4. package/bench/bench.js +117 -0
  5. package/bench/json.js +124 -0
  6. package/bench/package.json +8 -0
  7. package/lib/bin.js +1 -1
  8. package/package.json +29 -47
  9. package/types/lib/4array.d.ts +1 -1
  10. package/types/lib/5map.d.ts +1 -1
  11. package/types/lib/6tag.d.ts +1 -1
  12. package/types/lib/7float.d.ts +1 -1
  13. package/types/lib/bin.d.ts +1 -0
  14. package/types/lib/token.d.ts +13 -13
  15. package/cjs/browser-test/appendix_a.js +0 -643
  16. package/cjs/browser-test/common.js +0 -24
  17. package/cjs/browser-test/node-test-bin.js +0 -348
  18. package/cjs/browser-test/noop-bin-test.js +0 -4
  19. package/cjs/browser-test/test-0uint.js +0 -158
  20. package/cjs/browser-test/test-1negint.js +0 -152
  21. package/cjs/browser-test/test-2bytes.js +0 -254
  22. package/cjs/browser-test/test-3string.js +0 -144
  23. package/cjs/browser-test/test-4array.js +0 -200
  24. package/cjs/browser-test/test-5map.js +0 -667
  25. package/cjs/browser-test/test-6tag.js +0 -75
  26. package/cjs/browser-test/test-7float.js +0 -253
  27. package/cjs/browser-test/test-bl.js +0 -91
  28. package/cjs/browser-test/test-cbor-vectors.js +0 -98
  29. package/cjs/browser-test/test-decode-errors.js +0 -77
  30. package/cjs/browser-test/test-fuzz.js +0 -56
  31. package/cjs/browser-test/test-json.js +0 -281
  32. package/cjs/browser-test/test-length.js +0 -63
  33. package/cjs/cborg.js +0 -14
  34. package/cjs/lib/0uint.js +0 -163
  35. package/cjs/lib/1negint.js +0 -63
  36. package/cjs/lib/2bytes.js +0 -62
  37. package/cjs/lib/3string.js +0 -46
  38. package/cjs/lib/4array.js +0 -51
  39. package/cjs/lib/5map.js +0 -51
  40. package/cjs/lib/6tag.js +0 -36
  41. package/cjs/lib/7float.js +0 -188
  42. package/cjs/lib/bin.js +0 -139
  43. package/cjs/lib/bl.js +0 -77
  44. package/cjs/lib/byte-utils.js +0 -245
  45. package/cjs/lib/common.js +0 -22
  46. package/cjs/lib/decode.js +0 -140
  47. package/cjs/lib/diagnostic.js +0 -124
  48. package/cjs/lib/encode.js +0 -248
  49. package/cjs/lib/is.js +0 -87
  50. package/cjs/lib/json/decode.js +0 -414
  51. package/cjs/lib/json/encode.js +0 -161
  52. package/cjs/lib/json/json.js +0 -12
  53. package/cjs/lib/jump.js +0 -174
  54. package/cjs/lib/length.js +0 -36
  55. package/cjs/lib/token.js +0 -46
  56. package/cjs/node-test/appendix_a.js +0 -643
  57. package/cjs/node-test/common.js +0 -24
  58. package/cjs/node-test/node-test-bin.js +0 -348
  59. package/cjs/node-test/noop-bin-test.js +0 -4
  60. package/cjs/node-test/test-0uint.js +0 -158
  61. package/cjs/node-test/test-1negint.js +0 -152
  62. package/cjs/node-test/test-2bytes.js +0 -254
  63. package/cjs/node-test/test-3string.js +0 -144
  64. package/cjs/node-test/test-4array.js +0 -200
  65. package/cjs/node-test/test-5map.js +0 -667
  66. package/cjs/node-test/test-6tag.js +0 -75
  67. package/cjs/node-test/test-7float.js +0 -253
  68. package/cjs/node-test/test-bl.js +0 -91
  69. package/cjs/node-test/test-cbor-vectors.js +0 -98
  70. package/cjs/node-test/test-decode-errors.js +0 -77
  71. package/cjs/node-test/test-fuzz.js +0 -56
  72. package/cjs/node-test/test-json.js +0 -281
  73. package/cjs/node-test/test-length.js +0 -63
  74. package/cjs/taglib.js +0 -44
  75. package/cli.js +0 -7
  76. package/esm/browser-test/appendix_a.js +0 -640
  77. package/esm/browser-test/common.js +0 -19
  78. package/esm/browser-test/node-test-bin.js +0 -339
  79. package/esm/browser-test/noop-bin-test.js +0 -1
  80. package/esm/browser-test/test-0uint.js +0 -155
  81. package/esm/browser-test/test-1negint.js +0 -149
  82. package/esm/browser-test/test-2bytes.js +0 -252
  83. package/esm/browser-test/test-3string.js +0 -141
  84. package/esm/browser-test/test-4array.js +0 -197
  85. package/esm/browser-test/test-5map.js +0 -664
  86. package/esm/browser-test/test-6tag.js +0 -78
  87. package/esm/browser-test/test-7float.js +0 -250
  88. package/esm/browser-test/test-bl.js +0 -84
  89. package/esm/browser-test/test-cbor-vectors.js +0 -95
  90. package/esm/browser-test/test-decode-errors.js +0 -69
  91. package/esm/browser-test/test-fuzz.js +0 -50
  92. package/esm/browser-test/test-json.js +0 -279
  93. package/esm/browser-test/test-length.js +0 -55
  94. package/esm/cborg.js +0 -12
  95. package/esm/lib/0uint.js +0 -152
  96. package/esm/lib/1negint.js +0 -55
  97. package/esm/lib/2bytes.js +0 -59
  98. package/esm/lib/3string.js +0 -43
  99. package/esm/lib/4array.js +0 -41
  100. package/esm/lib/5map.js +0 -41
  101. package/esm/lib/6tag.js +0 -27
  102. package/esm/lib/7float.js +0 -179
  103. package/esm/lib/bin.js +0 -137
  104. package/esm/lib/bl.js +0 -74
  105. package/esm/lib/byte-utils.js +0 -228
  106. package/esm/lib/common.js +0 -19
  107. package/esm/lib/decode.js +0 -139
  108. package/esm/lib/diagnostic.js +0 -123
  109. package/esm/lib/encode.js +0 -246
  110. package/esm/lib/is.js +0 -81
  111. package/esm/lib/json/decode.js +0 -413
  112. package/esm/lib/json/encode.js +0 -160
  113. package/esm/lib/json/json.js +0 -10
  114. package/esm/lib/jump.js +0 -168
  115. package/esm/lib/length.js +0 -31
  116. package/esm/lib/token.js +0 -43
  117. package/esm/node-test/appendix_a.js +0 -640
  118. package/esm/node-test/common.js +0 -19
  119. package/esm/node-test/node-test-bin.js +0 -339
  120. package/esm/node-test/noop-bin-test.js +0 -1
  121. package/esm/node-test/test-0uint.js +0 -155
  122. package/esm/node-test/test-1negint.js +0 -149
  123. package/esm/node-test/test-2bytes.js +0 -252
  124. package/esm/node-test/test-3string.js +0 -141
  125. package/esm/node-test/test-4array.js +0 -197
  126. package/esm/node-test/test-5map.js +0 -664
  127. package/esm/node-test/test-6tag.js +0 -78
  128. package/esm/node-test/test-7float.js +0 -250
  129. package/esm/node-test/test-bl.js +0 -84
  130. package/esm/node-test/test-cbor-vectors.js +0 -95
  131. package/esm/node-test/test-decode-errors.js +0 -69
  132. package/esm/node-test/test-fuzz.js +0 -50
  133. package/esm/node-test/test-json.js +0 -279
  134. package/esm/node-test/test-length.js +0 -55
  135. package/esm/package.json +0 -4
  136. package/esm/taglib.js +0 -37
  137. package/index.js +0 -1
  138. package/json +0 -1
  139. package/length +0 -1
  140. package/taglib +0 -1
package/cjs/lib/encode.js DELETED
@@ -1,248 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var is = require('./is.js');
6
- var token = require('./token.js');
7
- var bl = require('./bl.js');
8
- var common = require('./common.js');
9
- var jump = require('./jump.js');
10
- var byteUtils = require('./byte-utils.js');
11
- var _0uint = require('./0uint.js');
12
- var _1negint = require('./1negint.js');
13
- var _2bytes = require('./2bytes.js');
14
- var _3string = require('./3string.js');
15
- var _4array = require('./4array.js');
16
- var _5map = require('./5map.js');
17
- var _6tag = require('./6tag.js');
18
- var _7float = require('./7float.js');
19
-
20
- const defaultEncodeOptions = {
21
- float64: false,
22
- mapSorter,
23
- quickEncodeToken: jump.quickEncodeToken
24
- };
25
- function makeCborEncoders() {
26
- const encoders = [];
27
- encoders[token.Type.uint.major] = _0uint.encodeUint;
28
- encoders[token.Type.negint.major] = _1negint.encodeNegint;
29
- encoders[token.Type.bytes.major] = _2bytes.encodeBytes;
30
- encoders[token.Type.string.major] = _3string.encodeString;
31
- encoders[token.Type.array.major] = _4array.encodeArray;
32
- encoders[token.Type.map.major] = _5map.encodeMap;
33
- encoders[token.Type.tag.major] = _6tag.encodeTag;
34
- encoders[token.Type.float.major] = _7float.encodeFloat;
35
- return encoders;
36
- }
37
- const cborEncoders = makeCborEncoders();
38
- const buf = new bl.Bl();
39
- class Ref {
40
- constructor(obj, parent) {
41
- this.obj = obj;
42
- this.parent = parent;
43
- }
44
- includes(obj) {
45
- let p = this;
46
- do {
47
- if (p.obj === obj) {
48
- return true;
49
- }
50
- } while (p = p.parent);
51
- return false;
52
- }
53
- static createCheck(stack, obj) {
54
- if (stack && stack.includes(obj)) {
55
- throw new Error(`${ common.encodeErrPrefix } object contains circular references`);
56
- }
57
- return new Ref(obj, stack);
58
- }
59
- }
60
- const simpleTokens = {
61
- null: new token.Token(token.Type.null, null),
62
- undefined: new token.Token(token.Type.undefined, undefined),
63
- true: new token.Token(token.Type.true, true),
64
- false: new token.Token(token.Type.false, false),
65
- emptyArray: new token.Token(token.Type.array, 0),
66
- emptyMap: new token.Token(token.Type.map, 0)
67
- };
68
- const typeEncoders = {
69
- number(obj, _typ, _options, _refStack) {
70
- if (!Number.isInteger(obj) || !Number.isSafeInteger(obj)) {
71
- return new token.Token(token.Type.float, obj);
72
- } else if (obj >= 0) {
73
- return new token.Token(token.Type.uint, obj);
74
- } else {
75
- return new token.Token(token.Type.negint, obj);
76
- }
77
- },
78
- bigint(obj, _typ, _options, _refStack) {
79
- if (obj >= BigInt(0)) {
80
- return new token.Token(token.Type.uint, obj);
81
- } else {
82
- return new token.Token(token.Type.negint, obj);
83
- }
84
- },
85
- Uint8Array(obj, _typ, _options, _refStack) {
86
- return new token.Token(token.Type.bytes, obj);
87
- },
88
- string(obj, _typ, _options, _refStack) {
89
- return new token.Token(token.Type.string, obj);
90
- },
91
- boolean(obj, _typ, _options, _refStack) {
92
- return obj ? simpleTokens.true : simpleTokens.false;
93
- },
94
- null(_obj, _typ, _options, _refStack) {
95
- return simpleTokens.null;
96
- },
97
- undefined(_obj, _typ, _options, _refStack) {
98
- return simpleTokens.undefined;
99
- },
100
- ArrayBuffer(obj, _typ, _options, _refStack) {
101
- return new token.Token(token.Type.bytes, new Uint8Array(obj));
102
- },
103
- DataView(obj, _typ, _options, _refStack) {
104
- return new token.Token(token.Type.bytes, new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength));
105
- },
106
- Array(obj, _typ, options, refStack) {
107
- if (!obj.length) {
108
- if (options.addBreakTokens === true) {
109
- return [
110
- simpleTokens.emptyArray,
111
- new token.Token(token.Type.break)
112
- ];
113
- }
114
- return simpleTokens.emptyArray;
115
- }
116
- refStack = Ref.createCheck(refStack, obj);
117
- const entries = [];
118
- let i = 0;
119
- for (const e of obj) {
120
- entries[i++] = objectToTokens(e, options, refStack);
121
- }
122
- if (options.addBreakTokens) {
123
- return [
124
- new token.Token(token.Type.array, obj.length),
125
- entries,
126
- new token.Token(token.Type.break)
127
- ];
128
- }
129
- return [
130
- new token.Token(token.Type.array, obj.length),
131
- entries
132
- ];
133
- },
134
- Object(obj, typ, options, refStack) {
135
- const isMap = typ !== 'Object';
136
- const keys = isMap ? obj.keys() : Object.keys(obj);
137
- const length = isMap ? obj.size : keys.length;
138
- if (!length) {
139
- if (options.addBreakTokens === true) {
140
- return [
141
- simpleTokens.emptyMap,
142
- new token.Token(token.Type.break)
143
- ];
144
- }
145
- return simpleTokens.emptyMap;
146
- }
147
- refStack = Ref.createCheck(refStack, obj);
148
- const entries = [];
149
- let i = 0;
150
- for (const key of keys) {
151
- entries[i++] = [
152
- objectToTokens(key, options, refStack),
153
- objectToTokens(isMap ? obj.get(key) : obj[key], options, refStack)
154
- ];
155
- }
156
- sortMapEntries(entries, options);
157
- if (options.addBreakTokens) {
158
- return [
159
- new token.Token(token.Type.map, length),
160
- entries,
161
- new token.Token(token.Type.break)
162
- ];
163
- }
164
- return [
165
- new token.Token(token.Type.map, length),
166
- entries
167
- ];
168
- }
169
- };
170
- typeEncoders.Map = typeEncoders.Object;
171
- typeEncoders.Buffer = typeEncoders.Uint8Array;
172
- for (const typ of 'Uint8Clamped Uint16 Uint32 Int8 Int16 Int32 BigUint64 BigInt64 Float32 Float64'.split(' ')) {
173
- typeEncoders[`${ typ }Array`] = typeEncoders.DataView;
174
- }
175
- function objectToTokens(obj, options = {}, refStack) {
176
- const typ = is.is(obj);
177
- const customTypeEncoder = options && options.typeEncoders && options.typeEncoders[typ] || typeEncoders[typ];
178
- if (typeof customTypeEncoder === 'function') {
179
- const tokens = customTypeEncoder(obj, typ, options, refStack);
180
- if (tokens != null) {
181
- return tokens;
182
- }
183
- }
184
- const typeEncoder = typeEncoders[typ];
185
- if (!typeEncoder) {
186
- throw new Error(`${ common.encodeErrPrefix } unsupported type: ${ typ }`);
187
- }
188
- return typeEncoder(obj, typ, options, refStack);
189
- }
190
- function sortMapEntries(entries, options) {
191
- if (options.mapSorter) {
192
- entries.sort(options.mapSorter);
193
- }
194
- }
195
- function mapSorter(e1, e2) {
196
- const keyToken1 = Array.isArray(e1[0]) ? e1[0][0] : e1[0];
197
- const keyToken2 = Array.isArray(e2[0]) ? e2[0][0] : e2[0];
198
- if (keyToken1.type !== keyToken2.type) {
199
- return keyToken1.type.compare(keyToken2.type);
200
- }
201
- const major = keyToken1.type.major;
202
- const tcmp = cborEncoders[major].compareTokens(keyToken1, keyToken2);
203
- if (tcmp === 0) {
204
- console.warn('WARNING: complex key types used, CBOR key sorting guarantees are gone');
205
- }
206
- return tcmp;
207
- }
208
- function tokensToEncoded(buf, tokens, encoders, options) {
209
- if (Array.isArray(tokens)) {
210
- for (const token of tokens) {
211
- tokensToEncoded(buf, token, encoders, options);
212
- }
213
- } else {
214
- encoders[tokens.type.major](buf, tokens, options);
215
- }
216
- }
217
- function encodeCustom(data, encoders, options) {
218
- const tokens = objectToTokens(data, options);
219
- if (!Array.isArray(tokens) && options.quickEncodeToken) {
220
- const quickBytes = options.quickEncodeToken(tokens);
221
- if (quickBytes) {
222
- return quickBytes;
223
- }
224
- const encoder = encoders[tokens.type.major];
225
- if (encoder.encodedSize) {
226
- const size = encoder.encodedSize(tokens, options);
227
- const buf = new bl.Bl(size);
228
- encoder(buf, tokens, options);
229
- if (buf.chunks.length !== 1) {
230
- throw new Error(`Unexpected error: pre-calculated length for ${ tokens } was wrong`);
231
- }
232
- return byteUtils.asU8A(buf.chunks[0]);
233
- }
234
- }
235
- buf.reset();
236
- tokensToEncoded(buf, tokens, encoders, options);
237
- return buf.toBytes(true);
238
- }
239
- function encode(data, options) {
240
- options = Object.assign({}, defaultEncodeOptions, options);
241
- return encodeCustom(data, cborEncoders, options);
242
- }
243
-
244
- exports.Ref = Ref;
245
- exports.encode = encode;
246
- exports.encodeCustom = encodeCustom;
247
- exports.makeCborEncoders = makeCborEncoders;
248
- exports.objectToTokens = objectToTokens;
package/cjs/lib/is.js DELETED
@@ -1,87 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const typeofs = [
6
- 'string',
7
- 'number',
8
- 'bigint',
9
- 'symbol'
10
- ];
11
- const objectTypeNames = [
12
- 'Function',
13
- 'Generator',
14
- 'AsyncGenerator',
15
- 'GeneratorFunction',
16
- 'AsyncGeneratorFunction',
17
- 'AsyncFunction',
18
- 'Observable',
19
- 'Array',
20
- 'Buffer',
21
- 'Object',
22
- 'RegExp',
23
- 'Date',
24
- 'Error',
25
- 'Map',
26
- 'Set',
27
- 'WeakMap',
28
- 'WeakSet',
29
- 'ArrayBuffer',
30
- 'SharedArrayBuffer',
31
- 'DataView',
32
- 'Promise',
33
- 'URL',
34
- 'HTMLElement',
35
- 'Int8Array',
36
- 'Uint8Array',
37
- 'Uint8ClampedArray',
38
- 'Int16Array',
39
- 'Uint16Array',
40
- 'Int32Array',
41
- 'Uint32Array',
42
- 'Float32Array',
43
- 'Float64Array',
44
- 'BigInt64Array',
45
- 'BigUint64Array'
46
- ];
47
- function is(value) {
48
- if (value === null) {
49
- return 'null';
50
- }
51
- if (value === undefined) {
52
- return 'undefined';
53
- }
54
- if (value === true || value === false) {
55
- return 'boolean';
56
- }
57
- const typeOf = typeof value;
58
- if (typeofs.includes(typeOf)) {
59
- return typeOf;
60
- }
61
- if (typeOf === 'function') {
62
- return 'Function';
63
- }
64
- if (Array.isArray(value)) {
65
- return 'Array';
66
- }
67
- if (isBuffer(value)) {
68
- return 'Buffer';
69
- }
70
- const objectType = getObjectType(value);
71
- if (objectType) {
72
- return objectType;
73
- }
74
- return 'Object';
75
- }
76
- function isBuffer(value) {
77
- return value && value.constructor && value.constructor.isBuffer && value.constructor.isBuffer.call(null, value);
78
- }
79
- function getObjectType(value) {
80
- const objectTypeName = Object.prototype.toString.call(value).slice(8, -1);
81
- if (objectTypeNames.includes(objectTypeName)) {
82
- return objectTypeName;
83
- }
84
- return undefined;
85
- }
86
-
87
- exports.is = is;