libshv-js 3.22.2 → 3.23.1

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.
@@ -0,0 +1,2 @@
1
+ export type StringGetter = string | Promise<string> | (() => string | Promise<string>);
2
+ export declare const resolveString: (input: StringGetter) => string | Promise<string>;
package/dist/utils.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveString = void 0;
4
+ const resolveString = (input) => {
5
+ if (typeof input === 'function') {
6
+ input = input();
7
+ }
8
+ return input;
9
+ };
10
+ exports.resolveString = resolveString;
package/dist/vue-shv.d.ts CHANGED
@@ -4,15 +4,16 @@ import PKCE from 'js-pkce';
4
4
  import * as z from './zod';
5
5
  import { type shvMapType } from './rpcvalue';
6
6
  import { RpcValue } from './rpcvalue';
7
+ import { StringGetter } from './utils';
7
8
  type GlobalResourceOptions<ResourceType> = {
8
- shvPath: string;
9
+ shvPath: StringGetter;
9
10
  method: string;
10
11
  validator: z.ZodType<ResourceType>;
11
12
  signalName: string;
12
13
  signalHandler: (param: RpcValue, resource: Ref<ResourceType | undefined>, reinit: () => void) => void;
13
14
  };
14
15
  type VueShvOptions = {
15
- wsUri: string;
16
+ wsUri: StringGetter;
16
17
  onRequest: WsClientOptionsLogin['onRequest'];
17
18
  azureCodeRedirect?: string;
18
19
  mountPoint?: string;
@@ -63,7 +64,7 @@ declare const makePkce: (oauthOptions: {
63
64
  }) => PKCE;
64
65
  export { makePkce as azure };
65
66
  export declare function useShv(options: VueShvOptions): {
66
- azureAuth: (redirectTo: string) => void;
67
+ azureAuth: (redirectTo: string) => Promise<void>;
67
68
  azureHandlerError: Ref<string, string>;
68
69
  connected: Ref<"connected" | "connecting" | "disconnected", "connected" | "connecting" | "disconnected">;
69
70
  displayEmail: Ref<string, string>;
@@ -81,9 +82,9 @@ export declare function useShv(options: VueShvOptions): {
81
82
  default: ResourceType;
82
83
  }): () => ComputedRef<ResourceType>;
83
84
  };
84
- makeRpcCall: <ResultType>(shvPath: string, method: string, validator: z.ZodType<ResultType>) => () => Promise<Error | ResultType>;
85
- makeRpcCallParam: <ResultType, ParamType extends RpcValue>(shvPath: string, method: string, _paramType: z.ZodType<ParamType>, validator: z.ZodType<ResultType>) => (param: ParamType) => Promise<Error | ResultType>;
86
- rpcCall: (shvPath: string, method: string, params?: RpcValue) => Promise<Error | RpcValue>;
85
+ makeRpcCall: <ResultType>(shvPath: StringGetter, method: string, validator: z.ZodType<ResultType>) => () => Promise<Error | ResultType>;
86
+ makeRpcCallParam: <ResultType, ParamType extends RpcValue>(shvPath: StringGetter, method: string, _paramType: z.ZodType<ParamType>, validator: z.ZodType<ResultType>) => (param: ParamType) => Promise<Error | ResultType>;
87
+ rpcCall: (shvPath: StringGetter, method: string, params?: RpcValue) => Promise<Error | RpcValue>;
87
88
  setShvCredentials: (userName: string, password: string) => void;
88
89
  shvLocalStorage: import("@vueuse/shared").RemovableRef<ShvLocalStorage>;
89
90
  shvLogout: () => void;
package/dist/vue-shv.js CHANGED
@@ -43,6 +43,7 @@ const ws_client_1 = require("./ws-client");
43
43
  const core_1 = require("@vueuse/core");
44
44
  const js_pkce_1 = __importDefault(require("js-pkce"));
45
45
  const z = __importStar(require("./zod"));
46
+ const utils_1 = require("./utils");
46
47
  const OAuth2AzureWorkflowZod = z.map({
47
48
  type: z.literal('oauth2-azure'),
48
49
  authorizeUrl: z.string(),
@@ -133,7 +134,7 @@ function useShv(options) {
133
134
  logContents.value = logContents.value + args.join(' ') + '\n';
134
135
  console.debug(...args);
135
136
  };
136
- const azureAuth = (redirectTo) => {
137
+ const azureAuth = async (redirectTo) => {
137
138
  if (options.azureCodeRedirect === undefined) {
138
139
  throw new Error('Azure redirect URL not set');
139
140
  }
@@ -147,7 +148,7 @@ function useShv(options) {
147
148
  };
148
149
  state.ws = new ws_client_1.WsClient({
149
150
  logDebug: debug,
150
- wsUri: options.wsUri,
151
+ wsUri: await (0, utils_1.resolveString)(options.wsUri),
151
152
  onWorkflows(workflows) {
152
153
  if (!Array.isArray(workflows)) {
153
154
  noBrokerSupport();
@@ -161,6 +162,7 @@ function useShv(options) {
161
162
  shvSessionStorage.value.azureWorkflow = parsedWorkflow.data;
162
163
  globalThis.location.replace(makePkce({ ...parsedWorkflow.data, azureCodeRedirect }).authorizeUrl());
163
164
  }
165
+ noBrokerSupport();
164
166
  },
165
167
  onWorkflowsFailed() {
166
168
  noBrokerSupport();
@@ -174,10 +176,10 @@ function useShv(options) {
174
176
  };
175
177
  const rpcCall = async (shvPath, method, params) => {
176
178
  const shv = await getConnection();
177
- return shv.callRpcMethod(shvPath, method, params);
179
+ return shv.callRpcMethod(await (0, utils_1.resolveString)(shvPath), method, params);
178
180
  };
179
181
  const makeRpcCall = (shvPath, method, validator) => async () => {
180
- const resultOrError = await rpcCall(shvPath, method);
182
+ const resultOrError = await rpcCall(await (0, utils_1.resolveString)(shvPath), method);
181
183
  if (resultOrError instanceof Error) {
182
184
  return resultOrError;
183
185
  }
@@ -188,7 +190,7 @@ function useShv(options) {
188
190
  return parsed.data;
189
191
  };
190
192
  const makeRpcCallParam = (shvPath, method, _paramType, validator) => async (param) => {
191
- const resultOrError = await rpcCall(shvPath, method, param);
193
+ const resultOrError = await rpcCall(await (0, utils_1.resolveString)(shvPath), method, param);
192
194
  if (resultOrError instanceof Error) {
193
195
  return resultOrError;
194
196
  }
@@ -226,8 +228,9 @@ function useShv(options) {
226
228
  return;
227
229
  }
228
230
  connected.value = 'connecting';
229
- const doConnect = (user, password, loginType) => {
230
- console.log('Connecting to', options.wsUri);
231
+ const doConnect = async (user, password, loginType) => {
232
+ const wsUri = await (0, utils_1.resolveString)(options.wsUri);
233
+ console.log('Connecting to', wsUri);
231
234
  const makeLoginOptions = () => {
232
235
  switch (loginType) {
233
236
  case 'PLAIN':
@@ -245,7 +248,7 @@ function useShv(options) {
245
248
  };
246
249
  state.ws = new ws_client_1.WsClient({
247
250
  login: makeLoginOptions(),
248
- wsUri: options.wsUri,
251
+ wsUri,
249
252
  timeout: 120_000,
250
253
  mountPoint: options.mountPoint,
251
254
  logDebug: debug,
@@ -254,11 +257,11 @@ function useShv(options) {
254
257
  waiter.resolve(state.ws);
255
258
  }
256
259
  waitingForSocket.length = 0;
257
- console.log('Connected to', options.wsUri);
260
+ console.log('Connected to', wsUri);
258
261
  connected.value = 'connected';
259
262
  },
260
263
  onConnectionFailure(error) {
261
- console.error(`Failed to connect to: ${options.wsUri}`, error);
264
+ console.error(`Failed to connect to: ${wsUri}`, error);
262
265
  shvLogout();
263
266
  loginFailure.value = {
264
267
  reason: LoginFailureReason.CouldntLogin,
@@ -269,7 +272,7 @@ function useShv(options) {
269
272
  connected.value = 'disconnected';
270
273
  const RECONNECT_INTERVAL = 3000;
271
274
  if (shvLocalStorage.value.azureAccessToken !== undefined) {
272
- console.log('Disconnected from', options.wsUri, 'reconnecting in', RECONNECT_INTERVAL, 'ms');
275
+ console.log('Disconnected from', wsUri, 'reconnecting in', RECONNECT_INTERVAL, 'ms');
273
276
  state.reconnectService = globalThis.setTimeout(async () => {
274
277
  await getConnection();
275
278
  }, RECONNECT_INTERVAL);
@@ -284,7 +287,10 @@ function useShv(options) {
284
287
  displayName.value = shvUser;
285
288
  displayShortName.value = shvUser[0];
286
289
  displayFullName.value = shvUser;
287
- doConnect(shvUser, shvPassword, 'PLAIN');
290
+ doConnect(shvUser, shvPassword, 'PLAIN')
291
+ .catch((error) => {
292
+ console.error('Unexpected failure in doConnect:', error);
293
+ });
288
294
  return;
289
295
  }
290
296
  azureAvailable().then(isAzureAvailable => {
@@ -301,7 +307,10 @@ function useShv(options) {
301
307
  }
302
308
  const accessToken = shvLocalStorage.value.azureAccessToken;
303
309
  if (accessToken !== undefined) {
304
- doConnect('', accessToken, 'TOKEN');
310
+ doConnect('', accessToken, 'TOKEN')
311
+ .catch((error) => {
312
+ console.error('Unexpected failure in doConnect:', error);
313
+ });
305
314
  }
306
315
  }).catch((error) => {
307
316
  console.error('Failed to fetch verify azure info, redirecting to the login page in 3 seconds', error);
@@ -316,7 +325,6 @@ function useShv(options) {
316
325
  };
317
326
  function makeGlobalResource(options) {
318
327
  const resource = (0, vue_1.ref)();
319
- const resIdentifier = `${options.shvPath}:${options.method}`;
320
328
  const resourceCall = makeRpcCall(options.shvPath, options.method, options.validator);
321
329
  const refreshValue = async () => {
322
330
  const newResource = await resourceCall();
@@ -326,10 +334,12 @@ function useShv(options) {
326
334
  resource.value = newResource;
327
335
  };
328
336
  const initialize = async () => {
337
+ const shvPath = await (0, utils_1.resolveString)(options.shvPath);
338
+ const resIdentifier = `${shvPath}:${options.method}`;
329
339
  try {
330
340
  await refreshValue();
331
341
  const connection = await getConnection();
332
- connection.subscribe(`Global-${resIdentifier}:`, options.shvPath, options.signalName, (_path, _method, param) => {
342
+ await connection.subscribe(`Global-${resIdentifier}:`, shvPath, options.signalName, (_path, _method, param) => {
333
343
  options.signalHandler(param, resource, async () => refreshValue().catch((error) => {
334
344
  console.error(`Failed to initialize ${resIdentifier}`, error);
335
345
  }));
@@ -343,10 +353,12 @@ function useShv(options) {
343
353
  return () => {
344
354
  if (!initialized) {
345
355
  initialized = true;
346
- (0, vue_1.watchEffect)(() => {
356
+ (0, vue_1.watchEffect)(async () => {
347
357
  if (connected.value !== 'connected') {
348
358
  return;
349
359
  }
360
+ const shvPath = await (0, utils_1.resolveString)(options.shvPath);
361
+ const resIdentifier = `${shvPath}:${options.method}`;
350
362
  initialize().catch((error) => {
351
363
  console.error(`Failed to initialize ${resIdentifier}`, error);
352
364
  });
@@ -1,5 +1,6 @@
1
1
  import { type RpcMessage, type ErrorMap } from './rpcmessage';
2
2
  import { type RpcValue, type Null, type Int, type IMap, type ShvMap } from './rpcvalue';
3
+ import { StringGetter } from './utils';
3
4
  import * as z from './zod';
4
5
  type SubscriptionCallback = (path: string, method: string, param?: RpcValue) => void;
5
6
  type ResultOrError<T = RpcValue> = T | Error;
@@ -220,8 +221,8 @@ declare class WsClient {
220
221
  callRpcMethod(shvPath: string | undefined, method: 'ls', params?: RpcValue, delayCallback?: (progress: number) => void): RpcRequestPromise<ResultOrError<LsResult>>;
221
222
  callRpcMethod(shvPath: string | undefined, method: string, params?: RpcValue, delayCallback?: (progress: number) => void): RpcRequestPromise<ResultOrError>;
222
223
  sendRpcMessage(rpcMsg: RpcMessage): void;
223
- subscribe(subscriber: string, path: string, method: string, callback: SubscriptionCallback): void;
224
- unsubscribe(subscriber: string, path: string, method: string): void;
224
+ subscribe(subscriber: string, pathGetter: StringGetter, method: string, callback: SubscriptionCallback): Promise<void>;
225
+ unsubscribe(subscriber: string, pathGetter: StringGetter, method: string): Promise<void>;
225
226
  sendPing(): void;
226
227
  accessGrantForMethodCall(path: string, method: string): RpcRequestPromise<ResultOrError<string>>;
227
228
  close(): void;
package/dist/ws-client.js CHANGED
@@ -39,6 +39,7 @@ const chainpack_1 = require("./chainpack");
39
39
  const cpon_1 = require("./cpon");
40
40
  const rpcmessage_1 = require("./rpcmessage");
41
41
  const rpcvalue_1 = require("./rpcvalue");
42
+ const utils_1 = require("./utils");
42
43
  const z = __importStar(require("./zod"));
43
44
  const DEFAULT_TIMEOUT = 5000;
44
45
  const DEFAULT_PING_INTERVAL = 30 * 1000;
@@ -445,11 +446,12 @@ class WsClient {
445
446
  this.websocket.send(dgram.buffer);
446
447
  }
447
448
  }
448
- subscribe(subscriber, path, method, callback) {
449
+ async subscribe(subscriber, pathGetter, method, callback) {
449
450
  if (this.subscriptions.some(val => val.subscriber === subscriber && val.path === path && val.method === method)) {
450
451
  this.logDebug(`Already subscribed {$path}:${method} for subscriber ${subscriber}`);
451
452
  return;
452
453
  }
454
+ const path = await (0, utils_1.resolveString)(pathGetter);
453
455
  // If this path:method has not been subscribed on the broker, do it now
454
456
  if (!this.subscriptions.some(val => val.path === path && val.method === method)) {
455
457
  this.callRpcMethod('.broker/app', 'subscribe', (0, rpcvalue_1.makeMap)({
@@ -465,7 +467,8 @@ class WsClient {
465
467
  callback,
466
468
  });
467
469
  }
468
- unsubscribe(subscriber, path, method) {
470
+ async unsubscribe(subscriber, pathGetter, method) {
471
+ const path = await (0, utils_1.resolveString)(pathGetter);
469
472
  const idx = this.subscriptions.findIndex(val => val.subscriber === subscriber && val.path === path && val.method === method);
470
473
  if (idx === -1) {
471
474
  this.logDebug(`No such subscription ${path}:${method} for subscriber ${subscriber}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libshv-js",
3
- "version": "3.22.2",
3
+ "version": "3.23.1",
4
4
  "description": "Typescript implementation of libshv",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -33,6 +33,7 @@
33
33
  "./json": "./dist/json.js",
34
34
  "./rpcmessage": "./dist/rpcmessage.js",
35
35
  "./rpcvalue": "./dist/rpcvalue.js",
36
+ "./utils": "./dist/utils.js",
36
37
  "./vue-shv": "./dist/vue-shv.js",
37
38
  "./ws-client": "./dist/ws-client.js",
38
39
  "./zod": "./dist/zod.js"