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

Add support for remote\local "pre" function invocation #302

Merged
merged 2 commits into from
Oct 7, 2019
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
21 changes: 16 additions & 5 deletions lib-es5/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module.exports = {
},
ensurePresenceOf
};
exports = module.exports;
let exports = module.exports;
var utils = module.exports;

try {
Expand Down Expand Up @@ -239,9 +239,19 @@ function process_custom_function(customFunction) {
}
if (customFunction.function_type === "remote") {
return [customFunction.function_type, base64EncodeURL(customFunction.source)].join(":");
} else {
return [customFunction.function_type, customFunction.source].join(":");
}
return [customFunction.function_type, customFunction.source].join(":");
}

/**
* Parse custom_pre_function options
* @private
* @param {object|*} customPreFunction a custom function object containing function_type and source values
* @return {string|*} custom_pre_function transformation string
*/
function process_custom_pre_function(customPreFunction) {
var result = process_custom_function(customPreFunction);
return utils.isString(result) ? `pre:${result}` : null;
}

/**
Expand Down Expand Up @@ -565,6 +575,7 @@ function generate_transformation_string(options) {
var underlay = process_layer(utils.option_consume(options, "underlay"));
var ifValue = process_if(utils.option_consume(options, "if"));
var custom_function = process_custom_function(utils.option_consume(options, "custom_function"));
var custom_pre_function = process_custom_pre_function(utils.option_consume(options, "custom_pre_function"));
var fps = utils.option_consume(options, 'fps');
if (isArray(fps)) {
fps = fps.join('-');
Expand All @@ -579,7 +590,7 @@ function generate_transformation_string(options) {
dpr: normalize_expression(dpr),
e: normalize_expression(effect),
fl: flags,
fn: custom_function,
fn: custom_function || custom_pre_function,
fps: fps,
h: normalize_expression(height),
ki: normalize_expression(utils.option_consume(options, "keyframe_interval")),
Expand Down Expand Up @@ -1546,4 +1557,4 @@ exports.build_streaming_profiles_param = build_streaming_profiles_param;
exports.hashToParameters = hashToParameters;
exports.present = present;
exports.only = only;
exports.jsonArrayParam = jsonArrayParam;
exports.jsonArrayParam = jsonArrayParam;
17 changes: 14 additions & 3 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,19 @@ function process_custom_function(customFunction) {
}
if (customFunction.function_type === "remote") {
return [customFunction.function_type, base64EncodeURL(customFunction.source)].join(":");
} else {
return [customFunction.function_type, customFunction.source].join(":");
}
return [customFunction.function_type, customFunction.source].join(":");
}

/**
* Parse custom_pre_function options
* @private
* @param {object|*} customPreFunction a custom function object containing function_type and source values
* @return {string|*} custom_pre_function transformation string
*/
function process_custom_pre_function(customPreFunction) {
let result = process_custom_function(customPreFunction);
return utils.isString(result) ? `pre:${result}` : null;
}

/**
Expand Down Expand Up @@ -543,6 +553,7 @@ function generate_transformation_string(options) {
let underlay = process_layer(utils.option_consume(options, "underlay"));
let ifValue = process_if(utils.option_consume(options, "if"));
let custom_function = process_custom_function(utils.option_consume(options, "custom_function"));
let custom_pre_function = process_custom_pre_function(utils.option_consume(options, "custom_pre_function"));
let fps = utils.option_consume(options, 'fps');
if (isArray(fps)) {
fps = fps.join('-');
Expand All @@ -557,7 +568,7 @@ function generate_transformation_string(options) {
dpr: normalize_expression(dpr),
e: normalize_expression(effect),
fl: flags,
fn: custom_function,
fn: custom_function || custom_pre_function,
fps: fps,
h: normalize_expression(height),
ki: normalize_expression(utils.option_consume(options, "keyframe_interval")),
Expand Down
46 changes: 36 additions & 10 deletions test/cloudinary_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ describe("cloudinary", function () {
it('should support custom function of type wasm with a source', function () {
var options, result;
options = {
custom_function: {function_type: 'wasm', source: 'blur.wasm'}
custom_function: { function_type: 'wasm', source: 'blur.wasm' },
};
result = cloudinary.utils.url("test", options);
expect(options).to.eql({});
Expand All @@ -433,7 +433,7 @@ describe("cloudinary", function () {
it('should support arbitrary custom function types', function () {
var options, result;
options = {
custom_function: {function_type: 'amazing', source: 'awesome'}
custom_function: { function_type: 'amazing', source: 'awesome' },
};
result = cloudinary.utils.url("test", options);
expect(options).to.eql({});
Expand All @@ -442,7 +442,7 @@ describe("cloudinary", function () {
it('should support custom function with no source', function () {
var options, result;
options = {
custom_function: {function_type: 'wasm'}
custom_function: { function_type: 'wasm' },
};
result = cloudinary.utils.url("test", options);
expect(options).to.eql({});
Expand All @@ -451,7 +451,7 @@ describe("cloudinary", function () {
it('should support custom function with no function_type', function () {
var options, result;
options = {
custom_function: {source: 'blur.wasm'}
custom_function: { source: 'blur.wasm' },
};
result = cloudinary.utils.url("test", options);
expect(options).to.eql({});
Expand All @@ -460,7 +460,7 @@ describe("cloudinary", function () {
it('should support custom function that is not an object', function () {
var options, result;
options = {
custom_function: []
custom_function: [],
};
result = cloudinary.utils.url("test", options);
expect(options).to.eql({});
Expand All @@ -469,30 +469,56 @@ describe("cloudinary", function () {
it('should support custom function with no function_type or source', function () {
var options, result;
options = {
custom_function: {}
custom_function: {},
};
result = cloudinary.utils.url("test", options);
expect(options).to.eql({});
expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/fn_:/test");
});
it('should support custom function of type remote', function() {
it('should support custom function of type remote', function () {
var options, result;
options = {
custom_function: {function_type: 'remote', source: 'https://df34ra4a.execute-api.us-west-2.amazonaws.com/default/cloudinaryFunction'}
custom_function: {
function_type: 'remote',
source:
'https://df34ra4a.execute-api.us-west-2.amazonaws.com/default/cloudinaryFunction',
},
};
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");
});
it('should should not include custom function with undefined value', function() {
it('should should not include custom function with undefined value', function () {
var options, result;
options = {
custom_function: undefined
custom_function: undefined,
};
result = cloudinary.utils.url("test", options);
expect(options).to.eql({});
expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/test");
});
it('should support custom pre function', function () {
var options, result;
options = {
custom_pre_function: {
function_type: 'remote',
source:
'https://df34ra4a.execute-api.us-west-2.amazonaws.com/default/cloudinaryFunction',
},
};
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");
});
it('should support custom pre function with no function_type or source', function () {
var options, result;
options = {
custom_pre_function: {},
};
result = cloudinary.utils.url("test", options);
expect(options).to.eql({});
expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/fn_pre::/test");
});
it("should support density", function () {
var options, result;
options = {
Expand Down