nwglobal

Node.js globals for node-webkit's HTML5

npm install nwglobal
4 downloads in the last week
12 downloads in the last month

This Node.js module (nwglobal) provides a workaround for node-webkit's issues #702, #716, #832.

These issues happen in node-webkit because, as the modules run in Node context, the constructors of their global objects (such as Date or ArrayBuffer or even Array) differ from WebKit's.

(An example below demonstrates that you may pass an array from some <script>…</script> to the async module that you have previously required, but that module cannot recognize such an array.)

To prevent the trouble, nwglobal exports Node's constructors. You may use them instead of the constructors available in WebKit's context, and then you may pass the resulting object instances to any Node code.

Installation

  • Latest packaged version: npm install nwglobal

  • Latest githubbed version: npm install https://github.com/Mithgol/nwglobal/tarball/master

You may visit https://github.com/Mithgol/nwglobal#readme occasionally to read the latest README because the package's version is not planned to grow after changes when they happen in README only. (However, npm publish --force may happen eventually.)

Example

Classic async waterfall example:

require('async').waterfall([
    function(callback){
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
   // result now equals 'done'
   console.log(result);
});

does not report 'done' in node-webkit (issue #832), but can be fixed with the following changes:

require('async').waterfall( require('nwglobal').Array(
    function(callback){
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
), function (err, result) {
   // result now equals 'done'
   console.log(result);
});

Implementation details

The following Node.js globals are available as the exported fields of require('nwglobal'):

  • Standard object types: Array, Boolean, Date, Function, Number, Object, RegExp, String.

  • Typed array types: ArrayBuffer, DataView, Float32Array, Float64Array, Int16Array, Int32Array, Int8Array, Uint16Array, Uint32Array, Uint8Array.

  • Error types: Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError.

  • Special value types: Infinity, NaN, undefined, null.

However, the latter four (Infinity, NaN, undefined, null) are actually superglobal (i.e. they are the same in Node's and WebKit's contexts). You may use nwglobal to check it with the following four statements in node-webkit's “Developer Tools” console:

  • null === require('nwglobal').null

  • typeof require('nwglobal').undefined === 'undefined'

  • Infinity === require('nwglobal').Infinity

  • isNaN( require('nwglobal').NaN )

These statements are true. (Meaning that you won't need these four exported values IRL.)

Limits

It is not (yet?) possible to replace constructors of elementary {} and [] values, see the discussion for details and a corresponding example.

License

MIT License, see the LICENSE file.

npm loves you