-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
43 lines (41 loc) · 1.35 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import * as grpc from "@grpc/grpc-js";
import * as fabric from "./protos/io/defang/v1/fabric_grpc_pb";
import { Status } from "@grpc/grpc-js/build/src/constants";
function hasPort(url: string): boolean {
return /:\d+$/.test(url);
}
export function newClient(
fabricDns: string,
accessToken: string
): fabric.FabricControllerClient {
const serviceConfig: grpc.ServiceConfig = {
loadBalancingConfig: [],
methodConfig: [
{
name: [{ service: "io.defang.v1.FabricController" }],
retryPolicy: {
maxAttempts: 5,
initialBackoff: "1s",
maxBackoff: "10s",
backoffMultiplier: 2,
retryableStatusCodes: [Status.UNAVAILABLE, Status.INTERNAL],
},
},
],
};
const noTenant = fabricDns.replace(/^.*@/, "");
const withPort = hasPort(noTenant) ? noTenant : `${noTenant}:443`;
return new fabric.FabricControllerClient(
withPort,
grpc.credentials.combineChannelCredentials(
grpc.credentials.createSsl(),
grpc.credentials.createFromMetadataGenerator((_, callback) => {
const metadata = new grpc.Metadata();
// TODO: automatically generate a new token once it expires
metadata.set("authorization", "Bearer " + accessToken);
callback(null, metadata);
})
),
{ "grpc.service_config": JSON.stringify(serviceConfig) }
);
}