示例
toad-cache 3.7.4: Lru
已验证示例 — npm toad-cache 3.7.4: Lru. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: require('toad-cache').Lru is exported as an alias of LruObject with…
sha256:0133ec9182b709c9486099c2a69c82fd329ace98263430a986f276e3d404d57b
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 1
- 构建过它的签名密钥
- 1
声明的环境
node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| 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 |
案例
HOW- 目标
- verify toad-cache.Lru in pkg:npm/toad-cache@3.7.4
- 符号
-
- toad-cache.Lru
- 环境
- node 22.23.2
- 创建时间
- 2026-11-29T01:20:01Z
契约
- require('toad-cache').Lru is exported as an alias of LruObject with defaults max=1000, ttl=0, size=0
- new Lru(max, ttlInMsecs) throws 'Invalid max value' or 'Invalid ttl value' for negative, non-integer, or non-number inputs
- Lru stores and returns falsy values (0, '', false, null) and prototype-name keys like 'toString' while returning undefined for missing keys
- Lru(max) evicts the least-recently-used entry when inserting past max, and both get() and overwriting set() refresh entry recency
- Lru supports getMany(), keys(), evict(), delete(), deleteMany(), clear(), and unlimited capacity when max is 0
- Lru(max, ttlInMsecs) sets expiresAt(key) to Date.now() + ttlInMsecs and lazily deletes and returns undefined once Date.now() >= expiry
文件
- PROMPT.md
- csx.json
- index.js
- package-lock.json
- package.json
- spec.json
- test/contract.cjs
源代码
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.Lru 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.Lru
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.
{"case":{"caseId":"case:sha256:145aaeaa691ea7a2f7f70cd211b041e1a27a562dc10a6e57e1178e8f05654950","constraints":{"executionContext":"node"},"contract":["require('toad-cache').Lru is exported as an alias of LruObject with defaults max=1000, ttl=0, size=0","new Lru(max, ttlInMsecs) throws 'Invalid max value' or 'Invalid ttl value' for negative, non-integer, or non-number inputs","Lru stores and returns falsy values (0, '', false, null) and prototype-name keys like 'toString' while returning undefined for missing keys","Lru(max) evicts the least-recently-used entry when inserting past max, and both get() and overwriting set() refresh entry recency","Lru supports getMany(), keys(), evict(), delete(), deleteMany(), clear(), and unlimited capacity when max is 0","Lru(max, ttlInMsecs) sets expiresAt(key) to Date.now() + ttlInMsecs and lazily deletes and returns undefined once Date.now() \u003e= expiry"],"goal":"verify toad-cache.Lru in pkg:npm/toad-cache@3.7.4","kind":"HOW","packages":["pkg:npm/toad-cache@3.7.4"],"schemaVersion":1,"symbols":["toad-cache.Lru"]},"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.Lru"],"verifierAdapter":"node-typescript@1"}
'use strict';
const { Lru } = require('toad-cache');
function createLruCache(max, ttlInMsecs) {
return new Lru(max, ttlInMsecs);
}
module.exports = {
Lru,
createLruCache,
};
{
"name": "toad-cache-lru-sample",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "toad-cache-lru-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"
}
}
}
}
{
"name": "toad-cache-lru-sample",
"version": "1.0.0",
"private": true,
"main": "index.js",
"scripts": {
"test": "node test/contract.cjs"
},
"dependencies": {
"toad-cache": "3.7.4"
}
}
{
"schemaVersion": 1,
"goal": "verify toad-cache.Lru in pkg:npm/toad-cache@3.7.4",
"kind": "HOW",
"packages": [
"pkg:npm/toad-cache@3.7.4"
],
"symbols": [
"toad-cache.Lru"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"packageManager": "npm@10.9.8",
"runtime": "node@22.23.2"
}
}
'use strict';
const assert = require('node:assert/strict');
const { Lru, LruObject } = require('toad-cache');
const { createLruCache } = require('../index.js');
// 1. Export identity and default constructor values
assert.strictEqual(Lru, LruObject);
const defaultCache = createLruCache();
assert.ok(defaultCache instanceof Lru);
assert.strictEqual(defaultCache.max, 1000);
assert.strictEqual(defaultCache.ttl, 0);
assert.strictEqual(defaultCache.size, 0);
assert.strictEqual(defaultCache.first, null);
assert.strictEqual(defaultCache.last, null);
// 2. Constructor parameter validation (non-negative integers required)
assert.throws(() => new Lru(-1), /Invalid max value/);
assert.throws(() => new Lru(1.5), /Invalid max value/);
assert.throws(() => new Lru('10'), /Invalid max value/);
assert.throws(() => new Lru(10, -1), /Invalid ttl value/);
assert.throws(() => new Lru(10, 0.5), /Invalid ttl value/);
assert.throws(() => new Lru(10, '50'), /Invalid ttl value/);
// 3. Basic set/get, falsy values, and prototype-safe keys
const basic = createLruCache(5, 0);
basic.set('zero', 0);
basic.set('empty', '');
basic.set('flag', false);
basic.set('nil', null);
basic.set('toString', 'safe');
assert.strictEqual(basic.size, 5);
assert.strictEqual(basic.get('zero'), 0);
assert.strictEqual(basic.get('empty'), '');
assert.strictEqual(basic.get('flag'), false);
assert.strictEqual(basic.get('nil'), null);
assert.strictEqual(basic.get('toString'), 'safe');
assert.strictEqual(basic.get('missing'), undefined);
assert.strictEqual(basic.expiresAt('zero'), 0);
assert.strictEqual(basic.expiresAt('missing'), undefined);
// 4. LRU eviction order on get() and set() updates
const lru = createLruCache(2, 0);
lru.set('a', 1);
lru.set('b', 2);
assert.strictEqual(lru.size, 2);
assert.strictEqual(lru.first.key, 'a');
assert.strictEqual(lru.last.key, 'b');
// Accessing 'a' promotes it to most-recently-used so 'b' becomes the eviction candidate
assert.strictEqual(lru.get('a'), 1);
assert.strictEqual(lru.first.key, 'b');
assert.strictEqual(lru.last.key, 'a');
// Adding 'c' evicts 'b'
lru.set('c', 3);
assert.strictEqual(lru.size, 2);
assert.strictEqual(lru.get('b'), undefined);
assert.strictEqual(lru.get('a'), 1);
assert.strictEqual(lru.get('c'), 3);
// Replacing an existing key updates its value without growing size and bumps recency
lru.set('a', 10);
assert.strictEqual(lru.size, 2);
assert.strictEqual(lru.last.key, 'a');
assert.strictEqual(lru.get('a'), 10);
// Adding 'd' now evicts 'c' (the least-recently-used entry)
lru.set('d', 4);
assert.strictEqual(lru.size, 2);
assert.strictEqual(lru.get('c'), undefined);
assert.deepStrictEqual(lru.getMany(['a', 'd', 'missing']), [10, 4, undefined]);
// 5. keys(), evict(), delete(), deleteMany(), clear()
const ops = createLruCache(4, 0);
ops.set('k1', 'v1');
ops.set('k2', 'v2');
ops.set('k3', 'v3');
ops.set('k4', 'v4');
assert.deepStrictEqual(ops.keys().sort(), ['k1', 'k2', 'k3', 'k4']);
ops.evict();
assert.strictEqual(ops.get('k1'), undefined);
assert.strictEqual(ops.size, 3);
ops.delete('k3');
assert.strictEqual(ops.get('k3'), undefined);
assert.strictEqual(ops.size, 2);
ops.deleteMany(['k2', 'nonexistent']);
assert.strictEqual(ops.size, 1);
assert.strictEqual(ops.get('k4'), 'v4');
ops.clear();
assert.strictEqual(ops.size, 0);
assert.strictEqual(ops.first, null);
assert.strictEqual(ops.last, null);
assert.deepStrictEqual(ops.keys(), []);
ops.evict(); // no-op on empty cache
assert.strictEqual(ops.size, 0);
// 6. Unlimited capacity when max is 0
const unlimited = createLruCache(0, 0);
unlimited.set('x', 1);
unlimited.set('y', 2);
unlimited.set('z', 3);
assert.strictEqual(unlimited.size, 3);
// 7. TTL expiration and expiresAt() behavior (deterministic clock)
const originalNow = Date.now;
let fakeNow = 1_700_000_000_000;
Date.now = () => fakeNow;
try {
const timed = createLruCache(3, 50);
timed.set('entry', 'val');
assert.strictEqual(timed.expiresAt('entry'), fakeNow + 50);
fakeNow += 49;
assert.strictEqual(timed.get('entry'), 'val');
assert.strictEqual(timed.size, 1);
fakeNow += 1; // now === expiry; item is expired and removed on get()
assert.strictEqual(timed.get('entry'), undefined);
assert.strictEqual(timed.size, 0);
assert.strictEqual(timed.expiresAt('entry'), undefined);
} finally {
Date.now = originalNow;
}
原始种子者
匿名