Skip to content

Commit dc9752a

Browse files
committed
change CompressionAlgorithms to an enum
1 parent 9150bdf commit dc9752a

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

packages/grpc-js/src/channel-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface ChannelOptions {
3838
'grpc.enable_http_proxy'?: number;
3939
'grpc.http_connect_target'?: string;
4040
'grpc.http_connect_creds'?: string;
41-
'grpc.default_compression_algorithm'?: keyof typeof CompressionAlgorithms;
41+
'grpc.default_compression_algorithm'?: CompressionAlgorithms;
4242
'grpc-node.max_session_memory'?: number;
4343
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4444
[key: string]: any;

packages/grpc-js/src/compression-algorithms.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*
1616
*/
1717

18-
export const CompressionAlgorithms = {
19-
0: 'identity',
20-
1: 'deflate',
21-
2: 'gzip'
22-
} as const;
18+
export enum CompressionAlgorithms {
19+
identity = 0,
20+
deflate = 1,
21+
gzip = 2
22+
};

packages/grpc-js/src/compression-filter.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ import { BaseFilter, Filter, FilterFactory } from './filter';
2626
import * as logging from './logging';
2727
import { Metadata, MetadataValue } from './metadata';
2828

29-
const CompressionAlgorithKeys = new Set(Object.keys(CompressionAlgorithms));
30-
31-
const isCompressionAlgorithmKey = (key: number | undefined): key is keyof typeof CompressionAlgorithms => {
32-
return typeof key === 'number' && CompressionAlgorithKeys.has(key.toString());
29+
const isCompressionAlgorithmKey = (key: number): key is CompressionAlgorithms => {
30+
return typeof key === 'number' && typeof CompressionAlgorithms[key] === 'string';
3331
}
3432

35-
type CompressionAlgorithm = (typeof CompressionAlgorithms)[keyof typeof CompressionAlgorithms];
33+
type CompressionAlgorithm = keyof typeof CompressionAlgorithms;
3634

3735
type SharedCompressionFilterConfig = {
3836
serverSupportedEncodingHeader?: string;
@@ -191,7 +189,7 @@ export class CompressionFilter extends BaseFilter implements Filter {
191189
const compressionAlgorithmKey = channelOptions['grpc.default_compression_algorithm'];
192190
if (compressionAlgorithmKey !== undefined) {
193191
if (isCompressionAlgorithmKey(compressionAlgorithmKey)) {
194-
const clientSelectedEncoding = CompressionAlgorithms[compressionAlgorithmKey];
192+
const clientSelectedEncoding = CompressionAlgorithms[compressionAlgorithmKey] as CompressionAlgorithm;
195193
const serverSupportedEncodings = sharedFilterConfig.serverSupportedEncodingHeader?.split(',');
196194
/**
197195
* There are two possible situations here:

0 commit comments

Comments
 (0)