2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-09-02 17:07:11 -04:00
|
|
|
import { TypedArray } from "./types.ts";
|
|
|
|
import { window } from "./window.ts";
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-08-17 16:34:30 -04:00
|
|
|
let logDebug = false;
|
2019-02-02 18:27:53 -05:00
|
|
|
let logSource = "JS";
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2019-02-02 18:27:53 -05:00
|
|
|
export function setLogDebug(debug: boolean, source?: string): void {
|
2018-08-17 16:34:30 -04:00
|
|
|
logDebug = debug;
|
2019-02-02 18:27:53 -05:00
|
|
|
if (source) {
|
|
|
|
logSource = source;
|
|
|
|
}
|
2018-08-17 16:34:30 -04:00
|
|
|
}
|
2018-07-06 11:20:35 -04:00
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Debug logging for deno.
|
2018-09-14 04:48:12 -04:00
|
|
|
* Enable with the `--log-debug` or `-D` command line flag.
|
2018-09-04 15:23:38 -04:00
|
|
|
* @internal
|
|
|
|
*/
|
2019-03-09 12:30:38 -05:00
|
|
|
export function log(...args: unknown[]): void {
|
2018-08-17 16:34:30 -04:00
|
|
|
if (logDebug) {
|
2019-09-15 11:04:05 -04:00
|
|
|
// if we destructure `console` off `window` too early, we don't bind to
|
|
|
|
// the right console, therefore we don't log anything out.
|
|
|
|
window.console.log(`DEBUG ${logSource} -`, ...args);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2019-11-13 13:42:34 -05:00
|
|
|
export function assert(cond: unknown, msg = "assert"): asserts cond {
|
2018-07-06 11:20:35 -04:00
|
|
|
if (!cond) {
|
2018-07-16 23:25:50 -04:00
|
|
|
throw Error(msg);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-07-06 11:20:35 -04:00
|
|
|
export function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
|
|
|
|
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
|
|
|
|
return ab as ArrayBuffer;
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-07-06 11:20:35 -04:00
|
|
|
export function arrayToStr(ui8: Uint8Array): string {
|
|
|
|
return String.fromCharCode(...ui8);
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** A `Resolvable` is a Promise with the `reject` and `resolve` functions
|
2018-09-04 15:23:38 -04:00
|
|
|
* placed as methods on the promise object itself. It allows you to do:
|
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* const p = createResolvable<number>();
|
|
|
|
* // ...
|
|
|
|
* p.resolve(42);
|
2018-09-04 15:23:38 -04:00
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* It'd be prettier to make `Resolvable` a class that inherits from `Promise`,
|
2018-09-04 15:23:38 -04:00
|
|
|
* rather than an interface. This is possible in ES2016, however typescript
|
|
|
|
* produces broken code when targeting ES5 code.
|
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* 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
|
2018-09-04 15:23:38 -04:00
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
|
2018-08-15 12:40:30 -04:00
|
|
|
export interface ResolvableMethods<T> {
|
2018-07-06 11:20:35 -04:00
|
|
|
resolve: (value?: T | PromiseLike<T>) => void;
|
2019-03-09 12:30:38 -05:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2018-07-06 11:20:35 -04:00
|
|
|
reject: (reason?: any) => void;
|
|
|
|
}
|
2018-08-15 12:40:30 -04:00
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-08-15 20:57:36 -04:00
|
|
|
export type Resolvable<T> = Promise<T> & ResolvableMethods<T>;
|
2018-08-15 12:40:30 -04:00
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-07-06 11:20:35 -04:00
|
|
|
export function createResolvable<T>(): Resolvable<T> {
|
2018-08-15 12:40:30 -04:00
|
|
|
let methods: ResolvableMethods<T>;
|
2019-11-13 13:42:34 -05:00
|
|
|
const promise = new Promise<T>((resolve, reject): void => {
|
|
|
|
methods = { resolve, reject };
|
|
|
|
});
|
2018-08-15 12:40:30 -04:00
|
|
|
// TypeScript doesn't know that the Promise callback occurs synchronously
|
|
|
|
// therefore use of not null assertion (`!`)
|
|
|
|
return Object.assign(promise, methods!) as Resolvable<T>;
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
2018-08-19 15:04:27 -04:00
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-08-19 15:04:27 -04:00
|
|
|
export function notImplemented(): never {
|
|
|
|
throw new Error("Not implemented");
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-08-19 15:04:27 -04:00
|
|
|
export function unreachable(): never {
|
|
|
|
throw new Error("Code not reachable");
|
|
|
|
}
|
2018-09-05 22:13:36 -04:00
|
|
|
|
2018-09-14 08:45:50 -04:00
|
|
|
// @internal
|
2018-09-05 22:13:36 -04:00
|
|
|
export function hexdump(u8: Uint8Array): string {
|
|
|
|
return Array.prototype.map
|
2019-11-13 13:42:34 -05:00
|
|
|
.call(u8, (x: number): string => {
|
|
|
|
return ("00" + x.toString(16)).slice(-2);
|
|
|
|
})
|
2018-09-05 22:13:36 -04:00
|
|
|
.join(" ");
|
|
|
|
}
|
2018-09-14 08:45:50 -04:00
|
|
|
|
|
|
|
// @internal
|
|
|
|
export function containsOnlyASCII(str: string): boolean {
|
|
|
|
if (typeof str !== "string") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return /^[\x00-\x7F]*$/.test(str);
|
|
|
|
}
|
2018-10-05 12:16:24 -04:00
|
|
|
|
2018-11-14 21:19:38 -05:00
|
|
|
const TypedArrayConstructor = Object.getPrototypeOf(Uint8Array);
|
|
|
|
export function isTypedArray(x: unknown): x is TypedArray {
|
|
|
|
return x instanceof TypedArrayConstructor;
|
|
|
|
}
|
2018-11-15 23:07:40 -05:00
|
|
|
|
|
|
|
// Returns whether o is an object, not null, and not a function.
|
|
|
|
// @internal
|
|
|
|
export function isObject(o: unknown): o is object {
|
|
|
|
return o != null && typeof o === "object";
|
|
|
|
}
|
2018-12-23 23:07:58 -05:00
|
|
|
|
2019-06-14 11:46:07 -04:00
|
|
|
// Returns whether o is iterable.
|
|
|
|
export function isIterable<T, P extends keyof T, K extends T[P]>(
|
|
|
|
o: T
|
|
|
|
): o is T & Iterable<[P, K]> {
|
|
|
|
// checks for null and undefined
|
|
|
|
if (o == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
typeof ((o as unknown) as Iterable<[P, K]>)[Symbol.iterator] === "function"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-23 23:07:58 -05:00
|
|
|
// @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);
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 10:02:44 -05:00
|
|
|
|
2019-04-19 20:39:54 -04:00
|
|
|
// @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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-05 10:02:44 -05:00
|
|
|
// 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");
|
|
|
|
}
|
2019-04-18 21:56:33 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2019-05-01 05:08:12 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Split a number into two parts: lower 32 bit and higher 32 bit
|
|
|
|
* (as if the number is represented as uint64.)
|
|
|
|
*
|
|
|
|
* @param n Number to split.
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export function splitNumberToParts(n: number): number[] {
|
|
|
|
// JS bitwise operators (OR, SHIFT) operate as if number is uint32.
|
|
|
|
const lower = n | 0;
|
|
|
|
// This is also faster than Math.floor(n / 0x100000000) in V8.
|
|
|
|
const higher = (n - lower) / 0x100000000;
|
|
|
|
return [lower, higher];
|
|
|
|
}
|
2019-11-13 10:35:56 -05:00
|
|
|
|
|
|
|
/** Return the common path shared by the `paths`.
|
|
|
|
*
|
|
|
|
* @param paths The set of paths to compare.
|
|
|
|
* @param sep An optional separator to use. Defaults to `/`.
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export function commonPath(paths: string[], sep = "/"): string {
|
|
|
|
const [first = "", ...remaining] = paths;
|
|
|
|
if (first === "" || remaining.length === 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
const parts = first.split(sep);
|
|
|
|
|
|
|
|
let endOfPrefix = parts.length;
|
|
|
|
for (const path of remaining) {
|
|
|
|
const compare = path.split(sep);
|
|
|
|
for (let i = 0; i < endOfPrefix; i++) {
|
|
|
|
if (compare[i] !== parts[i]) {
|
|
|
|
endOfPrefix = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endOfPrefix === 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const prefix = parts.slice(0, endOfPrefix).join(sep);
|
|
|
|
return prefix.endsWith(sep) ? prefix : `${prefix}${sep}`;
|
|
|
|
}
|
2019-11-20 11:02:08 -05:00
|
|
|
|
|
|
|
/** Utility function to turn the number of bytes into a human readable
|
|
|
|
* unit */
|
|
|
|
export function humanFileSize(bytes: number): string {
|
|
|
|
const thresh = 1000;
|
|
|
|
if (Math.abs(bytes) < thresh) {
|
|
|
|
return bytes + " B";
|
|
|
|
}
|
|
|
|
const units = ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
|
|
let u = -1;
|
|
|
|
do {
|
|
|
|
bytes /= thresh;
|
|
|
|
++u;
|
|
|
|
} while (Math.abs(bytes) >= thresh && u < units.length - 1);
|
|
|
|
return `${bytes.toFixed(1)} ${units[u]}`;
|
|
|
|
}
|