summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/_util
diff options
context:
space:
mode:
Diffstat (limited to 'ext/node/polyfills/_util')
-rw-r--r--ext/node/polyfills/_util/_util_callbackify.ts129
-rw-r--r--ext/node/polyfills/_util/asserts.ts21
-rw-r--r--ext/node/polyfills/_util/async.ts55
-rw-r--r--ext/node/polyfills/_util/os.ts22
-rw-r--r--ext/node/polyfills/_util/std_asserts.ts293
-rw-r--r--ext/node/polyfills/_util/std_fmt_colors.ts519
-rw-r--r--ext/node/polyfills/_util/std_testing_diff.ts440
7 files changed, 1479 insertions, 0 deletions
diff --git a/ext/node/polyfills/_util/_util_callbackify.ts b/ext/node/polyfills/_util/_util_callbackify.ts
new file mode 100644
index 000000000..fe83a227d
--- /dev/null
+++ b/ext/node/polyfills/_util/_util_callbackify.ts
@@ -0,0 +1,129 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+//
+// Adapted from Node.js. Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+// These are simplified versions of the "real" errors in Node.
+
+import { nextTick } from "internal:deno_node/polyfills/_next_tick.ts";
+
+class NodeFalsyValueRejectionError extends Error {
+ public reason: unknown;
+ public code = "ERR_FALSY_VALUE_REJECTION";
+ constructor(reason: unknown) {
+ super("Promise was rejected with falsy value");
+ this.reason = reason;
+ }
+}
+class NodeInvalidArgTypeError extends TypeError {
+ public code = "ERR_INVALID_ARG_TYPE";
+ constructor(argumentName: string) {
+ super(`The ${argumentName} argument must be of type function.`);
+ }
+}
+
+type Callback<ResultT> =
+ | ((err: Error) => void)
+ | ((err: null, result: ResultT) => void);
+
+function callbackify<ResultT>(
+ fn: () => PromiseLike<ResultT>,
+): (callback: Callback<ResultT>) => void;
+function callbackify<ArgT, ResultT>(
+ fn: (arg: ArgT) => PromiseLike<ResultT>,
+): (arg: ArgT, callback: Callback<ResultT>) => void;
+function callbackify<Arg1T, Arg2T, ResultT>(
+ fn: (arg1: Arg1T, arg2: Arg2T) => PromiseLike<ResultT>,
+): (arg1: Arg1T, arg2: Arg2T, callback: Callback<ResultT>) => void;
+function callbackify<Arg1T, Arg2T, Arg3T, ResultT>(
+ fn: (arg1: Arg1T, arg2: Arg2T, arg3: Arg3T) => PromiseLike<ResultT>,
+): (arg1: Arg1T, arg2: Arg2T, arg3: Arg3T, callback: Callback<ResultT>) => void;
+function callbackify<Arg1T, Arg2T, Arg3T, Arg4T, ResultT>(
+ fn: (
+ arg1: Arg1T,
+ arg2: Arg2T,
+ arg3: Arg3T,
+ arg4: Arg4T,
+ ) => PromiseLike<ResultT>,
+): (
+ arg1: Arg1T,
+ arg2: Arg2T,
+ arg3: Arg3T,
+ arg4: Arg4T,
+ callback: Callback<ResultT>,
+) => void;
+function callbackify<Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, ResultT>(
+ fn: (
+ arg1: Arg1T,
+ arg2: Arg2T,
+ arg3: Arg3T,
+ arg4: Arg4T,
+ arg5: Arg5T,
+ ) => PromiseLike<ResultT>,
+): (
+ arg1: Arg1T,
+ arg2: Arg2T,
+ arg3: Arg3T,
+ arg4: Arg4T,
+ arg5: Arg5T,
+ callback: Callback<ResultT>,
+) => void;
+
+function callbackify<ResultT>(
+ original: (...args: unknown[]) => PromiseLike<ResultT>,
+): (...args: unknown[]) => void {
+ if (typeof original !== "function") {
+ throw new NodeInvalidArgTypeError('"original"');
+ }
+
+ const callbackified = function (this: unknown, ...args: unknown[]) {
+ const maybeCb = args.pop();
+ if (typeof maybeCb !== "function") {
+ throw new NodeInvalidArgTypeError("last");
+ }
+ const cb = (...args: unknown[]) => {
+ maybeCb.apply(this, args);
+ };
+ original.apply(this, args).then(
+ (ret: unknown) => {
+ nextTick(cb.bind(this, null, ret));
+ },
+ (rej: unknown) => {
+ rej = rej || new NodeFalsyValueRejectionError(rej);
+ nextTick(cb.bind(this, rej));
+ },
+ );
+ };
+
+ const descriptors = Object.getOwnPropertyDescriptors(original);
+ // It is possible to manipulate a functions `length` or `name` property. This
+ // guards against the manipulation.
+ if (typeof descriptors.length.value === "number") {
+ descriptors.length.value++;
+ }
+ if (typeof descriptors.name.value === "string") {
+ descriptors.name.value += "Callbackified";
+ }
+ Object.defineProperties(callbackified, descriptors);
+ return callbackified;
+}
+
+export { callbackify };
diff --git a/ext/node/polyfills/_util/asserts.ts b/ext/node/polyfills/_util/asserts.ts
new file mode 100644
index 000000000..7760c8639
--- /dev/null
+++ b/ext/node/polyfills/_util/asserts.ts
@@ -0,0 +1,21 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+/** Assertion error class for node compat layer's internal code. */
+export class NodeCompatAssertionError extends Error {
+ constructor(message: string) {
+ super(message);
+ this.name = "NodeCompatAssertionError";
+ }
+}
+
+/** Make an assertion, if not `true`, then throw. */
+export function assert(expr: unknown, msg = ""): asserts expr {
+ if (!expr) {
+ throw new NodeCompatAssertionError(msg);
+ }
+}
+
+/** Use this to assert unreachable code. */
+export function unreachable(): never {
+ throw new NodeCompatAssertionError("unreachable");
+}
diff --git a/ext/node/polyfills/_util/async.ts b/ext/node/polyfills/_util/async.ts
new file mode 100644
index 000000000..b508dbfed
--- /dev/null
+++ b/ext/node/polyfills/_util/async.ts
@@ -0,0 +1,55 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+// This module is vendored from std/async/deferred.ts and std/async/delay.ts
+// (with some modifications)
+
+export interface Deferred<T> extends Promise<T> {
+ readonly state: "pending" | "fulfilled" | "rejected";
+ resolve(value?: T | PromiseLike<T>): void;
+ // deno-lint-ignore no-explicit-any
+ reject(reason?: any): void;
+}
+
+/** Creates a Promise with the `reject` and `resolve` functions */
+export function deferred<T>(): Deferred<T> {
+ let methods;
+ let state = "pending";
+ const promise = new Promise<T>((resolve, reject) => {
+ methods = {
+ async resolve(value: T | PromiseLike<T>) {
+ await value;
+ state = "fulfilled";
+ resolve(value);
+ },
+ // deno-lint-ignore no-explicit-any
+ reject(reason?: any) {
+ state = "rejected";
+ reject(reason);
+ },
+ };
+ });
+ Object.defineProperty(promise, "state", { get: () => state });
+ return Object.assign(promise, methods) as Deferred<T>;
+}
+
+/** Resolve a Promise after a given amount of milliseconds. */
+export function delay(
+ ms: number,
+ options: { signal?: AbortSignal } = {},
+): Promise<void> {
+ const { signal } = options;
+ if (signal?.aborted) {
+ return Promise.reject(new DOMException("Delay was aborted.", "AbortError"));
+ }
+ return new Promise((resolve, reject) => {
+ const abort = () => {
+ clearTimeout(i);
+ reject(new DOMException("Delay was aborted.", "AbortError"));
+ };
+ const done = () => {
+ signal?.removeEventListener("abort", abort);
+ resolve();
+ };
+ const i = setTimeout(done, ms);
+ signal?.addEventListener("abort", abort, { once: true });
+ });
+}
diff --git a/ext/node/polyfills/_util/os.ts b/ext/node/polyfills/_util/os.ts
new file mode 100644
index 000000000..66d18534c
--- /dev/null
+++ b/ext/node/polyfills/_util/os.ts
@@ -0,0 +1,22 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+export type OSType = "windows" | "linux" | "darwin" | "freebsd";
+
+export const osType: OSType = (() => {
+ // deno-lint-ignore no-explicit-any
+ const { Deno } = globalThis as any;
+ if (typeof Deno?.build?.os === "string") {
+ return Deno.build.os;
+ }
+
+ // deno-lint-ignore no-explicit-any
+ const { navigator } = globalThis as any;
+ if (navigator?.appVersion?.includes?.("Win")) {
+ return "windows";
+ }
+
+ return "linux";
+})();
+
+export const isWindows = osType === "windows";
+export const isLinux = osType === "linux";
diff --git a/ext/node/polyfills/_util/std_asserts.ts b/ext/node/polyfills/_util/std_asserts.ts
new file mode 100644
index 000000000..8c4c80078
--- /dev/null
+++ b/ext/node/polyfills/_util/std_asserts.ts
@@ -0,0 +1,293 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+// vendored from std/testing/asserts.ts
+
+import { red } from "internal:deno_node/polyfills/_util/std_fmt_colors.ts";
+import {
+ buildMessage,
+ diff,
+ diffstr,
+} from "internal:deno_node/polyfills/_util/std_testing_diff.ts";
+
+/** Converts the input into a string. Objects, Sets and Maps are sorted so as to
+ * make tests less flaky */
+export function format(v: unknown): string {
+ // deno-lint-ignore no-explicit-any
+ const { Deno } = globalThis as any;
+ return typeof Deno?.inspect === "function"
+ ? Deno.inspect(v, {
+ depth: Infinity,
+ sorted: true,
+ trailingComma: true,
+ compact: false,
+ iterableLimit: Infinity,
+ // getters should be true in assertEquals.
+ getters: true,
+ })
+ : `"${String(v).replace(/(?=["\\])/g, "\\")}"`;
+}
+
+const CAN_NOT_DISPLAY = "[Cannot display]";
+
+export class AssertionError extends Error {
+ override name = "AssertionError";
+ constructor(message: string) {
+ super(message);
+ }
+}
+
+function isKeyedCollection(x: unknown): x is Set<unknown> {
+ return [Symbol.iterator, "size"].every((k) => k in (x as Set<unknown>));
+}
+
+/** Deep equality comparison used in assertions */
+export function equal(c: unknown, d: unknown): boolean {
+ const seen = new Map();
+ return (function compare(a: unknown, b: unknown): boolean {
+ // Have to render RegExp & Date for string comparison
+ // unless it's mistreated as object
+ if (
+ a &&
+ b &&
+ ((a instanceof RegExp && b instanceof RegExp) ||
+ (a instanceof URL && b instanceof URL))
+ ) {
+ return String(a) === String(b);
+ }
+ if (a instanceof Date && b instanceof Date) {
+ const aTime = a.getTime();
+ const bTime = b.getTime();
+ // Check for NaN equality manually since NaN is not
+ // equal to itself.
+ if (Number.isNaN(aTime) && Number.isNaN(bTime)) {
+ return true;
+ }
+ return aTime === bTime;
+ }
+ if (typeof a === "number" && typeof b === "number") {
+ return Number.isNaN(a) && Number.isNaN(b) || a === b;
+ }
+ if (Object.is(a, b)) {
+ return true;
+ }
+ if (a && typeof a === "object" && b && typeof b === "object") {
+ if (a && b && !constructorsEqual(a, b)) {
+ return false;
+ }
+ if (a instanceof WeakMap || b instanceof WeakMap) {
+ if (!(a instanceof WeakMap && b instanceof WeakMap)) return false;
+ throw new TypeError("cannot compare WeakMap instances");
+ }
+ if (a instanceof WeakSet || b instanceof WeakSet) {
+ if (!(a instanceof WeakSet && b instanceof WeakSet)) return false;
+ throw new TypeError("cannot compare WeakSet instances");
+ }
+ if (seen.get(a) === b) {
+ return true;
+ }
+ if (Object.keys(a || {}).length !== Object.keys(b || {}).length) {
+ return false;
+ }
+ seen.set(a, b);
+ if (isKeyedCollection(a) && isKeyedCollection(b)) {
+ if (a.size !== b.size) {
+ return false;
+ }
+
+ let unmatchedEntries = a.size;
+
+ for (const [aKey, aValue] of a.entries()) {
+ for (const [bKey, bValue] of b.entries()) {
+ /* Given that Map keys can be references, we need
+ * to ensure that they are also deeply equal */
+ if (
+ (aKey === aValue && bKey === bValue && compare(aKey, bKey)) ||
+ (compare(aKey, bKey) && compare(aValue, bValue))
+ ) {
+ unmatchedEntries--;
+ break;
+ }
+ }
+ }
+
+ return unmatchedEntries === 0;
+ }
+ const merged = { ...a, ...b };
+ for (
+ const key of [
+ ...Object.getOwnPropertyNames(merged),
+ ...Object.getOwnPropertySymbols(merged),
+ ]
+ ) {
+ type Key = keyof typeof merged;
+ if (!compare(a && a[key as Key], b && b[key as Key])) {
+ return false;
+ }
+ if (((key in a) && (!(key in b))) || ((key in b) && (!(key in a)))) {
+ return false;
+ }
+ }
+ if (a instanceof WeakRef || b instanceof WeakRef) {
+ if (!(a instanceof WeakRef && b instanceof WeakRef)) return false;
+ return compare(a.deref(), b.deref());
+ }
+ return true;
+ }
+ return false;
+ })(c, d);
+}
+
+// deno-lint-ignore ban-types
+function constructorsEqual(a: object, b: object) {
+ return a.constructor === b.constructor ||
+ a.constructor === Object && !b.constructor ||
+ !a.constructor && b.constructor === Object;
+}
+
+/** Make an assertion, error will be thrown if `expr` does not have truthy value. */
+export function assert(expr: unknown, msg = ""): asserts expr {
+ if (!expr) {
+ throw new AssertionError(msg);
+ }
+}
+
+/** Make an assertion that `actual` and `expected` are equal, deeply. If not
+ * deeply equal, then throw. */
+export function assertEquals<T>(actual: T, expected: T, msg?: string) {
+ if (equal(actual, expected)) {
+ return;
+ }
+ let message = "";
+ const actualString = format(actual);
+ const expectedString = format(expected);
+ try {
+ const stringDiff = (typeof actual === "string") &&
+ (typeof expected === "string");
+ const diffResult = stringDiff
+ ? diffstr(actual as string, expected as string)
+ : diff(actualString.split("\n"), expectedString.split("\n"));
+ const diffMsg = buildMessage(diffResult, { stringDiff }).join("\n");
+ message = `Values are not equal:\n${diffMsg}`;
+ } catch {
+ message = `\n${red(red(CAN_NOT_DISPLAY))} + \n\n`;
+ }
+ if (msg) {
+ message = msg;
+ }
+ throw new AssertionError(message);
+}
+
+/** Make an assertion that `actual` and `expected` are not equal, deeply.
+ * If not then throw. */
+export function assertNotEquals<T>(actual: T, expected: T, msg?: string) {
+ if (!equal(actual, expected)) {
+ return;
+ }
+ let actualString: string;
+ let expectedString: string;
+ try {
+ actualString = String(actual);
+ } catch {
+ actualString = "[Cannot display]";
+ }
+ try {
+ expectedString = String(expected);
+ } catch {
+ expectedString = "[Cannot display]";
+ }
+ if (!msg) {
+ msg = `actual: ${actualString} expected not to be: ${expectedString}`;
+ }
+ throw new AssertionError(msg);
+}
+
+/** Make an assertion that `actual` and `expected` are strictly equal. If
+ * not then throw. */
+export function assertStrictEquals<T>(
+ actual: unknown,
+ expected: T,
+ msg?: string,
+): asserts actual is T {
+ if (Object.is(actual, expected)) {
+ return;
+ }
+
+ let message: string;
+
+ if (msg) {
+ message = msg;
+ } else {
+ const actualString = format(actual);
+ const expectedString = format(expected);
+
+ if (actualString === expectedString) {
+ const withOffset = actualString
+ .split("\n")
+ .map((l) => ` ${l}`)
+ .join("\n");
+ message =
+ `Values have the same structure but are not reference-equal:\n\n${
+ red(withOffset)
+ }\n`;
+ } else {
+ try {
+ const stringDiff = (typeof actual === "string") &&
+ (typeof expected === "string");
+ const diffResult = stringDiff
+ ? diffstr(actual as string, expected as string)
+ : diff(actualString.split("\n"), expectedString.split("\n"));
+ const diffMsg = buildMessage(diffResult, { stringDiff }).join("\n");
+ message = `Values are not strictly equal:\n${diffMsg}`;
+ } catch {
+ message = `\n${CAN_NOT_DISPLAY} + \n\n`;
+ }
+ }
+ }
+
+ throw new AssertionError(message);
+}
+
+/** Make an assertion that `actual` and `expected` are not strictly equal.
+ * If the values are strictly equal then throw. */
+export function assertNotStrictEquals<T>(
+ actual: T,
+ expected: T,
+ msg?: string,
+) {
+ if (!Object.is(actual, expected)) {
+ return;
+ }
+
+ throw new AssertionError(
+ msg ?? `Expected "actual" to be strictly unequal to: ${format(actual)}\n`,
+ );
+}
+
+/** Make an assertion that `actual` match RegExp `expected`. If not
+ * then throw. */
+export function assertMatch(
+ actual: string,
+ expected: RegExp,
+ msg?: string,
+) {
+ if (!expected.test(actual)) {
+ if (!msg) {
+ msg = `actual: "${actual}" expected to match: "${expected}"`;
+ }
+ throw new AssertionError(msg);
+ }
+}
+
+/** Make an assertion that `actual` not match RegExp `expected`. If match
+ * then throw. */
+export function assertNotMatch(
+ actual: string,
+ expected: RegExp,
+ msg?: string,
+) {
+ if (expected.test(actual)) {
+ if (!msg) {
+ msg = `actual: "${actual}" expected to not match: "${expected}"`;
+ }
+ throw new AssertionError(msg);
+ }
+}
diff --git a/ext/node/polyfills/_util/std_fmt_colors.ts b/ext/node/polyfills/_util/std_fmt_colors.ts
new file mode 100644
index 000000000..d54e2c83d
--- /dev/null
+++ b/ext/node/polyfills/_util/std_fmt_colors.ts
@@ -0,0 +1,519 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+// This file is vendored from std/fmt/colors.ts
+
+// TODO(kt3k): Initialize this at the start of runtime
+// based on Deno.noColor
+const noColor = false;
+
+interface Code {
+ open: string;
+ close: string;
+ regexp: RegExp;
+}
+
+/** RGB 8-bits per channel. Each in range `0->255` or `0x00->0xff` */
+interface Rgb {
+ r: number;
+ g: number;
+ b: number;
+}
+
+let enabled = !noColor;
+
+/**
+ * Set changing text color to enabled or disabled
+ * @param value
+ */
+export function setColorEnabled(value: boolean) {
+ if (noColor) {
+ return;
+ }
+
+ enabled = value;
+}
+
+/** Get whether text color change is enabled or disabled. */
+export function getColorEnabled(): boolean {
+ return enabled;
+}
+
+/**
+ * Builds color code
+ * @param open
+ * @param close
+ */
+function code(open: number[], close: number): Code {
+ return {
+ open: `\x1b[${open.join(";")}m`,
+ close: `\x1b[${close}m`,
+ regexp: new RegExp(`\\x1b\\[${close}m`, "g"),
+ };
+}
+
+/**
+ * Applies color and background based on color code and its associated text
+ * @param str text to apply color settings to
+ * @param code color code to apply
+ */
+function run(str: string, code: Code): string {
+ return enabled
+ ? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
+ : str;
+}
+
+/**
+ * Reset the text modified
+ * @param str text to reset
+ */
+export function reset(str: string): string {
+ return run(str, code([0], 0));
+}
+
+/**
+ * Make the text bold.
+ * @param str text to make bold
+ */
+export function bold(str: string): string {
+ return run(str, code([1], 22));
+}
+
+/**
+ * The text emits only a small amount of light.
+ * @param str text to dim
+ */
+export function dim(str: string): string {
+ return run(str, code([2], 22));
+}
+
+/**
+ * Make the text italic.
+ * @param str text to make italic
+ */
+export function italic(str: string): string {
+ return run(str, code([3], 23));
+}
+
+/**
+ * Make the text underline.
+ * @param str text to underline
+ */
+export function underline(str: string): string {
+ return run(str, code([4], 24));
+}
+
+/**
+ * Invert background color and text color.
+ * @param str text to invert its color
+ */
+export function inverse(str: string): string {
+ return run(str, code([7], 27));
+}
+
+/**
+ * Make the text hidden.
+ * @param str text to hide
+ */
+export function hidden(str: string): string {
+ return run(str, code([8], 28));
+}
+
+/**
+ * Put horizontal line through the center of the text.
+ * @param str text to strike through
+ */
+export function strikethrough(str: string): string {
+ return run(str, code([9], 29));
+}
+
+/**
+ * Set text color to black.
+ * @param str text to make black
+ */
+export function black(str: string): string {
+ return run(str, code([30], 39));
+}
+
+/**
+ * Set text color to red.
+ * @param str text to make red
+ */
+export function red(str: string): string {
+ return run(str, code([31], 39));
+}
+
+/**
+ * Set text color to green.
+ * @param str text to make green
+ */
+export function green(str: string): string {
+ return run(str, code([32], 39));
+}
+
+/**
+ * Set text color to yellow.
+ * @param str text to make yellow
+ */
+export function yellow(str: string): string {
+ return run(str, code([33], 39));
+}
+
+/**
+ * Set text color to blue.
+ * @param str text to make blue
+ */
+export function blue(str: string): string {
+ return run(str, code([34], 39));
+}
+
+/**
+ * Set text color to magenta.
+ * @param str text to make magenta
+ */
+export function magenta(str: string): string {
+ return run(str, code([35], 39));
+}
+
+/**
+ * Set text color to cyan.
+ * @param str text to make cyan
+ */
+export function cyan(str: string): string {
+ return run(str, code([36], 39));
+}
+
+/**
+ * Set text color to white.
+ * @param str text to make white
+ */
+export function white(str: string): string {
+ return run(str, code([37], 39));
+}
+
+/**
+ * Set text color to gray.
+ * @param str text to make gray
+ */
+export function gray(str: string): string {
+ return brightBlack(str);
+}
+
+/**
+ * Set text color to bright black.
+ * @param str text to make bright-black
+ */
+export function brightBlack(str: string): string {
+ return run(str, code([90], 39));
+}
+
+/**
+ * Set text color to bright red.
+ * @param str text to make bright-red
+ */
+export function brightRed(str: string): string {
+ return run(str, code([91], 39));
+}
+
+/**
+ * Set text color to bright green.
+ * @param str text to make bright-green
+ */
+export function brightGreen(str: string): string {
+ return run(str, code([92], 39));
+}
+
+/**
+ * Set text color to bright yellow.
+ * @param str text to make bright-yellow
+ */
+export function brightYellow(str: string): string {
+ return run(str, code([93], 39));
+}
+
+/**
+ * Set text color to bright blue.
+ * @param str text to make bright-blue
+ */
+export function brightBlue(str: string): string {
+ return run(str, code([94], 39));
+}
+
+/**
+ * Set text color to bright magenta.
+ * @param str text to make bright-magenta
+ */
+export function brightMagenta(str: string): string {
+ return run(str, code([95], 39));
+}
+
+/**
+ * Set text color to bright cyan.
+ * @param str text to make bright-cyan
+ */
+export function brightCyan(str: string): string {
+ return run(str, code([96], 39));
+}
+
+/**
+ * Set text color to bright white.
+ * @param str text to make bright-white
+ */
+export function brightWhite(str: string): string {
+ return run(str, code([97], 39));
+}
+
+/**
+ * Set background color to black.
+ * @param str text to make its background black
+ */
+export function bgBlack(str: string): string {
+ return run(str, code([40], 49));
+}
+
+/**
+ * Set background color to red.
+ * @param str text to make its background red
+ */
+export function bgRed(str: string): string {
+ return run(str, code([41], 49));
+}
+
+/**
+ * Set background color to green.
+ * @param str text to make its background green
+ */
+export function bgGreen(str: string): string {
+ return run(str, code([42], 49));
+}
+
+/**
+ * Set background color to yellow.
+ * @param str text to make its background yellow
+ */
+export function bgYellow(str: string): string {
+ return run(str, code([43], 49));
+}
+
+/**
+ * Set background color to blue.
+ * @param str text to make its background blue
+ */
+export function bgBlue(str: string): string {
+ return run(str, code([44], 49));
+}
+
+/**
+ * Set background color to magenta.
+ * @param str text to make its background magenta
+ */
+export function bgMagenta(str: string): string {
+ return run(str, code([45], 49));
+}
+
+/**
+ * Set background color to cyan.
+ * @param str text to make its background cyan
+ */
+export function bgCyan(str: string): string {
+ return run(str, code([46], 49));
+}
+
+/**
+ * Set background color to white.
+ * @param str text to make its background white
+ */
+export function bgWhite(str: string): string {
+ return run(str, code([47], 49));
+}
+
+/**
+ * Set background color to bright black.
+ * @param str text to make its background bright-black
+ */
+export function bgBrightBlack(str: string): string {
+ return run(str, code([100], 49));
+}
+
+/**
+ * Set background color to bright red.
+ * @param str text to make its background bright-red
+ */
+export function bgBrightRed(str: string): string {
+ return run(str, code([101], 49));
+}
+
+/**
+ * Set background color to bright green.
+ * @param str text to make its background bright-green
+ */
+export function bgBrightGreen(str: string): string {
+ return run(str, code([102], 49));
+}
+
+/**
+ * Set background color to bright yellow.
+ * @param str text to make its background bright-yellow
+ */
+export function bgBrightYellow(str: string): string {
+ return run(str, code([103], 49));
+}
+
+/**
+ * Set background color to bright blue.
+ * @param str text to make its background bright-blue
+ */
+export function bgBrightBlue(str: string): string {
+ return run(str, code([104], 49));
+}
+
+/**
+ * Set background color to bright magenta.
+ * @param str text to make its background bright-magenta
+ */
+export function bgBrightMagenta(str: string): string {
+ return run(str, code([105], 49));
+}
+
+/**
+ * Set background color to bright cyan.
+ * @param str text to make its background bright-cyan
+ */
+export function bgBrightCyan(str: string): string {
+ return run(str, code([106], 49));
+}
+
+/**
+ * Set background color to bright white.
+ * @param str text to make its background bright-white
+ */
+export function bgBrightWhite(str: string): string {
+ return run(str, code([107], 49));
+}
+
+/* Special Color Sequences */
+
+/**
+ * Clam and truncate color codes
+ * @param n
+ * @param max number to truncate to
+ * @param min number to truncate from
+ */
+function clampAndTruncate(n: number, max = 255, min = 0): number {
+ return Math.trunc(Math.max(Math.min(n, max), min));
+}
+
+/**
+ * Set text color using paletted 8bit colors.
+ * https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
+ * @param str text color to apply paletted 8bit colors to
+ * @param color code
+ */
+export function rgb8(str: string, color: number): string {
+ return run(str, code([38, 5, clampAndTruncate(color)], 39));
+}
+
+/**
+ * Set background color using paletted 8bit colors.
+ * https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
+ * @param str text color to apply paletted 8bit background colors to
+ * @param color code
+ */
+export function bgRgb8(str: string, color: number): string {
+ return run(str, code([48, 5, clampAndTruncate(color)], 49));
+}
+
+/**
+ * Set text color using 24bit rgb.
+ * `color` can be a number in range `0x000000` to `0xffffff` or
+ * an `Rgb`.
+ *
+ * To produce the color magenta:
+ *
+ * ```ts
+ * import { rgb24 } from "https://deno.land/std@$STD_VERSION/fmt/colors.ts";
+ * rgb24("foo", 0xff00ff);
+ * rgb24("foo", {r: 255, g: 0, b: 255});
+ * ```
+ * @param str text color to apply 24bit rgb to
+ * @param color code
+ */
+export function rgb24(str: string, color: number | Rgb): string {
+ if (typeof color === "number") {
+ return run(
+ str,
+ code(
+ [38, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff],
+ 39,
+ ),
+ );
+ }
+ return run(
+ str,
+ code(
+ [
+ 38,
+ 2,
+ clampAndTruncate(color.r),
+ clampAndTruncate(color.g),
+ clampAndTruncate(color.b),
+ ],
+ 39,
+ ),
+ );
+}
+
+/**
+ * Set background color using 24bit rgb.
+ * `color` can be a number in range `0x000000` to `0xffffff` or
+ * an `Rgb`.
+ *
+ * To produce the color magenta:
+ *
+ * ```ts
+ * import { bgRgb24 } from "https://deno.land/std@$STD_VERSION/fmt/colors.ts";
+ * bgRgb24("foo", 0xff00ff);
+ * bgRgb24("foo", {r: 255, g: 0, b: 255});
+ * ```
+ * @param str text color to apply 24bit rgb to
+ * @param color code
+ */
+export function bgRgb24(str: string, color: number | Rgb): string {
+ if (typeof color === "number") {
+ return run(
+ str,
+ code(
+ [48, 2, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff],
+ 49,
+ ),
+ );
+ }
+ return run(
+ str,
+ code(
+ [
+ 48,
+ 2,
+ clampAndTruncate(color.r),
+ clampAndTruncate(color.g),
+ clampAndTruncate(color.b),
+ ],
+ 49,
+ ),
+ );
+}
+
+// https://github.com/chalk/ansi-regex/blob/02fa893d619d3da85411acc8fd4e2eea0e95a9d9/index.js
+const ANSI_PATTERN = new RegExp(
+ [
+ "[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
+ "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))",
+ ].join("|"),
+ "g",
+);
+
+/**
+ * Remove ANSI escape codes from the string.
+ * @param string to remove ANSI escape codes from
+ */
+export function stripColor(string: string): string {
+ return string.replace(ANSI_PATTERN, "");
+}
diff --git a/ext/node/polyfills/_util/std_testing_diff.ts b/ext/node/polyfills/_util/std_testing_diff.ts
new file mode 100644
index 000000000..766b5efdc
--- /dev/null
+++ b/ext/node/polyfills/_util/std_testing_diff.ts
@@ -0,0 +1,440 @@
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+// This file was vendored from std/testing/_diff.ts
+
+import {
+ bgGreen,
+ bgRed,
+ bold,
+ gray,
+ green,
+ red,
+ white,
+} from "internal:deno_node/polyfills/_util/std_fmt_colors.ts";
+
+interface FarthestPoint {
+ y: number;
+ id: number;
+}
+
+export enum DiffType {
+ removed = "removed",
+ common = "common",
+ added = "added",
+}
+
+export interface DiffResult<T> {
+ type: DiffType;
+ value: T;
+ details?: Array<DiffResult<T>>;
+}
+
+const REMOVED = 1;
+const COMMON = 2;
+const ADDED = 3;
+
+function createCommon<T>(A: T[], B: T[], reverse?: boolean): T[] {
+ const common = [];
+ if (A.length === 0 || B.length === 0) return [];
+ for (let i = 0; i < Math.min(A.length, B.length); i += 1) {
+ if (
+ A[reverse ? A.length - i - 1 : i] === B[reverse ? B.length - i - 1 : i]
+ ) {
+ common.push(A[reverse ? A.length - i - 1 : i]);
+ } else {
+ return common;
+ }
+ }
+ return common;
+}
+
+/**
+ * Renders the differences between the actual and expected values
+ * @param A Actual value
+ * @param B Expected value
+ */
+export function diff<T>(A: T[], B: T[]): Array<DiffResult<T>> {
+ const prefixCommon = createCommon(A, B);
+ const suffixCommon = createCommon(
+ A.slice(prefixCommon.length),
+ B.slice(prefixCommon.length),
+ true,
+ ).reverse();
+ A = suffixCommon.length
+ ? A.slice(prefixCommon.length, -suffixCommon.length)
+ : A.slice(prefixCommon.length);
+ B = suffixCommon.length
+ ? B.slice(prefixCommon.length, -suffixCommon.length)
+ : B.slice(prefixCommon.length);
+ const swapped = B.length > A.length;
+ [A, B] = swapped ? [B, A] : [A, B];
+ const M = A.length;
+ const N = B.length;
+ if (!M && !N && !suffixCommon.length && !prefixCommon.length) return [];
+ if (!N) {
+ return [
+ ...prefixCommon.map(
+ (c): DiffResult<typeof c> => ({ type: DiffType.common, value: c }),
+ ),
+ ...A.map(
+ (a): DiffResult<typeof a> => ({
+ type: swapped ? DiffType.added : DiffType.removed,
+ value: a,
+ }),
+ ),
+ ...suffixCommon.map(
+ (c): DiffResult<typeof c> => ({ type: DiffType.common, value: c }),
+ ),
+ ];
+ }
+ const offset = N;
+ const delta = M - N;
+ const size = M + N + 1;
+ const fp: FarthestPoint[] = Array.from(
+ { length: size },
+ () => ({ y: -1, id: -1 }),
+ );
+ /**
+ * INFO:
+ * This buffer is used to save memory and improve performance.
+ * The first half is used to save route and last half is used to save diff
+ * type.
+ * This is because, when I kept new uint8array area to save type,performance
+ * worsened.
+ */
+ const routes = new Uint32Array((M * N + size + 1) * 2);
+ const diffTypesPtrOffset = routes.length / 2;
+ let ptr = 0;
+ let p = -1;
+
+ function backTrace<T>(
+ A: T[],
+ B: T[],
+ current: FarthestPoint,
+ swapped: boolean,
+ ): Array<{
+ type: DiffType;
+ value: T;
+ }> {
+ const M = A.length;
+ const N = B.length;
+ const result = [];
+ let a = M - 1;
+ let b = N - 1;
+ let j = routes[current.id];
+ let type = routes[current.id + diffTypesPtrOffset];
+ while (true) {
+ if (!j && !type) break;
+ const prev = j;
+ if (type === REMOVED) {
+ result.unshift({
+ type: swapped ? DiffType.removed : DiffType.added,
+ value: B[b],
+ });
+ b -= 1;
+ } else if (type === ADDED) {
+ result.unshift({
+ type: swapped ? DiffType.added : DiffType.removed,
+ value: A[a],
+ });
+ a -= 1;
+ } else {
+ result.unshift({ type: DiffType.common, value: A[a] });
+ a -= 1;
+ b -= 1;
+ }
+ j = routes[prev];
+ type = routes[prev + diffTypesPtrOffset];
+ }
+ return result;
+ }
+
+ function createFP(
+ slide: FarthestPoint,
+ down: FarthestPoint,
+ k: number,
+ M: number,
+ ): FarthestPoint {
+ if (slide && slide.y === -1 && down && down.y === -1) {
+ return { y: 0, id: 0 };
+ }
+ if (
+ (down && down.y === -1) ||
+ k === M ||
+ (slide && slide.y) > (down && down.y) + 1
+ ) {
+ const prev = slide.id;
+ ptr++;
+ routes[ptr] = prev;
+ routes[ptr + diffTypesPtrOffset] = ADDED;
+ return { y: slide.y, id: ptr };
+ } else {
+ const prev = down.id;
+ ptr++;
+ routes[ptr] = prev;
+ routes[ptr + diffTypesPtrOffset] = REMOVED;
+ return { y: down.y + 1, id: ptr };
+ }
+ }
+
+ function snake<T>(
+ k: number,
+ slide: FarthestPoint,
+ down: FarthestPoint,
+ _offset: number,
+ A: T[],
+ B: T[],
+ ): FarthestPoint {
+ const M = A.length;
+ const N = B.length;
+ if (k < -N || M < k) return { y: -1, id: -1 };
+ const fp = createFP(slide, down, k, M);
+ while (fp.y + k < M && fp.y < N && A[fp.y + k] === B[fp.y]) {
+ const prev = fp.id;
+ ptr++;
+ fp.id = ptr;
+ fp.y += 1;
+ routes[ptr] = prev;
+ routes[ptr + diffTypesPtrOffset] = COMMON;
+ }
+ return fp;
+ }
+
+ while (fp[delta + offset].y < N) {
+ p = p + 1;
+ for (let k = -p; k < delta; ++k) {
+ fp[k + offset] = snake(
+ k,
+ fp[k - 1 + offset],
+ fp[k + 1 + offset],
+ offset,
+ A,
+ B,
+ );
+ }
+ for (let k = delta + p; k > delta; --k) {
+ fp[k + offset] = snake(
+ k,
+ fp[k - 1 + offset],
+ fp[k + 1 + offset],
+ offset,
+ A,
+ B,
+ );
+ }
+ fp[delta + offset] = snake(
+ delta,
+ fp[delta - 1 + offset],
+ fp[delta + 1 + offset],
+ offset,
+ A,
+ B,
+ );
+ }
+ return [
+ ...prefixCommon.map(
+ (c): DiffResult<typeof c> => ({ type: DiffType.common, value: c }),
+ ),
+ ...backTrace(A, B, fp[delta + offset], swapped),
+ ...suffixCommon.map(
+ (c): DiffResult<typeof c> => ({ type: DiffType.common, value: c }),
+ ),
+ ];
+}
+
+/**
+ * Renders the differences between the actual and expected strings
+ * Partially inspired from https://github.com/kpdecker/jsdiff
+ * @param A Actual string
+ * @param B Expected string
+ */
+export function diffstr(A: string, B: string) {
+ function unescape(string: string): string {
+ // unescape invisible characters.
+ // ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#escape_sequences
+ return string
+ .replaceAll("\b", "\\b")
+ .replaceAll("\f", "\\f")
+ .replaceAll("\t", "\\t")
+ .replaceAll("\v", "\\v")
+ .replaceAll( // does not remove line breaks
+ /\r\n|\r|\n/g,
+ (str) => str === "\r" ? "\\r" : str === "\n" ? "\\n\n" : "\\r\\n\r\n",
+ );
+ }
+
+ function tokenize(string: string, { wordDiff = false } = {}): string[] {
+ if (wordDiff) {
+ // Split string on whitespace symbols
+ const tokens = string.split(/([^\S\r\n]+|[()[\]{}'"\r\n]|\b)/);
+ // Extended Latin character set
+ const words =
+ /^[a-zA-Z\u{C0}-\u{FF}\u{D8}-\u{F6}\u{F8}-\u{2C6}\u{2C8}-\u{2D7}\u{2DE}-\u{2FF}\u{1E00}-\u{1EFF}]+$/u;
+
+ // Join boundary splits that we do not consider to be boundaries and merge empty strings surrounded by word chars
+ for (let i = 0; i < tokens.length - 1; i++) {
+ if (
+ !tokens[i + 1] && tokens[i + 2] && words.test(tokens[i]) &&
+ words.test(tokens[i + 2])
+ ) {
+ tokens[i] += tokens[i + 2];
+ tokens.splice(i + 1, 2);
+ i--;
+ }
+ }
+ return tokens.filter((token) => token);
+ } else {
+ // Split string on new lines symbols
+ const tokens = [], lines = string.split(/(\n|\r\n)/);
+
+ // Ignore final empty token when text ends with a newline
+ if (!lines[lines.length - 1]) {
+ lines.pop();
+ }
+
+ // Merge the content and line separators into single tokens
+ for (let i = 0; i < lines.length; i++) {
+ if (i % 2) {
+ tokens[tokens.length - 1] += lines[i];
+ } else {
+ tokens.push(lines[i]);
+ }
+ }
+ return tokens;
+ }
+ }
+
+ // Create details by filtering relevant word-diff for current line
+ // and merge "space-diff" if surrounded by word-diff for cleaner displays
+ function createDetails(
+ line: DiffResult<string>,
+ tokens: Array<DiffResult<string>>,
+ ) {
+ return tokens.filter(({ type }) =>
+ type === line.type || type === DiffType.common
+ ).map((result, i, t) => {
+ if (
+ (result.type === DiffType.common) && (t[i - 1]) &&
+ (t[i - 1]?.type === t[i + 1]?.type) && /\s+/.test(result.value)
+ ) {
+ return {
+ ...result,
+ type: t[i - 1].type,
+ };
+ }
+ return result;
+ });
+ }
+
+ // Compute multi-line diff
+ const diffResult = diff(
+ tokenize(`${unescape(A)}\n`),
+ tokenize(`${unescape(B)}\n`),
+ );
+
+ const added = [], removed = [];
+ for (const result of diffResult) {
+ if (result.type === DiffType.added) {
+ added.push(result);
+ }
+ if (result.type === DiffType.removed) {
+ removed.push(result);
+ }
+ }
+
+ // Compute word-diff
+ const aLines = added.length < removed.length ? added : removed;
+ const bLines = aLines === removed ? added : removed;
+ for (const a of aLines) {
+ let tokens = [] as Array<DiffResult<string>>,
+ b: undefined | DiffResult<string>;
+ // Search another diff line with at least one common token
+ while (bLines.length) {
+ b = bLines.shift();
+ tokens = diff(
+ tokenize(a.value, { wordDiff: true }),
+ tokenize(b?.value ?? "", { wordDiff: true }),
+ );
+ if (
+ tokens.some(({ type, value }) =>
+ type === DiffType.common && value.trim().length
+ )
+ ) {
+ break;
+ }
+ }
+ // Register word-diff details
+ a.details = createDetails(a, tokens);
+ if (b) {
+ b.details = createDetails(b, tokens);
+ }
+ }
+
+ return diffResult;
+}
+
+/**
+ * Colors the output of assertion diffs
+ * @param diffType Difference type, either added or removed
+ */
+function createColor(
+ diffType: DiffType,
+ { background = false } = {},
+): (s: string) => string {
+ // TODO(@littledivy): Remove this when we can detect
+ // true color terminals.
+ // https://github.com/denoland/deno_std/issues/2575
+ background = false;
+ switch (diffType) {
+ case DiffType.added:
+ return (s: string): string =>
+ background ? bgGreen(white(s)) : green(bold(s));
+ case DiffType.removed:
+ return (s: string): string => background ? bgRed(white(s)) : red(bold(s));
+ default:
+ return white;
+ }
+}
+
+/**
+ * Prefixes `+` or `-` in diff output
+ * @param diffType Difference type, either added or removed
+ */
+function createSign(diffType: DiffType): string {
+ switch (diffType) {
+ case DiffType.added:
+ return "+ ";
+ case DiffType.removed:
+ return "- ";
+ default:
+ return " ";
+ }
+}
+
+export function buildMessage(
+ diffResult: ReadonlyArray<DiffResult<string>>,
+ { stringDiff = false } = {},
+): string[] {
+ const messages: string[] = [], diffMessages: string[] = [];
+ messages.push("");
+ messages.push("");
+ messages.push(
+ ` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${
+ green(bold("Expected"))
+ }`,
+ );
+ messages.push("");
+ messages.push("");
+ diffResult.forEach((result: DiffResult<string>) => {
+ const c = createColor(result.type);
+ const line = result.details?.map((detail) =>
+ detail.type !== DiffType.common
+ ? createColor(detail.type, { background: true })(detail.value)
+ : detail.value
+ ).join("") ?? result.value;
+ diffMessages.push(c(`${createSign(result.type)}${line}`));
+ });
+ messages.push(...(stringDiff ? [diffMessages.join("")] : diffMessages));
+ messages.push("");
+
+ return messages;
+}