Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 59672933cf6f0494e19ac3a57ae64b33b61f7077 @jed committed Mar 2, 2011
Showing with 160 additions and 0 deletions.
  1. +20 −0 LICENSE.txt
  2. +73 −0 index.js
  3. +7 −0 package.json
  4. +60 −0 test.js
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Jed Schmidt, http://jedschmidt.com/
+
+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.
@@ -0,0 +1,73 @@
+var fs = require( "fs" )
+ , exec = require( "child_process" ).exec
+ , Localhoses = function(){}
+
+if ( process.platform != "darwin" ) throw "Sorry, only tested on OS X."
+
+Localhoses.prototype = {
+ hostsPath: "/private/etc/hosts",
+ backupSuffix: ".localhoseBackup",
+
+ domains: function(){ return Object.keys( this ) },
+
+ set: function() {
+ for ( var i = arguments.length; i--; ) this[ arguments[ i ] ] = true
+
+ return this.save()
+ },
+
+ unset: function() {
+ var domains = 0 in arguments ? arguments : this.domains()
+
+ for ( var i = domains.length; i--; ) delete this[ domains[ i ] ]
+
+ return this.save()
+ },
+
+ teardown: function() {
+ var hostsPath = this.hostsPath
+ , backupPath = hostsPath + this.backupSuffix
+ , backup
+
+ try {
+ backup = fs.readFileSync( backupPath )
+ fs.unlinkSync( backupPath )
+ fs.writeFileSync( hostsPath, backup )
+ }
+
+ catch( e ){}
+
+ return this
+ },
+
+ toString: function() {
+ return [ "\n\n# The following were added by 'localhose':" ]
+ .concat( this.domains() )
+ .join( "\n127.0.0.1\t" )
+ },
+
+ save: function() {
+ exec( "dscacheutil -flushcache" )
+
+ if ( !this.domains().length ) return this.teardown()
+
+ var hostsPath = this.hostsPath
+ , backupPath = hostsPath + this.backupSuffix
+ , hosts, backup
+
+ try { hosts = fs.readFileSync( hostsPath ) }
+ catch ( e ) { throw "/etc/hosts does not exist or cannot be read." }
+
+ try { backup = fs.readFileSync( backupPath ) }
+ catch ( e ) {
+ try { fs.writeFileSync( backupPath, backup = hosts ) }
+ catch ( e ){ throw backupPath + " is not writeable." }
+ }
+
+ fs.writeFileSync( hostsPath, backup + this.toString() )
+
+ return this
+ }
+}
+
+module.exports = new Localhoses
@@ -0,0 +1,7 @@
+{
+ "name": "localhose",
+ "version": "0.0.1",
+ "description": "Local DNS hosing for easy development",
+
+ "main": "./index"
+}
60 test.js
@@ -0,0 +1,60 @@
+var assert = require( "assert" )
+ , http = require( "http" )
+ , localhose = require( "localhose" ).unset()
+
+ , url = require( "url" ).parse( "http://nodejs.org/" )
+ , sassyTitle = "rYah'S aweSOmE homEpagE!"
+
+ , server = http.createServer( handler )
+
+server.listen( 80 )
+
+getTitle( url, function( title ) {
+ console.log( "\nfetching title for " + url.host )
+ console.log( "- expected title: node.js" )
+ console.log( "- actual title: " + title )
+
+ assert.equal( title, "node.js" )
+
+ localhose.set( url.host )
+
+ getTitle( url, function( title ) {
+ console.log( "\nfetching title for " + url.host )
+ console.log( "- expected title: " + sassyTitle )
+ console.log( "- actual title: " + title )
+
+ assert.equal( title, sassyTitle )
+
+ localhose.unset()
+
+ getTitle( url, function( title ) {
+ console.log( "\nfetching title for " + url.host )
+ console.log( "- expected title: node.js" )
+ console.log( "- actual title: " + title )
+
+ assert.equal( title, "node.js" )
+
+ server.close()
+ })
+ })
+})
+
+function handler( req, res ) {
+ res.writeHead( 200, { "Content-Type": "text/html" } )
+ res.end(
+ "<html>" +
+ "<head><title>" + sassyTitle + "</title></head>" +
+ "<body>welcome to " + sassyTitle + "</body>" +
+ "</html>"
+ )
+}
+
+function getTitle( uri, cb ) {
+ var body = ""
+
+ http.get( url, function( res ) {
+ res
+ .on( "data", function( data ){ body += data } )
+ .on( "end", function(){ cb( ( /<title>([^<]+)/( body ) || [] )[ 1 ] ) } )
+ })
+}

0 comments on commit 5967293

Please sign in to comment.