Skip to content

Commit

Permalink
fix buffer.transcode if both encodings are same
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Aug 24, 2024
1 parent 537c806 commit 18045db
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
8 changes: 0 additions & 8 deletions src/workerd/api/node/i18n.c++
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,6 @@ void Converter::setSubstituteChars(kj::StringPtr sub) {

kj::Array<kj::byte> transcode(
kj::ArrayPtr<kj::byte> source, Encoding fromEncoding, Encoding toEncoding) {
// Optimization:
// If both encodings are same, we just return a copy of the buffer.
if (fromEncoding == toEncoding) {
auto destbuf = kj::heapArray<kj::byte>(source.size());
destbuf.asPtr().copyFrom(source);
return destbuf.asBytes().attach(kj::mv(destbuf));
}

TranscodeImpl transcode_function = &TranscodeDefault;
switch (fromEncoding) {
case Encoding::ASCII:
Expand Down
7 changes: 7 additions & 0 deletions src/workerd/api/node/tests/buffer-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6045,6 +6045,13 @@ export const transcodeTest = {
ok(copied_value.buffer.detached);
ok(!original.buffer.detached);
}

// Same encoding types should return in a value that replaces
// invalid characters with replacement characters.
deepStrictEqual(
transcode(Buffer.from([0x80]), 'utf8', 'utf8'),
Buffer.from([0xef, 0xbf, 0xbd])
);
},
};

Expand Down

0 comments on commit 18045db

Please sign in to comment.