1.0.22 • Published 4 years ago

simple-continuation-local-storage v1.0.22

Weekly downloads
22
License
ISC
Repository
-
Last release
4 years ago

Simple Continuation Local Storage

When coding in Node it can involve a lot of parameter passing to enable sub functions to keep track of all of the state being set elsewhere. It can be very difficult to do this if you are using 3rd party libraries or practicing things like Inversion of Control with events.

What you want is:

  • Something a bit like a global variable
  • The equivalent of Thread Local Storage in many systems but that works

We call the resumption of a routine after an async call or promise chain a "Continuation" and hence continuation local storage allows you to set some variables that will be retained through the lifetime of a request dramatically simplifying your code.

There are a few things out there that do this, but they all require too much boiler plate for me.

With simple-continuation-local-storage you just access anything you like off a simple variable and it will be properly set up for every different request being served by your server.

It does this by using a Proxy, available on Node > 6.0.0

Installation

npm i --save simple-continuation-local-storage

Usage

const cls = require('simple-continuation-local-storage')
app.get('/request', async (request, response)=>{
    try {
        cls.$init()
        cls.request = request;
        cls.response = response;
        await getUserSecurity();
        await runSafeFunction();
        response.status(200).end()
    } catch(e) {
        console.error(e)
    }
 })

async function runSafeFunction() {
    if(cls.authenticated && cls.user && cls.user.permissions.includes('doit')) {
       cls.response.write("Secret stuff")
    }
}

async function getUserSecurity() {
    if(cls.request.url.searchParams.token==='magic') {
        cls.authenticated = true
        cls.user = {
            email: "mike@talbot.com",
            id: 1,
            permissions: ['doit']
        }
    }
    cls.response.status(401).send("Unauthorised")
    throw new Error("Unauthorised")
}

Demo

Available on Code Sandbox

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago