Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to demux a buffer #129

Open
Mrono opened this issue Mar 27, 2021 · 0 comments
Open

Add the ability to demux a buffer #129

Mrono opened this issue Mar 27, 2021 · 0 comments

Comments

@Mrono
Copy link

Mrono commented Mar 27, 2021

Currently the docker api returns a multiplexed buffer for log files when follow=false. As it is not a stream the current function of demuxStream doesn't function with it. This is an idea I had in mind for lib/modem.js along side demuxStream

Modem.prototype.demuxBuffer = function(buffer) {
  return new Promise((res, rej) => {
    var nextDataType = null;
    var nextDataLength = null;
    var output = {
      stdout: [],
      stderr: []
    }
    function processData() {
      if (!nextDataType) {
        if (buffer.length >= 8) {
          var header = bufferSlice(8);
          nextDataType = header.readUInt8(0);
          nextDataLength = header.readUInt32BE(4);
          // It's possible we got a "data" that contains multiple messages
          // Process the next one
          processData();
        }
      } else {
        if (buffer.length >= nextDataLength) {
          var content = bufferSlice(nextDataLength);
          if (nextDataType === 1) {
            output.stdout.push(content);
          } else {
            output.stderr.push(content);
          }
          nextDataType = null;
          // It's possible we got a "data" that contains multiple messages
          // Process the next one
          processData();

          // Done processing, send data
          if (buffer.length == 0) {
            res(output);
          }
        }
      }
    }

    function bufferSlice(end) {
      var out = buffer.slice(0, end);
      buffer = Buffer.from(buffer.slice(end, buffer.length));
      return out;
    }

    processData();
  });
}
@Mrono Mrono changed the title Add the ability to demux a string Add the ability to demux a buffer Mar 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant