Files
bun.sh/test/regression/issue23966.test.ts
robobun 0ad4e6af2d Fix Buffer.isEncoding('') to return false (#23968)
## Summary
Fixes `Buffer.isEncoding('')` to return `false` instead of `true`,
matching Node.js behavior.

## Description
Previously, `Buffer.isEncoding('')` incorrectly returned `true` in Bun,
while Node.js correctly returns `false`. This was caused by
`parseEnumerationFromView` in `JSBufferEncodingType.cpp` treating empty
strings (length 0) as valid utf8 encoding.

The fix modifies the switch statement to return `std::nullopt` for empty
strings, along with other invalid short strings.

## Changes
- Modified `src/bun.js/bindings/JSBufferEncodingType.cpp` to return
`std::nullopt` for empty strings
- Added regression test `test/regression/issue23966.test.ts`

## Test Plan
- [x] Test fails with `USE_SYSTEM_BUN=1 bun test
test/regression/issue23966.test.ts` (confirms bug exists)
- [x] Test passes with `bun bd test test/regression/issue23966.test.ts`
(confirms fix works)
- [x] Verified behavior matches Node.js v24.3.0
- [x] All test cases for valid/invalid encodings pass

Fixes #23966

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Bot <claude-bot@bun.sh>
Co-authored-by: Claude <noreply@anthropic.com>
2025-10-22 16:15:29 -07:00

43 lines
1.2 KiB
TypeScript

// https://github.com/oven-sh/bun/issues/23966
// Buffer.isEncoding() behaves differently in Bun compared to Node.js
import { expect, test } from "bun:test";
import { Buffer } from "node:buffer";
test.concurrent("Buffer.isEncoding('') should return false", () => {
expect(Buffer.isEncoding("")).toBe(false);
});
const validEncodings = [
"utf8",
"utf-8",
"hex",
"base64",
"ascii",
"latin1",
"binary",
"ucs2",
"ucs-2",
"utf16le",
"utf-16le",
];
const invalidEncodings = ["invalid", "utf32", "something"];
const nonStringValues = [
{ value: 123, name: "number" },
{ value: null, name: "null" },
{ value: undefined, name: "undefined" },
{ value: {}, name: "object" },
{ value: [], name: "array" },
];
test.concurrent.each(validEncodings)("Buffer.isEncoding('%s') should return true", encoding => {
expect(Buffer.isEncoding(encoding)).toBe(true);
});
test.concurrent.each(invalidEncodings)("Buffer.isEncoding('%s') should return false", encoding => {
expect(Buffer.isEncoding(encoding)).toBe(false);
});
test.concurrent.each(nonStringValues)("Buffer.isEncoding($name) should return false for non-string", ({ value }) => {
expect(Buffer.isEncoding(value as any)).toBe(false);
});