CodeSampleX

Exemple

nanoid 3.3.17: customAlphabet, customRandom, urlAlphabet

Échantillon vérifié pour npm nanoid 3.3.17: customAlphabet, customRandom, urlAlphabet. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a…

sha256:696ce4f6dac6dc462be5dcc5a51132515eaafc97decd3088eb4383386b99956e

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

Cas

HOW
Objectif
verify pkg:npm/nanoid@3.3.17
Paquets
Symboles
  • nanoid
  • customAlphabet
  • customRandom
  • urlAlphabet
Environnement
node 22.23.2
Créé
2026-08-23T18:27:07Z

Contrat

  1. require nanoid, customAlphabet, customRandom, and urlAlphabet in CommonJS
  2. assert default nanoid produces 21 URL-safe characters
  3. assert nanoid with custom length produces specified character count
  4. assert customAlphabet produces IDs using only the custom character set and length
  5. assert customRandom with explicit byte generator produces matching ID
  6. assert 1000 generated IDs are unique

Fichiers

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

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/nanoid@3.3.17
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/nanoid@3.3.17
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:4ae2db15894d8bc017f48434b1f288117f8f93109b2444890ff70b5123ebbb7b","constraints":{"executionContext":"node"},"contract":["require nanoid, customAlphabet, customRandom, and urlAlphabet in CommonJS","assert default nanoid produces 21 URL-safe characters","assert nanoid with custom length produces specified character count","assert customAlphabet produces IDs using only the custom character set and length","assert customRandom with explicit byte generator produces matching ID","assert 1000 generated IDs are unique"],"goal":"verify pkg:npm/nanoid@3.3.17","kind":"HOW","packages":["pkg:npm/nanoid@3.3.17"],"schemaVersion":1,"symbols":["nanoid","customAlphabet","customRandom","urlAlphabet"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/nanoid@3.3.17"],"schemaVersion":1,"subject":"pkg:npm/nanoid@3.3.17","symbols":["nanoid","customAlphabet","customRandom","urlAlphabet"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "csx-sample-nanoid-cjs",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "csx-sample-nanoid-cjs",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "nanoid": "3.3.17"
      }
    },
    "node_modules/nanoid": {
      "version": "3.3.17",
      "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.17.tgz",
      "integrity": "sha512-xQLf0A3HOMlgHq0n247/LRuAOYmB7dXJ/DvAxGvsSBij45XtBSmQycu+F8ODbHwns/XyFZagyL1+J0Offw1E0g==",
      "funding": [
        {
          "type": "github",
          "url": "https://github.com/sponsors/ai"
        }
      ],
      "license": "MIT",
      "bin": {
        "nanoid": "bin/nanoid.cjs"
      },
      "engines": {
        "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
      }
    }
  }
}
package.json
{
  "name": "csx-sample-nanoid-cjs",
  "version": "1.0.0",
  "private": true,
  "license": "MIT-0",
  "dependencies": {
    "nanoid": "3.3.17"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/nanoid@3.3.17",
  "kind": "HOW",
  "packages": [
    "pkg:npm/nanoid@3.3.17"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
const { nanoid, customAlphabet, customRandom, urlAlphabet, random } = require('nanoid');

function generateId(size) {
  return size ? nanoid(size) : nanoid();
}

function createCustomGenerator(alphabet, size) {
  return customAlphabet(alphabet, size);
}

function createCustomRandomGenerator(alphabet, size, randomFn) {
  return customRandom(alphabet, size, randomFn || random);
}

module.exports = {
  generateId,
  createCustomGenerator,
  createCustomRandomGenerator,
  urlAlphabet,
  nanoid,
  customAlphabet,
  customRandom,
  random
};
test/contract.js
const assert = require('node:assert/strict');
const crypto = require('node:crypto');
const {
  generateId,
  createCustomGenerator,
  createCustomRandomGenerator,
  urlAlphabet,
  nanoid,
  customAlphabet,
  customRandom
} = require('../src/index.js');

// 1. CommonJS exports are available
assert.equal(typeof nanoid, 'function', 'nanoid should be a function');
assert.equal(typeof customAlphabet, 'function', 'customAlphabet should be a function');
assert.equal(typeof customRandom, 'function', 'customRandom should be a function');
assert.equal(typeof urlAlphabet, 'string', 'urlAlphabet should be a string');
assert.equal(urlAlphabet.length, 64, 'urlAlphabet should contain 64 characters');

// 2. Default ID generation
const defaultId = generateId();
assert.equal(defaultId.length, 21, 'default nanoid should have length 21');
assert.match(defaultId, /^[A-Za-z0-9_-]{21}$/, 'default nanoid should only contain URL-safe chars');

// 3. Custom length generation
const customLengthId = generateId(32);
assert.equal(customLengthId.length, 32, 'nanoid(32) should have length 32');
assert.match(customLengthId, /^[A-Za-z0-9_-]{32}$/, 'nanoid(32) should only contain URL-safe chars');

// 4. Custom alphabet generator
const hexGenerator = createCustomGenerator('0123456789abcdef', 16);
const hexId = hexGenerator();
assert.equal(hexId.length, 16, 'customAlphabet id should have length 16');
assert.match(hexId, /^[0-9a-f]{16}$/, 'customAlphabet id should only contain hex chars');

// 5. Custom random generator with explicit crypto random bytes
const customRng = (bytes) => crypto.randomBytes(bytes);
const secureGen = createCustomRandomGenerator('ABCDEF123456', 12, customRng);
const secureId = secureGen();
assert.equal(secureId.length, 12, 'customRandom id should have length 12');
assert.match(secureId, /^[A-F1-6]{12}$/, 'customRandom id should match alphabet');

// 6. Uniqueness test across 1000 IDs
const seen = new Set();
for (let i = 0; i < 1000; i++) {
  seen.add(generateId());
}
assert.equal(seen.size, 1000, '1000 generated nanoids should be unique');

console.log('CONTRACT PASS: nanoid 3.3.17 CommonJS API verified successfully');

Seeder d'origine

anonyme