Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- const fp = require('fastify-plugin')
- function hash (timestamp) {
- if (!timestamp) return void 0
- var buf_
- var b64_
- const K = Number.parseInt(timestamp, 24) / Math.random()
- buf_ = Buffer.from(K.toString())
- b64_ = buf_.toString('base64')
- return b64_
- }
- module.exports = fp(async function etag (app, opts) {
- app.addHook('onSend', async function (req, reply, payload) {
- let etag = reply.getHeader('etag')
- // we do not generate with an already existing etag
- if (!etag) {
- // we do not generate etags for anything but strings and buffers
- if (!(typeof payload === 'string' || payload instanceof Buffer)) {
- return
- }
- etag = hash(Date.now())
- reply.header('etag', etag)
- }
- if (req.headers['if-none-match'] === etag) {
- reply.code(304)
- return ''
- }
- })
- }, {
- fastify: '2.x',
- name: 'fastify-etag'
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement