avoscloud-code-mock-sdk
Advanced tools
Comparing version 0.2.1 to 0.2.2-beta
@@ -13,4 +13,6 @@ var AV = require('avoscloud-sdk').AV; | ||
var cronJob = require('cron').CronJob; | ||
var qs = require('qs'); | ||
var Global = {} | ||
var _ref, _ref1; | ||
@@ -124,30 +126,67 @@ if ((_ref = https.globalAgent) != null) { | ||
if (!/[a-zA-Z0-9]+/.exec(name)) { | ||
throw "The timer name must be an valid identifier."; | ||
} | ||
if ((typeof func) !== 'function') { | ||
throw "The func must be a function."; | ||
} | ||
if ((typeof cron) !== 'string') { | ||
throw "The cron must be a valid string in the form of 'sec min hour dayOfMonth month dayOfWeek [year]'."; | ||
} | ||
if (cron.split(" ").length < 6) { | ||
throw "The cron must be a valid string in the form of 'sec min hour dayOfMonth month dayOfWeek [year]'."; | ||
} | ||
new cronJob(cron, func, null, true); | ||
if (!/[a-zA-Z0-9]+/.exec(name)) { | ||
throw "The timer name must be an valid identifier."; | ||
} | ||
if ((typeof func) !== 'function') { | ||
throw "The func must be a function."; | ||
} | ||
if ((typeof cron) !== 'string') { | ||
throw "The cron must be a valid string in the form of 'sec min hour dayOfMonth month dayOfWeek [year]'."; | ||
} | ||
if (cron.split(" ").length < 6) { | ||
throw "The cron must be a valid string in the form of 'sec min hour dayOfMonth month dayOfWeek [year]'."; | ||
} | ||
new cronJob(cron, func, null, true); | ||
}; | ||
HTTPResponse = (function() { | ||
function HTTPResponse(buffer, headers, response, status, text) { | ||
this.buffer = buffer != null ? buffer : null; | ||
this.headers = headers != null ? headers : {}; | ||
this.response = response != null ? response : null; | ||
this.status = status != null ? status : null; | ||
this.text = text != null ? text : null; | ||
} | ||
function HTTPResponse(buffer, headers, response, status, text) { | ||
this.buffer = buffer != null ? buffer : null; | ||
this.headers = headers != null ? headers : {}; | ||
this.response = response != null ? response : null; | ||
this.status = status != null ? status : null; | ||
this.text = text != null ? text : null; | ||
} | ||
return HTTPResponse; | ||
return HTTPResponse; | ||
})(); | ||
mimeTypes = [ | ||
{ | ||
pattern: /$text\/plain.*/, | ||
process: function(res) { | ||
return res.text; | ||
} | ||
}, { | ||
pattern: /$application\/json.*/, | ||
process: function(res) { | ||
return JSON.parse(res.text); | ||
} | ||
}, { | ||
pattern: /application\/x-www-form-urlencoded/, | ||
process: function(res) { | ||
return qs.parse(res.buffer); | ||
} | ||
} | ||
]; | ||
trySetData = function(httpRes) { | ||
var contentType, type; | ||
contentType = httpRes.headers['content-type']; | ||
type = _.find(mimeTypes, function(mimeType) { | ||
return mimeType.pattern.exec; | ||
}); | ||
if (type != null) { | ||
try{ | ||
return httpRes.data = type.process(httpRes); | ||
}catch(e){ | ||
httpRes.data = httpRes.buffer; | ||
} | ||
} else { | ||
return httpRes.data = httpRes.buffer; | ||
} | ||
}; | ||
AV.Cloud.HTTPResponse = HTTPResponse; | ||
@@ -180,2 +219,5 @@ | ||
} | ||
delete options.params; | ||
delete options.body; | ||
delete options.url; | ||
requestOptions = { | ||
@@ -188,6 +230,7 @@ host: hostname, | ||
}; | ||
requestOptions = _.extend(requestOptions, options); | ||
httpResponse = new HTTPResponse; | ||
request = http_module.request(requestOptions, function(res) { | ||
var chunkList, contentLength; | ||
httpResponse.headers = res.headers || {}; | ||
httpResponse.status = res.statusCode; | ||
@@ -212,2 +255,3 @@ httpResponse.text = ''; | ||
} | ||
trySetData(httpResponse); | ||
if (httpResponse.status < 200 || httpResponse.status >= 400) { | ||
@@ -229,10 +273,25 @@ return promise.reject(httpResponse); | ||
Global.files = {} | ||
function watchFile(f, name){ | ||
if(Global.files[f]) | ||
return; | ||
Global.files[f] = true; | ||
fs.watchFile(f,{ persistent: true, interval: 2000 },function(curr, prev){ | ||
if(curr.mtime != prev.mtime){ | ||
console.log("File " + f + " is changed,reload it..."); | ||
requireFromFile(f, name); | ||
} | ||
}); | ||
} | ||
//Mock express | ||
var Module = module.constructor; | ||
var paths = module.paths; | ||
function requireFromString(src, filename) { | ||
function requireFromFile(path, filename) { | ||
var src = fs.readFileSync(path, 'utf-8'); | ||
var m = new Module(); | ||
m.paths = module.paths; | ||
m._compile("var AV = require('avoscloud-sdk').AV;var __production=0; \n" + src, filename); | ||
watchFile(path,filename); | ||
return m.exports; | ||
@@ -245,3 +304,3 @@ } | ||
id = Global.rootPath + id; | ||
return requireFromString(fs.readFileSync(require.resolve(id), 'utf-8'), id); | ||
return requireFromFile(require.resolve(id), id); | ||
} | ||
@@ -252,2 +311,11 @@ result = Module._load(id, this); | ||
result = function(){ | ||
if(Global.app!=null){ | ||
delete Global.app.routes.get; | ||
delete Global.app.routes.post; | ||
delete Global.app.routes.put; | ||
delete Global.app.routes.delete; | ||
delete Global.app.routes.options; | ||
addSystemEndpoints(Global.app); | ||
return Global.app; | ||
} | ||
var app = oldExpress(); | ||
@@ -334,3 +402,11 @@ app.__listen = app.listen; | ||
} | ||
function addSystemEndpoints(app){ | ||
//Added test endpoints. | ||
app.post("/avos/:className/:func", function(req, res){ | ||
processRequest('object', req, res); | ||
}); | ||
app.post("/avos/:name", function(req, res){ | ||
processRequest("function", req, res); | ||
}); | ||
} | ||
exports.runCloudCode = function(rootPath){ | ||
@@ -350,3 +426,3 @@ Global.rootPath = rootPath; | ||
var cloudPath = path.resolve(Global.rootPath + 'cloud/main.js'); | ||
requireFromString(fs.readFileSync(cloudPath, 'utf-8'), 'cloud/main.js'); | ||
requireFromFile(cloudPath, 'cloud/main.js'); | ||
//Stratup mock server. | ||
@@ -359,12 +435,19 @@ var app = Global.app; | ||
var port = app.port || 3000; | ||
//Added test endpoints. | ||
app.post("/avos/:className/:func", function(req, res){ | ||
processRequest('object', req, res); | ||
process.on('uncaughtException', function (err) { | ||
var msg = err; | ||
var stack = null; | ||
if(err.message){ | ||
msg = err.message; | ||
} | ||
if(err.stack){ | ||
stack = err.stack; | ||
} | ||
console.error((new Date).toUTCString() + ' uncaughtException:', msg); | ||
console.error(stack); | ||
}); | ||
app.post("/avos/:name", function(req, res){ | ||
processRequest("function", req, res); | ||
}); | ||
addSystemEndpoints(app); | ||
app.__listen(port, function() { | ||
return console.log("Mock Server is listening on " + port + "\nPress CTRL-C to stop server."); | ||
}); | ||
} | ||
} | ||
{ | ||
"name": "avoscloud-code-mock-sdk", | ||
"version": "0.2.1", | ||
"version": "0.2.2-beta", | ||
"main": "./lib/cloud_code.js", | ||
"description": "AVOSCloud Cloud Code Mock SDK.!", | ||
"dependencies": { | ||
"qs" : "0.6.5", | ||
"avoscloud-sdk" : "latest", | ||
@@ -8,0 +9,0 @@ "underscore" : "1.4.4", |
@@ -0,1 +1,8 @@ | ||
## 更新日志 | ||
* 2013-10-18 更新0.2.2 beta版本,支持代码热加载,修复httpRequest没有返回data的bug。 | ||
* 2013-10-18 更新0.2.1版本,修复AV.Cloud.httpRequest | ||
* 2013-10-17 更新0.2.0版本,添加定时器API | ||
## 说明 | ||
@@ -6,3 +13,3 @@ | ||
* 要在本地调试云代码,你需要安装[node.js](http://nodejs.org)最新版本。 | ||
* 运行命令: `sudo npm install -g avoscloud-code-mock-sdk` 安装调试SDK。 | ||
* 运行命令: `sudo npm install -g avoscloud-code-mock-sdk` 安装调试SDK。以后更新升级也请执行此命令。 | ||
* 在项目根目录运行`avoscloud`,将启动本地调试服务器。 | ||
@@ -28,4 +35,1 @@ * 访问`http://localhost:3000/`即可访问到你的云主机代码,子路径按照你在`app.js`里配置的即可访问。 | ||
## 更新日志 | ||
* 2013-10-17 更新0.2.0版本,添加定时器API |
Sorry, the diff of this file is not supported yet
907776
8
416
34
16
+ Addedqs@0.6.5