示例
which 2.0.2: sync
已验证示例 — npm which 2.0.2: sync. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: which.sync resolves the path to an executable from a custom search path
sha256:e900d51b6145e11f69e755489fbfb845057b7db355a3628b87929a5ff8021425
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 1
- 构建过它的签名密钥
- 1
声明的环境
linux 24 · ubuntu · glibc 2.39 x64 npm
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| 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-23 |
案例
HOW- 目标
- verify which.sync in pkg:npm/which@2.0.2
- 符号
-
- which.sync
- 创建时间
- 2026-08-23T04:23:24Z
契约
- which.sync resolves the path to an executable from a custom search path
- which.sync throws an ENOENT error when the executable is not found
- which.sync with nothrow option returns null when the executable is not found
- which.sync with all option returns an array of all matching executable paths
- which.sync respects custom path delimiter with colon option
- which.sync directly resolves executable paths containing directory slashes
- findExecutableSync and tryFindExecutableSync helper functions correctly wrap which.sync behavior
文件
- PROMPT.md
- csx.json
- package-lock.json
- package.json
- spec.json
- src/index.mjs
- 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 which.sync in pkg:npm/which@2.0.2
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/which@2.0.2
Demonstrate these symbols/APIs:
- which.sync
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:ccd41ccf7d3f37b27bb0bc9046c7fbe00693a1920b0a56ba28e117fa0b4a38be","contract":["which.sync resolves the path to an executable from a custom search path","which.sync throws an ENOENT error when the executable is not found","which.sync with nothrow option returns null when the executable is not found","which.sync with all option returns an array of all matching executable paths","which.sync respects custom path delimiter with colon option","which.sync directly resolves executable paths containing directory slashes","findExecutableSync and tryFindExecutableSync helper functions correctly wrap which.sync behavior"],"goal":"verify which.sync in pkg:npm/which@2.0.2","kind":"HOW","packages":["pkg:npm/which@2.0.2"],"schemaVersion":1,"symbols":["which.sync"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/which@2.0.2"],"schemaVersion":1,"subject":"pkg:npm/which@2.0.2","symbols":["which.sync"],"verifierAdapter":"node-typescript@1"}
{
"name": "sample-which-sync",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-which-sync",
"version": "1.0.0",
"license": "MIT-0",
"dependencies": {
"which": "2.0.2"
}
},
"node_modules/isexe": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
"integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
"license": "ISC"
},
"node_modules/which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
"integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
"license": "ISC",
"dependencies": {
"isexe": "^2.0.0"
},
"bin": {
"node-which": "bin/node-which"
},
"engines": {
"node": ">= 8"
}
}
}
}
{
"name": "sample-which-sync",
"version": "1.0.0",
"private": true,
"description": "Clean-room verification of which.sync in which@2.0.2",
"main": "src/index.mjs",
"type": "module",
"license": "MIT-0",
"dependencies": {
"which": "2.0.2"
}
}
{
"schemaVersion": 1,
"goal": "verify which.sync in pkg:npm/which@2.0.2",
"kind": "HOW",
"packages": [
"pkg:npm/which@2.0.2"
],
"symbols": [
"which.sync"
]
}
import which from "which";
/**
* Synchronously finds the first matching executable path in the search path or PATH.
*
* @param {string} cmd - Command name or executable file path.
* @param {object} [options={}] - Search options.
* @returns {string} Absolute or relative path to the resolved executable.
*/
export function findExecutableSync(cmd, options = {}) {
return which.sync(cmd, options);
}
/**
* Synchronously finds all matching executable paths in the search path or PATH.
*
* @param {string} cmd - Command name or executable file path.
* @param {object} [options={}] - Search options.
* @returns {string[]} Array of matching executable paths.
*/
export function findAllExecutablesSync(cmd, options = {}) {
return which.sync(cmd, { ...options, all: true });
}
/**
* Safely checks if an executable exists, returning its path or null if not found.
*
* @param {string} cmd - Command name or executable file path.
* @param {object} [options={}] - Search options.
* @returns {string|null} Resolved executable path or null if not found.
*/
export function tryFindExecutableSync(cmd, options = {}) {
return which.sync(cmd, { ...options, nothrow: true });
}
export { which };
export default which;
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import which, {
findExecutableSync,
findAllExecutablesSync,
tryFindExecutableSync,
} from "../src/index.mjs";
const tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), "csx-which-contract-"));
const binA = path.join(tmpBase, "binA");
const binB = path.join(tmpBase, "binB");
try {
fs.mkdirSync(binA, { recursive: true });
fs.mkdirSync(binB, { recursive: true });
const exeA = path.join(binA, "tool-alpha");
const exeB = path.join(binB, "tool-alpha");
const exeOnlyB = path.join(binB, "tool-beta");
fs.writeFileSync(exeA, "#!/bin/sh\necho alpha-a\n", { mode: 0o755 });
fs.writeFileSync(exeB, "#!/bin/sh\necho alpha-b\n", { mode: 0o755 });
fs.writeFileSync(exeOnlyB, "#!/bin/sh\necho beta-b\n", { mode: 0o755 });
// 1. which.sync resolves the path to an executable from a custom search path
{
const resolved = which.sync("tool-alpha", { path: binA });
assert.strictEqual(resolved, exeA, "which.sync should find tool-alpha in binA");
const resolvedBeta = which.sync("tool-beta", { path: `${binA}:${binB}` });
assert.strictEqual(resolvedBeta, exeOnlyB, "which.sync should find tool-beta in binB");
}
// 2. which.sync throws an ENOENT error when the executable is not found
{
assert.throws(
() => which.sync("non-existent-binary-12345", { path: binA }),
(err) => {
assert.strictEqual(err.code, "ENOENT", "Error code must be ENOENT");
assert.ok(err.message.includes("not found: non-existent-binary-12345"), "Error message should mention binary name");
return true;
}
);
}
// 3. which.sync with nothrow option returns null when the executable is not found
{
const result = which.sync("non-existent-binary-12345", { path: binA, nothrow: true });
assert.strictEqual(result, null, "which.sync with nothrow should return null when binary is missing");
}
// 4. which.sync with all option returns an array of all matching executable paths
{
const allMatches = which.sync("tool-alpha", { path: `${binA}:${binB}`, all: true });
assert.ok(Array.isArray(allMatches), "Result must be an array");
assert.strictEqual(allMatches.length, 2, "Both executable locations should be found");
assert.strictEqual(allMatches[0], exeA);
assert.strictEqual(allMatches[1], exeB);
const allBeta = which.sync("tool-beta", { path: `${binA}:${binB}`, all: true });
assert.deepStrictEqual(allBeta, [exeOnlyB]);
}
// 5. which.sync respects custom path delimiter with colon option
{
const resolved = which.sync("tool-beta", {
path: `${binA}|${binB}`,
colon: "|",
});
assert.strictEqual(resolved, exeOnlyB, "which.sync should parse custom colon delimiter");
}
// 6. which.sync directly resolves executable paths containing directory slashes
{
const resolvedDirect = which.sync(exeA);
assert.strictEqual(resolvedDirect, exeA, "Direct path with slashes should resolve to executable");
assert.throws(
() => which.sync(path.join(binA, "nonexistent-file")),
(err) => {
assert.strictEqual(err.code, "ENOENT");
return true;
}
);
}
// 7. findExecutableSync and tryFindExecutableSync helper functions correctly wrap which.sync behavior
{
const found = findExecutableSync("tool-alpha", { path: binA });
assert.strictEqual(found, exeA);
const all = findAllExecutablesSync("tool-alpha", { path: `${binA}:${binB}` });
assert.deepStrictEqual(all, [exeA, exeB]);
const safeNull = tryFindExecutableSync("non-existent", { path: binA });
assert.strictEqual(safeNull, null);
}
console.log("All which.sync contract assertions passed successfully.");
} finally {
fs.rmSync(tmpBase, { recursive: true, force: true });
}
原始种子者
匿名