sonamu 0.13.11 → 0.14.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/dist/api/caster.d.ts.map +1 -1
- package/dist/api/caster.js +2 -1
- package/dist/api/code-converters.d.ts +5 -1
- package/dist/api/code-converters.d.ts.map +1 -1
- package/dist/api/code-converters.js +24 -7
- package/dist/api/config.d.ts +7 -0
- package/dist/api/config.d.ts.map +1 -1
- package/dist/api/config.js +1 -1
- package/dist/api/decorators.d.ts +1 -0
- package/dist/api/decorators.d.ts.map +1 -1
- package/dist/api/decorators.js +1 -1
- package/dist/api/guards.d.ts +37 -0
- package/dist/api/guards.d.ts.map +1 -0
- package/dist/api/guards.js +96 -0
- package/dist/api/sonamu.d.ts +14 -1
- package/dist/api/sonamu.d.ts.map +1 -1
- package/dist/api/sonamu.js +121 -25
- package/dist/api/websocket-helpers.d.ts +6 -1
- package/dist/api/websocket-helpers.d.ts.map +1 -1
- package/dist/api/websocket-helpers.js +7 -4
- package/dist/auth/auth-entity-policy.d.ts +32 -0
- package/dist/auth/auth-entity-policy.d.ts.map +1 -0
- package/dist/auth/auth-entity-policy.js +44 -0
- package/dist/auth/auth-secret.d.ts +24 -0
- package/dist/auth/auth-secret.d.ts.map +1 -0
- package/dist/auth/auth-secret.js +46 -0
- package/dist/auth/base-url.d.ts +39 -0
- package/dist/auth/base-url.d.ts.map +1 -0
- package/dist/auth/base-url.js +84 -0
- package/dist/auth/better-auth-entities.js +20 -7
- package/dist/auth/better-auth-options.d.ts +15 -0
- package/dist/auth/better-auth-options.d.ts.map +1 -0
- package/dist/auth/better-auth-options.js +38 -0
- package/dist/auth/better-auth-request.d.ts +8 -1
- package/dist/auth/better-auth-request.d.ts.map +1 -1
- package/dist/auth/better-auth-request.js +26 -3
- package/dist/auth/plugins/entity-definitions/admin.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/admin.js +6 -1
- package/dist/auth/plugins/entity-definitions/anonymous.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/anonymous.js +2 -1
- package/dist/auth/plugins/entity-definitions/api-key.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/api-key.js +18 -2
- package/dist/auth/plugins/entity-definitions/audit-log.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/audit-log.js +27 -1
- package/dist/auth/plugins/entity-definitions/jwt.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/jwt.js +4 -2
- package/dist/auth/plugins/entity-definitions/organization.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/organization.js +15 -1
- package/dist/auth/plugins/entity-definitions/passkey.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/passkey.js +9 -1
- package/dist/auth/plugins/entity-definitions/phone-number.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/phone-number.js +2 -1
- package/dist/auth/plugins/entity-definitions/sso.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/sso.js +7 -3
- package/dist/auth/plugins/entity-definitions/two-factor.d.ts.map +1 -1
- package/dist/auth/plugins/entity-definitions/two-factor.js +8 -3
- package/dist/auth/verify-email-guard.d.ts +37 -0
- package/dist/auth/verify-email-guard.d.ts.map +1 -0
- package/dist/auth/verify-email-guard.js +48 -0
- package/dist/bin/build-config.d.ts.map +1 -1
- package/dist/bin/build-config.js +6 -2
- package/dist/bin/typecheck-web.js +1 -1
- package/dist/cache/decorator.d.ts +12 -0
- package/dist/cache/decorator.d.ts.map +1 -1
- package/dist/cache/decorator.js +86 -19
- package/dist/cache/index.d.ts +1 -1
- package/dist/cache/index.d.ts.map +1 -1
- package/dist/cache/index.js +3 -3
- package/dist/cache/types.d.ts +16 -0
- package/dist/cache/types.d.ts.map +1 -1
- package/dist/cli/plugin-loader.d.ts +17 -0
- package/dist/cli/plugin-loader.d.ts.map +1 -0
- package/dist/cli/plugin-loader.js +178 -0
- package/dist/cli/plugin.d.ts +65 -0
- package/dist/cli/plugin.d.ts.map +1 -0
- package/dist/cli/plugin.js +7 -0
- package/dist/code-formatting/oxfmt.js +1 -1
- package/dist/code-formatting/oxlint.js +1 -1
- package/dist/database/db.d.ts +35 -10
- package/dist/database/db.d.ts.map +1 -1
- package/dist/database/db.js +37 -4
- package/dist/database/puri.d.ts +8 -0
- package/dist/database/puri.d.ts.map +1 -1
- package/dist/database/puri.js +108 -171
- package/dist/database/puri.types.d.ts +2 -0
- package/dist/database/puri.types.d.ts.map +1 -1
- package/dist/database/puri.types.js +1 -1
- package/dist/database/sql-expression-brand.d.ts +10 -0
- package/dist/database/sql-expression-brand.d.ts.map +1 -0
- package/dist/database/sql-expression-brand.js +10 -0
- package/dist/dict/en.d.ts +1 -0
- package/dist/dict/en.d.ts.map +1 -1
- package/dist/dict/en.js +2 -1
- package/dist/dict/ko.d.ts +1 -0
- package/dist/dict/ko.d.ts.map +1 -1
- package/dist/dict/ko.js +2 -1
- package/dist/dict/sonamu-dictionary.d.ts +23 -1
- package/dist/dict/sonamu-dictionary.d.ts.map +1 -1
- package/dist/dict/sonamu-dictionary.js +187 -32
- package/dist/entity/entity-codegen-safety.d.ts +105 -0
- package/dist/entity/entity-codegen-safety.d.ts.map +1 -0
- package/dist/entity/entity-codegen-safety.js +70 -0
- package/dist/entity/entity-manager.d.ts +32 -0
- package/dist/entity/entity-manager.d.ts.map +1 -1
- package/dist/entity/entity-manager.js +21 -2
- package/dist/entity/entity.d.ts +5 -2
- package/dist/entity/entity.d.ts.map +1 -1
- package/dist/entity/entity.js +19 -8
- package/dist/env.d.ts +19 -0
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +31 -3
- package/dist/filter/utils.d.ts +2 -2
- package/dist/filter/utils.d.ts.map +1 -1
- package/dist/filter/utils.js +16 -11
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -6
- package/dist/logger/configure.d.ts.map +1 -1
- package/dist/logger/configure.js +10 -3
- package/dist/migration/code-generation.d.ts.map +1 -1
- package/dist/migration/code-generation.js +177 -96
- package/dist/migration/migrator.d.ts +16 -0
- package/dist/migration/migrator.d.ts.map +1 -1
- package/dist/migration/migrator.js +28 -3
- package/dist/migration/slack-confirm.d.ts +6 -2
- package/dist/migration/slack-confirm.d.ts.map +1 -1
- package/dist/migration/slack-confirm.js +11 -9
- package/dist/ssr/errors.d.ts +13 -0
- package/dist/ssr/errors.d.ts.map +1 -0
- package/dist/ssr/errors.js +12 -0
- package/dist/ssr/index.d.ts +1 -0
- package/dist/ssr/index.d.ts.map +1 -1
- package/dist/ssr/index.js +5 -2
- package/dist/ssr/renderer.d.ts +6 -0
- package/dist/ssr/renderer.d.ts.map +1 -1
- package/dist/ssr/renderer.js +10 -1
- package/dist/storage/base-file.d.ts +6 -2
- package/dist/storage/base-file.d.ts.map +1 -1
- package/dist/storage/base-file.js +10 -5
- package/dist/storage/buffered-file.d.ts +1 -0
- package/dist/storage/buffered-file.d.ts.map +1 -1
- package/dist/storage/buffered-file.js +5 -2
- package/dist/storage/index.d.ts +1 -0
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +4 -2
- package/dist/storage/upload-safety.d.ts +47 -0
- package/dist/storage/upload-safety.d.ts.map +1 -0
- package/dist/storage/upload-safety.js +112 -0
- package/dist/stream/ws.d.ts +6 -0
- package/dist/stream/ws.d.ts.map +1 -1
- package/dist/stream/ws.js +52 -6
- package/dist/syncer/code-generator.d.ts +2 -0
- package/dist/syncer/code-generator.d.ts.map +1 -1
- package/dist/syncer/code-generator.js +21 -7
- package/dist/template/implementations/entity.template.d.ts.map +1 -1
- package/dist/template/implementations/entity.template.js +40 -35
- package/dist/template/implementations/generated.template.d.ts +10 -0
- package/dist/template/implementations/generated.template.d.ts.map +1 -1
- package/dist/template/implementations/generated.template.js +46 -7
- package/dist/template/implementations/generated_http.template.d.ts.map +1 -1
- package/dist/template/implementations/generated_http.template.js +3 -1
- package/dist/template/implementations/init_types.template.d.ts.map +1 -1
- package/dist/template/implementations/init_types.template.js +3 -8
- package/dist/template/implementations/model.template.d.ts.map +1 -1
- package/dist/template/implementations/model.template.js +9 -2
- package/dist/template/implementations/sd.template.d.ts +0 -1
- package/dist/template/implementations/sd.template.d.ts.map +1 -1
- package/dist/template/implementations/sd.template.js +2 -5
- package/dist/template/implementations/view_form.template.d.ts.map +1 -1
- package/dist/template/implementations/view_form.template.js +8 -6
- package/dist/template/implementations/view_list.template.d.ts.map +1 -1
- package/dist/template/implementations/view_list.template.js +8 -4
- package/dist/testing/dev-test-routes.d.ts.map +1 -1
- package/dist/testing/dev-test-routes.js +7 -4
- package/dist/tooling/__tests__/helpers/sonamu-test-root.js +1 -1
- package/dist/tooling/cdd-service.js +1 -1
- package/dist/tooling/index.d.ts +1 -0
- package/dist/tooling/index.d.ts.map +1 -1
- package/dist/tooling/index.js +3 -1
- package/dist/tsdown.api.config.js +1 -1
- package/dist/types/types.d.ts +81 -1
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/types.js +2 -1
- package/dist/ui/__tests__/ui-test-helpers.d.ts +10 -0
- package/dist/ui/__tests__/ui-test-helpers.d.ts.map +1 -0
- package/dist/ui/__tests__/ui-test-helpers.js +75 -0
- package/dist/ui/access-guard.d.ts +42 -0
- package/dist/ui/access-guard.d.ts.map +1 -0
- package/dist/ui/access-guard.js +94 -0
- package/dist/ui/api.d.ts.map +1 -1
- package/dist/ui/api.js +40 -95
- package/dist/ui/dev-host-guard.d.ts +28 -0
- package/dist/ui/dev-host-guard.d.ts.map +1 -0
- package/dist/ui/dev-host-guard.js +67 -0
- package/dist/ui/migrations-api.d.ts.map +1 -1
- package/dist/ui/migrations-api.js +72 -16
- package/dist/ui/open-vscode-api.d.ts +4 -0
- package/dist/ui/open-vscode-api.d.ts.map +1 -0
- package/dist/ui/open-vscode-api.js +65 -0
- package/dist/ui/scaffolding-api.d.ts +18 -0
- package/dist/ui/scaffolding-api.d.ts.map +1 -0
- package/dist/ui/scaffolding-api.js +111 -0
- package/dist/ui/ui-db-presets.d.ts +19 -0
- package/dist/ui/ui-db-presets.d.ts.map +1 -0
- package/dist/ui/ui-db-presets.js +34 -0
- package/dist/ui/ui-request-guard.d.ts +11 -0
- package/dist/ui/ui-request-guard.d.ts.map +1 -0
- package/dist/ui/ui-request-guard.js +38 -0
- package/dist/ui-web/assets/{index-QDMyy31K.js → index-BPqP-tug.js} +39 -39
- package/dist/ui-web/index.html +2 -1
- package/package.json +1 -1
- package/src/__tests__/env.test.ts +121 -2
- package/src/api/__tests__/code-converters.union.test.ts +97 -0
- package/src/api/__tests__/guards.test.ts +233 -0
- package/src/api/__tests__/sonamu.auth-secret.test.ts +77 -0
- package/src/api/__tests__/sonamu.dev-host-guard.test.ts +130 -0
- package/src/api/__tests__/sonamu.guards.test.ts +327 -0
- package/src/api/__tests__/sonamu.upload-limits.test.ts +156 -0
- package/src/api/__tests__/websocket-helpers.test.ts +101 -0
- package/src/api/caster.ts +7 -0
- package/src/api/code-converters.ts +44 -12
- package/src/api/config.ts +8 -0
- package/src/api/decorators.ts +1 -0
- package/src/api/guards.ts +130 -0
- package/src/api/sonamu.ts +195 -39
- package/src/api/websocket-helpers.ts +11 -6
- package/src/auth/__tests__/auth-entity-policy.test.ts +59 -0
- package/src/auth/__tests__/auth-secret.test.ts +133 -0
- package/src/auth/__tests__/base-url-host-poisoning.test.ts +193 -0
- package/src/auth/__tests__/base-url.test.ts +117 -0
- package/src/auth/__tests__/better-auth-options.test.ts +194 -0
- package/src/auth/__tests__/better-auth-request.test.ts +45 -0
- package/src/auth/__tests__/verify-email-guard.test.ts +228 -0
- package/src/auth/auth-entity-policy.ts +48 -0
- package/src/auth/auth-secret.ts +77 -0
- package/src/auth/base-url.ts +109 -0
- package/src/auth/better-auth-entities.ts +30 -30
- package/src/auth/better-auth-options.ts +39 -0
- package/src/auth/better-auth-request.ts +28 -1
- package/src/auth/plugins/entity-definitions/admin.ts +5 -0
- package/src/auth/plugins/entity-definitions/anonymous.ts +9 -1
- package/src/auth/plugins/entity-definitions/api-key.ts +53 -16
- package/src/auth/plugins/entity-definitions/audit-log.ts +122 -21
- package/src/auth/plugins/entity-definitions/jwt.ts +5 -4
- package/src/auth/plugins/entity-definitions/organization.ts +26 -6
- package/src/auth/plugins/entity-definitions/passkey.ts +8 -7
- package/src/auth/plugins/entity-definitions/phone-number.ts +1 -0
- package/src/auth/plugins/entity-definitions/sso.ts +20 -15
- package/src/auth/plugins/entity-definitions/two-factor.ts +21 -5
- package/src/auth/verify-email-guard.ts +82 -0
- package/src/bin/__tests__/build-web-command.test.ts +24 -0
- package/src/bin/build-config.ts +5 -0
- package/src/cache/decorator.ts +116 -21
- package/src/cache/index.ts +6 -1
- package/src/cache/types.ts +18 -0
- package/src/cli/plugin-loader.ts +238 -0
- package/src/cli/plugin.ts +82 -0
- package/src/database/__tests__/db.test.ts +64 -1
- package/src/database/__tests__/puri.test.ts +56 -0
- package/src/database/db.ts +56 -6
- package/src/database/puri.ts +135 -125
- package/src/database/puri.types.test-d.ts +12 -0
- package/src/database/puri.types.ts +3 -0
- package/src/database/sql-expression-brand.ts +9 -0
- package/src/dict/__tests__/sonamu-dictionary.test.ts +217 -0
- package/src/dict/en.ts +2 -0
- package/src/dict/ko.ts +2 -0
- package/src/dict/sonamu-dictionary.ts +298 -38
- package/src/entity/__tests__/entity-codegen-safety.test.ts +326 -0
- package/src/entity/entity-codegen-safety.ts +126 -0
- package/src/entity/entity-manager.ts +29 -1
- package/src/entity/entity.ts +26 -12
- package/src/env.ts +52 -1
- package/src/filter/utils.ts +19 -7
- package/src/index.ts +4 -3
- package/src/logger/__tests__/configure.test.ts +51 -1
- package/src/logger/configure.ts +14 -1
- package/src/migration/__tests__/code-generation.escape.test.ts +322 -0
- package/src/migration/__tests__/migrator.test.ts +76 -1
- package/src/migration/code-generation.ts +247 -117
- package/src/migration/migrator.ts +45 -4
- package/src/migration/slack-confirm.ts +19 -5
- package/src/ssr/__tests__/preload-guard.test.ts +274 -0
- package/src/ssr/errors.ts +11 -0
- package/src/ssr/index.ts +1 -0
- package/src/ssr/renderer.ts +12 -0
- package/src/storage/__tests__/upload-safety.test.ts +179 -0
- package/src/storage/base-file.ts +8 -4
- package/src/storage/buffered-file.ts +3 -1
- package/src/storage/index.ts +9 -0
- package/src/storage/upload-safety.ts +155 -0
- package/src/stream/__tests__/ws.test.ts +97 -0
- package/src/stream/ws.ts +67 -5
- package/src/syncer/__tests__/code-generator-path-containment.test.ts +105 -0
- package/src/syncer/code-generator.ts +34 -6
- package/src/template/__tests__/generated.template.search-text.test.ts +5 -1
- package/src/template/__tests__/model.template.auth-entity.test.ts +42 -0
- package/src/template/__tests__/save-params.template.test.ts +219 -0
- package/src/template/implementations/entity.template.ts +53 -48
- package/src/template/implementations/generated.template.ts +87 -12
- package/src/template/implementations/generated_http.template.ts +5 -0
- package/src/template/implementations/init_types.template.ts +4 -22
- package/src/template/implementations/model.template.ts +15 -1
- package/src/template/implementations/sd.template.ts +4 -5
- package/src/template/implementations/view_form.template.ts +13 -5
- package/src/template/implementations/view_list.template.ts +12 -3
- package/src/testing/__tests__/dev-test-routes.test.ts +99 -0
- package/src/testing/dev-test-routes.ts +7 -3
- package/src/tooling/__tests__/cli-plugins.contract.test.ts +273 -0
- package/src/tooling/index.ts +1 -0
- package/src/types/types.ts +22 -1
- package/src/ui/__tests__/access-guard.test.ts +217 -0
- package/src/ui/__tests__/dev-host-guard.test.ts +161 -0
- package/src/ui/__tests__/fixture-api.test.ts +92 -0
- package/src/ui/__tests__/migrations-api.test.ts +352 -0
- package/src/ui/__tests__/open-vscode.test.ts +151 -0
- package/src/ui/__tests__/scaffolding-api.test.ts +232 -0
- package/src/ui/__tests__/ui-process-routes.test.ts +166 -0
- package/src/ui/__tests__/ui-test-helpers.ts +71 -0
- package/src/ui/access-guard.ts +115 -0
- package/src/ui/api.ts +65 -198
- package/src/ui/dev-host-guard.ts +109 -0
- package/src/ui/migrations-api.ts +130 -24
- package/src/ui/open-vscode-api.ts +82 -0
- package/src/ui/scaffolding-api.ts +177 -0
- package/src/ui/ui-db-presets.ts +41 -0
- package/src/ui/ui-request-guard.ts +53 -0
package/src/ui/migrations-api.ts
CHANGED
|
@@ -21,17 +21,26 @@ import {
|
|
|
21
21
|
type MigrationTarget,
|
|
22
22
|
} from "../migration/types";
|
|
23
23
|
import { forwardAsyncErrors } from "./async-route";
|
|
24
|
+
import { isUIDBPreset } from "./ui-db-presets";
|
|
24
25
|
|
|
25
26
|
type ApprovalReady = { type: "ready" };
|
|
26
27
|
type ApprovalResult = ApprovalReady | SlackConfirmPendingResult;
|
|
28
|
+
type ApprovalAction = Exclude<MigrationAction, "shadow">;
|
|
27
29
|
type ApplyBody = { targets: MigrationTarget[]; requestor?: string };
|
|
30
|
+
type ApprovalRequestBody = ApplyBody & { action?: ApprovalAction };
|
|
28
31
|
type LegacyApplyBody = ApplyBody & { force?: boolean; forceReason?: string };
|
|
29
32
|
|
|
30
33
|
const LOCAL_HOSTS = new Set(["localhost", "127.0.0.1", "0.0.0.0", "::1"]);
|
|
31
34
|
|
|
32
35
|
function assertTargets(migrator: Migrator, targets: MigrationTarget[]): void {
|
|
36
|
+
// urlencoded 단일 값처럼 배열이 아닌 입력은 대상 판별 전에 거부합니다.
|
|
37
|
+
if (!Array.isArray(targets)) {
|
|
38
|
+
throw new BadRequestException(SD("error.badRequest"), {
|
|
39
|
+
message: "Invalid migration targets: (not an array)",
|
|
40
|
+
});
|
|
41
|
+
}
|
|
33
42
|
const allowed = new Set(migrator.getConnections().map(({ connKey }) => connKey));
|
|
34
|
-
const invalid = targets.filter((target) => !allowed.has(target));
|
|
43
|
+
const invalid = targets.filter((target) => !allowed.has(target)).map(String);
|
|
35
44
|
if (targets.length === 0 || invalid.length > 0) {
|
|
36
45
|
throw new BadRequestException(SD("error.badRequest"), {
|
|
37
46
|
message: `Invalid migration targets: ${invalid.length === 0 ? "(empty)" : invalid.join(", ")}`,
|
|
@@ -64,11 +73,41 @@ function getPendingMigrations(statuses: MigrationConnectionStatus[]) {
|
|
|
64
73
|
return [...new Set(statuses.flatMap(({ pending }) => pending))];
|
|
65
74
|
}
|
|
66
75
|
|
|
67
|
-
|
|
76
|
+
/**
|
|
77
|
+
* 승인 요청을 식별하는 마이그레이션 목록을 만듭니다.
|
|
78
|
+
*
|
|
79
|
+
* apply는 pending 파일 이름을, rollback은 대상별 최신 batch 번호를 포함한 항목을 씁니다.
|
|
80
|
+
* rollback 항목은 apply 항목과 겹치지 않으므로 apply 승인으로 rollback을 실행할 수 없고,
|
|
81
|
+
* 다시 적용되어 batch 번호가 바뀌면 새 승인을 받아야 합니다.
|
|
82
|
+
*/
|
|
83
|
+
async function getApprovalMigrations(
|
|
68
84
|
migrator: Migrator,
|
|
85
|
+
action: ApprovalAction,
|
|
69
86
|
targets: MigrationTarget[],
|
|
70
|
-
|
|
87
|
+
statuses: MigrationConnectionStatus[],
|
|
88
|
+
): Promise<string[]> {
|
|
89
|
+
if (action === "apply") {
|
|
90
|
+
return getPendingMigrations(statuses);
|
|
91
|
+
}
|
|
92
|
+
const batches = await Promise.all(
|
|
93
|
+
targets.map(async (target) => ({ target, ...(await migrator.getLatestAppliedBatch(target)) })),
|
|
94
|
+
);
|
|
95
|
+
return batches.flatMap(({ target, batchNo, files }) =>
|
|
96
|
+
files.map((file) => `[rollback:${String(target)}#${batchNo}] ${file}`),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function requestApproval(
|
|
101
|
+
migrator: Migrator,
|
|
102
|
+
body: ApprovalRequestBody,
|
|
71
103
|
): Promise<ApprovalResult> {
|
|
104
|
+
const { targets, requestor } = body;
|
|
105
|
+
const action = body.action ?? "apply";
|
|
106
|
+
if (action !== "apply" && action !== "rollback") {
|
|
107
|
+
throw new BadRequestException(SD("error.badRequest"), {
|
|
108
|
+
message: `Invalid approval action: ${String(action)}`,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
72
111
|
assertTargets(migrator, targets);
|
|
73
112
|
const statuses = await getAvailableTargetStatuses(migrator, targets);
|
|
74
113
|
const slack = new SlackConfirm();
|
|
@@ -77,7 +116,7 @@ async function requestApproval(
|
|
|
77
116
|
return { type: "ready" };
|
|
78
117
|
}
|
|
79
118
|
|
|
80
|
-
const pending =
|
|
119
|
+
const pending = await getApprovalMigrations(migrator, action, targets, statuses);
|
|
81
120
|
if (pending.length === 0) {
|
|
82
121
|
return { type: "ready" };
|
|
83
122
|
}
|
|
@@ -94,12 +133,18 @@ async function requestApproval(
|
|
|
94
133
|
return { type: "pending", ...existing };
|
|
95
134
|
}
|
|
96
135
|
|
|
97
|
-
const created = await slack.postApprovalRequest(pending, targets, requestor);
|
|
136
|
+
const created = await slack.postApprovalRequest(pending, targets, requestor, action);
|
|
98
137
|
await slack.saveRequest(pending, created.channel, created.ts);
|
|
99
138
|
return { type: "pending", ...created };
|
|
100
139
|
}
|
|
101
140
|
|
|
102
|
-
|
|
141
|
+
/**
|
|
142
|
+
* apply·rollback 실행 직전에 Slack 승인이 끝났는지 확인합니다.
|
|
143
|
+
* 승인 대상이 아니면 null을, 승인된 요청이면 실행 로그를 남길 Slack 메시지를 반환합니다.
|
|
144
|
+
*/
|
|
145
|
+
async function requireApproved(
|
|
146
|
+
migrator: Migrator,
|
|
147
|
+
action: ApprovalAction,
|
|
103
148
|
body: ApplyBody,
|
|
104
149
|
statuses: MigrationConnectionStatus[],
|
|
105
150
|
): Promise<{ channel: string; ts: string } | null> {
|
|
@@ -109,7 +154,7 @@ async function requireApprovedApply(
|
|
|
109
154
|
return null;
|
|
110
155
|
}
|
|
111
156
|
|
|
112
|
-
const pending =
|
|
157
|
+
const pending = await getApprovalMigrations(migrator, action, body.targets, statuses);
|
|
113
158
|
if (pending.length === 0) {
|
|
114
159
|
return null;
|
|
115
160
|
}
|
|
@@ -179,6 +224,54 @@ async function runLegacyApply(
|
|
|
179
224
|
return result;
|
|
180
225
|
}
|
|
181
226
|
|
|
227
|
+
/**
|
|
228
|
+
* 이전 runAction 호출의 rollback 경로입니다.
|
|
229
|
+
* 승인 대상이면 apply와 같이 요청을 만들거나 기존 승인 상태를 따르며, force 생략은 허용하지 않습니다.
|
|
230
|
+
*/
|
|
231
|
+
async function runLegacyRollback(
|
|
232
|
+
migrator: Migrator,
|
|
233
|
+
body: ApplyBody,
|
|
234
|
+
): Promise<MigrationResult | SlackConfirmPendingResult> {
|
|
235
|
+
assertTargets(migrator, body.targets);
|
|
236
|
+
const statuses = await getAvailableTargetStatuses(migrator, body.targets);
|
|
237
|
+
const slack = new SlackConfirm();
|
|
238
|
+
const requiresApproval = body.targets.some((target) => targetRequiresApproval(slack, target));
|
|
239
|
+
if (!requiresApproval || !slack.isConfigured()) {
|
|
240
|
+
return migrator.rollback(body.targets);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const pending = await getApprovalMigrations(migrator, "rollback", body.targets, statuses);
|
|
244
|
+
if (pending.length === 0) {
|
|
245
|
+
return migrator.rollback(body.targets);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const existing = await slack.getExistingRequest(pending);
|
|
249
|
+
if (existing === null) {
|
|
250
|
+
const created = await slack.postApprovalRequest(
|
|
251
|
+
pending,
|
|
252
|
+
body.targets,
|
|
253
|
+
body.requestor,
|
|
254
|
+
"rollback",
|
|
255
|
+
);
|
|
256
|
+
await slack.saveRequest(pending, created.channel, created.ts);
|
|
257
|
+
return { type: "pending", ...created };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const approval = await slack.checkApproval(existing.channel, existing.ts);
|
|
261
|
+
if (approval.rejected) {
|
|
262
|
+
throw new BadRequestException(SD("sonamu.error.migrationRejected"));
|
|
263
|
+
}
|
|
264
|
+
if (!approval.approved) {
|
|
265
|
+
return { type: "pending", ...existing };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const result = await migrator.rollback(body.targets);
|
|
269
|
+
if (result.length > 0) {
|
|
270
|
+
await slack.logExecution(existing.channel, existing.ts, result, body.requestor, "rollback");
|
|
271
|
+
}
|
|
272
|
+
return result;
|
|
273
|
+
}
|
|
274
|
+
|
|
182
275
|
function sendMigrationStream(
|
|
183
276
|
reply: FastifyReply,
|
|
184
277
|
action: MigrationAction,
|
|
@@ -247,7 +340,9 @@ function sendMigrationStream(
|
|
|
247
340
|
}
|
|
248
341
|
|
|
249
342
|
export async function registerMigrationsApi(server: FastifyInstance) {
|
|
250
|
-
|
|
343
|
+
// dev 서버 UI는 브라우저에서 호출되므로 개발용 DB(development·test·fixture와 그 readonly)만 대상으로 받습니다.
|
|
344
|
+
// production 제외는 허용 목록과 별개로 한 번 더 막아 두는 방어선입니다.
|
|
345
|
+
const migrator = new Migrator({ excludedTargets: ["production"], targetFilter: isUIDBPreset });
|
|
251
346
|
|
|
252
347
|
server.get("/api/migrations/connections", async () => ({
|
|
253
348
|
connections: migrator.getConnections(),
|
|
@@ -280,9 +375,9 @@ export async function registerMigrationsApi(server: FastifyInstance) {
|
|
|
280
375
|
})),
|
|
281
376
|
);
|
|
282
377
|
|
|
283
|
-
server.post<{ Body:
|
|
378
|
+
server.post<{ Body: ApprovalRequestBody }>("/api/migrations/request-approval", (request) =>
|
|
284
379
|
forwardAsyncErrors(async () => {
|
|
285
|
-
return requestApproval(migrator, request.body
|
|
380
|
+
return requestApproval(migrator, request.body);
|
|
286
381
|
}),
|
|
287
382
|
);
|
|
288
383
|
server.post<{ Body: { channel: string; ts: string } }>(
|
|
@@ -321,7 +416,7 @@ export async function registerMigrationsApi(server: FastifyInstance) {
|
|
|
321
416
|
server.post<{ Body: ApplyBody }>("/api/migrations/apply", async (request, reply) => {
|
|
322
417
|
assertTargets(migrator, request.body.targets);
|
|
323
418
|
const statuses = await getAvailableTargetStatuses(migrator, request.body.targets);
|
|
324
|
-
const approval = await
|
|
419
|
+
const approval = await requireApproved(migrator, "apply", request.body, statuses);
|
|
325
420
|
return sendMigrationStream(
|
|
326
421
|
reply,
|
|
327
422
|
"apply",
|
|
@@ -339,16 +434,29 @@ export async function registerMigrationsApi(server: FastifyInstance) {
|
|
|
339
434
|
},
|
|
340
435
|
);
|
|
341
436
|
});
|
|
342
|
-
server.post<{ Body:
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
437
|
+
server.post<{ Body: ApplyBody }>("/api/migrations/rollback", async (request, reply) => {
|
|
438
|
+
assertTargets(migrator, request.body.targets);
|
|
439
|
+
const statuses = await getAvailableTargetStatuses(migrator, request.body.targets);
|
|
440
|
+
// rollback은 데이터를 지울 수 있으므로 apply와 같은 Slack 승인을 거칩니다.
|
|
441
|
+
const approval = await requireApproved(migrator, "rollback", request.body, statuses);
|
|
442
|
+
return sendMigrationStream(
|
|
443
|
+
reply,
|
|
444
|
+
"rollback",
|
|
445
|
+
request.body.targets,
|
|
446
|
+
(onProgress) => migrator.rollback(request.body.targets, { onProgress }),
|
|
447
|
+
approval === null
|
|
448
|
+
? undefined
|
|
449
|
+
: async (result) => {
|
|
450
|
+
await new SlackConfirm().logExecution(
|
|
451
|
+
approval.channel,
|
|
452
|
+
approval.ts,
|
|
453
|
+
result,
|
|
454
|
+
request.body.requestor,
|
|
455
|
+
"rollback",
|
|
456
|
+
);
|
|
457
|
+
},
|
|
458
|
+
);
|
|
459
|
+
});
|
|
352
460
|
|
|
353
461
|
server.post<{ Body: { compareConnKey?: MigrationTarget } }>(
|
|
354
462
|
"/api/migrations/generatePreparedCodes",
|
|
@@ -373,9 +481,7 @@ export async function registerMigrationsApi(server: FastifyInstance) {
|
|
|
373
481
|
if (request.body.action === "apply") {
|
|
374
482
|
return runLegacyApply(migrator, request.body);
|
|
375
483
|
}
|
|
376
|
-
|
|
377
|
-
await getAvailableTargetStatuses(migrator, request.body.targets);
|
|
378
|
-
return migrator.rollback(request.body.targets);
|
|
484
|
+
return runLegacyRollback(migrator, request.body);
|
|
379
485
|
}),
|
|
380
486
|
);
|
|
381
487
|
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { execFileSync } from "child_process";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
import { type FastifyInstance } from "fastify";
|
|
6
|
+
|
|
7
|
+
import { Sonamu } from "../api/sonamu";
|
|
8
|
+
import { SD } from "../dict/sd";
|
|
9
|
+
import { EntityManager } from "../entity/entity-manager";
|
|
10
|
+
import { BadRequestException } from "../exceptions/so-exceptions";
|
|
11
|
+
import { forwardAsyncErrors } from "./async-route";
|
|
12
|
+
import { requireSameOriginUIRequest } from "./ui-request-guard";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* openVscode가 받은 absPath를 앱 루트 아래에 실제로 있는 파일의 경로로 확정합니다.
|
|
16
|
+
* 심볼릭 링크나 `..`로 앱 루트를 벗어나는 경로, 없는 경로, 디렉터리는 거부합니다.
|
|
17
|
+
*/
|
|
18
|
+
function resolveOpenVscodeAbsPath(absPath: string): string {
|
|
19
|
+
const appRootPath = fs.realpathSync(Sonamu.appRootPath);
|
|
20
|
+
const realPath = (() => {
|
|
21
|
+
try {
|
|
22
|
+
return fs.realpathSync(path.resolve(appRootPath, absPath));
|
|
23
|
+
} catch {
|
|
24
|
+
throw new BadRequestException(SD("error.badRequest"));
|
|
25
|
+
}
|
|
26
|
+
})();
|
|
27
|
+
|
|
28
|
+
const relativePath = path.relative(appRootPath, realPath);
|
|
29
|
+
const isInsideAppRoot =
|
|
30
|
+
relativePath !== "" &&
|
|
31
|
+
relativePath !== ".." &&
|
|
32
|
+
!relativePath.startsWith(`..${path.sep}`) &&
|
|
33
|
+
!path.isAbsolute(relativePath);
|
|
34
|
+
if (!isInsideAppRoot || !fs.statSync(realPath).isFile()) {
|
|
35
|
+
throw new BadRequestException(SD("error.badRequest"));
|
|
36
|
+
}
|
|
37
|
+
return realPath;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 엔티티 파일이나 지정한 경로를 VS Code로 여는 Sonamu UI 도구 API를 등록합니다. */
|
|
41
|
+
export async function registerOpenVscodeApi(server: FastifyInstance) {
|
|
42
|
+
// 프로세스를 실행하는 라우트는 동일 출처 JSON POST만 받습니다.
|
|
43
|
+
server.post<{
|
|
44
|
+
Body: {
|
|
45
|
+
entityId?: string;
|
|
46
|
+
preset?: "types" | "entity.json" | "generated";
|
|
47
|
+
absPath?: string;
|
|
48
|
+
};
|
|
49
|
+
}>("/api/tools/openVscode", { onRequest: requireSameOriginUIRequest }, (request) =>
|
|
50
|
+
forwardAsyncErrors(async () => {
|
|
51
|
+
const { entityId, preset, absPath } = request.body;
|
|
52
|
+
|
|
53
|
+
const targetPath = (() => {
|
|
54
|
+
if (entityId && preset) {
|
|
55
|
+
const entity = EntityManager.get(entityId);
|
|
56
|
+
const { names } = entity;
|
|
57
|
+
|
|
58
|
+
const { apiRootPath } = Sonamu;
|
|
59
|
+
const filename = (() => {
|
|
60
|
+
switch (preset) {
|
|
61
|
+
case "types":
|
|
62
|
+
return `${names.fs}.types.ts`;
|
|
63
|
+
case "entity.json":
|
|
64
|
+
return `${names.fs}.entity.json`;
|
|
65
|
+
case "generated":
|
|
66
|
+
return `${names.fs}.generated.ts`;
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
})();
|
|
70
|
+
return `${apiRootPath}/src/application/${entity.names.parentFs}/${filename}`;
|
|
71
|
+
} else {
|
|
72
|
+
if (!absPath) {
|
|
73
|
+
throw new BadRequestException(SD("sonamu.error.presetOrAbsPathRequired"));
|
|
74
|
+
}
|
|
75
|
+
return resolveOpenVscodeAbsPath(absPath);
|
|
76
|
+
}
|
|
77
|
+
})();
|
|
78
|
+
// 셸을 거치지 않고 경로를 `--` 뒤에 두어, 명령이나 code 옵션으로 해석되지 않도록 합니다.
|
|
79
|
+
execFileSync("code", ["--", targetPath]);
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { type FastifyInstance } from "fastify";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
import { Sonamu } from "../api/sonamu";
|
|
5
|
+
import { SD } from "../dict/sd";
|
|
6
|
+
import { sonamuDictionary } from "../dict/sonamu-dictionary";
|
|
7
|
+
import { EntityManager } from "../entity/entity-manager";
|
|
8
|
+
import {
|
|
9
|
+
BadRequestException,
|
|
10
|
+
isSoException,
|
|
11
|
+
ServiceUnavailableException,
|
|
12
|
+
} from "../exceptions/so-exceptions";
|
|
13
|
+
import { TemplateManager } from "../template/template-manager";
|
|
14
|
+
import { type PathAndCode } from "../types/types";
|
|
15
|
+
import { nonNullable } from "../utils/utils";
|
|
16
|
+
import { forwardAsyncErrors } from "./async-route";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Sonamu UI 스캐폴딩에서 생성할 수 있는 템플릿 키입니다.
|
|
20
|
+
* entity·generated 같은 동기화용 템플릿은 엔티티 정의나 생성 코드 전체를 덮어쓰므로 받지 않습니다.
|
|
21
|
+
*/
|
|
22
|
+
export const ScaffoldingTemplateKey = z.enum([
|
|
23
|
+
"model",
|
|
24
|
+
"model_test",
|
|
25
|
+
"view_list",
|
|
26
|
+
"view_search_input",
|
|
27
|
+
"view_form",
|
|
28
|
+
"view_id_all_select",
|
|
29
|
+
]);
|
|
30
|
+
export type ScaffoldingTemplateKey = z.infer<typeof ScaffoldingTemplateKey>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* createEntity와 같은 entityId 규칙입니다.
|
|
34
|
+
* 경로 구분자·`..`·따옴표가 생성 파일 경로나 생성 코드에 들어가지 않게 합니다.
|
|
35
|
+
*/
|
|
36
|
+
const ScaffoldingEntityId = z.string().regex(/^[A-Z][a-zA-Z0-9]*$/);
|
|
37
|
+
|
|
38
|
+
const ScaffoldingOption = z.object({
|
|
39
|
+
entityId: ScaffoldingEntityId,
|
|
40
|
+
templateKey: ScaffoldingTemplateKey,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const ScaffoldingGetStatusBody = z.object({
|
|
44
|
+
entityIds: z.array(ScaffoldingEntityId),
|
|
45
|
+
templateKeys: z.array(ScaffoldingTemplateKey),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const ScaffoldingGenerateBody = z.object({
|
|
49
|
+
options: z.array(ScaffoldingOption.extend({ overwrite: z.boolean().optional() })),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const ScaffoldingPreviewBody = z.object({
|
|
53
|
+
option: ScaffoldingOption,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
/** 요청 본문을 스키마로 검증하고, 형식이 어긋나면 어떤 파일도 건드리기 전에 400으로 거부합니다. */
|
|
57
|
+
function parseScaffoldingBody<Schema extends z.ZodType>(
|
|
58
|
+
schema: Schema,
|
|
59
|
+
body: Parameters<Schema["safeParse"]>[0],
|
|
60
|
+
): z.infer<Schema> {
|
|
61
|
+
const result = schema.safeParse(body);
|
|
62
|
+
if (!result.success) {
|
|
63
|
+
throw new BadRequestException(SD("error.badRequest"), result.error.issues);
|
|
64
|
+
}
|
|
65
|
+
return result.data;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 등록되지 않은 엔티티로는 스캐폴딩 파일을 만들거나 경로를 조회하지 않습니다. */
|
|
69
|
+
function assertRegisteredEntity(entityId: string): void {
|
|
70
|
+
if (!EntityManager.exists(entityId)) {
|
|
71
|
+
throw new BadRequestException(SD("error.targetNotFound"));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Sonamu UI의 스캐폴딩(상태 조회·생성·미리보기) API를 등록합니다. */
|
|
76
|
+
export async function registerScaffoldingApi(server: FastifyInstance) {
|
|
77
|
+
server.post("/api/scaffolding/getStatus", (request) =>
|
|
78
|
+
forwardAsyncErrors(async () => {
|
|
79
|
+
const { entityIds, templateKeys: requestedTemplateKeys } = parseScaffoldingBody(
|
|
80
|
+
ScaffoldingGetStatusBody,
|
|
81
|
+
request.body,
|
|
82
|
+
);
|
|
83
|
+
if (entityIds.length === 0) {
|
|
84
|
+
throw new BadRequestException(SD("sonamu.error.entityIdsRequired"));
|
|
85
|
+
} else if (requestedTemplateKeys.length === 0) {
|
|
86
|
+
throw new BadRequestException(SD("sonamu.error.templateKeysRequired"));
|
|
87
|
+
}
|
|
88
|
+
entityIds.forEach(assertRegisteredEntity);
|
|
89
|
+
|
|
90
|
+
// sorting
|
|
91
|
+
entityIds.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
92
|
+
const templateKeys = ScaffoldingTemplateKey.options.filter((tk) =>
|
|
93
|
+
requestedTemplateKeys.includes(tk),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const combinations = entityIds.flatMap((entityId) => {
|
|
97
|
+
return templateKeys.map((templateKey) => [entityId, templateKey] as const);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const statuses = await Promise.all(
|
|
101
|
+
combinations.map(async ([entityId, templateKey]) => {
|
|
102
|
+
const { subPath, fullPath, isExists } = await Sonamu.syncer.checkExistsGenCode(
|
|
103
|
+
entityId,
|
|
104
|
+
templateKey,
|
|
105
|
+
);
|
|
106
|
+
return {
|
|
107
|
+
entityId,
|
|
108
|
+
templateKey,
|
|
109
|
+
subPath,
|
|
110
|
+
fullPath,
|
|
111
|
+
isExists,
|
|
112
|
+
};
|
|
113
|
+
}),
|
|
114
|
+
);
|
|
115
|
+
return { statuses };
|
|
116
|
+
}),
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
server.post("/api/scaffolding/generate", (request) =>
|
|
120
|
+
forwardAsyncErrors(async () => {
|
|
121
|
+
const { options } = parseScaffoldingBody(ScaffoldingGenerateBody, request.body);
|
|
122
|
+
if (options.length === 0) {
|
|
123
|
+
throw new BadRequestException(SD("sonamu.error.optionsRequired"));
|
|
124
|
+
}
|
|
125
|
+
// 하나라도 잘못된 항목이 있으면 아무 파일도 만들지 않도록 생성 전에 전부 확인한다.
|
|
126
|
+
options.forEach(({ entityId }) => assertRegisteredEntity(entityId));
|
|
127
|
+
|
|
128
|
+
// 1. 모든 템플릿에서 필요한 dict 키를 수집
|
|
129
|
+
const keys = options.flatMap(({ templateKey }) => {
|
|
130
|
+
const template = TemplateManager.get(templateKey);
|
|
131
|
+
return template.getRequiredDictKeys() ?? [];
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// 2. target별로 ensureDictKeys 호출 (순차 처리)
|
|
135
|
+
await sonamuDictionary.ensureDictKeys([...new Set(keys)]);
|
|
136
|
+
|
|
137
|
+
// 3. 템플릿 생성 (병렬 처리)
|
|
138
|
+
const result = await Promise.all(
|
|
139
|
+
options.map(async ({ entityId, templateKey, overwrite }) => {
|
|
140
|
+
try {
|
|
141
|
+
return await Sonamu.syncer.generateTemplate(templateKey, { entityId }, { overwrite });
|
|
142
|
+
} catch (e) {
|
|
143
|
+
if (isSoException(e) && e.statusCode === 541) {
|
|
144
|
+
return null;
|
|
145
|
+
} else {
|
|
146
|
+
console.error(e);
|
|
147
|
+
throw e;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}),
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
if (result.filter(nonNullable).length === 0) {
|
|
154
|
+
throw new ServiceUnavailableException(SD("sonamu.error.allFilesGenerated"));
|
|
155
|
+
}
|
|
156
|
+
return result;
|
|
157
|
+
}),
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
server.post("/api/scaffolding/preview", (request): Promise<{ pathAndCodes: PathAndCode[] }> =>
|
|
161
|
+
forwardAsyncErrors(async () => {
|
|
162
|
+
const {
|
|
163
|
+
option: { entityId, templateKey },
|
|
164
|
+
} = parseScaffoldingBody(ScaffoldingPreviewBody, request.body);
|
|
165
|
+
assertRegisteredEntity(entityId);
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
const pathAndCodes = await Sonamu.syncer.renderTemplate(templateKey, { entityId });
|
|
169
|
+
|
|
170
|
+
return { pathAndCodes };
|
|
171
|
+
} catch (e) {
|
|
172
|
+
console.error(e);
|
|
173
|
+
throw e;
|
|
174
|
+
}
|
|
175
|
+
}),
|
|
176
|
+
);
|
|
177
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type SonamuDBConfig, type SonamuDBPreset } from "../database/db";
|
|
2
|
+
import { SD } from "../dict/sd";
|
|
3
|
+
import { BadRequestException } from "../exceptions/so-exceptions";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Sonamu UI(dev 서버) HTTP API가 다룰 수 있는 DB 프리셋입니다.
|
|
7
|
+
*
|
|
8
|
+
* dev 서버는 브라우저에서 호출되므로 staging·production 같은 운영 계열 프리셋은
|
|
9
|
+
* 마이그레이션·fixture 조회·저장 대상에서 제외하고, CLI로만 다루게 합니다.
|
|
10
|
+
*/
|
|
11
|
+
export const UI_DB_PRESETS = [
|
|
12
|
+
"development",
|
|
13
|
+
"development_readonly",
|
|
14
|
+
"test",
|
|
15
|
+
"test_readonly",
|
|
16
|
+
"fixture",
|
|
17
|
+
] as const satisfies readonly SonamuDBPreset[];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 주어진 값이 UI API에서 허용되는 DB 프리셋인지 확인합니다.
|
|
21
|
+
* 요청 본문이 배열 등 다른 형태여도 허용 목록과 일치하지 않으므로 거부됩니다.
|
|
22
|
+
*/
|
|
23
|
+
export function isUIDBPreset(preset: string): preset is (typeof UI_DB_PRESETS)[number] {
|
|
24
|
+
return UI_DB_PRESETS.some((allowed) => allowed === preset);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* UI API 요청의 DB 프리셋 값을 검증합니다.
|
|
29
|
+
* 허용 목록 밖이거나 현재 설정에 없는 프리셋이면 DB에 연결하기 전에 거부합니다.
|
|
30
|
+
*/
|
|
31
|
+
export function assertUIDBPreset(
|
|
32
|
+
preset: string,
|
|
33
|
+
dbConfig: SonamuDBConfig,
|
|
34
|
+
field: string,
|
|
35
|
+
): asserts preset is keyof SonamuDBConfig {
|
|
36
|
+
if (!isUIDBPreset(preset) || !Object.hasOwn(dbConfig, preset)) {
|
|
37
|
+
throw new BadRequestException(SD("error.badRequest"), {
|
|
38
|
+
message: `DB preset is not allowed in Sonamu UI (${field}): ${String(preset)}`,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type FastifyReply, type FastifyRequest } from "fastify";
|
|
2
|
+
|
|
3
|
+
import { SD } from "../dict/sd";
|
|
4
|
+
|
|
5
|
+
/** Sonamu UI가 모든 요청에 붙이는 표식 헤더입니다. 커스텀 헤더라서 교차 출처 요청은 반드시 preflight를 거칩니다. */
|
|
6
|
+
export const SONAMU_UI_REQUEST_HEADER = "x-sonamu-ui";
|
|
7
|
+
|
|
8
|
+
const ALLOWED_FETCH_SITES = new Set(["same-origin", "none"]);
|
|
9
|
+
|
|
10
|
+
function isSameOriginUIRequest(request: FastifyRequest): boolean {
|
|
11
|
+
// form·no-cors fetch 같은 단순 요청은 application/json을 보낼 수 없습니다.
|
|
12
|
+
const contentType = request.headers["content-type"]?.split(";")[0]?.trim().toLowerCase();
|
|
13
|
+
if (contentType !== "application/json") {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (request.headers[SONAMU_UI_REQUEST_HEADER] !== "1") {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 앱이 CORS를 넓게 열어 preflight가 통과하더라도 브라우저가 붙인 출처 표시로 다시 거릅니다.
|
|
21
|
+
const fetchSite = request.headers["sec-fetch-site"];
|
|
22
|
+
if (fetchSite !== undefined) {
|
|
23
|
+
return ALLOWED_FETCH_SITES.has(fetchSite);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Sec-Fetch-Site를 보내지 않는 브라우저는 Origin과 Host를 비교합니다.
|
|
27
|
+
const origin = request.headers.origin;
|
|
28
|
+
if (origin === undefined) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
return new URL(origin).host === request.headers.host;
|
|
33
|
+
} catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 개발자 PC에서 프로세스를 실행하는 Sonamu UI 라우트용 onRequest 훅입니다.
|
|
40
|
+
*
|
|
41
|
+
* 동일 출처 Sonamu UI가 보낸 JSON 요청만 통과시키고, 다른 출처 웹페이지가 보낸
|
|
42
|
+
* form·no-cors fetch·교차 출처 요청은 본문을 파싱하기 전에 403으로 거부합니다.
|
|
43
|
+
*/
|
|
44
|
+
export async function requireSameOriginUIRequest(
|
|
45
|
+
request: FastifyRequest,
|
|
46
|
+
reply: FastifyReply,
|
|
47
|
+
): Promise<FastifyReply | undefined> {
|
|
48
|
+
if (!isSameOriginUIRequest(request)) {
|
|
49
|
+
// async 훅에서 응답을 끝낼 때는 reply를 반환해야 핸들러로 넘어가지 않습니다.
|
|
50
|
+
return reply.code(403).send({ message: SD("error.forbidden"), issues: [] });
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|