From 637a9ecd6a3f3311601b4de36f6035ae63297f77 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 11 Apr 2020 17:19:36 -0400 Subject: [PATCH] dedup URLSearchParams, URL, Location, DOMStringList (#4719) --- cli/js/globals.ts | 4 +- cli/js/web/body.ts | 2 +- cli/js/web/dom_types.d.ts | 68 --------------------------------- cli/js/web/dom_util.ts | 6 +-- cli/js/web/fetch.ts | 2 - cli/js/web/location.ts | 2 - cli/js/web/url.ts | 5 +-- cli/js/web/url_search_params.ts | 7 ++-- 8 files changed, 10 insertions(+), 86 deletions(-) diff --git a/cli/js/globals.ts b/cli/js/globals.ts index 059a70ee775813..7e708d0188014d 100644 --- a/cli/js/globals.ts +++ b/cli/js/globals.ts @@ -213,8 +213,8 @@ export const windowOrWorkerGlobalScopeProperties = { DOMException: nonEnumerable(domException.DOMExceptionImpl), Event: nonEnumerable(event.EventImpl), EventTarget: nonEnumerable(eventTarget.EventTargetImpl), - URL: nonEnumerable(url.URL), - URLSearchParams: nonEnumerable(urlSearchParams.URLSearchParams), + URL: nonEnumerable(url.URLImpl), + URLSearchParams: nonEnumerable(urlSearchParams.URLSearchParamsImpl), Headers: nonEnumerable(headers.Headers), FormData: nonEnumerable(formData.FormData), TextEncoder: nonEnumerable(textEncoding.TextEncoder), diff --git a/cli/js/web/body.ts b/cli/js/web/body.ts index 7e398a5354a84b..472f3fda2de656 100644 --- a/cli/js/web/body.ts +++ b/cli/js/web/body.ts @@ -23,7 +23,7 @@ export type BodySource = | Blob | BufferSource | domTypes.FormData - | domTypes.URLSearchParams + | URLSearchParams | domTypes.ReadableStream | string; diff --git a/cli/js/web/dom_types.d.ts b/cli/js/web/dom_types.d.ts index bfdf18b8cb8177..5ec6e8b1a2c5aa 100644 --- a/cli/js/web/dom_types.d.ts +++ b/cli/js/web/dom_types.d.ts @@ -47,29 +47,6 @@ export interface ProgressEventInit extends EventInit { total?: number; } -export class URLSearchParams { - constructor( - init?: string[][] | Record | string | URLSearchParams - ); - append(name: string, value: string): void; - delete(name: string): void; - get(name: string): string | null; - getAll(name: string): string[]; - has(name: string): boolean; - set(name: string, value: string): void; - sort(): void; - toString(): string; - forEach( - callbackfn: (value: string, key: string, parent: this) => void, - thisArg?: any - ): void; - [Symbol.iterator](): IterableIterator<[string, string]>; - entries(): IterableIterator<[string, string]>; - keys(): IterableIterator; - values(): IterableIterator; - static toString(): string; -} - export interface UIEventInit extends EventInit { detail?: number; // adjust Window -> Node @@ -654,48 +631,3 @@ export interface ResponseConstructor { error(): Response; redirect(url: string, status?: number): Response; } - -export class DOMStringList { - readonly length: number; - contains(string: string): boolean; - item(index: number): string | null; - [index: number]: string; - [Symbol.iterator](): IterableIterator; -} - -export class Location { - readonly ancestorOrigins: DOMStringList; - hash: string; - host: string; - hostname: string; - href: string; - toString(): string; - readonly origin: string; - pathname: string; - port: string; - protocol: string; - search: string; - assign(url: string): void; - reload(): void; - replace(url: string): void; -} - -export class URL { - constructor(url: string, base?: string | URL); - hash: string; - host: string; - hostname: string; - href: string; - toString(): string; - readonly origin: string; - password: string; - pathname: string; - port: string; - protocol: string; - search: string; - readonly searchParams: URLSearchParams; - username: string; - toJSON(): string; - static createObjectURL(object: any): string; - static revokeObjectURL(url: string): void; -} diff --git a/cli/js/web/dom_util.ts b/cli/js/web/dom_util.ts index d12593e8e88da4..4b9ce3f50abe10 100644 --- a/cli/js/web/dom_util.ts +++ b/cli/js/web/dom_util.ts @@ -1,8 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import * as domTypes from "./dom_types.d.ts"; - -export function getDOMStringList(arr: string[]): domTypes.DOMStringList { +export function getDOMStringList(arr: string[]): DOMStringList { Object.defineProperties(arr, { contains: { value(searchElement: string): boolean { @@ -16,5 +14,5 @@ export function getDOMStringList(arr: string[]): domTypes.DOMStringList { }, }, }); - return arr as string[] & domTypes.DOMStringList; + return arr as string[] & DOMStringList; } diff --git a/cli/js/web/fetch.ts b/cli/js/web/fetch.ts index da6482ba3ee487..5a5ac5cfeba37c 100644 --- a/cli/js/web/fetch.ts +++ b/cli/js/web/fetch.ts @@ -10,8 +10,6 @@ import { read } from "../ops/io.ts"; import { close } from "../ops/resources.ts"; import { Buffer } from "../buffer.ts"; import { FormData } from "./form_data.ts"; -import { URL } from "./url.ts"; -import { URLSearchParams } from "./url_search_params.ts"; import { fetch as opFetch, FetchResponse } from "../ops/fetch.ts"; import { DomFileImpl } from "./dom_file.ts"; diff --git a/cli/js/web/location.ts b/cli/js/web/location.ts index 9b59842b74bc22..fe0ca2da288a5a 100644 --- a/cli/js/web/location.ts +++ b/cli/js/web/location.ts @@ -1,7 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { URL } from "./url.ts"; import { notImplemented } from "../util.ts"; -import { DOMStringList, Location } from "./dom_types.d.ts"; import { getDOMStringList } from "./dom_util.ts"; export class LocationImpl implements Location { diff --git a/cli/js/web/url.ts b/cli/js/web/url.ts index 374997a1ec2198..374ddd252c20b1 100644 --- a/cli/js/web/url.ts +++ b/cli/js/web/url.ts @@ -1,7 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { customInspect } from "./console.ts"; -import * as domTypes from "./dom_types.d.ts"; -import { urls, URLSearchParams } from "./url_search_params.ts"; +import { urls } from "./url_search_params.ts"; import { getRandomValues } from "../ops/get_random_values.ts"; interface URLParts { @@ -139,7 +138,7 @@ function resolvePathFromBase(path: string, basePath: string): string { /** @internal */ export const parts = new WeakMap(); -export class URL implements domTypes.URL { +export class URLImpl implements URL { #searchParams!: URLSearchParams; [customInspect](): string { diff --git a/cli/js/web/url_search_params.ts b/cli/js/web/url_search_params.ts index 0e41bdbf2e00ed..221a70ab12b1ac 100644 --- a/cli/js/web/url_search_params.ts +++ b/cli/js/web/url_search_params.ts @@ -1,6 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import * as domTypes from "./dom_types.d.ts"; -import { URL, parts } from "./url.ts"; +import { parts } from "./url.ts"; import { isIterable, requiredArguments } from "./util.ts"; /** @internal */ @@ -45,7 +44,7 @@ function handleArrayInitialization( } } -export class URLSearchParams implements domTypes.URLSearchParams { +export class URLSearchParamsImpl implements URLSearchParams { #params: Array<[string, string]> = []; constructor(init: string | string[][] | Record = "") { @@ -63,7 +62,7 @@ export class URLSearchParams implements domTypes.URLSearchParams { return; } - if (init instanceof URLSearchParams) { + if (init instanceof URLSearchParamsImpl) { this.#params = [...init.#params]; return; }