From b9c75e2e5ab048d695219f38d1b469e9672a8ac6 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Tue, 18 Nov 2025 15:42:19 -0500 Subject: [PATCH 1/2] refactor(NODE-7304): remove usages in src of promisify --- src/cmap/wire_protocol/compression.ts | 20 +++++++++++++++++--- src/mongo_logger.ts | 16 ++++++++++------ src/utils.ts | 10 ++++++++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/cmap/wire_protocol/compression.ts b/src/cmap/wire_protocol/compression.ts index b7d2980210e..52ed799556d 100644 --- a/src/cmap/wire_protocol/compression.ts +++ b/src/cmap/wire_protocol/compression.ts @@ -1,4 +1,3 @@ -import { promisify } from 'util'; import * as zlib from 'zlib'; import { LEGACY_HELLO_COMMAND } from '../../constants'; @@ -43,8 +42,23 @@ export const uncompressibleCommands = new Set([ const ZSTD_COMPRESSION_LEVEL = 3; -const zlibInflate = promisify(zlib.inflate.bind(zlib)); -const zlibDeflate = promisify(zlib.deflate.bind(zlib)); +const zlibInflate = (buf: zlib.InputType) => { + return new Promise((resolve, reject) => { + zlib.inflate(buf, (error, result) => { + if (error) reject(error); + resolve(result); + }); + }); +}; + +const zlibDeflate = (buf: zlib.InputType, options: zlib.ZlibOptions) => { + return new Promise((resolve, reject) => { + zlib.deflate(buf, options, (error, result) => { + if (error) reject(error); + resolve(result); + }); + }); +}; let zstd: ZStandard; let Snappy: SnappyLib | null = null; diff --git a/src/mongo_logger.ts b/src/mongo_logger.ts index af724017090..52bd4411985 100644 --- a/src/mongo_logger.ts +++ b/src/mongo_logger.ts @@ -1,4 +1,4 @@ -import { inspect, promisify } from 'util'; +import { inspect } from 'util'; import { type Binary, @@ -240,11 +240,15 @@ export function createStdioLogger(stream: { write: NodeJS.WriteStream['write']; }): MongoDBLogWritable { return { - write: promisify((log: Log, cb: (error?: Error | null) => void): unknown => { - const logLine = inspect(log, { compact: true, breakLength: Infinity }); - stream.write(`${logLine}\n`, 'utf-8', cb); - return; - }) + write: (log: Log): Promise => { + return new Promise((resolve, reject) => { + const logLine = inspect(log, { compact: true, breakLength: Infinity }); + stream.write(`${logLine}\n`, 'utf-8', error => { + if (error) reject(error); + resolve(true); + }); + }); + } }; } diff --git a/src/utils.ts b/src/utils.ts index e078f91d7c5..3b789d6101f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,7 +4,6 @@ import { type EventEmitter } from 'events'; import { promises as fs } from 'fs'; import * as http from 'http'; import { clearTimeout, setTimeout } from 'timers'; -import { promisify } from 'util'; import { deserialize, type Document, ObjectId, resolveBSONOptions } from './bson'; import type { Connection } from './cmap/connection'; @@ -1236,7 +1235,14 @@ export function squashError(_error: unknown) { return; } -export const randomBytes = promisify(crypto.randomBytes); +export const randomBytes = (size: number) => { + return new Promise((resolve, reject) => { + crypto.randomBytes(size, (error: Error | null, buf: Buffer) => { + if (error) reject(error); + resolve(buf); + }); + }); +}; /** * Replicates the events.once helper. From 461bcde9c4d87f55e297ef84e0ce7f85ec63cb79 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 19 Nov 2025 14:41:03 -0500 Subject: [PATCH 2/2] fix: return on reject --- src/cmap/wire_protocol/compression.ts | 4 ++-- src/mongo_logger.ts | 2 +- src/utils.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cmap/wire_protocol/compression.ts b/src/cmap/wire_protocol/compression.ts index 52ed799556d..4ee941ff3c9 100644 --- a/src/cmap/wire_protocol/compression.ts +++ b/src/cmap/wire_protocol/compression.ts @@ -45,7 +45,7 @@ const ZSTD_COMPRESSION_LEVEL = 3; const zlibInflate = (buf: zlib.InputType) => { return new Promise((resolve, reject) => { zlib.inflate(buf, (error, result) => { - if (error) reject(error); + if (error) return reject(error); resolve(result); }); }); @@ -54,7 +54,7 @@ const zlibInflate = (buf: zlib.InputType) => { const zlibDeflate = (buf: zlib.InputType, options: zlib.ZlibOptions) => { return new Promise((resolve, reject) => { zlib.deflate(buf, options, (error, result) => { - if (error) reject(error); + if (error) return reject(error); resolve(result); }); }); diff --git a/src/mongo_logger.ts b/src/mongo_logger.ts index 52bd4411985..fd869d63394 100644 --- a/src/mongo_logger.ts +++ b/src/mongo_logger.ts @@ -244,7 +244,7 @@ export function createStdioLogger(stream: { return new Promise((resolve, reject) => { const logLine = inspect(log, { compact: true, breakLength: Infinity }); stream.write(`${logLine}\n`, 'utf-8', error => { - if (error) reject(error); + if (error) return reject(error); resolve(true); }); }); diff --git a/src/utils.ts b/src/utils.ts index 3b789d6101f..49aa15ccea4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1238,7 +1238,7 @@ export function squashError(_error: unknown) { export const randomBytes = (size: number) => { return new Promise((resolve, reject) => { crypto.randomBytes(size, (error: Error | null, buf: Buffer) => { - if (error) reject(error); + if (error) return reject(error); resolve(buf); }); });