libshv-js 3.2.4 → 3.3.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/package.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "name": "libshv-js",
3
- "version": "3.2.4",
3
+ "version": "3.3.0",
4
4
  "description": "Typescript implementation of libshv",
5
5
  "scripts": {
6
- "test": "echo \"Error: no test specified\" && exit 1"
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "lint": "xo --reporter unix src/* --extension=vue",
8
+ "fix": "xo --reporter unix src/* --extension=vue --fix"
7
9
  },
8
10
  "author": "Elektroline",
9
11
  "repository": {
package/src/chainpack.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import {utf8ToString} from './cpon';
2
- import {type RpcValue, type RpcValueType, type DateTime, Decimal, Double, type IMap, Int, type MetaMap, RpcValueWithMetaData, type ShvMap, UInt, withOffset, shvMapType} from './rpcvalue';
2
+ import {type RpcValue, type RpcValueType, type DateTime, Decimal, Double, type IMap, type Int, type MetaMap, RpcValueWithMetaData, type ShvMap, UInt, withOffset, shvMapType} from './rpcvalue';
3
3
  import {UnpackContext, PackContext} from './cpcontext';
4
4
 
5
5
  enum PackingSchema {
@@ -106,7 +106,7 @@ class ChainPackReader {
106
106
 
107
107
  if (codePointAt < PackingSchema.Null) {
108
108
  if (codePointAt >= PackingSchema.IntThreshold) {
109
- return impl_return(new Int(codePointAt - 64));
109
+ return impl_return(codePointAt - 64);
110
110
  }
111
111
  return impl_return(new UInt(codePointAt));
112
112
  }
@@ -121,7 +121,7 @@ class ChainPackReader {
121
121
  return impl_return(false);
122
122
  }
123
123
  case PackingSchema.Int: {
124
- return impl_return(new Int(this.readIntData()));
124
+ return impl_return(this.readIntData());
125
125
  }
126
126
  case PackingSchema.UInt: {
127
127
  return impl_return(new UInt(this.readUIntData()));
@@ -325,11 +325,11 @@ class ChainPackReader {
325
325
  const val = this.read();
326
326
  if (map[shvMapType] === 'metamap' && typeof key === 'string') {
327
327
  map[key] = val;
328
- } else if (map[shvMapType] === 'metamap' && key instanceof Int) {
328
+ } else if (map[shvMapType] === 'metamap' && typeof key === 'number') {
329
329
  map[Number(key)] = val;
330
330
  } else if (map[shvMapType] === 'map' && typeof key === 'string') {
331
331
  map[key] = val;
332
- } else if (map[shvMapType] === 'imap' && key instanceof Int) {
332
+ } else if (map[shvMapType] === 'imap' && typeof key === 'number') {
333
333
  map[Number(key)] = val;
334
334
  } else {
335
335
  throw new TypeError('Malformed map, invalid key');
@@ -367,7 +367,7 @@ class ChainPackWriter {
367
367
  case rpc_val instanceof UInt:
368
368
  this.writeUInt(rpc_val);
369
369
  break;
370
- case rpc_val instanceof Int:
370
+ case typeof rpc_val === 'number':
371
371
  this.writeInt(rpc_val);
372
372
  break;
373
373
  case rpc_val instanceof Decimal:
@@ -567,13 +567,13 @@ class ChainPackWriter {
567
567
  throw new TypeError('Invalid NaN IMap key');
568
568
  }
569
569
 
570
- this.writeInt(new Int(int_key));
570
+ this.writeInt(int_key);
571
571
  } else if (map[shvMapType] === 'metamap') {
572
572
  const int_key = Number(key);
573
573
  if (Number.isNaN(int_key)) {
574
574
  this.writeJSString(key.toString());
575
575
  } else {
576
- this.writeInt(new Int(int_key));
576
+ this.writeInt(int_key);
577
577
  }
578
578
  } else {
579
579
  this.writeJSString(key.toString());
package/src/cpon.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {type RpcValue, type RpcValueType, type DateTime, type List, Decimal, Double, type IMap, Int, type MetaMap, RpcValueWithMetaData, type ShvMap, UInt, withOffset, shvMapType, isShvMap, isIMap} from './rpcvalue';
1
+ 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';
2
2
  import {PackContext, UnpackContext} from './cpcontext';
3
3
 
4
4
  const hexify = (byte: number) => {
@@ -475,7 +475,7 @@ class CponReader {
475
475
  if (neg) {
476
476
  val = -val;
477
477
  }
478
- return new Int(val);
478
+ return val;
479
479
  }
480
480
 
481
481
  readNumber() {
@@ -547,7 +547,7 @@ class CponReader {
547
547
  if (is_uint) {
548
548
  return new UInt(mantisa);
549
549
  }
550
- return new Int(is_neg ? -mantisa : mantisa);
550
+ return is_neg ? -mantisa : mantisa;
551
551
  }
552
552
 
553
553
  private implReadMap(map_type: 'map', terminator: number): ShvMap;
@@ -575,11 +575,11 @@ class CponReader {
575
575
 
576
576
  if (map[shvMapType] === 'metamap' && typeof key === 'string') {
577
577
  map[key] = val;
578
- } else if (map[shvMapType] === 'metamap' && (key instanceof UInt || key instanceof Int)) {
578
+ } else if (map[shvMapType] === 'metamap' && (key instanceof UInt || typeof key === 'number')) {
579
579
  map[Number(key)] = val;
580
580
  } else if (map[shvMapType] === 'map' && typeof key === 'string') {
581
581
  map[key] = val;
582
- } else if (map[shvMapType] === 'imap' && (key instanceof UInt || key instanceof Int)) {
582
+ } else if (map[shvMapType] === 'imap' && (key instanceof UInt || typeof key === 'number')) {
583
583
  map[Number(key)] = val;
584
584
  } else {
585
585
  throw new TypeError('Malformed map, invalid key');
@@ -618,7 +618,7 @@ class CponWriter {
618
618
  case rpc_val instanceof UInt:
619
619
  this.writeUInt(rpc_val);
620
620
  break;
621
- case rpc_val instanceof Int:
621
+ case typeof rpc_val === 'number':
622
622
  this.writeInt(rpc_val);
623
623
  break;
624
624
  case rpc_val instanceof Double:
@@ -786,7 +786,7 @@ class CponWriter {
786
786
  throw new TypeError('Invalid NaN IMap key');
787
787
  }
788
788
 
789
- this.writeInt(new Int(int_key));
789
+ this.writeInt(int_key);
790
790
  } else if (map[shvMapType] === 'metamap') {
791
791
  const int_key = Number(key);
792
792
  if (Number.isNaN(int_key)) {
@@ -794,7 +794,7 @@ class CponWriter {
794
794
  this.ctx.writeStringUtf8(key.toString());
795
795
  this.ctx.putByte('"'.codePointAt(0)!);
796
796
  } else {
797
- this.writeInt(new Int(int_key));
797
+ this.writeInt(int_key);
798
798
  }
799
799
  } else {
800
800
  this.writeJSString(key.toString());
package/src/rpcmessage.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {type IMap, Int, isIMap, makeIMap, makeMetaMap, type MetaMap, type RpcValue, RpcValueWithMetaData} from './rpcvalue';
1
+ import {type IMap, type Int, isIMap, makeIMap, makeMetaMap, type MetaMap, type RpcValue, RpcValueWithMetaData} from './rpcvalue';
2
2
  import {toCpon} from './cpon';
3
3
  import {toChainPack} from './chainpack';
4
4
 
@@ -31,7 +31,7 @@ enum ErrorCode {
31
31
  }
32
32
 
33
33
  export type ErrorMap = {
34
- [ERROR_CODE]: Int<ErrorCode>;
34
+ [ERROR_CODE]: ErrorCode;
35
35
  [ERROR_MESSAGE]?: string;
36
36
  [ERROR_DATA]?: RpcValue;
37
37
  };
@@ -99,8 +99,8 @@ class RpcMessage {
99
99
  return (this.meta[TagRequestId] as Int);
100
100
  }
101
101
 
102
- setRequestId(id: number | Int) {
103
- this.meta[TagRequestId] = new Int(id);
102
+ setRequestId(id: number) {
103
+ this.meta[TagRequestId] = id;
104
104
  }
105
105
 
106
106
  callerIds(): RpcValue[] | undefined {
@@ -142,7 +142,7 @@ class RpcMessage {
142
142
  }
143
143
 
144
144
  const error_map = this.value[KeyError];
145
- if (!(error_map[ERROR_CODE] instanceof Int)) {
145
+ if (!(typeof error_map[ERROR_CODE] !== 'number')) {
146
146
  return new ProtocolError('Response had an error, but this error did not contain at least an error code');
147
147
  }
148
148
 
package/src/rpcvalue.ts CHANGED
@@ -1,24 +1,3 @@
1
- class Int<T = number> {
2
- readonly value: T;
3
-
4
- constructor(u: T | Int<T>) {
5
- if (u instanceof Int) {
6
- this.value = u.value;
7
- return;
8
- }
9
-
10
- if (!Number.isInteger(u)) {
11
- throw new TypeError('Value for Int must a positive integral number');
12
- }
13
-
14
- this.value = u;
15
- }
16
-
17
- [Symbol.toPrimitive](_hint: string) {
18
- return this.value;
19
- }
20
- }
21
-
22
1
  class UInt<T extends number = number> {
23
2
  readonly value: T;
24
3
 
@@ -91,6 +70,8 @@ type ShvMap<T extends Record<string, RpcValue> = Record<string, any>> = {
91
70
  [shvMapType]: 'map';
92
71
  };
93
72
 
73
+ export type Int = number;
74
+
94
75
  export type RpcValueType =
95
76
  Null |
96
77
  Bool |
@@ -136,4 +117,4 @@ class RpcValueWithMetaData {
136
117
 
137
118
  export type RpcValue = RpcValueType | RpcValueWithMetaData;
138
119
 
139
- export {shvMapType, Decimal, Double, type IMap, Int, type MetaMap, RpcValueWithMetaData, type ShvMap, UInt, withOffset, makeMap, makeIMap, makeMetaMap, isIMap, isShvMap};
120
+ export {shvMapType, Decimal, Double, type IMap, type MetaMap, RpcValueWithMetaData, type ShvMap, UInt, withOffset, makeMap, makeIMap, makeMetaMap, isIMap, isShvMap};
package/src/ws-client.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import {ChainPackReader, ChainpackProtocolType, ChainPackWriter} from './chainpack';
2
2
  import {type CponReader, CponProtocolType} from './cpon';
3
3
  import {ERROR_MESSAGE, ErrorCode, ERROR_CODE, RpcMessage, type RpcResponse, MethodCallTimeout} from './rpcmessage';
4
- import {type RpcValue, type Null, Int, type IMap, type ShvMap, makeMap, makeIMap} from './rpcvalue';
4
+ import {type RpcValue, type Null, type Int, type IMap, type ShvMap, makeMap, makeIMap} from './rpcvalue';
5
5
 
6
6
  const DEFAULT_TIMEOUT = 5000;
7
7
 
@@ -63,7 +63,7 @@ export const DIR_SIGNALS = 6;
63
63
  export const DIR_EXTRA = 63;
64
64
  type DirResult = Array<IMap<{
65
65
  [DIR_NAME]: string;
66
- [DIR_FLAGS]: Int<DirFlags>;
66
+ [DIR_FLAGS]: DirFlags;
67
67
  [DIR_PARAM]: string | Null;
68
68
  [DIR_RESULT]: string | Null;
69
69
  [DIR_ACCESS]: Int;
@@ -197,7 +197,7 @@ class WsClient {
197
197
  const promise = new Promise<RpcResponse>(resolve => {
198
198
  this.rpcHandlers[rq_id] = {resolve, timeout_handle: self.setTimeout(() => {
199
199
  resolve(new MethodCallTimeout(makeIMap({
200
- [ERROR_CODE]: new Int(ErrorCode.MethodCallTimeout),
200
+ [ERROR_CODE]: ErrorCode.MethodCallTimeout,
201
201
  [ERROR_MESSAGE]: `Shv call timeout after: ${this.timeout} msec.`,
202
202
  })));
203
203
  }, this.timeout)};
package/src/zod.ts CHANGED
@@ -1,11 +1,10 @@
1
1
  import {z, type ZodType} from 'zod';
2
- import {Int, shvMapType, UInt} from './rpcvalue';
2
+ import {shvMapType, UInt} from './rpcvalue';
3
3
 
4
4
  export const map = <T extends Record<string, ZodType<any, any, any>>>(schema: T) => z.object(schema).and(z.object({[shvMapType]: z.literal('map')}));
5
5
  export const recmap = <T extends ZodType<any, any, any>>(schema: T) => z.record(z.string(), schema).and(z.object({[shvMapType]: z.literal('map')}));
6
6
  export const imap = <T extends Record<string, ZodType<any, any, any>>>(schema: T) => z.object(schema).and(z.object({[shvMapType]: z.literal('imap')}));
7
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments -- Zod needs the default argument, otherwise it'll infer as Int<unknown>
8
- export const int = () => z.instanceof(Int<number>);
7
+ export const int = () => z.number();
9
8
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments -- Zod needs the default argument, otherwise it'll infer as UInt<unknown>
10
9
  export const uint = () => z.instanceof(UInt<number>);
11
10