0

I'm using dockerode and a read stream using the cat command to resolve the contents of a file, however I'm also getting encoded characters that I assume are for defining a file format. I know I could remove them by removing x characters from the beginning of the string although I feel theres probably a better safer way of properly reading the file.

container.exec({
  Cmd: ['sh', '-c', 'cat /myfile.json'],
  AttachStdin: true,
  AttachStdout: true,
  AttachStderr: true,
}, (err, exec) => {
  exec.start({
    stdin: true
  }, (err, stream) => {
    if (err) {
      return res.status(500).json({
        success: false,
        message: 'Error reading file.',
      });
    }
    let data = '';
    stream.on('data', (chunk) => {
      data += chunk;
    });
    stream.on('end', () => {
      return res.status(200).json({
        success: true,
        data
      });
    });
  });
});

The original contents of the file are [], however it returns \x01\x00\x00\x00\x00\x00\x00\x02[]

How can I only get the contents [] without using something like a substring method?

Finbar
  • 1,127
  • 1
  • 5
  • 17

1 Answers1

0

I could find no other solution that substringing the first eight characters, even using stream#setEncoding. If anyone has a better solution please feel free to post here, as this is still generally unresolved and should be treated as a hacky bodge.

Finbar
  • 1,127
  • 1
  • 5
  • 17