From 9a07c414bf51ba6b1c7de5992ac9b1a9d2523ac2 Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Thu, 30 Oct 2025 09:12:11 +1100 Subject: [PATCH 1/2] fix: sample size handling, build and release --- src/encoding/ascii.test.ts | 8 -------- src/index.test.ts | 10 ++++++++++ src/index.ts | 17 ++++++++++------- src/test/data/encodings/shortascii | 1 - 4 files changed, 20 insertions(+), 16 deletions(-) delete mode 100644 src/test/data/encodings/shortascii diff --git a/src/encoding/ascii.test.ts b/src/encoding/ascii.test.ts index 85d7c91..49853b0 100644 --- a/src/encoding/ascii.test.ts +++ b/src/encoding/ascii.test.ts @@ -7,11 +7,3 @@ describe('ASCII', () => { ).toBe('ASCII'); }); }); - -describe('ASCII', () => { - it('should return ASCII', () => { - expect( - chardet.detectFileSync(__dirname + '/../test/data/encodings/shortascii', { sampleSize: 32 }), - ).toBe('ASCII'); - }); -}); \ No newline at end of file diff --git a/src/index.test.ts b/src/index.test.ts index 5e9a03c..3835e6c 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -53,6 +53,16 @@ describe('chardet', () => { const res = await chardet.detectFile(path, { sampleSize: 32, offset: 64 }); expect(res).toBe('UTF-8'); }); + + it('should work as expected with sampleSize larger than actual file size (1)', async () => { + const res = await chardet.detectFile(path, { sampleSize: 1_000_000 }); + expect(res).toBe('UTF-8'); + }); + + it('should work as expected with sampleSize larger than actual file size (2)', async () => { + const res = await chardet.detectFile(__dirname + '/test/data/encodings/koi8r', { sampleSize: 1024 * 1024 }); + expect(res).toBe('KOI8-R'); + }); }); describe('#detectFileSync', () => { diff --git a/src/index.ts b/src/index.ts index 4050da3..5fe0c2c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -103,27 +103,30 @@ export const detectFile = ( let fd: any; const fs = loadFs(); - const handler = (err: Error | null | undefined, buffer: Buffer) => { + const handler = (err: Error | null, buffer: Buffer | null) => { if (fd) { fs.closeSync(fd); } if (err) { reject(err); - } else { + } else if (buffer) { resolve(detect(buffer)); + } else { + reject(new Error('No error and no buffer received')); } }; - if (opts && opts.sampleSize) { + const sampleSize = opts?.sampleSize || 0; + if (sampleSize > 0) { fd = fs.openSync(filepath, 'r'); - let sample = Buffer.allocUnsafe(opts.sampleSize); + let sample = Buffer.allocUnsafe(sampleSize); - fs.read(fd, sample, 0, opts.sampleSize, opts.offset, (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: Buffer) => { + fs.read(fd, sample, 0, sampleSize, opts.offset, (err: NodeJS.ErrnoException | null, bytesRead: number) => { if (err) { - handler(err); + handler(err, null); } else { - if (bytesRead < opts.sampleSize!) { + if (bytesRead < sampleSize) { sample = sample.subarray(0, bytesRead); } handler(null, sample); diff --git a/src/test/data/encodings/shortascii b/src/test/data/encodings/shortascii deleted file mode 100644 index eba61a7..0000000 --- a/src/test/data/encodings/shortascii +++ /dev/null @@ -1 +0,0 @@ -short \ No newline at end of file From 83360f90f533bdcd372e5090fc627a45f18d8876 Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Thu, 30 Oct 2025 09:12:46 +1100 Subject: [PATCH 2/2] Development snapshot --- src/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.test.ts b/src/index.test.ts index 3835e6c..877f1a0 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -55,7 +55,7 @@ describe('chardet', () => { }); it('should work as expected with sampleSize larger than actual file size (1)', async () => { - const res = await chardet.detectFile(path, { sampleSize: 1_000_000 }); + const res = await chardet.detectFile(path, { sampleSize: 1024 * 1024 }); expect(res).toBe('UTF-8'); });