Skip to content

publicclass/open-uri

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Asynchronous Open URI

A CommonJS module inspired by Rubys Open-URI library.

Install

It's available on npm, so a simple npm install open-uri should be enough.

Usage

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:pass@ftp.example.com/myfile.txt",function(err,txt){console.log(txt)})

Supported schemes

http, https, file & ftp

Uses the addressable module for parsing the scheme and the mime module for parsing the data.

Options for all schemes

  • 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 to true.

Options for HTTP(S)

  • follow (boolean) Follow redirects. Defaults to true.
  • gzip (boolean) If the request should be attempted with gzip. Defaults to true.
  • 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 var NODE_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.

Options for file

  • encoding (string) The encoding of the file to read.

Options for FTP

  • anonymous (boolean) If no user info is in the URI, add anonymous. Defaults to true.

History

0.4.1

  • [Fix] Added buffer-option to file and ftp schemes.
  • [Change] Updated mime dependency to 1.2.5.

0.4.0

  • [Change] Added the buffer option, which is an inverted stream, which is now deprecated.
  • [Fix] Avoid a global variable (by senorpedro)
  • [Fix] HTTP/HTTPS Only 301, 302 and 307 is proper statusCodes for redirects.

0.3.5

  • [Fix] Use Buffer.byteLength(body) only on strings, and body.length on Buffers.
  • [Fix] Support for broken redirect implementations that give relative Location: headers.

0.3.4

  • [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).

0.3.3

  • [Fix] Avoid 'Out of bounds'-errors by using Buffer.byteLength(chunk) instead of chunk.length.

0.3.2

  • [Fix] Errors was being thrown when using userinfo in HTTP uris.

0.3.1

  • [Fix] Updated addressable dependency to 0.3.3. Now both addressable.URI and Nodes built-in URL objects are valid as the uri argument.
  • [Fix] Fixed error thrown when an error occurs in utils.buffer().

0.3.0

  • [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.

0.2.2

  • [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.

0.2.1

  • [Feature] Better parsing of content using the mime module for Content-Type lookup.

  • [Feature] FTP Initial support using the node-ftp module.

0.2.0

  • [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.

0.1.0

  • Initial version.

TODO

  • 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://bot@chat.freenode.net/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; or sqlite3://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 like function(err,rows,meta){}.

Thanks to

License

(The MIT License)

Copyright (c) 2011 Robert Sköld <robert@publicclass.se>

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.

About

Asynchronous Open URI, a CommonJS module inspired by Rubys Open-URI library.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •