niba-api 0.1.7 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +1 -1
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1 +1 @@
1
- import t from"http";import r from"koa";import e from"koa-bodyparser";import n from"@babel/runtime/helpers/typeof";import u from"@babel/runtime/helpers/asyncToGenerator";import a from"@babel/runtime/helpers/slicedToArray";import o from"@babel/runtime/helpers/defineProperty";import i from"@babel/runtime/regenerator";import{promisify as c}from"node:util";import{createCipheriv as s,scrypt as f,createDecipheriv as l,randomBytes as p}from"node:crypto";import h from"cacache";import v from"path";import d from"os";import b from"@babel/runtime/helpers/createClass";import m from"@babel/runtime/helpers/classCallCheck";import y from"@babel/runtime/helpers/inherits";import g from"@babel/runtime/helpers/possibleConstructorReturn";import x from"@babel/runtime/helpers/getPrototypeOf";import w from"@babel/runtime/helpers/wrapNativeSuper";var k=console;function O(t){k=t}var j=k;function S(t,r){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable}))),e.push.apply(e,n)}return e}function P(t){for(var r=1;r<arguments.length;r++){var e=null!=arguments[r]?arguments[r]:{};r%2?S(Object(e),!0).forEach((function(r){o(t,r,e[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(e)):S(Object(e)).forEach((function(r){Object.defineProperty(t,r,Object.getOwnPropertyDescriptor(e,r))}))}return t}function A(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!e){if(Array.isArray(t)||(e=function(t,r){if(!t)return;if("string"==typeof t)return C(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);"Object"===e&&t.constructor&&(e=t.constructor.name);if("Map"===e||"Set"===e)return Array.from(t);if("Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return C(t,r)}(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var n=0,u=function(){};return{s:u,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:u}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,i=!1;return{s:function(){e=e.call(t)},n:function(){var t=e.next();return o=t.done,t},e:function(t){i=!0,a=t},f:function(){try{o||null==e.return||e.return()}finally{if(i)throw a}}}}function C(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}var B=/\((.*?)\)/g,G=/(\(\?)?:\w+/g,q=/\*\w+/g,E=/[\-{}\[\]+?.,\\\^$|#\s]/g;function R(t){return"string"!=typeof t?t:t.indexOf(",")<0?t.trim():t.split(",").map((function(t){return/^\d+(\.\d+)?$/g.test(t.trim())?parseFloat(t.trim()):t.trim()})).filter((function(t){return t}))}function I(t,r){var e,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"",u=A(t);try{var a,i=function(){var t=e.value,u="".concat(n).concat(t.path),a=function(t){var r=t.replace(E,"\\$&").replace(B,"(?:$1)?").replace(G,(function(t,r){return r?t:"([^/?]+)"})).replace(q,"([^?]*?)");return new RegExp("^"+r+"(?:\\?([\\s\\S]*))?$")}(u);if(!a.test(r))return 0;var i={},c=u.match(G);if(!c)return{v:{route:t,patterns:i}};var s=function(t,r){var e=t.exec(r).slice(1);return e.map((function(t,r){return r===e.length-1?t||null:t?decodeURIComponent(t):null}))}(a,r);return c.forEach((function(t,r){Object.assign(i,o({},t.slice(1),R(s[r])))})),{v:{route:t,patterns:i}}};for(u.s();!(e=u.n()).done;)if(0!==(a=i())&&a)return a.v}catch(t){u.e(t)}finally{u.f()}return null}function T(t,r){var e={},n={};r instanceof Array?r.forEach((function(t){Object.assign(n,o({},t,t))})):Object.assign(n,r);for(var u=0,i=Object.entries(t);u<i.length;u++)for(var c=a(i[u],2),s=c[0],f=R(c[1]),l=0,p=Object.entries(n);l<p.length;l++){var h=a(p[l],2),v=h[0];s===h[1]&&Object.assign(e,o({},v,f));break}return e}function L(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";return function(){var e=u(i.mark((function e(u,a){var c,s,f,l,p,h,v,d,b,m;return i.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(s=u.path,f=I(t,s,r)){e.next=4;break}return e.abrupt("return");case 4:if(l=f.route,p=f.patterns,j.debug("--\x3e route %o, patterns: %o",l,p),"string"!=typeof l.method||u.method.toLowerCase()===l.method.toLowerCase()){e.next=9;break}return u.throw(405),e.abrupt("return");case 9:if(h=l.handler,j.debug("--\x3e handler: %o, type: %o",h,n(h)),h&&"function"==typeof h){e.next=14;break}return u.throw(500,"The handler cannot be found"),e.abrupt("return");case 14:if(v=u.state.USER,j.debug("--\x3e current user: %o",v),!l.auth||v){e.next=19;break}return u.throw(401),e.abrupt("return");case 19:if(d=null!==(c=u.state.ACL)&&void 0!==c?c:[],"string"!=typeof l.auth||d.includes(l.auth)){e.next=23;break}return u.throw(403),e.abrupt("return");case 23:return b=P(P({request:u.request,response:u.response},p),{},{_user:v}),"string"==typeof l.body?Object.assign(b,o({},l.body,u.request.body)):Object.assign(b,{body:u.request.body}),l.header&&Object.assign(b,T(u.request.header,l.header)),l.query&&Object.assign(b,T(u.request.query,l.query)),e.next=29,h(b);case 29:if(m=e.sent,!(u.status>=300&&u.status<400)){e.next=32;break}return e.abrupt("return");case 32:return u.body=null!=m?m:null,e.next=35,a();case 35:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}()}var D="aes-192-cbc",N="bmliYW5pYmFsYW5uaWJhIQ==",U="bmliYW5pYmFjaG91bmliYQ==";function Y(t,r){return $.apply(this,arguments)}function $(){return($=u(i.mark((function t(r,e){var n,u,a,o,l;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,c(f)(e,N,24);case 2:return n=t.sent,u=Buffer.from(U,"base64"),a=s(D,n,u),o=a.update(r),l=a.final(),t.abrupt("return",Buffer.concat([o,l]));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _(t,r){return F.apply(this,arguments)}function F(){return(F=u(i.mark((function t(r,e){var n,u,a,o,s;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,c(f)(e,N,24);case 2:return n=t.sent,u=Buffer.from(U,"base64"),a=l(D,n,u),o=a.update(r),s=a.final(),t.abrupt("return",Buffer.concat([o,s]));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Q(t,r){return z.apply(this,arguments)}function z(){return(z=u(i.mark((function t(r,e){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,Y(Buffer.from(r,"utf8"),e);case 2:return n=t.sent,t.abrupt("return",n.toString("base64url"));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function W(t,r){return J.apply(this,arguments)}function J(){return(J=u(i.mark((function t(r,e){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,_(Buffer.from(r,"base64url"),e);case 2:return n=t.sent,t.abrupt("return",n.toString("utf8"));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var M=v.resolve(d.tmpdir(),"niba_cache");function X(){return(X=u(i.mark((function t(){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,h.ls(M);case 3:return t.abrupt("return",t.sent);case 6:t.prev=6,t.t0=t.catch(0);case 8:case"end":return t.stop()}}),t,null,[[0,6]])})))).apply(this,arguments)}function H(t){return K.apply(this,arguments)}function K(){return(K=u(i.mark((function t(r){var e,n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,h.get(M,r);case 3:return e=t.sent,n=e.data,t.abrupt("return",n.toString("utf-8"));case 8:return t.prev=8,t.t0=t.catch(0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,null,[[0,8]])})))).apply(this,arguments)}function V(){return(V=u(i.mark((function t(r){var e;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,H(r);case 2:return e=t.sent,t.abrupt("return",e&&JSON.parse(e));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Z(t,r){return tt.apply(this,arguments)}function tt(){return(tt=u(i.mark((function t(r,e){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,h.put(M,r,e+"");case 3:t.next=7;break;case 5:t.prev=5,t.t0=t.catch(0);case 7:case"end":return t.stop()}}),t,null,[[0,5]])})))).apply(this,arguments)}function rt(){return(rt=u(i.mark((function t(r,e){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:Z(r,JSON.stringify(e));case 1:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function et(){return(et=u(i.mark((function t(r){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,h.rm.entry(M,r);case 3:t.next=7;break;case 5:t.prev=5,t.t0=t.catch(0);case 7:case"end":return t.stop()}}),t,null,[[0,5]])})))).apply(this,arguments)}function nt(){return(nt=u(i.mark((function t(){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,h.rm.all(M);case 3:t.next=7;break;case 5:t.prev=5,t.t0=t.catch(0);case 7:case"end":return t.stop()}}),t,null,[[0,5]])})))).apply(this,arguments)}var ut=Object.freeze({__proto__:null,clear:function(){return nt.apply(this,arguments)},evict:function(t){return et.apply(this,arguments)},getObject:function(t){return V.apply(this,arguments)},getText:H,ls:function(){return X.apply(this,arguments)},setObject:function(t,r){return rt.apply(this,arguments)},setPath:function(t){M=t},setText:Z}),at="eD300Ezu9r4Q3CPM7XQt-Q";function ot(t){return it.apply(this,arguments)}function it(){return(it=u(i.mark((function t(r){var e,n,u,a,o,s,f,l,h,v,d,b;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=r.uid,n=r.device,u=void 0===n?"unknow":n,a=r.key,o=void 0===a?at:a,s=r.timeout,f=void 0===s?30:s,l=(new Date).getTime(),t.next=4,c(p)(16);case 4:return h=t.sent,v=Buffer.from(l+60*f*1e3+"","ascii"),d=Buffer.concat([v,Buffer.from("".concat(e,"<=>").concat(u,"<=>").concat(l)),h]),t.next=9,Y(d,o);case 9:return b=t.sent,t.next=12,Z("".concat(e,"@").concat(u),l);case 12:return t.abrupt("return",b.toString("base64url"));case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ct(t){return st.apply(this,arguments)}function st(){return(st=u(i.mark((function t(r){var e,n,u,o,c,s,f,l,p,h,v,d;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=r.token,n=r.key,u=void 0===n?at:n,o=Buffer.from(e,"base64url"),t.next=4,_(o,u);case 4:if(c=t.sent,s=c.subarray(0,c.length-16).toString("utf8"),!(parseInt(s.slice(0,13))<(new Date).getTime())){t.next=9;break}return t.abrupt("return",null);case 9:return f=s.slice(13).split("<=>"),l=a(f,3),p=l[0],h=l[1],v=l[2],t.next=12,H("".concat(p,"@").concat(h));case 12:if(d=t.sent,!(parseInt(v)<parseInt(d))){t.next=15;break}return t.abrupt("return",null);case 15:return t.abrupt("return",{uid:p,device:h});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ft(t){var r=t.userGetter,e=t.ACLGetter,n=t.tokenParser,a=void 0===n?ct:n;return function(){var t=u(i.mark((function t(n,u){var o,c,s,f,l,p,h,v,d;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if((o=null)||(s=null===(c=n.headers)||void 0===c?void 0:c.authorization,o=null==s?void 0:s.substring(7).trim()),o||(o=null===(f=n.query)||void 0===f?void 0:f.token),j.debug("--\x3e token: %s",o),o){t.next=8;break}return t.next=7,u();case 7:return t.abrupt("return");case 8:return t.prev=8,t.next=11,a({token:o});case 11:if(l=t.sent,p=l.uid,h=l.device,j.debug("uid: %s, device: %s",p,h),!r||"function"!=typeof r){t.next=20;break}return t.next=18,r({uid:p,device:h});case 18:v=t.sent,n.state.USER=v;case 20:if(!e||"function"!=typeof e){t.next=25;break}return t.next=23,e({uid:p,device:h});case 23:d=t.sent,n.state.ACL=d;case 25:t.next=30;break;case 27:t.prev=27,t.t0=t.catch(8),n.throw(401);case 30:return t.next=32,u();case 32:case"end":return t.stop()}}),t,null,[[8,27]])})));return function(r,e){return t.apply(this,arguments)}}()}function lt(){return function(){var t=u(i.mark((function t(r,e){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,e();case 2:(n=r.accepts("*/*","text/*","application/json"))&&(r.type="".concat(n,"; charset=utf-8"));case 4:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}()}function pt(t){var r=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}();return function(){var e,n=x(t);if(r){var u=x(this).constructor;e=Reflect.construct(n,arguments,u)}else e=n.apply(this,arguments);return g(this,e)}}var ht=function(t){y(e,t);var r=pt(e);function e(t){var n,u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:500;return m(this,e),(n=r.call(this,t)).code=u,n}return b(e)}(w(Error)),vt=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Bad Request";return m(this,e),r.call(this,t,400)}return b(e)}(),dt=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Unauthorized";return m(this,e),r.call(this,t,401)}return b(e)}(),bt=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Forbidden";return m(this,e),r.call(this,t,403)}return b(e)}(),mt=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Not Found";return m(this,e),r.call(this,t,404)}return b(e)}(),yt=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Method Not Allowed";return m(this,e),r.call(this,t,405)}return b(e)}(),gt=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Not Acceptable";return m(this,e),r.call(this,t,406)}return b(e)}(),xt=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Proxy Authentication Required";return m(this,e),r.call(this,t,407)}return b(e)}(),wt=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Time-out";return m(this,e),r.call(this,t,408)}return b(e)}(),kt=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Conflict";return m(this,e),r.call(this,t,409)}return b(e)}(),Ot=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Internal Server Error";return m(this,e),r.call(this,t,500)}return b(e)}(),jt=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Not Implemented";return m(this,e),r.call(this,t,501)}return b(e)}(),St=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Bad Gateway";return m(this,e),r.call(this,t,502)}return b(e)}(),Pt=function(t){y(e,ht);var r=pt(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Service Unavailable";return m(this,e),r.call(this,t,503)}return b(e)}();function At(t,r){j.debug("--\x3e error: %O",t),t instanceof ht&&r.throw(t.code,t.message)}function Ct(n){var u=n.context,a=void 0===u?"":u,o=n.routes,i=void 0===o?[]:o,c=n.plugins,s=void 0===c?[]:c,f=n.userGetter,l=void 0===f?null:f,p=n.ACLGetter,h=void 0===p?null:p,v=n.tokenParser,d=void 0===v?null:v,b=n.logger,m=void 0===b?null:b;m&&O(m);var y=new r;return y.on("error",At),y.use(e({enableTypes:["json","form","text"]})),y.use(ft({userGetter:l,ACLGetter:h,tokenParser:d})),y.use(L(i,a)),y.use(lt()),s.forEach((function(t){y.use(t)})),t.createServer(y.callback())}function Bt(t){var r=t.context,e=void 0===r?"":r,n=t.routes,u=void 0===n?[]:n,a=t.plugins,o=void 0===a?[]:a,i=t.hostname,c=void 0===i?"localhost":i,s=t.port,f=void 0===s?9090:s,l=t.userGetter,p=void 0===l?null:l,h=t.ACLGetter,v=void 0===h?null:h,d=t.tokenParser,b=void 0===d?null:d,m=t.logger,y=Ct({context:e,routes:u,plugins:o,logger:void 0===m?null:m,userGetter:p,ACLGetter:v,tokenParser:b});j.info("--\x3e niba is listening at http://".concat(c,":").concat(f)),y.listen(f,c)}export{St as BadGateway,vt as BadRequest,kt as Conflict,bt as Forbidden,Ot as InternalServerError,yt as MethodNotAllowed,ht as NBError,gt as NotAcceptable,mt as NotFound,jt as NotImplemented,xt as ProxyAuthenticationRequired,Pt as ServiceUnavailable,wt as Timeout,dt as Unauthorized,ft as authentication,ut as cache,Ct as createServer,ot as createToken,_ as decrypt,W as decryptText,L as dispatcher,Y as encrypt,Q as encryptText,At as errorHandle,j as logger,ct as parseToken,lt as rjson,Bt as serve,O as setLogger};
1
+ import t from"http";import r from"koa";import e from"koa-bodyparser";import n from"@babel/runtime/helpers/typeof";import u from"@babel/runtime/helpers/asyncToGenerator";import a from"@babel/runtime/helpers/slicedToArray";import o from"@babel/runtime/helpers/defineProperty";import i from"@babel/runtime/regenerator";import c from"@babel/runtime/helpers/createClass";import s from"@babel/runtime/helpers/classCallCheck";import f from"@babel/runtime/helpers/inherits";import l from"@babel/runtime/helpers/possibleConstructorReturn";import p from"@babel/runtime/helpers/getPrototypeOf";import v from"@babel/runtime/helpers/wrapNativeSuper";import{promisify as h}from"node:util";import{createCipheriv as d,scrypt as b,createDecipheriv as m,randomBytes as y}from"node:crypto";import g from"cacache";import x from"path";import w from"os";var k=console;function O(t){k=t}var j=k;function S(t){var r=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}();return function(){var e,n=p(t);if(r){var u=p(this).constructor;e=Reflect.construct(n,arguments,u)}else e=n.apply(this,arguments);return l(this,e)}}var P=function(t){f(e,t);var r=S(e);function e(t){var n,u=arguments.length>1&&void 0!==arguments[1]?arguments[1]:500;return s(this,e),(n=r.call(this,t)).code=u,n}return c(e)}(v(Error)),A=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Bad Request";return s(this,e),r.call(this,t,400)}return c(e)}(),C=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Unauthorized";return s(this,e),r.call(this,t,401)}return c(e)}(),B=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Forbidden";return s(this,e),r.call(this,t,403)}return c(e)}(),G=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Not Found";return s(this,e),r.call(this,t,404)}return c(e)}(),q=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Method Not Allowed";return s(this,e),r.call(this,t,405)}return c(e)}(),E=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Not Acceptable";return s(this,e),r.call(this,t,406)}return c(e)}(),R=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Proxy Authentication Required";return s(this,e),r.call(this,t,407)}return c(e)}(),I=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Time-out";return s(this,e),r.call(this,t,408)}return c(e)}(),T=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Conflict";return s(this,e),r.call(this,t,409)}return c(e)}(),L=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Internal Server Error";return s(this,e),r.call(this,t,500)}return c(e)}(),D=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Not Implemented";return s(this,e),r.call(this,t,501)}return c(e)}(),N=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Bad Gateway";return s(this,e),r.call(this,t,502)}return c(e)}(),U=function(t){f(e,P);var r=S(e);function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Service Unavailable";return s(this,e),r.call(this,t,503)}return c(e)}();function Y(t,r){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable}))),e.push.apply(e,n)}return e}function $(t){for(var r=1;r<arguments.length;r++){var e=null!=arguments[r]?arguments[r]:{};r%2?Y(Object(e),!0).forEach((function(r){o(t,r,e[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(e)):Y(Object(e)).forEach((function(r){Object.defineProperty(t,r,Object.getOwnPropertyDescriptor(e,r))}))}return t}function _(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!e){if(Array.isArray(t)||(e=function(t,r){if(!t)return;if("string"==typeof t)return F(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);"Object"===e&&t.constructor&&(e=t.constructor.name);if("Map"===e||"Set"===e)return Array.from(t);if("Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e))return F(t,r)}(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var n=0,u=function(){};return{s:u,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:u}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,i=!1;return{s:function(){e=e.call(t)},n:function(){var t=e.next();return o=t.done,t},e:function(t){i=!0,a=t},f:function(){try{o||null==e.return||e.return()}finally{if(i)throw a}}}}function F(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}var Q=/\((.*?)\)/g,z=/(\(\?)?:\w+/g,W=/\*\w+/g,J=/[\-{}\[\]+?.,\\\^$|#\s]/g;function M(t){return"string"!=typeof t?t:t.indexOf(",")<0?t.trim():t.split(",").map((function(t){return/^\d+(\.\d+)?$/g.test(t.trim())?parseFloat(t.trim()):t.trim()})).filter((function(t){return t}))}function X(t,r){var e,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"",u=_(t);try{var a,i=function(){var t=e.value,u="".concat(n).concat(t.path),a=function(t){var r=t.replace(J,"\\$&").replace(Q,"(?:$1)?").replace(z,(function(t,r){return r?t:"([^/?]+)"})).replace(W,"([^?]*?)");return new RegExp("^"+r+"(?:\\?([\\s\\S]*))?$")}(u);if(!a.test(r))return 0;var i={},c=u.match(z);if(!c)return{v:{route:t,patterns:i}};var s=function(t,r){var e=t.exec(r).slice(1);return e.map((function(t,r){return r===e.length-1?t||null:t?decodeURIComponent(t):null}))}(a,r);return c.forEach((function(t,r){Object.assign(i,o({},t.slice(1),M(s[r])))})),{v:{route:t,patterns:i}}};for(u.s();!(e=u.n()).done;)if(0!==(a=i())&&a)return a.v}catch(t){u.e(t)}finally{u.f()}return null}function H(t,r){var e={},n={};r instanceof Array?r.forEach((function(t){Object.assign(n,o({},t,t))})):Object.assign(n,r);for(var u=0,i=Object.entries(t);u<i.length;u++)for(var c=a(i[u],2),s=c[0],f=M(c[1]),l=0,p=Object.entries(n);l<p.length;l++){var v=a(p[l],2),h=v[0];s===v[1]&&Object.assign(e,o({},h,f));break}return e}function K(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";return function(){var e=u(i.mark((function e(u,a){var c,s,f,l,p,v,h,d,b,m,y;return i.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(f=u.path,l=X(t,f,r)){e.next=4;break}return e.abrupt("return");case 4:if(p=l.route,v=l.patterns,j.debug("--\x3e route %o, patterns: %o",p,v),"string"!=typeof p.method||u.method.toLowerCase()===p.method.toLowerCase()){e.next=9;break}return u.throw(405),e.abrupt("return");case 9:if(h=p.handler,j.debug("--\x3e handler: %o, type: %o",h,n(h)),h&&"function"==typeof h){e.next=14;break}return u.throw(500,"The handler cannot be found"),e.abrupt("return");case 14:if(d=u.state.USER,j.debug("--\x3e current user: %o",d),!p.auth||d){e.next=19;break}return u.throw(401),e.abrupt("return");case 19:if(b=null!==(c=u.state.ACL)&&void 0!==c?c:[],"string"!=typeof p.auth||b.includes(p.auth)){e.next=23;break}return u.throw(403),e.abrupt("return");case 23:return m=$($({request:u.request,response:u.response},v),{},{_user:d}),"string"==typeof p.body?Object.assign(m,o({},p.body,u.request.body)):Object.assign(m,{body:u.request.body}),p.header&&Object.assign(m,H(u.request.header,p.header)),p.query&&Object.assign(m,H(u.request.query,p.query)),y={},e.prev=28,e.next=31,h(m);case 31:y=e.sent,e.next=38;break;case 34:e.prev=34,e.t0=e.catch(28),j.info("--\x3e error: %O",e.t0),e.t0 instanceof P?u.throw(e.t0.code,e.t0.message):u.throw(500,e.t0.message);case 38:if(!(u.status>=300&&u.status<400)){e.next=40;break}return e.abrupt("return");case 40:return u.body=null!==(s=y)&&void 0!==s?s:null,e.next=43,a();case 43:case"end":return e.stop()}}),e,null,[[28,34]])})));return function(t,r){return e.apply(this,arguments)}}()}var V="aes-192-cbc",Z="bmliYW5pYmFsYW5uaWJhIQ==",tt="bmliYW5pYmFjaG91bmliYQ==";function rt(t,r){return et.apply(this,arguments)}function et(){return(et=u(i.mark((function t(r,e){var n,u,a,o,c;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,h(b)(e,Z,24);case 2:return n=t.sent,u=Buffer.from(tt,"base64"),a=d(V,n,u),o=a.update(r),c=a.final(),t.abrupt("return",Buffer.concat([o,c]));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nt(t,r){return ut.apply(this,arguments)}function ut(){return(ut=u(i.mark((function t(r,e){var n,u,a,o,c;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,h(b)(e,Z,24);case 2:return n=t.sent,u=Buffer.from(tt,"base64"),a=m(V,n,u),o=a.update(r),c=a.final(),t.abrupt("return",Buffer.concat([o,c]));case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function at(t,r){return ot.apply(this,arguments)}function ot(){return(ot=u(i.mark((function t(r,e){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,rt(Buffer.from(r,"utf8"),e);case 2:return n=t.sent,t.abrupt("return",n.toString("base64url"));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function it(t,r){return ct.apply(this,arguments)}function ct(){return(ct=u(i.mark((function t(r,e){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,nt(Buffer.from(r,"base64url"),e);case 2:return n=t.sent,t.abrupt("return",n.toString("utf8"));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var st=x.resolve(w.tmpdir(),"niba_cache");function ft(){return(ft=u(i.mark((function t(){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,g.ls(st);case 3:return t.abrupt("return",t.sent);case 6:t.prev=6,t.t0=t.catch(0);case 8:case"end":return t.stop()}}),t,null,[[0,6]])})))).apply(this,arguments)}function lt(t){return pt.apply(this,arguments)}function pt(){return(pt=u(i.mark((function t(r){var e,n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,g.get(st,r);case 3:return e=t.sent,n=e.data,t.abrupt("return",n.toString("utf-8"));case 8:return t.prev=8,t.t0=t.catch(0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,null,[[0,8]])})))).apply(this,arguments)}function vt(){return(vt=u(i.mark((function t(r){var e;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,lt(r);case 2:return e=t.sent,t.abrupt("return",e&&JSON.parse(e));case 4:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ht(t,r){return dt.apply(this,arguments)}function dt(){return(dt=u(i.mark((function t(r,e){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,g.put(st,r,e+"");case 3:t.next=7;break;case 5:t.prev=5,t.t0=t.catch(0);case 7:case"end":return t.stop()}}),t,null,[[0,5]])})))).apply(this,arguments)}function bt(){return(bt=u(i.mark((function t(r,e){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:ht(r,JSON.stringify(e));case 1:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=u(i.mark((function t(r){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,g.rm.entry(st,r);case 3:t.next=7;break;case 5:t.prev=5,t.t0=t.catch(0);case 7:case"end":return t.stop()}}),t,null,[[0,5]])})))).apply(this,arguments)}function yt(){return(yt=u(i.mark((function t(){return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,g.rm.all(st);case 3:t.next=7;break;case 5:t.prev=5,t.t0=t.catch(0);case 7:case"end":return t.stop()}}),t,null,[[0,5]])})))).apply(this,arguments)}var gt=Object.freeze({__proto__:null,clear:function(){return yt.apply(this,arguments)},evict:function(t){return mt.apply(this,arguments)},getObject:function(t){return vt.apply(this,arguments)},getText:lt,ls:function(){return ft.apply(this,arguments)},setObject:function(t,r){return bt.apply(this,arguments)},setPath:function(t){st=t},setText:ht}),xt="eD300Ezu9r4Q3CPM7XQt-Q";function wt(t){return kt.apply(this,arguments)}function kt(){return(kt=u(i.mark((function t(r){var e,n,u,a,o,c,s,f,l,p,v,d;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=r.uid,n=r.device,u=void 0===n?"unknow":n,a=r.key,o=void 0===a?xt:a,c=r.timeout,s=void 0===c?30:c,f=(new Date).getTime(),t.next=4,h(y)(16);case 4:return l=t.sent,p=Buffer.from(f+60*s*1e3+"","ascii"),v=Buffer.concat([p,Buffer.from("".concat(e,"<=>").concat(u,"<=>").concat(f)),l]),t.next=9,rt(v,o);case 9:return d=t.sent,t.next=12,ht("".concat(e,"@").concat(u),f);case 12:return t.abrupt("return",d.toString("base64url"));case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ot(t){return jt.apply(this,arguments)}function jt(){return(jt=u(i.mark((function t(r){var e,n,u,o,c,s,f,l,p,v,h,d;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=r.token,n=r.key,u=void 0===n?xt:n,o=Buffer.from(e,"base64url"),t.next=4,nt(o,u);case 4:if(c=t.sent,s=c.subarray(0,c.length-16).toString("utf8"),!(parseInt(s.slice(0,13))<(new Date).getTime())){t.next=9;break}return t.abrupt("return",null);case 9:return f=s.slice(13).split("<=>"),l=a(f,3),p=l[0],v=l[1],h=l[2],t.next=12,lt("".concat(p,"@").concat(v));case 12:if(d=t.sent,!(parseInt(h)<parseInt(d))){t.next=15;break}return t.abrupt("return",null);case 15:return t.abrupt("return",{uid:p,device:v});case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function St(t){var r=t.userGetter,e=t.ACLGetter,n=t.tokenParser,a=void 0===n?Ot:n;return function(){var t=u(i.mark((function t(n,u){var o,c,s,f,l,p,v,h,d;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if((o=null)||(s=null===(c=n.headers)||void 0===c?void 0:c.authorization,o=null==s?void 0:s.substring(7).trim()),o||(o=null===(f=n.query)||void 0===f?void 0:f.token),j.debug("--\x3e token: %s",o),o){t.next=8;break}return t.next=7,u();case 7:return t.abrupt("return");case 8:return t.prev=8,t.next=11,a({token:o});case 11:if(l=t.sent,p=l.uid,v=l.device,j.debug("uid: %s, device: %s",p,v),!r||"function"!=typeof r){t.next=20;break}return t.next=18,r({uid:p,device:v});case 18:h=t.sent,n.state.USER=h;case 20:if(!e||"function"!=typeof e){t.next=25;break}return t.next=23,e({uid:p,device:v});case 23:d=t.sent,n.state.ACL=d;case 25:t.next=30;break;case 27:t.prev=27,t.t0=t.catch(8),n.throw(401);case 30:return t.next=32,u();case 32:case"end":return t.stop()}}),t,null,[[8,27]])})));return function(r,e){return t.apply(this,arguments)}}()}function Pt(){return function(){var t=u(i.mark((function t(r,e){var n;return i.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,e();case 2:(n=r.accepts("*/*","text/*","application/json"))&&(r.type="".concat(n,"; charset=utf-8"));case 4:case"end":return t.stop()}}),t)})));return function(r,e){return t.apply(this,arguments)}}()}function At(n){var u=n.context,a=void 0===u?"":u,o=n.routes,i=void 0===o?[]:o,c=n.plugins,s=void 0===c?[]:c,f=n.userGetter,l=void 0===f?null:f,p=n.ACLGetter,v=void 0===p?null:p,h=n.tokenParser,d=void 0===h?null:h,b=n.logger,m=void 0===b?null:b;m&&O(m);var y=new r;return y.use(e({enableTypes:["json","form","text"]})),y.use(St({userGetter:l,ACLGetter:v,tokenParser:d})),y.use(K(i,a)),y.use(Pt()),s.forEach((function(t){y.use(t)})),t.createServer(y.callback())}function Ct(t){var r=t.context,e=void 0===r?"":r,n=t.routes,u=void 0===n?[]:n,a=t.plugins,o=void 0===a?[]:a,i=t.hostname,c=void 0===i?"localhost":i,s=t.port,f=void 0===s?9090:s,l=t.userGetter,p=void 0===l?null:l,v=t.ACLGetter,h=void 0===v?null:v,d=t.tokenParser,b=void 0===d?null:d,m=t.logger,y=At({context:e,routes:u,plugins:o,logger:void 0===m?null:m,userGetter:p,ACLGetter:h,tokenParser:b});j.info("--\x3e niba is listening at http://".concat(c,":").concat(f)),y.listen(f,c)}export{N as BadGateway,A as BadRequest,T as Conflict,B as Forbidden,L as InternalServerError,q as MethodNotAllowed,P as NBError,E as NotAcceptable,G as NotFound,D as NotImplemented,R as ProxyAuthenticationRequired,U as ServiceUnavailable,I as Timeout,C as Unauthorized,St as authentication,gt as cache,At as createServer,wt as createToken,nt as decrypt,it as decryptText,K as dispatcher,rt as encrypt,at as encryptText,j as logger,Ot as parseToken,Pt as rjson,Ct as serve,O as setLogger};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "niba-api",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "An easy-to-use RESTful API development framework",
5
5
  "main": "index.js",
6
6
  "type": "module",