Files
NapCatQQ/.pnpm-store/v3/files/2d/45adef8034ffcffb631d413144a9164fa4dfc15b9ce5216bcef7afd8177f2472d7ddb18fa23a011489fe123b4a6cb59b5620430681d248a34d4fb1c6b3620a
TanTan e311ca3275
Build NapCat Artifacts / Windows Loader Test (push) Canceled after 0s
Build NapCat Artifacts / Type Check & Test (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Framework (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Shell (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

63 lines
1.6 KiB
Plaintext

'use strict';
var fo = require('../../file-operations');
var readStream = require('../../src/read-contents/read-stream');
function writeStream(file, optResolver, onWritten) {
var flags = fo.getFlags({
overwrite: optResolver.resolve('overwrite', file),
append: optResolver.resolve('append', file),
});
var opt = {
mode: file.stat.mode,
// TODO: need to test this
flags: flags,
};
// TODO: is this the best API?
var outStream = fo.createWriteStream(file.path, opt, onFlush);
file.contents.once('error', onComplete);
outStream.once('error', onComplete);
outStream.once('finish', onComplete);
// TODO: should this use a clone?
file.contents.pipe(outStream);
function onComplete(streamErr) {
// Cleanup event handlers before closing
file.contents.removeListener('error', onComplete);
outStream.removeListener('error', onComplete);
outStream.removeListener('finish', onComplete);
// Need to guarantee the fd is closed before forwarding the error
outStream.once('close', onClose);
outStream.end();
function onClose(closeErr) {
onWritten(streamErr || closeErr);
}
}
// Cleanup
function onFlush(fd, callback) {
// TODO: removing this before readStream because it replaces the stream
file.contents.removeListener('error', onComplete);
// TODO: this is doing sync stuff & the callback seems unnecessary
// TODO: Replace the contents stream or use a clone?
readStream(file, complete);
function complete() {
if (typeof fd !== 'number') {
return callback();
}
fo.updateMetadata(fd, file, callback);
}
}
}
module.exports = writeStream;