| 479 | * @returns {Promise<void>} |
| 480 | */ |
| 481 | const writeFile = async (name, content, size) => { |
| 482 | const file = name |
| 483 | ? join(this.fs, filename, `../${name}${extension}`) |
| 484 | : filename; |
| 485 | await new Promise( |
| 486 | /** |
| 487 | * Handles the callback logic for this hook. |
| 488 | * @param {(value?: undefined) => void} resolve resolve |
| 489 | * @param {(reason?: Error | null) => void} reject reject |
| 490 | */ |
| 491 | (resolve, reject) => { |
| 492 | let stream = this.fs.createWriteStream(`${file}_`); |
| 493 | /** @type {undefined | import("zlib").Gzip | import("zlib").BrotliCompress} */ |
| 494 | let compression; |
| 495 | if (file.endsWith(".gz")) { |
| 496 | compression = createGzip({ |
| 497 | chunkSize: COMPRESSION_CHUNK_SIZE, |
| 498 | level: zConstants.Z_BEST_SPEED |
| 499 | }); |
| 500 | } else if (file.endsWith(".br")) { |
| 501 | compression = createBrotliCompress({ |
| 502 | chunkSize: COMPRESSION_CHUNK_SIZE, |
| 503 | params: { |
| 504 | [zConstants.BROTLI_PARAM_MODE]: zConstants.BROTLI_MODE_TEXT, |
| 505 | [zConstants.BROTLI_PARAM_QUALITY]: 2, |
| 506 | [zConstants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING]: true, |
| 507 | [zConstants.BROTLI_PARAM_SIZE_HINT]: size |
| 508 | } |
| 509 | }); |
| 510 | } |
| 511 | if (compression) { |
| 512 | pipeline(compression, stream, reject); |
| 513 | stream = compression; |
| 514 | stream.on("finish", () => resolve()); |
| 515 | } else { |
| 516 | stream.on("error", (err) => reject(err)); |
| 517 | stream.on("finish", () => resolve()); |
| 518 | } |
| 519 | // split into chunks for WRITE_LIMIT_CHUNK size |
| 520 | /** @type {Buffer[]} */ |
| 521 | const chunks = []; |
| 522 | for (const b of content) { |
| 523 | if (b.length < WRITE_LIMIT_CHUNK) { |
| 524 | chunks.push(b); |
| 525 | } else { |
| 526 | for (let i = 0; i < b.length; i += WRITE_LIMIT_CHUNK) { |
| 527 | chunks.push(b.subarray(i, i + WRITE_LIMIT_CHUNK)); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | const len = chunks.length; |
| 533 | let i = 0; |
| 534 | /** |
| 535 | * Processes the provided err. |
| 536 | * @param {(Error | null)=} err err |
| 537 | */ |
| 538 | const batchWrite = (err) => { |