ObliviousSet
ObliviousSet provides a JavaScript Set-like data structure where each entry is associated with a Time-To-Live (TTL). Unlike traditional caching mechanisms that often rely on intervals or timeouts for eviction, ObliviousSet's design avoids these, enabling proper garbage collection of the set instance when no active references exist. The current stable version is 2.0.0, which targets modern Node.js environments (>=16). While a strict release cadence isn't published, the package is actively maintained with updates released as needed for features or bug fixes. Its key differentiator is the efficient, passive expiration model where entries are only 'removed' (marked as expired) when explicitly checked via the `has()` method or when iterating over active elements, rather than through eager background processes. This minimizes overhead and resource consumption.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/oblivious-set/dist/index.mjs not supported.
cause Attempting to import `oblivious-set` using CommonJS `require()` syntax in a project that is configured as CommonJS, while `oblivious-set` is an ESM-first package.fixConvert your project to use ES Modules by setting `"type": "module"` in your `package.json` and using `import` statements, or use dynamic import: `const { ObliviousSet } = await import('oblivious-set');`. -
TypeError: ObliviousSet is not a constructor
cause This typically occurs when trying to instantiate `ObliviousSet` with `new` after an incorrect `require()` or `import` that didn't correctly resolve the default or named export.fixEnsure you are using the correct named import: `import { ObliviousSet } from 'oblivious-set';` and that your environment supports ES Modules. -
ReferenceError: ObliviousSet is not defined
cause The `ObliviousSet` class was used without being correctly imported or declared in the current scope.fixAdd the import statement `import { ObliviousSet } from 'oblivious-set';` at the top of your file.
Warnings
- gotcha ObliviousSet entries are not eagerly purged or deleted via background timers. An entry's TTL is only evaluated and applied when `has()`, `get()`, or an iteration method (`forEach`, `values`, `entries`, `keys`) is called on it. This means `size` can include expired items until such an operation occurs.
- breaking Version 2.0.0 of `oblivious-set` moved to an ESM-first distribution and raised the minimum Node.js requirement to version 16. Projects targeting older Node.js versions or relying exclusively on CommonJS `require()` syntax may encounter import errors.
- gotcha Adding a value that already exists in the set will reset its Time-To-Live (TTL) to the initial duration configured for the set. This means subsequent `has()` checks will reflect the new expiration time.
Install
-
npm install oblivious-set -
yarn add oblivious-set -
pnpm add oblivious-set
Imports
- ObliviousSet
const ObliviousSet = require('oblivious-set');import { ObliviousSet } from 'oblivious-set'; - ObliviousSet (Type)
import { ObliviousSet } from 'oblivious-set';import type { ObliviousSet } from 'oblivious-set';
Quickstart
import { ObliviousSet } from 'oblivious-set';
// Create a set with a TTL of 100 milliseconds
const obliviousSet = new ObliviousSet(100);
// Add a value; its TTL starts now
obliviousSet.add('user_session_123');
// Check existence immediately
console.log('Has user_session_123 after add:', obliviousSet.has('user_session_123')); // true
// Wait for the TTL to expire
setTimeout(() => {
console.log('Has user_session_123 after 150ms:', obliviousSet.has('user_session_123')); // false
// Add another value and check its initial state
obliviousSet.add('another_item');
console.log('Has another_item:', obliviousSet.has('another_item')); // true
// Clear all entries from the set
obliviousSet.clear();
console.log('Set size after clear:', obliviousSet.size); // 0
}, 150);