Doing several tests of files validation, I need a file with more than 250MB, but I don't want to store it in the repo.
I'm trying to create a file with NodeJs in time execution with several functions I've found, but they don't work for me.
This one creates the file, but it doesn't have the size I'm passing. Files have just 1 byte.
createFile(filePath, size) {
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
const fd = fs.openSync(`${filePath}`, 'w');
//fs.writeSync(fd, 'ok', Math.max(0, size - 2));
fs.writeSync(fd, Buffer.alloc(1, 'ok', 'base64'), 0, 1,
//Math.max(0, size - 1)
size - 1
);
fs.closeSync(fd);
resolve(true);
} catch (error) {
reject(error);
}
}, 0);
});
}
I'll appreciate any help
Buffer.alloc(1...)may be the issue
When I said that, I meant that Buffer.alloc(someNumber) would be the length of your buffer, which is why the length of the file written to ended up as 1
I kept your return promise logic in the createFile function below
createFile(filePath, size) {
return new Promise((work,fail)=>{
fs.writeFile(filePath, Buffer.alloc(size), err=>err?fail(err):work())
//fs.writeFile(desiredPath, desiredSize, (reject if error and resolve if none))
})
}