dbgate-api 4.4.3 → 4.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ const fs = require('fs');
2
2
  const os = require('os');
3
3
  const path = require('path');
4
4
  const processArgs = require('./processArgs');
5
+ const isElectron = require('is-electron');
5
6
 
6
7
  const platform = process.env.OS_OVERRIDE ? process.env.OS_OVERRIDE : process.platform;
7
8
  const isWindows = platform === 'win32';
@@ -10,6 +11,7 @@ const isLinux = platform === 'linux';
10
11
  const isDocker = fs.existsSync('/home/dbgate-docker/public');
11
12
  const isDevMode = process.env.DEVMODE == '1';
12
13
  const isNpmDist = !!global['dbgateApiModulePath'];
14
+ const isForkedApi = processArgs.isForkedApi;
13
15
 
14
16
  // function moduleAvailable(name) {
15
17
  // try {
@@ -20,14 +22,14 @@ const isNpmDist = !!global['dbgateApiModulePath'];
20
22
  // }
21
23
  // }
22
24
 
23
- const isElectronBundle = processArgs.isElectronBundle;
24
-
25
25
  const platformInfo = {
26
26
  isWindows,
27
27
  isMac,
28
28
  isLinux,
29
29
  isDocker,
30
- isElectronBundle,
30
+ isElectronBundle: isElectron() && !isDevMode,
31
+ isForkedApi,
32
+ isElectron: isElectron(),
31
33
  isDevMode,
32
34
  isNpmDist,
33
35
  isSnap: process.env.ELECTRON_SNAP == 'true',
@@ -7,15 +7,17 @@ function getNamedArg(name) {
7
7
  }
8
8
 
9
9
  const checkParent = process.argv.includes('--checkParent');
10
- const dynport = process.argv.includes('--dynport');
11
- const nativeModules = getNamedArg('--native-modules');
12
10
  const startProcess = getNamedArg('--start-process');
13
- const isElectronBundle = process.argv.includes('--is-electron-bundle');
11
+ const isForkedApi = process.argv.includes('--is-forked-api');
12
+
13
+ function getPassArgs() {
14
+ if (global['NATIVE_MODULES']) return ['--native-modules', global['NATIVE_MODULES']];
15
+ return [];
16
+ }
14
17
 
15
18
  module.exports = {
16
19
  checkParent,
17
- nativeModules,
18
20
  startProcess,
19
- dynport,
20
- isElectronBundle,
21
+ isForkedApi,
22
+ getPassArgs,
21
23
  };
@@ -1,5 +1,6 @@
1
1
  const _ = require('lodash');
2
2
  const requirePlugin = require('../shell/requirePlugin');
3
+ const { pickSafeConnectionInfo } = require('./crypting');
3
4
 
4
5
  /** @returns {import('dbgate-types').EngineDriver} */
5
6
  function requireEngineDriver(connection) {
@@ -10,14 +11,14 @@ function requireEngineDriver(connection) {
10
11
  engine = connection.engine;
11
12
  }
12
13
  if (!engine) {
13
- throw new Error('Could not get driver from connection');
14
+ throw new Error(`Could not get driver from connection ${JSON.stringify(pickSafeConnectionInfo(connection))}`);
14
15
  }
15
16
  if (engine.includes('@')) {
16
17
  const [shortName, packageName] = engine.split('@');
17
18
  const plugin = requirePlugin(packageName);
18
19
  return plugin.drivers.find(x => x.engine == engine);
19
20
  }
20
- throw new Error(`Could not found engine driver ${engine}`);
21
+ throw new Error(`Could not find engine driver ${engine}`);
21
22
  }
22
23
 
23
24
  module.exports = requireEngineDriver;
@@ -1,19 +1,29 @@
1
- let socket = null;
1
+ let sseResponse = null;
2
+ let electronSender = null;
3
+ let init = '';
2
4
 
3
5
  module.exports = {
4
- set(value) {
5
- socket = value;
6
+ setSseResponse(value) {
7
+ sseResponse = value;
6
8
  },
7
- get() {
8
- return socket;
9
+ setElectronSender(value) {
10
+ electronSender = value;
9
11
  },
10
12
  emit(message, data) {
11
- // console.log('EMIT:', message, data);
12
- socket.emit(message, data);
13
+ if (electronSender) {
14
+ electronSender.send(message, data == null ? null : data);
15
+ } else if (sseResponse) {
16
+ if (init) {
17
+ sseResponse.write(init);
18
+ init = '';
19
+ }
20
+ sseResponse.write(`event: ${message}\ndata: ${JSON.stringify(data == null ? null : data)}\n\n`);
21
+ } else {
22
+ init += sseResponse;
23
+ }
13
24
  },
14
25
  emitChanged(key) {
15
- // console.log('EMIT_CHANGED:', key);
16
- socket.emit('clean-cache', key);
17
- socket.emit(key);
26
+ this.emit('clean-cache', key);
27
+ this.emit(key);
18
28
  },
19
29
  };
@@ -4,7 +4,7 @@ const express = require('express');
4
4
  /**
5
5
  * @param {string} route
6
6
  */
7
- module.exports = function useController(app, route, controller) {
7
+ module.exports = function useController(app, electron, route, controller) {
8
8
  const router = express.Router();
9
9
 
10
10
  if (controller._init) {
@@ -23,24 +23,39 @@ module.exports = function useController(app, route, controller) {
23
23
  const meta = controller[`${key}_meta`];
24
24
  if (!meta) continue;
25
25
 
26
- let method = 'get';
26
+ const routeAction = `/${_.kebabCase(key)}`;
27
+
28
+ if (electron) {
29
+ if (meta === true) {
30
+ const handler = `${route.substring(1)}-${_.kebabCase(key)}`;
31
+ // console.log('REGISTERING HANDLER', handler);
32
+ electron.ipcMain.handle(handler, async (event, args) => {
33
+ const data = await controller[key](args);
34
+ // console.log('HANDLED API', handler, data);
35
+ return data;
36
+ });
37
+ }
38
+
39
+ continue;
40
+ }
41
+
42
+ let method = 'post';
27
43
  let raw = false;
28
44
  let rawParams = false;
29
45
 
30
- if (_.isString(meta)) {
31
- method = meta;
32
- }
46
+ // if (_.isString(meta)) {
47
+ // method = meta;
48
+ // }
33
49
  if (_.isPlainObject(meta)) {
34
50
  method = meta.method;
35
51
  raw = meta.raw;
36
52
  rawParams = meta.rawParams;
37
53
  }
38
54
 
39
- const route = `/${_.kebabCase(key)}`;
40
55
  if (raw) {
41
- router[method](route, controller[key]);
56
+ router[method](routeAction, controller[key]);
42
57
  } else {
43
- router[method](route, async (req, res) => {
58
+ router[method](routeAction, async (req, res) => {
44
59
  // if (controller._init && !controller._init_called) {
45
60
  // await controller._init();
46
61
  // controller._init_called = true;
@@ -58,5 +73,7 @@ module.exports = function useController(app, route, controller) {
58
73
  }
59
74
  }
60
75
 
61
- app.use(route, router);
76
+ if (app) {
77
+ app.use(route, router);
78
+ }
62
79
  };