@opengis/fastify-table 1.2.25 → 1.2.26
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.
package/package.json
CHANGED
|
@@ -5,13 +5,15 @@ types.setTypeParser(1082, (stringValue) => stringValue);
|
|
|
5
5
|
types.setTypeParser(1114, (stringValue) => stringValue);
|
|
6
6
|
|
|
7
7
|
import config from '../../../../config.js';
|
|
8
|
+
import dblist from '../../../../dblist.js';
|
|
8
9
|
import pgClients from '../pgClients.js';
|
|
9
10
|
import init from './init.js';
|
|
10
11
|
|
|
11
12
|
function getPG(param) {
|
|
13
|
+
const dbListParams = dblist.find(el => el.database === (param?.db || param?.database || param));
|
|
12
14
|
const {
|
|
13
15
|
user, password, host, port, db, database, name: origin,
|
|
14
|
-
} = param || {};
|
|
16
|
+
} = dbListParams || param || {};
|
|
15
17
|
const name = origin || db || database || param || 'client';
|
|
16
18
|
if (pgClients[name]) return pgClients[name];
|
|
17
19
|
|
|
@@ -2,15 +2,18 @@ import pg from 'pg';
|
|
|
2
2
|
|
|
3
3
|
const { types } = pg;
|
|
4
4
|
types.setTypeParser(1082, (stringValue) => stringValue);
|
|
5
|
+
types.setTypeParser(1114, (stringValue) => stringValue);
|
|
5
6
|
|
|
6
7
|
import config from '../../../../config.js';
|
|
8
|
+
import dblist from '../../../../dblist.js';
|
|
7
9
|
import pgClients from '../pgClients.js';
|
|
8
10
|
import init from './init.js';
|
|
9
11
|
|
|
10
12
|
async function getPGAsync(param) {
|
|
13
|
+
const dbListParams = dblist.find(el => el.database === (param?.db || param?.database || param));
|
|
11
14
|
const {
|
|
12
15
|
user, password, host, port, db, database, name: origin,
|
|
13
|
-
} = typeof param === 'string' ? { db: param } : param || {};
|
|
16
|
+
} = dbListParams ?? (typeof param === 'string' ? { db: param } : param || {});
|
|
14
17
|
const name = origin || db || database || param || 'client';
|
|
15
18
|
|
|
16
19
|
if (pgClients[name]?.tlist) return pgClients[name];
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import pgClients from './pgClients.js';
|
|
2
|
-
import
|
|
3
|
-
import getPG from './funcs/getPG.js';
|
|
2
|
+
import getPGAsync from './funcs/getPGAsync.js';
|
|
4
3
|
|
|
5
4
|
function close() {
|
|
6
5
|
Object.keys(pgClients).forEach((el) => {
|
|
@@ -8,16 +7,29 @@ function close() {
|
|
|
8
7
|
});
|
|
9
8
|
}
|
|
10
9
|
|
|
10
|
+
async function getHeadersPG(req, config) {
|
|
11
|
+
if (!req.headers?.token || !req.headers?.referer) return null;
|
|
12
|
+
const refererHost = req.headers?.referer?.match?.(/(https?:\/\/[^\/]+)/g)?.[0];
|
|
13
|
+
const validToken = (req.ip === '127.0.0.1' || config.debug) && req.headers?.uid && req.headers?.token && refererHost && config.auth?.tokens?.[refererHost] === req.headers?.token;
|
|
14
|
+
|
|
15
|
+
if (validToken && req.headers?.db) {
|
|
16
|
+
const pg = pgClients[req.headers.db]
|
|
17
|
+
|| await getPGAsync(req.headers.db);
|
|
18
|
+
return pg;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
11
22
|
async function plugin(fastify, config) {
|
|
12
|
-
const client =
|
|
13
|
-
|
|
23
|
+
const client = await getPGAsync({ ...config.pg || {}, name: 'client' });
|
|
24
|
+
|
|
14
25
|
fastify.addHook('onRequest', async (req) => {
|
|
15
|
-
|
|
26
|
+
const headersPG = await getHeadersPG(req, config);
|
|
27
|
+
req.pg = headersPG || req.pg || client;
|
|
28
|
+
if (headersPG) {
|
|
29
|
+
req.user = { uid: req.headers?.uid };
|
|
30
|
+
}
|
|
16
31
|
});
|
|
17
32
|
|
|
18
|
-
// fastify.decorate('autoIndex', autoIndex);
|
|
19
|
-
// fastify.decorate('getMeta', getMeta);
|
|
20
|
-
// fastify.decorate('getPG', getPG);
|
|
21
33
|
fastify.addHook('onClose', close);
|
|
22
34
|
}
|
|
23
35
|
|