Giter Site home page Giter Site logo

Comments (10)

climba03003 avatar climba03003 commented on June 10, 2024 1

I don't get the point, why it is not working using commonjs? isn't cjs supported anymore?

It is supported in commonjs. And I have no idea why it didn't works for you without a minimal repo.

My guess is about there are something wrong about your setup. For example, the routes and @fastify/compress are inside separate encapsulation context.

Another though is all the plugin is registered inside the same file. And you did not await that file in the most upper layer.

from fastify-compress.

climba03003 avatar climba03003 commented on June 10, 2024 1

and that means the better way to make compress works with commonjs is to wrap all in a async start function

I always recommend people to put all the routes inside a plugin. So, they will not face similar issue.
I do not recommend to place fastify.get or similar in the same level of fastify.register.

For example,

fastify.register(plugin, {})

fastify.get('/', function() {})

should refactor into

fastify.register(plugin, {})

fastify.register(function(fastify, __, done) {
  fastify.get('/', function() {})

  done()
})

In this case, wrapper of async start is not needed.

from fastify-compress.

climba03003 avatar climba03003 commented on June 10, 2024

I recommend you use the form of

import fastifyCompress from '@fastify/compress`

fastify.register(fastifyCompress, { global: false })

It often the problem of the user do not configure TypeScript correctly. So, the type merging is not working.

from fastify-compress.

climba03003 avatar climba03003 commented on June 10, 2024

I may mis-understand the issue. It is not TypeScript related.

You are required to await the registration for it works.

await fastify.register(require('@fastify/compress'), { global: false })

The reason behind in the fastify migration guide

from fastify-compress.

danielecr avatar danielecr commented on June 10, 2024
(async () => {
    await fastify.register(require('@fastify/compress'),{threshold: 2048, global: true })
    console.log("compress ok");
})();

"compress ok" is printed. But still "reply.compress is not a function"

For curiosity I added console.log() before line 204, in
https://github.com/fastify/fastify-compress/blob/master/index.js#L204

and it is not printed. The reply is used for a http POST method, I do not know if it changes something

from fastify-compress.

climba03003 avatar climba03003 commented on June 10, 2024

By looking at the test, your case should be supported.

let usedCustom = false
const customZlib = { createGzip: () => (usedCustom = true) && zlib.createGzip() }
const fastify = Fastify()
await fastify.register(compressPlugin, { global: false })
fastify.get('/', (request, reply) => {
// compress function should still be available
t.type(reply.compress, 'function')
reply.send({ foo: 1 })
})
fastify.get('/custom', {
compress: { zlib: customZlib }
}, (request, reply) => {
reply
.type('text/plain')
.compress(createReadStream('./package.json'))
})
fastify.get('/standard', {
compress: { threshold: 1 }
}, (request, reply) => {
reply.send({ foo: 1 })
})

I may expect your application is run into some encapsulated context, so the route and plugin are actually in two different context.

Can you provide a minimal repo that I can actually run?

from fastify-compress.

danielecr avatar danielecr commented on June 10, 2024

let usedCustom = false
const customZlib = { createGzip: () => (usedCustom = true) && zlib.createGzip() }
const fastify = Fastify()
await fastify.register(compressPlugin, { global: false })
fastify.get('/', (request, reply) => {
// compress function should still be available
t.type(reply.compress, 'function')
reply.send({ foo: 1 })
})
fastify.get('/custom', {
compress: { zlib: customZlib }
}, (request, reply) => {
reply
.type('text/plain')
.compress(createReadStream('./package.json'))
})
fastify.get('/standard', {
compress: { threshold: 1 }
}, (request, reply) => {
reply.send({ foo: 1 })
})

and is it possible to add a fastify.post(...) test case?

from fastify-compress.

climba03003 avatar climba03003 commented on June 10, 2024

is it possible to add a fastify.post(...) test case?

I didn't see how .post is related, but welcome to add one.

from fastify-compress.

danielecr avatar danielecr commented on June 10, 2024

is it possible to add a fastify.post(...) test case?

I didn't see how .post is related, but welcome to add one.

I don't get the point, why it is not working using commonjs? isn't cjs supported anymore?

from fastify-compress.

danielecr avatar danielecr commented on June 10, 2024

I don't get the point, why it is not working using commonjs? isn't cjs supported anymore?

It is supported in commonjs. And I have no idea why it didn't works for you without a minimal repo.

My guess is about there are something wrong about your setup. For example, the routes and @fastify/compress are inside separate encapsulation context.

Another though is all the plugin is registered inside the same file. And you did not await that file in the most upper layer.

Thank you so much! Exactly that. Now I get it, it was not the "right fastify object" the one I attached the route, and that means the better way to make compress works with commonjs is to wrap all in a async start function

from fastify-compress.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.