Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit.

  • Loading branch information...
commit e8300abc84cf16f11a7ce3a3b880fde4ad9901c4 0 parents
@mikeal authored
76 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 being writing to it and if the file isn't there it will just be created.
+
+```javascript
+var request = 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(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 in to 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.
182 main.js
@@ -0,0 +1,182 @@
+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 = 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) {
+ 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.end('Not Found')
+ } else {
+ self.dest.statusCode = 500
+ self.end(err.message)
+ }
+ return
+ }
+
+ // Source is an HTTP Server Request
+ if (self.src && (self.src.method === 'GET' || self.src.method === 'HEAD')) {
+
+ self.dest.setHeader('content-type', self.mimetype)
+ self.dest.setHeader('etag', self.etag)
+ self.dest.setHeader('last-modified', self.lastmodified)
+
+ if (self.dest && 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-modifified-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 if (self.dest && !self.dest.writeHead) {
+ // Destination is not an HTTP response, GET and HEAD method are not allowed
+ return
+ }
+ if (self.dest || 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)
+ 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)
+ } else {
+ finish(err, stats)
+ }
+
+ })
+
+}
+util.inherits(File, stream.Stream)
+File.prototype.pipe = function (dest, options) {
+ this.dest = dest
+ this.destOptions = options
+ // 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 () {
+ if (this.buffering) {
+ this.ended = true
+ } else {
+ this.emit('end')
+ }
+}
+
+module.exports = function (options) {
+ return new File(options)
+}
+module.exports.File = File
146 mimetypes.js
@@ -0,0 +1,146 @@
+// 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",
+ "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;
+};
14 package.json
@@ -0,0 +1,14 @@
+{
+ "name": "filed",
+ "description": "Simplified file library.",
+ "version": "0.0.1",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/mikeal/filed.git"
+ },
+ "author": "Mikeal Rogers <mikeal.rogers@gmail.com>",
+ "engines": {
+ "node": "*"
+ },
+ "main": "./main"
+}
47 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;
20 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;
197 test.js
@@ -0,0 +1,197 @@
+var filed = require('../filed')
+ , server = require('./test-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.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-modifified-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")
+ })
+
+ })
+
+}
+testhttp()
+
+process.on('exit', function () {console.log('All tests passed.')})
1  test/index.html
@@ -0,0 +1 @@
+test
20 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;
197 test/test.js
@@ -0,0 +1,197 @@
+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.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-modifified-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")
+ })
+
+ })
+
+}
+testhttp()
+
+process.on('exit', function () {console.log('All tests passed.')})
Please sign in to comment.
Something went wrong with that request. Please try again.