generated from fastify/skeleton
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
170 lines (152 loc) · 3.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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