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

compatibility.ts

54 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`This client is not supported by this server. Update your app or use a compatible release to connect to ${{ host: descriptor.label }}.`,
27 ),
28 })
29 : new ConnectionBlockedError({
30 reason: "unsupported",
31 ...translatableDetail(
32 msg`This client requires a newer server. Update T3 Code on ${{ host: descriptor.label }} to connect.`,
33 ),
34 ...(canSelfUpdate(descriptor) ? { serverUpdateRequired: true } : {}),
35 });
37​
38function canSelfUpdate(descriptor: ExecutionEnvironmentDescriptor): boolean {
39 const { serverSelfUpdate, desktopAppUpdate } = descriptor.capabilities;
40 return (
41 serverSelfUpdate !== undefined &&
42 (serverSelfUpdate !== "desktop-managed" || desktopAppUpdate === true)
43 );
45​
46export function appendOrchestrationProtocol(socketUrl: string): string {
47 const url = new URL(socketUrl);
48 url.searchParams.set(ORCHESTRATION_PROTOCOL_QUERY_PARAM, String(ORCHESTRATION_PROTOCOL_VERSION));
49 url.searchParams.set(
50 CLIENT_CAPABILITIES_QUERY_PARAM,
51 [SETTINGS_SECRET_REFERENCES_CAPABILITY, HTTP_USAGE_LIMIT_SOURCES_CAPABILITY].join(","),
52 );
53 return url.toString();