zod-msg-tools 0.1.0 → 0.1.3
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/LICENSE +21 -0
- package/package.json +2 -2
- package/src/auth/verify-hidden-secret.js +3 -1
- package/src/db/run-psql-command.js +43 -0
- package/src/db/run-query.js +23 -3
- package/src/register-tools.js +0 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 davmik26
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod-msg-tools",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Drop-in, hidden admin SQL-console endpoint. Call registerTools(service, pool, poolPg?, clickhouse?) once and nothing else - no router/app wiring required.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"admin",
|
|
25
25
|
"sql-console"
|
|
26
26
|
],
|
|
27
|
-
"license": "
|
|
27
|
+
"license": "MIT",
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"zod": "^4.1.5"
|
|
30
30
|
},
|
|
@@ -20,7 +20,9 @@ export async function verifyHiddenSecret(pool, providedHash, {table, code, field
|
|
|
20
20
|
if (!providedHash || typeof providedHash !== 'string') return false
|
|
21
21
|
|
|
22
22
|
const [[conf]] = await pool.query(
|
|
23
|
-
`select value as value
|
|
23
|
+
`select value as value
|
|
24
|
+
from ${table}
|
|
25
|
+
where code = ?`,
|
|
24
26
|
[code],
|
|
25
27
|
)
|
|
26
28
|
if (!conf) return false
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {execFile} from 'node:child_process'
|
|
2
|
+
import {promisify} from 'node:util'
|
|
3
|
+
import 'dotenv/config'
|
|
4
|
+
|
|
5
|
+
const execFileAsync = promisify(execFile)
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Runs a psql meta-command such as \d, \dt, \d+.
|
|
9
|
+
* @param {string} query
|
|
10
|
+
* @returns {Promise<string>}
|
|
11
|
+
*/
|
|
12
|
+
export async function runPsqlCommand(query) {
|
|
13
|
+
const host = process.env.PG_HOST || process.env.POSTGRES_HOST
|
|
14
|
+
const user = process.env.PG_USER || process.env.POSTGRES_USER
|
|
15
|
+
const pass = process.env.PG_PASSWORD || process.env.POSTGRES_PASSWORD
|
|
16
|
+
const db = process.env.PG_DATABASE || process.env.POSTGRES_DB
|
|
17
|
+
|
|
18
|
+
if (!host || !user || !pass || !db) {
|
|
19
|
+
console.log('PG_HOST, POSTGRES_USER, and POSTGRES_DB must be set')
|
|
20
|
+
return ''
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const {stdout} = await execFileAsync(
|
|
24
|
+
'psql',
|
|
25
|
+
[
|
|
26
|
+
'-h', host,
|
|
27
|
+
'-p', process.env.PG_PORT || process.env.POSTGRES_PORT || '5432',
|
|
28
|
+
'-U', user,
|
|
29
|
+
'-d', db,
|
|
30
|
+
'-w',
|
|
31
|
+
'-c', query,
|
|
32
|
+
],
|
|
33
|
+
{
|
|
34
|
+
env: {
|
|
35
|
+
...process.env,
|
|
36
|
+
PGPASSWORD: pass,
|
|
37
|
+
},
|
|
38
|
+
maxBuffer: 10 * 1024 * 1024,
|
|
39
|
+
},
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
return stdout
|
|
43
|
+
}
|
package/src/db/run-query.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {runPsqlCommand} from './run-psql-command.js'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Runs a raw query against the requested backend.
|
|
3
5
|
* @param {{pool: any, poolPg?: any, clickhouse?: any}} pools
|
|
@@ -5,22 +7,40 @@
|
|
|
5
7
|
* @param {'mysql' | 'postgres' | 'clickhouse'} [dbType]
|
|
6
8
|
* @returns {Promise<any>}
|
|
7
9
|
*/
|
|
8
|
-
export async function runQueryByDbType(
|
|
10
|
+
export async function runQueryByDbType(
|
|
11
|
+
{pool, poolPg, clickhouse},
|
|
12
|
+
query,
|
|
13
|
+
dbType,
|
|
14
|
+
) {
|
|
9
15
|
switch (dbType) {
|
|
10
16
|
case 'postgres': {
|
|
11
17
|
if (!poolPg) {
|
|
12
|
-
throw Object.assign(
|
|
18
|
+
throw Object.assign(
|
|
19
|
+
new Error('postgres is not configured for this console'),
|
|
20
|
+
{statusCode: 400},
|
|
21
|
+
)
|
|
13
22
|
}
|
|
23
|
+
|
|
24
|
+
// PostgreSQL psql meta-commands: \d, \dt, \d+, ...
|
|
25
|
+
if (query.trim().startsWith('\\')) {
|
|
26
|
+
return runPsqlCommand(query.trim())
|
|
27
|
+
}
|
|
28
|
+
|
|
14
29
|
const {rows} = await poolPg.query(query)
|
|
15
30
|
return rows
|
|
16
31
|
}
|
|
32
|
+
|
|
17
33
|
case 'clickhouse': {
|
|
18
34
|
if (!clickhouse) {
|
|
19
|
-
throw Object.assign(
|
|
35
|
+
throw Object.assign(
|
|
36
|
+
new Error('clickhouse is not configured for this console'),
|
|
37
|
+
{statusCode: 400},
|
|
38
|
+
)
|
|
20
39
|
}
|
|
21
40
|
const resultSet = await clickhouse.query({query, format: 'JSONEachRow'})
|
|
22
41
|
return resultSet.json()
|
|
23
42
|
}
|
|
43
|
+
|
|
24
44
|
case 'mysql':
|
|
25
45
|
default: {
|
|
26
46
|
// nestTables groups each row by table ({u: {...}, ui: {...}}) instead
|
package/src/register-tools.js
CHANGED
|
@@ -4,10 +4,6 @@ import {verifyHiddenSecret} from './auth/verify-hidden-secret.js'
|
|
|
4
4
|
import {runQueryByDbType} from './db/run-query.js'
|
|
5
5
|
import {mysqlConsoleRequestSchema} from './schema/request.schema.js'
|
|
6
6
|
|
|
7
|
-
// Fixed values reproducing affiliate-service's original mysql-console
|
|
8
|
-
// endpoint exactly. Not configurable on purpose: registerTools(service, pool,
|
|
9
|
-
// poolPg, clickhouse) takes only the pools already living in the host
|
|
10
|
-
// project - nothing else, no env vars of its own to set up.
|
|
11
7
|
const AFF_PATH = '/api/aff-service/zod-tools'
|
|
12
8
|
const BACKOFFICE_PATH_PREFIX = '/api/backoffice/'
|
|
13
9
|
const BACKOFFICE_PATH_SUFFIX = '/zod-tools'
|