CodeSampleX

Beispiel

http-cache-semantics 4.2.0: stale

Verifiziertes Beispiel für npm http-cache-semantics 4.2.0: stale. Der Vertrag lief auf node 22 · linux debian/x64 · docker und bestand.

sha256:0fd61ab36cb50efacd3bfca6b64a13ace3da820fd576b9c192d7b6206f2262d3

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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-06

Fall

HOW
Ziel
verify http-cache-semantics.stale in pkg:npm/http-cache-semantics@4.2.0
Pakete
Symbole
  • http-cache-semantics.stale
Umgebung
node 22.23.2
Erstellt
2026-09-06T02:55:58Z

Contract

  1. stale returns false when response has unexpired max-age and zero age
  2. stale returns true when response has max-age set to zero
  3. stale returns true when response age header exceeds max-age
  4. stale returns false when response age header is below max-age
  5. stale transitions from false to true when cache resident time exceeds max-age
  6. stale returns true when response contains no-cache directive
  7. stale returns true when response contains no-store directive
  8. stale returns true in shared cache when s-maxage is exceeded even if max-age is unexpired
  9. stale returns false in private cache when s-maxage is exceeded but max-age is unexpired
  10. stale returns true when response Expires header is in the past

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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 http-cache-semantics.stale in pkg:npm/http-cache-semantics@4.2.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/http-cache-semantics@4.2.0
Demonstrate these symbols/APIs:
  - http-cache-semantics.stale

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:65bb070a17fe1b6b168dd7c5f532a9ef7e7b270b3027a9b1421ee8d1980fcffe","contract":["stale returns false when response has unexpired max-age and zero age","stale returns true when response has max-age set to zero","stale returns true when response age header exceeds max-age","stale returns false when response age header is below max-age","stale transitions from false to true when cache resident time exceeds max-age","stale returns true when response contains no-cache directive","stale returns true when response contains no-store directive","stale returns true in shared cache when s-maxage is exceeded even if max-age is unexpired","stale returns false in private cache when s-maxage is exceeded but max-age is unexpired","stale returns true when response Expires header is in the past"],"goal":"verify http-cache-semantics.stale in pkg:npm/http-cache-semantics@4.2.0","kind":"HOW","packages":["pkg:npm/http-cache-semantics@4.2.0"],"schemaVersion":1,"symbols":["http-cache-semantics.stale"]},"contractCommand":["node","test/contract.mjs"],"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/http-cache-semantics@4.2.0"],"schemaVersion":1,"subject":"pkg:npm/http-cache-semantics@4.2.0","symbols":["http-cache-semantics.stale"],"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.2.0"
      }
    },
    "node_modules/http-cache-semantics": {
      "version": "4.2.0",
      "resolved": "https://registry.npmjs.org/http-cache-semantics/-/http-cache-semantics-4.2.0.tgz",
      "integrity": "sha512-dTxcvPXqPvXBQpq5dUr6mEMJX4oIEFv6bwom3FDwKRDsuIjjJGANqhBuoAn9c1RQJIdAKav33ED65E2ys+87QQ==",
      "license": "BSD-2-Clause"
    }
  }
}
package.json
{
  "name": "sample-http-cache-semantics",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for http-cache-semantics@4.2.0",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "http-cache-semantics": "4.2.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify http-cache-semantics.stale in pkg:npm/http-cache-semantics@4.2.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/http-cache-semantics@4.2.0"
  ],
  "symbols": [
    "http-cache-semantics.stale"
  ]
}
src/index.js
'use strict';

const CachePolicy = require('http-cache-semantics');

/**
 * Creates a new CachePolicy instance.
 *
 * @param {object} req - HTTP request representation.
 * @param {object} res - HTTP response representation.
 * @param {object} [options] - Cache policy options.
 * @returns {CachePolicy} The created cache policy.
 */
function createPolicy(req, res, options) {
  return new CachePolicy(req, res, options);
}

/**
 * Determines whether a cached response is stale.
 *
 * @param {CachePolicy} policy - CachePolicy instance.
 * @returns {boolean} True if the cached response is past its expiration.
 */
function isStale(policy) {
  return policy.stale();
}

module.exports = {
  CachePolicy,
  createPolicy,
  isStale,
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const { CachePolicy, createPolicy, isStale } = require('../src/index.js');

async function testAll() {
  // Export and symbol verification
  assert.equal(typeof CachePolicy, 'function', 'CachePolicy should be a constructor function');
  assert.equal(typeof createPolicy, 'function', 'createPolicy should be a function');
  assert.equal(typeof isStale, 'function', 'isStale should be a function');
  assert.equal(typeof CachePolicy.prototype.stale, 'function', 'CachePolicy.prototype.stale must be a function');

  const baseReq = {
    url: '/api/resource',
    method: 'GET',
    headers: { host: 'example.com' },
  };

  // 1. stale returns false when response has unexpired max-age and zero age
  {
    const res = {
      status: 200,
      headers: {
        'cache-control': 'public, max-age=3600',
      },
    };
    const policy = createPolicy(baseReq, res);
    assert.equal(policy.stale(), false);
    assert.equal(isStale(policy), false);
  }

  // 2. stale returns true when response has max-age set to zero
  {
    const res = {
      status: 200,
      headers: {
        'cache-control': 'public, max-age=0',
      },
    };
    const policy = createPolicy(baseReq, res);
    assert.equal(policy.stale(), true);
    assert.equal(isStale(policy), true);
  }

  // 3. stale returns true when response age header exceeds max-age
  {
    const res = {
      status: 200,
      headers: {
        'cache-control': 'public, max-age=60',
        age: '120',
      },
    };
    const policy = createPolicy(baseReq, res);
    assert.equal(policy.stale(), true);
    assert.equal(isStale(policy), true);
  }

  // 4. stale returns false when response age header is below max-age
  {
    const res = {
      status: 200,
      headers: {
        'cache-control': 'public, max-age=120',
        age: '30',
      },
    };
    const policy = createPolicy(baseReq, res);
    assert.equal(policy.stale(), false);
    assert.equal(isStale(policy), false);
  }

  // 5. stale transitions from false to true when cache resident time exceeds max-age
  {
    const res = {
      status: 200,
      headers: {
        'cache-control': 'public, max-age=10',
      },
    };
    const policy = createPolicy(baseReq, res);
    assert.equal(policy.stale(), false);
    assert.equal(isStale(policy), false);

    const initialNow = policy.now();
    policy.now = () => initialNow + 15000;
    assert.equal(policy.stale(), true);
    assert.equal(isStale(policy), true);
  }

  // 6. stale returns true when response contains no-cache directive
  {
    const res = {
      status: 200,
      headers: {
        'cache-control': 'no-cache',
      },
    };
    const policy = createPolicy(baseReq, res);
    assert.equal(policy.stale(), true);
    assert.equal(isStale(policy), true);
  }

  // 7. stale returns true when response contains no-store directive
  {
    const res = {
      status: 200,
      headers: {
        'cache-control': 'no-store',
      },
    };
    const policy = createPolicy(baseReq, res);
    assert.equal(policy.stale(), true);
    assert.equal(isStale(policy), true);
  }

  // 8. stale returns true in shared cache when s-maxage is exceeded even if max-age is unexpired
  {
    const res = {
      status: 200,
      headers: {
        'cache-control': 'public, s-maxage=10, max-age=3600',
        age: '25',
      },
    };
    const policy = createPolicy(baseReq, res);
    assert.equal(policy.stale(), true);
    assert.equal(isStale(policy), true);
  }

  // 9. stale returns false in private cache when s-maxage is exceeded but max-age is unexpired
  {
    const res = {
      status: 200,
      headers: {
        'cache-control': 'public, s-maxage=10, max-age=3600',
        age: '25',
      },
    };
    const policy = createPolicy(baseReq, res, { shared: false });
    assert.equal(policy.stale(), false);
    assert.equal(isStale(policy), false);
  }

  // 10. stale returns true when response Expires header is in the past
  {
    const res = {
      status: 200,
      headers: {
        expires: 'Mon, 01 Jan 1990 00:00:00 GMT',
      },
    };
    const policy = createPolicy(baseReq, res);
    assert.equal(policy.stale(), true);
    assert.equal(isStale(policy), true);
  }

  console.log('All http-cache-semantics stale contract assertions passed.');
}

testAll().catch((err) => {
  console.error('Contract test failure:', err);
  process.exit(1);
});

Ursprungs-Seeder

anonym