Skip to content

Commit

Permalink
Move etagsCacheMaxSize to a server option. Closes #99
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Oct 27, 2017
1 parent 5b48693 commit aa3fe79
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -127,16 +127,16 @@ server.ext('onPreResponse', (request, responder) => {
After registration, this plugin adds a new method to the `responder` object and exposes the `'file'`
and `'directory'` route handlers.

### Registration options
Note that **inert** uses the custom `'file'` `variety` to signal that the response is a static
file generated by this plugin.

### Server options

**inert** accepts the following registration options:
**inert** handles the following server plugin options on `plugins.inert`:

- `etagsCacheMaxSize` - sets the maximum number of file etag hash values stored in the
etags cache. Defaults to `10000`.

Note that **inert** uses the custom `'file'` `variety` to signal that the response is a static
file generated by this plugin.

### `responder.file(path, [options])`

Transmits a file from the file system. The 'Content-Type' header defaults to the matching mime
Expand Down
13 changes: 7 additions & 6 deletions lib/index.js
Expand Up @@ -6,26 +6,27 @@ const Directory = require('./directory');
const Etag = require('./etag');
const File = require('./file');
const Hoek = require('hoek');
const Joi = require('joi');


// Declare internals

const internals = {
defaults: {
etagsCacheMaxSize: 1000
}
schema: Joi.object({
etagsCacheMaxSize: Joi.number().integer().min(0).default(1000)
}).required()
};


exports.plugin = {
pkg: require('../package.json'),
once: true,

register: function (server, options) {
register(server) {

const settings = Hoek.applyToDefaults(internals.defaults, options);
const settings = Joi.attempt(Hoek.reach(server.settings.plugins, 'inert') || {}, internals.schema, 'Invalid "inert" server options');

server.expose('_etags', settings.etagsCacheMaxSize ? new Etag.Cache(settings.etagsCacheMaxSize) : null);
server.expose('_etags', settings.etagsCacheMaxSize > 0 ? new Etag.Cache(settings.etagsCacheMaxSize) : null);

server.decorate('handler', 'file', File.handler);
server.decorate('handler', 'directory', Directory.handler);
Expand Down
7 changes: 3 additions & 4 deletions test/file.js
Expand Up @@ -24,8 +24,7 @@ const internals = {};
// Test shortcuts

const lab = exports.lab = Lab.script();
const describe = lab.describe;
const it = lab.it;
const { describe, it } = lab;
const expect = Code.expect;


Expand All @@ -35,9 +34,9 @@ describe('file', () => {

const provisionServer = async (options, etagsCacheMaxSize) => {

const defaults = { compression: { minBytes: 1 } };
const defaults = { compression: { minBytes: 1 }, plugins: { inert: { etagsCacheMaxSize } } };
const server = new Hapi.Server(Hoek.applyToDefaults(defaults, options || {}));
await server.register(etagsCacheMaxSize !== undefined ? { plugin: Inert, options: { etagsCacheMaxSize } } : Inert);
await server.register(Inert);
return server;
};

Expand Down

0 comments on commit aa3fe79

Please sign in to comment.