Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace into-stream with Readable.from #289

Closed
wants to merge 2 commits into from
Closed

replace into-stream with Readable.from #289

wants to merge 2 commits into from

Conversation

mcollina
Copy link
Member

Fixes #288

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
test/issue-288.test.js Dismissed Show dismissed Hide dismissed
test/issue-288.test.js Dismissed Show dismissed Hide dismissed
@@ -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')
Copy link
Member

@climba03003 climba03003 Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note, should install readable-stream once and update the package.json dependencies.

const fsPromises = require('fs').promises
const { join } = require('path')
const { fetch } = require('undici')

Copy link
Contributor

@stanleyxu2005 stanleyxu2005 Mar 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const longStringWithEmoji = new Array(5_000)
.fill('0')
.map(() => {
const random = new Array(10).fill('A').join('🍃')
return random + '- FASTIFY COMPRESS,🍃 FASTIFY COMPRESS'
})
.join('\n')

const { test } = require('tap')
const Fastify = require('fastify')
const fastifyCompress = require('..')
const fsPromises = require('fs').promises
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const fsPromises = require('fs').promises

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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const longStringWithEmoji = await fsPromises.readFile(join(__dirname, './test.txt'), 'utf-8')

})

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const longStringWithEmoji = await fsPromises.readFile(join(__dirname, './test.txt'), 'utf-8')
const longStringWithEmoji = await fsPromises.readFile(join(__dirname, './test.txt'), 'utf-8')

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// search for "hydra.alibaba.com" will see 2 wired question marks instead of emoji
// Expect response should not contain any �

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

return longStringWithEmoji // <--- the file content is corrupt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return longStringWithEmoji // <--- the file content is corrupt
return longStringWithEmoji

Comment on lines +27 to +28
return longStringWithEmoji // <--- the file content is ok
// search for "hydra.alibaba.com" will see emoji
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return longStringWithEmoji // <--- the file content is ok
// search for "hydra.alibaba.com" will see emoji
return longStringWithEmoji
// Expect response should not contain any �

@climba03003
Copy link
Member

Readable.from doesn't like a directly replacement.
into-stream support more data types than expected.

const response2 = await fetch(`${url}/good`)
const body = await response.text()
const body2 = await response2.text()
t.equal(body, body2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.equal(body, body2)
t.equal(body, body2)
t.equal(body.indexOf('�'), -1)
t.equal(body2.indexOf('�'), -1)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure number of emoji is valid

@stanleyxu2005
Copy link
Contributor

@mcollina I suggest to generate a long string instead of loading from test.txt

@Uzlopak Uzlopak closed this Mar 29, 2024
@gurgunday gurgunday deleted the fix-288 branch March 29, 2024 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unicode emoji converted to broken chars in certain extreme long string responses
4 participants