generated from fastify/skeleton
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial commit * test * improve further * exit early if header is not a string
- Loading branch information
Showing
19 changed files
with
484 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"all": true, | ||
"check-coverage": true, | ||
"reporter": [ | ||
"lcov", | ||
"text" | ||
], | ||
"include": [ | ||
"index.js" | ||
], | ||
"branches": 100, | ||
"lines": 100, | ||
"functions": 100, | ||
"statements": 100 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,52 @@ | ||
# skeleton | ||
|
||
Template repository to create standardized Fastify plugins. | ||
|
||
# Getting started | ||
|
||
- Click on `Use this template` above to create a new repository based on this repository. | ||
|
||
# What's included? | ||
|
||
1. Github CI Actions for installing, testing your package. | ||
2. Github CI Actions to validate different package managers. | ||
3. Dependabot V2 config to automate dependency updates. | ||
4. Template for the GitHub App [Stale](https://github.com/apps/stale) to mark issues as stale. | ||
5. Template for the GitHub App [tests-checker](https://github.com/apps/tests-checker) to check if a PR contains tests. | ||
|
||
# Repository structure | ||
|
||
``` | ||
├── .github | ||
│ ├── workflows | ||
│ │ ├── ci.yml | ||
│ │ └── package-manager-ci.yml | ||
│ ├── .stale.yml | ||
│ ├── dependabot.yml | ||
│ └── tests_checker.yml | ||
│ | ||
├── docs (Documentation) | ||
│ | ||
├── examples (Code examples) | ||
│ | ||
├── test (Application tests) | ||
│ | ||
├── types (Typescript types) | ||
│ | ||
└── README.md | ||
``` | ||
# @fastify/accept-negotiator | ||
|
||
|
||
![CI](https://github.com/fastify/fastify-accept-negotiator/workflows/CI/badge.svg) | ||
[![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-encoding').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-encoding').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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict' | ||
|
||
const { Suite } = require('benchmark') | ||
const { Negotiator } = require('..') | ||
|
||
const benchCases = require('./benchCases') | ||
|
||
for (let i = 0; i < benchCases.length; ++i) { | ||
const [header, supportedEncodings] = benchCases[i] | ||
const suite = new Suite() | ||
const negotiator = new Negotiator({ supportedValues: supportedEncodings }) | ||
suite | ||
.add(`${header} and ${supportedEncodings}`, function () { | ||
negotiator.negotiate(header) | ||
}) | ||
.on('cycle', function (event) { | ||
console.log(String(event.target)) | ||
}) | ||
.run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module.exports = [ | ||
['identity;q=1', ['gzip', 'identity'], 'identity'], | ||
['gzip;q=1, identity;q=0.5', ['gzip', 'deflate'], 'gzip'], | ||
['deflate;q=0.5,identity; q=0.5', ['gzip', 'deflate'], 'deflate'], | ||
['deflate;q=0.5, gzip;q=0.5', ['gzip', 'deflate'], 'gzip'], | ||
['deflate;q=0.5, gzip;q=0.5', ['deflate', 'gzip'], 'deflate'], | ||
['*', ['gzip', 'deflate'], 'gzip'], | ||
['deflate;q=1.0, *', ['gzip'], 'gzip'], | ||
['test,br', ['br'], 'br'], | ||
['gzip;q=0', [], null], | ||
['gzip;q=0', ['gzip', 'identity'], null], | ||
['white rabbit', ['gzip', 'identity'], null], | ||
[undefined, ['gzip', 'identity'], undefined], | ||
['compress;q=0.5, gzip;q=1.0', ['gzip', 'compress'], 'gzip'], | ||
['gzip;q=1.0, compress;q=0.5', ['compress', 'gzip'], 'gzip'], | ||
['compress;q=0.5, gzip;q=1.0', ['compress'], 'compress'], | ||
['gzip, deflate, br', ['br', 'gzip', 'deflate'], 'br'], | ||
['*', ['br', 'gzip', 'deflate'], 'br'], | ||
['*;q=0, identity;q=1', ['gzip', 'identity'], 'identity'], | ||
['identity;q=0', ['identity'], null], | ||
['gzip, compress;q=0', ['compress', 'gzip'], 'gzip'], | ||
['gzip;q=0.8, deflate', ['gzip', 'deflate'], 'deflate'], | ||
['gzip;q=0.8, identity;q=0.5, *;q=0.3', ['deflate', 'gzip', 'br'], 'gzip'] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict' | ||
|
||
const { Suite } = require('benchmark') | ||
const encodingNegotiator = require('..') | ||
|
||
const suite = new Suite() | ||
|
||
const benchCases = require('./benchCases') | ||
|
||
for (let i = 0; i < benchCases.length; ++i) { | ||
const [header, supportedEncodings] = benchCases[i] | ||
suite.add(`${header} and ${supportedEncodings}`, function () { | ||
encodingNegotiator.negotiate(header, supportedEncodings) | ||
}) | ||
} | ||
suite | ||
.on('cycle', function (event) { | ||
console.log(String(event.target)) | ||
}) | ||
.on('complete', function () { | ||
console.log('Fastest is ' + this.filter('fastest').map('name')) | ||
console.log('Slowest is ' + this.filter('slowest').map('name')) | ||
}) | ||
.run({ async: true }) |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.