'use strict';

const https = require('https');

function _request(options, body) {
  return new Promise(function(resolve, reject) {
    const request = https.request(Object.assign({
      hostname: 'api.mojang.com',
      headers: { 'Content-Type': 'application/json' },
    }, options), function(resp) {
      let data = [];

      resp.on('data', function(chunk) {
        data.push(chunk);
      });

      resp.on('end', function() {
        data = JSON.parse(Buffer.concat(data).toString());
        resolve(data);
      });

      resp.on('error', reject);
    });
    request.on('error', reject);

    if (typeof body === 'object' && !(body instanceof Buffer)) {
      request.write(JSON.stringify(body));
    } else if (typeof body !== 'undefined') {
      request.write(body);
    }
    request.end();
  });
}

module.exports = _request;

'use strict';

const https = require('https');

function _request(options, body) {
  return new Promise(function(resolve, reject) {
    const request = https.request(Object.assign({
      hostname: 'api.mojang.com',
      headers: { 'Content-Type': 'application/json' },
    }, options), function(resp) {
      let data = [];

      resp.on('data', function(chunk) {
        data.push(chunk);
      });

      resp.on('end', function() {
        if(data.length < 1){
          return reject(new Error("No data received."));
        }
        data = JSON.parse(Buffer.concat(data).toString());
        resolve(data);
      });

      resp.on('error', reject);
    });
    request.on('error', reject);

    if (typeof body === 'object' && !(body instanceof Buffer)) {
      request.write(JSON.stringify(body));
    } else if (typeof body !== 'undefined') {
      request.write(body);
    }
    request.end();
  });
}

module.exports = _request;

Patch when no data received.
Patch when no data received.

If there was not a response from the Mojang servers then an error would occur:

```
SyntaxError: Unexpected end of input
    at Object.parse (native)
    at IncomingMessage.<anonymous> (/home/player-info/node_modules/mojang/lib/_request.js:18:21)
    at emitNone (events.js:85:20)
    at IncomingMessage.emit (events.js:179:7)
    at endReadableNT (_stream_readable.js:913:12)
    at _combinedTickCallback (node.js:377:13)
    at process._tickCallback (node.js:401:11)
```

You can replicate by looking up a username which does not exist.