pinojs / sonic-boom

Extremely fast utf8 only stream implementation
MIT License
266 stars 41 forks source link

Does not reopen a new file during async writing #160

Open zaverden opened 1 year ago

zaverden commented 1 year ago

According to the docs I can pass a new file path for reopen to continue to write into new file.

However, if I call reopen during async write it ignores new file path (https://github.com/pinojs/sonic-boom/blob/v3.2.0/index.js#L304-L308)

Code to reproduce

import { once } from "node:events";
import SonicBoom from "sonic-boom";

const startingPath = "1.log";
const newPath = "2.log";

const boom = new SonicBoom({ dest: startingPath, sync: false, append: true });

await once(boom, "ready");
console.log("file:", boom.file); // 1.log

boom.write("expect f1\n");

boom.once("write", () => {
  console.log("_writing:", boom._writing); // true
  boom.reopen(newPath);
});

await once(boom, "ready"); // emitted on reopen
console.log("file:", boom.file); //! 1.log, expected 2.log

boom.write("expect f2\n"); //! written into 1.log, expected into 2.log

workaround

- boom.reopen(newPath);
+ boom.file = newPath; 
+ boom.reopen(); 
mcollina commented 1 year ago

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

rluvaton commented 1 year ago

I'll take it :)

rluvaton commented 1 year ago

I can't find a performant way to do this without cloning data that should be in the previous file

if you want to make sure that until you call reopen all pending writes finish you can listen to drain