diff --git a/lib-es5/utils/index.js b/lib-es5/utils/index.js index 7bce0170..defd847c 100644 --- a/lib-es5/utils/index.js +++ b/lib-es5/utils/index.js @@ -81,7 +81,7 @@ module.exports = { }, ensurePresenceOf }; -exports = module.exports; +let exports = module.exports; var utils = module.exports; try { @@ -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; } /** @@ -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('-'); @@ -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")), @@ -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; \ No newline at end of file +exports.jsonArrayParam = jsonArrayParam; diff --git a/lib/utils/index.js b/lib/utils/index.js index 20f315c6..505bd3d3 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -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; } /** @@ -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('-'); @@ -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")), diff --git a/test/cloudinary_spec.js b/test/cloudinary_spec.js index 2d1a7c42..8d6f0937 100644 --- a/test/cloudinary_spec.js +++ b/test/cloudinary_spec.js @@ -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({}); @@ -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({}); @@ -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({}); @@ -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({}); @@ -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({}); @@ -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 = {