| 454 | } |
| 455 | |
| 456 | #openFile(file) { |
| 457 | this.#opening = true; |
| 458 | this.#writing = true; |
| 459 | this.#asyncDrainScheduled = false; |
| 460 | |
| 461 | // NOTE: 'error' and 'ready' events emitted below only relevant when sonic.sync===false |
| 462 | // for sync mode, there is no way to add a listener that will receive these |
| 463 | |
| 464 | const fileOpened = (err, fd) => { |
| 465 | if (err) { |
| 466 | this.#reopening = false; |
| 467 | this.#writing = false; |
| 468 | this.#opening = false; |
| 469 | |
| 470 | if (this.#sync) { |
| 471 | process.nextTick(() => { |
| 472 | if (this.listenerCount('error') > 0) { |
| 473 | this.emit('error', err); |
| 474 | } |
| 475 | }); |
| 476 | } else { |
| 477 | this.emit('error', err); |
| 478 | } |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | const reopening = this.#reopening; |
| 483 | |
| 484 | this.#fd = fd; |
| 485 | this.#file = file; |
| 486 | this.#reopening = false; |
| 487 | this.#opening = false; |
| 488 | this.#writing = false; |
| 489 | |
| 490 | if (this.#sync) { |
| 491 | process.nextTick(() => this.emit('ready')); |
| 492 | } else { |
| 493 | this.emit('ready'); |
| 494 | } |
| 495 | |
| 496 | if (this.#destroyed) { |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | // start |
| 501 | if ((!this.#writing && this.#len > this.#minLength) || this.#flushPending) { |
| 502 | this.#actualWrite(); |
| 503 | } else if (reopening) { |
| 504 | process.nextTick(() => this.emit('drain')); |
| 505 | } |
| 506 | }; |
| 507 | |
| 508 | const flags = this.#append ? 'a' : 'w'; |
| 509 | const mode = this.#mode; |
| 510 | |
| 511 | if (this.#sync) { |
| 512 | try { |
| 513 | if (this.#mkdir) this.#fs.mkdirSync(path.dirname(file), { recursive: true }); |