CodeSampleX

Ejemplo

toad-cache 3.7.4: LruObject

Muestra verificada para npm toad-cache 3.7.4: LruObject. El contrato se ejecutó en node 22 · linux debian/x64 · docker y pasó.

sha256:8b7abd9b9ad834f64fbb7d00f7b39d7bae34e34e3ac0e8c800987a97c1e4aac6

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-11-29

Caso

HOW
Objetivo
verify toad-cache.LruObject in pkg:npm/toad-cache@3.7.4
Paquetes
Símbolos
  • toad-cache.LruObject
Entorno
node 22.23.2
Creado
2026-11-29T21:34:30Z

Contrato

  1. new LruObject() defaults to max=1000, ttl=0, size=0, and returns undefined for missing keys
  2. new LruObject(max, ttlInMsecs) rejects negative or non-integer max with 'Invalid max value' and invalid ttlInMsecs with 'Invalid ttl value'
  3. get() and overwriting set() promote an entry to most-recently-used so capacity overflow evicts the least-recently-used entry
  4. null-prototype backing storage handles keys named 'toString' and 'constructor' without colliding with Object.prototype
  5. getMany(), keys(), evict(), delete(), deleteMany(), and clear() maintain accurate entries and size
  6. max=0 disables capacity eviction
  7. ttlInMsecs>0 records a numeric expiry via expiresAt() and removes expired entries on get() returning undefined

Archivos

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • test/contract.cjs

Descargar el artefacto de código fuente (tar.gz)

Código fuente

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify toad-cache.LruObject in pkg:npm/toad-cache@3.7.4
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/toad-cache@3.7.4
Demonstrate these symbols/APIs:
  - toad-cache.LruObject
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:82efd25aa6add9e19d7e374f08df85c8908142ecccf093593840d2a66ee66ab0","constraints":{"executionContext":"node"},"contract":["new LruObject() defaults to max=1000, ttl=0, size=0, and returns undefined for missing keys","new LruObject(max, ttlInMsecs) rejects negative or non-integer max with 'Invalid max value' and invalid ttlInMsecs with 'Invalid ttl value'","get() and overwriting set() promote an entry to most-recently-used so capacity overflow evicts the least-recently-used entry","null-prototype backing storage handles keys named 'toString' and 'constructor' without colliding with Object.prototype","getMany(), keys(), evict(), delete(), deleteMany(), and clear() maintain accurate entries and size","max=0 disables capacity eviction","ttlInMsecs\u003e0 records a numeric expiry via expiresAt() and removes expired entries on get() returning undefined"],"goal":"verify toad-cache.LruObject in pkg:npm/toad-cache@3.7.4","kind":"HOW","packages":["pkg:npm/toad-cache@3.7.4"],"schemaVersion":1,"symbols":["toad-cache.LruObject"]},"contractCommand":["node","test/contract.cjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/toad-cache@3.7.4"],"schemaVersion":1,"subject":"pkg:npm/toad-cache@3.7.4","symbols":["toad-cache.LruObject"],"verifierAdapter":"node-typescript@1"}
index.js
'use strict';

const { LruObject } = require('toad-cache');

function createLruCache(max, ttlInMsecs) {
  return new LruObject(max, ttlInMsecs);
}

module.exports = {
  createLruCache,
  LruObject,
};
package-lock.json
{
  "name": "toad-cache-lru-object-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "toad-cache-lru-object-sample",
      "version": "1.0.0",
      "dependencies": {
        "toad-cache": "3.7.4"
      }
    },
    "node_modules/toad-cache": {
      "version": "3.7.4",
      "resolved": "https://registry.npmjs.org/toad-cache/-/toad-cache-3.7.4.tgz",
      "integrity": "sha512-m1TdR/rvT7kgGJZhspNtXdsdYk0fddFpJJFlG5s+UkPFo6lkLoZ3YLOaovPYjq1R75NP5JfeTlSHaOsE09peCg==",
      "license": "MIT",
      "engines": {
        "node": ">=20"
      }
    }
  }
}
package.json
{
  "name": "toad-cache-lru-object-sample",
  "version": "1.0.0",
  "private": true,
  "type": "commonjs",
  "description": "Minimal sample verifying toad-cache.LruObject in toad-cache@3.7.4",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.cjs"
  },
  "dependencies": {
    "toad-cache": "3.7.4"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify toad-cache.LruObject in pkg:npm/toad-cache@3.7.4",
  "kind": "HOW",
  "packages": [
    "pkg:npm/toad-cache@3.7.4"
  ],
  "symbols": [
    "toad-cache.LruObject"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.cjs
'use strict';

const assert = require('node:assert/strict');
const { setTimeout: delay } = require('node:timers/promises');
const { createLruCache, LruObject } = require('../index.js');

async function main() {
  // 1. Default constructor values
  const defaultCache = new LruObject();
  assert.equal(defaultCache.max, 1000);
  assert.equal(defaultCache.ttl, 0);
  assert.equal(defaultCache.size, 0);
  assert.equal(defaultCache.get('missing'), undefined);
  assert.equal(defaultCache.expiresAt('missing'), undefined);

  // 2. Constructor parameter validation
  assert.throws(() => new LruObject(-1), { message: 'Invalid max value' });
  assert.throws(() => new LruObject(1.5), { message: 'Invalid max value' });
  assert.throws(() => new LruObject(10, -1), { message: 'Invalid ttl value' });
  assert.throws(() => new LruObject(10, 0.5), { message: 'Invalid ttl value' });

  // 3. LRU eviction order and recency promotion via get() and set()
  const cache = createLruCache(2, 0);
  cache.set('a', 1);
  cache.set('b', 2);
  assert.equal(cache.size, 2);
  assert.equal(cache.expiresAt('a'), 0);

  // Reading 'a' promotes it to most-recently-used; inserting 'c' evicts 'b'
  assert.equal(cache.get('a'), 1);
  cache.set('c', 3);
  assert.equal(cache.size, 2);
  assert.equal(cache.get('b'), undefined);
  assert.equal(cache.get('a'), 1);
  assert.equal(cache.get('c'), 3);

  // Updating an existing key replaces its value and bumps it to most-recently-used
  cache.set('a', 10);
  assert.equal(cache.size, 2);
  cache.set('d', 4);
  assert.equal(cache.get('c'), undefined);
  assert.equal(cache.get('a'), 10);
  assert.equal(cache.get('d'), 4);
  assert.deepEqual(cache.keys().sort(), ['a', 'd']);

  // 4. Null-prototype storage avoids collisions with Object.prototype keys
  const protoCache = createLruCache(3, 0);
  assert.equal(protoCache.get('toString'), undefined);
  protoCache.set('toString', 'custom-value');
  protoCache.set('constructor', 'ctor-value');
  assert.equal(protoCache.get('toString'), 'custom-value');
  assert.equal(protoCache.get('constructor'), 'ctor-value');

  // 5. Batch operations, explicit evict(), delete(), deleteMany(), clear()
  const opsCache = createLruCache(4, 0);
  opsCache.set('k1', 'v1');
  opsCache.set('k2', 'v2');
  opsCache.set('k3', 'v3');
  assert.deepEqual(opsCache.getMany(['k2', 'missing', 'k1']), ['v2', undefined, 'v1']);
  // Least-recently-used is now 'k3' (k2 and k1 were bumped by getMany)
  opsCache.evict();
  assert.equal(opsCache.get('k3'), undefined);
  assert.equal(opsCache.size, 2);

  opsCache.set('k4', 'v4');
  opsCache.delete('k2');
  assert.equal(opsCache.get('k2'), undefined);
  assert.equal(opsCache.size, 2);

  opsCache.deleteMany(['k1', 'nonexistent']);
  assert.equal(opsCache.get('k1'), undefined);
  assert.equal(opsCache.size, 1);

  opsCache.clear();
  assert.equal(opsCache.size, 0);
  assert.deepEqual(opsCache.keys(), []);

  // 6. Unlimited capacity when max === 0
  const unlimited = createLruCache(0, 0);
  unlimited.set('x', 1);
  unlimited.set('y', 2);
  unlimited.set('z', 3);
  assert.equal(unlimited.size, 3);

  // 7. TTL expiration removes stale entries on get()
  const ttlCache = createLruCache(5, 30);
  const beforeSet = Date.now();
  ttlCache.set('ephemeral', 'fresh');
  const expiry = ttlCache.expiresAt('ephemeral');
  assert.equal(typeof expiry, 'number');
  assert.ok(expiry >= beforeSet + 30);
  assert.equal(ttlCache.get('ephemeral'), 'fresh');

  await delay(60);
  assert.equal(ttlCache.get('ephemeral'), undefined);
  assert.equal(ttlCache.size, 0);

  console.log('toad-cache.LruObject contract passed');
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});

Seeder de origen

anónimo