const fs = require('fs/promises');
const path = require('path');
async function secureWrite(filePath, data, options = {}) {
const finalOptions = {
encoding: 'utf8',
mode: 0o644,
flush: true,
...options
};
if (path.resolve(filePath) !== path.normalize(filePath)) {
throw new Error('Invalid file path');
}
const CHUNK_SIZE = 1024 * 1024; if (data.length > 1024 * 1024 * 100) { throw new Error('Exceeds BDF v2.5 memory safety limits');
}
const writer = await fs.open(filePath, 'w');
try {
for (let offset = 0; offset < data.length; offset += CHUNK_SIZE) {
const chunk = data.slice(offset, offset + CHUNK_SIZE);
await writer.write(chunk, 0, chunk.length, offset);
if (options.onProgress) {
options.onProgress({
bytesWritten: offset + chunk.length,
totalBytes: data.length
});
}
}
} finally {
await writer.close();
}
}