🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

@bogeychan/elysia-etag

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bogeychan/elysia-etag

A plugin for Elysia.js for automatic HTTP ETag generation

latest
Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
797
11.47%
Maintainers
1
Weekly downloads
 
Created
Source

@bogeychan/elysia-etag

A plugin for Elysia.js for automatic HTTP ETag generation

Installation

bun add @bogeychan/elysia-etag

Usage

import { Elysia } from 'elysia'
import { etag } from '@bogeychan/elysia-etag'

const app = new Elysia()
	.use(etag())
	.get('/', () => 'Checkout the response headers!')
	.get('/custom-etag', (ctx) => {
		// This line disables automatic ETag generation
		// It will still return a 304 - Not Modified - status code if the ETag matches
		ctx.setETag('"myETag"')
		return 'Hello ETag!'
	})
	.listen(3000)

console.log(`Listening on ${app.server!.url}`)

Use serialize to convert incompatible data to a hashable format

With JSON

new Elysia()
	.use(
		etag({
			serialize(response) {
				if (typeof response === 'object') {
					return JSON.stringify(response)
				}
			}
		})
	)
	.get('/', () => ({ my: 'json' }))

With BunFile

new Elysia()
	.use(
		etag({
			serialize(response) {
				if (response instanceof Blob && 'lastModified' in response) {
					// based on last modified UNIX timestamp
					return (response as BunFile).lastModified.toString()

					// based on file content
					return (response as BunFile).bytes()
				}
			}
		})
	)
	.get('/', () => Bun.file('./file.txt'))

Provide your own hash function

let myInsecureChangeCounter = 0

new Elysia()
	.use(
		etag({
			hash: (response) => {
				return (response as string) + myInsecureChangeCounter++
			}
		})
	)
	.get('/', () => 'Checkout the response headers!')

Checkout the examples folder on github for further use cases.

Credits ❤️

This project was inspired by @fastify/etag

Author

bogeychan

License

MIT

Keywords

elysia

FAQs

Package last updated on 26 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts