-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
compression.ts
130 lines (118 loc) · 3.57 KB
/
compression.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as zlib from 'zlib';
import { LEGACY_HELLO_COMMAND } from '../../constants';
import { PKG_VERSION, Snappy, ZStandard } from '../../deps';
import { MongoDecompressionError, MongoInvalidArgumentError } from '../../error';
import type { Callback } from '../../utils';
import type { OperationDescription } from '../message_stream';
/** @public */
export const Compressor = Object.freeze({
none: 0,
snappy: 1,
zlib: 2,
zstd: 3
} as const);
/** @public */
export type Compressor = typeof Compressor[CompressorName];
/** @public */
export type CompressorName = keyof typeof Compressor;
export const uncompressibleCommands = new Set([
LEGACY_HELLO_COMMAND,
'saslStart',
'saslContinue',
'getnonce',
'authenticate',
'createUser',
'updateUser',
'copydbSaslStart',
'copydbgetnonce',
'copydb'
]);
const MAX_COMPRESSOR_ID = 3;
const ZSTD_COMPRESSION_LEVEL = 3;
// Facilitate compressing a message using an agreed compressor
export function compress(
self: { options: OperationDescription & zlib.ZlibOptions },
dataToBeCompressed: Buffer,
callback: Callback<Buffer>
): void {
const zlibOptions = {} as zlib.ZlibOptions;
switch (self.options.agreedCompressor) {
case 'snappy': {
if ('kModuleError' in Snappy) {
return callback(Snappy['kModuleError']);
}
if (Snappy[PKG_VERSION].major <= 6) {
Snappy.compress(dataToBeCompressed, callback);
} else {
Snappy.compress(dataToBeCompressed).then(
buffer => callback(undefined, buffer),
error => callback(error)
);
}
break;
}
case 'zlib':
// Determine zlibCompressionLevel
if (self.options.zlibCompressionLevel) {
zlibOptions.level = self.options.zlibCompressionLevel;
}
zlib.deflate(dataToBeCompressed, zlibOptions, callback as zlib.CompressCallback);
break;
case 'zstd':
if ('kModuleError' in ZStandard) {
return callback(ZStandard['kModuleError']);
}
ZStandard.compress(dataToBeCompressed, ZSTD_COMPRESSION_LEVEL).then(
buffer => callback(undefined, buffer),
error => callback(error)
);
break;
default:
throw new MongoInvalidArgumentError(
`Unknown compressor ${self.options.agreedCompressor} failed to compress`
);
}
}
// Decompress a message using the given compressor
export function decompress(
compressorID: Compressor,
compressedData: Buffer,
callback: Callback<Buffer>
): void {
if (compressorID < 0 || compressorID > MAX_COMPRESSOR_ID) {
throw new MongoDecompressionError(
`Server sent message compressed using an unsupported compressor. (Received compressor ID ${compressorID})`
);
}
switch (compressorID) {
case Compressor.snappy: {
if ('kModuleError' in Snappy) {
return callback(Snappy['kModuleError']);
}
if (Snappy[PKG_VERSION].major <= 6) {
Snappy.uncompress(compressedData, { asBuffer: true }, callback);
} else {
Snappy.uncompress(compressedData, { asBuffer: true }).then(
buffer => callback(undefined, buffer),
error => callback(error)
);
}
break;
}
case Compressor.zstd: {
if ('kModuleError' in ZStandard) {
return callback(ZStandard['kModuleError']);
}
ZStandard.decompress(compressedData).then(
buffer => callback(undefined, buffer),
error => callback(error)
);
break;
}
case Compressor.zlib:
zlib.inflate(compressedData, callback as zlib.CompressCallback);
break;
default:
callback(undefined, compressedData);
}
}