1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-04 08:54:20 -05:00
denoland-deno/cli/js/web/headers.ts

152 lines
4.7 KiB
TypeScript
Raw Normal View History

2020-01-02 15:13:47 -05:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
2020-03-05 07:05:41 -05:00
import { DomIterableMixin } from "./dom_iterable.ts";
import { requiredArguments } from "./util.ts";
import { customInspect } from "./console.ts";
2018-11-02 21:43:37 -04:00
// From node-fetch
// Copyright (c) 2016 David Frank. MIT License.
const invalidTokenRegex = /[^\^_`a-zA-Z\-0-9!#$%&'*+.|~]/;
const invalidHeaderCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2020-04-13 22:46:23 -04:00
function isHeaders(value: any): value is Headers {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
2018-11-02 21:43:37 -04:00
return value instanceof Headers;
}
const headerMap = Symbol("header map");
// TODO: headerGuard? Investigate if it is needed
// node-fetch did not implement this but it is in the spec
function normalizeParams(name: string, value?: string): string[] {
name = String(name).toLowerCase();
value = String(value).trim();
return [name, value];
}
2018-11-02 21:43:37 -04:00
// The following name/value validations are copied from
// https://github.com/bitinn/node-fetch/blob/master/src/headers.js
// Copyright (c) 2016 David Frank. MIT License.
function validateName(name: string): void {
if (invalidTokenRegex.test(name) || name === "") {
throw new TypeError(`${name} is not a legal HTTP header name`);
}
}
2018-12-29 07:30:11 -05:00
function validateValue(value: string): void {
if (invalidHeaderCharRegex.test(value)) {
throw new TypeError(`${value} is not a legal HTTP header value`);
}
}
// ref: https://fetch.spec.whatwg.org/#dom-headers
class HeadersBase {
[headerMap]: Map<string, string>;
2020-04-13 22:46:23 -04:00
constructor(init?: HeadersInit) {
2018-11-02 21:43:37 -04:00
if (init === null) {
throw new TypeError(
"Failed to construct 'Headers'; The provided value was not valid"
);
} else if (isHeaders(init)) {
this[headerMap] = new Map(init);
} else {
this[headerMap] = new Map();
if (Array.isArray(init)) {
2018-12-29 07:30:11 -05:00
for (const tuple of init) {
// If header does not contain exactly two items,
// then throw a TypeError.
// ref: https://fetch.spec.whatwg.org/#concept-headers-fill
requiredArguments(
"Headers.constructor tuple array argument",
tuple.length,
2
);
2018-12-29 07:30:11 -05:00
const [name, value] = normalizeParams(tuple[0], tuple[1]);
validateName(name);
validateValue(value);
2018-11-02 21:43:37 -04:00
const existingValue = this[headerMap].get(name);
this[headerMap].set(
name,
existingValue ? `${existingValue}, ${value}` : value
);
}
} else if (init) {
const names = Object.keys(init);
for (const rawName of names) {
const rawValue = init[rawName];
const [name, value] = normalizeParams(rawName, rawValue);
validateName(name);
validateValue(value);
2018-11-02 21:43:37 -04:00
this[headerMap].set(name, value);
}
}
}
}
[customInspect](): string {
let headerSize = this[headerMap].size;
let output = "";
this[headerMap].forEach((value, key) => {
const prefix = headerSize === this[headerMap].size ? " " : "";
const postfix = headerSize === 1 ? " " : ", ";
output = output + `${prefix}${key}: ${value}${postfix}`;
headerSize--;
});
return `Headers {${output}}`;
}
// ref: https://fetch.spec.whatwg.org/#concept-headers-append
2018-11-02 21:43:37 -04:00
append(name: string, value: string): void {
requiredArguments("Headers.append", arguments.length, 2);
const [newname, newvalue] = normalizeParams(name, value);
validateName(newname);
validateValue(newvalue);
2018-11-02 21:43:37 -04:00
const v = this[headerMap].get(newname);
const str = v ? `${v}, ${newvalue}` : newvalue;
this[headerMap].set(newname, str);
}
delete(name: string): void {
requiredArguments("Headers.delete", arguments.length, 1);
const [newname] = normalizeParams(name);
validateName(newname);
2018-11-02 21:43:37 -04:00
this[headerMap].delete(newname);
}
get(name: string): string | null {
requiredArguments("Headers.get", arguments.length, 1);
const [newname] = normalizeParams(name);
validateName(newname);
2018-11-02 21:43:37 -04:00
const value = this[headerMap].get(newname);
return value || null;
}
has(name: string): boolean {
requiredArguments("Headers.has", arguments.length, 1);
const [newname] = normalizeParams(name);
validateName(newname);
2018-11-02 21:43:37 -04:00
return this[headerMap].has(newname);
}
set(name: string, value: string): void {
requiredArguments("Headers.set", arguments.length, 2);
const [newname, newvalue] = normalizeParams(name, value);
validateName(newname);
validateValue(newvalue);
2018-11-02 21:43:37 -04:00
this[headerMap].set(newname, newvalue);
}
get [Symbol.toStringTag](): string {
return "Headers";
}
2018-11-02 21:43:37 -04:00
}
// @internal
2020-04-13 22:46:23 -04:00
export class HeadersImpl extends DomIterableMixin<
string,
string,
typeof HeadersBase
>(HeadersBase, headerMap) {}