diff options
Diffstat (limited to 'cli/js/web')
-rw-r--r-- | cli/js/web/console.ts | 3 | ||||
-rw-r--r-- | cli/js/web/console_table.ts | 2 | ||||
-rw-r--r-- | cli/js/web/custom_event.ts | 2 | ||||
-rw-r--r-- | cli/js/web/dom_iterable.ts | 2 | ||||
-rw-r--r-- | cli/js/web/event.ts | 2 | ||||
-rw-r--r-- | cli/js/web/event_target.ts | 2 | ||||
-rw-r--r-- | cli/js/web/fetch.ts | 8 | ||||
-rw-r--r-- | cli/js/web/form_data.ts | 2 | ||||
-rw-r--r-- | cli/js/web/headers.ts | 2 | ||||
-rw-r--r-- | cli/js/web/url_search_params.ts | 2 | ||||
-rw-r--r-- | cli/js/web/util.ts | 78 |
11 files changed, 89 insertions, 16 deletions
diff --git a/cli/js/web/console.ts b/cli/js/web/console.ts index 601d5fd09..8a34d62a1 100644 --- a/cli/js/web/console.ts +++ b/cli/js/web/console.ts @@ -1,6 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { isTypedArray } from "../util.ts"; -import { TypedArray } from "../types.ts"; +import { isTypedArray, TypedArray } from "./util.ts"; import { TextEncoder } from "./text_encoding.ts"; import { File, stdout } from "../files.ts"; import { cliTable } from "./console_table.ts"; diff --git a/cli/js/web/console_table.ts b/cli/js/web/console_table.ts index 276d77f1d..7e698f712 100644 --- a/cli/js/web/console_table.ts +++ b/cli/js/web/console_table.ts @@ -2,7 +2,7 @@ // Forked from Node's lib/internal/cli_table.js import { TextEncoder } from "./text_encoding.ts"; -import { hasOwnProperty } from "../util.ts"; +import { hasOwnProperty } from "./util.ts"; const encoder = new TextEncoder(); diff --git a/cli/js/web/custom_event.ts b/cli/js/web/custom_event.ts index 6c8a3c19b..2d94df56d 100644 --- a/cli/js/web/custom_event.ts +++ b/cli/js/web/custom_event.ts @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as domTypes from "./dom_types.ts"; import * as event from "./event.ts"; -import { getPrivateValue, requiredArguments } from "../util.ts"; +import { getPrivateValue, requiredArguments } from "./util.ts"; // WeakMaps are recommended for private attributes (see MDN link below) // https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Add-on_SDK/Guides/Contributor_s_Guide/Private_Properties#Using_WeakMaps diff --git a/cli/js/web/dom_iterable.ts b/cli/js/web/dom_iterable.ts index bd8e7d8cd..f365d555f 100644 --- a/cli/js/web/dom_iterable.ts +++ b/cli/js/web/dom_iterable.ts @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // eslint-disable-next-line @typescript-eslint/no-unused-vars import { DomIterable } from "./dom_types.ts"; -import { requiredArguments } from "../util.ts"; +import { requiredArguments } from "./util.ts"; import { exposeForTest } from "../internals.ts"; // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/cli/js/web/event.ts b/cli/js/web/event.ts index e365fb6b2..a30e33447 100644 --- a/cli/js/web/event.ts +++ b/cli/js/web/event.ts @@ -1,6 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as domTypes from "./dom_types.ts"; -import { getPrivateValue, requiredArguments } from "../util.ts"; +import { getPrivateValue, requiredArguments } from "./util.ts"; // WeakMaps are recommended for private attributes (see MDN link below) // https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Add-on_SDK/Guides/Contributor_s_Guide/Private_Properties#Using_WeakMaps diff --git a/cli/js/web/event_target.ts b/cli/js/web/event_target.ts index 09e80a731..03557526a 100644 --- a/cli/js/web/event_target.ts +++ b/cli/js/web/event_target.ts @@ -1,6 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as domTypes from "./dom_types.ts"; -import { hasOwnProperty, requiredArguments } from "../util.ts"; +import { hasOwnProperty, requiredArguments } from "./util.ts"; import { getRoot, isNode, diff --git a/cli/js/web/fetch.ts b/cli/js/web/fetch.ts index d9918ba14..daa926e51 100644 --- a/cli/js/web/fetch.ts +++ b/cli/js/web/fetch.ts @@ -1,10 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { - assert, - createResolvable, - notImplemented, - isTypedArray -} from "../util.ts"; +import { assert, createResolvable, notImplemented } from "../util.ts"; +import { isTypedArray } from "./util.ts"; import * as domTypes from "./dom_types.ts"; import { TextDecoder, TextEncoder } from "./text_encoding.ts"; import { DenoBlob, bytesSymbol as blobBytesSymbol } from "./blob.ts"; diff --git a/cli/js/web/form_data.ts b/cli/js/web/form_data.ts index 9c0590c32..461210b9d 100644 --- a/cli/js/web/form_data.ts +++ b/cli/js/web/form_data.ts @@ -3,7 +3,7 @@ import * as domTypes from "./dom_types.ts"; import * as blob from "./blob.ts"; import * as domFile from "./dom_file.ts"; import { DomIterableMixin } from "./dom_iterable.ts"; -import { requiredArguments } from "../util.ts"; +import { requiredArguments } from "./util.ts"; const dataSymbol = Symbol("data"); diff --git a/cli/js/web/headers.ts b/cli/js/web/headers.ts index 652dd2de6..9ff594224 100644 --- a/cli/js/web/headers.ts +++ b/cli/js/web/headers.ts @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as domTypes from "./dom_types.ts"; import { DomIterableMixin } from "./dom_iterable.ts"; -import { requiredArguments } from "../util.ts"; +import { requiredArguments } from "./util.ts"; import { customInspect } from "./console.ts"; // From node-fetch diff --git a/cli/js/web/url_search_params.ts b/cli/js/web/url_search_params.ts index 2248a5388..ed93045da 100644 --- a/cli/js/web/url_search_params.ts +++ b/cli/js/web/url_search_params.ts @@ -1,6 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { URL } from "./url.ts"; -import { requiredArguments } from "../util.ts"; +import { requiredArguments } from "./util.ts"; // Returns whether o is iterable. // @internal diff --git a/cli/js/web/util.ts b/cli/js/web/util.ts new file mode 100644 index 000000000..b0a050973 --- /dev/null +++ b/cli/js/web/util.ts @@ -0,0 +1,78 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +export type TypedArray = Uint8Array | Float32Array | Int32Array; +const TypedArrayConstructor = Object.getPrototypeOf(Uint8Array); +export function isTypedArray(x: unknown): x is TypedArray { + return x instanceof TypedArrayConstructor; +} + +// @internal +export function requiredArguments( + name: string, + length: number, + required: number +): void { + if (length < required) { + const errMsg = `${name} requires at least ${required} argument${ + required === 1 ? "" : "s" + }, but only ${length} present`; + throw new TypeError(errMsg); + } +} + +// @internal +export function immutableDefine( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + o: any, + p: string | number | symbol, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + value: any +): void { + Object.defineProperty(o, p, { + value, + configurable: false, + writable: false + }); +} + +// Returns values from a WeakMap to emulate private properties in JavaScript +export function getPrivateValue< + K extends object, + V extends object, + W extends keyof V +>(instance: K, weakMap: WeakMap<K, V>, key: W): V[W] { + if (weakMap.has(instance)) { + return weakMap.get(instance)![key]; + } + throw new TypeError("Illegal invocation"); +} + +/** + * Determines whether an object has a property with the specified name. + * Avoid calling prototype builtin `hasOwnProperty` for two reasons: + * + * 1. `hasOwnProperty` is defined on the object as something else: + * + * const options = { + * ending: 'utf8', + * hasOwnProperty: 'foo' + * }; + * options.hasOwnProperty('ending') // throws a TypeError + * + * 2. The object doesn't inherit from `Object.prototype`: + * + * const options = Object.create(null); + * options.ending = 'utf8'; + * options.hasOwnProperty('ending'); // throws a TypeError + * + * @param obj A Object. + * @param v A property name. + * @see https://eslint.org/docs/rules/no-prototype-builtins + * @internal + */ +export function hasOwnProperty<T>(obj: T, v: PropertyKey): boolean { + if (obj == null) { + return false; + } + return Object.prototype.hasOwnProperty.call(obj, v); +} |