Skip to content

Commit 66f972c

Browse files
committed
grpc-js: Implement unbind
1 parent 779e970 commit 66f972c

File tree

5 files changed

+655
-329
lines changed

5 files changed

+655
-329
lines changed

packages/grpc-js/src/server-credentials.ts

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface KeyCertPair {
2626
export abstract class ServerCredentials {
2727
abstract _isSecure(): boolean;
2828
abstract _getSettings(): SecureServerOptions | null;
29+
abstract _equals(other: ServerCredentials): boolean;
2930

3031
static createInsecure(): ServerCredentials {
3132
return new InsecureServerCredentials();
@@ -48,8 +49,8 @@ export abstract class ServerCredentials {
4849
throw new TypeError('checkClientCertificate must be a boolean');
4950
}
5051

51-
const cert = [];
52-
const key = [];
52+
const cert: Buffer[] = [];
53+
const key: Buffer[] = [];
5354

5455
for (let i = 0; i < keyCertPairs.length; i++) {
5556
const pair = keyCertPairs[i];
@@ -71,7 +72,7 @@ export abstract class ServerCredentials {
7172
}
7273

7374
return new SecureServerCredentials({
74-
ca: rootCerts || getDefaultRootsData() || undefined,
75+
ca: rootCerts ?? getDefaultRootsData() ?? undefined,
7576
cert,
7677
key,
7778
requestCert: checkClientCertificate,
@@ -88,6 +89,10 @@ class InsecureServerCredentials extends ServerCredentials {
8889
_getSettings(): null {
8990
return null;
9091
}
92+
93+
_equals(other: ServerCredentials): boolean {
94+
return other instanceof InsecureServerCredentials;
95+
}
9196
}
9297

9398
class SecureServerCredentials extends ServerCredentials {
@@ -105,4 +110,82 @@ class SecureServerCredentials extends ServerCredentials {
105110
_getSettings(): SecureServerOptions {
106111
return this.options;
107112
}
113+
114+
/**
115+
* Checks equality by checking the options that are actually set by
116+
* createSsl.
117+
* @param other
118+
* @returns
119+
*/
120+
_equals(other: ServerCredentials): boolean {
121+
if (this === other) {
122+
return true;
123+
}
124+
if (!(other instanceof SecureServerCredentials)) {
125+
return false;
126+
}
127+
// options.ca equality check
128+
if (Buffer.isBuffer(this.options.ca) && Buffer.isBuffer(other.options.ca)) {
129+
if (!this.options.ca.equals(other.options.ca)) {
130+
return false;
131+
}
132+
} else {
133+
if (this.options.ca !== other.options.ca) {
134+
return false;
135+
}
136+
}
137+
// options.cert equality check
138+
if (Array.isArray(this.options.cert) && Array.isArray(other.options.cert)) {
139+
if (this.options.cert.length !== other.options.cert.length) {
140+
return false;
141+
}
142+
for (let i = 0; i < this.options.cert.length; i++) {
143+
const thisCert = this.options.cert[i];
144+
const otherCert = other.options.cert[i];
145+
if (Buffer.isBuffer(thisCert) && Buffer.isBuffer(otherCert)) {
146+
if (!thisCert.equals(otherCert)) {
147+
return false;
148+
}
149+
} else {
150+
if (thisCert !== otherCert) {
151+
return false;
152+
}
153+
}
154+
}
155+
} else {
156+
if (this.options.cert !== other.options.cert) {
157+
return false;
158+
}
159+
}
160+
// options.key equality check
161+
if (Array.isArray(this.options.key) && Array.isArray(other.options.key)) {
162+
if (this.options.key.length !== other.options.key.length) {
163+
return false;
164+
}
165+
for (let i = 0; i < this.options.key.length; i++) {
166+
const thisKey = this.options.key[i];
167+
const otherKey = other.options.key[i];
168+
if (Buffer.isBuffer(thisKey) && Buffer.isBuffer(otherKey)) {
169+
if (!thisKey.equals(otherKey)) {
170+
return false;
171+
}
172+
} else {
173+
if (thisKey !== otherKey) {
174+
return false;
175+
}
176+
}
177+
}
178+
} else {
179+
if (this.options.key !== other.options.key) {
180+
return false;
181+
}
182+
}
183+
// options.requestCert equality check
184+
if (this.options.requestCert !== other.options.requestCert) {
185+
return false;
186+
}
187+
/* ciphers is derived from a value that is constant for the process, so no
188+
* equality check is needed. */
189+
return true;
190+
}
108191
}

0 commit comments

Comments
 (0)