-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcompression.test.ts
More file actions
78 lines (64 loc) · 3 KB
/
compression.test.ts
File metadata and controls
78 lines (64 loc) · 3 KB
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
import { expect } from 'chai';
import { compress, Compressor, decompress } from '../../../mongodb';
describe('compression', function () {
describe('.compress()', function () {
context('when the compression library is zstd', function () {
const buffer = Buffer.from('test', 'utf8');
context('when a level is not provided', function () {
const options = { agreedCompressor: 'zstd' as const, zlibCompressionLevel: 0 };
it('compresses the data', async function () {
const data = await compress(options, buffer);
expect(data).to.not.deep.equal(buffer);
expect(await decompress(Compressor.zstd, data)).to.deep.equal(buffer);
});
});
});
context('when the agreed compressor is zlib', () => {
const options = { agreedCompressor: 'zlib' as const, zlibCompressionLevel: 2 };
const input = Buffer.from('test', 'utf8');
it('compresses input with zlib', async () => {
const data = await compress(options, input);
// https://www.rfc-editor.org/rfc/rfc1950 (always leads with 0x78)
expect(data.toString('hex', 0, 1)).to.equal('78');
});
});
context('when the agreed compressor is snappy', () => {
const options = { agreedCompressor: 'snappy' as const, zlibCompressionLevel: 2 };
const input = Buffer.from('test', 'utf8');
it('compresses input with snappy', async () => {
// https://github.com/google/snappy/blob/main/format_description.txt#L18
// Snappy starts with the length of the uncompressed data in bytes
const data = await compress(options, input);
expect(data.toString('hex', 0, 1)).to.equal('04');
});
});
});
describe('.decompress()', function () {
context('when the compression library is zstd', function () {
const buffer = Buffer.from('test', 'utf8');
const options = { agreedCompressor: 'zstd' as const, zlibCompressionLevel: 0 };
it('decompresses the data', async function () {
const data = await compress(options, buffer);
const decompressed = await decompress(Compressor.zstd, data);
expect(decompressed).to.deep.equal(buffer);
});
});
context('when the input has a compressorID corresponding to zlib', () => {
// zlib compressed string "test"
const input = Buffer.from('785e2b492d2e0100045d01c1', 'hex');
it('decompresses input with zlib', async () => {
const data = await decompress(Compressor.zlib, input);
expect(data.toString('utf8')).to.equal('test');
});
});
context('when the agreed compressor is snappy', () => {
// https://github.com/google/snappy/blob/main/format_description.txt#L18
// 0x04 is the size, 0x0c are flags
const input = Buffer.from('040c' + Buffer.from('test', 'utf8').toString('hex'), 'hex');
it('decompresses input with snappy', async () => {
const data = await decompress(Compressor.snappy, input);
expect(data.toString('utf8')).to.equal('test');
});
});
});
});