CodeSampleX

Exemple

p-try 2.2.0: default

Échantillon vérifié pour npm p-try 2.2.0: default. Le contrat s'est exécuté sur node 22 · linux debian/x64 · docker et a réussi.

sha256:685604f87d8a0a5fd5839c59650dc29c20db2b288329ea251d2c60d3624bb35b

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 debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-06

Cas

HOW
Objectif
verify p-try.default in pkg:npm/p-try@2.2.0
Paquets
Symboles
  • p-try.default
Créé
2026-09-06T21:02:48Z

Contrat

  1. p-try.default executes a synchronous function and returns a Promise resolving to its return value
  2. p-try.default passes provided arguments to the function
  3. p-try.default catches synchronous errors and converts them to rejected promises
  4. p-try.default handles asynchronous functions and resolves or rejects according to the returned promise

Fichiers

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • 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 p-try.default in pkg:npm/p-try@2.2.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/p-try@2.2.0
Demonstrate these symbols/APIs:
  - p-try.default

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:1cf3a195610e5ad28b0a04321dc876a898f1641ee80456f9cf7b2be5f26d422f","contract":["p-try.default executes a synchronous function and returns a Promise resolving to its return value","p-try.default passes provided arguments to the function","p-try.default catches synchronous errors and converts them to rejected promises","p-try.default handles asynchronous functions and resolves or rejects according to the returned promise"],"goal":"verify p-try.default in pkg:npm/p-try@2.2.0","kind":"HOW","packages":["pkg:npm/p-try@2.2.0"],"schemaVersion":1,"symbols":["p-try.default"]},"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/p-try@2.2.0"],"schemaVersion":1,"subject":"pkg:npm/p-try@2.2.0","symbols":["p-try.default"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-p-try",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-p-try",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "p-try": "2.2.0"
      }
    },
    "node_modules/p-try": {
      "version": "2.2.0",
      "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
      "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==",
      "license": "MIT",
      "engines": {
        "node": ">=6"
      }
    }
  }
}
package.json
{
  "name": "sample-p-try",
  "version": "1.0.0",
  "private": true,
  "description": "verify p-try.default in pkg:npm/p-try@2.2.0",
  "main": "src/index.js",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "p-try": "2.2.0"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify p-try.default in pkg:npm/p-try@2.2.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/p-try@2.2.0"
  ],
  "symbols": [
    "p-try.default"
  ]
}
src/index.js
'use strict';

const pTry = require('p-try');

/**
 * Executes a function inside a promise chain using p-try.default.
 *
 * @param {Function} fn - Function to run.
 * @param {...any} args - Arguments to forward to fn.
 * @returns {Promise<any>} A promise resolving with the returned value or rejecting if an error is thrown.
 */
function runWithPTry(fn, ...args) {
  return pTry.default(fn, ...args);
}

module.exports = {
  pTry,
  runWithPTry
};
test/contract.mjs
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const pTryModule = require('p-try');
const { pTry, runWithPTry } = require('../src/index.js');

// 1. p-try.default executes a synchronous function and returns a Promise resolving to its return value
{
  assert.strictEqual(typeof pTryModule.default, 'function', 'pTryModule.default must be a function');
  assert.strictEqual(pTryModule.default, pTryModule, 'pTryModule.default must equal the exported pTry function');

  const syncResult = await pTryModule.default(() => 'sync-value');
  assert.strictEqual(syncResult, 'sync-value', 'pTryModule.default should resolve to synchronous return value');

  const wrapperResult = await runWithPTry(() => 42);
  assert.strictEqual(wrapperResult, 42, 'runWithPTry should resolve to return value');

  const promiseInstance = pTryModule.default(() => 'promise-instance-check');
  assert.ok(promiseInstance instanceof Promise, 'pTry.default must return a native Promise instance');
  await promiseInstance;
}

// 2. p-try.default passes provided arguments to the function
{
  const result = await pTryModule.default(
    (a, b, c) => `${a}-${b}-${c}`,
    'first',
    'second',
    'third'
  );
  assert.strictEqual(result, 'first-second-third', 'pTry.default should pass arguments in order');

  const wrapperResult = await runWithPTry((x, y) => x * y, 6, 7);
  assert.strictEqual(wrapperResult, 42, 'runWithPTry should forward arguments to target function');
}

// 3. p-try.default catches synchronous errors and converts them to rejected promises
{
  const expectedError = new Error('synchronous failure');
  await assert.rejects(
    async () => {
      await pTryModule.default(() => {
        throw expectedError;
      });
    },
    {
      name: 'Error',
      message: 'synchronous failure'
    },
    'pTry.default must convert thrown synchronous exceptions into promise rejections'
  );

  await assert.rejects(
    async () => {
      await runWithPTry(() => {
        throw new TypeError('invalid argument type');
      });
    },
    {
      name: 'TypeError',
      message: 'invalid argument type'
    },
    'runWithPTry must convert thrown synchronous exceptions into promise rejections'
  );
}

// 4. p-try.default handles asynchronous functions and resolves or rejects according to the returned promise
{
  const asyncResult = await pTryModule.default(async (num) => {
    return num * 10;
  }, 5);
  assert.strictEqual(asyncResult, 50, 'pTry.default should await and resolve the async promise');

  await assert.rejects(
    async () => {
      await pTryModule.default(async () => {
        throw new Error('async failure');
      });
    },
    {
      name: 'Error',
      message: 'async failure'
    },
    'pTry.default must reject when the returned promise rejects'
  );
}

console.log('All contract assertions passed successfully.');

Seeder d'origine

anonyme