iconv-lite encode and decode: working versions (tested 2026-10-16)
Encode strings to win1252, shift_jis and utf16le Buffers with iconv-lite, decode them back and assert exact byte values, round trips and encodingExists checks fully offline.
passed8 passed · 0 failed2026-10-16 19:54 UTC- Status
- passed
- Last passed
- 2026-10-16 19:54 UTC
- Tests
- 8 / 8
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
iconv-lite@0.7.3@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38958328921
The code
Exactly the files that passed. Your agent can pull them with get_recipe("iconv-lite-encode-decode") (install), or see them on GitHub.
package.json
{
"name": "iconv-lite-encode-decode",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"iconv-lite": "0.7.3"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/codec.ts
import iconv from "iconv-lite";
export const encodings = ["win1252", "shift_jis", "utf16le"] as const;
export type SupportedEncoding = (typeof encodings)[number];
export function supports(encoding: string): boolean {
return iconv.encodingExists(encoding);
}
export function encode(text: string, encoding: SupportedEncoding): Buffer {
if (!iconv.encodingExists(encoding)) {
throw new Error(`Unsupported encoding: ${encoding}`);
}
return iconv.encode(text, encoding);
}
export function decode(buffer: Buffer, encoding: SupportedEncoding): string {
if (!iconv.encodingExists(encoding)) {
throw new Error(`Unsupported encoding: ${encoding}`);
}
return iconv.decode(buffer, encoding);
}
export function roundTrip(text: string, encoding: SupportedEncoding): string {
return decode(encode(text, encoding), encoding);
}
test/codec.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import iconv from "iconv-lite";
import { decode, encode, roundTrip, supports } from "../src/codec.ts";
test("encodingExists reports known and unknown encodings", () => {
assert.equal(supports("win1252"), true);
assert.equal(supports("shift_jis"), true);
assert.equal(supports("utf16le"), true);
assert.equal(iconv.encodingExists("definitely-not-real"), false);
});
test("encodes win1252 to exact bytes", () => {
const buffer = encode("café", "win1252");
assert.deepEqual([...buffer], [0x63, 0x61, 0x66, 0xe9]);
});
test("decodes win1252 bytes back to text", () => {
const text = decode(Buffer.from([0x63, 0x61, 0x66, 0xe9]), "win1252");
assert.equal(text, "café");
});
test("encodes shift_jis to exact bytes", () => {
const buffer = encode("日本語", "shift_jis");
assert.deepEqual([...buffer], [0x93, 0xfa, 0x96, 0x7b, 0x8c, 0xea]);
});
test("decodes shift_jis bytes back to text", () => {
const bytes = Buffer.from([0x93, 0xfa, 0x96, 0x7b, 0x8c, 0xea]);
assert.equal(decode(bytes, "shift_jis"), "日本語");
});
test("encodes utf16le to exact bytes", () => {
const buffer = encode("Hi", "utf16le");
assert.deepEqual([...buffer], [0x48, 0x00, 0x69, 0x00]);
});
test("decodes utf16le bytes back to text", () => {
const bytes = Buffer.from([0x48, 0x00, 0x69, 0x00]);
assert.equal(decode(bytes, "utf16le"), "Hi");
});
test("round trips each supported encoding", () => {
assert.equal(roundTrip("café", "win1252"), "café");
assert.equal(roundTrip("日本語", "shift_jis"), "日本語");
assert.equal(roundTrip("Hi", "utf16le"), "Hi");
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.