New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@peerbit/cache

Package Overview
Dependencies
Maintainers
0
Versions
126
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@peerbit/cache - npm Package Compare versions

Comparing version 2.0.6 to 2.1.0-171d517

dist/src/index.d.ts

116

package.json
{
"name": "@peerbit/cache",
"version": "2.0.6",
"description": "Simple cache",
"type": "module",
"publishConfig": {
"access": "public"
},
"files": [
"lib",
"src",
"!src/**/__tests__",
"!lib/**/__tests__",
"LICENSE"
],
"module": "lib/esm/index.js",
"types": "lib/esm/index.d.ts",
"exports": {
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/dao-xyz/peerbit"
},
"homepage": "https://github.com/dao-xyz/peerbit",
"bugs": "https://github.com/dao-xyz/peerbit/issues",
"scripts": {
"clean": "shx rm -rf lib/*",
"build": "yarn clean && tsc -p tsconfig.json",
"test": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.ts --runInBand --forceExit",
"test:unit": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.unit.ts --runInBand --forceExit",
"test:integration": "node ../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit"
},
"license": "MIT",
"devDependencies": {
"@peerbit/time": "2.0.6",
"@types/yallist": "^4.0.1"
},
"dependencies": {
"yallist": "^4.0.0"
},
"localMaintainers": [
"dao.xyz"
],
"gitHead": "e50907578b203c2f16199e91545c5213cf8cdc3e"
"name": "@peerbit/cache",
"version": "2.1.0-171d517",
"description": "Simple cache",
"type": "module",
"types": "./dist/src/index.d.ts",
"typesVersions": {
"*": {
"*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
],
"src/*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
]
}
},
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"eslintConfig": {
"extends": "peerbit",
"parserOptions": {
"project": true,
"sourceType": "module"
},
"ignorePatterns": [
"!.aegir.js",
"test/ts-use",
"*.d.ts"
]
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/dao-xyz/peerbit"
},
"homepage": "https://github.com/dao-xyz/peerbit",
"bugs": "https://github.com/dao-xyz/peerbit/issues",
"scripts": {
"clean": "aegir clean",
"build": "aegir build --no-bundle",
"test": "aegir test",
"lint": "aegir lint"
},
"license": "MIT",
"devDependencies": {
"@peerbit/time": "2.0.7-171d517",
"@types/yallist": "^4.0.4"
},
"dependencies": {
"yallist": "^4.0.0"
},
"localMaintainers": [
"dao.xyz"
]
}
// Fifo
import yallist from "yallist";
export type CacheData<T> = { value?: T | null; time: number; size: number };
export interface CacheData<T> {
value?: T | null;
time: number;
size: number;
}
type Key = string | bigint | number;
export class Cache<T = undefined> {
private _map: Map<string, CacheData<T>>;
private deleted: Set<string>;
private list: yallist<string>;
private _map: Map<Key, CacheData<T>>;
private deleted: Set<Key>;
private list: yallist<Key>;
currentSize: number;

@@ -22,3 +27,4 @@ deletedSize: number;

}
has(key: string) {
has(key: Key) {
this.trim();

@@ -31,7 +37,7 @@ if (this.deleted.has(key)) {

get map(): Map<string, CacheData<T>> {
get map(): Map<Key, CacheData<T>> {
return this._map;
}
get(key: string): T | null | undefined {
get(key: Key): T | null | undefined {
this.trim();

@@ -47,6 +53,8 @@ if (this.deleted.has(key)) {

const headKey = this.list.head;
if (headKey?.value !== undefined) {
const cacheValue = this._map.get(headKey.value)!;
const outOfDate =
this.ttl !== undefined && cacheValue.time < time - this.ttl;
if (headKey?.value != null) {
const cacheValue = this._map.get(headKey.value);
if (!cacheValue) {
throw new Error("Cache value not found");
}
const outOfDate = this.ttl != null && cacheValue.time < time - this.ttl;
if (outOfDate || this.currentSize > this.max) {

@@ -68,4 +76,4 @@ this.list.shift();

del(key: string) {
const cacheValue = this._map.get(key)!;
del(key: Key) {
const cacheValue = this._map.get(key);
if (cacheValue && !this.deleted.has(key)) {

@@ -79,3 +87,3 @@ this.deleted.add(key);

add(key: string, value?: T, size = 1) {
add(key: Key, value?: T, size = 1) {
this.deleted.delete(key);

@@ -90,2 +98,3 @@ const time = +new Date();

}
clear() {

@@ -92,0 +101,0 @@ this.list = yallist.create();

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