Socket
Socket
Sign inDemoInstall

async-local-storage

Package Overview
Dependencies
3
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    async-local-storage

Get the value like thread-local storage in threaded programming


Version published
Weekly downloads
4.3K
decreased by-9.16%
Maintainers
1
Install size
85.4 kB
Created
Weekly downloads
 

Changelog

Source

1.1.1

  • Fix the context is lost in process next tick

Readme

Source

async-local-storage

Build Status Coverage Status npm Github Releases

I want something like thread-local storage in threaded programming and async_hooks is usable in node.js 8.0, so there is an easy way to use thread-local.

API

const als = require('async-local-storage');
als.enable();
setTimeout(() => {
  const id = randomBytes(8);
  als.set('id', id);
  delay().then(() => {
    assert.equal(als.get('id'), id);
    return readfilePromise(__filename);
  }).then(() => {
    assert.equal(als.get('id'), id);
    return superagent.get('http://www.baidu.com/');
  }).then(() => {
    assert.equal(als.get('id'), id);
  });
}, 100);

enable

enable the async hooks

const als = require('async-local-storage');
als.enable();

disable

disable the async hooks

const als = require('async-local-storage');
als.enable();
setTimeout(() => {
  als.disable();
}, 100);

size

get the size of storage

const als = require('async-local-storage');
als.enable();
setTimeout(() => {
  console.info(als.size());
}, 100);

set

set the value by key for the current id

  • key the key
  • value the value
const assert = require('assert');
const als = require('async-local-storage');
const crypto = require('crypto');
const randomBytes = length => crypto.randomBytes(length).toString('hex')

als.enable()
setTimeout(() => {
  const id = randomBytes(8);
  als.set('id', id);
  const check = (times, id, subId) => {
    assert.equal(als.get('id'), id);
    assert.equal(als.get('subId'), subId);
    if (times < 0) {
      done();
    } else {
      setTimeout(() => {
        check(times - 1, id, subId);
      }, 10);
    }
  };
  setTimeout(() => {
    const subId = randomBytes(8);
    als.set('subId', subId);
    assert.equal(als.get('id'), id);
    check(3, id, subId);
  }, 10);
}, 10);

get

get the value by key, if will find from parent, self --> parent --> parent, until the value is not undefined

  • key the key
const assert = require('assert');
const als = require('async-local-storage');
const crypto = require('crypto');
const randomBytes = length => crypto.randomBytes(length).toString('hex')

als.enable()
setTimeout(() => {
  const id = randomBytes(8);
  als.set('id', id);
  const check = (times, id, subId) => {
    assert.equal(als.get('id'), id);
    assert.equal(als.get('subId'), subId);
    if (times < 0) {
      done();
    } else {
      setTimeout(() => {
        check(times - 1, id, subId);
      }, 10);
    }
  };
  setTimeout(() => {
    const subId = randomBytes(8);
    als.set('subId', subId);
    assert.equal(als.get('id'), id);
    check(3, id, subId);
  }, 10);
}, 10);

currentId

Get the current id

const assert = require('assert');
const als = require('async-local-storage');
als.enable();
setTimeout(() => {
  assert(als.currentId());
}, 10);

use

  • id The tigger id, default is als.currentId()
const assert = require('assert');
const als = require('async-local-storage');
als.enable();
setTimeout(() => {
  assert(als.use());
}, 10);

License

MIT

Keywords

FAQs

Last updated on 26 Jan 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc