libshv-js 3.9.0 → 3.9.2
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 +94 -0
- package/dist/chainpack.d.ts +39 -0
- package/dist/chainpack.js +555 -0
- package/dist/cpcontext.d.ts +18 -0
- package/dist/cpcontext.js +74 -0
- package/dist/cpon.d.ts +55 -0
- package/dist/cpon.js +891 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/json.d.ts +3 -0
- package/dist/json.js +113 -0
- package/dist/rpcmessage.d.ts +73 -0
- package/dist/rpcmessage.js +37 -0
- package/dist/rpcvalue.d.ts +56 -0
- package/dist/rpcvalue.js +85 -0
- package/dist/shv-ri.d.ts +12 -0
- package/dist/shv-ri.js +51 -0
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +6 -0
- package/dist/ws-client.d.ts +239 -0
- package/dist/ws-client.js +560 -0
- package/package.json +1 -1
- package/src/rpcmessage.ts +2 -0
- package/src/ws-client.ts +4 -3
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/json.d.ts
ADDED
package/dist/json.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { UnpackContext } from './cpcontext';
|
|
2
|
+
import { CponReader, stringifyDate } from './cpon';
|
|
3
|
+
import { Decimal, Double, isIMap, isShvMap, makeIMap, makeMap, makeMetaMap, RpcValueWithMetaData, UInt } from './rpcvalue';
|
|
4
|
+
const decodeMap = (x) => makeMap(Object.fromEntries(Object.entries(x).map(([key, value]) => [key, decodeValueJson(value)])));
|
|
5
|
+
const decodeValueJson = (x) => {
|
|
6
|
+
switch (true) {
|
|
7
|
+
case Array.isArray(x): {
|
|
8
|
+
const list = x;
|
|
9
|
+
const handleType = (typeName, typeValue) => {
|
|
10
|
+
switch (true) {
|
|
11
|
+
case typeName === 'Blob': {
|
|
12
|
+
if (typeof typeValue !== 'string' || typeValue.length % 2 !== 0) {
|
|
13
|
+
throw new TypeError(`Invalid JSON-encoded Blob: '${JSON.stringify(typeValue)}'`);
|
|
14
|
+
}
|
|
15
|
+
const buffer = new ArrayBuffer(typeValue.length / 2);
|
|
16
|
+
const array = new Uint8Array(buffer);
|
|
17
|
+
// I can assume even length because of the previous check.
|
|
18
|
+
for (let i = 0; i < typeValue.length / 2; i++) {
|
|
19
|
+
array[i] = Number.parseInt(typeValue.slice(i * 2, (i * 2) + 2), 16);
|
|
20
|
+
}
|
|
21
|
+
return buffer;
|
|
22
|
+
}
|
|
23
|
+
case typeName === 'IMap':
|
|
24
|
+
if (typeof typeValue !== 'object' || typeValue === null) {
|
|
25
|
+
throw new TypeError(`Invalid JSON-encoded IMap: '${JSON.stringify(typeValue)}'`);
|
|
26
|
+
}
|
|
27
|
+
if (Object.keys(typeValue).some(key => !/^-?\d+$/.test(key))) {
|
|
28
|
+
throw new TypeError(`Invalid JSON-encoded IMap (non-Int key): '${JSON.stringify(typeValue)}'`);
|
|
29
|
+
}
|
|
30
|
+
return makeIMap(decodeMap(typeValue));
|
|
31
|
+
case typeName === 'DateTime':
|
|
32
|
+
if (typeof typeValue !== 'string') {
|
|
33
|
+
throw new TypeError(`Invalid JSON-encoded DateTime: '${JSON.stringify(typeValue)}'`);
|
|
34
|
+
}
|
|
35
|
+
// Kinda lame that I'm encoding the datetime string, but this is the easiest way right now.
|
|
36
|
+
return new CponReader(new UnpackContext(new TextEncoder().encode(typeValue).buffer)).readDateTimeInner();
|
|
37
|
+
default:
|
|
38
|
+
throw new TypeError(`Invalid JSON-encoded type name: '${JSON.stringify(typeName)}'`);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
switch (true) {
|
|
42
|
+
case list.length === 3 && list[0] === JSON_TAG_TYPE:
|
|
43
|
+
return handleType(list[1], list[2]);
|
|
44
|
+
case list.length === 3 && list[0] === JSON_TAG_META:
|
|
45
|
+
case list.length === 5 && list[0] === JSON_TAG_META && list[2] === JSON_TAG_TYPE: {
|
|
46
|
+
if (typeof list[1] !== 'object' || list[1] === null) {
|
|
47
|
+
throw new TypeError(`Invalid JSON-encoded MetaData: '${JSON.stringify(list[1])}'`);
|
|
48
|
+
}
|
|
49
|
+
const value = (() => {
|
|
50
|
+
if (list.length === 3) {
|
|
51
|
+
const decoded = decodeValueJson(list[2]);
|
|
52
|
+
if (decoded instanceof RpcValueWithMetaData) {
|
|
53
|
+
throw new TypeError('Logic error: got RpcValueWithMetaData as the value in JSON-encoded RpcValueWithMetaData, please report this bug to the maintainer.');
|
|
54
|
+
}
|
|
55
|
+
return decoded;
|
|
56
|
+
}
|
|
57
|
+
return handleType(list[3], list[4]);
|
|
58
|
+
})();
|
|
59
|
+
return new RpcValueWithMetaData(makeMetaMap(decodeMap(list[1])), value);
|
|
60
|
+
}
|
|
61
|
+
default:
|
|
62
|
+
return list.map(value => decodeValueJson(value));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
case x === null:
|
|
66
|
+
return undefined;
|
|
67
|
+
case typeof x === 'object':
|
|
68
|
+
return decodeMap(x);
|
|
69
|
+
case typeof x === 'number':
|
|
70
|
+
case typeof x === 'boolean':
|
|
71
|
+
case typeof x === 'string':
|
|
72
|
+
return x;
|
|
73
|
+
default:
|
|
74
|
+
throw new Error(`Unknown JSON-encoded value: ${JSON.stringify(x)}`);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
export const fromJson = (str) => decodeValueJson(JSON.parse(str));
|
|
78
|
+
const JSON_TAG_META = '!shvMeta';
|
|
79
|
+
const JSON_TAG_TYPE = '!shvType';
|
|
80
|
+
const encodeValueJson = (x) => {
|
|
81
|
+
switch (true) {
|
|
82
|
+
case x instanceof RpcValueWithMetaData: {
|
|
83
|
+
const res = [JSON_TAG_META, encodeValueJson(makeMap(x.meta))];
|
|
84
|
+
const encoded = encodeValueJson(x.value);
|
|
85
|
+
if (Array.isArray(encoded) && encoded[0] === JSON_TAG_TYPE) {
|
|
86
|
+
return [...res, ...encoded];
|
|
87
|
+
}
|
|
88
|
+
return [...res, encoded];
|
|
89
|
+
}
|
|
90
|
+
case Array.isArray(x):
|
|
91
|
+
return x.map(value => encodeValueJson(value));
|
|
92
|
+
case isShvMap(x): {
|
|
93
|
+
const map = x;
|
|
94
|
+
return Object.fromEntries(Object.entries(map).map(([key, value]) => [key, encodeValueJson(value)]));
|
|
95
|
+
}
|
|
96
|
+
case isIMap(x):
|
|
97
|
+
return [JSON_TAG_TYPE, 'IMap', encodeValueJson(makeMap(x))];
|
|
98
|
+
case x instanceof ArrayBuffer:
|
|
99
|
+
return [JSON_TAG_TYPE, 'Blob', new Uint8Array(x).reduce((acc, v) => acc + v.toString(16).padStart(2, '0'), '')];
|
|
100
|
+
case x instanceof Date:
|
|
101
|
+
return [JSON_TAG_TYPE, 'DateTime', stringifyDate(x)];
|
|
102
|
+
case x === undefined:
|
|
103
|
+
// eslint-disable-next-line unicorn/no-null
|
|
104
|
+
return null;
|
|
105
|
+
case x instanceof Decimal:
|
|
106
|
+
case x instanceof UInt:
|
|
107
|
+
case x instanceof Double:
|
|
108
|
+
throw new TypeError('Decimal/Double/UInt not supported in JSON');
|
|
109
|
+
default:
|
|
110
|
+
return x;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
export const toJson = (value) => JSON.stringify(encodeValueJson(value));
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Double, IMap, MetaMap, RpcValue, type RpcValueWithMetaData } from './rpcvalue';
|
|
2
|
+
export declare const RPC_MESSAGE_REQUEST_ID = 8;
|
|
3
|
+
export declare const RPC_MESSAGE_SHV_PATH = 9;
|
|
4
|
+
export declare const RPC_MESSAGE_METHOD = 10;
|
|
5
|
+
export declare const RPC_MESSAGE_CALLER_IDS = 11;
|
|
6
|
+
export declare const RPC_MESSAGE_USER_ID = 16;
|
|
7
|
+
export declare const RPC_MESSAGE_ACCESS_LEVEL = 17;
|
|
8
|
+
export declare const RPC_MESSAGE_PARAMS = 1;
|
|
9
|
+
export declare const RPC_MESSAGE_RESULT = 2;
|
|
10
|
+
export declare const RPC_MESSAGE_ERROR = 3;
|
|
11
|
+
export declare const RPC_MESSAGE_DELAY = 4;
|
|
12
|
+
export declare const RPC_MESSAGE_ABORT = 5;
|
|
13
|
+
export declare const ERROR_CODE = 1;
|
|
14
|
+
export declare const ERROR_MESSAGE = 2;
|
|
15
|
+
export declare const ERROR_DATA = 3;
|
|
16
|
+
export declare enum ErrorCode {
|
|
17
|
+
InvalidRequest = 1,
|
|
18
|
+
MethodNotFound = 2,
|
|
19
|
+
InvalidParams = 3,
|
|
20
|
+
InternalError = 4,
|
|
21
|
+
ParseError = 5,
|
|
22
|
+
MethodCallTimeout = 6,
|
|
23
|
+
MethodCallCancelled = 7,
|
|
24
|
+
MethodCallException = 8,
|
|
25
|
+
Unknown = 9,
|
|
26
|
+
LoginRequired = 10,
|
|
27
|
+
UserIDRequired = 11,
|
|
28
|
+
NotImplemented = 12
|
|
29
|
+
}
|
|
30
|
+
export type ErrorMap = IMap<{
|
|
31
|
+
[ERROR_CODE]: number;
|
|
32
|
+
[ERROR_MESSAGE]?: string | undefined;
|
|
33
|
+
[ERROR_DATA]?: RpcValue;
|
|
34
|
+
}>;
|
|
35
|
+
type RpcRequestMeta = MetaMap<{
|
|
36
|
+
[RPC_MESSAGE_REQUEST_ID]: number;
|
|
37
|
+
[RPC_MESSAGE_SHV_PATH]: string;
|
|
38
|
+
[RPC_MESSAGE_METHOD]: string;
|
|
39
|
+
[RPC_MESSAGE_CALLER_IDS]?: number | number[] | undefined;
|
|
40
|
+
[RPC_MESSAGE_USER_ID]?: string | undefined;
|
|
41
|
+
[RPC_MESSAGE_ACCESS_LEVEL]?: number | undefined;
|
|
42
|
+
}>;
|
|
43
|
+
type RpcRequestValue = IMap<{
|
|
44
|
+
[RPC_MESSAGE_PARAMS]?: RpcValue;
|
|
45
|
+
}> | IMap<{
|
|
46
|
+
[RPC_MESSAGE_ABORT]: boolean;
|
|
47
|
+
}>;
|
|
48
|
+
export type RpcRequest = RpcValueWithMetaData<RpcRequestMeta, RpcRequestValue>;
|
|
49
|
+
type RpcResponseMeta = MetaMap<{
|
|
50
|
+
[RPC_MESSAGE_REQUEST_ID]: number;
|
|
51
|
+
[RPC_MESSAGE_CALLER_IDS]?: number | number[] | undefined;
|
|
52
|
+
}>;
|
|
53
|
+
export type RpcResponseValue = IMap<{
|
|
54
|
+
[RPC_MESSAGE_RESULT]: RpcValue;
|
|
55
|
+
}> | IMap<{
|
|
56
|
+
[RPC_MESSAGE_ERROR]: ErrorMap;
|
|
57
|
+
}> | IMap<{
|
|
58
|
+
[RPC_MESSAGE_DELAY]: Double;
|
|
59
|
+
}>;
|
|
60
|
+
export type RpcResponse = RpcValueWithMetaData<RpcResponseMeta, RpcResponseValue>;
|
|
61
|
+
type RpcSignalMeta = MetaMap<{
|
|
62
|
+
[RPC_MESSAGE_SHV_PATH]: string;
|
|
63
|
+
[RPC_MESSAGE_METHOD]: string;
|
|
64
|
+
}>;
|
|
65
|
+
type RpcSignalValue = IMap<{
|
|
66
|
+
[RPC_MESSAGE_PARAMS]?: RpcValue;
|
|
67
|
+
}>;
|
|
68
|
+
export type RpcSignal = RpcValueWithMetaData<RpcSignalMeta, RpcSignalValue>;
|
|
69
|
+
export type RpcMessage = RpcRequest | RpcResponse | RpcSignal;
|
|
70
|
+
export declare const isSignal: (message: unknown) => message is RpcSignal;
|
|
71
|
+
export declare const isRequest: (message: unknown) => message is RpcRequest;
|
|
72
|
+
export declare const isResponse: (message: unknown) => message is RpcResponse;
|
|
73
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const RPC_MESSAGE_REQUEST_ID = 8;
|
|
2
|
+
export const RPC_MESSAGE_SHV_PATH = 9;
|
|
3
|
+
export const RPC_MESSAGE_METHOD = 10;
|
|
4
|
+
export const RPC_MESSAGE_CALLER_IDS = 11;
|
|
5
|
+
export const RPC_MESSAGE_USER_ID = 16;
|
|
6
|
+
export const RPC_MESSAGE_ACCESS_LEVEL = 17;
|
|
7
|
+
export const RPC_MESSAGE_PARAMS = 1;
|
|
8
|
+
export const RPC_MESSAGE_RESULT = 2;
|
|
9
|
+
export const RPC_MESSAGE_ERROR = 3;
|
|
10
|
+
export const RPC_MESSAGE_DELAY = 4;
|
|
11
|
+
export const RPC_MESSAGE_ABORT = 5;
|
|
12
|
+
export const ERROR_CODE = 1;
|
|
13
|
+
export const ERROR_MESSAGE = 2;
|
|
14
|
+
export const ERROR_DATA = 3;
|
|
15
|
+
export var ErrorCode;
|
|
16
|
+
(function (ErrorCode) {
|
|
17
|
+
ErrorCode[ErrorCode["InvalidRequest"] = 1] = "InvalidRequest";
|
|
18
|
+
ErrorCode[ErrorCode["MethodNotFound"] = 2] = "MethodNotFound";
|
|
19
|
+
ErrorCode[ErrorCode["InvalidParams"] = 3] = "InvalidParams";
|
|
20
|
+
ErrorCode[ErrorCode["InternalError"] = 4] = "InternalError";
|
|
21
|
+
ErrorCode[ErrorCode["ParseError"] = 5] = "ParseError";
|
|
22
|
+
ErrorCode[ErrorCode["MethodCallTimeout"] = 6] = "MethodCallTimeout";
|
|
23
|
+
ErrorCode[ErrorCode["MethodCallCancelled"] = 7] = "MethodCallCancelled";
|
|
24
|
+
ErrorCode[ErrorCode["MethodCallException"] = 8] = "MethodCallException";
|
|
25
|
+
ErrorCode[ErrorCode["Unknown"] = 9] = "Unknown";
|
|
26
|
+
ErrorCode[ErrorCode["LoginRequired"] = 10] = "LoginRequired";
|
|
27
|
+
ErrorCode[ErrorCode["UserIDRequired"] = 11] = "UserIDRequired";
|
|
28
|
+
ErrorCode[ErrorCode["NotImplemented"] = 12] = "NotImplemented";
|
|
29
|
+
})(ErrorCode || (ErrorCode = {}));
|
|
30
|
+
const implRpcMessage = (message) => typeof message === 'object'
|
|
31
|
+
&& message !== null
|
|
32
|
+
&& 'meta' in message
|
|
33
|
+
&& typeof message.meta === 'object'
|
|
34
|
+
&& message.meta !== null;
|
|
35
|
+
export const isSignal = (message) => implRpcMessage(message) && !(RPC_MESSAGE_REQUEST_ID in message.meta) && RPC_MESSAGE_METHOD in message.meta;
|
|
36
|
+
export const isRequest = (message) => implRpcMessage(message) && RPC_MESSAGE_REQUEST_ID in message.meta && RPC_MESSAGE_METHOD in message.meta;
|
|
37
|
+
export const isResponse = (message) => implRpcMessage(message) && RPC_MESSAGE_REQUEST_ID in message.meta && !(RPC_MESSAGE_METHOD in message.meta);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
declare class UInt<T extends number = number> {
|
|
2
|
+
readonly value: T;
|
|
3
|
+
constructor(u: T | UInt<T>);
|
|
4
|
+
[Symbol.toPrimitive](): T;
|
|
5
|
+
toString(): string;
|
|
6
|
+
}
|
|
7
|
+
declare class Double {
|
|
8
|
+
readonly value: number;
|
|
9
|
+
constructor(u: number);
|
|
10
|
+
}
|
|
11
|
+
declare class Decimal {
|
|
12
|
+
mantisa: number;
|
|
13
|
+
exponent: number;
|
|
14
|
+
constructor(mantisa: number, exponent: number);
|
|
15
|
+
}
|
|
16
|
+
export type Null = undefined;
|
|
17
|
+
export type Bool = boolean;
|
|
18
|
+
export type Blob = ArrayBuffer;
|
|
19
|
+
export type ShvString = string;
|
|
20
|
+
export type DateTime = Date & {
|
|
21
|
+
utc_offset?: number;
|
|
22
|
+
};
|
|
23
|
+
declare const withOffset: (date: Date, utcOffset?: number) => DateTime;
|
|
24
|
+
export type List = RpcValue[];
|
|
25
|
+
declare const shvMapType: unique symbol;
|
|
26
|
+
type IMap<T extends Record<number, RpcValue> = Record<number, any>> = {
|
|
27
|
+
[Key in keyof T]: T[Key];
|
|
28
|
+
} & {
|
|
29
|
+
[shvMapType]: 'imap';
|
|
30
|
+
};
|
|
31
|
+
type ShvMap<T extends Record<string, RpcValue> = Record<string, any>> = {
|
|
32
|
+
[Key in keyof T]: T[Key];
|
|
33
|
+
} & {
|
|
34
|
+
[shvMapType]: 'map';
|
|
35
|
+
};
|
|
36
|
+
export type Int = number;
|
|
37
|
+
export type RpcValueType = Null | Bool | Int | UInt | Double | Decimal | Blob | ShvString | DateTime | List | ShvMap | IMap;
|
|
38
|
+
type MetaMap<T extends Record<string | number, RpcValue> = Record<string | number, RpcValue>> = {
|
|
39
|
+
[Key in keyof T]: T[Key];
|
|
40
|
+
} & {
|
|
41
|
+
[shvMapType]: 'metamap';
|
|
42
|
+
};
|
|
43
|
+
declare const isShvMap: (x: unknown) => x is ShvMap;
|
|
44
|
+
declare const isIMap: (x: unknown) => x is IMap;
|
|
45
|
+
declare const isMetaMap: (x: unknown) => x is IMap;
|
|
46
|
+
export declare const typeName: (x: unknown) => string;
|
|
47
|
+
declare const makeMetaMap: <T extends Record<string | number, RpcValue> = Record<string | number, RpcValue>, U extends Record<number, RpcValue> = Omit<T, typeof shvMapType>>(x?: U) => MetaMap<U>;
|
|
48
|
+
declare const makeIMap: <T extends Record<number, RpcValue> = Record<number, RpcValue>, U extends Record<number, RpcValue> = Omit<T, typeof shvMapType>>(x?: U) => IMap<U>;
|
|
49
|
+
declare const makeMap: <T extends Record<string, RpcValue> = Record<string, RpcValue>, U extends Record<string, RpcValue> = Omit<T, typeof shvMapType>>(x?: U) => ShvMap<U>;
|
|
50
|
+
declare class RpcValueWithMetaData<MetaSchema extends MetaMap = MetaMap, ValueSchema extends RpcValueType = RpcValueType> {
|
|
51
|
+
meta: MetaSchema;
|
|
52
|
+
value: ValueSchema;
|
|
53
|
+
constructor(meta: MetaSchema, value: ValueSchema);
|
|
54
|
+
}
|
|
55
|
+
export type RpcValue = RpcValueType | RpcValueWithMetaData;
|
|
56
|
+
export { shvMapType, Decimal, Double, type IMap, type MetaMap, RpcValueWithMetaData, type ShvMap, UInt, withOffset, makeMap, makeIMap, makeMetaMap, isIMap, isMetaMap, isShvMap };
|
package/dist/rpcvalue.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
class UInt {
|
|
2
|
+
value;
|
|
3
|
+
constructor(u) {
|
|
4
|
+
if (u instanceof UInt) {
|
|
5
|
+
this.value = u.value;
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
if (u < 0 || !Number.isInteger(u)) {
|
|
9
|
+
throw new Error(`Invalid value '${u}' for UInt must a positive integral number`);
|
|
10
|
+
}
|
|
11
|
+
this.value = u;
|
|
12
|
+
}
|
|
13
|
+
[Symbol.toPrimitive]() {
|
|
14
|
+
return this.value;
|
|
15
|
+
}
|
|
16
|
+
toString() {
|
|
17
|
+
return `${this.value.toString()}`;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
class Double {
|
|
21
|
+
value;
|
|
22
|
+
constructor(u) {
|
|
23
|
+
this.value = u;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
class Decimal {
|
|
27
|
+
mantisa;
|
|
28
|
+
exponent;
|
|
29
|
+
constructor(mantisa, exponent) {
|
|
30
|
+
if (!Number.isInteger(exponent)) {
|
|
31
|
+
throw new TypeError(`Decimal: exponent must be integral (${exponent})`);
|
|
32
|
+
}
|
|
33
|
+
this.mantisa = mantisa;
|
|
34
|
+
this.exponent = exponent;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const withOffset = (date, utcOffset) => {
|
|
38
|
+
const clonedDate = new Date(date);
|
|
39
|
+
clonedDate.utc_offset = utcOffset;
|
|
40
|
+
return clonedDate;
|
|
41
|
+
};
|
|
42
|
+
const shvMapType = Symbol('shvMapType');
|
|
43
|
+
const isShvMap = (x) => typeof x === 'object' && x[shvMapType] === 'map';
|
|
44
|
+
const isIMap = (x) => typeof x === 'object' && x[shvMapType] === 'imap';
|
|
45
|
+
const isMetaMap = (x) => typeof x === 'object' && x[shvMapType] === 'metamap';
|
|
46
|
+
export const typeName = (x) => {
|
|
47
|
+
switch (true) {
|
|
48
|
+
case x === undefined: return 'Null';
|
|
49
|
+
case typeof x === 'boolean': return 'Bool';
|
|
50
|
+
case typeof x === 'string': return 'String';
|
|
51
|
+
case x instanceof ArrayBuffer: return 'Blob';
|
|
52
|
+
case x instanceof UInt: return 'UInt';
|
|
53
|
+
case typeof x === 'number': return 'Int';
|
|
54
|
+
case x instanceof Double: return 'Double';
|
|
55
|
+
case x instanceof Decimal: return 'Decimal';
|
|
56
|
+
case Array.isArray(x): return 'List';
|
|
57
|
+
case x instanceof Date: return 'Date';
|
|
58
|
+
case isShvMap(x): return 'ShvMap';
|
|
59
|
+
case isIMap(x): return 'IMap';
|
|
60
|
+
case isMetaMap(x): return 'MetaMap';
|
|
61
|
+
default: return '<unknown type>';
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const makeMetaMap = (x = {}) => ({
|
|
65
|
+
...x,
|
|
66
|
+
[shvMapType]: 'metamap',
|
|
67
|
+
});
|
|
68
|
+
const makeIMap = (x = {}) => ({
|
|
69
|
+
...x,
|
|
70
|
+
[shvMapType]: 'imap',
|
|
71
|
+
});
|
|
72
|
+
const makeMap = (x = {}) => ({
|
|
73
|
+
...x,
|
|
74
|
+
[shvMapType]: 'map',
|
|
75
|
+
});
|
|
76
|
+
class RpcValueWithMetaData {
|
|
77
|
+
meta;
|
|
78
|
+
value;
|
|
79
|
+
constructor(meta, value) {
|
|
80
|
+
this.meta = meta;
|
|
81
|
+
this.value = value;
|
|
82
|
+
/* nothing */
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export { shvMapType, Decimal, Double, RpcValueWithMetaData, UInt, withOffset, makeMap, makeIMap, makeMetaMap, isIMap, isMetaMap, isShvMap };
|
package/dist/shv-ri.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class ShvRI {
|
|
2
|
+
private ri;
|
|
3
|
+
private methodSepIx;
|
|
4
|
+
private signalSepIx?;
|
|
5
|
+
constructor(ri: string);
|
|
6
|
+
path(): string;
|
|
7
|
+
method(): string;
|
|
8
|
+
signal(): string | undefined;
|
|
9
|
+
hasSignal(): boolean;
|
|
10
|
+
asString(): string;
|
|
11
|
+
static fromPathMethodSignal(path: string, method: string, signal?: string): ShvRI;
|
|
12
|
+
}
|
package/dist/shv-ri.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export class ShvRI {
|
|
2
|
+
ri;
|
|
3
|
+
methodSepIx;
|
|
4
|
+
signalSepIx;
|
|
5
|
+
constructor(ri) {
|
|
6
|
+
const methodSepIx = ri.indexOf(':');
|
|
7
|
+
if (methodSepIx === -1) {
|
|
8
|
+
throw new Error('Method separator ":" is missing.');
|
|
9
|
+
}
|
|
10
|
+
const signalSepRelative = ri.indexOf(':', methodSepIx + 1);
|
|
11
|
+
const signalSepIx = signalSepRelative !== -1 ? signalSepRelative : undefined;
|
|
12
|
+
this.ri = ri;
|
|
13
|
+
this.methodSepIx = methodSepIx;
|
|
14
|
+
this.signalSepIx = signalSepIx;
|
|
15
|
+
const method = this.method();
|
|
16
|
+
if (method.length === 0) {
|
|
17
|
+
throw new Error('Method must not be empty.');
|
|
18
|
+
}
|
|
19
|
+
const signal = this.signal();
|
|
20
|
+
if (signal?.length === 0) {
|
|
21
|
+
throw new Error('Signal, if present, must not be empty.');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
path() {
|
|
25
|
+
return this.ri.slice(0, this.methodSepIx);
|
|
26
|
+
}
|
|
27
|
+
method() {
|
|
28
|
+
if (this.signalSepIx !== undefined) {
|
|
29
|
+
return this.ri.slice(this.methodSepIx + 1, this.signalSepIx);
|
|
30
|
+
}
|
|
31
|
+
return this.ri.slice(this.methodSepIx + 1);
|
|
32
|
+
}
|
|
33
|
+
signal() {
|
|
34
|
+
if (this.signalSepIx !== undefined) {
|
|
35
|
+
return this.ri.slice(this.signalSepIx + 1);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
hasSignal() {
|
|
39
|
+
return this.signalSepIx !== undefined;
|
|
40
|
+
}
|
|
41
|
+
asString() {
|
|
42
|
+
return this.ri;
|
|
43
|
+
}
|
|
44
|
+
static fromPathMethodSignal(path, method, signal) {
|
|
45
|
+
const m = method.length === 0 ? '*' : method;
|
|
46
|
+
const ri = signal !== undefined
|
|
47
|
+
? `${path}:${m}:${signal}`
|
|
48
|
+
: `${path}:${m}`;
|
|
49
|
+
return new ShvRI(ri);
|
|
50
|
+
}
|
|
51
|
+
}
|
package/dist/utils.d.ts
ADDED