1
1
import { forIn } from 'lodash'
2
2
import { unzip } from 'gzip-js'
3
3
import { decompress as decompressFzstd } from 'fzstd'
4
+ // @ts -ignore
4
5
import { decompress as decompressLz4 } from 'lz4js'
5
6
import { decompress as decompressSnappy } from '@stablelib/snappy'
7
+ // @ts -ignore
8
+ import { decompress as decompressBrotli } from 'brotli-unicode/js'
9
+ import { inflate } from 'pako'
6
10
import { COMPRESSOR_MAGIC_SYMBOLS , ICompressorMagicSymbols , KeyValueCompressor } from 'uiSrc/constants'
7
11
import { RedisResponseBuffer , RedisString } from 'uiSrc/slices/interfaces'
8
- import { anyToBuffer , isEqualBuffers , Nullable } from 'uiSrc/utils'
12
+ import { anyToBuffer , bufferToString , bufferToUint8Array , isEqualBuffers , Nullable } from 'uiSrc/utils'
9
13
10
14
const decompressingBuffer = (
11
15
reply : RedisResponseBuffer ,
12
16
compressorInit : Nullable < KeyValueCompressor > = null ,
13
17
) : { value : RedisString , compressor : Nullable < KeyValueCompressor > , isCompressed : boolean } => {
14
18
const compressorByValue : Nullable < KeyValueCompressor > = getCompressor ( reply )
15
19
const compressor = compressorInit === compressorByValue
16
- || ( ! compressorByValue && compressorInit === KeyValueCompressor . SNAPPY )
20
+ || ( ! compressorByValue && compressorInit )
17
21
? compressorInit
18
22
: null
19
23
@@ -56,6 +60,26 @@ const decompressingBuffer = (
56
60
isCompressed : ! isEqualBuffers ( value , reply ) ,
57
61
}
58
62
}
63
+ case KeyValueCompressor . Brotli : {
64
+ const value = anyToBuffer ( decompressBrotli ( bufferToString ( reply ) ) )
65
+
66
+ return {
67
+ value,
68
+ compressor,
69
+ isCompressed : ! isEqualBuffers ( value , reply ) ,
70
+ }
71
+ }
72
+ case KeyValueCompressor . PHPGZCompress : {
73
+ const decompressedValue = inflate ( bufferToUint8Array ( reply ) )
74
+ if ( ! decompressedValue ) return { value : reply , compressor : null , isCompressed : false }
75
+
76
+ const value = anyToBuffer ( decompressedValue )
77
+ return {
78
+ value,
79
+ compressor,
80
+ isCompressed : ! isEqualBuffers ( value , reply ) ,
81
+ }
82
+ }
59
83
default : {
60
84
return { value : reply , compressor : null , isCompressed : false }
61
85
}
@@ -73,10 +97,9 @@ const getCompressor = (reply: RedisResponseBuffer): Nullable<KeyValueCompressor>
73
97
COMPRESSOR_MAGIC_SYMBOLS ,
74
98
( magicSymbols : string , compressorName : string ) => {
75
99
if (
76
- replyStart . startsWith ( magicSymbols )
100
+ magicSymbols
101
+ && replyStart . startsWith ( magicSymbols )
77
102
&& replyStart . length > magicSymbols . length
78
- // no magic symbols for SNAPPY
79
- && compressorName !== KeyValueCompressor . SNAPPY
80
103
) {
81
104
compressor = compressorName as KeyValueCompressor
82
105
return false // break loop
0 commit comments