@edge-base/server 0.1.1
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/admin-build/.gitkeep +0 -0
- package/admin-build/_app/env.js +1 -0
- package/admin-build/_app/immutable/assets/0.Bm6cF078.css +1 -0
- package/admin-build/_app/immutable/assets/1.BfW3pUNa.css +1 -0
- package/admin-build/_app/immutable/assets/11.CVmQOewb.css +1 -0
- package/admin-build/_app/immutable/assets/12.B1EhbRZT.css +1 -0
- package/admin-build/_app/immutable/assets/13.BvwYeuwE.css +1 -0
- package/admin-build/_app/immutable/assets/14.CdVfcO0R.css +1 -0
- package/admin-build/_app/immutable/assets/15.2yeZ66b-.css +1 -0
- package/admin-build/_app/immutable/assets/17.BVg0JEVu.css +1 -0
- package/admin-build/_app/immutable/assets/18.Rwnl3x_i.css +1 -0
- package/admin-build/_app/immutable/assets/20.DsPWA9AV.css +1 -0
- package/admin-build/_app/immutable/assets/21.Dz2RJ56c.css +1 -0
- package/admin-build/_app/immutable/assets/22.DwNLk5Ai.css +1 -0
- package/admin-build/_app/immutable/assets/23.CFpu0gOO.css +1 -0
- package/admin-build/_app/immutable/assets/24.Cy5LBeoJ.css +1 -0
- package/admin-build/_app/immutable/assets/25.pUyLVf-h.css +1 -0
- package/admin-build/_app/immutable/assets/26.DBcGrlXa.css +1 -0
- package/admin-build/_app/immutable/assets/27.BswYyAJD.css +1 -0
- package/admin-build/_app/immutable/assets/28.B4ueB1Kf.css +1 -0
- package/admin-build/_app/immutable/assets/29.B-qU6PdF.css +1 -0
- package/admin-build/_app/immutable/assets/3.Dg81Pgmd.css +1 -0
- package/admin-build/_app/immutable/assets/30.CsdWum94.css +1 -0
- package/admin-build/_app/immutable/assets/31.U6OwIp50.css +1 -0
- package/admin-build/_app/immutable/assets/4.CyawCCux.css +1 -0
- package/admin-build/_app/immutable/assets/5.C0YO2HTk.css +1 -0
- package/admin-build/_app/immutable/assets/8.Br5jd6kD.css +1 -0
- package/admin-build/_app/immutable/assets/Badge.EMYLHBxE.css +1 -0
- package/admin-build/_app/immutable/assets/Button.DpzMRTjK.css +1 -0
- package/admin-build/_app/immutable/assets/ConfirmDialog.DAnaWRRk.css +1 -0
- package/admin-build/_app/immutable/assets/EmptyState.CwKsu57Y.css +1 -0
- package/admin-build/_app/immutable/assets/Input.BDUSenmU.css +1 -0
- package/admin-build/_app/immutable/assets/Modal.Dm5B0Xie.css +1 -0
- package/admin-build/_app/immutable/assets/PageShell.CmU-Xh-b.css +1 -0
- package/admin-build/_app/immutable/assets/SchemaFieldEditor.g4NsCdno.css +1 -0
- package/admin-build/_app/immutable/assets/Select.BW4Keufm.css +1 -0
- package/admin-build/_app/immutable/assets/Skeleton.KWUulTKJ.css +1 -0
- package/admin-build/_app/immutable/assets/Tabs.CniGYb67.css +1 -0
- package/admin-build/_app/immutable/assets/TimeChart.BTCDAvmT.css +1 -0
- package/admin-build/_app/immutable/assets/Toggle.Cy_K12OM.css +1 -0
- package/admin-build/_app/immutable/assets/TopList.ClFzmPlA.css +1 -0
- package/admin-build/_app/immutable/chunks/7B47DvSx.js +1 -0
- package/admin-build/_app/immutable/chunks/7f08Id8e.js +1 -0
- package/admin-build/_app/immutable/chunks/8wJeQ7LN.js +1 -0
- package/admin-build/_app/immutable/chunks/B-h2afW5.js +1 -0
- package/admin-build/_app/immutable/chunks/B8vJP3wz.js +1 -0
- package/admin-build/_app/immutable/chunks/BR_fL5Yv.js +1 -0
- package/admin-build/_app/immutable/chunks/BY92tFS2.js +1 -0
- package/admin-build/_app/immutable/chunks/BcR-Rdj9.js +1 -0
- package/admin-build/_app/immutable/chunks/BdrwyZv8.js +1 -0
- package/admin-build/_app/immutable/chunks/Bh56EfQ_.js +1 -0
- package/admin-build/_app/immutable/chunks/BkrCkgYp.js +1 -0
- package/admin-build/_app/immutable/chunks/BmRjiP5k.js +1 -0
- package/admin-build/_app/immutable/chunks/BsokvhWC.js +1 -0
- package/admin-build/_app/immutable/chunks/C4D51vTW.js +1 -0
- package/admin-build/_app/immutable/chunks/C6puvcoR.js +2 -0
- package/admin-build/_app/immutable/chunks/CCKNu7m7.js +1 -0
- package/admin-build/_app/immutable/chunks/CWj6FrbW.js +1 -0
- package/admin-build/_app/immutable/chunks/Ce-ngf4p.js +5 -0
- package/admin-build/_app/immutable/chunks/Cs0GwzJA.js +1 -0
- package/admin-build/_app/immutable/chunks/CwROoZK0.js +1 -0
- package/admin-build/_app/immutable/chunks/CxCPv_Ut.js +1 -0
- package/admin-build/_app/immutable/chunks/CxbRue-5.js +1 -0
- package/admin-build/_app/immutable/chunks/CyqB6g-D.js +1 -0
- package/admin-build/_app/immutable/chunks/D5h5A1cc.js +2 -0
- package/admin-build/_app/immutable/chunks/DnyL7Zq-.js +1 -0
- package/admin-build/_app/immutable/chunks/DoPXzH7F.js +1 -0
- package/admin-build/_app/immutable/chunks/DrQSgw-f.js +1 -0
- package/admin-build/_app/immutable/chunks/DttM2zNO.js +1 -0
- package/admin-build/_app/immutable/chunks/DuXuUBWN.js +1 -0
- package/admin-build/_app/immutable/chunks/MdeqaOQx.js +10 -0
- package/admin-build/_app/immutable/chunks/NuUjtcO2.js +1 -0
- package/admin-build/_app/immutable/chunks/Q2nPFxS6.js +1 -0
- package/admin-build/_app/immutable/chunks/R6arueIl.js +1 -0
- package/admin-build/_app/immutable/chunks/UUazaC_N.js +1 -0
- package/admin-build/_app/immutable/chunks/cOYbrQxx.js +1 -0
- package/admin-build/_app/immutable/chunks/eFQHTGwA.js +1 -0
- package/admin-build/_app/immutable/chunks/ehbppgYb.js +1 -0
- package/admin-build/_app/immutable/chunks/glwixJlP.js +1 -0
- package/admin-build/_app/immutable/chunks/vApWTCBs.js +1 -0
- package/admin-build/_app/immutable/chunks/w89G9Xpi.js +1 -0
- package/admin-build/_app/immutable/chunks/wJsUhbfZ.js +1 -0
- package/admin-build/_app/immutable/chunks/zfauFM8P.js +1 -0
- package/admin-build/_app/immutable/entry/app.CcO-Uos3.js +2 -0
- package/admin-build/_app/immutable/entry/start.COebYq3I.js +1 -0
- package/admin-build/_app/immutable/nodes/0.CjtHKU-6.js +1 -0
- package/admin-build/_app/immutable/nodes/1.DEisjlM0.js +1 -0
- package/admin-build/_app/immutable/nodes/10.CvhdyWVB.js +1 -0
- package/admin-build/_app/immutable/nodes/11.DjHqcOvy.js +1 -0
- package/admin-build/_app/immutable/nodes/12.mQLz4Mj_.js +1 -0
- package/admin-build/_app/immutable/nodes/13.CBonZZyP.js +110 -0
- package/admin-build/_app/immutable/nodes/14.d-oiZL0j.js +3 -0
- package/admin-build/_app/immutable/nodes/15.CKPQsUYF.js +1 -0
- package/admin-build/_app/immutable/nodes/16.wPzAPQGx.js +1 -0
- package/admin-build/_app/immutable/nodes/17.DayhKyEZ.js +1 -0
- package/admin-build/_app/immutable/nodes/18.DKwS0Ir0.js +1 -0
- package/admin-build/_app/immutable/nodes/19.wPzAPQGx.js +1 -0
- package/admin-build/_app/immutable/nodes/2.BKoKrw1i.js +1 -0
- package/admin-build/_app/immutable/nodes/20.BvIkkkrW.js +1 -0
- package/admin-build/_app/immutable/nodes/21.DMaFhdHk.js +128 -0
- package/admin-build/_app/immutable/nodes/22.3xdgwuK1.js +1 -0
- package/admin-build/_app/immutable/nodes/23.8Bvgjbsl.js +112 -0
- package/admin-build/_app/immutable/nodes/24.DzSSzRhG.js +2 -0
- package/admin-build/_app/immutable/nodes/25.9KKYBnAE.js +2 -0
- package/admin-build/_app/immutable/nodes/26.Bhn9dfhY.js +1 -0
- package/admin-build/_app/immutable/nodes/27.kRLiC24G.js +1 -0
- package/admin-build/_app/immutable/nodes/28.BVIN1-7N.js +1 -0
- package/admin-build/_app/immutable/nodes/29.3yabZWj4.js +1 -0
- package/admin-build/_app/immutable/nodes/3.BFtSOkX7.js +2 -0
- package/admin-build/_app/immutable/nodes/30.CyCQlwaP.js +1 -0
- package/admin-build/_app/immutable/nodes/31.C4LDXjES.js +1 -0
- package/admin-build/_app/immutable/nodes/4.CvbiMlCa.js +1 -0
- package/admin-build/_app/immutable/nodes/5.C6BLv2eM.js +1 -0
- package/admin-build/_app/immutable/nodes/6.BcXvfl2P.js +1 -0
- package/admin-build/_app/immutable/nodes/7.CIuqhPiK.js +1 -0
- package/admin-build/_app/immutable/nodes/8.BQOR_JfO.js +1 -0
- package/admin-build/_app/immutable/nodes/9.NZqXQxPy.js +1 -0
- package/admin-build/_app/version.json +1 -0
- package/admin-build/favicon.svg +26 -0
- package/admin-build/index.html +45 -0
- package/openapi.json +19543 -0
- package/package.json +66 -0
- package/src/__tests__/admin-assets.test.ts +55 -0
- package/src/__tests__/admin-data-routes.test.ts +488 -0
- package/src/__tests__/admin-db-target.test.ts +103 -0
- package/src/__tests__/admin-routing.test.ts +31 -0
- package/src/__tests__/admin-user-management.test.ts +311 -0
- package/src/__tests__/analytics-query.test.ts +75 -0
- package/src/__tests__/auth-d1.test.ts +749 -0
- package/src/__tests__/auth-db-adapter.test.ts +73 -0
- package/src/__tests__/auth-jwt.test.ts +440 -0
- package/src/__tests__/auth-oauth.test.ts +389 -0
- package/src/__tests__/auth-password.test.ts +367 -0
- package/src/__tests__/auth-redirect.test.ts +87 -0
- package/src/__tests__/backup-restore.test.ts +711 -0
- package/src/__tests__/broadcast.test.ts +128 -0
- package/src/__tests__/cli.test.ts +178 -0
- package/src/__tests__/cloudflare-realtime.test.ts +113 -0
- package/src/__tests__/config.test.ts +469 -0
- package/src/__tests__/cors.test.ts +154 -0
- package/src/__tests__/cron.test.ts +302 -0
- package/src/__tests__/d1-handler.test.ts +402 -0
- package/src/__tests__/d1-sql.test.ts +120 -0
- package/src/__tests__/database-live-config.test.ts +42 -0
- package/src/__tests__/database-live-emitter.test.ts +56 -0
- package/src/__tests__/database-live-filters.test.ts +63 -0
- package/src/__tests__/database-live-route.test.ts +113 -0
- package/src/__tests__/db-sql.test.ts +163 -0
- package/src/__tests__/do-lifecycle.test.ts +263 -0
- package/src/__tests__/do-router.test.ts +729 -0
- package/src/__tests__/email-provider.test.ts +128 -0
- package/src/__tests__/email-templates.test.ts +528 -0
- package/src/__tests__/error-format.test.ts +250 -0
- package/src/__tests__/field-ops.test.ts +242 -0
- package/src/__tests__/functions-context.test.ts +334 -0
- package/src/__tests__/functions-d1-proxy.test.ts +229 -0
- package/src/__tests__/functions-registry-runtime-config.test.ts +17 -0
- package/src/__tests__/functions-route.test.ts +139 -0
- package/src/__tests__/internal-request.test.ts +77 -0
- package/src/__tests__/log-writer.test.ts +44 -0
- package/src/__tests__/logger.test.ts +58 -0
- package/src/__tests__/meta-admin-proxy.test.ts +48 -0
- package/src/__tests__/meta-export-coverage.test.ts +191 -0
- package/src/__tests__/meta-route-registration.test.ts +47 -0
- package/src/__tests__/namespace-dump.test.ts +28 -0
- package/src/__tests__/oauth-providers.test.ts +337 -0
- package/src/__tests__/openapi-coverage.test.ts +144 -0
- package/src/__tests__/pagination.test.ts +59 -0
- package/src/__tests__/password-policy.test.ts +191 -0
- package/src/__tests__/plugin-migrations.test.ts +379 -0
- package/src/__tests__/postgres-batch-compat.test.ts +133 -0
- package/src/__tests__/postgres-dialect.test.ts +328 -0
- package/src/__tests__/postgres-executor.test.ts +79 -0
- package/src/__tests__/postgres-field-ops-compat.test.ts +222 -0
- package/src/__tests__/postgres-schema-init.test.ts +105 -0
- package/src/__tests__/postgres-table-utils.test.ts +107 -0
- package/src/__tests__/presence.test.ts +199 -0
- package/src/__tests__/provider.test.ts +550 -0
- package/src/__tests__/public-user-profile.test.ts +339 -0
- package/src/__tests__/push-handlers.test.ts +179 -0
- package/src/__tests__/push-provider.test.ts +80 -0
- package/src/__tests__/push-token.test.ts +418 -0
- package/src/__tests__/query.test.ts +771 -0
- package/src/__tests__/rate-limit.test.ts +260 -0
- package/src/__tests__/room-access-policy.test.ts +101 -0
- package/src/__tests__/room-handler-context.test.ts +130 -0
- package/src/__tests__/room-monitoring.test.ts +138 -0
- package/src/__tests__/room-runtime-routing.test.ts +222 -0
- package/src/__tests__/room.test.ts +254 -0
- package/src/__tests__/route-parser.test.ts +490 -0
- package/src/__tests__/rules.test.ts +234 -0
- package/src/__tests__/runtime-surface-accounting.test.ts +120 -0
- package/src/__tests__/scheduled.test.ts +80 -0
- package/src/__tests__/schema.test.ts +1273 -0
- package/src/__tests__/security-hardening.test.ts +312 -0
- package/src/__tests__/server.unit.test.ts +333 -0
- package/src/__tests__/service-key-db-proxy.test.ts +650 -0
- package/src/__tests__/service-key-provider-bypass.test.ts +138 -0
- package/src/__tests__/service-key.test.ts +757 -0
- package/src/__tests__/smoke-skip-report.test.ts +72 -0
- package/src/__tests__/sms-provider.test.ts +39 -0
- package/src/__tests__/sql-route.test.ts +218 -0
- package/src/__tests__/storage-hook-context.test.ts +115 -0
- package/src/__tests__/totp.test.ts +200 -0
- package/src/__tests__/uuid.test.ts +144 -0
- package/src/__tests__/validation.test.ts +773 -0
- package/src/__tests__/websocket-pending.test.ts +163 -0
- package/src/_functions-registry.ts +51 -0
- package/src/bench-entry.ts +9 -0
- package/src/cloudflare-test.d.ts +1 -0
- package/src/durable-objects/auth-do.ts +49 -0
- package/src/durable-objects/database-do.ts +2240 -0
- package/src/durable-objects/database-live-do.ts +949 -0
- package/src/durable-objects/logs-do.ts +1200 -0
- package/src/durable-objects/room-runtime-base.ts +1604 -0
- package/src/durable-objects/rooms-do.ts +2191 -0
- package/src/generated-config.ts +6 -0
- package/src/index.ts +382 -0
- package/src/lib/admin-assets.ts +54 -0
- package/src/lib/admin-db-target.ts +301 -0
- package/src/lib/admin-routing.ts +35 -0
- package/src/lib/admin-user-management.ts +464 -0
- package/src/lib/analytics-adapter.ts +103 -0
- package/src/lib/analytics-query.ts +579 -0
- package/src/lib/auth-d1-service.ts +1193 -0
- package/src/lib/auth-d1.ts +1056 -0
- package/src/lib/auth-db-adapter.ts +289 -0
- package/src/lib/auth-redirect.ts +116 -0
- package/src/lib/cidr.ts +115 -0
- package/src/lib/client-ip.ts +51 -0
- package/src/lib/cloudflare-realtime.ts +251 -0
- package/src/lib/control-db.ts +36 -0
- package/src/lib/cron.ts +163 -0
- package/src/lib/d1-handler.ts +1425 -0
- package/src/lib/d1-schema-init.ts +255 -0
- package/src/lib/d1-sql.ts +33 -0
- package/src/lib/database-live-config.ts +24 -0
- package/src/lib/database-live-emitter.ts +111 -0
- package/src/lib/db-sql.ts +66 -0
- package/src/lib/do-retry.ts +36 -0
- package/src/lib/do-router.ts +270 -0
- package/src/lib/do-sql.ts +73 -0
- package/src/lib/email-provider.ts +379 -0
- package/src/lib/email-templates.ts +285 -0
- package/src/lib/email-translations.ts +422 -0
- package/src/lib/errors.ts +151 -0
- package/src/lib/functions.ts +2091 -0
- package/src/lib/hono.ts +56 -0
- package/src/lib/internal-request.ts +56 -0
- package/src/lib/jwt.ts +354 -0
- package/src/lib/log-writer.ts +272 -0
- package/src/lib/namespace-dump.ts +125 -0
- package/src/lib/oauth-providers.ts +1225 -0
- package/src/lib/op-parser.ts +99 -0
- package/src/lib/openapi.ts +146 -0
- package/src/lib/pagination.ts +19 -0
- package/src/lib/password-policy.ts +102 -0
- package/src/lib/password.ts +145 -0
- package/src/lib/plugin-migrations.ts +612 -0
- package/src/lib/postgres-executor.ts +203 -0
- package/src/lib/postgres-handler.ts +1102 -0
- package/src/lib/postgres-schema-init.ts +341 -0
- package/src/lib/postgres-table-utils.ts +87 -0
- package/src/lib/public-user-profile.ts +187 -0
- package/src/lib/push-provider.ts +409 -0
- package/src/lib/push-token.ts +294 -0
- package/src/lib/query-engine.ts +768 -0
- package/src/lib/room-monitoring.ts +97 -0
- package/src/lib/room-runtime.ts +14 -0
- package/src/lib/route-parser.ts +434 -0
- package/src/lib/schema.ts +538 -0
- package/src/lib/schemas.ts +152 -0
- package/src/lib/service-key.ts +419 -0
- package/src/lib/sms-provider.ts +230 -0
- package/src/lib/startup-config.ts +99 -0
- package/src/lib/totp.ts +242 -0
- package/src/lib/uuid.ts +87 -0
- package/src/lib/validation.ts +205 -0
- package/src/lib/version.ts +2 -0
- package/src/lib/websocket-pending.ts +40 -0
- package/src/middleware/auth.ts +169 -0
- package/src/middleware/captcha-verify.ts +217 -0
- package/src/middleware/cors.ts +159 -0
- package/src/middleware/error-handler.ts +54 -0
- package/src/middleware/internal-guard.ts +26 -0
- package/src/middleware/logger.ts +126 -0
- package/src/middleware/rate-limit.ts +283 -0
- package/src/middleware/rules.ts +475 -0
- package/src/routes/admin-auth.ts +447 -0
- package/src/routes/admin.ts +3501 -0
- package/src/routes/analytics-api.ts +290 -0
- package/src/routes/auth.ts +4222 -0
- package/src/routes/backup.ts +1466 -0
- package/src/routes/config.ts +53 -0
- package/src/routes/d1.ts +109 -0
- package/src/routes/database-live.ts +281 -0
- package/src/routes/functions.ts +155 -0
- package/src/routes/health.ts +32 -0
- package/src/routes/kv.ts +167 -0
- package/src/routes/oauth.ts +1055 -0
- package/src/routes/push.ts +1465 -0
- package/src/routes/room.ts +639 -0
- package/src/routes/schema-endpoint.ts +76 -0
- package/src/routes/sql.ts +176 -0
- package/src/routes/storage.ts +1674 -0
- package/src/routes/tables.ts +699 -0
- package/src/routes/users.ts +21 -0
- package/src/routes/vectorize.ts +372 -0
- package/src/types.ts +99 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 서버 단위 테스트 — database-live broadcast 로직
|
|
3
|
+
* 1-19 broadcast.test.ts — 20개
|
|
4
|
+
*
|
|
5
|
+
* 실행: cd packages/server && npx vitest run src/__tests__/broadcast.test.ts
|
|
6
|
+
*
|
|
7
|
+
* NOTE: broadcast HTTP API는 database-live.ts에서 Service Key 필수.
|
|
8
|
+
* 여기서는 broadcast 페이로드 구조 + 서비스 키 가드 로직 검증
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect } from 'vitest';
|
|
12
|
+
|
|
13
|
+
// ── Broadcast 페이로드 검증 로직 (database-live.ts와 동일 패턴) ───────────────────
|
|
14
|
+
|
|
15
|
+
interface BroadcastBody {
|
|
16
|
+
channel?: string;
|
|
17
|
+
event?: string;
|
|
18
|
+
payload?: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function validateBroadcastBody(body: BroadcastBody): { valid: boolean; error?: string } {
|
|
22
|
+
if (!body.channel || typeof body.channel !== 'string') {
|
|
23
|
+
return { valid: false, error: 'channel is required' };
|
|
24
|
+
}
|
|
25
|
+
if (!body.event || typeof body.event !== 'string') {
|
|
26
|
+
return { valid: false, error: 'event is required' };
|
|
27
|
+
}
|
|
28
|
+
return { valid: true };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
//: Broadcast requires Service Key
|
|
32
|
+
function checkServiceKey(key: string | undefined): 'missing' | 'invalid' | 'valid' {
|
|
33
|
+
if (!key) return 'missing';
|
|
34
|
+
if (!key.startsWith('sk_')) return 'invalid'; // simplified check
|
|
35
|
+
return 'valid';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ─── A. 페이로드 검증 ─────────────────────────────────────────────────────────
|
|
39
|
+
|
|
40
|
+
describe('validateBroadcastBody', () => {
|
|
41
|
+
it('valid body → valid', () => {
|
|
42
|
+
const result = validateBroadcastBody({ channel: 'ch-1', event: 'message' });
|
|
43
|
+
expect(result.valid).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('missing channel → invalid', () => {
|
|
47
|
+
const result = validateBroadcastBody({ event: 'message' });
|
|
48
|
+
expect(result.valid).toBe(false);
|
|
49
|
+
expect(result.error).toContain('channel');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('missing event → invalid', () => {
|
|
53
|
+
const result = validateBroadcastBody({ channel: 'ch-1' });
|
|
54
|
+
expect(result.valid).toBe(false);
|
|
55
|
+
expect(result.error).toContain('event');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('empty channel → invalid', () => {
|
|
59
|
+
const result = validateBroadcastBody({ channel: '', event: 'msg' });
|
|
60
|
+
expect(result.valid).toBe(false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('empty event → invalid', () => {
|
|
64
|
+
const result = validateBroadcastBody({ channel: 'ch-1', event: '' });
|
|
65
|
+
expect(result.valid).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('with payload → valid', () => {
|
|
69
|
+
const result = validateBroadcastBody({
|
|
70
|
+
channel: 'ch-1',
|
|
71
|
+
event: 'data',
|
|
72
|
+
payload: { key: 'value' },
|
|
73
|
+
});
|
|
74
|
+
expect(result.valid).toBe(true);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('payload undefined (optional) → valid', () => {
|
|
78
|
+
const result = validateBroadcastBody({ channel: 'ch', event: 'ev' });
|
|
79
|
+
expect(result.valid).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// ─── B. Service Key 가드 ───────────────────────────────
|
|
84
|
+
|
|
85
|
+
describe('checkServiceKey', () => {
|
|
86
|
+
it('missing key → missing', () => {
|
|
87
|
+
expect(checkServiceKey(undefined)).toBe('missing');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('empty string → missing', () => {
|
|
91
|
+
// falsy
|
|
92
|
+
expect(checkServiceKey('')).toBe('missing');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('valid sk_ prefix → valid', () => {
|
|
96
|
+
expect(checkServiceKey('sk_test_valid_key')).toBe('valid');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('invalid format → invalid', () => {
|
|
100
|
+
expect(checkServiceKey('Bearer my-token')).toBe('invalid');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('random string → invalid', () => {
|
|
104
|
+
expect(checkServiceKey('random-key-without-prefix')).toBe('invalid');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// ─── C. Broadcast 이벤트 구조 ─────────────────────────────────────────────────
|
|
109
|
+
|
|
110
|
+
describe('Broadcast event structure', () => {
|
|
111
|
+
it('fire-and-forget: no return value expected', () => {
|
|
112
|
+
// Broadcast는 fire-and-forget → 반환값 없음, ok: true만
|
|
113
|
+
const broadcastResult = { ok: true };
|
|
114
|
+
expect(broadcastResult.ok).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('broadcast payload merged with defaults', () => {
|
|
118
|
+
const payload = undefined;
|
|
119
|
+
const normalizedPayload = payload ?? {};
|
|
120
|
+
expect(normalizedPayload).toEqual({});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('broadcast event name is preserved', () => {
|
|
124
|
+
const event = 'game:scored';
|
|
125
|
+
// event는 그대로 전달
|
|
126
|
+
expect(event).toBe('game:scored');
|
|
127
|
+
});
|
|
128
|
+
});
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 서버 단위 테스트 — CLI keys.ts + service-key.ts
|
|
3
|
+
* 1-24 cli.test.ts — 40개
|
|
4
|
+
*
|
|
5
|
+
* 실행: cd packages/server && npx vitest run src/__tests__/cli.test.ts
|
|
6
|
+
*
|
|
7
|
+
* NOTE:
|
|
8
|
+
* - CLI 명령 실행 없이 순수 함수만 테스트
|
|
9
|
+
* - generateServiceKey / maskKey → keys.ts에서 export
|
|
10
|
+
* - matchesScope / buildConstraintCtx → service-key.ts에서 export
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, it, expect } from 'vitest';
|
|
14
|
+
import { generateServiceKey, maskKey } from '../../../cli/src/commands/keys.js';
|
|
15
|
+
import {
|
|
16
|
+
matchesScope,
|
|
17
|
+
buildConstraintCtx,
|
|
18
|
+
} from '../lib/service-key.js';
|
|
19
|
+
import type { ServiceKeyEntry } from '@edge-base/shared';
|
|
20
|
+
|
|
21
|
+
// ─── A. generateServiceKey ────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
describe('generateServiceKey', () => {
|
|
24
|
+
it('returns 64-character hex string', () => {
|
|
25
|
+
const key = generateServiceKey();
|
|
26
|
+
expect(key).toHaveLength(64);
|
|
27
|
+
expect(key).toMatch(/^[0-9a-f]+$/);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('generates unique keys each call', () => {
|
|
31
|
+
const a = generateServiceKey();
|
|
32
|
+
const b = generateServiceKey();
|
|
33
|
+
expect(a).not.toBe(b);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('is cryptographically unpredictable (entropy check via uniqueness)', () => {
|
|
37
|
+
const set = new Set<string>();
|
|
38
|
+
for (let i = 0; i < 20; i++) set.add(generateServiceKey());
|
|
39
|
+
expect(set.size).toBe(20);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// ─── B. maskKey ──────────────────────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
describe('maskKey', () => {
|
|
46
|
+
it('short key (<=8) → ****', () => {
|
|
47
|
+
expect(maskKey('abc')).toBe('****');
|
|
48
|
+
expect(maskKey('12345678')).toBe('****');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('long key → sk_***...{last4}', () => {
|
|
52
|
+
const key = '0'.repeat(60) + 'abcd';
|
|
53
|
+
const masked = maskKey(key);
|
|
54
|
+
expect(masked).toMatch(/^sk_\*+abcd$/);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('shows last 4 characters', () => {
|
|
58
|
+
const key = generateServiceKey();
|
|
59
|
+
const masked = maskKey(key);
|
|
60
|
+
expect(masked.endsWith(key.slice(-4))).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('always starts with sk_', () => {
|
|
64
|
+
const key = generateServiceKey();
|
|
65
|
+
expect(maskKey(key).startsWith('sk_')).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('does not reveal full key', () => {
|
|
69
|
+
const key = generateServiceKey();
|
|
70
|
+
expect(maskKey(key)).not.toBe(key);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// ─── C. matchesScope ─────────────────────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
describe('matchesScope', () => {
|
|
77
|
+
const rootEntry: ServiceKeyEntry = {
|
|
78
|
+
kid: 'root-1',
|
|
79
|
+
tier: 'root',
|
|
80
|
+
scopes: [],
|
|
81
|
+
secretSource: 'inline',
|
|
82
|
+
inlineSecret: 'sk',
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const scopedEntry: ServiceKeyEntry = {
|
|
86
|
+
kid: 'scoped-1',
|
|
87
|
+
tier: 'scoped',
|
|
88
|
+
scopes: ['storage:bucket:avatars:write'],
|
|
89
|
+
secretSource: 'inline',
|
|
90
|
+
inlineSecret: 'sk',
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const wildcardEntry: ServiceKeyEntry = {
|
|
94
|
+
kid: 'wild-1',
|
|
95
|
+
tier: 'scoped',
|
|
96
|
+
scopes: ['storage:*:*:*'],
|
|
97
|
+
secretSource: 'inline',
|
|
98
|
+
inlineSecret: 'sk',
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const globalWildEntry: ServiceKeyEntry = {
|
|
102
|
+
kid: 'global-1',
|
|
103
|
+
tier: 'scoped',
|
|
104
|
+
scopes: ['*'],
|
|
105
|
+
secretSource: 'inline',
|
|
106
|
+
inlineSecret: 'sk',
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
it('root tier always passes', () => {
|
|
110
|
+
expect(matchesScope('storage:bucket:avatars:write', rootEntry)).toBe(true);
|
|
111
|
+
expect(matchesScope('db:table:posts:read', rootEntry)).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('exact scope match → valid', () => {
|
|
115
|
+
expect(matchesScope('storage:bucket:avatars:write', scopedEntry)).toBe(true);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('scope mismatch → invalid', () => {
|
|
119
|
+
expect(matchesScope('storage:bucket:photos:write', scopedEntry)).toBe(false);
|
|
120
|
+
expect(matchesScope('db:table:posts:read', scopedEntry)).toBe(false);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('wildcard scope matches domain:*:*:*', () => {
|
|
124
|
+
expect(matchesScope('storage:bucket:avatars:write', wildcardEntry)).toBe(true);
|
|
125
|
+
expect(matchesScope('storage:bucket:photos:read', wildcardEntry)).toBe(true);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('wildcard does not cross domains', () => {
|
|
129
|
+
expect(matchesScope('db:table:posts:read', wildcardEntry)).toBe(false);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('global wildcard "*" matches anything', () => {
|
|
133
|
+
expect(matchesScope('anything:goes:here:now', globalWildEntry)).toBe(true);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('different segment count → no match', () => {
|
|
137
|
+
const partial: ServiceKeyEntry = {
|
|
138
|
+
kid: 'partial',
|
|
139
|
+
tier: 'scoped',
|
|
140
|
+
scopes: ['storage:bucket'],
|
|
141
|
+
secretSource: 'inline',
|
|
142
|
+
inlineSecret: 'sk',
|
|
143
|
+
};
|
|
144
|
+
expect(matchesScope('storage:bucket:avatars:write', partial)).toBe(false);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// ─── E. buildConstraintCtx ────────────────────────────────────────────────────
|
|
149
|
+
|
|
150
|
+
describe('buildConstraintCtx', () => {
|
|
151
|
+
it('extracts env from ENVIRONMENT', () => {
|
|
152
|
+
const ctx = buildConstraintCtx({ ENVIRONMENT: 'production' });
|
|
153
|
+
expect(ctx.env).toBe('production');
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('no env → undefined', () => {
|
|
157
|
+
const ctx = buildConstraintCtx({});
|
|
158
|
+
expect(ctx.env).toBeUndefined();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('extracts IP from cf-connecting-ip header', () => {
|
|
162
|
+
const req = { header: (name: string) => name === 'cf-connecting-ip' ? '1.2.3.4' : undefined };
|
|
163
|
+
const ctx = buildConstraintCtx({ EDGEBASE_CONFIG: '{}' }, req);
|
|
164
|
+
expect(ctx.ip).toBe('1.2.3.4');
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('ignores x-forwarded-for unless trustSelfHostedProxy is enabled', () => {
|
|
168
|
+
const req = { header: (name: string) => name === 'x-forwarded-for' ? '5.6.7.8, 9.10.11.12' : undefined };
|
|
169
|
+
const ctx = buildConstraintCtx({ EDGEBASE_CONFIG: '{}' }, req);
|
|
170
|
+
expect(ctx.ip).toBeUndefined();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('falls back to x-forwarded-for when trustSelfHostedProxy is enabled', () => {
|
|
174
|
+
const req = { header: (name: string) => name === 'x-forwarded-for' ? '5.6.7.8, 9.10.11.12' : undefined };
|
|
175
|
+
const ctx = buildConstraintCtx({ EDGEBASE_CONFIG: JSON.stringify({ trustSelfHostedProxy: true }) }, req);
|
|
176
|
+
expect(ctx.ip).toBe('5.6.7.8');
|
|
177
|
+
});
|
|
178
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
CloudflareRealtimeClient,
|
|
4
|
+
assertCloudflareRealtimeConfig,
|
|
5
|
+
createCloudflareRealtimeClient,
|
|
6
|
+
hasCloudflareRealtimeConfig,
|
|
7
|
+
} from '../lib/cloudflare-realtime.js';
|
|
8
|
+
|
|
9
|
+
describe('cloudflare realtime helpers', () => {
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
vi.restoreAllMocks();
|
|
12
|
+
vi.unstubAllGlobals();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('detects whether the required realtime config is present', () => {
|
|
16
|
+
expect(hasCloudflareRealtimeConfig({})).toBe(false);
|
|
17
|
+
expect(hasCloudflareRealtimeConfig({
|
|
18
|
+
CF_REALTIME_APP_ID: ' app-123 ',
|
|
19
|
+
CF_REALTIME_APP_SECRET: ' secret-456 ',
|
|
20
|
+
})).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('normalizes realtime config and defaults the base URL', () => {
|
|
24
|
+
expect(assertCloudflareRealtimeConfig({
|
|
25
|
+
CF_REALTIME_APP_ID: ' app-123 ',
|
|
26
|
+
CF_REALTIME_APP_SECRET: ' secret-456 ',
|
|
27
|
+
CF_REALTIME_TURN_KEY_ID: ' key-1 ',
|
|
28
|
+
CF_REALTIME_TURN_API_TOKEN: ' token-1 ',
|
|
29
|
+
})).toEqual({
|
|
30
|
+
appId: 'app-123',
|
|
31
|
+
appSecret: 'secret-456',
|
|
32
|
+
baseUrl: 'https://rtc.live.cloudflare.com/v1',
|
|
33
|
+
turnKeyId: 'key-1',
|
|
34
|
+
turnApiToken: 'token-1',
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
expect(() => assertCloudflareRealtimeConfig({
|
|
38
|
+
CF_REALTIME_APP_ID: 'missing-secret',
|
|
39
|
+
})).toThrow('Cloudflare Realtime is not configured');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('creates a realtime client and sends authenticated session requests', async () => {
|
|
43
|
+
const fetchMock = vi.fn(async (_input: RequestInfo | URL, _init?: RequestInit) =>
|
|
44
|
+
new Response(JSON.stringify({ sessionId: 'sess-1' }), {
|
|
45
|
+
status: 200,
|
|
46
|
+
headers: { 'Content-Type': 'application/json' },
|
|
47
|
+
}));
|
|
48
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
49
|
+
|
|
50
|
+
const client = createCloudflareRealtimeClient({
|
|
51
|
+
CF_REALTIME_APP_ID: 'app-123',
|
|
52
|
+
CF_REALTIME_APP_SECRET: 'secret-456',
|
|
53
|
+
} as never);
|
|
54
|
+
|
|
55
|
+
expect(client).toBeInstanceOf(CloudflareRealtimeClient);
|
|
56
|
+
await expect(client.createSession({
|
|
57
|
+
sessionDescription: {
|
|
58
|
+
sdp: 'offer-sdp',
|
|
59
|
+
type: 'offer',
|
|
60
|
+
},
|
|
61
|
+
}, {
|
|
62
|
+
thirdparty: true,
|
|
63
|
+
correlationId: 'corr-1',
|
|
64
|
+
})).resolves.toEqual({ sessionId: 'sess-1' });
|
|
65
|
+
|
|
66
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
67
|
+
expect(fetchMock.mock.calls[0]?.[0]).toBe(
|
|
68
|
+
'https://rtc.live.cloudflare.com/v1/apps/app-123/sessions/new?thirdparty=true&correlationId=corr-1',
|
|
69
|
+
);
|
|
70
|
+
expect(fetchMock.mock.calls[0]?.[1]).toMatchObject({
|
|
71
|
+
method: 'POST',
|
|
72
|
+
headers: {
|
|
73
|
+
Authorization: 'Bearer secret-456',
|
|
74
|
+
'Content-Type': 'application/json',
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('uses TURN credentials when generating ICE servers', async () => {
|
|
80
|
+
const fetchMock = vi.fn(async () =>
|
|
81
|
+
new Response(JSON.stringify({
|
|
82
|
+
iceServers: [{ urls: 'turn:global.example.com', username: 'user', credential: 'pass' }],
|
|
83
|
+
}), {
|
|
84
|
+
status: 200,
|
|
85
|
+
headers: { 'Content-Type': 'application/json' },
|
|
86
|
+
}));
|
|
87
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
88
|
+
|
|
89
|
+
const client = new CloudflareRealtimeClient({
|
|
90
|
+
CF_REALTIME_APP_ID: 'app-123',
|
|
91
|
+
CF_REALTIME_APP_SECRET: 'secret-456',
|
|
92
|
+
CF_REALTIME_BASE_URL: 'https://rtc.example.com/base/',
|
|
93
|
+
CF_REALTIME_TURN_KEY_ID: 'turn-key',
|
|
94
|
+
CF_REALTIME_TURN_API_TOKEN: 'turn-token',
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
await expect(client.generateIceServers(120)).resolves.toEqual({
|
|
98
|
+
iceServers: [{ urls: 'turn:global.example.com', username: 'user', credential: 'pass' }],
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
expect(fetchMock).toHaveBeenCalledWith(
|
|
102
|
+
'https://rtc.example.com/base/turn/keys/turn-key/credentials/generate-ice-servers',
|
|
103
|
+
{
|
|
104
|
+
method: 'POST',
|
|
105
|
+
headers: {
|
|
106
|
+
Authorization: 'Bearer turn-token',
|
|
107
|
+
'Content-Type': 'application/json',
|
|
108
|
+
},
|
|
109
|
+
body: JSON.stringify({ ttl: 120 }),
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
});
|