mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
258 lines
8.5 KiB
JavaScript
258 lines
8.5 KiB
JavaScript
(function(){
|
|
'strict mode';
|
|
|
|
/* Global var accessible from any loaded module */
|
|
global.cloudcmd = {};
|
|
|
|
var DIR, LIBDIR, SRVDIR, JSONDIR,
|
|
Util,
|
|
|
|
SLASH,
|
|
ISWIN32,
|
|
ext,
|
|
path, fs, zlib, url,
|
|
|
|
OK, FILE_NOT_FOUND;
|
|
|
|
/* Consts */
|
|
|
|
exports.OK = OK = 200;
|
|
exports.FILE_NOT_FOUND = 404;
|
|
exports.REQUEST = 'request';
|
|
exports.RESPONSE = 'response';
|
|
|
|
/* Native Modules*/
|
|
exports.crypto = require('crypto'),
|
|
exports.child_process = require('child_process'),
|
|
exports.fs = fs = require('fs'),
|
|
exports.http = require('http'),
|
|
exports.https = require('https'),
|
|
exports.path = path = require('path'),
|
|
exports.url = url = require('url'),
|
|
exports.querystring = require('querystring'),
|
|
|
|
/* Constants */
|
|
/* current dir + 2 levels up */
|
|
exports.WIN32 = ISWIN32 = isWin32();
|
|
exports.SLASH = SLASH = ISWIN32 ? '\\' : '/',
|
|
|
|
exports.SRVDIR = SRVDIR = __dirname + SLASH,
|
|
exports.LIBDIR = LIBDIR = path.normalize(SRVDIR + '../'),
|
|
exports.DIR = DIR = path.normalize(LIBDIR + '../'),
|
|
exports.HTMLDIR = DIR + 'html' + SLASH,
|
|
exports.JSONDIR = JSONDIR = DIR + 'json' + SLASH,
|
|
|
|
/* Functions */
|
|
exports.require = mrequire,
|
|
exports.librequire = librequire,
|
|
exports.srvrequire = srvrequire,
|
|
exports.rootrequire = rootrequire,
|
|
exports.generateHeaders = generateHeaders,
|
|
exports.getQuery = getQuery,
|
|
exports.sendFile = sendFile,
|
|
|
|
/* compitability with old versions of node */
|
|
exports.fs.exists = exports.fs.exists || exports.path.exists;
|
|
//exports.fs.watch = isWin32 ? fs.watch : linuxWatch;
|
|
|
|
|
|
/* Needed Modules */
|
|
exports.util = Util = require(LIBDIR + 'util'),
|
|
|
|
exports.zlib = zlib = mrequire('zlib'),
|
|
|
|
/* Main Information */
|
|
exports.modules = jsonrequire('modules');
|
|
exports.ext = ext = jsonrequire('ext');
|
|
exports.mainpackage = rootrequire('package');
|
|
/* base configuration */
|
|
exports.config = {
|
|
server : true,
|
|
socket : true,
|
|
port : 80
|
|
},
|
|
|
|
|
|
/*
|
|
* Any of loaded below modules could work with global var so
|
|
* it should be initialized first. Becouse of almost any of
|
|
* moudles do not depends on each other all needed information
|
|
* for all modules is initialized hear.
|
|
*/
|
|
global.cloudcmd.main = exports;
|
|
|
|
exports.VOLUMES = getVolumes(),
|
|
|
|
/* Additional Modules */
|
|
exports.socket = srvrequire('socket'),
|
|
exports.auth = srvrequire('auth').auth,
|
|
exports.appcache = srvrequire('appcache'),
|
|
exports.cache = srvrequire('cache').Cache,
|
|
exports.cloudfunc = librequire('cloudfunc'),
|
|
exports.rest = srvrequire('rest').api,
|
|
exports.update = srvrequire('update'),
|
|
exports.ischanged = srvrequire('ischanged');
|
|
exports.commander = srvrequire('commander');
|
|
exports.minify = srvrequire('minify').Minify;
|
|
/*
|
|
* second initializing after all modules load, so global var is
|
|
* totally filled of all information that should know all modules
|
|
*/
|
|
global.cloudcmd.main = exports;
|
|
/**
|
|
* function do safe require of needed module
|
|
* @param {Strin} pSrc
|
|
*/
|
|
function mrequire(pSrc){
|
|
var lModule,
|
|
lError = Util.tryCatchLog(function(){
|
|
lModule = require(pSrc);
|
|
});
|
|
|
|
if(lError)
|
|
console.log(lError);
|
|
|
|
return lModule;
|
|
}
|
|
|
|
function rootrequire(pSrc){ return mrequire(DIR + pSrc); }
|
|
|
|
function librequire(pSrc){ return mrequire(LIBDIR + pSrc); }
|
|
|
|
function srvrequire(pSrc){ return mrequire(SRVDIR + pSrc); }
|
|
|
|
function jsonrequire(pSrc){ return mrequire(JSONDIR + pSrc);}
|
|
|
|
/**
|
|
* function check is current platform is win32
|
|
*/
|
|
function isWin32(){ return process.platform === 'win32'; }
|
|
|
|
/**
|
|
* get volumes if win32 or get nothing if nix
|
|
*/
|
|
function getVolumes(){
|
|
var lRet = ISWIN32 ? [] : '/';
|
|
|
|
if(ISWIN32)
|
|
srvrequire('win').getVolumes(function(pVolumes){
|
|
console.log(pVolumes);
|
|
exports.VOLUMES = pVolumes;
|
|
});
|
|
|
|
return lRet;
|
|
}
|
|
|
|
/**
|
|
* Функция создаёт заголовки файлов
|
|
* в зависимости от расширения файла
|
|
* перед отправкой их клиенту
|
|
* @param pName - имя файла
|
|
* @param pGzip - данные сжаты gzip'ом
|
|
*/
|
|
function generateHeaders(pName, pGzip, pQuery){
|
|
var lRet,
|
|
lType = '',
|
|
lContentEncoding = '',
|
|
lCacheControl = 0,
|
|
|
|
lExt = Util.getExtension(pName);
|
|
|
|
if( Util.strCmp(lExt, '.appcache') )
|
|
lCacheControl = 1;
|
|
|
|
lType = ext[lExt] || 'text/plain';
|
|
|
|
/* if type of file any, but img -
|
|
* then we shoud specify charset
|
|
*/
|
|
if( !Util.isContainStr(lType, 'img') )
|
|
lContentEncoding = '; charset=UTF-8';
|
|
|
|
if(Util.strCmp(pQuery, 'download') )
|
|
lType = 'application/octet-stream';
|
|
|
|
|
|
if(!lCacheControl)
|
|
lCacheControl = 31337 * 21;
|
|
|
|
lRet = {
|
|
'Content-Type': lType + lContentEncoding,
|
|
'cache-control': 'max-age=' + lCacheControl,
|
|
'last-modified': new Date().toString(),
|
|
/* https://developers.google.com/speed/docs/best-practices
|
|
/caching?hl=ru#LeverageProxyCaching */
|
|
'Vary': 'Accept-Encoding'
|
|
};
|
|
|
|
if(pGzip)
|
|
lRet['content-encoding'] = 'gzip';
|
|
|
|
return lRet;
|
|
}
|
|
|
|
/**
|
|
* send file to client thru pipe
|
|
* and gzip it if client support
|
|
*
|
|
* @param pName - имя файла
|
|
* @param pGzip - данные сжаты gzip'ом
|
|
*/
|
|
function sendFile(pParams){
|
|
var lRet,
|
|
lName, lReq, lRes, lQuery, lGziped;
|
|
|
|
if(pParams){
|
|
lName = pParams.name,
|
|
lReq = pParams.request,
|
|
lRes = pParams.response;
|
|
lGziped = pParams.gziped;
|
|
lQuery = getQuery(lReq);
|
|
}
|
|
|
|
if(lName && lRes && lReq){
|
|
var lEnc = lReq.headers['accept-encoding'] || '',
|
|
lGzip = lEnc.match(/\bgzip\b/),
|
|
|
|
lReadStream = fs.createReadStream(lName, {
|
|
'bufferSize': 4 * 1024
|
|
});
|
|
|
|
lReadStream.on('error', function(pError){
|
|
lRes.writeHead(FILE_NOT_FOUND, 'OK');
|
|
lRes.end(String(pError));
|
|
});
|
|
|
|
lRes.writeHead(OK, generateHeaders(lName, lGzip, lQuery ) );
|
|
|
|
if (lGzip && !lGziped)
|
|
lReadStream = lReadStream.pipe( zlib.createGzip() );
|
|
|
|
lReadStream.pipe(lRes);
|
|
|
|
lRet = true;
|
|
}
|
|
|
|
return lRet;
|
|
}
|
|
|
|
function getQuery(pReq){
|
|
var lQuery, lParsedUrl;
|
|
|
|
if(pReq){
|
|
lParsedUrl = url.parse(pReq.url);
|
|
lQuery = lParsedUrl.query;
|
|
}
|
|
|
|
return lQuery;
|
|
}
|
|
|
|
|
|
function linuxWatch(pFile, pCallBack){
|
|
fs.watchFile(pFile, function(pCurr, pPrev){
|
|
if(pCurr.mtime !== pPrev.mtime)
|
|
Util.exec(pCallBack);
|
|
});
|
|
}
|
|
|
|
})();
|