From 6f5087de7c9d4be3497f680e7cfe8715b5f47fdd 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 readonly Refs: https://github.com/nodejs/node/issues/14619 --- 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);