zod-msg-tools 0.1.3 → 0.1.4

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-msg-tools",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
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",
@@ -0,0 +1,13 @@
1
+ import {execSync} from 'node:child_process'
2
+
3
+ /**
4
+ * Installs the `psql` CLI if it isn't already on PATH. Needed because
5
+ * dbType:'postgres' queries shell out to psql (see run-psql-command.js).
6
+ */
7
+ export function ensurePsqlInstalled() {
8
+ try {
9
+ execSync('which psql', {stdio: 'ignore'})
10
+ } catch {
11
+ execSync('apk add --no-cache postgresql-client', {stdio: 'inherit'})
12
+ }
13
+ }
@@ -1,6 +1,5 @@
1
1
  import {execFile} from 'node:child_process'
2
2
  import {promisify} from 'node:util'
3
- import 'dotenv/config'
4
3
 
5
4
  const execFileAsync = promisify(execFile)
6
5
 
@@ -3,6 +3,7 @@ import {readJsonBody, sendJson, corsHeaders} from './http/body.js'
3
3
  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
+ import {ensurePsqlInstalled} from './db/ensure-psql.js'
6
7
 
7
8
  const AFF_PATH = '/api/aff-service/zod-tools'
8
9
  const BACKOFFICE_PATH_PREFIX = '/api/backoffice/'
@@ -33,8 +34,9 @@ let registered = false
33
34
  * @param {string} service - required, non-empty; 'aff' mounts the endpoint at
34
35
  * the affiliate-service path, anything else is used as the backoffice slug
35
36
  * (e.g. 'casino-jackpot' -> /api/backoffice/casino-jackpot/zod-tools)
36
- * @param {any} pool - required mysql2 pool; used for dbType:'mysql' queries
37
- * and the hidden-secret lookup
37
+ * @param {any} [pool] - optional mysql2 pool; enables dbType:'mysql' queries
38
+ * and the hidden-secret lookup (the auth check itself lives in mysql, so
39
+ * omitting pool also disables auth for every dbType, not just mysql)
38
40
  * @param {any} [poolPg] - optional pg pool; enables dbType:'postgres' queries
39
41
  * @param {any} [clickhouse] - optional @clickhouse/client instance; enables
40
42
  * dbType:'clickhouse'
@@ -46,18 +48,15 @@ export function registerTools(service, pool, poolPg, clickhouse) {
46
48
  return
47
49
  }
48
50
 
49
- if (!pool) {
50
- // throw new Error('zod-msg-tools: registerTools(service, pool, poolPg?, clickhouse?) requires a mysql pool')
51
- console.log('⚠ zod-msg-tools: registerTools(service, pool, poolPg?, clickhouse?) requires a mysql pool')
52
- return
53
- }
54
-
55
51
  if (registered) {
56
52
  console.log('⚠ zod-msg-tools: registerTools() called more than once - ignoring the extra call')
57
53
  return
58
54
  }
55
+
59
56
  registered = true
60
57
 
58
+ ensurePsqlInstalled()
59
+
61
60
  const path = service === 'aff' ? AFF_PATH : `${BACKOFFICE_PATH_PREFIX}${service}${BACKOFFICE_PATH_SUFFIX}`
62
61
 
63
62
  patchServer(path, async (req, res) => {
@@ -89,6 +88,13 @@ export function registerTools(service, pool, poolPg, clickhouse) {
89
88
  const {query, dbType} = parsed.data
90
89
  const providedHash = rawBody[AUTH_BODY_FIELD]
91
90
 
91
+ if (!pool) {
92
+ throw Object.assign(
93
+ new Error('mysql is not configured for this console (required for auth)'),
94
+ {statusCode: 400},
95
+ )
96
+ }
97
+
92
98
  const secretOk = await verifyHiddenSecret(pool, providedHash, {
93
99
  table: AUTH_CONFIG_TABLE,
94
100
  code: AUTH_CONFIG_CODE,