CodeSampleX

示例

xmlhttprequest-ssl 2.1.2

已验证示例 — npm xmlhttprequest-ssl 2.1.2. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: XMLHttpRequest is exported as a constructor function

sha256:25c1a73d8bd1543c9db4c7043051cf24b6b505f957ac24fee045be5ca44a12f6

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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 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-27

案例

HOW
目标
verify pkg:npm/xmlhttprequest-ssl@2.1.2
包
环境
node 22.23.2
创建时间
2026-08-27T14:15:04Z

契约

  1. XMLHttpRequest is exported as a constructor function
  2. XMLHttpRequest opens and executes an asynchronous HTTP GET request
  3. XMLHttpRequest sets request headers and sends HTTP POST with JSON body
  4. XMLHttpRequest onreadystatechange captures status, responseText, and response headers

文件

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

下载源代码构件 (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/xmlhttprequest-ssl@2.1.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/xmlhttprequest-ssl@2.1.2
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.
csx.json
{"case":{"caseId":"case:sha256:3f2ac4d47b3d7b418fbcaa368542a156a2a001d34865b3e11ba720eaa957f011","constraints":{"executionContext":"node"},"contract":["XMLHttpRequest is exported as a constructor function","XMLHttpRequest opens and executes an asynchronous HTTP GET request","XMLHttpRequest sets request headers and sends HTTP POST with JSON body","XMLHttpRequest onreadystatechange captures status, responseText, and response headers"],"goal":"verify pkg:npm/xmlhttprequest-ssl@2.1.2","kind":"HOW","packages":["pkg:npm/xmlhttprequest-ssl@2.1.2"],"schemaVersion":1},"contractCommand":["node","test/contract.js"],"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/xmlhttprequest-ssl@2.1.2"],"schemaVersion":1,"subject":"pkg:npm/xmlhttprequest-ssl@2.1.2","verifierAdapter":"node-typescript@1"}
index.js
const { XMLHttpRequest } = require('xmlhttprequest-ssl');

function createRequest(method, url, headers = {}, body = null) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open(method, url, true);

    for (const [key, value] of Object.entries(headers)) {
      xhr.setRequestHeader(key, value);
    }

    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        if (xhr.status >= 200 && xhr.status < 300) {
          resolve({
            status: xhr.status,
            statusText: xhr.statusText,
            headers: xhr.getAllResponseHeaders(),
            body: xhr.responseText
          });
        } else {
          reject(new Error(`HTTP error ${xhr.status}: ${xhr.statusText}`));
        }
      }
    };

    xhr.onerror = function (err) {
      reject(err || new Error('Network error'));
    };

    xhr.send(body);
  });
}

module.exports = {
  XMLHttpRequest,
  createRequest
};
package-lock.json
{
  "name": "sample-xmlhttprequest-ssl",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-xmlhttprequest-ssl",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "xmlhttprequest-ssl": "2.1.2"
      }
    },
    "node_modules/xmlhttprequest-ssl": {
      "version": "2.1.2",
      "resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-2.1.2.tgz",
      "integrity": "sha512-TEU+nJVUUnA4CYJFLvK5X9AOeH4KvDvhIfm0vV1GaQRtchnG0hgK5p8hw/xjv8cunWYCsiPCSDzObPyhEwq3KQ==",
      "engines": {
        "node": ">=0.4.0"
      }
    }
  }
}
package.json
{
  "name": "sample-xmlhttprequest-ssl",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room verification sample for xmlhttprequest-ssl",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "xmlhttprequest-ssl": "2.1.2"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/xmlhttprequest-ssl@2.1.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/xmlhttprequest-ssl@2.1.2"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.js
const http = require('http');
const assert = require('assert');
const { XMLHttpRequest, createRequest } = require('../index.js');

// Assertion 1: XMLHttpRequest is exported as a constructor function
assert.strictEqual(typeof XMLHttpRequest, 'function', 'XMLHttpRequest should be a constructor function');

const server = http.createServer((req, res) => {
  if (req.method === 'GET' && req.url === '/ping') {
    res.writeHead(200, {
      'Content-Type': 'application/json',
      'X-Server-Id': 'test-server'
    });
    res.end(JSON.stringify({ message: 'pong', ok: true }));
  } else if (req.method === 'POST' && req.url === '/echo') {
    let rawData = '';
    req.on('data', chunk => {
      rawData += chunk;
    });
    req.on('end', () => {
      res.writeHead(200, {
        'Content-Type': 'application/json',
        'X-Echo-Status': 'success'
      });
      res.end(JSON.stringify({ received: JSON.parse(rawData) }));
    });
  } else {
    res.writeHead(404);
    res.end();
  }
});

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

  try {
    // Assertion 2: XMLHttpRequest opens and executes an asynchronous HTTP GET request
    const getRes = await createRequest('GET', `${baseUrl}/ping`, { 'Accept': 'application/json' });
    assert.strictEqual(getRes.status, 200, 'GET status should be 200');
    const getBody = JSON.parse(getRes.body);
    assert.strictEqual(getBody.ok, true, 'GET body.ok should be true');
    assert.strictEqual(getBody.message, 'pong', 'GET body.message should be pong');

    // Assertion 3: XMLHttpRequest sets request headers and sends HTTP POST with JSON body
    const postPayload = { testKey: 'testVal', count: 42 };
    const postRes = await createRequest('POST', `${baseUrl}/echo`, {
      'Content-Type': 'application/json',
      'X-Client-Id': 'client-42'
    }, JSON.stringify(postPayload));
    assert.strictEqual(postRes.status, 200, 'POST status should be 200');
    const postBody = JSON.parse(postRes.body);
    assert.deepStrictEqual(postBody.received, postPayload, 'POST body should echo sent payload');

    // Assertion 4: XMLHttpRequest onreadystatechange captures status, responseText, and response headers
    await new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      xhr.open('GET', `${baseUrl}/ping`, true);
      xhr.setRequestHeader('Accept', 'application/json');

      const statesSeen = [];
      xhr.onreadystatechange = function () {
        statesSeen.push(xhr.readyState);
        if (xhr.readyState === 4) {
          try {
            assert.strictEqual(xhr.status, 200, 'readyState 4 status should be 200');
            assert(xhr.responseText.includes('"ok":true'), 'responseText should include ok:true');
            const headers = xhr.getAllResponseHeaders();
            assert(headers.toLowerCase().includes('x-server-id'), 'response headers should include x-server-id');
            assert.strictEqual(xhr.getResponseHeader('X-Server-Id'), 'test-server', 'getResponseHeader should return header value');
            assert(statesSeen.includes(4), 'statesSeen should include 4');
            resolve();
          } catch (e) {
            reject(e);
          }
        }
      };
      xhr.onerror = function (err) {
        reject(err || new Error('XHR error'));
      };
      xhr.send();
    });

    console.log('Contract tests passed successfully.');
    server.close(() => {
      process.exit(0);
    });
  } catch (err) {
    console.error('Contract test failed:', err);
    server.close(() => {
      process.exit(1);
    });
  }
});

原始种子者

匿名