mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
387 lines
12 KiB
JavaScript
387 lines
12 KiB
JavaScript
(function(){
|
|
'strict mode';
|
|
|
|
/* Global var accessible from any loaded module */
|
|
global.cloudcmd = {};
|
|
|
|
var DIR, LIBDIR, SRVDIR, JSONDIR, HTMLDIR,
|
|
Util,
|
|
|
|
SLASH,
|
|
ISWIN32,
|
|
ext,
|
|
path, fs, zlib, url,
|
|
|
|
OK, FILE_NOT_FOUND,
|
|
|
|
Config = {
|
|
server : true,
|
|
socket : true,
|
|
port : 80
|
|
};
|
|
|
|
/* Consts */
|
|
|
|
exports.OK = OK = 200;
|
|
exports.FILE_NOT_FOUND = 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 = HTMLDIR = DIR + 'html' + SLASH,
|
|
exports.JSONDIR = JSONDIR = DIR + 'json' + SLASH,
|
|
|
|
/* Functions */
|
|
exports.require = mrequire,
|
|
exports.librequire = librequire,
|
|
exports.srvrequire = srvrequire,
|
|
exports.rootrequire = rootrequire,
|
|
exports.quietrequire = quietrequire,
|
|
|
|
exports.generateHeaders = generateHeaders,
|
|
exports.getQuery = getQuery,
|
|
exports.isGZIP = isGZIP,
|
|
|
|
exports.sendFile = sendFile,
|
|
exports.sendResponse = sendResponse,
|
|
exports.sendError = sendError,
|
|
|
|
exports.checkParams = checkParams,
|
|
|
|
/* compitability with old versions of node */
|
|
exports.fs.exists = exports.fs.exists || exports.path.exists,
|
|
|
|
/* 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 = Config,
|
|
|
|
|
|
/*
|
|
* 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.tryCatch(function(){
|
|
lModule = require(pSrc);
|
|
});
|
|
|
|
if(lError){
|
|
Util.log('Module ' + pSrc + ' not connected');
|
|
Util.log('Change json/config.json to prevent this message');
|
|
Util.log('or install module ' + pSrc);
|
|
}
|
|
|
|
return lModule;
|
|
}
|
|
|
|
function quietrequire(pSrc){
|
|
var lModule;
|
|
|
|
Util.tryCatch(function(){
|
|
lModule = require(pSrc);
|
|
});
|
|
|
|
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 pParams
|
|
* name - имя файла
|
|
* gzip - данные сжаты gzip'ом
|
|
* query
|
|
* https://developers.google.com/speed/docs/best-practices/caching?hl=ru#LeverageProxyCaching
|
|
*/
|
|
function generateHeaders(pParams){
|
|
var lRet = Util.checkObjTrue(pParams, ['name']),
|
|
p = pParams;
|
|
|
|
if(lRet){
|
|
var lExt = Util.getExtension(p.name),
|
|
lType = ext[lExt] || 'text/plain',
|
|
lContentEncoding = '';
|
|
|
|
/* if type of file any, but img - then we shoud specify charset */
|
|
if( !Util.isContainStr(lType, 'img') )
|
|
lContentEncoding = '; charset=UTF-8';
|
|
|
|
if( Util.isContainStr(p.query, 'download') )
|
|
lType = 'application/octet-stream';
|
|
|
|
|
|
lRet = {
|
|
'Content-Type': lType + lContentEncoding,
|
|
'last-modified': new Date().toString(),
|
|
'Vary': 'Accept-Encoding'
|
|
};
|
|
|
|
if( !Util.strCmp(lExt, '.appcache') && exports.config.cache){
|
|
var lCacheControl = 31337 * 21;
|
|
lRet['cache-control'] = 'max-age=' + lCacheControl;
|
|
}
|
|
|
|
if(p.gzip)
|
|
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, lGziped;
|
|
|
|
if(pParams){
|
|
lName = pParams.name,
|
|
lReq = pParams.request,
|
|
lRes = pParams.response;
|
|
lGziped = pParams.gziped;
|
|
}
|
|
if(lName && lRes && lReq){
|
|
var lGzip = isGZIP(lReq),
|
|
|
|
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({
|
|
name : lName,
|
|
gzip : lGzip,
|
|
query : getQuery(lReq)
|
|
}) );
|
|
|
|
if (lGzip && !lGziped)
|
|
lReadStream = lReadStream.pipe( zlib.createGzip() );
|
|
|
|
lReadStream.pipe(lRes);
|
|
|
|
lRet = true;
|
|
}
|
|
|
|
return lRet;
|
|
}
|
|
|
|
|
|
/**
|
|
* Функция высылает ответ серверу
|
|
* @param pHead - заголовок
|
|
* @param Data - данные
|
|
* @param pName - имя отсылаемого файла
|
|
*/
|
|
function sendResponse(pParams, pData){
|
|
var lRet = Util.checkObjTrue(pParams,
|
|
['name', 'request', 'response']);
|
|
|
|
if(lRet){
|
|
var p = pParams;
|
|
|
|
var lQuery = getQuery(p.request),
|
|
/* download, json */
|
|
lGzip = isGZIP(p.request),
|
|
lHead = generateHeaders({
|
|
name : p.name,
|
|
gzip : lGzip,
|
|
query : lQuery
|
|
});
|
|
|
|
/* если браузер поддерживает gzip-сжатие - сжимаем данные*/
|
|
Util.ifExec(!lGzip,
|
|
function(pParams){
|
|
var lRet = Util.checkObj(pParams, ['data']);
|
|
|
|
if(lRet){
|
|
p.status = pParams.status || p.status;
|
|
p.data = pParams.data;
|
|
}
|
|
|
|
p.response.writeHead(p.status || OK, lHead);
|
|
p.response.end(p.data);
|
|
|
|
Util.log( p.name + ' sended');
|
|
},
|
|
|
|
function(pCallBack){
|
|
zlib.gzip (p.data || pData, Util.call(gzipData, {
|
|
callback : pCallBack
|
|
}));
|
|
});
|
|
}
|
|
}
|
|
|
|
function sendError(pParams, pError){
|
|
var lRet = Util.checkObjTrue(pParams,
|
|
['name', 'request', 'response']);
|
|
|
|
if(lRet){
|
|
var p = pParams;
|
|
p.status = FILE_NOT_FOUND;
|
|
p.data = p.data || pError.toString();
|
|
sendResponse(p);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Функция получает сжатые данные
|
|
* @param pHeader - заголовок файла
|
|
* @pName
|
|
*/
|
|
function gzipData(pParams){
|
|
var lRet = checkCallBackParams(pParams);
|
|
|
|
if(lRet)
|
|
lRet = Util.checkObj(pParams.params, ['callback']);
|
|
|
|
if(lRet){
|
|
var p = pParams,
|
|
c = pParams.params,
|
|
lParams = {};
|
|
|
|
if(!p.error)
|
|
lParams.data = p.data;
|
|
else{
|
|
lParams.status = FILE_NOT_FOUND;
|
|
lParams.data = p.error.toString();
|
|
}
|
|
|
|
Util.exec(c.callback, lParams);
|
|
}
|
|
}
|
|
|
|
|
|
function checkCallBackParams(pParams){
|
|
return Util.checkObj(pParams, ['error', 'data', 'params']);
|
|
}
|
|
|
|
function checkParams(pParams, pAdditional){
|
|
var lRet = Util.checkObjTrue( pParams, ['name', 'request', 'response'] );
|
|
|
|
if(lRet && pAdditional)
|
|
lRet = Util.checkObjTrue( pParams, pAdditional);
|
|
|
|
return lRet;
|
|
}
|
|
|
|
function getQuery(pReq){
|
|
var lQuery, lParsedUrl;
|
|
|
|
if(pReq){
|
|
lParsedUrl = url.parse(pReq.url);
|
|
lQuery = lParsedUrl.query;
|
|
}
|
|
|
|
return lQuery;
|
|
}
|
|
|
|
function isGZIP(pReq){
|
|
var lEnc, lGZIP;
|
|
if(pReq){
|
|
lEnc = pReq.headers['accept-encoding'] || '';
|
|
lGZIP = lEnc.match(/\bgzip\b/);
|
|
}
|
|
|
|
return lGZIP;
|
|
}
|
|
|
|
|
|
function linuxWatch(pFile, pCallBack){
|
|
fs.watchFile(pFile, function(pCurr, pPrev){
|
|
if(pCurr.mtime !== pPrev.mtime)
|
|
Util.exec(pCallBack);
|
|
});
|
|
}
|
|
|
|
})();
|