index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.getUri = exports.isValidProtocol = exports.protocols = void 0;
  7. const debug_1 = __importDefault(require("debug"));
  8. // Built-in protocols
  9. const data_1 = require("./data");
  10. const file_1 = require("./file");
  11. const ftp_1 = require("./ftp");
  12. const http_1 = require("./http");
  13. const https_1 = require("./https");
  14. const debug = (0, debug_1.default)('get-uri');
  15. exports.protocols = {
  16. data: data_1.data,
  17. file: file_1.file,
  18. ftp: ftp_1.ftp,
  19. http: http_1.http,
  20. https: https_1.https,
  21. };
  22. const VALID_PROTOCOLS = new Set(Object.keys(exports.protocols));
  23. function isValidProtocol(p) {
  24. return VALID_PROTOCOLS.has(p);
  25. }
  26. exports.isValidProtocol = isValidProtocol;
  27. /**
  28. * Async function that returns a `stream.Readable` instance that will output
  29. * the contents of the given URI.
  30. *
  31. * For caching purposes, you can pass in a `stream` instance from a previous
  32. * `getUri()` call as a `cache: stream` option, and if the destination has
  33. * not changed since the last time the endpoint was retreived then the callback
  34. * will be invoked with an Error object with `code` set to "ENOTMODIFIED" and
  35. * `null` for the "stream" instance argument. In this case, you can skip
  36. * retreiving the file again and continue to use the previous payload.
  37. *
  38. * @param {String} uri URI to retrieve
  39. * @param {Object} opts optional "options" object
  40. * @api public
  41. */
  42. async function getUri(uri, opts) {
  43. debug('getUri(%o)', uri);
  44. if (!uri) {
  45. throw new TypeError('Must pass in a URI to "getUri()"');
  46. }
  47. const url = typeof uri === 'string' ? new URL(uri) : uri;
  48. // Strip trailing `:`
  49. const protocol = url.protocol.replace(/:$/, '');
  50. if (!isValidProtocol(protocol)) {
  51. throw new TypeError(`Unsupported protocol "${protocol}" specified in URI: "${uri}"`);
  52. }
  53. const getter = exports.protocols[protocol];
  54. return getter(url, opts);
  55. }
  56. exports.getUri = getUri;
  57. //# sourceMappingURL=index.js.map