2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-11-26 07:50:08 -05:00
|
|
|
import { deferred } from "../async/mod.ts";
|
|
|
|
import { fail } from "../testing/asserts.ts";
|
|
|
|
|
|
|
|
export type BinaryEncodings = "binary";
|
|
|
|
|
|
|
|
export type TextEncodings =
|
|
|
|
| "ascii"
|
|
|
|
| "utf8"
|
|
|
|
| "utf-8"
|
|
|
|
| "utf16le"
|
|
|
|
| "ucs2"
|
|
|
|
| "ucs-2"
|
|
|
|
| "base64"
|
|
|
|
| "latin1"
|
|
|
|
| "hex";
|
|
|
|
|
|
|
|
export type Encodings = BinaryEncodings | TextEncodings;
|
2020-09-21 08:26:41 -04:00
|
|
|
|
2019-11-12 15:51:14 -05:00
|
|
|
export function notImplemented(msg?: string): never {
|
|
|
|
const message = msg ? `Not implemented: ${msg}` : "Not implemented";
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:35:51 -04:00
|
|
|
export type _TextDecoder = typeof TextDecoder.prototype;
|
|
|
|
export const _TextDecoder = TextDecoder;
|
|
|
|
|
|
|
|
export type _TextEncoder = typeof TextEncoder.prototype;
|
|
|
|
export const _TextEncoder = TextEncoder;
|
|
|
|
|
2019-11-12 15:51:14 -05:00
|
|
|
// API helpers
|
|
|
|
|
|
|
|
export type MaybeNull<T> = T | null;
|
|
|
|
export type MaybeDefined<T> = T | undefined;
|
|
|
|
export type MaybeEmpty<T> = T | null | undefined;
|
|
|
|
|
|
|
|
export function intoCallbackAPI<T>(
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2019-11-12 15:51:14 -05:00
|
|
|
func: (...args: any[]) => Promise<T>,
|
|
|
|
cb: MaybeEmpty<(err: MaybeNull<Error>, value: MaybeEmpty<T>) => void>,
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2019-11-12 15:51:14 -05:00
|
|
|
...args: any[]
|
|
|
|
): void {
|
|
|
|
func(...args)
|
2020-03-28 13:03:49 -04:00
|
|
|
.then((value) => cb && cb(null, value))
|
|
|
|
.catch((err) => cb && cb(err, null));
|
2019-11-12 15:51:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function intoCallbackAPIWithIntercept<T1, T2>(
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2019-11-12 15:51:14 -05:00
|
|
|
func: (...args: any[]) => Promise<T1>,
|
|
|
|
interceptor: (v: T1) => T2,
|
|
|
|
cb: MaybeEmpty<(err: MaybeNull<Error>, value: MaybeEmpty<T2>) => void>,
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2019-11-12 15:51:14 -05:00
|
|
|
...args: any[]
|
|
|
|
): void {
|
|
|
|
func(...args)
|
2020-03-28 13:03:49 -04:00
|
|
|
.then((value) => cb && cb(null, interceptor(value)))
|
|
|
|
.catch((err) => cb && cb(err, null));
|
2019-11-12 15:51:14 -05:00
|
|
|
}
|
2020-05-20 10:37:30 -04:00
|
|
|
|
|
|
|
export function spliceOne(list: string[], index: number): void {
|
|
|
|
for (; index + 1 < list.length; index++) list[index] = list[index + 1];
|
|
|
|
list.pop();
|
|
|
|
}
|
2020-06-28 12:27:02 -04:00
|
|
|
|
|
|
|
// Taken from: https://github.com/nodejs/node/blob/ba684805b6c0eded76e5cd89ee00328ac7a59365/lib/internal/util.js#L125
|
|
|
|
// Return undefined if there is no match.
|
|
|
|
// Move the "slow cases" to a separate function to make sure this function gets
|
|
|
|
// inlined properly. That prioritizes the common case.
|
2020-11-26 07:50:08 -05:00
|
|
|
export function normalizeEncoding(
|
|
|
|
enc: string | null,
|
|
|
|
): TextEncodings | undefined {
|
2020-06-28 12:27:02 -04:00
|
|
|
if (enc == null || enc === "utf8" || enc === "utf-8") return "utf8";
|
|
|
|
return slowCases(enc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/nodejs/node/blob/ba684805b6c0eded76e5cd89ee00328ac7a59365/lib/internal/util.js#L130
|
2020-11-26 07:50:08 -05:00
|
|
|
function slowCases(enc: string): TextEncodings | undefined {
|
2020-06-28 12:27:02 -04:00
|
|
|
switch (enc.length) {
|
|
|
|
case 4:
|
|
|
|
if (enc === "UTF8") return "utf8";
|
|
|
|
if (enc === "ucs2" || enc === "UCS2") return "utf16le";
|
|
|
|
enc = `${enc}`.toLowerCase();
|
|
|
|
if (enc === "utf8") return "utf8";
|
|
|
|
if (enc === "ucs2") return "utf16le";
|
|
|
|
break;
|
|
|
|
case 3:
|
2020-07-14 15:24:17 -04:00
|
|
|
if (enc === "hex" || enc === "HEX" || `${enc}`.toLowerCase() === "hex") {
|
2020-06-28 12:27:02 -04:00
|
|
|
return "hex";
|
2020-07-14 15:24:17 -04:00
|
|
|
}
|
2020-06-28 12:27:02 -04:00
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
if (enc === "ascii") return "ascii";
|
|
|
|
if (enc === "ucs-2") return "utf16le";
|
|
|
|
if (enc === "UTF-8") return "utf8";
|
|
|
|
if (enc === "ASCII") return "ascii";
|
|
|
|
if (enc === "UCS-2") return "utf16le";
|
|
|
|
enc = `${enc}`.toLowerCase();
|
|
|
|
if (enc === "utf-8") return "utf8";
|
|
|
|
if (enc === "ascii") return "ascii";
|
|
|
|
if (enc === "ucs-2") return "utf16le";
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (enc === "base64") return "base64";
|
|
|
|
if (enc === "latin1" || enc === "binary") return "latin1";
|
|
|
|
if (enc === "BASE64") return "base64";
|
|
|
|
if (enc === "LATIN1" || enc === "BINARY") return "latin1";
|
|
|
|
enc = `${enc}`.toLowerCase();
|
|
|
|
if (enc === "base64") return "base64";
|
|
|
|
if (enc === "latin1" || enc === "binary") return "latin1";
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
if (
|
|
|
|
enc === "utf16le" ||
|
|
|
|
enc === "UTF16LE" ||
|
|
|
|
`${enc}`.toLowerCase() === "utf16le"
|
2020-07-14 15:24:17 -04:00
|
|
|
) {
|
2020-06-28 12:27:02 -04:00
|
|
|
return "utf16le";
|
2020-07-14 15:24:17 -04:00
|
|
|
}
|
2020-06-28 12:27:02 -04:00
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
if (
|
|
|
|
enc === "utf-16le" ||
|
|
|
|
enc === "UTF-16LE" ||
|
|
|
|
`${enc}`.toLowerCase() === "utf-16le"
|
2020-07-14 15:24:17 -04:00
|
|
|
) {
|
2020-06-28 12:27:02 -04:00
|
|
|
return "utf16le";
|
2020-07-14 15:24:17 -04:00
|
|
|
}
|
2020-06-28 12:27:02 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (enc === "") return "utf8";
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 05:00:38 -04:00
|
|
|
|
|
|
|
export function validateIntegerRange(
|
|
|
|
value: number,
|
|
|
|
name: string,
|
|
|
|
min = -2147483648,
|
|
|
|
max = 2147483647,
|
|
|
|
): void {
|
|
|
|
// The defaults for min and max correspond to the limits of 32-bit integers.
|
|
|
|
if (!Number.isInteger(value)) {
|
|
|
|
throw new Error(`${name} must be 'an integer' but was ${value}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < min || value > max) {
|
|
|
|
throw new Error(
|
|
|
|
`${name} must be >= ${min} && <= ${max}. Value was ${value}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-11-21 16:13:18 -05:00
|
|
|
|
|
|
|
type OptionalSpread<T> = T extends undefined ? []
|
|
|
|
: [T];
|
|
|
|
|
2020-11-26 07:50:08 -05:00
|
|
|
export function once<T = undefined>(
|
|
|
|
callback: (...args: OptionalSpread<T>) => void,
|
|
|
|
) {
|
2020-11-21 16:13:18 -05:00
|
|
|
let called = false;
|
2020-11-26 07:50:08 -05:00
|
|
|
return function (this: unknown, ...args: OptionalSpread<T>) {
|
2020-11-21 16:13:18 -05:00
|
|
|
if (called) return;
|
|
|
|
called = true;
|
|
|
|
callback.apply(this, args);
|
|
|
|
};
|
|
|
|
}
|
2020-11-26 07:50:08 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} [expectedExecutions = 1]
|
|
|
|
* @param {number} [timeout = 1000] Milliseconds to wait before the promise is forcefully exited
|
|
|
|
*/
|
|
|
|
export function mustCall<T extends unknown[]>(
|
|
|
|
fn: ((...args: T) => void) = () => {},
|
|
|
|
expectedExecutions = 1,
|
|
|
|
timeout = 1000,
|
|
|
|
): [Promise<void>, (...args: T) => void] {
|
|
|
|
if (expectedExecutions < 1) {
|
|
|
|
throw new Error("Expected executions can't be lower than 1");
|
|
|
|
}
|
|
|
|
let timesExecuted = 0;
|
|
|
|
const completed = deferred();
|
|
|
|
|
|
|
|
const abort = setTimeout(() => completed.reject(), timeout);
|
|
|
|
|
|
|
|
function callback(this: unknown, ...args: T) {
|
|
|
|
timesExecuted++;
|
|
|
|
if (timesExecuted === expectedExecutions) {
|
|
|
|
completed.resolve();
|
|
|
|
}
|
|
|
|
fn.apply(this, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = completed
|
|
|
|
.then(() => clearTimeout(abort))
|
|
|
|
.catch(() =>
|
|
|
|
fail(
|
|
|
|
`Async operation not completed: Expected ${expectedExecutions}, executed ${timesExecuted}`,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return [
|
|
|
|
result,
|
|
|
|
callback,
|
|
|
|
];
|
|
|
|
}
|