Socket
Socket
Sign inDemoInstall

async-local-storage

Package Overview
Dependencies
1
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.1.0 to 2.2.0

17

als.js

@@ -185,10 +185,10 @@ const asyncHooks = require('async_hooks');

*/
exports.getFromParent = (key) => {
exports.getFromParent = key => {
const currentData = map.get(getCurrentId());
if (!currentData) {
return null
return null;
}
const value = get({ parent: currentData.parent }, key)
return value
}
const value = get({parent: currentData.parent}, key);
return value;
};

@@ -234,1 +234,8 @@ /**

};
/**
* Get all data of async locatl storage, please don't modify the data
*/
exports.getAllData = function getAllData() {
return map;
};

@@ -10,6 +10,3 @@ const path = require('path');

const als = require('./als');
const {
server,
redisClient,
} = require('./support/server');
const {server, redisClient} = require('./support/server');

@@ -35,3 +32,3 @@ const topList = [];

let err = null;
topList.forEach((item) => {
topList.forEach(item => {
if (ids.indexOf(item.id) !== -1) {

@@ -93,3 +90,3 @@ err = new Error('the id should be unique');

expect(als.get('id')).toBe(id);
topList.push(als.top());
topList.push(als.top());
});

@@ -155,3 +152,3 @@ });

expect(als.get('id')).toBe(id);
topList.push(als.top());
topList.push(als.top());
});

@@ -178,3 +175,3 @@

expect(als.get('id')).toBe(id);
topList.push(als.top());
topList.push(als.top());
});

@@ -204,3 +201,3 @@ });

expect(als.get('id')).toBe(id);
topList.push(als.top());
topList.push(als.top());
});

@@ -216,16 +213,18 @@ });

als.set('id', id);
return delay(10).then(() => {
const fn1 = delay(10).then(() => {
const current = als.getCurrentData();
expect(current.user).toBeUndefined();
return delay(10)
.then(() => {
const fn1 = delay(10).then(() => {
const current = als.getCurrentData();
expect(current.user).toBeUndefined();
});
const fn2 = new Promise(resolve => {
als.set('user', user);
resolve();
});
return Promise.all([fn1, fn2]);
})
.then(() => {
expect(als.get('id')).toBe(id);
expect(als.get('user')).toBe(user);
});
const fn2 = new Promise((resolve) => {
als.set('user', user);
resolve();
});
return Promise.all([fn1, fn2]);
}).then(() => {
expect(als.get('id')).toBe(id);
expect(als.get('user')).toBe(user);
});
});

@@ -238,10 +237,12 @@

als.set('id', id);
return delay(10).then(() => {
expect(als.get('id')).toBe(id);
als.set('id', 'a');
als.set('user', user);
}).then(() => {
expect(als.get('id')).toBe('a');
expect(als.get('user')).toBe(user);
});
return delay(10)
.then(() => {
expect(als.get('id')).toBe(id);
als.set('id', 'a');
als.set('user', user);
})
.then(() => {
expect(als.get('id')).toBe('a');
expect(als.get('user')).toBe(user);
});
});

@@ -256,9 +257,11 @@

const current = als.getCurrentData();
return delay(10).then(() => {
als.set('user', user);
}).then(() => {
expect(current.id).toBe(id);
expect(current.user).toBe(user);
als.disableLinkedTop();
});
return delay(10)
.then(() => {
als.set('user', user);
})
.then(() => {
expect(current.id).toBe(id);
expect(current.user).toBe(user);
als.disableLinkedTop();
});
});

@@ -285,9 +288,10 @@ });

describe('koa', () => {
const check = (url) => {
const check = url => {
const fns = [1, 2, 3, 4, 5].map(() => {
const id = randomBytes(8);
return request(server).get(url)
return request(server)
.get(url)
.set('X-Request-Id', id)
.expect(200)
.then((response) => {
.then(response => {
expect(response.text).toBe(id);

@@ -308,3 +312,4 @@ });

test('get request id(all)', () => check('/?fs=true&delay=true&http=true&session=true'))
test('get request id(all)', () =>
check('/?fs=true&delay=true&http=true&session=true'));
});

@@ -314,55 +319,59 @@

test('top', () => {
als.scope()
let id = als.getFromParent('id')
als.scope();
let id = als.getFromParent('id');
expect(id).toBe(undefined);
als.set('id', 1);
id = als.getFromParent('id')
id = als.getFromParent('id');
expect(id).toBe(undefined);
})
});
describe('1 level', () => {
test('single', () => {
als.scope()
als.scope();
als.set('id', 1);
return delay(10).then(() => {
expect(als.getFromParent('id')).toBe(1)
als.set('id', 2)
expect(als.getFromParent('id')).toBe(1)
expect(als.get('id')).toBe(2)
})
})
expect(als.getFromParent('id')).toBe(1);
als.set('id', 2);
expect(als.getFromParent('id')).toBe(1);
expect(als.get('id')).toBe(2);
});
});
test('multiple', () => {
als.scope()
als.scope();
als.set('id', 1);
return Promise.all([
delay(10).then(() => {
expect(als.getFromParent('id')).toBe(1)
als.set('id', 2)
expect(als.getFromParent('id')).toBe(1)
expect(als.get('id')).toBe(2)
expect(als.getFromParent('id')).toBe(1);
als.set('id', 2);
expect(als.getFromParent('id')).toBe(1);
expect(als.get('id')).toBe(2);
}),
delay(10).then(() => {
expect(als.getFromParent('id')).toBe(1)
als.set('id', 3)
expect(als.getFromParent('id')).toBe(1)
expect(als.get('id')).toBe(3)
expect(als.getFromParent('id')).toBe(1);
als.set('id', 3);
expect(als.getFromParent('id')).toBe(1);
expect(als.get('id')).toBe(3);
}),
]).then(() => {
expect(als.get('id')).toBe(1)
})
})
})
expect(als.get('id')).toBe(1);
});
});
});
test('2 level', () => {
als.scope()
als.scope();
als.set('id', 1);
return delay(10).then(() => {
als.set('key2', 1)
als.set('key2', 1);
return delay(10).then(() => {
expect(als.get('key2')).toBe(1)
expect(als.getFromParent('id')).toBe(1)
als.set('id', 2)
expect(als.getFromParent('id')).toBe(1)
expect(als.get('id')).toBe(2)
})
})
})
})
expect(als.get('key2')).toBe(1);
expect(als.getFromParent('id')).toBe(1);
als.set('id', 2);
expect(als.getFromParent('id')).toBe(1);
expect(als.get('id')).toBe(2);
});
});
});
});
describe('get all data', () => {
expect(als.getAllData()).toBeDefined();
});
{
"name": "async-local-storage",
"description": "Get the value like thread-local storage in threaded programming",
"version": "2.1.0",
"version": "2.2.0",
"author": "Tree Xie <vicansocanbico@gmail.com>",

@@ -6,0 +6,0 @@ "keywords": [

Sorry, the diff of this file is not supported yet

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