CodeSampleX

サンプル

http-cache-semantics 4.3.0: CachePolicy, CachePolicy.storable, CachePolicy.stale

検証済みサンプル — npm http-cache-semantics 4.3.0: CachePolicy, CachePolicy.storable, CachePolicy.stale. node 22 · linux debian/x64 · docker で contract を実行し、成功しました.

sha256:81aa90e528785d4293dae39801fcd3a6a6a3bc1a0e53e0eb4d05667aab145824

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 node linux 24 · ubuntu · glibc 2.39 x64 node npm

検証実行環境

環境 コントラクト ステージ 実行日
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-10-17

ケース

HOW
ゴール
verify pkg:npm/http-cache-semantics@4.3.0
パッケージ
シンボル
  • CachePolicy
  • CachePolicy.storable
  • CachePolicy.stale
  • CachePolicy.timeToLive
  • CachePolicy.satisfiesWithoutRevalidation
  • CachePolicy.revalidationHeaders
  • CachePolicy.revalidatedPolicy
  • CachePolicy.toObject
  • CachePolicy.fromObject
環境
node
作成日
2026-10-17T23:42:28Z

コントラクト

  1. CachePolicy.storable evaluates whether an HTTP response can be stored in cache according to Cache-Control headers
  2. CachePolicy.stale and CachePolicy.timeToLive evaluate whether a cached HTTP response has expired based on max-age
  3. CachePolicy.satisfiesWithoutRevalidation determines if a cached response can satisfy a new incoming HTTP request
  4. CachePolicy.revalidationHeaders generates conditional request headers including if-none-match for 304 revalidation
  5. CachePolicy.revalidatedPolicy returns updated policy instance merging cached headers with 304 revalidation response headers
  6. CachePolicy.toObject and CachePolicy.fromObject serialize and restore policy state

ファイル

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

ソースアーティファクトをダウンロード (tar.gz)

ソース

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/http-cache-semantics@4.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/http-cache-semantics@4.3.0

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:86cd9831f7974ccc828601407b3134153528a0864e929376dd12b7337e76388e","contract":["CachePolicy.storable evaluates whether an HTTP response can be stored in cache according to Cache-Control headers","CachePolicy.stale and CachePolicy.timeToLive evaluate whether a cached HTTP response has expired based on max-age","CachePolicy.satisfiesWithoutRevalidation determines if a cached response can satisfy a new incoming HTTP request","CachePolicy.revalidationHeaders generates conditional request headers including if-none-match for 304 revalidation","CachePolicy.revalidatedPolicy returns updated policy instance merging cached headers with 304 revalidation response headers","CachePolicy.toObject and CachePolicy.fromObject serialize and restore policy state"],"goal":"verify pkg:npm/http-cache-semantics@4.3.0","kind":"HOW","packages":["pkg:npm/http-cache-semantics@4.3.0"],"schemaVersion":1,"symbols":["CachePolicy","CachePolicy.storable","CachePolicy.stale","CachePolicy.timeToLive","CachePolicy.satisfiesWithoutRevalidation","CachePolicy.revalidationHeaders","CachePolicy.revalidatedPolicy","CachePolicy.toObject","CachePolicy.fromObject"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/http-cache-semantics@4.3.0"],"schemaVersion":1,"subject":"pkg:npm/http-cache-semantics@4.3.0","symbols":["CachePolicy","CachePolicy.storable","CachePolicy.stale","CachePolicy.timeToLive","CachePolicy.satisfiesWithoutRevalidation","CachePolicy.revalidationHeaders","CachePolicy.revalidatedPolicy","CachePolicy.toObject","CachePolicy.fromObject"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-http-cache-semantics",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-http-cache-semantics",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "http-cache-semantics": "4.3.0"
      }
    },
    "node_modules/http-cache-semantics": {
      "version": "4.3.0",
      "resolved": "https://registry.npmjs.org/http-cache-semantics/-/http-cache-semantics-4.3.0.tgz",
      "integrity": "sha512-M5t5LlJpS1UHMjvwRQVdFHvPISGeLAxNcrWuJkeGh0KxsqCHZ1O3NXZU/8x7cD0BDcGW8kapxMKTvwlqrNkHkA==",
      "license": "BSD-2-Clause"
    }
  }
}
package.json
{
  "name": "sample-http-cache-semantics",
  "version": "1.0.0",
  "private": true,
  "description": "verify pkg:npm/http-cache-semantics@4.3.0",
  "type": "module",
  "main": "src/index.mjs",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "http-cache-semantics": "4.3.0"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/http-cache-semantics@4.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/http-cache-semantics@4.3.0"
  ]
}
src/index.mjs
import CachePolicy from 'http-cache-semantics';

export { CachePolicy };

export function createCachePolicy(request, response, options) {
  return new CachePolicy(request, response, options);
}

export function isResponseStorable(request, response, options) {
  const policy = new CachePolicy(request, response, options);
  return policy.storable();
}

export function isCacheFresh(policy) {
  return !policy.stale();
}

export function canSatisfyRequest(policy, request) {
  return policy.satisfiesWithoutRevalidation(request);
}

export function getRevalidationHeaders(policy, request) {
  return policy.revalidationHeaders(request);
}

export function applyRevalidation(policy, revalidationRequest, revalidationResponse) {
  return policy.revalidatedPolicy(revalidationRequest, revalidationResponse);
}
test/contract.mjs
import assert from 'node:assert/strict';
import CachePolicy from 'http-cache-semantics';
import {
  createCachePolicy,
  isResponseStorable,
  isCacheFresh,
  canSatisfyRequest,
  getRevalidationHeaders,
  applyRevalidation,
} from '../src/index.mjs';

function runContract() {
  const req = {
    url: 'https://example.com/api/items',
    method: 'GET',
    headers: {
      accept: 'application/json',
    },
  };

  const resFresh = {
    status: 200,
    headers: {
      'cache-control': 'public, max-age=3600',
      etag: '"v1-etag"',
      'last-modified': 'Wed, 21 Oct 2025 07:28:00 GMT',
      'content-type': 'application/json',
    },
  };

  // 1. CachePolicy.storable evaluates whether an HTTP response can be stored in cache according to Cache-Control headers
  const policyFresh = createCachePolicy(req, resFresh);
  assert.strictEqual(policyFresh.storable(), true);
  assert.strictEqual(isResponseStorable(req, resFresh), true);

  const resNoStore = {
    status: 200,
    headers: {
      'cache-control': 'no-store',
    },
  };
  assert.strictEqual(isResponseStorable(req, resNoStore), false);

  // 2. CachePolicy.stale and timeToLive evaluate whether a cached HTTP response has expired based on max-age
  assert.strictEqual(policyFresh.stale(), false);
  assert.strictEqual(isCacheFresh(policyFresh), true);
  assert.strictEqual(policyFresh.timeToLive() > 0, true);

  const resStale = {
    status: 200,
    headers: {
      'cache-control': 'public, max-age=0',
      etag: '"v1-etag"',
    },
  };
  const policyStale = createCachePolicy(req, resStale);
  assert.strictEqual(policyStale.stale(), true);
  assert.strictEqual(isCacheFresh(policyStale), false);

  // 3. CachePolicy.satisfiesWithoutRevalidation determines if a cached response can satisfy a new incoming HTTP request
  assert.strictEqual(canSatisfyRequest(policyFresh, req), true);
  assert.strictEqual(
    canSatisfyRequest(policyFresh, {
      url: 'https://example.com/api/items',
      method: 'POST',
      headers: { accept: 'application/json' },
    }),
    false
  );
  assert.strictEqual(canSatisfyRequest(policyStale, req), false);

  // 4. CachePolicy.revalidationHeaders generates conditional request headers including if-none-match for 304 revalidation
  const revalHeaders = getRevalidationHeaders(policyStale, req);
  assert.strictEqual(revalHeaders['if-none-match'], '"v1-etag"');

  // 5. CachePolicy.revalidatedPolicy returns updated policy instance merging cached headers with 304 revalidation response headers
  const revalReq = {
    url: 'https://example.com/api/items',
    method: 'GET',
    headers: revalHeaders,
  };
  const revalRes = {
    status: 304,
    headers: {
      'cache-control': 'public, max-age=7200',
      etag: '"v1-etag"',
    },
  };
  const revalidated = applyRevalidation(policyStale, revalReq, revalRes);
  assert.strictEqual(revalidated.matches, true);
  assert.strictEqual(revalidated.modified, false);
  assert.strictEqual(revalidated.policy.storable(), true);
  assert.strictEqual(revalidated.policy.stale(), false);

  // 6. CachePolicy.toObject and CachePolicy.fromObject serialize and restore policy state
  const obj = policyFresh.toObject();
  const restored = CachePolicy.fromObject(obj);
  assert.strictEqual(restored.storable(), true);
  assert.strictEqual(restored.stale(), false);
  assert.deepStrictEqual(restored.responseHeaders()['content-type'], 'application/json');
}

runContract();

オリジンシーダー

匿名