summaryrefslogtreecommitdiff
path: root/node_modules/os-shim/lib/os.js
blob: c7a29d527039399d9120a50788132c08359e9517 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var os = require('os')
var osShim

'use strict';

// clone the 'os' module object to avoid mutations and unexpected behavior
module.exports = osShim = clone(os)

//
// apply the missing API
//

if (!os.tmpdir) {
  osShim.tmpdir = tmpdir
}

if (!os.platform) {
  osShim.platform = platform
}

if (!os.arch) {
  osShim.arch = arch
}

if (!os.endianness) {
  osShim.endianness = endianness
}

if (!os.EOL) {
  Object.defineProperty(osShim, 'EOL', {
    get: function () {
      return process.platform === 'win32' ? '\n\r' : '\n'
    }
  })
}

function tmpdir() {
  var isWindows = process.platform === 'win32'
  var env = process.env

  if (isWindows) {
    return env.TEMP ||
           env.TMP ||
           (env.SystemRoot || env.windir) + '\\temp';
  } else {
    return env.TMPDIR ||
           env.TMP ||
           env.TEMP ||
           '/tmp';
  }
}

function platform() {
  return process.platform
}

function arch() {
  return process.arch
}

function endianness() {
  var isEndianness = ((new Uint32Array((new Uint8Array([1,2,3,4])).buffer))[0] === 0x04030201)
  return isEndianness ? 'LE' : 'BE'
}

function clone(object) {
  var prop, cloneObj = {}
  for (prop in object) {
    if (object.hasOwnProperty(prop)) {
      cloneObj[prop] = object[prop]
    }
  }
  return cloneObj
}