CodeSampleX

サンプル

nanoid 5.1.16: urlAlphabet

検証済みサンプル — npm nanoid 5.1.16: urlAlphabet. node 22 · linux debian/x64 · docker で contract を実行し、成功しました: urlAlphabet is an exported 64-character string…

sha256:ae5b17892d50a6ef5c6ae48844fb968e72c2bafa0a62a6fe367d4eb24b87715f

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

検証実行環境

環境 コントラクト ステージ 実行日
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

ケース

HOW
ゴール
verify nanoid.urlAlphabet in pkg:npm/nanoid@5.1.16
パッケージ
シンボル
  • nanoid.urlAlphabet
環境
node 22.23.2
作成日
2026-09-06T19:12:42Z

コントラクト

  1. urlAlphabet is an exported 64-character string containing URL-safe characters
  2. urlAlphabet contains 64 unique characters with numbers, lowercase letters, uppercase letters, hyphen, and underscore
  3. urlAlphabet characters all conform to URL-safe ASCII character set
  4. urlAlphabet is exported from the root nanoid package as an ESM named export
  5. urlAlphabet can be used with customAlphabet to generate URL-safe identifiers

ファイル

  • PROMPT.md
  • csx.json
  • index.js
  • 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.urlAlphabet in pkg:npm/nanoid@5.1.16
Kind: HOW

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

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:e35ecb1826688c99ed16e6e31b785050d0b0752eb1c5d2ba5fb672779580de77","contract":["urlAlphabet is an exported 64-character string containing URL-safe characters","urlAlphabet contains 64 unique characters with numbers, lowercase letters, uppercase letters, hyphen, and underscore","urlAlphabet characters all conform to URL-safe ASCII character set","urlAlphabet is exported from the root nanoid package as an ESM named export","urlAlphabet can be used with customAlphabet to generate URL-safe identifiers"],"goal":"verify nanoid.urlAlphabet in pkg:npm/nanoid@5.1.16","kind":"HOW","packages":["pkg:npm/nanoid@5.1.16"],"schemaVersion":1,"symbols":["nanoid.urlAlphabet"]},"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@5.1.16"],"schemaVersion":1,"subject":"pkg:npm/nanoid@5.1.16","symbols":["nanoid.urlAlphabet"],"verifierAdapter":"node-typescript@1"}
index.js
import { urlAlphabet } from 'nanoid';

/**
 * Returns the default URL-safe alphabet string used by Nano ID.
 * @returns {string} The 64-character URL-safe alphabet.
 */
export function getUrlAlphabet() {
  return urlAlphabet;
}

/**
 * Validates whether all characters in a given string belong to nanoid.urlAlphabet.
 * @param {string} str - String to validate.
 * @returns {boolean} True if all characters are in urlAlphabet, false otherwise.
 */
export function isUrlSafeString(str) {
  if (typeof str !== 'string') {
    return false;
  }
  for (let i = 0; i < str.length; i++) {
    if (!urlAlphabet.includes(str[i])) {
      return false;
    }
  }
  return true;
}

export { urlAlphabet };
package-lock.json
{
  "name": "sample-nanoid-urlalphabet",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-nanoid-urlalphabet",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "nanoid": "5.1.16"
      }
    },
    "node_modules/nanoid": {
      "version": "5.1.16",
      "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-5.1.16.tgz",
      "integrity": "sha512-kVrnsrJqMR8+oLJnGEmSWw9BivK5mt7H3FZatVRjrc5wGqFYuBxX1yG7+A7Gi5AefkX6t/oCkizcQgpu0cY1dQ==",
      "funding": [
        {
          "type": "github",
          "url": "https://github.com/sponsors/ai"
        }
      ],
      "license": "MIT",
      "bin": {
        "nanoid": "bin/nanoid.js"
      },
      "engines": {
        "node": "^18 || >=20"
      }
    }
  }
}
package.json
{
  "name": "sample-nanoid-urlalphabet",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "description": "verify nanoid.urlAlphabet in pkg:npm/nanoid@5.1.16",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "nanoid": "5.1.16"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify nanoid.urlAlphabet in pkg:npm/nanoid@5.1.16",
  "kind": "HOW",
  "packages": [
    "pkg:npm/nanoid@5.1.16"
  ],
  "symbols": [
    "nanoid.urlAlphabet"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import { urlAlphabet as rootAlphabet, customAlphabet } from 'nanoid';
import { urlAlphabet, getUrlAlphabet, isUrlSafeString } from '../index.js';

function testAlphabetStructure() {
  // 1. urlAlphabet is an exported 64-character string containing URL-safe characters
  assert.strictEqual(typeof urlAlphabet, 'string', 'urlAlphabet must be a string');
  assert.strictEqual(urlAlphabet.length, 64, 'urlAlphabet must have length 64');
  assert.strictEqual(getUrlAlphabet(), urlAlphabet, 'getUrlAlphabet helper must return urlAlphabet');

  // 2. urlAlphabet contains 64 unique characters with numbers, lowercase letters, uppercase letters, hyphen, and underscore
  const charSet = new Set(urlAlphabet);
  assert.strictEqual(charSet.size, 64, 'urlAlphabet must contain 64 unique characters');

  for (let c = 65; c <= 90; c++) {
    assert.ok(charSet.has(String.fromCharCode(c)), `Must contain uppercase letter ${String.fromCharCode(c)}`);
  }
  for (let c = 97; c <= 122; c++) {
    assert.ok(charSet.has(String.fromCharCode(c)), `Must contain lowercase letter ${String.fromCharCode(c)}`);
  }
  for (let c = 48; c <= 57; c++) {
    assert.ok(charSet.has(String.fromCharCode(c)), `Must contain digit ${String.fromCharCode(c)}`);
  }
  assert.ok(charSet.has('-'), 'Must contain hyphen (-) character');
  assert.ok(charSet.has('_'), 'Must contain underscore (_) character');

  // 3. urlAlphabet characters all conform to URL-safe ASCII character set
  assert.match(urlAlphabet, /^[A-Za-z0-9_-]{64}$/, 'urlAlphabet characters must all be URL-safe ASCII');
  assert.strictEqual(isUrlSafeString(urlAlphabet), true, 'urlAlphabet must pass isUrlSafeString check');
  assert.strictEqual(isUrlSafeString('hello-world_123'), true);
  assert.strictEqual(isUrlSafeString('hello world!'), false);
  assert.strictEqual(isUrlSafeString(null), false);

  // 4. urlAlphabet is exported from the root nanoid package as an ESM named export
  assert.strictEqual(rootAlphabet, urlAlphabet, 'root export must match urlAlphabet');

  // 5. urlAlphabet can be used with customAlphabet to generate URL-safe identifiers
  const generate = customAlphabet(urlAlphabet, 21);
  assert.strictEqual(typeof generate, 'function', 'customAlphabet should return a generator');
  const id = generate();
  assert.strictEqual(typeof id, 'string', 'Generated ID must be a string');
  assert.strictEqual(id.length, 21, 'Generated ID length must match configured size');
  assert.strictEqual(isUrlSafeString(id), true, 'Generated ID characters must all belong to urlAlphabet');
}

function runAll() {
  testAlphabetStructure();
  console.log('Contract verification finished successfully.');
}

try {
  runAll();
  process.exit(0);
} catch (err) {
  console.error('Contract verification failed:', err);
  process.exit(1);
}

オリジンシーダー

匿名