UNPKG

vox-core

Version:

Runtime de aplicaciones multiplataforma

184 lines 8.2 kB
var $mod$2 = core.VW.Ecma2015.Utils.module(require('path')); var Fs = core.System.IO.Fs; var $mod$3 = core.VW.Ecma2015.Utils.module(require('fs')); var StaticServe = core.VW.Http.StaticServe; var $mod$4 = core.VW.Ecma2015.Utils.module(require('mime-types')); var $mod$5 = core.VW.Ecma2015.Utils.module(require('zlib')); var $mod$6 = core.VW.Ecma2015.Utils.module(require('compressible')); { var Server = function Server() { Server.$constructor ? Server.$constructor.apply(this, arguments) : Server.$superClass && Server.$superClass.apply(this, arguments); }; Server.$constructor = function () { this.$paths = []; }; Server.prototype.addPath = function (path) { this.$paths.push(path); }; Server.prototype.removePath = function (path) { var index = this.$paths.indexOf(path); this.$paths[index] = undefined; }; Server.prototype.handle = function (args) { var url = args.request.url; var path, pathO; for (var i = 0; i < this.$paths.length; i++) { pathO = this.$paths[i]; if (pathO) { path = $mod$2.default.join(pathO, url); if (Fs.sync.exists(path)) break; path = undefined; } } if (!path) return this.error404(args); return this.responseFile(path, args); }; Server.prototype.responseFile = function callee$0$0(path, args) { var modified, stat, mtime2, mtime, modifieddate, range, startByte, endByte, dirOrNot, stream, partial, length, len, gzip, deflate, encoding, mtype, charset, head, id, stream2; return regeneratorRuntime.async(function callee$0$0$(context$1$0) { while (1) switch (context$1$0.prev = context$1$0.next) { case 0: modified = args.request.headers['if-modified-since']; context$1$0.next = 3; return regeneratorRuntime.awrap(Fs.async.stat(path)); case 3: stat = context$1$0.sent; mtime2 = stat.mtime; mtime = mtime2.getTime(); if (!modified) { context$1$0.next = 11; break; } modifieddate = new Date(modified); modifieddate = modifieddate.getTime(); if (!(modifieddate - mtime < 1000)) { context$1$0.next = 11; break; } return context$1$0.abrupt('return', this.notmodified304(args)); case 11: range = args.request.headers['range']; if (range && range.startsWith('bytes=')) { range = range.split('='); if (range.length == 2) { range = range[1].split('-'); startByte = range[0] | 0; endByte = range[1] | 0; } } dirOrNot = false; if (!Fs.sync.exists(path)) { dirOrNot = true; } context$1$0.next = 17; return regeneratorRuntime.awrap(Fs.async.stat(path)); case 17: stat = context$1$0.sent; if (stat.isDirectory()) dirOrNot = true; if (!dirOrNot) { context$1$0.next = 23; break; } args.response.writeHead(403); args.response.end(); return context$1$0.abrupt('return'); case 23: stream = new core.System.IO.FileStream(path, core.System.IO.FileMode.Open, core.System.IO.FileAccess.Read); stream.position = 0; partial = startByte > 0 || endByte < stream.length - 1; 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; } if (args.request.headers['accept-encoding'] && length > 100) { encoding = args.request.headers['accept-encoding'].toUpperCase(); gzip = encoding.indexOf('GZIP') >= 0; deflate = encoding.indexOf('DEFLATE') >= 0; } mtype = this.getMimeType(path); charset = $mod$4.default.charsets.lookup(mtype); head = { 'last-modified': mtime2.toGMTString(), '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 (!$mod$6.default(mtype)) { deflate = gzip = false; } if (gzip) { head['content-encoding'] = 'gzip'; } else { head['content-length'] = length; } if (this.fixedHeaders) { for (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); stream2 = $mod$3.default.createReadStream('', { fd: stream.$fd, start: stream.position, end: stream.maxPosition }); if (gzip) { vw.info('streaming with gzip'); stream2.pipe($mod$5.default.createGzip()).pipe(args.response); } else { vw.info('streaming without compression'); stream2.pipe(args.response); } case 40: case 'end': return context$1$0.stop(); } }, null, this); }; Server.prototype.getMimeType = function (file) { return $mod$4.default.lookup($mod$2.default.extname(file)) || 'text/plain'; }; Server.prototype.serverError500 = function (args, 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(); }; Server.prototype.notmodified304 = function (args) { args.response.writeHead(304, { 'accept-range': 'bytes', 'cache-control': 'max-age=1800' }); args.response.end(); }; Server.prototype.error404 = function (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(); }; } exports.default = Server;