@zhin.js/gateway 1.1.2 → 1.2.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 +4 -4
- package/lib/database/config.js +31 -0
- package/lib/database/console-role.d.ts +35 -0
- package/lib/database/console-role.js +108 -0
- package/lib/database/gateway-store.d.ts +43 -0
- package/lib/database/gateway-store.js +90 -0
- package/lib/database/index.d.ts +2 -0
- package/lib/database/index.js +2 -0
- package/lib/database/project-database.d.ts +45 -0
- package/lib/database/project-database.js +212 -6
- package/lib/http/gateway-console.d.ts +15 -8
- package/lib/http/gateway-console.js +128 -45
- package/lib/http/plugin-switch-rpc.js +5 -1
- package/lib/http/start-gateway.js +9 -0
- package/lib/iam/index.d.ts +4 -3
- package/lib/iam/index.js +4 -3
- package/lib/iam/install.d.ts +17 -11
- package/lib/iam/install.js +39 -18
- package/lib/iam/legacy-import.d.ts +151 -0
- package/lib/iam/legacy-import.js +600 -0
- package/lib/iam/local-admin.d.ts +2 -0
- package/lib/iam/local-admin.js +6 -1
- package/lib/iam/routes.js +10 -2
- package/lib/iam/rpc.js +1 -1
- package/lib/iam/service.d.ts +31 -0
- package/lib/iam/service.js +49 -4
- package/lib/iam/setup-service.d.ts +2 -0
- package/lib/iam/setup-service.js +11 -4
- package/lib/iam/tables.d.ts +6 -6
- package/lib/iam/tables.js +6 -8
- package/lib/local/protocol.d.ts +6 -0
- package/lib/local/protocol.js +6 -0
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Zhin.js 管理网关([ADR 0054](../../../docs/adr/0054-management-gateway.md))。网关负责宿主层面的事:Console 账号与权限、终端、
|
|
4
4
|
项目配置与 `.env`、依赖事务、实例监管。它**不依赖 `zhin.js` 与 IM 层**,不加载插件代码(`pnpm check:architecture` 强制)。
|
|
5
5
|
|
|
6
|
-
网关进程(`zhin start`;前台运行用 `zhin start --foreground
|
|
6
|
+
网关进程(`zhin start`;前台运行用 `zhin start --foreground`)把 `console_*` 账号表放在自己的 `.zhin/gateway.db`(ADR 0059,与项目数据库方言无关;升级时自动从项目数据库迁入):登录、初始化、`iam.*`、`onboarding.update`、终端与审计
|
|
7
7
|
都在网关里,没有实例时 Console 照样可用;受管实例只按转发的主体做授权。`--standalone`(开发 / 测试,无网关)的实例仍在进程内
|
|
8
8
|
装配 `installConsoleIam` / `installHostConsoleTerminal`。
|
|
9
9
|
|
|
@@ -27,7 +27,7 @@ zhin console init # 首次创建 Console 所有者;之后 http.token
|
|
|
27
27
|
|
|
28
28
|
| 子路径 | 内容 |
|
|
29
29
|
|---|---|
|
|
30
|
-
| `@zhin.js/gateway/iam` | Console 账号、角色、会话、API 令牌、审计与初始化(ADR 0049):`ConsoleIamService`、`ConsoleSetupService`、`installConsoleIam`、`registerConsoleAuthRoutes`、`dispatchIamRpc` |
|
|
30
|
+
| `@zhin.js/gateway/iam` | Console 账号、角色、会话、API 令牌、审计与初始化(ADR 0049):`ConsoleIamService`、`ConsoleSetupService`、`installConsoleIam`、`registerConsoleAuthRoutes`、`dispatchIamRpc`、`importLegacyIamTables` / `revertLegacyIamImport` / `inspectLegacyIamImport`(旧版项目库账号表迁入 gateway.db 与回退,ADR 0059) |
|
|
31
31
|
| `@zhin.js/gateway/terminal` | Console 终端(ADR 0051):`TerminalService`、`installHostConsoleTerminal`、`parseTerminalConfig` |
|
|
32
32
|
| `@zhin.js/gateway/config` | 原始配置文件与 `.env` 读写:`ConsoleConfigurationStore`、`readEnvFile` / `writeEnvFile`、配置路径与补丁、Console Schema |
|
|
33
33
|
| `@zhin.js/gateway/plugin-management` | 项目级依赖事务:`PluginManagementWorkflow`、`PluginManagementActivity`、插件用例 |
|
|
@@ -36,8 +36,8 @@ zhin console init # 首次创建 Console 所有者;之后 http.token
|
|
|
36
36
|
| `@zhin.js/gateway/usage` | 用量统计聚合 |
|
|
37
37
|
| `@zhin.js/gateway/onboarding` | Console 引导(ADR 0053):`.zhin/onboarding.json` 状态(`OnboardingStateStore`、自动识别已配置步骤)、`OnboardingService`、安装白名单与 `authorizeInstall`、流式 `installPackages` |
|
|
38
38
|
| `@zhin.js/gateway/http` | HTML / SSE 响应工具(实例也导入,不加载 Node HTTP 栈) |
|
|
39
|
-
| `@zhin.js/gateway/server` | 网关 HTTP 服务器(`GatewayHttpServer`:自有路由、`{base}` 认证后转发、其他路由原样转发、实例离线 503 / 超时 504 / 其他 502;`useConsoleRpc` 登记更多网关 RPC)、`gateway.*` Console RPC 路由、`startGatewayConsole
|
|
40
|
-
| `@zhin.js/gateway/database` |
|
|
39
|
+
| `@zhin.js/gateway/server` | 网关 HTTP 服务器(`GatewayHttpServer`:自有路由、`{base}` 认证后转发、其他路由原样转发、实例离线 503 / 超时 504 / 其他 502;`useConsoleRpc` 登记更多网关 RPC)、`gateway.*` Console RPC 路由、`startGatewayConsole`(gateway.db、旧表迁移、本地认证、`/auth/*`、`iam.*`、`onboarding.update`、初始化向导、终端、审计)、`registerGatewayEventRoutes`(合并 `{base}/events`、`{base}/gateway/packages/progress`)、`startGateway`(服务器 + Console + 实例监管 + `@zhin.js/host-http/bridge` 实例传输)、`runGateway`(`zhin start` 的监管 + 网关入口) |
|
|
40
|
+
| `@zhin.js/gateway/database` | 网关自己的存储与临时项目库连接(ADR 0054 §7、ADR 0059):`openGatewayStore`(`.zhin/gateway.db`,`0600`,`gateway_meta`)、`loadProjectDatabaseConfig` / `resolveProjectDatabaseConfig`(与实例 `resolveDatabaseConfig` 同一规则,含 `database.console`)、`ProjectDatabase`(SQLite WAL;不用同步的 `busy_timeout`,`SQLITE_BUSY` 以异步退避重试,默认 5 秒预算,超时返回 `503 database_busy` 与 `Retry-After`;`tableExists` / `readSnapshot` / `dropTables` 供迁移使用)、`setupConsoleRole`(`zhin db console-role`:PostgreSQL / MySQL 的 Console SQL 受限账号) |
|
|
41
41
|
| `@zhin.js/gateway` | 以上全部 |
|
|
42
42
|
|
|
43
43
|
```ts
|
package/lib/database/config.js
CHANGED
|
@@ -53,6 +53,32 @@ function normalizeRedisDatabase(value) {
|
|
|
53
53
|
}
|
|
54
54
|
return normalized;
|
|
55
55
|
}
|
|
56
|
+
/** `database.console` (ADR 0059 §4; port of the instance's check): PostgreSQL / MySQL only. */
|
|
57
|
+
function normalizeConsoleCredential(config) {
|
|
58
|
+
if (!('console' in config) || config.console === undefined)
|
|
59
|
+
return;
|
|
60
|
+
const value = config.console;
|
|
61
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
62
|
+
throw configurationError('database.console', '必须是对象 { user, password }');
|
|
63
|
+
const block = { ...value };
|
|
64
|
+
// `${ZHIN_DB_CONSOLE_*}` not set (yet): no restricted account — raw SQL stays off, the rest works.
|
|
65
|
+
if ((block.user === undefined || block.user === '') && (block.password === undefined || block.password === '')) {
|
|
66
|
+
delete config.console;
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
normalizeRequiredText(block, 'user', 'database.console.user', 'ZHIN_DB_CONSOLE_USER=zhin_console_xxxxxxxx');
|
|
70
|
+
if (typeof block.password !== 'string' || block.password === '') {
|
|
71
|
+
throw configurationError('database.console.password', '不能为空', 'ZHIN_DB_CONSOLE_PASSWORD=…(zhin db console-role 生成)');
|
|
72
|
+
}
|
|
73
|
+
if (String(block.user) === String(config.user ?? '')) {
|
|
74
|
+
throw configurationError('database.console.user', '不能与主账号 database.user 相同(原始 SQL 只在受限账号上执行)');
|
|
75
|
+
}
|
|
76
|
+
for (const key of Object.keys(block)) {
|
|
77
|
+
if (key !== 'user' && key !== 'password')
|
|
78
|
+
throw configurationError(`database.console.${key}`, '不支持(只有 user 与 password)');
|
|
79
|
+
}
|
|
80
|
+
config.console = Object.freeze({ user: block.user, password: block.password });
|
|
81
|
+
}
|
|
56
82
|
/** Validates an expanded `database` block (port of `normalizeDatabaseHostConfig`). */
|
|
57
83
|
export function normalizeProjectDatabaseConfig(input) {
|
|
58
84
|
const dialect = input.dialect;
|
|
@@ -60,6 +86,9 @@ export function normalizeProjectDatabaseConfig(input) {
|
|
|
60
86
|
throw new TypeError(`数据库配置 database.dialect 不支持 ${JSON.stringify(dialect)};可选值为 sqlite、mysql、pg、mongodb、redis、memory。`);
|
|
61
87
|
}
|
|
62
88
|
const config = { ...input, dialect };
|
|
89
|
+
if (dialect !== 'pg' && dialect !== 'mysql')
|
|
90
|
+
delete config.console;
|
|
91
|
+
delete config.denyAttach;
|
|
63
92
|
switch (dialect) {
|
|
64
93
|
case 'mysql':
|
|
65
94
|
normalizeOptionalText(config, 'host', 'database.host', 'DB_HOST=127.0.0.1');
|
|
@@ -69,6 +98,7 @@ export function normalizeProjectDatabaseConfig(input) {
|
|
|
69
98
|
config.port = normalizePort(config.port, 'database.port', 'DB_PORT=3306');
|
|
70
99
|
if (config.port === undefined)
|
|
71
100
|
delete config.port;
|
|
101
|
+
normalizeConsoleCredential(config);
|
|
72
102
|
break;
|
|
73
103
|
case 'pg':
|
|
74
104
|
normalizeOptionalText(config, 'host', 'database.host', 'DB_HOST=127.0.0.1');
|
|
@@ -78,6 +108,7 @@ export function normalizeProjectDatabaseConfig(input) {
|
|
|
78
108
|
config.port = normalizePort(config.port, 'database.port', 'DB_PORT=5432');
|
|
79
109
|
if (config.port === undefined)
|
|
80
110
|
delete config.port;
|
|
111
|
+
normalizeConsoleCredential(config);
|
|
81
112
|
break;
|
|
82
113
|
case 'mongodb':
|
|
83
114
|
normalizeRequiredText(config, 'url', 'database.url', 'DB_URL=mongodb://127.0.0.1:27017');
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type ConsoleCredentialDialect } from '@zhin.js/database';
|
|
2
|
+
export declare const CONSOLE_ROLE_USER_ENV = "ZHIN_DB_CONSOLE_USER";
|
|
3
|
+
export declare const CONSOLE_ROLE_PASSWORD_ENV = "ZHIN_DB_CONSOLE_PASSWORD";
|
|
4
|
+
/** Stands for the password in `--print-sql` output (the real one is only written to `.env`). */
|
|
5
|
+
export declare const CONSOLE_ROLE_PASSWORD_PLACEHOLDER = "<ZHIN_DB_CONSOLE_PASSWORD>";
|
|
6
|
+
export declare class ConsoleRoleError extends Error {
|
|
7
|
+
readonly code: 'unsupported' | 'unavailable' | 'pending_migration' | 'denied';
|
|
8
|
+
constructor(code: 'unsupported' | 'unavailable' | 'pending_migration' | 'denied', message: string, options?: {
|
|
9
|
+
cause?: unknown;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export interface SetupConsoleRoleOptions {
|
|
13
|
+
readonly projectRoot: string;
|
|
14
|
+
/** Selects `.env.<environment>` for `${…}` (default `development`). */
|
|
15
|
+
readonly environment?: string;
|
|
16
|
+
/** Role name (default `zhin_console_<hash of the project root>`). */
|
|
17
|
+
readonly role?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Do not create the account: write the password to `.env` and the reference to the config,
|
|
20
|
+
* and return the statements for a DBA (password shown as {@link CONSOLE_ROLE_PASSWORD_PLACEHOLDER}).
|
|
21
|
+
*/
|
|
22
|
+
readonly printSql?: boolean;
|
|
23
|
+
}
|
|
24
|
+
export interface SetupConsoleRoleResult {
|
|
25
|
+
readonly dialect: ConsoleCredentialDialect;
|
|
26
|
+
readonly user: string;
|
|
27
|
+
/** Created / rotated by this run (false with `printSql`). */
|
|
28
|
+
readonly provisioned: boolean;
|
|
29
|
+
/** The statements (password replaced by the placeholder). */
|
|
30
|
+
readonly statements: readonly string[];
|
|
31
|
+
readonly envFile: string;
|
|
32
|
+
}
|
|
33
|
+
export declare function setupConsoleRole(options: SetupConsoleRoleOptions): Promise<SetupConsoleRoleResult>;
|
|
34
|
+
/** The default role name of a project (what `zhin db console-role` creates). */
|
|
35
|
+
export declare function defaultConsoleRoleName(projectRoot: string): string;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `zhin db console-role` and `zhin doctor --fix` (ADR 0059 §4): create the restricted database
|
|
3
|
+
* account raw Console SQL runs on (PostgreSQL / MySQL), with the main account, store its password
|
|
4
|
+
* in `.env` and reference it from `database.console`. Never run at instance start: the instance
|
|
5
|
+
* only probes the account (and refreshes PostgreSQL grants).
|
|
6
|
+
*/
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import { createConfigDocument } from '@zhin.js/runtime';
|
|
9
|
+
import { DEFAULT_ROOT_CONFIG_FILE_NAME } from '@zhin.js/plugin-runtime';
|
|
10
|
+
import { consoleRoleName, generateConsolePassword, planConsoleRole, provisionConsoleRole, scrubSecret, } from '@zhin.js/database';
|
|
11
|
+
import { ConsoleConfigurationStore } from '../config/configuration.js';
|
|
12
|
+
import { readEnvFile, writeEnvFile } from '../config/environment-files.js';
|
|
13
|
+
import { upsertDotenv } from '../iam/setup-service.js';
|
|
14
|
+
import { IAM_TABLES } from '../iam/tables.js';
|
|
15
|
+
import { findConfigFile } from '../project/config-file.js';
|
|
16
|
+
import { loadExpandedProjectConfig, resolveProjectDatabaseConfig } from './config.js';
|
|
17
|
+
import { ProjectDatabase } from './project-database.js';
|
|
18
|
+
export const CONSOLE_ROLE_USER_ENV = 'ZHIN_DB_CONSOLE_USER';
|
|
19
|
+
export const CONSOLE_ROLE_PASSWORD_ENV = 'ZHIN_DB_CONSOLE_PASSWORD';
|
|
20
|
+
/** Stands for the password in `--print-sql` output (the real one is only written to `.env`). */
|
|
21
|
+
export const CONSOLE_ROLE_PASSWORD_PLACEHOLDER = `<${CONSOLE_ROLE_PASSWORD_ENV}>`;
|
|
22
|
+
export class ConsoleRoleError extends Error {
|
|
23
|
+
code;
|
|
24
|
+
constructor(code, message, options) {
|
|
25
|
+
super(message, options);
|
|
26
|
+
this.code = code;
|
|
27
|
+
this.name = 'ConsoleRoleError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const ENV_REFERENCE = { user: `\${${CONSOLE_ROLE_USER_ENV}}`, password: `\${${CONSOLE_ROLE_PASSWORD_ENV}}` };
|
|
31
|
+
export async function setupConsoleRole(options) {
|
|
32
|
+
const { projectRoot } = options;
|
|
33
|
+
const expanded = await loadExpandedProjectConfig(projectRoot, options.environment ?? 'development');
|
|
34
|
+
const config = await resolveProjectDatabaseConfig(projectRoot, expanded);
|
|
35
|
+
if (config.dialect !== 'pg' && config.dialect !== 'mysql') {
|
|
36
|
+
throw new ConsoleRoleError('unsupported', `项目数据库是 ${config.dialect}:只有 PostgreSQL / MySQL 需要受限账号(SQLite 的 Console 账号在另一个文件里,且实例连接拒绝 ATTACH)`);
|
|
37
|
+
}
|
|
38
|
+
const dialect = config.dialect;
|
|
39
|
+
const main = new ProjectDatabase(config);
|
|
40
|
+
try {
|
|
41
|
+
await main.start();
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
throw new ConsoleRoleError('unavailable', `无法用主账号连接项目数据库:${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
45
|
+
}
|
|
46
|
+
const password = generateConsolePassword();
|
|
47
|
+
let statements;
|
|
48
|
+
let user;
|
|
49
|
+
try {
|
|
50
|
+
// The old console_* tables would be inside a MySQL database-wide grant: import them first.
|
|
51
|
+
const legacy = await main.tableExists(Object.values(IAM_TABLES));
|
|
52
|
+
if (legacy.length) {
|
|
53
|
+
throw new ConsoleRoleError('pending_migration', `项目数据库里还有旧版的 Console 账号表(${legacy.join(', ')}):先运行 zhin start 或 zhin console migrate 完成迁移`);
|
|
54
|
+
}
|
|
55
|
+
const run = (sql, params) => main.query(sql, params);
|
|
56
|
+
const request = { dialect, projectKey: projectRoot, password, ...(options.role ? { role: options.role } : {}) };
|
|
57
|
+
if (options.printSql) {
|
|
58
|
+
const plan = await planConsoleRole(run, request);
|
|
59
|
+
statements = plan.statements;
|
|
60
|
+
user = plan.user;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
try {
|
|
64
|
+
const plan = await provisionConsoleRole(run, request);
|
|
65
|
+
statements = plan.statements;
|
|
66
|
+
user = plan.user;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
// The message never carries the new password (provisionConsoleRole scrubs; scrub again here).
|
|
70
|
+
throw new ConsoleRoleError('denied', scrubSecret(`主账号无法创建受限账号(${error instanceof Error ? error.message : String(error)});改用 zhin db console-role --print-sql 把 SQL 交给 DBA 执行`, [password]));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (error instanceof ConsoleRoleError)
|
|
76
|
+
throw error;
|
|
77
|
+
throw new ConsoleRoleError('unavailable', scrubSecret(error instanceof Error ? error.message : String(error), [password]));
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
await main.stop().catch(() => undefined);
|
|
81
|
+
}
|
|
82
|
+
// The password lives only in .env (ADR 0057); the config references it.
|
|
83
|
+
await writeEnvFile(projectRoot, '.env', upsertDotenv(await readEnvFile(projectRoot, '.env'), {
|
|
84
|
+
[CONSOLE_ROLE_USER_ENV]: user,
|
|
85
|
+
[CONSOLE_ROLE_PASSWORD_ENV]: password,
|
|
86
|
+
}));
|
|
87
|
+
const store = new ConsoleConfigurationStore({
|
|
88
|
+
projectRoot,
|
|
89
|
+
document: createConfigDocument(join(projectRoot, findConfigFile(projectRoot) ?? DEFAULT_ROOT_CONFIG_FILE_NAME)),
|
|
90
|
+
});
|
|
91
|
+
const document = await store.readDocument();
|
|
92
|
+
const block = document.database && typeof document.database === 'object' && !Array.isArray(document.database)
|
|
93
|
+
? document.database
|
|
94
|
+
: { dialect };
|
|
95
|
+
await store.setKey('database', { ...block, console: { ...ENV_REFERENCE } });
|
|
96
|
+
return Object.freeze({
|
|
97
|
+
dialect,
|
|
98
|
+
user,
|
|
99
|
+
provisioned: !options.printSql,
|
|
100
|
+
// PostgreSQL statements carry a SCRAM verifier, never the password; MySQL's are shown with a placeholder.
|
|
101
|
+
statements: Object.freeze(statements.map(statement => scrubSecret(statement, [password]).split('[redacted]').join(CONSOLE_ROLE_PASSWORD_PLACEHOLDER))),
|
|
102
|
+
envFile: '.env',
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/** The default role name of a project (what `zhin db console-role` creates). */
|
|
106
|
+
export function defaultConsoleRoleName(projectRoot) {
|
|
107
|
+
return consoleRoleName(projectRoot);
|
|
108
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { DatabaseHostModel } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import { type ProjectDatabaseTransaction } from './project-database.js';
|
|
3
|
+
/** Project-relative path of the gateway store. */
|
|
4
|
+
export declare const GATEWAY_STORE_FILE: string;
|
|
5
|
+
/** Key / value table of the gateway store (`schema_version`, `legacy_import`). */
|
|
6
|
+
export declare const GATEWAY_META_TABLE = "gateway_meta";
|
|
7
|
+
/** Schema version written to `gateway_meta.schema_version`. */
|
|
8
|
+
export declare const GATEWAY_STORE_SCHEMA_VERSION = 1;
|
|
9
|
+
export interface GatewayStore {
|
|
10
|
+
/** Absolute path of `gateway.db`. */
|
|
11
|
+
readonly filename: string;
|
|
12
|
+
/**
|
|
13
|
+
* Random id of this store (`gateway_meta.store_id`), also written to the marker in the project
|
|
14
|
+
* database (ADR 0059 §3 修订): a new or different gateway.db is noticed instead of reopening setup.
|
|
15
|
+
*/
|
|
16
|
+
readonly storeId: string;
|
|
17
|
+
readonly started: boolean;
|
|
18
|
+
readonly models: {
|
|
19
|
+
get(name: string): DatabaseHostModel | undefined;
|
|
20
|
+
};
|
|
21
|
+
/** JSON value of a `gateway_meta` key, or undefined. */
|
|
22
|
+
getMeta<T = unknown>(key: string, trx?: ProjectDatabaseTransaction): Promise<T | undefined>;
|
|
23
|
+
setMeta(key: string, value: unknown, trx?: ProjectDatabaseTransaction): Promise<void>;
|
|
24
|
+
deleteMeta(key: string, trx?: ProjectDatabaseTransaction): Promise<void>;
|
|
25
|
+
/** A write transaction (`BEGIN IMMEDIATE`: also the lock between the gateway and the CLI). */
|
|
26
|
+
transaction<T>(callback: (trx: ProjectDatabaseTransaction) => Promise<T>): Promise<T>;
|
|
27
|
+
query<T = unknown>(sql: string, params?: unknown[]): Promise<T>;
|
|
28
|
+
/** Rows in each of the access-control tables. */
|
|
29
|
+
counts(trx?: ProjectDatabaseTransaction): Promise<Readonly<Record<string, number>>>;
|
|
30
|
+
/** A consistent copy of `gateway.db` (0600). */
|
|
31
|
+
backupTo(destination: string): Promise<void>;
|
|
32
|
+
close(): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
export interface OpenGatewayStoreOptions {
|
|
35
|
+
/** Overrides the file (tests). Default `<projectRoot>/.zhin/gateway.db`. */
|
|
36
|
+
readonly filename?: string;
|
|
37
|
+
/** `SQLITE_BUSY` async retry budget (default {@link GATEWAY_SQLITE_BUSY_RETRY_MS}). */
|
|
38
|
+
readonly busyRetryMs?: number;
|
|
39
|
+
}
|
|
40
|
+
/** `<projectRoot>/.zhin/gateway.db`. */
|
|
41
|
+
export declare function gatewayStorePath(projectRoot: string): string;
|
|
42
|
+
/** Opens (creating if needed) the gateway store with its tables. */
|
|
43
|
+
export declare function openGatewayStore(projectRoot: string, options?: OpenGatewayStoreOptions): Promise<GatewayStore>;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gateway's own store `.zhin/gateway.db` (ADR 0059 §1): the six Console access-control
|
|
3
|
+
* tables (`IAM_TABLES`) and `gateway_meta`, in SQLite whatever dialect the project database
|
|
4
|
+
* uses. Owner-only (0600, `-wal` / `-shm` included).
|
|
5
|
+
*
|
|
6
|
+
* Opened only by the gateway, by `zhin console …` while no gateway runs, and by a standalone
|
|
7
|
+
* instance (`zhin start --standalone`, development and tests only: there the instance process
|
|
8
|
+
* holds both connections, so there is no process isolation). A supervised instance never opens
|
|
9
|
+
* it, and its own SQLite connection refuses `ATTACH` (`denyAttach`).
|
|
10
|
+
*/
|
|
11
|
+
import { randomBytes } from 'node:crypto';
|
|
12
|
+
import { chmodSync, closeSync, existsSync, mkdirSync, openSync } from 'node:fs';
|
|
13
|
+
import { dirname, join } from 'node:path';
|
|
14
|
+
import { defineConsoleIamTables, IAM_TABLES } from '../iam/tables.js';
|
|
15
|
+
import { GATEWAY_SQLITE_BUSY_RETRY_MS, ProjectDatabase } from './project-database.js';
|
|
16
|
+
/** Project-relative path of the gateway store. */
|
|
17
|
+
export const GATEWAY_STORE_FILE = join('.zhin', 'gateway.db');
|
|
18
|
+
/** Key / value table of the gateway store (`schema_version`, `legacy_import`). */
|
|
19
|
+
export const GATEWAY_META_TABLE = 'gateway_meta';
|
|
20
|
+
/** Schema version written to `gateway_meta.schema_version`. */
|
|
21
|
+
export const GATEWAY_STORE_SCHEMA_VERSION = 1;
|
|
22
|
+
const META_DEFINITION = Object.freeze({
|
|
23
|
+
key: { type: 'text', length: 64, primary: true, nullable: false },
|
|
24
|
+
value: { type: 'text', nullable: false },
|
|
25
|
+
});
|
|
26
|
+
/** `<projectRoot>/.zhin/gateway.db`. */
|
|
27
|
+
export function gatewayStorePath(projectRoot) {
|
|
28
|
+
return join(projectRoot, GATEWAY_STORE_FILE);
|
|
29
|
+
}
|
|
30
|
+
function restrict(file) {
|
|
31
|
+
if (process.platform === 'win32')
|
|
32
|
+
return;
|
|
33
|
+
if (existsSync(file))
|
|
34
|
+
chmodSync(file, 0o600);
|
|
35
|
+
}
|
|
36
|
+
/** Opens (creating if needed) the gateway store with its tables. */
|
|
37
|
+
export async function openGatewayStore(projectRoot, options = {}) {
|
|
38
|
+
const filename = options.filename ?? gatewayStorePath(projectRoot);
|
|
39
|
+
mkdirSync(dirname(filename), { recursive: true });
|
|
40
|
+
// Create the file owner-only before SQLite does (it would follow the umask); SQLite gives the
|
|
41
|
+
// -wal / -shm files the mode of the database file.
|
|
42
|
+
if (!existsSync(filename))
|
|
43
|
+
closeSync(openSync(filename, 'a', 0o600));
|
|
44
|
+
restrict(filename);
|
|
45
|
+
const database = new ProjectDatabase({ dialect: 'sqlite', filename, busyRetryMs: options.busyRetryMs ?? GATEWAY_SQLITE_BUSY_RETRY_MS });
|
|
46
|
+
defineConsoleIamTables(database);
|
|
47
|
+
database.define(GATEWAY_META_TABLE, META_DEFINITION);
|
|
48
|
+
await database.start();
|
|
49
|
+
for (const suffix of ['', '-wal', '-shm'])
|
|
50
|
+
restrict(`${filename}${suffix}`);
|
|
51
|
+
const run = (sql, params, trx) => (trx ? trx.query(sql, params) : database.query(sql, params));
|
|
52
|
+
let storeId = '';
|
|
53
|
+
const store = {
|
|
54
|
+
filename,
|
|
55
|
+
get storeId() { return storeId; },
|
|
56
|
+
get started() { return database.started; },
|
|
57
|
+
models: database.models,
|
|
58
|
+
async getMeta(key, trx) {
|
|
59
|
+
const rows = await run(`SELECT value FROM ${GATEWAY_META_TABLE} WHERE key = ?`, [key], trx);
|
|
60
|
+
return rows[0] ? JSON.parse(rows[0].value) : undefined;
|
|
61
|
+
},
|
|
62
|
+
async setMeta(key, value, trx) {
|
|
63
|
+
await run(`INSERT INTO ${GATEWAY_META_TABLE} (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value`, [key, JSON.stringify(value)], trx);
|
|
64
|
+
},
|
|
65
|
+
async deleteMeta(key, trx) {
|
|
66
|
+
await run(`DELETE FROM ${GATEWAY_META_TABLE} WHERE key = ?`, [key], trx);
|
|
67
|
+
},
|
|
68
|
+
transaction: callback => database.transaction(callback),
|
|
69
|
+
query: (sql, params) => database.query(sql, params),
|
|
70
|
+
async counts(trx) {
|
|
71
|
+
const out = {};
|
|
72
|
+
for (const table of Object.values(IAM_TABLES)) {
|
|
73
|
+
const [row] = await run(`SELECT count(*) AS n FROM "${table}"`, [], trx);
|
|
74
|
+
out[table] = Number(row?.n ?? 0);
|
|
75
|
+
}
|
|
76
|
+
return Object.freeze(out);
|
|
77
|
+
},
|
|
78
|
+
backupTo: destination => database.backupTo(destination),
|
|
79
|
+
close: () => database.stop(),
|
|
80
|
+
};
|
|
81
|
+
if (await store.getMeta('schema_version') === undefined)
|
|
82
|
+
await store.setMeta('schema_version', GATEWAY_STORE_SCHEMA_VERSION);
|
|
83
|
+
storeId = await store.getMeta('store_id') ?? '';
|
|
84
|
+
if (!storeId) {
|
|
85
|
+
// Insert-if-absent: two processes opening a new store agree on one id.
|
|
86
|
+
await database.query(`INSERT INTO ${GATEWAY_META_TABLE} (key, value) VALUES ('store_id', ?) ON CONFLICT(key) DO NOTHING`, [JSON.stringify(randomBytes(16).toString('hex'))]);
|
|
87
|
+
storeId = await store.getMeta('store_id') ?? '';
|
|
88
|
+
}
|
|
89
|
+
return Object.freeze(store);
|
|
90
|
+
}
|
package/lib/database/index.d.ts
CHANGED
package/lib/database/index.js
CHANGED
|
@@ -14,6 +14,21 @@ export declare const DATABASE_BUSY_RETRY_AFTER_SECONDS = 2;
|
|
|
14
14
|
export declare function isDatabaseBusyError(error: unknown): boolean;
|
|
15
15
|
/** `503 { code: 'database_busy' }` with `Retry-After`. */
|
|
16
16
|
export declare function databaseBusyResponse(extra?: Readonly<Record<string, unknown>>): Response;
|
|
17
|
+
/** One statement inside {@link ProjectDatabase.transaction}. */
|
|
18
|
+
export interface ProjectDatabaseTransaction {
|
|
19
|
+
query<T = unknown>(sql: string, params?: unknown[]): Promise<T>;
|
|
20
|
+
}
|
|
21
|
+
/** The gateway's marker in the project database. */
|
|
22
|
+
export interface StoreMarker {
|
|
23
|
+
readonly storeId: string;
|
|
24
|
+
readonly migratedAt: number;
|
|
25
|
+
}
|
|
26
|
+
/** Rows keyed by table name. */
|
|
27
|
+
export type TableRows = Readonly<Record<string, readonly Record<string, unknown>[]>>;
|
|
28
|
+
/** Rows read per batch by {@link ProjectDatabase.readSnapshot}. */
|
|
29
|
+
export declare const SNAPSHOT_BATCH_SIZE = 500;
|
|
30
|
+
/** A quoted table name for `dialect`. */
|
|
31
|
+
export declare function quoteTableName(dialect: string, name: string): string;
|
|
17
32
|
/** One database connection; tables are defined before {@link start}. */
|
|
18
33
|
export declare class ProjectDatabase {
|
|
19
34
|
#private;
|
|
@@ -26,6 +41,36 @@ export declare class ProjectDatabase {
|
|
|
26
41
|
readonly models: Readonly<{
|
|
27
42
|
get: (name: string) => DatabaseHostModel | undefined;
|
|
28
43
|
}>;
|
|
44
|
+
/** Whether raw SQL / transactions are available (SQLite, MySQL, PostgreSQL). */
|
|
45
|
+
get relational(): boolean;
|
|
46
|
+
/** One raw statement (gateway-internal: the gateway store, the legacy import and its revert). */
|
|
47
|
+
query<T = unknown>(sql: string, params?: unknown[]): Promise<T>;
|
|
48
|
+
/** A transaction (SQLite write transactions are `BEGIN IMMEDIATE`). */
|
|
49
|
+
transaction<T>(callback: (trx: ProjectDatabaseTransaction) => Promise<T>, options?: {
|
|
50
|
+
readOnly?: boolean;
|
|
51
|
+
repeatableRead?: boolean;
|
|
52
|
+
}): Promise<T>;
|
|
53
|
+
/** SQLite: a consistent copy of the file at `destination` (0600). */
|
|
54
|
+
backupTo(destination: string): Promise<void>;
|
|
55
|
+
/** Which of `names` exist as tables (collections), read from the system catalog without defining them. */
|
|
56
|
+
tableExists(names: readonly string[]): Promise<string[]>;
|
|
57
|
+
/**
|
|
58
|
+
* All rows of `tables`, read in batches of {@link SNAPSHOT_BATCH_SIZE} ordered by `id`. SQL
|
|
59
|
+
* dialects read inside one read-only repeatable-read transaction (one snapshot); MongoDB reads
|
|
60
|
+
* the collections one after another.
|
|
61
|
+
*/
|
|
62
|
+
readSnapshot(tables: readonly string[]): Promise<TableRows>;
|
|
63
|
+
/**
|
|
64
|
+
* Drops `tables` if they exist: in one transaction on SQLite / PostgreSQL, one by one on MySQL
|
|
65
|
+
* (DDL commits implicitly) and MongoDB, where an interrupted run resumes with the rest.
|
|
66
|
+
*/
|
|
67
|
+
dropTables(tables: readonly string[]): Promise<void>;
|
|
68
|
+
/** The gateway's marker (ADR 0059 §3 修订): which gateway.db holds the Console accounts, or undefined. */
|
|
69
|
+
readStoreMarker(): Promise<StoreMarker | undefined>;
|
|
70
|
+
/** Writes (replaces) the marker. */
|
|
71
|
+
writeStoreMarker(marker: StoreMarker): Promise<void>;
|
|
72
|
+
/** Removes the marker (revert to a version without it). */
|
|
73
|
+
deleteStoreMarker(): Promise<void>;
|
|
29
74
|
start(): Promise<void>;
|
|
30
75
|
stop(): Promise<void>;
|
|
31
76
|
}
|