Skip to content

Commit f4eead6

Browse files
authored
fix(ext/node): use primordials in ext/node/polyfills/_utils.ts (denoland#24253)
1 parent 8dd7beb commit f4eead6

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

ext/node/polyfills/_utils.ts

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
22

3-
// TODO(petamoriken): enable prefer-primordials for node polyfills
4-
// deno-lint-ignore-file prefer-primordials
5-
3+
import { primordials } from "ext:core/mod.js";
4+
const {
5+
Error,
6+
PromisePrototypeThen,
7+
ArrayPrototypePop,
8+
StringPrototypeToLowerCase,
9+
NumberIsInteger,
10+
ObjectGetOwnPropertyNames,
11+
ReflectGetOwnPropertyDescriptor,
12+
ObjectDefineProperty,
13+
NumberIsSafeInteger,
14+
FunctionPrototypeApply,
15+
SafeArrayIterator,
16+
} = primordials;
617
import { TextDecoder, TextEncoder } from "ext:deno_web/08_text_encoding.js";
718
import { errorMap } from "ext:deno_node/internal_binding/uv.ts";
819
import { codes } from "ext:deno_node/internal/error_codes.ts";
@@ -53,9 +64,10 @@ export function intoCallbackAPI<T>(
5364
// deno-lint-ignore no-explicit-any
5465
...args: any[]
5566
) {
56-
func(...args).then(
57-
(value) => cb && cb(null, value),
58-
(err) => cb && cb(err),
67+
PromisePrototypeThen(
68+
func(...new SafeArrayIterator(args)),
69+
(value: T) => cb && cb(null, value),
70+
(err: MaybeNull<Error>) => cb && cb(err),
5971
);
6072
}
6173

@@ -67,15 +79,18 @@ export function intoCallbackAPIWithIntercept<T1, T2>(
6779
// deno-lint-ignore no-explicit-any
6880
...args: any[]
6981
) {
70-
func(...args).then(
71-
(value) => cb && cb(null, interceptor(value)),
72-
(err) => cb && cb(err),
82+
PromisePrototypeThen(
83+
func(...new SafeArrayIterator(args)),
84+
(value: T1) => cb && cb(null, interceptor(value)),
85+
(err: MaybeNull<Error>) => cb && cb(err),
7386
);
7487
}
7588

7689
export function spliceOne(list: string[], index: number) {
77-
for (; index + 1 < list.length; index++) list[index] = list[index + 1];
78-
list.pop();
90+
for (; index + 1 < list.length; index++) {
91+
list[index] = list[index + 1];
92+
}
93+
ArrayPrototypePop(list);
7994
}
8095

8196
// Taken from: https://github.com/nodejs/node/blob/ba684805b6c0eded76e5cd89ee00328ac7a59365/lib/internal/util.js#L125
@@ -95,12 +110,15 @@ function slowCases(enc: string): TextEncodings | undefined {
95110
case 4:
96111
if (enc === "UTF8") return "utf8";
97112
if (enc === "ucs2" || enc === "UCS2") return "utf16le";
98-
enc = `${enc}`.toLowerCase();
113+
enc = StringPrototypeToLowerCase(enc);
99114
if (enc === "utf8") return "utf8";
100115
if (enc === "ucs2") return "utf16le";
101116
break;
102117
case 3:
103-
if (enc === "hex" || enc === "HEX" || `${enc}`.toLowerCase() === "hex") {
118+
if (
119+
enc === "hex" || enc === "HEX" ||
120+
StringPrototypeToLowerCase(enc) === "hex"
121+
) {
104122
return "hex";
105123
}
106124
break;
@@ -110,7 +128,7 @@ function slowCases(enc: string): TextEncodings | undefined {
110128
if (enc === "UTF-8") return "utf8";
111129
if (enc === "ASCII") return "ascii";
112130
if (enc === "UCS-2") return "utf16le";
113-
enc = `${enc}`.toLowerCase();
131+
enc = StringPrototypeToLowerCase(enc);
114132
if (enc === "utf-8") return "utf8";
115133
if (enc === "ascii") return "ascii";
116134
if (enc === "ucs-2") return "utf16le";
@@ -120,15 +138,15 @@ function slowCases(enc: string): TextEncodings | undefined {
120138
if (enc === "latin1" || enc === "binary") return "latin1";
121139
if (enc === "BASE64") return "base64";
122140
if (enc === "LATIN1" || enc === "BINARY") return "latin1";
123-
enc = `${enc}`.toLowerCase();
141+
enc = StringPrototypeToLowerCase(enc);
124142
if (enc === "base64") return "base64";
125143
if (enc === "latin1" || enc === "binary") return "latin1";
126144
break;
127145
case 7:
128146
if (
129147
enc === "utf16le" ||
130148
enc === "UTF16LE" ||
131-
`${enc}`.toLowerCase() === "utf16le"
149+
StringPrototypeToLowerCase(enc) === "utf16le"
132150
) {
133151
return "utf16le";
134152
}
@@ -137,7 +155,7 @@ function slowCases(enc: string): TextEncodings | undefined {
137155
if (
138156
enc === "utf-16le" ||
139157
enc === "UTF-16LE" ||
140-
`${enc}`.toLowerCase() === "utf-16le"
158+
StringPrototypeToLowerCase(enc) === "utf-16le"
141159
) {
142160
return "utf16le";
143161
}
@@ -154,7 +172,7 @@ export function validateIntegerRange(
154172
max = 2147483647,
155173
) {
156174
// The defaults for min and max correspond to the limits of 32-bit integers.
157-
if (!Number.isInteger(value)) {
175+
if (!NumberIsInteger(value)) {
158176
throw new Error(`${name} must be 'an integer' but was ${value}`);
159177
}
160178

@@ -175,26 +193,26 @@ export function once<T = undefined>(
175193
return function (this: unknown, ...args: OptionalSpread<T>) {
176194
if (called) return;
177195
called = true;
178-
callback.apply(this, args);
196+
FunctionPrototypeApply(callback, this, args);
179197
};
180198
}
181199

182200
export function makeMethodsEnumerable(klass: { new (): unknown }) {
183201
const proto = klass.prototype;
184-
for (const key of Object.getOwnPropertyNames(proto)) {
202+
const names = ObjectGetOwnPropertyNames(proto);
203+
for (let i = 0; i < names.length; i++) {
204+
const key = names[i];
185205
const value = proto[key];
186206
if (typeof value === "function") {
187-
const desc = Reflect.getOwnPropertyDescriptor(proto, key);
207+
const desc = ReflectGetOwnPropertyDescriptor(proto, key);
188208
if (desc) {
189209
desc.enumerable = true;
190-
Object.defineProperty(proto, key, desc);
210+
ObjectDefineProperty(proto, key, desc);
191211
}
192212
}
193213
}
194214
}
195215

196-
const NumberIsSafeInteger = Number.isSafeInteger;
197-
198216
/**
199217
* Returns a system error name from an error code number.
200218
* @param code error code number

0 commit comments

Comments
 (0)