CodeSampleX

Exemple

quick-lru 5.1.1: QuickLRU

Échantillon vérifié pour npm quick-lru 5.1.1: QuickLRU. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a réussi.

sha256:d933984dd5e340e8e728bc3ddfa3bdae72c5e0d67def59ba84b66715cfc69869

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
2
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-26
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-09-07

Cas

HOW
Objectif
verify pkg:npm/quick-lru@5.1.1
Paquets
Symboles
  • QuickLRU
Créé
2026-08-26T08:05:43Z

Contrat

  1. new QuickLRU({ maxSize: number }) creates a new QuickLRU cache instance
  2. new QuickLRU() throws TypeError when maxSize option is omitted or not greater than 0
  3. set(key, value) inserts a key-value entry and returns the instance
  4. get(key) returns stored value and updates recency
  5. peek(key) returns stored value without updating recency
  6. has(key) returns true for existing key and false for missing key
  7. delete(key) removes key and returns true if existed
  8. clear() removes all entries and resets size to 0
  9. size returns the current number of cached items
  10. evicts least recently used items when cache exceeds maxSize
  11. onEviction callback is called when items are evicted

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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 pkg:npm/quick-lru@5.1.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/quick-lru@5.1.1
Demonstrate these symbols/APIs:
  - QuickLRU

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:5e3408cd8f7fbce815a5e13dcde60c36a9b0074cc06c3995ac36f34bfbcbebfb","contract":["new QuickLRU({ maxSize: number }) creates a new QuickLRU cache instance","new QuickLRU() throws TypeError when maxSize option is omitted or not greater than 0","set(key, value) inserts a key-value entry and returns the instance","get(key) returns stored value and updates recency","peek(key) returns stored value without updating recency","has(key) returns true for existing key and false for missing key","delete(key) removes key and returns true if existed","clear() removes all entries and resets size to 0","size returns the current number of cached items","evicts least recently used items when cache exceeds maxSize","onEviction callback is called when items are evicted"],"goal":"verify pkg:npm/quick-lru@5.1.1","kind":"HOW","packages":["pkg:npm/quick-lru@5.1.1"],"schemaVersion":1,"symbols":["QuickLRU"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/quick-lru@5.1.1"],"schemaVersion":1,"subject":"pkg:npm/quick-lru@5.1.1","symbols":["QuickLRU"],"verifierAdapter":"node-typescript@1"}
index.mjs
import QuickLRU from 'quick-lru';

export function createLRUCache(maxSize = 100, onEviction) {
	return new QuickLRU({ maxSize, onEviction });
}

export function demoQuickLRU() {
	const lru = new QuickLRU({ maxSize: 2 });
	lru.set('first', '1');
	lru.set('second', '2');
	lru.set('third', '3'); // evicts 'first'
	return {
		hasFirst: lru.has('first'),
		second: lru.get('second'),
		third: lru.get('third'),
		size: lru.size
	};
}
package-lock.json
{
  "name": "quick-lru-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "quick-lru-sample",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "quick-lru": "5.1.1"
      }
    },
    "node_modules/quick-lru": {
      "version": "5.1.1",
      "resolved": "https://registry.npmjs.org/quick-lru/-/quick-lru-5.1.1.tgz",
      "integrity": "sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==",
      "license": "MIT",
      "engines": {
        "node": ">=10"
      },
      "funding": {
        "url": "https://github.com/sponsors/sindresorhus"
      }
    }
  }
}
package.json
{
  "name": "quick-lru-sample",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "license": "MIT-0",
  "dependencies": {
    "quick-lru": "5.1.1"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/quick-lru@5.1.1",
  "kind": "HOW",
  "packages": [
    "pkg:npm/quick-lru@5.1.1"
  ],
  "symbols": [
    "QuickLRU"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import QuickLRU from 'quick-lru';

// 1. Constructor validation
assert.throws(() => new QuickLRU(), {
	name: 'TypeError',
	message: '`maxSize` must be a number greater than 0'
});

assert.throws(() => new QuickLRU({ maxSize: 0 }), {
	name: 'TypeError'
});

assert.throws(() => new QuickLRU({ maxSize: -5 }), {
	name: 'TypeError'
});

// 2. Instance creation and basic operations
const lru = new QuickLRU({ maxSize: 3 });
assert.strictEqual(lru.maxSize, 3);
assert.strictEqual(lru.size, 0);

// 3. set and chainability
const setReturn = lru.set('a', 1);
assert.strictEqual(setReturn, lru);
lru.set('b', 2);
assert.strictEqual(lru.size, 2);

// 4. get and has
assert.strictEqual(lru.has('a'), true);
assert.strictEqual(lru.has('b'), true);
assert.strictEqual(lru.has('c'), false);
assert.strictEqual(lru.get('a'), 1);
assert.strictEqual(lru.get('b'), 2);
assert.strictEqual(lru.get('nonexistent'), undefined);

// 5. peek without updating recency
assert.strictEqual(lru.peek('a'), 1);
assert.strictEqual(lru.peek('missing'), undefined);

// 6. delete
assert.strictEqual(lru.delete('a'), true);
assert.strictEqual(lru.delete('a'), false);
assert.strictEqual(lru.has('a'), false);
assert.strictEqual(lru.get('a'), undefined);
assert.strictEqual(lru.size, 1);

// 7. clear
lru.clear();
assert.strictEqual(lru.size, 0);
assert.strictEqual(lru.has('b'), false);

// 8. Eviction and maxSize behavior
const evicted = [];
const cacheWithEviction = new QuickLRU({
	maxSize: 2,
	onEviction: (key, value) => {
		evicted.push({ key, value });
	}
});

cacheWithEviction.set('x', 10);
cacheWithEviction.set('y', 20);
assert.strictEqual(cacheWithEviction.size, 2);

// Adding 3rd item triggers internal rotation in QuickLRU
cacheWithEviction.set('z', 30);
assert.strictEqual(cacheWithEviction.has('z'), true);

// Adding 4th item triggers eviction of oldCache items
cacheWithEviction.set('w', 40);
assert.strictEqual(evicted.length > 0, true);

// 9. Iteration
const iterCache = new QuickLRU({ maxSize: 5 });
iterCache.set('k1', 'v1');
iterCache.set('k2', 'v2');

const keys = [...iterCache.keys()];
const values = [...iterCache.values()];
const entries = [...iterCache];

assert.deepStrictEqual(keys.sort(), ['k1', 'k2']);
assert.deepStrictEqual(values.sort(), ['v1', 'v2']);
assert.strictEqual(entries.length, 2);

console.log('All contract assertions passed successfully.');

Seeder d'origine

anonyme