Open History to see the last commit
1import {
2 CLIENT_CAPABILITIES_QUERY_PARAM,
3 ORCHESTRATION_PROTOCOL_QUERY_PARAM,
4 ORCHESTRATION_PROTOCOL_VERSION,
5 SETTINGS_SECRET_REFERENCES_CAPABILITY,
6 type ExecutionEnvironmentDescriptor,
7} from "@t3tools/contracts";
8
9import { ConnectionBlockedError } from "./model.ts";
10
11export function orchestrationProtocolCompatibilityError(
12 descriptor: ExecutionEnvironmentDescriptor,
13): ConnectionBlockedError | null {
14 // Servers shipped before negotiation use the original wire protocol.
15 const serverProtocolVersion = descriptor.orchestrationProtocolVersion ?? 1;
16 if (serverProtocolVersion === ORCHESTRATION_PROTOCOL_VERSION) {
17 return null;
18 }
19 return serverProtocolVersion > ORCHESTRATION_PROTOCOL_VERSION
20 ? new ConnectionBlockedError({
21 reason: "unsupported",
22 detail: `This client is not supported by this server. Update your app or use a compatible release to connect to ${descriptor.label}.`,
23 })
24 : new ConnectionBlockedError({
25 reason: "unsupported",
26 detail: `This client requires a newer server. Update T3 Code on ${descriptor.label} to connect.`,
27 ...(canSelfUpdate(descriptor) ? { serverUpdateRequired: true } : {}),
28 });
29}
30
31function canSelfUpdate(descriptor: ExecutionEnvironmentDescriptor): boolean {
32 const { serverSelfUpdate, desktopAppUpdate } = descriptor.capabilities;
33 return (
34 serverSelfUpdate !== undefined &&
35 (serverSelfUpdate !== "desktop-managed" || desktopAppUpdate === true)
36 );
37}
38
39export function appendOrchestrationProtocol(socketUrl: string): string {
40 const url = new URL(socketUrl);
41 url.searchParams.set(ORCHESTRATION_PROTOCOL_QUERY_PARAM, String(ORCHESTRATION_PROTOCOL_VERSION));
42 url.searchParams.set(CLIENT_CAPABILITIES_QUERY_PARAM, SETTINGS_SECRET_REFERENCES_CAPABILITY);
43 return url.toString();
44}