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
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import os from "os";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
import { Workbook } from "@sheetkit/node";
|
|
6
|
+
import ts from "@typescript/typescript6";
|
|
7
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
8
|
+
|
|
9
|
+
import { type SonamuConfig } from "../../api/config";
|
|
10
|
+
import { Sonamu } from "../../api/sonamu";
|
|
11
|
+
import { type AbsolutePath } from "../../utils/path-utils";
|
|
12
|
+
import { setSDConfig } from "../sd";
|
|
13
|
+
import { SonamuDictionary } from "../sonamu-dictionary";
|
|
14
|
+
|
|
15
|
+
const i18n = { defaultLocale: "ko", supportedLocales: ["ko", "en"] };
|
|
16
|
+
|
|
17
|
+
// 사전 관리 코드는 config의 i18n과 api.dir만 읽지만, SonamuConfig 전체 계약을 만족하는 값을 쓴다.
|
|
18
|
+
const testConfig = {
|
|
19
|
+
api: { dir: "api", route: { prefix: "/api" } },
|
|
20
|
+
i18n,
|
|
21
|
+
sync: { targets: [] },
|
|
22
|
+
database: {},
|
|
23
|
+
server: {
|
|
24
|
+
apiConfig: {
|
|
25
|
+
contextProvider: (defaultContext) => defaultContext,
|
|
26
|
+
guardHandler: () => undefined,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
} satisfies SonamuConfig;
|
|
30
|
+
|
|
31
|
+
const KO_SEED = `export default {
|
|
32
|
+
"common.results": (count: number) => \`\${count}개 결과\`,
|
|
33
|
+
"common.save": "저장",
|
|
34
|
+
} as const;
|
|
35
|
+
`;
|
|
36
|
+
|
|
37
|
+
const EN_SEED = `import { defineLocale } from "./sd.generated";
|
|
38
|
+
|
|
39
|
+
export default defineLocale({
|
|
40
|
+
"common.results": (count: number) => \`\${count} results\`,
|
|
41
|
+
"common.save": "Save",
|
|
42
|
+
});
|
|
43
|
+
`;
|
|
44
|
+
|
|
45
|
+
// 따옴표를 닫고 계산된 속성 이름으로 코드를 끼워 넣으려는 키
|
|
46
|
+
const INJECTION_KEY = 'audit.x": 0, [globalThis.__son623 = 1]: 0, "audit.y';
|
|
47
|
+
// 함수처럼 보이지만 허용 목록(isExpressionFunction)을 통과하지 못하는 값
|
|
48
|
+
const NEW_FUNCTION_VALUE = "() => globalThis.__son623";
|
|
49
|
+
|
|
50
|
+
let apiRoot: AbsolutePath;
|
|
51
|
+
let dict: SonamuDictionary;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 경로가 `/`로 시작하는지 확인해 AbsolutePath로 좁힙니다.
|
|
55
|
+
*/
|
|
56
|
+
function isAbsolutePath(value: string): value is AbsolutePath {
|
|
57
|
+
return value.startsWith("/");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function dictPath(locale: string): string {
|
|
61
|
+
return path.join(apiRoot, "src", "i18n", `${locale}.ts`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function readDict(locale: string): string {
|
|
65
|
+
return fs.readFileSync(dictPath(locale), "utf-8");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 사전 파일의 기본 내보내기 객체 속성을 검사합니다.
|
|
70
|
+
* 계산된 속성 이름이 있으면 실패시키고, 키와 값의 종류를 돌려줍니다.
|
|
71
|
+
*/
|
|
72
|
+
function inspectDict(locale: string): Map<string, "string" | "function"> {
|
|
73
|
+
const source = ts.createSourceFile(
|
|
74
|
+
dictPath(locale),
|
|
75
|
+
readDict(locale),
|
|
76
|
+
ts.ScriptTarget.Latest,
|
|
77
|
+
true,
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const visit = (node: ts.Node): void => {
|
|
81
|
+
expect(ts.isComputedPropertyName(node), node.getText(source)).toBe(false);
|
|
82
|
+
ts.forEachChild(node, visit);
|
|
83
|
+
};
|
|
84
|
+
visit(source);
|
|
85
|
+
|
|
86
|
+
const result = new Map<string, "string" | "function">();
|
|
87
|
+
for (const statement of source.statements) {
|
|
88
|
+
if (!ts.isExportAssignment(statement)) continue;
|
|
89
|
+
let expr: ts.Expression = statement.expression;
|
|
90
|
+
if (ts.isAsExpression(expr)) expr = expr.expression;
|
|
91
|
+
if (ts.isCallExpression(expr)) expr = expr.arguments[0];
|
|
92
|
+
if (!ts.isObjectLiteralExpression(expr)) throw new Error("객체 리터럴이 아닙니다");
|
|
93
|
+
for (const prop of expr.properties) {
|
|
94
|
+
if (!ts.isPropertyAssignment(prop) || !ts.isStringLiteral(prop.name)) {
|
|
95
|
+
throw new Error(`허용되지 않는 속성: ${prop.getText(source)}`);
|
|
96
|
+
}
|
|
97
|
+
if (ts.isStringLiteral(prop.initializer)) {
|
|
98
|
+
result.set(prop.name.text, "string");
|
|
99
|
+
} else if (ts.isArrowFunction(prop.initializer)) {
|
|
100
|
+
result.set(prop.name.text, "function");
|
|
101
|
+
} else {
|
|
102
|
+
throw new Error(`허용되지 않는 값: ${prop.initializer.getText(source)}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function buildXlsx(rows: string[][]): Buffer {
|
|
110
|
+
const wb = new Workbook();
|
|
111
|
+
wb.setRowValues("Sheet1", 1, "A", ["key", "source", "ko", "en"]);
|
|
112
|
+
rows.forEach((row, index) => {
|
|
113
|
+
wb.setRowValues("Sheet1", index + 2, "A", row);
|
|
114
|
+
});
|
|
115
|
+
return wb.writeBufferSync();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
beforeEach(() => {
|
|
119
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "son623-dict-"));
|
|
120
|
+
if (!isAbsolutePath(tempDir)) {
|
|
121
|
+
throw new Error(`임시 디렉터리가 절대 경로가 아닙니다: ${tempDir}`);
|
|
122
|
+
}
|
|
123
|
+
apiRoot = tempDir;
|
|
124
|
+
fs.mkdirSync(path.join(apiRoot, "src", "i18n"), { recursive: true });
|
|
125
|
+
fs.writeFileSync(dictPath("ko"), KO_SEED);
|
|
126
|
+
fs.writeFileSync(dictPath("en"), EN_SEED);
|
|
127
|
+
|
|
128
|
+
setSDConfig(i18n);
|
|
129
|
+
vi.spyOn(Sonamu, "apiRootPath", "get").mockReturnValue(apiRoot);
|
|
130
|
+
vi.spyOn(Sonamu, "config", "get").mockReturnValue(testConfig);
|
|
131
|
+
dict = new SonamuDictionary();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
afterEach(() => {
|
|
135
|
+
vi.restoreAllMocks();
|
|
136
|
+
fs.rmSync(apiRoot, { recursive: true, force: true });
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe("SonamuDictionary 편집 API는 외부 키를 코드로 쓰지 않는다", () => {
|
|
140
|
+
it("createEntry: 따옴표를 닫는 키도 문자열 리터럴 키로만 기록한다", async () => {
|
|
141
|
+
await dict.createEntry({ key: INJECTION_KEY, values: { ko: "v" } });
|
|
142
|
+
|
|
143
|
+
const entries = inspectDict("ko");
|
|
144
|
+
expect(entries.get(INJECTION_KEY)).toBe("string");
|
|
145
|
+
expect(dict.loadProjectDict("ko").entries).toContainEqual({
|
|
146
|
+
key: INJECTION_KEY,
|
|
147
|
+
value: "v",
|
|
148
|
+
isFunction: false,
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("createEntry: 허용된 화살표 함수는 함수로, 허용 목록 밖의 함수 형태 값은 문자열로 기록한다", async () => {
|
|
153
|
+
await dict.createEntry({
|
|
154
|
+
key: "audit.fn",
|
|
155
|
+
values: { ko: "(count: number) => `${count}건`", en: NEW_FUNCTION_VALUE },
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
expect(inspectDict("ko").get("audit.fn")).toBe("function");
|
|
159
|
+
expect(inspectDict("en").get("audit.fn")).toBe("string");
|
|
160
|
+
expect(readDict("en")).not.toContain(`: ${NEW_FUNCTION_VALUE},`);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("updateEntry: 새 키 이름에 코드를 넣어도 문자열 리터럴 키로만 기록한다", async () => {
|
|
164
|
+
await dict.updateEntry({
|
|
165
|
+
oldKey: "common.save",
|
|
166
|
+
newKey: INJECTION_KEY,
|
|
167
|
+
source: "project",
|
|
168
|
+
values: { ko: "저장" },
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const entries = inspectDict("ko");
|
|
172
|
+
expect(entries.get(INJECTION_KEY)).toBe("string");
|
|
173
|
+
expect(entries.has("common.save")).toBe(false);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("updateEntry: 키 이름만 바꾸면 사전 파일에 있던 함수 값은 그대로 유지한다", async () => {
|
|
177
|
+
const values = Object.fromEntries(
|
|
178
|
+
i18n.supportedLocales.map((locale) => {
|
|
179
|
+
const entry = dict.loadProjectDict(locale).entries.find((e) => e.key === "common.results");
|
|
180
|
+
return [locale, entry?.value ?? ""];
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
await dict.updateEntry({
|
|
185
|
+
oldKey: "common.results",
|
|
186
|
+
newKey: "common.count",
|
|
187
|
+
source: "project",
|
|
188
|
+
values,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
expect(inspectDict("ko").get("common.count")).toBe("function");
|
|
192
|
+
expect(inspectDict("en").get("common.count")).toBe("function");
|
|
193
|
+
expect(inspectDict("ko").has("common.results")).toBe(false);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
describe("SonamuDictionary Excel import는 외부 키를 코드로 쓰지 않는다", () => {
|
|
198
|
+
it("코드를 넣은 키는 문자열 리터럴 키로, 내보낸 그대로의 함수 값은 기존 함수로 기록한다", async () => {
|
|
199
|
+
const buffer = buildXlsx([
|
|
200
|
+
[INJECTION_KEY, "project", "v", "v"],
|
|
201
|
+
[
|
|
202
|
+
"common.results",
|
|
203
|
+
"project",
|
|
204
|
+
"(count: number) => `${count}개 결과`",
|
|
205
|
+
"(count: number) => `${count} results`",
|
|
206
|
+
],
|
|
207
|
+
]);
|
|
208
|
+
|
|
209
|
+
await dict.importFromExcel(buffer);
|
|
210
|
+
|
|
211
|
+
for (const locale of i18n.supportedLocales) {
|
|
212
|
+
const entries = inspectDict(locale);
|
|
213
|
+
expect(entries.get(INJECTION_KEY)).toBe("string");
|
|
214
|
+
expect(entries.get("common.results")).toBe("function");
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
});
|
package/src/dict/en.ts
CHANGED
|
@@ -67,6 +67,8 @@ export default {
|
|
|
67
67
|
"sonamu.error.shadowDbTestFailed": "Error during Shadow DB test",
|
|
68
68
|
"sonamu.error.allFilesExist": "All files already exist at the path.",
|
|
69
69
|
"sonamu.error.allFilesGenerated": "All files have already been generated.",
|
|
70
|
+
"sonamu.error.authEntityModelScaffoldRefused": (entityId: string) =>
|
|
71
|
+
`${entityId} is a better-auth credential entity, so a model cannot be scaffolded for it. The generated unguarded API would expose secrets such as session tokens and password hashes. Use the better-auth API, or write only the methods you need with an admin guard.`,
|
|
70
72
|
"sonamu.error.fixtureRecordRequired": "Fixture record not found. Please fetch fixtures first.",
|
|
71
73
|
"sonamu.error.presetOrAbsPathRequired": "preset or absPath must be provided",
|
|
72
74
|
"sonamu.error.entityIdsRequired": "entityIds must be provided",
|
package/src/dict/ko.ts
CHANGED
|
@@ -67,6 +67,8 @@ export default {
|
|
|
67
67
|
"sonamu.error.shadowDbTestFailed": "Shadow DB 테스트 진행 중 에러",
|
|
68
68
|
"sonamu.error.allFilesExist": "이미 경로에 모든 파일이 존재합니다.",
|
|
69
69
|
"sonamu.error.allFilesGenerated": "이미 모든 파일이 생성된 상태입니다.",
|
|
70
|
+
"sonamu.error.authEntityModelScaffoldRefused": (entityId: string) =>
|
|
71
|
+
`${entityId} 엔티티는 better-auth 인증 정보를 저장하므로 모델을 스캐폴드할 수 없습니다. 생성되는 가드 없는 API가 세션 토큰·비밀번호 해시 같은 비밀 값을 노출합니다. better-auth API를 쓰거나, 필요한 메서드만 관리자 가드를 걸어 직접 작성하세요.`,
|
|
70
72
|
"sonamu.error.fixtureRecordRequired": "픽스쳐 레코드가 없습니다. 픽스쳐 조회 후 시도하세요.",
|
|
71
73
|
"sonamu.error.presetOrAbsPathRequired": "preset or absPath must be provided",
|
|
72
74
|
"sonamu.error.entityIdsRequired": "entityIds must be provided",
|
|
@@ -57,6 +57,193 @@ function readCellText(cell: JsRowCell | undefined): string {
|
|
|
57
57
|
return "";
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
+
/** 사전 함수 값에서 부를 수 있는 헬퍼. generateProjectDict가 이 이름들의 import를 만든다. */
|
|
61
|
+
const DICT_HELPER_NAMES = new Set(["plural", "josa", "format"]);
|
|
62
|
+
|
|
63
|
+
/** 프로토타입 체인을 따라 Function 생성자 등에 닿을 수 있어 접근을 막는 프로퍼티 이름 */
|
|
64
|
+
const FORBIDDEN_PROPERTY_NAMES = new Set([
|
|
65
|
+
"constructor",
|
|
66
|
+
"prototype",
|
|
67
|
+
"__proto__",
|
|
68
|
+
"__defineGetter__",
|
|
69
|
+
"__defineSetter__",
|
|
70
|
+
"__lookupGetter__",
|
|
71
|
+
"__lookupSetter__",
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
/** 매개변수 값에 대해 부를 수 있는 원시 값 메서드. 앱 객체의 임의 메서드 호출은 막는다. */
|
|
75
|
+
const SAFE_METHOD_NAMES = new Set([
|
|
76
|
+
"join",
|
|
77
|
+
"padEnd",
|
|
78
|
+
"padStart",
|
|
79
|
+
"slice",
|
|
80
|
+
"substring",
|
|
81
|
+
"toFixed",
|
|
82
|
+
"toLocaleDateString",
|
|
83
|
+
"toLocaleString",
|
|
84
|
+
"toLocaleTimeString",
|
|
85
|
+
"toLowerCase",
|
|
86
|
+
"toString",
|
|
87
|
+
"toUpperCase",
|
|
88
|
+
"trim",
|
|
89
|
+
]);
|
|
90
|
+
|
|
91
|
+
const SAFE_BINARY_OPERATORS = new Set<ts.SyntaxKind>([
|
|
92
|
+
ts.SyntaxKind.PlusToken,
|
|
93
|
+
ts.SyntaxKind.MinusToken,
|
|
94
|
+
ts.SyntaxKind.AsteriskToken,
|
|
95
|
+
ts.SyntaxKind.SlashToken,
|
|
96
|
+
ts.SyntaxKind.PercentToken,
|
|
97
|
+
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
|
98
|
+
ts.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
99
|
+
ts.SyntaxKind.EqualsEqualsToken,
|
|
100
|
+
ts.SyntaxKind.ExclamationEqualsToken,
|
|
101
|
+
ts.SyntaxKind.LessThanToken,
|
|
102
|
+
ts.SyntaxKind.GreaterThanToken,
|
|
103
|
+
ts.SyntaxKind.LessThanEqualsToken,
|
|
104
|
+
ts.SyntaxKind.GreaterThanEqualsToken,
|
|
105
|
+
ts.SyntaxKind.AmpersandAmpersandToken,
|
|
106
|
+
ts.SyntaxKind.BarBarToken,
|
|
107
|
+
ts.SyntaxKind.QuestionQuestionToken,
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
const SAFE_PREFIX_OPERATORS = new Set<ts.SyntaxKind>([
|
|
111
|
+
ts.SyntaxKind.ExclamationToken,
|
|
112
|
+
ts.SyntaxKind.MinusToken,
|
|
113
|
+
ts.SyntaxKind.PlusToken,
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 소스 안에 주석이 있는지 확인합니다.
|
|
118
|
+
* 주석은 항상 어떤 토큰의 앞쪽 trivia에 들어가므로, 모든 말단 토큰의 앞쪽 trivia가 공백뿐인지 봅니다.
|
|
119
|
+
*/
|
|
120
|
+
function hasComment(node: ts.Node, sourceFile: ts.SourceFile): boolean {
|
|
121
|
+
const children = node.getChildren(sourceFile);
|
|
122
|
+
if (children.length === 0) {
|
|
123
|
+
const trivia = sourceFile.text.slice(node.pos, node.getStart(sourceFile));
|
|
124
|
+
return !/^\s*$/.test(trivia);
|
|
125
|
+
}
|
|
126
|
+
return children.some((child) => hasComment(child, sourceFile));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 사전 함수 값의 화살표 함수가 허용된 템플릿 형식인지 확인합니다.
|
|
131
|
+
* 블록 본문·async·기본값·구조 분해·나머지 매개변수와 헬퍼 이름을 가리는 매개변수는 받지 않습니다.
|
|
132
|
+
*/
|
|
133
|
+
function isSafeDictArrowFunction(fn: ts.ArrowFunction, outerScope: ReadonlySet<string>): boolean {
|
|
134
|
+
if ((fn.modifiers?.length ?? 0) > 0 || fn.typeParameters !== undefined || ts.isBlock(fn.body)) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
const scope = new Set(outerScope);
|
|
138
|
+
for (const param of fn.parameters) {
|
|
139
|
+
if (
|
|
140
|
+
!ts.isIdentifier(param.name) ||
|
|
141
|
+
param.initializer !== undefined ||
|
|
142
|
+
param.dotDotDotToken !== undefined ||
|
|
143
|
+
(param.modifiers?.length ?? 0) > 0 ||
|
|
144
|
+
DICT_HELPER_NAMES.has(param.name.text)
|
|
145
|
+
) {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
// 타입 주석은 런타임에 지워지므로 검사하지 않습니다.
|
|
149
|
+
scope.add(param.name.text);
|
|
150
|
+
}
|
|
151
|
+
return isSafeDictExpression(fn.body, scope);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 사전 함수 본문의 표현식이 허용 목록 안의 구문과 식별자만 쓰는지 확인합니다.
|
|
156
|
+
* 식별자는 매개변수와 plural/josa/format 헬퍼만 참조할 수 있습니다.
|
|
157
|
+
*/
|
|
158
|
+
function isSafeDictExpression(node: ts.Expression, scope: ReadonlySet<string>): boolean {
|
|
159
|
+
switch (node.kind) {
|
|
160
|
+
case ts.SyntaxKind.StringLiteral:
|
|
161
|
+
case ts.SyntaxKind.NumericLiteral:
|
|
162
|
+
case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
|
|
163
|
+
case ts.SyntaxKind.TrueKeyword:
|
|
164
|
+
case ts.SyntaxKind.FalseKeyword:
|
|
165
|
+
case ts.SyntaxKind.NullKeyword:
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
if (ts.isIdentifier(node)) {
|
|
169
|
+
return scope.has(node.text);
|
|
170
|
+
}
|
|
171
|
+
if (ts.isTemplateExpression(node)) {
|
|
172
|
+
return node.templateSpans.every((span) => isSafeDictExpression(span.expression, scope));
|
|
173
|
+
}
|
|
174
|
+
if (ts.isParenthesizedExpression(node) || ts.isTypeOfExpression(node)) {
|
|
175
|
+
return isSafeDictExpression(node.expression, scope);
|
|
176
|
+
}
|
|
177
|
+
if (ts.isConditionalExpression(node)) {
|
|
178
|
+
return (
|
|
179
|
+
isSafeDictExpression(node.condition, scope) &&
|
|
180
|
+
isSafeDictExpression(node.whenTrue, scope) &&
|
|
181
|
+
isSafeDictExpression(node.whenFalse, scope)
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
if (ts.isBinaryExpression(node)) {
|
|
185
|
+
return (
|
|
186
|
+
SAFE_BINARY_OPERATORS.has(node.operatorToken.kind) &&
|
|
187
|
+
isSafeDictExpression(node.left, scope) &&
|
|
188
|
+
isSafeDictExpression(node.right, scope)
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
if (ts.isPrefixUnaryExpression(node)) {
|
|
192
|
+
return SAFE_PREFIX_OPERATORS.has(node.operator) && isSafeDictExpression(node.operand, scope);
|
|
193
|
+
}
|
|
194
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
195
|
+
return (
|
|
196
|
+
ts.isIdentifier(node.name) &&
|
|
197
|
+
!FORBIDDEN_PROPERTY_NAMES.has(node.name.text) &&
|
|
198
|
+
isSafeDictExpression(node.expression, scope)
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
if (ts.isCallExpression(node)) {
|
|
202
|
+
return (
|
|
203
|
+
node.typeArguments === undefined &&
|
|
204
|
+
isSafeDictCallee(node.expression, scope) &&
|
|
205
|
+
node.arguments.every((arg) => isSafeDictExpression(arg, scope))
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
209
|
+
// plural(n, { one: ..., other: ... }) 옵션 객체
|
|
210
|
+
return node.properties.every(
|
|
211
|
+
(prop) =>
|
|
212
|
+
ts.isPropertyAssignment(prop) &&
|
|
213
|
+
(ts.isIdentifier(prop.name) ||
|
|
214
|
+
ts.isStringLiteral(prop.name) ||
|
|
215
|
+
ts.isNumericLiteral(prop.name)) &&
|
|
216
|
+
prop.name.text !== "__proto__" &&
|
|
217
|
+
isSafeDictExpression(prop.initializer, scope),
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
if (ts.isArrowFunction(node)) {
|
|
221
|
+
// plural의 other: (n) => ... 처럼 중첩된 화살표 함수
|
|
222
|
+
return isSafeDictArrowFunction(node, scope);
|
|
223
|
+
}
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 호출 대상이 plural/josa, format.<메서드>, 또는 안전한 원시 값 메서드인지 확인합니다.
|
|
229
|
+
*/
|
|
230
|
+
function isSafeDictCallee(callee: ts.Expression, scope: ReadonlySet<string>): boolean {
|
|
231
|
+
if (ts.isIdentifier(callee)) {
|
|
232
|
+
return callee.text === "plural" || callee.text === "josa";
|
|
233
|
+
}
|
|
234
|
+
if (!ts.isPropertyAccessExpression(callee) || !ts.isIdentifier(callee.name)) {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
const methodName = callee.name.text;
|
|
238
|
+
if (FORBIDDEN_PROPERTY_NAMES.has(methodName)) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
if (ts.isIdentifier(callee.expression) && callee.expression.text === "format") {
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
return SAFE_METHOD_NAMES.has(methodName) && isSafeDictExpression(callee.expression, scope);
|
|
245
|
+
}
|
|
246
|
+
|
|
60
247
|
/**
|
|
61
248
|
* Sonamu Dictionary 관리 클래스
|
|
62
249
|
* i18n 딕셔너리의 CRUD 및 Excel import/export를 담당합니다.
|
|
@@ -116,7 +303,14 @@ export class SonamuDictionary {
|
|
|
116
303
|
}
|
|
117
304
|
|
|
118
305
|
/**
|
|
119
|
-
*
|
|
306
|
+
* 사전 값 문자열을 함수로 출력해도 되는지 판별합니다.
|
|
307
|
+
*
|
|
308
|
+
* Excel import와 사전 편집 API의 값은 외부 입력이므로, generateProjectDict가 원문 그대로
|
|
309
|
+
* TS 소스에 쓰는 함수 값은 엄격하게 검사합니다. 아래를 모두 만족할 때만 true이고,
|
|
310
|
+
* 그 밖의 값은 false가 되어 문자열 리터럴로 저장됩니다.
|
|
311
|
+
* - 문법 오류·주석 없이 전체가 화살표 함수 표현식 하나
|
|
312
|
+
* - 매개변수는 기본값 없는 단순 식별자, 본문은 블록이 아닌 표현식
|
|
313
|
+
* - 본문은 리터럴, 템플릿 리터럴, 매개변수, plural/josa/format 헬퍼, 안전한 연산자와 메서드만 사용
|
|
120
314
|
*/
|
|
121
315
|
isExpressionFunction(code: string): boolean {
|
|
122
316
|
// 빈 문자열이나 공백만 있는 경우
|
|
@@ -124,9 +318,31 @@ export class SonamuDictionary {
|
|
|
124
318
|
return false;
|
|
125
319
|
}
|
|
126
320
|
|
|
127
|
-
const
|
|
321
|
+
const sourceFile = ts.createSourceFile(
|
|
322
|
+
"dict-value.ts",
|
|
323
|
+
code,
|
|
324
|
+
ts.ScriptTarget.Latest,
|
|
325
|
+
true,
|
|
326
|
+
ts.ScriptKind.TS,
|
|
327
|
+
);
|
|
328
|
+
const [statement, ...rest] = sourceFile.statements;
|
|
329
|
+
if (
|
|
330
|
+
statement === undefined ||
|
|
331
|
+
rest.length > 0 ||
|
|
332
|
+
!ts.isExpressionStatement(statement) ||
|
|
333
|
+
// 세미콜론 등 표현식 뒤에 붙은 토큰이 없어야 한다
|
|
334
|
+
statement.end !== statement.expression.end ||
|
|
335
|
+
!ts.isArrowFunction(statement.expression)
|
|
336
|
+
) {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const { diagnostics } = ts.transpileModule(code, { reportDiagnostics: true });
|
|
341
|
+
if ((diagnostics?.length ?? 0) > 0 || hasComment(sourceFile, sourceFile)) {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
128
344
|
|
|
129
|
-
return
|
|
345
|
+
return isSafeDictArrowFunction(statement.expression, new Set());
|
|
130
346
|
}
|
|
131
347
|
|
|
132
348
|
/**
|
|
@@ -377,7 +593,7 @@ export class SonamuDictionary {
|
|
|
377
593
|
|
|
378
594
|
// format 사용 시 createFormat 호출 추가
|
|
379
595
|
if (usesFormat) {
|
|
380
|
-
lines.push(`const format = createFormat(
|
|
596
|
+
lines.push(`const format = createFormat(${JSON.stringify(locale)});`);
|
|
381
597
|
lines.push("");
|
|
382
598
|
}
|
|
383
599
|
|
|
@@ -392,11 +608,13 @@ export class SonamuDictionary {
|
|
|
392
608
|
}
|
|
393
609
|
|
|
394
610
|
for (const entry of sorted) {
|
|
611
|
+
// 키는 외부 입력(Excel import, 사전 편집)이므로 항상 문자열 리터럴로 직렬화합니다.
|
|
612
|
+
const key = JSON.stringify(entry.key);
|
|
395
613
|
if (entry.isFunction) {
|
|
396
|
-
// 함수인 경우: 원형 그대로 출력
|
|
397
|
-
lines.push(`
|
|
614
|
+
// 함수인 경우: 원형 그대로 출력 (외부 입력은 isExpressionFunction 검사를 통과한 값만 함수가 됨)
|
|
615
|
+
lines.push(` ${key}: ${entry.value},`);
|
|
398
616
|
} else {
|
|
399
|
-
lines.push(`
|
|
617
|
+
lines.push(` ${key}: ${JSON.stringify(entry.value)},`);
|
|
400
618
|
}
|
|
401
619
|
}
|
|
402
620
|
|
|
@@ -751,6 +969,35 @@ export class SonamuDictionary {
|
|
|
751
969
|
};
|
|
752
970
|
}
|
|
753
971
|
|
|
972
|
+
/**
|
|
973
|
+
* UI 편집 API나 Excel import로 받은 외부 값을 사전 엔트리로 만듭니다.
|
|
974
|
+
*
|
|
975
|
+
* 같은 locale 사전 파일에 이미 있는 함수 엔트리(`trustedKeys` 중 하나의 키)와 원문이 같으면
|
|
976
|
+
* 그 함수를 그대로 유지합니다. 내보낸 Excel을 다시 가져오거나 키 이름만 바꿀 때, 허용 목록에
|
|
977
|
+
* 없는 형태의 기존 함수도 문자열로 바뀌지 않게 하기 위함입니다.
|
|
978
|
+
* 그 밖의 값은 허용된 화살표 함수(`isExpressionFunction`)일 때만 함수 엔트리가 되고,
|
|
979
|
+
* 나머지는 모두 문자열 엔트리로 저장합니다.
|
|
980
|
+
*
|
|
981
|
+
* @param key - 저장할 키
|
|
982
|
+
* @param value - 외부에서 받은 값 (trim된 문자열)
|
|
983
|
+
* @param existingEntries - 같은 locale의 현재 사전 파일 엔트리
|
|
984
|
+
* @param trustedKeys - 기존 함수를 이어받을 수 있는 키 (기본값: `key`, 키 변경 시 이전 키 포함)
|
|
985
|
+
*/
|
|
986
|
+
private toInputEntry(
|
|
987
|
+
key: string,
|
|
988
|
+
value: string,
|
|
989
|
+
existingEntries: DictEntry[],
|
|
990
|
+
trustedKeys: string[] = [key],
|
|
991
|
+
): DictEntry {
|
|
992
|
+
const preserved = existingEntries.find(
|
|
993
|
+
(e) => e.isFunction && trustedKeys.includes(e.key) && e.value === value,
|
|
994
|
+
);
|
|
995
|
+
if (preserved) {
|
|
996
|
+
return { key, value: preserved.value, isFunction: true };
|
|
997
|
+
}
|
|
998
|
+
return { key, value, isFunction: this.isExpressionFunction(value) };
|
|
999
|
+
}
|
|
1000
|
+
|
|
754
1001
|
/**
|
|
755
1002
|
* Excel에서 가져오기
|
|
756
1003
|
*
|
|
@@ -771,6 +1018,15 @@ export class SonamuDictionary {
|
|
|
771
1018
|
let updatedEntities = 0;
|
|
772
1019
|
let updatedLocales = 0;
|
|
773
1020
|
|
|
1021
|
+
// 함수 엔트리 유지 판단에 쓸 locale별 현재 사전 파일 엔트리
|
|
1022
|
+
const existingDictEntries: Record<string, DictEntry[]> = {};
|
|
1023
|
+
for (const locale of locales) {
|
|
1024
|
+
existingDictEntries[locale] = this.loadProjectDict(locale).entries;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
// entity.json 갱신은 모든 행을 엔트리로 만든 뒤에 적용한다 (중간에 실패한 import가 일부만 반영되지 않게).
|
|
1028
|
+
const entityUpdates: { key: string; value: string }[] = [];
|
|
1029
|
+
|
|
774
1030
|
// locale별 project dict entries
|
|
775
1031
|
const projectDictEntries: Record<string, DictEntry[]> = {};
|
|
776
1032
|
for (const locale of locales) {
|
|
@@ -825,10 +1081,7 @@ export class SonamuDictionary {
|
|
|
825
1081
|
// entity source: default locale만 entity.json에 저장
|
|
826
1082
|
const defaultValue = rowValues[defaultLocale];
|
|
827
1083
|
if (defaultValue) {
|
|
828
|
-
|
|
829
|
-
if (updated) {
|
|
830
|
-
updatedEntities++;
|
|
831
|
-
}
|
|
1084
|
+
entityUpdates.push({ key, value: defaultValue });
|
|
832
1085
|
}
|
|
833
1086
|
|
|
834
1087
|
// non-default locale은 project dict에 저장
|
|
@@ -836,11 +1089,9 @@ export class SonamuDictionary {
|
|
|
836
1089
|
if (locale === defaultLocale) continue;
|
|
837
1090
|
const cellValue = rowValues[locale]?.trim();
|
|
838
1091
|
if (cellValue) {
|
|
839
|
-
projectDictEntries[locale].push(
|
|
840
|
-
key,
|
|
841
|
-
|
|
842
|
-
isFunction: this.isExpressionFunction(cellValue),
|
|
843
|
-
});
|
|
1092
|
+
projectDictEntries[locale].push(
|
|
1093
|
+
this.toInputEntry(key, cellValue, existingDictEntries[locale]),
|
|
1094
|
+
);
|
|
844
1095
|
}
|
|
845
1096
|
}
|
|
846
1097
|
} else if (source === "project") {
|
|
@@ -848,16 +1099,21 @@ export class SonamuDictionary {
|
|
|
848
1099
|
for (const locale of locales) {
|
|
849
1100
|
const cellValue = rowValues[locale]?.trim();
|
|
850
1101
|
if (cellValue) {
|
|
851
|
-
projectDictEntries[locale].push(
|
|
852
|
-
key,
|
|
853
|
-
|
|
854
|
-
isFunction: this.isExpressionFunction(cellValue),
|
|
855
|
-
});
|
|
1102
|
+
projectDictEntries[locale].push(
|
|
1103
|
+
this.toInputEntry(key, cellValue, existingDictEntries[locale]),
|
|
1104
|
+
);
|
|
856
1105
|
}
|
|
857
1106
|
}
|
|
858
1107
|
}
|
|
859
1108
|
}
|
|
860
1109
|
|
|
1110
|
+
for (const { key, value } of entityUpdates) {
|
|
1111
|
+
const updated = await this.updateEntityByKey(key, value);
|
|
1112
|
+
if (updated) {
|
|
1113
|
+
updatedEntities++;
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
|
|
861
1117
|
// Project dict 파일 생성
|
|
862
1118
|
for (const locale of locales) {
|
|
863
1119
|
const entries = projectDictEntries[locale];
|
|
@@ -888,15 +1144,12 @@ export class SonamuDictionary {
|
|
|
888
1144
|
const { defaultLocale, supportedLocales } = this.getI18nConfig();
|
|
889
1145
|
const locales = supportedLocales;
|
|
890
1146
|
|
|
891
|
-
// entity source의 default locale 처리
|
|
892
|
-
if (source === "entity" && values[defaultLocale]) {
|
|
893
|
-
await this.updateEntityByKey(newKey, values[defaultLocale]);
|
|
894
|
-
}
|
|
895
|
-
|
|
896
1147
|
// project dict 업데이트
|
|
897
1148
|
// - entity의 non-default locale
|
|
898
1149
|
// - project source의 모든 locale
|
|
899
1150
|
// - sonamu source의 모든 locale (override)
|
|
1151
|
+
// 모든 locale 엔트리를 먼저 만든 뒤 저장한다 (중간에 실패한 요청이 일부 locale만 바꾸지 않게).
|
|
1152
|
+
const pendingSaves: { locale: string; entries: DictEntry[] }[] = [];
|
|
900
1153
|
for (const locale of locales) {
|
|
901
1154
|
// entity source의 default locale은 entity.json에서 처리했으므로 스킵
|
|
902
1155
|
if (source === "entity" && locale === defaultLocale) continue;
|
|
@@ -906,6 +1159,8 @@ export class SonamuDictionary {
|
|
|
906
1159
|
|
|
907
1160
|
// 기존 dict 로드
|
|
908
1161
|
const { entries } = this.loadProjectDict(locale);
|
|
1162
|
+
// 기존 함수 유지 판단용 원본 (아래에서 entries를 바꾸기 전 상태)
|
|
1163
|
+
const originalEntries = [...entries];
|
|
909
1164
|
|
|
910
1165
|
// key 변경 시 기존 key 제거
|
|
911
1166
|
if (oldKey !== newKey) {
|
|
@@ -917,11 +1172,7 @@ export class SonamuDictionary {
|
|
|
917
1172
|
|
|
918
1173
|
// 새 값 업데이트 또는 추가
|
|
919
1174
|
const existingIndex = entries.findIndex((e) => e.key === newKey);
|
|
920
|
-
const newEntry
|
|
921
|
-
key: newKey,
|
|
922
|
-
value: cellValue,
|
|
923
|
-
isFunction: this.isExpressionFunction(cellValue),
|
|
924
|
-
};
|
|
1175
|
+
const newEntry = this.toInputEntry(newKey, cellValue, originalEntries, [oldKey, newKey]);
|
|
925
1176
|
|
|
926
1177
|
if (existingIndex !== -1) {
|
|
927
1178
|
entries[existingIndex] = newEntry;
|
|
@@ -929,7 +1180,16 @@ export class SonamuDictionary {
|
|
|
929
1180
|
entries.push(newEntry);
|
|
930
1181
|
}
|
|
931
1182
|
|
|
932
|
-
|
|
1183
|
+
pendingSaves.push({ locale, entries });
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
// entity source의 default locale 처리
|
|
1187
|
+
if (source === "entity" && values[defaultLocale]) {
|
|
1188
|
+
await this.updateEntityByKey(newKey, values[defaultLocale]);
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
// dict 파일 저장
|
|
1192
|
+
for (const { locale, entries } of pendingSaves) {
|
|
933
1193
|
await this.saveDictFile(locale, entries, locale === defaultLocale);
|
|
934
1194
|
}
|
|
935
1195
|
}
|
|
@@ -955,18 +1215,18 @@ export class SonamuDictionary {
|
|
|
955
1215
|
}
|
|
956
1216
|
}
|
|
957
1217
|
|
|
958
|
-
// 각 locale에 새 키 추가
|
|
1218
|
+
// 각 locale에 새 키 추가 (모든 locale 엔트리를 먼저 만든 뒤 저장)
|
|
1219
|
+
const pendingSaves: { locale: string; entries: DictEntry[] }[] = [];
|
|
959
1220
|
for (const locale of locales) {
|
|
960
1221
|
const cellValue = values[locale]?.trim();
|
|
961
1222
|
if (!cellValue) continue;
|
|
962
1223
|
|
|
963
1224
|
const { entries } = this.loadProjectDict(locale);
|
|
964
|
-
entries.push(
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
isFunction: this.isExpressionFunction(cellValue),
|
|
968
|
-
});
|
|
1225
|
+
entries.push(this.toInputEntry(key, cellValue, []));
|
|
1226
|
+
pendingSaves.push({ locale, entries });
|
|
1227
|
+
}
|
|
969
1228
|
|
|
1229
|
+
for (const { locale, entries } of pendingSaves) {
|
|
970
1230
|
await this.saveDictFile(locale, entries, locale === defaultLocale);
|
|
971
1231
|
}
|
|
972
1232
|
}
|