Skip to content
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
3 changes: 1 addition & 2 deletions lib/api_client/execute_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const config = require("../config");
const https = /^http:/.test(config().upload_prefix) ? require('http') : require('https');
const querystring = require("querystring");
const Q = require('q');
const url = require('url');
const utils = require("../utils");
const ensureOption = require('../utils/ensureOption').defaults(config());
Expand All @@ -13,7 +12,7 @@ const agent = config.api_proxy ? new https.Agent(config.api_proxy) : null;

function execute_request(method, params, auth, api_url, callback, options = {}) {
method = method.toUpperCase();
const deferred = Q.defer();
const deferred = utils.deferredPromise();

let query_params, handle_response; // declare to user later
let key = auth.key;
Expand Down
3 changes: 1 addition & 2 deletions lib/uploader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const fs = require('fs');
const { extname, basename } = require('path');
const Q = require('q');
const Writable = require("stream").Writable;
const urlLib = require('url');

Expand Down Expand Up @@ -466,7 +465,7 @@ function call_api(action, callback, options, get_params) {

const USE_PROMISES = !options.disable_promises;

let deferred = Q.defer();
const deferred = utils.deferredPromise();
if (options == null) {
options = {};
}
Expand Down
18 changes: 18 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const {
SUPPORTED_SIGNATURE_ALGORITHMS,
DEFAULT_SIGNATURE_ALGORITHM
} = require('./consts');
const applyQCompat = require("./qPolyfill");

function textStyle(layer) {
let keywords = [];
Expand Down Expand Up @@ -1652,6 +1653,23 @@ function jsonArrayParam(data, modifier) {
*/
exports.NOP = function () {
};

function deferredPromise() {
let resolve, reject
const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
applyQCompat(promise);
return {
resolve,
reject,
promise
};
}

exports.deferredPromise = deferredPromise;

exports.generate_auth_token = generate_auth_token;
exports.getUserAgent = getUserAgent;
exports.build_upload_params = build_upload_params;
Expand Down
131 changes: 131 additions & 0 deletions lib/utils/qPolyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
const scheduleCompatCallback = typeof setImmediate === 'function'
? (fn) => setImmediate(fn)
: (fn) => setTimeout(fn, 0);

function qFinally(onFinally) {
const handler = typeof onFinally === 'function' ? onFinally : () => onFinally;
const PromiseCtor = typeof this.constructor === 'function' && typeof this.constructor.resolve === 'function'
? this.constructor
: Promise;
return this.then(
(value) => PromiseCtor.resolve(handler()).then(() => value),
(reason) => PromiseCtor.resolve(handler()).then(() => {
throw reason;
})
);
}

function qFin(handler) {
return this.finally(handler);
}

function qDone(onFulfilled, onRejected) {
this.then(onFulfilled, onRejected).catch((err) => {
scheduleCompatCallback(() => {
throw err;
});
});
}

function qNodeify(callback) {
if (typeof callback !== 'function') {
return this;
}

this.then(
(value) => scheduleCompatCallback(() => callback(null, value)),
(error) => scheduleCompatCallback(() => callback(error))
);

return this;
}

function qFail(onRejected) {
return this.catch(onRejected);
}

function qProgress() {
return this;
}

function qSpread(onFulfilled, onRejected) {
return this.then(
(values) => {
if (typeof onFulfilled !== 'function') {
return values;
}
if (Array.isArray(values)) {
// eslint-disable-next-line prefer-spread
return onFulfilled.apply(void 0, values);
}
return onFulfilled(values);
},
onRejected
);
}

function applyQCompat(promise) {
if (!promise || (typeof promise !== 'object' && typeof promise !== 'function')) {
return promise;
}

if (promise.__cloudinaryQCompatApplied) {
return promise;
}

Object.defineProperty(promise, '__cloudinaryQCompatApplied', {
value: true,
enumerable: false
});

const nativeThen = promise.then;
if (typeof nativeThen === 'function') {
promise.then = function (...args) {
return applyQCompat(nativeThen.apply(this, args));
};
}

const nativeCatch = promise.catch;
if (typeof nativeCatch === 'function') {
promise.catch = function (...args) {
return applyQCompat(nativeCatch.apply(this, args));
};
}

const nativeFinally = promise.finally;
if (typeof nativeFinally === 'function') {
promise.finally = function (...args) {
return applyQCompat(nativeFinally.apply(this, args));
};
} else {
promise.finally = qFinally;
}

if (typeof promise.fin !== 'function') {
promise.fin = qFin;
}

if (typeof promise.done !== 'function') {
promise.done = qDone;
}

if (typeof promise.nodeify !== 'function') {
promise.nodeify = qNodeify;
}

if (typeof promise.fail !== 'function') {
promise.fail = qFail;
}

if (typeof promise.progress !== 'function') {
promise.progress = qProgress;
}

if (typeof promise.spread !== 'function') {
promise.spread = qSpread;
}

return promise;
}

module.exports = applyQCompat;
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
},
"main": "cloudinary.js",
"dependencies": {
"lodash": "^4.17.21",
"q": "^1.5.1"
"lodash": "^4.17.21"
},
"devDependencies": {
"@types/expect.js": "^0.3.29",
Expand Down
Loading