better-grpc 0.3.1 → 0.3.2

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
@@ -96,6 +96,16 @@ import { createGrpcClient } from 'better-grpc';
96
96
  const client = await createGrpcClient('localhost:50051', myClientImpl);
97
97
  ```
98
98
 
99
+ By default, SSL/TLS is auto-detected based on the address (SSL for non-localhost addresses). You can explicitly specify credentials:
100
+
101
+ ```typescript
102
+ // Force SSL/TLS connection
103
+ const client = await createGrpcClient('my-server.com:50051', 'ssl', myClientImpl);
104
+
105
+ // Force insecure (plaintext) connection
106
+ const client = await createGrpcClient('my-server.com:50051', 'insecure', myClientImpl);
107
+ ```
108
+
99
109
  You can also override gRPC channel options (defaults are exported as `DEFAULT_OPTIONS`):
100
110
 
101
111
  ```typescript
@@ -293,12 +303,20 @@ Creates and starts a gRPC server. Returns service callables that can be invoked
293
303
 
294
304
  - `createGrpcClient(address: string, ...services: ServiceImpl[])`
295
305
 
296
- Creates and starts a gRPC client using `DEFAULT_OPTIONS`.
306
+ Creates and starts a gRPC client using `DEFAULT_OPTIONS`. SSL/TLS is auto-detected based on the address (SSL for non-localhost addresses).
307
+
308
+ - `createGrpcClient(address: string, credentials: "ssl" | "insecure", ...services: ServiceImpl[])`
309
+
310
+ Creates and starts a gRPC client with explicit credential mode. Use `"ssl"` for TLS or `"insecure"` for plaintext connections.
297
311
 
298
312
  - `createGrpcClient(address: string, options: ChannelOptions, ...services: ServiceImpl[])`
299
313
 
300
314
  Creates and starts a gRPC client with custom gRPC channel options. `DEFAULT_OPTIONS` is exported for easy overrides.
301
315
 
316
+ - `createGrpcClient(address: string, credentials: "ssl" | "insecure", options: ChannelOptions, ...services: ServiceImpl[])`
317
+
318
+ Creates and starts a gRPC client with both explicit credentials and custom channel options.
319
+
302
320
  ### Server-side bidi listen
303
321
 
304
322
  For bidi streams, the server exposes a `.listen()` method to handle incoming connections:
package/dist/index.d.ts CHANGED
@@ -100,6 +100,16 @@ type ServiceNameOf<T extends ServiceImpl<any, any>> = T extends ServiceImpl<infe
100
100
  type ServiceCallable<T, WithListen extends boolean = true> = T extends ServiceImpl<infer S, infer Mode> ? Mode extends "server" ? ClientCallable<InstanceType<S>, WithListen> : ServerCallable<InstanceType<S>> : never;
101
101
 
102
102
  declare const DEFAULT_OPTIONS: ChannelOptions;
103
+ declare function createGrpcClient<T extends ServiceImpl<any, "client">[]>(address: string, credentials: "ssl" | "insecure", ...serviceImpls: T): Promise<{
104
+ [I in T[number] as ServiceNameOf<I>]: ServiceCallable<I>;
105
+ } & {
106
+ clientID: string;
107
+ }>;
108
+ declare function createGrpcClient<T extends ServiceImpl<any, "client">[]>(address: string, credentials: "ssl" | "insecure", grpcOptions: ChannelOptions, ...serviceImpls: T): Promise<{
109
+ [I in T[number] as ServiceNameOf<I>]: ServiceCallable<I>;
110
+ } & {
111
+ clientID: string;
112
+ }>;
103
113
  declare function createGrpcClient<T extends ServiceImpl<any, "client">[]>(address: string, ...serviceImpls: T): Promise<{
104
114
  [I in T[number] as ServiceNameOf<I>]: ServiceCallable<I>;
105
115
  } & {
package/dist/index.js CHANGED
@@ -24226,13 +24226,20 @@ var GrpcClient = class {
24226
24226
  pendingBidi = /* @__PURE__ */ new Map();
24227
24227
  // bidi that is waiting for context
24228
24228
  pendingBidiAck = /* @__PURE__ */ new Map();
24229
- constructor(address, grpcOptions, serviceImpls) {
24229
+ constructor(address, credentialsArg, grpcOptions, serviceImpls) {
24230
24230
  this.clientID = crypto.randomUUID();
24231
24231
  this.address = address;
24232
24232
  this.serviceImpls = serviceImpls;
24233
24233
  this.proto = loadProtoFromString(buildProtoString(serviceImpls));
24234
- const useSSL = !address.includes("localhost") && !address.includes("127.0.0.1") && !address.includes("0.0.0.0");
24235
- const credentials = useSSL ? ChannelCredentials.createSsl() : ChannelCredentials.createInsecure();
24234
+ let credentials;
24235
+ if (credentialsArg === "ssl") {
24236
+ credentials = ChannelCredentials.createSsl();
24237
+ } else if (credentialsArg === "insecure") {
24238
+ credentials = ChannelCredentials.createInsecure();
24239
+ } else {
24240
+ const useSSL = !address.includes("localhost") && !address.includes("127.0.0.1") && !address.includes("0.0.0.0");
24241
+ credentials = useSSL ? ChannelCredentials.createSsl() : ChannelCredentials.createInsecure();
24242
+ }
24236
24243
  this.channel = createChannel(address, credentials, grpcOptions);
24237
24244
  this.clientFactory = createClientFactory();
24238
24245
  }
@@ -24458,12 +24465,39 @@ var DEFAULT_OPTIONS = {
24458
24465
  "grpc.keepalive_timeout_ms": 1e4,
24459
24466
  "grpc.keepalive_permit_without_calls": 1
24460
24467
  };
24461
- async function createGrpcClient(address, grpcOptionsOrServiceImpls, ...serviceImpls) {
24462
- const isServiceImpl = grpcOptionsOrServiceImpls instanceof Object && "type" in grpcOptionsOrServiceImpls && "serviceClass" in grpcOptionsOrServiceImpls;
24463
- const isChannelOptions = !isServiceImpl;
24464
- const grpcOptions = isChannelOptions ? grpcOptionsOrServiceImpls : DEFAULT_OPTIONS;
24465
- const allServiceImpls = isChannelOptions ? serviceImpls : [grpcOptionsOrServiceImpls, ...serviceImpls];
24466
- const grpcClientInstance = new GrpcClient(address, grpcOptions, allServiceImpls);
24468
+ async function createGrpcClient(address, credentialsOrOptionsOrServiceImpl, grpcOptionsOrServiceImpl, ...serviceImpls) {
24469
+ const isServiceImpl = (val) => val instanceof Object && "type" in val && "serviceClass" in val;
24470
+ const isCredentials = (val) => val === "ssl" || val === "insecure";
24471
+ const isChannelOptions = (val) => val instanceof Object && !isServiceImpl(val);
24472
+ let credentials;
24473
+ let grpcOptions = DEFAULT_OPTIONS;
24474
+ let allServiceImpls;
24475
+ if (isCredentials(credentialsOrOptionsOrServiceImpl)) {
24476
+ credentials = credentialsOrOptionsOrServiceImpl;
24477
+ if (grpcOptionsOrServiceImpl === void 0) {
24478
+ allServiceImpls = serviceImpls;
24479
+ } else if (isServiceImpl(grpcOptionsOrServiceImpl)) {
24480
+ allServiceImpls = [grpcOptionsOrServiceImpl, ...serviceImpls];
24481
+ } else {
24482
+ grpcOptions = grpcOptionsOrServiceImpl;
24483
+ allServiceImpls = serviceImpls;
24484
+ }
24485
+ } else if (isServiceImpl(credentialsOrOptionsOrServiceImpl)) {
24486
+ allServiceImpls = [credentialsOrOptionsOrServiceImpl];
24487
+ if (grpcOptionsOrServiceImpl !== void 0) {
24488
+ allServiceImpls.push(grpcOptionsOrServiceImpl, ...serviceImpls);
24489
+ }
24490
+ } else if (isChannelOptions(credentialsOrOptionsOrServiceImpl)) {
24491
+ grpcOptions = credentialsOrOptionsOrServiceImpl;
24492
+ if (grpcOptionsOrServiceImpl !== void 0) {
24493
+ allServiceImpls = [grpcOptionsOrServiceImpl, ...serviceImpls];
24494
+ } else {
24495
+ allServiceImpls = serviceImpls;
24496
+ }
24497
+ } else {
24498
+ allServiceImpls = serviceImpls;
24499
+ }
24500
+ const grpcClientInstance = new GrpcClient(address, credentials, grpcOptions, allServiceImpls);
24467
24501
  grpcClientInstance.start();
24468
24502
  await grpcClientInstance.waitUntilReady();
24469
24503
  grpcClientInstance.watching();