Skip to content

Commit

Permalink
replace into-stream with Readable.from
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Mar 29, 2024
1 parent 53e5443 commit af6e4c5
Show file tree
Hide file tree
Showing 4 changed files with 3,861 additions and 6 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const fp = require('fastify-plugin')
const encodingNegotiator = require('@fastify/accept-negotiator')
const pump = require('pump')
const mimedb = require('mime-db')
const intoStream = require('into-stream')
const { Readable } = require('readable-stream')
const peek = require('peek-stream')
const { Minipass } = require('minipass')
const pumpify = require('pumpify')
Expand Down Expand Up @@ -273,7 +273,7 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
if (Buffer.byteLength(payload) < params.threshold) {
return next()
}
payload = intoStream(payload)
payload = Readable.from(payload)
}

params.removeContentLengthHeader
Expand Down Expand Up @@ -398,7 +398,7 @@ function compress (params) {
if (Buffer.byteLength(payload) < params.threshold) {
return this.send(payload)
}
payload = intoStream(payload)
payload = Readable.from(payload)
}

params.removeContentLengthHeader
Expand Down Expand Up @@ -506,7 +506,7 @@ function maybeUnzip (payload, serialize) {
// handle case where serialize doesn't return a string or Buffer
if (!Buffer.isBuffer(buf)) return result
if (isCompressed(buf) === 0) return result
return intoStream(result)
return Readable.from(result)
}

function zipStream (deflate, encoding) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"dependencies": {
"@fastify/accept-negotiator": "^1.1.0",
"fastify-plugin": "^4.5.0",
"into-stream": "^6.0.0",
"mime-db": "^1.52.0",
"minipass": "^7.0.2",
"peek-stream": "^1.1.3",
Expand All @@ -26,7 +25,8 @@
"standard": "^17.1.0",
"tap": "^16.3.7",
"tsd": "^0.30.0",
"typescript": "^5.1.6"
"typescript": "^5.1.6",
"undici": "^5.28.3"
},
"scripts": {
"coverage": "npm run test:unit -- --coverage-report=html",
Expand Down
40 changes: 40 additions & 0 deletions test/issue-288.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const { test } = require('tap')
const Fastify = require('fastify')
const fastifyCompress = require('..')
const fsPromises = require("fs").promises
const { join } = require('path')
const { fetch } = require('undici')

test('should not corrupt the file content', async (t) => {
const fastify = new Fastify()
t.teardown(() => fastify.close())

fastify.register(async (instance, opts) => {
await fastify.register(fastifyCompress)
instance.get('/issue', async (req, reply) => {
const longStringWithEmoji = await fsPromises.readFile(join(__dirname, "./test.txt"), "utf-8");

return longStringWithEmoji // <--- the file content is corrupt
// search for "hydra.alibaba.com" will see 2 wired question marks instead of emoji
})

Check failure

Code scanning / CodeQL

Missing rate limiting High test

This route handler performs
a file system access
, but is not rate-limited.
})

fastify.get('/good', async (req, reply) => {
const longStringWithEmoji = await fsPromises.readFile(join(__dirname, "./test.txt"), "utf-8");

return longStringWithEmoji // <--- the file content is ok
// search for "hydra.alibaba.com" will see emoji
})

Check failure

Code scanning / CodeQL

Missing rate limiting High test

This route handler performs
a file system access
, but is not rate-limited.

await fastify.listen({ port: 0 })

const { port } = fastify.server.address()
const url = `http://localhost:${port}`
const response = await fetch(`${url}/issue`)
const response2 = await fetch(`${url}/good`)
const body = await response.text()
const body2 = await response2.text()
t.equal(body, body2)
})
Loading

0 comments on commit af6e4c5

Please sign in to comment.