CodeSampleX

샘플

resolve-from 4.0.0

검증된 샘플 — npm resolve-from 4.0.0. node 22 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다: resolveFrom resolves a relative file path from a specified…

sha256:79ed2be4d403a02cf7f1980e76ed964a76533064e1df32c36ccc8b89c0686361

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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 pkg:npm/resolve-from@4.0.0
패키지
생성일
2026-08-23T13:48:35Z

컨트랙트

  1. resolveFrom resolves a relative file path from a specified directory
  2. resolveFrom resolves an installed npm package from a specified directory
  3. resolveFrom throws MODULE_NOT_FOUND error when the target module does not exist
  4. resolveFrom throws TypeError when fromDirectory or moduleId is not a string
  5. resolveFrom.silent returns null instead of throwing when the target module does not exist
  6. resolveFrom.silent returns resolved absolute path when target exists
  7. resolveModuleFrom and tryResolveModuleFrom helper functions wrap resolveFrom and resolveFrom.silent

파일

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • 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/resolve-from@4.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/resolve-from@4.0.0

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:3817a5d8de81e46e67b5cf8f8913c25571c1d5283d5725c2e5cfb88a963f7985","contract":["resolveFrom resolves a relative file path from a specified directory","resolveFrom resolves an installed npm package from a specified directory","resolveFrom throws MODULE_NOT_FOUND error when the target module does not exist","resolveFrom throws TypeError when fromDirectory or moduleId is not a string","resolveFrom.silent returns null instead of throwing when the target module does not exist","resolveFrom.silent returns resolved absolute path when target exists","resolveModuleFrom and tryResolveModuleFrom helper functions wrap resolveFrom and resolveFrom.silent"],"goal":"verify pkg:npm/resolve-from@4.0.0","kind":"HOW","packages":["pkg:npm/resolve-from@4.0.0"],"schemaVersion":1},"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/resolve-from@4.0.0"],"schemaVersion":1,"subject":"pkg:npm/resolve-from@4.0.0","verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-resolve-from",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-resolve-from",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "resolve-from": "4.0.0"
      }
    },
    "node_modules/resolve-from": {
      "version": "4.0.0",
      "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz",
      "integrity": "sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==",
      "license": "MIT",
      "engines": {
        "node": ">=4"
      }
    }
  }
}
package.json
{
  "name": "sample-resolve-from",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for resolve-from@4.0.0",
  "main": "src/index.js",
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "resolve-from": "4.0.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/resolve-from@4.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/resolve-from@4.0.0"
  ]
}
src/index.mjs
import resolveFrom from 'resolve-from';

/**
 * Resolves the path of a module from a given directory.
 *
 * @param {string} fromDirectory - Directory to resolve from.
 * @param {string} moduleId - Target module ID or file path to resolve.
 * @returns {string} Absolute path to the resolved module.
 * @throws {TypeError} If fromDirectory or moduleId is not a string.
 * @throws {Error} If module cannot be resolved (code: 'MODULE_NOT_FOUND').
 */
export function resolveModuleFrom(fromDirectory, moduleId) {
  return resolveFrom(fromDirectory, moduleId);
}

/**
 * Safely resolves the path of a module from a given directory without throwing if not found.
 *
 * @param {string} fromDirectory - Directory to resolve from.
 * @param {string} moduleId - Target module ID or file path to resolve.
 * @returns {string|null} Absolute path to the resolved module, or null if resolution fails.
 */
export function tryResolveModuleFrom(fromDirectory, moduleId) {
  return resolveFrom.silent(fromDirectory, moduleId);
}

export { resolveFrom };
export default resolveFrom;
test/contract.mjs
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import resolveFrom, {
  resolveModuleFrom,
  tryResolveModuleFrom,
} from '../src/index.mjs';

const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'csx-resolve-from-contract-'));

try {
  // Set up mock directory structure
  const subDir = path.join(tmpDir, 'sub');
  const nodeModulesDir = path.join(tmpDir, 'node_modules', 'dummy-pkg');
  fs.mkdirSync(subDir, { recursive: true });
  fs.mkdirSync(nodeModulesDir, { recursive: true });

  const targetFile = path.join(subDir, 'target.js');
  const jsonFile = path.join(subDir, 'data.json');
  const pkgIndex = path.join(nodeModulesDir, 'index.js');
  const pkgJson = path.join(nodeModulesDir, 'package.json');

  fs.writeFileSync(targetFile, 'export const val = 42;\n');
  fs.writeFileSync(jsonFile, '{"key": "value"}\n');
  fs.writeFileSync(pkgIndex, 'module.exports = { name: "dummy" };\n');
  fs.writeFileSync(pkgJson, JSON.stringify({ name: 'dummy-pkg', main: 'index.js' }));

  // 1. resolveFrom resolves a relative file path from a specified directory
  {
    const resolved = resolveFrom(tmpDir, './sub/target.js');
    assert.strictEqual(fs.realpathSync(resolved), fs.realpathSync(targetFile));

    const resolvedWithoutExt = resolveFrom(tmpDir, './sub/target');
    assert.strictEqual(fs.realpathSync(resolvedWithoutExt), fs.realpathSync(targetFile));

    const resolvedFromSub = resolveFrom(subDir, './data.json');
    assert.strictEqual(fs.realpathSync(resolvedFromSub), fs.realpathSync(jsonFile));
  }

  // 2. resolveFrom resolves an installed npm package from a specified directory
  {
    const resolvedPkg = resolveFrom(tmpDir, 'dummy-pkg');
    assert.strictEqual(fs.realpathSync(resolvedPkg), fs.realpathSync(pkgIndex));
  }

  // 3. resolveFrom throws MODULE_NOT_FOUND error when the target module does not exist
  {
    assert.throws(
      () => resolveFrom(tmpDir, './sub/missing-file.js'),
      (err) => {
        assert.strictEqual(err.code, 'MODULE_NOT_FOUND');
        assert.ok(err.message.includes('missing-file.js'));
        return true;
      }
    );

    assert.throws(
      () => resolveFrom(tmpDir, 'non-existent-package-xyz'),
      (err) => {
        assert.strictEqual(err.code, 'MODULE_NOT_FOUND');
        return true;
      }
    );
  }

  // 4. resolveFrom throws TypeError when fromDirectory or moduleId is not a string
  {
    assert.throws(
      () => resolveFrom(null, './sub/target.js'),
      {
        name: 'TypeError',
        message: 'Expected `fromDir` to be of type `string`, got `object`'
      }
    );

    assert.throws(
      () => resolveFrom(123, './sub/target.js'),
      {
        name: 'TypeError',
        message: 'Expected `fromDir` to be of type `string`, got `number`'
      }
    );

    assert.throws(
      () => resolveFrom(tmpDir, null),
      {
        name: 'TypeError',
        message: 'Expected `moduleId` to be of type `string`, got `object`'
      }
    );

    assert.throws(
      () => resolveFrom(tmpDir, 456),
      {
        name: 'TypeError',
        message: 'Expected `moduleId` to be of type `string`, got `number`'
      }
    );
  }

  // 5. resolveFrom.silent returns null instead of throwing when the target module does not exist
  {
    const resultMissingFile = resolveFrom.silent(tmpDir, './sub/missing.js');
    assert.strictEqual(resultMissingFile, null);

    const resultMissingPkg = resolveFrom.silent(tmpDir, 'missing-package-abc');
    assert.strictEqual(resultMissingPkg, null);
  }

  // 6. resolveFrom.silent returns resolved absolute path when target exists
  {
    const result = resolveFrom.silent(tmpDir, './sub/target.js');
    assert.strictEqual(fs.realpathSync(result), fs.realpathSync(targetFile));

    const resultPkg = resolveFrom.silent(tmpDir, 'dummy-pkg');
    assert.strictEqual(fs.realpathSync(resultPkg), fs.realpathSync(pkgIndex));
  }

  // 7. resolveModuleFrom and tryResolveModuleFrom helper functions wrap resolveFrom and resolveFrom.silent
  {
    const resolved = resolveModuleFrom(tmpDir, './sub/target.js');
    assert.strictEqual(fs.realpathSync(resolved), fs.realpathSync(targetFile));

    const silentResolved = tryResolveModuleFrom(tmpDir, './sub/target.js');
    assert.strictEqual(fs.realpathSync(silentResolved), fs.realpathSync(targetFile));

    const silentMissing = tryResolveModuleFrom(tmpDir, './sub/missing.js');
    assert.strictEqual(silentMissing, null);
  }

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

오리진 시더

익명