// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { TypedArray } from "./types.ts"; let logDebug = false; let logSource = "JS"; // @internal export function setLogDebug(debug: boolean, source?: string): void { logDebug = debug; if (source) { logSource = source; } } /** Debug logging for deno. * Enable with the `--log-debug` or `-D` command line flag. * @internal */ export function log(...args: unknown[]): void { if (logDebug) { // if we destructure `console` off `globalThis` too early, we don't bind to // the right console, therefore we don't log anything out. globalThis.console.log(`DEBUG ${logSource} -`, ...args); } } // @internal export function assert(cond: unknown, msg = "assert"): asserts cond { if (!cond) { throw Error(msg); } } /** A `Resolvable` is a Promise with the `reject` and `resolve` functions * placed as methods on the promise object itself. It allows you to do: * * const p = createResolvable(); * // ... * p.resolve(42); * * It'd be prettier to make `Resolvable` a class that inherits from `Promise`, * rather than an interface. This is possible in ES2016, however typescript * produces broken code when targeting ES5 code. * * At the time of writing, the GitHub issue is closed in favour of a proposed * solution that is awaiting feedback. * * @see https://github.com/Microsoft/TypeScript/issues/15202 * @see https://github.com/Microsoft/TypeScript/issues/15397 * @internal */ export type ResolveFunction = (value?: T | PromiseLike) => void; // eslint-disable-next-line @typescript-eslint/no-explicit-any export type RejectFunction = (reason?: any) => void; export interface ResolvableMethods { resolve: ResolveFunction; reject: RejectFunction; } // @internal export type Resolvable = Promise & ResolvableMethods; // @internal export function createResolvable(): Resolvable { let resolve: ResolveFunction; let reject: RejectFunction; const promise = new Promise((res, rej): void => { resolve = res; reject = rej; }) as Resolvable; promise.resolve = resolve!; promise.reject = reject!; return promise; } // @internal export function notImplemented(): never { throw new Error("Not implemented"); } // @internal export function unreachable(): never { throw new Error("Code not reachable"); } 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, 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(obj: T, v: PropertyKey): boolean { if (obj == null) { return false; } return Object.prototype.hasOwnProperty.call(obj, v); }