CodeSampleX

示例

@noble/hashes 2.3.0: concatBytes

已验证示例 — npm @noble/hashes 2.3.0: concatBytes. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: concatBytes concatenates multiple Uint8Array instances…

sha256:28b4c603f45b4b6eaec568c2b06f1dc1e8cfa95c54ad35f3f532158eb74ce58f

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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 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 @noble/hashes.concatBytes in pkg:npm/%40noble/hashes@2.3.0
包
符号
  • @noble/hashes.concatBytes
环境
node 22.23.2
创建时间
2026-09-06T12:42:35Z

契约

  1. concatBytes concatenates multiple Uint8Array instances into a single contiguous Uint8Array
  2. concatBytes returns an empty Uint8Array when called with no arguments or empty arrays
  3. concatBytes correctly handles Uint8Array subarrays and offset buffer views
  4. concatBytes produces an independent copy of byte data without mutating input arrays
  5. concatBytes rejects non-Uint8Array inputs by throwing TypeError

文件

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • 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 @noble/hashes.concatBytes in pkg:npm/%40noble/hashes@2.3.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40noble/hashes@2.3.0
Demonstrate these symbols/APIs:
  - @noble/hashes.concatBytes
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:8488c016c306c23d0c92b2191441b69e2602b3b7d6ecf26d3af4f36650b64549","constraints":{"executionContext":"node"},"contract":["concatBytes concatenates multiple Uint8Array instances into a single contiguous Uint8Array","concatBytes returns an empty Uint8Array when called with no arguments or empty arrays","concatBytes correctly handles Uint8Array subarrays and offset buffer views","concatBytes produces an independent copy of byte data without mutating input arrays","concatBytes rejects non-Uint8Array inputs by throwing TypeError"],"goal":"verify @noble/hashes.concatBytes in pkg:npm/%40noble/hashes@2.3.0","kind":"HOW","packages":["pkg:npm/%40noble/hashes@2.3.0"],"schemaVersion":1,"symbols":["@noble/hashes.concatBytes"]},"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/%40noble/hashes@2.3.0"],"schemaVersion":1,"subject":"pkg:npm/%40noble/hashes@2.3.0","symbols":["@noble/hashes.concatBytes"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-noble-hashes-concat-bytes",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-noble-hashes-concat-bytes",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@noble/hashes": "2.3.0"
      }
    },
    "node_modules/@noble/hashes": {
      "version": "2.3.0",
      "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-2.3.0.tgz",
      "integrity": "sha512-oN+QwyX7VSHotibwubG3kpzbwKrfnyR6OOO+3Nk/53ADL7FmgHHz4TgrbaYKvvOw09u6QTx0oiH1cNCIOuN0CQ==",
      "license": "MIT",
      "engines": {
        "node": ">= 20.19.0"
      },
      "funding": {
        "url": "https://paulmillr.com/funding/"
      }
    }
  }
}
package.json
{
  "name": "sample-noble-hashes-concat-bytes",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "@noble/hashes": "2.3.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @noble/hashes.concatBytes in pkg:npm/%40noble/hashes@2.3.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40noble/hashes@2.3.0"
  ],
  "symbols": [
    "@noble/hashes.concatBytes"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.mjs
import { concatBytes } from '@noble/hashes/utils.js';

export { concatBytes };

/**
 * Combines multiple Uint8Array instances into a single contiguous Uint8Array.
 * @param {...Uint8Array} arrays
 * @returns {Uint8Array}
 */
export function combineChunks(...arrays) {
  return concatBytes(...arrays);
}

/**
 * Concatenates UTF-8 encoded text strings into a single Uint8Array.
 * @param {...string} strings
 * @returns {Uint8Array}
 */
export function concatStrings(...strings) {
  const encoder = new TextEncoder();
  const byteArrays = strings.map((s) => encoder.encode(s));
  return concatBytes(...byteArrays);
}
test/contract.mjs
import assert from 'node:assert/strict';
import { concatBytes, combineChunks, concatStrings } from '../src/index.mjs';

// 1. concatBytes concatenates multiple Uint8Array instances into a single contiguous Uint8Array
{
  const a = new Uint8Array([0x01, 0x02]);
  const b = new Uint8Array([0x03, 0x04, 0x05]);
  const c = new Uint8Array([0x06]);
  const merged = concatBytes(a, b, c);

  assert.equal(merged instanceof Uint8Array, true);
  assert.equal(merged.length, 6);
  assert.deepEqual(merged, new Uint8Array([1, 2, 3, 4, 5, 6]));

  const combined = combineChunks(a, b, c);
  assert.deepEqual(combined, new Uint8Array([1, 2, 3, 4, 5, 6]));

  const textMerged = concatStrings('hello', ' ', 'world');
  assert.deepEqual(textMerged, new TextEncoder().encode('hello world'));
}

// 2. concatBytes returns an empty Uint8Array when called with no arguments or empty arrays
{
  const empty1 = concatBytes();
  assert.equal(empty1 instanceof Uint8Array, true);
  assert.equal(empty1.length, 0);

  const empty2 = concatBytes(new Uint8Array(0), new Uint8Array(0));
  assert.equal(empty2 instanceof Uint8Array, true);
  assert.equal(empty2.length, 0);

  const emptyCombined = combineChunks();
  assert.equal(emptyCombined.length, 0);

  const emptyStrings = concatStrings();
  assert.equal(emptyStrings.length, 0);
}

// 3. concatBytes correctly handles Uint8Array subarrays and offset buffer views
{
  const buffer = new ArrayBuffer(8);
  const view1 = new Uint8Array(buffer, 2, 3);
  view1[0] = 0xaa;
  view1[1] = 0xbb;
  view1[2] = 0xcc;

  const base = new Uint8Array([0x10, 0x20, 0x30, 0x40]);
  const view2 = base.subarray(1, 3); // [0x20, 0x30]

  const merged = concatBytes(view1, view2);
  assert.equal(merged.length, 5);
  assert.deepEqual(merged, new Uint8Array([0xaa, 0xbb, 0xcc, 0x20, 0x30]));
}

// 4. concatBytes produces an independent copy of byte data without mutating input arrays
{
  const part1 = new Uint8Array([10, 20]);
  const part2 = new Uint8Array([30, 40]);
  const result = concatBytes(part1, part2);

  assert.deepEqual(result, new Uint8Array([10, 20, 30, 40]));

  // Mutating output does not affect inputs
  result[0] = 99;
  assert.equal(part1[0], 10);

  // Mutating input after concat does not affect output
  part2[1] = 88;
  assert.equal(result[3], 40);
}

// 5. concatBytes rejects non-Uint8Array inputs by throwing TypeError
{
  assert.throws(() => concatBytes('not a byte array'), TypeError);
  assert.throws(() => concatBytes(null), TypeError);
  assert.throws(() => concatBytes(undefined), TypeError);
  assert.throws(() => concatBytes([1, 2, 3]), TypeError);
  assert.throws(() => concatBytes(12345), TypeError);
  assert.throws(() => concatBytes({}), TypeError);
  assert.throws(() => concatBytes(new Uint8Array([1]), 'invalid'), TypeError);

  assert.throws(() => combineChunks('invalid'), TypeError);
}

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

原始种子者

匿名