
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
@tinyhttp/accepts
Advanced tools
@tinyhttp/accepts is a package for handling HTTP content negotiation in Node.js applications. It helps in determining the best content type, language, encoding, and charset to respond with based on the client's request headers.
Content Type Negotiation
This feature allows you to determine the best content type to respond with based on the client's Accept header. The code sample demonstrates how to respond with HTML, JSON, or plain text depending on what the client prefers.
const accepts = require('@tinyhttp/accepts');
const http = require('http');
http.createServer((req, res) => {
const accept = accepts(req);
const type = accept.type(['html', 'json', 'text']);
if (type === 'html') {
res.setHeader('Content-Type', 'text/html');
res.end('<p>Hello, world!</p>');
} else if (type === 'json') {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ message: 'Hello, world!' }));
} else {
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, world!');
}
}).listen(3000);
Language Negotiation
This feature allows you to determine the best language to respond with based on the client's Accept-Language header. The code sample demonstrates how to respond in English, Spanish, or French depending on the client's preference.
const accepts = require('@tinyhttp/accepts');
const http = require('http');
http.createServer((req, res) => {
const accept = accepts(req);
const lang = accept.language(['en', 'es', 'fr']);
if (lang === 'en') {
res.end('Hello, world!');
} else if (lang === 'es') {
res.end('¡Hola, mundo!');
} else if (lang === 'fr') {
res.end('Bonjour, le monde!');
} else {
res.end('Hello, world!');
}
}).listen(3000);
Encoding Negotiation
This feature allows you to determine the best encoding to respond with based on the client's Accept-Encoding header. The code sample demonstrates how to respond with gzip, deflate, or plain content depending on the client's preference.
const accepts = require('@tinyhttp/accepts');
const http = require('http');
http.createServer((req, res) => {
const accept = accepts(req);
const encoding = accept.encoding(['gzip', 'deflate', 'identity']);
if (encoding === 'gzip') {
res.setHeader('Content-Encoding', 'gzip');
res.end('gzipped content');
} else if (encoding === 'deflate') {
res.setHeader('Content-Encoding', 'deflate');
res.end('deflated content');
} else {
res.end('plain content');
}
}).listen(3000);
Charset Negotiation
This feature allows you to determine the best charset to respond with based on the client's Accept-Charset header. The code sample demonstrates how to respond with utf-8, iso-8859-1, or windows-1252 charset depending on the client's preference.
const accepts = require('@tinyhttp/accepts');
const http = require('http');
http.createServer((req, res) => {
const accept = accepts(req);
const charset = accept.charset(['utf-8', 'iso-8859-1', 'windows-1252']);
if (charset === 'utf-8') {
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.end('Hello, world!');
} else if (charset === 'iso-8859-1') {
res.setHeader('Content-Type', 'text/plain; charset=iso-8859-1');
res.end('Hello, world!');
} else if (charset === 'windows-1252') {
res.setHeader('Content-Type', 'text/plain; charset=windows-1252');
res.end('Hello, world!');
} else {
res.end('Hello, world!');
}
}).listen(3000);
The 'accepts' package is a content negotiation library for Node.js. It provides similar functionalities to @tinyhttp/accepts, such as determining the best content type, language, encoding, and charset to respond with based on the client's request headers. It is widely used and well-documented.
The 'negotiator' package is another content negotiation library for Node.js. It provides similar functionalities to @tinyhttp/accepts, including content type, language, encoding, and charset negotiation. It is a lower-level library compared to 'accepts' and offers more granular control over the negotiation process.
accepts
rewrite in TypeScript.
Higher level content negotiation based on negotiator. Extracted from koa for general use.
In addition to negotiator, it allows:
(['text/html', 'application/json'])
as well as
('text/html', 'application/json')
.json
.false
when no types match*
pnpm i @tinyhttp/accepts
import { Accepts } from '@tinyhttp/accepts'
Create a new Accepts
object for the given req
.
.charset(charsets)
Return the first accepted charset. If nothing in charsets
is accepted, then
false
is returned.
.charsets()
Return the charsets that the request accepts, in the order of the client's preference (most preferred first).
.encoding(encodings)
Return the first accepted encoding. If nothing in encodings
is accepted, then
false
is returned.
.encodings()
Return the encodings that the request accepts, in the order of the client's preference (most preferred first).
.language(languages)
Return the first accepted language. If nothing in languages
is accepted, then
false
is returned.
.languages()
Return the languages that the request accepts, in the order of the client's preference (most preferred first).
.type(types)
Return the first accepted type (and it is returned as the same text as what
appears in the types
array). If nothing in types
is accepted, then false
is returned.
The types
array can contain full MIME types or file extensions. Any value that
is not a full MIME types is passed to require('mime-types').lookup
.
.types()
Return the types that the request accepts, in the order of the client's preference (most preferred first).
This simple example shows how to use accepts
to return a different typed
respond body based on what the client wants to accept. The server lists it's
preferences in order and will get back the best match between the client and
server.
import Accepts from '@tinyhttp/accepts'
import { createServer } from 'node:http'
createServer((req, res) => {
const accept = new Accepts(req)
// the order of this list is significant; should be server preferred order
switch (accept.type(['json', 'html'])) {
case 'json':
res.setHeader('Content-Type', 'application/json')
res.write('{"hello":"world!"}')
break
case 'html':
res.setHeader('Content-Type', 'text/html')
res.write('<b>hello, world!</b>')
break
default:
// the fallback is text/plain, so no need to specify it above
res.setHeader('Content-Type', 'text/plain')
res.write('hello, world!')
break
}
res.end()
}).listen(3000)
You can test this out with the cURL program:
curl -I -H 'Accept: text/html' http://localhost:3000/
FAQs
accepts rewrite in TypeScript
The npm package @tinyhttp/accepts receives a total of 150,198 weekly downloads. As such, @tinyhttp/accepts popularity was classified as popular.
We found that @tinyhttp/accepts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.