2024-04-25 04:47:33 +03:00
|
|
|
import { inflateSync } from 'node:zlib'
|
2023-11-04 06:44:18 +03:00
|
|
|
|
2024-09-05 03:34:13 +03:00
|
|
|
import { utf8 } from '@fuman/utils'
|
2024-12-03 09:55:37 +03:00
|
|
|
import { beforeAll, describe, expect, it } from 'vitest'
|
2023-11-29 20:31:18 +03:00
|
|
|
|
2024-03-01 01:52:17 +03:00
|
|
|
import { __getWasm, deflateMaxSize } from '../src/index.js'
|
2024-08-13 04:53:07 +03:00
|
|
|
|
2024-03-01 01:52:17 +03:00
|
|
|
import { initWasm } from './init.js'
|
2023-11-04 06:44:18 +03:00
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
beforeAll(async () => {
|
2024-03-01 01:52:17 +03:00
|
|
|
await initWasm()
|
2023-11-04 06:44:18 +03:00
|
|
|
})
|
|
|
|
|
2023-11-29 20:31:18 +03:00
|
|
|
function inflateSyncWrap(data: Uint8Array) {
|
2024-04-25 04:47:33 +03:00
|
|
|
if (import.meta.env.TEST_ENV === 'browser' || import.meta.env.TEST_ENV === 'deno') {
|
2023-11-29 20:31:18 +03:00
|
|
|
// @ts-expect-error fucking crutch because @jspm/core uses Buffer.isBuffer for some reason
|
|
|
|
data._isBuffer = true
|
|
|
|
|
|
|
|
return new Uint8Array(inflateSync(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
return inflateSync(data)
|
|
|
|
}
|
|
|
|
|
2023-11-04 06:44:18 +03:00
|
|
|
describe('zlib deflate', () => {
|
|
|
|
it('should add zlib headers', () => {
|
2024-09-05 03:34:13 +03:00
|
|
|
const res = deflateMaxSize(utf8.encoder.encode('hello world'), 100)
|
2023-11-04 06:44:18 +03:00
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(res).not.toBeNull()
|
2024-08-13 04:53:07 +03:00
|
|
|
expect(res!.slice(0, 2)).toEqual(new Uint8Array([0x78, 0x9C]))
|
2023-11-04 06:44:18 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return null if compressed data is larger than size', () => {
|
2024-09-05 03:34:13 +03:00
|
|
|
const res = deflateMaxSize(utf8.encoder.encode('hello world'), 1)
|
2023-11-04 06:44:18 +03:00
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(res).toBeNull()
|
2023-11-04 06:44:18 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should correctly deflate', () => {
|
|
|
|
const data = Array.from({ length: 1000 }, () => 'a').join('')
|
2024-09-05 03:34:13 +03:00
|
|
|
const res = deflateMaxSize(utf8.encoder.encode(data), 100)
|
2023-11-04 06:44:18 +03:00
|
|
|
|
2023-11-09 00:20:43 +03:00
|
|
|
expect(res).not.toBeNull()
|
|
|
|
expect(res!.length).toBeLessThan(100)
|
2024-09-05 03:34:13 +03:00
|
|
|
expect(inflateSyncWrap(res!)).toEqual(utf8.encoder.encode(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('')
|
2024-09-05 03:34:13 +03:00
|
|
|
const deflated = deflateMaxSize(utf8.encoder.encode(data), 100)
|
2023-11-04 06:44:18 +03:00
|
|
|
|
2023-11-29 20:31:18 +03:00
|
|
|
const res = inflateSyncWrap(deflated!)
|
2023-11-04 06:44:18 +03:00
|
|
|
|
2024-09-05 03:34:13 +03:00
|
|
|
expect(utf8.decoder.decode(res)).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
|
|
|
})
|
|
|
|
})
|