UNPKG

@jsmonday/imgproxy

Version:

ImgProxy Library for Node.js that works both on browser and server

3 lines (2 loc) 1.61 kB
var t=require("create-hmac"),e=Buffer;"undefined"!=typeof window&&(e=require("buffer/").Buffer),module.exports=function(){function i(t){this.options={config:{key:t.key,salt:t.salt,url:t.url},settings:{enlarge:1,width:1e3,height:1e3,gravity:"ce",resizeType:"fill",extension:void 0,originalImage:void 0}}}return i.prototype.setOption=function(t,e){this.options.settings[t]=e},i.prototype.image=function(t){return this.setOption("originalImage",t),this},i.prototype.width=function(t){return this.setOption("width",t),this},i.prototype.height=function(t){return this.setOption("height",t),this},i.prototype.gravity=function(t){return this.setOption("gravity",t),this},i.prototype.enlarge=function(t){return this.setOption("enlarge",t),this},i.prototype.resizeType=function(t){return this.setOption("resizeType",t),this},i.prototype.extension=function(t){return this.setOption("extension",t),this},i.prototype.sign=function(e){var i=this.hexDecode(this.options.config.key),n=this.hexDecode(this.options.config.salt),o=t("sha256",i);return o.update(n),o.update(e),this.urlSafeBase64(o.digest())},i.prototype.hexDecode=function(t){return e.from(t,"hex")},i.prototype.urlSafeBase64=function(t){return e.from(t).toString("base64").replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")},i.prototype.get=function(){var t=this.options.settings,e=this.options.config;if(!t.originalImage)throw"Missing required param: image";var i=this.urlSafeBase64(t.originalImage),n="/"+t.resizeType+"/"+t.width+"/"+t.height+"/"+t.gravity+"/"+t.enlarge+"/"+i;return e.url+"/"+this.sign(n)+n},i}(); //# sourceMappingURL=index.module.js.map