Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): util.callbackify #22200

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Rewrite node util.callbackify
  • Loading branch information
littledivy committed Jan 31, 2024
commit 94a95c79b148a126406e30e1e7cf565761619d44
23 changes: 23 additions & 0 deletions cli/tests/unit_node/util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,26 @@ Deno.test({
fn();
},
});

Deno.test({
name: "[util] callbackify() works",
fn() {
const fn = util.callbackify(() => Promise.resolve("foo"));
fn((err, value) => {
assert(err === null);
assert(value === "foo");
});
},
});

Deno.test({
name: "[util] callbackify(undefined) throws",
fn() {
assertThrows(
// @ts-expect-error
() => util.callbackify(undefined),
TypeError,
'The "original" argument must be of type function',
);
},
});
2 changes: 1 addition & 1 deletion ext/node/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ deno_core::extension!(deno_node,
"_stream.mjs",
"_tls_common.ts",
"_tls_wrap.ts",
"_util/_util_callbackify.ts",
"_util/_util_callbackify.js",
"_util/asserts.ts",
"_util/async.ts",
"_util/os.ts",
Expand Down
87 changes: 87 additions & 0 deletions ext/node/polyfills/_util/_util_callbackify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2018-2024 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 { primordials } from "ext:core/mod.js";
const {
ArrayPrototypePop,
Error,
FunctionPrototypeBind,
ReflectApply,
ObjectDefineProperties,
ObjectGetOwnPropertyDescriptors,
ObjectSetPrototypeOf,
ObjectValues,
} = primordials;

import { nextTick } from "ext:deno_node/_next_tick.ts";
import { validateFunction } from "ext:deno_node/internal/validators.mjs";

class NodeFalsyValueRejectionError extends Error {
code = "ERR_FALSY_VALUE_REJECTION";
constructor(reason) {
super("Promise was rejected with falsy value");
this.reason = reason;
}
}

function callbackify(original) {
validateFunction(original, "original");

// We DO NOT return the promise as it gives the user a false sense that
// the promise is actually somehow related to the callback's execution
// and that the callback throwing will reject the promise.
function callbackified(...args) {
const maybeCb = ArrayPrototypePop(args);
validateFunction(maybeCb, "last argument");
const cb = FunctionPrototypeBind(maybeCb, this);
// In true node style we process the callback on `nextTick` with all the
// implications (stack, `uncaughtException`, `async_hooks`)
ReflectApply(original, this, args)
.then((ret) => nextTick(cb, null, ret), (rej) => {
rej = rej || new NodeFalsyValueRejectionError(rej);
return nextTick(cb, rej);
});
}

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") {
descriptors.length.value++;
}
if (typeof descriptors.name.value === "string") {
descriptors.name.value += "Callbackified";
}
const propertiesValues = ObjectValues(descriptors);
for (let i = 0; i < propertiesValues.length; i++) {
// We want to use null-prototype objects to not rely on globally mutable
// %Object.prototype%.
ObjectSetPrototypeOf(propertiesValues[i], null);
}
ObjectDefineProperties(callbackified, descriptors);
return callbackified;
}

export { callbackify };
142 changes: 0 additions & 142 deletions ext/node/polyfills/_util/_util_callbackify.ts

This file was deleted.

2 changes: 1 addition & 1 deletion ext/node/polyfills/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const {
} = primordials;

import { promisify } from "ext:deno_node/internal/util.mjs";
import { callbackify } from "ext:deno_node/_util/_util_callbackify.ts";
import { callbackify } from "ext:deno_node/_util/_util_callbackify.js";
import { debuglog } from "ext:deno_node/internal/util/debuglog.ts";
import {
format,
Expand Down
Loading