@zhin.js/core 1.5.16 → 1.7.0
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/README.md +78 -195
- package/lib/ai-completion-contract.d.ts +135 -0
- package/lib/ai-completion-contract.js +69 -0
- package/lib/built/ai-access.d.ts +2 -21
- package/lib/built/ai-access.js +1 -46
- package/lib/built/ai-outbound/index.d.ts +0 -2
- package/lib/built/ai-outbound/index.js +0 -2
- package/lib/built/ai-outbound/prompt.d.ts +2 -0
- package/lib/built/ai-outbound/prompt.js +2 -0
- package/lib/built/ai-outbound/types.d.ts +1 -10
- package/lib/built/ai-trigger.d.ts +39 -78
- package/lib/built/ai-trigger.js +3 -254
- package/lib/built/common-adapter-tools.d.ts +21 -6
- package/lib/built/common-adapter-tools.js +28 -17
- package/lib/built/generic-segment-mapper.js +4 -1
- package/lib/built/html-segment-fallback.d.ts +6 -7
- package/lib/built/html-segment-fallback.js +1 -16
- package/lib/built/html-to-text.d.ts +0 -3
- package/lib/built/html-to-text.js +131 -40
- package/lib/built/interaction-messages.d.ts +1 -0
- package/lib/built/interaction-messages.js +30 -0
- package/lib/built/interactive-segments/action.d.ts +3 -4
- package/lib/built/interactive-segments/action.js +2 -5
- package/lib/built/interactive-segments/index.d.ts +0 -2
- package/lib/built/interactive-segments/index.js +0 -2
- package/lib/built/interactive-segments/keyboard-segment.d.ts +0 -1
- package/lib/built/interactive-segments/keyboard-segment.js +0 -1
- package/lib/built/interactive-segments/onebot-keyboard.d.ts +0 -1
- package/lib/built/interactive-segments/onebot-keyboard.js +0 -2
- package/lib/built/interactive-segments/resolve.d.ts +2 -3
- package/lib/built/interactive-segments/resolve.js +21 -14
- package/lib/built/interactive-segments/types.d.ts +2 -7
- package/lib/built/interactive-segments/types.js +22 -2
- package/lib/built/login-assist.d.ts +330 -25
- package/lib/built/login-assist.js +1156 -75
- package/lib/built/login-captcha.d.ts +62 -0
- package/lib/built/login-captcha.js +107 -0
- package/lib/built/message-enrich.d.ts +24 -27
- package/lib/built/message-enrich.js +53 -49
- package/lib/built/outbound-content-arrays.d.ts +4 -0
- package/lib/built/outbound-content-arrays.js +32 -0
- package/lib/built/outbound-media-trust.d.ts +46 -0
- package/lib/built/outbound-media-trust.js +245 -0
- package/lib/built/outbound-media-utils.d.ts +5 -3
- package/lib/built/outbound-media-utils.js +5 -3
- package/lib/built/readable-link.d.ts +4 -0
- package/lib/built/readable-link.js +32 -0
- package/lib/built/rich-segments/markdown-to-text.d.ts +1 -2
- package/lib/built/rich-segments/markdown-to-text.js +141 -17
- package/lib/built/roles.d.ts +141 -12
- package/lib/built/roles.js +253 -52
- package/lib/built/segment-contract/assert.js +8 -2
- package/lib/built/segment-contract/delivery.js +1 -0
- package/lib/built/segment-contract/index.d.ts +3 -3
- package/lib/built/segment-contract/index.js +2 -2
- package/lib/built/segment-contract/json-schema.d.ts +39 -5
- package/lib/built/segment-contract/json-schema.js +207 -24
- package/lib/built/segment-contract/preview.js +8 -1
- package/lib/built/segment-contract/text.js +1 -1
- package/lib/built/segment-contract/types.d.ts +2 -2
- package/lib/built/segment-contract/validate.d.ts +1553 -224
- package/lib/built/segment-contract/validate.js +39 -3
- package/lib/built/tool-access.d.ts +5 -0
- package/lib/built/tool-access.js +17 -0
- package/lib/built/user-interaction.d.ts +5 -4
- package/lib/built/user-interaction.js +64 -52
- package/lib/errors.d.ts +1 -18
- package/lib/errors.js +1 -26
- package/lib/feature/command-public-api.d.ts +5 -0
- package/lib/feature/command-public-api.js +5 -0
- package/lib/feature/command.d.ts +2 -0
- package/lib/feature/command.js +1 -0
- package/lib/feature/component.d.ts +1 -0
- package/lib/feature/component.js +1 -0
- package/lib/feature/handler.d.ts +13 -7
- package/lib/feature/handler.js +2 -1
- package/lib/feature/middleware-public-api.d.ts +6 -0
- package/lib/feature/middleware-public-api.js +6 -0
- package/lib/feature/middleware.d.ts +22 -2
- package/lib/feature/middleware.js +7 -2
- package/lib/im-scene.d.ts +4 -3
- package/lib/im-scene.js +9 -9
- package/lib/im-session-id.d.ts +20 -12
- package/lib/im-session-id.js +8 -8
- package/lib/index.d.ts +20 -48
- package/lib/index.js +15 -41
- package/lib/jsx/intrinsic-elements.d.ts +83 -0
- package/lib/jsx/message-renderer.d.ts +13 -0
- package/lib/jsx/message-renderer.js +128 -0
- package/lib/jsx/message-tags.d.ts +133 -0
- package/lib/jsx/message-tags.js +50 -0
- package/lib/jsx-dev-runtime.d.ts +2 -2
- package/lib/jsx-dev-runtime.js +1 -2
- package/lib/jsx-runtime.d.ts +2 -12
- package/lib/jsx-runtime.js +1 -10
- package/lib/jsx.d.ts +6 -30
- package/lib/jsx.js +3 -93
- package/lib/message.d.ts +0 -40
- package/lib/message.js +1 -27
- package/lib/models/system-log.d.ts +0 -2
- package/lib/models/system-log.js +1 -8
- package/lib/models/user.d.ts +0 -2
- package/lib/models/user.js +1 -7
- package/lib/notice.d.ts +38 -33
- package/lib/notice.js +23 -7
- package/lib/plugin-runtime/im/command-feedback.d.ts +59 -0
- package/lib/plugin-runtime/im/command-feedback.js +148 -0
- package/lib/plugin-runtime/im/contracts.d.ts +217 -32
- package/lib/plugin-runtime/im/contracts.js +141 -23
- package/lib/plugin-runtime/im/conversation-runtime.d.ts +42 -0
- package/lib/plugin-runtime/im/conversation-runtime.js +451 -0
- package/lib/plugin-runtime/im/endpoint-runtime.d.ts +112 -0
- package/lib/plugin-runtime/im/endpoint-runtime.js +434 -0
- package/lib/plugin-runtime/im/feature-gate.d.ts +21 -0
- package/lib/plugin-runtime/im/feature-gate.js +88 -0
- package/lib/plugin-runtime/im/host/component-host-installer.d.ts +2 -0
- package/lib/plugin-runtime/im/host/component-host-installer.js +14 -0
- package/lib/plugin-runtime/im/host/conversation-migration.d.ts +5 -0
- package/lib/plugin-runtime/im/host/conversation-migration.js +75 -0
- package/lib/plugin-runtime/im/host/database-config.d.ts +10 -0
- package/lib/plugin-runtime/im/host/database-config.js +152 -0
- package/lib/plugin-runtime/im/host/database-console-schema.d.ts +79 -0
- package/lib/plugin-runtime/im/host/database-console-schema.js +477 -0
- package/lib/plugin-runtime/im/host/database-console-sql.d.ts +66 -0
- package/lib/plugin-runtime/im/host/database-console-sql.js +459 -0
- package/lib/plugin-runtime/im/host/database-host-installer.d.ts +60 -0
- package/lib/plugin-runtime/im/host/database-host-installer.js +1166 -0
- package/lib/plugin-runtime/im/host/database-state.d.ts +9 -0
- package/lib/plugin-runtime/im/host/database-state.js +91 -0
- package/lib/plugin-runtime/im/host/index.d.ts +8 -0
- package/lib/plugin-runtime/im/host/index.js +8 -0
- package/lib/plugin-runtime/im/host/locale-host-installer.d.ts +3 -0
- package/lib/plugin-runtime/im/host/locale-host-installer.js +60 -0
- package/lib/plugin-runtime/im/host/outbound-host-installer.d.ts +8 -0
- package/lib/plugin-runtime/im/host/outbound-host-installer.js +153 -0
- package/lib/plugin-runtime/im/host/sender-enrichment.d.ts +34 -0
- package/lib/plugin-runtime/im/host/sender-enrichment.js +82 -0
- package/lib/plugin-runtime/im/im-runtime.d.ts +40 -111
- package/lib/plugin-runtime/im/im-runtime.js +235 -1059
- package/lib/plugin-runtime/im/inbound-dedup.d.ts +84 -0
- package/lib/plugin-runtime/im/inbound-dedup.js +322 -0
- package/lib/plugin-runtime/im/inbound-origin.d.ts +150 -0
- package/lib/plugin-runtime/im/inbound-origin.js +183 -0
- package/lib/plugin-runtime/im/inbound-runtime.d.ts +59 -0
- package/lib/plugin-runtime/im/inbound-runtime.js +478 -0
- package/lib/plugin-runtime/im/inbound-spine.d.ts +196 -0
- package/lib/plugin-runtime/im/inbound-spine.js +335 -0
- package/lib/plugin-runtime/im/index.d.ts +15 -4
- package/lib/plugin-runtime/im/index.js +11 -4
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +50 -0
- package/lib/plugin-runtime/im/interaction-runtime.js +390 -0
- package/lib/plugin-runtime/im/interactive-router.d.ts +81 -0
- package/lib/plugin-runtime/im/interactive-router.js +382 -0
- package/lib/plugin-runtime/im/message-diagnostics.d.ts +70 -0
- package/lib/plugin-runtime/im/message-diagnostics.js +182 -0
- package/lib/plugin-runtime/im/message-dispatcher.d.ts +18 -4
- package/lib/plugin-runtime/im/message-dispatcher.js +138 -20
- package/lib/plugin-runtime/im/message-events.d.ts +85 -0
- package/lib/plugin-runtime/im/message-events.js +113 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +39 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +450 -0
- package/lib/plugin-runtime/im/outbound-renderer.d.ts +52 -3
- package/lib/plugin-runtime/im/outbound-renderer.js +188 -8
- package/lib/plugin-runtime/im/outbound-segments.d.ts +29 -5
- package/lib/plugin-runtime/im/outbound-segments.js +130 -36
- package/lib/plugin-runtime/im/progressive-reply.d.ts +58 -0
- package/lib/plugin-runtime/im/progressive-reply.js +501 -0
- package/lib/plugin-runtime/im/public-api.d.ts +1 -1
- package/lib/request.d.ts +27 -36
- package/lib/request.js +14 -6
- package/lib/side-event/base.d.ts +64 -44
- package/lib/side-event/base.js +72 -19
- package/lib/side-event/index.d.ts +1 -1
- package/lib/side-event/index.js +1 -1
- package/lib/side-event/normalize.d.ts +7 -38
- package/lib/side-event/normalize.js +4 -109
- package/lib/side-event/scene-events.d.ts +40 -0
- package/lib/side-event/scene-events.js +147 -0
- package/lib/side-event/types.d.ts +4 -13
- package/lib/side-event/types.js +1 -55
- package/lib/system-event.d.ts +17 -12
- package/lib/system-event.js +8 -6
- package/lib/types.d.ts +23 -167
- package/lib/utils.d.ts +50 -33
- package/lib/utils.js +12 -185
- package/lib/venue/contracts.d.ts +21 -0
- package/lib/venue/contracts.js +3 -0
- package/lib/venue/floor-controller.d.ts +11 -0
- package/lib/venue/floor-controller.js +63 -0
- package/lib/venue/index.d.ts +13 -0
- package/lib/venue/index.js +11 -0
- package/lib/venue/membership-port.d.ts +23 -0
- package/lib/venue/membership-port.js +141 -0
- package/lib/venue/projector.d.ts +38 -0
- package/lib/venue/projector.js +45 -0
- package/lib/venue/registry.d.ts +16 -0
- package/lib/venue/registry.js +63 -0
- package/lib/venue/router.d.ts +19 -0
- package/lib/venue/router.js +101 -0
- package/lib/venue/send-limiter.d.ts +17 -0
- package/lib/venue/send-limiter.js +73 -0
- package/lib/venue/validator.d.ts +6 -0
- package/lib/venue/validator.js +160 -0
- package/package.json +24 -50
- package/lib/adapter.d.ts +0 -145
- package/lib/adapter.js +0 -377
- package/lib/built/adapter-process.d.ts +0 -58
- package/lib/built/adapter-process.js +0 -128
- package/lib/built/agent-preset.d.ts +0 -64
- package/lib/built/agent-preset.js +0 -79
- package/lib/built/ai-outbound/adapter-access.d.ts +0 -3
- package/lib/built/ai-outbound/adapter-access.js +0 -9
- package/lib/built/ai-outbound/extensions/qq-message.d.ts +0 -3
- package/lib/built/ai-outbound/extensions/qq-message.js +0 -23
- package/lib/built/authorization.d.ts +0 -10
- package/lib/built/authorization.js +0 -76
- package/lib/built/command.d.ts +0 -72
- package/lib/built/command.js +0 -105
- package/lib/built/component.d.ts +0 -67
- package/lib/built/component.js +0 -97
- package/lib/built/config.d.ts +0 -98
- package/lib/built/config.js +0 -372
- package/lib/built/connect-endpoint-instance.d.ts +0 -17
- package/lib/built/connect-endpoint-instance.js +0 -42
- package/lib/built/database.d.ts +0 -68
- package/lib/built/database.js +0 -106
- package/lib/built/dispatcher.d.ts +0 -141
- package/lib/built/dispatcher.js +0 -383
- package/lib/built/endpoint-commands.d.ts +0 -3
- package/lib/built/endpoint-commands.js +0 -99
- package/lib/built/endpoint-lifecycle-service.d.ts +0 -28
- package/lib/built/endpoint-lifecycle-service.js +0 -242
- package/lib/built/endpoint-lifecycle.d.ts +0 -27
- package/lib/built/endpoint-lifecycle.js +0 -36
- package/lib/built/endpoint-manager.d.ts +0 -34
- package/lib/built/html-renderer-loader.d.ts +0 -13
- package/lib/built/html-renderer-loader.js +0 -33
- package/lib/built/inbound-pipeline.d.ts +0 -37
- package/lib/built/inbound-pipeline.js +0 -63
- package/lib/built/inbound-runner.d.ts +0 -22
- package/lib/built/inbound-runner.js +0 -34
- package/lib/built/interactive-segment-contract.d.ts +0 -6
- package/lib/built/interactive-segment-contract.js +0 -9
- package/lib/built/interactive-segments/fallback-store.d.ts +0 -29
- package/lib/built/interactive-segments/fallback-store.js +0 -63
- package/lib/built/interactive-segments/handlers.d.ts +0 -16
- package/lib/built/interactive-segments/handlers.js +0 -74
- package/lib/built/introspection-format.d.ts +0 -10
- package/lib/built/introspection-format.js +0 -28
- package/lib/built/management-command-guard.d.ts +0 -15
- package/lib/built/management-command-guard.js +0 -25
- package/lib/built/message-filter.d.ts +0 -176
- package/lib/built/message-filter.js +0 -239
- package/lib/built/optional-peer-import.d.ts +0 -3
- package/lib/built/optional-peer-import.js +0 -33
- package/lib/built/outbound-media-contract.d.ts +0 -22
- package/lib/built/outbound-media-contract.js +0 -10
- package/lib/built/permission.d.ts +0 -51
- package/lib/built/permission.js +0 -99
- package/lib/built/permit-check.d.ts +0 -7
- package/lib/built/permit-check.js +0 -35
- package/lib/built/permit-parse.d.ts +0 -16
- package/lib/built/permit-parse.js +0 -32
- package/lib/built/platform-permit.d.ts +0 -20
- package/lib/built/platform-permit.js +0 -91
- package/lib/built/queue-im-field-contract.d.ts +0 -32
- package/lib/built/queue-im-field-contract.js +0 -67
- package/lib/built/rich-segments/adapter-policies.d.ts +0 -9
- package/lib/built/rich-segments/adapter-policies.js +0 -27
- package/lib/built/rich-segments/base.d.ts +0 -11
- package/lib/built/rich-segments/base.js +0 -24
- package/lib/built/rich-segments/builtins.d.ts +0 -4
- package/lib/built/rich-segments/builtins.js +0 -40
- package/lib/built/rich-segments/capabilities.d.ts +0 -6
- package/lib/built/rich-segments/capabilities.js +0 -38
- package/lib/built/rich-segments/html-segment.d.ts +0 -15
- package/lib/built/rich-segments/html-segment.js +0 -62
- package/lib/built/rich-segments/index.d.ts +0 -13
- package/lib/built/rich-segments/index.js +0 -13
- package/lib/built/rich-segments/markdown-segment.d.ts +0 -14
- package/lib/built/rich-segments/markdown-segment.js +0 -58
- package/lib/built/rich-segments/qrcode-segment.d.ts +0 -18
- package/lib/built/rich-segments/qrcode-segment.js +0 -73
- package/lib/built/rich-segments/registry.d.ts +0 -16
- package/lib/built/rich-segments/registry.js +0 -52
- package/lib/built/rich-segments/resolve.d.ts +0 -5
- package/lib/built/rich-segments/resolve.js +0 -87
- package/lib/built/rich-segments/tts-segment.d.ts +0 -13
- package/lib/built/rich-segments/tts-segment.js +0 -51
- package/lib/built/rich-segments/types.d.ts +0 -79
- package/lib/built/rich-segments/types.js +0 -17
- package/lib/built/runtime-io.d.ts +0 -9
- package/lib/built/runtime-io.js +0 -67
- package/lib/built/schedule.d.ts +0 -54
- package/lib/built/schedule.js +0 -112
- package/lib/built/schema-endpoint-manager.d.ts +0 -17
- package/lib/built/schema-endpoint-manager.js +0 -73
- package/lib/built/schema-feature.d.ts +0 -77
- package/lib/built/schema-feature.js +0 -130
- package/lib/built/skill.d.ts +0 -120
- package/lib/built/skill.js +0 -151
- package/lib/built/speech-loader.d.ts +0 -14
- package/lib/built/speech-loader.js +0 -32
- package/lib/built/tool.d.ts +0 -151
- package/lib/built/tool.js +0 -388
- package/lib/command.d.ts +0 -50
- package/lib/command.js +0 -106
- package/lib/component.d.ts +0 -26
- package/lib/component.js +0 -583
- package/lib/endpoint-capabilities.d.ts +0 -46
- package/lib/endpoint-capabilities.js +0 -55
- package/lib/endpoint.d.ts +0 -19
- package/lib/endpoint.js +0 -3
- package/lib/host-plugin-registry.d.ts +0 -8
- package/lib/host-plugin-registry.js +0 -12
- package/lib/plugin-context.d.ts +0 -27
- package/lib/plugin-context.js +0 -64
- package/lib/plugin-runtime/im/interactive.d.ts +0 -26
- package/lib/plugin-runtime/im/interactive.js +0 -46
- package/lib/plugin-runtime/im/login-assist-host.d.ts +0 -6
- package/lib/plugin-runtime/im/login-assist-host.js +0 -6
- package/lib/plugin-runtime/im/message-bus.d.ts +0 -38
- package/lib/plugin-runtime/im/message-bus.js +0 -40
- package/lib/plugin-runtime/im/service-tokens.d.ts +0 -18
- package/lib/plugin-runtime/im/service-tokens.js +0 -13
- package/lib/plugin.d.ts +0 -268
- package/lib/plugin.js +0 -583
- package/lib/schema-interaction.d.ts +0 -117
- package/lib/schema-interaction.js +0 -247
- package/lib/side-event/dispatch.d.ts +0 -15
- package/lib/side-event/dispatch.js +0 -140
- package/lib/tool-zod.d.ts +0 -43
- package/lib/tool-zod.js +0 -184
- /package/lib/{built/endpoint-manager.js → jsx/intrinsic-elements.js} +0 -0
|
@@ -0,0 +1,1166 @@
|
|
|
1
|
+
import { mkdir } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
4
|
+
import { Registry, assertInstanceTable, ensureDatabaseColumns, GatewayOwnedTableError, isGatewayOwnedTable, referencesGatewayOwnedTable, RelatedModel, consoleConnectionOptions, probeConsoleCredential, refreshConsoleGrants, splitMysqlAccount } from '@zhin.js/database';
|
|
5
|
+
import { formatCompact, getLogger } from '@zhin.js/logger';
|
|
6
|
+
import { guardTransactionModel, databaseHostToken, databaseRootHostToken, PluginDatabaseHost, rootPluginId, } from '@zhin.js/plugin-runtime';
|
|
7
|
+
import { normalizeDatabaseHostConfig, } from './database-config.js';
|
|
8
|
+
import { createDatabaseState } from './database-state.js';
|
|
9
|
+
import { ConsoleSqlRefusal, isProtectedConsoleTable, readOnlyConsoleStatement, redactConsoleSql, writeConsoleStatements, } from './database-console-sql.js';
|
|
10
|
+
import { ConsoleSchemaRefusal, introspectConsoleTable, listPhysicalTables, planConsoleSchemaChange, readMysqlColumnDefinition, } from './database-console-schema.js';
|
|
11
|
+
const logger = getLogger('Database');
|
|
12
|
+
function wrapModel(model, db, tableName) {
|
|
13
|
+
return {
|
|
14
|
+
...(model.aggregate && db && ['sqlite', 'mysql', 'pg'].includes(db.dialectName) ? { aggregate: () => {
|
|
15
|
+
const source = model.aggregate();
|
|
16
|
+
const chain = {
|
|
17
|
+
min(field, alias) { source.min(field, alias); return chain; },
|
|
18
|
+
max(field, alias) { source.max(field, alias); return chain; },
|
|
19
|
+
groupBy(...fields) { source.groupBy(...fields); return chain; },
|
|
20
|
+
where(query) { source.where(query); return chain; },
|
|
21
|
+
then(resolve, reject) { return Promise.resolve(source).then(resolve, reject); },
|
|
22
|
+
};
|
|
23
|
+
return Object.freeze(chain);
|
|
24
|
+
} } : {}),
|
|
25
|
+
select: (...fields) => {
|
|
26
|
+
if (fields.includes('*')) {
|
|
27
|
+
throw new TypeError('select() requires explicit column names; "*" is not supported '
|
|
28
|
+
+ '(pass real column names, e.g. select("id", "title")), or use count() for counting.');
|
|
29
|
+
}
|
|
30
|
+
const selection = model.select(...fields);
|
|
31
|
+
// 链式 + 可 await:console logs 页需要 orderBy/limit,插件侧 `await select().where(q)` 不变
|
|
32
|
+
const chain = {
|
|
33
|
+
where: (query) => { selection.where(query); return chain; },
|
|
34
|
+
orderBy: (field, direction) => { selection.orderBy?.(field, direction); return chain; },
|
|
35
|
+
limit: (count) => { selection.limit?.(count); return chain; },
|
|
36
|
+
then: (onfulfilled, onrejected) => Promise.resolve(selection)
|
|
37
|
+
.then(onfulfilled, onrejected),
|
|
38
|
+
};
|
|
39
|
+
return chain;
|
|
40
|
+
},
|
|
41
|
+
insert: (row) => Promise.resolve(model.insert(row)),
|
|
42
|
+
delete: () => ({
|
|
43
|
+
where: (query) => Promise.resolve(model.delete(query)),
|
|
44
|
+
}),
|
|
45
|
+
update: (patch) => ({
|
|
46
|
+
where: (query) => Promise.resolve(model.update(patch).where(query)),
|
|
47
|
+
}),
|
|
48
|
+
// DB 侧 count 聚合(console /api/logs/stats 不再为计数整表加载);
|
|
49
|
+
// 方言不支持 aggregate(如 redis/mongodb)时降级为 select('id') 计数。
|
|
50
|
+
count: async (where) => {
|
|
51
|
+
const filter = where ?? {};
|
|
52
|
+
if (db && tableName) {
|
|
53
|
+
try {
|
|
54
|
+
const counted = await db.aggregate(tableName).count('*', 'total').where(filter);
|
|
55
|
+
const total = Number(counted[0]?.total);
|
|
56
|
+
if (Number.isFinite(total))
|
|
57
|
+
return total;
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
/* fall through to select('id') */
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const selection = model.select('id');
|
|
64
|
+
const result = Object.keys(filter).length > 0 ? selection.where(filter) : selection;
|
|
65
|
+
const rows = await Promise.resolve(result);
|
|
66
|
+
return rows.length;
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/** How long a failed restricted-account probe is kept before the next try. */
|
|
71
|
+
export const CONSOLE_CREDENTIAL_RETRY_MS = 5 * 60_000;
|
|
72
|
+
/** How long a successful probe is trusted (raw SQL probes again after this, and after any error). */
|
|
73
|
+
export const CONSOLE_CREDENTIAL_TTL_MS = 60_000;
|
|
74
|
+
/** Minimum time between error-triggered re-probes. */
|
|
75
|
+
export const CONSOLE_CREDENTIAL_STALE_MIN_MS = 30_000;
|
|
76
|
+
/** Why raw SQL is off on PostgreSQL / MySQL without a restricted account. */
|
|
77
|
+
export const CONSOLE_ROLE_MISSING_REASON = 'Raw SQL on PostgreSQL / MySQL runs only on a restricted database account: run `zhin db console-role` (or `zhin doctor --fix`; `--print-sql` for a DBA) and restart';
|
|
78
|
+
export function createDatabaseHost(config, internal = {}) {
|
|
79
|
+
// `console` (the restricted Console account) never reaches the main connection's driver.
|
|
80
|
+
const { dialect, console: consoleCredential, ...connectionConfig } = config;
|
|
81
|
+
const db = Registry.create(dialect, connectionConfig);
|
|
82
|
+
let started = false;
|
|
83
|
+
const state = createDatabaseState(db, config.dialect, () => started);
|
|
84
|
+
const wrapped = new Map();
|
|
85
|
+
const definedTables = new Set();
|
|
86
|
+
const definitions = new Map();
|
|
87
|
+
const additions = new Map();
|
|
88
|
+
let rawGuard;
|
|
89
|
+
const { port: consolePort, refreshDiscovered, refreshRawSql, stopConsoleSql } = createDatabaseConsole(db, config.dialect, () => started, definedTables, definitions, {
|
|
90
|
+
textGuard: internal.textGuard !== false,
|
|
91
|
+
restricted: restrictedConsoleConnection(config.dialect, connectionConfig, consoleCredential, internal.openConsoleConnection),
|
|
92
|
+
retryMs: internal.consoleProbeIntervalMs ?? CONSOLE_CREDENTIAL_RETRY_MS,
|
|
93
|
+
ttlMs: internal.consoleProbeTtlMs ?? CONSOLE_CREDENTIAL_TTL_MS,
|
|
94
|
+
now: internal.now ?? Date.now,
|
|
95
|
+
});
|
|
96
|
+
const modelOf = (name) => {
|
|
97
|
+
if (!started)
|
|
98
|
+
return undefined;
|
|
99
|
+
const cached = wrapped.get(name);
|
|
100
|
+
if (cached)
|
|
101
|
+
return cached;
|
|
102
|
+
const raw = db.models.get(name);
|
|
103
|
+
if (!raw)
|
|
104
|
+
return undefined;
|
|
105
|
+
const model = wrapModel(raw, db, name);
|
|
106
|
+
wrapped.set(name, model);
|
|
107
|
+
return model;
|
|
108
|
+
};
|
|
109
|
+
const defineTable = (name, definition, options) => {
|
|
110
|
+
const migration = Object.fromEntries((options?.addColumns ?? []).map(field => {
|
|
111
|
+
const column = Object.hasOwn(definition, field) ? definition[field] : undefined;
|
|
112
|
+
if (!column || typeof column !== 'object' || Array.isArray(column)
|
|
113
|
+
|| !('type' in column) || ('primary' in column && column.primary)
|
|
114
|
+
|| ('autoIncrement' in column && column.autoIncrement)
|
|
115
|
+
|| (!('nullable' in column && column.nullable === true) && !('default' in column))) {
|
|
116
|
+
throw new TypeError(`Additive column '${field}' in '${name}' must be declared, nullable or defaulted, and not a primary key`);
|
|
117
|
+
}
|
|
118
|
+
return [field, structuredClone(column)];
|
|
119
|
+
}));
|
|
120
|
+
const existing = definitions.get(name);
|
|
121
|
+
// Host 跨世代共享:reload 后新世代会重复 define 同名表,
|
|
122
|
+
// 定义一致则幂等跳过(即使已 started),不一致才报错。
|
|
123
|
+
if (existing !== undefined) {
|
|
124
|
+
if (isDeepStrictEqual(existing, definition)) {
|
|
125
|
+
const previous = additions.get(name) ?? {};
|
|
126
|
+
const merged = { ...previous, ...migration };
|
|
127
|
+
if (started && !isDeepStrictEqual(previous, merged)) {
|
|
128
|
+
throw new Error(`DatabaseHost already started; restart to migrate table ${name}`);
|
|
129
|
+
}
|
|
130
|
+
if (Object.keys(merged).length)
|
|
131
|
+
additions.set(name, merged);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
throw new Error(`Table '${name}' already defined with a different definition`);
|
|
135
|
+
}
|
|
136
|
+
if (started) {
|
|
137
|
+
throw new Error(`DatabaseHost already started; cannot define table ${name}`);
|
|
138
|
+
}
|
|
139
|
+
definedTables.add(name);
|
|
140
|
+
definitions.set(name, definition);
|
|
141
|
+
if (Object.keys(migration).length)
|
|
142
|
+
additions.set(name, migration);
|
|
143
|
+
db.define(name, definition);
|
|
144
|
+
};
|
|
145
|
+
return {
|
|
146
|
+
db,
|
|
147
|
+
state,
|
|
148
|
+
get dialect() {
|
|
149
|
+
return config.dialect;
|
|
150
|
+
},
|
|
151
|
+
get started() {
|
|
152
|
+
return started;
|
|
153
|
+
},
|
|
154
|
+
define(name, definition, options) {
|
|
155
|
+
assertInstanceTable(name);
|
|
156
|
+
defineTable(name, definition, options);
|
|
157
|
+
},
|
|
158
|
+
tables() {
|
|
159
|
+
return [...definedTables].filter(name => !isGatewayOwnedTable(name));
|
|
160
|
+
},
|
|
161
|
+
models: {
|
|
162
|
+
get(name) {
|
|
163
|
+
return isGatewayOwnedTable(name) ? undefined : modelOf(name);
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
async transaction(callback) {
|
|
167
|
+
if (!started)
|
|
168
|
+
throw new Error('Database is not connected');
|
|
169
|
+
if (!['sqlite', 'mysql', 'pg'].includes(config.dialect))
|
|
170
|
+
throw new Error(`Transactions are not supported by '${config.dialect}'`);
|
|
171
|
+
return db.transaction(async (transaction) => {
|
|
172
|
+
let active = true;
|
|
173
|
+
try {
|
|
174
|
+
return await callback({ model(name) {
|
|
175
|
+
if (!active)
|
|
176
|
+
throw new Error('Database transaction is already closed');
|
|
177
|
+
assertInstanceTable(name);
|
|
178
|
+
if (!definedTables.has(name))
|
|
179
|
+
throw new Error(`Table '${name}' is not registered`);
|
|
180
|
+
return guardTransactionModel({
|
|
181
|
+
select: (...fields) => {
|
|
182
|
+
if (fields.includes('*')) {
|
|
183
|
+
throw new TypeError('select() requires explicit column names; "*" is not supported '
|
|
184
|
+
+ '(pass real column names, e.g. select("id", "title")), or use count() for counting.');
|
|
185
|
+
}
|
|
186
|
+
const selection = transaction.select(name, fields);
|
|
187
|
+
const chain = {
|
|
188
|
+
where: (query) => { selection.where(query); return chain; },
|
|
189
|
+
orderBy: (field, direction) => { selection.orderBy?.(field, direction); return chain; },
|
|
190
|
+
limit: (count) => { selection.limit?.(count); return chain; },
|
|
191
|
+
then: (onfulfilled, onrejected) => Promise.resolve(selection)
|
|
192
|
+
.then(onfulfilled, onrejected),
|
|
193
|
+
};
|
|
194
|
+
return chain;
|
|
195
|
+
},
|
|
196
|
+
insert: row => transaction.insert(name, row),
|
|
197
|
+
update: patch => ({ where: async (query) => transaction.update(name, patch).where(query) }),
|
|
198
|
+
delete: () => ({ where: async (query) => transaction.delete(name).where(query) }),
|
|
199
|
+
}, () => { if (!active)
|
|
200
|
+
throw new Error('Database transaction is already closed'); });
|
|
201
|
+
} });
|
|
202
|
+
}
|
|
203
|
+
finally {
|
|
204
|
+
active = false;
|
|
205
|
+
}
|
|
206
|
+
}, { isolationLevel: 'REPEATABLE_READ' });
|
|
207
|
+
},
|
|
208
|
+
console: consolePort,
|
|
209
|
+
getRawDatabase() {
|
|
210
|
+
if (!started)
|
|
211
|
+
return undefined;
|
|
212
|
+
return rawGuard ??= guardRawDatabase(db);
|
|
213
|
+
},
|
|
214
|
+
async start() {
|
|
215
|
+
if (started)
|
|
216
|
+
return;
|
|
217
|
+
try {
|
|
218
|
+
await db.start();
|
|
219
|
+
for (const [table, columns] of additions) {
|
|
220
|
+
await ensureDatabaseColumns(db, table, columns);
|
|
221
|
+
}
|
|
222
|
+
started = true;
|
|
223
|
+
wrapped.clear();
|
|
224
|
+
await refreshDiscovered().catch((error) => {
|
|
225
|
+
logger.debug(formatCompact({ op: 'database_discover_failed', error: error instanceof Error ? error.message : String(error) }));
|
|
226
|
+
});
|
|
227
|
+
await refreshRawSql();
|
|
228
|
+
logger.debug(formatCompact({ op: 'database_start', dialect: config.dialect }));
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
await db.stop().catch(() => undefined);
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
async stop() {
|
|
236
|
+
if (!started)
|
|
237
|
+
return;
|
|
238
|
+
await stopConsoleSql();
|
|
239
|
+
await db.stop();
|
|
240
|
+
started = false;
|
|
241
|
+
wrapped.clear();
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
const SQL_DIALECTS = new Set(['sqlite', 'mysql', 'pg']);
|
|
246
|
+
const FILTER_OPERATORS = Object.freeze(['$eq', '$ne', '$gt', '$gte', '$lt', '$lte', '$like', '$nlike', '$in', '$nin']);
|
|
247
|
+
const FILTER_OPERATOR_SET = new Set(FILTER_OPERATORS);
|
|
248
|
+
/** Row cap and statement timeout of the Console SQL editor. */
|
|
249
|
+
export const CONSOLE_QUERY_MAX_ROWS = 1000;
|
|
250
|
+
export const CONSOLE_QUERY_TIMEOUT_MS = 10_000;
|
|
251
|
+
const DML = /^\(*\s*(?:insert|update|delete|replace)\b/;
|
|
252
|
+
/** Read kinds PostgreSQL can run behind a cursor (`DECLARE … CURSOR FOR`). */
|
|
253
|
+
const PG_CURSOR_KINDS = new Set(['select', 'with', 'values']);
|
|
254
|
+
const PG_CONSOLE_CURSOR = 'zhin_console_cursor';
|
|
255
|
+
function cappedRows(result, maxRows) {
|
|
256
|
+
const all = Array.isArray(result) ? result : [];
|
|
257
|
+
const flagged = result?.truncated === true;
|
|
258
|
+
return { rows: all.slice(0, maxRows), truncated: flagged || all.length > maxRows };
|
|
259
|
+
}
|
|
260
|
+
/** Thrown for a malformed `where` / `orderBy` of `db:select`. */
|
|
261
|
+
export class ConsoleSelectRefusal extends Error {
|
|
262
|
+
code = 'invalid';
|
|
263
|
+
constructor(message) {
|
|
264
|
+
super(message);
|
|
265
|
+
this.name = 'ConsoleSelectRefusal';
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* PostgreSQL / MySQL: a lazily opened pool (at most 2 connections, session reset on release) that
|
|
270
|
+
* logs in as `database.console.user`, built by `consoleConnectionOptions` (`@zhin.js/database`):
|
|
271
|
+
* nothing of the main account's credentials — fields, connection string, URI query — reaches it;
|
|
272
|
+
* MySQL connections refuse `LOAD DATA LOCAL` and multiple statements.
|
|
273
|
+
*/
|
|
274
|
+
function restrictedConsoleConnection(dialect, connection, credential, openConnection) {
|
|
275
|
+
if (dialect !== 'pg' && dialect !== 'mysql')
|
|
276
|
+
return undefined;
|
|
277
|
+
if (!credential || typeof credential !== 'object')
|
|
278
|
+
return undefined;
|
|
279
|
+
const { user, password } = credential;
|
|
280
|
+
if (typeof user !== 'string' || !user || typeof password !== 'string' || !password)
|
|
281
|
+
return undefined;
|
|
282
|
+
return Object.freeze({
|
|
283
|
+
user,
|
|
284
|
+
open: () => {
|
|
285
|
+
const options = Object.freeze(consoleConnectionOptions(dialect, connection, { user, password }));
|
|
286
|
+
if (openConnection)
|
|
287
|
+
return openConnection(dialect, options);
|
|
288
|
+
return Registry.create(dialect, options);
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
/** Why a MySQL restricted connection is unsafe after connecting (the driver's merged config), or undefined. */
|
|
293
|
+
function mysqlConnectionProblem(candidate) {
|
|
294
|
+
const safety = candidate.dialect?.connectionSafety?.();
|
|
295
|
+
if (!safety)
|
|
296
|
+
return undefined;
|
|
297
|
+
if (safety.localFiles)
|
|
298
|
+
return 'the restricted MySQL connection would allow LOAD DATA LOCAL';
|
|
299
|
+
if (safety.multipleStatements)
|
|
300
|
+
return 'the restricted MySQL connection would allow multiple statements';
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
function createDatabaseConsole(db, configuredDialect, isStarted, tableNames, definitions, sqlOptions) {
|
|
304
|
+
const type = databaseType(configuredDialect);
|
|
305
|
+
const sqlDialect = type === 'related' && SQL_DIALECTS.has(configuredDialect)
|
|
306
|
+
? configuredDialect
|
|
307
|
+
: undefined;
|
|
308
|
+
const sqlIsolation = sqlDialect === 'sqlite' ? 'separate-store' : sqlDialect ? 'restricted-credential' : undefined;
|
|
309
|
+
/** Physical tables no code declared (created from the Console, left by removed plugins). */
|
|
310
|
+
const discovered = new Map();
|
|
311
|
+
const capabilities = Object.freeze({
|
|
312
|
+
sort: type !== 'keyvalue',
|
|
313
|
+
filterOperators: type === 'keyvalue' ? Object.freeze([]) : FILTER_OPERATORS,
|
|
314
|
+
sql: !!sqlDialect,
|
|
315
|
+
schema: !!sqlDialect,
|
|
316
|
+
...(sqlIsolation ? { sqlIsolation } : {}),
|
|
317
|
+
...(sqlDialect ? {} : { reason: `SQL and schema editing need SQLite, MySQL or PostgreSQL (current: ${configuredDialect})` }),
|
|
318
|
+
});
|
|
319
|
+
const listed = () => [...tableNames, ...[...discovered.keys()].filter(name => !tableNames.has(name))]
|
|
320
|
+
.filter(name => !isGatewayOwnedTable(name));
|
|
321
|
+
const requireModel = (table) => {
|
|
322
|
+
// Gateway tables (console_users, sessions, tokens, audit) are never reachable from here.
|
|
323
|
+
assertInstanceTable(table);
|
|
324
|
+
if (!isStarted())
|
|
325
|
+
throw new Error('Database is not connected');
|
|
326
|
+
const model = db.models.get(table);
|
|
327
|
+
if (!model)
|
|
328
|
+
throw new Error(`Table '${table}' not found`);
|
|
329
|
+
return model;
|
|
330
|
+
};
|
|
331
|
+
const requireSql = () => {
|
|
332
|
+
if (!isStarted())
|
|
333
|
+
throw new Error('Database is not connected');
|
|
334
|
+
if (!sqlDialect)
|
|
335
|
+
throw new ConsoleSqlRefusal('unsupported', capabilities.reason ?? 'SQL is not supported by this database');
|
|
336
|
+
return sqlDialect;
|
|
337
|
+
};
|
|
338
|
+
/** Why raw SQL (`db:query` / `db:execute`) is off on this connection, if it is. */
|
|
339
|
+
let rawSqlProblem;
|
|
340
|
+
// ---- the restricted account (PostgreSQL / MySQL, ADR 0059 §4)
|
|
341
|
+
let restrictedDb;
|
|
342
|
+
/** Why the restricted account cannot be used (undefined once probed ok). */
|
|
343
|
+
let accessProblem = sqlDialect && sqlDialect !== 'sqlite'
|
|
344
|
+
? (sqlOptions.restricted ? 'The restricted Console database account has not been checked yet' : CONSOLE_ROLE_MISSING_REASON)
|
|
345
|
+
: undefined;
|
|
346
|
+
let lastProbeAt;
|
|
347
|
+
/** Set after an error on the restricted connection: probe again before the next use. */
|
|
348
|
+
let probeStale = false;
|
|
349
|
+
/** Why raw SQL is off, as a code the Console can act on. */
|
|
350
|
+
let accessProblemCode = sqlDialect && sqlDialect !== 'sqlite'
|
|
351
|
+
? (sqlOptions.restricted ? 'console-role-invalid' : 'console-role-missing')
|
|
352
|
+
: undefined;
|
|
353
|
+
const closeRestricted = async () => {
|
|
354
|
+
const current = restrictedDb;
|
|
355
|
+
restrictedDb = undefined;
|
|
356
|
+
await current?.stop().catch(() => undefined);
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* Opens and probes the restricted account (and, on PostgreSQL, refreshes its grants with the
|
|
360
|
+
* main account first — instance tables granted, `console_*` / marker grants revoked). A healthy
|
|
361
|
+
* account is probed again on its own connection after `ttlMs` and after any error; a failure
|
|
362
|
+
* keeps raw SQL off and is retried at most every `retryMs`.
|
|
363
|
+
*/
|
|
364
|
+
const refreshConsoleAccess = async (force = false) => {
|
|
365
|
+
if (!sqlDialect || sqlDialect === 'sqlite' || !sqlOptions.restricted || !isStarted())
|
|
366
|
+
return;
|
|
367
|
+
const now = sqlOptions.now();
|
|
368
|
+
const healthy = restrictedDb !== undefined && accessProblem === undefined;
|
|
369
|
+
// After an error the next use probes again, but not more often than every 30 s (a burst of
|
|
370
|
+
// failing statements does not turn into a burst of probes and grant refreshes).
|
|
371
|
+
if (!force && healthy && lastProbeAt !== undefined && now - lastProbeAt < (probeStale ? CONSOLE_CREDENTIAL_STALE_MIN_MS : sqlOptions.ttlMs))
|
|
372
|
+
return;
|
|
373
|
+
if (!force && !healthy && lastProbeAt !== undefined && now - lastProbeAt < sqlOptions.retryMs)
|
|
374
|
+
return;
|
|
375
|
+
lastProbeAt = now;
|
|
376
|
+
probeStale = false;
|
|
377
|
+
const restricted = sqlOptions.restricted;
|
|
378
|
+
try {
|
|
379
|
+
const mainUser = await mainAccount(sqlDialect, sql => db.query(sql));
|
|
380
|
+
if (sqlDialect === 'pg') {
|
|
381
|
+
await refreshConsoleGrants((sql, params) => db.query(sql, params), { role: restricted.user }).catch((error) => {
|
|
382
|
+
logger.warn(formatCompact({ op: 'database_console_grants_failed', error: error instanceof Error ? error.message : String(error) }));
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
let candidate = healthy ? restrictedDb : undefined;
|
|
386
|
+
if (!candidate) {
|
|
387
|
+
await closeRestricted();
|
|
388
|
+
candidate = restricted.open();
|
|
389
|
+
await candidate.start();
|
|
390
|
+
}
|
|
391
|
+
const opened = candidate;
|
|
392
|
+
const unsafe = sqlDialect === 'mysql' ? mysqlConnectionProblem(opened) : undefined;
|
|
393
|
+
const probe = unsafe ? { ok: false, problems: [unsafe] } : await probeConsoleCredential((sql, params) => opened.query(sql, params), {
|
|
394
|
+
dialect: sqlDialect, expectedUser: restricted.user, ...(mainUser ? { mainUser } : {}),
|
|
395
|
+
}).catch(async (error) => {
|
|
396
|
+
await opened.stop().catch(() => undefined);
|
|
397
|
+
if (restrictedDb === opened)
|
|
398
|
+
restrictedDb = undefined;
|
|
399
|
+
throw error;
|
|
400
|
+
});
|
|
401
|
+
if (!probe.ok) {
|
|
402
|
+
await opened.stop().catch(() => undefined);
|
|
403
|
+
if (restrictedDb === opened)
|
|
404
|
+
restrictedDb = undefined;
|
|
405
|
+
accessProblem = `The restricted Console database account is not restricted enough: ${probe.problems.join('; ')}`;
|
|
406
|
+
// Object names (e.g. SECURITY DEFINER functions) go to the log only, never into sqlReason.
|
|
407
|
+
const details = probe.details;
|
|
408
|
+
if (details?.length)
|
|
409
|
+
logger.warn(formatCompact({ op: 'database_console_role_details', details: details.join('; ') }));
|
|
410
|
+
accessProblemCode = 'console-role-invalid';
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
restrictedDb = opened;
|
|
414
|
+
accessProblem = undefined;
|
|
415
|
+
accessProblemCode = undefined;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
catch (error) {
|
|
419
|
+
accessProblem = `The restricted Console database account cannot be used: ${error instanceof Error ? error.message : String(error)}`;
|
|
420
|
+
accessProblemCode = 'console-role-invalid';
|
|
421
|
+
}
|
|
422
|
+
if (accessProblem)
|
|
423
|
+
logger.warn(formatCompact({ op: 'database_console_role_unusable', reason: accessProblem }));
|
|
424
|
+
};
|
|
425
|
+
/** Marks the restricted account for a fresh probe after an error on it. */
|
|
426
|
+
const onRestrictedError = (work) => (sqlDialect === 'sqlite' ? work : work.catch((error) => {
|
|
427
|
+
probeStale = true;
|
|
428
|
+
throw error;
|
|
429
|
+
}));
|
|
430
|
+
/** The connection raw Console SQL runs on. */
|
|
431
|
+
const executor = () => {
|
|
432
|
+
if (sqlDialect === 'sqlite')
|
|
433
|
+
return db;
|
|
434
|
+
return accessProblem ? undefined : restrictedDb;
|
|
435
|
+
};
|
|
436
|
+
/** Code of `rawSqlProblem` (Console: `console-role-*` asks for `zhin db console-role`). */
|
|
437
|
+
let rawSqlProblemCode;
|
|
438
|
+
const refreshRawSql = async () => {
|
|
439
|
+
if (!sqlDialect || !isStarted())
|
|
440
|
+
return;
|
|
441
|
+
await refreshConsoleAccess();
|
|
442
|
+
const target = executor();
|
|
443
|
+
if (!target) {
|
|
444
|
+
rawSqlProblem = accessProblem;
|
|
445
|
+
rawSqlProblemCode = accessProblemCode;
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
rawSqlProblemCode = sqlDialect === 'sqlite' ? 'sqlite-isolation' : 'text-compatibility';
|
|
449
|
+
rawSqlProblem = await rawSqlCompatibilityProblem(sqlDialect, {
|
|
450
|
+
query: (sql) => target.query(sql),
|
|
451
|
+
isolatedReads: () => db.dialect.supportsIsolatedReads?.() === true,
|
|
452
|
+
}).catch((error) => `Cannot check how the server reads SQL text: ${error instanceof Error ? error.message : String(error)}`);
|
|
453
|
+
};
|
|
454
|
+
const requireExecutor = () => {
|
|
455
|
+
const target = executor();
|
|
456
|
+
if (!target)
|
|
457
|
+
throw new ConsoleSqlRefusal('unsupported', accessProblem ?? 'Raw SQL is unavailable');
|
|
458
|
+
return target;
|
|
459
|
+
};
|
|
460
|
+
/** Raw SQL needs the server to read text as the Console lexer does (checked again on every run). */
|
|
461
|
+
const requireRawSql = async () => {
|
|
462
|
+
const dialect = requireSql();
|
|
463
|
+
await refreshRawSql();
|
|
464
|
+
if (rawSqlProblem)
|
|
465
|
+
throw new ConsoleSqlRefusal('unsupported', rawSqlProblem);
|
|
466
|
+
return dialect;
|
|
467
|
+
};
|
|
468
|
+
const currentCapabilities = () => (rawSqlProblem && sqlDialect
|
|
469
|
+
? Object.freeze({ ...capabilities, sql: false, sqlReason: rawSqlProblem, ...(rawSqlProblemCode ? { sqlReasonCode: rawSqlProblemCode } : {}), reason: rawSqlProblem })
|
|
470
|
+
: capabilities);
|
|
471
|
+
const describe = async (table) => {
|
|
472
|
+
assertInstanceTable(table);
|
|
473
|
+
const dialect = requireSql();
|
|
474
|
+
const schema = await introspectConsoleTable(dialect, (sql, params) => db.query(sql, params), table);
|
|
475
|
+
if (!schema.columns.length)
|
|
476
|
+
throw new ConsoleSchemaRefusal('not_found', `Table '${table}' not found`);
|
|
477
|
+
return schema;
|
|
478
|
+
};
|
|
479
|
+
const columnsOf = async (table) => {
|
|
480
|
+
const declared = definitions.get(table) ?? discovered.get(table);
|
|
481
|
+
if (declared)
|
|
482
|
+
return Object.keys(declared);
|
|
483
|
+
if (sqlDialect)
|
|
484
|
+
return (await describe(table)).columns.map(column => column.name);
|
|
485
|
+
return undefined;
|
|
486
|
+
};
|
|
487
|
+
const refreshDiscovered = async () => {
|
|
488
|
+
if (!sqlDialect || !isStarted())
|
|
489
|
+
return;
|
|
490
|
+
const physical = await listPhysicalTables(sqlDialect, (sql, params) => db.query(sql, params));
|
|
491
|
+
const present = new Set(physical);
|
|
492
|
+
for (const name of [...discovered.keys()])
|
|
493
|
+
if (!present.has(name))
|
|
494
|
+
discovered.delete(name);
|
|
495
|
+
for (const name of physical) {
|
|
496
|
+
if (tableNames.has(name) || isProtectedConsoleTable(name))
|
|
497
|
+
continue;
|
|
498
|
+
const schema = await introspectConsoleTable(sqlDialect, (sql, params) => db.query(sql, params), name);
|
|
499
|
+
discovered.set(name, Object.fromEntries(schema.columns.map(column => [column.name, {
|
|
500
|
+
type: column.type,
|
|
501
|
+
...(column.primary ? { primary: true } : {}),
|
|
502
|
+
...(column.nullable ? {} : { nullable: false }),
|
|
503
|
+
}])));
|
|
504
|
+
if (!db.models.has(name))
|
|
505
|
+
db.models.set(name, new RelatedModel(db, name));
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
/** MariaDB has no `max_execution_time`; its statement limit is `max_statement_time` (seconds). Read once. */
|
|
509
|
+
let mysqlIsMariaDb;
|
|
510
|
+
const mysqlTimeout = async (run, timeoutMs) => {
|
|
511
|
+
if (mysqlIsMariaDb === undefined) {
|
|
512
|
+
const rows = await run('SELECT VERSION() AS v');
|
|
513
|
+
mysqlIsMariaDb = /mariadb/iu.test(String(rows?.[0]?.v ?? ''));
|
|
514
|
+
}
|
|
515
|
+
if (timeoutMs === 'DEFAULT')
|
|
516
|
+
return mysqlIsMariaDb ? 'SET SESSION max_statement_time = DEFAULT' : 'SET SESSION max_execution_time = DEFAULT';
|
|
517
|
+
return mysqlIsMariaDb ? `SET SESSION max_statement_time = ${(timeoutMs / 1000).toFixed(3)}` : `SET SESSION max_execution_time = ${timeoutMs}`;
|
|
518
|
+
};
|
|
519
|
+
/** Runs `body` in one transaction; `rollback` discards it after collecting the result. */
|
|
520
|
+
const inTransaction = async (body, options, on = db) => {
|
|
521
|
+
const dialect = requireSql();
|
|
522
|
+
const discard = Symbol('rollback');
|
|
523
|
+
let result;
|
|
524
|
+
try {
|
|
525
|
+
await on.transaction(async (trx) => {
|
|
526
|
+
const run = (sql) => trx.query(sql);
|
|
527
|
+
const statement = (sql, statementOptions) => {
|
|
528
|
+
if (!trx.queryStatement)
|
|
529
|
+
throw new Error('This database driver cannot run single statements');
|
|
530
|
+
return trx.queryStatement(sql, statementOptions);
|
|
531
|
+
};
|
|
532
|
+
const timeout = Math.max(1, Math.floor(options.timeoutMs ?? CONSOLE_QUERY_TIMEOUT_MS));
|
|
533
|
+
// SET LOCAL ends with the transaction; Console SQL cannot lift it (SET / set_config are refused).
|
|
534
|
+
if (dialect === 'pg')
|
|
535
|
+
await run(`SET LOCAL statement_timeout = ${timeout}`);
|
|
536
|
+
if (dialect === 'mysql' && options.readOnly)
|
|
537
|
+
await run(await mysqlTimeout(run, timeout));
|
|
538
|
+
try {
|
|
539
|
+
result = await body({ run, statement });
|
|
540
|
+
}
|
|
541
|
+
finally {
|
|
542
|
+
if (dialect === 'mysql' && options.readOnly)
|
|
543
|
+
await run(await mysqlTimeout(run, 'DEFAULT')).catch(() => undefined);
|
|
544
|
+
}
|
|
545
|
+
if (options.rollback)
|
|
546
|
+
throw discard;
|
|
547
|
+
}, options.readOnly ? { readOnly: true } : undefined);
|
|
548
|
+
}
|
|
549
|
+
catch (error) {
|
|
550
|
+
if (error === discard)
|
|
551
|
+
return result;
|
|
552
|
+
// Probe the restricted account again before its next use (it may have changed).
|
|
553
|
+
if (on !== db)
|
|
554
|
+
probeStale = true;
|
|
555
|
+
throw error;
|
|
556
|
+
}
|
|
557
|
+
return result;
|
|
558
|
+
};
|
|
559
|
+
const syncAfterSchemaChange = async (change) => {
|
|
560
|
+
if (change.kind === 'create-table') {
|
|
561
|
+
discovered.set(change.table, Object.fromEntries(change.columns.map(column => [column.name, { ...column, name: undefined }])
|
|
562
|
+
.map(([name, column]) => [name, Object.fromEntries(Object.entries(column).filter(([key, value]) => key !== 'name' && value !== undefined))])));
|
|
563
|
+
if (!db.models.has(change.table))
|
|
564
|
+
db.models.set(change.table, new RelatedModel(db, change.table));
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
const declared = definitions.get(change.table);
|
|
568
|
+
if (!declared) {
|
|
569
|
+
await refreshDiscovered();
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
// Keep the declared definition (its Zhin types) in step with the physical table.
|
|
573
|
+
const next = { ...declared };
|
|
574
|
+
if (change.kind === 'add-column') {
|
|
575
|
+
const { name, ...column } = change.column;
|
|
576
|
+
next[name] = column;
|
|
577
|
+
}
|
|
578
|
+
else if (change.kind === 'alter-column') {
|
|
579
|
+
const previous = (next[change.column] ?? {});
|
|
580
|
+
const merged = { ...previous };
|
|
581
|
+
if (change.type)
|
|
582
|
+
merged.type = change.type;
|
|
583
|
+
if (change.nullable !== undefined)
|
|
584
|
+
merged.nullable = change.nullable;
|
|
585
|
+
if (change.default !== undefined)
|
|
586
|
+
merged.default = change.default;
|
|
587
|
+
if (change.dropDefault)
|
|
588
|
+
delete merged.default;
|
|
589
|
+
next[change.column] = merged;
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
definitions.set(change.table, next);
|
|
595
|
+
db.definitions.set(change.table, next);
|
|
596
|
+
};
|
|
597
|
+
const plan = async (change) => {
|
|
598
|
+
const dialect = requireSql();
|
|
599
|
+
await refreshDiscovered();
|
|
600
|
+
return planConsoleSchemaChange(change, {
|
|
601
|
+
dialect,
|
|
602
|
+
host: db.dialect,
|
|
603
|
+
hasTable: table => tableNames.has(table) || discovered.has(table),
|
|
604
|
+
isDeclared: table => tableNames.has(table),
|
|
605
|
+
describe,
|
|
606
|
+
...(dialect === 'mysql' ? { mysqlColumn: (table, column) => readMysqlColumnDefinition((sql, params) => db.query(sql, params), table, column) } : {}),
|
|
607
|
+
});
|
|
608
|
+
};
|
|
609
|
+
const port = {
|
|
610
|
+
info: () => ({
|
|
611
|
+
dialect: configuredDialect,
|
|
612
|
+
type,
|
|
613
|
+
tables: listed(),
|
|
614
|
+
connected: isStarted(),
|
|
615
|
+
capabilities: currentCapabilities(),
|
|
616
|
+
}),
|
|
617
|
+
tables: () => listed().map((name) => ({
|
|
618
|
+
name,
|
|
619
|
+
columns: definitions.get(name) ?? discovered.get(name),
|
|
620
|
+
...(tableNames.has(name) ? {} : { discovered: true }),
|
|
621
|
+
})),
|
|
622
|
+
async select(table, page, pageSize, where, orderBy) {
|
|
623
|
+
const model = requireModel(table);
|
|
624
|
+
if (type === 'keyvalue') {
|
|
625
|
+
if (!model.entries)
|
|
626
|
+
throw new Error(`Table '${table}' is not a key-value bucket`);
|
|
627
|
+
const all = await model.entries();
|
|
628
|
+
const start = (page - 1) * pageSize;
|
|
629
|
+
return {
|
|
630
|
+
rows: all.slice(start, start + pageSize).map(([key, value]) => ({ key, value })),
|
|
631
|
+
total: all.length,
|
|
632
|
+
page,
|
|
633
|
+
pageSize,
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
const filter = where ?? {};
|
|
637
|
+
const columns = type === 'related' ? await columnsOf(table) : undefined;
|
|
638
|
+
validateWhere(filter, columns, 0);
|
|
639
|
+
const order = orderBy ? validateOrderBy(orderBy, columns) : undefined;
|
|
640
|
+
let total;
|
|
641
|
+
try {
|
|
642
|
+
const counted = await db.aggregate(table).count('*', 'total').where(filter);
|
|
643
|
+
total = Number(counted[0]?.total ?? 0);
|
|
644
|
+
}
|
|
645
|
+
catch {
|
|
646
|
+
const all = await model.select();
|
|
647
|
+
total = all.length;
|
|
648
|
+
}
|
|
649
|
+
let selection = model.select();
|
|
650
|
+
if (Object.keys(filter).length > 0)
|
|
651
|
+
selection = selection.where(filter);
|
|
652
|
+
if (order) {
|
|
653
|
+
selection = selection.orderBy(order.column, order.direction === 'desc' ? 'DESC' : 'ASC');
|
|
654
|
+
// A unique tie-breaker keeps pages stable when many rows share the sorted value.
|
|
655
|
+
const tieBreaker = primaryKeyOf(definitions.get(table) ?? discovered.get(table)) ?? (type === 'document' ? '_id' : undefined);
|
|
656
|
+
if (tieBreaker && tieBreaker !== order.column)
|
|
657
|
+
selection = selection.orderBy(tieBreaker, 'ASC');
|
|
658
|
+
}
|
|
659
|
+
const rows = await selection.limit(pageSize).offset((page - 1) * pageSize);
|
|
660
|
+
return { rows, total, page, pageSize, ...(order ? { orderBy: order } : {}) };
|
|
661
|
+
},
|
|
662
|
+
async insert(table, row) {
|
|
663
|
+
const model = requireModel(table);
|
|
664
|
+
if (type === 'keyvalue') {
|
|
665
|
+
if (!model.set || row.key == null)
|
|
666
|
+
throw new Error('key is required for KV insert');
|
|
667
|
+
await model.set(String(row.key), row.value);
|
|
668
|
+
}
|
|
669
|
+
else if (type === 'document' && model.create) {
|
|
670
|
+
await model.create(row);
|
|
671
|
+
}
|
|
672
|
+
else {
|
|
673
|
+
await model.insert(row);
|
|
674
|
+
}
|
|
675
|
+
},
|
|
676
|
+
async update(table, row, where) {
|
|
677
|
+
const model = requireModel(table);
|
|
678
|
+
if (type === 'keyvalue') {
|
|
679
|
+
if (!model.set || where.key == null)
|
|
680
|
+
throw new Error('key is required for KV update');
|
|
681
|
+
await model.set(String(where.key), row.value);
|
|
682
|
+
return 1;
|
|
683
|
+
}
|
|
684
|
+
if (type === 'document' && where._id != null && model.updateById) {
|
|
685
|
+
return await model.updateById(String(where._id), row);
|
|
686
|
+
}
|
|
687
|
+
return await model.update(row).where(where);
|
|
688
|
+
},
|
|
689
|
+
async delete(table, where) {
|
|
690
|
+
const model = requireModel(table);
|
|
691
|
+
if (type === 'keyvalue') {
|
|
692
|
+
if (!model.deleteByKey || where.key == null)
|
|
693
|
+
throw new Error('key is required for KV delete');
|
|
694
|
+
await model.deleteByKey(String(where.key));
|
|
695
|
+
return 1;
|
|
696
|
+
}
|
|
697
|
+
if (type === 'document' && where._id != null && model.deleteById) {
|
|
698
|
+
return await model.deleteById(String(where._id));
|
|
699
|
+
}
|
|
700
|
+
return await model.delete(where);
|
|
701
|
+
},
|
|
702
|
+
async dropTable(table) {
|
|
703
|
+
requireModel(table);
|
|
704
|
+
await db.query(db.dialect.formatDropTable(table, true));
|
|
705
|
+
db.models.delete(table);
|
|
706
|
+
db.definitions.delete(table);
|
|
707
|
+
tableNames.delete(table);
|
|
708
|
+
definitions.delete(table);
|
|
709
|
+
discovered.delete(table);
|
|
710
|
+
},
|
|
711
|
+
async kvGet(table, key) {
|
|
712
|
+
const model = requireModel(table);
|
|
713
|
+
if (!model.get)
|
|
714
|
+
throw new Error(`Table '${table}' is not a key-value bucket`);
|
|
715
|
+
return await model.get(key);
|
|
716
|
+
},
|
|
717
|
+
async kvSet(table, key, value, ttl) {
|
|
718
|
+
const model = requireModel(table);
|
|
719
|
+
if (!model.set)
|
|
720
|
+
throw new Error(`Table '${table}' is not a key-value bucket`);
|
|
721
|
+
await model.set(key, value, ttl);
|
|
722
|
+
},
|
|
723
|
+
async kvDelete(table, key) {
|
|
724
|
+
const model = requireModel(table);
|
|
725
|
+
if (!model.deleteByKey)
|
|
726
|
+
throw new Error(`Table '${table}' is not a key-value bucket`);
|
|
727
|
+
await model.deleteByKey(key);
|
|
728
|
+
},
|
|
729
|
+
async kvEntries(table) {
|
|
730
|
+
const model = requireModel(table);
|
|
731
|
+
if (!model.entries)
|
|
732
|
+
throw new Error(`Table '${table}' is not a key-value bucket`);
|
|
733
|
+
return (await model.entries()).map(([key, value]) => ({ key, value }));
|
|
734
|
+
},
|
|
735
|
+
async query(sql, options) {
|
|
736
|
+
const dialect = await requireRawSql();
|
|
737
|
+
const on = requireExecutor();
|
|
738
|
+
const statement = sqlOptions.textGuard ? readOnlyConsoleStatement(sql, dialect) : { text: sql, kind: 'unchecked' };
|
|
739
|
+
const maxRows = Math.min(Math.max(1, Math.floor(options?.maxRows ?? CONSOLE_QUERY_MAX_ROWS)), CONSOLE_QUERY_MAX_ROWS);
|
|
740
|
+
const timeoutMs = Math.max(1, Math.floor(options?.timeoutMs ?? CONSOLE_QUERY_TIMEOUT_MS));
|
|
741
|
+
const started = Date.now();
|
|
742
|
+
let read;
|
|
743
|
+
if (dialect === 'sqlite' && db.dialect.supportsIsolatedReads?.() && db.dialect.queryReadOnlyIsolated) {
|
|
744
|
+
// node:sqlite cannot interrupt a statement: run it in a child process, killed at the budget.
|
|
745
|
+
read = await db.dialect.queryReadOnlyIsolated(statement.text, { maxRows, timeoutMs });
|
|
746
|
+
}
|
|
747
|
+
else {
|
|
748
|
+
read = await inTransaction(async (trx) => {
|
|
749
|
+
if (dialect === 'pg' && PG_CURSOR_KINDS.has(statement.kind)) {
|
|
750
|
+
// A cursor reads only the rows the cap keeps instead of the whole result.
|
|
751
|
+
await trx.statement(`DECLARE ${PG_CONSOLE_CURSOR} NO SCROLL CURSOR FOR\n${statement.text}\n`);
|
|
752
|
+
return cappedRows(await trx.statement(`FETCH FORWARD ${maxRows + 1} FROM ${PG_CONSOLE_CURSOR}`), maxRows);
|
|
753
|
+
}
|
|
754
|
+
return cappedRows(await trx.statement(statement.text, { maxRows }), maxRows);
|
|
755
|
+
}, { readOnly: true, rollback: true, timeoutMs }, on);
|
|
756
|
+
}
|
|
757
|
+
const rows = read.rows.map(jsonSafeRow);
|
|
758
|
+
const columns = read.columns?.length ? [...read.columns] : [...new Set(rows.flatMap(row => Object.keys(row)))];
|
|
759
|
+
return Object.freeze({ columns, rows, rowCount: rows.length, truncated: read.truncated, durationMs: Date.now() - started });
|
|
760
|
+
},
|
|
761
|
+
async execute(sql, options) {
|
|
762
|
+
const dialect = await requireRawSql();
|
|
763
|
+
const on = requireExecutor();
|
|
764
|
+
const statements = sqlOptions.textGuard ? writeConsoleStatements(sql, dialect) : [{ text: sql, code: sql.trim().toLowerCase() }];
|
|
765
|
+
const dryRun = options?.dryRun === true;
|
|
766
|
+
const started = Date.now();
|
|
767
|
+
const runAll = async (run) => {
|
|
768
|
+
const results = [];
|
|
769
|
+
for (const statement of statements)
|
|
770
|
+
results.push(statementResult(statement.text, await run(statement.text)));
|
|
771
|
+
return results;
|
|
772
|
+
};
|
|
773
|
+
// Every statement goes through the single-statement protocol, so a COMMIT the lexer missed
|
|
774
|
+
// cannot ride along (and transaction control is refused): a dry run always rolls back.
|
|
775
|
+
// MySQL commits DDL implicitly: only all-DML batches run in a transaction (and can be dry-run).
|
|
776
|
+
const transactional = dialect !== 'mysql' || statements.every(statement => DML.test(statement.code));
|
|
777
|
+
if (!transactional && dryRun) {
|
|
778
|
+
return Object.freeze({ statements: statements.map(statement => ({ sql: statement.text })), dryRun, estimateUnavailable: true, durationMs: 0 });
|
|
779
|
+
}
|
|
780
|
+
const results = transactional
|
|
781
|
+
? await inTransaction(trx => runAll(text => trx.statement(text)), { rollback: dryRun, ...(options?.timeoutMs ? { timeoutMs: options.timeoutMs } : {}) }, on)
|
|
782
|
+
// MySQL DDL: dialect queries are prepared statements (one statement each).
|
|
783
|
+
: await runAll(text => onRestrictedError(on.query(text)));
|
|
784
|
+
if (!dryRun)
|
|
785
|
+
await refreshDiscovered().catch(() => undefined);
|
|
786
|
+
return Object.freeze({ statements: Object.freeze(results), dryRun, durationMs: Date.now() - started });
|
|
787
|
+
},
|
|
788
|
+
redactSql: (sql) => redactConsoleSql(sql, sqlDialect ?? 'mysql'),
|
|
789
|
+
describe,
|
|
790
|
+
previewSchema: plan,
|
|
791
|
+
async applySchema(change, confirm) {
|
|
792
|
+
const dialect = requireSql();
|
|
793
|
+
const planned = await plan(change);
|
|
794
|
+
// The planned DDL travels with a failure too, so the audit log records what was attempted.
|
|
795
|
+
const attempted = (error) => {
|
|
796
|
+
if (error && typeof error === 'object' && !Object.isFrozen(error)) {
|
|
797
|
+
Object.defineProperty(error, 'statements', { value: planned.statements, enumerable: false, configurable: true });
|
|
798
|
+
}
|
|
799
|
+
return error;
|
|
800
|
+
};
|
|
801
|
+
if (planned.destructive && confirm !== planned.confirm) {
|
|
802
|
+
throw attempted(new ConsoleSchemaRefusal('confirm_required', `Type '${planned.confirm}' to confirm this change`));
|
|
803
|
+
}
|
|
804
|
+
try {
|
|
805
|
+
if (dialect === 'mysql') {
|
|
806
|
+
for (const statement of planned.statements)
|
|
807
|
+
await db.query(statement);
|
|
808
|
+
}
|
|
809
|
+
else {
|
|
810
|
+
await inTransaction(async (trx) => { for (const statement of planned.statements)
|
|
811
|
+
await trx.run(statement); }, {});
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
catch (error) {
|
|
815
|
+
throw attempted(error);
|
|
816
|
+
}
|
|
817
|
+
await syncAfterSchemaChange(planned.change);
|
|
818
|
+
return planned;
|
|
819
|
+
},
|
|
820
|
+
};
|
|
821
|
+
return { port: Object.freeze(port), refreshDiscovered, refreshRawSql, stopConsoleSql: closeRestricted };
|
|
822
|
+
}
|
|
823
|
+
/** The main account's name (PostgreSQL `current_user`, MySQL user part of `CURRENT_USER()`). */
|
|
824
|
+
async function mainAccount(dialect, query) {
|
|
825
|
+
const rows = await query(dialect === 'pg' ? 'SELECT current_user AS account' : 'SELECT CURRENT_USER() AS account');
|
|
826
|
+
const account = rows?.[0]?.account;
|
|
827
|
+
if (typeof account !== 'string')
|
|
828
|
+
return undefined;
|
|
829
|
+
return dialect === 'mysql' ? splitMysqlAccount(account).user : account;
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Why the Console must not run raw SQL on this connection, or undefined. The Console lexer reads
|
|
833
|
+
* strings the way the server does by default; a server that treats backslashes (MySQL
|
|
834
|
+
* `NO_BACKSLASH_ESCAPES`, PostgreSQL `standard_conforming_strings = off`) or double quotes (MySQL
|
|
835
|
+
* `ANSI_QUOTES`) differently would split and classify statements differently. SQLite needs a file
|
|
836
|
+
* database: only a separate connection can be stopped at the time limit.
|
|
837
|
+
*/
|
|
838
|
+
export async function rawSqlCompatibilityProblem(dialect, probe) {
|
|
839
|
+
if (dialect === 'sqlite') {
|
|
840
|
+
return probe.isolatedReads() ? undefined : 'The SQL console is unsupported for an in-memory SQLite database (a query could not be stopped at the time limit)';
|
|
841
|
+
}
|
|
842
|
+
if (dialect === 'pg') {
|
|
843
|
+
const rows = await probe.query('SELECT current_setting(\'standard_conforming_strings\') AS value');
|
|
844
|
+
const value = String(rows?.[0]?.value ?? '').toLowerCase();
|
|
845
|
+
return value === 'on' ? undefined : `The SQL console needs standard_conforming_strings = on (the server reports '${value || 'unknown'}'), so that it reads string literals as the server does`;
|
|
846
|
+
}
|
|
847
|
+
const rows = await probe.query('SELECT @@SESSION.sql_mode AS mode');
|
|
848
|
+
const modes = String(rows?.[0]?.mode ?? '').toUpperCase().split(',').map(mode => mode.trim());
|
|
849
|
+
const conflicting = modes.filter(mode => mode === 'NO_BACKSLASH_ESCAPES' || mode === 'ANSI_QUOTES' || mode === 'ANSI');
|
|
850
|
+
return conflicting.length ? `The SQL console is off while sql_mode contains ${conflicting.join(', ')}: the server would read string literals differently from the Console` : undefined;
|
|
851
|
+
}
|
|
852
|
+
function primaryKeyOf(definition) {
|
|
853
|
+
if (!definition)
|
|
854
|
+
return undefined;
|
|
855
|
+
return Object.entries(definition).find(([, column]) => column && typeof column === 'object' && column.primary === true)?.[0];
|
|
856
|
+
}
|
|
857
|
+
function validateOrderBy(orderBy, columns) {
|
|
858
|
+
const column = typeof orderBy?.column === 'string' ? orderBy.column : '';
|
|
859
|
+
if (!column || column.startsWith('$') || (columns && !columns.includes(column)))
|
|
860
|
+
throw new ConsoleSelectRefusal(`Unknown sort column '${column}'`);
|
|
861
|
+
if (orderBy.direction !== 'asc' && orderBy.direction !== 'desc')
|
|
862
|
+
throw new ConsoleSelectRefusal('Sort direction must be asc or desc');
|
|
863
|
+
return Object.freeze({ column, direction: orderBy.direction });
|
|
864
|
+
}
|
|
865
|
+
function isScalar(value) {
|
|
866
|
+
return value === null || typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value));
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* `where` of `db:select`: known columns (related tables), allow-listed operators, scalar values.
|
|
870
|
+
* Column names are checked because the query builder quotes identifiers without escaping.
|
|
871
|
+
*/
|
|
872
|
+
function validateWhere(where, columns, depth) {
|
|
873
|
+
if (depth > 4)
|
|
874
|
+
throw new ConsoleSelectRefusal('Filter is nested too deeply');
|
|
875
|
+
const keys = Object.keys(where);
|
|
876
|
+
if (keys.length > 50)
|
|
877
|
+
throw new ConsoleSelectRefusal('Too many filter conditions');
|
|
878
|
+
for (const key of keys) {
|
|
879
|
+
const value = where[key];
|
|
880
|
+
if (key === '$and' || key === '$or') {
|
|
881
|
+
if (!Array.isArray(value) || !value.length || value.length > 50)
|
|
882
|
+
throw new ConsoleSelectRefusal(`${key} needs a non-empty array of conditions`);
|
|
883
|
+
for (const entry of value) {
|
|
884
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry) || !Object.keys(entry).length)
|
|
885
|
+
throw new ConsoleSelectRefusal(`${key} needs a non-empty array of conditions`);
|
|
886
|
+
validateWhere(entry, columns, depth + 1);
|
|
887
|
+
}
|
|
888
|
+
continue;
|
|
889
|
+
}
|
|
890
|
+
if (key === '$not') {
|
|
891
|
+
if (!value || typeof value !== 'object' || Array.isArray(value) || !Object.keys(value).length)
|
|
892
|
+
throw new ConsoleSelectRefusal('$not needs a condition');
|
|
893
|
+
validateWhere(value, columns, depth + 1);
|
|
894
|
+
continue;
|
|
895
|
+
}
|
|
896
|
+
if (key.startsWith('$') || (columns && !columns.includes(key)))
|
|
897
|
+
throw new ConsoleSelectRefusal(`Unknown filter column '${key}'`);
|
|
898
|
+
if (isScalar(value))
|
|
899
|
+
continue;
|
|
900
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
901
|
+
throw new ConsoleSelectRefusal(`Unsupported filter value for '${key}'`);
|
|
902
|
+
const operators = Object.entries(value);
|
|
903
|
+
// `{}` would compile to no condition (or invalid SQL) instead of filtering.
|
|
904
|
+
if (!operators.length)
|
|
905
|
+
throw new ConsoleSelectRefusal(`Filter on '${key}' needs an operator`);
|
|
906
|
+
for (const [operator, operand] of operators) {
|
|
907
|
+
if (!FILTER_OPERATOR_SET.has(operator))
|
|
908
|
+
throw new ConsoleSelectRefusal(`Unsupported filter operator '${operator}'`);
|
|
909
|
+
const ok = operator === '$in' || operator === '$nin'
|
|
910
|
+
? Array.isArray(operand) && operand.length > 0 && operand.length <= 500 && operand.every(isScalar)
|
|
911
|
+
: operator === '$like' || operator === '$nlike' ? typeof operand === 'string' : isScalar(operand);
|
|
912
|
+
if (!ok)
|
|
913
|
+
throw new ConsoleSelectRefusal(`Unsupported value for ${operator} on '${key}'`);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
function statementResult(sql, result) {
|
|
918
|
+
if (typeof result === 'number')
|
|
919
|
+
return Object.freeze({ sql, affected: result });
|
|
920
|
+
if (Array.isArray(result)) {
|
|
921
|
+
const info = result;
|
|
922
|
+
if (typeof info.rowCount === 'number' && /^(?:INSERT|UPDATE|DELETE|MERGE)$/.test(String(info.command))) {
|
|
923
|
+
return Object.freeze({ sql, affected: info.rowCount });
|
|
924
|
+
}
|
|
925
|
+
return Object.freeze({ sql, ...(result.length ? { rows: result.length } : {}) });
|
|
926
|
+
}
|
|
927
|
+
if (result && typeof result === 'object') {
|
|
928
|
+
const record = result;
|
|
929
|
+
const affected = typeof record.changes === 'number' ? record.changes : typeof record.affectedRows === 'number' ? record.affectedRows : undefined;
|
|
930
|
+
return Object.freeze({ sql, ...(affected !== undefined ? { affected } : {}) });
|
|
931
|
+
}
|
|
932
|
+
return Object.freeze({ sql });
|
|
933
|
+
}
|
|
934
|
+
/** JSON-safe cell values for the RPC reply (bigint, binary, dates). */
|
|
935
|
+
function jsonSafeRow(row) {
|
|
936
|
+
const out = {};
|
|
937
|
+
for (const [key, value] of Object.entries(row ?? {})) {
|
|
938
|
+
if (typeof value === 'bigint')
|
|
939
|
+
out[key] = Number.isSafeInteger(Number(value)) ? Number(value) : value.toString();
|
|
940
|
+
else if (value instanceof Date)
|
|
941
|
+
out[key] = value.toISOString();
|
|
942
|
+
else if (value instanceof Uint8Array)
|
|
943
|
+
out[key] = `<binary ${value.byteLength} bytes>`;
|
|
944
|
+
else
|
|
945
|
+
out[key] = value;
|
|
946
|
+
}
|
|
947
|
+
return out;
|
|
948
|
+
}
|
|
949
|
+
/**
|
|
950
|
+
* Root-only raw registry with the gateway's tables cut off: raw SQL naming them and table-name
|
|
951
|
+
* arguments (`model`, `define`, `select`, …) are refused. Methods run on the real registry.
|
|
952
|
+
*/
|
|
953
|
+
/** Kinds of values a facade passes through unchanged (data, not handles). */
|
|
954
|
+
function isPlainData(value) {
|
|
955
|
+
if (value === null || typeof value !== 'object')
|
|
956
|
+
return true;
|
|
957
|
+
if (Array.isArray(value) || value instanceof Date || value instanceof Uint8Array || value instanceof RegExp || value instanceof Error)
|
|
958
|
+
return true;
|
|
959
|
+
const proto = Object.getPrototypeOf(value);
|
|
960
|
+
return proto === Object.prototype || proto === null;
|
|
961
|
+
}
|
|
962
|
+
/** Properties a facade never exposes, whatever object it wraps. */
|
|
963
|
+
const FACADE_HIDDEN = new Set(['constructor', 'dialect', 'database', 'db', 'config', 'transactionContext', 'connection', 'pool']);
|
|
964
|
+
/**
|
|
965
|
+
* A frozen object exposing only the methods (and primitive-valued fields) of `target`. Every result
|
|
966
|
+
* that is itself a handle (a model, a query builder, a transaction context, a Map of models) is
|
|
967
|
+
* wrapped the same way; function arguments (callbacks) receive wrapped values. Methods are looked
|
|
968
|
+
* up on `target` at call time, so instrumenting the real object (tests spy on `host.db`) still
|
|
969
|
+
* works, while nothing reachable from the facade leads back to the dialect, its config or the
|
|
970
|
+
* driver handle — and the facade itself cannot be modified.
|
|
971
|
+
*/
|
|
972
|
+
function methodFacade(target, guard, cache) {
|
|
973
|
+
const cached = cache.get(target);
|
|
974
|
+
if (cached)
|
|
975
|
+
return cached;
|
|
976
|
+
const facade = Object.create(null);
|
|
977
|
+
cache.set(target, facade);
|
|
978
|
+
const wrapValue = (value) => {
|
|
979
|
+
if (value === target)
|
|
980
|
+
return facade;
|
|
981
|
+
if (value instanceof Promise)
|
|
982
|
+
return value.then(wrapValue);
|
|
983
|
+
if (value instanceof Map)
|
|
984
|
+
return mapFacade(value, guard, cache);
|
|
985
|
+
if (typeof value === 'function')
|
|
986
|
+
return (...args) => wrapValue(value(...args.map(wrapValue)));
|
|
987
|
+
if (isPlainData(value))
|
|
988
|
+
return value;
|
|
989
|
+
return methodFacade(value, guard, cache);
|
|
990
|
+
};
|
|
991
|
+
const names = new Set();
|
|
992
|
+
for (let level = target; level && level !== Object.prototype; level = Object.getPrototypeOf(level)) {
|
|
993
|
+
for (const name of Object.getOwnPropertyNames(level))
|
|
994
|
+
names.add(name);
|
|
995
|
+
}
|
|
996
|
+
for (const name of names) {
|
|
997
|
+
if (FACADE_HIDDEN.has(name) || name.startsWith('_') || name.startsWith('#'))
|
|
998
|
+
continue;
|
|
999
|
+
let probe;
|
|
1000
|
+
try {
|
|
1001
|
+
probe = Reflect.get(target, name, target);
|
|
1002
|
+
}
|
|
1003
|
+
catch {
|
|
1004
|
+
continue;
|
|
1005
|
+
}
|
|
1006
|
+
if (typeof probe === 'function') {
|
|
1007
|
+
facade[name] = (...args) => {
|
|
1008
|
+
const checked = guard(name, args) ?? args;
|
|
1009
|
+
const method = Reflect.get(target, name, target);
|
|
1010
|
+
// Callbacks (transactions, `then`) receive wrapped handles.
|
|
1011
|
+
const passed = checked.map(arg => (typeof arg === 'function' ? (...inner) => arg(...inner.map(wrapValue)) : arg));
|
|
1012
|
+
return wrapValue(method.apply(target, passed));
|
|
1013
|
+
};
|
|
1014
|
+
}
|
|
1015
|
+
else if (probe instanceof Map) {
|
|
1016
|
+
Object.defineProperty(facade, name, { enumerable: true, get: () => mapFacade(Reflect.get(target, name, target), guard, cache) });
|
|
1017
|
+
}
|
|
1018
|
+
else if (probe === undefined || probe === null || typeof probe !== 'object') {
|
|
1019
|
+
Object.defineProperty(facade, name, { enumerable: true, get: () => { const value = Reflect.get(target, name, target); return value !== null && typeof value === 'object' ? undefined : value; } });
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return Object.freeze(facade);
|
|
1023
|
+
}
|
|
1024
|
+
/** Read-only, fully wrapped view of a Map (every way of reading it: get, iteration, forEach). */
|
|
1025
|
+
function mapFacade(map, guard, cache) {
|
|
1026
|
+
const cached = cache.get(map);
|
|
1027
|
+
if (cached)
|
|
1028
|
+
return cached;
|
|
1029
|
+
const wrap = (value) => (isPlainData(value) || typeof value !== 'object' ? value : methodFacade(value, guard, cache));
|
|
1030
|
+
const entries = function* () { for (const [key, value] of map)
|
|
1031
|
+
yield [key, wrap(value)]; };
|
|
1032
|
+
const view = Object.freeze({
|
|
1033
|
+
get: (key) => { guard('get', [key]); return wrap(map.get(key)); },
|
|
1034
|
+
has: (key) => map.has(key),
|
|
1035
|
+
get size() { return map.size; },
|
|
1036
|
+
keys: () => map.keys(),
|
|
1037
|
+
values: function* () { for (const [, value] of entries())
|
|
1038
|
+
yield value; },
|
|
1039
|
+
entries,
|
|
1040
|
+
forEach: (callback) => { for (const [key, value] of entries())
|
|
1041
|
+
callback(value, key, view); },
|
|
1042
|
+
[Symbol.iterator]: entries,
|
|
1043
|
+
[Symbol.toStringTag]: 'Map',
|
|
1044
|
+
});
|
|
1045
|
+
cache.set(map, view);
|
|
1046
|
+
return view;
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* The raw registry as plugins get it (`getRawDatabase()`): an explicit facade of its methods
|
|
1050
|
+
* (`query`, `transaction` with a wrapped transaction context, `model` / `models` with every Map
|
|
1051
|
+
* read wrapped, `select` / `insert` / `update` / `delete` / … returning wrapped builders), frozen,
|
|
1052
|
+
* with no `dialect`, `database`, `config` or driver handle anywhere behind it. Gateway tables are
|
|
1053
|
+
* refused: raw SQL naming them and table-name arguments. Plugins are trusted code in the same
|
|
1054
|
+
* process and user (ADR 0059 §1): this keeps accidental and Console / Agent-driven access off the
|
|
1055
|
+
* gateway's data; it does not contain a malicious plugin.
|
|
1056
|
+
*/
|
|
1057
|
+
function guardRawDatabase(db) {
|
|
1058
|
+
const guard = (_method, args) => {
|
|
1059
|
+
// Every method, every argument (not by method name: `query`, `queryStatement`, `$raw`
|
|
1060
|
+
// conditions, …): each string anywhere in the arguments is SQL text as far as this check
|
|
1061
|
+
// knows. Data that literally names a gateway table is refused as well (fail closed).
|
|
1062
|
+
const checked = args.map(arg => checkedSqlArgument(arg, 0));
|
|
1063
|
+
const [first] = checked;
|
|
1064
|
+
if (typeof first === 'string')
|
|
1065
|
+
assertInstanceTable(first);
|
|
1066
|
+
return checked;
|
|
1067
|
+
};
|
|
1068
|
+
return methodFacade(db, guard, new WeakMap());
|
|
1069
|
+
}
|
|
1070
|
+
const SQL_ARGUMENT_MAX_DEPTH = 16;
|
|
1071
|
+
/**
|
|
1072
|
+
* Checks one raw-handle argument for gateway table names, at any depth of arrays and plain
|
|
1073
|
+
* objects, and returns the checked copy that goes to the driver: arrays and plain objects are
|
|
1074
|
+
* copied (each element / property read once), string-like values (`new String(…)`, objects with
|
|
1075
|
+
* their own `toString` / `valueOf` / `Symbol.toPrimitive`) are converted once. Nothing can read
|
|
1076
|
+
* differently at check and at use (a getter, a `toString` answering differently the second time).
|
|
1077
|
+
*/
|
|
1078
|
+
function checkedSqlArgument(value, depth) {
|
|
1079
|
+
if (typeof value === 'string') {
|
|
1080
|
+
if (referencesGatewayOwnedTable(value))
|
|
1081
|
+
throw new GatewayOwnedTableError();
|
|
1082
|
+
return value;
|
|
1083
|
+
}
|
|
1084
|
+
if (value === null || typeof value !== 'object')
|
|
1085
|
+
return value;
|
|
1086
|
+
if (depth >= SQL_ARGUMENT_MAX_DEPTH)
|
|
1087
|
+
throw new TypeError('Raw database argument is nested too deeply');
|
|
1088
|
+
if (value instanceof String)
|
|
1089
|
+
return checkedSqlArgument(String.prototype.valueOf.call(value), depth + 1);
|
|
1090
|
+
if (value instanceof Date || ArrayBuffer.isView(value) || value instanceof ArrayBuffer)
|
|
1091
|
+
return value;
|
|
1092
|
+
if (Array.isArray(value))
|
|
1093
|
+
return Array.from(value, item => checkedSqlArgument(item, depth + 1));
|
|
1094
|
+
const proto = Object.getPrototypeOf(value);
|
|
1095
|
+
if (proto === Object.prototype || proto === null) {
|
|
1096
|
+
if (stringConvertible(value))
|
|
1097
|
+
return checkedSqlArgument(String(value), depth + 1);
|
|
1098
|
+
const copy = Object.create(proto);
|
|
1099
|
+
for (const [key, item] of Object.entries(value)) {
|
|
1100
|
+
if (referencesGatewayOwnedTable(key))
|
|
1101
|
+
throw new GatewayOwnedTableError();
|
|
1102
|
+
Object.defineProperty(copy, key, { value: checkedSqlArgument(item, depth + 1), enumerable: true, writable: true, configurable: true });
|
|
1103
|
+
}
|
|
1104
|
+
return copy;
|
|
1105
|
+
}
|
|
1106
|
+
// Other class instances: string-convertible ones are what a driver would stringify.
|
|
1107
|
+
return stringConvertible(value) ? checkedSqlArgument(String(value), depth + 1) : value;
|
|
1108
|
+
}
|
|
1109
|
+
/** An object whose string conversion is its own code (`toString`, `valueOf`, `Symbol.toPrimitive`). */
|
|
1110
|
+
function stringConvertible(value) {
|
|
1111
|
+
const own = (name) => {
|
|
1112
|
+
const method = value[name];
|
|
1113
|
+
return typeof method === 'function' && method !== Object.prototype[name];
|
|
1114
|
+
};
|
|
1115
|
+
return typeof value[Symbol.toPrimitive] === 'function' || own('toString') || own('valueOf');
|
|
1116
|
+
}
|
|
1117
|
+
function databaseType(dialect) {
|
|
1118
|
+
if (dialect === 'mongodb')
|
|
1119
|
+
return 'document';
|
|
1120
|
+
if (dialect === 'redis')
|
|
1121
|
+
return 'keyvalue';
|
|
1122
|
+
return 'related';
|
|
1123
|
+
}
|
|
1124
|
+
export async function resolveDatabaseConfig(projectRoot, config) {
|
|
1125
|
+
const document = await readConfigDocument(config);
|
|
1126
|
+
const configured = document && typeof document === 'object'
|
|
1127
|
+
? document.database
|
|
1128
|
+
: undefined;
|
|
1129
|
+
if (configured && typeof configured === 'object' && !Array.isArray(configured)) {
|
|
1130
|
+
const value = configured;
|
|
1131
|
+
const dialect = typeof value.dialect === 'string' ? value.dialect : 'sqlite';
|
|
1132
|
+
return normalizeDatabaseHostConfig({ ...value, dialect });
|
|
1133
|
+
}
|
|
1134
|
+
const filename = join(projectRoot, '.zhin', 'data.sqlite');
|
|
1135
|
+
await mkdir(dirname(filename), { recursive: true });
|
|
1136
|
+
return Object.freeze({ dialect: 'sqlite', filename });
|
|
1137
|
+
}
|
|
1138
|
+
export function installDatabaseHost(host) {
|
|
1139
|
+
return ({ resources, handoff, admission }) => {
|
|
1140
|
+
// Domain tables (e.g. github_oauth_users) are defined by owning plugins in setup().
|
|
1141
|
+
// Plugins receive a root-scoped facade through the historical token. The
|
|
1142
|
+
// raw host remains available only to CLI composition through its root token.
|
|
1143
|
+
resources.provide(databaseRootHostToken, host);
|
|
1144
|
+
resources.provide(databaseHostToken, new PluginDatabaseHost(rootPluginId(), host, admission));
|
|
1145
|
+
// Host 是进程级共享资源:不能随世代 dispose / 回滚 stop(旧世代仍在用)。
|
|
1146
|
+
// start 幂等,stop 由进程退出路径(start-command control.stop)统一触发。
|
|
1147
|
+
handoff.add({
|
|
1148
|
+
activateNext: async (signal) => {
|
|
1149
|
+
signal.throwIfAborted();
|
|
1150
|
+
await host.start();
|
|
1151
|
+
signal.throwIfAborted();
|
|
1152
|
+
},
|
|
1153
|
+
});
|
|
1154
|
+
};
|
|
1155
|
+
}
|
|
1156
|
+
async function readConfigDocument(config) {
|
|
1157
|
+
if (!isConfigDocumentPort(config))
|
|
1158
|
+
return config;
|
|
1159
|
+
return (await config.read()).document;
|
|
1160
|
+
}
|
|
1161
|
+
function isConfigDocumentPort(value) {
|
|
1162
|
+
if (!value || typeof value !== 'object')
|
|
1163
|
+
return false;
|
|
1164
|
+
const candidate = value;
|
|
1165
|
+
return typeof candidate.read === 'function';
|
|
1166
|
+
}
|