imgproxy
Version:
NodeJS client library to generate imgproxy urls
38 lines (36 loc) • 1.12 kB
JavaScript
// src/utils.ts
import crypto from "crypto";
var isRGBColor = (obj) => {
return typeof obj === "object" && "r" in obj && "g" in obj && "b" in obj;
};
var isFocusPoint = (obj) => {
return typeof obj === "object" && "x" in obj && "y" in obj;
};
var isOffsetGravity = (obj) => {
return typeof obj === "object" && "xOffset" in obj && "yOffset" in obj && "type" in obj;
};
var isSecureConfig = (config) => {
return "key" in config && "salt" in config;
};
var hexDecode = (hex) => Buffer.from(hex, "hex");
var encodeUrlChars = (url) => url.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
var urlSafeEncode = (data) => {
if (Buffer.isBuffer(data)) {
return encodeUrlChars(data.toString("base64"));
}
return encodeUrlChars(Buffer.from(data, "utf-8").toString("base64"));
};
var sign = (key, salt, target, size = 32) => {
const hmac = crypto.createHmac("sha256", hexDecode(key));
hmac.update(hexDecode(salt));
hmac.update(target);
return urlSafeEncode(hmac.digest().slice(0, size));
};
export {
isRGBColor,
isFocusPoint,
isOffsetGravity,
isSecureConfig,
urlSafeEncode,
sign
};