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.
@@ -1,108 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fromCpon = exports.toCpon = exports.CPON_PROTOCOL_TYPE = exports.CponReader = exports.CponWriter = exports.utf8ToString = exports.OneLiners = exports.stringifyDate = void 0;
1
4
  /* eslint-disable no-bitwise */
2
- import {type RpcValue, type RpcValueType, type DateTime, type List, Decimal, Double, type IMap, type Int, type MetaMap, RpcValueWithMetaData, type ShvMap, UInt, withOffset, shvMapType, isShvMap, isIMap} from './rpcvalue';
3
- import {PackContext, UnpackContext} from './cpcontext';
4
-
5
- export const stringifyDate = (dt: DateTime) => {
5
+ const rpcvalue_1 = require("./rpcvalue");
6
+ const cpcontext_1 = require("./cpcontext");
7
+ const stringifyDate = (dt) => {
6
8
  const epochMsec = dt.getTime();
7
9
  let utcOffset = dt.utc_offset;
8
10
  const localMsec = epochMsec + (60_000 * (utcOffset ?? 0));
9
11
  let res = new Date(localMsec).toISOString();
10
12
  const rtrim = ((localMsec % 1000) !== 0) ? 1 : 5;
11
13
  res = res.slice(0, Math.max(0, res.length - rtrim));
12
-
13
14
  if (utcOffset === undefined || utcOffset === 0) {
14
15
  res += 'Z';
15
- } else {
16
+ }
17
+ else {
16
18
  if (utcOffset < 0) {
17
19
  res += '-';
18
20
  utcOffset = -utcOffset;
19
- } else {
21
+ }
22
+ else {
20
23
  res += '+';
21
24
  }
22
-
23
25
  res += (Math.trunc(utcOffset / 60)).toString().padStart(2, '0');
24
26
  if ((utcOffset % 60) !== 0) {
25
27
  res += (utcOffset % 60).toString().padStart(2, '0');
26
28
  }
27
29
  }
28
-
29
30
  return res;
30
31
  };
31
-
32
- const hexify = (byte: number) => {
32
+ exports.stringifyDate = stringifyDate;
33
+ const hexify = (byte) => {
33
34
  if (byte < 10) {
34
35
  return 48 + byte;
35
36
  }
36
-
37
37
  if (byte < 16) {
38
38
  return 97 + byte - 10;
39
39
  }
40
-
41
- return '?'.codePointAt(0)!;
40
+ return '?'.codePointAt(0);
42
41
  };
43
-
44
- const unhex = (byte: number) => {
42
+ const unhex = (byte) => {
45
43
  if (byte >= 48 && byte <= 57) { // 0-9
46
44
  return byte - 48;
47
45
  }
48
-
49
46
  if (byte >= 65 && byte <= 70) { // A-F
50
47
  return byte - 65 + 10;
51
48
  }
52
-
53
49
  if (byte >= 97 && byte <= 102) { // a-f
54
50
  return byte - 97 + 10;
55
51
  }
56
-
57
52
  throw new TypeError('Invalid HEX digit: ' + byte);
58
53
  };
59
-
60
- const utf8ToString = (bytearray: ArrayBuffer) => {
54
+ const utf8ToString = (bytearray) => {
61
55
  const uint8Array = new Uint8Array(bytearray);
62
56
  let str = '';
63
57
  for (let i = 0; i < uint8Array.length; i++) {
64
58
  const value = uint8Array[i];
65
-
66
59
  if (value < 0x80) {
67
60
  str += String.fromCodePoint(value);
68
- } else if (value > 0xBF && value < 0xE0) {
61
+ }
62
+ else if (value > 0xBF && value < 0xE0) {
69
63
  str += String.fromCodePoint(((value & 0x1F) << 6) | (uint8Array[i + 1] & 0x3F));
70
64
  i += 1;
71
- } else if (value > 0xDF && value < 0xF0) {
65
+ }
66
+ else if (value > 0xDF && value < 0xF0) {
72
67
  str += String.fromCodePoint(((value & 0x0F) << 12) | ((uint8Array[i + 1] & 0x3F) << 6) | (uint8Array[i + 2] & 0x3F));
73
68
  i += 2;
74
- } else {
69
+ }
70
+ else {
75
71
  // surrogate pair
76
72
  const charCode = (((value & 0x07) << 18) | ((uint8Array[i + 1] & 0x3F) << 12) | ((uint8Array[i + 2] & 0x3F) << 6) | (uint8Array[i + 3] & 0x3F)) - 0x01_00_00;
77
-
78
73
  str += String.fromCodePoint((charCode >> 10) | 0xD8_00, (charCode & 0x03_FF) | 0xDC_00);
79
74
  i += 3;
80
75
  }
81
76
  }
82
-
83
77
  return str;
84
78
  };
85
-
79
+ exports.utf8ToString = utf8ToString;
86
80
  class CponReader {
87
- ctx: UnpackContext;
88
-
89
- constructor(unpackContext: UnpackContext) {
81
+ ctx;
82
+ constructor(unpackContext) {
90
83
  this.ctx = unpackContext;
91
84
  }
92
-
93
- read(): RpcValue {
94
- let meta: MetaMap | undefined;
85
+ read() {
86
+ let meta;
95
87
  this.skipWhitespace();
96
88
  let b = this.ctx.peekByte();
97
89
  if (b === '<'.codePointAt(0)) {
98
90
  meta = this.readMetaMap();
99
91
  }
100
-
101
- const implReturn = (x: RpcValueType) => {
102
- const ret = meta !== undefined ? new RpcValueWithMetaData(meta, x) : x;
92
+ const implReturn = (x) => {
93
+ const ret = meta !== undefined ? new rpcvalue_1.RpcValueWithMetaData(meta, x) : x;
103
94
  return ret;
104
95
  };
105
-
106
96
  this.skipWhitespace();
107
97
  b = this.ctx.peekByte();
108
98
  // console.log("CHAR:", b, String.fromCodePoint(b));
@@ -110,91 +100,74 @@ class CponReader {
110
100
  if ((b >= 48 && b <= 57) || b === 43 || b === 45) {
111
101
  return implReturn(this.readNumber());
112
102
  }
113
-
114
103
  if (b === '"'.codePointAt(0)) {
115
104
  return implReturn(this.readCString());
116
105
  }
117
-
118
106
  if (b === 'b'.codePointAt(0)) {
119
107
  this.ctx.getByte();
120
108
  b = this.ctx.peekByte();
121
109
  if (b === '"'.codePointAt(0)) {
122
110
  return implReturn(this.readBlobEsc());
123
111
  }
124
-
125
112
  throw new TypeError('Invalid Blob prefix.');
126
113
  }
127
-
128
114
  if (b === 'x'.codePointAt(0)) {
129
115
  this.ctx.getByte();
130
116
  b = this.ctx.peekByte();
131
117
  if (b === '"'.codePointAt(0)) {
132
118
  return implReturn(this.readBlobHex());
133
119
  }
134
-
135
120
  throw new TypeError('Invalid HEX Blob prefix.');
136
121
  }
137
-
138
122
  if (b === '['.codePointAt(0)) {
139
123
  return implReturn(this.readList());
140
124
  }
141
-
142
125
  if (b === '{'.codePointAt(0)) {
143
126
  return implReturn(this.readMap());
144
127
  }
145
-
146
128
  if (b === 'i'.codePointAt(0)) {
147
129
  this.ctx.getByte();
148
130
  b = this.ctx.peekByte();
149
131
  if (b === '{'.codePointAt(0)) {
150
132
  return implReturn(this.readIMap());
151
133
  }
152
-
153
134
  throw new TypeError('Invalid IMap prefix.');
154
135
  }
155
-
156
136
  if (b === 'd'.codePointAt(0)) {
157
137
  this.ctx.getByte();
158
138
  b = this.ctx.peekByte();
159
139
  if (b === '"'.codePointAt(0)) {
160
140
  return implReturn(this.readDateTime());
161
141
  }
162
-
163
142
  throw new TypeError('Invalid DateTime prefix.');
164
143
  }
165
-
166
144
  if (b === 't'.codePointAt(0)) {
167
145
  this.ctx.getBytes('true');
168
146
  return implReturn(true);
169
147
  }
170
-
171
148
  if (b === 'f'.codePointAt(0)) {
172
149
  this.ctx.getBytes('false');
173
150
  return implReturn(false);
174
151
  }
175
-
176
152
  if (b === 'n'.codePointAt(0)) {
177
153
  this.ctx.getBytes('null');
178
154
  return implReturn(undefined);
179
155
  }
180
-
181
156
  throw new TypeError('Malformed Cpon input.');
182
157
  }
183
-
184
158
  skipWhitespace() {
185
- const SPACE = ' '.codePointAt(0)!;
186
- const SLASH = '/'.codePointAt(0)!;
187
- const STAR = '*'.codePointAt(0)!;
188
- const LF = '\n'.codePointAt(0)!;
189
- const KEY_DELIM = ':'.codePointAt(0)!;
190
- const FIELD_DELIM = ','.codePointAt(0)!;
159
+ const SPACE = ' '.codePointAt(0);
160
+ const SLASH = '/'.codePointAt(0);
161
+ const STAR = '*'.codePointAt(0);
162
+ const LF = '\n'.codePointAt(0);
163
+ const KEY_DELIM = ':'.codePointAt(0);
164
+ const FIELD_DELIM = ','.codePointAt(0);
191
165
  /* eslint-disable max-depth */
192
166
  while (true) {
193
167
  let b = this.ctx.peekByte();
194
168
  if (b < 1) {
195
169
  return;
196
170
  }
197
-
198
171
  if (b > SPACE) {
199
172
  switch (b) {
200
173
  case SLASH: {
@@ -211,7 +184,8 @@ class CponReader {
211
184
  }
212
185
  }
213
186
  }
214
- } else if (b === SLASH) {
187
+ }
188
+ else if (b === SLASH) {
215
189
  // to end of line comment entered;
216
190
  while (true) {
217
191
  b = this.ctx.getByte();
@@ -219,33 +193,30 @@ class CponReader {
219
193
  break;
220
194
  }
221
195
  }
222
- } else {
196
+ }
197
+ else {
223
198
  throw new TypeError('Malformed comment');
224
199
  }
225
-
226
200
  break;
227
201
  }
228
-
229
202
  case KEY_DELIM: {
230
203
  this.ctx.getByte();
231
204
  continue;
232
205
  }
233
-
234
206
  case FIELD_DELIM: {
235
207
  this.ctx.getByte();
236
208
  continue;
237
209
  }
238
-
239
210
  default:
240
211
  return;
241
212
  }
242
- } else {
213
+ }
214
+ else {
243
215
  this.ctx.getByte();
244
216
  }
245
217
  }
246
218
  /* eslint-enable max-depth */
247
219
  }
248
-
249
220
  readDateTime() {
250
221
  this.ctx.getByte(); // eat '"'
251
222
  let b = this.ctx.peekByte();
@@ -254,17 +225,13 @@ class CponReader {
254
225
  this.ctx.getByte();
255
226
  throw new TypeError('Malformed empty date separator in DateTime');
256
227
  }
257
-
258
228
  const date = this.readDateTimeInner();
259
-
260
229
  b = this.ctx.getByte();
261
230
  if (b !== '"'.codePointAt(0)) {
262
231
  throw new TypeError('DateTime literal should be terminated by \'"\'.');
263
232
  }
264
-
265
233
  return date;
266
234
  }
267
-
268
235
  readDateTimeInner() {
269
236
  let year = 0;
270
237
  let month = 0;
@@ -274,55 +241,43 @@ class CponReader {
274
241
  let sec = 0;
275
242
  let msec = 0;
276
243
  let utcOffset = 0;
277
-
278
244
  year = Number(this.readInt());
279
-
280
245
  let b = this.ctx.getByte();
281
246
  if (b !== '-'.codePointAt(0)) {
282
247
  throw new TypeError('Malformed year-month separator in DateTime');
283
248
  }
284
-
285
249
  month = Number(this.readInt());
286
-
287
250
  b = this.ctx.getByte();
288
251
  if (b !== '-'.codePointAt(0)) {
289
252
  throw new TypeError('Malformed year-month separator in DateTime');
290
253
  }
291
-
292
254
  day = Number(this.readInt());
293
-
294
255
  b = this.ctx.getByte();
295
256
  if (b !== ' '.codePointAt(0) && b !== 'T'.codePointAt(0)) {
296
257
  throw new TypeError('Malformed date-time separator in DateTime');
297
258
  }
298
-
299
259
  hour = Number(this.readInt());
300
-
301
260
  b = this.ctx.getByte();
302
261
  if (b !== ':'.codePointAt(0)) {
303
262
  throw new TypeError('Malformed year-month separator in DateTime');
304
263
  }
305
-
306
264
  min = Number(this.readInt());
307
-
308
265
  b = this.ctx.getByte();
309
266
  if (b !== ':'.codePointAt(0)) {
310
267
  throw new TypeError('Malformed year-month separator in DateTime');
311
268
  }
312
-
313
269
  sec = Number(this.readInt());
314
-
315
270
  b = this.ctx.peekByte();
316
271
  if (b === '.'.codePointAt(0)) {
317
272
  this.ctx.getByte();
318
273
  msec = Number(this.readInt());
319
274
  }
320
-
321
275
  b = this.ctx.peekByte();
322
276
  if (b === 'Z'.codePointAt(0)) {
323
277
  // zulu time
324
278
  this.ctx.getByte();
325
- } else if (b === '+'.codePointAt(0) || b === '-'.codePointAt(0)) {
279
+ }
280
+ else if (b === '+'.codePointAt(0) || b === '-'.codePointAt(0)) {
326
281
  // UTC time offset
327
282
  this.ctx.getByte();
328
283
  const ix1 = this.ctx.index;
@@ -331,26 +286,23 @@ class CponReader {
331
286
  if (!(n === 2 || n === 4)) {
332
287
  throw new TypeError('Malformed TS offset in DateTime.');
333
288
  }
334
-
335
289
  if (n === 2) {
336
290
  utcOffset = 60 * val;
337
- } else if (n === 4) {
291
+ }
292
+ else if (n === 4) {
338
293
  utcOffset = (60 * (Math.trunc(val / 100))) + (val % 100);
339
294
  }
340
-
341
295
  if (b === '-'.codePointAt(0)) {
342
296
  utcOffset = -utcOffset;
343
297
  }
344
298
  }
345
-
346
299
  // let epoch_sec = CponReader.timegm(year, month, mday, hour, min, sec);
347
300
  let epochMsec = Date.UTC(year, month - 1, day, hour, min, sec, msec);
348
301
  epochMsec -= utcOffset * 60_000;
349
- return withOffset(new Date(epochMsec), utcOffset ?? undefined);
302
+ return (0, rpcvalue_1.withOffset)(new Date(epochMsec), utcOffset ?? undefined);
350
303
  }
351
-
352
304
  readCString() {
353
- const pctx = new PackContext();
305
+ const pctx = new cpcontext_1.PackContext();
354
306
  this.ctx.getByte(); // eat '"'
355
307
  while (true) {
356
308
  let b = this.ctx.getByte();
@@ -358,25 +310,25 @@ class CponReader {
358
310
  b = this.ctx.getByte();
359
311
  switch (b) {
360
312
  case '\\'.codePointAt(0):
361
- pctx.putByte('\\'.codePointAt(0)!);
313
+ pctx.putByte('\\'.codePointAt(0));
362
314
  break;
363
315
  case '"'.codePointAt(0):
364
- pctx.putByte('"'.codePointAt(0)!);
316
+ pctx.putByte('"'.codePointAt(0));
365
317
  break;
366
318
  case 'b'.codePointAt(0):
367
- pctx.putByte('\b'.codePointAt(0)!);
319
+ pctx.putByte('\b'.codePointAt(0));
368
320
  break;
369
321
  case 'f'.codePointAt(0):
370
- pctx.putByte('\f'.codePointAt(0)!);
322
+ pctx.putByte('\f'.codePointAt(0));
371
323
  break;
372
324
  case 'n'.codePointAt(0):
373
- pctx.putByte('\n'.codePointAt(0)!);
325
+ pctx.putByte('\n'.codePointAt(0));
374
326
  break;
375
327
  case 'r'.codePointAt(0):
376
- pctx.putByte('\r'.codePointAt(0)!);
328
+ pctx.putByte('\r'.codePointAt(0));
377
329
  break;
378
330
  case 't'.codePointAt(0):
379
- pctx.putByte('\t'.codePointAt(0)!);
331
+ pctx.putByte('\t'.codePointAt(0));
380
332
  break;
381
333
  case '0'.codePointAt(0):
382
334
  pctx.putByte(0);
@@ -385,19 +337,19 @@ class CponReader {
385
337
  pctx.putByte(b);
386
338
  break;
387
339
  }
388
- } else if (b === '"'.codePointAt(0)) {
340
+ }
341
+ else if (b === '"'.codePointAt(0)) {
389
342
  // end of string
390
343
  break;
391
- } else {
344
+ }
345
+ else {
392
346
  pctx.putByte(b);
393
347
  }
394
348
  }
395
-
396
349
  return utf8ToString(pctx.buffer());
397
350
  }
398
-
399
351
  readBlobEsc() {
400
- const pctx = new PackContext();
352
+ const pctx = new cpcontext_1.PackContext();
401
353
  this.ctx.getByte(); // eat '"'
402
354
  while (true) {
403
355
  let b = this.ctx.getByte();
@@ -405,19 +357,19 @@ class CponReader {
405
357
  b = this.ctx.getByte();
406
358
  switch (b) {
407
359
  case '\\'.codePointAt(0):
408
- pctx.putByte('\\'.codePointAt(0)!);
360
+ pctx.putByte('\\'.codePointAt(0));
409
361
  break;
410
362
  case '"'.codePointAt(0):
411
- pctx.putByte('"'.codePointAt(0)!);
363
+ pctx.putByte('"'.codePointAt(0));
412
364
  break;
413
365
  case 'n'.codePointAt(0):
414
- pctx.putByte('\n'.codePointAt(0)!);
366
+ pctx.putByte('\n'.codePointAt(0));
415
367
  break;
416
368
  case 'r'.codePointAt(0):
417
- pctx.putByte('\r'.codePointAt(0)!);
369
+ pctx.putByte('\r'.codePointAt(0));
418
370
  break;
419
371
  case 't'.codePointAt(0):
420
- pctx.putByte('\t'.codePointAt(0)!);
372
+ pctx.putByte('\t'.codePointAt(0));
421
373
  break;
422
374
  default: {
423
375
  const b2 = (unhex(b) * 16) + unhex(this.ctx.getByte());
@@ -425,36 +377,36 @@ class CponReader {
425
377
  break;
426
378
  }
427
379
  }
428
- } else if (b === '"'.codePointAt(0)) {
380
+ }
381
+ else if (b === '"'.codePointAt(0)) {
429
382
  // end of string
430
383
  break;
431
- } else if (b < 128) {
384
+ }
385
+ else if (b < 128) {
432
386
  pctx.putByte(b);
433
- } else {
387
+ }
388
+ else {
434
389
  throw new TypeError('Escaped Blob characters must be lower than 128, code: ' + b);
435
390
  }
436
391
  }
437
-
438
392
  return pctx.buffer();
439
393
  }
440
-
441
394
  readBlobHex() {
442
- const pctx = new PackContext();
395
+ const pctx = new cpcontext_1.PackContext();
443
396
  this.ctx.getByte(); // eat '"'
444
397
  while (true) {
445
398
  const b = this.ctx.getByte();
446
399
  if (b === '"'.codePointAt(0)) {
447
400
  // end of string
448
401
  break;
449
- } else {
402
+ }
403
+ else {
450
404
  const b2 = (unhex(b) * 16) + unhex(this.ctx.getByte());
451
405
  pctx.putByte(b2);
452
406
  }
453
407
  }
454
-
455
408
  return pctx.buffer();
456
409
  }
457
-
458
410
  readList() {
459
411
  const lst = [];
460
412
  this.ctx.getByte(); // eat '['
@@ -465,89 +417,79 @@ class CponReader {
465
417
  this.ctx.getByte();
466
418
  break;
467
419
  }
468
-
469
420
  const item = this.read();
470
421
  lst.push(item);
471
422
  }
472
-
473
423
  return lst;
474
424
  }
475
-
476
425
  readMetaMap() {
477
- return this.implReadMap('metamap', '>'.codePointAt(0)!);
426
+ return this.implReadMap('metamap', '>'.codePointAt(0));
478
427
  }
479
-
480
428
  readMap() {
481
- return this.implReadMap('map', '}'.codePointAt(0)!);
429
+ return this.implReadMap('map', '}'.codePointAt(0));
482
430
  }
483
-
484
431
  readIMap() {
485
- return this.implReadMap('imap', '}'.codePointAt(0)!);
432
+ return this.implReadMap('imap', '}'.codePointAt(0));
486
433
  }
487
-
488
434
  readInt() {
489
435
  let base = 10;
490
436
  let val = 0;
491
437
  let neg = false;
492
438
  let n = 0;
493
- for (; ; n++) {
439
+ for (;; n++) {
494
440
  const b = this.ctx.peekByte();
495
441
  if (b < 0) {
496
442
  break;
497
443
  }
498
-
499
444
  if (b === 43 || b === 45) { // '+','-'
500
445
  if (n !== 0) {
501
446
  break;
502
447
  }
503
-
504
448
  this.ctx.getByte();
505
449
  if (b === 45) {
506
450
  neg = true;
507
451
  }
508
- } else if (b === 120) { // 'x'
452
+ }
453
+ else if (b === 120) { // 'x'
509
454
  if (n === 1 && val !== 0) {
510
455
  break;
511
456
  }
512
-
513
457
  if (n !== 1) {
514
458
  break;
515
459
  }
516
-
517
460
  this.ctx.getByte();
518
461
  base = 16;
519
- } else if (b >= 48 && b <= 57) { // '0' - '9'
462
+ }
463
+ else if (b >= 48 && b <= 57) { // '0' - '9'
520
464
  this.ctx.getByte();
521
465
  val *= base;
522
466
  val += b - 48;
523
- } else if (b >= 65 && b <= 70) { // 'A' - 'F'
467
+ }
468
+ else if (b >= 65 && b <= 70) { // 'A' - 'F'
524
469
  if (base !== 16) {
525
470
  break;
526
471
  }
527
-
528
472
  this.ctx.getByte();
529
473
  val *= base;
530
474
  val += b - 65 + 10;
531
- } else if (b >= 97 && b <= 102) { // 'a' - 'f'
475
+ }
476
+ else if (b >= 97 && b <= 102) { // 'a' - 'f'
532
477
  if (base !== 16) {
533
478
  break;
534
479
  }
535
-
536
480
  this.ctx.getByte();
537
481
  val *= base;
538
482
  val += b - 97 + 10;
539
- } else {
483
+ }
484
+ else {
540
485
  break;
541
486
  }
542
487
  }
543
-
544
488
  if (neg) {
545
489
  val = -val;
546
490
  }
547
-
548
491
  return val;
549
492
  }
550
-
551
493
  readNumber() {
552
494
  let mantisa = 0;
553
495
  let exponent = 0;
@@ -556,16 +498,15 @@ class CponReader {
556
498
  let isDecimal = false;
557
499
  let isUint = false;
558
500
  let isNeg = false;
559
-
560
501
  let b = this.ctx.peekByte();
561
- if (b === 43) {// '+'
502
+ if (b === 43) { // '+'
562
503
  isNeg = false;
563
504
  b = this.ctx.getByte();
564
- } else if (b === 45) {// '-'
505
+ }
506
+ else if (b === 45) { // '-'
565
507
  isNeg = true;
566
508
  b = this.ctx.getByte();
567
509
  }
568
-
569
510
  mantisa = Number(this.readInt());
570
511
  b = this.ctx.peekByte();
571
512
  (() => {
@@ -576,7 +517,6 @@ class CponReader {
576
517
  this.ctx.getByte();
577
518
  return;
578
519
  }
579
-
580
520
  case '.'.codePointAt(0): {
581
521
  isDecimal = true;
582
522
  this.ctx.getByte();
@@ -589,10 +529,8 @@ class CponReader {
589
529
  if (b >= 0) {
590
530
  continue;
591
531
  }
592
-
593
532
  return;
594
533
  }
595
-
596
534
  case 'e'.codePointAt(0):
597
535
  case 'E'.codePointAt(0): {
598
536
  isDecimal = true;
@@ -602,39 +540,29 @@ class CponReader {
602
540
  if (ix1 === this.ctx.index) {
603
541
  throw new TypeError('Malformed number exponential part.');
604
542
  }
605
-
606
543
  return;
607
544
  }
608
-
609
545
  default:
610
546
  return;
611
547
  }
612
548
  }
613
549
  })();
614
-
615
550
  if (isDecimal) {
616
551
  for (let i = 0; i < decCnt; ++i) {
617
552
  mantisa *= 10;
618
553
  }
619
-
620
554
  mantisa += decimals;
621
555
  mantisa = isNeg ? -mantisa : mantisa;
622
- return new Decimal(mantisa, exponent - decCnt);
556
+ return new rpcvalue_1.Decimal(mantisa, exponent - decCnt);
623
557
  }
624
-
625
558
  if (isUint) {
626
- return new UInt(mantisa);
559
+ return new rpcvalue_1.UInt(mantisa);
627
560
  }
628
-
629
561
  return isNeg ? -mantisa : mantisa;
630
562
  }
631
-
632
- private implReadMap(mapType: 'map', terminator: number): ShvMap;
633
- private implReadMap(mapType: 'imap', terminator: number): IMap;
634
- private implReadMap(mapType: 'metamap', terminator: number): MetaMap;
635
- private implReadMap(mapType: 'map' | 'imap' | 'metamap', terminator: number) {
636
- const map: MetaMap | ShvMap | IMap = {
637
- [shvMapType]: mapType,
563
+ implReadMap(mapType, terminator) {
564
+ const map = {
565
+ [rpcvalue_1.shvMapType]: mapType,
638
566
  };
639
567
  this.ctx.getByte(); // eat start
640
568
  while (true) {
@@ -644,46 +572,47 @@ class CponReader {
644
572
  this.ctx.getByte();
645
573
  break;
646
574
  }
647
-
648
575
  const key = this.read();
649
- if (key instanceof RpcValueWithMetaData) {
576
+ if (key instanceof rpcvalue_1.RpcValueWithMetaData) {
650
577
  throw new TypeError('Map/IMap/MetaMap key can\'t have its own MetaData');
651
578
  }
652
-
653
579
  this.skipWhitespace();
654
580
  const val = this.read();
655
-
656
- if (map[shvMapType] === 'metamap' && typeof key === 'string') {
581
+ if (map[rpcvalue_1.shvMapType] === 'metamap' && typeof key === 'string') {
657
582
  map[key] = val;
658
- } else if (map[shvMapType] === 'metamap' && (key instanceof UInt || typeof key === 'number')) {
583
+ }
584
+ else if (map[rpcvalue_1.shvMapType] === 'metamap' && (key instanceof rpcvalue_1.UInt || typeof key === 'number')) {
659
585
  map[Number(key)] = val;
660
- } else if (map[shvMapType] === 'map' && typeof key === 'string') {
586
+ }
587
+ else if (map[rpcvalue_1.shvMapType] === 'map' && typeof key === 'string') {
661
588
  map[key] = val;
662
- } else if (map[shvMapType] === 'imap' && (key instanceof UInt || typeof key === 'number')) {
589
+ }
590
+ else if (map[rpcvalue_1.shvMapType] === 'imap' && (key instanceof rpcvalue_1.UInt || typeof key === 'number')) {
663
591
  map[Number(key)] = val;
664
- } else {
592
+ }
593
+ else {
665
594
  throw new TypeError('Malformed map, invalid key');
666
595
  }
667
596
  }
668
-
669
597
  return map;
670
598
  }
671
599
  }
672
-
600
+ exports.CponReader = CponReader;
673
601
  class CponWriter {
674
- ctx: PackContext;
602
+ indentString;
603
+ oneLiners;
604
+ ctx;
675
605
  nestLevel = 0;
676
-
677
- constructor(private readonly indentString?: string, private oneLiners: OneLiners = OneLiners.Yes) {
678
- this.ctx = new PackContext();
606
+ constructor(indentString, oneLiners = OneLiners.Yes) {
607
+ this.indentString = indentString;
608
+ this.oneLiners = oneLiners;
609
+ this.ctx = new cpcontext_1.PackContext();
679
610
  }
680
-
681
- write(rpcVal: RpcValue) {
682
- if (rpcVal instanceof RpcValueWithMetaData) {
611
+ write(rpcVal) {
612
+ if (rpcVal instanceof rpcvalue_1.RpcValueWithMetaData) {
683
613
  this.writeMeta(rpcVal.meta);
684
614
  rpcVal = rpcVal.value;
685
615
  }
686
-
687
616
  switch (true) {
688
617
  case rpcVal === undefined:
689
618
  this.ctx.writeStringUtf8('null');
@@ -697,16 +626,16 @@ class CponWriter {
697
626
  case rpcVal instanceof ArrayBuffer:
698
627
  this.writeBlob(rpcVal);
699
628
  break;
700
- case rpcVal instanceof UInt:
629
+ case rpcVal instanceof rpcvalue_1.UInt:
701
630
  this.writeUInt(rpcVal);
702
631
  break;
703
632
  case typeof rpcVal === 'number':
704
633
  this.writeInt(rpcVal);
705
634
  break;
706
- case rpcVal instanceof Double:
635
+ case rpcVal instanceof rpcvalue_1.Double:
707
636
  this.writeDouble(rpcVal);
708
637
  break;
709
- case rpcVal instanceof Decimal:
638
+ case rpcVal instanceof rpcvalue_1.Decimal:
710
639
  this.writeDecimal(rpcVal);
711
640
  break;
712
641
  case Array.isArray(rpcVal):
@@ -716,7 +645,7 @@ class CponWriter {
716
645
  this.writeDateTime(rpcVal);
717
646
  break;
718
647
  case typeof rpcVal === 'object':
719
- switch (rpcVal[shvMapType]) {
648
+ switch (rpcVal[rpcvalue_1.shvMapType]) {
720
649
  case 'imap':
721
650
  this.writeIMap(rpcVal);
722
651
  break;
@@ -724,49 +653,45 @@ class CponWriter {
724
653
  this.writeMap(rpcVal);
725
654
  break;
726
655
  }
727
-
728
656
  break;
729
657
  default:
730
658
  console.log('Can\'t serialize rpc value', rpcVal);
731
659
  throw new Error('Can\'t serialize rpc value');
732
660
  }
733
661
  }
734
-
735
- writeJSString(str: string) {
662
+ writeJSString(str) {
736
663
  this.ctx.writeStringUtf8('"');
737
664
  for (let i = 0; i < str.length; i++) {
738
- const charcode = str.codePointAt(i)!;
665
+ const charcode = str.codePointAt(i);
739
666
  switch (charcode) {
740
667
  case 0:
741
- this.ctx.writeStringUtf8(String.raw`\0`);
668
+ this.ctx.writeStringUtf8(String.raw `\0`);
742
669
  break;
743
670
  case '\\'.codePointAt(0):
744
671
  this.ctx.writeStringUtf8('\\\\');
745
672
  break;
746
673
  case '\t'.codePointAt(0):
747
- this.ctx.writeStringUtf8(String.raw`\t`);
674
+ this.ctx.writeStringUtf8(String.raw `\t`);
748
675
  break;
749
676
  case '\b'.codePointAt(0):
750
- this.ctx.writeStringUtf8(String.raw`\b`);
677
+ this.ctx.writeStringUtf8(String.raw `\b`);
751
678
  break;
752
679
  case '\r'.codePointAt(0):
753
- this.ctx.writeStringUtf8(String.raw`\r`);
680
+ this.ctx.writeStringUtf8(String.raw `\r`);
754
681
  break;
755
682
  case '\n'.codePointAt(0):
756
- this.ctx.writeStringUtf8(String.raw`\n`);
683
+ this.ctx.writeStringUtf8(String.raw `\n`);
757
684
  break;
758
685
  case '"'.codePointAt(0):
759
- this.ctx.writeStringUtf8(String.raw`\"`);
686
+ this.ctx.writeStringUtf8(String.raw `\"`);
760
687
  break;
761
688
  default:
762
689
  this.ctx.writeCharCodeUtf8(charcode);
763
690
  }
764
691
  }
765
-
766
692
  this.ctx.writeStringUtf8('"');
767
693
  }
768
-
769
- writeBlob(buffer: ArrayBuffer) {
694
+ writeBlob(buffer) {
770
695
  this.ctx.writeStringUtf8('b"');
771
696
  const data = new Uint8Array(buffer);
772
697
  for (const b of data) {
@@ -775,226 +700,201 @@ class CponWriter {
775
700
  this.ctx.writeStringUtf8('\\\\');
776
701
  break;
777
702
  case '\t'.codePointAt(0):
778
- this.ctx.writeStringUtf8(String.raw`\t`);
703
+ this.ctx.writeStringUtf8(String.raw `\t`);
779
704
  break;
780
705
  case '\r'.codePointAt(0):
781
- this.ctx.writeStringUtf8(String.raw`\r`);
706
+ this.ctx.writeStringUtf8(String.raw `\r`);
782
707
  break;
783
708
  case '\n'.codePointAt(0):
784
- this.ctx.writeStringUtf8(String.raw`\n`);
709
+ this.ctx.writeStringUtf8(String.raw `\n`);
785
710
  break;
786
711
  case '"'.codePointAt(0):
787
- this.ctx.writeStringUtf8(String.raw`\"`);
712
+ this.ctx.writeStringUtf8(String.raw `\"`);
788
713
  break;
789
714
  default:
790
715
  if (b >= 32 && b < 127) {
791
716
  this.ctx.putByte(b);
792
- } else {
793
- this.ctx.putByte('\\'.codePointAt(0)!);
717
+ }
718
+ else {
719
+ this.ctx.putByte('\\'.codePointAt(0));
794
720
  this.ctx.putByte(hexify(b / 16));
795
721
  this.ctx.putByte(hexify(b % 16));
796
722
  }
797
723
  }
798
724
  }
799
-
800
725
  this.ctx.writeStringUtf8('"');
801
726
  }
802
-
803
- writeDateTime(dt: DateTime) {
727
+ writeDateTime(dt) {
804
728
  this.ctx.writeStringUtf8('d"');
805
- this.ctx.writeStringUtf8(stringifyDate(dt));
729
+ this.ctx.writeStringUtf8((0, exports.stringifyDate)(dt));
806
730
  this.ctx.writeStringUtf8('"');
807
731
  }
808
-
809
- writeBool(b: boolean) {
732
+ writeBool(b) {
810
733
  this.ctx.writeStringUtf8(b ? 'true' : 'false');
811
734
  }
812
-
813
- writeMeta(map: MetaMap) {
735
+ writeMeta(map) {
814
736
  this.writeMapContent(map, '<', '>');
815
737
  }
816
-
817
- writeIMap(map: IMap) {
738
+ writeIMap(map) {
818
739
  this.writeMapContent(map, 'i{', '}');
819
740
  }
820
-
821
- writeMap(map: ShvMap) {
741
+ writeMap(map) {
822
742
  this.writeMapContent(map, '{', '}');
823
743
  }
824
-
825
- writeMapContent(map: MetaMap | ShvMap | IMap, delimiterStart: string, delimiterEnd: string) {
744
+ writeMapContent(map, delimiterStart, delimiterEnd) {
826
745
  this.ctx.writeStringUtf8(delimiterStart);
827
746
  this.increaseIndentIfNotOneLiner(map);
828
747
  this.doIndentIfNotOneliner(map);
829
748
  let first = true;
830
- for (const [key, value] of Object.entries<RpcValue>(map)) {
749
+ for (const [key, value] of Object.entries(map)) {
831
750
  if (!first) {
832
- this.ctx.putByte(','.codePointAt(0)!);
751
+ this.ctx.putByte(','.codePointAt(0));
833
752
  this.doIndentIfNotOneliner(map);
834
753
  }
835
-
836
754
  first = false;
837
-
838
- if (map[shvMapType] === 'imap') {
755
+ if (map[rpcvalue_1.shvMapType] === 'imap') {
839
756
  const intKey = Number(key);
840
757
  if (Number.isNaN(intKey)) {
841
758
  throw new TypeError('Invalid NaN IMap key');
842
759
  }
843
-
844
760
  this.writeInt(intKey);
845
- } else if (map[shvMapType] === 'metamap') {
761
+ }
762
+ else if (map[rpcvalue_1.shvMapType] === 'metamap') {
846
763
  const intKey = Number(key);
847
764
  if (Number.isNaN(intKey)) {
848
- this.ctx.putByte('"'.codePointAt(0)!);
765
+ this.ctx.putByte('"'.codePointAt(0));
849
766
  this.ctx.writeStringUtf8(key.toString());
850
- this.ctx.putByte('"'.codePointAt(0)!);
851
- } else {
767
+ this.ctx.putByte('"'.codePointAt(0));
768
+ }
769
+ else {
852
770
  this.writeInt(intKey);
853
771
  }
854
- } else {
772
+ }
773
+ else {
855
774
  this.writeJSString(key.toString());
856
775
  }
857
-
858
776
  this.ctx.writeStringUtf8(':');
859
777
  this.write(value);
860
778
  }
861
-
862
779
  this.decreaseIndentIfNotOneLiner(map);
863
780
  this.doIndentIfNotOneliner(map);
864
781
  this.ctx.writeStringUtf8(delimiterEnd);
865
782
  }
866
-
867
- writeList(lst: RpcValue[]) {
868
- this.ctx.putByte('['.codePointAt(0)!);
783
+ writeList(lst) {
784
+ this.ctx.putByte('['.codePointAt(0));
869
785
  this.increaseIndentIfNotOneLiner(lst);
870
786
  this.doIndentIfNotOneliner(lst);
871
787
  for (const [i, element] of lst.entries()) {
872
788
  if (i > 0) {
873
- this.ctx.putByte(','.codePointAt(0)!);
789
+ this.ctx.putByte(','.codePointAt(0));
874
790
  this.doIndentIfNotOneliner(lst);
875
791
  }
876
-
877
792
  this.write(element);
878
793
  }
879
-
880
794
  this.decreaseIndentIfNotOneLiner(lst);
881
795
  this.doIndentIfNotOneliner(lst);
882
- this.ctx.putByte(']'.codePointAt(0)!);
796
+ this.ctx.putByte(']'.codePointAt(0));
883
797
  }
884
-
885
- writeUInt(num: UInt) {
798
+ writeUInt(num) {
886
799
  const s = Number(num).toString();
887
800
  this.ctx.writeStringUtf8(s);
888
- this.ctx.putByte('u'.codePointAt(0)!);
801
+ this.ctx.putByte('u'.codePointAt(0));
889
802
  }
890
-
891
- writeInt(num: Int) {
803
+ writeInt(num) {
892
804
  const s = Number(num).toString();
893
805
  this.ctx.writeStringUtf8(s);
894
806
  }
895
-
896
- writeDouble(num: Double) {
807
+ writeDouble(num) {
897
808
  let s = num.value.toString();
898
809
  if (!s.includes('.')) {
899
810
  s += '.';
900
811
  }
901
-
902
812
  this.ctx.writeStringUtf8(s);
903
813
  }
904
-
905
- writeDecimal(val: Decimal) {
906
- let {mantisa} = val;
907
- const {exponent} = val;
814
+ writeDecimal(val) {
815
+ let { mantisa } = val;
816
+ const { exponent } = val;
908
817
  if (mantisa < 0) {
909
818
  mantisa = -mantisa;
910
- this.ctx.putByte('-'.codePointAt(0)!);
819
+ this.ctx.putByte('-'.codePointAt(0));
911
820
  }
912
-
913
821
  let str = mantisa.toString();
914
822
  const n = str.length;
915
823
  const decPlaces = -exponent;
916
824
  if (decPlaces > 0 && decPlaces < n) {
917
825
  const dotIx = n - decPlaces;
918
826
  str = str.slice(0, dotIx) + '.' + str.slice(dotIx);
919
- } else if (decPlaces > 0 && decPlaces <= 3) {
827
+ }
828
+ else if (decPlaces > 0 && decPlaces <= 3) {
920
829
  const extraZeroCnt = decPlaces - n;
921
830
  let str0 = '0.';
922
831
  for (let i = 0; i < extraZeroCnt; ++i) {
923
832
  str0 += '0';
924
833
  }
925
-
926
834
  str = str0 + str;
927
- } else if (decPlaces < 0 && n + exponent <= 9) {
835
+ }
836
+ else if (decPlaces < 0 && n + exponent <= 9) {
928
837
  for (let i = 0; i < exponent; ++i) {
929
838
  str += '0';
930
839
  }
931
-
932
840
  str += '.';
933
- } else if (decPlaces === 0) {
841
+ }
842
+ else if (decPlaces === 0) {
934
843
  str += '.';
935
- } else {
844
+ }
845
+ else {
936
846
  str += 'e' + exponent;
937
847
  }
938
-
939
848
  for (let i = 0; i < str.length; ++i) {
940
- this.ctx.putByte(str.codePointAt(i)!);
849
+ this.ctx.putByte(str.codePointAt(i));
941
850
  }
942
851
  }
943
-
944
- private doIndentIfNotOneliner(map: MetaMap | ShvMap | IMap | List) {
852
+ doIndentIfNotOneliner(map) {
945
853
  if (this.indentString !== undefined && !this.isOneLiner(map)) {
946
- this.ctx.putByte('\n'.codePointAt(0)!);
854
+ this.ctx.putByte('\n'.codePointAt(0));
947
855
  this.ctx.writeStringUtf8(this.indentString.repeat(this.nestLevel));
948
856
  }
949
857
  }
950
-
951
- private isOneLiner(value: MetaMap | ShvMap | IMap | List) {
858
+ isOneLiner(value) {
952
859
  if (this.oneLiners === OneLiners.No) {
953
860
  return false;
954
861
  }
955
-
956
862
  const keyThreshold = Array.isArray(value) ? 10 : 5;
957
-
958
863
  if (Array.isArray(value)) {
959
- return value.length <= keyThreshold && !(value.some(x => Array.isArray(x) || isShvMap(x) || isIMap(x)));
864
+ return value.length <= keyThreshold && !(value.some(x => Array.isArray(x) || (0, rpcvalue_1.isShvMap)(x) || (0, rpcvalue_1.isIMap)(x)));
960
865
  }
961
-
962
- return Object.keys(value).length <= keyThreshold && !(Object.values(value).some(x => Array.isArray(x) || isShvMap(x) || isIMap(x)));
866
+ return Object.keys(value).length <= keyThreshold && !(Object.values(value).some(x => Array.isArray(x) || (0, rpcvalue_1.isShvMap)(x) || (0, rpcvalue_1.isIMap)(x)));
963
867
  }
964
-
965
- private increaseIndentIfNotOneLiner(value: MetaMap | ShvMap | IMap | List) {
868
+ increaseIndentIfNotOneLiner(value) {
966
869
  if (!this.isOneLiner(value)) {
967
870
  this.nestLevel++;
968
871
  }
969
872
  }
970
-
971
- private decreaseIndentIfNotOneLiner(value: MetaMap | ShvMap | IMap | List) {
873
+ decreaseIndentIfNotOneLiner(value) {
972
874
  if (!this.isOneLiner(value)) {
973
875
  this.nestLevel--;
974
876
  }
975
877
  }
976
878
  }
977
-
978
- export enum OneLiners {
979
- Yes,
980
- No,
981
- }
982
-
983
- const toCpon = (value: RpcValue, indentString?: string, oneLiners: OneLiners = OneLiners.Yes) => {
879
+ exports.CponWriter = CponWriter;
880
+ var OneLiners;
881
+ (function (OneLiners) {
882
+ OneLiners[OneLiners["Yes"] = 0] = "Yes";
883
+ OneLiners[OneLiners["No"] = 1] = "No";
884
+ })(OneLiners || (exports.OneLiners = OneLiners = {}));
885
+ const toCpon = (value, indentString, oneLiners = OneLiners.Yes) => {
984
886
  const wr = new CponWriter(indentString, oneLiners);
985
887
  wr.write(value);
986
888
  return new TextDecoder().decode(wr.ctx.buffer());
987
889
  };
988
-
989
- const fromCpon = (str: string | Uint8Array) => {
890
+ exports.toCpon = toCpon;
891
+ const fromCpon = (str) => {
990
892
  if (typeof str === 'string') {
991
893
  str = new TextEncoder().encode(str);
992
894
  }
993
-
994
- const rd = new CponReader(new UnpackContext(str.buffer));
895
+ const rd = new CponReader(new cpcontext_1.UnpackContext(str.buffer));
995
896
  return rd.read();
996
897
  };
997
-
898
+ exports.fromCpon = fromCpon;
998
899
  const CPON_PROTOCOL_TYPE = 2;
999
-
1000
- export {utf8ToString, CponWriter, CponReader, CPON_PROTOCOL_TYPE, toCpon, fromCpon};
900
+ exports.CPON_PROTOCOL_TYPE = CPON_PROTOCOL_TYPE;