Skip to content

Commit 43ff12e

Browse files
authored
refactor: Remove util dependency (#402)
Reduce the dependency on node's util by replacing the deprecate function and inspect symbol NODE-2826
1 parent 5f0c8f2 commit 43ff12e

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default [
3232
plugins: [
3333
nodeResolve({ preferBuiltins: false }),
3434
commonjs(),
35-
nodePolyfills(),
35+
nodePolyfills({ include: ['buffer'] }),
3636
babel({
3737
babelHelpers: 'bundled',
3838
presets: [

src/objectid.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { Buffer } from 'buffer';
22
import { ensureBuffer } from './ensure_buffer';
3-
import { randomBytes } from './parser/utils';
4-
5-
declare const require: Function;
6-
// eslint-disable-next-line @typescript-eslint/no-var-requires
7-
const { deprecate, inspect } = require('util');
3+
import { deprecate, randomBytes } from './parser/utils';
84

95
// constants
106
const PROCESS_UNIQUE = randomBytes(5);
@@ -366,12 +362,14 @@ Object.defineProperty(ObjectId, 'get_inc', {
366362
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
367363
});
368364

365+
const inspect = Symbol.for('nodejs.util.inspect.custom');
369366
/**
370367
* Converts to a string representation of this Id.
371368
*
372369
* @returns return the 24 character hex string representation.
373370
* @internal
374371
*/
375-
Object.defineProperty(ObjectId.prototype, inspect.custom || 'inspect', ObjectId.prototype.toString);
372+
Object.defineProperty(ObjectId.prototype, inspect, ObjectId.prototype.toString);
373+
Object.defineProperty(ObjectId.prototype, 'inspect', ObjectId.prototype.toString);
376374

377375
Object.defineProperty(ObjectId.prototype, '_bsontype', { value: 'ObjectID' });

src/parser/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ declare let window: any;
2020
declare let require: Function;
2121
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2222
declare let global: any;
23+
declare const self: unknown;
2324

2425
export let randomBytes = insecureRandomBytes;
2526
if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {
@@ -73,3 +74,20 @@ export function isDate(d: unknown): d is Date {
7374
export function isObjectLike(candidate: unknown): candidate is Record<string, unknown> {
7475
return typeof candidate === 'object' && candidate !== null;
7576
}
77+
78+
declare let console: { warn(...message: unknown[]): void };
79+
export function deprecate<T extends Function>(fn: T, message: string): T {
80+
if (typeof window === 'undefined' || typeof self === 'undefined') {
81+
// eslint-disable-next-line @typescript-eslint/no-var-requires
82+
return require('util').deprecate(fn, message);
83+
}
84+
let warned = false;
85+
function deprecated(this: unknown, ...args: unknown[]) {
86+
if (!warned) {
87+
console.warn(message);
88+
warned = true;
89+
}
90+
return fn.apply(this, ...args);
91+
}
92+
return (deprecated as unknown) as T;
93+
}

0 commit comments

Comments
 (0)