UNPKG

fa-toolkit

Version:

a devtool for fast app

1 lines 10.1 kB
"use strict";function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function _regeneratorRuntime(){_regeneratorRuntime=function(){return a};var a={},t=Object.prototype,s=t.hasOwnProperty,u=Object.defineProperty||function(t,e,r){t[e]=r.value},e="function"==typeof Symbol?Symbol:{},n=e.iterator||"@@iterator",r=e.asyncIterator||"@@asyncIterator",o=e.toStringTag||"@@toStringTag";function i(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{i({},"")}catch(t){i=function(t,e,r){return t[e]=r}}function c(t,e,r,n){var o,i,a,c,e=e&&e.prototype instanceof h?e:h,e=Object.create(e.prototype),n=new b(n||[]);return u(e,"_invoke",{value:(o=t,i=r,a=n,c="suspendedStart",function(t,e){if("executing"===c)throw new Error("Generator is already running");if("completed"===c){if("throw"===t)throw e;return x()}for(a.method=t,a.arg=e;;){var r=a.delegate;if(r){var n=function t(e,r){var n=r.method,o=e.iterator[n];if(void 0===o)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=void 0,t(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),l;o=f(o,e.iterator,r.arg);if("throw"===o.type)return r.method="throw",r.arg=o.arg,r.delegate=null,l;var o=o.arg;return o?o.done?(r[e.resultName]=o.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=void 0),r.delegate=null,l):o:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,l)}(r,a);if(n){if(n===l)continue;return n}}if("next"===a.method)a.sent=a._sent=a.arg;else if("throw"===a.method){if("suspendedStart"===c)throw c="completed",a.arg;a.dispatchException(a.arg)}else"return"===a.method&&a.abrupt("return",a.arg);c="executing";n=f(o,i,a);if("normal"===n.type){if(c=a.done?"completed":"suspendedYield",n.arg===l)continue;return{value:n.arg,done:a.done}}"throw"===n.type&&(c="completed",a.method="throw",a.arg=n.arg)}})}),e}function f(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}a.wrap=c;var l={};function h(){}function p(){}function g(){}var y={};i(y,n,function(){return this});e=Object.getPrototypeOf,e=e&&e(e(L([])));e&&e!==t&&s.call(e,n)&&(y=e);var d=g.prototype=h.prototype=Object.create(y);function v(t){["next","throw","return"].forEach(function(e){i(t,e,function(t){return this._invoke(e,t)})})}function m(a,c){var e;u(this,"_invoke",{value:function(r,n){function t(){return new c(function(t,e){!function e(t,r,n,o){t=f(a[t],a,r);if("throw"!==t.type){var i=t.arg,r=i.value;return r&&"object"==_typeof(r)&&s.call(r,"__await")?c.resolve(r.__await).then(function(t){e("next",t,n,o)},function(t){e("throw",t,n,o)}):c.resolve(r).then(function(t){i.value=t,n(i)},function(t){return e("throw",t,n,o)})}o(t.arg)}(r,n,t,e)})}return e=e?e.then(t,t):t()}})}function w(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function b(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(w,this),this.reset(!0)}function L(e){if(e){var t=e[n];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var r=-1,t=function t(){for(;++r<e.length;)if(s.call(e,r))return t.value=e[r],t.done=!1,t;return t.value=void 0,t.done=!0,t};return t.next=t}}return{next:x}}function x(){return{value:void 0,done:!0}}return u(d,"constructor",{value:p.prototype=g,configurable:!0}),u(g,"constructor",{value:p,configurable:!0}),p.displayName=i(g,o,"GeneratorFunction"),a.isGeneratorFunction=function(t){t="function"==typeof t&&t.constructor;return!!t&&(t===p||"GeneratorFunction"===(t.displayName||t.name))},a.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,g):(t.__proto__=g,i(t,o,"GeneratorFunction")),t.prototype=Object.create(d),t},a.awrap=function(t){return{__await:t}},v(m.prototype),i(m.prototype,r,function(){return this}),a.AsyncIterator=m,a.async=function(t,e,r,n,o){void 0===o&&(o=Promise);var i=new m(c(t,e,r,n),o);return a.isGeneratorFunction(e)?i:i.next().then(function(t){return t.done?t.value:i.next()})},v(d),i(d,o,"Generator"),i(d,n,function(){return this}),i(d,"toString",function(){return"[object Generator]"}),a.keys=function(t){var e,r=Object(t),n=[];for(e in r)n.push(e);return n.reverse(),function t(){for(;n.length;){var e=n.pop();if(e in r)return t.value=e,t.done=!1,t}return t.done=!0,t}},a.values=L,b.prototype={constructor:b,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(_),!t)for(var e in this)"t"===e.charAt(0)&&s.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(r){if(this.done)throw r;var n=this;function t(t,e){return i.type="throw",i.arg=r,n.next=t,e&&(n.method="next",n.arg=void 0),!!e}for(var e=this.tryEntries.length-1;0<=e;--e){var o=this.tryEntries[e],i=o.completion;if("root"===o.tryLoc)return t("end");if(o.tryLoc<=this.prev){var a=s.call(o,"catchLoc"),c=s.call(o,"finallyLoc");if(a&&c){if(this.prev<o.catchLoc)return t(o.catchLoc,!0);if(this.prev<o.finallyLoc)return t(o.finallyLoc)}else if(a){if(this.prev<o.catchLoc)return t(o.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return t(o.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;0<=r;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&s.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var o=n;break}}var i=(o=o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc?null:o)?o.completion:{};return i.type=t,i.arg=e,o?(this.method="next",this.next=o.finallyLoc,l):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),l},finish:function(t){for(var e=this.tryEntries.length-1;0<=e;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),_(r),l}},catch:function(t){for(var e=this.tryEntries.length-1;0<=e;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n,o=r.completion;return"throw"===o.type&&(n=o.arg,_(r)),n}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:L(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},a}function asyncGeneratorStep(t,e,r,n,o,i,a){try{var c=t[i](a),s=c.value}catch(t){return void r(t)}c.done?e(s):Promise.resolve(s).then(n,o)}function _asyncToGenerator(c){return function(){var t=this,a=arguments;return new Promise(function(e,r){var n=c.apply(t,a);function o(t){asyncGeneratorStep(n,e,r,o,i,"next",t)}function i(t){asyncGeneratorStep(n,e,r,o,i,"throw",t)}o(void 0)})}}var _crypto=require("crypto"),http=require("http"),constants=require("./constants"),_require=require("js-base64"),encodeURL=_require.encodeURL,encode=_require.encode;function getRequestBody(t){return constants.getDssConfigs().dssKeyId?{version:"1",payload:encode(t),paddingMode:"1",keyId:constants.getDssConfigs().dssKeyId}:{version:"1",payload:encodeURL(t),paddingMode:"1"}}var reqHeader={"Content-Type":"application/json"};function getAuthorization(t){var e=(new Date).getTime(),r="POST&"+new URL(constants.getDssHost()).pathname+"&&"+JSON.stringify(t)+"&appid="+constants.getDssKeyId()+"&timestamp="+e,t=constants.getDssKey(),n=_crypto.createHmac("sha256",t).update(r).digest("base64");return"CLOUDSOA-HMAC-SHA256 appid=".concat(constants.getDssKeyId(),", timestamp=").concat(e,', signature="').concat(n,'"')}function sendRequest(t){var e=getRequestBody(t),t=(new Date).getTime();reqHeader.Authorization=getAuthorization(e),reqHeader["X-Request-ID"]=t;var n=new URL(constants.getDssHost()),o=n.pathname;return new Promise(function(r){var t=http.request({host:n.hostname,port:8108,method:"POST",path:o,headers:reqHeader},function(t){var e="";t.on("data",function(t){e+=t}),t.on("end",function(){r(JSON.parse(e))})});t.on("error",function(t){console.error("sign request error: ".concat(t.message))}),t.write(JSON.stringify(e)),t.end()})}function getSignDigestOnline(t){return _getSignDigestOnline.apply(this,arguments)}function _getSignDigestOnline(){return(_getSignDigestOnline=_asyncToGenerator(_regeneratorRuntime().mark(function t(e){var r,n,o;return _regeneratorRuntime().wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,sendRequest(e);case 2:if(r=t.sent,n={},r&&0===r.status){t.next=7;break}throw o=r&&r.result&&(r.result.errMsg||r.result.errMsp)||"unknown error",new Error("sign request error: ".concat(o));case 7:return n.cert=r.result.cert,n.signature=r.result.signature,t.abrupt("return",n);case 10:case"end":return t.stop()}},t)}))).apply(this,arguments)}function Base64UrlToBase64(t){if(t){var e=t.replace(/-/g,"+").replace(/_/g,"/"),t=e.length%4;return e+"====".substring(0,t)}}function generateCertificate(t){return _generateCertificate.apply(this,arguments)}function _generateCertificate(){return(_generateCertificate=_asyncToGenerator(_regeneratorRuntime().mark(function t(e){var r;return _regeneratorRuntime().wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return constants.setDssConfig(e),t.next=3,getSignDigestOnline("abc");case 3:return r=t.sent,r=Base64UrlToBase64(r.cert).replace(/(.{64})/g,"$1\n"),t.abrupt("return",constants.CERT_HEAD+r+constants.CERT_END);case 6:case"end":return t.stop()}},t)}))).apply(this,arguments)}function setOnlineParam(t,e){constants.setDssKey(t),constants.setDssHost(e)}module.exports={getSignDigestOnline:getSignDigestOnline,Base64UrlToBase64:Base64UrlToBase64,setOnlineParam:setOnlineParam,generateCertificate:generateCertificate};