diff --git a/src/node_file.cc b/src/node_file.cc index 5d06960953a013..9da3fc7f2e0002 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -515,12 +515,17 @@ static void InternalModuleReadFile(const FunctionCallbackInfo& args) { start = 3; // Skip UTF-8 BOM. } - Local chars_string = - String::NewFromUtf8(env->isolate(), - &chars[start], - String::kNormalString, - offset - start); - args.GetReturnValue().Set(chars_string); + const size_t size = offset - start; + if (size == 0) { + args.GetReturnValue().SetEmptyString(); + } else { + Local chars_string = + String::NewFromUtf8(env->isolate(), + &chars[start], + String::kNormalString, + size); + args.GetReturnValue().Set(chars_string); + } } // Used to speed up module loading. Returns 0 if the path refers to diff --git a/test/fixtures/empty-with-bom.txt b/test/fixtures/empty-with-bom.txt new file mode 100644 index 00000000000000..5f282702bb03ef --- /dev/null +++ b/test/fixtures/empty-with-bom.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/parallel/test-module-binding.js b/test/parallel/test-module-binding.js new file mode 100644 index 00000000000000..ba11c3e47ec646 --- /dev/null +++ b/test/parallel/test-module-binding.js @@ -0,0 +1,9 @@ +'use strict'; +require('../common'); +const fixtures = require('../common/fixtures'); +const { internalModuleReadFile } = process.binding('fs'); +const { strictEqual } = require('assert'); + +strictEqual(internalModuleReadFile('nosuchfile'), undefined); +strictEqual(internalModuleReadFile(fixtures.path('empty.txt')), ''); +strictEqual(internalModuleReadFile(fixtures.path('empty-with-bom.txt')), '');