@holochain/client 0.17.0-dev.5 → 0.17.0-dev.7

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/README.md CHANGED
@@ -34,7 +34,7 @@ npm install --save-exact @holochain/client
34
34
  ```typescript
35
35
  import { ActionHash, AdminWebsocket, AppAgentWebsocket, CellType } from "@holochain/client";
36
36
 
37
- const adminWs = await AdminWebsocket.connect("ws://127.0.0.1:65000");
37
+ const adminWs = await AdminWebsocket.connect({url: "ws://127.0.0.1:65000"});
38
38
  const agent_key = await adminWs.generateAgentPubKey();
39
39
  const role_name = "role";
40
40
  const installed_app_id = "test-app";
@@ -51,10 +51,7 @@ if (!(CellType.Provisioned in appInfo.cell_info[role_name][0])) {
51
51
  const { cell_id } = appInfo.cell_info[role_name][0][CellType.Provisioned];
52
52
  await adminWs.authorizeSigningCredentials(cell_id);
53
53
  await adminWs.attachAppInterface({ port: 65001 });
54
- const appAgentWs = await AppAgentWebsocket.connect(
55
- "ws://127.0.0.1:65001",
56
- installed_app_id
57
- );
54
+ const appAgentWs = await AppAgentWebsocket.connect(installed_app_id, {url: "ws://127.0.0.1:65001"});
58
55
 
59
56
  const zomeCallPayload: CallZomeRequest = {
60
57
  cell_id,
@@ -74,7 +71,7 @@ await adminWs.client.close();
74
71
  ```typescript
75
72
  import { AdminWebsocket, AppWebsocket, CellType } from "@holochain/client";
76
73
 
77
- const adminWs = await AdminWebsocket.connect("ws://127.0.0.1:65000");
74
+ const adminWs = await AdminWebsocket.connect({url: "ws://127.0.0.1:65000"});
78
75
  const agent_key = await adminWs.generateAgentPubKey();
79
76
  const installed_app_id = "test-app";
80
77
  const appInfo = await adminWs.installApp({
@@ -90,7 +87,7 @@ if (!(CellType.Provisioned in appInfo.cell_info["role"][0])) {
90
87
  const { cell_id } = appInfo.cell_info["role"][0][CellType.Provisioned];
91
88
  await adminWs.authorizeSigningCredentials(cell_id);
92
89
  await adminWs.attachAppInterface({ port: 65001 });
93
- const appWs = await AppWebsocket.connect("ws://127.0.0.1:65001");
90
+ const appWs = await AppWebsocket.connect({url: "ws://127.0.0.1:65001"});
94
91
 
95
92
  let signalCb;
96
93
  const signalReceived = new Promise<void>((resolve) => {
@@ -1,7 +1,7 @@
1
1
  import { CapSecret, GrantedFunctions } from "../../hdk/capabilities.js";
2
2
  import type { AgentPubKey, CellId } from "../../types.js";
3
3
  import { WsClient } from "../client.js";
4
- import { Requester, Transformer } from "../common.js";
4
+ import { WebsocketConnectionOptions, Requester, Transformer } from "../common.js";
5
5
  import { AddAgentInfoRequest, AddAgentInfoResponse, AdminApi, AgentInfoRequest, AgentInfoResponse, AttachAppInterfaceRequest, AttachAppInterfaceResponse, DeleteCloneCellRequest, DeleteCloneCellResponse, DisableAppRequest, DisableAppResponse, DumpFullStateRequest, DumpFullStateResponse, DumpNetworkStatsRequest, DumpNetworkStatsResponse, DumpStateRequest, DumpStateResponse, EnableAppRequest, EnableAppResponse, GenerateAgentPubKeyRequest, GenerateAgentPubKeyResponse, GetDnaDefinitionRequest, GetDnaDefinitionResponse, GrantZomeCallCapabilityRequest, GrantZomeCallCapabilityResponse, InstallAppRequest, InstallAppResponse, ListAppInterfacesRequest, ListAppInterfacesResponse, ListAppsRequest, ListAppsResponse, ListCellIdsRequest, ListCellIdsResponse, ListDnasRequest, ListDnasResponse, RegisterDnaRequest, RegisterDnaResponse, StorageInfoRequest, StorageInfoResponse, UninstallAppRequest, UninstallAppResponse, UpdateCoordinatorsRequest, UpdateCoordinatorsResponse } from "./types.js";
6
6
  /**
7
7
  * A class for interacting with a conductor's Admin API.
@@ -21,11 +21,10 @@ export declare class AdminWebsocket implements AdminApi {
21
21
  /**
22
22
  * Factory mehtod to create a new instance connected to the given URL.
23
23
  *
24
- * @param url - A `ws://` URL used as the connection address.
25
- * @param defaultTimeout - The default timeout for any request.
24
+ * @param options - {@link (WebsocketConnectionOptions:interface)}
26
25
  * @returns A promise for a new connected instance.
27
26
  */
28
- static connect(url: URL, defaultTimeout?: number): Promise<AdminWebsocket>;
27
+ static connect(options?: WebsocketConnectionOptions): Promise<AdminWebsocket>;
29
28
  _requester<ReqI, ReqO, ResI, ResO>(tag: string, transformer?: Transformer<ReqI, ReqO, ResI, ResO>): (req: ReqI, timeout?: number | undefined) => Promise<ResO>;
30
29
  /**
31
30
  * Send a request to open the given port for {@link AppWebsocket} connections.
@@ -26,18 +26,20 @@ export class AdminWebsocket {
26
26
  /**
27
27
  * Factory mehtod to create a new instance connected to the given URL.
28
28
  *
29
- * @param url - A `ws://` URL used as the connection address.
30
- * @param defaultTimeout - The default timeout for any request.
29
+ * @param options - {@link (WebsocketConnectionOptions:interface)}
31
30
  * @returns A promise for a new connected instance.
32
31
  */
33
- static async connect(url, defaultTimeout) {
32
+ static async connect(options = {}) {
34
33
  // Check if we are in the launcher's environment, and if so, redirect the url to connect to
35
34
  const env = getLauncherEnvironment();
36
35
  if (env?.ADMIN_INTERFACE_PORT) {
37
- url = new URL(`ws://127.0.0.1:${env.ADMIN_INTERFACE_PORT}`);
36
+ options.url = new URL(`ws://127.0.0.1:${env.ADMIN_INTERFACE_PORT}`);
38
37
  }
39
- const wsClient = await WsClient.connect(url);
40
- return new AdminWebsocket(wsClient, defaultTimeout);
38
+ if (!options.url) {
39
+ throw new Error("Unable to connect to Admin Websocket: No url provided and not in a Launcher environment.");
40
+ }
41
+ const wsClient = await WsClient.connect(options.url);
42
+ return new AdminWebsocket(wsClient, options.defaultTimeout);
41
43
  }
42
44
  _requester(tag, transformer) {
43
45
  return requesterTransformer((req, timeout) => promiseTimeout(this.client.request(req), tag, timeout || this.defaultTimeout).then(catchError), tag, transformer);
@@ -32,7 +32,7 @@ export type AppInfoRequest = {
32
32
  /**
33
33
  * @public
34
34
  */
35
- export type AppInfoResponse = AppInfo;
35
+ export type AppInfoResponse = AppInfo | null;
36
36
  /**
37
37
  * @public
38
38
  */
@@ -2,7 +2,7 @@ import Emittery from "emittery";
2
2
  import { CapSecret } from "../../hdk/capabilities.js";
3
3
  import { InstalledAppId } from "../../types.js";
4
4
  import { WsClient } from "../client.js";
5
- import { Requester, Transformer } from "../common.js";
5
+ import { WebsocketConnectionOptions, Requester, Transformer } from "../common.js";
6
6
  import { Nonce256Bit } from "../zome-call-signing.js";
7
7
  import { AppApi, AppInfoRequest, AppInfoResponse, CallZomeRequest, CallZomeResponse, CreateCloneCellRequest, CreateCloneCellResponse, DisableCloneCellRequest, DisableCloneCellResponse, EnableCloneCellRequest, EnableCloneCellResponse, NetworkInfoRequest, NetworkInfoResponse } from "./types.js";
8
8
  /**
@@ -19,11 +19,10 @@ export declare class AppWebsocket extends Emittery implements AppApi {
19
19
  /**
20
20
  * Instance factory for creating AppWebsockets.
21
21
  *
22
- * @param url - The `ws://` URL of the App API to connect to.
23
- * @param defaultTimeout - Timeout to default to for all operations.
22
+ * @param options - {@link (WebsocketConnectionOptions:interface)}
24
23
  * @returns A new instance of an AppWebsocket.
25
24
  */
26
- static connect(url: URL, defaultTimeout?: number): Promise<AppWebsocket>;
25
+ static connect(options?: WebsocketConnectionOptions): Promise<AppWebsocket>;
27
26
  _requester<ReqI, ReqO, ResI, ResO>(tag: string, transformer?: Transformer<ReqI, ReqO, ResI, ResO>): (req: ReqI, timeout?: number | undefined) => Promise<ResO>;
28
27
  /**
29
28
  * Request the app's info, including all cell infos.
@@ -36,18 +36,20 @@ export class AppWebsocket extends Emittery {
36
36
  /**
37
37
  * Instance factory for creating AppWebsockets.
38
38
  *
39
- * @param url - The `ws://` URL of the App API to connect to.
40
- * @param defaultTimeout - Timeout to default to for all operations.
39
+ * @param options - {@link (WebsocketConnectionOptions:interface)}
41
40
  * @returns A new instance of an AppWebsocket.
42
41
  */
43
- static async connect(url, defaultTimeout) {
42
+ static async connect(options = {}) {
44
43
  // Check if we are in the launcher's environment, and if so, redirect the url to connect to
45
44
  const env = getLauncherEnvironment();
46
45
  if (env?.APP_INTERFACE_PORT) {
47
- url = new URL(`ws://127.0.0.1:${env.APP_INTERFACE_PORT}`);
46
+ options.url = new URL(`ws://127.0.0.1:${env.APP_INTERFACE_PORT}`);
48
47
  }
49
- const wsClient = await WsClient.connect(url);
50
- const appWebsocket = new AppWebsocket(wsClient, defaultTimeout, env?.INSTALLED_APP_ID);
48
+ if (!options.url) {
49
+ throw new Error("Unable to connect to App Websocket: No url provided and not in a Launcher environment.");
50
+ }
51
+ const wsClient = await WsClient.connect(options.url);
52
+ const appWebsocket = new AppWebsocket(wsClient, options.defaultTimeout, env?.INSTALLED_APP_ID);
51
53
  wsClient.on("signal", (signal) => appWebsocket.emit("signal", signal));
52
54
  return appWebsocket;
53
55
  }
@@ -3,6 +3,7 @@ import { AgentPubKey, CellId, InstalledAppId, RoleName } from "../../types.js";
3
3
  import { AppInfo } from "../admin/types.js";
4
4
  import { AppSignalCb, CallZomeResponse, CreateCloneCellResponse, DisableCloneCellResponse, EnableCloneCellResponse, NetworkInfoResponse } from "../app/types.js";
5
5
  import { AppWebsocket } from "../app/websocket.js";
6
+ import { WebsocketConnectionOptions } from "../common.js";
6
7
  import { AppAgentCallZomeRequest, AppAgentClient, AppAgentEvents, AppAgentNetworkInfoRequest, AppCreateCloneCellRequest, AppDisableCloneCellRequest, AppEnableCloneCellRequest } from "./types.js";
7
8
  /**
8
9
  * A class to establish a websocket connection to an App interface, for a
@@ -13,7 +14,7 @@ import { AppAgentCallZomeRequest, AppAgentClient, AppAgentEvents, AppAgentNetwor
13
14
  export declare class AppAgentWebsocket implements AppAgentClient {
14
15
  readonly appWebsocket: AppWebsocket;
15
16
  installedAppId: InstalledAppId;
16
- cachedAppInfo?: AppInfo;
17
+ cachedAppInfo?: AppInfo | null;
17
18
  myPubKey: AgentPubKey;
18
19
  readonly emitter: Emittery<AppAgentEvents>;
19
20
  private constructor();
@@ -26,12 +27,11 @@ export declare class AppAgentWebsocket implements AppAgentClient {
26
27
  /**
27
28
  * Instance factory for creating AppAgentWebsockets.
28
29
  *
29
- * @param url - The `ws://` URL of the App API to connect to.
30
30
  * @param installed_app_id - ID of the App to link to.
31
- * @param defaultTimeout - Timeout to default to for all operations.
31
+ * @param options - {@link (WebsocketConnectionOptions:interface)}
32
32
  * @returns A new instance of an AppAgentWebsocket.
33
33
  */
34
- static connect(url: URL, installed_app_id: InstalledAppId, defaultTimeout?: number): Promise<AppAgentWebsocket>;
34
+ static connect(installed_app_id: InstalledAppId, options?: WebsocketConnectionOptions): Promise<AppAgentWebsocket>;
35
35
  /**
36
36
  * Get a cell id by its role name or clone id.
37
37
  *
@@ -3,7 +3,7 @@ import { omit } from "lodash-es";
3
3
  import { getLauncherEnvironment } from "../../environments/launcher.js";
4
4
  import { CellType } from "../admin/types.js";
5
5
  import { AppWebsocket } from "../app/websocket.js";
6
- import { getBaseRoleNameFromCloneId, isCloneId } from "../common.js";
6
+ import { HolochainError, getBaseRoleNameFromCloneId, isCloneId, } from "../common.js";
7
7
  /**
8
8
  * A class to establish a websocket connection to an App interface, for a
9
9
  * specific agent and app.
@@ -18,6 +18,7 @@ export class AppAgentWebsocket {
18
18
  emitter;
19
19
  constructor(appWebsocket, installedAppId, myPubKey) {
20
20
  this.appWebsocket = appWebsocket;
21
+ this.cachedAppInfo = null;
21
22
  this.emitter = new Emittery();
22
23
  // Ensure all super methods are bound to this instance because Emittery relies on `this` being the instance.
23
24
  // Please retain until the upstream is fixed https://github.com/sindresorhus/emittery/issues/86.
@@ -46,22 +47,25 @@ export class AppAgentWebsocket {
46
47
  const appInfo = await this.appWebsocket.appInfo({
47
48
  installed_app_id: this.installedAppId,
48
49
  });
50
+ if (!appInfo) {
51
+ throw new HolochainError("AppNotFound", `App info not found for the provided id "${this.installedAppId}". App needs to be installed and enabled.`);
52
+ }
49
53
  this.cachedAppInfo = appInfo;
50
54
  return appInfo;
51
55
  }
52
56
  /**
53
57
  * Instance factory for creating AppAgentWebsockets.
54
58
  *
55
- * @param url - The `ws://` URL of the App API to connect to.
56
59
  * @param installed_app_id - ID of the App to link to.
57
- * @param defaultTimeout - Timeout to default to for all operations.
60
+ * @param options - {@link (WebsocketConnectionOptions:interface)}
58
61
  * @returns A new instance of an AppAgentWebsocket.
59
62
  */
60
- static async connect(url, installed_app_id, defaultTimeout) {
61
- const appWebsocket = await AppWebsocket.connect(url, defaultTimeout);
62
- const appInfo = await appWebsocket.appInfo({
63
- installed_app_id: installed_app_id,
64
- });
63
+ static async connect(installed_app_id, options = {}) {
64
+ const appWebsocket = await AppWebsocket.connect(options);
65
+ const appInfo = await appWebsocket.appInfo({ installed_app_id });
66
+ if (!appInfo) {
67
+ throw new HolochainError("AppNotFound", `App info not found for the provided id "${installed_app_id}". App needs to be installed and enabled.`);
68
+ }
65
69
  const appAgentWs = new AppAgentWebsocket(appWebsocket, installed_app_id, appInfo.agent_pub_key);
66
70
  appAgentWs.cachedAppInfo = appInfo;
67
71
  return appAgentWs;
@@ -14,7 +14,7 @@ export declare class WsClient extends Emittery {
14
14
  url: URL | undefined;
15
15
  private pendingRequests;
16
16
  private index;
17
- constructor(socket: IsoWebSocket, url: URL);
17
+ constructor(socket: IsoWebSocket, url?: URL);
18
18
  private setupSocket;
19
19
  /**
20
20
  * Instance factory for creating WsClients.
@@ -78,3 +78,18 @@ export declare class CloneId {
78
78
  toString(): string;
79
79
  getBaseRoleName(): string;
80
80
  }
81
+ /**
82
+ * Options for a Websocket connection.
83
+ *
84
+ * @public
85
+ */
86
+ export interface WebsocketConnectionOptions {
87
+ /**
88
+ * The `ws://` URL of the Websocket server to connect to. Not required when connecting to App API from a Launcher or Kangaroo environment.
89
+ */
90
+ url?: URL;
91
+ /**
92
+ * Timeout to default to for all operations.
93
+ */
94
+ defaultTimeout?: number;
95
+ }
@@ -3,5 +3,5 @@ export * from "./admin/index.js";
3
3
  export * from "./app-agent/index.js";
4
4
  export * from "./app/index.js";
5
5
  export { IsoWebSocket, WsClient } from "./client.js";
6
- export { CloneId, HolochainError, Requester, Transformer, getBaseRoleNameFromCloneId, isCloneId, } from "./common.js";
6
+ export { CloneId, HolochainError, Requester, Transformer, WebsocketConnectionOptions, getBaseRoleNameFromCloneId, isCloneId, } from "./common.js";
7
7
  export * from "./zome-call-signing.js";
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.39.4"
8
+ "packageVersion": "7.41.0"
9
9
  }
10
10
  ]
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holochain/client",
3
- "version": "0.17.0-dev.5",
3
+ "version": "0.17.0-dev.7",
4
4
  "description": "A JavaScript client for the Holochain Conductor API",
5
5
  "author": "Holochain Foundation <info@holochain.org> (http://holochain.org)",
6
6
  "license": "CAL-1.0",
@@ -71,4 +71,4 @@
71
71
  "ts-node": "^10.9.1",
72
72
  "typescript": "^4.9.5"
73
73
  }
74
- }
74
+ }