1 line
No EOL
5.8 KiB
JavaScript
1 line
No EOL
5.8 KiB
JavaScript
!function(o,e){"object"==typeof exports&&"undefined"!=typeof module?e():"function"==typeof define&&define.amd?define(e):e()}(this,function(){"use strict";function o(o,e,t){n._mode="browser";var r=new window.XMLHttpRequest;if(!window)throw new Error('no global browser "window" object found.');if(!window.location)throw new Error('no global browser "window.location" object found.');o.protocol=o.protocol||window.location.protocol||"http",":"!==o.protocol[o.protocol.length-1]&&(o.protocol+=":"),o.hostname=o.hostname||o.host||window.location.hostname;var a=80;o.protocol&&(a=o.protocol.indexOf("https")!==-1?443:80);var s=o.hostname.toLowerCase().trim(),i=window.location.hostname.toLowerCase().trim();s.indexOf(i)!==-1&&(a=window.location.port||a),o.port=o.port||a;var p=o.protocol+"//"+(o.hostname+(o.port?":"+o.port:"")),l=p+o.path;n.debug&&console.log("dasu: url ["+l+"]"),n.debug&&(console.log("--- dasu opts ---"),console.log(o),console.log()),r.open(o.method,l,!0),r.onload=function(){t(null,r,r.responseText)},r.onerror=function(){var n=""+o.method.toUpperCase()+" "+l,a={name:"NetworkError",message:r.responseText||"NetworkError: XMLHttpRequest ("+n+")",desc:n,url:l,opts:o,data:e,origin:p,responseText:r.responseText};a.toString=function(){return a.message+"\n"+JSON.stringify(o,null,2).split(",")},a.message=a.toString(),t(a)};for(var d=Object.keys(o.headers),c=0;c<d.length;c++){var h=d[c].toLowerCase(),u=o.headers[h];r.setRequestHeader(h,u)}try{r.send(e)}catch(n){var f=""+o.method.toUpperCase()+" "+l;n.desc=f,n.url=l,n.opts=o,n.data=e,n.origin=p,n.toString=function(){return n.message+"\n"+JSON.stringify(o,null,2).split(",")},t(n)}return{abort:function(){r.abort()}}}function e(o,e,t){n._mode="node";var a=r("http"),s=r("https"),i=r("zlib");o=o||{},o.hostname=o.hostname||o.host,o.protocol=o.protocol||"http";var p=a;o.protocol&&o.protocol.indexOf("https")!==-1&&(p=s),":"!==o.protocol[o.protocol.length-1]&&(o.protocol+=":"),n.debug&&(console.log("--- dasu opts ---"),console.log(o),console.log());var l=p.request(o,function(o){var e=[],n=o;if(i.createBrotliDecompress&&"br"===o.headers["content-encoding"]){var r=i.createBrotliDecompress();n=r,o.pipe(r)}if(i.createGunzip&&"gzip"===o.headers["content-encoding"]){var a=i.createGunzip();n=a,o.pipe(a)}if(i.createDeflate&&"deflate"===o.headers["content-encoding"]){var s=i.createDeflate();n=s,o.pipe(s)}var p=o.headers["content-type"];p&&p.indexOf("text/")>=0?p.indexOf("ascii")>=0?n.setEncoding("ascii"):n.setEncoding("utf8"):n.setEncoding("binary"),n.on("data",function(o){e.push(o)}),n.on("end",function(){var n=e.join("");o.responseText=o.responseText||n,t(null,o,n)})});return l.on("error",function(n){var r=o.protocol+"//"+(o.hostname+(o.port?":"+o.port:"")),a=r+o.path,s=""+o.method.toUpperCase()+" "+a;n.desc=s,n.url=a,n.opts=o,n.data=e,n.origin=r,n.toString=function(){return n.message+"\n"+JSON.stringify(o,null,2).split(",")},n.message=n.toString(),t(n)}),n.debug&&(console.log("dasu typeof dataString: "+typeof e),e&&e.length>=0&&console.log("dasu sending length: "+e.length)),l.write(e),l.end(),{abort:function(){l.abort()}}}function t(t,s){function i(o,e,l){if(o||void 0===e)s(o);else{if(!e.getResponseHeader&&e.headers&&(e.getResponseHeader=function(o){return e.headers[o]}),e.getAllResponseHeaders&&!e.headers&&(e.headers=e.getAllResponseHeaders()),!e.getAllResponseHeaders&&e.headers&&(e.getAllResponseHeaders=function(){return e.headers}),void 0===e.status?e.status=e.statusCode:e.statusCode=e.status,n.debug&&(n.log+="\n-------------\n",n.log+=new Date,n.log+="headers:\n"+JSON.stringify(e.headers,null,2)+"\n\n",n.log+="body:\n"+l+"\n"),n.follow&&e.status>=300&&e.status<400&&e.headers.location&&m<w){m++;var d=e.headers.location;if("//"===d.slice(0,2)&&(d=t.protocol+":"+d),"/"===d[0])return g.path=d,a(g,h,i);var c;if("undefined"!=typeof URL?c=new URL(d):"node"===p&&(c=r("url").parse(d)),n.debug)if("node"===p)try{var u=r("fs");u.writeFileSync("debug.dasu-response.log",n.log,"utf8")}catch(o){}else console.log(n.log);if(c){Object.keys(g).forEach(function(o){g[o]=c[o]}),g.protocol=c.protocol,g.host=c.host,g.hostname=c.hostname,g.port=c.port;var f=c.path||c.pathname||"",b=c.search||"",v=f+b;return g.path=v,g.pathname=v,a(g,h,i)}}s(void 0,e,l)}}var p;if("browser"===n.mode||"node"!==n.mode&&"undefined"!=typeof window&&"undefined"!=typeof window.XMLHttpRequest?(p="browser",a=o):(p="node",a=e),"string"==typeof t){var l;if(l="undefined"!=typeof URL?new URL(t):r("url").parse(t),"object"!=typeof l)throw new Error("failed to parse params shorthand: "+t);t={},t.method="GET",t.protocol=l.protocol,t.host=l.host,t.hostname=l.hostname,t.port=l.port,t.path=l.path||l.pathname}var d="",c=t.data||t.json||"",h="";switch(typeof c){case"object":c.toString==={}.toString?(h=JSON.stringify(c),d="application/json"):(n.debug&&console.log("dasu: complex object data"),h=c,d="application/octet-stream");break;case"string":if(c.length>1&&("{"===c[0]||"["===c[0]))try{JSON.parse(c),console&&console.warn?console.warn("[WARNING] dasu: Sending data that may be JSON as text/plain"):console.log("[WARNING] dasu: Sending data that may be JSON as text/plain")}catch(o){}h=c,d="text/plain";break;default:h=""+c,d="text/plain"}t=Object.assign({},t),delete t.data,d&&(t.headers=Object.assign({},{"content-type":d},t.headers||{}),"application/octet-stream"===d&&h&&h.length&&(t.headers=Object.assign({},{"content-length":h.length},t.headers||{}))),t.method=t.method||"GET",t.path=t.path||"/","/"!==t.path[0]&&(t.path="/"+t.path);var u="http";if(t.port){var f=Number(t.port);String(f)===t.port&&(t.port=f),443===t.port&&(u="https")}t.protocol||(t.protocol=u);var g={protocol:t.protocol,host:t.host,hostname:t.hostname,port:t.port,path:t.path,method:t.method,headers:t.headers},m=0,w=3;return n.log="",a(g,h,i)}var n={follow:!0,mode:"auto"},r=require,a=function(){throw new Error("dasu: no _request implementation found!")};n.xhr=function(o,e){t(o,function(o,t,n){e(o,n)})},n.req=t,module.exports=n}); |