libshv-js 3.16.1 → 3.20.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.
- package/dist/access.d.ts +69 -0
- package/dist/access.js +98 -0
- package/dist/chainpack.d.ts +39 -0
- package/{src/chainpack.ts → dist/chainpack.js} +120 -218
- package/dist/cpcontext.d.ts +18 -0
- package/{src/cpcontext.ts → dist/cpcontext.js} +18 -27
- package/dist/cpon.d.ts +55 -0
- package/{src/cpon.ts → dist/cpon.js} +192 -292
- package/dist/index.d.ts +10 -0
- package/dist/index.js +26 -0
- package/dist/json.d.ts +3 -0
- package/{src/json.ts → dist/json.js} +31 -49
- package/dist/rpcmessage.d.ts +285 -0
- package/dist/rpcmessage.js +111 -0
- package/dist/rpcvalue.d.ts +56 -0
- package/dist/rpcvalue.js +100 -0
- package/dist/vue-shv.d.ts +91 -0
- package/{src/vue-shv.ts → dist/vue-shv.js} +104 -164
- package/dist/ws-client.d.ts +232 -0
- package/dist/ws-client.js +586 -0
- package/dist/zod.d.ts +30 -0
- package/dist/zod.js +112 -0
- package/package.json +21 -13
- package/.github/workflows/check-version-bump.yml +0 -22
- package/.github/workflows/lint.yml +0 -33
- package/.github/workflows/publish.yml +0 -24
- package/.github/workflows/test.yml +0 -31
- package/eslint.config.js +0 -103
- package/examples/websocketosaurus.html +0 -44
- package/examples/websocketosaurus.js +0 -94
- package/src/access.ts +0 -103
- package/src/rpcmessage.ts +0 -91
- package/src/rpcvalue.ts +0 -147
- package/src/ws-client.ts +0 -708
- package/src/zod.ts +0 -95
- package/test.ts +0 -110
- package/test_json.ts +0 -44
- package/test_zod.ts +0 -24
- package/tsconfig.json +0 -108
|
@@ -1,64 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CHAINPACK_PROTOCOL_TYPE = exports.ChainPackWriter = exports.ChainPackReader = exports.fromChainPack = exports.toChainPack = void 0;
|
|
1
4
|
/* eslint-disable no-bitwise */
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
IntThreshold = 64
|
|
8
|
-
Null = 128
|
|
9
|
-
UInt = 129
|
|
10
|
-
Int = 130
|
|
11
|
-
Double = 131
|
|
12
|
-
Bool = 132
|
|
13
|
-
Blob = 133
|
|
14
|
-
String = 134
|
|
15
|
-
List = 136
|
|
16
|
-
Map = 137
|
|
17
|
-
IMap = 138
|
|
18
|
-
MetaMap = 139
|
|
19
|
-
Decimal = 140
|
|
20
|
-
DateTime = 141
|
|
21
|
-
CString = 142
|
|
22
|
-
False = 253
|
|
23
|
-
True = 254
|
|
24
|
-
Term = 255
|
|
25
|
-
}
|
|
26
|
-
|
|
5
|
+
const cpon_1 = require("./cpon");
|
|
6
|
+
const rpcvalue_1 = require("./rpcvalue");
|
|
7
|
+
const cpcontext_1 = require("./cpcontext");
|
|
8
|
+
var PackingSchema;
|
|
9
|
+
(function (PackingSchema) {
|
|
10
|
+
PackingSchema[PackingSchema["IntThreshold"] = 64] = "IntThreshold";
|
|
11
|
+
PackingSchema[PackingSchema["Null"] = 128] = "Null";
|
|
12
|
+
PackingSchema[PackingSchema["UInt"] = 129] = "UInt";
|
|
13
|
+
PackingSchema[PackingSchema["Int"] = 130] = "Int";
|
|
14
|
+
PackingSchema[PackingSchema["Double"] = 131] = "Double";
|
|
15
|
+
PackingSchema[PackingSchema["Bool"] = 132] = "Bool";
|
|
16
|
+
PackingSchema[PackingSchema["Blob"] = 133] = "Blob";
|
|
17
|
+
PackingSchema[PackingSchema["String"] = 134] = "String";
|
|
18
|
+
PackingSchema[PackingSchema["List"] = 136] = "List";
|
|
19
|
+
PackingSchema[PackingSchema["Map"] = 137] = "Map";
|
|
20
|
+
PackingSchema[PackingSchema["IMap"] = 138] = "IMap";
|
|
21
|
+
PackingSchema[PackingSchema["MetaMap"] = 139] = "MetaMap";
|
|
22
|
+
PackingSchema[PackingSchema["Decimal"] = 140] = "Decimal";
|
|
23
|
+
PackingSchema[PackingSchema["DateTime"] = 141] = "DateTime";
|
|
24
|
+
PackingSchema[PackingSchema["CString"] = 142] = "CString";
|
|
25
|
+
PackingSchema[PackingSchema["False"] = 253] = "False";
|
|
26
|
+
PackingSchema[PackingSchema["True"] = 254] = "True";
|
|
27
|
+
PackingSchema[PackingSchema["Term"] = 255] = "Term";
|
|
28
|
+
})(PackingSchema || (PackingSchema = {}));
|
|
27
29
|
const SHV_EPOCH_MSEC = 1_517_529_600_000;
|
|
28
30
|
const INVALID_MIN_OFFSET_FROM_UTC = (-64 * 15);
|
|
29
|
-
|
|
30
|
-
const divInt = (n: bigint, d: bigint) => {
|
|
31
|
+
const divInt = (n, d) => {
|
|
31
32
|
const r = n % d;
|
|
32
|
-
return [(n - r) / d, r]
|
|
33
|
+
return [(n - r) / d, r];
|
|
33
34
|
};
|
|
34
|
-
|
|
35
|
-
const uint8ArrayBoBigint = (bytes: Uint8Array, type: 'Int' | 'UInt') => {
|
|
35
|
+
const uint8ArrayBoBigint = (bytes, type) => {
|
|
36
36
|
let isNeg = false;
|
|
37
|
-
|
|
38
37
|
if (type === 'Int') {
|
|
39
38
|
if (bytes.length < 5) {
|
|
40
39
|
const signMask = 0x80 >> bytes.length;
|
|
41
40
|
isNeg = Boolean(bytes[0] & signMask);
|
|
42
41
|
bytes[0] &= ~signMask;
|
|
43
|
-
}
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
44
|
isNeg = Boolean(bytes[1] & 128);
|
|
45
45
|
bytes[1] &= ~128;
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
|
|
49
48
|
let ret = 0n;
|
|
50
49
|
for (const byte of bytes) {
|
|
51
50
|
ret = (ret * BigInt(256)) + BigInt(byte);
|
|
52
51
|
}
|
|
53
|
-
|
|
54
52
|
if (isNeg) {
|
|
55
53
|
ret = -ret;
|
|
56
54
|
}
|
|
57
|
-
|
|
58
55
|
return ret;
|
|
59
56
|
};
|
|
60
|
-
|
|
61
|
-
const numberToUint8Array = (num: bigint) => {
|
|
57
|
+
const numberToUint8Array = (num) => {
|
|
62
58
|
let bytes = new Uint8Array(8);
|
|
63
59
|
let len = 0;
|
|
64
60
|
do {
|
|
@@ -66,163 +62,130 @@ const numberToUint8Array = (num: bigint) => {
|
|
|
66
62
|
num = res[0];
|
|
67
63
|
bytes[len++] = Number(res[1]);
|
|
68
64
|
} while (num !== 0n);
|
|
69
|
-
|
|
70
65
|
bytes = bytes.subarray(0, len);
|
|
71
66
|
bytes.reverse();
|
|
72
67
|
return bytes;
|
|
73
68
|
};
|
|
74
|
-
|
|
75
|
-
const significantBitsCount = (num: bigint) => {
|
|
69
|
+
const significantBitsCount = (num) => {
|
|
76
70
|
let ret = 0;
|
|
77
71
|
while (num > 0) {
|
|
78
72
|
ret++;
|
|
79
73
|
num >>= 1n;
|
|
80
74
|
}
|
|
81
|
-
|
|
82
75
|
return ret;
|
|
83
76
|
};
|
|
84
|
-
|
|
85
77
|
class ChainPackReader {
|
|
86
|
-
ctx
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
this.ctx = data instanceof UnpackContext ? data : new UnpackContext(data);
|
|
78
|
+
ctx;
|
|
79
|
+
constructor(data) {
|
|
80
|
+
this.ctx = data instanceof cpcontext_1.UnpackContext ? data : new cpcontext_1.UnpackContext(data);
|
|
90
81
|
}
|
|
91
|
-
|
|
92
82
|
/* eslint-disable complexity */
|
|
93
|
-
read()
|
|
94
|
-
let meta
|
|
95
|
-
let codePointAt = this.ctx.getByte()
|
|
96
|
-
|
|
83
|
+
read() {
|
|
84
|
+
let meta;
|
|
85
|
+
let codePointAt = this.ctx.getByte();
|
|
97
86
|
if (codePointAt === PackingSchema.MetaMap) {
|
|
98
87
|
meta = this.readMetaMap();
|
|
99
88
|
codePointAt = this.ctx.getByte();
|
|
100
89
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const ret = meta !== undefined ? new RpcValueWithMetaData(meta, x) : x;
|
|
90
|
+
const implReturn = (x) => {
|
|
91
|
+
const ret = meta !== undefined ? new rpcvalue_1.RpcValueWithMetaData(meta, x) : x;
|
|
104
92
|
return ret;
|
|
105
93
|
};
|
|
106
|
-
|
|
107
94
|
if (codePointAt < PackingSchema.Null) {
|
|
108
95
|
if (codePointAt >= PackingSchema.IntThreshold) {
|
|
109
96
|
return implReturn(codePointAt - 64);
|
|
110
97
|
}
|
|
111
|
-
|
|
112
|
-
return implReturn(new UInt(codePointAt));
|
|
98
|
+
return implReturn(new rpcvalue_1.UInt(codePointAt));
|
|
113
99
|
}
|
|
114
|
-
|
|
115
100
|
switch (codePointAt) {
|
|
116
101
|
case PackingSchema.Null: {
|
|
117
102
|
return implReturn(undefined);
|
|
118
103
|
}
|
|
119
|
-
|
|
120
104
|
case PackingSchema.True: {
|
|
121
105
|
return implReturn(true);
|
|
122
106
|
}
|
|
123
|
-
|
|
124
107
|
case PackingSchema.False: {
|
|
125
108
|
return implReturn(false);
|
|
126
109
|
}
|
|
127
|
-
|
|
128
110
|
case PackingSchema.Int: {
|
|
129
111
|
return implReturn(this.readIntData());
|
|
130
112
|
}
|
|
131
|
-
|
|
132
113
|
case PackingSchema.UInt: {
|
|
133
|
-
return implReturn(new UInt(this.readUIntData()));
|
|
114
|
+
return implReturn(new rpcvalue_1.UInt(this.readUIntData()));
|
|
134
115
|
}
|
|
135
|
-
|
|
136
116
|
case PackingSchema.Double: {
|
|
137
117
|
const data = new Uint8Array(8);
|
|
138
118
|
for (let i = 0; i < 8; i++) {
|
|
139
119
|
data[i] = this.ctx.getByte();
|
|
140
120
|
}
|
|
141
|
-
|
|
142
|
-
return implReturn(new Double(new DataView(data.buffer).getFloat64(0, true))); // little endian
|
|
121
|
+
return implReturn(new rpcvalue_1.Double(new DataView(data.buffer).getFloat64(0, true))); // little endian
|
|
143
122
|
}
|
|
144
|
-
|
|
145
123
|
case PackingSchema.Decimal: {
|
|
146
124
|
const mant = this.readIntData();
|
|
147
125
|
const exp = this.readIntData();
|
|
148
|
-
return implReturn(new Decimal(mant, exp));
|
|
126
|
+
return implReturn(new rpcvalue_1.Decimal(mant, exp));
|
|
149
127
|
}
|
|
150
|
-
|
|
151
128
|
case PackingSchema.DateTime: {
|
|
152
129
|
let bi = this.readUIntDataHelper('UInt');
|
|
153
130
|
const hasTzOffset = bi & 1n;
|
|
154
131
|
const hasNotMsec = bi & 2n;
|
|
155
132
|
bi >>= 2n;
|
|
156
|
-
|
|
157
133
|
let offset = 0;
|
|
158
134
|
if (hasTzOffset !== 0n) {
|
|
159
|
-
offset = Number(bi &
|
|
135
|
+
offset = Number(bi & 0x7fn);
|
|
160
136
|
if ((offset & 0x40) !== 0) {
|
|
161
137
|
// sign extension
|
|
162
138
|
offset -= 128;
|
|
163
139
|
}
|
|
164
|
-
|
|
165
140
|
bi >>= 7n;
|
|
166
141
|
}
|
|
167
|
-
|
|
168
142
|
offset *= 15;
|
|
169
|
-
|
|
170
143
|
if (offset === INVALID_MIN_OFFSET_FROM_UTC) {
|
|
171
144
|
return implReturn(undefined);
|
|
172
145
|
}
|
|
173
|
-
|
|
174
146
|
let msec = Number(bi);
|
|
175
147
|
if (hasNotMsec !== 0n) {
|
|
176
148
|
msec *= 1000;
|
|
177
149
|
}
|
|
178
|
-
|
|
179
150
|
msec += SHV_EPOCH_MSEC;
|
|
180
151
|
msec -= offset * 60_000;
|
|
181
|
-
return withOffset(new Date(msec), offset ?? undefined);
|
|
152
|
+
return (0, rpcvalue_1.withOffset)(new Date(msec), offset ?? undefined);
|
|
182
153
|
}
|
|
183
|
-
|
|
184
154
|
case PackingSchema.Map: {
|
|
185
155
|
return implReturn(this.readMap());
|
|
186
156
|
}
|
|
187
|
-
|
|
188
157
|
case PackingSchema.IMap: {
|
|
189
158
|
return implReturn(this.readIMap());
|
|
190
159
|
}
|
|
191
|
-
|
|
192
160
|
case PackingSchema.List: {
|
|
193
161
|
return implReturn(this.readList());
|
|
194
162
|
}
|
|
195
|
-
|
|
196
163
|
case PackingSchema.Blob: {
|
|
197
164
|
const strLen = this.readUIntData();
|
|
198
165
|
const arr = new Uint8Array(strLen);
|
|
199
166
|
for (let i = 0; i < strLen; i++) {
|
|
200
167
|
arr[i] = this.ctx.getByte();
|
|
201
168
|
}
|
|
202
|
-
|
|
203
169
|
return implReturn(arr.buffer);
|
|
204
170
|
}
|
|
205
|
-
|
|
206
171
|
case PackingSchema.String: {
|
|
207
172
|
const strLen = this.readUIntData();
|
|
208
173
|
const arr = new Uint8Array(strLen);
|
|
209
174
|
for (let i = 0; i < strLen; i++) {
|
|
210
175
|
arr[i] = this.ctx.getByte();
|
|
211
176
|
}
|
|
212
|
-
|
|
213
|
-
return implReturn(utf8ToString(arr.buffer));
|
|
177
|
+
return implReturn((0, cpon_1.utf8ToString)(arr.buffer));
|
|
214
178
|
}
|
|
215
|
-
|
|
216
179
|
case PackingSchema.CString: {
|
|
217
180
|
// variation of CponReader.readCString()
|
|
218
|
-
const pctx = new PackContext();
|
|
181
|
+
const pctx = new cpcontext_1.PackContext();
|
|
219
182
|
while (true) {
|
|
220
183
|
let b = this.ctx.getByte();
|
|
221
184
|
if (b === '\\'.codePointAt(0)) {
|
|
222
185
|
b = this.ctx.getByte();
|
|
223
186
|
switch (b) {
|
|
224
187
|
case '\\'.codePointAt(0):
|
|
225
|
-
pctx.putByte('\\'.codePointAt(0)
|
|
188
|
+
pctx.putByte('\\'.codePointAt(0));
|
|
226
189
|
break;
|
|
227
190
|
case '0'.codePointAt(0):
|
|
228
191
|
pctx.putByte(0);
|
|
@@ -231,24 +194,23 @@ class ChainPackReader {
|
|
|
231
194
|
pctx.putByte(b);
|
|
232
195
|
break;
|
|
233
196
|
}
|
|
234
|
-
}
|
|
197
|
+
}
|
|
198
|
+
else if (b === 0) {
|
|
235
199
|
// end of string
|
|
236
200
|
break;
|
|
237
|
-
}
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
238
203
|
pctx.putByte(b);
|
|
239
204
|
}
|
|
240
205
|
}
|
|
241
|
-
|
|
242
|
-
return implReturn(utf8ToString(pctx.buffer()));
|
|
206
|
+
return implReturn((0, cpon_1.utf8ToString)(pctx.buffer()));
|
|
243
207
|
}
|
|
244
|
-
|
|
245
208
|
default:
|
|
246
209
|
throw new TypeError('ChainPack - Invalid type info: ' + codePointAt);
|
|
247
210
|
}
|
|
248
211
|
}
|
|
249
212
|
/* eslint-enable complexity */
|
|
250
|
-
|
|
251
|
-
readUIntDataHelper(type: 'Int' | 'UInt') {
|
|
213
|
+
readUIntDataHelper(type) {
|
|
252
214
|
let num = 0;
|
|
253
215
|
const head = this.ctx.getByte();
|
|
254
216
|
let bytesToReadCnt;
|
|
@@ -258,135 +220,113 @@ class ChainPackReader {
|
|
|
258
220
|
num = head & 127;
|
|
259
221
|
break;
|
|
260
222
|
}
|
|
261
|
-
|
|
262
223
|
case head & 64: {
|
|
263
224
|
bytesToReadCnt = 1;
|
|
264
225
|
num = head & 63;
|
|
265
226
|
break;
|
|
266
227
|
}
|
|
267
|
-
|
|
268
228
|
case head & 32: {
|
|
269
229
|
bytesToReadCnt = 2;
|
|
270
230
|
num = head & 31;
|
|
271
231
|
break;
|
|
272
232
|
}
|
|
273
|
-
|
|
274
233
|
case head & 16: {
|
|
275
234
|
bytesToReadCnt = 3;
|
|
276
235
|
num = head & 15;
|
|
277
236
|
break;
|
|
278
237
|
}
|
|
279
|
-
|
|
280
238
|
default: {
|
|
281
239
|
bytesToReadCnt = (head & 0xF) + 4;
|
|
282
240
|
}
|
|
283
241
|
}
|
|
284
|
-
|
|
285
242
|
const bytes = new Uint8Array(bytesToReadCnt + 1);
|
|
286
243
|
bytes[0] = num;
|
|
287
244
|
for (let i = 0; i < bytesToReadCnt; i++) {
|
|
288
245
|
const r = this.ctx.getByte();
|
|
289
246
|
bytes[i + 1] = r;
|
|
290
247
|
}
|
|
291
|
-
|
|
292
248
|
return uint8ArrayBoBigint(bytes, type);
|
|
293
249
|
}
|
|
294
|
-
|
|
295
250
|
readIntData() {
|
|
296
251
|
const val = this.readUIntDataHelper('Int');
|
|
297
252
|
if (val <= Number.MIN_SAFE_INTEGER) {
|
|
298
253
|
return Number.MIN_SAFE_INTEGER;
|
|
299
254
|
}
|
|
300
|
-
|
|
301
255
|
if (val >= Number.MAX_SAFE_INTEGER) {
|
|
302
256
|
return Number.MAX_SAFE_INTEGER;
|
|
303
257
|
}
|
|
304
|
-
|
|
305
258
|
return Number(val);
|
|
306
259
|
}
|
|
307
|
-
|
|
308
260
|
readUIntData() {
|
|
309
261
|
const val = this.readUIntDataHelper('UInt');
|
|
310
262
|
if (val >= Number.MAX_SAFE_INTEGER) {
|
|
311
263
|
return Number.MAX_SAFE_INTEGER;
|
|
312
264
|
}
|
|
313
|
-
|
|
314
265
|
return Number(val);
|
|
315
266
|
}
|
|
316
|
-
|
|
317
267
|
readList() {
|
|
318
268
|
const lst = [];
|
|
319
269
|
while (true) {
|
|
320
270
|
const b = this.ctx.peekByte();
|
|
321
|
-
if (b
|
|
271
|
+
if (b === PackingSchema.Term) {
|
|
322
272
|
this.ctx.getByte();
|
|
323
273
|
break;
|
|
324
274
|
}
|
|
325
|
-
|
|
326
275
|
lst.push(this.read());
|
|
327
276
|
}
|
|
328
|
-
|
|
329
277
|
return lst;
|
|
330
278
|
}
|
|
331
|
-
|
|
332
279
|
readMetaMap() {
|
|
333
280
|
return this.implReadMap('metamap');
|
|
334
281
|
}
|
|
335
|
-
|
|
336
282
|
readMap() {
|
|
337
283
|
return this.implReadMap('map');
|
|
338
284
|
}
|
|
339
|
-
|
|
340
285
|
readIMap() {
|
|
341
286
|
return this.implReadMap('imap');
|
|
342
287
|
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
private implReadMap(mapType: 'metamap'): MetaMap;
|
|
347
|
-
private implReadMap(mapType: 'map' | 'imap' | 'metamap') {
|
|
348
|
-
const map: ShvMap | IMap | MetaMap = {
|
|
349
|
-
[shvMapType]: mapType,
|
|
288
|
+
implReadMap(mapType) {
|
|
289
|
+
const map = {
|
|
290
|
+
[rpcvalue_1.shvMapType]: mapType,
|
|
350
291
|
};
|
|
351
292
|
while (true) {
|
|
352
293
|
const b = this.ctx.peekByte();
|
|
353
|
-
if (b
|
|
294
|
+
if (b === PackingSchema.Term) {
|
|
354
295
|
this.ctx.getByte();
|
|
355
296
|
break;
|
|
356
297
|
}
|
|
357
|
-
|
|
358
298
|
const key = this.read();
|
|
359
299
|
const val = this.read();
|
|
360
|
-
if (map[shvMapType] === 'metamap' && typeof key === 'string') {
|
|
300
|
+
if (map[rpcvalue_1.shvMapType] === 'metamap' && typeof key === 'string') {
|
|
361
301
|
map[key] = val;
|
|
362
|
-
}
|
|
302
|
+
}
|
|
303
|
+
else if (map[rpcvalue_1.shvMapType] === 'metamap' && typeof key === 'number') {
|
|
363
304
|
map[Number(key)] = val;
|
|
364
|
-
}
|
|
305
|
+
}
|
|
306
|
+
else if (map[rpcvalue_1.shvMapType] === 'map' && typeof key === 'string') {
|
|
365
307
|
map[key] = val;
|
|
366
|
-
}
|
|
308
|
+
}
|
|
309
|
+
else if (map[rpcvalue_1.shvMapType] === 'imap' && typeof key === 'number') {
|
|
367
310
|
map[Number(key)] = val;
|
|
368
|
-
}
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
369
313
|
throw new TypeError('Malformed map, invalid key');
|
|
370
314
|
}
|
|
371
315
|
}
|
|
372
|
-
|
|
373
316
|
return map;
|
|
374
317
|
}
|
|
375
318
|
}
|
|
376
|
-
|
|
319
|
+
exports.ChainPackReader = ChainPackReader;
|
|
377
320
|
class ChainPackWriter {
|
|
378
|
-
ctx
|
|
379
|
-
|
|
321
|
+
ctx;
|
|
380
322
|
constructor() {
|
|
381
|
-
this.ctx = new PackContext();
|
|
323
|
+
this.ctx = new cpcontext_1.PackContext();
|
|
382
324
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
if (rpcVal instanceof RpcValueWithMetaData) {
|
|
325
|
+
write(rpcVal) {
|
|
326
|
+
if (rpcVal instanceof rpcvalue_1.RpcValueWithMetaData) {
|
|
386
327
|
this.writeMeta(rpcVal.meta);
|
|
387
328
|
rpcVal = rpcVal.value;
|
|
388
329
|
}
|
|
389
|
-
|
|
390
330
|
switch (true) {
|
|
391
331
|
case rpcVal === undefined:
|
|
392
332
|
this.ctx.putByte(PackingSchema.Null);
|
|
@@ -400,13 +340,13 @@ class ChainPackWriter {
|
|
|
400
340
|
case rpcVal instanceof ArrayBuffer:
|
|
401
341
|
this.writeBlob(rpcVal);
|
|
402
342
|
break;
|
|
403
|
-
case rpcVal instanceof UInt:
|
|
343
|
+
case rpcVal instanceof rpcvalue_1.UInt:
|
|
404
344
|
this.writeUInt(rpcVal);
|
|
405
345
|
break;
|
|
406
346
|
case typeof rpcVal === 'number':
|
|
407
347
|
this.writeInt(rpcVal);
|
|
408
348
|
break;
|
|
409
|
-
case rpcVal instanceof Decimal:
|
|
349
|
+
case rpcVal instanceof rpcvalue_1.Decimal:
|
|
410
350
|
this.writeDecimal(rpcVal);
|
|
411
351
|
break;
|
|
412
352
|
case Array.isArray(rpcVal):
|
|
@@ -415,11 +355,11 @@ class ChainPackWriter {
|
|
|
415
355
|
case rpcVal instanceof Date:
|
|
416
356
|
this.writeDateTime(rpcVal);
|
|
417
357
|
break;
|
|
418
|
-
case rpcVal instanceof Double:
|
|
358
|
+
case rpcVal instanceof rpcvalue_1.Double:
|
|
419
359
|
this.writeDouble(rpcVal);
|
|
420
360
|
break;
|
|
421
361
|
case typeof rpcVal === 'object':
|
|
422
|
-
switch (rpcVal[shvMapType]) {
|
|
362
|
+
switch (rpcVal[rpcvalue_1.shvMapType]) {
|
|
423
363
|
case 'imap':
|
|
424
364
|
this.writeIMap(rpcVal);
|
|
425
365
|
break;
|
|
@@ -427,114 +367,95 @@ class ChainPackWriter {
|
|
|
427
367
|
this.writeMap(rpcVal);
|
|
428
368
|
break;
|
|
429
369
|
}
|
|
430
|
-
|
|
431
370
|
break;
|
|
432
371
|
default:
|
|
433
372
|
console.log('Can\'t serialize', rpcVal);
|
|
434
373
|
throw new Error('Can\'t serialize');
|
|
435
374
|
}
|
|
436
375
|
}
|
|
437
|
-
|
|
438
|
-
writeUIntDataHelper(num: bigint, type: 'Int' | 'UInt') {
|
|
376
|
+
writeUIntDataHelper(num, type) {
|
|
439
377
|
const isNegative = num < 0;
|
|
440
378
|
if (isNegative) {
|
|
441
379
|
num = -num;
|
|
442
380
|
}
|
|
443
|
-
|
|
444
|
-
const significantBits = significantBitsCount(num) + (/* one more needed for sign bit */ type === 'Int' ? 1 : 0);
|
|
381
|
+
const significantBits = significantBitsCount(num) + ( /* one more needed for sign bit */type === 'Int' ? 1 : 0);
|
|
445
382
|
const bytesNeeded = significantBits <= 28 ? Math.trunc((significantBits - 1) / 7) + 1 : Math.trunc((significantBits - 1) / 8) + 2;
|
|
446
|
-
|
|
447
383
|
switch (bytesNeeded) {
|
|
448
384
|
case 0:
|
|
449
385
|
throw new Error(`Failed to count bytes needed for ${num}`);
|
|
450
386
|
case 1:
|
|
451
387
|
if (isNegative) {
|
|
452
|
-
num |=
|
|
388
|
+
num |= 64n;
|
|
453
389
|
}
|
|
454
|
-
|
|
455
390
|
break;
|
|
456
391
|
case 2:
|
|
457
|
-
num |=
|
|
392
|
+
num |= 128n << 8n;
|
|
458
393
|
if (isNegative) {
|
|
459
|
-
num |=
|
|
394
|
+
num |= 32n << 8n;
|
|
460
395
|
}
|
|
461
|
-
|
|
462
396
|
break;
|
|
463
397
|
case 3:
|
|
464
|
-
num |=
|
|
398
|
+
num |= 192n << 16n;
|
|
465
399
|
if (isNegative) {
|
|
466
|
-
num |=
|
|
400
|
+
num |= 16n << 16n;
|
|
467
401
|
}
|
|
468
|
-
|
|
469
402
|
break;
|
|
470
403
|
case 4:
|
|
471
|
-
num |=
|
|
404
|
+
num |= 224n << 24n;
|
|
472
405
|
if (isNegative) {
|
|
473
|
-
num |=
|
|
406
|
+
num |= 8n << 24n;
|
|
474
407
|
}
|
|
475
|
-
|
|
476
408
|
break;
|
|
477
409
|
default:
|
|
478
|
-
num |=
|
|
410
|
+
num |= 240n << BigInt((bytesNeeded - 1) * 8);
|
|
479
411
|
num |= BigInt(bytesNeeded - 4 /* n is offset by 4 */ - 1 /* for the control byte */) << BigInt((bytesNeeded - 1) * 8);
|
|
480
412
|
if (isNegative) {
|
|
481
|
-
num |=
|
|
413
|
+
num |= 128n << BigInt((bytesNeeded - 2) * 8);
|
|
482
414
|
}
|
|
483
|
-
|
|
484
415
|
break;
|
|
485
416
|
}
|
|
486
|
-
|
|
487
417
|
const bytes = numberToUint8Array(num);
|
|
488
418
|
for (const byte of bytes) {
|
|
489
419
|
this.ctx.putByte(byte);
|
|
490
420
|
}
|
|
491
421
|
}
|
|
492
|
-
|
|
493
|
-
writeUIntData(num: number) {
|
|
422
|
+
writeUIntData(num) {
|
|
494
423
|
this.writeUIntDataHelper(BigInt(num), 'UInt');
|
|
495
424
|
}
|
|
496
|
-
|
|
497
|
-
writeIntData(snum: number) {
|
|
425
|
+
writeIntData(snum) {
|
|
498
426
|
this.writeUIntDataHelper(BigInt(snum), 'Int');
|
|
499
427
|
}
|
|
500
|
-
|
|
501
|
-
writeInt(n: Int) {
|
|
428
|
+
writeInt(n) {
|
|
502
429
|
if (Number(n) >= 0 && Number(n) < 64) {
|
|
503
430
|
this.ctx.putByte(Number(n) + 64);
|
|
504
431
|
return;
|
|
505
432
|
}
|
|
506
|
-
|
|
507
433
|
this.ctx.putByte(PackingSchema.Int);
|
|
508
434
|
this.writeIntData(Number(n));
|
|
509
435
|
}
|
|
510
|
-
|
|
511
|
-
writeUInt(n: UInt) {
|
|
436
|
+
writeUInt(n) {
|
|
512
437
|
if (Number(n) < 64) {
|
|
513
438
|
this.ctx.putByte(Number(n));
|
|
514
439
|
return;
|
|
515
440
|
}
|
|
516
|
-
|
|
517
441
|
this.ctx.putByte(PackingSchema.UInt);
|
|
518
442
|
this.writeUIntData(Number(n));
|
|
519
443
|
}
|
|
520
|
-
|
|
521
|
-
writeJSString(str: string) {
|
|
444
|
+
writeJSString(str) {
|
|
522
445
|
this.ctx.putByte(PackingSchema.String);
|
|
523
|
-
const pctx = new PackContext();
|
|
446
|
+
const pctx = new cpcontext_1.PackContext();
|
|
524
447
|
pctx.writeStringUtf8(str);
|
|
525
448
|
this.writeUIntData(pctx.length);
|
|
526
449
|
for (let i = 0; i < pctx.length; i++) {
|
|
527
450
|
this.ctx.putByte(pctx.data[i]);
|
|
528
451
|
}
|
|
529
452
|
}
|
|
530
|
-
|
|
531
|
-
writeDecimal(val: Decimal) {
|
|
453
|
+
writeDecimal(val) {
|
|
532
454
|
this.ctx.putByte(PackingSchema.Decimal);
|
|
533
455
|
this.writeIntData(val.mantisa);
|
|
534
456
|
this.writeIntData(val.exponent);
|
|
535
457
|
}
|
|
536
|
-
|
|
537
|
-
writeBlob(blob: ArrayBuffer) {
|
|
458
|
+
writeBlob(blob) {
|
|
538
459
|
this.ctx.putByte(PackingSchema.Blob);
|
|
539
460
|
const arr = new Uint8Array(blob);
|
|
540
461
|
this.writeUIntData(arr.length);
|
|
@@ -542,119 +463,100 @@ class ChainPackWriter {
|
|
|
542
463
|
this.ctx.putByte(element);
|
|
543
464
|
}
|
|
544
465
|
}
|
|
545
|
-
|
|
546
|
-
writeDateTime(dt: DateTime) {
|
|
466
|
+
writeDateTime(dt) {
|
|
547
467
|
this.ctx.putByte(PackingSchema.DateTime);
|
|
548
|
-
|
|
549
468
|
let msecs = (dt.getTime() + (60_000 * (dt.utc_offset ?? 0))) - SHV_EPOCH_MSEC;
|
|
550
469
|
if (msecs < 0) {
|
|
551
470
|
throw new RangeError('DateTime prior to 2018-02-02 are not supported in current ChainPack implementation.');
|
|
552
471
|
}
|
|
553
|
-
|
|
554
472
|
const ms = msecs % 1000;
|
|
555
473
|
if (ms === 0) {
|
|
556
474
|
msecs /= 1000;
|
|
557
475
|
}
|
|
558
|
-
|
|
559
476
|
let bi = BigInt(msecs);
|
|
560
477
|
if (dt.utc_offset !== undefined && dt.utc_offset !== 0) {
|
|
561
478
|
bi <<= 7n;
|
|
562
479
|
bi |= BigInt((dt.utc_offset / 15) & 0x7F);
|
|
563
480
|
}
|
|
564
|
-
|
|
565
481
|
bi <<= 2n;
|
|
566
|
-
|
|
567
482
|
if (dt.utc_offset !== undefined && dt.utc_offset !== 0) {
|
|
568
483
|
bi |= 1n;
|
|
569
484
|
}
|
|
570
|
-
|
|
571
485
|
if (ms === 0) {
|
|
572
486
|
bi |= 2n;
|
|
573
487
|
}
|
|
574
|
-
|
|
575
488
|
// save as signed int
|
|
576
489
|
this.writeUIntDataHelper(bi, 'UInt');
|
|
577
490
|
}
|
|
578
|
-
|
|
579
|
-
writeDouble(double: Double) {
|
|
491
|
+
writeDouble(double) {
|
|
580
492
|
this.ctx.putByte(PackingSchema.Double);
|
|
581
493
|
const buffer = new ArrayBuffer(8);
|
|
582
494
|
const view = new DataView(buffer);
|
|
583
|
-
|
|
584
495
|
view.setFloat64(0, double.value, true);
|
|
585
|
-
|
|
586
496
|
const data = new Uint8Array(buffer);
|
|
587
497
|
for (let i = 0; i < 8; i++) {
|
|
588
498
|
this.ctx.putByte(data[i]);
|
|
589
499
|
}
|
|
590
500
|
}
|
|
591
|
-
|
|
592
|
-
writeList(lst: RpcValue[]) {
|
|
501
|
+
writeList(lst) {
|
|
593
502
|
this.ctx.putByte(PackingSchema.List);
|
|
594
503
|
for (const element of lst) {
|
|
595
504
|
this.write(element);
|
|
596
505
|
}
|
|
597
|
-
|
|
598
506
|
this.ctx.putByte(PackingSchema.Term);
|
|
599
507
|
}
|
|
600
|
-
|
|
601
|
-
writeMeta(map: MetaMap) {
|
|
508
|
+
writeMeta(map) {
|
|
602
509
|
this.ctx.putByte(PackingSchema.MetaMap);
|
|
603
510
|
this.writeMapContent(map);
|
|
604
511
|
}
|
|
605
|
-
|
|
606
|
-
writeIMap(map: IMap) {
|
|
512
|
+
writeIMap(map) {
|
|
607
513
|
this.ctx.putByte(PackingSchema.IMap);
|
|
608
514
|
this.writeMapContent(map);
|
|
609
515
|
}
|
|
610
|
-
|
|
611
|
-
writeMap(map: ShvMap) {
|
|
516
|
+
writeMap(map) {
|
|
612
517
|
this.ctx.putByte(PackingSchema.Map);
|
|
613
518
|
this.writeMapContent(map);
|
|
614
519
|
}
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
for (const [key, value] of Object.entries<RpcValue>(map)) {
|
|
520
|
+
writeMapContent(map) {
|
|
521
|
+
for (const [key, value] of Object.entries(map)) {
|
|
618
522
|
if (value === undefined) {
|
|
619
523
|
continue;
|
|
620
524
|
}
|
|
621
|
-
|
|
622
|
-
if (map[shvMapType] === 'imap') {
|
|
525
|
+
if (map[rpcvalue_1.shvMapType] === 'imap') {
|
|
623
526
|
const intKey = Number(key);
|
|
624
527
|
if (Number.isNaN(intKey)) {
|
|
625
528
|
throw new TypeError('Invalid NaN IMap key');
|
|
626
529
|
}
|
|
627
|
-
|
|
628
530
|
this.writeInt(intKey);
|
|
629
|
-
}
|
|
531
|
+
}
|
|
532
|
+
else if (map[rpcvalue_1.shvMapType] === 'metamap') {
|
|
630
533
|
const intKey = Number(key);
|
|
631
534
|
if (Number.isNaN(intKey)) {
|
|
632
535
|
this.writeJSString(key.toString());
|
|
633
|
-
}
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
634
538
|
this.writeInt(intKey);
|
|
635
539
|
}
|
|
636
|
-
}
|
|
540
|
+
}
|
|
541
|
+
else {
|
|
637
542
|
this.writeJSString(key.toString());
|
|
638
543
|
}
|
|
639
|
-
|
|
640
544
|
this.write(value);
|
|
641
545
|
}
|
|
642
|
-
|
|
643
546
|
this.ctx.putByte(PackingSchema.Term);
|
|
644
547
|
}
|
|
645
548
|
}
|
|
646
|
-
|
|
647
|
-
const toChainPack = (value
|
|
549
|
+
exports.ChainPackWriter = ChainPackWriter;
|
|
550
|
+
const toChainPack = (value) => {
|
|
648
551
|
const wr = new ChainPackWriter();
|
|
649
552
|
wr.write(value);
|
|
650
553
|
return wr.ctx.buffer();
|
|
651
554
|
};
|
|
652
|
-
|
|
653
|
-
const fromChainPack = (buffer
|
|
555
|
+
exports.toChainPack = toChainPack;
|
|
556
|
+
const fromChainPack = (buffer) => {
|
|
654
557
|
const reader = new ChainPackReader(buffer);
|
|
655
558
|
return reader.read();
|
|
656
559
|
};
|
|
657
|
-
|
|
560
|
+
exports.fromChainPack = fromChainPack;
|
|
658
561
|
const CHAINPACK_PROTOCOL_TYPE = 1;
|
|
659
|
-
|
|
660
|
-
export {toChainPack, fromChainPack, ChainPackReader, ChainPackWriter, CHAINPACK_PROTOCOL_TYPE};
|
|
562
|
+
exports.CHAINPACK_PROTOCOL_TYPE = CHAINPACK_PROTOCOL_TYPE;
|