@holochain/client
Advanced tools
Comparing version 0.17.0-dev.7 to 0.17.0-dev.8
@@ -10,2 +10,7 @@ import { Action, DhtOp, Entry, ZomeCallCapGrant } from "../../hdk/index.js"; | ||
port?: number; | ||
/** | ||
* Comma separated list of origins, or `*` to allow any origin. | ||
* For example: `http://localhost:3000,http://localhost:3001` | ||
*/ | ||
allowed_origins: string; | ||
}; | ||
@@ -12,0 +17,0 @@ /** |
import { getLauncherEnvironment } from "../../environments/launcher.js"; | ||
import { GrantedFunctionsType, } from "../../hdk/capabilities.js"; | ||
import { WsClient } from "../client.js"; | ||
import { catchError, DEFAULT_TIMEOUT, promiseTimeout, requesterTransformer, } from "../common.js"; | ||
import { catchError, DEFAULT_TIMEOUT, promiseTimeout, requesterTransformer, HolochainError, } from "../common.js"; | ||
import { generateSigningKeyPair, randomCapSecret, setSigningCredentials, } from "../zome-call-signing.js"; | ||
@@ -39,5 +39,5 @@ import { AppStatusFilter, } from "./types.js"; | ||
if (!options.url) { | ||
throw new Error("Unable to connect to Admin Websocket: No url provided and not in a Launcher environment."); | ||
throw new HolochainError("ConnectionUrlMissing", `unable to connect to Conductor API - no url provided and not in a launcher environment.`); | ||
} | ||
const wsClient = await WsClient.connect(options.url); | ||
const wsClient = await WsClient.connect(options.url, options.wsClientOptions); | ||
return new AdminWebsocket(wsClient, options.defaultTimeout); | ||
@@ -44,0 +44,0 @@ } |
@@ -84,7 +84,7 @@ import Emittery from "emittery"; | ||
if (!(baseRoleName in appInfo.cell_info)) { | ||
throw new Error(`No cell found with role_name ${roleName}`); | ||
throw new HolochainError("NoCellForRoleName", `no cell found with role_name ${roleName}`); | ||
} | ||
const cloneCell = appInfo.cell_info[baseRoleName].find((c) => CellType.Cloned in c && c[CellType.Cloned].clone_id === roleName); | ||
if (!cloneCell || !(CellType.Cloned in cloneCell)) { | ||
throw new Error(`No clone cell found with clone id ${roleName}`); | ||
throw new HolochainError("NoCellForCloneId", `no clone cell found with clone id ${roleName}`); | ||
} | ||
@@ -94,7 +94,7 @@ return cloneCell[CellType.Cloned].cell_id; | ||
if (!(roleName in appInfo.cell_info)) { | ||
throw new Error(`No cell found with role_name ${roleName}`); | ||
throw new HolochainError("NoCellForRoleName", `no cell found with role_name ${roleName}`); | ||
} | ||
const cell = appInfo.cell_info[roleName].find((c) => CellType.Provisioned in c); | ||
if (!cell || !(CellType.Provisioned in cell)) { | ||
throw new Error(`No provisioned cell found with role_name ${roleName}`); | ||
throw new HolochainError("NoProvisionedCellForRoleName", `no provisioned cell found with role_name ${roleName}`); | ||
} | ||
@@ -130,3 +130,3 @@ return cell[CellType.Provisioned].cell_id; | ||
} | ||
throw new Error("callZome requires a role_name or cell_id arg"); | ||
throw new HolochainError("MissingRoleNameOrCellId", "callZome requires a role_name or cell_id argument"); | ||
} | ||
@@ -133,0 +133,0 @@ /** |
@@ -8,3 +8,3 @@ import { hashZomeCall } from "@holochain/serialization"; | ||
import { WsClient } from "../client.js"; | ||
import { DEFAULT_TIMEOUT, catchError, promiseTimeout, requesterTransformer, } from "../common.js"; | ||
import { DEFAULT_TIMEOUT, catchError, promiseTimeout, requesterTransformer, HolochainError, } from "../common.js"; | ||
import { getNonceExpiration, getSigningCredentials, randomNonce, } from "../zome-call-signing.js"; | ||
@@ -50,5 +50,5 @@ /** | ||
if (!options.url) { | ||
throw new Error("Unable to connect to App Websocket: No url provided and not in a Launcher environment."); | ||
throw new HolochainError("ConnectionUrlMissing", `unable to connect to Conductor API - no url provided and not in a launcher environment.`); | ||
} | ||
const wsClient = await WsClient.connect(options.url); | ||
const wsClient = await WsClient.connect(options.url, options.wsClientOptions); | ||
const appWebsocket = new AppWebsocket(wsClient, options.defaultTimeout, env?.INSTALLED_APP_ID); | ||
@@ -139,3 +139,3 @@ wsClient.on("signal", (signal) => appWebsocket.emit("signal", signal)); | ||
if (!signingCredentialsForCell) { | ||
throw new Error(`cannot sign zome call: no signing credentials have been authorized for cell [${encodeHashToBase64(request.cell_id[0])}, ${encodeHashToBase64(request.cell_id[1])}]`); | ||
throw new HolochainError("NoSigningCredentialsForCell", `no signing credentials have been authorized for cell [${encodeHashToBase64(request.cell_id[0])}, ${encodeHashToBase64(request.cell_id[1])}]`); | ||
} | ||
@@ -142,0 +142,0 @@ const unsignedZomeCallPayload = { |
/// <reference types="ws" /> | ||
import Emittery from "emittery"; | ||
import IsoWebSocket from "isomorphic-ws"; | ||
import { WsClientOptions } from "./common.js"; | ||
/** | ||
@@ -15,5 +16,6 @@ * A WebSocket client which can make requests and receive responses, | ||
url: URL | undefined; | ||
options: WsClientOptions; | ||
private pendingRequests; | ||
private index; | ||
constructor(socket: IsoWebSocket, url?: URL); | ||
constructor(socket: IsoWebSocket, url?: URL, options?: WsClientOptions); | ||
private setupSocket; | ||
@@ -26,3 +28,3 @@ /** | ||
*/ | ||
static connect(url: URL): Promise<WsClient>; | ||
static connect(url: URL, options?: WsClientOptions): Promise<WsClient>; | ||
/** | ||
@@ -29,0 +31,0 @@ * Sends data as a signal. |
@@ -5,2 +5,3 @@ import { decode, encode } from "@msgpack/msgpack"; | ||
import { SignalType } from "./app/types.js"; | ||
import { HolochainError } from "./common.js"; | ||
/** | ||
@@ -17,8 +18,10 @@ * A WebSocket client which can make requests and receive responses, | ||
url; | ||
options; | ||
pendingRequests; | ||
index; | ||
constructor(socket, url) { | ||
constructor(socket, url, options) { | ||
super(); | ||
this.socket = socket; | ||
this.url = url; | ||
this.options = options || {}; | ||
this.pendingRequests = {}; | ||
@@ -42,3 +45,3 @@ this.index = 0; | ||
else { | ||
throw new Error("websocket client: unknown message format"); | ||
throw new HolochainError("UnknownMessageFormat", `incoming message has unknown message format - ${deserializedData}`); | ||
} | ||
@@ -50,3 +53,3 @@ } | ||
if (message.data === null) { | ||
throw new Error("received a signal without data"); | ||
throw new HolochainError("UnknownSignalFormat", "incoming signal has no data"); | ||
} | ||
@@ -73,3 +76,3 @@ const deserializedSignal = decode(message.data); | ||
else { | ||
console.error(`Got unrecognized Websocket message type: ${message.type}`); | ||
throw new HolochainError("UnknownMessageType", `incoming message has unknown type - ${message.type}`); | ||
} | ||
@@ -81,3 +84,3 @@ }; | ||
pendingRequestIds.forEach((id) => { | ||
const error = new Error(`Websocket closed with pending requests. Close event code: ${event.code}, request id: ${id}`); | ||
const error = new HolochainError("ClientClosedWithPendingRequests", `client closed with pending requests - close event code: ${event.code}, request id: ${id}`); | ||
this.pendingRequests[id].reject(error); | ||
@@ -95,10 +98,10 @@ delete this.pendingRequests[id]; | ||
*/ | ||
static connect(url) { | ||
static connect(url, options) { | ||
return new Promise((resolve, reject) => { | ||
const socket = new IsoWebSocket(url); | ||
socket.onerror = () => { | ||
reject(new Error(`could not connect to holochain conductor, please check that a conductor service is running and available at ${url}`)); | ||
const socket = new IsoWebSocket(url, options); | ||
socket.onerror = (errorEvent) => { | ||
reject(new HolochainError("ConnectionError", `could not connect to Holochain Conductor API at ${url} - ${errorEvent.error}`)); | ||
}; | ||
socket.onopen = () => { | ||
const client = new WsClient(socket, url); | ||
const client = new WsClient(socket, url, options); | ||
resolve(client); | ||
@@ -136,6 +139,6 @@ }; | ||
// typescript forgets in this promise scope that this.url is not undefined | ||
const socket = new IsoWebSocket(this.url); | ||
const socket = new IsoWebSocket(this.url, this.options); | ||
this.socket = socket; | ||
socket.onerror = () => { | ||
reject(new Error(`could not connect to Holochain conductor, please check that a conductor service is running and available at ${this.url}`)); | ||
socket.onerror = (errorEvent) => { | ||
reject(new HolochainError("ConnectionError", `could not connect to Holochain Conductor API at ${this.url} - ${errorEvent.error}`)); | ||
}; | ||
@@ -176,3 +179,3 @@ socket.onopen = () => { | ||
else { | ||
console.error(`Got response with no matching request. id=${id}`); | ||
console.error(`got response with no matching request. id = ${id} msg = ${msg}`); | ||
} | ||
@@ -202,3 +205,3 @@ } | ||
} | ||
throw new Error(`unknown message format ${JSON.stringify(message, null, 4)}`); | ||
throw new HolochainError("UnknownMessageFormat", `incoming message has unknown message format ${JSON.stringify(message, null, 4)}`); | ||
} | ||
@@ -211,4 +214,4 @@ function assertHolochainSignal(signal) { | ||
} | ||
throw new Error(`unknown signal format ${JSON.stringify(signal, null, 4)}`); | ||
throw new HolochainError("UnknownSignalFormat", `incoming signal has unknown signal format ${JSON.stringify(signal, null, 4)}`); | ||
} | ||
export { IsoWebSocket }; |
@@ -0,2 +1,4 @@ | ||
/// <reference types="ws" /> | ||
import { RoleName } from "../types.js"; | ||
import { IsoWebSocket } from "./client.js"; | ||
export declare const DEFAULT_TIMEOUT = 60000; | ||
@@ -82,2 +84,6 @@ /** | ||
/** | ||
* @public | ||
*/ | ||
export type WsClientOptions = Pick<IsoWebSocket.ClientOptions, "origin">; | ||
/** | ||
* Options for a Websocket connection. | ||
@@ -93,2 +99,6 @@ * | ||
/** | ||
* Options to pass to the underlying websocket connection. | ||
*/ | ||
wsClientOptions?: WsClientOptions; | ||
/** | ||
* Timeout to default to for all operations. | ||
@@ -95,0 +105,0 @@ */ |
@@ -78,3 +78,3 @@ const ERROR_TYPE = "error"; | ||
if (!isCloneId(roleName)) { | ||
throw new Error("invalid clone id: no clone id delimiter found in role name"); | ||
throw new HolochainError("MissingCloneIdDelimiter", `invalid clone id - no clone id delimiter found in role name ${roleName}`); | ||
} | ||
@@ -106,3 +106,3 @@ return roleName.split(CLONE_ID_DELIMITER)[0]; | ||
if (parts.length !== 2) { | ||
throw new Error("Malformed clone id: must consist of {role id.clone index}"); | ||
throw new HolochainError("MalformedCloneId", `clone id must consist of 'role_id.clone_index', but got ${roleName}`); | ||
} | ||
@@ -109,0 +109,0 @@ return new CloneId(parts[0], parseInt(parts[1])); |
@@ -6,3 +6,3 @@ export { hashZomeCall } from "@holochain/serialization"; | ||
export { IsoWebSocket, WsClient } from "./client.js"; | ||
export { CloneId, HolochainError, Requester, Transformer, WebsocketConnectionOptions, getBaseRoleNameFromCloneId, isCloneId, } from "./common.js"; | ||
export { CloneId, HolochainError, Requester, Transformer, WebsocketConnectionOptions, WsClientOptions, getBaseRoleNameFromCloneId, isCloneId, } from "./common.js"; | ||
export * from "./zome-call-signing.js"; |
@@ -8,5 +8,5 @@ // This file is read by tools that parse documentation comments conforming to the TSDoc standard. | ||
"packageName": "@microsoft/api-extractor", | ||
"packageVersion": "7.41.0" | ||
"packageVersion": "7.43.0" | ||
} | ||
] | ||
} |
{ | ||
"name": "@holochain/client", | ||
"version": "0.17.0-dev.7", | ||
"version": "0.17.0-dev.8", | ||
"description": "A JavaScript client for the Holochain Conductor API", | ||
@@ -40,3 +40,4 @@ "author": "Holochain Foundation <info@holochain.org> (http://holochain.org)", | ||
"build:docs": "api-extractor run --local && api-documenter markdown -i docs/temp -o docs", | ||
"build": "npm run build:lib && npm run build:docs" | ||
"build": "npm run build:lib && npm run build:docs", | ||
"prepublishOnly": "npm run lint && npm run build" | ||
}, | ||
@@ -43,0 +44,0 @@ "dependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
133584
3705