@ones-op/node-host
Version:
ONES Open Platform Node.js plugin host
86 lines (85 loc) • 2.73 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHashJSON = exports.readPartialFile = exports.toNumber = exports.getWorkerSpacePath = exports.getCodeInfo = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const config_1 = require("./config");
const hash_json_1 = __importDefault(require("./hash.json"));
/**
* 获取插件后端 dist 代码以及路径名称
*/
function getCodeInfo() {
let pluginPath;
if (config_1.config.cli.debug_mode) {
pluginPath = './';
}
else {
pluginPath = config_1.config.plugin.path_to_plugin;
}
const index = config_1.config.plugin.backend_index[0] !== '/'
? path_1.default.join(pluginPath, config_1.config.plugin.backend_index)
: config_1.config.plugin.backend_index;
const code = fs_1.default.readFileSync(index, 'utf-8');
const filename = `file:///${index}`;
return {
filename,
path: pluginPath,
code,
};
}
exports.getCodeInfo = getCodeInfo;
/**
* 获取插件资源路径
export function getPluginPath(
organizationUUID: string,
teamUUID: string,
applicationID: string,
applicationVersion: string
) {
const basePath = config.platform.path_to_plugin
return path.join(basePath, organizationUUID, teamUUID, applicationID, applicationVersion)
}
*/
function getWorkerSpacePath(plugin) {
return config_1.config.cli.debug_mode ? './workspace' : plugin.OpkWorkSpacePath;
}
exports.getWorkerSpacePath = getWorkerSpacePath;
function toNumber(num) {
if (typeof num === 'number') {
return num;
}
else if (typeof num === 'string') {
return Number(num);
}
else {
return num.toNumber();
}
}
exports.toNumber = toNumber;
function readPartialFile(path, offset, length) {
return new Promise((resolve, reject) => {
const buffer = Buffer.alloc(length);
fs_1.default.open(path, 'r', (err, fd) => {
if (err)
return reject(err);
fs_1.default.read(fd, buffer, 0, length, offset, (err, bytesRead, buffer) => {
if (err)
return reject(err);
fs_1.default.close(fd, (err) => {
if (err)
return reject(err);
resolve(buffer.slice(0, bytesRead));
});
});
});
});
}
exports.readPartialFile = readPartialFile;
function getHashJSON() {
const json = hash_json_1.default;
return json;
}
exports.getHashJSON = getHashJSON;