Skip to content

Commit

Permalink
[breadboard/remote] Implement ProxyClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
dglazkov committed Nov 23, 2023
1 parent 2b274f9 commit 1af0798
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion seeds/breadboard/src/remote/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@

import { Board } from "../board.js";
import { callHandler } from "../handler.js";
import { NodeHandlers } from "../types.js";
import {
InputValues,
NodeDescriptor,
NodeHandlers,
OutputValues,
} from "../types.js";
import {
AnyProxyRequestMessage,
AnyProxyResponseMessage,
ClientTransport,
ServerTransport,
} from "./protocol.js";

Expand Down Expand Up @@ -73,3 +79,45 @@ export class ProxyServer {
}
}
}

type ProxyClientTransport = ClientTransport<
AnyProxyRequestMessage,
AnyProxyResponseMessage
>;

export class ProxyClient {
#transport: ProxyClientTransport;

constructor(transport: ProxyClientTransport) {
this.#transport = transport;
}

async proxy(
node: NodeDescriptor,
inputs: InputValues
): Promise<OutputValues> {
const stream = this.#transport.createClientStream();
const writer = stream.writableRequests.getWriter();
const reader = stream.readableResponses.getReader();

writer.write(["proxy", { node, inputs }]);
writer.close();

const result = await reader.read();
if (result.done)
throw new Error("Unexpected proxy failure: empty response.");

const [type] = result.value;
if (type === "proxy") {
const [, { outputs }] = result.value;
return outputs;
} else if (type === "error") {
const [, { error }] = result.value;
throw new Error(error);
} else {
throw new Error(
`Unexpected proxy failure: unknown response type "${type}".`
);
}
}
}

0 comments on commit 1af0798

Please sign in to comment.