Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

compatibility.ts

59 lines2.1 KB

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