2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-06-27 02:18:22 -04:00
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { primordials } from "ext:core/mod.js";
|
2023-11-01 03:06:25 -04:00
|
|
|
const {
|
|
|
|
ArrayIsArray,
|
|
|
|
ArrayPrototypeJoin,
|
|
|
|
Date,
|
|
|
|
DatePrototypeGetDate,
|
|
|
|
DatePrototypeGetHours,
|
|
|
|
DatePrototypeGetMinutes,
|
|
|
|
DatePrototypeGetMonth,
|
|
|
|
DatePrototypeGetSeconds,
|
|
|
|
ErrorPrototype,
|
|
|
|
NumberPrototypeToString,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
ObjectKeys,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
ObjectPrototypeToString,
|
|
|
|
ObjectSetPrototypeOf,
|
|
|
|
ReflectApply,
|
|
|
|
ReflectConstruct,
|
|
|
|
SafeSet,
|
|
|
|
SetPrototypeAdd,
|
|
|
|
SetPrototypeHas,
|
2023-11-06 16:18:28 -05:00
|
|
|
StringPrototypeIsWellFormed,
|
2023-11-01 03:06:25 -04:00
|
|
|
StringPrototypePadStart,
|
2023-11-06 16:18:28 -05:00
|
|
|
StringPrototypeToWellFormed,
|
2024-09-05 03:22:52 -04:00
|
|
|
PromiseResolve,
|
2023-11-01 03:06:25 -04:00
|
|
|
} = primordials;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2024-09-05 03:22:52 -04:00
|
|
|
import {
|
|
|
|
createDeferredPromise,
|
|
|
|
promisify,
|
|
|
|
} from "ext:deno_node/internal/util.mjs";
|
2024-01-31 22:21:10 -05:00
|
|
|
import { callbackify } from "ext:deno_node/_util/_util_callbackify.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
import { debuglog } from "ext:deno_node/internal/util/debuglog.ts";
|
|
|
|
import {
|
|
|
|
format,
|
|
|
|
formatWithOptions,
|
|
|
|
inspect,
|
|
|
|
stripVTControlCharacters,
|
2024-10-12 15:36:23 -04:00
|
|
|
styleText,
|
2024-01-10 17:37:25 -05:00
|
|
|
} from "ext:deno_node/internal/util/inspect.mjs";
|
|
|
|
import { codes } from "ext:deno_node/internal/error_codes.ts";
|
|
|
|
import types from "node:util/types";
|
|
|
|
import { Buffer } from "node:buffer";
|
|
|
|
import { isDeepStrictEqual } from "ext:deno_node/internal/util/comparisons.ts";
|
|
|
|
import process from "node:process";
|
2024-09-05 03:22:52 -04:00
|
|
|
import {
|
|
|
|
validateAbortSignal,
|
|
|
|
validateString,
|
|
|
|
} from "ext:deno_node/internal/validators.mjs";
|
2024-01-10 17:37:25 -05:00
|
|
|
import { parseArgs } from "ext:deno_node/internal/util/parse_args/parse_args.js";
|
2024-09-05 03:22:52 -04:00
|
|
|
import * as abortSignal from "ext:deno_web/03_abort_signal.js";
|
|
|
|
import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts";
|
2024-01-10 17:37:25 -05:00
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
export {
|
|
|
|
callbackify,
|
|
|
|
debuglog,
|
2024-08-08 07:52:14 -04:00
|
|
|
debuglog as debug,
|
2023-02-14 11:38:45 -05:00
|
|
|
format,
|
|
|
|
formatWithOptions,
|
|
|
|
inspect,
|
2023-11-29 01:42:58 -05:00
|
|
|
parseArgs,
|
2023-02-14 11:38:45 -05:00
|
|
|
promisify,
|
|
|
|
stripVTControlCharacters,
|
2024-10-12 15:36:23 -04:00
|
|
|
styleText,
|
2023-02-14 11:38:45 -05:00
|
|
|
types,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @deprecated - use `Array.isArray()` instead. */
|
2023-11-01 03:06:25 -04:00
|
|
|
export const isArray = ArrayIsArray;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-11-01 03:06:25 -04:00
|
|
|
/** @deprecated - use `typeof value === "boolean" instead. */
|
2023-02-14 11:38:45 -05:00
|
|
|
export function isBoolean(value: unknown): boolean {
|
2023-11-01 03:06:25 -04:00
|
|
|
return typeof value === "boolean";
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated - use `value === null` instead. */
|
|
|
|
export function isNull(value: unknown): boolean {
|
|
|
|
return value === null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated - use `value === null || value === undefined` instead. */
|
|
|
|
export function isNullOrUndefined(value: unknown): boolean {
|
|
|
|
return value === null || value === undefined;
|
|
|
|
}
|
|
|
|
|
2023-11-01 03:06:25 -04:00
|
|
|
/** @deprecated - use `typeof value === "number" instead. */
|
2023-02-14 11:38:45 -05:00
|
|
|
export function isNumber(value: unknown): boolean {
|
2023-11-01 03:06:25 -04:00
|
|
|
return typeof value === "number";
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2023-11-01 03:06:25 -04:00
|
|
|
/** @deprecated - use `typeof value === "string" instead. */
|
2023-02-14 11:38:45 -05:00
|
|
|
export function isString(value: unknown): boolean {
|
2023-11-01 03:06:25 -04:00
|
|
|
return typeof value === "string";
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated - use `typeof value === "symbol"` instead. */
|
|
|
|
export function isSymbol(value: unknown): boolean {
|
|
|
|
return typeof value === "symbol";
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated - use `value === undefined` instead. */
|
|
|
|
export function isUndefined(value: unknown): boolean {
|
|
|
|
return value === undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated - use `value !== null && typeof value === "object"` instead. */
|
|
|
|
export function isObject(value: unknown): boolean {
|
|
|
|
return value !== null && typeof value === "object";
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated - use `e instanceof Error` instead. */
|
|
|
|
export function isError(e: unknown): boolean {
|
2023-11-01 03:06:25 -04:00
|
|
|
return ObjectPrototypeToString(e) === "[object Error]" ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(ErrorPrototype, e);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated - use `typeof value === "function"` instead. */
|
|
|
|
export function isFunction(value: unknown): boolean {
|
|
|
|
return typeof value === "function";
|
|
|
|
}
|
|
|
|
|
2023-12-19 01:05:49 -05:00
|
|
|
/** @deprecated Use util.types.isRegExp() instead. */
|
|
|
|
export const isRegExp = types.isRegExp;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
/** @deprecated Use util.types.isDate() instead. */
|
2023-12-19 01:05:49 -05:00
|
|
|
export const isDate = types.isDate;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
/** @deprecated - use `value === null || (typeof value !== "object" && typeof value !== "function")` instead. */
|
|
|
|
export function isPrimitive(value: unknown): boolean {
|
|
|
|
return (
|
|
|
|
value === null || (typeof value !== "object" && typeof value !== "function")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @deprecated Use Buffer.isBuffer() instead. */
|
2023-11-01 03:06:25 -04:00
|
|
|
export const isBuffer = Buffer.isBuffer;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
/** @deprecated Use Object.assign() instead. */
|
|
|
|
export function _extend(
|
|
|
|
target: Record<string, unknown>,
|
|
|
|
source: unknown,
|
|
|
|
): Record<string, unknown> {
|
|
|
|
// Don't do anything if source isn't an object
|
|
|
|
if (source === null || typeof source !== "object") return target;
|
|
|
|
|
2023-11-01 03:06:25 -04:00
|
|
|
const keys = ObjectKeys(source!);
|
2023-02-14 11:38:45 -05:00
|
|
|
let i = keys.length;
|
|
|
|
while (i--) {
|
|
|
|
target[keys[i]] = (source as Record<string, unknown>)[keys[i]];
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* https://nodejs.org/api/util.html#util_util_inherits_constructor_superconstructor
|
|
|
|
* @param ctor Constructor function which needs to inherit the prototype.
|
|
|
|
* @param superCtor Constructor function to inherit prototype from.
|
|
|
|
*/
|
|
|
|
export function inherits<T, U>(
|
|
|
|
ctor: new (...args: unknown[]) => T,
|
|
|
|
superCtor: new (...args: unknown[]) => U,
|
|
|
|
) {
|
|
|
|
if (ctor === undefined || ctor === null) {
|
|
|
|
throw new codes.ERR_INVALID_ARG_TYPE("ctor", "Function", ctor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (superCtor === undefined || superCtor === null) {
|
|
|
|
throw new codes.ERR_INVALID_ARG_TYPE("superCtor", "Function", superCtor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (superCtor.prototype === undefined) {
|
|
|
|
throw new codes.ERR_INVALID_ARG_TYPE(
|
|
|
|
"superCtor.prototype",
|
|
|
|
"Object",
|
|
|
|
superCtor.prototype,
|
|
|
|
);
|
|
|
|
}
|
2023-11-01 03:06:25 -04:00
|
|
|
ObjectDefineProperty(ctor, "super_", {
|
2024-09-06 06:52:59 -04:00
|
|
|
__proto__: null,
|
2023-02-14 11:38:45 -05:00
|
|
|
value: superCtor,
|
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
});
|
2023-11-01 03:06:25 -04:00
|
|
|
ObjectSetPrototypeOf(ctor.prototype, superCtor.prototype);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
import {
|
|
|
|
_TextDecoder,
|
|
|
|
_TextEncoder,
|
|
|
|
getSystemErrorName,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/_utils.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
/** The global TextDecoder */
|
|
|
|
export type TextDecoder = import("./_utils.ts")._TextDecoder;
|
|
|
|
export const TextDecoder = _TextDecoder;
|
|
|
|
|
|
|
|
/** The global TextEncoder */
|
|
|
|
export type TextEncoder = import("./_utils.ts")._TextEncoder;
|
|
|
|
export const TextEncoder = _TextEncoder;
|
|
|
|
|
2023-11-06 16:18:28 -05:00
|
|
|
export function toUSVString(str: string): string {
|
|
|
|
if (StringPrototypeIsWellFormed(str)) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
return StringPrototypeToWellFormed(str);
|
|
|
|
}
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
function pad(n: number) {
|
2023-11-01 03:06:25 -04:00
|
|
|
return StringPrototypePadStart(NumberPrototypeToString(n), 2, "0");
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const months = [
|
|
|
|
"Jan",
|
|
|
|
"Feb",
|
|
|
|
"Mar",
|
|
|
|
"Apr",
|
|
|
|
"May",
|
|
|
|
"Jun",
|
|
|
|
"Jul",
|
|
|
|
"Aug",
|
|
|
|
"Sep",
|
|
|
|
"Oct",
|
|
|
|
"Nov",
|
|
|
|
"Dec",
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns 26 Feb 16:19:34
|
|
|
|
*/
|
|
|
|
function timestamp(): string {
|
|
|
|
const d = new Date();
|
2023-11-01 03:06:25 -04:00
|
|
|
const t = ArrayPrototypeJoin([
|
|
|
|
pad(DatePrototypeGetHours(d)),
|
|
|
|
pad(DatePrototypeGetMinutes(d)),
|
|
|
|
pad(DatePrototypeGetSeconds(d)),
|
|
|
|
], ":");
|
|
|
|
return `${DatePrototypeGetDate(d)} ${months[DatePrototypeGetMonth(d)]} ${t}`;
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log is just a thin wrapper to console.log that prepends a timestamp
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
export function log(...args: any[]) {
|
2024-08-20 15:14:37 -04:00
|
|
|
// deno-lint-ignore no-console
|
2023-11-01 03:06:25 -04:00
|
|
|
console.log("%s - %s", timestamp(), ReflectApply(format, undefined, args));
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keep a list of deprecation codes that have been warned on so we only warn on
|
|
|
|
// each one once.
|
2023-11-01 03:06:25 -04:00
|
|
|
const codesWarned = new SafeSet();
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
// Mark that a method should not be used.
|
|
|
|
// Returns a modified function which warns once by default.
|
|
|
|
// If --no-deprecation is set, then it is a no-op.
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
export function deprecate(fn: any, msg: string, code?: any) {
|
|
|
|
if (process.noDeprecation === true) {
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code !== undefined) {
|
|
|
|
validateString(code, "code");
|
|
|
|
}
|
|
|
|
|
|
|
|
let warned = false;
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
function deprecated(this: any, ...args: any[]) {
|
|
|
|
if (!warned) {
|
|
|
|
warned = true;
|
|
|
|
if (code !== undefined) {
|
2023-11-01 03:06:25 -04:00
|
|
|
if (!SetPrototypeHas(codesWarned, code)) {
|
2023-02-14 11:38:45 -05:00
|
|
|
process.emitWarning(msg, "DeprecationWarning", code, deprecated);
|
2023-11-01 03:06:25 -04:00
|
|
|
SetPrototypeAdd(codesWarned, code);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
process.emitWarning(msg, "DeprecationWarning", deprecated as any);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (new.target) {
|
2023-11-01 03:06:25 -04:00
|
|
|
return ReflectConstruct(fn, args, new.target);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
2023-11-01 03:06:25 -04:00
|
|
|
return ReflectApply(fn, this, args);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// The wrapper will keep the same prototype as fn to maintain prototype chain
|
2023-11-01 03:06:25 -04:00
|
|
|
ObjectSetPrototypeOf(deprecated, fn);
|
2023-02-14 11:38:45 -05:00
|
|
|
if (fn.prototype) {
|
|
|
|
// Setting this (rather than using Object.setPrototype, as above) ensures
|
|
|
|
// that calling the unwrapped constructor gives an instanceof the wrapped
|
|
|
|
// constructor.
|
|
|
|
deprecated.prototype = fn.prototype;
|
|
|
|
}
|
|
|
|
|
|
|
|
return deprecated;
|
|
|
|
}
|
|
|
|
|
2024-09-05 03:22:52 -04:00
|
|
|
// deno-lint-ignore require-await
|
|
|
|
export async function aborted(
|
|
|
|
signal: AbortSignal,
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
_resource: any,
|
|
|
|
): Promise<void> {
|
|
|
|
if (signal === undefined) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE("signal", "AbortSignal", signal);
|
|
|
|
}
|
|
|
|
validateAbortSignal(signal, "signal");
|
|
|
|
if (signal.aborted) {
|
|
|
|
return PromiseResolve();
|
|
|
|
}
|
|
|
|
const abortPromise = createDeferredPromise();
|
|
|
|
signal[abortSignal.add](abortPromise.resolve);
|
|
|
|
return abortPromise.promise;
|
|
|
|
}
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
export { getSystemErrorName, isDeepStrictEqual };
|
|
|
|
|
|
|
|
export default {
|
|
|
|
format,
|
|
|
|
formatWithOptions,
|
|
|
|
inspect,
|
|
|
|
isArray,
|
|
|
|
isBoolean,
|
|
|
|
isNull,
|
|
|
|
isNullOrUndefined,
|
|
|
|
isNumber,
|
|
|
|
isString,
|
|
|
|
isSymbol,
|
|
|
|
isUndefined,
|
|
|
|
isObject,
|
|
|
|
isError,
|
|
|
|
isFunction,
|
|
|
|
isRegExp,
|
|
|
|
isDate,
|
|
|
|
isPrimitive,
|
|
|
|
isBuffer,
|
|
|
|
_extend,
|
|
|
|
getSystemErrorName,
|
2024-09-05 03:22:52 -04:00
|
|
|
aborted,
|
2023-02-14 11:38:45 -05:00
|
|
|
deprecate,
|
|
|
|
callbackify,
|
2023-11-29 01:42:58 -05:00
|
|
|
parseArgs,
|
2023-02-14 11:38:45 -05:00
|
|
|
promisify,
|
|
|
|
inherits,
|
|
|
|
types,
|
|
|
|
stripVTControlCharacters,
|
|
|
|
TextDecoder,
|
|
|
|
TextEncoder,
|
2023-11-06 16:18:28 -05:00
|
|
|
toUSVString,
|
2023-02-14 11:38:45 -05:00
|
|
|
log,
|
|
|
|
debuglog,
|
2024-08-08 07:52:14 -04:00
|
|
|
debug: debuglog,
|
2023-02-14 11:38:45 -05:00
|
|
|
isDeepStrictEqual,
|
2024-10-12 15:36:23 -04:00
|
|
|
styleText,
|
2023-02-14 11:38:45 -05:00
|
|
|
};
|