Skip to content

Commit

Permalink
fix(ext/node): use primordials in ext/node/polyfills/_util (#21444)
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Dec 8, 2023
1 parent 3a74fa6 commit b24356d
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 167 deletions.
29 changes: 19 additions & 10 deletions ext/node/polyfills/_util/_util_callbackify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 21,20 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials

// These are simplified versions of the "real" errors in Node.

import { primordials } from "ext:core/mod.js";
import { nextTick } from "ext:deno_node/_next_tick.ts";
const {
ArrayPrototypePop,
Error,
FunctionPrototypeApply,
FunctionPrototypeBind,
ObjectDefineProperties,
ObjectGetOwnPropertyDescriptors,
PromisePrototypeThen,
TypeError,
} = primordials;

class NodeFalsyValueRejectionError extends Error {
public reason: unknown;
Expand Down Expand Up @@ -98,25 106,26 @@ function callbackify<ResultT>(
}

const callbackified = function (this: unknown, ...args: unknown[]) {
const maybeCb = args.pop();
const maybeCb = ArrayPrototypePop(args);
if (typeof maybeCb !== "function") {
throw new NodeInvalidArgTypeError("last");
}
const cb = (...args: unknown[]) => {
maybeCb.apply(this, args);
FunctionPrototypeApply(maybeCb, this, args);
};
original.apply(this, args).then(
PromisePrototypeThen(
FunctionPrototypeApply(this, args),
(ret: unknown) => {
nextTick(cb.bind(this, null, ret));
nextTick(FunctionPrototypeBind(cb, this, null, ret));
},
(rej: unknown) => {
rej = rej || new NodeFalsyValueRejectionError(rej);
nextTick(cb.bind(this, rej));
nextTick(FunctionPrototypeBind(cb, this, rej));
},
);
};

const descriptors = Object.getOwnPropertyDescriptors(original);
const descriptors = ObjectGetOwnPropertyDescriptors(original);
// It is possible to manipulate a functions `length` or `name` property. This
// guards against the manipulation.
if (typeof descriptors.length.value === "number") {
Expand All @@ -125,7 134,7 @@ function callbackify<ResultT>(
if (typeof descriptors.name.value === "string") {
descriptors.name.value = "Callbackified";
}
Object.defineProperties(callbackified, descriptors);
ObjectDefineProperties(callbackified, descriptors);
return callbackified;
}

Expand Down
6 changes: 4 additions & 2 deletions ext/node/polyfills/_util/asserts.ts
Original file line number Diff line number Diff line change
@@ -1,7 1,9 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials
import { primordials } from "ext:core/mod.js";
const {
Error,
} = primordials;

/** Assertion error class for node compat layer's internal code. */
export class NodeCompatAssertionError extends Error {
Expand Down
12 changes: 8 additions & 4 deletions ext/node/polyfills/_util/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 2,12 @@
// This module is vendored from std/async/delay.ts
// (with some modifications)

// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials
import { primordials } from "ext:core/mod.js";
import { clearTimeout, setTimeout } from "ext:deno_web/02_timers.js";
const {
Promise,
PromiseReject,
} = primordials;

/** Resolve a Promise after a given amount of milliseconds. */
export function delay(
Expand All @@ -12,12 16,12 @@ export function delay(
): Promise<void> {
const { signal } = options;
if (signal?.aborted) {
return Promise.reject(new DOMException("Delay was aborted.", "AbortError"));
return PromiseReject(signal.reason);
}
return new Promise((resolve, reject) => {
const abort = () => {
clearTimeout(i);
reject(new DOMException("Delay was aborted.", "AbortError"));
reject(signal!.reason);
};
const done = () => {
signal?.removeEventListener("abort", abort);
Expand Down
3 changes: 2 additions & 1 deletion ext/node/polyfills/_util/os.ts
Original file line number Diff line number Diff line change
@@ -1,6 1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

const { ops } = globalThis.__bootstrap.core;
import { core } from "ext:core/mod.js";
const ops = core.ops;

export type OSType = "windows" | "linux" | "darwin" | "freebsd" | "openbsd";

Expand Down
Loading

0 comments on commit b24356d

Please sign in to comment.