Skip to content

Commit 42e9c45

Browse files
committed
lib,src: split up big builtin scripts
Reduces the body of parsed source code from 210 kb to 80 kb for a null program (`node -e 0`) and from 255 kb to 190 kb for a one-liner that prints some output to the console. It doesn't seem to have much impact on the size of the generated code (on my system I see an 8% reduction with --nolazy and 3% without) but hopefully it speeds up startup on slow systems because there is now less source code to parse.
1 parent ba02bd0 commit 42e9c45

26 files changed

+401
-327
lines changed

lib/_stream_duplex.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
module.exports = Duplex;
99

10-
const util = require('util');
1110
const Readable = require('_stream_readable');
1211
const Writable = require('_stream_writable');
12+
const inherits = require('internal/inherits');
1313

14-
util.inherits(Duplex, Readable);
14+
inherits(Duplex, Readable);
1515

1616
var keys = Object.keys(Writable.prototype);
1717
for (var v = 0; v < keys.length; v++) {

lib/_stream_passthrough.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
module.exports = PassThrough;
88

99
const Transform = require('_stream_transform');
10-
const util = require('util');
11-
util.inherits(PassThrough, Transform);
10+
const inherits = require('internal/inherits');
11+
inherits(PassThrough, Transform);
1212

1313
function PassThrough(options) {
1414
if (!(this instanceof PassThrough))

lib/_stream_readable.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
module.exports = Readable;
44
Readable.ReadableState = ReadableState;
55

6-
const EE = require('events').EventEmitter;
7-
const Stream = require('stream');
86
const Buffer = require('buffer').Buffer;
9-
const util = require('util');
10-
const debug = util.debuglog('stream');
7+
const EE = require('events').EventEmitter;
8+
const Stream = require('internal/stream');
9+
const debug = require('internal/debuglog')('stream');
10+
const inherits = require('internal/inherits');
11+
12+
var Duplex; // Break recursive dependency with _stream_duplex.js.
1113
var StringDecoder;
1214

13-
util.inherits(Readable, Stream);
15+
inherits(Readable, Stream);
1416

1517
function ReadableState(options, stream) {
1618
options = options || {};
@@ -19,7 +21,10 @@ function ReadableState(options, stream) {
1921
// make all the buffer merging and length checks go away
2022
this.objectMode = !!options.objectMode;
2123

22-
if (stream instanceof Stream.Duplex)
24+
if (Duplex === undefined)
25+
Duplex = require('_stream_duplex');
26+
27+
if (stream instanceof Duplex)
2328
this.objectMode = this.objectMode || !!options.readableObjectMode;
2429

2530
// the point at which it stops calling _read() to fill the buffer

lib/_stream_transform.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
module.exports = Transform;
4646

4747
const Duplex = require('_stream_duplex');
48-
const util = require('util');
49-
util.inherits(Transform, Duplex);
48+
const inherits = require('internal/inherits');
49+
inherits(Transform, Duplex);
5050

5151

5252
function TransformState(stream) {

lib/_stream_wrap.js

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

33
const assert = require('assert');
4-
const util = require('util');
54
const Socket = require('net').Socket;
65
const JSStream = process.binding('js_stream').JSStream;
76
const uv = process.binding('uv');
8-
const debug = util.debuglog('stream_wrap');
7+
const debug = require('internal/debuglog')('stream_wrap');
8+
const inherits = require('internal/inherits');
99

1010
function StreamWrap(stream) {
1111
const handle = new JSStream();
@@ -57,7 +57,7 @@ function StreamWrap(stream) {
5757
handle: handle
5858
});
5959
}
60-
util.inherits(StreamWrap, Socket);
60+
inherits(StreamWrap, Socket);
6161
module.exports = StreamWrap;
6262

6363
// require('_stream_wrap').StreamWrap

lib/_stream_writable.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
module.exports = Writable;
88
Writable.WritableState = WritableState;
99

10-
const util = require('util');
11-
const internalUtil = require('internal/util');
12-
const Stream = require('stream');
1310
const Buffer = require('buffer').Buffer;
11+
const Stream = require('internal/stream');
12+
const inherits = require('internal/inherits');
13+
const internalUtil = require('internal/util');
14+
15+
inherits(Writable, Stream);
1416

15-
util.inherits(Writable, Stream);
17+
var Duplex; // Break recursive dependency with _stream_duplex.js.
1618

1719
function nop() {}
1820

@@ -30,7 +32,10 @@ function WritableState(options, stream) {
3032
// contains buffers or objects.
3133
this.objectMode = !!options.objectMode;
3234

33-
if (stream instanceof Stream.Duplex)
35+
if (Duplex === undefined)
36+
Duplex = require('_stream_duplex');
37+
38+
if (stream instanceof Duplex)
3439
this.objectMode = this.objectMode || !!options.writableObjectMode;
3540

3641
// the point at which write() starts returning false
@@ -130,8 +135,13 @@ Object.defineProperty(WritableState.prototype, 'buffer', {
130135
function Writable(options) {
131136
// Writable ctor is applied to Duplexes, though they're not
132137
// instanceof Writable, they're instanceof Readable.
133-
if (!(this instanceof Writable) && !(this instanceof Stream.Duplex))
134-
return new Writable(options);
138+
if (!(this instanceof Writable)) {
139+
if (Duplex === undefined)
140+
Duplex = require('_stream_duplex');
141+
142+
if (!(this instanceof Duplex))
143+
return new Writable(options);
144+
}
135145

136146
this._writableState = new WritableState(options, this);
137147

lib/console.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
const util = require('util');
3+
const format = require('internal/format');
4+
45

56
function Console(stdout, stderr) {
67
if (!(this instanceof Console)) {
@@ -33,23 +34,30 @@ function Console(stdout, stderr) {
3334
}
3435

3536
Console.prototype.log = function() {
36-
this._stdout.write(util.format.apply(this, arguments) + '\n');
37+
this._stdout.write(format.apply(this, arguments) + '\n');
3738
};
3839

3940

4041
Console.prototype.info = Console.prototype.log;
4142

4243

4344
Console.prototype.warn = function() {
44-
this._stderr.write(util.format.apply(this, arguments) + '\n');
45+
this._stderr.write(format.apply(this, arguments) + '\n');
4546
};
4647

4748

4849
Console.prototype.error = Console.prototype.warn;
4950

5051

52+
var extend;
53+
var inspect;
54+
5155
Console.prototype.dir = function(object, options) {
52-
this._stdout.write(util.inspect(object, util._extend({
56+
if (extend === undefined) {
57+
extend = require('internal/extend');
58+
inspect = require('util').inspect;
59+
}
60+
this._stdout.write(inspect(object, extend({
5361
customInspect: false
5462
}, options)) + '\n');
5563
};
@@ -75,7 +83,7 @@ Console.prototype.trace = function trace() {
7583
// exposed.
7684
var err = new Error();
7785
err.name = 'Trace';
78-
err.message = util.format.apply(this, arguments);
86+
err.message = format.apply(this, arguments);
7987
Error.captureStackTrace(err, trace);
8088
this.error(err.stack);
8189
};
@@ -84,7 +92,7 @@ Console.prototype.trace = function trace() {
8492
Console.prototype.assert = function(expression) {
8593
if (!expression) {
8694
var arr = Array.prototype.slice.call(arguments, 1);
87-
require('assert').ok(false, util.format.apply(this, arr));
95+
require('assert').ok(false, format.apply(this, arr));
8896
}
8997
};
9098

lib/dgram.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
const assert = require('assert');
44
const Buffer = require('buffer').Buffer;
5-
const util = require('util');
65
const events = require('events');
76
const constants = require('constants');
87

8+
const errnoException = require('internal/errno_exception');
9+
const exceptionWithHostPort = require('internal/exception_with_host_port');
10+
const inherits = require('internal/inherits');
11+
912
const UDP = process.binding('udp_wrap').UDP;
1013
const SendWrap = process.binding('udp_wrap').SendWrap;
1114

@@ -17,9 +20,6 @@ const BIND_STATE_BOUND = 2;
1720
var cluster = null;
1821
var dns = null;
1922

20-
const errnoException = util._errnoException;
21-
const exceptionWithHostPort = util._exceptionWithHostPort;
22-
2323
function lookup(address, family, callback) {
2424
if (!dns)
2525
dns = require('dns');
@@ -101,7 +101,7 @@ function Socket(type, listener) {
101101
if (typeof listener === 'function')
102102
this.on('message', listener);
103103
}
104-
util.inherits(Socket, events.EventEmitter);
104+
inherits(Socket, events.EventEmitter);
105105
exports.Socket = Socket;
106106

107107

lib/fs.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
'use strict';
55

66
const SlowBuffer = require('buffer').SlowBuffer;
7-
const util = require('util');
87
const pathModule = require('path');
98

109
const binding = process.binding('fs');
@@ -34,7 +33,9 @@ const O_WRONLY = constants.O_WRONLY || 0;
3433
const isWindows = process.platform === 'win32';
3534

3635
const DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
37-
const errnoException = util._errnoException;
36+
const errnoException = require('internal/errno_exception');
37+
const extend = require('internal/extend');
38+
const inherits = require('internal/inherits');
3839

3940
function throwOptionsError(options) {
4041
throw new TypeError('Expected options to be either an object or a string, ' +
@@ -1048,7 +1049,7 @@ function toUnixTimestamp(time) {
10481049
if (typeof time === 'number') {
10491050
return time;
10501051
}
1051-
if (util.isDate(time)) {
1052+
if (Object.prototype.toString.call(time) === '[object Date]') {
10521053
// convert to 123.456 UNIX timestamp
10531054
return time.getTime() / 1000;
10541055
}
@@ -1185,7 +1186,7 @@ fs.appendFile = function(path, data, options, callback_) {
11851186
}
11861187

11871188
if (!options.flag)
1188-
options = util._extend({ flag: 'a' }, options);
1189+
options = extend({ flag: 'a' }, options);
11891190
fs.writeFile(path, data, options, callback);
11901191
};
11911192

@@ -1198,7 +1199,7 @@ fs.appendFileSync = function(path, data, options) {
11981199
throwOptionsError(options);
11991200
}
12001201
if (!options.flag)
1201-
options = util._extend({ flag: 'a' }, options);
1202+
options = extend({ flag: 'a' }, options);
12021203

12031204
fs.writeFileSync(path, data, options);
12041205
};
@@ -1219,7 +1220,7 @@ function FSWatcher() {
12191220
}
12201221
};
12211222
}
1222-
util.inherits(FSWatcher, EventEmitter);
1223+
inherits(FSWatcher, EventEmitter);
12231224

12241225
FSWatcher.prototype.start = function(filename, persistent, recursive) {
12251226
nullCheck(filename);
@@ -1289,7 +1290,7 @@ function StatWatcher() {
12891290
self.emit('stop');
12901291
};
12911292
}
1292-
util.inherits(StatWatcher, EventEmitter);
1293+
inherits(StatWatcher, EventEmitter);
12931294

12941295

12951296
StatWatcher.prototype.start = function(filename, persistent, interval) {
@@ -1319,7 +1320,7 @@ fs.watchFile = function(filename, options, listener) {
13191320
};
13201321

13211322
if (options !== null && typeof options === 'object') {
1322-
options = util._extend(defaults, options);
1323+
options = extend(defaults, options);
13231324
} else {
13241325
listener = options;
13251326
options = defaults;
@@ -1607,7 +1608,7 @@ fs.createReadStream = function(path, options) {
16071608
return new ReadStream(path, options);
16081609
};
16091610

1610-
util.inherits(ReadStream, Readable);
1611+
inherits(ReadStream, Readable);
16111612
fs.ReadStream = ReadStream;
16121613

16131614
function ReadStream(path, options) {
@@ -1779,7 +1780,7 @@ fs.createWriteStream = function(path, options) {
17791780
return new WriteStream(path, options);
17801781
};
17811782

1782-
util.inherits(WriteStream, Writable);
1783+
inherits(WriteStream, Writable);
17831784
fs.WriteStream = WriteStream;
17841785
function WriteStream(path, options) {
17851786
if (!(this instanceof WriteStream))
@@ -1887,7 +1888,7 @@ function SyncWriteStream(fd, options) {
18871888
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
18881889
}
18891890

1890-
util.inherits(SyncWriteStream, Stream);
1891+
inherits(SyncWriteStream, Stream);
18911892

18921893

18931894
// Export

lib/internal/assert.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
module.exports = function assert(expression, message) {
4+
if (!expression)
5+
throw new Error(`Internal assertion failed: ${message || 'no message'}`);
6+
};

lib/internal/debuglog.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const debugs = {};
4+
var debugEnviron;
5+
var format;
6+
7+
module.exports = function(set) {
8+
if (debugEnviron === undefined)
9+
debugEnviron = process.env.NODE_DEBUG || '';
10+
set = set.toUpperCase();
11+
if (!debugs[set]) {
12+
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
13+
var pid = process.pid;
14+
debugs[set] = function() {
15+
if (format === undefined)
16+
format = require('internal/format');
17+
const msg = format.apply(exports, arguments);
18+
console.error('%s %d: %s', set, pid, msg);
19+
};
20+
} else {
21+
debugs[set] = function() {};
22+
}
23+
}
24+
return debugs[set];
25+
};

lib/internal/errno_exception.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
var uv;
4+
5+
module.exports = function errnoException(err, syscall, original) {
6+
if (uv === undefined)
7+
uv = process.binding('uv');
8+
var errname = uv.errname(err);
9+
var message = syscall + ' ' + errname;
10+
if (original)
11+
message += ' ' + original;
12+
var e = new Error(message);
13+
e.code = errname;
14+
e.errno = errname;
15+
e.syscall = syscall;
16+
return e;
17+
};

0 commit comments

Comments
 (0)