Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Bug 935114 - Node 'os'
Browse files Browse the repository at this point in the history
  • Loading branch information
jsantell committed Jan 9, 2014
1 parent 6a8d60d commit 576c8b2
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
76 changes: 76 additions & 0 deletions lib/node/os.js
@@ -0,0 +1,76 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

'use strict';

module.metadata = {
"stability": "unstable"
};

const { Cc, Ci } = require('chrome');
const system = require('../sdk/system');
const runtime = require('../sdk/system/runtime');
const oscpu = Cc["@mozilla.org/network/protocol;1?name=http"]
.getService(Ci.nsIHttpProtocolHandler).oscpu;
const hostname = Cc["@mozilla.org/network/dns-service;1"]
.getService(Ci.nsIDNSService).myHostName;
const isWindows = system.platform === 'win32';
const endianness = ((new Uint32Array((new Uint8Array([1,2,3,4])).buffer))[0] === 0x04030201) ? 'LE' : 'BE';

/**
* Returns a path to a temp directory
*/
exports.tmpdir = () => system.pathFor('TmpD');

/**
* Returns the endianness of the architecture: either 'LE' or 'BE'
*/
exports.endianness = () => endianness;

/**
* Returns hostname of the machine
*/
exports.hostname = () => hostname;

/**
* Name of the OS type
* Possible values:
* https://developer.mozilla.org/en/OS_TARGET
*/
exports.type = () => runtime.OS;

/**
* Name of the OS Platform in lower case string.
* Possible values:
* https://developer.mozilla.org/en/OS_TARGET
*/
exports.platform = () => system.platform;

/**
* Type of processor architecture running:
* 'arm', 'ia32', 'x86', 'x64'
*/
exports.arch = () => system.architecture;

/**
* Returns the operating system release.
*/
exports.release = () => {
let match = oscpu.match(/(\d[\.\d]*)/);
return match && match.length > 1 ? match[1] : oscpu;
};

/**
* Returns EOL character for the OS
*/
exports.EOL = isWindows ? '\r\n' : '\n';

/**
* Returns [0, 0, 0], as this is not implemented.
*/
exports.loadavg = () => [0, 0, 0];

['uptime', 'totalmem', 'freemem', 'cpus'].forEach(method => {
exports[method] = () => { throw new Error('os.' + method + ' is not supported.'); };
});
32 changes: 32 additions & 0 deletions test/test-node-os.js
@@ -0,0 +1,32 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

let os = require("node/os");
let system = require("sdk/system");

exports["test os"] = function (assert) {
assert.equal(os.tmpdir(), system.pathFor("TmpD"), "os.tmpdir() matches temp dir");
assert.ok(os.endianness() === "BE" || os.endianness() === "LE", "os.endianness is BE or LE");

assert.ok(~["arm", "ia32", "x86", "x64"].indexOf(os.arch()), "os.arch() returns a proper architecture");
assert.ok(os.type().length > 0, "os.type() returns a value");
assert.equal(typeof os.type(), "string", "os.type() returns a string");
assert.ok(os.platform().length > 0, "os.platform() returns a value");
assert.equal(typeof os.platform(), "string", "os.platform() returns a string");

assert.ok(os.release().length > 0, "os.release() returns a value");
assert.equal(typeof os.release(), "string", "os.release() returns a string");
assert.ok(os.hostname().length > 0, "os.hostname() returns a value");
assert.equal(typeof os.hostname(), "string", "os.hostname() returns a string");
assert.ok(os.EOL === "\n" || os.EOL === "\r\n", "os.EOL returns a correct EOL char");

assert.deepEqual(os.loadavg(), [0, 0, 0], "os.loadavg() returns an array of 0s");

["uptime", "totalmem", "freemem", "cpus"].forEach(method => {
assert.throws(() => os[method](), "os." + method + " throws");
});
};

require("test").run(exports);

0 comments on commit 576c8b2

Please sign in to comment.