minestat-es
Version:
Promise-based Minecraft server status
2 lines (1 loc) • 5.5 kB
JavaScript
import{connect as r}from"net";import{hrtime as e}from"process";import{promises as n}from"dns";import t from"varint";var o=function(){return o=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},o.apply(this,arguments)};function a(r,e,n,t){return new(n||(n=Promise))(function(o,a){function i(r){try{l(t.next(r))}catch(r){a(r)}}function u(r){try{l(t.throw(r))}catch(r){a(r)}}function l(r){var e;r.done?o(r.value):(e=r.value,e instanceof n?e:new n(function(r){r(e)})).then(i,u)}l((t=t.apply(r,e||[])).next())})}function i(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(l){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,l])}}}"function"==typeof SuppressedError&&SuppressedError;var u,l,c=Buffer.from([254,1]),f=function(){function r(){}return r.prototype.handshakePacket=function(){return c},r.prototype.pingPacket=function(){return c},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}(),s=t.encode,p=t.encodingLength,y=function(r,e){return Buffer.concat([Buffer.from(s(h(r)+e.length)),Buffer.from(s(r)),e])},d=s,h=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},m=function(){function r(){}return r.prototype.handshakePacket=function(r,e){var n=Buffer.alloc(2);n.writeUInt16BE(e);var t=y(0,Buffer.concat([Buffer.from(d(757)),Buffer.from(d(r.length)),Buffer.from(r),n,Buffer.from(d(1))])),o=y(0,Buffer.alloc(0));return Buffer.concat([t,o])},r.prototype.pingPacket=function(){var r=Buffer.alloc(8);return r.writeUint32BE(0),y(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*h(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"}(u||(u={})),function(r){r.Legacy="legacy",r.Modern="modern"}(l||(l={}));var g=function(r){var n=e(r);return 1e3*n[0]+n[1]/1e6};function w(t){return a(this,void 0,void 0,function(){var a,u,c,s,p,y;return i(this,function(i){switch(i.label){case 0:return a=null,u=null,"hostname"in t?(d=t.hostname,h="_minecraft._tcp.",c="".concat(d.startsWith(h)?"":h).concat(d),[4,n.resolveSrv(c)]):[3,2];case 1:if(!(s=i.sent()).length)throw new Error("No DNS records found for hostname ".concat(c));return p=s[Math.floor(Math.random()*s.length)],a=p.name,u=p.port,[3,3];case 2:a=(y=t).address,u=y.port,i.label=3;case 3:return t.timeout||(t.timeout=5e3),t.protocol||(t.protocol=l.Legacy),[2,new Promise(function(n,i){try{var c=null,s=[0,0],p=null;switch(t.protocol){case l.Legacy:c=new f;break;case l.Modern:c=new m}var y=function(r){return n({online:!1,error:r})},d=r(u,a,function(){c instanceof f&&(s=e()),d.write(c.handshakePacket(a,u))});d.setTimeout(t.timeout,function(){d.end(),y()}),d.on("error",function(r){d.end(),y(r)}),d.on("data",function(r){if(c instanceof f){var a=g(s);n(o(o({},c.parse(r)),{pingMs:a}))}else if(t.ping)if(p){a=g(s);d.end(),n(o(o({},p),{pingMs:a}))}else p=c.parse(r),s=e(),d.write(c.pingPacket(),function(){d.on("close",function(){var r=g(s);d.end(),n(o(o({},p),{pingMs:r}))})});else d.end(),n(c.parse(r))})}catch(r){i(r)}})]}var d,h})})}export{l as QueryProtocols,w as fetchServerInfo};