hapijs / glue

Server composer for hapi.js
Other
245 stars 62 forks source link

Server cache options in hapi 18 cannot be passed #128

Closed hgrubst closed 5 years ago

hgrubst commented 5 years ago

I am currently migrating from hapi 17 to hapi 18 and cannot configure my server cache successfully with glue. Although the doc mentions specific support to call 'require' on the 'provider' option, this is forcing you to have provider as a string instead of an object, hence prevent you from passing additional options. Here is how my config looks like :

 "server": {
        ...
        "cache": {
            "name": "rate-limit-cache",
            "provider": {
                "constructor": "@hapi/catbox-redis",
                "options": {
                    "partition": "rate-limit-cache",
                    "host": "redis-cluster.domain.com",
                    "port": 6379,
                    "password": "secret"
                }
            }
        }
    }

What I would like is that glue takes care of calling require on the 'constructor' property if the passed in provider is an object which is currently not the case. Here is the relevant code in glue :

        const caches = [];
        const config = [].concat(serverOpts.cache);
        for (let i = 0; i < config.length; ++i) {
            let item = config[i];
            if (typeof item === 'string') {
                item = { provider: item };
            }

            if (typeof item.provider === 'string') {
                let strategy = item.provider;
                if (relativeTo && strategy[0] === '.') {
                    strategy = Path.join(relativeTo, strategy);
                }

                item.provider = require(strategy);
            }

            caches.push(item);
        }

        serverOpts.cache = caches;

As you can see, it will only call require when setting the provider to a string, basically preventing you from passing further options. Am I missing something else here ?

hueniverse commented 5 years ago

I'll take a PR if you want to try it.

hgrubst commented 5 years ago

Sure, I've raised https://github.com/hapijs/glue/pull/129

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.