visionmedia/superagent — pull request #287 by Zulunko (2013-11-15 21:38:54+00:00)
Title: Added pipe redirection and authorization mapping.

This makes piping work properly with redirection by not using the given stream until a response that does not have a redirect status code.

In addition, this adds the ```authmap``` function to a request and changes ```auth``` to use ```authmap``` in the background. The ```authmap``` function takes in an object/an array of objects defined as ```{"regex":"", "user":"", "pass":""}```. When sending a request, URLs are matched against the given regexes (in order) and the first match is used for the request to the URL. A normal ```auth``` call creates an array of one object with a regex of ```.*```, meaning the ```auth``` function will still work the same as it always has.

There are two cases in which the ```authmap``` function is useful:

1. If you have an application that needs to contact a lot of different servers with different authentications, you can store the authentications in an array (that ```authmap``` can read) and then just send the same array every single time (rather than manually specifying individual authentications per-call).
2. If your request gets redirected and you need authentication for the redirected location, this will automatically handle that for you, provided you gave it an object with a regex that matches the redirected location.

Sample request:

```javascript
var request = require('superagent');

request
.get('http://myserver/1')
.authmap([
{'regex':'.*myserver.*', 'user':'me', 'pass':'mypassword'},
{'regex':'.*redirectserver.*', 'user':'admin', 'pass':'root'}])
.end(function(res) {
if (res.ok) {
console.log("Success.");
} else {
console.log("Failure?!");
}
});
```

The above request will use the ```.*myserver.*``` regex authentication for its first request. If the above request gets redirected to something that matches ```.*redirectserver.*```, it will use the matching authentication to connect to that server. If the request gets redirected to a non-matching URL, it will use no authentication.