2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-08-17 16:34:30 -04:00
|
|
|
import { TypedArray } from "./types";
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-08-17 16:34:30 -04:00
|
|
|
let logDebug = false;
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-08-17 16:34:30 -04:00
|
|
|
export function setLogDebug(debug: boolean): void {
|
|
|
|
logDebug = debug;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2018-07-06 11:20:35 -04:00
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
export function log(...args: any[]): void {
|
2018-08-17 16:34:30 -04:00
|
|
|
if (logDebug) {
|
|
|
|
console.log("DEBUG JS -", ...args);
|
2018-07-06 11:20:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-07-16 23:25:50 -04:00
|
|
|
export function assert(cond: boolean, msg = "assert") {
|
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;
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
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>;
|
2018-07-06 11:20:35 -04:00
|
|
|
const promise = new Promise<T>((resolve, reject) => {
|
|
|
|
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
|
|
|
|
.call(u8, (x: number) => {
|
|
|
|
return ("00" + x.toString(16)).slice(-2);
|
|
|
|
})
|
|
|
|
.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
|
|
|
|
|
|
|
// @internal
|
|
|
|
export interface Deferred {
|
|
|
|
promise: Promise<void>;
|
|
|
|
resolve: Function;
|
|
|
|
reject: Function;
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Create a wrapper around a promise that could be resolved externally. */
|
|
|
|
// @internal
|
2018-10-05 12:16:24 -04:00
|
|
|
export function deferred(): Deferred {
|
|
|
|
let resolve: Function | undefined;
|
|
|
|
let reject: Function | undefined;
|
|
|
|
const promise = new Promise<void>((res, rej) => {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
promise,
|
|
|
|
resolve: resolve!,
|
|
|
|
reject: reject!
|
|
|
|
};
|
|
|
|
}
|
2018-10-19 12:12:36 -04:00
|
|
|
|
|
|
|
/** Create a IterableIterator. */
|
|
|
|
// @internal
|
|
|
|
export class CreateIterableIterator<T> implements IterableIterator<T> {
|
|
|
|
private readonly _iterators: IterableIterator<T>;
|
|
|
|
readonly [Symbol.toStringTag] = "Iterator";
|
|
|
|
|
|
|
|
constructor(iterators: IterableIterator<T>) {
|
|
|
|
this._iterators = iterators;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Symbol.iterator](): IterableIterator<T> {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
next(): IteratorResult<T> {
|
|
|
|
return this._iterators.next();
|
|
|
|
}
|
|
|
|
}
|