Skip to content

Commit

Permalink
Merge pull request #2853 from cloudflare/yagiz/remove-unnecessary
Browse files Browse the repository at this point in the history
remove unnecessary deferredPromise declarations
  • Loading branch information
anonrig authored Oct 7, 2024
2 parents 0d58827 + 50fb47e commit acda144
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 117 deletions.
16 changes: 0 additions & 16 deletions src/node/internal/internal_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,3 @@ export function getOwnNonIndexProperties(
}
return result;
}

export function createDeferredPromise() {
let resolve;
let reject;

// eslint-disable-next-line promise/param-names
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return {
promise,
resolve,
reject,
};
}
9 changes: 3 additions & 6 deletions src/node/internal/streams_adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ import {
AbortError,
} from 'node-internal:internal_errors';

import {
createDeferredPromise,
normalizeEncoding,
} from 'node-internal:internal_utils';
import { normalizeEncoding } from 'node-internal:internal_utils';

import { validateBoolean, validateObject } from 'node-internal:validators';

Expand Down Expand Up @@ -144,7 +141,7 @@ export function newWritableStreamFromStreamWritable(streamWritable) {

async write(chunk) {
if (streamWritable.writableNeedDrain || !streamWritable.write(chunk)) {
backpressurePromise = createDeferredPromise();
backpressurePromise = Promise.withResolvers();
return backpressurePromise.promise.finally(() => {
backpressurePromise = undefined;
});
Expand All @@ -157,7 +154,7 @@ export function newWritableStreamFromStreamWritable(streamWritable) {

close() {
if (closed === undefined && !isWritableEnded(streamWritable)) {
closed = createDeferredPromise();
closed = Promise.withResolvers();
streamWritable.end();
return closed.promise;
}
Expand Down
6 changes: 2 additions & 4 deletions src/node/internal/streams_duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import {
newReadableWritablePairFromDuplex,
} from 'node-internal:streams_adapters';

import { createDeferredPromise } from 'node-internal:internal_utils';

import * as process from 'node-internal:process';

import {
Expand Down Expand Up @@ -365,7 +363,7 @@ function duplexify(body, name) {
}

function fromAsyncGen(fn) {
let { promise, resolve } = createDeferredPromise();
let { promise, resolve } = Promise.withResolvers();
const ac = new AbortController();
const signal = ac.signal;
const value = fn(
Expand All @@ -380,7 +378,7 @@ function fromAsyncGen(fn) {
throw new AbortError(undefined, {
cause: signal.reason,
});
({ promise, resolve } = createDeferredPromise());
({ promise, resolve } = Promise.withResolvers());
yield chunk;
}
})(),
Expand Down
6 changes: 2 additions & 4 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ export const TextDecoder = globalThis.TextDecoder;
export const TextEncoder = globalThis.TextEncoder;

export function toUSVString(input: any) {
// TODO(cleanup): Apparently the typescript types for this aren't available yet?
return (`${input}` as any).toWellFormed();
return input.toWellFormed();
}

function pad(n: any): string {
Expand Down Expand Up @@ -231,8 +230,7 @@ export async function aborted(signal: AbortSignal, resource: object) {
allowFunction: true,
});
if (signal.aborted) return Promise.resolve();
// TODO(cleanup): Apparently withResolvers isn't part of type defs we use yet
const { promise, resolve } = (Promise as any).withResolvers();
const { promise, resolve } = Promise.withResolvers();
const opts = { __proto__: null, once: true };
signal.addEventListener('abort', resolve, opts);
return promise;
Expand Down
15 changes: 1 addition & 14 deletions src/workerd/api/node/tests/crypto_hash-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,22 +264,9 @@ export const hash_copy_test = {
},
};

function deferredPromise() {
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return {
promise,
resolve,
reject,
};
}

export const hash_pipe_test = {
async test(ctrl, env, ctx) {
const p = deferredPromise();
const p = Promise.withResolvers();

const s = new stream.PassThrough();
const h = crypto.createHash('sha512');
Expand Down
27 changes: 7 additions & 20 deletions src/workerd/api/node/tests/crypto_hkdf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ import {
getHashes,
} from 'node:crypto';

function deferredPromise() {
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return {
promise,
resolve,
reject,
};
}

export const hkdf_error_tests = {
async test(ctrl, env, ctx) {
assert.throws(() => hkdf(), {
Expand Down Expand Up @@ -130,7 +117,7 @@ export const hkdf_error_tests = {
);

{
const p = deferredPromise();
const p = Promise.withResolvers();
hkdf('unknown', 'a', '', '', 10, (err, asyncResult) => {
if (err) {
return p.reject(err);
Expand All @@ -151,7 +138,7 @@ export const hkdf_error_tests = {
});

{
const p = deferredPromise();
const p = Promise.withResolvers();
hkdf('sha512', 'a', '', '', 64 * 255 + 1, (err, asyncResult) => {
if (err) {
return p.reject(err);
Expand All @@ -171,7 +158,7 @@ async function hkdfTestAlg([hash, secret, salt, info, length]) {
const syncResult = hkdfSync(hash, secret, salt, info, length);
assert.ok(syncResult instanceof ArrayBuffer);
let is_async = false;
const p = deferredPromise();
const p = Promise.withResolvers();

hkdf(hash, secret, salt, info, length, (err, asyncResult) => {
if (err) return p.reject(err);
Expand All @@ -190,7 +177,7 @@ async function hkdfTestAlg([hash, secret, salt, info, length]) {
const buf_secret = Buffer.from(secret);
const buf_salt = Buffer.from(salt);
const buf_info = Buffer.from(info);
const p = deferredPromise();
const p = Promise.withResolvers();

const syncResult = hkdfSync(hash, buf_secret, buf_salt, buf_info, length);
hkdf(hash, buf_secret, buf_salt, buf_info, length, (err, asyncResult) => {
Expand All @@ -206,7 +193,7 @@ async function hkdfTestAlg([hash, secret, salt, info, length]) {
// const key_secret = createSecretKey(Buffer.from(secret));
// const buf_salt = Buffer.from(salt);
// const buf_info = Buffer.from(info);
// const p = deferredPromise();
// const p = Promise.withResolvers();
//
// const syncResult = hkdfSync(hash, key_secret, buf_salt, buf_info, length);
// hkdf(hash, key_secret, buf_salt, buf_info, length, (err, asyncResult) => {
Expand All @@ -218,7 +205,7 @@ async function hkdfTestAlg([hash, secret, salt, info, length]) {
// }

{
const p = deferredPromise();
const p = Promise.withResolvers();
const ta_secret = new Uint8Array(Buffer.from(secret));
const ta_salt = new Uint16Array(Buffer.from(salt));
const ta_info = new Uint32Array(Buffer.from(info));
Expand All @@ -242,7 +229,7 @@ async function hkdfTestAlg([hash, secret, salt, info, length]) {
}

{
const p = deferredPromise();
const p = Promise.withResolvers();
const ta_secret = new Uint8Array(Buffer.from(secret));
const a_salt = new ArrayBuffer(0);
const a_info = new ArrayBuffer(1);
Expand Down
19 changes: 3 additions & 16 deletions src/workerd/api/node/tests/crypto_pbkdf2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,6 @@
import * as assert from 'node:assert';
import * as crypto from 'node:crypto';

function deferredPromise() {
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return {
promise,
resolve,
reject,
};
}

function mustNotCall() {
return () => {};
}
Expand All @@ -54,7 +41,7 @@ async function runPBKDF2(password, salt, iterations, keylen, hash) {
hash
);

const p = deferredPromise();
const p = Promise.withResolvers();
crypto.pbkdf2(
password,
salt,
Expand Down Expand Up @@ -211,7 +198,7 @@ export const empty_pwd_test = {
async test(ctrl, env, ctx) {
// Should not get FATAL ERROR with empty password and salt
// https://github.com/nodejs/node/issues/8571
const p = deferredPromise();
const p = Promise.withResolvers();
crypto.pbkdf2('', '', 1, 32, 'sha256', (err, prime) => {
p.resolve();
});
Expand Down Expand Up @@ -310,7 +297,7 @@ export const TypedArray_tests = {
export const invalid_digest_tests = {
async test(ctrl, env, ctx) {
{
const p = deferredPromise();
const p = Promise.withResolvers();
crypto.pbkdf2('pass', 'salt', 8, 8, 'md55', (err, prime) => {
if (err) return p.reject(err);
});
Expand Down
29 changes: 8 additions & 21 deletions src/workerd/api/node/tests/crypto_random-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@ import {

import { Buffer } from 'node:buffer';

function deferredPromise() {
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return {
promise,
resolve,
reject,
};
}

export const test = {
async test(ctrl, env, ctx) {
[1, 'hello', {}, []].forEach((i) => {
Expand Down Expand Up @@ -141,7 +128,7 @@ export const test = {
}

{
const p = deferredPromise();
const p = Promise.withResolvers();
generatePrime(80, (err, prime) => {
ok(checkPrimeSync(prime));
checkPrime(prime, (err, result) => {
Expand All @@ -155,7 +142,7 @@ export const test = {
ok(checkPrimeSync(generatePrimeSync(80)));

{
const p = deferredPromise();
const p = Promise.withResolvers();
generatePrime(80, {}, (err, prime) => {
if (err) return p.reject(err);
ok(checkPrimeSync(prime));
Expand All @@ -167,7 +154,7 @@ export const test = {
ok(checkPrimeSync(generatePrimeSync(80, {})));

{
const p = deferredPromise();
const p = Promise.withResolvers();
generatePrime(32, { safe: true }, (err, prime) => {
if (err) return p.reject(err);
ok(checkPrimeSync(prime));
Expand Down Expand Up @@ -197,7 +184,7 @@ export const test = {
const rem_buf = Buffer.from([rem]);

{
const p = deferredPromise();
const p = Promise.withResolvers();
generatePrime(32, { add: add_buf, rem: rem_buf }, (err, prime) => {
if (err) return p.reject(err);
ok(checkPrimeSync(prime));
Expand Down Expand Up @@ -229,7 +216,7 @@ export const test = {
}

{
const p = deferredPromise();
const p = Promise.withResolvers();
generatePrime(
128,
{
Expand All @@ -244,7 +231,7 @@ export const test = {
await rejects(p.promise);
}
{
const p = deferredPromise();
const p = Promise.withResolvers();
generatePrime(
128,
{
Expand Down Expand Up @@ -383,7 +370,7 @@ export const test = {
strictEqual(typeof prime, 'bigint');
strictEqual(prime, 7n);
ok(checkPrimeSync(prime));
const p = deferredPromise();
const p = Promise.withResolvers();
checkPrime(prime, (err, result) => {
if (err) return p.reject(err);
p.resolve(result);
Expand All @@ -392,7 +379,7 @@ export const test = {
}

{
const p = deferredPromise();
const p = Promise.withResolvers();
generatePrime(3, { bigint: true }, (err, prime) => {
if (err) return p.reject(err);
strictEqual(typeof prime, 'bigint');
Expand Down
21 changes: 6 additions & 15 deletions src/workerd/api/node/tests/diagnostics-channel-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ import {

import { AsyncLocalStorage } from 'node:async_hooks';

function deferredPromise() {
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}

export const test_basics = {
async test(ctrl, env, ctx) {
ok(!hasSubscribers('foo'));
Expand All @@ -28,7 +19,7 @@ export const test_basics = {
strictEqual(channel1, channel2);
ok(channel1 instanceof Channel);

const messagePromise = deferredPromise();
const messagePromise = Promise.withResolvers();

const listener = (message) => {
try {
Expand Down Expand Up @@ -66,11 +57,11 @@ export const test_tracing = {
tc.start.bindStore(als);

const promises = [
deferredPromise(),
deferredPromise(),
deferredPromise(),
deferredPromise(),
deferredPromise(),
Promise.withResolvers(),
Promise.withResolvers(),
Promise.withResolvers(),
Promise.withResolvers(),
Promise.withResolvers(),
];

const context = {};
Expand Down
Loading

0 comments on commit acda144

Please sign in to comment.