CodeSampleX

示例

nanoid 3.3.17: random

已验证示例 — npm nanoid 3.3.17: random. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过: assert random is a function exported by nanoid

sha256:9887735978b5e89271cb7311eca90e1be9775c165e7401c2920c6c582514bea0

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

验证运行环境

环境 契约 阶段 运行日期
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-27

案例

HOW
目标
verify nanoid.random in pkg:npm/nanoid@3.3.17
包
符号
  • nanoid.random
环境
node 22.23.2
创建时间
2026-08-27T16:26:39Z

契约

  1. assert random is a function exported by nanoid
  2. assert random returns a Uint8Array of the requested byte length
  3. assert random returns an empty Uint8Array when requested length is 0
  4. assert random generates cryptographically distinct byte sequences across consecutive calls
  5. assert random outputs bytes spanning the full 0-255 uint8 range over multiple calls
  6. assert random functions as a compatible RNG source for customRandom

文件

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

下载源代码构件 (tar.gz)

源代码

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 nanoid.random in pkg:npm/nanoid@3.3.17
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/nanoid@3.3.17
Demonstrate these symbols/APIs:
  - nanoid.random

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:77f71e332ed0228a9a80763968f5e900f7342d7a16336530d77a99daee005ba5","contract":["assert random is a function exported by nanoid","assert random returns a Uint8Array of the requested byte length","assert random returns an empty Uint8Array when requested length is 0","assert random generates cryptographically distinct byte sequences across consecutive calls","assert random outputs bytes spanning the full 0-255 uint8 range over multiple calls","assert random functions as a compatible RNG source for customRandom"],"goal":"verify nanoid.random in pkg:npm/nanoid@3.3.17","kind":"HOW","packages":["pkg:npm/nanoid@3.3.17"],"schemaVersion":1,"symbols":["nanoid.random"]},"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/nanoid@3.3.17"],"schemaVersion":1,"subject":"pkg:npm/nanoid@3.3.17","symbols":["nanoid.random"],"verifierAdapter":"node-typescript@1"}
index.mjs
import { random, customRandom } from 'nanoid';

/**
 * Generate cryptographically secure random bytes of specified size.
 * @param {number} bytes
 * @returns {Uint8Array}
 */
export function generateRandomBytes(bytes) {
  return random(bytes);
}

/**
 * Create a custom ID generator backed by nanoid's random byte generator.
 * @param {string} alphabet
 * @param {number} size
 * @returns {() => string}
 */
export function createCustomIdGenerator(alphabet, size) {
  return customRandom(alphabet, size, random);
}

export { random };
package-lock.json
{
  "name": "sample-nanoid-random",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-nanoid-random",
      "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==",
      "license": "MIT",
      "bin": {
        "nanoid": "bin/nanoid.cjs"
      },
      "engines": {
        "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
      }
    }
  }
}
package.json
{
  "name": "sample-nanoid-random",
  "version": "1.0.0",
  "description": "Demonstrate cryptographic byte generation with nanoid.random in pkg:npm/nanoid@3.3.17",
  "main": "index.mjs",
  "type": "module",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "nanoid": "3.3.17"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify nanoid.random in pkg:npm/nanoid@3.3.17",
  "kind": "HOW",
  "packages": [
    "pkg:npm/nanoid@3.3.17"
  ],
  "symbols": [
    "nanoid.random"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import { random, customRandom } from 'nanoid';
import { generateRandomBytes, createCustomIdGenerator } from '../index.mjs';

// 1. assert random is a function exported by nanoid
{
  assert.equal(typeof random, 'function', 'random must be exported as a function');
  assert.equal(typeof generateRandomBytes, 'function', 'generateRandomBytes helper must be a function');
}

// 2. assert random returns a Uint8Array of the requested byte length
{
  const lengths = [1, 8, 16, 32, 64, 128];
  for (const len of lengths) {
    const bytes = random(len);
    assert.ok(bytes instanceof Uint8Array, 'random must return a Uint8Array');
    assert.equal(bytes.length, len, `random(${len}) must return Uint8Array with length ${len}`);
    assert.equal(bytes.byteLength, len, `random(${len}) byteLength must match length`);
  }
}

// 3. assert random returns an empty Uint8Array when requested length is 0
{
  const emptyBytes = random(0);
  assert.ok(emptyBytes instanceof Uint8Array, 'random(0) must return a Uint8Array');
  assert.equal(emptyBytes.length, 0, 'random(0) length must be 0');
}

// 4. assert random generates cryptographically distinct byte sequences across consecutive calls
{
  const samples = new Set();
  const iterations = 50;
  for (let i = 0; i < iterations; i++) {
    const bytes = random(16);
    const hex = Buffer.from(bytes).toString('hex');
    samples.add(hex);
  }
  assert.equal(samples.size, iterations, 'All consecutive random byte sequences of length 16 must be unique');
}

// 5. assert random outputs bytes spanning the full 0-255 uint8 range over multiple calls
{
  const seenBytes = new Set();
  // Drawing 1000 chunks of 32 bytes (32,000 bytes total) gives near-certain coverage of 0-255
  for (let i = 0; i < 1000; i++) {
    const chunk = random(32);
    for (let j = 0; j < chunk.length; j++) {
      seenBytes.add(chunk[j]);
    }
  }
  assert.equal(seenBytes.size, 256, 'random generator should cover all 256 byte values');
}

// 6. assert random functions as a compatible RNG source for customRandom
{
  const alphabet = '0123456789abcdef';
  const size = 16;
  const customGen = customRandom(alphabet, size, random);
  const id1 = customGen();
  const id2 = customGen();

  assert.equal(typeof id1, 'string', 'customRandom with random must generate a string');
  assert.equal(id1.length, size, 'Generated id length must match requested size');
  assert.notEqual(id1, id2, 'Consecutive generated IDs should be distinct');
  for (const ch of id1) {
    assert.ok(alphabet.includes(ch), `Character "${ch}" must belong to custom alphabet`);
  }

  const helperGen = createCustomIdGenerator('ABCDEF', 8);
  const helperId = helperGen();
  assert.equal(helperId.length, 8);
  for (const ch of helperId) {
    assert.ok('ABCDEF'.includes(ch));
  }
}

console.log('CONTRACT PASS: nanoid.random verified successfully');

原始种子者

匿名