@@ -69,6 +69,37 @@ describe("Brotli-wasm", () => {
69
69
expect ( result ) . to . equal ( input ) ;
70
70
} ) ;
71
71
72
+ it ( "can streamingly compress data" , ( ) => {
73
+ const input = Buffer . from ( "Test input data" ) ;
74
+ const input1 = input . slice ( 0 , input . length / 2 ) ;
75
+ const input2 = input . slice ( input . length / 2 ) ;
76
+ const stream = new brotli . CompressStream ( ) ;
77
+ const output1 = stream . compress ( input1 , 100 ) ;
78
+ expect ( stream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
79
+ const output2 = stream . compress ( input2 , 100 ) ;
80
+ expect ( stream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
81
+ const output3 = stream . compress ( undefined , 100 ) ;
82
+ expect ( stream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . ResultSuccess ) ;
83
+ expect ( Buffer . concat ( [ output1 , output2 , output3 ] ) . toString ( 'base64' ) ) . to . equal ( 'Gw4A+KWpyubolCCjVAjmxJ4D' ) ;
84
+ } ) ;
85
+
86
+ it ( "can streamingly compress data with a different quality setting" , ( ) => {
87
+ const input = Buffer . from ( "Test input data" ) ;
88
+ const input1 = input . slice ( 0 , input . length / 2 ) ;
89
+ const input2 = input . slice ( input . length / 2 ) ;
90
+ const quality = 1 ;
91
+ const stream = new brotli . CompressStream ( quality ) ;
92
+ const output1 = stream . compress ( input1 , 100 ) ;
93
+ expect ( stream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
94
+ const output2 = stream . compress ( input2 , 100 ) ;
95
+ expect ( stream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
96
+ const output3 = stream . compress ( undefined , 100 ) ;
97
+ expect ( stream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . ResultSuccess ) ;
98
+ // It will be CwOAVGVzdCBpbjgACHB1dCBkYXRhAw== which is different.
99
+ // But it can still be decompressed back to the original string.
100
+ // expect(Buffer.concat([output1, output2, output3]).toString('base64')).to.equal('CweAVGVzdCBpbnB1dCBkYXRhAw==');
101
+ } ) ;
102
+
72
103
it ( "can streamingly decompress data" , ( ) => {
73
104
// Generated with: echo -n '$CONTENT' | brotli --stdout - | base64
74
105
const input = Buffer . from ( 'GxoAABypU587dC0k9ianQOgqjS32iUTcCA==' , 'base64' ) ;
@@ -82,6 +113,21 @@ describe("Brotli-wasm", () => {
82
113
expect ( Buffer . concat ( [ output1 , output2 ] ) . toString ( 'utf8' ) ) . to . equal ( 'Brotli brotli brotli brotli' ) ;
83
114
} ) ;
84
115
116
+ it ( "does not fail when streamingly compressing with an illegal quality value" , ( ) => {
117
+ const input = Buffer . from ( "Test input data" ) ;
118
+ const input1 = input . slice ( 0 , input . length / 2 ) ;
119
+ const input2 = input . slice ( input . length / 2 ) ;
120
+ const quality = 12 ;
121
+ const stream = new brotli . CompressStream ( quality ) ;
122
+ const output1 = stream . compress ( input1 , 100 ) ;
123
+ expect ( stream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
124
+ const output2 = stream . compress ( input2 , 100 ) ;
125
+ expect ( stream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
126
+ const output3 = stream . compress ( undefined , 100 ) ;
127
+ expect ( stream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . ResultSuccess ) ;
128
+ expect ( Buffer . concat ( [ output1 , output2 , output3 ] ) . toString ( 'base64' ) ) . to . equal ( 'Gw4A+KWpyubolCCjVAjmxJ4D' ) ;
129
+ } ) ;
130
+
85
131
it ( "cleanly fails when streamingly decompressing garbage" , ( ) => {
86
132
const input = Buffer . from ( "This is not brotli data, it's just a string" ) ;
87
133
const stream = new brotli . DecompressStream ( ) ;
@@ -90,4 +136,57 @@ describe("Brotli-wasm", () => {
90
136
) . to . throw ( 'Brotli streaming decompress failed' ) ;
91
137
expect ( stream . result ( ) ) . to . lt ( 0 ) ;
92
138
} ) ;
139
+
140
+ it ( "can compress & decompress back to the original result" , ( ) => {
141
+ const s = "Some thrilling text I urgently need to compress" ;
142
+ const encInput = Buffer . from ( s ) ;
143
+ const encInput1 = encInput . slice ( 0 , encInput . length / 2 ) ;
144
+ const encInput2 = encInput . slice ( encInput . length / 2 ) ;
145
+ const encStream = new brotli . CompressStream ( ) ;
146
+ const encOutput1 = encStream . compress ( encInput1 , 100 ) ;
147
+ expect ( encStream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
148
+ const encOutput2 = encStream . compress ( encInput2 , 100 ) ;
149
+ expect ( encStream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
150
+ const encOutput3 = encStream . compress ( undefined , 100 ) ;
151
+ expect ( encStream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . ResultSuccess ) ;
152
+ const encOutput = Buffer . concat ( [ encOutput1 , encOutput2 , encOutput3 ] ) ;
153
+
154
+ const decInput1 = encOutput . slice ( 0 , encOutput . length / 2 ) ;
155
+ const decInput2 = encOutput . slice ( encOutput . length / 2 ) ;
156
+ const decStream = new brotli . DecompressStream ( ) ;
157
+ const decOutput1 = decStream . decompress ( decInput1 , 100 ) ;
158
+ expect ( decStream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
159
+ const decOutput2 = decStream . decompress ( decInput2 , 100 ) ;
160
+ expect ( decStream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . ResultSuccess ) ;
161
+ const decOutput = Buffer . concat ( [ decOutput1 , decOutput2 ] ) ;
162
+
163
+ expect ( decOutput . toString ( 'utf8' ) ) . to . equal ( s ) ;
164
+ } ) ;
165
+
166
+ it ( "can compress & decompress back to the original result with a different quality setting" , ( ) => {
167
+ const s = "Some thrilling text I urgently need to compress" ;
168
+ const encInput = Buffer . from ( s ) ;
169
+ const encInput1 = encInput . slice ( 0 , encInput . length / 2 ) ;
170
+ const encInput2 = encInput . slice ( encInput . length / 2 ) ;
171
+ const quality = 3 ;
172
+ const encStream = new brotli . CompressStream ( quality ) ;
173
+ const encOutput1 = encStream . compress ( encInput1 , 100 ) ;
174
+ expect ( encStream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
175
+ const encOutput2 = encStream . compress ( encInput2 , 100 ) ;
176
+ expect ( encStream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
177
+ const encOutput3 = encStream . compress ( undefined , 100 ) ;
178
+ expect ( encStream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . ResultSuccess ) ;
179
+ const encOutput = Buffer . concat ( [ encOutput1 , encOutput2 , encOutput3 ] ) ;
180
+
181
+ const decInput1 = encOutput . slice ( 0 , encOutput . length / 2 ) ;
182
+ const decInput2 = encOutput . slice ( encOutput . length / 2 ) ;
183
+ const decStream = new brotli . DecompressStream ( ) ;
184
+ const decOutput1 = decStream . decompress ( decInput1 , 100 ) ;
185
+ expect ( decStream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . NeedsMoreInput ) ;
186
+ const decOutput2 = decStream . decompress ( decInput2 , 100 ) ;
187
+ expect ( decStream . result ( ) ) . to . equal ( brotli . BrotliStreamResult . ResultSuccess ) ;
188
+ const decOutput = Buffer . concat ( [ decOutput1 , decOutput2 ] ) ;
189
+
190
+ expect ( decOutput . toString ( 'utf8' ) ) . to . equal ( s ) ;
191
+ } ) ;
93
192
} ) ;
0 commit comments