diff --git a/src/engine.ts b/src/engine.ts index c9b4d2e4..9446cc37 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -563,7 +563,8 @@ function dynamicReplacement(_file_rr, _config_rr, _data_rr) { } if (needsByteOrSize && -1 === _bytes) { - _bytes = Buffer.from(_text).length; + // The size in the encoding of the file, not of the text as utf8 + _bytes = Buffer.byteLength(_text, _config_rr.encoding); _size = readableSize(_bytes); } diff --git a/test/js/encoding.test.js b/test/js/encoding.test.js index 8fc75d70..ae651cfc 100644 --- a/test/js/encoding.test.js +++ b/test/js/encoding.test.js @@ -125,18 +125,18 @@ describe('--encoding', () => { expect((await rr('æ', 'text.length + text', config)).out).toBe('f3fæoo\n'); }); - test.todo( - 'BUG #688: with --replacement-js `bytes` is the size of the file in its encoding', - async () => { - fs.writeFileSync('my.file', latin1()); - const config = { - includeGlob: ['my.file'], - encoding: 'latin1', - replacementJs: true, - output: true, - }; - expect((await rr('^', 'bytes + ":"', config)).out).toBe('3:fæo'); - } - ); + test('with --replacement-js `bytes` and `size` are the size of the file in its encoding (#688)', async () => { + // 1500 times `æ`: one byte each in latin1, but two in utf8 (that would give 3000 and 2.9 KB) + fs.writeFileSync('my.file', Buffer.alloc(1500, 0xe6)); + const config = { + includeGlob: ['my.file'], + encoding: 'latin1', + replacementJs: true, + output: true, + }; + expect((await rr('^', 'bytes + ":" + size + ":"', config)).out).toBe( + '1500:1.5 KB:' + 'æ'.repeat(1500) + '\n' + ); + }); }); });