CodeSampleX

Exemple

fdir 6.5.0: crawl directory with full paths synchronously and asynchronously

Échantillon vérifié pour npm fdir 6.5.0: crawl directory with full paths synchronously and asynchronously. Le contrat s'est exécuté sur node 22 · linux…

sha256:0c88980863efcd1c016cdd1841ccc5bc89791a85d509f1d1dc32f0662f8eb16c

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é node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

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

Cas

HOW
Objectif
crawl directory with full paths synchronously and asynchronously
Paquets
Symboles
  • fdir
  • fdir.crawl
  • fdir.filter
  • fdir.withFullPaths
Environnement
node 22.23.2
Créé
2026-08-26T07:30:41Z

Contrat

  1. assert fdir is a constructor function with crawl, filter, and withFullPaths methods
  2. assert fdir synchronously crawls a directory and returns all file paths with full paths
  3. assert fdir asynchronously crawls a directory with a Promise returning all file paths with full paths
  4. assert fdir applies custom filter predicate synchronously to match specific files
  5. assert fdir applies custom filter predicate asynchronously with a Promise
  6. assert fdir returns relative file paths when withFullPaths is not enabled

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: crawl directory with full paths synchronously and asynchronously
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/fdir@6.5.0
Demonstrate these symbols/APIs:
  - fdir
  - fdir.crawl
  - fdir.filter
  - fdir.withFullPaths

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:affa0945247bcf62e1ca12a98e51812bbafeda128f39ec64ac77a12ab2ac2eb7","contract":["assert fdir is a constructor function with crawl, filter, and withFullPaths methods","assert fdir synchronously crawls a directory and returns all file paths with full paths","assert fdir asynchronously crawls a directory with a Promise returning all file paths with full paths","assert fdir applies custom filter predicate synchronously to match specific files","assert fdir applies custom filter predicate asynchronously with a Promise","assert fdir returns relative file paths when withFullPaths is not enabled"],"goal":"crawl directory with full paths synchronously and asynchronously","kind":"HOW","packages":["pkg:npm/fdir@6.5.0"],"schemaVersion":1,"symbols":["fdir","fdir.crawl","fdir.filter","fdir.withFullPaths"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/fdir@6.5.0"],"schemaVersion":1,"subject":"pkg:npm/fdir@6.5.0","symbols":["fdir","fdir.crawl","fdir.filter","fdir.withFullPaths"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "csx-sample-fdir",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "csx-sample-fdir",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "fdir": "6.5.0"
      }
    },
    "node_modules/fdir": {
      "version": "6.5.0",
      "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz",
      "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==",
      "license": "MIT",
      "engines": {
        "node": ">=12.0.0"
      },
      "peerDependencies": {
        "picomatch": "^3 || ^4"
      },
      "peerDependenciesMeta": {
        "picomatch": {
          "optional": true
        }
      }
    }
  }
}
package.json
{
  "name": "csx-sample-fdir",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "fdir": "6.5.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "crawl directory with full paths synchronously and asynchronously",
  "kind": "HOW",
  "packages": [
    "pkg:npm/fdir@6.5.0"
  ],
  "symbols": [
    "fdir",
    "fdir.crawl",
    "fdir.filter",
    "fdir.withFullPaths"
  ]
}
src/index.mjs
import { fdir } from 'fdir';

export function crawlDirectorySync(dirPath, options = {}) {
  let crawler = new fdir();
  if (options.withFullPaths !== false) {
    crawler = crawler.withFullPaths();
  }
  if (typeof options.filter === 'function') {
    crawler = crawler.filter(options.filter);
  }
  return crawler.crawl(dirPath).sync();
}

export async function crawlDirectoryAsync(dirPath, options = {}) {
  let crawler = new fdir();
  if (options.withFullPaths !== false) {
    crawler = crawler.withFullPaths();
  }
  if (typeof options.filter === 'function') {
    crawler = crawler.filter(options.filter);
  }
  return await crawler.crawl(dirPath).withPromise();
}

export { fdir };
test/contract.mjs
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { fdir, crawlDirectorySync, crawlDirectoryAsync } from '../src/index.mjs';

const tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), 'csx-fdir-contract-'));
const dirAlpha = path.join(tmpBase, 'alpha');
const dirBeta = path.join(tmpBase, 'beta');
const dirNested = path.join(dirBeta, 'nested');

fs.mkdirSync(dirAlpha, { recursive: true });
fs.mkdirSync(dirNested, { recursive: true });

const file1 = path.join(dirAlpha, 'file1.txt');
const file2 = path.join(dirAlpha, 'file2.log');
const file3 = path.join(dirBeta, 'file3.txt');
const file4 = path.join(dirNested, 'file4.md');

fs.writeFileSync(file1, 'hello from file1', 'utf8');
fs.writeFileSync(file2, 'log content', 'utf8');
fs.writeFileSync(file3, 'hello from file3', 'utf8');
fs.writeFileSync(file4, 'markdown content', 'utf8');

try {
  // 1. Verify fdir exports and instance methods
  assert.equal(typeof fdir, 'function', 'fdir must be a constructor function');
  const instance = new fdir();
  assert.equal(typeof instance.crawl, 'function', 'fdir.prototype.crawl must be a function');
  assert.equal(typeof instance.filter, 'function', 'fdir.prototype.filter must be a function');
  assert.equal(typeof instance.withFullPaths, 'function', 'fdir.prototype.withFullPaths must be a function');

  // 2. Synchronous directory crawl with full paths
  const syncResults = crawlDirectorySync(tmpBase);
  assert.ok(Array.isArray(syncResults), 'crawlDirectorySync must return an array');
  assert.equal(syncResults.length, 4, 'crawlDirectorySync should find all 4 files');
  assert.ok(syncResults.includes(file1), 'syncResults must include file1 full path');
  assert.ok(syncResults.includes(file2), 'syncResults must include file2 full path');
  assert.ok(syncResults.includes(file3), 'syncResults must include file3 full path');
  assert.ok(syncResults.includes(file4), 'syncResults must include file4 full path');

  // 3. Asynchronous directory crawl with Promise and full paths
  const asyncResults = await crawlDirectoryAsync(tmpBase);
  assert.ok(Array.isArray(asyncResults), 'crawlDirectoryAsync must return an array');
  assert.equal(asyncResults.length, 4, 'crawlDirectoryAsync should find all 4 files');
  assert.ok(asyncResults.includes(file1), 'asyncResults must include file1 full path');
  assert.ok(asyncResults.includes(file2), 'asyncResults must include file2 full path');
  assert.ok(asyncResults.includes(file3), 'asyncResults must include file3 full path');
  assert.ok(asyncResults.includes(file4), 'asyncResults must include file4 full path');

  // 4. Synchronous directory crawl with custom filter
  const txtOnlySync = crawlDirectorySync(tmpBase, {
    filter: (filePath) => filePath.endsWith('.txt'),
  });
  assert.equal(txtOnlySync.length, 2, 'filter should only return 2 .txt files');
  assert.ok(txtOnlySync.includes(file1), 'txtOnlySync must include file1');
  assert.ok(txtOnlySync.includes(file3), 'txtOnlySync must include file3');
  assert.ok(!txtOnlySync.includes(file2), 'txtOnlySync must not include file2');
  assert.ok(!txtOnlySync.includes(file4), 'txtOnlySync must not include file4');

  // 5. Asynchronous directory crawl with custom filter
  const txtOnlyAsync = await crawlDirectoryAsync(tmpBase, {
    filter: (filePath) => filePath.endsWith('.txt'),
  });
  assert.equal(txtOnlyAsync.length, 2, 'async filter should only return 2 .txt files');
  assert.ok(txtOnlyAsync.includes(file1), 'txtOnlyAsync must include file1');
  assert.ok(txtOnlyAsync.includes(file3), 'txtOnlyAsync must include file3');

  // 6. Direct fdir instance crawling with relative paths vs full paths
  const relativeCrawler = new fdir().crawl(tmpBase);
  const relativeFiles = relativeCrawler.sync();
  assert.equal(relativeFiles.length, 4, 'relative crawl should find 4 files');
  assert.ok(!relativeFiles.includes(file1), 'relativeFiles should not contain absolute paths');

  console.log('CONTRACT PASS: fdir 6.5.0 verified successfully');
} finally {
  fs.rmSync(tmpBase, { recursive: true, force: true });
}

Seeder d'origine

anonyme