libshv-js 8.0.0 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -51,7 +51,7 @@ type RpcResponseMeta = MetaMap<{
51
51
  [RPC_MESSAGE_CALLER_IDS]?: number | number[] | undefined;
52
52
  }>;
53
53
  export type RpcResponseValue = IMap<{
54
- [RPC_MESSAGE_RESULT]: RpcValue;
54
+ [RPC_MESSAGE_RESULT]?: RpcValue;
55
55
  }> | IMap<{
56
56
  [RPC_MESSAGE_ERROR]: ErrorMap;
57
57
  }> | IMap<{
@@ -211,10 +211,11 @@ export type CallRpcMethodOptions = {
211
211
  beforeSendHook?: (rpcMessage: RpcRequest) => RpcRequest;
212
212
  explicitNullParam?: boolean;
213
213
  };
214
- declare enum ShvApiVersion {
214
+ export declare enum ShvApiVersion {
215
215
  V2 = 0,
216
216
  V3 = 1
217
217
  }
218
+ export declare const pathMatchesSubscription: (subscriptionPath: string, signalPath: string, apiVersion: ShvApiVersion) => boolean;
218
219
  declare class WsClient {
219
220
  private requestId;
220
221
  private pingTimerId;
package/dist/ws-client.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { minimatch } from 'minimatch';
1
2
  import { accessLevelFromAccessString } from './access.js';
2
3
  import { ChainPackReader, CHAINPACK_PROTOCOL_TYPE, ChainPackWriter, toChainPack } from './chainpack.js';
3
4
  import { CPON_PROTOCOL_TYPE, toCpon } from './cpon.js';
@@ -123,11 +124,19 @@ class RpcRequestPromise extends Promise {
123
124
  this.progressQuery = progressQuery;
124
125
  }
125
126
  }
126
- var ShvApiVersion;
127
+ export var ShvApiVersion;
127
128
  (function (ShvApiVersion) {
128
129
  ShvApiVersion[ShvApiVersion["V2"] = 0] = "V2";
129
130
  ShvApiVersion[ShvApiVersion["V3"] = 1] = "V3";
130
131
  })(ShvApiVersion || (ShvApiVersion = {}));
132
+ export const pathMatchesSubscription = (subscriptionPath, signalPath, apiVersion) => {
133
+ if (apiVersion === ShvApiVersion.V2) {
134
+ const remainder = signalPath.slice(subscriptionPath.length);
135
+ return signalPath.startsWith(subscriptionPath) && (remainder.length === 0 || remainder.startsWith('/'));
136
+ }
137
+ const recursivePathPrefix = subscriptionPath.endsWith('/**') ? subscriptionPath.slice(0, -3) : undefined;
138
+ return (recursivePathPrefix !== undefined && minimatch(signalPath, recursivePathPrefix)) || minimatch(signalPath, subscriptionPath);
139
+ };
131
140
  class WsClient {
132
141
  requestId = 1;
133
142
  pingTimerId = undefined;
@@ -147,11 +156,12 @@ class WsClient {
147
156
  this.timeout = 'timeout' in options && options.timeout !== undefined ? options.timeout : DEFAULT_TIMEOUT;
148
157
  this.websocket = new WebSocket(options.wsUri);
149
158
  this.websocket.binaryType = 'arraybuffer';
150
- const handleSignal = (signal) => {
159
+ const handleSignal = async (signal) => {
160
+ const apiVersion = await this.getShvApiVersion();
151
161
  for (const sub of this.subscriptions) {
152
162
  const shvPath = signal.meta[RPC_MESSAGE_SHV_PATH];
153
163
  const signalName = signal.meta[RPC_MESSAGE_METHOD];
154
- if (shvPath.startsWith(sub.path) && signalName === sub.signal) {
164
+ if (pathMatchesSubscription(sub.path, shvPath, apiVersion) && signalName === sub.signal) {
155
165
  sub.callback(shvPath, signalName, signal.value[RPC_MESSAGE_PARAMS]);
156
166
  }
157
167
  }
@@ -362,7 +372,7 @@ class WsClient {
362
372
  const rpcMsg = parseMessage(rpcVal);
363
373
  this.logDebug(`message received: ${toCpon(rpcMsg)}`);
364
374
  if (isSignal(rpcMsg)) {
365
- handleSignal(rpcMsg);
375
+ await handleSignal(rpcMsg);
366
376
  }
367
377
  else if (isRequest(rpcMsg)) {
368
378
  await handleRequest(rpcMsg);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libshv-js",
3
- "version": "8.0.0",
3
+ "version": "9.0.0",
4
4
  "description": "Typescript implementation of libshv",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -46,5 +46,8 @@
46
46
  "eslint-plugin-unicorn": "^73.0.0",
47
47
  "typescript": "^6.0.2",
48
48
  "typescript-eslint": "^8.46.1"
49
+ },
50
+ "dependencies": {
51
+ "minimatch": "^10.2.6"
49
52
  }
50
53
  }