From f5ad682fd74ff2de94ab21b9a5f397dd13bc4dd6 Mon Sep 17 00:00:00 2001 From: Ben Heidemann Date: Sun, 18 Apr 2021 16:15:02 +0100 Subject: [PATCH] fix: allow encodings other that utf8 when using writeFile --- src/filesystem/implementation.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index 90fbd22..33ed78c 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -1870,11 +1870,16 @@ function writeFile(context, path, data, options, callback) { } data = data || ''; - if(typeof data === 'number') { - data = '' + data; - } - if(typeof data === 'string' && (options.encoding || 'utf8') === 'utf8') { - data = Buffer.from(data); + if(!Buffer.isBuffer(data)) { + if(typeof data === 'number') { + data = '' + data; + } + if(typeof data !== 'string') { + data = Buffer.from(data.toString()); + } + else { + data = Buffer.from(data, options.encoding || 'utf8'); + } } open_file(context, path, flags, function(err, fileNode) {