Search code examples
node.jsstreambufferapolloapollo-client

How to set utf-8 encoding in the uploading file when using apollo-upload-client?


I am uploading a text file using apollo-upload-client. When the file hits the server it comes in 7bit encoding, e.g.:

const { filename, createReadStream, encoding } = await file;
console.log({ encoding });

--> { encoding: "7bit" }

I then make a stream as follow:

const stream = createReadStream({ encoding: "utf8" });

and then a string out for the stream using the following function:

function streamToString(stream, cb) {
  const chunks = [];
  stream.on("data", (chunk) => {
    chunks.push(chunk.toString());
  });
  stream.on("end", () => {
    cb(chunks.join(""));
  });
}

the result:

streamToString(stream, async (data) => {
            
    console.log({data});

   --> good string �� good string ��

}

This is what gets saved in the DB good string �� good string ��.

I guess I need to send the file to the server with proper encoding, perhaps as utf-8. How can I do it?


Solution

  • the streamToString function was implemented incorrectly. Here is the correct version:

     const chunks = [];
    
      readStream.on("data", function (chunk) {
        chunks.push(chunk);
      });
    
      // Send the buffer or you can put it into a var
      readStream.on("end", function () {
        res.send(Buffer.concat(chunks));
      });