index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. const debug_1 = __importDefault(require("debug"));
  6. const url_1 = require("url");
  7. // Built-in protocols
  8. const data_1 = __importDefault(require("./data"));
  9. const file_1 = __importDefault(require("./file"));
  10. const ftp_1 = __importDefault(require("./ftp"));
  11. const http_1 = __importDefault(require("./http"));
  12. const https_1 = __importDefault(require("./https"));
  13. const debug = debug_1.default('get-uri');
  14. function getUri(uri, opts, fn) {
  15. const p = new Promise((resolve, reject) => {
  16. debug('getUri(%o)', uri);
  17. if (typeof opts === 'function') {
  18. fn = opts;
  19. opts = undefined;
  20. }
  21. if (!uri) {
  22. reject(new TypeError('Must pass in a URI to "get"'));
  23. return;
  24. }
  25. const parsed = url_1.parse(uri);
  26. // Strip trailing `:`
  27. const protocol = (parsed.protocol || '').replace(/:$/, '');
  28. if (!protocol) {
  29. reject(new TypeError(`URI does not contain a protocol: ${uri}`));
  30. return;
  31. }
  32. const getter = getUri.protocols[protocol];
  33. if (typeof getter !== 'function') {
  34. throw new TypeError(`Unsupported protocol "${protocol}" specified in URI: ${uri}`);
  35. }
  36. resolve(getter(parsed, opts || {}));
  37. });
  38. if (typeof fn === 'function') {
  39. p.then(rtn => fn(null, rtn), err => fn(err));
  40. }
  41. else {
  42. return p;
  43. }
  44. }
  45. (function (getUri) {
  46. getUri.protocols = {
  47. data: data_1.default,
  48. file: file_1.default,
  49. ftp: ftp_1.default,
  50. http: http_1.default,
  51. https: https_1.default
  52. };
  53. })(getUri || (getUri = {}));
  54. module.exports = getUri;
  55. //# sourceMappingURL=index.js.map