feat: 优化API 修复http模块错误

This commit is contained in:
coding 2018-01-09 12:19:02 +00:00
parent a6be26206e
commit 96f93754e6
3 changed files with 30 additions and 35 deletions

View File

@ -111,8 +111,8 @@ function EventHandlerDefault() {
* @param ignoreCancel
*/
this.listen = function listen(jsp, event, exec, priority, ignoreCancel) {
if (!jsp || !jsp.description || !jsp.description.name) throw new TypeError('插件名称为空 请检查传入参数!');
var name = jsp.description.name;
if (ext.isNull(name)) throw new TypeError('插件名称为空 请检查传入参数!');
var eventCls = this.name2Class(name, event);
if (!eventCls) { return; }
if (typeof priority === 'boolean') {
@ -128,7 +128,7 @@ function EventHandlerDefault() {
if (!listenerMap[name]) listenerMap[name] = [];
var offExec = function () {
this.unregister(eventCls, listener);
console.debug('插件 %s 注销事件 %s'.format(name, this.class2Name(eventCls)));
console.debug('插件 %s 注销事件 %s'.format(name, eventCls.name.substring(eventCls.name.lastIndexOf(".") + 1)));
}.bind(this);
var off = {
event: eventCls,
@ -153,4 +153,4 @@ module.exports = {
delete EventHandler.listenerMap[jsp.description.name];
}
}
};
};

View File

@ -2,7 +2,7 @@
/**
* MiaoScript脚本插件加载类
*/
/*global Java, base, module, exports, require, __FILE__*/
/*global Java, module, exports, require, __FILE__*/
// var zip = require("core/zip");
var fs = require('core/fs');
var yaml = require('modules/yaml');
@ -87,7 +87,7 @@ function loadPlugin(file) {
hook: function (origin) {
return beforeLoadHook(origin);
}
});
})
console.debug("插件编译结果: %s".format(JSON.stringify(plugin)));
var desc = plugin.description;
if (!desc || !desc.name) {
@ -127,16 +127,15 @@ function initPlugin(file, plugin) {
// 初始化 __FILE__
plugin.__FILE__ = file;
// 初始化 __DATA__
plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name);
plugin.__DATA__ = plugin.dataFolder = fs.file(file.parentFile, plugin.description.name);
// 初始化 getDataFolder()
plugin.getDataFolder = function () {
plugin.getDataFolder = function getDataFolder() {
return plugin.__DATA__;
};
}
// 初始化 getFile()
plugin.getFile = function (name) {
plugin.getFile = plugin.file = function getFile(name) {
return fs.file(plugin.getDataFolder(), name);
};
// 初始化插件配置相关方法
initPluginConfig(plugin);
@ -164,9 +163,9 @@ function initPluginConfig(plugin) {
if (!file.isFile) {
file = plugin.getFile(file);
}
return yaml.safeLoad(base.read(file));
return yaml.safeLoad(fs.read(file), { json: true });
}
};
}
/**
* 重载配置文件
* @constructor
@ -174,7 +173,7 @@ function initPluginConfig(plugin) {
*/
plugin.reloadConfig = function () {
plugin.config = plugin.getConfig(plugin.configFile);
};
}
/**
* 保存配置文件
* @constructor
@ -184,13 +183,13 @@ function initPluginConfig(plugin) {
switch (arguments.length) {
case 0:
plugin.configFile.parentFile.mkdirs();
base.save(plugin.configFile, yaml.safeDump(plugin.config));
fs.save(plugin.configFile, yaml.safeDump(plugin.config));
break;
case 2:
base.save(arguments[0], yaml.safeDump(arguments[1]));
fs.save(fs.file(plugin.__DATA__, arguments[0]), yaml.safeDump(arguments[1]));
break;
}
};
}
if (plugin.configFile.isFile()) {
plugin.config = plugin.getConfig('config.yml');
} else if (plugin.description.config) {
@ -199,10 +198,6 @@ function initPluginConfig(plugin) {
}
}
function runAndCatch(jsp, name, ext) {
}
function checkAndGet(args) {
if (args.length === 0) {
return plugins;
@ -223,15 +218,13 @@ function checkAndRun(args, name, ext) {
for (var i in pls) {
var jsp = pls[i];
var exec = jsp[name];
if (exec) {
try {
// 绑定方法的this到插件自身
exec.bind(jsp)();
if (ext) ext.bind(jsp)();
} catch (ex) {
console.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s'.format(jsp.description.name, name, ex.message));
console.ex(ex);
}
try {
// 绑定方法的this到插件自身
if (typeof exec === "function") exec.call(jsp);
if (ext) ext.call(jsp);
} catch (ex) {
console.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s'.format(jsp.description.name, name, ex.message));
console.ex(ex);
}
}
}

View File

@ -47,7 +47,7 @@ function open(url, method, header) {
var conn = new URL(url).openConnection();
if (conn instanceof HttpsURLConnection) {
conn.setHostnameVerifier(TrustAnyHostnameVerifier);
conn.setSSLSocketFactory(TrustAnyHostnameVerifier);
conn.setSSLSocketFactory(SSLSocketFactory);
}
conn.setRequestMethod(method);
conn.setDoOutput(true);
@ -81,10 +81,12 @@ function request(url, method, header, params, body) {
var conn = open(buildUrl(url, params), method, header);
try {
conn.connect();
var out = conn.getOutputStream();
out.write(new String(body).getBytes(config.Charset));
out.flush();
out.close();
if (body) {
var out = conn.getOutputStream();
out.write(new String(body).getBytes(config.Charset));
out.flush();
out.close();
}
return response(conn);
} finally {
conn.disconnect();
@ -110,4 +112,4 @@ var http = {
}
})
exports = module.exports = http;
exports = module.exports = http;