diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2018-11-02 21:43:37 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-11-02 20:50:01 -0700 |
commit | 6446bc532840319ac9603c016e2ef419094fdeec (patch) | |
tree | 401e4a70956602dbbb032d942226c64ae2fbb5c4 /js/headers.ts | |
parent | ee24254bade280ea3c0121c074951396863393ac (diff) |
Move fetch headers into its own file.
Diffstat (limited to 'js/headers.ts')
-rw-r--r-- | js/headers.ts | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/js/headers.ts b/js/headers.ts new file mode 100644 index 000000000..6892b0ef2 --- /dev/null +++ b/js/headers.ts @@ -0,0 +1,85 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +import * as domTypes from "./dom_types"; +import { DomIterableMixin } from "./mixins/dom_iterable"; + +// tslint:disable-next-line:no-any +function isHeaders(value: any): value is domTypes.Headers { + return value instanceof Headers; +} + +const headerMap = Symbol("header map"); + +// ref: https://fetch.spec.whatwg.org/#dom-headers +class HeadersBase { + private [headerMap]: Map<string, string>; + + private _normalizeParams(name: string, value?: string): string[] { + name = String(name).toLowerCase(); + value = String(value).trim(); + return [name, value]; + } + + constructor(init?: domTypes.HeadersInit) { + 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)) { + for (const [rawName, rawValue] of init) { + const [name, value] = this._normalizeParams(rawName, rawValue); + 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] = this._normalizeParams(rawName, rawValue); + this[headerMap].set(name, value); + } + } + } + } + + append(name: string, value: string): void { + const [newname, newvalue] = this._normalizeParams(name, value); + const v = this[headerMap].get(newname); + const str = v ? `${v}, ${newvalue}` : newvalue; + this[headerMap].set(newname, str); + } + + delete(name: string): void { + const [newname] = this._normalizeParams(name); + this[headerMap].delete(newname); + } + + get(name: string): string | null { + const [newname] = this._normalizeParams(name); + const value = this[headerMap].get(newname); + return value || null; + } + + has(name: string): boolean { + const [newname] = this._normalizeParams(name); + return this[headerMap].has(newname); + } + + set(name: string, value: string): void { + const [newname, newvalue] = this._normalizeParams(name, value); + this[headerMap].set(newname, newvalue); + } +} + +// @internal +// tslint:disable-next-line:variable-name +export const Headers = DomIterableMixin<string, string, typeof HeadersBase>( + HeadersBase, + headerMap +); |