Skip to content

Commit d75f8e7

Browse files
Merge pull request #212 from kartikaysaxena/fix-util
util: fix util for changes to `channel-credentials.ts` in grpc-js
1 parent 6482d1b commit d75f8e7

File tree

4 files changed

+49
-21
lines changed

4 files changed

+49
-21
lines changed

examples/v1/example.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { v1 } from '@authzed/authzed-node';
2-
const { promises: promiseClient } = client; // access client.promises
32
// set up it on localhost like this:
43
// const client = v1.NewClient('mytokenhere', 'localhost:50051', 1);
54
const client = v1.NewClient('mytokenhere');
5+
const { promises: promiseClient } = client; // access client.promises after instantiating client
66

77
const writeRequest = v1.WriteSchemaRequest.create({
88
schema: `definition test/user {}

js-dist/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"only-run-tests": "vitest"
2323
},
2424
"dependencies": {
25-
"@grpc/grpc-js": "~1.12.5",
25+
"@grpc/grpc-js": "~1.13.1",
2626
"google-protobuf": "^3.15.3"
2727
},
2828
"devDependencies": {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"build-js-client": "tsc --declaration false --outDir js-dist"
3939
},
4040
"dependencies": {
41-
"@grpc/grpc-js": "~1.12.5",
41+
"@grpc/grpc-js": "^1.13.1",
4242
"@protobuf-ts/runtime": "^2.9.6",
4343
"@protobuf-ts/runtime-rpc": "^2.9.6",
4444
"google-protobuf": "^3.21.4"

src/util.ts

+46-18
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,60 @@
11
import * as grpc from "@grpc/grpc-js";
22
import { NextCall } from "@grpc/grpc-js/build/src/client-interceptors.js";
3-
import { ConnectionOptions } from "tls";
3+
import * as net from "net";
4+
import { SecureConnector } from "@grpc/grpc-js/build/src/channel-credentials.js";
5+
import { GrpcUri } from "@grpc/grpc-js/build/src/uri-parser.js";
46

57
// NOTE: Copied from channel-credentials.ts in gRPC Node package because its not exported:
68
// https://github.com/grpc/grpc-node/blob/3106057f5ad8f79a71d2ae411e116ad308a2e835/packages/grpc-js/src/call-credentials.ts#L143
79
class ComposedChannelCredentials extends grpc.ChannelCredentials {
810
constructor(
911
private channelCredentials: KnownInsecureChannelCredentialsImpl,
10-
callCreds: grpc.CallCredentials
12+
private callCreds: grpc.CallCredentials
1113
) {
12-
super(callCreds);
14+
super();
15+
// NOTE: leaving this here to show what changed from the upstream.
16+
/*
17+
if (!channelCredentials._isSecure()) {
18+
throw new Error('Cannot compose insecure credentials');
19+
}
20+
*/
1321
}
1422
compose(callCredentials: grpc.CallCredentials) {
1523
const combinedCallCredentials =
16-
this.callCredentials.compose(callCredentials);
24+
this.callCreds.compose(callCredentials);
1725
return new ComposedChannelCredentials(
1826
this.channelCredentials,
1927
combinedCallCredentials
2028
);
2129
}
22-
23-
_getConnectionOptions(): ConnectionOptions | null {
24-
return this.channelCredentials._getConnectionOptions();
25-
}
2630
_isSecure(): boolean {
2731
return false;
2832
}
33+
34+
// NOTE: this is copied from the InsecureChannelCredentialsImpl class
35+
_createSecureConnector(
36+
channelTarget: GrpcUri,
37+
options: grpc.ChannelOptions,
38+
callCredentials?: grpc.CallCredentials
39+
): SecureConnector {
40+
return {
41+
connect: async (socket: net.Socket) => {
42+
return { socket, secure: false };
43+
},
44+
waitForReady: async () => {},
45+
getCallCredentials: () => callCredentials || this.callCreds,
46+
destroy: () => {}
47+
};
48+
}
49+
2950
_equals(other: grpc.ChannelCredentials): boolean {
3051
if (this === other) {
3152
return true;
3253
}
3354
if (other instanceof ComposedChannelCredentials) {
3455
return (
3556
this.channelCredentials._equals(other.channelCredentials) &&
36-
this.callCredentials._equals(other.callCredentials)
57+
this.callCreds._equals(other.callCreds)
3758
);
3859
} else {
3960
return false;
@@ -44,25 +65,32 @@ class ComposedChannelCredentials extends grpc.ChannelCredentials {
4465
// Create our own known insecure channel creds.
4566
// See https://github.com/grpc/grpc-node/issues/543 for why this is necessary.
4667
class KnownInsecureChannelCredentialsImpl extends grpc.ChannelCredentials {
47-
constructor(callCredentials?: grpc.CallCredentials) {
48-
super(callCredentials);
49-
}
5068

5169
compose(callCredentials: grpc.CallCredentials): grpc.ChannelCredentials {
52-
const combinedCallCredentials =
53-
this.callCredentials.compose(callCredentials);
54-
return new ComposedChannelCredentials(this, combinedCallCredentials);
70+
return new ComposedChannelCredentials(this, callCredentials);
5571
}
5672

57-
_getConnectionOptions(): ConnectionOptions {
58-
return {};
59-
}
6073
_isSecure(): boolean {
6174
return false;
6275
}
6376
_equals(other: grpc.ChannelCredentials): boolean {
6477
return other instanceof KnownInsecureChannelCredentialsImpl;
6578
}
79+
80+
_createSecureConnector(
81+
channelTarget: GrpcUri,
82+
options: grpc.ChannelOptions,
83+
callCredentials: grpc.CallCredentials
84+
): SecureConnector {
85+
return {
86+
connect: async (socket: net.Socket) => {
87+
return { socket, secure: false };
88+
},
89+
waitForReady: async () => {},
90+
getCallCredentials: () => callCredentials,
91+
destroy: () => {}
92+
};
93+
}
6694
}
6795

6896
export enum ClientSecurity {

0 commit comments

Comments
 (0)