@holochain/tryorama
Version:
Toolset to manage Holochain conductors and facilitate running test scenarios
83 lines (82 loc) • 2.59 kB
JavaScript
import msgpack from "@msgpack/msgpack";
import assert from "node:assert";
/**
* Deserialize the binary response from TryCP
*
* @param response - The response to deserialize.
* @returns The deserialized response.
*
* @internal
*/
export const deserializeTryCpResponse = (response) => {
const decodedData = msgpack.decode(response);
assertIsResponseWrapper(decodedData);
return decodedData;
};
/**
* Deserialize a binary signal from TryCP
*
* @param signal - The signal to deserialize.
* @returns The deserialized signal.
*/
export const deserializeTryCpSignal = (signal) => {
const deserializedSignal = msgpack.decode(signal);
assertIsRawSignal(deserializedSignal);
if (deserializedSignal.type === "app") {
const decodedPayload = msgpack.decode(deserializedSignal.value.signal);
const app_signal = {
cell_id: deserializedSignal.value.cell_id,
zome_name: deserializedSignal.value.zome_name,
payload: decodedPayload,
};
return { type: "app", value: app_signal };
}
else {
throw new Error("Receiving system signals is not implemented yet");
}
};
/**
* Deserialize the binary response from the Admin or App API
*
* @param response - The response to deserialize.
* @returns The deserialized response.
*
* @internal
*/
export const deserializeApiResponse = (response) => {
const decodedResponse = msgpack.decode(response);
assertIsApiResponse(decodedResponse);
return decodedResponse;
};
/**
* Deserialize the App API response's payload
*
* @param payload - The payload to deserialize.
* @typeParam P - The type of the response's payload.
* @returns The deserialized payload.
*
* @internal
*/
export const deserializeZomeResponsePayload = (payload) => {
const deserializedPayload = msgpack.decode(payload);
return deserializedPayload;
};
function assertIsResponseWrapper(response) {
assert(response !== null &&
typeof response === "object" &&
"type" in response &&
// responses contain "id" and "response"
(("id" in response && "response" in response) ||
//and signals contain "port" and "data"
("port" in response && "data" in response)));
}
function assertIsApiResponse(response) {
assert(response && typeof response === "object" && "type" in response);
}
function assertIsRawSignal(signal) {
assert(typeof signal === "object" &&
signal &&
"type" in signal &&
"value" in signal &&
["app", "signal"].some((type) => signal.type === type));
}