Skip to content

Commit 761b9bf

Browse files
authored
feat(NODE-7304): remove usages in src of promisify (#4799)
1 parent 49c5b6f commit 761b9bf

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

src/cmap/wire_protocol/compression.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { promisify } from 'util';
21
import * as zlib from 'zlib';
32

43
import { LEGACY_HELLO_COMMAND } from '../../constants';
@@ -43,8 +42,23 @@ export const uncompressibleCommands = new Set([
4342

4443
const ZSTD_COMPRESSION_LEVEL = 3;
4544

46-
const zlibInflate = promisify(zlib.inflate.bind(zlib));
47-
const zlibDeflate = promisify(zlib.deflate.bind(zlib));
45+
const zlibInflate = (buf: zlib.InputType) => {
46+
return new Promise<Buffer>((resolve, reject) => {
47+
zlib.inflate(buf, (error, result) => {
48+
if (error) return reject(error);
49+
resolve(result);
50+
});
51+
});
52+
};
53+
54+
const zlibDeflate = (buf: zlib.InputType, options: zlib.ZlibOptions) => {
55+
return new Promise<Buffer>((resolve, reject) => {
56+
zlib.deflate(buf, options, (error, result) => {
57+
if (error) return reject(error);
58+
resolve(result);
59+
});
60+
});
61+
};
4862

4963
let zstd: ZStandard;
5064
let Snappy: SnappyLib | null = null;

src/mongo_logger.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { inspect, promisify } from 'util';
1+
import { inspect } from 'util';
22

33
import {
44
type Binary,
@@ -240,11 +240,15 @@ export function createStdioLogger(stream: {
240240
write: NodeJS.WriteStream['write'];
241241
}): MongoDBLogWritable {
242242
return {
243-
write: promisify((log: Log, cb: (error?: Error | null) => void): unknown => {
244-
const logLine = inspect(log, { compact: true, breakLength: Infinity });
245-
stream.write(`${logLine}\n`, 'utf-8', cb);
246-
return;
247-
})
243+
write: (log: Log): Promise<unknown> => {
244+
return new Promise((resolve, reject) => {
245+
const logLine = inspect(log, { compact: true, breakLength: Infinity });
246+
stream.write(`${logLine}\n`, 'utf-8', error => {
247+
if (error) return reject(error);
248+
resolve(true);
249+
});
250+
});
251+
}
248252
};
249253
}
250254

src/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { type EventEmitter } from 'events';
44
import { promises as fs } from 'fs';
55
import * as http from 'http';
66
import { clearTimeout, setTimeout } from 'timers';
7-
import { promisify } from 'util';
87

98
import { deserialize, type Document, ObjectId, resolveBSONOptions } from './bson';
109
import type { Connection } from './cmap/connection';
@@ -1236,7 +1235,14 @@ export function squashError(_error: unknown) {
12361235
return;
12371236
}
12381237

1239-
export const randomBytes = promisify(crypto.randomBytes);
1238+
export const randomBytes = (size: number) => {
1239+
return new Promise<Buffer>((resolve, reject) => {
1240+
crypto.randomBytes(size, (error: Error | null, buf: Buffer) => {
1241+
if (error) return reject(error);
1242+
resolve(buf);
1243+
});
1244+
});
1245+
};
12401246

12411247
/**
12421248
* Replicates the events.once helper.

0 commit comments

Comments
 (0)