Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@creditkarma/async-scope

Package Overview
Dependencies
Maintainers
5
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@creditkarma/async-scope - npm Package Compare versions

Comparing version 0.0.8 to 0.0.9

6

dist/main/AsyncScope.d.ts

@@ -7,4 +7,7 @@ import { IAsyncOptions, IAsyncScope } from './types';

private purgeInterval;
private maxSize;
private lastPurge;
constructor({nodeExpiration, purgeInterval}?: IAsyncOptions);
private previousId;
private asyncHooks;
constructor({nodeExpiration, purgeInterval, maxSize, asyncHooks}?: IAsyncOptions);
get<T>(key: string): T | null;

@@ -14,4 +17,5 @@ set<T>(key: string, value: T): void;

lineage(): Array<number>;
private removeOldest();
private addNode(asyncId, parentId);
private purge();
}

@@ -10,9 +10,12 @@ "use strict";

}
constructor({ nodeExpiration = constants_1.NODE_EXPIRATION, purgeInterval = constants_1.PURGE_INTERVAL, } = {}) {
constructor({ nodeExpiration = constants_1.NODE_EXPIRATION, purgeInterval = constants_1.PURGE_INTERVAL, maxSize = constants_1.MAX_SIZE, asyncHooks = AsyncHooks, } = {}) {
const self = this;
this.asyncMap = {};
this.asyncMap = { size: 0, oldestId: -1 };
this.nodeExpiration = nodeExpiration;
this.purgeInterval = purgeInterval;
this.maxSize = maxSize;
this.lastPurge = Date.now();
AsyncHooks.createHook({
this.previousId = -1;
this.asyncHooks = asyncHooks;
asyncHooks.createHook({
init(asyncId, type, triggerAsyncId, resource) {

@@ -28,2 +31,10 @@ if (self.asyncMap[triggerAsyncId] === undefined) {

}
if (self.asyncMap.oldestId === -1) {
self.asyncMap.oldestId = asyncId;
}
const previousNode = self.asyncMap[self.previousId];
if (previousNode !== undefined) {
previousNode.nextId = asyncId;
}
self.previousId = asyncId;
self.purge();

@@ -44,3 +55,3 @@ },

get(key) {
const activeId = AsyncHooks.executionAsyncId();
const activeId = this.asyncHooks.executionAsyncId();
this.addNode(activeId, null);

@@ -50,3 +61,3 @@ return Utils.recursiveGet(key, activeId, this.asyncMap);

set(key, value) {
const activeId = AsyncHooks.executionAsyncId();
const activeId = this.asyncHooks.executionAsyncId();
this.addNode(activeId, null);

@@ -59,14 +70,35 @@ const activeNode = this.asyncMap[activeId];

delete(key) {
const activeId = AsyncHooks.executionAsyncId();
const activeId = this.asyncHooks.executionAsyncId();
Utils.recursiveDelete(key, activeId, this.asyncMap);
}
lineage() {
const activeId = AsyncHooks.executionAsyncId();
const activeId = this.asyncHooks.executionAsyncId();
return Utils.lineageFor(activeId, this.asyncMap);
}
removeOldest() {
const oldestId = this.asyncMap.oldestId;
const nodeToDelete = this.asyncMap[oldestId];
if (nodeToDelete !== undefined) {
delete this.asyncMap[this.asyncMap.oldestId];
this.asyncMap.oldestId = nodeToDelete.nextId;
if (nodeToDelete.parentId !== null) {
const parentNode = this.asyncMap[nodeToDelete.parentId];
if (parentNode !== undefined) {
parentNode.children.splice(parentNode.children.indexOf(oldestId), 1);
}
}
}
}
addNode(asyncId, parentId) {
if (this.asyncMap[asyncId] === undefined) {
if (this.asyncMap.size < this.maxSize) {
this.asyncMap.size += 1;
}
else {
this.removeOldest();
}
this.asyncMap[asyncId] = {
id: asyncId,
timestamp: Date.now(),
nextId: -1,
parentId,

@@ -73,0 +105,0 @@ exited: false,

export declare const NODE_EXPIRATION: number;
export declare const PURGE_INTERVAL: number;
export declare const MAX_SIZE: number;

@@ -5,2 +5,3 @@ "use strict";

exports.PURGE_INTERVAL = 10000;
exports.MAX_SIZE = 20000;
//# sourceMappingURL=constants.js.map

@@ -0,1 +1,2 @@

import { IAsyncHooks } from '@creditkarma/async-hooks';
export interface IAsyncScope {

@@ -9,2 +10,4 @@ get<T>(key: string): T | null;

purgeInterval?: number;
maxSize?: number;
asyncHooks?: IAsyncHooks;
}

@@ -17,2 +20,3 @@ export interface IDictionary {

timestamp: number;
nextId: number;
parentId: number | null;

@@ -24,3 +28,5 @@ exited: boolean;

export interface IAsyncMap {
size: number;
oldestId: number;
[asyncId: number]: IAsyncNode;
}

@@ -15,4 +15,4 @@ "use strict";

const nextParentId = asyncNode.parentId;
delete asyncMap[parentId];
if (nextParentId !== null) {
delete asyncMap[parentId];
asyncId = parentId;

@@ -69,4 +69,7 @@ parentId = nextParentId;

}
else {
return [asyncId];
}
}
return [asyncId];
return [];
}

@@ -81,3 +84,7 @@ exports.lineageFor = lineageFor;

delete asyncMap[asyncId];
if (nodeToDestroy.id === asyncMap.oldestId) {
asyncMap.oldestId = nodeToDestroy.nextId;
}
cleanUpParents(asyncId, parentId, asyncMap);
asyncMap.size = (Object.keys(asyncMap).length - 2);
}

@@ -84,0 +91,0 @@ }

11

package.json
{
"name": "@creditkarma/async-scope",
"version": "0.0.8",
"version": "0.0.9",
"description": "A thread local approximation built on async hooks, written in TypeScript",

@@ -22,3 +22,2 @@ "main": "dist/main/index.js",

"lint": "tslint --fix './src/**/*.ts'",
"prepublish": "npm run build",
"prebuild": "npm run clean",

@@ -28,5 +27,5 @@ "build": "npm run lint && tsc",

"pretest": "npm run build",
"test": "npm run test:only --",
"test:watch": "watch 'npm run test:only' dist",
"test:only": "lab --timeout 15000 --verbose -l -S -P spec dist/tests/",
"test": "npm run test:unit && npm run test:integration",
"test:unit": "lab --timeout 15000 --verbose -l -S -P spec dist/tests/unit",
"test:integration": "lab --timeout 15000 --verbose -l -S -P spec dist/tests/integration",
"release:patch": "npm version patch && npm run release:publish",

@@ -44,3 +43,3 @@ "release:minor": "npm version minor && npm run release:publish",

"dependencies": {
"@creditkarma/async-hooks": "0.0.5"
"@creditkarma/async-hooks": "0.0.6"
},

@@ -47,0 +46,0 @@ "devDependencies": {

@@ -13,14 +13,14 @@ # Async Scope

setTimeout(() => {
function childFunction() {
asyncScope.get<number>('foo') // returns 6
}
function childFunction() {
asyncScope.get<number>('foo') // returns 6
}
function parentFunction() {
asyncScope.set('foo', 6)
setTimeout(() => {
childFunction()
}, 1000)
}
function parentFunction() {
asyncScope.set('foo', 6)
setTimeout(() => {
childFunction()
}, 1000)
}
parentFunction()
parentFunction()
}, 500)

@@ -43,3 +43,3 @@

When constructing a new instance there are two optional parameters `nodeExpiration` and `purgeInterval`. The idea behind Async Scope is that data should be very short-lived. Depending on what you are storing memory foot print could be non-trivial if left running for a long period of time. These options configure expiration of data. `nodeExpiration` defines how long data in a particular context should be allowed to live, defaults to 10 minutes. The other option `purgeInterval` is how often the store looks for and ejects expired data, defaults to 5 minutes.
When constructing a new instance there are three optional parameters `nodeExpiration`, `purgeInterval` and `maxSize`. The idea behind Async Scope is that data should be very short-lived. Depending on what you are storing memory foot print could be non-trivial if left running for a long period of time. These options configure expiration of data. `nodeExpiration` defines how long data in a particular context should be allowed to live, defaults to 5 seconds. The option `purgeInterval` is how often the store looks for and ejects expired data, defaults to 10 seconds. The final option `maxSize` configures how many objects to hold in the scope store, defaults to 20000 items. If the `maxSize` is reach older objects are ejected to make room for new ones.

@@ -50,4 +50,5 @@ ```typescript

const asyncScope: AsyncScope = new AsyncScope({
nodeExpiration: 600000
purgeInterval: 300000,
nodeExpiration: 5000
purgeInterval: 10000,
maxSize: 20000,
})

@@ -54,0 +55,0 @@ ```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc