-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy patharduino-core-service-client.ts
53 lines (49 loc) · 1.66 KB
/
arduino-core-service-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
44
45
46
47
48
49
50
51
52
53
import { credentials, makeClientConstructor } from '@grpc/grpc-js';
import * as commandsGrpcPb from './cli-protocol/cc/arduino/cli/commands/v1/commands_grpc_pb';
import { ArduinoCoreServiceClient } from './cli-protocol/cc/arduino/cli/commands/v1/commands_grpc_pb';
export interface CreateClientOptions {
/**
* The port to the Arduino CLI daemon.
*/
readonly port: number;
/**
* Defaults to `'localhost'`.
*/
readonly host?: string;
/**
* gRCP channel options. Defaults to `createDefaultChannelOptions` with `'0.0.0'` `appVersion`
*/
readonly channelOptions?: Record<string, unknown>;
}
export function createDefaultChannelOptions(
appVersion = '0.0.0'
): Record<string, unknown> {
return {
'grpc.max_send_message_length': 512 * 1024 * 1024,
'grpc.max_receive_message_length': 512 * 1024 * 1024,
'grpc.primary_user_agent': `arduino-ide/${appVersion}`,
};
}
export function createArduinoCoreServiceClient(
options: CreateClientOptions
): ArduinoCoreServiceClient {
const {
port,
host = 'localhost',
channelOptions = createDefaultChannelOptions(),
} = options;
const address = `${host}:${port}`;
// https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/master/doc/grpcjs_support.md#usage
const ArduinoCoreServiceClient = makeClientConstructor(
// @ts-expect-error: ignore
commandsGrpcPb['cc.arduino.cli.commands.v1.ArduinoCoreService'],
'ArduinoCoreServiceService'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) as any;
const client = new ArduinoCoreServiceClient(
address,
credentials.createInsecure(),
channelOptions
) as ArduinoCoreServiceClient;
return client;
}