Projektstart

This commit is contained in:
2026-01-22 15:49:12 +01:00
parent 7212eb6f7a
commit 57e5f652f8
10637 changed files with 2598792 additions and 64 deletions

View File

@@ -0,0 +1,22 @@
The MIT License
Copyright (c) 2022 The Fastify Team
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,52 @@
# @fastify/accept-negotiator
[![CI](https://github.com/fastify/accept-negotiator/workflows/CI/badge.svg)](https://github.com/fastify/accept-negotiator/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/accept-negotiator.svg?style=flat)](https://www.npmjs.com/package/@fastify/accept-negotiator)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
A negotiator for the accept-headers
### Install
```
npm i @fastify/accept-negotiator
```
### Usage
The module exports a function that you can use for negotiating an accept-header, e.g. accept-encoding. It takes 2 parameters:
```
negotiate(header, supportedValues)
```
- `header` (`string`, required) - The accept-header, e.g. accept-encoding
- `supportedValues` (`string[]`, required) - The values, which are supported
```js
const negotiate = require('@fastify/accept-negotiator').negotiate
const encoding = negotiate('gzip, deflate, br', ['br'])
console.log(encoding) // 'br*
```
The module also exports a class that you can use for negotiating an accept-header, e.g. accept-encoding, and use caching for better performance.
```
Negotiate(supportedValues)
```
- `supportedValues` (`string[]`, required) - The values, which are supported
- `cache` (`{ set: Function; get: Function; has: Function }`, optional) - A Cache-Store, e.g. ES6-Map or mnemonist LRUCache
```js
const Negotiator = require('@fastify/accept-negotiator').Negotiator
const encodingNegotiator = new Negotiator({ supportedValues: ['br'], cache: new Map() })
const encoding = encodingNegotiator.negotiate('gzip, deflate, br')
console.log(encoding) // 'br*
```
## License
Licensed under [MIT](./LICENSE).

View File

@@ -0,0 +1,170 @@
'use strict'
function Negotiator (options) {
if (!new.target) {
return new Negotiator(options)
}
const {
supportedValues = [],
cache
} = (options && typeof options === 'object' && options) || {}
this.supportedValues = supportedValues
this.cache = cache
}
Negotiator.prototype.negotiate = function (header) {
if (typeof header !== 'string') {
return null
}
if (!this.cache) {
return negotiate(header, this.supportedValues)
}
if (!this.cache.has(header)) {
this.cache.set(header, negotiate(header, this.supportedValues))
}
return this.cache.get(header)
}
function negotiate (header, supportedValues) {
if (
!header ||
!Array.isArray(supportedValues) ||
supportedValues.length === 0
) {
return null
}
if (header === '*') {
return supportedValues[0]
}
let preferredEncoding = null
let preferredEncodingPriority = Infinity
let preferredEncodingQuality = 0
function processMatch (enc, quality) {
if (quality === 0 || preferredEncodingQuality > quality) {
return false
}
const encoding = (enc === '*' && supportedValues[0]) || enc
const priority = supportedValues.indexOf(encoding)
if (priority === -1) {
return false
}
if (priority === 0 && quality === 1) {
preferredEncoding = encoding
return true
} else if (preferredEncodingQuality < quality) {
preferredEncoding = encoding
preferredEncodingPriority = priority
preferredEncodingQuality = quality
} else if (preferredEncodingPriority > priority) {
preferredEncoding = encoding
preferredEncodingPriority = priority
preferredEncodingQuality = quality
}
return false
}
parse(header, processMatch)
return preferredEncoding
}
const BEGIN = 0
const TOKEN = 1
const QUALITY = 2
const END = 3
function parse (header, processMatch) {
let str = ''
let quality
let state = BEGIN
for (let i = 0, il = header.length; i < il; ++i) {
const char = header[i]
if (char === ' ' || char === '\t') {
continue
} else if (char === ';') {
if (state === TOKEN) {
state = QUALITY
quality = ''
}
continue
} else if (char === ',') {
if (state === TOKEN) {
if (processMatch(str, 1)) {
state = END
break
}
state = BEGIN
str = ''
} else if (state === QUALITY) {
if (processMatch(str, parseFloat(quality) || 0)) {
state = END
break
}
state = BEGIN
str = ''
quality = ''
}
continue
} else if (
state === QUALITY
) {
if (char === 'q' || char === '=') {
continue
} else if (
char === '.' ||
char === '1' ||
char === '0' ||
char === '2' ||
char === '3' ||
char === '4' ||
char === '5' ||
char === '6' ||
char === '7' ||
char === '8' ||
char === '9'
) {
quality += char
continue
}
} else if (state === BEGIN) {
state = TOKEN
str += char
continue
}
if (state === TOKEN) {
const prevChar = header[i - 1]
if (prevChar === ' ' || prevChar === '\t') {
str = ''
}
str += char
continue
}
if (processMatch(str, parseFloat(quality) || 0)) {
state = END
break
}
state = BEGIN
str = char
quality = ''
}
if (state === TOKEN) {
processMatch(str, 1)
} else if (state === QUALITY) {
processMatch(str, parseFloat(quality) || 0)
}
}
module.exports = negotiate
module.exports.default = negotiate
module.exports.negotiate = negotiate
module.exports.Negotiator = Negotiator

View File

@@ -0,0 +1,51 @@
{
"name": "@fastify/accept-negotiator",
"version": "1.1.0",
"description": "a negotiator for the accept-headers",
"type": "commonjs",
"main": "index.js",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard index.js test/* benchmarks/*",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:typescript": "tsd"
},
"standard": {
"ignore": [
"index.d.ts"
]
},
"keywords": [
"encoding",
"negotiator",
"accept-encoding",
"accept",
"http",
"header"
],
"files": [
"README.md",
"LICENSE",
"index.js",
"types/index.d.ts"
],
"author": "Aras Abbasi",
"license": "MIT",
"devDependencies": {
"benchmark": "2.1.4",
"standard": "17.0.0",
"tap": "^16.3.0",
"tsd": "^0.24.1"
},
"engines": {
"node": ">=14"
},
"repository": {
"type": "git",
"url": "git+https://github.com/fastify/accept-negotiator.git"
},
"bugs": {
"url": "https://github.com/fastify/accept-negotiator/issues"
}
}

View File

@@ -0,0 +1,17 @@
type CacheStore = { set: (key: string, value: string) => CacheStore, get: (key: string) => string | undefined, has: (key: string) => boolean }
type NegotiateFn = typeof negotiate
declare namespace negotiate {
export class Negotiator<K extends string = string> {
constructor (options: { supportedValues: K[]; cache?: CacheStore })
negotiate(header: string): K | null
}
export const negotiate: NegotiateFn
export { negotiate as default }
}
declare function negotiate<K extends string = string>(header: string, supportedValues: K[]): K | null;
export = negotiate;