From f6caeb952605ffa9968ccd88eb330829f980a536 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Thu, 17 Aug 2017 11:52:30 +0800 Subject: [PATCH] os: make EOL configurable and read only MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/14622 Fixes: https://github.com/nodejs/node/issues/14619 Reviewed-By: Vse Mozhet Byt Reviewed-By: Timothy Gu Reviewed-By: Tobias Nießen Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca Reviewed-By: Alexey Orlenko Reviewed-By: Joyee Cheung Reviewed-By: James M Snell Reviewed-By: Rod Vagg Reviewed-By: Refael Ackermann Reviewed-By: Matteo Collina --- lib/os.js | 18 +++++++++++++----- test/parallel/test-os-eol.js | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-os-eol.js diff --git a/lib/os.js b/lib/os.js index 078dba3fcca071..0973e14788b5f7 100644 --- a/lib/os.js +++ b/lib/os.js @@ -140,7 +140,6 @@ function networkInterfaces() { module.exports = exports = { arch, cpus, - EOL: isWindows ? '\r\n' : '\n', endianness, freemem: getFreeMem, homedir: getHomeDirectory, @@ -162,8 +161,17 @@ module.exports = exports = { tmpDir: deprecate(tmpdir, tmpDirDeprecationMsg, 'DEP0022') }; -Object.defineProperty(module.exports, 'constants', { - configurable: false, - enumerable: true, - value: constants +Object.defineProperties(module.exports, { + constants: { + configurable: false, + enumerable: true, + value: constants + }, + + EOL: { + configurable: true, + enumerable: true, + writable: false, + value: isWindows ? '\r\n' : '\n' + } }); diff --git a/test/parallel/test-os-eol.js b/test/parallel/test-os-eol.js new file mode 100644 index 00000000000000..7a7a300717cfe8 --- /dev/null +++ b/test/parallel/test-os-eol.js @@ -0,0 +1,27 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const os = require('os'); + +const eol = common.isWindows ? '\r\n' : '\n'; + +assert.strictEqual(os.EOL, eol); + +common.expectsError(function() { + os.EOL = 123; +}, { + type: TypeError, + message: /^Cannot assign to read only property 'EOL' of object '#'$/ +}); + +const foo = 'foo'; +Object.defineProperties(os, { + EOL: { + configurable: true, + enumerable: true, + writable: false, + value: foo + } +}); +assert.strictEqual(os.EOL, foo);