molnarg / node-http2

An HTTP/2 client and server implementation for node.js
MIT License
1.79k stars 187 forks source link

Avoid the Assertion error? #244

Open 5nyper opened 7 years ago

5nyper commented 7 years ago

At times I would get this error:

assert.js:85
  throw new assert.AssertionError({
  ^
AssertionError: false == true
    at Connection._send (/Users/vikaton/Desktop/speech2text/node_modules/http2/lib/protocol/connection.js:326:9)
    at runCallback (timers.js:651:20)
    at tryOnImmediate (timers.js:624:5)
    at processImmediate [as _immediateCallback] (timers.js:596:5)

when running this function

function post(audioBuffer) {
  return new Promise((resolve, reject) => {
   // some headers and content
  let req = require('http2').request(options, function(res) {
      console.log("CALLBACK HAS BEEN CALLED BACK")
      console.log(res.statusCode)
      if (res.statusCode > 204) {
          console.log("ERR with return code " + res.statusCode)
          req.end('', function(){
            console.log('ended request cause of bad code')
          })
          reject('BAD CODE')
      }
      else if (res.statusCode == 204) {
        in_session = false
        player.play('204.mp3', function(err) {
          if (err) throw err
        })
        reject('NO REPSONSE') . // usually this doesnt fire cause I get The assertion error here
      }
        streamToBuffer(res, function(err, buffer) {
            console.log('response', buffer.length);
            if (err) {
                console.error('error', err);
                return false;
            }

            let parsedMessage = httpParser(buffer);
            var multipart = parsedMessage.multipart;

            if (Array.isArray(multipart)) {
                multipart.forEach(function(part) {
                    //console.log(part)
                    var headers = part.headers;
                    var bodyBuffer = part.body;
                    var contentType = _.get(headers, 'Content-Type');

                    if (bodyBuffer) {
                        if (contentType === 'application/octet-stream') {
                            fs.writeFileSync('audio.mp3', bodyBuffer)
                            player.play('audio.mp3', function(err) {
                                if (err) throw err
                  in_session = false
                  resolve('SUCCESS')
                            })
                        } else if (contentType === 'application/json') {
                            var body = JSON.parse(bodyBuffer.toString('utf8'));
                            console.log(body) //bookmark
                        }
                    }
                });
            }
        });

        req.on('error', function(e) {
            console.log('problem with request: ' + e.message);
        });
      req.setTimeout(5000, function(){
        in_session = false
        console.log('timed out')
      })
    });
    console.log('REQ IS ' + req)
    if (isStream(audioBuffer)) {
        streamToBuffer(audioBuffer, function(error, buffer) {
            if (error) {
                console.error(error);
                return false;
            }
            sendRequest(buffer); //check before this
        });
    } else if (isBuffer(audioBuffer)) {
        sendRequest(audioBuffer);
    } else {
        console.error('Audio buffer invalid');
    }

    function sendRequest(audBuffer) {
      console.log('should sent data')
        req.write(postDataStart);
        console.log(postDataStart)
        req.write(audBuffer);
        console.log(audBuffer)
        req.write(postDataEnd);
        console.log(postDataEnd)
        req.end(null, function(){
        console.log('ended request and sent data')
      })
    }
  })
}

My question is, what is the underlying cause for this error in the code (if its even the code thats the problem) and how can I avoid it?

5nyper commented 7 years ago

Apologies for the horrible formatting :/

zaknuces commented 7 years ago

239