Skip to content

Commit b5aae52

Browse files
Revert "util: move util._extend to eol"
This reverts commit ac97a53. PR-URL: #53429 Refs: tape-testing/tape#607 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ee8e841 commit b5aae52

File tree

7 files changed

+93
-7
lines changed

7 files changed

+93
-7
lines changed

benchmark/es/spread-assign.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
22

33
const common = require('../common.js');
4+
const util = require('util');
45

56
const bench = common.createBenchmark(main, {
6-
method: ['spread', 'assign'],
7+
method: ['spread', 'assign', '_extend'],
78
count: [5, 10, 20],
89
n: [1e6],
910
});
@@ -17,6 +18,12 @@ function main({ n, context, count, rest, method }) {
1718
let obj;
1819

1920
switch (method) {
21+
case '_extend':
22+
bench.start();
23+
for (let i = 0; i < n; i++)
24+
obj = util._extend({}, src);
25+
bench.end(n);
26+
break;
2027
case 'assign':
2128
bench.start();
2229
for (let i = 0; i < n; i++)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const util = require('util');
5+
6+
const bench = common.createBenchmark(main, {
7+
type: ['extend', 'assign'],
8+
n: [10e4],
9+
});
10+
11+
function main({ n, type }) {
12+
let fn;
13+
if (type === 'extend') {
14+
fn = util._extend;
15+
} else if (type === 'assign') {
16+
fn = Object.assign;
17+
}
18+
19+
// Force-optimize the method to test so that the benchmark doesn't
20+
// get disrupted by the optimizer kicking in halfway through.
21+
for (let i = 0; i < type.length * 10; i += 1)
22+
fn({}, process.env);
23+
24+
const obj = new Proxy({}, { set: function(a, b, c) { return true; } });
25+
26+
bench.start();
27+
for (let j = 0; j < n; j += 1)
28+
fn(obj, process.env);
29+
bench.end(n);
30+
}

doc/api/deprecations.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -1408,9 +1408,6 @@ requirements and complexity of your application.
14081408

14091409
<!-- YAML
14101410
changes:
1411-
- version: REPLACEME
1412-
pr-url: https://github.com/nodejs/node/pull/52744
1413-
description: End-of-Life deprecation.
14141411
- version: v22.0.0
14151412
pr-url: https://github.com/nodejs/node/pull/50488
14161413
description: Runtime deprecation.
@@ -1422,9 +1419,9 @@ changes:
14221419
description: Documentation-only deprecation.
14231420
-->
14241421

1425-
Type: End-of-Life
1422+
Type: Runtime
14261423

1427-
The `util._extend()` API has been removed because it's an unmaintained
1424+
The [`util._extend()`][] API is deprecated because it's an unmaintained
14281425
legacy API that was exposed to user land by accident.
14291426
Please use `target = Object.assign(target, source)` instead.
14301427

@@ -3836,6 +3833,7 @@ and [`crypto.setEngine()`][] all depend on this functionality from OpenSSL.
38363833
[`url.format()`]: url.md#urlformaturlobject
38373834
[`url.parse()`]: url.md#urlparseurlstring-parsequerystring-slashesdenotehost
38383835
[`url.resolve()`]: url.md#urlresolvefrom-to
3836+
[`util._extend()`]: util.md#util_extendtarget-source
38393837
[`util.getSystemErrorName()`]: util.md#utilgetsystemerrornameerr
38403838
[`util.inspect()`]: util.md#utilinspectobject-options
38413839
[`util.inspect.custom`]: util.md#utilinspectcustom

doc/api/util.md

+19
Original file line numberDiff line numberDiff line change
@@ -2923,6 +2923,24 @@ util.types.isWeakSet(new WeakSet()); // Returns true
29232923
The following APIs are deprecated and should no longer be used. Existing
29242924
applications and modules should be updated to find alternative approaches.
29252925
2926+
### `util._extend(target, source)`
2927+
2928+
<!-- YAML
2929+
added: v0.7.5
2930+
deprecated: v6.0.0
2931+
-->
2932+
2933+
> Stability: 0 - Deprecated: Use [`Object.assign()`][] instead.
2934+
2935+
* `target` {Object}
2936+
* `source` {Object}
2937+
2938+
The `util._extend()` method was never intended to be used outside of internal
2939+
Node.js modules. The community found and used it anyway.
2940+
2941+
It is deprecated and should not be used in new code. JavaScript comes with very
2942+
similar built-in functionality through [`Object.assign()`][].
2943+
29262944
### `util.isArray(object)`
29272945
29282946
<!-- YAML
@@ -2972,6 +2990,7 @@ util.isArray({});
29722990
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
29732991
[`MIMEparams`]: #class-utilmimeparams
29742992
[`Map`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
2993+
[`Object.assign()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
29752994
[`Object.freeze()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
29762995
[`Promise`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
29772996
[`Proxy`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy

lib/util.js

+23
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,26 @@ function inherits(ctor, superCtor) {
153153
ObjectSetPrototypeOf(ctor.prototype, superCtor.prototype);
154154
}
155155

156+
/**
157+
* @deprecated since v6.0.0
158+
* @template T
159+
* @template S
160+
* @param {T} target
161+
* @param {S} source
162+
* @returns {S extends null ? T : (T & S)}
163+
*/
164+
function _extend(target, source) {
165+
// Don't do anything if source isn't an object
166+
if (source === null || typeof source !== 'object') return target;
167+
168+
const keys = ObjectKeys(source);
169+
let i = keys.length;
170+
while (i--) {
171+
target[keys[i]] = source[keys[i]];
172+
}
173+
return target;
174+
}
175+
156176
const callbackifyOnRejected = (reason, cb) => {
157177
// `!reason` guard inspired by bluebird (Ref: https://goo.gl/t5IS6M).
158178
// Because `null` is a special error value in callbacks which means "no error
@@ -259,6 +279,9 @@ function parseEnv(content) {
259279
module.exports = {
260280
_errnoException,
261281
_exceptionWithHostPort,
282+
_extend: deprecate(_extend,
283+
'The `util._extend` API is deprecated. Please use Object.assign() instead.',
284+
'DEP0060'),
262285
callbackify,
263286
debug: debuglog,
264287
debuglog,

test/fixtures/tls-connect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ exports.connect = function connect(options, callback) {
5757
}).listen(0, function() {
5858
server.server = this;
5959

60-
const optClient = Object.assign({
60+
const optClient = util._extend({
6161
port: this.address().port,
6262
}, options.client);
6363

test/parallel/test-util.js

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ assert.strictEqual(util.isArray(/regexp/), false);
4040
assert.strictEqual(util.isArray(new Error()), false);
4141
assert.strictEqual(util.isArray({ __proto__: Array.prototype }), false);
4242

43+
// _extend
44+
assert.deepStrictEqual(util._extend({ a: 1 }), { a: 1 });
45+
assert.deepStrictEqual(util._extend({ a: 1 }, []), { a: 1 });
46+
assert.deepStrictEqual(util._extend({ a: 1 }, null), { a: 1 });
47+
assert.deepStrictEqual(util._extend({ a: 1 }, true), { a: 1 });
48+
assert.deepStrictEqual(util._extend({ a: 1 }, false), { a: 1 });
49+
assert.deepStrictEqual(util._extend({ a: 1 }, { b: 2 }), { a: 1, b: 2 });
50+
assert.deepStrictEqual(util._extend({ a: 1, b: 2 }, { b: 3 }), { a: 1, b: 3 });
51+
4352
assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd');
4453

4554
{

0 commit comments

Comments
 (0)