Context API (TLS)

17 views
Skip to first unread message

Bruno Jouhier

unread,
Feb 7, 2011, 4:49:30 PM2/7/11
to streamline.js
I have added a context API, which gives something similar to TLS
(thread local storage) in threaded systems.

Alexey Petrushin

unread,
Jan 17, 2014, 7:03:00 AM1/17/14
to stream...@googlegroups.com
Can you please add example how to use it? I guess it can be used for example to put (req, res) in such context and pass it inside of async function automatically?

Bruno Jouhier

unread,
Jan 18, 2014, 8:51:08 AM1/18/14
to stream...@googlegroups.com
The API is described here: https://github.com/Sage/streamlinejs/blob/master/lib/globals.md but it probably need a bit more explanation.

As the doc says, the streams module (or ez-streams) sets up an empty context at the beginning of every request. So you can just use this context:

var globals = require('streamline/lib/globals');
var ez = require('ez-streams');

ez.streams.devices.http.server(function(request, response, _) {
    globals.context.request = request;
    globals.context.response = response;
    setTimeout(_, 0); // yield to the event loop
    f1(_);
}).listen(_, port);

function f1(_) {
    // retrieve from globals.request
    var request = globals.context.request;
    var response = globals.context.response;
}

Warning: the following will *not* work:

// top of the file
var context = require('streamline/lib/globals').context;

You can put whatever you want in the context. If all your code executes in the context of an HTTP request you can put the request/response pair. If your code needs to be more versatile (also work from command line, TCP server, etc), then you should put lower level objects like locale name, login id, security context).

Bruno

Alexey Petrushin

unread,
Jan 19, 2014, 3:14:19 AM1/19/14
to stream...@googlegroups.com
Thanks, I use similar thing with Fiber.current for example, @params taken from such scope here https://github.com/sinizinairina/mono-example/blob/master/controllers/Posts.coffee#L21

Is it possible to use more compact form? Typing `globals.context.variable` looks too long, maybe something shorter would be possible, like `context.variable`?

Bruno Jouhier

unread,
Jan 19, 2014, 7:08:29 AM1/19/14
to stream...@googlegroups.com
You can shorten the path. For example var cx = globals.context; and then cx.variable;

The only gotcha is that you need to retrieve cx inside a function which is called to handle the request. You should not declare and assign it at the top of the module.

BTW, I'm thinking of setting up the initial context with two members (request and response) in ez-streams's http listener, instead of an empty context. This way you'd be able to retrieve the requests headers (which is what you're usually interested in) anywhere down the line with cx.request.headers.

Bruno

Alexey Petrushin

unread,
Jan 19, 2014, 9:37:49 AM1/19/14
to stream...@googlegroups.com
> The only gotcha is that you need to retrieve cx inside a function which is called to handle the request. You should not declare and assign it at the top of the module. 

Exactly, so this won't actually shorten it, you has to type extra symbols every time.

Alexey Petrushin

unread,
Jan 19, 2014, 9:43:04 AM1/19/14
to stream...@googlegroups.com
> The only gotcha is that you need to retrieve cx inside a function which is called to handle the request. You should not declare and assign it at the top of the module. 

Exactly, so this won't actually shorten it, you has to type extra symbols every time.

On Sunday, 19 January 2014 16:08:29 UTC+4, Bruno Jouhier wrote:
Reply all
Reply to author
Forward
0 new messages