Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate url-safe base64 strings in remote custom functions #446

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib-es5/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ function process_custom_function(customFunction) {
return customFunction;
}
if (customFunction.function_type === "remote") {
return [customFunction.function_type, base64EncodeURL(customFunction.source)].join(":");
var encodedSource = base64EncodeURL(customFunction.source).replace(/\+/g, '-') // Convert '+' to '-'
.replace(/\//g, '_') // Convert '/' to '_'
.replace(/=+$/, ''); // Remove ending '='
return [customFunction.function_type, encodedSource].join(":");
}
return [customFunction.function_type, customFunction.source].join(":");
}
Expand Down
6 changes: 5 additions & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ function process_custom_function(customFunction) {
return customFunction;
}
if (customFunction.function_type === "remote") {
return [customFunction.function_type, base64EncodeURL(customFunction.source)].join(":");
const encodedSource = base64EncodeURL(customFunction.source)
.replace(/\+/g, '-') // Convert '+' to '-'
.replace(/\//g, '_') // Convert '/' to '_'
.replace(/=+$/, ''); // Remove ending '='
return [customFunction.function_type, encodedSource].join(":");
}
return [customFunction.function_type, customFunction.source].join(":");
}
Expand Down
17 changes: 15 additions & 2 deletions test/unit/cloudinary_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ describe("cloudinary", function () {
};
result = cloudinary.utils.url("test", options);
expect(options).to.eql({});
expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/fn_remote:aHR0cHM6Ly9kZjM0cmE0YS5leGVjdXRlLWFwaS51cy13ZXN0LTIuYW1hem9uYXdzLmNvbS9kZWZhdWx0L2Nsb3VkaW5hcnlGdW5jdGlvbg==/test");
expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/fn_remote:aHR0cHM6Ly9kZjM0cmE0YS5leGVjdXRlLWFwaS51cy13ZXN0LTIuYW1hem9uYXdzLmNvbS9kZWZhdWx0L2Nsb3VkaW5hcnlGdW5jdGlvbg/test");
});
it('should should not include custom function with undefined value', function () {
var options, result;
Expand All @@ -567,7 +567,20 @@ describe("cloudinary", function () {
};
result = cloudinary.utils.url("test", options);
expect(options).to.eql({});
expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/fn_pre:remote:aHR0cHM6Ly9kZjM0cmE0YS5leGVjdXRlLWFwaS51cy13ZXN0LTIuYW1hem9uYXdzLmNvbS9kZWZhdWx0L2Nsb3VkaW5hcnlGdW5jdGlvbg==/test");
expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/fn_pre:remote:aHR0cHM6Ly9kZjM0cmE0YS5leGVjdXRlLWFwaS51cy13ZXN0LTIuYW1hem9uYXdzLmNvbS9kZWZhdWx0L2Nsb3VkaW5hcnlGdW5jdGlvbg/test");
});
it('should generate url safe base64 in remote custom pre function', function () {
var options, result;
options = {
custom_pre_function: {
function_type: 'remote',
source:
"https://opengraphimg.com/.netlify/functions/generate-opengraph?author=opengraphimg&title=Hey%20Chris%20this%20is%20working"
}
};
result = cloudinary.utils.url("test", options);
expect(options).to.eql({});
expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/fn_pre:remote:aHR0cHM6Ly9vcGVuZ3JhcGhpbWcuY29tLy5uZXRsaWZ5L2Z1bmN0aW9ucy9nZW5lcmF0ZS1vcGVuZ3JhcGg_YXV0aG9yPW9wZW5ncmFwaGltZyZ0aXRsZT1IZXklMjBDaHJpcyUyMHRoaXMlMjBpcyUyMHdvcmtpbmc/test");
});
it('should support custom pre function with no function_type or source', function () {
var options, result;
Expand Down