jaelis-node 1.9.0 → 2.0.0

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 (173) hide show
  1. package/README.md +147 -433
  2. package/bin/jaelis-node.js +79 -504
  3. package/lib/index.js +31 -2740
  4. package/lib/node.js +271 -0
  5. package/lib/rpc.js +315 -0
  6. package/lib/storage.js +198 -0
  7. package/lib/sync.js +366 -0
  8. package/package.json +19 -53
  9. package/config/default.json +0 -74
  10. package/config/mainnet.json +0 -30
  11. package/config/testnet.json +0 -26
  12. package/lib/JAELIS-VM/lib/adapters/evm-adapter.js +0 -454
  13. package/lib/JAELIS-VM/lib/adapters/index.js +0 -411
  14. package/lib/JAELIS-VM/lib/adapters/svm-adapter.js +0 -457
  15. package/lib/JAELIS-VM/lib/compiler/jir-compiler.js +0 -1097
  16. package/lib/JAELIS-VM/lib/execution/engine.js +0 -1183
  17. package/lib/JAELIS-VM/lib/index.js +0 -440
  18. package/lib/JAELIS-VM/lib/integration/jaelis-integration.js +0 -543
  19. package/lib/JAELIS-VM/lib/serialization/serializer.js +0 -819
  20. package/lib/JAELIS-VM/lib/state/state-manager.js +0 -1116
  21. package/lib/JAELIS-VM/lib/translator/bytecode-translator.js +0 -1222
  22. package/lib/JAELIS-VM/lib/unified/cross-chain-deploy.js +0 -1678
  23. package/lib/JAELIS-VM/lib/unified/cross-chain-state.js +0 -836
  24. package/lib/JAELIS-VM/lib/unified/dynamic-contracts.js +0 -1127
  25. package/lib/JAELIS-VM/lib/unified/index.js +0 -456
  26. package/lib/JAELIS-VM/lib/unified/jaelis-abi.js +0 -1150
  27. package/lib/JAELIS-VM/lib/unified/unified-compiler.js +0 -1350
  28. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds +0 -12
  29. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.cmd +0 -17
  30. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.ps1 +0 -28
  31. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds +0 -12
  32. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.cmd +0 -17
  33. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.ps1 +0 -28
  34. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages +0 -12
  35. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
  36. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
  37. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
  38. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
  39. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
  40. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
  41. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
  42. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
  43. package/lib/JAELIS-VM/node_modules/.package-lock.json +0 -127
  44. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/README.md +0 -1
  45. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/index.js +0 -0
  46. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.abi115.node +0 -0
  47. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.napi.node +0 -0
  48. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/package.json +0 -17
  49. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/README.md +0 -1
  50. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/index.js +0 -0
  51. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.abi115.node +0 -0
  52. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.napi.node +0 -0
  53. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/package.json +0 -17
  54. package/lib/JAELIS-VM/node_modules/cbor-extract/LICENSE +0 -21
  55. package/lib/JAELIS-VM/node_modules/cbor-extract/README.md +0 -5
  56. package/lib/JAELIS-VM/node_modules/cbor-extract/bin/download-prebuilds.js +0 -11
  57. package/lib/JAELIS-VM/node_modules/cbor-extract/binding.gyp +0 -60
  58. package/lib/JAELIS-VM/node_modules/cbor-extract/index.js +0 -1
  59. package/lib/JAELIS-VM/node_modules/cbor-extract/package.json +0 -50
  60. package/lib/JAELIS-VM/node_modules/cbor-extract/src/extract.cpp +0 -198
  61. package/lib/JAELIS-VM/node_modules/cbor-x/LICENSE +0 -21
  62. package/lib/JAELIS-VM/node_modules/cbor-x/README.md +0 -380
  63. package/lib/JAELIS-VM/node_modules/cbor-x/SECURITY.md +0 -11
  64. package/lib/JAELIS-VM/node_modules/cbor-x/benchmark.md +0 -73
  65. package/lib/JAELIS-VM/node_modules/cbor-x/browser.js +0 -11
  66. package/lib/JAELIS-VM/node_modules/cbor-x/decode.d.ts +0 -2
  67. package/lib/JAELIS-VM/node_modules/cbor-x/decode.js +0 -1300
  68. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs +0 -1244
  69. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs.map +0 -1
  70. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs +0 -2509
  71. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs.map +0 -1
  72. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js +0 -2
  73. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js.map +0 -1
  74. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js +0 -2508
  75. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js.map +0 -1
  76. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js +0 -2
  77. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js.map +0 -1
  78. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs +0 -2629
  79. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs.map +0 -1
  80. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js +0 -3343
  81. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js.map +0 -1
  82. package/lib/JAELIS-VM/node_modules/cbor-x/encode.d.ts +0 -1
  83. package/lib/JAELIS-VM/node_modules/cbor-x/encode.js +0 -1231
  84. package/lib/JAELIS-VM/node_modules/cbor-x/index.d.ts +0 -79
  85. package/lib/JAELIS-VM/node_modules/cbor-x/index.js +0 -3
  86. package/lib/JAELIS-VM/node_modules/cbor-x/iterators.js +0 -85
  87. package/lib/JAELIS-VM/node_modules/cbor-x/node-index.js +0 -24
  88. package/lib/JAELIS-VM/node_modules/cbor-x/package.json +0 -94
  89. package/lib/JAELIS-VM/node_modules/cbor-x/rollup.config.js +0 -88
  90. package/lib/JAELIS-VM/node_modules/cbor-x/stream.js +0 -61
  91. package/lib/JAELIS-VM/node_modules/cbor-x/webpack.config.js +0 -19
  92. package/lib/JAELIS-VM/node_modules/detect-libc/LICENSE +0 -201
  93. package/lib/JAELIS-VM/node_modules/detect-libc/README.md +0 -163
  94. package/lib/JAELIS-VM/node_modules/detect-libc/index.d.ts +0 -14
  95. package/lib/JAELIS-VM/node_modules/detect-libc/lib/detect-libc.js +0 -313
  96. package/lib/JAELIS-VM/node_modules/detect-libc/lib/elf.js +0 -39
  97. package/lib/JAELIS-VM/node_modules/detect-libc/lib/filesystem.js +0 -51
  98. package/lib/JAELIS-VM/node_modules/detect-libc/lib/process.js +0 -24
  99. package/lib/JAELIS-VM/node_modules/detect-libc/package.json +0 -44
  100. package/lib/JAELIS-VM/node_modules/msgpackr/LICENSE +0 -21
  101. package/lib/JAELIS-VM/node_modules/msgpackr/README.md +0 -372
  102. package/lib/JAELIS-VM/node_modules/msgpackr/SECURITY.md +0 -11
  103. package/lib/JAELIS-VM/node_modules/msgpackr/benchmark.md +0 -67
  104. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs +0 -2407
  105. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs.map +0 -1
  106. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js +0 -2
  107. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js.map +0 -1
  108. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js +0 -2406
  109. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js.map +0 -1
  110. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js +0 -2
  111. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js.map +0 -1
  112. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs +0 -3320
  113. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs.map +0 -1
  114. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js +0 -4540
  115. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js.map +0 -1
  116. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs +0 -1250
  117. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs.map +0 -1
  118. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.cts +0 -91
  119. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.ts +0 -91
  120. package/lib/JAELIS-VM/node_modules/msgpackr/index.js +0 -5
  121. package/lib/JAELIS-VM/node_modules/msgpackr/iterators.js +0 -87
  122. package/lib/JAELIS-VM/node_modules/msgpackr/node-index.js +0 -25
  123. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.cts +0 -1
  124. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.ts +0 -1
  125. package/lib/JAELIS-VM/node_modules/msgpackr/pack.js +0 -1141
  126. package/lib/JAELIS-VM/node_modules/msgpackr/package.json +0 -104
  127. package/lib/JAELIS-VM/node_modules/msgpackr/rollup.config.js +0 -88
  128. package/lib/JAELIS-VM/node_modules/msgpackr/stream.js +0 -57
  129. package/lib/JAELIS-VM/node_modules/msgpackr/struct.js +0 -815
  130. package/lib/JAELIS-VM/node_modules/msgpackr/test-worker.js +0 -3
  131. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.cts +0 -2
  132. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.ts +0 -2
  133. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.js +0 -1221
  134. package/lib/JAELIS-VM/node_modules/msgpackr-extract/LICENSE +0 -21
  135. package/lib/JAELIS-VM/node_modules/msgpackr-extract/README.md +0 -5
  136. package/lib/JAELIS-VM/node_modules/msgpackr-extract/bin/download-prebuilds.js +0 -13
  137. package/lib/JAELIS-VM/node_modules/msgpackr-extract/binding.gyp +0 -63
  138. package/lib/JAELIS-VM/node_modules/msgpackr-extract/index.js +0 -1
  139. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages +0 -12
  140. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
  141. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
  142. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
  143. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
  144. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
  145. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
  146. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
  147. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
  148. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
  149. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/README.md +0 -58
  150. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/bin.js +0 -82
  151. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
  152. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/index.js +0 -6
  153. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/node-gyp-build.js +0 -236
  154. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
  155. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/package.json +0 -32
  156. package/lib/JAELIS-VM/node_modules/msgpackr-extract/package.json +0 -50
  157. package/lib/JAELIS-VM/node_modules/msgpackr-extract/src/extract.cpp +0 -274
  158. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
  159. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/README.md +0 -58
  160. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/bin.js +0 -77
  161. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
  162. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/index.js +0 -224
  163. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
  164. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/package.json +0 -32
  165. package/lib/JAELIS-VM/package-lock.json +0 -284
  166. package/lib/JAELIS-VM/package.json +0 -38
  167. package/lib/JAELIS-VM/test/comprehensive.test.js +0 -267
  168. package/lib/JAELIS-VM/test/cross-chain-test.js +0 -470
  169. package/lib/JAELIS-VM/test/unified-vm-test.js +0 -459
  170. package/lib/JAELIS-VM/test/unified.test.js +0 -166
  171. package/lib/JAELIS-VM/test/vm.test.js +0 -599
  172. package/lib/settlement-server.js +0 -999
  173. package/lib/vm/index.js +0 -397
@@ -1,198 +0,0 @@
1
- /*
2
- This is responsible for extracting the strings, in bulk, from a CBOR buffer. Creating strings from buffers can
3
- be one of the biggest performance bottlenecks of parsing, but creating an array of extracting strings all at once
4
- provides much better performance. This will parse and produce up to 256 strings at once .The JS parser can call this multiple
5
- times as necessary to get more strings. This must be partially capable of parsing CBOR so it can know where to
6
- find the string tokens and determine their position and length. All strings are decoded as UTF-8.
7
- */
8
- #include <node_api.h>
9
- #if ENABLE_V8_API
10
- #include <v8.h>
11
- #endif
12
-
13
- #ifndef thread_local
14
- #ifdef __GNUC__
15
- # define thread_local __thread
16
- #elif __STDC_VERSION__ >= 201112L
17
- # define thread_local _Thread_local
18
- #elif defined(_MSC_VER)
19
- # define thread_local __declspec(thread)
20
- #else
21
- # define thread_local
22
- #endif
23
- #endif
24
-
25
- const int MAX_TARGET_SIZE = 255;
26
- napi_value unexpectedEnd(napi_env env) {
27
- napi_value returnValue;
28
- napi_get_undefined(env, &returnValue);
29
- napi_throw_type_error(env, NULL, "Unexpected end of buffer reading string");
30
- return returnValue;
31
- }
32
- class Extractor {
33
- public:
34
- napi_value target[MAX_TARGET_SIZE + 1]; // leave one for the queued string
35
-
36
- uint8_t* source;
37
- uint32_t position = 0;
38
- uint32_t writePosition = 0;
39
- uint32_t stringStart = 0;
40
- uint32_t lastStringEnd = 0;
41
-
42
- void readString(napi_env env, uint32_t length, bool allowStringBlocks) {
43
- uint32_t start = position;
44
- uint32_t end = position + length;
45
- if (allowStringBlocks) { // for larger strings, we don't bother to check every character for being latin, and just go right to creating a new string
46
- while(position < end) {
47
- if (source[position] < 0x80) // ensure we character is latin and can be decoded as one byte
48
- position++;
49
- else {
50
- break;
51
- }
52
- }
53
- }
54
- if (position < end) {
55
- // non-latin character
56
- if (lastStringEnd) {
57
- napi_value value;
58
- napi_create_string_latin1(env, (const char*) source + stringStart, lastStringEnd - stringStart, &value);
59
- target[writePosition++] = value;
60
- lastStringEnd = 0;
61
- }
62
- // use standard utf-8 conversion
63
- napi_value value;
64
- napi_create_string_utf8(env, (const char*) source + start, (int) length, &value);
65
- target[writePosition++] = value;
66
- position = end;
67
- return;
68
- }
69
-
70
- if (lastStringEnd) {
71
- if (start - lastStringEnd > 40 || end - stringStart > 6000) {
72
- napi_value value;
73
- napi_create_string_latin1(env, (const char*) source + stringStart, lastStringEnd - stringStart, &value);
74
- target[writePosition++] = value;
75
- stringStart = start;
76
- }
77
- } else {
78
- stringStart = start;
79
- }
80
- lastStringEnd = end;
81
- }
82
- napi_value extractStrings(napi_env env, uint32_t startingPosition, uint32_t size, uint32_t firstStringSize, uint8_t* inputSource) {
83
- writePosition = 0;
84
- lastStringEnd = 0;
85
- position = startingPosition;
86
- source = inputSource;
87
- readString(env, firstStringSize, firstStringSize < 0x100);
88
- while (position < size) {
89
- uint8_t token = source[position++];
90
- uint8_t majorType = token >> 5;
91
- token = token & 0x1f;
92
- if (majorType == 2 || majorType == 3) {
93
- uint32_t length;
94
- switch (token) {
95
- case 0x18:
96
- if (position + 1 > size) {
97
- return unexpectedEnd(env);
98
- }
99
- length = source[position++];
100
- break;
101
- case 0x19:
102
- if (position + 2 > size) {
103
- return unexpectedEnd(env);
104
- }
105
- length = source[position++] << 8;
106
- length += source[position++];
107
- break;
108
- case 0x1a:
109
- if (position + 4 > size) {
110
- return unexpectedEnd(env);
111
- }
112
- length = source[position++] << 24;
113
- length += source[position++] << 16;
114
- length += source[position++] << 8;
115
- length += source[position++];
116
- break;
117
- case 0x1b:
118
- return unexpectedEnd(env);
119
- default:
120
- length = token;
121
- }
122
- if (majorType == 3) {
123
- // string
124
- if (length + position > size) {
125
- return unexpectedEnd(env);
126
- }
127
- readString(env, length, length < 0x100);
128
- if (writePosition >= MAX_TARGET_SIZE)
129
- break;
130
- } else { // binary data
131
- position += length;
132
- }
133
-
134
- } else { // all other tokens
135
- switch (token) {
136
- case 0x18:
137
- position++;
138
- break;
139
- case 0x19:
140
- position += 2;
141
- break;
142
- case 0x1a:
143
- position += 4;
144
- break;
145
- case 0x1b:
146
- position += 8;
147
- break;
148
- }
149
- }
150
- }
151
- if (lastStringEnd) {
152
- napi_value value;
153
- napi_create_string_latin1(env, (const char*) source + stringStart, lastStringEnd - stringStart, &value);
154
- if (writePosition == 0) {
155
- return value;
156
- }
157
- target[writePosition++] = value;
158
- } else if (writePosition == 1) {
159
- return target[0];
160
- }
161
- napi_value array;
162
- #if ENABLE_V8_API
163
- v8::Local<v8::Array> v8Array = v8::Array::New(v8::Isolate::GetCurrent(), (v8::Local<v8::Value>*) target, writePosition);
164
- memcpy(&array, &v8Array, sizeof(array));
165
- #else
166
- napi_create_array_with_length(env, writePosition, &array);
167
- for (int i = 0; i < writePosition; i++) {
168
- napi_set_element(env, array, i, target[i]);
169
- }
170
- #endif
171
- return array;
172
- }
173
- };
174
-
175
- static thread_local Extractor* extractor;
176
-
177
- napi_value extractStrings(napi_env env, napi_callback_info info) {
178
- size_t argc = 4;
179
- napi_value args[4];
180
- napi_get_cb_info(env, info, &argc, args, NULL, NULL);
181
- uint32_t position;
182
- uint32_t size;
183
- uint32_t firstStringSize;
184
- napi_get_value_uint32(env, args[0], &position);
185
- napi_get_value_uint32(env, args[1], &size);
186
- napi_get_value_uint32(env, args[2], &firstStringSize);
187
- uint8_t* source;
188
- size_t buffer_size;
189
- napi_get_buffer_info(env, args[3], (void**) &source, &buffer_size);
190
- return extractor->extractStrings(env, position, size, firstStringSize, source);
191
- }
192
- #define EXPORT_NAPI_FUNCTION(name, func) { napi_property_descriptor desc = { name, 0, func, 0, 0, 0, (napi_property_attributes) (napi_writable | napi_configurable), 0 }; napi_define_properties(env, exports, 1, &desc); }
193
-
194
- NAPI_MODULE_INIT() {
195
- extractor = new Extractor(); // create our thread-local extractor
196
- EXPORT_NAPI_FUNCTION("extractStrings", extractStrings);
197
- return exports;
198
- }
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2020 Kris Zyp
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,380 +0,0 @@
1
- # cbor-x
2
- [![license](https://img.shields.io/badge/license-MIT-brightgreen)](LICENSE)
3
- [![npm version](https://img.shields.io/npm/v/cbor-x.svg?style=flat-square)](https://www.npmjs.org/package/cbor-x)
4
- [![encode](https://img.shields.io/badge/encode-1.5GB%2Fs-yellow)](benchmark.md)
5
- [![decode](https://img.shields.io/badge/decode-2GB%2Fs-yellow)](benchmark.md)
6
- [![types](https://img.shields.io/npm/types/cbor-x)](README.md)
7
- [![module](https://img.shields.io/badge/module-ESM%2FCJS-blue)](README.md)
8
- [![license](https://img.shields.io/badge/license-MIT-brightgreen)](LICENSE)
9
-
10
- <img align="right" src="./assets/performance.png" width="380"/>
11
-
12
- The cbor-x package is an extremely fast and conformant CBOR NodeJS/JavaScript implementation. Currently, it is over 3-10x faster than any other CBOR JS implementation (including cbor-js and cborg) and faster than most MessagePack encoders, Avro, and generally faster than native V8 JSON.stringify/parse, on NodeJS. It implements the CBOR format as specificed in [RFC-8949](https://www.rfc-editor.org/rfc/rfc8949.html), [RFC-8746](https://tools.ietf.org/html/rfc8746), [RFC-8742](https://datatracker.ietf.org/doc/html/rfc8742), [Packed CBOR](https://datatracker.ietf.org/doc/html/draft-ietf-cbor-packed), numerous [registered IANA tag extensions](https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml) (the `x` in cbor-x), and proposed optional [record extension](https://github.com/kriszyp/cbor-records), for defining record structures that makes CBOR even faster and more compact, often over twice as fast as even native JSON functions, and 15-50% more compact. See the performance section for more details. Structured cloning (with support for cyclical references) is supported through these tag extensions.
13
-
14
- ## Basic Usage
15
- Install on NodeJS with:
16
-
17
- ```
18
- npm i cbor-x
19
- ```
20
- And `import` or `require` it for basic standard serialization/encoding (`encode`) and deserialization/decoding (`decode`) functions:
21
- ```JavaScript
22
- import { decode, encode } from 'cbor-x';
23
- let serializedAsBuffer = encode(value);
24
- let data = decode(serializedAsBuffer);
25
- ```
26
- This `encode` function will generate standard CBOR without any extensions that should be compatible with any standard CBOR parser/decoder. It will serialize JavaScript objects as CBOR `map`s by default. The `decode` function will deserialize CBOR `map`s as an `Object` with the properties from the map. The cbor-x package runs on any modern JS platform, but does have additional optimizations for NodeJS usage (and will use a node addon for performance boost as an optional dependency).
27
-
28
- ## Deno Usage
29
- Cbor-x modules are standard ESM modules and can be loaded directly from the [deno.land registry for cbor](https://deno.land/x/cbor) for use in Deno. The standard encode and decode functionality is available on Deno, like other platforms.
30
-
31
- ### Streams
32
- We can use the including streaming functionality (which further improves performance). The `EncoderStream` is a NodeJS transform stream that can be used to serialize objects to a binary stream (writing to network/socket, IPC, etc.), and the `DecoderStream` can be used to deserialize objects from a binary sream (reading from network/socket, etc.):
33
-
34
- ```JavaScript
35
- import { EncoderStream } from 'cbor-x';
36
- let stream = new EncoderStream();
37
- stream.write(myData);
38
-
39
- ```
40
- Or for a full example of sending and receiving data on a stream:
41
- ```JavaScript
42
- import { EncoderStream } from 'cbor-x';
43
- let sendingStream = new EncoderStream();
44
- let receivingStream = new DecoderStream();
45
- // we are just piping to our own stream, but normally you would send and
46
- // receive over some type of inter-process or network connection.
47
- sendingStream.pipe(receivingStream);
48
- sendingStream.write(myData);
49
- receivingStream.on('data', (data) => {
50
- // received data
51
- });
52
- ```
53
- The `EncoderStream` and `DecoderStream` instances will have also the record structure extension enabled by default (see below).
54
-
55
- ### Iterables
56
- In addition to using CBOR with streams, CBOR can also encode to an iterable that can be iterated as a sequence of binary chunks with `encodeAsIterable`, which facilitates progressive encoding:
57
- ```JavaScript
58
- import { encodeAsIterable } from 'cbor-x';
59
-
60
- for (let binaryChunk of encodeAsIterable(data)){
61
- // progressively get binary chunks as data is encoded
62
- }
63
- ```
64
- And `encodeAsAsyncIterable` is also available, which returns an async iterable, and can be used to encode data from async iterables as well as Blob data.
65
-
66
- ```JavaScript
67
- import { encodeAsAsyncIterable } from 'cbor-x';
68
-
69
- let data = { blob: new Blob(...) };
70
- for await (let binaryChunk of encodeAsAsyncIterable(data)){
71
- // progressively get binary chunks as asynchronous data source is encoded
72
- }
73
- ```
74
-
75
- ## Deno Usage
76
- Cbor-x modules are standard ESM modules and can be loaded directly from the [deno.land registry for cbor](https://deno.land/x/cbor) for use in Deno. The standard pack/encode and unpack/decode functionality is available on Deno, like other platforms.
77
-
78
- ## Browser Usage
79
- Cbor-x works as standalone JavaScript as well, and runs on modern browsers. It includes a bundled script, at `dist/index.js` for ease of direct loading:
80
- ```HTML
81
- <script src="node_modules/cbor-x/dist/index.js"></script>
82
- ```
83
-
84
- This is UMD based, and will register as a module if possible, or create a `CBOR` global with all the exported functions.
85
-
86
- For module-based development, it is recommended that you directly import the module of interest, to minimize dependencies that get pulled into your application:
87
- ```JavaScript
88
- import { decode } from 'cbor-x/decode' // if you only need to decode
89
- ```
90
-
91
- ## Structured Cloning
92
- You can also use cbor-x for [structured cloning](https://html.spec.whatwg.org/multipage/structured-data.html). By enabling the `structuredClone` option, you can include references to other objects or cyclic references, and object identity will be preserved. Structured cloning also enables preserving certain typed objects like `Error`, `Set`, `RegExp` and TypedArray instances, using [registered CBOR tag extensions](https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml). For example:
93
- ```JavaScript
94
- let obj = {
95
- set: new Set(['a', 'b']),
96
- regular: /a\spattern/
97
- };
98
- obj.self = obj;
99
- let encoder = new Encoder({ structuredClone: true });
100
- let serialized = encoder.encode(obj);
101
- let copy = encoder.decode(serialized);
102
- copy.self === copy // true
103
- copy.set.has('a') // true
104
-
105
- ```
106
-
107
- This option is disabled by default because reference checking degrades performance (by about 25-30%). (Note this implementation doesn't serialize every class/type specified in the HTML specification since not all of them make sense for storing across platforms.)
108
-
109
- ## Record / Object Structures
110
- There is a critical difference between maps (or dictionaries) that hold an arbitrary set of keys and values (JavaScript `Map` is designed for these), and records or object structures that have a well-defined set of fields. Typical JS objects/records may have many instances re(use) the same structure. By using the record extension, this distinction is preserved in CBOR and the encoding can reuse structures and not only provides better type preservation, but yield much more compact encodings and increase decoding performance by 2-3x. Cbor-x automatically generates record definitions that are reused and referenced by objects with the same structure. Records use CBOR's tags to align well CBOR's tag/extension mechanism. There are a number of ways to use this to our advantage. For large object structures with repeating nested objects with similar structures, simply serializing with the record extension can yield significant benefits. To use the record structures extension, we create a new `Encoder` instance. By default a new `Encoder` instance will have the record extension enabled:
111
- ```JavaScript
112
- import { Encoder } from 'cbor-x';
113
- let encoder = new Encoder();
114
- encoder.encode(myBigData);
115
- ```
116
-
117
- Another way to further leverage the benefits of the cbor-x record structures is to use streams that naturally allow for data to reuse based on previous record structures. The stream classes have the record structure extension enabled by default and provide excellent out-of-the-box performance.
118
-
119
- When creating a new `Encoder`, `EncoderStream`, or `DecoderStream` instance, we can enable or disable the record structure extension with the `objectsAsMaps` property. When this is `true`, the record structure extension will be disabled, and all objects will revert to being serialized using MessageMap `map`s, and all `map`s will be deserialized to JS `Object`s as properties (like the standalone `encode` and `decode` functions).
120
-
121
- Streaming with record structures works by encoding a structure the first time it is seen in a stream and referencing the structure in later messages that are sent across that stream. When an encoder can expect a decoder to understand previous structure references, this can be configured using the `sequential: true` flag, which is auto-enabled by streams, but can also be used with Packr instances.
122
-
123
- ### Shared Record Structures
124
- Another useful way of using cbor-x, and the record extension, is for storing data in a databases, files, or other storage systems. If a number of objects with common data structures are being stored, a shared structure can be used to greatly improve data storage and deserialization efficiency. In the simplest form, provide a `structures` array, which is updated if any new object structure is encountered:
125
-
126
- ```JavaScript
127
- import { Encoder } from 'cbor-x';
128
- let encoder = new Encoder({
129
- structures: [... structures that were last generated ...]
130
- });
131
- ```
132
- If you are working with persisted data, you will need to persist the `structures` data when it is updated. Cbor-x provides an API for loading and saving the `structures` on demand (which is robust and can be used in multiple-process situations where other processes may be updating this same `structures` array), we just need to provide a way to store the generated shared structure so it is available to deserialize stored data in the future:
133
- ```JavaScript
134
- import { Encoder } from 'cbor-x';
135
- let encoder = new Encoder({
136
- getStructures() {
137
- // storing our data in file (but we could also store in a db or key-value store)
138
- return decode(readFileSync('my-shared-structures.cbor')) || [];
139
- },
140
- saveStructures(structures) {
141
- writeFileSync('my-shared-structures.cbor', encode(structures))
142
- },
143
- structures: []
144
- });
145
- ```
146
- Cbor-x will automatically add and saves structures as it encounters any new object structures (up to a limit of 64). It will always add structures in incremental/compatible way: Any object encoded with an earlier structure can be decoded with a later version (as long as it is persisted).
147
-
148
- ### Reading Multiple Values
149
- If you have a buffer with multiple values sequentially encoded, you can choose to parse and read multiple values. This can be done using the `decodeMultiple` function/method, which can return an array of all the values it can sequentially parse within the provided buffer. For example:
150
- ```js
151
- let data = new Uint8Array([1, 2, 3]) // encodings of values 1, 2, and 3
152
- let values = decodeMultiple(data) // [1, 2, 3]
153
- ```
154
- Alternately, you can provide a callback function that is called as the parsing occurs with each value, and can optionally terminate the parsing by returning `false`:
155
- ```js
156
- let data = new Uint8Array([1, 2, 3]) // encodings of values 1, 2, and 3
157
- decodeMultiple(data, (value) => {
158
- // called for each value
159
- // return false if you wish to end the parsing
160
- })
161
- ```
162
- ### KeyMaps for Senml
163
- KeyMaps can be used to remap properties of source Objects and Maps to numerical equivalents for more efficient encoding.
164
- The principle driver for this feature is to support `application/senml+cbor`content-encoding as defined in https://datatracker.ietf.org/doc/html/rfc8428#section-6 for use in LWM2M application (see http://www.openmobilealliance.org/release/LightweightM2M/V1_2-20201110-A/HTML-Version/OMA-TS-LightweightM2M_Core-V1_2-20201110-A.html#7-4-7-0-747-SenML-CBOR)
165
-
166
- Records are also supported in conjunction with keyMaps, but these are disabled by default when keyMaps are specified as use of the two features does not introduce any additional compression efficiency unless that the data arrays are quite large (> 10 items).
167
-
168
- ```JavaScript
169
- import { Encoder } from 'cbor-x'
170
- const data = [
171
- { bn: '/3303/0/5700', bt: 1278887, v: 35.5 },
172
- { t: 10, v: 34 },
173
- { t: 20, v: 33 },
174
- { t: 30, v: 32 },
175
- { t: 40, v: 31 },
176
- { t: 50, v: 30 }
177
- ]
178
-
179
- let senmlKeys = { bs: -6, bv: -5, bu: -4, bt: -3, bn: -2, bver: -1, n: 0, u: 1, v: 2, vs: 3, vb: 4, s: 5, t: 6, ut: 7, vd: 8}}
180
- let senmlCbor = new Encoder({ keyMap: senmlKeys })
181
- let basicCbor = new Encoder()
182
- let senmlBuff = senmlCbor.encode(data)
183
- let basicBuff = basicCbor.encode(data)
184
- console.log('Senml CBOR size:', senmlBuff.length) // 77
185
- console.log('Basic CBOR size:', basicBuff.length) // 90
186
- assert.deepEqual(senmlEncoder.decode(senmlBuff), data)
187
-
188
- ```
189
-
190
- ### CBOR Packing
191
- [Packed CBOR](https://datatracker.ietf.org/doc/html/draft-ietf-cbor-packed) is additional specification for CBOR which allows for compact encoding of data that has repeated values. Cbor-x supports decoding packed CBOR, no flags or options needed. Cbor-x can also optionally generate packed CBOR (with the `pack` option), which will cause the encoder to look for repeated strings in a data structure that is being encoded, and store the strings in a packed table that can be referenced, to reduce encoding size. This involves extra overhead and reduces encoding performance, and generally does not yield as much compaction as standard compression tools. However, this is can be much faster than encoding plus compression, while still providing some level of reduction in encoding size. In addition to size reduction, packed CBOR is also usually faster to decode (assuming that some repetitive values could be found/packed).
192
-
193
- Cbor-x also has in-progress effort to support shared packed tables.
194
-
195
-
196
- ## Options
197
- The following options properties can be provided to the Encoder or Decoder constructor:
198
-
199
- * `keyMap` - This can be set to an object which will be used to map keys in the source Object or Map to other keys including integers. This allows for more efficient encoding, and enables support for numeric cbar tag encodings such as used by `application/senml+cbor` (https://datatracker.ietf.org/doc/html/rfc8428#section-6)
200
- * `useRecords` - Setting this to `false` disables the record extension and stores JavaScript objects as CBOR maps (with tag 259), and decodes maps as JavaScript `Object`s, which ensures compatibilty with other decoders.
201
- * `structures` - Provides the array of structures that is to be used for record extension, if you want the structures saved and used again. This array will be modified in place with new record structures that are serialized (if less than 64 structures are in the array).
202
- * `structuredClone` - This enables the structured cloning extensions that will encode object/cyclic references and additional built-in types/classes.
203
- * `mapsAsObjects` - If `true`, this will decode CBOR maps and JS `Object`s with the map entries decoded to object properties. If `false`, maps are decoded as JavaScript `Map`s. This is disabled by default if `useRecords` is enabled (`Map`s are preserved since they are distinct from records), and is enabled by default if `useRecords` is disabled.
204
- * `useFloat32` - This will enable cbor-x to encode non-integer numbers as 32-bit (4 byte) floating point numbers. See next section for possible values.
205
- * `alwaysUseFloat` - This will force cbor-x to encode any number, including integers, as floating-point numbers.
206
- * `pack` - This will enable [CBOR packing](https://datatracker.ietf.org/doc/html/draft-ietf-cbor-packed) for encoding, as described above.
207
- * `variableMapSize` - This will use varying map size definition (from single-byte to full 32-bit representation) based on the number of keys when encoding objects, which yields slightly more compact encodings (for small objects), but is typically 5-10% slower during encoding. This is only relevant when record extension is disabled.
208
- * `copyBuffers` - When decoding a CBOR message with binary data (Buffers are encoded as binary data), copy the buffer rather than providing a slice/view of the buffer. If you want your input data to be collected or modified while the decoded embedded buffer continues to live on, you can use this option (there is extra overhead to copying).
209
- * `bundleStrings` - If `true` this uses a custom extension that bundles strings together, so that they can be decoded more quickly on browsers and Deno that do not have access to the NodeJS addon. This a custom extension, so both encoder and decoder need to support this. This can yield significant decoding performance increases on browsers (30%-50%).
210
- * `useTimestamp32` - Encode JS `Date`s in 32-bit format when possible by dropping the milliseconds. This is a more efficient encoding of dates. You can also cause dates to use 32-bit format by manually setting the milliseconds to zero (`date.setMilliseconds(0)`).
211
- * `sequential` - Encode structures in serialized data, and reference previously encoded structures with expectation that decoder will read the encoded structures in the same order as encoded, with `unpackMultiple`.
212
- * `largeBigIntToFloat` - If a bigint needs to be encoded that is larger than will fit in 64-bit integers, it will be encoded as a float-64 (otherwise will throw a RangeError).
213
- * `useTag259ForMaps` - This flag indicates if [tag 259 (explicit maps)](https://github.com/shanewholloway/js-cbor-codec/blob/master/docs/CBOR-259-spec--explicit-maps.md) should be used to encode JS `Map`s. When using records is enabled, this is disabled by default, since plain objects are encoded with record structures and unambigiously differentiated from `Map`s, which are encoded as CBOR maps. Without using records, this enabled by default and is necessary to distinguish plain objects from `Map`s (but can be disabled by setting this to `false`).
214
- * `tagUint8Array` - Indicates if tag 64 should be used for `Uint8Array`s.
215
- * `int64AsNumber` - This will decode uint64 and int64 numbers as standard JS numbers rather than as bigint numbers.
216
-
217
- ### 32-bit Float Options
218
- By default all non-integer numbers are serialized as 64-bit float (double). This is fast, and ensures maximum precision. However, often real-world data doesn't not need 64-bits of precision, and using 32-bit encoding can be much more space efficient. There are several options that provide more efficient encodings. Using the decimal rounding options for encoding and decoding provides lossless storage of common decimal representations like 7.99, in more efficient 32-bit format (rather than 64-bit). The `useFloat32` property has several possible options, available from the module as constants:
219
- ```JavaScript
220
- import { ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } from 'cbor-x'
221
- ```
222
-
223
- * `ALWAYS` (1) - Always will encode non-integers (absolute less than 2147483648) as 32-bit float.
224
- * `DECIMAL_ROUND` (3) - Always will encode non-integers as 32-bit float, and when decoding 32-bit float, round to the significant decimal digits (usually 7, but 6 or 8 digits for some ranges).
225
- * `DECIMAL_FIT` (4) - Only encode non-integers as 32-bit float if all significant digits (usually up to 7) can be unamiguously encoded as a 32-bit float, and decode with decimal rounding (same as above). This will ensure round-trip encoding/decoding without loss in precision and uses 32-bit when possible.
226
-
227
- Note, that the performance is decreased with decimal rounding by about 20-25%, although if only 5% of your values are floating point, that will only have about a 1% impact overall.
228
-
229
- In addition, msgpackr exports a `roundFloat32(number)` function that can be used to round floating point numbers to the maximum significant decimal digits that can be stored in 32-bit float, just as DECIMAL_ROUND does when decoding. This can be useful for determining how a number will be decoded prior to encoding it.
230
-
231
- ### Setting Size Limits
232
- You can set size limits on objects, arrays, and maps to prevent resource exhaustion when decoding. This can be done by calling the setMaxLimits export. Each of the properties are optional (only provide
233
- the properties you want to change), for example (with the defaults):
234
- ```JavaScript
235
- import { setMaxLimits } from 'cbor-x';
236
- setMaxLimits({
237
- maxArraySize: 112810000,
238
- maxMapSize: 16810000,
239
- maxObjectSize : 16710000
240
- });
241
- ```
242
-
243
- ## Performance
244
- Cbor-x is fast. Really fast. Here is comparison with the next fastest JS projects using the benchmark tool from `msgpack-lite` (and the sample data is from some clinical research data we use that has a good mix of different value types and structures). It also includes comparison to V8 native JSON functionality, and JavaScript Avro (`avsc`, a very optimized Avro implementation):
245
-
246
- ### Native Acceleration
247
- Cbor-x employs an optional native node-addon to accelerate the parsing of strings. This should be automatically installed and utilized on NodeJS. However, you can verify this by checking the `isNativeAccelerationEnabled` property that is exported from cbor-x. If this is `false`, the `cbor-extract` package may not have been properly installed, and you may want to verify that it is installed correctly:
248
- ```js
249
- import { isNativeAccelerationEnabled } from 'cbor-x'
250
- if (!isNativeAccelerationEnabled)
251
- console.warn('Native acceleration not enabled, verify that install finished properly')
252
- ```
253
-
254
-
255
- operation | op | ms | op/s
256
- ---------------------------------------------------------- | ------: | ----: | -----:
257
- buf = Buffer(JSON.stringify(obj)); | 78200 | 5004 | 15627
258
- obj = JSON.parse(buf); | 89600 | 5003 | 17909
259
- require("cbor-x").encode(obj); | 163100 | 5001 | 32613
260
- require("cbor-x").decode(buf); | 100200 | 5004 | 20023
261
- cbor-x w/ shared structures: packr.encode(obj); | 178300 | 5002 | 35645
262
- cbor-x w/ shared structures: packr.decode(buf); | 414000 | 5000 | 82800
263
- buf = require("cbor").encode(obj); | 7800 | 5016 | 1555
264
- obj = require("cbor").decode(buf); | 3200 | 5087 | 629
265
- buf = require("cbor-sync").encode(obj); | 18600 | 5012 | 3711
266
- obj = require("cbor-sync").decode(buf); | 20000 | 5020 | 3984
267
- buf = require("msgpack-lite").encode(obj); | 30900 | 5013 | 6163
268
- obj = require("msgpack-lite").decode(buf); | 15800 | 5012 | 3152
269
- buf = require("notepack").encode(obj); | 62600 | 5006 | 12504
270
- obj = require("notepack").decode(buf); | 33700 | 5007 | 6730
271
- require("avsc")...make schema/type...type.toBuffer(obj); | 86900 | 5002 | 17373
272
- require("avsc")...make schema/type...type.fromBuffer(obj); | 106100 | 5000 | 21220
273
-
274
- All benchmarks were performed on Node 14.8.0 (Windows i7-4770 3.4Ghz).
275
- (`avsc` is schema-based and more comparable in style to cbor-x with shared structures).
276
-
277
- Here is a benchmark of streaming data (again borrowed from `msgpack-lite`'s benchmarking), where cbor-x is able to take advantage of the structured record extension and really demonstrate its performance capabilities:
278
-
279
- operation (1000000 x 2) | op | ms | op/s
280
- ------------------------------------------------ | ------: | ----: | -----:
281
- new EncoderStream().write(obj); | 1000000 | 372 | 2688172
282
- new DecoderStream().write(buf); | 1000000 | 247 | 4048582
283
- stream.write(msgpack.encode(obj)); | 1000000 | 2898 | 345065
284
- stream.write(msgpack.decode(buf)); | 1000000 | 1969 | 507872
285
- stream.write(notepack.encode(obj)); | 1000000 | 901 | 1109877
286
- stream.write(notepack.decode(buf)); | 1000000 | 1012 | 988142
287
- msgpack.Encoder().on("data",ondata).encode(obj); | 1000000 | 1763 | 567214
288
- msgpack.createDecodeStream().write(buf); | 1000000 | 2222 | 450045
289
- msgpack.createEncodeStream().write(obj); | 1000000 | 1577 | 634115
290
- msgpack.Decoder().on("data",ondata).decode(buf); | 1000000 | 2246 | 445235
291
-
292
- See the [benchmark.md](benchmark.md) for more benchmarks and information about benchmarking.
293
-
294
- ## Custom Extensions
295
- You can add your own custom extensions, which can be used to encode specific types/classes in certain ways. This is done by using the `addExtension` function, and specifying the class, extension type code (custom extensions should be a number greater than 40500, all others are reserved for CBOR or cbor-x), and your encode and decode functions (or just the one you need). You can use cbor-x encoding and decoding within your extensions:
296
- ```JavaScript
297
- import { addExtension, Encoder } from 'cbor-x';
298
-
299
- class MyCustomClass {...}
300
-
301
- let extEncoder = new Encoder();
302
- addExtension({
303
- Class: MyCustomClass,
304
- tag: 43311, // register our own extension code (a tag code)
305
- encode(instance, encode) {
306
- // define how your custom class should be encoded
307
- encode(instance.myData); // return a buffer
308
- }
309
- decode(data) {
310
- // define how your custom class should be decoded
311
- let instance = new MyCustomClass();
312
- instance.myData = data
313
- return instance; // decoded value from buffer
314
- }
315
- });
316
- ```
317
-
318
- ## Unknown Tags
319
- If no extension is registered for a tag, the decoder will return an instance of the `Tag` class, where the value provided for the tag will be available in the `value` property of the `Tag` instance. The `Tag` class is an export of the package and decode module.
320
-
321
- ### CBOR Compliance
322
- The cbor-x package is designed to encode and decode to the CBOR extended generic data model, implementing extensions to support the extended model, and will generally attempt to use preferred serializations where feasible. When duplicate keys are encountered in maps, previous entries will be lost, and the final entry is preserved.
323
-
324
- ### Additional Performance Optimizations
325
- Cbor-x is already fast, but here are some tips for making it faster.
326
-
327
- #### Arena Allocation (`useBuffer()`)
328
- During the serialization process, data is written to buffers. Again, allocating new buffers is a relatively expensive process, and the `useBuffer` method can help allow reuse of buffers that will further improve performance. With `useBuffer` method, you can provide a buffer, serialize data into it, and when it is known that you are done using that buffer, you can call `useBuffer` again to reuse it. The use of `useBuffer` is never required, buffers will still be handled and cleaned up through GC if not used, it just provides a small performance boost.
329
-
330
- ## Extensions
331
- Cbor-x currently uses tag id 105 and 26880-27135 for its [proposed extension for records](https://github.com/kriszyp/cbor-records).
332
-
333
- ### Dates
334
- Cbor-x saves all JavaScript `Date`s using the standard CBOR date extension (tag 1).
335
-
336
- ### Structured Cloning
337
- With structured cloning enabled, cbor-x will also use tags/extensions to store Set, Map, Error, RegExp, ArrayBufferView objects and preserve their types.
338
-
339
- ### List of supported tags for decoding
340
- Here is a list of CBOR tags that are supported for decoding:
341
-
342
- * 0 - String date
343
- * 1 - Numeric Date
344
- * 2 - BigInt
345
- * 3 - Negative BigInt
346
- * 6 - Packed string reference
347
- * 27 - Generic named objects (used for Error, RegExp)
348
- * 28, 29 - Value sharing/object referencing
349
- * 51 - Packed table
350
- * 64 - Uint8Array
351
- * 68 - Uint8ClampedArray
352
- * 69 - Uint16Array
353
- * 70 - Uint32Array
354
- * 71 - BigUint64Array
355
- * 72 - Int8Array
356
- * 77 - Int16Array
357
- * 78 - Int32Array
358
- * 79 - BigInt64Array
359
- * 81 - Float32Array
360
- * 82 - Float64Array
361
- * 105 - Records
362
- * 258 - Set
363
- * 259 - Map
364
- * 57344 - 57599 - Records
365
-
366
- ## Alternate Encoding/Package
367
- The high-performance serialization and deserialization algorithms in this package are also available in the [msgpackr](https://github.com/kriszyp/msgpackr) for the MessagePack format, with the same API and design. A quick summary of the pros and cons of using MessagePack vs CBOR are:
368
- * MessagePack has wider adoption and msgpackr has broader usage.
369
- * CBOR has an [official IETF standardization track](https://www.rfc-editor.org/rfc/rfc8949.html), and the record extensions is conceptually/philosophically a better fit for CBOR tags.
370
-
371
- ## License
372
-
373
- MIT
374
-
375
- ### Browser Consideration
376
- CBOR can be a great choice for high-performance data delivery to browsers, as reasonable data size is possible without compression. And CBOR works very well in modern browsers. However, it is worth noting that if you want highly compact data, brotli or gzip are most effective in compressing, and CBOR's character frequency tends to defeat Huffman encoding used by these standard compression algorithms, often resulting in less compact data than compressed JSON.
377
-
378
- ### Credits
379
-
380
- Various projects have been inspirations for this, and code has been borrowed from https://github.com/msgpack/msgpack-javascript and https://github.com/mtth/avsc.
@@ -1,11 +0,0 @@
1
- # Security Policy
2
-
3
- ## Supported Versions
4
-
5
- | Version | Supported |
6
- | ------- | ------------------ |
7
- | 0.9.x | :white_check_mark: |
8
-
9
- ## Reporting a Vulnerability
10
-
11
- Please report security vulnerabilities to kriszyp@gmail.com.