2020-06-08 13:26:52 -04:00
|
|
|
// Copyright 2018-2020 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.
|
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
|
|
|
assertStrictEquals,
|
|
|
|
assertThrowsAsync,
|
|
|
|
} from "../../testing/asserts.ts";
|
|
|
|
import { promisify } from "./_util_promisify.ts";
|
|
|
|
import * as fs from "../fs.ts";
|
|
|
|
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-08-24 19:43:54 -04:00
|
|
|
type VoidFunction = (...args: any[]) => void;
|
|
|
|
|
2020-06-08 13:26:52 -04:00
|
|
|
const readFile = promisify(fs.readFile);
|
2020-07-02 12:03:15 -04:00
|
|
|
const customPromisifyArgs = Symbol.for("nodejs.util.promisify.customArgs");
|
2020-06-08 13:26:52 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Errors should reject the promise",
|
|
|
|
async function testPromiseRejection() {
|
|
|
|
await assertThrowsAsync(() => readFile("/dontexist"), Deno.errors.NotFound);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-06-08 13:26:52 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("Promisify.custom", async function testPromisifyCustom() {
|
2020-06-08 13:26:52 -04:00
|
|
|
function fn(): void {}
|
|
|
|
|
|
|
|
function promisifedFn(): void {}
|
2020-07-26 10:41:10 -04:00
|
|
|
// @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
|
2020-06-08 13:26:52 -04:00
|
|
|
fn[promisify.custom] = promisifedFn;
|
|
|
|
|
|
|
|
const promisifiedFnA = promisify(fn);
|
|
|
|
const promisifiedFnB = promisify(promisifiedFnA);
|
|
|
|
assertStrictEquals(promisifiedFnA, promisifedFn);
|
|
|
|
assertStrictEquals(promisifiedFnB, promisifedFn);
|
|
|
|
|
|
|
|
await promisifiedFnA;
|
|
|
|
await promisifiedFnB;
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("promiisfy.custom symbol", function testPromisifyCustomSymbol() {
|
2020-06-08 13:26:52 -04:00
|
|
|
function fn(): void {}
|
|
|
|
|
|
|
|
function promisifiedFn(): void {}
|
|
|
|
|
|
|
|
// util.promisify.custom is a shared symbol which can be accessed
|
|
|
|
// as `Symbol.for("nodejs.util.promisify.custom")`.
|
|
|
|
const kCustomPromisifiedSymbol = Symbol.for("nodejs.util.promisify.custom");
|
2020-07-26 10:41:10 -04:00
|
|
|
// @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
|
2020-06-08 13:26:52 -04:00
|
|
|
fn[kCustomPromisifiedSymbol] = promisifiedFn;
|
|
|
|
|
|
|
|
assertStrictEquals(kCustomPromisifiedSymbol, promisify.custom);
|
|
|
|
assertStrictEquals(promisify(fn), promisifiedFn);
|
|
|
|
assertStrictEquals(promisify(promisify(fn)), promisifiedFn);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("Invalid argument should throw", function testThrowInvalidArgument() {
|
2020-06-08 13:26:52 -04:00
|
|
|
function fn(): void {}
|
2020-07-26 10:41:10 -04:00
|
|
|
// @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
|
2020-06-08 13:26:52 -04:00
|
|
|
fn[promisify.custom] = 42;
|
|
|
|
try {
|
|
|
|
promisify(fn);
|
|
|
|
} catch (e) {
|
|
|
|
assertStrictEquals(e.code, "ERR_INVALID_ARG_TYPE");
|
|
|
|
assert(e instanceof TypeError);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("Custom promisify args", async function testPromisifyCustomArgs() {
|
2020-06-08 13:26:52 -04:00
|
|
|
const firstValue = 5;
|
|
|
|
const secondValue = 17;
|
|
|
|
|
2020-08-24 19:43:54 -04:00
|
|
|
function fn(callback: VoidFunction): void {
|
2020-06-08 13:26:52 -04:00
|
|
|
callback(null, firstValue, secondValue);
|
|
|
|
}
|
|
|
|
|
2020-07-26 10:41:10 -04:00
|
|
|
// @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
|
2020-06-08 13:26:52 -04:00
|
|
|
fn[customPromisifyArgs] = ["first", "second"];
|
|
|
|
|
|
|
|
const obj = await promisify(fn)();
|
|
|
|
assertEquals(obj, { first: firstValue, second: secondValue });
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Multiple callback args without custom promisify args",
|
|
|
|
async function testPromisifyWithoutCustomArgs() {
|
2020-08-24 19:43:54 -04:00
|
|
|
function fn(callback: VoidFunction): void {
|
2020-06-12 15:23:38 -04:00
|
|
|
callback(null, "foo", "bar");
|
|
|
|
}
|
|
|
|
const value = await promisify(fn)();
|
|
|
|
assertStrictEquals(value, "foo");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-06-08 13:26:52 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Undefined resolved value",
|
|
|
|
async function testPromisifyWithUndefinedResolvedValue() {
|
2020-08-24 19:43:54 -04:00
|
|
|
function fn(callback: VoidFunction): void {
|
2020-06-12 15:23:38 -04:00
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
const value = await promisify(fn)();
|
|
|
|
assertStrictEquals(value, undefined);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-06-08 13:26:52 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Undefined resolved value II",
|
|
|
|
async function testPromisifyWithUndefinedResolvedValueII() {
|
2020-08-24 19:43:54 -04:00
|
|
|
function fn(callback: VoidFunction): void {
|
2020-06-12 15:23:38 -04:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
const value = await promisify(fn)();
|
|
|
|
assertStrictEquals(value, undefined);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-06-08 13:26:52 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Resolved value: number",
|
|
|
|
async function testPromisifyWithNumberResolvedValue() {
|
2020-08-24 19:43:54 -04:00
|
|
|
function fn(err: Error | null, val: number, callback: VoidFunction): void {
|
2020-06-12 15:23:38 -04:00
|
|
|
callback(err, val);
|
|
|
|
}
|
|
|
|
const value = await promisify(fn)(null, 42);
|
|
|
|
assertStrictEquals(value, 42);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-06-08 13:26:52 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Rejected value",
|
|
|
|
async function testPromisifyWithNumberRejectedValue() {
|
2020-08-24 19:43:54 -04:00
|
|
|
function fn(err: Error | null, val: null, callback: VoidFunction): void {
|
2020-06-12 15:23:38 -04:00
|
|
|
callback(err, val);
|
|
|
|
}
|
|
|
|
await assertThrowsAsync(
|
|
|
|
() => promisify(fn)(new Error("oops"), null),
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"oops",
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-06-08 13:26:52 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("Rejected value", async function testPromisifyWithAsObjectMethod() {
|
2020-08-24 19:43:54 -04:00
|
|
|
const o: { fn?: VoidFunction } = {};
|
|
|
|
const fn = promisify(function (this: unknown, cb: VoidFunction): void {
|
2020-06-08 13:26:52 -04:00
|
|
|
cb(null, this === o);
|
|
|
|
});
|
|
|
|
|
|
|
|
o.fn = fn;
|
|
|
|
|
|
|
|
const val = await o.fn();
|
|
|
|
assert(val);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"Multiple callback",
|
|
|
|
async function testPromisifyWithMultipleCallback() {
|
|
|
|
const err = new Error(
|
2020-07-14 15:24:17 -04:00
|
|
|
"Should not have called the callback with the error.",
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
|
|
|
const stack = err.stack;
|
|
|
|
|
2020-08-24 19:43:54 -04:00
|
|
|
const fn = promisify(function (cb: VoidFunction): void {
|
2020-06-12 15:23:38 -04:00
|
|
|
cb(null);
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
await fn();
|
|
|
|
await Promise.resolve();
|
|
|
|
return assertStrictEquals(stack, err.stack);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-06-08 13:26:52 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("Promisify a promise", function testPromisifyPromise() {
|
2020-06-08 13:26:52 -04:00
|
|
|
function c(): void {}
|
|
|
|
const a = promisify(function (): void {});
|
|
|
|
const b = promisify(a);
|
|
|
|
assert(c !== a);
|
|
|
|
assertStrictEquals(a, b);
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("Test error", async function testInvalidArguments() {
|
2020-06-08 13:26:52 -04:00
|
|
|
let errToThrow;
|
|
|
|
|
|
|
|
const thrower = promisify(function (
|
|
|
|
a: number,
|
|
|
|
b: number,
|
|
|
|
c: number,
|
2020-08-24 19:43:54 -04:00
|
|
|
cb: VoidFunction,
|
2020-06-08 13:26:52 -04:00
|
|
|
): void {
|
|
|
|
errToThrow = new Error(`${a}-${b}-${c}-${cb}`);
|
|
|
|
throw errToThrow;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await thrower(1, 2, 3);
|
|
|
|
throw new Error(`should've failed`);
|
|
|
|
} catch (e) {
|
|
|
|
assertStrictEquals(e, errToThrow);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("Test invalid arguments", function testInvalidArguments() {
|
2020-06-08 13:26:52 -04:00
|
|
|
[undefined, null, true, 0, "str", {}, [], Symbol()].forEach((input) => {
|
|
|
|
try {
|
2020-07-26 10:41:10 -04:00
|
|
|
// @ts-expect-error TypeScript
|
2020-06-08 13:26:52 -04:00
|
|
|
promisify(input);
|
|
|
|
} catch (e) {
|
|
|
|
assertStrictEquals(e.code, "ERR_INVALID_ARG_TYPE");
|
|
|
|
assert(e instanceof TypeError);
|
|
|
|
assertEquals(
|
|
|
|
e.message,
|
2020-07-14 15:24:17 -04:00
|
|
|
`The "original" argument must be of type Function. Received ${typeof input}`,
|
2020-06-08 13:26:52 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|