示例
semver 5.7.2: major
已验证示例 — npm semver 5.7.2: major. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: major returns integer major component for standard semantic version…
sha256:aa7cdb815f7800413330dcdebfbd1fe7452e6d0fc4f82496d8cf9e884f6eaf7c
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 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-28 |
案例
HOW- 目标
- verify semver.major in pkg:npm/semver@5.7.2
- 符号
-
- semver.major
- 环境
- node 22.23.2
- 创建时间
- 2026-08-28T04:39:20Z
契约
- major returns integer major component for standard semantic version strings
- major strips leading v prefix when extracting major version
- major extracts correct major version when prerelease identifiers and build metadata are present
- major throws TypeError for invalid version formats or non-string inputs
- major supports loose parsing mode to handle whitespace after v prefix
文件
- PROMPT.md
- csx.json
- package-lock.json
- package.json
- spec.json
- src/index.js
- test/contract.mjs
源代码
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 semver.major in pkg:npm/semver@5.7.2
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/semver@5.7.2
Demonstrate these symbols/APIs:
- semver.major
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:70a153538ef00d3003b2762aa53f5496ec9b95cc2b535db7304f602d3ca51a5b","contract":["major returns integer major component for standard semantic version strings","major strips leading v prefix when extracting major version","major extracts correct major version when prerelease identifiers and build metadata are present","major throws TypeError for invalid version formats or non-string inputs","major supports loose parsing mode to handle whitespace after v prefix"],"goal":"verify semver.major in pkg:npm/semver@5.7.2","kind":"HOW","packages":["pkg:npm/semver@5.7.2"],"schemaVersion":1,"symbols":["semver.major"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","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/semver@5.7.2"],"schemaVersion":1,"subject":"pkg:npm/semver@5.7.2","symbols":["semver.major"],"verifierAdapter":"node-typescript@1"}
{
"name": "sample-semver-major",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-semver-major",
"version": "1.0.0",
"license": "MIT-0",
"dependencies": {
"semver": "5.7.2"
}
},
"node_modules/semver": {
"version": "5.7.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.2.tgz",
"integrity": "sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==",
"license": "ISC",
"bin": {
"semver": "bin/semver"
}
}
}
}
{
"name": "sample-semver-major",
"version": "1.0.0",
"private": true,
"description": "Clean-room code sample for semver.major in semver@5.7.2",
"main": "src/index.js",
"license": "MIT-0",
"dependencies": {
"semver": "5.7.2"
}
}
{
"schemaVersion": 1,
"goal": "verify semver.major in pkg:npm/semver@5.7.2",
"kind": "HOW",
"packages": [
"pkg:npm/semver@5.7.2"
],
"symbols": [
"semver.major"
]
}
const semver = require('semver');
/**
* Extracts the major version number from a semantic version string.
*
* @param {string} version - The version string to extract major from.
* @param {boolean|object} [loose=false] - Whether to allow loose parsing.
* @returns {number} The integer major version.
* @throws {TypeError} If the version string is invalid.
*/
function extractMajor(version, loose = false) {
return semver.major(version, loose);
}
/**
* Safely extracts the major version number without throwing.
* Returns null if the version is invalid.
*
* @param {string} version - The version string to parse.
* @param {boolean|object} [loose=false] - Whether to allow loose parsing.
* @returns {number|null} The integer major version, or null if invalid.
*/
function safeExtractMajor(version, loose = false) {
try {
return semver.major(version, loose);
} catch {
return null;
}
}
module.exports = {
semver,
major: semver.major,
extractMajor,
safeExtractMajor
};
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const {
semver,
major,
extractMajor,
safeExtractMajor
} = require('../src/index.js');
async function runContractTests() {
// 1. Export verification
assert.equal(typeof semver.major, 'function', 'semver.major must be a function');
assert.equal(typeof major, 'function', 'major export must be a function');
assert.equal(typeof extractMajor, 'function', 'extractMajor helper must be a function');
assert.equal(typeof safeExtractMajor, 'function', 'safeExtractMajor helper must be a function');
// 2. major returns integer major component for standard semantic version strings
assert.equal(semver.major('1.2.3'), 1, 'major of 1.2.3 should be 1');
assert.equal(major('2.4.10'), 2, 'major of 2.4.10 should be 2');
assert.equal(extractMajor('0.1.0'), 0, 'extractMajor of 0.1.0 should be 0');
assert.equal(extractMajor('10.20.30'), 10, 'extractMajor of 10.20.30 should be 10');
assert.equal(safeExtractMajor('5.0.0'), 5, 'safeExtractMajor of 5.0.0 should be 5');
// 3. major strips leading v prefix when extracting major version
assert.equal(semver.major('v1.0.0'), 1, 'leading v should be stripped');
assert.equal(major('v0.0.1'), 0, 'leading v should be stripped on 0.0.1');
assert.equal(extractMajor('v3.14.159'), 3, 'leading v should be stripped in extractMajor');
assert.equal(safeExtractMajor('v4.2.0'), 4, 'leading v should be stripped in safeExtractMajor');
// 4. major extracts correct major version when prerelease identifiers and build metadata are present
assert.equal(semver.major('3.4.5-alpha.1'), 3, 'prerelease tag should be ignored for major version');
assert.equal(major('2.0.0-rc.2'), 2, 'rc prerelease tag should be ignored for major version');
assert.equal(extractMajor('4.0.0+build.123'), 4, 'build metadata should be ignored for major version');
assert.equal(extractMajor('1.2.3-beta.1+20130313144700'), 1, 'prerelease and build metadata ignored');
assert.equal(safeExtractMajor('7.0.0-beta.5'), 7, 'prerelease tag handled in safeExtractMajor');
// 5. major throws TypeError for invalid version formats or non-string inputs
assert.throws(
() => semver.major('not-a-version'),
{
name: 'TypeError',
message: 'Invalid Version: not-a-version'
},
'invalid string should throw TypeError'
);
assert.throws(
() => major('1.2'),
{
name: 'TypeError',
message: 'Invalid Version: 1.2'
},
'incomplete two-part version should throw TypeError'
);
assert.throws(
() => extractMajor(''),
{
name: 'TypeError',
message: 'Invalid Version: '
},
'empty string should throw TypeError'
);
assert.throws(
() => extractMajor(null),
{
name: 'TypeError',
message: 'Invalid Version: null'
},
'null input should throw TypeError'
);
assert.throws(
() => extractMajor(undefined),
{
name: 'TypeError',
message: 'Invalid Version: undefined'
},
'undefined input should throw TypeError'
);
assert.throws(
() => extractMajor(123),
{
name: 'TypeError',
message: 'Invalid Version: 123'
},
'numeric input should throw TypeError'
);
assert.equal(safeExtractMajor('not-a-version'), null, 'safeExtractMajor should return null on invalid version');
assert.equal(safeExtractMajor(null), null, 'safeExtractMajor should return null on null');
// 6. major supports loose parsing mode to handle whitespace after v prefix
assert.equal(semver.major('v 6.0.0', true), 6, 'loose mode should parse space after v');
assert.equal(extractMajor('v 8.1.0', { loose: true }), 8, 'loose mode via options object should work');
assert.equal(safeExtractMajor('v 9.0.0', true), 9, 'safeExtractMajor supports loose flag');
assert.throws(
() => semver.major('v 6.0.0', false),
{
name: 'TypeError',
message: 'Invalid Version: v 6.0.0'
},
'strict mode should reject space after v'
);
assert.equal(safeExtractMajor('v 6.0.0', false), null, 'safeExtractMajor should return null for strict rejection');
console.log('All semver.major contract tests passed successfully.');
}
runContractTests().catch((err) => {
console.error('Contract test failure:', err);
process.exit(1);
});
原始种子者
匿名