CodeSampleX

サンプル

iconv-lite 0.4.24: encode

検証済みサンプル — npm iconv-lite 0.4.24: encode. node 22 · linux alpine/x64 · docker で contract を実行し、成功しました: assert iconv.encode encodes string to UTF-8 buffer…

sha256:ff2939347d4aa812769f82ce21ff373429ed13f12ed8981237b568803567a3bc

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。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 alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-20

ケース

HOW
ゴール
verify iconv-lite.encode in pkg:npm/iconv-lite@0.4.24
パッケージ
シンボル
  • iconv-lite.encode
環境
node 22.23.2
作成日
2026-08-20T19:02:54Z

コントラクト

  1. assert iconv.encode encodes string to UTF-8 buffer correctly and returns a Buffer instance
  2. assert iconv.encode encodes legacy single-byte encodings including Windows-1252 and Windows-1251
  3. assert iconv.encode encodes multibyte CJK strings to EUC-KR, Shift_JIS, and GBK byte buffers
  4. assert iconv.encode prepends byte order mark when addBOM option is enabled
  5. assert iconv.encode encodes UTF-16LE and UTF-16BE buffers correctly

ファイル

  • 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 iconv-lite.encode in pkg:npm/iconv-lite@0.4.24
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/iconv-lite@0.4.24
Demonstrate these symbols/APIs:
  - iconv-lite.encode
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - 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:826e74e4eb3d9d23a14403156875f405317bcb46f31b1dd71027b582fb5fd558","constraints":{"executionContext":"node"},"contract":["assert iconv.encode encodes string to UTF-8 buffer correctly and returns a Buffer instance","assert iconv.encode encodes legacy single-byte encodings including Windows-1252 and Windows-1251","assert iconv.encode encodes multibyte CJK strings to EUC-KR, Shift_JIS, and GBK byte buffers","assert iconv.encode prepends byte order mark when addBOM option is enabled","assert iconv.encode encodes UTF-16LE and UTF-16BE buffers correctly"],"goal":"verify iconv-lite.encode in pkg:npm/iconv-lite@0.4.24","kind":"HOW","packages":["pkg:npm/iconv-lite@0.4.24"],"schemaVersion":1,"symbols":["iconv-lite.encode"]},"contractCommand":["node","test/contract.mjs"],"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/iconv-lite@0.4.24"],"schemaVersion":1,"subject":"pkg:npm/iconv-lite@0.4.24","symbols":["iconv-lite.encode"],"verifierAdapter":"node-typescript@1"}
index.js
const iconv = require('iconv-lite');

/**
 * Encodes a string into a buffer using the specified encoding.
 *
 * @param {string} str - The string to encode.
 * @param {string} encoding - The character encoding name (e.g. 'utf-8', 'euc-kr', 'shift_jis', 'win1251', 'utf16le').
 * @param {Object} [options] - Optional encoding options (e.g. { addBOM: true }).
 * @returns {Buffer} The encoded binary buffer.
 */
function encodeText(str, encoding, options) {
  return iconv.encode(str, encoding, options);
}

module.exports = {
  encodeText,
  iconv,
};
package-lock.json
{
  "name": "sample-iconv-lite-encode",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-iconv-lite-encode",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "iconv-lite": "0.4.24"
      }
    },
    "node_modules/iconv-lite": {
      "version": "0.4.24",
      "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
      "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
      "license": "MIT",
      "dependencies": {
        "safer-buffer": ">= 2.1.2 < 3"
      },
      "engines": {
        "node": ">=0.10.0"
      }
    },
    "node_modules/safer-buffer": {
      "version": "2.1.2",
      "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
      "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-iconv-lite-encode",
  "version": "1.0.0",
  "description": "Clean-room sample verifying iconv-lite.encode functionality",
  "main": "index.js",
  "license": "MIT-0",
  "dependencies": {
    "iconv-lite": "0.4.24"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify iconv-lite.encode in pkg:npm/iconv-lite@0.4.24",
  "kind": "HOW",
  "packages": [
    "pkg:npm/iconv-lite@0.4.24"
  ],
  "symbols": [
    "iconv-lite.encode"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "runtime": "node@22.23.2"
  }
}
test/contract.mjs
import assert from 'node:assert';
import iconv from 'iconv-lite';
import { encodeText } from '../index.js';

// 1. Encode string to UTF-8 buffer
const utf8Buf = encodeText('Hello, World!', 'utf-8');
assert(Buffer.isBuffer(utf8Buf), 'iconv.encode must return a Buffer instance');
assert.deepStrictEqual(
  Array.from(utf8Buf),
  [0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21],
  'iconv.encode must encode string to UTF-8 buffer correctly'
);

// 2. Encode legacy single-byte encodings (Windows-1252 and Windows-1251)
const win1252Buf = encodeText('Café', 'windows-1252');
assert.deepStrictEqual(
  Array.from(win1252Buf),
  [0x43, 0x61, 0x66, 0xe9],
  'iconv.encode must encode Windows-1252 correctly'
);

const win1251Buf = encodeText('Привет', 'win1251');
assert.deepStrictEqual(
  Array.from(win1251Buf),
  [0xcf, 0xf0, 0xe8, 0xe2, 0xe5, 0xf2],
  'iconv.encode must encode Windows-1251 correctly'
);

// 3. Encode multibyte CJK encodings (EUC-KR, Shift_JIS, GBK)
const eucKrBuf = encodeText('안녕하세요', 'euc-kr');
assert.strictEqual(
  iconv.decode(eucKrBuf, 'euc-kr'),
  '안녕하세요',
  'iconv.encode must encode EUC-KR multibyte sequences'
);

const sjisBuf = encodeText('こんにちは', 'shift_jis');
assert.strictEqual(
  iconv.decode(sjisBuf, 'shift_jis'),
  'こんにちは',
  'iconv.encode must encode Shift_JIS multibyte sequences'
);

const gbkBuf = encodeText('你好世界', 'gbk');
assert.strictEqual(
  iconv.decode(gbkBuf, 'gbk'),
  '你好世界',
  'iconv.encode must encode GBK multibyte sequences'
);

// 4. UTF-8 BOM handling: addBOM option prepends 0xEF, 0xBB, 0xBF
const bomBuf = encodeText('ABC', 'utf-8', { addBOM: true });
assert.deepStrictEqual(
  Array.from(bomBuf),
  [0xef, 0xbb, 0xbf, 0x41, 0x42, 0x43],
  'iconv.encode must prepend BOM when addBOM is true'
);

// 5. UTF-16LE and UTF-16BE encoding
const utf16leBuf = encodeText('Sample Test', 'utf-16le');
assert.strictEqual(
  iconv.decode(utf16leBuf, 'utf-16le'),
  'Sample Test',
  'iconv.encode must encode UTF-16LE correctly'
);

const utf16beBuf = encodeText('Sample Test', 'utf-16be');
assert.strictEqual(
  iconv.decode(utf16beBuf, 'utf-16be'),
  'Sample Test',
  'iconv.encode must encode UTF-16BE correctly'
);

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

オリジンシーダー

匿名