Skip to content

Commit

Permalink
Align types
Browse files Browse the repository at this point in the history
  • Loading branch information
ThetaSinner committed Jul 17, 2024
1 parent a185875 commit 3c29a2a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
8 changes: 8 additions & 0 deletions crates/trycp_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ pub enum MessageToClient {
},
}

/// Messages returned directly by the TryCp server, rather than relayed from Holochain
#[derive(Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "snake_case", tag = "type", content = "data")]
pub enum TryCpServerResponse {
/// See [DownloadLogsResponse].
DownloadLogs(DownloadLogsResponse),
}

/// The successful response type for a [Request::DownloadLogs] request.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct DownloadLogsResponse {
Expand Down
25 changes: 14 additions & 11 deletions crates/trycp_server/src/download_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
CONDUCTOR_STDOUT_LOG_FILENAME, LAIR_STDERR_LOG_FILENAME,
};
use snafu::{ResultExt, Snafu};
use trycp_api::DownloadLogsResponse;
use trycp_api::{DownloadLogsResponse, MessageResponse, TryCpServerResponse};

#[derive(Debug, Snafu)]
pub(crate) enum DownloadLogsError {
Expand All @@ -23,30 +23,33 @@ pub(crate) enum DownloadLogsError {
source
))]
HolochainStderr { id: String, source: std::io::Error },
#[snafu(display("Could not serialize response: {}", source))]
SerializeResponse { source: rmp_serde::encode::Error },
}

pub(crate) fn download_logs(id: String) -> Result<DownloadLogsResponse, DownloadLogsError> {
pub(crate) fn download_logs(id: String) -> Result<MessageResponse, DownloadLogsError> {
if !player_config_exists(&id) {
return Err(DownloadLogsError::PlayerNotConfigured { id });
}

let player_dir = get_player_dir(&id);

let lair_stderr = player_dir.join(LAIR_STDERR_LOG_FILENAME);
let lair_stderr =
std::fs::read(&lair_stderr).context(LairStdErr { id: id.clone() })?;
let lair_stderr = std::fs::read(&lair_stderr).context(LairStdErr { id: id.clone() })?;

let conductor_stdout = player_dir.join(CONDUCTOR_STDOUT_LOG_FILENAME);
let conductor_stdout =
std::fs::read(&conductor_stdout).context(HolochainStdout { id: id.clone() })?;

let conductor_stderr = player_dir.join(CONDUCTOR_STDERR_LOG_FILENAME);
let conductor_stderr =
std::fs::read(&conductor_stderr).context(HolochainStderr { id })?;
let conductor_stderr = std::fs::read(&conductor_stderr).context(HolochainStderr { id })?;

Ok(DownloadLogsResponse {
lair_stderr,
conductor_stdout,
conductor_stderr,
})
Ok(MessageResponse::Bytes(
rmp_serde::to_vec_named(&TryCpServerResponse::DownloadLogs(DownloadLogsResponse {
lair_stderr,
conductor_stdout,
conductor_stderr,
}))
.context(SerializeResponse)?,
))
}
6 changes: 4 additions & 2 deletions ts/src/trycp/conductor/conductor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,10 @@ export class TryCpConductor implements IConductor {
id: this.id,
});
assert(response !== TRYCP_SUCCESS_RESPONSE);
assert(typeof response !== "string");
return response as DownloadLogsResponse;
assert(typeof response === "object");
assert("type" in response);
assert(response.type === "download_logs");
return response.data;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions ts/src/trycp/trycp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,7 @@ export class TryCpClient {
}

private processSuccessResponse(response: _TryCpSuccessResponseSeralized) {
if (
response === TRYCP_SUCCESS_RESPONSE ||
typeof response === "string" ||
(typeof response === "object" && response.length === undefined) // Some object, duck-typed to not be a bytearray
) {
if (response === TRYCP_SUCCESS_RESPONSE || typeof response === "string") {
logger.debug(`response ${JSON.stringify(response, null, 4)}\n`);
return response;
}
Expand Down
7 changes: 6 additions & 1 deletion ts/src/trycp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,12 @@ export type TryCpApiResponse =
| TryCpControlResponse
| ApiErrorResponse;

export type TryCpControlResponse = DownloadLogsResponse;
export type TryCpControlResponse = DownloadLogsResponseType;

export interface DownloadLogsResponseType {
type: "download_logs";
data: DownloadLogsResponse;
}

export interface DownloadLogsResponse {
lair_stderr: Uint8Array;
Expand Down

0 comments on commit 3c29a2a

Please sign in to comment.