import Path from 'path' var Fs= core.System.IO.Fs import fs from 'fs' var StaticServe= core.VW.Http.StaticServe import Mime from 'mime-types' import Zlib from 'zlib' import Compressible from 'compressible' class Server{ constructor(){ this.$paths= [] } addPath(path){ this.$paths.push(path) } removePath(path){ var index= this.$paths.indexOf(path) this.$paths[index]= undefined } handle(args){ var url= args.request.url var path, pathO for(var i=0;i0 || endByte < stream.length-1, length var len=stream.length if(partial){ stream.position= startByte if(endByte>0){ stream.maxPosition= endByte+1 length= stream.maxPosition- stream.position } else{ length= len-stream.position stream.maxPosition= len } } else{ length= len } var gzip, deflate, encoding if(args.request.headers["accept-encoding"] && length>100){ // Si el tamaño del archivo es menor a 100 no vale la pena comprimir encoding=args.request.headers["accept-encoding"].toUpperCase() gzip= encoding.indexOf("GZIP")>=0 deflate= encoding.indexOf("DEFLATE")>=0 } var mtype= this.getMimeType(path) var charset= Mime.charsets.lookup(mtype) var head= { "last-modified": mtime2.toGMTString(), /*"content-length": length, */ "accept-range": "bytes", "accept-ranges": "bytes", "content-type": mtype + (charset?";Charset="+charset: ""), "cache-control": "max-age=1800" } head["ETag"]= mtime2.getTime().toString(16) if(partial){ head["content-range"]= "bytes " + stream.position.toString() + "-" + ((stream.maxPosition||length)-1).toString() + "/" + len.toString() } if(!Compressible(mtype)){ deflate=gzip=false } /*if(deflate){ head["content-encoding"]= "deflate" } else */if(gzip){ head["content-encoding"]= "gzip" } else{ head["content-length"]= length } if(this.fixedHeaders){ //vw.log(this.fixedHeaders) for(var id in this.fixedHeaders){ if(this.fixedHeaders[id]===undefined) delete head[id] else head[id]= this.fixedHeaders[id] } } args.response.writeHead(partial?206:200,head) //var stream2= StaticServe.FileSeekableStream.createStream(stream) var stream2= fs.createReadStream("", { fd: stream.$fd, start: stream.position, end: stream.maxPosition }) /* if(deflate){ vw.info("streaming with zlib") stream2.pipe(Zlib.createDeflate()).pipe(args.response) } else */if(gzip){ vw.info("streaming with gzip") stream2.pipe(Zlib.createGzip()).pipe(args.response) } else{ vw.info("streaming without compression") stream2.pipe(args.response) } //().pipe(args.response) //} //catch(e){ // this.serverError500(args, e) //} } getMimeType(file){ return Mime.lookup(Path.extname(file))|| "text/plain" } serverError500(args, e){ //vw.error(e) args.response.writeHead(500,{ "Content-type": "text/plain;charset=utf8" }) args.response.write(e.message) args.response.write("\n"+e.stack) args.response.end() } notmodified304(args){ args.response.writeHead(304,{ "accept-range": "bytes", "cache-control": "max-age=1800" }) args.response.end() } error404(args){ args.response.writeHead(404, { "Content-type": "text/plain;charset=utf8", "accept-range": "bytes" }) args.response.write("La url no es válida") args.response.end() } } export default Server