You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. get-uri
  2. =======
  3. ### Returns a `stream.Readable` from a URI string
  4. [![Build Status](https://travis-ci.org/TooTallNate/node-get-uri.svg?branch=master)](https://travis-ci.org/TooTallNate/node-get-uri)
  5. This high-level module accepts a URI string and returns a `Readable` stream
  6. instance. There is built-in support for a variety of "protocols", and it's
  7. easily extensible with more:
  8. | Protocol | Description | Example
  9. |:---------:|:-------------------------------:|:---------------------------------:
  10. | `data` | [Data URIs][data] | `data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D`
  11. | `file` | [File URIs][file] | `file:///c:/windows/example.ini`
  12. | `ftp` | [FTP URIs][ftp] | `ftp://ftp.kernel.org/pub/site/README`
  13. | `http` | [HTTP URIs][http] | `http://www.example.com/path/to/name`
  14. | `https` | [HTTPS URIs][https] | `https://www.example.com/path/to/name`
  15. Installation
  16. ------------
  17. Install with `npm`:
  18. ``` bash
  19. $ npm install get-uri
  20. ```
  21. Example
  22. -------
  23. To simply get a `stream.Readable` instance from a `file:` URI, try something like:
  24. ``` js
  25. var getUri = require('get-uri');
  26. // `file:` maps to a `fs.ReadStream` instance…
  27. getUri('file:///Users/nrajlich/wat.json', function (err, rs) {
  28. if (err) throw err;
  29. rs.pipe(process.stdout);
  30. });
  31. ```
  32. Missing Endpoints
  33. -----------------
  34. When you pass in a URI in which the resource referenced does not exist on the
  35. destination server, then a `NotFoundError` will be returned. The `code` of the
  36. error instance is set to `"ENOTFOUND"`, so you can special-case that in your code
  37. to detect when a bad filename is requested:
  38. ``` js
  39. getUri('http://example.com/resource.json', function (err, rs) {
  40. if (err) {
  41. if ('ENOTFOUND' == err.code) {
  42. // bad file path requested
  43. } else {
  44. // something else bad happened...
  45. throw err;
  46. }
  47. }
  48. // your app code…
  49. });
  50. ```
  51. Cacheability
  52. ------------
  53. When calling `getUri()` with the same URI multiple times, the `get-uri` module
  54. supports sending an indicator that the remote resource has not been modified
  55. since the last time it has been retreived from that node process.
  56. To do this, pass in a `cache` option to the "options object" argument
  57. with the value set to the `stream.Readable` instance that was previously
  58. returned. If the remote resource has not been changed since the last call for
  59. that same URI, then a `NotModifiedError` instance will be returned with it's
  60. `code` property set to `"ENOTMODIFIED"`.
  61. When the `"ENOTMODIFIED"` error occurs, then you can safely re-use the
  62. results from the previous `getUri()` call for that same URI:
  63. ``` js
  64. // maps to a `fs.ReadStream` instance
  65. getUri('http://example.com/resource.json', function (err, rs) {
  66. if (err) throw err;
  67. // … some time later, if you need to get this same URI again, pass in the
  68. // previous `stream.Readable` instance as `cache` option to potentially
  69. // receive an "ENOTMODIFIED" response:
  70. var opts = { cache: rs };
  71. getUri('http://example.com/resource.json', opts, function (err, rs2) {
  72. if (err) {
  73. if ('ENOTFOUND' == err.code) {
  74. // bad file path requested
  75. } else if ('ENOTMODIFIED' == err.code) {
  76. // source file has not been modified since last time it was requested,
  77. // so `rs2` is undefined and you are expected to re-use results from
  78. // a previous call to `getUri()`
  79. } else {
  80. // something else bad happened...
  81. throw err;
  82. }
  83. }
  84. });
  85. });
  86. ```
  87. API
  88. ---
  89. ### getUri(String uri[, Object options,] Function callback)
  90. A `uri` String is required. An optional `options` object may be passed in:
  91. - `cache` - A `stream.Readable` instance from a previous call to `getUri()` with the same URI. If this option is passed in, and the destination endpoint has not been modified, then an `ENOTMODIFIED` error is returned
  92. Any other options passed in to the `options` object will be passed through
  93. to the low-level connection creation functions (`http.get()`, `ftp.connect()`,
  94. etc).
  95. Invokes the given `callback` function with a `stream.Readable` instance to
  96. read the resource at the given `uri`.
  97. License
  98. -------
  99. (The MIT License)
  100. Copyright (c) 2014 Nathan Rajlich <nathan@tootallnate.net>
  101. Permission is hereby granted, free of charge, to any person obtaining
  102. a copy of this software and associated documentation files (the
  103. 'Software'), to deal in the Software without restriction, including
  104. without limitation the rights to use, copy, modify, merge, publish,
  105. distribute, sublicense, and/or sell copies of the Software, and to
  106. permit persons to whom the Software is furnished to do so, subject to
  107. the following conditions:
  108. The above copyright notice and this permission notice shall be
  109. included in all copies or substantial portions of the Software.
  110. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  111. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  112. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  113. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  114. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  115. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  116. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  117. [data]: http://tools.ietf.org/html/rfc2397
  118. [file]: http://tools.ietf.org/html/draft-hoffman-file-uri-03
  119. [ftp]: http://www.w3.org/Protocols/rfc959/
  120. [http]: http://www.w3.org/Protocols/rfc2616/rfc2616.html
  121. [https]: http://wikipedia.org/wiki/HTTP_Secure