tlrobinson / narwhal

[DEPRECATED] A JavaScript standard library, package manager, and more.
http://narwhaljs.org/
372 stars 16 forks source link

Narwhal

A general purpose JavaScript platform

Narwhal is a cross-platform, multi-interpreter, general purpose JavaScript platform. It aims to provide a solid foundation for building JavaScript applications, primarily outside the web browser. Narwhal includes a package manager, module system, and standard library for multiple JavaScript interpreters. Currently Narwhal's Rhino support is the most complete, but other engines are available too.

Narwhal's standard library conforms to the CommonJS standard. It is designed to work with multiple JavaScript interpreters, and to be easy to add support for new interpreters. Wherever possible, it is implemented in pure JavaScript to maximize reuse of code among engines.

Combined with Jack, a Rack-like JSGI compatible library, Narwhal provides a platform for creating server-side JavaScript web applications and frameworks such as Nitro.

Homepage:

Source & Download:

Mailing list:

IRC:

Documentation

Note: If you are viewing this on GitHub, the links below will not work. Find the pages listed below in the docs/ directory of this repository.

Quick Start

Download Narwhal.

Put Narwhal on your PATH environment variable.

Run narwhal or js (they are equivalent).

Look at the options for Narwhal.

And for Tusk, the package manager and virtual environment tool.

My First Web Server

Create a project "hello-web".

tusk init hello-web
cd hello-web

Enter your project as a "virtual environment" using activate or sea so that its libraries, binaries, and packages get automatically installed when you run Narwhal.

source bin/activate

or

bin/sea

Install some packages you will need, like Jack, the JSGI standard library for interoperable web services.

tusk install jack

Tusk gets downloaded and installed at "hello-web/packages/jack".

Create your jackconfig.js.

exports.app = function(env) {
    var text = "Hello, Web!";
    return {
        status : 200,
        headers : {
            "Content-Type" : "text/plain",
            "Content-Length" : String(text.length)
        },
        body : [text]
    };
};

Run it!

jackup

jackup looks for a file called jackconfig.js in the current directory, or you can specify a path to a Jack application.

Open http://localhost:8080/ in your web browser.

Module System Basics

Narwhal "scripts" are CommonJS compatible modules, much like Python or Ruby modules. You do not have to use module pattern boilerplate; every module has its own local scope. You can get the exports object of another module by calling require.

var FS = require("file");
FS.isFile("foo.txt");

Module identifiers for require come in three flavors: "top-level", "relative", and "absolute". In the above case, file is a "top-level" identifier, so it will load any module called file.js in the lib directory of whichever package comes first in the load path. Relative identifiers have . or .. as their first term, and terms are delimited with /. So, in the foo/bar module, require('./baz') will load foo/baz. Absolute module identifiers should not be used directly, but are produced when you execute a program module outside the module path. The module is identified by its fully-qualified path, starting with /.

You can export an object by assigning it to exports.

exports.foo = function () {
    return "Hello";
};

In a module, you also get a module object that has module.id and module.path properties so you can inspect your own top-level module identifier, and the path of your own module file. You also get a require.main property that tells you the top-level module identifier of the module that started the program.

if (require.main == module)
    main();

var settings = require(require.main);

var FS = require("file");
var path = FS.path(module.path);
var indexHtml = path.resolve("./template/index.html").read();

Beyond the CommonJS specification, you also get the print function and the system module object for free. The print function accepts variadic arguments and writes a single line containing the arguments delimited by spaces to standard output and flushes. The system module can be explicitly required with require("system") as is encouraged since it is necessary for CommonJS compliance. Do not use print or system in standard libraries.

Summary of Included Modules

Contributors

License

Copyright (c) 2009, 280 North Inc. <280north.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.