CodeSampleX

Ejemplo

fs.realpath 1.0.0: realpathSync

Muestra verificada para npm fs.realpath 1.0.0: realpathSync. El contrato se ejecutó en node 22 · linux debian/x64 · docker y pasó.

sha256:05e450c6eaf3490d8c86ba2dda92b73e9022b3342803104d1af61469c7cdcbe3

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-04

Caso

HOW
Objetivo
verify fs.realpath.realpathSync in pkg:npm/fs.realpath@1.0.0
Paquetes
Símbolos
  • fs.realpath.realpathSync
Entorno
node 22.23.2
Creado
2026-09-04T10:03:30Z

Contrato

  1. fs.realpath.realpathSync resolves an existing canonical file to its absolute path
  2. fs.realpath.realpathSync resolves single and multi-hop symlinks to their ultimate target path
  3. fs.realpath.realpathSync accepts an optional cache mapping object
  4. fs.realpath.realpathSync throws an ENOENT error when target path does not exist
  5. fs.realpath.realpathSync is exported directly as realpathSync and aliased as sync on fs.realpath
  6. safeResolvePath returns canonical path for existing target and null for non-existent target

Archivos

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

Descargar el artefacto de código fuente (tar.gz)

Código fuente

.npmrc
audit=false
fund=false
update-notifier=false
NOTES.md
# CSX Sample Notes: fs.realpath.realpathSync canonical resolution and symlink handling

## 1. Search Result
`search_known_solution` returned `NO_SAFE_MATCH` for `verify fs.realpath.realpathSync in pkg:npm/fs.realpath@1.0.0`. Existing samples cover general `fs.realpath` verification, while this sample specifically verifies synchronous canonical resolution, multi-hop symlink traversal, cache handling, and error contracts for `fs.realpath.realpathSync`.

## 2. What a Model Would Have Written
A naive model would:
1. Use `import { realpathSync } from 'fs.realpath'`, which resolves to `undefined` in Node ESM because `fs.realpath` exports a function via `module.exports = realpath` rather than named ESM exports.
2. Hardcode platform-dependent absolute paths in assertions or test fixtures instead of dynamically creating isolated temporary directories.
3. Assume `realpathSync` returns null or undefined on non-existent paths instead of throwing `ENOENT`.

## 3. How the Wrong Version Fails
1. Direct named import `import { realpathSync } from 'fs.realpath'` fails with `TypeError: realpathSync is not a function`.
2. Hardcoding `/tmp` or fixed paths causes test failures on systems where `os.tmpdir()` points elsewhere or fails leakage detection.
3. Expecting non-existent paths to return `null` directly from `realpathSync` fails because Node throws an `ENOENT` error synchronously.
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 fs.realpath.realpathSync in pkg:npm/fs.realpath@1.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/fs.realpath@1.0.0
Demonstrate these symbols/APIs:
  - fs.realpath.realpathSync

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:4e78555f02298d722783d4bc495c2f9697611fd1b20bd0f6453d2578574c2b5f","constraints":{"executionContext":"node"},"contract":["fs.realpath.realpathSync resolves an existing canonical file to its absolute path","fs.realpath.realpathSync resolves single and multi-hop symlinks to their ultimate target path","fs.realpath.realpathSync accepts an optional cache mapping object","fs.realpath.realpathSync throws an ENOENT error when target path does not exist","fs.realpath.realpathSync is exported directly as realpathSync and aliased as sync on fs.realpath","safeResolvePath returns canonical path for existing target and null for non-existent target"],"goal":"verify fs.realpath.realpathSync in pkg:npm/fs.realpath@1.0.0","kind":"HOW","packages":["pkg:npm/fs.realpath@1.0.0"],"schemaVersion":1,"symbols":["fs.realpath.realpathSync"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/fs.realpath@1.0.0"],"schemaVersion":1,"subject":"pkg:npm/fs.realpath@1.0.0","symbols":["fs.realpath.realpathSync"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-fs-realpath-sync",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-fs-realpath-sync",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "fs.realpath": "1.0.0"
      }
    },
    "node_modules/fs.realpath": {
      "version": "1.0.0",
      "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
      "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==",
      "license": "ISC"
    }
  }
}
package.json
{
  "name": "sample-fs-realpath-sync",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "fs.realpath": "1.0.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify fs.realpath.realpathSync in pkg:npm/fs.realpath@1.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/fs.realpath@1.0.0"
  ],
  "symbols": [
    "fs.realpath.realpathSync"
  ]
}
src/index.mjs
import realpathPkg from 'fs.realpath';

const { realpathSync, sync } = realpathPkg;

/**
 * Resolves a given path to its canonical absolute path synchronously.
 *
 * @param {string} targetPath - Path to resolve.
 * @param {object} [cache] - Optional cache object.
 * @returns {string} The canonical absolute path.
 */
export function resolveCanonicalPath(targetPath, cache) {
  return realpathSync(targetPath, cache);
}

/**
 * Resolves a path using the sync alias.
 *
 * @param {string} targetPath - Path to resolve.
 * @param {object} [cache] - Optional cache object.
 * @returns {string} The canonical absolute path.
 */
export function resolveUsingSyncAlias(targetPath, cache) {
  return sync(targetPath, cache);
}

/**
 * Safely resolves a path, returning null if ENOENT is thrown.
 *
 * @param {string} targetPath - Path to resolve.
 * @returns {string|null} The canonical path, or null if non-existent.
 */
export function safeResolvePath(targetPath) {
  try {
    return realpathSync(targetPath);
  } catch (err) {
    if (err && err.code === 'ENOENT') {
      return null;
    }
    throw err;
  }
}
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 realpathPkg from 'fs.realpath';
import {
  resolveCanonicalPath,
  resolveUsingSyncAlias,
  safeResolvePath
} from '../src/index.mjs';

const { realpathSync, sync } = realpathPkg;

const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'csx-fs-realpath-sync-'));

try {
  const targetFile = path.join(tmpDir, 'target.txt');
  fs.writeFileSync(targetFile, 'content');
  const linkFile1 = path.join(tmpDir, 'link1.txt');
  fs.symlinkSync(targetFile, linkFile1);
  const linkFile2 = path.join(tmpDir, 'link2.txt');
  fs.symlinkSync(linkFile1, linkFile2);

  const subDir = path.join(tmpDir, 'nested');
  fs.mkdirSync(subDir);
  const subFile = path.join(subDir, 'inner.txt');
  fs.writeFileSync(subFile, 'inner-content');
  const subLink = path.join(tmpDir, 'nested-link');
  fs.symlinkSync(subDir, subLink);

  // 1. fs.realpath.realpathSync resolves an existing canonical file to its absolute path
  {
    const resolved = realpathSync(targetFile);
    assert.equal(typeof resolved, 'string');
    assert.equal(resolved, fs.realpathSync(targetFile));
    assert.equal(resolveCanonicalPath(targetFile), resolved);
  }

  // 2. fs.realpath.realpathSync resolves single and multi-hop symlinks to their ultimate target path
  {
    const resolvedLink1 = realpathSync(linkFile1);
    const resolvedLink2 = realpathSync(linkFile2);
    const resolvedTarget = realpathSync(targetFile);
    assert.equal(resolvedLink1, resolvedTarget);
    assert.equal(resolvedLink2, resolvedTarget);

    const resolvedNested = realpathSync(path.join(subLink, 'inner.txt'));
    assert.equal(resolvedNested, realpathSync(subFile));
  }

  // 3. fs.realpath.realpathSync accepts an optional cache mapping object
  {
    const cache = {};
    const resolvedWithCache = realpathSync(targetFile, cache);
    assert.equal(resolvedWithCache, realpathSync(targetFile));
    assert.equal(resolveCanonicalPath(targetFile, cache), resolvedWithCache);
  }

  // 4. fs.realpath.realpathSync throws an ENOENT error when target path does not exist
  {
    const nonExistent = path.join(tmpDir, 'non-existent-file.txt');
    assert.throws(
      () => {
        realpathSync(nonExistent);
      },
      (err) => {
        return err && err.code === 'ENOENT';
      }
    );
  }

  // 5. fs.realpath.realpathSync is exported directly as realpathSync and aliased as sync on fs.realpath
  {
    assert.equal(typeof realpathPkg.realpathSync, 'function');
    assert.equal(typeof realpathPkg.sync, 'function');
    assert.equal(realpathPkg.realpathSync, realpathPkg.sync);
    assert.equal(sync(targetFile), realpathSync(targetFile));
    assert.equal(resolveUsingSyncAlias(targetFile), realpathSync(targetFile));
  }

  // 6. safeResolvePath returns canonical path for existing target and null for non-existent target
  {
    const resolved = safeResolvePath(targetFile);
    assert.equal(resolved, realpathSync(targetFile));

    const nonExistent = path.join(tmpDir, 'does-not-exist.txt');
    const resultNonExistent = safeResolvePath(nonExistent);
    assert.equal(resultNonExistent, null);
  }
} finally {
  fs.rmSync(tmpDir, { recursive: true, force: true });
}

Seeder de origen

anónimo