0

The error is: "Error [ERR_STREAM_WRITE_AFTER_END]: write after end"

Well, it seems like I lack understanding on the subject because I did search this error in stackoverflow and there was plenty of information about this.

Still, I couldn't solve my problem.

The thing is: It works for the first time. The second time gives the error.

So I'm trying to convert an HTML Page to docx and all is well.

The docx is not written to a folder but to a blob.

Then, I'm trying to create a stream from that blob and pipe it to Azure blob storage.

I think that I am missing something here.

CODE:

import * as HtmlDocx from "html-docx-js";
import * as fs from "fs";
import * as path from "path";
import * as stream from "stream";
import { AzureStorageService } from "../services/azure-storage.service";

const bufferStream: any = new stream.PassThrough();

export class DOCXGenerator {
  static async generateDocx(_data: any): Promise<any> {
    const inputFile: string = `${path.join(
      __dirname,
      "../templates/index.html"
    )}`;
    const outputFile: string = `${path.join(
      __dirname,
      "../uploads/" + _data.subtitle
    )}`;
    await fs.readFile(inputFile, "utf-8", async function(err, html) {
      let newHTML: any = html.replace(
        "uri",
        _data.subtitleURL.replace("/", "")
      );
      let newNewHTML: any = newHTML.replace(
        "titlesub",
        _data.subtitle.replace("/", "")
      );
      if (err) {
        throw err;
      }


      // ERROR STARTS HERE: 

      const docxAsBlob: any = await HtmlDocx.asBlob(newNewHTML);
      bufferStream.write(new Buffer(docxAsBlob));


      bufferStream.pipe(
        await AzureStorageService.createWriteStreamToBlockBlob(
          "container-name",_data.subtitle
        )
      );

      bufferStream.end();

      // ERROR ENDS HERE.
    });

    const newFileName: string = (("somne-prefix_" +
      _data.subtitle.replace("/", "")) as string)
      .split(" ")
      .join("_")
      .replace(/-/g, "_")
      .replace("pdf", "docx");
    return newFileName;
  }
}
monogate
  • 11
  • 1

0 Answers0