1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

fix(ext/node): use primordials in ext/node/polyfills/_utils.ts (#24253)

This commit is contained in:
Asher Gomez 2024-06-19 18:29:26 +10:00 committed by Bartek Iwańczuk
parent 5c5b4d7355
commit bec107cf0e
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750

View file

@ -1,8 +1,19 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// TODO(petamoriken): enable prefer-primordials for node polyfills import { primordials } from "ext:core/mod.js";
// deno-lint-ignore-file prefer-primordials const {
Error,
PromisePrototypeThen,
ArrayPrototypePop,
StringPrototypeToLowerCase,
NumberIsInteger,
ObjectGetOwnPropertyNames,
ReflectGetOwnPropertyDescriptor,
ObjectDefineProperty,
NumberIsSafeInteger,
FunctionPrototypeApply,
SafeArrayIterator,
} = primordials;
import { TextDecoder, TextEncoder } from "ext:deno_web/08_text_encoding.js"; import { TextDecoder, TextEncoder } from "ext:deno_web/08_text_encoding.js";
import { errorMap } from "ext:deno_node/internal_binding/uv.ts"; import { errorMap } from "ext:deno_node/internal_binding/uv.ts";
import { codes } from "ext:deno_node/internal/error_codes.ts"; import { codes } from "ext:deno_node/internal/error_codes.ts";
@ -53,9 +64,10 @@ export function intoCallbackAPI<T>(
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
...args: any[] ...args: any[]
) { ) {
func(...args).then( PromisePrototypeThen(
(value) => cb && cb(null, value), func(...new SafeArrayIterator(args)),
(err) => cb && cb(err), (value: T) => cb && cb(null, value),
(err: MaybeNull<Error>) => cb && cb(err),
); );
} }
@ -67,15 +79,18 @@ export function intoCallbackAPIWithIntercept<T1, T2>(
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
...args: any[] ...args: any[]
) { ) {
func(...args).then( PromisePrototypeThen(
(value) => cb && cb(null, interceptor(value)), func(...new SafeArrayIterator(args)),
(err) => cb && cb(err), (value: T1) => cb && cb(null, interceptor(value)),
(err: MaybeNull<Error>) => cb && cb(err),
); );
} }
export function spliceOne(list: string[], index: number) { export function spliceOne(list: string[], index: number) {
for (; index + 1 < list.length; index++) list[index] = list[index + 1]; for (; index + 1 < list.length; index++) {
list.pop(); list[index] = list[index + 1];
}
ArrayPrototypePop(list);
} }
// Taken from: https://github.com/nodejs/node/blob/ba684805b6c0eded76e5cd89ee00328ac7a59365/lib/internal/util.js#L125 // Taken from: https://github.com/nodejs/node/blob/ba684805b6c0eded76e5cd89ee00328ac7a59365/lib/internal/util.js#L125
@ -95,12 +110,15 @@ function slowCases(enc: string): TextEncodings | undefined {
case 4: case 4:
if (enc === "UTF8") return "utf8"; if (enc === "UTF8") return "utf8";
if (enc === "ucs2" || enc === "UCS2") return "utf16le"; if (enc === "ucs2" || enc === "UCS2") return "utf16le";
enc = `${enc}`.toLowerCase(); enc = StringPrototypeToLowerCase(enc);
if (enc === "utf8") return "utf8"; if (enc === "utf8") return "utf8";
if (enc === "ucs2") return "utf16le"; if (enc === "ucs2") return "utf16le";
break; break;
case 3: case 3:
if (enc === "hex" || enc === "HEX" || `${enc}`.toLowerCase() === "hex") { if (
enc === "hex" || enc === "HEX" ||
StringPrototypeToLowerCase(enc) === "hex"
) {
return "hex"; return "hex";
} }
break; break;
@ -110,7 +128,7 @@ function slowCases(enc: string): TextEncodings | undefined {
if (enc === "UTF-8") return "utf8"; if (enc === "UTF-8") return "utf8";
if (enc === "ASCII") return "ascii"; if (enc === "ASCII") return "ascii";
if (enc === "UCS-2") return "utf16le"; if (enc === "UCS-2") return "utf16le";
enc = `${enc}`.toLowerCase(); enc = StringPrototypeToLowerCase(enc);
if (enc === "utf-8") return "utf8"; if (enc === "utf-8") return "utf8";
if (enc === "ascii") return "ascii"; if (enc === "ascii") return "ascii";
if (enc === "ucs-2") return "utf16le"; if (enc === "ucs-2") return "utf16le";
@ -120,7 +138,7 @@ function slowCases(enc: string): TextEncodings | undefined {
if (enc === "latin1" || enc === "binary") return "latin1"; if (enc === "latin1" || enc === "binary") return "latin1";
if (enc === "BASE64") return "base64"; if (enc === "BASE64") return "base64";
if (enc === "LATIN1" || enc === "BINARY") return "latin1"; if (enc === "LATIN1" || enc === "BINARY") return "latin1";
enc = `${enc}`.toLowerCase(); enc = StringPrototypeToLowerCase(enc);
if (enc === "base64") return "base64"; if (enc === "base64") return "base64";
if (enc === "latin1" || enc === "binary") return "latin1"; if (enc === "latin1" || enc === "binary") return "latin1";
break; break;
@ -128,7 +146,7 @@ function slowCases(enc: string): TextEncodings | undefined {
if ( if (
enc === "utf16le" || enc === "utf16le" ||
enc === "UTF16LE" || enc === "UTF16LE" ||
`${enc}`.toLowerCase() === "utf16le" StringPrototypeToLowerCase(enc) === "utf16le"
) { ) {
return "utf16le"; return "utf16le";
} }
@ -137,7 +155,7 @@ function slowCases(enc: string): TextEncodings | undefined {
if ( if (
enc === "utf-16le" || enc === "utf-16le" ||
enc === "UTF-16LE" || enc === "UTF-16LE" ||
`${enc}`.toLowerCase() === "utf-16le" StringPrototypeToLowerCase(enc) === "utf-16le"
) { ) {
return "utf16le"; return "utf16le";
} }
@ -154,7 +172,7 @@ export function validateIntegerRange(
max = 2147483647, max = 2147483647,
) { ) {
// The defaults for min and max correspond to the limits of 32-bit integers. // The defaults for min and max correspond to the limits of 32-bit integers.
if (!Number.isInteger(value)) { if (!NumberIsInteger(value)) {
throw new Error(`${name} must be 'an integer' but was ${value}`); throw new Error(`${name} must be 'an integer' but was ${value}`);
} }
@ -175,26 +193,26 @@ export function once<T = undefined>(
return function (this: unknown, ...args: OptionalSpread<T>) { return function (this: unknown, ...args: OptionalSpread<T>) {
if (called) return; if (called) return;
called = true; called = true;
callback.apply(this, args); FunctionPrototypeApply(callback, this, args);
}; };
} }
export function makeMethodsEnumerable(klass: { new (): unknown }) { export function makeMethodsEnumerable(klass: { new (): unknown }) {
const proto = klass.prototype; const proto = klass.prototype;
for (const key of Object.getOwnPropertyNames(proto)) { const names = ObjectGetOwnPropertyNames(proto);
for (let i = 0; i < names.length; i++) {
const key = names[i];
const value = proto[key]; const value = proto[key];
if (typeof value === "function") { if (typeof value === "function") {
const desc = Reflect.getOwnPropertyDescriptor(proto, key); const desc = ReflectGetOwnPropertyDescriptor(proto, key);
if (desc) { if (desc) {
desc.enumerable = true; desc.enumerable = true;
Object.defineProperty(proto, key, desc); ObjectDefineProperty(proto, key, desc);
} }
} }
} }
} }
const NumberIsSafeInteger = Number.isSafeInteger;
/** /**
* Returns a system error name from an error code number. * Returns a system error name from an error code number.
* @param code error code number * @param code error code number