From 74f3afb32d3c5f338aca1f56945dc4df46c2b9c4 Mon Sep 17 00:00:00 2001 From: KaKa Date: Sun, 31 Mar 2024 00:16:25 +0800 Subject: [PATCH] refactor: remove `node-fetch` dev dependency --- package.json | 1 - test/regression/issue-288.test.js | 31 ++++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 464f429..f4be429 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,6 @@ "adm-zip": "^0.5.10", "fastify": "^4.19.2", "jsonstream": "^1.0.3", - "node-fetch": "^2", "standard": "^17.1.0", "tap": "^16.3.7", "tsd": "^0.30.0", diff --git a/test/regression/issue-288.test.js b/test/regression/issue-288.test.js index a4e4725..beee851 100644 --- a/test/regression/issue-288.test.js +++ b/test/regression/issue-288.test.js @@ -3,9 +3,29 @@ const { test } = require('tap') const Fastify = require('fastify') const fastifyCompress = require('../..') -const fetch = require('node-fetch') +const { request } = require('node:http') + +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(2) + // provide 2 byte unicode content const twoByteUnicodeContent = new Array(5_000) .fill('0') @@ -32,10 +52,11 @@ test('should not corrupt the file content', async (t) => { const address = await fastify.listen({ port: 0, host: '127.0.0.1' }) - const response1 = await fetch(`${address}/compress`) - const response2 = await fetch(`${address}/no-compress`) - const body1 = await response1.text() - const body2 = await response2.text() + const [body1, body2] = await Promise.all([ + fetch(`${address}/compress`), + fetch(`${address}/no-compress`) + ]) + t.equal(body1, body2) t.equal(body1, twoByteUnicodeContent) })