Skip to content

Commit

Permalink
feat: add option to disable automatic 304 responses (#117)
Browse files Browse the repository at this point in the history
* feat: add option to disable automatic 304 responses

* Apply suggestions from code review

* Update index.js

---------

Co-authored-by: Aras Abbasi <[email protected]>
  • Loading branch information
jamescallumyoung and Uzlopak authored Nov 29, 2023
1 parent f1ec3fc commit f3ddf3d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
[![NPM version](https://img.shields.io/npm/v/@fastify/etag.svg?style=flat)](https://www.npmjs.com/package/@fastify/etag)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)

A plugin for [Fastify](https://www.fastify.io) that automatically generates HTTP ETags and returns 304 when needed,
according to [RFC2616-sec13](https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html).
A plugin for [Fastify](https://www.fastify.io) that automatically generates HTTP ETags according to [RFC2616-sec13](https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html).

The plugin can optionally send a 304 status code when an ETag matches the if-none-match header.


## Install
Expand Down Expand Up @@ -45,6 +46,12 @@ app.listen(3000)

* `weak`: generates weak ETags by default. Default: `false`.

### Automatic 304 status codes

By default, the plugin sends a 304 status code when the ETag is equal to the Etag specified by the if-none-match request header.

This is often the desired behaviour, but can be disabled by setting `replyWith304: false`.

## Acknowledgements

The fnv1a logic was forked from https://github.com/sindresorhus/fnv1a and adapted to support buffers.
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function buildHashFn (algorithm = 'sha1', weak = false) {
.update(payload).digest('base64') + '"'
}

async function fastifyEtag (app, opts) {
const hash = buildHashFn(opts.algorithm, opts.weak)
async function fastifyEtag (app, { algorithm, weak, replyWith304 = true }) {
const hash = buildHashFn(algorithm, weak)

app.addHook('onSend', function (req, reply, payload, done) {
let etag = reply.getHeader('etag')
Expand All @@ -48,7 +48,7 @@ async function fastifyEtag (app, opts) {
reply.header('etag', etag)
}

if (req.headers['if-none-match'] === etag) {
if (replyWith304 && req.headers['if-none-match'] === etag) {
reply.code(304)
newPayload = ''
}
Expand Down
15 changes: 15 additions & 0 deletions test/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ module.exports = function ({ test }, etagOpts, hashFn) {
})
})

test('does not return a 304 when behaviour is disabled', async (t) => {
const res = await build({ replyWith304: false }).inject({
url: '/',
headers: {
'If-None-Match': hash
}
})

t.same(JSON.parse(res.body), { hello: 'world' })
t.equal(res.statusCode, 200)
t.match(res.headers, {
etag: hash
})
})

test('returns an already existing etag', async (t) => {
const res = await build().inject({
url: '/etag'
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare namespace fastifyEtag {
export interface FastifyEtagOptions {
algorithm?: 'fnv1a' | string;
weak?: boolean;
replyWith304?: boolean;
}

export const fastifyEtag: FastifyEtag
Expand Down

0 comments on commit f3ddf3d

Please sign in to comment.