Skip to content

Commit

Permalink
refactor: remove undici dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Mar 30, 2024
1 parent 848be9d commit df706da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"standard": "^17.1.0",
"tap": "^16.3.7",
"tsd": "^0.30.0",
"typescript": "^5.1.6",
"undici": "^5.28.3"
"typescript": "^5.1.6"
},
"scripts": {
"coverage": "npm run test:unit -- --coverage-report=html",
Expand Down
40 changes: 29 additions & 11 deletions test/issue-288.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,29 @@
const { test } = require('tap')
const Fastify = require('fastify')
const fastifyCompress = require('..')
const { request, setGlobalDispatcher, Agent } = require('undici')
const { request } = require('node:http')

setGlobalDispatcher(new Agent({
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10
}))
function fetch (url) {
return new Promise(function (resolve, reject) {
request(url, function (response) {
// we need to use Buffer.concat to prevent wrong utf8 handling
let body = Buffer.from('')
response.on('data', function (chunk) {
body = Buffer.concat([body, Buffer.from(chunk, 'utf-8')])
})
response.once('error', reject)
response.once('end', function () {
resolve(body.toString())
})
})
.once('error', reject)
.end()
})
}

test('should not corrupt the file content', async (t) => {
t.plan(3)

// provide 2 byte unicode content
const twoByteUnicodeContent = new Array(5_000)
.fill('0')
Expand All @@ -25,13 +40,13 @@ test('should not corrupt the file content', async (t) => {
fastify.register(async (instance, opts) => {
await fastify.register(fastifyCompress)
// compression
instance.get('/issue', async (req, reply) => {
instance.get('/compressed', async (req, reply) => {
return twoByteUnicodeContent
})
})

// no compression
fastify.get('/good', async (req, reply) => {
fastify.get('/raw', async (req, reply) => {
return twoByteUnicodeContent
})

Expand All @@ -40,9 +55,12 @@ test('should not corrupt the file content', async (t) => {
const { port } = fastify.server.address()
const url = `http://localhost:${port}`

const response = await request(`${url}/issue`)
const response2 = await request(`${url}/good`)
const body = await response.body.text()
const body2 = await response2.body.text()
const [body, body2] = await Promise.all([
fetch(`${url}/raw`),
fetch(`${url}/compressed`)
])

t.equal(twoByteUnicodeContent, body)
t.equal(twoByteUnicodeContent, body2)
t.equal(body, body2)
})

0 comments on commit df706da

Please sign in to comment.