decentralized-internet
Version:
An NPM library of programs to create decentralized web and distributed computing projects
1 lines • 6.33 kB
JavaScript
;const Hoek=require("hoek"),internals={STATUS_CODES:Object.setPrototypeOf({100:"Continue",101:"Switching Protocols",102:"Processing",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",300:"Multiple Choices",301:"Moved Permanently",302:"Moved Temporarily",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Time-out",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Request Entity Too Large",414:"Request-URI Too Large",415:"Unsupported Media Type",416:"Requested Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",425:"Unordered Collection",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Time-out",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",509:"Bandwidth Limit Exceeded",510:"Not Extended",511:"Network Authentication Required"},null)};exports.wrap=function(e,t,r){return Hoek.assert(e instanceof Error,"Cannot wrap non-Error object"),Hoek.assert(!e.isBoom||!t&&!r,"Cannot provide statusCode or message with boom error"),e.isBoom?e:internals.initialize(e,t||500,r)},exports.create=function(e,t,r){return internals.create(e,t,r,exports.create)},internals.create=function(e,t,r,n){if(t instanceof Error)return r&&(t.data=r),exports.wrap(t,e);const o=new Error(t||void 0);return Error.captureStackTrace(o,n),o.data=r||null,internals.initialize(o,e),o},internals.initialize=function(e,t,r){const n=parseInt(t,10);return Hoek.assert(!isNaN(n)&&n>=400,"First argument must be a number (400+):",t),e.isBoom=!0,e.isServer=n>=500,e.hasOwnProperty("data")||(e.data=null),e.output={statusCode:n,payload:{},headers:{}},e.reformat=internals.reformat,e.reformat(),r||e.message||(r=e.output.payload.error),r&&(e.message=r+(e.message?": "+e.message:"")),e},internals.reformat=function(){this.output.payload.statusCode=this.output.statusCode,this.output.payload.error=internals.STATUS_CODES[this.output.statusCode]||"Unknown",500===this.output.statusCode?this.output.payload.message="An internal server error occurred":this.message&&(this.output.payload.message=this.message)},exports.badRequest=function(e,t){return internals.create(400,e,t,exports.badRequest)},exports.unauthorized=function(e,t,r){const n=internals.create(401,e,void 0,exports.unauthorized);if(!t)return n;let o="";if("string"==typeof t){if(o=t,(r||e)&&(n.output.payload.attributes={}),r)if("string"==typeof r)o=o+" "+Hoek.escapeHeaderAttribute(r),n.output.payload.attributes=r;else{const e=Object.keys(r);for(let t=0;t<e.length;++t){const a=e[t];t&&(o+=",");let i=r[a];null==i&&(i=""),o=o+" "+a+'="'+Hoek.escapeHeaderAttribute(i.toString())+'"',n.output.payload.attributes[a]=i}}e?(r&&(o+=","),o=o+' error="'+Hoek.escapeHeaderAttribute(e)+'"',n.output.payload.attributes.error=e):n.isMissing=!0}else{const e=t;for(let t=0;t<e.length;++t)t&&(o+=", "),o+=e[t]}return n.output.headers["WWW-Authenticate"]=o,n},exports.paymentRequired=function(e,t){return internals.create(402,e,t,exports.paymentRequired)},exports.forbidden=function(e,t){return internals.create(403,e,t,exports.forbidden)},exports.notFound=function(e,t){return internals.create(404,e,t,exports.notFound)},exports.methodNotAllowed=function(e,t,r){const n=internals.create(405,e,t,exports.methodNotAllowed);return"string"==typeof r&&(r=[r]),Array.isArray(r)&&(n.output.headers.Allow=r.join(", ")),n},exports.notAcceptable=function(e,t){return internals.create(406,e,t,exports.notAcceptable)},exports.proxyAuthRequired=function(e,t){return internals.create(407,e,t,exports.proxyAuthRequired)},exports.clientTimeout=function(e,t){return internals.create(408,e,t,exports.clientTimeout)},exports.conflict=function(e,t){return internals.create(409,e,t,exports.conflict)},exports.resourceGone=function(e,t){return internals.create(410,e,t,exports.resourceGone)},exports.lengthRequired=function(e,t){return internals.create(411,e,t,exports.lengthRequired)},exports.preconditionFailed=function(e,t){return internals.create(412,e,t,exports.preconditionFailed)},exports.entityTooLarge=function(e,t){return internals.create(413,e,t,exports.entityTooLarge)},exports.uriTooLong=function(e,t){return internals.create(414,e,t,exports.uriTooLong)},exports.unsupportedMediaType=function(e,t){return internals.create(415,e,t,exports.unsupportedMediaType)},exports.rangeNotSatisfiable=function(e,t){return internals.create(416,e,t,exports.rangeNotSatisfiable)},exports.expectationFailed=function(e,t){return internals.create(417,e,t,exports.expectationFailed)},exports.teapot=function(e,t){return internals.create(418,e,t,exports.teapot)},exports.badData=function(e,t){return internals.create(422,e,t,exports.badData)},exports.locked=function(e,t){return internals.create(423,e,t,exports.locked)},exports.preconditionRequired=function(e,t){return internals.create(428,e,t,exports.preconditionRequired)},exports.tooManyRequests=function(e,t){return internals.create(429,e,t,exports.tooManyRequests)},exports.illegal=function(e,t){return internals.create(451,e,t,exports.illegal)},exports.internal=function(e,t,r){return internals.serverError(e,t,r,exports.internal)},internals.serverError=function(e,t,r,n){let o;return t instanceof Error?o=exports.wrap(t,r,e):(o=internals.create(r||500,e,void 0,n)).data=t,o},exports.notImplemented=function(e,t){return internals.serverError(e,t,501,exports.notImplemented)},exports.badGateway=function(e,t){return internals.serverError(e,t,502,exports.badGateway)},exports.serverUnavailable=function(e,t){return internals.serverError(e,t,503,exports.serverUnavailable)},exports.gatewayTimeout=function(e,t){return internals.serverError(e,t,504,exports.gatewayTimeout)},exports.badImplementation=function(e,t){const r=internals.serverError(e,t,500,exports.badImplementation);return r.isDeveloperError=!0,r};