mirror of
https://github.com/denoland/deno.git
synced 2025-01-22 06:09:25 -05:00
fix(node): util.callbackify
(#22200)
Fixes https://github.com/denoland/deno/issues/22180 Matches the Node.js implementation more closely. Removed types, they do not help just make it harder to debug with stack traces.
This commit is contained in:
parent
4b7c6049ef
commit
02c65fad45
4 changed files with 52 additions and 80 deletions
|
@ -292,3 +292,26 @@ Deno.test({
|
||||||
fn();
|
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: testing runtime error
|
||||||
|
() => util.callbackify(undefined),
|
||||||
|
TypeError,
|
||||||
|
'The "original" argument must be of type function',
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
|
@ -386,7 +386,7 @@ deno_core::extension!(deno_node,
|
||||||
"_stream.mjs",
|
"_stream.mjs",
|
||||||
"_tls_common.ts",
|
"_tls_common.ts",
|
||||||
"_tls_wrap.ts",
|
"_tls_wrap.ts",
|
||||||
"_util/_util_callbackify.ts",
|
"_util/_util_callbackify.js",
|
||||||
"_util/asserts.ts",
|
"_util/asserts.ts",
|
||||||
"_util/async.ts",
|
"_util/async.ts",
|
||||||
"_util/os.ts",
|
"_util/os.ts",
|
||||||
|
|
|
@ -26,105 +26,48 @@
|
||||||
import { primordials } from "ext:core/mod.js";
|
import { primordials } from "ext:core/mod.js";
|
||||||
const {
|
const {
|
||||||
ArrayPrototypePop,
|
ArrayPrototypePop,
|
||||||
ReflectApply,
|
|
||||||
Error,
|
Error,
|
||||||
FunctionPrototypeBind,
|
FunctionPrototypeBind,
|
||||||
|
ReflectApply,
|
||||||
ObjectDefineProperties,
|
ObjectDefineProperties,
|
||||||
ObjectGetOwnPropertyDescriptors,
|
ObjectGetOwnPropertyDescriptors,
|
||||||
|
ObjectSetPrototypeOf,
|
||||||
|
ObjectValues,
|
||||||
PromisePrototypeThen,
|
PromisePrototypeThen,
|
||||||
TypeError,
|
|
||||||
} = primordials;
|
} = primordials;
|
||||||
|
|
||||||
import { nextTick } from "ext:deno_node/_next_tick.ts";
|
import { nextTick } from "ext:deno_node/_next_tick.ts";
|
||||||
|
import { validateFunction } from "ext:deno_node/internal/validators.mjs";
|
||||||
|
|
||||||
class NodeFalsyValueRejectionError extends Error {
|
class NodeFalsyValueRejectionError extends Error {
|
||||||
public reason: unknown;
|
code = "ERR_FALSY_VALUE_REJECTION";
|
||||||
public code = "ERR_FALSY_VALUE_REJECTION";
|
constructor(reason) {
|
||||||
constructor(reason: unknown) {
|
|
||||||
super("Promise was rejected with falsy value");
|
super("Promise was rejected with falsy value");
|
||||||
this.reason = reason;
|
this.reason = reason;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class NodeInvalidArgTypeError extends TypeError {
|
|
||||||
public code = "ERR_INVALID_ARG_TYPE";
|
|
||||||
constructor(argumentName: string) {
|
|
||||||
super(`The ${argumentName} argument must be of type function.`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Callback<ResultT> =
|
function callbackify(original) {
|
||||||
| ((err: Error) => void)
|
validateFunction(original, "original");
|
||||||
| ((err: null, result: ResultT) => void);
|
|
||||||
|
|
||||||
function callbackify<ResultT>(
|
// We DO NOT return the promise as it gives the user a false sense that
|
||||||
fn: () => PromiseLike<ResultT>,
|
// the promise is actually somehow related to the callback's execution
|
||||||
): (callback: Callback<ResultT>) => void;
|
// and that the callback throwing will reject the promise.
|
||||||
function callbackify<ArgT, ResultT>(
|
function callbackified(...args) {
|
||||||
fn: (arg: ArgT) => PromiseLike<ResultT>,
|
|
||||||
): (arg: ArgT, callback: Callback<ResultT>) => void;
|
|
||||||
function callbackify<Arg1T, Arg2T, ResultT>(
|
|
||||||
fn: (arg1: Arg1T, arg2: Arg2T) => PromiseLike<ResultT>,
|
|
||||||
): (arg1: Arg1T, arg2: Arg2T, callback: Callback<ResultT>) => void;
|
|
||||||
function callbackify<Arg1T, Arg2T, Arg3T, ResultT>(
|
|
||||||
fn: (arg1: Arg1T, arg2: Arg2T, arg3: Arg3T) => PromiseLike<ResultT>,
|
|
||||||
): (arg1: Arg1T, arg2: Arg2T, arg3: Arg3T, callback: Callback<ResultT>) => void;
|
|
||||||
function callbackify<Arg1T, Arg2T, Arg3T, Arg4T, ResultT>(
|
|
||||||
fn: (
|
|
||||||
arg1: Arg1T,
|
|
||||||
arg2: Arg2T,
|
|
||||||
arg3: Arg3T,
|
|
||||||
arg4: Arg4T,
|
|
||||||
) => PromiseLike<ResultT>,
|
|
||||||
): (
|
|
||||||
arg1: Arg1T,
|
|
||||||
arg2: Arg2T,
|
|
||||||
arg3: Arg3T,
|
|
||||||
arg4: Arg4T,
|
|
||||||
callback: Callback<ResultT>,
|
|
||||||
) => void;
|
|
||||||
function callbackify<Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, ResultT>(
|
|
||||||
fn: (
|
|
||||||
arg1: Arg1T,
|
|
||||||
arg2: Arg2T,
|
|
||||||
arg3: Arg3T,
|
|
||||||
arg4: Arg4T,
|
|
||||||
arg5: Arg5T,
|
|
||||||
) => PromiseLike<ResultT>,
|
|
||||||
): (
|
|
||||||
arg1: Arg1T,
|
|
||||||
arg2: Arg2T,
|
|
||||||
arg3: Arg3T,
|
|
||||||
arg4: Arg4T,
|
|
||||||
arg5: Arg5T,
|
|
||||||
callback: Callback<ResultT>,
|
|
||||||
) => void;
|
|
||||||
|
|
||||||
function callbackify<ResultT>(
|
|
||||||
original: (...args: unknown[]) => PromiseLike<ResultT>,
|
|
||||||
): (...args: unknown[]) => void {
|
|
||||||
if (typeof original !== "function") {
|
|
||||||
throw new NodeInvalidArgTypeError('"original"');
|
|
||||||
}
|
|
||||||
|
|
||||||
const callbackified = function (this: unknown, ...args: unknown[]) {
|
|
||||||
const maybeCb = ArrayPrototypePop(args);
|
const maybeCb = ArrayPrototypePop(args);
|
||||||
if (typeof maybeCb !== "function") {
|
validateFunction(maybeCb, "last argument");
|
||||||
throw new NodeInvalidArgTypeError("last");
|
const cb = FunctionPrototypeBind(maybeCb, this);
|
||||||
}
|
// In true node style we process the callback on `nextTick` with all the
|
||||||
const cb = (...args: unknown[]) => {
|
// implications (stack, `uncaughtException`, `async_hooks`)
|
||||||
ReflectApply(maybeCb, this, args);
|
|
||||||
};
|
|
||||||
PromisePrototypeThen(
|
PromisePrototypeThen(
|
||||||
ReflectApply(this, args),
|
ReflectApply(original, this, args),
|
||||||
(ret: unknown) => {
|
(ret) => nextTick(cb, null, ret),
|
||||||
nextTick(FunctionPrototypeBind(cb, this, null, ret));
|
(rej) => {
|
||||||
},
|
|
||||||
(rej: unknown) => {
|
|
||||||
rej = rej || new NodeFalsyValueRejectionError(rej);
|
rej = rej || new NodeFalsyValueRejectionError(rej);
|
||||||
nextTick(FunctionPrototypeBind(cb, this, rej));
|
return nextTick(cb, rej);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
|
|
||||||
const descriptors = ObjectGetOwnPropertyDescriptors(original);
|
const descriptors = ObjectGetOwnPropertyDescriptors(original);
|
||||||
// It is possible to manipulate a functions `length` or `name` property. This
|
// It is possible to manipulate a functions `length` or `name` property. This
|
||||||
|
@ -135,6 +78,12 @@ function callbackify<ResultT>(
|
||||||
if (typeof descriptors.name.value === "string") {
|
if (typeof descriptors.name.value === "string") {
|
||||||
descriptors.name.value += "Callbackified";
|
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);
|
ObjectDefineProperties(callbackified, descriptors);
|
||||||
return callbackified;
|
return callbackified;
|
||||||
}
|
}
|
|
@ -28,7 +28,7 @@ const {
|
||||||
} = primordials;
|
} = primordials;
|
||||||
|
|
||||||
import { promisify } from "ext:deno_node/internal/util.mjs";
|
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 { debuglog } from "ext:deno_node/internal/util/debuglog.ts";
|
||||||
import {
|
import {
|
||||||
format,
|
format,
|
||||||
|
|
Loading…
Add table
Reference in a new issue