CodeSampleX

Ejemplo

graceful-fs 4.2.11: readFileSync

Muestra verificada para npm graceful-fs 4.2.11: readFileSync. El contrato se ejecutó en node 22 · linux debian/x64 · docker y pasó.

sha256:06af6fec3859f0d3813c9934ffa53bc26e6a123c12c18607b2367c40f57f696c

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 linux 24 · ubuntu · glibc 2.39 x64 npm

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-05

Caso

HOW
Objetivo
verify graceful-fs.readFileSync in pkg:npm/graceful-fs@4.2.11
Paquetes
Símbolos
  • graceful-fs.readFileSync
Creado
2026-09-05T19:04:52Z

Contrato

  1. graceful-fs.readFileSync(path, 'utf8') reads file content synchronously and returns a UTF-8 string
  2. graceful-fs.readFileSync(path) without encoding options returns a Buffer containing raw file bytes
  3. graceful-fs.readFileSync(path, { encoding: 'base64' }) returns file content as a base64 encoded string
  4. graceful-fs.readFileSync(fd) reads file content synchronously from an open file descriptor
  5. graceful-fs.readFileSync(nonExistentPath) throws an ENOENT error when target file does not exist

Archivos

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

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

Código fuente

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 graceful-fs.readFileSync in pkg:npm/graceful-fs@4.2.11
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/graceful-fs@4.2.11
Demonstrate these symbols/APIs:
  - graceful-fs.readFileSync

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:bea664fc689f0298b82d8684beed7f2e31b5ec8082d3db6da849343ea7171dea","contract":["graceful-fs.readFileSync(path, 'utf8') reads file content synchronously and returns a UTF-8 string","graceful-fs.readFileSync(path) without encoding options returns a Buffer containing raw file bytes","graceful-fs.readFileSync(path, { encoding: 'base64' }) returns file content as a base64 encoded string","graceful-fs.readFileSync(fd) reads file content synchronously from an open file descriptor","graceful-fs.readFileSync(nonExistentPath) throws an ENOENT error when target file does not exist"],"goal":"verify graceful-fs.readFileSync in pkg:npm/graceful-fs@4.2.11","kind":"HOW","packages":["pkg:npm/graceful-fs@4.2.11"],"schemaVersion":1,"symbols":["graceful-fs.readFileSync"]},"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/graceful-fs@4.2.11"],"schemaVersion":1,"subject":"pkg:npm/graceful-fs@4.2.11","symbols":["graceful-fs.readFileSync"],"verifierAdapter":"node-typescript@1"}
index.mjs
import gracefulFs from 'graceful-fs';

/**
 * Reads the entire contents of a file synchronously as a UTF-8 string.
 *
 * @param {string|Buffer|URL|number} pathOrFd - Path to file or file descriptor.
 * @returns {string} File contents decoded as UTF-8 string.
 */
export function readTextFile(pathOrFd) {
  return gracefulFs.readFileSync(pathOrFd, 'utf8');
}

/**
 * Reads the entire contents of a file synchronously as a raw Buffer.
 *
 * @param {string|Buffer|URL|number} pathOrFd - Path to file or file descriptor.
 * @returns {Buffer} File contents as raw byte Buffer.
 */
export function readBinaryFile(pathOrFd) {
  return gracefulFs.readFileSync(pathOrFd);
}

/**
 * Reads the contents of a file synchronously with custom options or encoding.
 *
 * @param {string|Buffer|URL|number} pathOrFd - Path to file or file descriptor.
 * @param {string|Object} options - Encoding string or options object.
 * @returns {string|Buffer} File contents.
 */
export function readFileWithOptions(pathOrFd, options) {
  return gracefulFs.readFileSync(pathOrFd, options);
}

export default gracefulFs;
package-lock.json
{
  "name": "sample-graceful-fs-readfilesync",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-graceful-fs-readfilesync",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "graceful-fs": "4.2.11"
      }
    },
    "node_modules/graceful-fs": {
      "version": "4.2.11",
      "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz",
      "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==",
      "license": "ISC"
    }
  }
}
package.json
{
  "name": "sample-graceful-fs-readfilesync",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "license": "MIT-0",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "graceful-fs": "4.2.11"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify graceful-fs.readFileSync in pkg:npm/graceful-fs@4.2.11",
  "kind": "HOW",
  "packages": [
    "pkg:npm/graceful-fs@4.2.11"
  ],
  "symbols": [
    "graceful-fs.readFileSync"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import gracefulFs from 'graceful-fs';
import { readTextFile, readBinaryFile, readFileWithOptions } from '../index.mjs';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const fixturePath = path.join(__dirname, 'fixture.txt');
const expectedContent = 'Hello CodeSampleX graceful-fs verification!\nLine 2 for multi-line test.\n';
const expectedBuffer = Buffer.from(expectedContent, 'utf8');

// 1. Verify graceful-fs.readFileSync(path, 'utf8') reads file content synchronously and returns a UTF-8 string
const strDirect = gracefulFs.readFileSync(fixturePath, 'utf8');
assert.equal(typeof strDirect, 'string');
assert.equal(strDirect, expectedContent);

const strHelper = readTextFile(fixturePath);
assert.equal(strHelper, expectedContent);

// 2. Verify graceful-fs.readFileSync(path) without encoding options returns a Buffer containing raw file bytes
const bufDirect = gracefulFs.readFileSync(fixturePath);
assert.ok(Buffer.isBuffer(bufDirect));
assert.ok(bufDirect.equals(expectedBuffer));

const bufHelper = readBinaryFile(fixturePath);
assert.ok(Buffer.isBuffer(bufHelper));
assert.ok(bufHelper.equals(expectedBuffer));

// 3. Verify graceful-fs.readFileSync(path, { encoding: 'base64' }) returns file content as a base64 encoded string
const b64Direct = gracefulFs.readFileSync(fixturePath, { encoding: 'base64' });
assert.equal(typeof b64Direct, 'string');
assert.equal(b64Direct, expectedBuffer.toString('base64'));

const b64Helper = readFileWithOptions(fixturePath, { encoding: 'base64' });
assert.equal(b64Helper, expectedBuffer.toString('base64'));

// 4. Verify graceful-fs.readFileSync(fd) reads file content synchronously from an open file descriptor
const fd1 = gracefulFs.openSync(fixturePath, 'r');
try {
  const fdBuf = gracefulFs.readFileSync(fd1);
  assert.ok(Buffer.isBuffer(fdBuf));
  assert.ok(fdBuf.equals(expectedBuffer));
} finally {
  gracefulFs.closeSync(fd1);
}

const fd2 = gracefulFs.openSync(fixturePath, 'r');
try {
  const fdStr = readTextFile(fd2);
  assert.equal(typeof fdStr, 'string');
  assert.equal(fdStr, expectedContent);
} finally {
  gracefulFs.closeSync(fd2);
}

// 5. Verify graceful-fs.readFileSync(nonExistentPath) throws an ENOENT error when target file does not exist
const nonExistentPath = path.join(__dirname, 'non_existent_fixture_12345.txt');
assert.throws(
  () => gracefulFs.readFileSync(nonExistentPath),
  (err) => {
    assert.equal(err.code, 'ENOENT');
    return true;
  }
);
assert.throws(
  () => readTextFile(nonExistentPath),
  (err) => {
    assert.equal(err.code, 'ENOENT');
    return true;
  }
);

console.log('Contract assertions passed successfully.');
test/fixture.txt
Hello CodeSampleX graceful-fs verification!
Line 2 for multi-line test.

Seeder de origen

anónimo