CodeSampleX

示例

follow-redirects 1.16.0

已验证示例 — npm follow-redirects 1.16.0. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: followRedirects exports http and https drop-in wrappers and wrap…

sha256:10bb4eb9761b8a066161ed3654884b7d3fcd66088b437fd14007bdc31ed8f6f1

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm

验证运行环境

环境 契约 阶段 运行日期
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-20

案例

HOW
目标
verify pkg:npm/follow-redirects@1.16.0
包
环境
node 22.23.2
创建时间
2026-08-20T20:35:02Z

契约

  1. followRedirects exports http and https drop-in wrappers and wrap function
  2. http.get follows 301 and 302 redirects automatically to destination URL and populates responseUrl
  3. trackRedirects option records redirect history array with status codes and URLs for each hop
  4. maxRedirects option limits maximum redirect count and emits error when limit is exceeded
  5. beforeRedirect callback allows intercepting and inspecting redirect options before following

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • 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/follow-redirects@1.16.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/follow-redirects@1.16.0
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - 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.
csx.json
{"case":{"caseId":"case:sha256:24396618ab3d586916cac766910334e6fb925c5e4bd0b91e19f846f0b9efbe34","constraints":{"executionContext":"node"},"contract":["followRedirects exports http and https drop-in wrappers and wrap function","http.get follows 301 and 302 redirects automatically to destination URL and populates responseUrl","trackRedirects option records redirect history array with status codes and URLs for each hop","maxRedirects option limits maximum redirect count and emits error when limit is exceeded","beforeRedirect callback allows intercepting and inspecting redirect options before following"],"goal":"verify pkg:npm/follow-redirects@1.16.0","kind":"HOW","packages":["pkg:npm/follow-redirects@1.16.0"],"schemaVersion":1},"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","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/follow-redirects@1.16.0"],"schemaVersion":1,"subject":"pkg:npm/follow-redirects@1.16.0","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-follow-redirects",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-follow-redirects",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "follow-redirects": "1.16.0"
      }
    },
    "node_modules/follow-redirects": {
      "version": "1.16.0",
      "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.16.0.tgz",
      "integrity": "sha512-y5rN/uOsadFT/JfYwhxRS5R7Qce+g3zG97+JrtFZlC9klX/W5hD7iiLzScI4nZqUS7DNUdhPgw4xI8W2LuXlUw==",
      "license": "MIT",
      "engines": {
        "node": ">=4.0"
      },
      "peerDependenciesMeta": {
        "debug": {
          "optional": true
        }
      }
    }
  }
}
package.json
{
  "name": "sample-follow-redirects",
  "version": "1.0.0",
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "follow-redirects": "1.16.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/follow-redirects@1.16.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/follow-redirects@1.16.0"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "runtime": "node@22.23.2"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import http from 'node:http';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const followRedirects = require('follow-redirects');
const { http: fHttp, https: fHttps } = followRedirects;

// 1. followRedirects exports http and https drop-in wrappers and wrap function
assert.equal(typeof fHttp, 'object', 'followRedirects.http should be an object');
assert.equal(typeof fHttp.get, 'function', 'followRedirects.http.get should be a function');
assert.equal(typeof fHttp.request, 'function', 'followRedirects.http.request should be a function');
assert.equal(typeof fHttps, 'object', 'followRedirects.https should be an object');
assert.equal(typeof fHttps.get, 'function', 'followRedirects.https.get should be a function');
assert.equal(typeof fHttps.request, 'function', 'followRedirects.https.request should be a function');
assert.equal(typeof followRedirects.wrap, 'function', 'followRedirects.wrap should be a function');

// Create local test HTTP server for offline testing
const server = http.createServer((req, res) => {
  if (req.url === '/redirect-1') {
    res.writeHead(302, { Location: '/redirect-2' });
    res.end();
  } else if (req.url === '/redirect-2') {
    res.writeHead(301, { Location: '/final' });
    res.end();
  } else if (req.url === '/final') {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('final destination reached');
  } else if (req.url === '/loop-a') {
    res.writeHead(302, { Location: '/loop-b' });
    res.end();
  } else if (req.url === '/loop-b') {
    res.writeHead(302, { Location: '/loop-a' });
    res.end();
  } else {
    res.writeHead(404);
    res.end('not found');
  }
});

await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
const port = server.address().port;
const baseUrl = `http://127.0.0.1:${port}`;

try {
  // 2. http.get follows 301 and 302 redirects automatically to destination URL and populates responseUrl
  await new Promise((resolve, reject) => {
    fHttp.get(`${baseUrl}/redirect-1`, (res) => {
      let data = '';
      res.on('data', (chunk) => { data += chunk; });
      res.on('end', () => {
        try {
          assert.equal(res.statusCode, 200);
          assert.equal(data, 'final destination reached');
          assert.equal(res.responseUrl, `${baseUrl}/final`);
          resolve();
        } catch (e) {
          reject(e);
        }
      });
    }).on('error', reject);
  });

  // 3. trackRedirects option records redirect history array with status codes and URLs for each hop
  await new Promise((resolve, reject) => {
    fHttp.get({
      host: '127.0.0.1',
      port,
      path: '/redirect-1',
      trackRedirects: true,
    }, (res) => {
      let data = '';
      res.on('data', (chunk) => { data += chunk; });
      res.on('end', () => {
        try {
          assert.equal(res.statusCode, 200);
          assert.ok(Array.isArray(res.redirects), 'res.redirects should be an array');
          assert.equal(res.redirects.length, 3);
          assert.equal(res.redirects[0].statusCode, 302);
          assert.equal(res.redirects[0].url, `${baseUrl}/redirect-1`);
          assert.equal(res.redirects[1].statusCode, 301);
          assert.equal(res.redirects[1].url, `${baseUrl}/redirect-2`);
          assert.equal(res.redirects[2].statusCode, 200);
          assert.equal(res.redirects[2].url, `${baseUrl}/final`);
          resolve();
        } catch (e) {
          reject(e);
        }
      });
    }).on('error', reject);
  });

  // 4. maxRedirects option limits maximum redirect count and emits error when limit is exceeded
  await new Promise((resolve, reject) => {
    const req = fHttp.get({
      host: '127.0.0.1',
      port,
      path: '/loop-a',
      maxRedirects: 3,
    });
    req.on('error', (err) => {
      try {
        assert.match(err.message, /Maximum number of redirects exceeded/i);
        resolve();
      } catch (e) {
        reject(e);
      }
    });
    req.on('response', () => {
      reject(new Error('Expected maxRedirects error but received response'));
    });
  });

  // 5. beforeRedirect callback allows intercepting and inspecting redirect options before following
  const redirectedHops = [];
  await new Promise((resolve, reject) => {
    fHttp.get({
      host: '127.0.0.1',
      port,
      path: '/redirect-1',
      beforeRedirect: (options, responseDetails) => {
        redirectedHops.push({
          path: options.path,
          statusCode: responseDetails.statusCode,
        });
      },
    }, (res) => {
      res.resume();
      res.on('end', () => {
        try {
          assert.equal(redirectedHops.length, 2);
          assert.equal(redirectedHops[0].statusCode, 302);
          assert.equal(redirectedHops[0].path, '/redirect-2');
          assert.equal(redirectedHops[1].statusCode, 301);
          assert.equal(redirectedHops[1].path, '/final');
          resolve();
        } catch (e) {
          reject(e);
        }
      });
    }).on('error', reject);
  });

  console.log('All contract assertions passed.');
} finally {
  await new Promise((resolve) => server.close(resolve));
}

原始种子者

匿名