camo: support URL encoding option

This commit is contained in:
Calvin Montgomery 2017-07-08 19:21:14 -07:00
parent 54045766f2
commit 486ce04a3e
6 changed files with 56 additions and 4 deletions

View file

@ -24,8 +24,14 @@ export function camoify(camoConfig: CamoConfig, url: string): string {
const hmac = crypto.createHmac('sha1', camoConfig.getKey());
hmac.update(url);
const digest = hmac.digest('hex');
const hexUrl = Buffer.from(url, 'utf8').toString('hex');
return `${camoConfig.getServer()}/${digest}/${hexUrl}`;
// https://github.com/atmos/camo#url-formats
if (camoConfig.getEncoding() === 'hex') {
const hexUrl = Buffer.from(url, 'utf8').toString('hex');
return `${camoConfig.getServer()}/${digest}/${hexUrl}`;
} else {
const encoded = encodeURIComponent(url);
return `${camoConfig.getServer()}/${digest}?url=${encoded}`;
}
}
export function transformImgTags(camoConfig: CamoConfig, tagName: string, attribs: Object) {

View file

@ -4,6 +4,14 @@ class CamoConfig {
if (this.config.server) {
this.config.server = this.config.server.replace(/\/+$/, '');
}
this.validate();
}
validate() {
if (this.config.encoding
&& !~['url', 'hex'].indexOf(this.config.encoding)) {
throw new Error(`Value for key 'encoding' must be either 'url' or 'hex', not '${this.config.encoding}'`);
}
}
isEnabled() {
@ -21,6 +29,10 @@ class CamoConfig {
getWhitelistedDomains() {
return this.config['whitelisted-domains'] || [];
}
getEncoding() {
return this.config.encoding || 'url';
}
}
export { CamoConfig };