From d478e52fe0bc396c8e65baf0861553a0d3de99db Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 6 May 2018 23:10:23 +0200 Subject: [PATCH] os: lazy loaded PR-URL: https://github.com/nodejs/node/pull/20567 Reviewed-By: Gus Caplan Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Jeremiah Senkpiel --- lib/assert.js | 2 +- lib/internal/constants.js | 4 ++++ lib/internal/tty.js | 9 ++++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 45ddbe5f817..91205af09ea 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -34,7 +34,7 @@ const { codes: { const { AssertionError, errorCache } = require('internal/assert'); const { openSync, closeSync, readSync } = require('fs'); const { inspect, types: { isPromise, isRegExp } } = require('util'); -const { EOL } = require('os'); +const { EOL } = require('internal/constants'); const { NativeModule } = require('internal/bootstrap/loaders'); // Escape control characters but not \n and \t to keep the line breaks and diff --git a/lib/internal/constants.js b/lib/internal/constants.js index 5c884ae6bc4..dfa30bea306 100644 --- a/lib/internal/constants.js +++ b/lib/internal/constants.js @@ -1,5 +1,7 @@ 'use strict'; +const isWindows = process.platform === 'win32'; + module.exports = { // Alphabet chars. CHAR_UPPERCASE_A: 65, /* A */ @@ -45,4 +47,6 @@ module.exports = { // Digits CHAR_0: 48, /* 0 */ CHAR_9: 57, /* 9 */ + + EOL: isWindows ? '\r\n' : '\n' }; diff --git a/lib/internal/tty.js b/lib/internal/tty.js index c4edab24fb8..1e60909f66a 100644 --- a/lib/internal/tty.js +++ b/lib/internal/tty.js @@ -22,9 +22,7 @@ 'use strict'; -const { release } = require('os'); - -const OSRelease = release().split('.'); +let OSRelease; const COLORS_2 = 1; const COLORS_16 = 4; @@ -75,6 +73,11 @@ function getColorDepth(env = process.env) { } if (process.platform === 'win32') { + // Lazy load for startup performance. + if (OSRelease === undefined) { + const { release } = require('os'); + OSRelease = release().split('.'); + } // Windows 10 build 10586 is the first Windows release that supports 256 // colors. Windows 10 build 14931 is the first release that supports // 16m/TrueColor.