Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

streaming files and support for ?query=s

  • Loading branch information...
commit ff09ec2a8044e2f9e8432b35a6900959465d422e 1 parent 4bf398b
@pvorb authored
View
2  LICENSE
@@ -1,4 +1,4 @@
-Copyright © 2012 Paul Vorbach <paul@vorb.de> (http://vorb.de)
+Copyright © 2012 Paul Vorbach
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
View
17 README.mkd → README.md
@@ -1,25 +1,22 @@
# wup
-tiny webserver
+tiny webserver for fast, straightforward tests
-wup does only serve files in the current working directory. It only serves HTTP
-GET requests. There are no configuration files. The port used is always 8080.
+wup serves files in the current working directory only. It serves HTTP GET
+requests. No POST, no PUT, etc. There are no configuration files. The web server
+will always run at `http://localhost:8080/`.
That's it.
## Installation
-```
-npm install -g wup
-```
+ npm install -g wup
## Usage
On the command line enter
-```
-wup
-```
+ wup
All files in the current working directory will become available at
`http://localhost:8080/`.
@@ -28,6 +25,8 @@ All files in the current working directory will become available at
Copyright © 2012 Paul Vorbach
+(MIT License)
+
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
View
76 node_modules/filed/README.md
@@ -0,0 +1,76 @@
+# filed -- Simplified file library.
+
+## Install
+
+<pre>
+ npm install filed
+</pre>
+
+## Super simple to use
+
+Filed does a lazy stat call so you can actually open a file and begin writing to it and if the file isn't there it will just be created.
+
+```javascript
+var filed = require('filed');
+var f = filed('/newfile')
+f.write('test')
+f.end()
+```
+
+## Streaming
+
+The returned file object is a stream so you can do standard stream stuff to it. Based on *what* you do the object it will be a read stream, a write stream.
+
+So if you send data to it, it'll be a write stream.
+
+```javascript
+fs.createReadStream.pipe(filed('/newfile'))
+```
+
+If you pipe it to a destination it'll be a read stream.
+
+```javascript
+filed('/myfile').pipe(fs.createWriteStream('/out'))
+```
+
+And of course you can pipe a filed object from itself to itself and it'll figure it out.
+
+```javascript
+filed('/myfile').pipe(filed('/newfile'))
+```
+
+Those familiar with [request](http://github.com/mikeal/request) will be familiar seeing object capability detection when doing HTTP. filed does this as well.
+
+```javascript
+http.createServer(function (req, resp) {
+ filed('/data.json').pipe(resp)
+})
+```
+
+Not only does the JSON file get streamed to the HTTP Response it will include an Etag, Last-Modified, Content-Length, and a Content-Type header based on the filed extension.
+
+```javascript
+http.createServer(function (req, resp) {
+ req.pipe(filed('/newfile')).pipe(resp)
+})
+```
+
+When accepting a PUT request data will be streamed to the file and a 201 status will be sent on the HTTP Response when the upload is finished.
+
+During a GET request a 404 Response will be sent if the file does not exist.
+
+```javascript
+http.createServer(function (req, resp) {
+ req.pipe(filed('/data.json')).pipe(resp)
+})
+```
+
+The Etag and Last-Modified headers filed creates are based solely on the stat() call so if you pipe a request to an existing file the cache control headers will be taken into account; a 304 response will be sent if the cache control headers match a new stat() call. This can be very helpful in avoiding unnecessary disc reads.
+
+```javascript
+http.createServer(function (req, resp) {
+ req.pipe(filed('/directory')).pipe(resp)
+})
+```
+
+Just to round out the full feature set and make it full file server if you give filed an existing directory it will actually check for an index.html file in that directory and serve it if it exists.
View
202 node_modules/filed/main.js
@@ -0,0 +1,202 @@
+var fs = require('fs')
+ , util = require('util')
+ , crypto = require('crypto')
+ , stream = require('stream')
+ , path = require('path')
+ , mimetypes = require('./mimetypes')
+ , rfc822 = require('./rfc822')
+ ;
+
+function File (options) {
+ stream.Stream.call(this)
+
+ this.writable = true
+ this.readable = true
+ this.buffers = []
+
+ var self = this
+
+ if (typeof options === 'string') options = {path:options}
+ if (!options.index) options.index = 'index.html'
+ self.writable = (typeof options.writable === "undefined") ? true : options.writable
+ self.readable = (typeof options.readable === "undefined") ? true : options.readable
+
+ self.path = options.path
+ self.index = options.index
+
+ self.on('pipe', function (src) {
+ this.src = src
+ })
+
+ this.buffering = true
+
+ this.mimetype = options.mimetype || mimetypes.lookup(this.path.slice(this.path.lastIndexOf('.')+1))
+
+ var stopBuffering = function () {
+ self.buffering = false
+ while (self.buffers.length) {
+ self.emit('data', self.buffers.shift())
+ }
+ if (self.ended) self.emit('end')
+ }
+
+ fs.stat(options.path, function (err, stats) {
+
+ var finish = function (err, stats) {
+ self.stat = stats
+ if (err && err.code === 'ENOENT' && !self.dest && !self.src) self.src = self.path
+ if (err && !self.dest && !self.src) return self.emit('error', err)
+ if (err && self.dest && !self.dest.writeHead) return self.emit('error', err)
+
+ // See if writes are disabled
+ if (self.src && self.src.method &&
+ !self.writable && self.dest.writeHead &&
+ (self.src.method === 'PUT' || self.src.method === 'POST')) {
+ self.dest.writeHead(405, {'content-type':'text/plain'})
+ self.dest.end(self.src.method+' Not Allowed')
+ return
+ }
+
+ if (!err) {
+ self.etag = crypto.createHash('md5').update(stats.ino+'/'+stats.mtime+'/'+stats.size).digest("hex")
+ self.lastmodified = rfc822.getRFC822Date(stats.mtime)
+ }
+
+ process.nextTick(function () {
+ stopBuffering()
+ })
+
+ // 404 and 500
+ if ( err && self.dest && self.dest.writeHead && // We have an error object and dest is an HTTP response
+ ( // Either we have a source and it's a GET/HEAD or we don't have a src
+ (self.src && (self.src.method == 'GET' || self.src.method === 'HEAD')) || (!self.src)
+ )
+ ) {
+ if (err.code === 'ENOENT') {
+ self.dest.statusCode = 404
+ self.dest.end('Not Found')
+ } else {
+ self.dest.statusCode = 500
+ self.dest.end(err.message)
+ }
+ return
+ }
+
+ // Source is an HTTP Server Request
+ if (self.src && (self.src.method === 'GET' || self.src.method === 'HEAD') && self.dest) {
+ if (self.dest.setHeader) {
+ self.dest.setHeader('content-type', self.mimetype)
+ self.dest.setHeader('etag', self.etag)
+ self.dest.setHeader('last-modified', self.lastmodified)
+ }
+
+ if (self.dest.writeHead) {
+ if (self.src && self.src.headers) {
+ if (self.src.headers['if-none-match'] === self.etag ||
+ // Lazy last-modifed matching but it's faster than parsing Datetime
+ self.src.headers['if-modified-since'] === self.lastmodified) {
+ self.dest.statusCode = 304
+ self.dest.end()
+ return
+ }
+ }
+ // We're going to return the whole file
+ self.dest.statusCode = 200
+ self.dest.setHeader('content-length', stats.size)
+ } else {
+ // Destination is not an HTTP response, GET and HEAD method are not allowed
+ return
+ }
+
+ if (self.src.method !== 'HEAD') {
+ fs.createReadStream(self.path).pipe(self.dest)
+ }
+ return
+ }
+
+ if (self.src && (self.src.method === 'PUT' || self.src.method === 'POST')) {
+ if (!err) {
+ // TODO handle overwrite case
+ return
+ }
+ stream.Stream.prototype.pipe.call(self, fs.createWriteStream(self.path))
+ if (self.dest && self.dest.writeHead) {
+ self.on('end', function () {
+ self.dest.statusCode = 201
+ self.dest.setHeader('content-length', 0)
+ self.dest.end()
+ })
+ }
+ return
+ }
+
+ // Desination is an HTTP response, we already handled 404 and 500
+ if (self.dest && self.dest.writeHead) {
+ self.dest.statusCode = 200
+ self.dest.setHeader('content-type', self.mimetype)
+ self.dest.setHeader('etag', self.etag)
+ self.dest.setHeader('last-modified', self.lastmodified)
+ self.dest.setHeader('content-length', stats.size)
+ fs.createReadStream(self.path).pipe(self.dest)
+ return
+ }
+
+ // Destination is not an HTTP request
+
+ if (self.src && !self.dest) {
+ stream.Stream.prototype.pipe.call(self, fs.createWriteStream(self.path))
+ } else if (self.dest && !self.src) {
+ fs.createReadStream(self.path).pipe(self.dest)
+ }
+ }
+
+ if (!err && stats.isDirectory()) {
+ self.path = path.join(self.path, self.index)
+ self.mimetype = mimetypes.lookup(self.path.slice(self.path.lastIndexOf('.')+1))
+ fs.stat(self.path, finish)
+ return
+ } else {
+ finish(err, stats)
+ }
+
+ if (!self.src && !self.dest) {
+ if (self.buffers.length > 0) {
+ stream.Stream.prototype.pipe.call(self, fs.createWriteStream(self.path))
+ } else if (self.listeners('data').length > 0) {
+ fs.createReadStream(self.path).pipe(self.dest)
+ } else {
+ fs.createReadStream(self.path).pipe(self)
+ }
+ }
+
+ })
+
+}
+util.inherits(File, stream.Stream)
+File.prototype.pipe = function (dest, options) {
+ this.dest = dest
+ this.destOptions = options
+ dest.emit('pipe', this)
+ // stream.Stream.prototype.pipe.call(this, dest, options)
+}
+File.prototype.write = function (chunk, encoding) {
+ if (encoding) chunk = chunk.toString(encoding)
+ if (this.buffering) {
+ this.buffers.push(chunk)
+ } else {
+ this.emit('data', chunk)
+ }
+}
+File.prototype.end = function (chunk) {
+ if (chunk) this.write(chunk)
+ if (this.buffering) {
+ this.ended = true
+ } else {
+ this.emit('end')
+ }
+}
+
+module.exports = function (options) {
+ return new File(options)
+}
+module.exports.File = File
View
147 node_modules/filed/mimetypes.js
@@ -0,0 +1,147 @@
+// from http://github.com/felixge/node-paperboy
+exports.types = {
+ "aiff":"audio/x-aiff",
+ "arj":"application/x-arj-compressed",
+ "asf":"video/x-ms-asf",
+ "asx":"video/x-ms-asx",
+ "au":"audio/ulaw",
+ "avi":"video/x-msvideo",
+ "bcpio":"application/x-bcpio",
+ "ccad":"application/clariscad",
+ "cod":"application/vnd.rim.cod",
+ "com":"application/x-msdos-program",
+ "cpio":"application/x-cpio",
+ "cpt":"application/mac-compactpro",
+ "csh":"application/x-csh",
+ "css":"text/css",
+ "deb":"application/x-debian-package",
+ "dl":"video/dl",
+ "doc":"application/msword",
+ "drw":"application/drafting",
+ "dvi":"application/x-dvi",
+ "dwg":"application/acad",
+ "dxf":"application/dxf",
+ "dxr":"application/x-director",
+ "etx":"text/x-setext",
+ "ez":"application/andrew-inset",
+ "fli":"video/x-fli",
+ "flv":"video/x-flv",
+ "gif":"image/gif",
+ "gl":"video/gl",
+ "gtar":"application/x-gtar",
+ "gz":"application/x-gzip",
+ "hdf":"application/x-hdf",
+ "hqx":"application/mac-binhex40",
+ "html":"text/html",
+ "ice":"x-conference/x-cooltalk",
+ "ico":"image/x-icon",
+ "ief":"image/ief",
+ "igs":"model/iges",
+ "ips":"application/x-ipscript",
+ "ipx":"application/x-ipix",
+ "jad":"text/vnd.sun.j2me.app-descriptor",
+ "jar":"application/java-archive",
+ "jpeg":"image/jpeg",
+ "jpg":"image/jpeg",
+ "js":"text/javascript",
+ "json":"application/json",
+ "latex":"application/x-latex",
+ "lsp":"application/x-lisp",
+ "lzh":"application/octet-stream",
+ "m":"text/plain",
+ "m3u":"audio/x-mpegurl",
+ "man":"application/x-troff-man",
+ "me":"application/x-troff-me",
+ "midi":"audio/midi",
+ "mif":"application/x-mif",
+ "mime":"www/mime",
+ "movie":"video/x-sgi-movie",
+ "mustache":"text/plain",
+ "mp4":"video/mp4",
+ "mpg":"video/mpeg",
+ "mpga":"audio/mpeg",
+ "ms":"application/x-troff-ms",
+ "nc":"application/x-netcdf",
+ "oda":"application/oda",
+ "ogm":"application/ogg",
+ "pbm":"image/x-portable-bitmap",
+ "pdf":"application/pdf",
+ "pgm":"image/x-portable-graymap",
+ "pgn":"application/x-chess-pgn",
+ "pgp":"application/pgp",
+ "pm":"application/x-perl",
+ "png":"image/png",
+ "pnm":"image/x-portable-anymap",
+ "ppm":"image/x-portable-pixmap",
+ "ppz":"application/vnd.ms-powerpoint",
+ "pre":"application/x-freelance",
+ "prt":"application/pro_eng",
+ "ps":"application/postscript",
+ "qt":"video/quicktime",
+ "ra":"audio/x-realaudio",
+ "rar":"application/x-rar-compressed",
+ "ras":"image/x-cmu-raster",
+ "rgb":"image/x-rgb",
+ "rm":"audio/x-pn-realaudio",
+ "rpm":"audio/x-pn-realaudio-plugin",
+ "rtf":"text/rtf",
+ "rtx":"text/richtext",
+ "scm":"application/x-lotusscreencam",
+ "set":"application/set",
+ "sgml":"text/sgml",
+ "sh":"application/x-sh",
+ "shar":"application/x-shar",
+ "silo":"model/mesh",
+ "sit":"application/x-stuffit",
+ "skt":"application/x-koan",
+ "smil":"application/smil",
+ "snd":"audio/basic",
+ "sol":"application/solids",
+ "spl":"application/x-futuresplash",
+ "src":"application/x-wais-source",
+ "stl":"application/SLA",
+ "stp":"application/STEP",
+ "sv4cpio":"application/x-sv4cpio",
+ "sv4crc":"application/x-sv4crc",
+ "svg":"image/svg+xml",
+ "swf":"application/x-shockwave-flash",
+ "tar":"application/x-tar",
+ "tcl":"application/x-tcl",
+ "tex":"application/x-tex",
+ "texinfo":"application/x-texinfo",
+ "tgz":"application/x-tar-gz",
+ "tiff":"image/tiff",
+ "tr":"application/x-troff",
+ "tsi":"audio/TSP-audio",
+ "tsp":"application/dsptype",
+ "tsv":"text/tab-separated-values",
+ "txt":"text/plain",
+ "unv":"application/i-deas",
+ "ustar":"application/x-ustar",
+ "vcd":"application/x-cdlink",
+ "vda":"application/vda",
+ "vivo":"video/vnd.vivo",
+ "vrm":"x-world/x-vrml",
+ "wav":"audio/x-wav",
+ "wax":"audio/x-ms-wax",
+ "wma":"audio/x-ms-wma",
+ "wmv":"video/x-ms-wmv",
+ "wmx":"video/x-ms-wmx",
+ "wrl":"model/vrml",
+ "wvx":"video/x-ms-wvx",
+ "xbm":"image/x-xbitmap",
+ "xlw":"application/vnd.ms-excel",
+ "xml":"text/xml",
+ "xpm":"image/x-xpixmap",
+ "xwd":"image/x-xwindowdump",
+ "xyz":"chemical/x-pdb",
+ "zip":"application/zip",
+};
+
+exports.lookup = function(ext, defaultType) {
+ defaultType = defaultType || 'application/octet-stream';
+
+ return (ext in exports.types)
+ ? exports.types[ext]
+ : defaultType;
+};
View
26 node_modules/filed/package.json
@@ -0,0 +1,26 @@
+{
+ "name": "filed",
+ "description": "Simplified file library.",
+ "version": "0.0.7",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/mikeal/filed.git"
+ },
+ "author": {
+ "name": "Mikeal Rogers",
+ "email": "mikeal.rogers@gmail.com"
+ },
+ "engines": {
+ "node": "*"
+ },
+ "scripts": {
+ "test": "node test/test.js"
+ },
+ "main": "./main",
+ "readme": "# filed -- Simplified file library.\n\n## Install\n\n<pre>\n npm install filed\n</pre>\n\n## Super simple to use\n\nFiled does a lazy stat call so you can actually open a file and begin writing to it and if the file isn't there it will just be created.\n\n```javascript\nvar filed = require('filed');\nvar f = filed('/newfile')\nf.write('test')\nf.end()\n```\n\n## Streaming\n\nThe returned file object is a stream so you can do standard stream stuff to it. Based on *what* you do the object it will be a read stream, a write stream.\n\nSo if you send data to it, it'll be a write stream.\n\n```javascript\nfs.createReadStream.pipe(filed('/newfile'))\n```\n\nIf you pipe it to a destination it'll be a read stream.\n\n```javascript\nfiled('/myfile').pipe(fs.createWriteStream('/out'))\n```\n\nAnd of course you can pipe a filed object from itself to itself and it'll figure it out.\n\n```javascript\nfiled('/myfile').pipe(filed('/newfile'))\n```\n\nThose familiar with [request](http://github.com/mikeal/request) will be familiar seeing object capability detection when doing HTTP. filed does this as well.\n\n```javascript\nhttp.createServer(function (req, resp) {\n filed('/data.json').pipe(resp)\n})\n```\n\nNot only does the JSON file get streamed to the HTTP Response it will include an Etag, Last-Modified, Content-Length, and a Content-Type header based on the filed extension.\n\n```javascript\nhttp.createServer(function (req, resp) {\n req.pipe(filed('/newfile')).pipe(resp)\n})\n```\n\nWhen accepting a PUT request data will be streamed to the file and a 201 status will be sent on the HTTP Response when the upload is finished.\n\nDuring a GET request a 404 Response will be sent if the file does not exist.\n\n```javascript\nhttp.createServer(function (req, resp) {\n req.pipe(filed('/data.json')).pipe(resp)\n})\n```\n\nThe Etag and Last-Modified headers filed creates are based solely on the stat() call so if you pipe a request to an existing file the cache control headers will be taken into account; a 304 response will be sent if the cache control headers match a new stat() call. This can be very helpful in avoiding unnecessary disc reads.\n\n```javascript\nhttp.createServer(function (req, resp) {\n req.pipe(filed('/directory')).pipe(resp)\n})\n```\n\nJust to round out the full feature set and make it full file server if you give filed an existing directory it will actually check for an index.html file in that directory and serve it if it exists.\n",
+ "_id": "filed@0.0.7",
+ "dist": {
+ "shasum": "ad6a591dc9553d16014bea6f1332888e970f8976"
+ },
+ "_from": "filed"
+}
View
47 node_modules/filed/rfc822.js
@@ -0,0 +1,47 @@
+// Support for rfc822, worst standard EVAR!
+
+// require('./date')
+
+function getRFC822Date(oDate)
+{
+ var aMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
+
+ var aDays = new Array( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
+ var dtm = new String();
+
+ dtm = aDays[oDate.getDay()] + ", ";
+ dtm += padWithZero(oDate.getDate()) + " ";
+ dtm += aMonths[oDate.getMonth()] + " ";
+ dtm += oDate.getFullYear() + " ";
+ dtm += padWithZero(oDate.getHours()) + ":";
+ dtm += padWithZero(oDate.getMinutes()) + ":";
+ dtm += padWithZero(oDate.getSeconds()) + " " ;
+ dtm += getTZOString(oDate.getTimezoneOffset());
+ return dtm;
+}
+//Pads numbers with a preceding 0 if the number is less than 10.
+function padWithZero(val)
+{
+ if (parseInt(val) < 10)
+ {
+ return "0" + val;
+ }
+ return val;
+}
+
+/* accepts the client's time zone offset from GMT in minutes as a parameter.
+returns the timezone offset in the format [+|-}DDDD */
+function getTZOString(timezoneOffset)
+{
+ var hours = Math.floor(timezoneOffset/60);
+ var modMin = Math.abs(timezoneOffset%60);
+ var s = new String();
+ s += (hours > 0) ? "-" : "+";
+ var absHours = Math.abs(hours)
+ s += (absHours < 10) ? "0" + absHours :absHours;
+ s += ((modMin == 0) ? "00" : modMin);
+ return(s);
+}
+
+exports.getRFC822Date = getRFC822Date;
View
1  node_modules/filed/test/index.html
@@ -0,0 +1 @@
+test
View
20 node_modules/filed/test/server.js
@@ -0,0 +1,20 @@
+var http = require('http')
+
+function createServer () {
+ var s = http.createServer(function (req, resp) {
+ resp.___end = resp.end
+ resp.end = function (chunk) {
+ s.completed[req.url] = true
+ resp.___end(chunk)
+ if (Object.keys(s._events).filter(function(n){return n[0] === '/'}).length === Object.keys(s.completed).length) {
+ setTimeout(function () {
+ s.close()
+ }, 0)
+ }
+ }
+ s.emit(req.url, req, resp)
+ })
+ s.completed = {}
+ return s;
+}
+module.exports = createServer;
View
209 node_modules/filed/test/test.js
@@ -0,0 +1,209 @@
+var filed = require('../main')
+ , server = require('./server')
+ , fs = require('fs')
+ , path = require('path')
+ , assert = require('assert')
+ , request = require('request')
+ , test1buffer = ''
+ , testfile = path.join(__dirname, 'test.js')
+ , writefile = path.join(__dirname, 'testdump-')
+ , cleanup = []
+ , validations = []
+ , port = 9090
+ , i = 0
+ , url = 'http://localhost:'+port
+ ;
+
+while (i < 50) {
+ try {fs.unlinkSync(writefile+i)}
+ catch (e) {}
+ i += 1
+}
+
+function FileValidator (path) {
+ this.path = path
+ this.buffers = []
+ this.len = 0
+ this.writable = true
+}
+FileValidator.prototype.write = function (chunk) {
+ this.buffers.push(chunk)
+ this.len += chunk.length
+}
+FileValidator.prototype.end = function () {
+ var body = new Buffer(this.len)
+ var i = 0
+ this.buffers.forEach(function (chunk) {
+ chunk.copy(body, i, 0, chunk.length)
+ i += chunk.length
+ })
+ var f = fs.readFileSync(this.path)
+ assert.equal(body.length, f.length)
+ assert.deepEqual(body, f)
+}
+FileValidator.prototype.on = function () {}
+FileValidator.prototype.removeListener = function () {}
+FileValidator.prototype.emit = function () {}
+
+function equalSync (f1, f2) {
+ f1 = fs.readFileSync(f1)
+ f2 = fs.readFileSync(f2)
+ assert.equal(f1.length, f2.length)
+ assert.deepEqual(f1, f2)
+}
+
+// Test reading
+filed(testfile).pipe(new FileValidator(testfile))
+
+// Test writing
+
+function testwrites () {
+ var x = filed(writefile+1)
+ , y = fs.createReadStream(testfile)
+ ;
+ y.pipe(x)
+ x.on('end', function () {
+ setTimeout(function () {
+ equalSync(writefile+1, testfile)
+ console.log("Passed writing files")
+ }, 1000)
+ })
+}
+testwrites()
+
+function testhttp () {
+ // Test HTTP use cases
+ var s = server()
+ s.on('/test-req', function (req, resp) {
+ // Take a request and write it, do not send filed to the response
+ req.pipe(filed(writefile+2))
+ req.on('end', function () {
+ resp.writeHead(201)
+ resp.end()
+ setTimeout(function () {
+ equalSync(writefile+2, testfile)
+ console.log("Passed PUT file with pipe req only")
+ }, 1000)
+ })
+ })
+
+ s.on('/test-req-resp', function (req, resp) {
+ // Take a request and write it and pipe filed to the response
+ var x = filed(writefile+3)
+ req.pipe(x)
+ x.pipe(resp)
+ req.on('end', function () {
+ setTimeout(function () {
+ equalSync(writefile+3, testfile)
+ console.log("Passed PUT file with pipe req and resp")
+ }, 1000)
+ })
+ })
+
+ s.on('/test-resp', function (req, resp) {
+ // Send a file to an HTTP response
+ filed(testfile).pipe(resp)
+ })
+
+ var fullpipe = function (req, resp) {
+ var x = filed(testfile)
+ req.pipe(x)
+ x.pipe(resp)
+ }
+
+ s.on('/test-etags-wo', function (req, resp) {
+ fullpipe(req, resp)
+ })
+ s.on('/test-etags-with', function (req, resp) {
+ fullpipe(req, resp)
+ })
+
+ s.on('/test-lastmodified-wo', function (req, resp) {
+ fullpipe(req, resp)
+ })
+ s.on('/test-lastmodified-with', function (req, resp) {
+ fullpipe(req, resp)
+ })
+
+ s.on('/test-index', function (req, resp) {
+ var x = filed(__dirname)
+ x.pipe(resp)
+ })
+
+ s.on('/test-index-full', function (req, resp) {
+ var x = filed(__dirname)
+ req.pipe(x)
+ x.pipe(resp)
+ })
+
+ s.on('/test-not-found', function (req, resp) {
+ var x = filed(__dirname + "/there-is-no-such-file-here.no-extension")
+ req.pipe(x)
+ x.pipe(resp)
+ })
+
+ s.listen(port, function () {
+
+ fs.createReadStream(testfile).pipe(request.put(url+'/test-req'))
+
+ fs.createReadStream(testfile).pipe(request.put(url+'/test-req-resp', function (e, resp) {
+ assert.equal(resp.statusCode, 201)
+ assert.equal(resp.headers['content-length'], '0')
+ }))
+
+ var x = request.get(url+'/test-resp', function (e, resp) {
+ if (e) throw e
+ assert.equal(resp.statusCode, 200)
+ assert.equal(resp.headers['content-type'], 'text/javascript')
+ console.log("Passed GET file without piping request")
+ })
+ x.pipe(new FileValidator(testfile))
+
+ request.get(url+'/test-etags-wo', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ request.get({url:url+'/test-etags-with', headers:{'if-none-match':resp.headers.etag}}, function (e, resp) {
+ if (e) throw e
+ if (resp.statusCode !== 304) throw new Error('Status code is not 304 it is '+resp.statusCode)
+ console.log("Passed GET with etag")
+ })
+ })
+
+ request.get(url+'/test-lastmodified-wo', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ request.get({url:url+'/test-lastmodified-with', headers:{'if-modified-since':resp.headers['last-modified']}}, function (e, resp) {
+ if (e) throw e
+ if (resp.statusCode !== 304) throw new Error('Status code is not 304 it is '+resp.statusCode)
+ console.log("Passed GET with if-modified-since")
+ })
+ })
+
+ request.get(url+'/test-index', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ assert.equal(resp.headers['content-type'], 'text/html')
+ assert.equal(body, fs.readFileSync(path.join(__dirname, 'index.html')).toString())
+ console.log("Passed GET of directory index")
+ })
+
+ request.get(url+'/test-index-full', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ assert.equal(resp.headers['content-type'], 'text/html')
+ assert.equal(body, fs.readFileSync(path.join(__dirname, 'index.html')).toString())
+ console.log("Passed GET of directory index, full pipe")
+ })
+
+ request.get(url+'/test-not-found', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 404) throw new Error('Status code is not 404 it is '+resp.statusCode)
+ console.log("Passed Not Found produces 404")
+ })
+
+ })
+
+}
+testhttp()
+
+process.on('exit', function () {console.log('All tests passed.')})
View
209 node_modules/filed/test/testdump-1
@@ -0,0 +1,209 @@
+var filed = require('../main')
+ , server = require('./server')
+ , fs = require('fs')
+ , path = require('path')
+ , assert = require('assert')
+ , request = require('request')
+ , test1buffer = ''
+ , testfile = path.join(__dirname, 'test.js')
+ , writefile = path.join(__dirname, 'testdump-')
+ , cleanup = []
+ , validations = []
+ , port = 9090
+ , i = 0
+ , url = 'http://localhost:'+port
+ ;
+
+while (i < 50) {
+ try {fs.unlinkSync(writefile+i)}
+ catch (e) {}
+ i += 1
+}
+
+function FileValidator (path) {
+ this.path = path
+ this.buffers = []
+ this.len = 0
+ this.writable = true
+}
+FileValidator.prototype.write = function (chunk) {
+ this.buffers.push(chunk)
+ this.len += chunk.length
+}
+FileValidator.prototype.end = function () {
+ var body = new Buffer(this.len)
+ var i = 0
+ this.buffers.forEach(function (chunk) {
+ chunk.copy(body, i, 0, chunk.length)
+ i += chunk.length
+ })
+ var f = fs.readFileSync(this.path)
+ assert.equal(body.length, f.length)
+ assert.deepEqual(body, f)
+}
+FileValidator.prototype.on = function () {}
+FileValidator.prototype.removeListener = function () {}
+FileValidator.prototype.emit = function () {}
+
+function equalSync (f1, f2) {
+ f1 = fs.readFileSync(f1)
+ f2 = fs.readFileSync(f2)
+ assert.equal(f1.length, f2.length)
+ assert.deepEqual(f1, f2)
+}
+
+// Test reading
+filed(testfile).pipe(new FileValidator(testfile))
+
+// Test writing
+
+function testwrites () {
+ var x = filed(writefile+1)
+ , y = fs.createReadStream(testfile)
+ ;
+ y.pipe(x)
+ x.on('end', function () {
+ setTimeout(function () {
+ equalSync(writefile+1, testfile)
+ console.log("Passed writing files")
+ }, 1000)
+ })
+}
+testwrites()
+
+function testhttp () {
+ // Test HTTP use cases
+ var s = server()
+ s.on('/test-req', function (req, resp) {
+ // Take a request and write it, do not send filed to the response
+ req.pipe(filed(writefile+2))
+ req.on('end', function () {
+ resp.writeHead(201)
+ resp.end()
+ setTimeout(function () {
+ equalSync(writefile+2, testfile)
+ console.log("Passed PUT file with pipe req only")
+ }, 1000)
+ })
+ })
+
+ s.on('/test-req-resp', function (req, resp) {
+ // Take a request and write it and pipe filed to the response
+ var x = filed(writefile+3)
+ req.pipe(x)
+ x.pipe(resp)
+ req.on('end', function () {
+ setTimeout(function () {
+ equalSync(writefile+3, testfile)
+ console.log("Passed PUT file with pipe req and resp")
+ }, 1000)
+ })
+ })
+
+ s.on('/test-resp', function (req, resp) {
+ // Send a file to an HTTP response
+ filed(testfile).pipe(resp)
+ })
+
+ var fullpipe = function (req, resp) {
+ var x = filed(testfile)
+ req.pipe(x)
+ x.pipe(resp)
+ }
+
+ s.on('/test-etags-wo', function (req, resp) {
+ fullpipe(req, resp)
+ })
+ s.on('/test-etags-with', function (req, resp) {
+ fullpipe(req, resp)
+ })
+
+ s.on('/test-lastmodified-wo', function (req, resp) {
+ fullpipe(req, resp)
+ })
+ s.on('/test-lastmodified-with', function (req, resp) {
+ fullpipe(req, resp)
+ })
+
+ s.on('/test-index', function (req, resp) {
+ var x = filed(__dirname)
+ x.pipe(resp)
+ })
+
+ s.on('/test-index-full', function (req, resp) {
+ var x = filed(__dirname)
+ req.pipe(x)
+ x.pipe(resp)
+ })
+
+ s.on('/test-not-found', function (req, resp) {
+ var x = filed(__dirname + "/there-is-no-such-file-here.no-extension")
+ req.pipe(x)
+ x.pipe(resp)
+ })
+
+ s.listen(port, function () {
+
+ fs.createReadStream(testfile).pipe(request.put(url+'/test-req'))
+
+ fs.createReadStream(testfile).pipe(request.put(url+'/test-req-resp', function (e, resp) {
+ assert.equal(resp.statusCode, 201)
+ assert.equal(resp.headers['content-length'], '0')
+ }))
+
+ var x = request.get(url+'/test-resp', function (e, resp) {
+ if (e) throw e
+ assert.equal(resp.statusCode, 200)
+ assert.equal(resp.headers['content-type'], 'text/javascript')
+ console.log("Passed GET file without piping request")
+ })
+ x.pipe(new FileValidator(testfile))
+
+ request.get(url+'/test-etags-wo', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ request.get({url:url+'/test-etags-with', headers:{'if-none-match':resp.headers.etag}}, function (e, resp) {
+ if (e) throw e
+ if (resp.statusCode !== 304) throw new Error('Status code is not 304 it is '+resp.statusCode)
+ console.log("Passed GET with etag")
+ })
+ })
+
+ request.get(url+'/test-lastmodified-wo', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ request.get({url:url+'/test-lastmodified-with', headers:{'if-modified-since':resp.headers['last-modified']}}, function (e, resp) {
+ if (e) throw e
+ if (resp.statusCode !== 304) throw new Error('Status code is not 304 it is '+resp.statusCode)
+ console.log("Passed GET with if-modified-since")
+ })
+ })
+
+ request.get(url+'/test-index', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ assert.equal(resp.headers['content-type'], 'text/html')
+ assert.equal(body, fs.readFileSync(path.join(__dirname, 'index.html')).toString())
+ console.log("Passed GET of directory index")
+ })
+
+ request.get(url+'/test-index-full', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ assert.equal(resp.headers['content-type'], 'text/html')
+ assert.equal(body, fs.readFileSync(path.join(__dirname, 'index.html')).toString())
+ console.log("Passed GET of directory index, full pipe")
+ })
+
+ request.get(url+'/test-not-found', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 404) throw new Error('Status code is not 404 it is '+resp.statusCode)
+ console.log("Passed Not Found produces 404")
+ })
+
+ })
+
+}
+testhttp()
+
+process.on('exit', function () {console.log('All tests passed.')})
View
209 node_modules/filed/test/testdump-2
@@ -0,0 +1,209 @@
+var filed = require('../main')
+ , server = require('./server')
+ , fs = require('fs')
+ , path = require('path')
+ , assert = require('assert')
+ , request = require('request')
+ , test1buffer = ''
+ , testfile = path.join(__dirname, 'test.js')
+ , writefile = path.join(__dirname, 'testdump-')
+ , cleanup = []
+ , validations = []
+ , port = 9090
+ , i = 0
+ , url = 'http://localhost:'+port
+ ;
+
+while (i < 50) {
+ try {fs.unlinkSync(writefile+i)}
+ catch (e) {}
+ i += 1
+}
+
+function FileValidator (path) {
+ this.path = path
+ this.buffers = []
+ this.len = 0
+ this.writable = true
+}
+FileValidator.prototype.write = function (chunk) {
+ this.buffers.push(chunk)
+ this.len += chunk.length
+}
+FileValidator.prototype.end = function () {
+ var body = new Buffer(this.len)
+ var i = 0
+ this.buffers.forEach(function (chunk) {
+ chunk.copy(body, i, 0, chunk.length)
+ i += chunk.length
+ })
+ var f = fs.readFileSync(this.path)
+ assert.equal(body.length, f.length)
+ assert.deepEqual(body, f)
+}
+FileValidator.prototype.on = function () {}
+FileValidator.prototype.removeListener = function () {}
+FileValidator.prototype.emit = function () {}
+
+function equalSync (f1, f2) {
+ f1 = fs.readFileSync(f1)
+ f2 = fs.readFileSync(f2)
+ assert.equal(f1.length, f2.length)
+ assert.deepEqual(f1, f2)
+}
+
+// Test reading
+filed(testfile).pipe(new FileValidator(testfile))
+
+// Test writing
+
+function testwrites () {
+ var x = filed(writefile+1)
+ , y = fs.createReadStream(testfile)
+ ;
+ y.pipe(x)
+ x.on('end', function () {
+ setTimeout(function () {
+ equalSync(writefile+1, testfile)
+ console.log("Passed writing files")
+ }, 1000)
+ })
+}
+testwrites()
+
+function testhttp () {
+ // Test HTTP use cases
+ var s = server()
+ s.on('/test-req', function (req, resp) {
+ // Take a request and write it, do not send filed to the response
+ req.pipe(filed(writefile+2))
+ req.on('end', function () {
+ resp.writeHead(201)
+ resp.end()
+ setTimeout(function () {
+ equalSync(writefile+2, testfile)
+ console.log("Passed PUT file with pipe req only")
+ }, 1000)
+ })
+ })
+
+ s.on('/test-req-resp', function (req, resp) {
+ // Take a request and write it and pipe filed to the response
+ var x = filed(writefile+3)
+ req.pipe(x)
+ x.pipe(resp)
+ req.on('end', function () {
+ setTimeout(function () {
+ equalSync(writefile+3, testfile)
+ console.log("Passed PUT file with pipe req and resp")
+ }, 1000)
+ })
+ })
+
+ s.on('/test-resp', function (req, resp) {
+ // Send a file to an HTTP response
+ filed(testfile).pipe(resp)
+ })
+
+ var fullpipe = function (req, resp) {
+ var x = filed(testfile)
+ req.pipe(x)
+ x.pipe(resp)
+ }
+
+ s.on('/test-etags-wo', function (req, resp) {
+ fullpipe(req, resp)
+ })
+ s.on('/test-etags-with', function (req, resp) {
+ fullpipe(req, resp)
+ })
+
+ s.on('/test-lastmodified-wo', function (req, resp) {
+ fullpipe(req, resp)
+ })
+ s.on('/test-lastmodified-with', function (req, resp) {
+ fullpipe(req, resp)
+ })
+
+ s.on('/test-index', function (req, resp) {
+ var x = filed(__dirname)
+ x.pipe(resp)
+ })
+
+ s.on('/test-index-full', function (req, resp) {
+ var x = filed(__dirname)
+ req.pipe(x)
+ x.pipe(resp)
+ })
+
+ s.on('/test-not-found', function (req, resp) {
+ var x = filed(__dirname + "/there-is-no-such-file-here.no-extension")
+ req.pipe(x)
+ x.pipe(resp)
+ })
+
+ s.listen(port, function () {
+
+ fs.createReadStream(testfile).pipe(request.put(url+'/test-req'))
+
+ fs.createReadStream(testfile).pipe(request.put(url+'/test-req-resp', function (e, resp) {
+ assert.equal(resp.statusCode, 201)
+ assert.equal(resp.headers['content-length'], '0')
+ }))
+
+ var x = request.get(url+'/test-resp', function (e, resp) {
+ if (e) throw e
+ assert.equal(resp.statusCode, 200)
+ assert.equal(resp.headers['content-type'], 'text/javascript')
+ console.log("Passed GET file without piping request")
+ })
+ x.pipe(new FileValidator(testfile))
+
+ request.get(url+'/test-etags-wo', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ request.get({url:url+'/test-etags-with', headers:{'if-none-match':resp.headers.etag}}, function (e, resp) {
+ if (e) throw e
+ if (resp.statusCode !== 304) throw new Error('Status code is not 304 it is '+resp.statusCode)
+ console.log("Passed GET with etag")
+ })
+ })
+
+ request.get(url+'/test-lastmodified-wo', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ request.get({url:url+'/test-lastmodified-with', headers:{'if-modified-since':resp.headers['last-modified']}}, function (e, resp) {
+ if (e) throw e
+ if (resp.statusCode !== 304) throw new Error('Status code is not 304 it is '+resp.statusCode)
+ console.log("Passed GET with if-modified-since")
+ })
+ })
+
+ request.get(url+'/test-index', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ assert.equal(resp.headers['content-type'], 'text/html')
+ assert.equal(body, fs.readFileSync(path.join(__dirname, 'index.html')).toString())
+ console.log("Passed GET of directory index")
+ })
+
+ request.get(url+'/test-index-full', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ assert.equal(resp.headers['content-type'], 'text/html')
+ assert.equal(body, fs.readFileSync(path.join(__dirname, 'index.html')).toString())
+ console.log("Passed GET of directory index, full pipe")
+ })
+
+ request.get(url+'/test-not-found', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 404) throw new Error('Status code is not 404 it is '+resp.statusCode)
+ console.log("Passed Not Found produces 404")
+ })
+
+ })
+
+}
+testhttp()
+
+process.on('exit', function () {console.log('All tests passed.')})
View
209 node_modules/filed/test/testdump-3
@@ -0,0 +1,209 @@
+var filed = require('../main')
+ , server = require('./server')
+ , fs = require('fs')
+ , path = require('path')
+ , assert = require('assert')
+ , request = require('request')
+ , test1buffer = ''
+ , testfile = path.join(__dirname, 'test.js')
+ , writefile = path.join(__dirname, 'testdump-')
+ , cleanup = []
+ , validations = []
+ , port = 9090
+ , i = 0
+ , url = 'http://localhost:'+port
+ ;
+
+while (i < 50) {
+ try {fs.unlinkSync(writefile+i)}
+ catch (e) {}
+ i += 1
+}
+
+function FileValidator (path) {
+ this.path = path
+ this.buffers = []
+ this.len = 0
+ this.writable = true
+}
+FileValidator.prototype.write = function (chunk) {
+ this.buffers.push(chunk)
+ this.len += chunk.length
+}
+FileValidator.prototype.end = function () {
+ var body = new Buffer(this.len)
+ var i = 0
+ this.buffers.forEach(function (chunk) {
+ chunk.copy(body, i, 0, chunk.length)
+ i += chunk.length
+ })
+ var f = fs.readFileSync(this.path)
+ assert.equal(body.length, f.length)
+ assert.deepEqual(body, f)
+}
+FileValidator.prototype.on = function () {}
+FileValidator.prototype.removeListener = function () {}
+FileValidator.prototype.emit = function () {}
+
+function equalSync (f1, f2) {
+ f1 = fs.readFileSync(f1)
+ f2 = fs.readFileSync(f2)
+ assert.equal(f1.length, f2.length)
+ assert.deepEqual(f1, f2)
+}
+
+// Test reading
+filed(testfile).pipe(new FileValidator(testfile))
+
+// Test writing
+
+function testwrites () {
+ var x = filed(writefile+1)
+ , y = fs.createReadStream(testfile)
+ ;
+ y.pipe(x)
+ x.on('end', function () {
+ setTimeout(function () {
+ equalSync(writefile+1, testfile)
+ console.log("Passed writing files")
+ }, 1000)
+ })
+}
+testwrites()
+
+function testhttp () {
+ // Test HTTP use cases
+ var s = server()
+ s.on('/test-req', function (req, resp) {
+ // Take a request and write it, do not send filed to the response
+ req.pipe(filed(writefile+2))
+ req.on('end', function () {
+ resp.writeHead(201)
+ resp.end()
+ setTimeout(function () {
+ equalSync(writefile+2, testfile)
+ console.log("Passed PUT file with pipe req only")
+ }, 1000)
+ })
+ })
+
+ s.on('/test-req-resp', function (req, resp) {
+ // Take a request and write it and pipe filed to the response
+ var x = filed(writefile+3)
+ req.pipe(x)
+ x.pipe(resp)
+ req.on('end', function () {
+ setTimeout(function () {
+ equalSync(writefile+3, testfile)
+ console.log("Passed PUT file with pipe req and resp")
+ }, 1000)
+ })
+ })
+
+ s.on('/test-resp', function (req, resp) {
+ // Send a file to an HTTP response
+ filed(testfile).pipe(resp)
+ })
+
+ var fullpipe = function (req, resp) {
+ var x = filed(testfile)
+ req.pipe(x)
+ x.pipe(resp)
+ }
+
+ s.on('/test-etags-wo', function (req, resp) {
+ fullpipe(req, resp)
+ })
+ s.on('/test-etags-with', function (req, resp) {
+ fullpipe(req, resp)
+ })
+
+ s.on('/test-lastmodified-wo', function (req, resp) {
+ fullpipe(req, resp)
+ })
+ s.on('/test-lastmodified-with', function (req, resp) {
+ fullpipe(req, resp)
+ })
+
+ s.on('/test-index', function (req, resp) {
+ var x = filed(__dirname)
+ x.pipe(resp)
+ })
+
+ s.on('/test-index-full', function (req, resp) {
+ var x = filed(__dirname)
+ req.pipe(x)
+ x.pipe(resp)
+ })
+
+ s.on('/test-not-found', function (req, resp) {
+ var x = filed(__dirname + "/there-is-no-such-file-here.no-extension")
+ req.pipe(x)
+ x.pipe(resp)
+ })
+
+ s.listen(port, function () {
+
+ fs.createReadStream(testfile).pipe(request.put(url+'/test-req'))
+
+ fs.createReadStream(testfile).pipe(request.put(url+'/test-req-resp', function (e, resp) {
+ assert.equal(resp.statusCode, 201)
+ assert.equal(resp.headers['content-length'], '0')
+ }))
+
+ var x = request.get(url+'/test-resp', function (e, resp) {
+ if (e) throw e
+ assert.equal(resp.statusCode, 200)
+ assert.equal(resp.headers['content-type'], 'text/javascript')
+ console.log("Passed GET file without piping request")
+ })
+ x.pipe(new FileValidator(testfile))
+
+ request.get(url+'/test-etags-wo', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ request.get({url:url+'/test-etags-with', headers:{'if-none-match':resp.headers.etag}}, function (e, resp) {
+ if (e) throw e
+ if (resp.statusCode !== 304) throw new Error('Status code is not 304 it is '+resp.statusCode)
+ console.log("Passed GET with etag")
+ })
+ })
+
+ request.get(url+'/test-lastmodified-wo', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ request.get({url:url+'/test-lastmodified-with', headers:{'if-modified-since':resp.headers['last-modified']}}, function (e, resp) {
+ if (e) throw e
+ if (resp.statusCode !== 304) throw new Error('Status code is not 304 it is '+resp.statusCode)
+ console.log("Passed GET with if-modified-since")
+ })
+ })
+
+ request.get(url+'/test-index', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ assert.equal(resp.headers['content-type'], 'text/html')
+ assert.equal(body, fs.readFileSync(path.join(__dirname, 'index.html')).toString())
+ console.log("Passed GET of directory index")
+ })
+
+ request.get(url+'/test-index-full', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 200) throw new Error('Status code is not 200 it is '+resp.statusCode)
+ assert.equal(resp.headers['content-type'], 'text/html')
+ assert.equal(body, fs.readFileSync(path.join(__dirname, 'index.html')).toString())
+ console.log("Passed GET of directory index, full pipe")
+ })
+
+ request.get(url+'/test-not-found', function (e, resp, body) {
+ if (e) throw e
+ if (resp.statusCode !== 404) throw new Error('Status code is not 404 it is '+resp.statusCode)
+ console.log("Passed Not Found produces 404")
+ })
+
+ })
+
+}
+testhttp()
+
+process.on('exit', function () {console.log('All tests passed.')})
View
22 npm-debug.log
@@ -0,0 +1,22 @@
+0 info it worked if it ends with ok
+1 verbose cli [ 'c:\\Program Files\\nodejs\\node.exe',
+1 verbose cli 'c:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
+1 verbose cli 'version',
+1 verbose cli '0.1.0' ]
+2 info using npm@1.1.59
+3 info using node@v0.8.8
+4 error Error: Version not changed
+4 error at c:\Program Files\nodejs\node_modules\npm\lib\version.js:58:45
+4 error at fs.readFile (fs.js:176:14)
+4 error at fs.close (c:\Program Files\nodejs\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:92:5)
+4 error at Object.oncomplete (fs.js:297:15)
+5 error If you need help, you may report this log at:
+5 error <http://github.com/isaacs/npm/issues>
+5 error or email it to:
+5 error <npm-@googlegroups.com>
+6 error System Windows_NT 6.1.7601
+7 error command "c:\\Program Files\\nodejs\\node.exe" "c:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "version" "0.1.0"
+8 error cwd c:\Dev\Web\wup
+9 error node -v v0.8.8
+10 error npm -v 1.1.59
+11 verbose exit [ 1, true ]
View
9 package.json
@@ -1,6 +1,6 @@
{
"name": "wup",
- "description": "tiny webserver",
+ "description": "tiny webserver for fast tests",
"author": "Paul Vorbach <paul@vorb.de> (http://vorb.de/)",
"tags": [ "http", "web", "deamon", "server", "www" ],
"version": "0.0.0",
@@ -15,8 +15,13 @@
"wup": "./wup.js"
},
"engines": {
- "node": ">=0.4.0"
+ "node": ">= 0.4.0"
},
+ "dependencies": {
+ "filed": "~ 0.0.7"
+ },
+ "devDependencies": {},
+ "optionalDependencies": {},
"licenses": [
{
"type": "MIT",
View
21 wup.js
@@ -3,6 +3,8 @@
var http = require('http');
var fs = require('fs');
var path = require('path');
+var url = require('url');
+var filed = require('filed');
var root = process.cwd();
@@ -26,13 +28,19 @@ var server = http.createServer(function (req, resp) {
return resp.end();
}
- req.url = req.url.replace(/\/$/, '/index.html');
- var file = path.resolve(root, '.'+req.url);
+ var reqPath = url.parse(req.url).pathname.replace(/\/$/, '/index.html');
+ var file = path.resolve(root, '.'+reqPath);
var ext = path.extname(file);
- fs.readFile(file, function (err, buf) {
+ fs.stat(file, function (err, stats) {
if (err) {
- console.error(err);
+ console.error(err.message);
+ resp.writeHead(404);
+ return resp.end();
+ }
+
+ if (!stats.isFile()) {
+ console.info('file "'+file+'" doesn\'t exist');
resp.writeHead(404);
return resp.end();
}
@@ -43,9 +51,10 @@ var server = http.createServer(function (req, resp) {
if (contentTypes[ext])
resp.setHeader('Content-Type', contentTypes[ext]);
else
- resp.setHeader('Content-Type', 'text/plain');
+ resp.setHeader('Content-Type', 'application/octet-stream');
- resp.end(buf);
+ // pipe file over http
+ filed(file).pipe(resp);
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.