2023-11-04 06:44:18 +03:00
|
|
|
/* eslint-disable no-restricted-globals */
|
2023-11-09 00:20:43 +03:00
|
|
|
import { beforeAll, describe, expect, it } from 'vitest'
|
2023-11-04 06:44:18 +03:00
|
|
|
import { gzipSync } from 'zlib'
|
|
|
|
|
|
|
|
import { __getWasm, gunzip, initAsync } from '../src/index.js'
|
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
beforeAll(async () => {
|
2023-11-04 06:44:18 +03:00
|
|
|
await initAsync()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('gunzip', () => {
|
|
|
|
it('should correctly read zlib headers', () => {
|
|
|
|
const wasm = __getWasm()
|
|
|
|
const data = gzipSync(Buffer.from('hello world'))
|
|
|
|
|
|
|
|
const inputPtr = wasm.__malloc(data.length)
|
|
|
|
new Uint8Array(wasm.memory.buffer).set(data, inputPtr)
|
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(wasm.libdeflate_gzip_get_output_size(inputPtr, data.length)).toEqual(11)
|
2023-11-04 06:44:18 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should correctly inflate', () => {
|
|
|
|
const data = Array.from({ length: 1000 }, () => 'a').join('')
|
|
|
|
const res = gzipSync(Buffer.from(data))
|
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res.length).toBeLessThan(100)
|
|
|
|
expect(gunzip(res)).toEqual(new Uint8Array(Buffer.from(data)))
|
2023-11-04 06:44:18 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not leak memory', () => {
|
|
|
|
const memSize = __getWasm().memory.buffer.byteLength
|
|
|
|
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
const data = Array.from({ length: 1000 }, () => 'a').join('')
|
|
|
|
const deflated = gzipSync(Buffer.from(data))
|
|
|
|
|
|
|
|
const res = gunzip(deflated)
|
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(Buffer.from(res).toString()).toEqual(data)
|
2023-11-04 06:44:18 +03:00
|
|
|
}
|
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(__getWasm().memory.buffer.byteLength).toEqual(memSize)
|
2023-11-04 06:44:18 +03:00
|
|
|
})
|
|
|
|
})
|