Skip to content

Commit 9aa7093

Browse files
committed
lib: remove circular reference
PR-URL: nodejs#14885 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 4ca8ff2 commit 9aa7093

File tree

2 files changed

+11
-26
lines changed

2 files changed

+11
-26
lines changed

lib/assert.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
'use strict';
2222

2323
const { compare } = process.binding('buffer');
24-
const util = require('util');
25-
const { isSet, isMap } = process.binding('util');
24+
const { isSet, isMap, isDate, isRegExp } = process.binding('util');
2625
const { objectToString } = require('internal/util');
2726
const errors = require('internal/errors');
2827

@@ -115,10 +114,9 @@ function areSimilarRegExps(a, b) {
115114
}
116115

117116
// For small buffers it's faster to compare the buffer in a loop. The c++
118-
// barrier including the Buffer.from operation takes the advantage of the faster
119-
// compare otherwise. 300 was the number after which compare became faster.
117+
// barrier including the Uint8Array operation takes the advantage of the faster
118+
// binary compare otherwise. The break even point was at about 300 characters.
120119
function areSimilarTypedArrays(a, b) {
121-
const { from } = require('buffer').Buffer;
122120
const len = a.byteLength;
123121
if (len !== b.byteLength) {
124122
return false;
@@ -131,8 +129,8 @@ function areSimilarTypedArrays(a, b) {
131129
}
132130
return true;
133131
}
134-
return compare(from(a.buffer, a.byteOffset, len),
135-
from(b.buffer, b.byteOffset, b.byteLength)) === 0;
132+
return compare(new Uint8Array(a.buffer, a.byteOffset, len),
133+
new Uint8Array(b.buffer, b.byteOffset, b.byteLength)) === 0;
136134
}
137135

138136
function isFloatTypedArrayTag(tag) {
@@ -186,11 +184,11 @@ function strictDeepEqual(actual, expected) {
186184
// Skip testing the part below and continue in the callee function.
187185
return;
188186
}
189-
if (util.isDate(actual)) {
187+
if (isDate(actual)) {
190188
if (actual.getTime() !== expected.getTime()) {
191189
return false;
192190
}
193-
} else if (util.isRegExp(actual)) {
191+
} else if (isRegExp(actual)) {
194192
if (!areSimilarRegExps(actual, expected)) {
195193
return false;
196194
}
@@ -219,10 +217,10 @@ function looseDeepEqual(actual, expected) {
219217
if (expected === null || typeof expected !== 'object') {
220218
return false;
221219
}
222-
if (util.isDate(actual) && util.isDate(expected)) {
220+
if (isDate(actual) && isDate(expected)) {
223221
return actual.getTime() === expected.getTime();
224222
}
225-
if (util.isRegExp(actual) && util.isRegExp(expected)) {
223+
if (isRegExp(actual) && isRegExp(expected)) {
226224
return areSimilarRegExps(actual, expected);
227225
}
228226
const actualTag = objectToString(actual);

lib/util.js

+2-15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const errors = require('internal/errors');
2525
const { TextDecoder, TextEncoder } = require('internal/encoding');
26+
const { isBuffer } = require('buffer').Buffer;
2627

2728
const { errname } = process.binding('uv');
2829

@@ -1134,6 +1135,7 @@ module.exports = exports = {
11341135
inspect,
11351136
isArray: Array.isArray,
11361137
isBoolean,
1138+
isBuffer,
11371139
isNull,
11381140
isNullOrUndefined,
11391141
isNumber,
@@ -1165,18 +1167,3 @@ module.exports = exports = {
11651167
'util.puts is deprecated. Use console.log instead.',
11661168
'DEP0027')
11671169
};
1168-
1169-
// Avoid a circular dependency
1170-
var isBuffer;
1171-
Object.defineProperty(exports, 'isBuffer', {
1172-
configurable: true,
1173-
enumerable: true,
1174-
get() {
1175-
if (!isBuffer)
1176-
isBuffer = require('buffer').Buffer.isBuffer;
1177-
return isBuffer;
1178-
},
1179-
set(val) {
1180-
isBuffer = val;
1181-
}
1182-
});

0 commit comments

Comments
 (0)