UNPKG

minestat-es

Version:

Promise-based Minecraft server status

2 lines (1 loc) 5.04 kB
import{hrtime as r}from"process";import e from"varint";import{connect as n}from"net";var t=function(){return t=Object.assign||function(r){for(var e,n=1,t=arguments.length;n<t;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(r[o]=e[o]);return r},t.apply(this,arguments)};function o(r,e,n,t){return new(n||(n=Promise))(function(o,a){function i(r){try{f(t.next(r))}catch(r){a(r)}}function u(r){try{f(t.throw(r))}catch(r){a(r)}}function f(r){var e;r.done?o(r.value):(e=r.value,e instanceof n?e:new n(function(r){r(e)})).then(i,u)}f((t=t.apply(r,e||[])).next())})}function a(r,e){var n,t,o,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=u(0),i.throw=u(1),i.return=u(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(u){return function(f){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,u[0]&&(a=0)),a;)try{if(n=1,t&&(o=2&u[0]?t.return:u[0]?t.throw||((o=t.return)&&o.call(t),0):t.next)&&!(o=o.call(t,u[1])).done)return o;switch(t=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return a.label++,{value:u[1],done:!1};case 5:a.label++,t=u[1],u=[0];continue;case 7:u=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){a=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){a.label=u[1];break}if(6===u[0]&&a.label<o[1]){a.label=o[1],o=u;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(u);break}o[2]&&a.ops.pop(),a.trys.pop();continue}u=e.call(r,a)}catch(r){u=[6,r],t=0}finally{n=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,f])}}}"function"==typeof SuppressedError&&SuppressedError;var i,u,f=Buffer.from([254,1]),c=function(){function r(){}return r.prototype.handshakePacket=function(){return f},r.prototype.pingPacket=function(){return f},r.prototype.parse=function(r){if(!(null==r?void 0:r.length))return{online:!1,error:new Error("Got empty reply from Minecraft server!")};if(255!==r[0])return{online:!1,error:new Error("Got invalid reply from Minecraft server!")};var e=r.readInt16BE(1);if(e<0||e>r.byteLength)return{online:!1,error:new Error("Got invalid reply from Minecraft server!")};var n=Buffer.alloc(2*e,0,"binary");r.copy(n,0,3),n.swap16();var t=n.toString("utf-16le").split("\0");if(t.length<6)return{online:!1,error:new Error("Got short reply from Minecraft server!")};var o=t[2],a=t[3],i=parseInt(t[4],10),u=parseInt(t[5],10);return isNaN(i)||isNaN(u)?{online:!1,error:new Error("Failed to parse player count numbers!")}:{online:!0,version:o,motd:a,players:i,maxPlayers:u}},r}(),l=e.encode,p=e.encodingLength,s=function(r,e){return Buffer.concat([Buffer.from(l(d(r)+e.length)),Buffer.from(l(r)),e])},y=l,d=p,v=function(r,e){var n;return void 0===e&&(e=""),(null===(n=r.extra)||void 0===n?void 0:n.length)&&(e+=r.extra.map(function(r){return v(r,"")}).join("")),e+=r.text},h=function(){function r(){}return r.prototype.handshakePacket=function(r,e){var n=Buffer.alloc(2);n.writeUInt16BE(e);var t=s(0,Buffer.concat([Buffer.from(y(757)),Buffer.from(y(r.length)),Buffer.from(r),n,Buffer.from(y(1))])),o=s(0,Buffer.alloc(0));return Buffer.concat([t,o])},r.prototype.pingPacket=function(){var r=Buffer.alloc(8);return r.writeUint32BE(0),s(1,r)},r.prototype.parse=function(r){if(!(null==r?void 0:r.length))return{online:!1,error:new Error("Got empty reply from Minecraft server!")};var e=r.subarray(2*d(r.length)+1);try{var n=JSON.parse(e.toString("utf-8"));return{online:!0,players:n.players.online,maxPlayers:n.players.max,version:n.version.name,motd:v(n.description),playerInfo:n.players.sample}}catch(r){return{online:!1,error:r}}},r}();!function(r){r.Black="black",r.DarkBlue="dark_blue",r.DarkGreen="dark_green",r.DarkCyan="dark_aqua",r.DarkRed="dark_red",r.DarkGray="dark_gray",r.Purple="dark_purple",r.Gold="gold",r.Gray="gray",r.Blue="blue",r.Green="green",r.Cyan="aqua",r.Red="red",r.Pink="light_purple",r.Yellow="yellow",r.White="white"}(i||(i={})),function(r){r.Legacy="legacy",r.Modern="modern"}(u||(u={}));var m=function(r,e){return"".concat(r.startsWith(e)?"":e).concat(r)},g=function(e){var n=r(e);return 1e3*n[0]+n[1]/1e6},w=function(e,o,a,i,f){return void 0===a&&(a=u.Legacy),void 0===i&&(i=5e3),void 0===f&&(f=!1),new Promise(function(l,p){try{var s=null,y=[0,0],d=null;switch(a){case u.Legacy:s=new c;break;case u.Modern:s=new h}var v=function(r){return l({online:!1,error:r})},m=n(o,e,function(){s instanceof c&&(y=r()),m.write(s.handshakePacket(e,o))});m.setTimeout(i,function(){m.end(),v()}),m.on("error",function(r){m.end(),v(r)}),m.on("data",function(e){if(s instanceof c){var n=g(y);l(t(t({},s.parse(e)),{pingMs:n}))}else if(f)if(d){n=g(y);m.end(),l(t(t({},d),{pingMs:n}))}else d=s.parse(e),y=r(),m.write(s.pingPacket(),function(){m.on("close",function(){var r=g(y);m.end(),l(t(t({},d),{pingMs:r}))})});else m.end(),l(s.parse(e))})}catch(r){p(r)}})};export{u as Q,o as _,a,w as g,m as p};