/
h54s.min.js
1 lines (1 loc) · 17.1 KB
/
h54s.min.js
1
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.h54s=e()}}(function(){return function e(t,r,o){function s(n,i){if(!r[n]){if(!t[n]){var l="function"==typeof require&&require;if(!i&&l)return l(n,!0);if(a)return a(n,!0);var c=new Error("Cannot find module '"+n+"'");throw c.code="MODULE_NOT_FOUND",c}var u=r[n]={exports:{}};t[n][0].call(u.exports,function(e){var r=t[n][1][e];return s(r?r:e)},u,u.exports,e,t,r,o)}return r[n].exports}for(var a="function"==typeof require&&require,n=0;n<o.length;n++)s(o[n]);return s}({1:[function(e,t,r){function o(e,t){Error.captureStackTrace&&Error.captureStackTrace(this),this.message=t,this.type=e}o.prototype=Object.create(Error.prototype,{constructor:{configurable:!1,enumerable:!1,writable:!1,value:o},name:{configurable:!1,enumerable:!1,writable:!1,value:"h54sError"}}),t.exports=o},{}],2:[function(e,t,r){var o=e("./error.js"),s=t.exports=function(t){function r(e){if(!e)return void this._ajax.setTimeout(this.ajaxTimeout);if("object"!=typeof e)throw new o("argumentError","First parameter should be config object");for(var t in e)e.hasOwnProperty(t)&&("url"!==t&&"loginUrl"!==t||"/"===e[t].charAt(0)||(e[t]="/"+e[t]),this[t]=e[t]);e.hostUrl&&("/"===e.hostUrl.charAt(e.hostUrl.length-1)&&(e.hostUrl=e.hostUrl.slice(0,-1)),this.hostUrl=e.hostUrl,this.url=e.hostUrl+this.url,this.loginUrl=e.hostUrl+this.loginUrl),this._ajax.setTimeout(this.ajaxTimeout)}if(this.maxXhrRetries=5,this.url="/SASStoredProcess/do",this.debug=!1,this.loginUrl="/SASLogon/Logon.do",this.retryAfterLogin=!0,this.sasApp="Stored Process Web App 9.3",this.ajaxTimeout=3e4,this.remoteConfigUpdateCallbacks=[],this._pendingCalls=[],this._ajax=e("./methods/ajax.js")(),r.call(this,t),t&&t.isRemoteConfig){var s=this;this._disableCalls=!0,this._ajax.get("h54sConfig.json").success(function(e){var o=JSON.parse(e.responseText);for(var a in o)o.hasOwnProperty(a)&&void 0===t[a]&&"isRemoteConfig"!==a&&(t[a]=o[a]);r.call(s,t);for(var n=0,i=s.remoteConfigUpdateCallbacks.length;i>n;n++){var l=s.remoteConfigUpdateCallbacks[n];l()}for(s._disableCalls=!1;s._pendingCalls.length>0;){var c=s._pendingCalls.shift(),u=c.sasProgram,p=c.callback,f=c.params;s.metadataRoot&&-1===c.params._program.indexOf(s.metadataRoot)&&(c.params._program=s.metadataRoot.replace(/\/?$/,"/")+c.params._program.replace(/^\//,"")),f._debug=s.debug?131:0,s.call(u,null,p,f)}}).error(function(e){throw new o("ajaxError","Remote config file cannot be loaded. Http status code: "+e.status)})}};s.version="0.11.0",s.prototype=e("./methods/methods.js"),s.Tables=e("./tables/tables.js"),e("./ie_polyfills.js")},{"./error.js":1,"./ie_polyfills.js":3,"./methods/ajax.js":5,"./methods/methods.js":6,"./tables/tables.js":8}],3:[function(e,t,r){t.exports=function(){Object.create||(Object.create=function(e,t){function r(){}if("undefined"!=typeof t)throw"The multiple-argument version of Object.create is not provided by this browser and cannot be shimmed.";return r.prototype=e,new r}),Object.keys||(Object.keys=function(){"use strict";var e=Object.prototype.hasOwnProperty,t=!{toString:null}.propertyIsEnumerable("toString"),r=["toString","toLocaleString","valueOf","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","constructor"],o=r.length;return function(s){if("object"!=typeof s&&("function"!=typeof s||null===s))throw new TypeError("Object.keys called on non-object");var a,n,i=[];for(a in s)e.call(s,a)&&i.push(a);if(t)for(n=0;o>n;n++)e.call(s,r[n])&&i.push(r[n]);return i}}()),Array.prototype.lastIndexOf||(Array.prototype.lastIndexOf=function(e){"use strict";if(void 0===this||null===this)throw new TypeError;var t,r,o=Object(this),s=o.length>>>0;if(0===s)return-1;for(t=s-1,arguments.length>1&&(t=Number(arguments[1]),t!=t?t=0:0!==t&&t!=1/0&&t!=-(1/0)&&(t=(t>0||-1)*Math.floor(Math.abs(t)))),r=t>=0?Math.min(t,s-1):s-Math.abs(t);r>=0;r--)if(r in o&&o[r]===e)return r;return-1})}()},{}],4:[function(e,t,r){var o={applicationLogs:[],debugData:[],sasErrors:[],failedRequests:[]},s={applicationLogs:100,debugData:20,failedRequests:20,sasErrors:100};t.exports.get={getSasErrors:function(){return o.sasErrors},getApplicationLogs:function(){return o.applicationLogs},getDebugData:function(){return o.debugData},getFailedRequests:function(){return o.failedRequests}},t.exports.clear={clearApplicationLogs:function(){o.applicationLogs.splice(0,o.applicationLogs.length)},clearDebugData:function(){o.debugData.splice(0,o.debugData.length)},clearSasErrors:function(){o.sasErrors.splice(0,o.sasErrors.length)},clearFailedRequests:function(){o.failedRequests.splice(0,o.failedRequests.length)},clearAllLogs:function(){this.clearApplicationLogs(),this.clearDebugData(),this.clearSasErrors(),this.clearFailedRequests()}},t.exports.addApplicationLog=function(e,t){if("blank"!==e){var r={message:e,time:new Date,sasProgram:t};o.applicationLogs.push(r),o.applicationLogs.length>s.applicationLogs&&o.applicationLogs.shift()}},t.exports.addDebugData=function(e,t,r,a){o.debugData.push({debugHtml:e,debugText:t,sasProgram:r,params:a,time:new Date}),o.debugData.length>s.debugData&&o.debugData.shift()},t.exports.addFailedRequest=function(e,t,r){o.failedRequests.push({responseHtml:e,responseText:t,sasProgram:r,time:new Date}),o.failedRequests.length>s.failedRequests&&o.failedRequests.shift()},t.exports.addSasErrors=function(e){for(o.sasErrors=o.sasErrors.concat(e);o.sasErrors.length>s.sasErrors;)o.sasErrors.shift()}},{}],5:[function(e,t,r){t.exports=function(){var e,t=3e4,r=function(r,o,s,a){var n={success:function(){},error:function(){}},i=XMLHttpRequest||ActiveXObject,l=new i("MSXML2.XMLHTTP.3.0");return l.open(r,o,!0),a||l.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),l.onreadystatechange=function(){4===l.readyState&&(clearTimeout(e),l.status>=200&&l.status<300?n.success.call(n,l):n.error.call(n,l))},t>0&&(e=setTimeout(function(){l.abort()},t)),l.send(s),{success:function(e){return n.success=e,this},error:function(e){return n.error=e,this}}},o=function(e){var t=[];for(var r in e)if(e.hasOwnProperty(r))if(e[r]instanceof Array)for(var o=0,s=e[r].length;s>o;o++)t.push(encodeURIComponent(r)+"="+encodeURIComponent(e[r][o]));else t.push(encodeURIComponent(r)+"="+encodeURIComponent(e[r]));return t.join("&")},s=function(e){var t=new FormData;for(var r in e)if(e.hasOwnProperty(r))if(e[r]instanceof Array)for(var o=0,s=e[r].length;s>o;o++)t.append(r,e[r][o]);else t.append(r,e[r]);return t};return{get:function(e,t){var s;"object"==typeof t&&(s=o(t));var a=s?e+"?"+s:e;return r("GET",a)},post:function(e,t,a){var n;return"object"==typeof t&&(n=a?s(t):o(t)),r("POST",e,n,a)},setTimeout:function(e){t=e}}}},{}],6:[function(e,t,r){var o=e("../error.js"),s=e("../logs.js");t.exports.call=function(e,t,r,a){var n=this,i=0,l=this.debug;if(!r||"function"!=typeof r)throw new o("argumentError","You must provide callback");if(!e)throw new o("argumentError","You must provide Sas program file path");if("string"!=typeof e)throw new o("argumentError","First parameter should be string");if(a||(a={_program:this.metadataRoot?this.metadataRoot.replace(/\/?$/,"/")+e.replace(/^\//,""):e,_debug:this.debug?131:0,_service:"default"}),t){if(!(t instanceof h54s.Tables))throw new o("argumentError","Wrong type of tables object");for(var c in t._tables)t._tables.hasOwnProperty(c)&&(a[c]=t._tables[c])}return this._disableCalls?void this._pendingCalls.push({sasProgram:e,callback:r,params:a}):void this._ajax.post(this.url,a,!0).success(function(t){if(n._utils.needToLogin.call(n,t)){if(n._pendingCalls.push({sasProgram:e,callback:r,params:a}),n._disableCalls)return;n._disableCalls=!0;try{var c=t.responseURL.match(/_sasapp=([^&]*)/);n.sasApp=c[1].replace(/\+/g," ")}catch(u){s.addApplicationLog("Cannot extract _sasapp parameter from login URL")}r(new o("notLoggedinError","You are not logged in"))}else{var p,f;if(l)try{p=n._utils.parseDebugRes(t.responseText,e,a),s.addApplicationLog(p.logmessage,e),p=n._utils.convertDates(p),f=n._utils.unescapeValues(p),r(void 0,f)}catch(u){if(u instanceof SyntaxError)r(new o("parseError",u.message));else if(u instanceof o)r(u);else{var h=new o("unknownError",u.message);h.stack=u.stack,r(h)}}else try{p=n._utils.parseRes(t.responseText,e,a),s.addApplicationLog(p.logmessage,e),p=n._utils.convertDates(p),f=n._utils.unescapeValues(p),r(void 0,f)}catch(u){if(u instanceof SyntaxError)i<n.maxXhrRetries?(n._ajax.post(n.url,a,!0).success(this.success).error(this.error),i++,s.addApplicationLog("Retrying #"+i,e)):(n._utils.parseErrorResponse(t.responseText,e),n._utils.addFailedResponse(t.responseText,e),r(new o("parseError","Unable to parse response json")));else if(u instanceof o)n._utils.parseErrorResponse(t.responseText,e),n._utils.addFailedResponse(t.responseText,e),r(u);else{n._utils.parseErrorResponse(t.responseText,e),n._utils.addFailedResponse(t.responseText,e);var g=new o("unknownError",u.message);g.stack=u.stack,r(g)}}}}).error(function(t){s.addApplicationLog("Request failed with status: "+t.status,e),r(new o("httpError",t.statusText))})},t.exports.login=function(e,t,r){var a=this;if(!e||!t)throw new o("argumentError","Credentials not set");if("string"!=typeof e||"string"!=typeof t)throw new o("argumentError","User and pass parameters must be strings");if(!r||"function"!=typeof r)throw new o("argumentError","You must provide callback");var n={_sasapp:a.sasApp,_service:"default",ux:e,px:t,username:e,password:t};for(var i in this._aditionalLoginParams)n[i]=this._aditionalLoginParams[i];this._ajax.post(this.loginUrl,n).success(function(e){if(a._utils.needToLogin.call(a,e))if(a._loginChanged||a._isNewLoginPage&&!a._aditionalLoginParams){delete a._loginChanged;var t=e.responseText.match(/<input.*"hidden"[^>]*>/g);t&&t.forEach(function(e){var t=e.match(/name="([^"]*)"\svalue="([^"]*)/);n[t[1]]=t[2]});var o=this.success,i=this.error;a._ajax.post(a.loginUrl,n).success(function(){a._ajax.get(a.url).success(o).error(i)}).error(this.error)}else s.addApplicationLog("Wrong username or password"),r(-1);else for(r(e.status),a._disableCalls=!1;a._pendingCalls.length>0;){var l=a._pendingCalls.shift(),c=l.sasProgram,u=l.callback,p=l.params;p._debug=a.debug?131:0,a.retryAfterLogin&&a.call(c,null,u,p)}}).error(function(e){s.addApplicationLog("Login failed with status code: "+e.status),r(e.status)})},t.exports.logout=function(e){this._ajax.get(this.url,{_action:"logoff"}).success(function(t){e()}).error(function(t){s.addApplicationLog("Logout failed with status code: "+t.status),e(t.status)})},t.exports.setDebugMode=function(){this.debug=!0},t.exports.unsetDebugMode=function(){this.debug=!1};for(var a in s.get)s.get.hasOwnProperty(a)&&(t.exports[a]=s.get[a]);for(var a in s.clear)s.clear.hasOwnProperty(a)&&(t.exports[a]=s.clear[a]);t.exports.onRemoteConfigUpdate=function(e){this.remoteConfigUpdateCallbacks.push(e)},t.exports._utils=e("./utils.js")},{"../error.js":1,"../logs.js":4,"./utils.js":7}],7:[function(e,t,r){var o=e("../logs.js"),s=e("../error.js"),a=/<title>(Stored Process Error|SASStoredProcess)<\/title>[\s\S]*<h2>Stored process not found:.*<\/h2>/;t.exports.parseRes=function(e,t,r){var o=e.match(a);if(o)throw new s("programNotFound","Sas program completed with errors");return JSON.parse(e.replace(/(\r\n|\r|\n)/g,""))},t.exports.parseDebugRes=function(e,t,r){var n=e.match(a);if(n)throw new s("programNotFound","Sas program completed with errors");patt=/^(.?--h54s-data-start--)([\S\s]*?)(--h54s-data-end--)/m,n=e.match(patt);var i=e.replace(patt,""),l=/<body.*>([\s\S]*)<\/body>/,c=i.match(l),u=c[1].replace(/<[^>]*>/g,"");if(u=this.decodeHTMLEntities(u),o.addDebugData(c[1],u,t,r),this.parseErrorResponse(e,t))throw new s("sasError","Sas program completed with errors");if(!n)throw new s("parseError","Unable to parse response json");var p=JSON.parse(n[2].replace(/(\r\n|\r|\n)/g,""));return p},t.exports.addFailedResponse=function(e,t){var r=/<script([\s\S]*)\/form>/,s=/display\s?:\s?none;?\s?/;e=e.replace(r,"").replace(s,"");var a=e.replace(/<[^>]*>/g,"");a=this.decodeHTMLEntities(a),o.addFailedRequest(e,a,t)},t.exports.unescapeValues=function(e){for(var t in e)"string"==typeof e[t]?e[t]=decodeURIComponent(e[t]):"object"==typeof e&&this.unescapeValues(e[t]);return e},t.exports.parseErrorResponse=function(e,t){var r=/ERROR(:\s|\s\d\d)(.*\.|.*\n.*\.)/gm,s=e.match(r);if(s){for(var a,n=0,i=s.length;i>n;n++)a=s[n].replace(/<[^>]*>/g,"").replace(/(\n|\s{2,})/g," "),a=this.decodeHTMLEntities(a),s[n]={sasProgram:t,message:a,time:new Date};return o.addSasErrors(s),!0}},t.exports.decodeHTMLEntities=function(e){var t=document.createElement("span"),r=e.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);/gi,function(e){return t.innerHTML=e,e=t.textContent||t.innerText});return r},t.exports.fromSasDateTime=function(e){var t=new Date("January 1, 1960 00:00:00"),r=e,o=t.getTimezoneOffset(),s=t.getTime(),a=1e3*r,n=a+s,i=new Date;i.setTime(n);var l=i.getTimezoneOffset(),c=60*(o-l)*1e3,u=n-c;return i.setTime(u),i},t.exports.convertDates=function(e){for(var t in e)"number"!=typeof e[t]||0!==t.indexOf("dt_")&&0!==t.indexOf("DT_")?"object"==typeof e&&this.convertDates(e[t]):e[t]=this.fromSasDateTime(e[t]);return e},t.exports.needToLogin=function(e){var t,r=/<form.+action="(.*Logon[^"]*).*>/,o=r.exec(e.responseText);if(o){var s=o[1].replace(/\?.*/,"");if("/"===s.charAt(0))t=this.hostUrl?this.hostUrl+s:s,t!==this.loginUrl&&(this._loginChanged=!0,this.loginUrl=t);else{var a=e.responseURL.lastIndexOf("/")+1,n=e.responseURL.substr(0,a).replace(/.*\/{2}[^\/]*/,"")+s;t=this.hostUrl?this.hostUrl+n:n,t!==this.loginUrl&&(this._loginChanged=!0,this.loginUrl=t)}var i=e.responseText.match(/<input.*"hidden"[^>]*>/g),l={};return i&&(this._isNewLoginPage=!0,i.forEach(function(e){var t=e.match(/name="([^"]*)"\svalue="([^"]*)/);l[t[1]]=t[2]}),this._aditionalLoginParams=l),!0}return!1}},{"../error.js":1,"../logs.js":4}],8:[function(e,t,r){function o(e,t,r){this._tables={},this._parameterThreshold=r||3e4,this.add(e,t)}var s=e("../error.js");o.prototype.add=function(e,t){if(!e||!t)throw new s("argumentError","Missing arguments");if(!(e instanceof Array))throw new s("argumentError","First argument must be array");if("string"!=typeof t)throw new s("argumentError","Second argument must be string");if(!isNaN(t[t.length-1]))throw new s("argumentError","Macro name cannot have number at the end");var r=this._utils.convertTableObject(e,this._parameterThreshold),o=[];o.push(JSON.stringify(r.spec));for(var a=0;a<r.data.length;a++){var n=JSON.stringify(r.data[a]);o.push(n)}this._tables[t]=o},o.prototype._utils=e("./utils.js"),t.exports=o},{"../error.js":1,"./utils.js":9}],9:[function(e,t,r){var o=e("../error.js"),s=e("../logs.js");t.exports.convertTableObject=function(e,t){function r(e){return"undefined"==typeof i[e.colName]?(i[e.colName]={},i[e.colName].colName=e.colName,i[e.colName].colType=e.colType,i[e.colName].colLength=e.colLength>0?e.colLength:1,0):i[e.colName].colType!==e.colType?-1:i[e.colName].colLength<e.colLength?(i[e.colName].colLength=e.colLength>0?e.colLength:1,0):void 0}var a=this;if(t>3e4&&console.warn("You should not set threshold larger than 30kb because of the SAS limitations"),"object"!=typeof e)throw new o("argumentError","The parameter passed to checkAndGetTypeObject is not an object");var n=e.length;if("number"!=typeof n)throw new o("argumentError","The parameter passed to checkAndGetTypeObject does not have a valid length and is most likely not an array");var i={},l=0,c=[],u=0;c[u]=[];for(var p=0,f=0;f<e.length;f++){c[u][p]={};var h=0;for(var g in e[f]){var d={},m=e[f][g];if(void 0!==m&&null!==m){if("number"==typeof m&&isNaN(m))throw new o("typeError","NaN value in one of the values (columns) is not allowed");if(m===-(1/0)||m===1/0)throw new o("typeError",m.toString()+" value in one of the values (columns) is not allowed");if(m===!0||m===!1)throw new o("typeError","Boolean value in one of the values (columns) is not allowed");var b=typeof m,v=m instanceof Date;if("number"===b?((m<Number.MIN_SAFE_INTEGER||m>Number.MAX_SAFE_INTEGER)&&s.addApplicationLog("Object["+f+"]."+g+" - This value exceeds expected numeric precision."),d.colName=g,d.colType="num",d.colLength=8,d.encodedLength=m.toString().length,c[u][p][g]=m):"string"!==b||v?v?(d.colName=g,d.colType="date",d.colLength=8,c[u][p][g]=a.toSasDateTime(m),d.encodedLength=c[u][p][g].toString().length):"object"==b&&(d.colName=g,d.colType="json",d.colLength=JSON.stringify(m).length,c[u][p][g]=encodeURIComponent(JSON.stringify(m)).replace(/'/g,"%27"),d.encodedLength=c[u][p][g].length):(d.colName=g,d.colType="string",d.colLength=m.length,""===m?c[u][p][g]=" ":c[u][p][g]=encodeURIComponent(m).replace(/'/g,"%27"),d.encodedLength=c[u][p][g].length),h=h+6+g.length+d.encodedLength,-1==r(d))throw new o("typeError","There is a type mismatch in the array between values (columns) of the same name.")}}if(0!==Object.keys(c[u][p]).length){if(h>t)throw new o("argumentError","Row "+p+" exceeds size limit of 32kb");if(l+h>t){var w=c[u].pop();u++,c[u]=[w],p=0,l=h}else l+=h;p++}else c[u].splice(p,1)}var y=[];for(var x in i)y.push(i[x]);return{spec:y,data:c,jsonLength:l}},t.exports.toSasDateTime=function(e){var t=new Date("January 1, 1960 00:00:00"),r=e,o=t.getTimezoneOffset(),s=r.getTimezoneOffset(),a=60*(s-o),n=t.getTime()/1e3,i=r.getTime()/1e3,l=Math.round(i-n-a);return l}},{"../error.js":1,"../logs.js":4}]},{},[2])(2)});