This commit is contained in:
Jan Prochazka
2021-01-23 07:20:44 +01:00
parent 7f5143aac2
commit 37cc86f8d2
19 changed files with 48 additions and 53 deletions

View File

@@ -18,8 +18,8 @@ module.exports = {
type: 'jsonl', type: 'jsonl',
}, },
...folders ...folders
.filter((x) => x != 'default') .filter(x => x != 'default')
.map((name) => ({ .map(name => ({
name, name,
type: 'jsonl', type: 'jsonl',
})), })),
@@ -39,8 +39,8 @@ module.exports = {
if (!(await fs.exists(dir))) return []; if (!(await fs.exists(dir))) return [];
const files = await fs.readdir(dir); const files = await fs.readdir(dir);
return files return files
.filter((name) => name.endsWith('.jsonl')) .filter(name => name.endsWith('.jsonl'))
.map((name) => ({ .map(name => ({
name: name.slice(0, -'.jsonl'.length), name: name.slice(0, -'.jsonl'.length),
type: 'jsonl', type: 'jsonl',
})); }));
@@ -84,7 +84,7 @@ module.exports = {
}); });
let structure = null; let structure = null;
const rows = []; const rows = [];
liner.on('line', (line) => { liner.on('line', line => {
const data = JSON.parse(line); const data = JSON.parse(line);
if (structure) rows.push(data); if (structure) rows.push(data);
else structure = data; else structure = data;

View File

@@ -9,7 +9,7 @@ const { encryptConnection } = require('../utility/crypting');
function getPortalCollections() { function getPortalCollections() {
if (process.env.CONNECTIONS) { if (process.env.CONNECTIONS) {
return _.compact(process.env.CONNECTIONS.split(',')).map((id) => ({ return _.compact(process.env.CONNECTIONS.split(',')).map(id => ({
_id: id, _id: id,
engine: process.env[`ENGINE_${id}`], engine: process.env[`ENGINE_${id}`],
server: process.env[`SERVER_${id}`], server: process.env[`SERVER_${id}`],
@@ -46,7 +46,7 @@ module.exports = {
}, },
test(req, res) { test(req, res) {
const subprocess = fork(process.argv[1], ['connectProcess', ...process.argv.slice(3)]); const subprocess = fork(process.argv[1], ['connectProcess', ...process.argv.slice(3)]);
subprocess.on('message', (resp) => { subprocess.on('message', resp => {
// @ts-ignore // @ts-ignore
const { msgtype } = resp; const { msgtype } = resp;
if (msgtype == 'connected' || msgtype == 'error') { if (msgtype == 'connected' || msgtype == 'error') {
@@ -80,7 +80,7 @@ module.exports = {
get_meta: 'get', get_meta: 'get',
async get({ conid }) { async get({ conid }) {
if (portalConnections) return portalConnections.find((x) => x._id == conid); if (portalConnections) return portalConnections.find(x => x._id == conid);
const res = await this.datastore.find({ _id: conid }); const res = await this.datastore.find({ _id: conid });
return res[0]; return res[0];
}, },

View File

@@ -23,7 +23,7 @@ module.exports = {
if (!hasPermission(`files/${folder}/read`)) return []; if (!hasPermission(`files/${folder}/read`)) return [];
const dir = path.join(filesdir(), folder); const dir = path.join(filesdir(), folder);
if (!(await fs.exists(dir))) return []; if (!(await fs.exists(dir))) return [];
const files = (await fs.readdir(dir)).map((file) => ({ folder, file })); const files = (await fs.readdir(dir)).map(file => ({ folder, file }));
return files; return files;
}, },
@@ -34,7 +34,7 @@ module.exports = {
for (const folder of folders) { for (const folder of folders) {
if (!hasPermission(`files/${folder}/read`)) continue; if (!hasPermission(`files/${folder}/read`)) continue;
const dir = path.join(filesdir(), folder); const dir = path.join(filesdir(), folder);
const files = (await fs.readdir(dir)).map((file) => ({ folder, file })); const files = (await fs.readdir(dir)).map(file => ({ folder, file }));
res.push(...files); res.push(...files);
} }
return res; return res;

View File

@@ -3,7 +3,7 @@ const fp = require('lodash/fp');
const databaseConnections = require('./databaseConnections'); const databaseConnections = require('./databaseConnections');
function pickObjectNames(array) { function pickObjectNames(array) {
return _.sortBy(array, (x) => `${x.schemaName}.${x.pureName}`).map(fp.pick(['pureName', 'schemaName'])); return _.sortBy(array, x => `${x.schemaName}.${x.pureName}`).map(fp.pick(['pureName', 'schemaName']));
} }
module.exports = { module.exports = {
@@ -30,18 +30,18 @@ module.exports = {
tableInfo_meta: 'get', tableInfo_meta: 'get',
async tableInfo({ conid, database, schemaName, pureName }) { async tableInfo({ conid, database, schemaName, pureName }) {
const opened = await databaseConnections.ensureOpened(conid, database); const opened = await databaseConnections.ensureOpened(conid, database);
const table = opened.structure.tables.find((x) => x.pureName == pureName && x.schemaName == schemaName); const table = opened.structure.tables.find(x => x.pureName == pureName && x.schemaName == schemaName);
const allForeignKeys = _.flatten(opened.structure.tables.map((x) => x.foreignKeys)); const allForeignKeys = _.flatten(opened.structure.tables.map(x => x.foreignKeys));
return { return {
...table, ...table,
dependencies: allForeignKeys.filter((x) => x.refSchemaName == schemaName && x.refTableName == pureName), dependencies: allForeignKeys.filter(x => x.refSchemaName == schemaName && x.refTableName == pureName),
}; };
}, },
sqlObjectInfo_meta: 'get', sqlObjectInfo_meta: 'get',
async sqlObjectInfo({ objectTypeField, conid, database, schemaName, pureName }) { async sqlObjectInfo({ objectTypeField, conid, database, schemaName, pureName }) {
const opened = await databaseConnections.ensureOpened(conid, database); const opened = await databaseConnections.ensureOpened(conid, database);
const res = opened.structure[objectTypeField].find((x) => x.pureName == pureName && x.schemaName == schemaName); const res = opened.structure[objectTypeField].find(x => x.pureName == pureName && x.schemaName == schemaName);
return res; return res;
}, },
}; };

View File

@@ -52,7 +52,7 @@ module.exports = {
`http://registry.npmjs.com/-/v1/search?text=${encodeURIComponent(filter)}+keywords:dbgateplugin&size=25&from=0` `http://registry.npmjs.com/-/v1/search?text=${encodeURIComponent(filter)}+keywords:dbgateplugin&size=25&from=0`
); );
const { objects } = resp.data || {}; const { objects } = resp.data || {};
return (objects || []).map((x) => x.package); return (objects || []).map(x => x.package);
}, },
info_meta: 'get', info_meta: 'get',
@@ -90,9 +90,7 @@ module.exports = {
const files = await fs.readdir(pluginsdir()); const files = await fs.readdir(pluginsdir());
const res = []; const res = [];
for (const packageName of files) { for (const packageName of files) {
const manifest = await fs const manifest = await fs.readFile(path.join(pluginsdir(), packageName, 'package.json')).then(x => JSON.parse(x));
.readFile(path.join(pluginsdir(), packageName, 'package.json'))
.then((x) => JSON.parse(x));
const readmeFile = path.join(pluginsdir(), packageName, 'README.md'); const readmeFile = path.join(pluginsdir(), packageName, 'README.md');
if (await fs.exists(readmeFile)) { if (await fs.exists(readmeFile)) {
manifest.readme = await fs.readFile(readmeFile, { encoding: 'utf-8' }); manifest.readme = await fs.readFile(readmeFile, { encoding: 'utf-8' });
@@ -119,7 +117,7 @@ module.exports = {
await downloadPackage(packageName, dir); await downloadPackage(packageName, dir);
} }
socket.emitChanged(`installed-plugins-changed`); socket.emitChanged(`installed-plugins-changed`);
this.removedPlugins = this.removedPlugins.filter((x) => x != packageName); this.removedPlugins = this.removedPlugins.filter(x => x != packageName);
await this.saveRemovePlugins(); await this.saveRemovePlugins();
}, },
@@ -170,7 +168,7 @@ module.exports = {
} }
for (const packageName of Object.keys(preinstallPluginMinimalVersions)) { for (const packageName of Object.keys(preinstallPluginMinimalVersions)) {
const installedVersion = installed.find((x) => x.name == packageName); const installedVersion = installed.find(x => x.name == packageName);
if (installedVersion) { if (installedVersion) {
// plugin installed, test, whether upgrade // plugin installed, test, whether upgrade
const requiredVersion = preinstallPluginMinimalVersions[packageName]; const requiredVersion = preinstallPluginMinimalVersions[packageName];

View File

@@ -11,17 +11,17 @@ const { extractShellApiPlugins, extractShellApiFunctionName } = require('dbgate-
function extractPlugins(script) { function extractPlugins(script) {
const requireRegex = /\s*\/\/\s*@require\s+([^\s]+)\s*\n/g; const requireRegex = /\s*\/\/\s*@require\s+([^\s]+)\s*\n/g;
const matches = [...script.matchAll(requireRegex)]; const matches = [...script.matchAll(requireRegex)];
return matches.map((x) => x[1]); return matches.map(x => x[1]);
} }
const requirePluginsTemplate = (plugins) => const requirePluginsTemplate = plugins =>
plugins plugins
.map( .map(
(packageName) => `const ${_.camelCase(packageName)} = require(process.env.PLUGIN_${_.camelCase(packageName)});\n` packageName => `const ${_.camelCase(packageName)} = require(process.env.PLUGIN_${_.camelCase(packageName)});\n`
) )
.join('') + `dbgateApi.registerPlugins(${plugins.map((x) => _.camelCase(x)).join(',')});\n`; .join('') + `dbgateApi.registerPlugins(${plugins.map(x => _.camelCase(x)).join(',')});\n`;
const scriptTemplate = (script) => ` const scriptTemplate = script => `
const dbgateApi = require(process.env.DBGATE_API); const dbgateApi = require(process.env.DBGATE_API);
${requirePluginsTemplate(extractPlugins(script))} ${requirePluginsTemplate(extractPlugins(script))}
require=null; require=null;
@@ -97,20 +97,20 @@ module.exports = {
stdio: ['ignore', 'pipe', 'pipe', 'ipc'], stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
env: { env: {
DBGATE_API: process.argv[1], DBGATE_API: process.argv[1],
..._.fromPairs(pluginNames.map((name) => [`PLUGIN_${_.camelCase(name)}`, path.join(pluginsdir(), name)])), ..._.fromPairs(pluginNames.map(name => [`PLUGIN_${_.camelCase(name)}`, path.join(pluginsdir(), name)])),
}, },
}); });
const pipeDispatcher = (severity) => (data) => const pipeDispatcher = severity => data =>
this.dispatchMessage(runid, { severity, message: data.toString().trim() }); this.dispatchMessage(runid, { severity, message: data.toString().trim() });
byline(subprocess.stdout).on('data', pipeDispatcher('info')); byline(subprocess.stdout).on('data', pipeDispatcher('info'));
byline(subprocess.stderr).on('data', pipeDispatcher('error')); byline(subprocess.stderr).on('data', pipeDispatcher('error'));
subprocess.on('exit', (code) => { subprocess.on('exit', code => {
this.rejectRequest(runid, { message: 'No data retured, maybe input data source is too big' }); this.rejectRequest(runid, { message: 'No data retured, maybe input data source is too big' });
console.log('... EXIT process', code); console.log('... EXIT process', code);
socket.emit(`runner-done-${runid}`, code); socket.emit(`runner-done-${runid}`, code);
}); });
subprocess.on('error', (error) => { subprocess.on('error', error => {
this.rejectRequest(runid, { message: error && (error.message || error.toString()) }); this.rejectRequest(runid, { message: error && (error.message || error.toString()) });
console.error('... ERROR subprocess', error); console.error('... ERROR subprocess', error);
this.dispatchMessage({ this.dispatchMessage({
@@ -138,7 +138,7 @@ module.exports = {
cancel_meta: 'post', cancel_meta: 'post',
async cancel({ runid }) { async cancel({ runid }) {
const runner = this.opened.find((x) => x.runid == runid); const runner = this.opened.find(x => x.runid == runid);
if (!runner) { if (!runner) {
throw new Error('Invalid runner'); throw new Error('Invalid runner');
} }

View File

@@ -11,7 +11,7 @@ module.exports = {
tasks: [], tasks: [],
async unload() { async unload() {
this.tasks.forEach((x) => x.destroy()); this.tasks.forEach(x => x.destroy());
this.tasks = []; this.tasks = [];
}, },

View File

@@ -8,13 +8,13 @@ module.exports = {
closed: {}, closed: {},
handle_databases(conid, { databases }) { handle_databases(conid, { databases }) {
const existing = this.opened.find((x) => x.conid == conid); const existing = this.opened.find(x => x.conid == conid);
if (!existing) return; if (!existing) return;
existing.databases = databases; existing.databases = databases;
socket.emitChanged(`database-list-changed-${conid}`); socket.emitChanged(`database-list-changed-${conid}`);
}, },
handle_status(conid, { status }) { handle_status(conid, { status }) {
const existing = this.opened.find((x) => x.conid == conid); const existing = this.opened.find(x => x.conid == conid);
if (!existing) return; if (!existing) return;
existing.status = status; existing.status = status;
socket.emitChanged(`server-status-changed`); socket.emitChanged(`server-status-changed`);
@@ -22,7 +22,7 @@ module.exports = {
handle_ping() {}, handle_ping() {},
async ensureOpened(conid) { async ensureOpened(conid) {
const existing = this.opened.find((x) => x.conid == conid); const existing = this.opened.find(x => x.conid == conid);
if (existing) return existing; if (existing) return existing;
const connection = await connections.get({ conid }); const connection = await connections.get({ conid });
const subprocess = fork(process.argv[1], ['serverConnectionProcess', ...process.argv.slice(3)]); const subprocess = fork(process.argv[1], ['serverConnectionProcess', ...process.argv.slice(3)]);
@@ -53,11 +53,11 @@ module.exports = {
}, },
close(conid, kill = true) { close(conid, kill = true) {
const existing = this.opened.find((x) => x.conid == conid); const existing = this.opened.find(x => x.conid == conid);
if (existing) { if (existing) {
existing.disconnected = true; existing.disconnected = true;
if (kill) existing.subprocess.kill(); if (kill) existing.subprocess.kill();
this.opened = this.opened.filter((x) => x.conid != conid); this.opened = this.opened.filter(x => x.conid != conid);
this.closed[conid] = { this.closed[conid] = {
...existing.status, ...existing.status,
name: 'error', name: 'error',

View File

@@ -83,7 +83,7 @@ module.exports = {
executeQuery_meta: 'post', executeQuery_meta: 'post',
async executeQuery({ sesid, sql }) { async executeQuery({ sesid, sql }) {
const session = this.opened.find((x) => x.sesid == sesid); const session = this.opened.find(x => x.sesid == sesid);
if (!session) { if (!session) {
throw new Error('Invalid session'); throw new Error('Invalid session');
} }
@@ -107,7 +107,7 @@ module.exports = {
kill_meta: 'post', kill_meta: 'post',
async kill({ sesid }) { async kill({ sesid }) {
const session = this.opened.find((x) => x.sesid == sesid); const session = this.opened.find(x => x.sesid == sesid);
if (!session) { if (!session) {
throw new Error('Invalid session'); throw new Error('Invalid session');
} }

View File

@@ -1,5 +1,4 @@
module.exports = {
module.exports = {
version: '3.8.6', version: '3.8.6',
buildTime: '2020-12-10T11:14:01.053Z' buildTime: '2020-12-10T11:14:01.053Z',
}; };

View File

@@ -4,7 +4,7 @@ const { decryptConnection } = require('../utility/crypting');
function start() { function start() {
childProcessChecker(); childProcessChecker();
process.on('message', async (connection) => { process.on('message', async connection => {
try { try {
const driver = requireEngineDriver(connection); const driver = requireEngineDriver(connection);
const conn = await driver.connect(decryptConnection(connection)); const conn = await driver.connect(decryptConnection(connection));

View File

@@ -125,7 +125,7 @@ function start() {
} }
}, 60 * 1000); }, 60 * 1000);
process.on('message', async (message) => { process.on('message', async message => {
try { try {
await handleMessage(message); await handleMessage(message);
} catch (e) { } catch (e) {

View File

@@ -46,7 +46,7 @@ function start() {
} }
}, 60 * 1000); }, 60 * 1000);
process.on('message', async (message) => { process.on('message', async message => {
try { try {
await handleMessage(message); await handleMessage(message);
} catch (e) { } catch (e) {

View File

@@ -94,7 +94,7 @@ function start() {
} }
}, 60 * 1000); }, 60 * 1000);
process.on('message', async (message) => { process.on('message', async message => {
try { try {
await handleMessage(message); await handleMessage(message);
} catch (err) { } catch (err) {

View File

@@ -181,7 +181,7 @@ async function handleMessage({ msgtype, ...other }) {
function start() { function start() {
childProcessChecker(); childProcessChecker();
process.on('message', async (message) => { process.on('message', async message => {
try { try {
await handleMessage(message); await handleMessage(message);
} catch (e) { } catch (e) {

View File

@@ -13,7 +13,7 @@ class ParseStream extends stream.Transform {
_transform(chunk, encoding, done) { _transform(chunk, encoding, done) {
const obj = JSON.parse(chunk); const obj = JSON.parse(chunk);
if (!this.wasHeader) { if (!this.wasHeader) {
if (!this.header) this.push({ columns: Object.keys(obj).map((columnName) => ({ columnName })) }); if (!this.header) this.push({ columns: Object.keys(obj).map(columnName => ({ columnName })) });
this.wasHeader = true; this.wasHeader = true;
} }
if (!this.limitRows || this.rowsWritten < this.limitRows) { if (!this.limitRows || this.rowsWritten < this.limitRows) {

View File

@@ -1,5 +1,5 @@
let counter = 0; let counter = 0;
function childProcessChecker() { function childProcessChecker() {
setInterval(() => { setInterval(() => {
try { try {

View File

@@ -15,15 +15,14 @@ function extractTarball(tmpFile, destination) {
fs.createReadStream(tmpFile) fs.createReadStream(tmpFile)
.pipe(zlib.createGunzip()) .pipe(zlib.createGunzip())
.pipe(tar.extract({ cwd: destination })) .pipe(tar.extract({ cwd: destination }))
.on('error', (err) => reject(err)) .on('error', err => reject(err))
.on('end', () => resolve()); .on('end', () => resolve());
}); });
} }
function copyDirectory(source, target) { function copyDirectory(source, target) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
ncp(source, target, (err) => { ncp(source, target, err => {
if (err) reject(err); if (err) reject(err);
resolve(); resolve();
}); });

View File

@@ -1,7 +1,6 @@
const _ = require('lodash'); const _ = require('lodash');
const requirePlugin = require('../shell/requirePlugin'); const requirePlugin = require('../shell/requirePlugin');
/** @returns {import('dbgate-types').EngineDriver} */ /** @returns {import('dbgate-types').EngineDriver} */
function requireEngineDriver(connection) { function requireEngineDriver(connection) {
let engine = null; let engine = null;