os.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. module.metadata = {
  6. "stability": "unstable"
  7. };
  8. const { Cc, Ci } = require('chrome');
  9. const system = require('../sdk/system');
  10. const runtime = require('../sdk/system/runtime');
  11. const oscpu = Cc["@mozilla.org/network/protocol;1?name=http"]
  12. .getService(Ci.nsIHttpProtocolHandler).oscpu;
  13. const hostname = Cc["@mozilla.org/network/dns-service;1"]
  14. .getService(Ci.nsIDNSService).myHostName;
  15. const isWindows = system.platform === 'win32';
  16. const endianness = ((new Uint32Array((new Uint8Array([1,2,3,4])).buffer))[0] === 0x04030201) ? 'LE' : 'BE';
  17. /**
  18. * Returns a path to a temp directory
  19. */
  20. exports.tmpdir = () => system.pathFor('TmpD');
  21. /**
  22. * Returns the endianness of the architecture: either 'LE' or 'BE'
  23. */
  24. exports.endianness = () => endianness;
  25. /**
  26. * Returns hostname of the machine
  27. */
  28. exports.hostname = () => hostname;
  29. /**
  30. * Name of the OS type
  31. * Possible values:
  32. * https://developer.mozilla.org/en/OS_TARGET
  33. */
  34. exports.type = () => runtime.OS;
  35. /**
  36. * Name of the OS Platform in lower case string.
  37. * Possible values:
  38. * https://developer.mozilla.org/en/OS_TARGET
  39. */
  40. exports.platform = () => system.platform;
  41. /**
  42. * Type of processor architecture running:
  43. * 'arm', 'ia32', 'x86', 'x64'
  44. */
  45. exports.arch = () => system.architecture;
  46. /**
  47. * Returns the operating system release.
  48. */
  49. exports.release = () => {
  50. let match = oscpu.match(/(\d[\.\d]*)/);
  51. return match && match.length > 1 ? match[1] : oscpu;
  52. };
  53. /**
  54. * Returns EOL character for the OS
  55. */
  56. exports.EOL = isWindows ? '\r\n' : '\n';
  57. /**
  58. * Returns [0, 0, 0], as this is not implemented.
  59. */
  60. exports.loadavg = () => [0, 0, 0];
  61. ['uptime', 'totalmem', 'freemem', 'cpus'].forEach(method => {
  62. exports[method] = () => { throw new Error('os.' + method + ' is not supported.'); };
  63. });