fastify/fastify-caching

Error when trying to use reply.etag and Redis

martosoler opened this issue · 2 comments

Hi,

I'm trying to build a POC where I can use both functionalities from fastify-caching:

  • Being able to set the object in a local Redis I have
  • Add etag header using reply.etag

This is what I have in my plugin:

/* eslint-disable no-console */
const fp = require("fastify-plugin");

module.exports = fp(function(fastify, opts, next) {
  const Redis = require("ioredis");

  const redis = new Redis({ host: "127.0.0.1", port: 6379 });

  const abcache = require("abstract-cache")({
    useAwait: false,
    driver: {
      name: "abstract-cache-redis",
      options: { client: redis }
    }
  });

  fastify.register(require("fastify-redis"), { client: redis });
  fastify.register(require("fastify-caching"), { cache: abcache });

  next();
});

And this is my request handler:

/* eslint-disable no-console */
module.exports = async fastify => {
  fastify.get("/", (req, reply) => {
    fastify.cache.set("hello", { hello: "world" }, 10000, err => {
      if (err) return reply.send(err);
      reply.etag("hey").send({ hello: "world" });
    });
  });
};

What I'm getting when I do a request is the following:

13:08:56.423] ERROR (48606 on xxxx-MacBook-Pro.local): Attempted to send payload of invalid type 'number'. Expected a string or Buffer.
    reqId: 1
    req: {
      "id": 1,
      "method": "GET",
      "url": "/hello",
      "hostname": "127.0.0.1:4444",
      "remoteAddress": "127.0.0.1",
      "remotePort": 61836
    }
    res: {
      "statusCode": 500
    }
    err: {
      "type": "TypeError",
      "message": "Attempted to send payload of invalid type 'number'. Expected a string or Buffer.",
      "stack":
          TypeError: Attempted to send payload of invalid type 'number'. Expected a string or Buffer.
              at onSendEnd (/Users/devgurus/projects/tottus/fastify-soler/node_modules/fastify/lib/reply.js:207:11)
              at wrapOnSendEnd (/Users/devgurus/projects/tottus/fastify-soler/node_modules/fastify/lib/reply.js:177:5)
              at next (/Users/devgurus/projects/tottus/fastify-soler/node_modules/fastify/lib/hookRunner.js:43:7)
              at client.set.then (/Users/devgurus/projects/tottus/fastify-soler/node_modules/abstract-cache/lib/wrapAwait.js:24:25)
              at process._tickCallback (internal/process/next_tick.js:68:7)
    }

If I remove the etag("hey"), the request is handled perfectly and I get the response as well as the object is added to the Redis cache.

Am I configuring something wrong?


Node: v10.13.0
fastify: ^1.13.0
fastify-caching: ^3.0.0
fastify-redis: ^2.0.0
abstract-cache: ^1.0.1
abstract-cache-redis: ^1.0.2

One thing I notice is that you are telling abstract-cache that the abstract-cache-redis driver should be using the callback protocol. This is not true. From the abstract-cache-redis readme:

This client implements the await style of the protocol.

Supplying your plugin and handler code is helpful, but I'd like to be able to easily startup a "broken" server. Can you include a server bootstrap script as well?

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.