mega-framework 0.1.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/.env +127 -0
- package/.env.example +186 -0
- package/.prettierrc.json +8 -0
- package/CHANGELOG.md +259 -0
- package/LICENSE +21 -0
- package/README.md +153 -0
- package/bin/mega-ws-hub.js +15 -0
- package/bin/mega.js +38 -0
- package/docker-compose.yml +201 -0
- package/eslint.config.js +57 -0
- package/infra/otel-collector-config.yaml +43 -0
- package/jsconfig.json +18 -0
- package/package.json +121 -0
- package/sample/crud/.env +18 -0
- package/sample/crud/.env.example +50 -0
- package/sample/crud/README.md +85 -0
- package/sample/crud/apps/main/app.config.js +114 -0
- package/sample/crud/apps/main/channels/chat-bus.js +115 -0
- package/sample/crud/apps/main/channels/chat-channel.js +145 -0
- package/sample/crud/apps/main/controllers/auth-controller.js +144 -0
- package/sample/crud/apps/main/controllers/cron-controller.js +34 -0
- package/sample/crud/apps/main/controllers/guide-controller.js +37 -0
- package/sample/crud/apps/main/controllers/jobs-controller.js +43 -0
- package/sample/crud/apps/main/controllers/logs-controller.js +35 -0
- package/sample/crud/apps/main/controllers/metrics-controller.js +22 -0
- package/sample/crud/apps/main/controllers/note-controller.js +116 -0
- package/sample/crud/apps/main/controllers/perf-controller.js +38 -0
- package/sample/crud/apps/main/controllers/redis-controller.js +36 -0
- package/sample/crud/apps/main/controllers/tracing-controller.js +43 -0
- package/sample/crud/apps/main/controllers/upload-controller.js +98 -0
- package/sample/crud/apps/main/controllers/user-controller.js +34 -0
- package/sample/crud/apps/main/controllers/web-controller.js +137 -0
- package/sample/crud/apps/main/controllers/worker-controller.js +57 -0
- package/sample/crud/apps/main/controllers/ws-controller.js +29 -0
- package/sample/crud/apps/main/jobs/email-job.js +72 -0
- package/sample/crud/apps/main/locales/client/en.json +3 -0
- package/sample/crud/apps/main/locales/client/ko.json +3 -0
- package/sample/crud/apps/main/locales/server/en.json +316 -0
- package/sample/crud/apps/main/locales/server/ko.json +316 -0
- package/sample/crud/apps/main/middleware/web-auth.js +40 -0
- package/sample/crud/apps/main/middleware/ws-auth.js +48 -0
- package/sample/crud/apps/main/migrations/20260606000001-create-users.js +27 -0
- package/sample/crud/apps/main/migrations/20260606000002-add-auth-to-users.js +30 -0
- package/sample/crud/apps/main/models/note.js +71 -0
- package/sample/crud/apps/main/models/user.js +86 -0
- package/sample/crud/apps/main/public/css/app.css +101 -0
- package/sample/crud/apps/main/public/css/guide.css +137 -0
- package/sample/crud/apps/main/public/js/app.js +54 -0
- package/sample/crud/apps/main/public/js/perf.js +129 -0
- package/sample/crud/apps/main/public/js/theme-init.js +12 -0
- package/sample/crud/apps/main/public/js/upload-demo.js +63 -0
- package/sample/crud/apps/main/public/js/worker-demo.js +92 -0
- package/sample/crud/apps/main/public/js/ws-chat.js +161 -0
- package/sample/crud/apps/main/public/vendor/bootstrap/bootstrap.bundle.min.js +7 -0
- package/sample/crud/apps/main/public/vendor/bootstrap/bootstrap.min.css +6 -0
- package/sample/crud/apps/main/public/vendor/highlight/github-dark.css +109 -0
- package/sample/crud/apps/main/public/vendor/highlight/github.css +118 -0
- package/sample/crud/apps/main/public/vendor/mega-client-wasm/README.md +19 -0
- package/sample/crud/apps/main/public/vendor/mega-client-wasm/mega_client_wasm.d.ts +196 -0
- package/sample/crud/apps/main/public/vendor/mega-client-wasm/mega_client_wasm.js +1187 -0
- package/sample/crud/apps/main/public/vendor/mega-client-wasm/mega_client_wasm_bg.wasm +0 -0
- package/sample/crud/apps/main/routes/auth.js +15 -0
- package/sample/crud/apps/main/routes/cron.js +14 -0
- package/sample/crud/apps/main/routes/guide.js +25 -0
- package/sample/crud/apps/main/routes/jobs.js +14 -0
- package/sample/crud/apps/main/routes/logs.js +28 -0
- package/sample/crud/apps/main/routes/metrics.js +13 -0
- package/sample/crud/apps/main/routes/notes.js +19 -0
- package/sample/crud/apps/main/routes/perf.js +47 -0
- package/sample/crud/apps/main/routes/redis.js +14 -0
- package/sample/crud/apps/main/routes/tracing.js +14 -0
- package/sample/crud/apps/main/routes/upload.js +16 -0
- package/sample/crud/apps/main/routes/users.js +54 -0
- package/sample/crud/apps/main/routes/web.js +23 -0
- package/sample/crud/apps/main/routes/worker.js +15 -0
- package/sample/crud/apps/main/routes/ws.js +30 -0
- package/sample/crud/apps/main/schedules/cron-counter-schedule.js +30 -0
- package/sample/crud/apps/main/services/auth-service.js +74 -0
- package/sample/crud/apps/main/services/cron-demo-service.js +66 -0
- package/sample/crud/apps/main/services/guide-service.js +145 -0
- package/sample/crud/apps/main/services/jobs-demo-service.js +83 -0
- package/sample/crud/apps/main/services/logs-demo-service.js +59 -0
- package/sample/crud/apps/main/services/metrics-demo-service.js +144 -0
- package/sample/crud/apps/main/services/note-service.js +75 -0
- package/sample/crud/apps/main/services/perf-service.js +302 -0
- package/sample/crud/apps/main/services/redis-demo-service.js +75 -0
- package/sample/crud/apps/main/services/tracing-demo-service.js +69 -0
- package/sample/crud/apps/main/services/upload-demo-service.js +48 -0
- package/sample/crud/apps/main/services/user-service.js +65 -0
- package/sample/crud/apps/main/views/auth/login.ejs +57 -0
- package/sample/crud/apps/main/views/auth/register.ejs +71 -0
- package/sample/crud/apps/main/views/cron/index.ejs +92 -0
- package/sample/crud/apps/main/views/guide/index.ejs +24 -0
- package/sample/crud/apps/main/views/guide/page.ejs +64 -0
- package/sample/crud/apps/main/views/home.ejs +82 -0
- package/sample/crud/apps/main/views/jobs/index.ejs +113 -0
- package/sample/crud/apps/main/views/layouts/main.ejs +112 -0
- package/sample/crud/apps/main/views/logs/index.ejs +80 -0
- package/sample/crud/apps/main/views/metrics/index.ejs +123 -0
- package/sample/crud/apps/main/views/notes/edit.ejs +45 -0
- package/sample/crud/apps/main/views/notes/list.ejs +74 -0
- package/sample/crud/apps/main/views/notes/new.ejs +45 -0
- package/sample/crud/apps/main/views/perf/index.ejs +90 -0
- package/sample/crud/apps/main/views/redis/index.ejs +65 -0
- package/sample/crud/apps/main/views/tracing/index.ejs +106 -0
- package/sample/crud/apps/main/views/upload/index.ejs +79 -0
- package/sample/crud/apps/main/views/users/edit.ejs +48 -0
- package/sample/crud/apps/main/views/users/list.ejs +81 -0
- package/sample/crud/apps/main/views/users/new.ejs +48 -0
- package/sample/crud/apps/main/views/worker/index.ejs +70 -0
- package/sample/crud/apps/main/views/ws/index.ejs +62 -0
- package/sample/crud/apps/main/workers/hash-worker.js +17 -0
- package/sample/crud/apps/main/workers/hash.task.js +22 -0
- package/sample/crud/ecosystem.config.cjs +9 -0
- package/sample/crud/mega.config.js +105 -0
- package/sample/crud/package-lock.json +5665 -0
- package/sample/crud/package.json +28 -0
- package/sample/crud/test/apps/main/auth-flow.integration.test.js +177 -0
- package/sample/crud/test/apps/main/auth-service.test.js +93 -0
- package/sample/crud/test/apps/main/chat-bus.test.js +101 -0
- package/sample/crud/test/apps/main/chat-channel.test.js +144 -0
- package/sample/crud/test/apps/main/cron-demo-service.test.js +93 -0
- package/sample/crud/test/apps/main/demo-flow.integration.test.js +386 -0
- package/sample/crud/test/apps/main/email-job.test.js +76 -0
- package/sample/crud/test/apps/main/guide-service.test.js +68 -0
- package/sample/crud/test/apps/main/hash-task.test.js +30 -0
- package/sample/crud/test/apps/main/jobs-demo-service.test.js +88 -0
- package/sample/crud/test/apps/main/logs-demo-service.test.js +85 -0
- package/sample/crud/test/apps/main/metrics-demo-service.test.js +90 -0
- package/sample/crud/test/apps/main/note-service.test.js +68 -0
- package/sample/crud/test/apps/main/perf-service.test.js +121 -0
- package/sample/crud/test/apps/main/perf.integration.test.js +202 -0
- package/sample/crud/test/apps/main/redis-demo-service.test.js +98 -0
- package/sample/crud/test/apps/main/tracing-demo-service.test.js +90 -0
- package/sample/crud/test/apps/main/upload-demo-service.test.js +61 -0
- package/sample/crud/test/apps/main/user-service.test.js +65 -0
- package/sample/crud/test/apps/main/ws-chat.integration.test.js +232 -0
- package/sample/crud/vitest.config.js +8 -0
- package/sample/crud/yarn.lock +2142 -0
- package/sample/simple/.env.example +15 -0
- package/sample/simple/README.md +52 -0
- package/sample/simple/apps/main/app.config.js +35 -0
- package/sample/simple/apps/main/controllers/pages-controller.js +22 -0
- package/sample/simple/apps/main/locales/client/en.json +3 -0
- package/sample/simple/apps/main/locales/client/ko.json +3 -0
- package/sample/simple/apps/main/locales/server/en.json +23 -0
- package/sample/simple/apps/main/locales/server/ko.json +23 -0
- package/sample/simple/apps/main/public/css/app.css +101 -0
- package/sample/simple/apps/main/public/hello.txt +1 -0
- package/sample/simple/apps/main/public/js/app.js +54 -0
- package/sample/simple/apps/main/public/js/theme-init.js +12 -0
- package/sample/simple/apps/main/public/vendor/bootstrap/bootstrap.bundle.min.js +7 -0
- package/sample/simple/apps/main/public/vendor/bootstrap/bootstrap.min.css +6 -0
- package/sample/simple/apps/main/routes/index.js +9 -0
- package/sample/simple/apps/main/routes/pages.js +12 -0
- package/sample/simple/apps/main/views/index.ejs +56 -0
- package/sample/simple/apps/main/views/layouts/main.ejs +74 -0
- package/sample/simple/ecosystem.config.cjs +10 -0
- package/sample/simple/mega.config.js +27 -0
- package/sample/simple/package-lock.json +1851 -0
- package/sample/simple/package.json +25 -0
- package/sample/simple/test/apps/main/index.test.js +13 -0
- package/sample/simple/vitest.config.js +8 -0
- package/src/adapters/adapter-manager.js +305 -0
- package/src/adapters/adapter-options.js +208 -0
- package/src/adapters/file-adapter.js +350 -0
- package/src/adapters/file-session-adapter.js +363 -0
- package/src/adapters/index.js +38 -0
- package/src/adapters/maria-adapter.js +425 -0
- package/src/adapters/mega-adapter.js +511 -0
- package/src/adapters/mega-bus-adapter.js +81 -0
- package/src/adapters/mega-cache-adapter.js +94 -0
- package/src/adapters/mega-db-adapter.js +72 -0
- package/src/adapters/mega-lock-adapter.js +118 -0
- package/src/adapters/mega-log-sink-adapter.js +46 -0
- package/src/adapters/mega-session-adapter.js +72 -0
- package/src/adapters/mongo-adapter.js +396 -0
- package/src/adapters/nats-adapter.js +370 -0
- package/src/adapters/postgres-adapter.js +341 -0
- package/src/adapters/redis-adapter.js +331 -0
- package/src/adapters/redis-session-adapter.js +261 -0
- package/src/adapters/redlock-adapter.js +385 -0
- package/src/adapters/registry.js +157 -0
- package/src/adapters/sqlite-adapter.js +309 -0
- package/src/auth/index.js +103 -0
- package/src/cli/commands/console-cmd.js +56 -0
- package/src/cli/commands/new.js +101 -0
- package/src/cli/commands/routes.js +107 -0
- package/src/cli/commands/scaffold.js +120 -0
- package/src/cli/commands/test-cmd.js +45 -0
- package/src/cli/generators/index.js +368 -0
- package/src/cli/index.js +472 -0
- package/src/cli/template-engine.js +72 -0
- package/src/cli/ws-hub.js +582 -0
- package/src/core/ajv-mapper.js +80 -0
- package/src/core/boot.js +323 -0
- package/src/core/cluster-metrics.js +278 -0
- package/src/core/config-loader.js +115 -0
- package/src/core/config-validator.js +322 -0
- package/src/core/ctx-builder.js +253 -0
- package/src/core/envelope.js +88 -0
- package/src/core/error-mapper.js +116 -0
- package/src/core/formbody.js +69 -0
- package/src/core/hub-link.js +552 -0
- package/src/core/i18n.js +525 -0
- package/src/core/index.js +63 -0
- package/src/core/mega-app.js +1138 -0
- package/src/core/mega-cluster.js +232 -0
- package/src/core/mega-server.js +176 -0
- package/src/core/mega-service.js +41 -0
- package/src/core/migration-runner.js +196 -0
- package/src/core/multipart.js +282 -0
- package/src/core/openapi.js +114 -0
- package/src/core/router.js +388 -0
- package/src/core/routes-loader.js +57 -0
- package/src/core/scope-registry.js +53 -0
- package/src/core/security.js +275 -0
- package/src/core/services-loader.js +98 -0
- package/src/core/session-cleanup-schedule.js +57 -0
- package/src/core/session-store.js +55 -0
- package/src/core/session.js +414 -0
- package/src/core/static-assets.js +126 -0
- package/src/core/template.js +294 -0
- package/src/core/workers-manager.js +193 -0
- package/src/core/ws-compression.js +112 -0
- package/src/core/ws-controller.js +109 -0
- package/src/core/ws-message.js +176 -0
- package/src/core/ws-upgrade.js +445 -0
- package/src/errors/config-error.js +16 -0
- package/src/errors/http-errors.js +130 -0
- package/src/errors/index.js +19 -0
- package/src/errors/mega-error.js +34 -0
- package/src/eslint-plugin/index.js +15 -0
- package/src/eslint-plugin/no-direct-model-import.js +113 -0
- package/src/index.js +131 -0
- package/src/lib/asp/config.js +83 -0
- package/src/lib/asp/crypto.js +145 -0
- package/src/lib/asp/errors.js +49 -0
- package/src/lib/asp/nonce-cache.js +94 -0
- package/src/lib/asp/plugin.js +263 -0
- package/src/lib/asp/ws-terminator.js +101 -0
- package/src/lib/env-mapper.js +222 -0
- package/src/lib/hub-protocol.js +322 -0
- package/src/lib/index.js +42 -0
- package/src/lib/logger/telegram-core.js +150 -0
- package/src/lib/logger/telegram-transport.js +126 -0
- package/src/lib/mega-brute-force.js +225 -0
- package/src/lib/mega-circuit-breaker.js +412 -0
- package/src/lib/mega-cron.js +169 -0
- package/src/lib/mega-hash.js +179 -0
- package/src/lib/mega-health.js +91 -0
- package/src/lib/mega-job-queue.js +600 -0
- package/src/lib/mega-job-worker.js +295 -0
- package/src/lib/mega-job.js +140 -0
- package/src/lib/mega-logger.js +128 -0
- package/src/lib/mega-metrics.js +661 -0
- package/src/lib/mega-plugin.js +650 -0
- package/src/lib/mega-retry.js +95 -0
- package/src/lib/mega-schedule.js +507 -0
- package/src/lib/mega-shutdown.js +176 -0
- package/src/lib/mega-tracing.js +715 -0
- package/src/lib/mega-worker.js +653 -0
- package/src/lib/worker-runner/process-entry.js +30 -0
- package/src/lib/worker-runner/task-dispatch.js +72 -0
- package/src/lib/worker-runner/thread-entry.js +26 -0
- package/src/models/index.js +7 -0
- package/src/models/mega-model.js +151 -0
- package/src/test/index.js +288 -0
- package/templates/adapter/code.tpl +40 -0
- package/templates/adapter/test.tpl +13 -0
- package/templates/app/app.config.tpl +10 -0
- package/templates/app/route.tpl +10 -0
- package/templates/app/test.tpl +13 -0
- package/templates/channel/code.tpl +38 -0
- package/templates/channel/test.tpl +19 -0
- package/templates/controller/code.tpl +16 -0
- package/templates/controller/route.tpl +9 -0
- package/templates/controller/test.tpl +14 -0
- package/templates/job/code.tpl +23 -0
- package/templates/job/test.tpl +17 -0
- package/templates/locale/code.tpl +3 -0
- package/templates/locale/test.tpl +13 -0
- package/templates/middleware/code.tpl +13 -0
- package/templates/middleware/test.tpl +11 -0
- package/templates/migration/code.tpl +20 -0
- package/templates/migration/test.tpl +14 -0
- package/templates/model/code.tpl +21 -0
- package/templates/model/test.tpl +29 -0
- package/templates/project/app.config.tpl +8 -0
- package/templates/project/app.config.views.tpl +37 -0
- package/templates/project/ecosystem.config.tpl +10 -0
- package/templates/project/env.tpl +12 -0
- package/templates/project/gitignore.tpl +8 -0
- package/templates/project/locales/client/en.json.tpl +3 -0
- package/templates/project/locales/client/ko.json.tpl +3 -0
- package/templates/project/locales/server/en.json.tpl +17 -0
- package/templates/project/locales/server/ko.json.tpl +17 -0
- package/templates/project/mega.config.tpl +11 -0
- package/templates/project/package.tpl +25 -0
- package/templates/project/public/css/app.css +101 -0
- package/templates/project/public/js/app.js +54 -0
- package/templates/project/public/js/theme-init.js +12 -0
- package/templates/project/public/vendor/bootstrap/bootstrap.bundle.min.js +7 -0
- package/templates/project/public/vendor/bootstrap/bootstrap.min.css +6 -0
- package/templates/project/readme.tpl +48 -0
- package/templates/project/route.test.tpl +13 -0
- package/templates/project/route.test.views.tpl +15 -0
- package/templates/project/route.tpl +10 -0
- package/templates/project/route.views.tpl +10 -0
- package/templates/project/views/index.ejs.tpl +58 -0
- package/templates/project/views/layout.ejs.tpl +73 -0
- package/templates/project/vitest.config.tpl +8 -0
- package/templates/route/code.tpl +11 -0
- package/templates/route/test.tpl +26 -0
- package/templates/schedule/code.tpl +19 -0
- package/templates/schedule/test.tpl +17 -0
- package/templates/service/code.tpl +18 -0
- package/templates/service/test.tpl +17 -0
- package/templates/worker/code.tpl +14 -0
- package/templates/worker/task.tpl +13 -0
- package/templates/worker/test.tpl +18 -0
- package/vitest.config.js +33 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* MegaDbAdapter — SQL · Document DB 어댑터 공통 베이스 (추상, 08-class-specs §3.3).
|
|
4
|
+
*
|
|
5
|
+
* 트랜잭션을 표준화한다. 구체: `MegaPostgresAdapter` / `MegaMongoAdapter` /
|
|
6
|
+
* `MegaMariaAdapter` / `MegaSqliteAdapter`.
|
|
7
|
+
*
|
|
8
|
+
* @module adapters/mega-db-adapter
|
|
9
|
+
*/
|
|
10
|
+
import { MegaInternalError } from '../errors/http-errors.js'
|
|
11
|
+
import { MegaAdapter } from './mega-adapter.js'
|
|
12
|
+
|
|
13
|
+
export class MegaDbAdapter extends MegaAdapter {
|
|
14
|
+
/**
|
|
15
|
+
* @param {object} [config]
|
|
16
|
+
*/
|
|
17
|
+
constructor(config) {
|
|
18
|
+
super(config)
|
|
19
|
+
if (new.target === MegaDbAdapter) {
|
|
20
|
+
throw new MegaInternalError(
|
|
21
|
+
'adapter.abstract_instantiation',
|
|
22
|
+
'MegaDbAdapter is abstract — use a concrete DB adapter (MegaPostgresAdapter, MegaSqliteAdapter, ...).',
|
|
23
|
+
{ details: { class: 'MegaDbAdapter' } },
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 명시적 트랜잭션 경계 (ADR-010). 성공 시 commit, throw 시 rollback.
|
|
30
|
+
* driver 별 구현 (postgres `BEGIN/COMMIT/ROLLBACK`, MongoDB `session.withTransaction`).
|
|
31
|
+
* nested 호출은 driver 별 (postgres SAVEPOINT, MongoDB throw `adapter.nested_transaction_unsupported`).
|
|
32
|
+
*
|
|
33
|
+
* @template T
|
|
34
|
+
* @param {(db: any) => Promise<T>} _fn - 트랜잭션 컨텍스트의 `db` 를 받는 콜백.
|
|
35
|
+
* @returns {Promise<T>}
|
|
36
|
+
*/
|
|
37
|
+
async withTransaction(_fn) {
|
|
38
|
+
return this._notImplemented('withTransaction')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 계측된 SQL 쿼리 실행 (ADR-138). `_instrument('query', …)` 로 감싸므로 자동 span(`<driver>.query`,
|
|
43
|
+
* `db.system.name`/`db.query.text`/`mega.rows_affected`)·stats·상태 검증이 한 번에 처리된다.
|
|
44
|
+
* `MegaModel.db.query`(native handle 직접 호출)와 달리 트레이싱/메트릭에 잡힌다.
|
|
45
|
+
*
|
|
46
|
+
* 진행 중 트랜잭션이 있으면 **같은 connection** 위에서 실행해 격리를 유지한다(구체 어댑터가
|
|
47
|
+
* 트랜잭션 컨텍스트를 인식) — 트랜잭션 밖이면 풀/단일 연결. 결과 형태는 driver native 를 그대로
|
|
48
|
+
* 돌려준다(ADR-009 — 어댑터별 차이 허용). SQL 어댑터(postgres/maria/sqlite) 만 구현하며,
|
|
49
|
+
* Document DB(mongo) 는 SQL `query` 개념이 없어 미구현(`adapter.not_implemented`).
|
|
50
|
+
*
|
|
51
|
+
* @param {string} _sql - 파라미터화된 SQL(placeholder `$1`/`?` 보존 — 값 인터폴레이션 금지).
|
|
52
|
+
* @param {any[]} [_params] - placeholder 바인딩 값.
|
|
53
|
+
* @returns {Promise<any>} driver native 쿼리 결과.
|
|
54
|
+
*/
|
|
55
|
+
async query(_sql, _params) {
|
|
56
|
+
return this._notImplemented('query')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* `query` span 시작 속성 — 파라미터화된 SQL 만 `statement` 로 싣는다(트레이서가 `db.query.text` 로
|
|
61
|
+
* 매핑). 문자열이 아니면(쿼리 빌더 객체 등) `mega.query_unparam:true` 로 폴백 — 원본 페이로드
|
|
62
|
+
* 노출·추측 정제를 피한다(ADR-138). placeholder(`$1`/`?`)는 보존되고 바인딩 값은 싣지 않으므로
|
|
63
|
+
* 시크릿 누출·라벨 카디널리티 폭증이 없다.
|
|
64
|
+
*
|
|
65
|
+
* @protected
|
|
66
|
+
* @param {unknown} sql
|
|
67
|
+
* @returns {object}
|
|
68
|
+
*/
|
|
69
|
+
_queryStartAttrs(sql) {
|
|
70
|
+
return typeof sql === 'string' ? { statement: sql } : { 'mega.query_unparam': true }
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* MegaLockAdapter — 분산 락(distributed lock) 표준 인터페이스 (추상, ADR-101/113).
|
|
4
|
+
*
|
|
5
|
+
* **첫 lock 도메인 어댑터 베이스**. DB(트랜잭션)·cache(key-value)·bus(메시징)와 구분되는
|
|
6
|
+
* 4번째 도메인으로, 여러 프로세스·인스턴스가 같은 자원을 동시에 건드리지 못하게 하는 상호배제를
|
|
7
|
+
* 표준화한다. `ctx.lock(alias)` 가 본 베이스 인스턴스를 반환한다 (ADR-102 글로벌 공유 모델 확장).
|
|
8
|
+
*
|
|
9
|
+
* # 표준 표면 (구체가 override)
|
|
10
|
+
* - `acquire(key, opts?)` — 락 획득. 경합 시 retry(opts) 소진 후 실패하면 throw. 성공 시 `Lock` 핸들.
|
|
11
|
+
* - `release(lock)` — 락 해제 (idempotent 권장 — 이미 만료/해제돼도 비치명적).
|
|
12
|
+
* - `extend(lock, ttl)` — 락 TTL 연장. **연장된 새 핸들을 반환**(구체 driver 가 새 핸들을 줄 수
|
|
13
|
+
* 있으므로 — redlock 이 그 예. void 로 두면 새 핸들이 유실됨, ADR-113).
|
|
14
|
+
* - `withLock(key, opts, fn)` — convenience: 획득→fn 실행→해제 보장(throw 시에도). 구체 driver 가
|
|
15
|
+
* 자동 연장(auto-extension)을 지원하면 fn 에 중단 신호를 넘길 수 있다(redlock `using`).
|
|
16
|
+
*
|
|
17
|
+
* # 구체 어댑터
|
|
18
|
+
* `MegaRedlockAdapter` (`redlock`, Redis 단일 노드, ADR-113). 다른 backend(Zookeeper/etcd 등)도
|
|
19
|
+
* 본 베이스를 상속해 동일 표면으로 끼울 수 있다.
|
|
20
|
+
*
|
|
21
|
+
* # TTL 단위 = 밀리초 (cache 와 다름)
|
|
22
|
+
* cache 의 ttl 은 **초**(Redis `EX`)지만, 분산 락 라이브러리(redlock)는 **밀리초**를 쓴다. 락은
|
|
23
|
+
* 임계구역 보호 시간이 보통 초 미만이라 ms 해상도가 필요하다. 혼동 방지로 {@link _assertLockTtl} 이
|
|
24
|
+
* 양의 정수 ms 만 통과시킨다 (cache 의 `_assertTtl` 과 의도적으로 분리 — 단위가 다름).
|
|
25
|
+
*
|
|
26
|
+
* @module adapters/mega-lock-adapter
|
|
27
|
+
*/
|
|
28
|
+
import { MegaInternalError, MegaValidationError } from '../errors/http-errors.js'
|
|
29
|
+
import { MegaAdapter } from './mega-adapter.js'
|
|
30
|
+
|
|
31
|
+
export class MegaLockAdapter extends MegaAdapter {
|
|
32
|
+
/**
|
|
33
|
+
* @param {object} [config]
|
|
34
|
+
*/
|
|
35
|
+
constructor(config) {
|
|
36
|
+
super(config)
|
|
37
|
+
if (new.target === MegaLockAdapter) {
|
|
38
|
+
throw new MegaInternalError(
|
|
39
|
+
'adapter.abstract_instantiation',
|
|
40
|
+
'MegaLockAdapter is abstract — use a concrete lock adapter (MegaRedlockAdapter).',
|
|
41
|
+
{ details: { class: 'MegaLockAdapter' } },
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 락 획득. `key` 에 대한 상호배제 락을 `opts.ttl`(ms) 동안 잡는다. 이미 다른 곳이 잡고 있으면
|
|
48
|
+
* retry 설정(opts)만큼 재시도하고, 그래도 못 잡으면 throw(경합은 정상 도메인 결과 — 호출부가 처리).
|
|
49
|
+
*
|
|
50
|
+
* @param {string} _key - 락 자원 키.
|
|
51
|
+
* @param {{ ttl?: number, retryCount?: number, retryDelay?: number, retryJitter?: number }} [_opts]
|
|
52
|
+
* - `ttl`(ms, 필수에 준함) 보유 시간. `retryCount/retryDelay/retryJitter` 는 경합 시 재시도 정책(driver 별).
|
|
53
|
+
* @returns {Promise<any>} 락 핸들(release/extend 에 전달).
|
|
54
|
+
*/
|
|
55
|
+
async acquire(_key, _opts) {
|
|
56
|
+
return this._notImplemented('acquire')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 락 해제. 이미 만료/해제됐어도 비치명적(자동 만료가 안전망) — 구체 어댑터는 best-effort 로 처리.
|
|
61
|
+
* @param {any} _lock - {@link acquire} 가 돌려준 핸들.
|
|
62
|
+
* @returns {Promise<void>}
|
|
63
|
+
*/
|
|
64
|
+
async release(_lock) {
|
|
65
|
+
return this._notImplemented('release')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 락 TTL 연장. 임계구역이 길어질 때 만료 전에 보유 시간을 늘린다.
|
|
70
|
+
*
|
|
71
|
+
* **반환된 핸들을 이후 release/extend 에 써야 한다** — 구체 driver(redlock)가 연장 시 **새 핸들**을
|
|
72
|
+
* 만들 수 있어, 기존 핸들은 무효가 될 수 있다(ADR-113). 그래서 베이스 계약을 `Promise<void>` 가
|
|
73
|
+
* 아니라 연장된 핸들 반환으로 둔다.
|
|
74
|
+
*
|
|
75
|
+
* @param {any} _lock @param {number} _ttl - 추가 보유 시간(ms).
|
|
76
|
+
* @returns {Promise<any>} 연장된 락 핸들.
|
|
77
|
+
*/
|
|
78
|
+
async extend(_lock, _ttl) {
|
|
79
|
+
return this._notImplemented('extend')
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* convenience — 락을 잡고 `fn` 을 실행한 뒤 **반드시 해제**한다(fn 이 throw 해도 release 보장).
|
|
84
|
+
*
|
|
85
|
+
* 구체 driver 가 자동 연장을 지원하면(redlock `using`) fn 실행 중 만료가 임박할 때 자동으로 TTL 을
|
|
86
|
+
* 늘리고, 연장 실패 시 fn 에 중단 신호(abort signal)를 전달한다 — 이 경우 fn 시그니처는 `(signal)`
|
|
87
|
+
* 이고, 자동 연장이 없는 단순 구현이면 `(lock)` 을 받는다(구체 어댑터 JSDoc 참조).
|
|
88
|
+
*
|
|
89
|
+
* @template T
|
|
90
|
+
* @param {string} _key @param {{ ttl?: number }} _opts @param {(arg: any) => Promise<T>} _fn
|
|
91
|
+
* @returns {Promise<T>}
|
|
92
|
+
*/
|
|
93
|
+
async withLock(_key, _opts, _fn) {
|
|
94
|
+
return this._notImplemented('withLock')
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 락 ttl 검증 헬퍼 (구체 어댑터의 acquire/extend/withLock 에서 사용). **밀리초** 양의 정수만 통과.
|
|
99
|
+
*
|
|
100
|
+
* cache 의 `_assertTtl`(초 단위, undefined=무한 허용)과 달리 락 ttl 은:
|
|
101
|
+
* - **필수** — 무한 락은 데드락 위험이라 허용하지 않는다(undefined 거부).
|
|
102
|
+
* - **밀리초** 양의 정수 — 0/음수/소수/비숫자 거부.
|
|
103
|
+
*
|
|
104
|
+
* @protected
|
|
105
|
+
* @param {number} [ttl] - 보유 시간(ms).
|
|
106
|
+
* @returns {void}
|
|
107
|
+
* @throws {MegaValidationError} `lock.invalid_ttl`
|
|
108
|
+
*/
|
|
109
|
+
_assertLockTtl(ttl) {
|
|
110
|
+
if (typeof ttl !== 'number' || Number.isNaN(ttl) || !Number.isInteger(ttl) || ttl <= 0) {
|
|
111
|
+
throw new MegaValidationError(
|
|
112
|
+
'lock.invalid_ttl',
|
|
113
|
+
`lock ttl must be a positive integer number of milliseconds (got: ${ttl}). Infinite locks are not allowed (deadlock risk).`,
|
|
114
|
+
{ details: { ttl } },
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* MegaLogSinkAdapter — 로그 sink 표준 인터페이스 (추상, 08-class-specs §3.6, ADR-023).
|
|
4
|
+
*
|
|
5
|
+
* pino 의 multi-sink 백엔드. 구체: `MegaConsoleSink` / `MegaFileSink` / `MegaTelegramSink`
|
|
6
|
+
* `write` 는 **동기** (메인 이벤트루프 blocking 금지 — 무거운 IO 는 worker thread).
|
|
7
|
+
* `connect` 는 보통 no-op, `disconnect` ≡ `close`.
|
|
8
|
+
*
|
|
9
|
+
* @module adapters/mega-log-sink-adapter
|
|
10
|
+
*/
|
|
11
|
+
import { MegaInternalError } from '../errors/http-errors.js'
|
|
12
|
+
import { MegaAdapter } from './mega-adapter.js'
|
|
13
|
+
|
|
14
|
+
export class MegaLogSinkAdapter extends MegaAdapter {
|
|
15
|
+
/**
|
|
16
|
+
* @param {object} [config]
|
|
17
|
+
*/
|
|
18
|
+
constructor(config) {
|
|
19
|
+
super(config)
|
|
20
|
+
if (new.target === MegaLogSinkAdapter) {
|
|
21
|
+
throw new MegaInternalError(
|
|
22
|
+
'adapter.abstract_instantiation',
|
|
23
|
+
'MegaLogSinkAdapter is abstract — use a concrete sink (MegaConsoleSink, MegaFileSink, MegaTelegramSink).',
|
|
24
|
+
{ details: { class: 'MegaLogSinkAdapter' } },
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 로그 레코드 기록 (동기). 비동기 IO 가 필요하면 내부 큐잉 + worker thread (구체별).
|
|
31
|
+
* @param {object} _record - pino 표준 record.
|
|
32
|
+
* @returns {void}
|
|
33
|
+
*/
|
|
34
|
+
write(_record) {
|
|
35
|
+
this._notImplemented('write')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 큐 drain + flush 보장. shutdown 마지막 단계 (07-sequence-diagrams §6).
|
|
40
|
+
* 디폴트는 `disconnect()` 위임 — 구체가 큐 drain 으로 override.
|
|
41
|
+
* @returns {Promise<void>}
|
|
42
|
+
*/
|
|
43
|
+
async close() {
|
|
44
|
+
return this.disconnect()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* MegaSessionAdapter — 세션 저장소 표준 인터페이스 (추상, 08-class-specs §3.7, ADR-046).
|
|
4
|
+
*
|
|
5
|
+
* file / Redis 양쪽 같은 표면. 구체: `MegaFileSessionAdapter` / `MegaRedisSessionAdapter`
|
|
6
|
+
* `sessionId` 는 ULID (외부 wire 에는 HMAC sign — 본 어댑터는 평문 sid 로만 동작).
|
|
7
|
+
* file 드라이버 `cleanup()` 은 `mega scheduler` 가 cron 으로 호출, Redis 는 TTL 자동.
|
|
8
|
+
*
|
|
9
|
+
* @module adapters/mega-session-adapter
|
|
10
|
+
*/
|
|
11
|
+
import { MegaInternalError } from '../errors/http-errors.js'
|
|
12
|
+
import { MegaAdapter } from './mega-adapter.js'
|
|
13
|
+
|
|
14
|
+
export class MegaSessionAdapter extends MegaAdapter {
|
|
15
|
+
/**
|
|
16
|
+
* @param {object} [config]
|
|
17
|
+
*/
|
|
18
|
+
constructor(config) {
|
|
19
|
+
super(config)
|
|
20
|
+
if (new.target === MegaSessionAdapter) {
|
|
21
|
+
throw new MegaInternalError(
|
|
22
|
+
'adapter.abstract_instantiation',
|
|
23
|
+
'MegaSessionAdapter is abstract — use a concrete session adapter (MegaFileSessionAdapter, MegaRedisSessionAdapter).',
|
|
24
|
+
{ details: { class: 'MegaSessionAdapter' } },
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param {string} _sessionId
|
|
31
|
+
* @returns {Promise<object | null>} 세션 record — 없으면 `null`.
|
|
32
|
+
*/
|
|
33
|
+
async load(_sessionId) {
|
|
34
|
+
return this._notImplemented('load')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {string} _sessionId
|
|
39
|
+
* @param {object} _record
|
|
40
|
+
* @returns {Promise<void>}
|
|
41
|
+
*/
|
|
42
|
+
async save(_sessionId, _record) {
|
|
43
|
+
return this._notImplemented('save')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {string} _sessionId
|
|
48
|
+
* @returns {Promise<void>}
|
|
49
|
+
*/
|
|
50
|
+
async destroy(_sessionId) {
|
|
51
|
+
return this._notImplemented('destroy')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* rolling TTL 갱신 (매 요청, ADR-046).
|
|
56
|
+
* @param {string} _sessionId
|
|
57
|
+
* @param {number} _ttlMs
|
|
58
|
+
* @returns {Promise<void>}
|
|
59
|
+
*/
|
|
60
|
+
async touch(_sessionId, _ttlMs) {
|
|
61
|
+
return this._notImplemented('touch')
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 만료 세션 정리. file 드라이버는 스캔 후 삭제, Redis 는 TTL 자동(no-op). `MegaSchedule` 로 자동 등록
|
|
66
|
+
* 가능(ADR-129/046).
|
|
67
|
+
* @returns {Promise<number>} 정리된(삭제된) 세션 수.
|
|
68
|
+
*/
|
|
69
|
+
async cleanup() {
|
|
70
|
+
return this._notImplemented('cleanup')
|
|
71
|
+
}
|
|
72
|
+
}
|