15 lines
75 KiB
JavaScript
15 lines
75 KiB
JavaScript
|
"use strict";var e=require("fs"),t=require("url"),s=require("child_process"),r=require("http"),i=require("https"),n=require("stream"),o=require("zlib"),a=require("net"),h=require("tls"),c=require("crypto"),l=require("events");function d(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function u(e,t){return t.forEach((function(t){t&&"string"!=typeof t&&!Array.isArray(t)&&Object.keys(t).forEach((function(s){if("default"!==s&&!(s in e)){var r=Object.getOwnPropertyDescriptor(t,s);Object.defineProperty(e,s,r.get?r:{enumerable:!0,get:function(){return t[s]}})}}))})),Object.freeze(e)}var p=d(e),f=d(t),_=d(s),y=d(r),m=d(i),g=d(n),b=d(o),v=d(a),E=d(h),w=d(c),S=d(l);const k=Object.create(null);k.open="0",k.close="1",k.ping="2",k.pong="3",k.message="4",k.upgrade="5",k.noop="6";const x=Object.create(null);Object.keys(k).forEach((e=>{x[k[e]]=e}));const O={type:"error",data:"parser error"},T=({type:e,data:t},s,r)=>{if(t instanceof ArrayBuffer||ArrayBuffer.isView(t)){const e=N(t);return r(R(e,s))}return r(k[e]+(t||""))},N=e=>Buffer.isBuffer(e)?e:e instanceof ArrayBuffer?Buffer.from(e):Buffer.from(e.buffer,e.byteOffset,e.byteLength),R=(e,t)=>t?e:"b"+e.toString("base64"),C=(e,t)=>{if("string"!=typeof e)return{type:"message",data:L(e,t)};const s=e.charAt(0);if("b"===s){const s=Buffer.from(e.substring(1),"base64");return{type:"message",data:L(s,t)}}return x[s]?e.length>1?{type:x[s],data:e.substring(1)}:{type:x[s]}:O},L=(e,t)=>{const s=Buffer.isBuffer(e);return"arraybuffer"===t&&s?A(e):e},A=e=>{const t=new ArrayBuffer(e.length),s=new Uint8Array(t);for(let t=0;t<e.length;t++)s[t]=e[t];return t},B=String.fromCharCode(30);function P(e){if(e)return function(e){for(var t in P.prototype)e[t]=P.prototype[t];return e}(e)}P.prototype.on=P.prototype.addEventListener=function(e,t){return this._callbacks=this._callbacks||{},(this._callbacks["$"+e]=this._callbacks["$"+e]||[]).push(t),this},P.prototype.once=function(e,t){function s(){this.off(e,s),t.apply(this,arguments)}return s.fn=t,this.on(e,s),this},P.prototype.off=P.prototype.removeListener=P.prototype.removeAllListeners=P.prototype.removeEventListener=function(e,t){if(this._callbacks=this._callbacks||{},0==arguments.length)return this._callbacks={},this;var s,r=this._callbacks["$"+e];if(!r)return this;if(1==arguments.length)return delete this._callbacks["$"+e],this;for(var i=0;i<r.length;i++)if((s=r[i])===t||s.fn===t){r.splice(i,1);break}return 0===r.length&&delete this._callbacks["$"+e],this},P.prototype.emit=function(e){this._callbacks=this._callbacks||{};for(var t=new Array(arguments.length-1),s=this._callbacks["$"+e],r=1;r<arguments.length;r++)t[r-1]=arguments[r];if(s){r=0;for(var i=(s=s.slice(0)).length;r<i;++r)s[r].apply(this,t)}return this},P.prototype.emitReserved=P.prototype.emit,P.prototype.listeners=function(e){return this._callbacks=this._callbacks||{},this._callbacks["$"+e]||[]},P.prototype.hasListeners=function(e){return!!this.listeners(e).length};const I=global;function D(e,...t){return t.reduce(((t,s)=>(e.hasOwnProperty(s)&&(t[s]=e[s]),t)),{})}const U=setTimeout,q=clearTimeout;function j(e,t){t.useNativeTimers?(e.setTimeoutFn=U.bind(I),e.clearTimeoutFn=q.bind(I)):(e.setTimeoutFn=setTimeout.bind(I),e.clearTimeoutFn=clearTimeout.bind(I))}class F extends Error{constructor(e,t,s){super(e),this.description=t,this.context=s,this.type="TransportError"}}class M extends P{constructor(e){super(),this.writable=!1,j(this,e),this.opts=e,this.query=e.query,this.readyState="",this.socket=e.socket}onError(e,t,s){return super.emitReserved("error",new F(e,t,s)),this}open(){return"closed"!==this.readyState&&""!==this.readyState||(this.readyState="opening",this.doOpen()),this}close(){return"opening"!==this.readyState&&"open"!==this.readyState||(this.doClose(),this.onClose()),this}send(e){"open"===this.readyState&&this.write(e)}onOpen(){this.readyState="open",this.writable=!0,super.emitReserved("open")}onData(e){const t=C(e,this.socket.binaryType);this.onPacket(t)}onPacket(e){super.emitReserved("packet",e)}onClose(e){this.readyState="closed",super.emitReserved("close",e)}}const W="01234567
|
||
|
/**
|
||
|
* Wrapper for built-in http.js to emulate the browser XMLHttpRequest object.
|
||
|
*
|
||
|
* This can be used with JS designed for browsers to improve reuse of code and
|
||
|
* allow the use of existing libraries.
|
||
|
*
|
||
|
* Usage: include("XMLHttpRequest.js") and use XMLHttpRequest per W3C specs.
|
||
|
*
|
||
|
* @author Dan DeFelippi <dan@driverdan.com>
|
||
|
* @contributor David Ellis <d.f.ellis@ieee.org>
|
||
|
* @license MIT
|
||
|
*/
|
||
|
var X=p.default,J=f.default,Q=_.default.spawn,Z=ee;function ee(e){e=e||{};var t,s,r=this,i=y.default,n=m.default,o={},a=!1,h={"User-Agent":"node-XMLHttpRequest",Accept:"*/*"},c=Object.assign({},h),l=["accept-charset","accept-encoding","access-control-request-headers","access-control-request-method","connection","content-length","content-transfer-encoding","cookie","cookie2","date","expect","host","keep-alive","origin","referer","te","trailer","transfer-encoding","upgrade","via"],d=["TRACE","TRACK","CONNECT"],u=!1,p=!1,f=!1,_={};this.UNSENT=0,this.OPENED=1,this.HEADERS_RECEIVED=2,this.LOADING=3,this.DONE=4,this.readyState=this.UNSENT,this.onreadystatechange=null,this.responseText="",this.responseXML="",this.status=null,this.statusText=null;this.open=function(e,t,s,r,i){if(this.abort(),p=!1,f=!1,!function(e){return e&&-1===d.indexOf(e)}(e))throw new Error("SecurityError: Request method not allowed");o={method:e,url:t.toString(),async:"boolean"!=typeof s||s,user:r||null,password:i||null},g(this.OPENED)},this.setDisableHeaderCheck=function(e){a=e},this.setRequestHeader=function(e,t){if(this.readyState!=this.OPENED)throw new Error("INVALID_STATE_ERR: setRequestHeader can only be called when state is OPEN");if(!function(e){return a||e&&-1===l.indexOf(e.toLowerCase())}(e))return console.warn('Refused to set unsafe header "'+e+'"'),!1;if(u)throw new Error("INVALID_STATE_ERR: send flag is true");return c[e]=t,!0},this.getResponseHeader=function(e){return"string"==typeof e&&this.readyState>this.OPENED&&s.headers[e.toLowerCase()]&&!p?s.headers[e.toLowerCase()]:null},this.getAllResponseHeaders=function(){if(this.readyState<this.HEADERS_RECEIVED||p)return"";var e="";for(var t in s.headers)"set-cookie"!==t&&"set-cookie2"!==t&&(e+=t+": "+s.headers[t]+"\r\n");return e.substr(0,e.length-2)},this.getRequestHeader=function(e){return"string"==typeof e&&c[e]?c[e]:""},this.send=function(a){if(this.readyState!=this.OPENED)throw new Error("INVALID_STATE_ERR: connection must be opened before send() is called");if(u)throw new Error("INVALID_STATE_ERR: send has already been called");var h,l=!1,d=!1,f=J.parse(o.url);switch(f.protocol){case"https:":l=!0;case"http:":h=f.hostname;break;case"file:":d=!0;break;case void 0:case"":h="localhost";break;default:throw new Error("Protocol not supported.")}if(d){if("GET"!==o.method)throw new Error("XMLHttpRequest: Only GET method is supported");if(o.async)X.readFile(unescape(f.pathname),"utf8",(function(e,t){e?r.handleError(e,e.errno||-1):(r.status=200,r.responseText=t,g(r.DONE))}));else try{this.responseText=X.readFileSync(unescape(f.pathname),"utf8"),this.status=200,g(r.DONE)}catch(e){this.handleError(e,e.errno||-1)}}else{var _=f.port||(l?443:80),y=f.pathname+(f.search?f.search:"");if(c.Host=h,l&&443===_||80===_||(c.Host+=":"+f.port),o.user){void 0===o.password&&(o.password="");var m=new Buffer(o.user+":"+o.password);c.Authorization="Basic "+m.toString("base64")}"GET"===o.method||"HEAD"===o.method?a=null:a?(c["Content-Length"]=Buffer.isBuffer(a)?a.length:Buffer.byteLength(a),c["Content-Type"]||(c["Content-Type"]="text/plain;charset=UTF-8")):"POST"===o.method&&(c["Content-Length"]=0);var b=e.agent||!1,v={host:h,port:_,path:y,method:o.method,headers:c,agent:b};if(l&&(v.pfx=e.pfx,v.key=e.key,v.passphrase=e.passphrase,v.cert=e.cert,v.ca=e.ca,v.ciphers=e.ciphers,v.rejectUnauthorized=!1!==e.rejectUnauthorized),p=!1,o.async){var E=l?n.request:i.request;u=!0,r.dispatchEvent("readystatechange");var w=function(i){if(302===(s=i).statusCode||303===s.statusCode||307===s.statusCode){o.url=s.headers.location;var n=J.parse(o.url);h=n.hostname;var a={hostname:n.hostname,port:n.port,path:n.path,method:303===s.statusCode?"GET":o.method,headers:c};return l&&(a.pfx=e.pfx,a.key=e.key,a.passphrase=e.passphrase,a.cert=e.cert,a.ca=e.ca,a.ciphers=e.ciphers,a.rejectUnauthorized=!1!==e.rejectUnauthorized),void(t=E(a,w).on("error",S)).end()}s&&s.setEncoding&&s.setEncoding("utf8"),g(r.HEADERS_RECEIVED),r.status=s.statusCode,s.on("data",(function(e){e&&(r.responseText+=e),u&&g(r.LOADING)})),s.on("end",(function(){u&&(u=!1,g(r.DONE))})),s.
|