On Monday, 9 September 2013 08:38:31 UTC+10, Adam Ahmed wrote:
>
> Hi all,
>
> I'm working on a website that does all the basic stuff: permissions,
> authentication, CRUD on objects, etc. One thing that has been a bit hard is
> passing around the context of a request - the session information, who the
> user is, etc.
>
> In everyone's favorite language (Java, obviously) you can write code like:
>
>
> impersonationService.doWithPermission(Permission.DELETE_ALL_THE_THINGS, new
> Callable() {
> public void call() {
> rmService.rmRf('/'); // does a permissions check against a
> ThreadLocal context. That context is passed to any new threads started by
> this one.
> }
> });
>
> In NodeJS, we don't have as much control over the thread context afaik.
> I've been manually passing the context information around:
>
>
> rmService.rmRf(requestContext.withPermission(Permission.DELETE_ALL_THE_THINGS),
>
> '/');
>
> This is ok, but can get unwieldy when the call to rmService is multiple
> levels deep in the call stack. It seems tedious to always be passing this
> requestContext around manually.
>
> I was wondering if anyone had a good solution to this, or if my best bet
> is to continue manually passing that info around.
>
> Alternatively, I was wondering if this idea had been brought up before.
> We'd create a concept very much like domains, but for passing contexts.
> Something like:
>
> Domain.prototype.run = function(fn) {
> var oldDomain = Domain.current;
> Domain.current = this; // would need to happen on every turn of
> the event loop, similar to domains
> fn();
> Domain.current = oldDomain;
> };
>
> app.delete('/', function(req, res, next) {
> var d = Domain.create();
> d.context =
> requestContext.withPermission(Permission.DELETE_ALL_THE_THINGS);
> d.run(function() {
> rmService.rmRf('/'); // can access the request context through
> Domain.current.context
> });
> });
>
>
The example might make more sense if it wasn't bogus-ly sync. rmRf makes
async calls before calling a callback.
app.delete('/', function(req, res, next) {
var d = Domain.create();
d.context =
requestContext.withPermission(Permission.DELETE_ALL_THE_THINGS);
d.run(function() {
rmService.rmRf('/', next); // can access the request context
through Domain.current.context, even after fs.unlink, or other async calls.
});
});
The point is that the link to your domain is handled for, even if async
tasks are interleaved between two different requests.
Thoughts?
>
> Adam
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.