Started to implement server
This commit is contained in:
1 parent
1f2ea8b7e5
commit
17a1b4bed3
177 files changed
+29894
-74
No files matched your search
@@ -0,0 +1,51 @@
|
||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
/*!
|
||||
* Adapted directly from negotiator at https://github.com/jshttp/negotiator/
|
||||
* which is licensed as follows:
|
||||
*
|
||||
* (The MIT License)
|
||||
*
|
||||
* Copyright (c) 2012-2014 Federico Romero
|
||||
* Copyright (c) 2012-2014 Isaac Z. Schlueter
|
||||
* Copyright (c) 2014-2015 Douglas Christopher Wilson
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
export interface Specificity {
|
||||
i: number;
|
||||
o?: number;
|
||||
q: number;
|
||||
s?: number;
|
||||
}
|
||||
|
||||
export function compareSpecs(a: Specificity, b: Specificity): number {
|
||||
return (
|
||||
b.q - a.q ||
|
||||
(b.s ?? 0) - (a.s ?? 0) ||
|
||||
(a.o ?? 0) - (b.o ?? 0) ||
|
||||
a.i - b.i ||
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
export function isQuality(spec: Specificity): boolean {
|
||||
return spec.q > 0;
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
/*!
|
||||
* Adapted directly from negotiator at https://github.com/jshttp/negotiator/
|
||||
* which is licensed as follows:
|
||||
*
|
||||
* (The MIT License)
|
||||
*
|
||||
* Copyright (c) 2012-2014 Federico Romero
|
||||
* Copyright (c) 2012-2014 Isaac Z. Schlueter
|
||||
* Copyright (c) 2014-2015 Douglas Christopher Wilson
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { compareSpecs, isQuality, Specificity } from "./common.ts";
|
||||
|
||||
interface EncodingSpecificity extends Specificity {
|
||||
encoding?: string;
|
||||
}
|
||||
|
||||
const simpleEncodingRegExp = /^\s*([^\s;]+)\s*(?:;(.*))?$/;
|
||||
|
||||
function parseEncoding(
|
||||
str: string,
|
||||
i: number,
|
||||
): EncodingSpecificity | undefined {
|
||||
const match = simpleEncodingRegExp.exec(str);
|
||||
if (!match) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const encoding = match[1];
|
||||
let q = 1;
|
||||
if (match[2]) {
|
||||
const params = match[2].split(";");
|
||||
for (const param of params) {
|
||||
const p = param.trim().split("=");
|
||||
if (p[0] === "q") {
|
||||
q = parseFloat(p[1]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { encoding, q, i };
|
||||
}
|
||||
|
||||
function specify(
|
||||
encoding: string,
|
||||
spec: EncodingSpecificity,
|
||||
i = -1,
|
||||
): Specificity | undefined {
|
||||
if (!spec.encoding) {
|
||||
return;
|
||||
}
|
||||
let s = 0;
|
||||
if (spec.encoding.toLocaleLowerCase() === encoding.toLocaleLowerCase()) {
|
||||
s = 1;
|
||||
} else if (spec.encoding !== "*") {
|
||||
return;
|
||||
}
|
||||
|
||||
return {
|
||||
i,
|
||||
o: spec.i,
|
||||
q: spec.q,
|
||||
s,
|
||||
};
|
||||
}
|
||||
|
||||
function parseAcceptEncoding(accept: string): EncodingSpecificity[] {
|
||||
const accepts = accept.split(",");
|
||||
const parsedAccepts: EncodingSpecificity[] = [];
|
||||
let hasIdentity = false;
|
||||
let minQuality = 1;
|
||||
|
||||
for (let i = 0; i < accepts.length; i++) {
|
||||
const encoding = parseEncoding(accepts[i].trim(), i);
|
||||
|
||||
if (encoding) {
|
||||
parsedAccepts.push(encoding);
|
||||
hasIdentity = hasIdentity || !!specify("identity", encoding);
|
||||
minQuality = Math.min(minQuality, encoding.q || 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasIdentity) {
|
||||
parsedAccepts.push({
|
||||
encoding: "identity",
|
||||
q: minQuality,
|
||||
i: accepts.length - 1,
|
||||
});
|
||||
}
|
||||
|
||||
return parsedAccepts;
|
||||
}
|
||||
|
||||
function getEncodingPriority(
|
||||
encoding: string,
|
||||
accepted: Specificity[],
|
||||
index: number,
|
||||
): Specificity {
|
||||
let priority: Specificity = { o: -1, q: 0, s: 0, i: 0 };
|
||||
|
||||
for (const s of accepted) {
|
||||
const spec = specify(encoding, s, index);
|
||||
|
||||
if (
|
||||
spec &&
|
||||
(priority.s! - spec.s! || priority.q - spec.q ||
|
||||
priority.o! - spec.o!) <
|
||||
0
|
||||
) {
|
||||
priority = spec;
|
||||
}
|
||||
}
|
||||
|
||||
return priority;
|
||||
}
|
||||
|
||||
/** Given an `Accept-Encoding` string, parse out the encoding returning a
|
||||
* negotiated encoding based on the `provided` encodings otherwise just a
|
||||
* prioritized array of encodings. */
|
||||
export function preferredEncodings(
|
||||
accept: string,
|
||||
provided?: string[],
|
||||
): string[] {
|
||||
const accepts = parseAcceptEncoding(accept);
|
||||
|
||||
if (!provided) {
|
||||
return accepts
|
||||
.filter(isQuality)
|
||||
.sort(compareSpecs)
|
||||
.map((spec) => spec.encoding!);
|
||||
}
|
||||
|
||||
const priorities = provided.map((type, index) =>
|
||||
getEncodingPriority(type, accepts, index)
|
||||
);
|
||||
|
||||
return priorities
|
||||
.filter(isQuality)
|
||||
.sort(compareSpecs)
|
||||
.map((priority) => provided[priorities.indexOf(priority)]);
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
/*!
|
||||
* Adapted directly from negotiator at https://github.com/jshttp/negotiator/
|
||||
* which is licensed as follows:
|
||||
*
|
||||
* (The MIT License)
|
||||
*
|
||||
* Copyright (c) 2012-2014 Federico Romero
|
||||
* Copyright (c) 2012-2014 Isaac Z. Schlueter
|
||||
* Copyright (c) 2014-2015 Douglas Christopher Wilson
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { compareSpecs, isQuality, Specificity } from "./common.ts";
|
||||
|
||||
interface LanguageSpecificity extends Specificity {
|
||||
prefix: string;
|
||||
suffix?: string;
|
||||
full: string;
|
||||
}
|
||||
|
||||
const SIMPLE_LANGUAGE_REGEXP = /^\s*([^\s\-;]+)(?:-([^\s;]+))?\s*(?:;(.*))?$/;
|
||||
|
||||
function parseLanguage(
|
||||
str: string,
|
||||
i: number,
|
||||
): LanguageSpecificity | undefined {
|
||||
const match = SIMPLE_LANGUAGE_REGEXP.exec(str);
|
||||
if (!match) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const [, prefix, suffix] = match;
|
||||
const full = suffix ? `${prefix}-${suffix}` : prefix;
|
||||
|
||||
let q = 1;
|
||||
if (match[3]) {
|
||||
const params = match[3].split(";");
|
||||
for (const param of params) {
|
||||
const [key, value] = param.trim().split("=");
|
||||
if (key === "q") {
|
||||
q = parseFloat(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { prefix, suffix, full, q, i };
|
||||
}
|
||||
|
||||
function parseAcceptLanguage(accept: string): LanguageSpecificity[] {
|
||||
const accepts = accept.split(",");
|
||||
const result: LanguageSpecificity[] = [];
|
||||
|
||||
for (let i = 0; i < accepts.length; i++) {
|
||||
const language = parseLanguage(accepts[i].trim(), i);
|
||||
if (language) {
|
||||
result.push(language);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function specify(
|
||||
language: string,
|
||||
spec: LanguageSpecificity,
|
||||
i: number,
|
||||
): Specificity | undefined {
|
||||
const p = parseLanguage(language, i);
|
||||
if (!p) {
|
||||
return undefined;
|
||||
}
|
||||
let s = 0;
|
||||
if (spec.full.toLowerCase() === p.full.toLowerCase()) {
|
||||
s |= 4;
|
||||
} else if (spec.prefix.toLowerCase() === p.prefix.toLowerCase()) {
|
||||
s |= 2;
|
||||
} else if (spec.full.toLowerCase() === p.prefix.toLowerCase()) {
|
||||
s |= 1;
|
||||
} else if (spec.full !== "*") {
|
||||
return;
|
||||
}
|
||||
|
||||
return { i, o: spec.i, q: spec.q, s };
|
||||
}
|
||||
|
||||
function getLanguagePriority(
|
||||
language: string,
|
||||
accepted: LanguageSpecificity[],
|
||||
index: number,
|
||||
): Specificity {
|
||||
let priority: Specificity = { i: -1, o: -1, q: 0, s: 0 };
|
||||
for (const accepts of accepted) {
|
||||
const spec = specify(language, accepts, index);
|
||||
if (
|
||||
spec &&
|
||||
((priority.s ?? 0) - (spec.s ?? 0) || priority.q - spec.q ||
|
||||
(priority.o ?? 0) - (spec.o ?? 0)) < 0
|
||||
) {
|
||||
priority = spec;
|
||||
}
|
||||
}
|
||||
return priority;
|
||||
}
|
||||
|
||||
export function preferredLanguages(
|
||||
accept = "*",
|
||||
provided?: string[],
|
||||
): string[] {
|
||||
const accepts = parseAcceptLanguage(accept);
|
||||
|
||||
if (!provided) {
|
||||
return accepts
|
||||
.filter(isQuality)
|
||||
.sort(compareSpecs)
|
||||
.map((spec) => spec.full);
|
||||
}
|
||||
|
||||
const priorities = provided
|
||||
.map((type, index) => getLanguagePriority(type, accepts, index));
|
||||
|
||||
return priorities
|
||||
.filter(isQuality)
|
||||
.sort(compareSpecs)
|
||||
.map((priority) => provided[priorities.indexOf(priority)]);
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
/*!
|
||||
* Adapted directly from negotiator at https://github.com/jshttp/negotiator/
|
||||
* which is licensed as follows:
|
||||
*
|
||||
* (The MIT License)
|
||||
*
|
||||
* Copyright (c) 2012-2014 Federico Romero
|
||||
* Copyright (c) 2012-2014 Isaac Z. Schlueter
|
||||
* Copyright (c) 2014-2015 Douglas Christopher Wilson
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
import { compareSpecs, isQuality, Specificity } from "./common.ts";
|
||||
|
||||
interface MediaTypeSpecificity extends Specificity {
|
||||
type: string;
|
||||
subtype: string;
|
||||
params: { [param: string]: string | undefined };
|
||||
}
|
||||
|
||||
const simpleMediaTypeRegExp = /^\s*([^\s\/;]+)\/([^;\s]+)\s*(?:;(.*))?$/;
|
||||
|
||||
function quoteCount(str: string): number {
|
||||
let count = 0;
|
||||
let index = 0;
|
||||
|
||||
while ((index = str.indexOf(`"`, index)) !== -1) {
|
||||
count++;
|
||||
index++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
function splitMediaTypes(accept: string): string[] {
|
||||
const accepts = accept.split(",");
|
||||
|
||||
let j = 0;
|
||||
for (let i = 1; i < accepts.length; i++) {
|
||||
if (quoteCount(accepts[j]) % 2 === 0) {
|
||||
accepts[++j] = accepts[i];
|
||||
} else {
|
||||
accepts[j] += `,${accepts[i]}`;
|
||||
}
|
||||
}
|
||||
|
||||
accepts.length = j + 1;
|
||||
|
||||
return accepts;
|
||||
}
|
||||
|
||||
function splitParameters(str: string): string[] {
|
||||
const parameters = str.split(";");
|
||||
|
||||
let j = 0;
|
||||
for (let i = 1; i < parameters.length; i++) {
|
||||
if (quoteCount(parameters[j]) % 2 === 0) {
|
||||
parameters[++j] = parameters[i];
|
||||
} else {
|
||||
parameters[j] += `;${parameters[i]}`;
|
||||
}
|
||||
}
|
||||
|
||||
parameters.length = j + 1;
|
||||
|
||||
return parameters.map((p) => p.trim());
|
||||
}
|
||||
|
||||
function splitKeyValuePair(str: string): [string, string | undefined] {
|
||||
const [key, value] = str.split("=");
|
||||
return [key.toLowerCase(), value];
|
||||
}
|
||||
|
||||
function parseMediaType(
|
||||
str: string,
|
||||
i: number,
|
||||
): MediaTypeSpecificity | undefined {
|
||||
const match = simpleMediaTypeRegExp.exec(str);
|
||||
|
||||
if (!match) {
|
||||
return;
|
||||
}
|
||||
|
||||
const params: { [param: string]: string | undefined } = Object.create(null);
|
||||
let q = 1;
|
||||
const [, type, subtype, parameters] = match;
|
||||
|
||||
if (parameters) {
|
||||
const kvps = splitParameters(parameters).map(splitKeyValuePair);
|
||||
|
||||
for (const [key, val] of kvps) {
|
||||
const value = val && val[0] === `"` && val[val.length - 1] === `"`
|
||||
? val.slice(1, val.length - 1)
|
||||
: val;
|
||||
|
||||
if (key === "q" && value) {
|
||||
q = parseFloat(value);
|
||||
break;
|
||||
}
|
||||
|
||||
params[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return { type, subtype, params, q, i };
|
||||
}
|
||||
|
||||
function parseAccept(accept: string): MediaTypeSpecificity[] {
|
||||
const accepts = splitMediaTypes(accept);
|
||||
|
||||
const mediaTypes: MediaTypeSpecificity[] = [];
|
||||
for (let i = 0; i < accepts.length; i++) {
|
||||
const mediaType = parseMediaType(accepts[i].trim(), i);
|
||||
|
||||
if (mediaType) {
|
||||
mediaTypes.push(mediaType);
|
||||
}
|
||||
}
|
||||
|
||||
return mediaTypes;
|
||||
}
|
||||
|
||||
function getFullType(spec: MediaTypeSpecificity) {
|
||||
return `${spec.type}/${spec.subtype}`;
|
||||
}
|
||||
|
||||
function specify(
|
||||
type: string,
|
||||
spec: MediaTypeSpecificity,
|
||||
index: number,
|
||||
): Specificity | undefined {
|
||||
const p = parseMediaType(type, index);
|
||||
|
||||
if (!p) {
|
||||
return;
|
||||
}
|
||||
|
||||
let s = 0;
|
||||
|
||||
if (spec.type.toLowerCase() === p.type.toLowerCase()) {
|
||||
s |= 4;
|
||||
} else if (spec.type !== "*") {
|
||||
return;
|
||||
}
|
||||
|
||||
if (spec.subtype.toLowerCase() === p.subtype.toLowerCase()) {
|
||||
s |= 2;
|
||||
} else if (spec.subtype !== "*") {
|
||||
return;
|
||||
}
|
||||
|
||||
const keys = Object.keys(spec.params);
|
||||
if (keys.length) {
|
||||
if (
|
||||
keys.every((key) =>
|
||||
(spec.params[key] || "").toLowerCase() ===
|
||||
(p.params[key] || "").toLowerCase()
|
||||
)
|
||||
) {
|
||||
s |= 1;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
i: index,
|
||||
o: spec.o,
|
||||
q: spec.q,
|
||||
s,
|
||||
};
|
||||
}
|
||||
|
||||
function getMediaTypePriority(
|
||||
type: string,
|
||||
accepted: MediaTypeSpecificity[],
|
||||
index: number,
|
||||
) {
|
||||
let priority: Specificity = { o: -1, q: 0, s: 0, i: index };
|
||||
|
||||
for (const accepts of accepted) {
|
||||
const spec = specify(type, accepts, index);
|
||||
|
||||
if (
|
||||
spec &&
|
||||
((priority.s || 0) - (spec.s || 0) ||
|
||||
(priority.q || 0) - (spec.q || 0) ||
|
||||
(priority.o || 0) - (spec.o || 0)) < 0
|
||||
) {
|
||||
priority = spec;
|
||||
}
|
||||
}
|
||||
|
||||
return priority;
|
||||
}
|
||||
|
||||
export function preferredMediaTypes(
|
||||
accept?: string | null,
|
||||
provided?: string[],
|
||||
): string[] {
|
||||
const accepts = parseAccept(accept === undefined ? "*/*" : accept || "");
|
||||
|
||||
if (!provided) {
|
||||
return accepts
|
||||
.filter(isQuality)
|
||||
.sort(compareSpecs)
|
||||
.map(getFullType);
|
||||
}
|
||||
|
||||
const priorities = provided.map((type, index) => {
|
||||
return getMediaTypePriority(type, accepts, index);
|
||||
});
|
||||
|
||||
return priorities
|
||||
.filter(isQuality)
|
||||
.sort(compareSpecs)
|
||||
.map((priority) => provided[priorities.indexOf(priority)]);
|
||||
}
|
||||
Reference in new issue
Block a user