schangxiang@126.com
2025-09-19 0821aa23eabe557c0d9ef5dbe6989c68be35d1fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
 
/**
 * Module dependencies.
 */
 
var parse = require('url').parse;
var debug = require('debug')('get-uri');
 
/**
 * Module exports.
 */
 
module.exports = exports = getUri;
 
/**
 * Supported "protocols".
 */
 
exports.protocols = {
  data: require('./data'),
  file: require('./file'),
  ftp: require('./ftp'),
  http: require('./http'),
  https: require('./https')
};
 
/**
 * Async function that returns a `stream.Readable` instance to the
 * callback function that will output the contents of the given URI.
 *
 * For caching purposes, you can pass in a `stream` instance from a previous
 * `getUri()` call as a `cache: stream` option, and if the destination has
 * not changed since the last time the endpoint was retreived then the callback
 * will be invoked with an Error object with `code` set to "ENOTMODIFIED" and
 * `null` for the "stream" instance argument. In this case, you can skip
 * retreiving the file again and continue to use the previous payload.
 *
 * @param {String} uri URI to retrieve
 * @param {Object} opts optional "options" object
 * @param {Function} fn callback function
 * @api public
 */
 
function getUri (uri, opts, fn) {
  debug('getUri(%o)', uri);
 
  if ('function' == typeof opts) {
    fn = opts;
    opts = null;
  }
  if ('function' != typeof fn) {
    throw new TypeError('a callback function must be provided');
  }
 
  if (!uri) return fn(new TypeError('must pass in a URI to "get"'));
 
  var parsed = parse(uri);
  var protocol = parsed.protocol;
  if (!protocol) return fn(new TypeError('URI does not contain a protocol: ' + uri));
 
  // strip trailing :
  protocol = protocol.replace(/\:$/, '');
 
  var getter = exports.protocols[protocol];
 
  if ('function' != typeof getter)
    return fn(new TypeError('unsupported protocol "' + protocol + '" specified in URI: ' + uri));
 
  getter(parsed, opts || {}, fn);
}