mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-07-12 11:43:10 +00:00
Compare commits
5 Commits
c9c70d9be6
...
137793cd77
Author | SHA1 | Date | |
---|---|---|---|
137793cd77 | |||
27636a4a72 | |||
d86c9728e3 | |||
7de4e4f1be | |||
3733eaea45 |
68
lib/caterpillar.js
Normal file
68
lib/caterpillar.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
// caterpillar.js
|
||||
// Integration with [Caterpillar Proxy](https://github.com/gnh1201/caterpillar) project
|
||||
// https://github.com/gnh1201/welsonjs
|
||||
var JSONRPC2 = require("lib/jsonrpc2");
|
||||
|
||||
function Caterpillar(url) {
|
||||
var rpc = JSONRPC2.create(url);
|
||||
var env = {
|
||||
"target": "http://localhost/",
|
||||
"method": ""
|
||||
};
|
||||
|
||||
this.set_env = function(k, v) {
|
||||
env[k] = v || null;
|
||||
};
|
||||
this.set_default_env = function(_env) {
|
||||
for (k in _env) {
|
||||
if (!(k in env)) {
|
||||
env[k] = _env[k];
|
||||
}
|
||||
}
|
||||
};
|
||||
this.set_method = function(method) {
|
||||
this.set_env("method", method);
|
||||
|
||||
if (env.method == "relay_mysql_query") {
|
||||
this.set_default_env({
|
||||
"mysql_hostname": "localhost",
|
||||
"mysql_username": "root",
|
||||
"mysql_password": null,
|
||||
"mysql_database": null,
|
||||
"mysql_port": "3306",
|
||||
"mysql_charset": "utf8"
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.exec = function() {
|
||||
var args = arguments;
|
||||
|
||||
if (env.method == "relay_mysql_query") {
|
||||
var query = arguments.join(' ');
|
||||
rpc.invoke(env.method, {
|
||||
"hostname": env.mysql_hostname,
|
||||
"username": env.mysql_username,
|
||||
"password": env.mysql_password,
|
||||
"database": env.mysql_database,
|
||||
"port": env.mysql_port,
|
||||
"charset": env.mysql_charset,
|
||||
"query": query
|
||||
}, null);
|
||||
return;
|
||||
}
|
||||
|
||||
rpc.invoke(env.method, {}, null);
|
||||
}
|
||||
}
|
||||
|
||||
function create(url) {
|
||||
return new Caterpillar(url);
|
||||
}
|
||||
|
||||
exports.create = create;
|
||||
|
||||
exports.VERSIONINFO = "Caterpillar Proxy Integration (caterpillar.js) version 0.1.1";
|
||||
exports.AUTHOR = "abuse@catswords.net";
|
||||
exports.global = global;
|
||||
exports.require = global.require;
|
|
@ -3,47 +3,9 @@
|
|||
// https://github.com/gnh1201/welsonjs
|
||||
var HTTP = require("lib/http");
|
||||
|
||||
function encode(method, params, id) {
|
||||
var data = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": method,
|
||||
"params": params,
|
||||
"id": id
|
||||
};
|
||||
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
|
||||
function resultEncode(result, id) {
|
||||
var data = {
|
||||
"jsonrpc": "2.0",
|
||||
"result": result
|
||||
"id": id
|
||||
};
|
||||
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
|
||||
function errorEncode(error, id) {
|
||||
var data = {
|
||||
"jsonrpc": "2.0",
|
||||
"error": error,
|
||||
"id": id
|
||||
}
|
||||
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
|
||||
// See also: https://github.com/gnh1201/caterpillar
|
||||
function JsonRpcObject() {
|
||||
this.url = "http://localhost:5555";
|
||||
|
||||
this.setUrl = function(url) {
|
||||
this.url = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
this.call = function(method, params, id) {
|
||||
function jsonrpc2(url) {
|
||||
this.url = url;
|
||||
this.invoke = function(method, params, id) {
|
||||
var result;
|
||||
var response = HTTP.create("MSXML")
|
||||
.setContentType("application/json")
|
||||
|
@ -67,16 +29,23 @@ function JsonRpcObject() {
|
|||
}
|
||||
}
|
||||
|
||||
function create() {
|
||||
return new JsonRpcObject();
|
||||
function encode(method, params, id) {
|
||||
return JSON.stringify({
|
||||
"jsonrpc": "2.0",
|
||||
"method": method,
|
||||
"params": params,
|
||||
"id": id
|
||||
});
|
||||
}
|
||||
|
||||
function create(url) {
|
||||
return new jsonrpc2(url);
|
||||
}
|
||||
|
||||
exports.encode = encode;
|
||||
exports.resultEncode = resultEncode;
|
||||
exports.errorEncode = errorEncode;
|
||||
exports.create = create;
|
||||
|
||||
exports.VERSIONINFO = "JSON-RPC 2.0 Interface (jsonrpc2.js) version 0.1";
|
||||
exports.VERSIONINFO = "JSON-RPC 2.0 Interface (jsonrpc2.js) version 0.1.2";
|
||||
exports.AUTHOR = "abuse@catswords.net";
|
||||
exports.global = global;
|
||||
exports.require = global.require;
|
||||
|
|
Loading…
Reference in New Issue
Block a user