Skip to content
/ local Public

An Ajax library that can target local JS code and web workers.

Notifications You must be signed in to change notification settings

pfrazee/local

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Local.js 0.6.2

[Repository | Issues | Documentation]

Overview

Local.js is an open-source security and IPC framework for socially-shareable Web plugins. It provides an Ajax interface for communicating interchangeably with javascript functions, parallel threads, and network peers. It also includes tools for managing and securing Web Workers, for emitting and subscribing to Server-Sent Events, and for exchanging and querying against links.

Examples

Run servers in the document:

local.addServer('foobar', function(req, res) {
    // Handles incoming requests from the application
    res.writeHead(200, 'ok', { 'Content-Type': 'text/plain' });
    res.end('Hello, application!');
});
local.dispatch({ method: 'GET', url: 'httpl://foobar' }).then(handle2or3xx, handle4or5xx);

Run servers in Web Workers:

local.spawnWorkerServer('http://myhost.com/myworker.js', function(req, res) {
    // Handles incoming requests from the worker
    res.writeHead(200, 'ok', { 'Content-Type': 'text/plain' });
    res.end('Hello, worker!');
});
local.dispatch({ method: 'GET', url: 'httpl://myworker.js' }).then(...);

Auto-spawn a temporary Worker to handle a request. After responding, the temp-worker is destroyed:

local.dispatch({ method: 'GET', url: 'httpl://myhost.com(/myworker.js)/' }).then(...);

Using bodyless request sugars:

local.HEAD('httpl://myhost.com(/myworker.js)/');
local.GET('httpl://myhost.com(/myworker.js)/');
local.GET({ url: 'httpl://myhost.com(/myworker.js)/', Accept: 'application/json' });
local.DELETE('httpl://myhost.com(/myworker.js)/');

// For reference, the non-sugar equivalent:
local.dispatch({ method: 'GET', url: 'httpl://myhost.com(/myworker.js)/', Accept: 'application/json' });

Using bodyfull request sugars:

local.POST({ foo: 'bar' }, 'httpl://myhost.com(/myworker.js)/');
local.PUT({ foo: 'bar' }, 'httpl://myhost.com(/myworker.js)/');
local.PATCH({ foo: 'bar' }, 'httpl://myhost.com(/myworker.js)/');
local.POST({ foo: 'bar' }, {
    url: 'httpl://myhost.com(/myworker.js)/',
    Accept: 'application/json',
    Content_Type: 'application/www-x-form-urlencoded'
});

// For reference, the non-sugar equivalent:
local.dispatch({
    method: 'POST',
    url: 'httpl://myhost.com(/myworker.js)/',
    Accept: 'application/json',
    Content_Type: 'application/www-x-form-urlencoded',
    body: { foo: 'bar' }
});

Note that headers may have their dashes changed to underscores. However, if in the request object, headers must be capitalized. To avoid this requirement, put them in the headers sub-object:

local.dispatch({
    method: 'POST',
    url: 'httpl://myhost.com(/myworker.js)/',
    headers: {
        accept: 'application/json',
        'content-type': 'application/www-x-form-urlencoded'
    },
    body: { foo: 'bar' }
});

If you need to stream the request, create a local.Request object, then send it to local.dispatch():

var req = new local.Request({ method: 'POST', url: 'http://foo.com', Content_Type: 'text/plain' });
local.dispatch(req).then(...);
for (var i = 0; i < 10; i++) {
    req.send(''+i+'\n');
}
req.end();

A dispatch call returns a promise which resolves to the response when the response-stream finishes. If the status is in the 200-300 range, it is fulfilled; otherwise, the promise is rejected with the response as the error-value.

If you need to stream the response, use the stream: true attribute in the request. The promise will be fulfilled when the headers are sent, and you can then attach to the 'data' and 'end' events:

local.GET({ url: 'http://foo.com', stream: true })
    .then(function(res) {
        console.log(res.status); // => 200
        res.on('data', function(chunk) {
            // ...
        });
        res.on('end', function() {
            // ...
        });
    });
}

Local.js includes a registry of content-type serializers and parsers which are auto-invoked on the 'end' events of requests and responses. By default, it ships with json and application/x-www-form-urlencoded, but you can register more with local.contentTypes.

If successful, the body attribute will include the parsed object. If parsing fails, or the content-type is not available, the body attribute will be a string. Note that server functions are fired when headers are received, and so must always wait for the 'end' event:

local.addServer('foobar', function(req, res) {
    console.log(req.header('Content-Type')); // => 'application/json'
    req.on('end', function() {
        res.writeHead(200, 'ok', { 'Content-Type': 'application/x-www-form-urlencoded' });
        res.end('foo='+req.body.foo);
    });
});
local.POST({ foo: 'bar' }, 'httpl://foobar') // will default content-type to json
    .then(function(res) {
        console.log(res.header('Content-Type')); // => 'application/x-www-form-urlencoded'
        console.log(res.body); // => { foo: 'bar' }
    });

Local.js also includes a registry of header serializers and parsers which are auto-invoked on dispatch - local.httpHeaders. By default, it ships with Link, Accept, and Via. In responses, the parsed headers are placed in the response.parsedHeaders object to avoid confusion:

local.HEAD('http://foo.com').then(function(res) {
    console.log(res.headers.link); // => '<http://foo.com>; rel="self service"; title="Foo!"'
    console.log(res.header('Link')); // => '<http://foo.com>; rel="self service"; title="Foo!"'
    console.log(res.parsedHeaders.link); // => [{ href: 'http://foo.com', rel: 'self service', title: 'Foo!' }]
});

To query the links, use local.queryLinks:

local.HEAD('http://foo.com').then(function(res) {
    var links = local.queryLinks(res.headers.link, { rel: 'self' });
    console.log(links); // => [{ href: 'http://foo.com', rel: 'service', title: 'Foo!' }]
    var links = local.queryLinks(res, { rel: 'self' })
    console.log(links); // => [{ href: 'http://foo.com', rel: 'service', title: 'Foo!' }]
});

To decide which content-type to respond with, use local.preferredType:

local.addServer('foobar', function(req, res) {
    var type = local.preferredType(req, ['text/html', 'text/plain', 'application/json']);
    if (!type) { return res.writeHead(406, 'Not Acceptable').end(); }
    if (type == 'text/html') { /* ... */ }
    // ...
});
local.GET({ url: 'httpl://foobar', Accept: 'text/plain' }); // will get text/plain
local.GET({ url: 'httpl://foobar', Accept: 'application/json, text/*' }); // will get application/json
local.GET({ url: 'httpl://foobar', Accept: '*/*' }); // will get text/html

Programmatically navigate with a headless user-agent:

// Fetch the profile of bob@foo.com
local.agent('http://foo.com')
    .follow({ rel: 'gwr.io/users' }) // documented at http://gwr.io/users
    .follow({ rel: 'item', id: 'bob' })
    .GET({ Accept: 'application/json' })

Further topics:


How it works

The core of Local.js is a message router which adds a new scheme, httpl://, for targeting requests at functions within the application. These in-app server functions work similarly to node.js servers, and support streaming for requests and responses. Special types of server functions, the "bridge" servers, serialize the streams into JSON and transport them over channels to other namespaces.

Further reading:


Getting Started

Download local.js or local.min.js from the repository. Then read the documentation at HTTPLocal.com. For an introduction to writing Local.js apps, read Intro: TodoSOA.

Getting Help

Contact @pfrazee or join #httpl on freenode.

Feedback & Bug Reporting

Send specific issues and suggestions to the GitHub issue tracker. Suggestions to improve the documentation can be added to the ongoing Documentation Improvements issue.


Misc

Special thanks and credits

Thank you to the following third-party library authors:

Special thanks to Goodybag.com for their support during the development of this project. If you're in Austin and need food delivered to the office, be sure to check out their website.

License

The MIT License (MIT) Copyright (c) 2014 Paul Frazee

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 OR COPYRIGHT HOLDERS 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.

About

An Ajax library that can target local JS code and web workers.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages