better-grpc 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -95,6 +95,21 @@ import { createGrpcClient } from 'better-grpc';
95
95
  const client = await createGrpcClient('localhost:50051', myClientImpl);
96
96
  ```
97
97
 
98
+ You can also override gRPC channel options (defaults are exported as `DEFAULT_OPTIONS`):
99
+
100
+ ```typescript
101
+ import { createGrpcClient, DEFAULT_OPTIONS } from 'better-grpc';
102
+
103
+ const client = await createGrpcClient(
104
+ 'localhost:50051',
105
+ {
106
+ ...DEFAULT_OPTIONS,
107
+ 'grpc.keepalive_time_ms': 15000,
108
+ },
109
+ myClientImpl
110
+ );
111
+ ```
112
+
98
113
  ### 5. Make remote calls
99
114
 
100
115
  Now you can call remote functions from both the client and the server.
@@ -216,7 +231,11 @@ Creates and starts a gRPC server.
216
231
 
217
232
  - `createGrpcClient(address: string, ...services: ServiceImpl[])`
218
233
 
219
- Creates and starts a gRPC client.
234
+ Creates and starts a gRPC client using `DEFAULT_OPTIONS`.
235
+
236
+ - `createGrpcClient(address: string, options: ChannelOptions, ...services: ServiceImpl[])`
237
+
238
+ Creates and starts a gRPC client with custom gRPC channel options. `DEFAULT_OPTIONS` is exported for easy overrides.
220
239
 
221
240
  ## Benchmarks
222
241
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { z } from 'zod';
2
+ import { ChannelOptions } from 'nice-grpc';
2
3
 
3
4
  type Context<Meta extends z.ZodObject<any> | undefined> = {
4
5
  metadata: Meta extends z.ZodObject<any> ? z.infer<Meta> : undefined;
@@ -75,12 +76,16 @@ declare function Service<N extends string>(name: N): (abstract new () => {
75
76
  type ServiceNameOf<T extends ServiceImpl<any, any>> = T extends ServiceImpl<infer ServiceClass, any> ? InstanceType<ServiceClass>[typeof ServiceNameTag] : never;
76
77
  type ServiceCallable<T> = T extends ServiceImpl<infer S, infer Mode> ? Mode extends "server" ? ClientFn<InstanceType<S>> : ServerFn<InstanceType<S>, true, true> : never;
77
78
 
79
+ declare const DEFAULT_OPTIONS: ChannelOptions;
78
80
  declare function createGrpcClient<T extends ServiceImpl<any, "client">[]>(address: string, ...serviceImpls: T): Promise<{
79
81
  [I in T[number] as ServiceNameOf<I>]: ServiceCallable<I>;
80
82
  }>;
83
+ declare function createGrpcClient<T extends ServiceImpl<any, "client">[]>(address: string, grpcOptions: ChannelOptions, ...serviceImpls: T): Promise<{
84
+ [I in T[number] as ServiceNameOf<I>]: ServiceCallable<I>;
85
+ }>;
81
86
 
82
87
  declare function createGrpcServer<T extends ServiceImpl<any, "server">[]>(port: number, ...serviceImpls: T): Promise<{
83
88
  [I in T[number] as ServiceNameOf<I>]: ServiceCallable<I>;
84
89
  }>;
85
90
 
86
- export { Service, bidi, client, createGrpcClient, createGrpcServer, server };
91
+ export { DEFAULT_OPTIONS, Service, bidi, client, createGrpcClient, createGrpcServer, server };
package/dist/index.js CHANGED
@@ -24220,19 +24220,13 @@ var GrpcClient = class {
24220
24220
  pendingBidi = /* @__PURE__ */ new Map();
24221
24221
  // bidi that is waiting for context
24222
24222
  pendingBidiAck = /* @__PURE__ */ new Map();
24223
- constructor(address, serviceImpls) {
24223
+ constructor(address, grpcOptions, serviceImpls) {
24224
24224
  this.address = address;
24225
24225
  this.serviceImpls = serviceImpls;
24226
24226
  this.proto = loadProtoFromString(buildProtoString(serviceImpls));
24227
24227
  const useSSL = !address.includes("localhost") && !address.includes("127.0.0.1") && !address.includes("0.0.0.0");
24228
24228
  const credentials = useSSL ? ChannelCredentials.createSsl() : ChannelCredentials.createInsecure();
24229
- this.channel = createChannel(address, credentials, {
24230
- "grpc.max_receive_message_length": 10 * 1024 * 1024,
24231
- "grpc.max_send_message_length": 10 * 1024 * 1024,
24232
- "grpc.keepalive_time_ms": 3e4,
24233
- "grpc.keepalive_timeout_ms": 1e4,
24234
- "grpc.keepalive_permit_without_calls": 1
24235
- });
24229
+ this.channel = createChannel(address, credentials, grpcOptions);
24236
24230
  this.clientFactory = createClientFactory();
24237
24231
  }
24238
24232
  start() {
@@ -24441,8 +24435,22 @@ var GrpcClient = class {
24441
24435
  };
24442
24436
 
24443
24437
  // src/runtime/grpc-client.ts
24444
- async function createGrpcClient(address, ...serviceImpls) {
24445
- const grpcClientInstance = new GrpcClient(address, serviceImpls);
24438
+ var DEFAULT_OPTIONS = {
24439
+ "grpc.max_receive_message_length": 10 * 1024 * 1024,
24440
+ "grpc.max_send_message_length": 10 * 1024 * 1024,
24441
+ "grpc.keepalive_time_ms": 3e4,
24442
+ "grpc.keepalive_timeout_ms": 1e4,
24443
+ "grpc.keepalive_permit_without_calls": 1,
24444
+ "grpc.http2.max_pings_without_data": 0,
24445
+ "grpc.http2.min_time_between_pings_ms": 1e4,
24446
+ "grpc.http2.min_ping_interval_without_data_ms": 1e4
24447
+ };
24448
+ async function createGrpcClient(address, grpcOptionsOrServiceImpls, ...serviceImpls) {
24449
+ const isServiceImpl = grpcOptionsOrServiceImpls instanceof Object && "type" in grpcOptionsOrServiceImpls && "serviceClass" in grpcOptionsOrServiceImpls;
24450
+ const isChannelOptions = !isServiceImpl;
24451
+ const grpcOptions = isChannelOptions ? grpcOptionsOrServiceImpls : DEFAULT_OPTIONS;
24452
+ const allServiceImpls = isChannelOptions ? serviceImpls : [grpcOptionsOrServiceImpls, ...serviceImpls];
24453
+ const grpcClientInstance = new GrpcClient(address, grpcOptions, allServiceImpls);
24446
24454
  grpcClientInstance.start();
24447
24455
  await grpcClientInstance.waitUntilReady();
24448
24456
  grpcClientInstance.watching();
@@ -24710,6 +24718,6 @@ long/umd/index.js:
24710
24718
  *)
24711
24719
  */
24712
24720
 
24713
- export { Service, bidi, client, createGrpcClient, createGrpcServer, server };
24721
+ export { DEFAULT_OPTIONS, Service, bidi, client, createGrpcClient, createGrpcServer, server };
24714
24722
  //# sourceMappingURL=index.js.map
24715
24723
  //# sourceMappingURL=index.js.map