Skip to content

Commit ee9d38a

Browse files
Switched to call-me-maybe (https://www.npmjs.com/package/call-me-maybe) for promises and callbacks
1 parent 6399fa2 commit ee9d38a

File tree

9 files changed

+216
-283
lines changed

9 files changed

+216
-283
lines changed

dist/ref-parser.js

Lines changed: 76 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ var Promise = require('./promise'),
254254
dereference = require('./dereference'),
255255
util = require('./util'),
256256
url = require('url'),
257+
maybe = require('call-me-maybe'),
257258
ono = require('ono');
258259

259260
module.exports = $RefParser;
@@ -326,14 +327,12 @@ $RefParser.prototype.parse = function(schema, options, callback) {
326327
var $ref = new $Ref(this.$refs, this._basePath);
327328
$ref.setValue(this.schema, args.options);
328329

329-
util.doCallback(args.callback, null, this.schema);
330-
return Promise.resolve(this.schema);
330+
return maybe(args.callback, Promise.resolve(this.schema));
331331
}
332332

333333
if (!args.schema || typeof(args.schema) !== 'string') {
334334
var err = ono('Expected a file path, URL, or object. Got %s', args.schema);
335-
util.doCallback(args.callback, err, args.schema);
336-
return Promise.reject(err);
335+
return maybe(args.callback, Promise.reject(err));
337336
}
338337

339338
var me = this;
@@ -352,13 +351,11 @@ $RefParser.prototype.parse = function(schema, options, callback) {
352351
}
353352
else {
354353
me.schema = $ref.value;
355-
util.doCallback(args.callback, null, me.schema);
356-
return me.schema;
354+
return maybe(args.callback, Promise.resolve(me.schema));
357355
}
358356
})
359357
.catch(function(err) {
360-
util.doCallback(args.callback, err, me.schema);
361-
return Promise.reject(err);
358+
return maybe(args.callback, Promise.reject(err));
362359
});
363360
};
364361

@@ -400,12 +397,10 @@ $RefParser.prototype.resolve = function(schema, options, callback) {
400397
return resolve(me, args.options);
401398
})
402399
.then(function() {
403-
util.doCallback(args.callback, null, me.$refs);
404-
return me.$refs;
400+
return maybe(args.callback, Promise.resolve(me.$refs));
405401
})
406402
.catch(function(err) {
407-
util.doCallback(args.callback, err, me.$refs);
408-
return Promise.reject(err);
403+
return maybe(args.callback, Promise.reject(err));
409404
});
410405
};
411406

@@ -441,12 +436,10 @@ $RefParser.prototype.bundle = function(schema, options, callback) {
441436
return this.resolve(args.schema, args.options)
442437
.then(function() {
443438
bundle(me, args.options);
444-
util.doCallback(args.callback, null, me.schema);
445-
return me.schema;
439+
return maybe(args.callback, Promise.resolve(me.schema));
446440
})
447441
.catch(function(err) {
448-
util.doCallback(args.callback, err, me.schema);
449-
return Promise.reject(err);
442+
return maybe(args.callback, Promise.reject(err));
450443
});
451444
};
452445

@@ -480,12 +473,10 @@ $RefParser.prototype.dereference = function(schema, options, callback) {
480473
return this.resolve(args.schema, args.options)
481474
.then(function() {
482475
dereference(me, args.options);
483-
util.doCallback(args.callback, null, me.schema);
484-
return me.schema;
476+
return maybe(args.callback, Promise.resolve(me.schema));
485477
})
486478
.catch(function(err) {
487-
util.doCallback(args.callback, err, me.schema);
488-
return Promise.reject(err);
479+
return maybe(args.callback, Promise.reject(err));
489480
});
490481
};
491482

@@ -513,7 +504,7 @@ function normalizeArgs(args) {
513504

514505
}).call(this,require("buffer").Buffer)
515506

516-
},{"./bundle":1,"./dereference":2,"./options":4,"./promise":7,"./read":8,"./ref":9,"./refs":10,"./resolve":11,"./util":12,"./yaml":14,"buffer":18,"ono":65,"url":90}],4:[function(require,module,exports){
507+
},{"./bundle":1,"./dereference":2,"./options":4,"./promise":7,"./read":8,"./ref":9,"./refs":10,"./resolve":11,"./util":12,"./yaml":13,"buffer":17,"call-me-maybe":19,"ono":65,"url":90}],4:[function(require,module,exports){
517508
'use strict';
518509

519510
module.exports = $RefParserOptions;
@@ -713,7 +704,7 @@ function isEmpty(value) {
713704

714705
}).call(this,require("buffer").Buffer)
715706

716-
},{"./util":12,"./yaml":14,"buffer":18,"ono":65}],6:[function(require,module,exports){
707+
},{"./util":12,"./yaml":13,"buffer":17,"ono":65}],6:[function(require,module,exports){
717708
'use strict';
718709

719710
module.exports = Pointer;
@@ -1204,7 +1195,7 @@ function download(protocol, u, options) {
12041195

12051196
}).call(this,require('_process'),require("buffer").Buffer)
12061197

1207-
},{"./parse":5,"./promise":7,"./ref":9,"./util":12,"_process":67,"buffer":18,"fs":17,"http":83,"https":27,"ono":65,"url":90}],9:[function(require,module,exports){
1198+
},{"./parse":5,"./promise":7,"./ref":9,"./util":12,"_process":67,"buffer":17,"fs":16,"http":83,"https":27,"ono":65,"url":90}],9:[function(require,module,exports){
12081199
'use strict';
12091200

12101201
module.exports = $Ref;
@@ -1727,49 +1718,8 @@ function crawl$Ref(path, pathFromRoot, $refs, options) {
17271718
(function (process){
17281719
'use strict';
17291720

1730-
var debug = require('debug');
1731-
1732-
exports.path = require('./path');
1733-
1734-
/**
1735-
* Writes messages to stdout.
1736-
* Log messages are suppressed by default, but can be enabled by setting the DEBUG variable.
1737-
* @type {function}
1738-
*/
1739-
exports.debug = debug('json-schema-ref-parser');
1740-
1741-
/**
1742-
* Asynchronously invokes the given callback function with the given parameters.
1743-
*
1744-
* @param {function|undefined} callback
1745-
* @param {*} [err]
1746-
* @param {...*} [params]
1747-
*/
1748-
exports.doCallback = function doCallback(callback, err, params) {
1749-
if (typeof(callback) === 'function') {
1750-
var args = Array.prototype.slice.call(arguments, 1);
1751-
1752-
/* istanbul ignore if: code-coverage doesn't run in the browser */
1753-
if (process.browser) {
1754-
process.nextTick(invokeCallback);
1755-
}
1756-
else {
1757-
setImmediate(invokeCallback);
1758-
}
1759-
}
1760-
1761-
function invokeCallback() {
1762-
callback.apply(null, args);
1763-
}
1764-
};
1765-
1766-
}).call(this,require('_process'))
1767-
1768-
},{"./path":13,"_process":67,"debug":21}],13:[function(require,module,exports){
1769-
(function (process){
1770-
'use strict';
1771-
1772-
var isWindows = /^win/.test(process.platform),
1721+
var debug = require('debug'),
1722+
isWindows = /^win/.test(process.platform),
17731723
forwardSlashPattern = /\//g,
17741724
protocolPattern = /^[a-z0-9.+-]+:\/\//i;
17751725

@@ -1789,12 +1739,24 @@ var urlDecodePatterns = [
17891739
/\%40/g, '@'
17901740
];
17911741

1742+
/**
1743+
* Writes messages to stdout.
1744+
* Log messages are suppressed by default, but can be enabled by setting the DEBUG variable.
1745+
* @type {function}
1746+
*/
1747+
exports.debug = debug('json-schema-ref-parser');
1748+
1749+
/**
1750+
* Utility functions for working with paths and URLs.
1751+
*/
1752+
exports.path = {};
1753+
17921754
/**
17931755
* Returns the current working directory (in Node) or the current page URL (in browsers).
17941756
*
17951757
* @returns {string}
17961758
*/
1797-
exports.cwd = function cwd() {
1759+
exports.path.cwd = function cwd() {
17981760
return process.browser ? location.href : process.cwd() + '/';
17991761
};
18001762

@@ -1804,7 +1766,7 @@ exports.cwd = function cwd() {
18041766
* @param {string} path
18051767
* @returns {boolean}
18061768
*/
1807-
exports.isUrl = function isUrl(path) {
1769+
exports.path.isUrl = function isUrl(path) {
18081770
return protocolPattern.test(path);
18091771
};
18101772

@@ -1814,8 +1776,8 @@ exports.isUrl = function isUrl(path) {
18141776
* @param {string} path
18151777
* @returns {string}
18161778
*/
1817-
exports.localPathToUrl = function localPathToUrl(path) {
1818-
if (!process.browser && !exports.isUrl(path)) {
1779+
exports.path.localPathToUrl = function localPathToUrl(path) {
1780+
if (!process.browser && !exports.path.isUrl(path)) {
18191781
// Manually encode characters that are not encoded by `encodeURI`
18201782
for (var i = 0; i < urlEncodePatterns.length; i += 2) {
18211783
path = path.replace(urlEncodePatterns[i], urlEncodePatterns[i + 1]);
@@ -1831,7 +1793,7 @@ exports.localPathToUrl = function localPathToUrl(path) {
18311793
* @param {string} url
18321794
* @returns {string}
18331795
*/
1834-
exports.urlToLocalPath = function urlToLocalPath(url) {
1796+
exports.path.urlToLocalPath = function urlToLocalPath(url) {
18351797
url = decodeURI(url);
18361798
// Manually decode characters that are not decoded by `decodeURI`
18371799
for (var i = 0; i < urlDecodePatterns.length; i += 2) {
@@ -1849,7 +1811,7 @@ exports.urlToLocalPath = function urlToLocalPath(url) {
18491811
* @param {string} path
18501812
* @returns {string}
18511813
*/
1852-
exports.getHash = function getHash(path) {
1814+
exports.path.getHash = function getHash(path) {
18531815
var hashIndex = path.indexOf('#');
18541816
if (hashIndex >= 0) {
18551817
return path.substr(hashIndex);
@@ -1863,7 +1825,7 @@ exports.getHash = function getHash(path) {
18631825
* @param {string} path
18641826
* @returns {string}
18651827
*/
1866-
exports.stripHash = function stripHash(path) {
1828+
exports.path.stripHash = function stripHash(path) {
18671829
var hashIndex = path.indexOf('#');
18681830
if (hashIndex >= 0) {
18691831
path = path.substr(0, hashIndex);
@@ -1877,7 +1839,7 @@ exports.stripHash = function stripHash(path) {
18771839
* @param {string} path
18781840
* @returns {string}
18791841
*/
1880-
exports.extname = function extname(path) {
1842+
exports.path.extname = function extname(path) {
18811843
var lastDot = path.lastIndexOf('.');
18821844
if (lastDot >= 0) {
18831845
return path.substr(lastDot).toLowerCase();
@@ -1887,7 +1849,7 @@ exports.extname = function extname(path) {
18871849

18881850
}).call(this,require('_process'))
18891851

1890-
},{"_process":67}],14:[function(require,module,exports){
1852+
},{"_process":67,"debug":21}],13:[function(require,module,exports){
18911853
'use strict';
18921854

18931855
var yaml = require('js-yaml');
@@ -1921,7 +1883,7 @@ module.exports = {
19211883
}
19221884
};
19231885

1924-
},{"js-yaml":34}],15:[function(require,module,exports){
1886+
},{"js-yaml":34}],14:[function(require,module,exports){
19251887
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
19261888

19271889
;(function (exports) {
@@ -2047,11 +2009,11 @@ var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
20472009
exports.fromByteArray = uint8ToBase64
20482010
}(typeof exports === 'undefined' ? (this.base64js = {}) : exports))
20492011

2050-
},{}],16:[function(require,module,exports){
2012+
},{}],15:[function(require,module,exports){
20512013

2052-
},{}],17:[function(require,module,exports){
2053-
arguments[4][16][0].apply(exports,arguments)
2054-
},{"dup":16}],18:[function(require,module,exports){
2014+
},{}],16:[function(require,module,exports){
2015+
arguments[4][15][0].apply(exports,arguments)
2016+
},{"dup":15}],17:[function(require,module,exports){
20552017
(function (global){
20562018
/*!
20572019
* The buffer module from node.js, for the browser.
@@ -3598,7 +3560,7 @@ function blitBuffer (src, dst, offset, length) {
35983560

35993561
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
36003562

3601-
},{"base64-js":15,"ieee754":28,"is-array":31}],19:[function(require,module,exports){
3563+
},{"base64-js":14,"ieee754":28,"is-array":31}],18:[function(require,module,exports){
36023564
module.exports = {
36033565
"100": "Continue",
36043566
"101": "Switching Protocols",
@@ -3659,7 +3621,32 @@ module.exports = {
36593621
"511": "Network Authentication Required"
36603622
}
36613623

3662-
},{}],20:[function(require,module,exports){
3624+
},{}],19:[function(require,module,exports){
3625+
(function (process,global){
3626+
"use strict"
3627+
3628+
var next = (global.process && process.nextTick) || global.setImmediate || function (f) {
3629+
setTimeout(f, 0)
3630+
}
3631+
3632+
module.exports = function maybe (cb, promise) {
3633+
if (cb) {
3634+
promise
3635+
.then(function (result) {
3636+
next(function () { cb(null, result) })
3637+
}, function (err) {
3638+
next(function () { cb(err) })
3639+
})
3640+
return undefined
3641+
}
3642+
else {
3643+
return promise
3644+
}
3645+
}
3646+
3647+
}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
3648+
3649+
},{"_process":67}],20:[function(require,module,exports){
36633650
(function (Buffer){
36643651
// Copyright Joyent, Inc. and other Node contributors.
36653652
//
@@ -14052,7 +14039,7 @@ module.exports = new Type('tag:yaml.org,2002:binary', {
1405214039
represent: representYamlBinary
1405314040
});
1405414041

14055-
},{"../type":47,"buffer":16}],49:[function(require,module,exports){
14042+
},{"../type":47,"buffer":15}],49:[function(require,module,exports){
1405614043
'use strict';
1405714044

1405814045
var Type = require('../type');
@@ -17202,7 +17189,7 @@ function indexOf (xs, x) {
1720217189

1720317190
}).call(this,require('_process'))
1720417191

17205-
},{"./_stream_duplex":74,"_process":67,"buffer":18,"core-util-is":20,"events":25,"inherits":30,"isarray":33,"process-nextick-args":66,"string_decoder/":89,"util":16}],77:[function(require,module,exports){
17192+
},{"./_stream_duplex":74,"_process":67,"buffer":17,"core-util-is":20,"events":25,"inherits":30,"isarray":33,"process-nextick-args":66,"string_decoder/":89,"util":15}],77:[function(require,module,exports){
1720617193
// a transform stream is a readable/writable stream where you do
1720717194
// something with the data. Sometimes it's called a "filter",
1720817195
// but that's not a great name for it, since that implies a thing where
@@ -17923,7 +17910,7 @@ function endWritable(stream, state, cb) {
1792317910
state.ended = true;
1792417911
}
1792517912

17926-
},{"./_stream_duplex":74,"buffer":18,"core-util-is":20,"events":25,"inherits":30,"process-nextick-args":66,"util-deprecate":91}],79:[function(require,module,exports){
17913+
},{"./_stream_duplex":74,"buffer":17,"core-util-is":20,"events":25,"inherits":30,"process-nextick-args":66,"util-deprecate":91}],79:[function(require,module,exports){
1792717914
module.exports = require("./lib/_stream_passthrough.js")
1792817915

1792917916
},{"./lib/_stream_passthrough.js":75}],80:[function(require,module,exports){
@@ -18021,7 +18008,7 @@ http.METHODS = [
1802118008
'UNLOCK',
1802218009
'UNSUBSCRIBE'
1802318010
]
18024-
},{"./lib/request":85,"builtin-status-codes":19,"url":90,"xtend":94}],84:[function(require,module,exports){
18011+
},{"./lib/request":85,"builtin-status-codes":18,"url":90,"xtend":94}],84:[function(require,module,exports){
1802518012
(function (global){
1802618013
exports.fetch = isFunction(global.fetch) && isFunction(global.ReadableByteStream)
1802718014

@@ -18349,7 +18336,7 @@ var unsafeHeaders = [
1834918336

1835018337
}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("buffer").Buffer)
1835118338

18352-
},{"./capability":84,"./response":86,"_process":67,"buffer":18,"foreach":26,"indexof":29,"inherits":30,"object-keys":87,"stream":72}],86:[function(require,module,exports){
18339+
},{"./capability":84,"./response":86,"_process":67,"buffer":17,"foreach":26,"indexof":29,"inherits":30,"object-keys":87,"stream":72}],86:[function(require,module,exports){
1835318340
(function (process,global,Buffer){
1835418341
var capability = require('./capability')
1835518342
var foreach = require('foreach')
@@ -18527,7 +18514,7 @@ IncomingMessage.prototype._onXHRProgress = function () {
1852718514

1852818515
}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {},require("buffer").Buffer)
1852918516

18530-
},{"./capability":84,"_process":67,"buffer":18,"foreach":26,"inherits":30,"stream":72}],87:[function(require,module,exports){
18517+
},{"./capability":84,"_process":67,"buffer":17,"foreach":26,"inherits":30,"stream":72}],87:[function(require,module,exports){
1853118518
'use strict';
1853218519

1853318520
// modified from https://github.com/es-shims/es5-shim
@@ -18893,7 +18880,7 @@ function base64DetectIncompleteChar(buffer) {
1889318880
this.charLength = this.charReceived ? 3 : 0;
1889418881
}
1889518882

18896-
},{"buffer":18}],90:[function(require,module,exports){
18883+
},{"buffer":17}],90:[function(require,module,exports){
1889718884
// Copyright Joyent, Inc. and other Node contributors.
1889818885
//
1889918886
// Permission is hereby granted, free of charge, to any person obtaining a

0 commit comments

Comments
 (0)