CodeSampleX

Exemplo

quick-lru 5.1.1: QuickLRU

Amostra verificada para npm quick-lru 5.1.1: QuickLRU. O contrato rodou em node 22 · linux alpine/x64 · docker e passou: new QuickLRU({ maxSize: number })…

sha256:d933984dd5e340e8e728bc3ddfa3bdae72c5e0d67def59ba84b66715cfc69869

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
2
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 npm

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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

Caso

HOW
Objetivo
verify pkg:npm/quick-lru@5.1.1
Pacotes
Símbolos
  • QuickLRU
Criado
2026-08-26T08:05:43Z

Contrato

  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

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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 de origem

anônimo