UNPKG

2.14 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.getUri = exports.isValidProtocol = exports.protocols = void 0;
7const debug_1 = __importDefault(require("debug"));
8// Built-in protocols
9const data_1 = require("./data");
10const file_1 = require("./file");
11const ftp_1 = require("./ftp");
12const http_1 = require("./http");
13const https_1 = require("./https");
14const debug = (0, debug_1.default)('get-uri');
15exports.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};
22const VALID_PROTOCOLS = new Set(Object.keys(exports.protocols));
23function isValidProtocol(p) {
24 return VALID_PROTOCOLS.has(p);
25}
26exports.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 */
42async 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}
56exports.getUri = getUri;
57//# sourceMappingURL=index.js.map
\No newline at end of file