CodeSampleX

示例

find-up 4.1.0: sync

已验证示例 — npm find-up 4.1.0: sync. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: assert findUp.sync returns the absolute path when target file exists…

sha256:6ca8a38c92a7c066ed100f1afd9c433bceae86b2e83f673f90f589d650bdce6c

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

案例

HOW
目标
verify find-up.sync in pkg:npm/find-up@4.1.0
包
符号
  • find-up.sync
环境
node 22.23.2
创建时间
2026-08-30T17:13:25Z

契约

  1. assert findUp.sync returns the absolute path when target file exists in the start directory
  2. assert findUp.sync walks up parent directories to find target file
  3. assert findUp.sync returns the first matching item when given an array of target filenames
  4. assert findUp.sync returns undefined when target file is not found in start or parent directories
  5. assert findUp.sync supports a custom synchronous matcher function to locate target path
  6. assert findUp.sync halts directory traversal when matcher returns findUp.stop

文件

  • PROMPT.md
  • csx.json
  • index.js
  • 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 find-up.sync in pkg:npm/find-up@4.1.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/find-up@4.1.0
Demonstrate these symbols/APIs:
  - find-up.sync
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:d5d7b94c4dbf641f6ce9d13b63f96f986f23335026b206f8fbb6133e317d6bc2","constraints":{"executionContext":"node"},"contract":["assert findUp.sync returns the absolute path when target file exists in the start directory","assert findUp.sync walks up parent directories to find target file","assert findUp.sync returns the first matching item when given an array of target filenames","assert findUp.sync returns undefined when target file is not found in start or parent directories","assert findUp.sync supports a custom synchronous matcher function to locate target path","assert findUp.sync halts directory traversal when matcher returns findUp.stop"],"goal":"verify find-up.sync in pkg:npm/find-up@4.1.0","kind":"HOW","packages":["pkg:npm/find-up@4.1.0"],"schemaVersion":1,"symbols":["find-up.sync"]},"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/find-up@4.1.0"],"schemaVersion":1,"subject":"pkg:npm/find-up@4.1.0","symbols":["find-up.sync"],"verifierAdapter":"node-typescript@1"}
index.js
const findUp = require('find-up');

/**
 * Synchronously finds a file or directory by walking up parent directories.
 *
 * @param {string|string[]} name - Filename or array of filenames to search for.
 * @param {object} [options] - Options passed to findUp.sync.
 * @param {string} [options.cwd] - Directory to start looking from.
 * @param {string} [options.type] - The type of path to match ('file' or 'directory').
 * @returns {string|undefined} First path found or undefined if not found.
 */
function findFileSync(name, options = {}) {
  return findUp.sync(name, options);
}

/**
 * Synchronously finds a file or directory using a custom matcher function.
 *
 * @param {Function} matcher - Function called with each directory that returns a file path or findUp.stop.
 * @param {object} [options] - Options passed to findUp.sync.
 * @param {string} [options.cwd] - Directory to start looking from.
 * @returns {string|undefined} First path found or undefined if not found.
 */
function findWithMatcherSync(matcher, options = {}) {
  return findUp.sync(matcher, options);
}

module.exports = {
  findFileSync,
  findWithMatcherSync,
  findUp,
};
package-lock.json
{
  "name": "sample-2997937507",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-2997937507",
      "version": "1.0.0",
      "license": "ISC",
      "dependencies": {
        "find-up": "^4.1.0"
      }
    },
    "node_modules/find-up": {
      "version": "4.1.0",
      "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz",
      "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==",
      "license": "MIT",
      "dependencies": {
        "locate-path": "^5.0.0",
        "path-exists": "^4.0.0"
      },
      "engines": {
        "node": ">=8"
      }
    },
    "node_modules/locate-path": {
      "version": "5.0.0",
      "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz",
      "integrity": "sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==",
      "license": "MIT",
      "dependencies": {
        "p-locate": "^4.1.0"
      },
      "engines": {
        "node": ">=8"
      }
    },
    "node_modules/p-limit": {
      "version": "2.3.0",
      "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",
      "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==",
      "license": "MIT",
      "dependencies": {
        "p-try": "^2.0.0"
      },
      "engines": {
        "node": ">=6"
      },
      "funding": {
        "url": "https://github.com/sponsors/sindresorhus"
      }
    },
    "node_modules/p-locate": {
      "version": "4.1.0",
      "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz",
      "integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==",
      "license": "MIT",
      "dependencies": {
        "p-limit": "^2.2.0"
      },
      "engines": {
        "node": ">=8"
      }
    },
    "node_modules/p-try": {
      "version": "2.2.0",
      "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
      "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==",
      "license": "MIT",
      "engines": {
        "node": ">=6"
      }
    },
    "node_modules/path-exists": {
      "version": "4.0.0",
      "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
      "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==",
      "license": "MIT",
      "engines": {
        "node": ">=8"
      }
    }
  }
}
package.json
{
  "name": "sample-find-up-sync",
  "version": "1.0.0",
  "description": "Clean-room sample verifying find-up.sync functionality",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "find-up": "4.1.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify find-up.sync in pkg:npm/find-up@4.1.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/find-up@4.1.0"
  ],
  "symbols": [
    "find-up.sync"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.mjs
import assert from 'node:assert';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import findUp from 'find-up';
import { findFileSync, findWithMatcherSync } from '../index.js';

// Setup isolated temporary directory structure for offline verification
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'findup-sync-contract-'));
const level1Dir = path.join(tmpDir, 'level1');
const level2Dir = path.join(level1Dir, 'level2');
fs.mkdirSync(level2Dir, { recursive: true });

const rootFile = path.join(tmpDir, 'root-config.json');
const level1File = path.join(level1Dir, 'level1-config.json');
const level2File = path.join(level2Dir, 'level2-config.json');

fs.writeFileSync(rootFile, '{"root": true}', 'utf8');
fs.writeFileSync(level1File, '{"level1": true}', 'utf8');
fs.writeFileSync(level2File, '{"level2": true}', 'utf8');

try {
  // 1. Find file in start directory synchronously
  const directLevel2 = findUp.sync('level2-config.json', { cwd: level2Dir });
  assert.strictEqual(directLevel2, level2File, 'findUp.sync must locate file in cwd');

  const wrapperLevel2 = findFileSync('level2-config.json', { cwd: level2Dir });
  assert.strictEqual(wrapperLevel2, level2File, 'findFileSync must locate file in cwd');

  // 2. Find file in parent directory by walking upwards
  const directLevel1 = findUp.sync('level1-config.json', { cwd: level2Dir });
  assert.strictEqual(directLevel1, level1File, 'findUp.sync must walk up to parent directory');

  const directRoot = findUp.sync('root-config.json', { cwd: level2Dir });
  assert.strictEqual(directRoot, rootFile, 'findUp.sync must walk up multiple parent levels');

  // 3. Find with array of filenames (first matching)
  const arrayMatch = findUp.sync(['nonexistent.json', 'level1-config.json', 'root-config.json'], { cwd: level2Dir });
  assert.strictEqual(arrayMatch, level1File, 'findUp.sync with array must find the first existing match traversing upwards');

  // 4. Return undefined when file is not found anywhere in ancestor tree
  const notFound = findUp.sync('definitely-does-not-exist.json', { cwd: level2Dir });
  assert.strictEqual(notFound, undefined, 'findUp.sync must return undefined when file does not exist');

  // 5. Custom synchronous matcher function
  const matcherFound = findWithMatcherSync(dir => {
    const candidate = path.join(dir, 'root-config.json');
    return fs.existsSync(candidate) ? candidate : undefined;
  }, { cwd: level2Dir });
  assert.strictEqual(matcherFound, rootFile, 'findWithMatcherSync must support custom directory matcher');

  // 6. Halt traversal when matcher returns findUp.stop
  let visitedCount = 0;
  const stoppedResult = findUp.sync(dir => {
    visitedCount += 1;
    return findUp.stop;
  }, { cwd: level2Dir });
  assert.strictEqual(stoppedResult, undefined, 'findUp.sync must return undefined when findUp.stop is returned');
  assert.strictEqual(visitedCount, 1, 'findUp.sync must stop traversal immediately when findUp.stop is returned');

  console.log('All contract assertions passed successfully.');
} finally {
  fs.rmSync(tmpDir, { recursive: true, force: true });
}

原始种子者

匿名