yao-node-client
Version:
A node client for yao application development
318 lines • 11.2 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Process = void 0;
const localcall_1 = require("./localcall");
const request_1 = __importDefault(require("./request"));
// import fs from "fs";
const path_1 = __importDefault(require("path"));
const array_1 = require("./array");
// export function CheckIsLocalProcessFilePath(name: string) {
// let paths = name.split(".");
// //有一些内部的process,比如Concat
// if (!paths || !paths.length) {
// return false;
// }
// if (!["scripts"].includes(paths[0])) {
// //不代理
// return false;
// }
// if (paths.length < 2) {
// throw Error("错误的流程名称");
// }
// const tokens = paths.splice(-1, 1);
// const method = tokens[0];
// const fname = paths.join(path.sep);
// // console.log(tokens, paths);
// let filePath = `dist/app/${fname}.js`;
// let fpath = path.resolve(filePath);
// if (!fs.existsSync(fpath)) {
// filePath = `dist/app/${fname}/index.js`;
// fpath = path.resolve(filePath);
// if (!fs.existsSync(fpath)) {
// return false;
// }
// }
// return { fpath, method };
// }
/**
* YAO Process处理器代理
* @param {string} method 处理器名称
* @param {...any} args 参数
* @returns
*/
function Process(method, ...args) {
let processName = method.toLowerCase();
if (["xiang.sys.print", "xiang.helper.print", "utils.fmt.print"].includes(processName)) {
console.log(JSON.stringify(args));
return;
}
if (["utils.str.concat", "xiang.helper.strconcat"].includes(processName)) {
return args.join("");
}
if ("utils.str.join" == processName) {
return args[0].join(args[2]);
}
if ("utils.str.joinpath" == processName) {
return path_1.default.join(...args);
}
if (["xiang.flow.sleep", "xiang.sys.sleep", "yao.sys.sleep"].includes(processName)) {
var waitTill = new Date(new Date().getTime() + args[0]);
while (waitTill > new Date()) { }
}
// encoding
if (method.startsWith("encoding.")) {
return processEncoding(method, ...args);
}
// Time
if (method.startsWith("utils.now")) {
return processTime(method, ...args);
}
// Tree
if (method == "utils.tree.Flatten") {
return processFlatten(...args);
}
// Map
if (method.startsWith("utils.map") || method.startsWith("xiang.helper.map")) {
return processMap(method, ...args);
}
// Array
if (method.startsWith("utils.arr") ||
method.startsWith("xiang.helper.array")) {
return processArry(method, ...args);
}
if (method.startsWith("scripts.") ||
method.startsWith("studio.") ||
method.startsWith("services.") ||
method.startsWith("widgets.")) {
let fname = (0, localcall_1.GetFileName)(method);
if (fname) {
return (0, localcall_1.CallLocalProcess)(fname, method, ...args);
}
}
return (0, request_1.default)({ type: "Process", method: method, args });
}
exports.Process = Process;
// Convert a hex string to a byte array
function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2) {
bytes.push(parseInt(hex.slice(c, c + 2), 16));
}
return bytes;
}
// Convert a byte array to a hex string
function bytesToHex(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
const current = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
hex.push((current >>> 4).toString(16));
hex.push((current & 0xf).toString(16));
}
return hex.join("");
}
function processEncoding(method, ...args) {
const process = method.toLowerCase();
if ("encoding.hex.encode" == process) {
return bytesToHex(args[0]);
}
if ("encoding.hex.decode" == process) {
return hexToBytes(args[0]);
}
if ("encoding.json.encode" == process) {
return JSON.stringify(args[0]);
}
if ("encoding.json.decode" == process) {
return JSON.parse(args[0]);
}
if ("encoding.base64.encode" == process) {
let buff = Buffer.from(args[0]);
return buff.toString("base64");
}
if ("encoding.base64.decode" == process) {
let buff = Buffer.from(args[0], "base64");
return buff.toString();
}
return (0, request_1.default)({ type: "Process", method: method, args });
}
function processTime(method, ...args) {
const process = method.toLowerCase();
if ("utils.now.date" == process) {
let dateBj = new Date();
const offset = dateBj.getTimezoneOffset();
dateBj = new Date(dateBj.getTime() - offset * 60 * 1000);
return dateBj.toISOString().split("T")[0];
}
if ("utils.now.datetime" == process) {
let dateBj = new Date();
const offset = dateBj.getTimezoneOffset();
dateBj = new Date(dateBj.getTime() - offset * 60 * 1000);
return dateBj.toISOString().slice(0, 19).replace("T", " ");
}
if ("utils.now.time" == process) {
let dateBj = new Date();
const offset = dateBj.getTimezoneOffset();
dateBj = new Date(dateBj.getTime() - offset * 60 * 1000);
return dateBj.toLocaleTimeString();
}
if ("utils.now.timestamp" == process) {
return new Date().getTime() / 1000;
}
if ("utils.now.timestampms" == process) {
return new Date().getTime();
}
return (0, request_1.default)({ type: "Process", method: method, args });
}
function processMap(method, ...args) {
let process = method.toLowerCase();
//参数1是object,参数2是key
if (["xiang.helper.mapget", "utils.map.get"].includes(process)) {
return args[0][args[1]];
}
//参数1是object,参数2是key,参数3是值
if (["xiang.helper.mapset", "utils.map.set"].includes(process)) {
args[0][args[1]] = args[2];
return args[0];
}
//参数1是object,参数2是key
if (["xiang.helper.mapdel", "utils.map.del", "utils.map.delmany"].includes(process)) {
let [record, ...keys] = args;
if (Array.isArray(keys)) {
for (const key of keys) {
delete record[key];
}
return record;
}
}
//参数1是object
if (["xiang.helper.mapkeys", "utils.map.keys"].includes(process)) {
return Object.keys(args[0]);
}
//参数1是object
if (["xiang.helper.mapvalues", "utils.map.values"].includes(process)) {
return Object.values(args[0]);
}
if (["xiang.helper.maptoarray", "utils.map.array"].includes(process)) {
let res = [];
for (const key in args[0]) {
res.push({
key: key,
value: args[0][key],
});
}
return res;
}
return (0, request_1.default)({ type: "Process", method: method, args });
}
function processArry(method, ...args) {
let process = method.toLowerCase();
if (["xiang.helper.arrayget", "utils.arr.get"].includes(process)) {
if (!Array.isArray(args[0])) {
throw Error("参数1必须是数组");
}
return args[0][args[1]];
}
if (["xiang.helper.arrayindexes", "utils.arr.indexes"].includes(process)) {
if (!Array.isArray(args[0])) {
throw Error("参数1必须是数组");
}
return args[0].map((_v, indx) => indx);
}
//将多个数据记录集合,合并为一个数据记录集合
if (["xiang.helper.arraypluck", "utils.arr.pluck"].includes(process)) {
if (!Array.isArray(args[0])) {
throw Error("参数1必须是数组");
}
return (0, array_1.arrayPluck)(args[0], args[1]);
}
//将多条数记录集合,分解为一个 columns:[]string 和 values: [][]interface{}
if (["xiang.helper.arraysplit", "utils.arr.split"].includes(process)) {
if (!Array.isArray(args[0])) {
throw Error("参数1必须是数组");
}
return (0, array_1.arraySplit)(args[0]);
}
if (["xiang.helper.arraycolumn", "utils.arr.column"].includes(process)) {
if (!Array.isArray(args[0])) {
throw Error("参数1必须是数组");
}
return (0, array_1.arrayColumn)(args[0], args[1]);
}
if (["xiang.helper.arraykeep", "utils.arr.keep"].includes(process)) {
if (!Array.isArray(args[0])) {
throw Error("参数1必须是数组");
}
return (0, array_1.arrayKeep)(args[0], args[1]);
}
if (["xiang.helper.arraytree", "utils.arr.tree"].includes(process)) {
if (!Array.isArray(args[0])) {
throw Error("参数1必须是数组");
}
return (0, array_1.ArrayTree)(args[0], args[1]);
}
if (["xiang.helper.arrayunique", "utils.arr.unique"].includes(process)) {
if (args.length !== 1) {
throw new Error("参数错误");
}
if (Array.isArray(args[0])) {
return (0, array_1.arrayUnique)(args[0]);
}
return args[0];
}
if (["xiang.helper.arraymapset", "utils.arr.mapset"].includes(process)) {
const arr = args[0];
if (arr) {
return (0, array_1.ArrayMapSet)(arr, args[1], args[2]);
}
else {
const arr2 = args[0];
if (arr2) {
return (0, array_1.ArrayMapSetMapStr)(arr2, args[1], args[2]);
}
}
return args[0];
}
return (0, request_1.default)({ type: "Process", method: method, args });
}
// ProcessFlatten utils.tree.Flatten cast to array
function processFlatten(...args) {
if (args.length < 1) {
throw new Error("参数错误");
}
const array = args[0];
const option = args[1] || {};
if (!option.hasOwnProperty("primary")) {
option.primary = "id";
}
if (!option.hasOwnProperty("children")) {
option.children = "children";
}
if (!option.hasOwnProperty("parent")) {
option.parent = "parent";
}
return flatten(array, option, null);
}
function flatten(array, option, id) {
const parent = `${option.parent}`;
const primary = `${option.primary}`;
const childrenField = `${option.children}`;
const res = [];
for (const v of array) {
const row = v;
if (!row) {
continue;
}
if (!(row instanceof Object)) {
continue;
}
row[parent] = id;
const children = row[childrenField];
delete row[childrenField];
res.push(row);
if (children) {
res.push(...flatten(children, option, row[primary]));
}
}
return res;
}
//# sourceMappingURL=process.js.map