Skip to content

Commit d5a376a

Browse files
BridgeARMylesBorins
authored andcommitted
lib: remove circular reference
PR-URL: #14885 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 391855c commit d5a376a

File tree

2 files changed

+11
-30
lines changed

2 files changed

+11
-30
lines changed

lib/assert.js

+9-15
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
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');
27-
const { Buffer } = require('buffer');
2826
const errors = require('internal/errors');
2927

3028
// The assert module provides functions that throw
@@ -108,8 +106,8 @@ function areSimilarRegExps(a, b) {
108106
}
109107

110108
// For small buffers it's faster to compare the buffer in a loop. The c++
111-
// barrier including the Buffer.from operation takes the advantage of the faster
112-
// compare otherwise. 300 was the number after which compare became faster.
109+
// barrier including the Uint8Array operation takes the advantage of the faster
110+
// binary compare otherwise. The break even point was at about 300 characters.
113111
function areSimilarTypedArrays(a, b) {
114112
const len = a.byteLength;
115113
if (len !== b.byteLength) {
@@ -123,12 +121,8 @@ function areSimilarTypedArrays(a, b) {
123121
}
124122
return true;
125123
}
126-
return compare(Buffer.from(a.buffer,
127-
a.byteOffset,
128-
len),
129-
Buffer.from(b.buffer,
130-
b.byteOffset,
131-
b.byteLength)) === 0;
124+
return compare(new Uint8Array(a.buffer, a.byteOffset, len),
125+
new Uint8Array(b.buffer, b.byteOffset, b.byteLength)) === 0;
132126
}
133127

134128
function isFloatTypedArrayTag(tag) {
@@ -189,11 +183,11 @@ function strictDeepEqual(actual, expected) {
189183
// Skip testing the part below and continue in the callee function.
190184
return;
191185
}
192-
if (util.isDate(actual)) {
186+
if (isDate(actual)) {
193187
if (actual.getTime() !== expected.getTime()) {
194188
return false;
195189
}
196-
} else if (util.isRegExp(actual)) {
190+
} else if (isRegExp(actual)) {
197191
if (!areSimilarRegExps(actual, expected)) {
198192
return false;
199193
}
@@ -229,10 +223,10 @@ function looseDeepEqual(actual, expected) {
229223
if (expected === null || typeof expected !== 'object') {
230224
return false;
231225
}
232-
if (util.isDate(actual) && util.isDate(expected)) {
226+
if (isDate(actual) && isDate(expected)) {
233227
return actual.getTime() === expected.getTime();
234228
}
235-
if (util.isRegExp(actual) && util.isRegExp(expected)) {
229+
if (isRegExp(actual) && isRegExp(expected)) {
236230
return areSimilarRegExps(actual, expected);
237231
}
238232
if (actual instanceof Error && expected instanceof Error) {

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

@@ -1118,6 +1119,7 @@ module.exports = exports = {
11181119
inspect,
11191120
isArray: Array.isArray,
11201121
isBoolean,
1122+
isBuffer,
11211123
isNull,
11221124
isNullOrUndefined,
11231125
isNumber,
@@ -1149,18 +1151,3 @@ module.exports = exports = {
11491151
'util.puts is deprecated. Use console.log instead.',
11501152
'DEP0027')
11511153
};
1152-
1153-
// Avoid a circular dependency
1154-
var isBuffer;
1155-
Object.defineProperty(exports, 'isBuffer', {
1156-
configurable: true,
1157-
enumerable: true,
1158-
get() {
1159-
if (!isBuffer)
1160-
isBuffer = require('buffer').Buffer.isBuffer;
1161-
return isBuffer;
1162-
},
1163-
set(val) {
1164-
isBuffer = val;
1165-
}
1166-
});

0 commit comments

Comments
 (0)