A CommonJS module inspired by Rubys Open-URI library.
It's available on npm, so a simple npm install open-uri
should be enough.
var open = require("open-uri");
// Get a website
open("http://google.com",function(err,google){console.log(google)})
// Get a file
open("/var/log/system.log",function(err,log){console.log(log)})
// Stream a file to STDOUT
open("file:///var/log/system.log",process.stdout)
// Stream a website to a file
var file = require("fs").createWriteStream("/tmp/goog.html")
open("https://encrypted.google.com/search?q=open+uri",file)
// Chain it
open("http://google.com",console.log)("http://publicclass.se",process.stdout)
// Get a file off of an FTP
open("ftp://user:[email protected]/myfile.txt",function(err,txt){console.log(txt)})
http, https, file & ftp
Uses the addressable module for parsing the scheme and the mime module for parsing the data.
buffer
(boolean) For when you only want the body to be buffered and returned as the second argument instead of receiving the direct stream. Defaults totrue
.
follow
(boolean) Follow redirects. Defaults totrue
.gzip
(boolean) If the request should be attempted with gzip. Defaults totrue
.headers
(object) Headers to pass along with the HTTP request.method
(string) HTTP request method. Default 'GET'.body
(readablestream|string|buffer) A body of data to pass with the request.key
(string) (HTTPS Only) Private key to use for SSL. Defaults to ENV varNODE_HTTPS_KEY
or attempts to find it as "./key.pem".cert
(string) (HTTPS Only) Public x509 certificate to use. Defaults to ENV var NODE_HTTPS_CERT or attempts to find it as "./cert.pem".ca
(string|array) (HTTPS Only) An authority certificate or array of authority certificates to check the remote host against.
encoding
(string) The encoding of the file to read.
anonymous
(boolean) If no user info is in the URI, add anonymous. Defaults totrue
.
- [Fix] Added
buffer
-option to file and ftp schemes. - [Change] Updated mime dependency to 1.2.5.
- [Change] Added the
buffer
option, which is an invertedstream
, which is now deprecated. - [Fix] Avoid a global variable (by senorpedro)
- [Fix] HTTP/HTTPS Only 301, 302 and 307 is proper statusCodes for redirects.
- [Fix] Use Buffer.byteLength(body) only on strings, and body.length on Buffers.
- [Fix] Support for broken redirect implementations that give relative Location: headers.
- [Fix] Use Buffer.byteLength(body) for Content-Length header as well, it's just better (and fixes a bug where the body was cut off).
- [Fix] Avoid 'Out of bounds'-errors by using Buffer.byteLength(chunk) instead of chunk.length.
- [Fix] Errors was being thrown when using userinfo in HTTP uris.
- [Fix] Updated
addressable
dependency to 0.3.3. Now both addressable.URI and Nodes built-in URL objects are valid as theuri
argument. - [Fix] Fixed error thrown when an error occurs in utils.buffer().
-
[Feature] Updated
addressable
dependency to 0.3.1. -
[Feature] HTTPS Added key and certificate parsing.
-
[Feature] HTTP/HTTPS Added a
method
option can be used if anything other than GET is desired. -
[Feature] HTTP/HTTPS Added a
body
options for passing a payload with the request. Can be either a ReadableStream, a String or a Buffer.
-
[Feature] Re-factored the schemes into separate files. Now adding more schemes will be much easier.
-
[Feature] Added a simple binary. Usage:
open-uri http://google.com
-
[Fix] FTP is now closing properly when completed or failed.
-
[Feature] Better parsing of content using the mime module for Content-Type lookup.
-
[Feature] FTP Initial support using the node-ftp module.
-
[Feature] HTTP(S) Now supports gzip responses (and adds a 'Accept-Encoding: gzip'-header if there is none already).
-
[Feature] HTTP(S) Now decodes JSON if response is of 'Content-Type: application/json'.
-
[Fix] HTTP(S) Fixed an issue with redirects.
- Initial version.
-
HTTP(S) Support older versions of Node? Currently only support 0.3.6 and up (because of the new HTTP Client API).
-
HTTP(S) Proxy support.
-
FTP Listing files (when uri.pathname ends with "/")
-
FTP Uploading files (when opts.body is passed)
-
A timeout option for all schemes would be useful.
-
A binary, just for fun really. Usage:
open-uri http://google.com
.- Output to process.stdout by default.
- Allow a REST interface? So if something is written to stdin it's sent as body to the url using POST by default, but definable with -X (curl style!)
- Pass command line arguments as options to the scheme functions. Ex.
open-uri -gzip http://google.com
sets opts.gzip to true.
-
More schemes support? Suggestions?
- Support for opening a file descriptor? Basically a shortcut for fs.readFile(fd).
- S3 (using knox module), should list objects when path ends with "/"
- NNTP (using node-nntp module)
- IRC (using node-irc module), open("irc://[email protected]/nodejs") -> callback for each message? with a say function in the callback? ex:
function(err,from,to,msg,say){ if( to == "bot" && ~msg.indexOf("hello") ){ say(from+": HI!") } }
this
could be the irc object so the socket can be closed... - SQL query. Is there a standard URI for this? Something like:
mysql://root@localhost/mydb?query=SELECT * FROM x;
orsqlite3://file.sqlite?query=SELECT * FROM x;
possibly with support for input escaping. open("sqlite3://file.sqlite?query=SELECT * FROM x WHERE id=? AND name=?;",[12,"bob"]) using the drivers own escaping. With a callback likefunction(err,rows,meta){}
.
-
Mikeal Rogers, for his excellent request module.
-
TJ Holowaychuck, for his minimalist projects inspiring this little tool.
-
Brian White, for ftp and nntp modules.
(The MIT License)
Copyright (c) 2011 Robert Sköld <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.