CodeSampleX

Exemple

resolve-from 4.0.0

Échantillon vérifié pour npm resolve-from 4.0.0. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a réussi.

sha256:79ed2be4d403a02cf7f1980e76ed964a76533064e1df32c36ccc8b89c0686361

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
verify pkg:npm/resolve-from@4.0.0
Paquets
Créé
2026-08-23T13:48:35Z

Contrat

  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

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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 });
}

Seeder d'origine

anonyme