@edge-base/server 0.2.3 → 0.2.5
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/_app/immutable/chunks/{DpVAayDG.js → 6oMK_164.js} +1 -1
- package/admin-build/_app/immutable/chunks/{B5Nwfelm.js → B2TnDKF7.js} +1 -1
- package/admin-build/_app/immutable/chunks/{DCvwWZrm.js → B6MschND.js} +1 -1
- package/admin-build/_app/immutable/chunks/{Du5vWVa2.js → B94PilAN.js} +1 -1
- package/admin-build/_app/immutable/chunks/{Dc1-6Po6.js → BEW7Ez_g.js} +1 -1
- package/admin-build/_app/immutable/chunks/{Dlty5069.js → BoOooyH6.js} +1 -1
- package/admin-build/_app/immutable/chunks/{CzSAxmuj.js → BqTb6Mxk.js} +1 -1
- package/admin-build/_app/immutable/chunks/{DCKcAiQH.js → BvHnF5tV.js} +1 -1
- package/admin-build/_app/immutable/chunks/{B-_-hJ9o.js → CaVKAiCe.js} +1 -1
- package/admin-build/_app/immutable/chunks/{DRqPU3wD.js → Cdm5zBRA.js} +1 -1
- package/admin-build/_app/immutable/chunks/{byv2rTy8.js → CrOZMmdF.js} +1 -1
- package/admin-build/_app/immutable/chunks/{DiyBpamp.js → Cw6OYcq-.js} +1 -1
- package/admin-build/_app/immutable/chunks/{A_3UuvCe.js → D2j3I1VQ.js} +1 -1
- package/admin-build/_app/immutable/chunks/{BxoNtYHK.js → DPdQ7z0T.js} +3 -3
- package/admin-build/_app/immutable/chunks/{nZvorU8i.js → J2Gw0SMu.js} +1 -1
- package/admin-build/_app/immutable/chunks/{CZ0TVkCa.js → pUxw8jfq.js} +1 -1
- package/admin-build/_app/immutable/entry/{app.CfrmEXPD.js → app.D3flihMw.js} +2 -2
- package/admin-build/_app/immutable/entry/start.Cl6sLxnz.js +1 -0
- package/admin-build/_app/immutable/nodes/{0.Cn2BZ4da.js → 0.CdczqZLK.js} +1 -1
- package/admin-build/_app/immutable/nodes/{1.Dv4LX_Co.js → 1.DxcSsEqS.js} +1 -1
- package/admin-build/_app/immutable/nodes/{10.DPVv3kat.js → 10.DuAd4aIm.js} +1 -1
- package/admin-build/_app/immutable/nodes/{11.CiCb6Ayu.js → 11.0jgHQL92.js} +1 -1
- package/admin-build/_app/immutable/nodes/{12.CIPyeekF.js → 12.CKNPqmyy.js} +1 -1
- package/admin-build/_app/immutable/nodes/{13.Z15Lt36e.js → 13.B1p2POXS.js} +1 -1
- package/admin-build/_app/immutable/nodes/{14.s0l5bAq3.js → 14.Bb-REBND.js} +1 -1
- package/admin-build/_app/immutable/nodes/{15.UwSSNO76.js → 15.1uBFCX0X.js} +1 -1
- package/admin-build/_app/immutable/nodes/{16.qiD8i883.js → 16.BR7WwQrS.js} +1 -1
- package/admin-build/_app/immutable/nodes/{17.Dy3dcSvu.js → 17.Cm57KKXV.js} +1 -1
- package/admin-build/_app/immutable/nodes/{18.DeXyPYsO.js → 18.CoiwfAuQ.js} +1 -1
- package/admin-build/_app/immutable/nodes/{19.CAbuyS6w.js → 19.B8ZdLlXj.js} +1 -1
- package/admin-build/_app/immutable/nodes/{20.Bec0T7un.js → 20.DnHeFlTv.js} +1 -1
- package/admin-build/_app/immutable/nodes/21.CJFaf0Ia.js +1 -0
- package/admin-build/_app/immutable/nodes/{22.CdVprrv2.js → 22.CItETFzy.js} +1 -1
- package/admin-build/_app/immutable/nodes/{23.Y8RzVLoF.js → 23.CWSGMcKJ.js} +1 -1
- package/admin-build/_app/immutable/nodes/{24.CWhHYFBx.js → 24.CWbEqNMB.js} +1 -1
- package/admin-build/_app/immutable/nodes/{25.wCBplOVt.js → 25.DRkLEhKi.js} +1 -1
- package/admin-build/_app/immutable/nodes/{26.Cod_JRFK.js → 26.BRxO8AYH.js} +1 -1
- package/admin-build/_app/immutable/nodes/{27.BO2HVMu9.js → 27.BLs-nVHz.js} +1 -1
- package/admin-build/_app/immutable/nodes/{28.DxG-FBVQ.js → 28.G79qkdBK.js} +1 -1
- package/admin-build/_app/immutable/nodes/{29.CjGqWGvE.js → 29.BOcI6g0N.js} +1 -1
- package/admin-build/_app/immutable/nodes/{3.By3_OmdZ.js → 3.B6q-7qr8.js} +1 -1
- package/admin-build/_app/immutable/nodes/{30.M_H7Htpq.js → 30.DAIC7dKd.js} +1 -1
- package/admin-build/_app/immutable/nodes/{31.DEU18izM.js → 31.pl0XXjXF.js} +1 -1
- package/admin-build/_app/immutable/nodes/{4.DeYhKtzJ.js → 4.DOdvVlZj.js} +1 -1
- package/admin-build/_app/immutable/nodes/{5.9WLgxhrD.js → 5.BW_zlgye.js} +1 -1
- package/admin-build/_app/immutable/nodes/{6.BdT2i_dd.js → 6.Dxy1CAI2.js} +1 -1
- package/admin-build/_app/immutable/nodes/{7.CHq0s4K6.js → 7.BG98w_o7.js} +1 -1
- package/admin-build/_app/immutable/nodes/{8.DuvRw-XZ.js → 8.DoG5R2rG.js} +1 -1
- package/admin-build/_app/immutable/nodes/{9.C2Ub82wn.js → 9.Dmxf6zAC.js} +1 -1
- package/admin-build/_app/version.json +1 -1
- package/admin-build/index.html +7 -7
- package/package.json +3 -3
- package/src/__tests__/admin-data-routes.test.ts +29 -0
- package/src/__tests__/database-do-route-validation.test.ts +108 -0
- package/src/__tests__/database-live-route.test.ts +82 -0
- package/src/__tests__/do-router.test.ts +116 -0
- package/src/__tests__/functions-context.test.ts +84 -0
- package/src/__tests__/functions-d1-proxy.test.ts +54 -0
- package/src/__tests__/meta-route-registration.test.ts +20 -15
- package/src/__tests__/plugin-migration-routing.test.ts +32 -0
- package/src/__tests__/provider-aware-sql.test.ts +9 -3
- package/src/__tests__/room-auth-state-loss.test.ts +122 -0
- package/src/__tests__/room-handler-context.test.ts +4 -4
- package/src/__tests__/room-rate-limit-scopes.test.ts +38 -0
- package/src/__tests__/runtime-startup.test.ts +49 -0
- package/src/__tests__/scheduled.test.ts +55 -0
- package/src/__tests__/service-key-db-proxy.test.ts +122 -1
- package/src/__tests__/sql-route.test.ts +66 -0
- package/src/__tests__/table-hook-runtime.test.ts +137 -0
- package/src/durable-objects/database-do.ts +50 -45
- package/src/durable-objects/database-live-do.ts +15 -0
- package/src/durable-objects/room-runtime-base.ts +387 -129
- package/src/durable-objects/rooms-do.ts +31 -24
- package/src/index.ts +334 -282
- package/src/lib/d1-handler.ts +10 -21
- package/src/lib/do-router.ts +135 -3
- package/src/lib/functions.ts +4 -3
- package/src/lib/internal-transport.ts +28 -12
- package/src/lib/plugin-migration-routing.ts +28 -0
- package/src/lib/postgres-handler.ts +12 -20
- package/src/lib/provider-aware-sql.ts +19 -15
- package/src/lib/runtime-startup.ts +53 -0
- package/src/lib/table-hook-runtime.ts +62 -0
- package/src/routes/admin.ts +41 -41
- package/src/routes/database-live.ts +110 -12
- package/src/routes/sql.ts +22 -17
- package/src/routes/tables.ts +42 -29
- package/admin-build/_app/immutable/entry/start.l1WvHznQ.js +0 -1
- package/admin-build/_app/immutable/nodes/21.DuDYelMY.js +0 -1
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { EdgeBaseConfig, HookCtx } from '@edge-base/shared';
|
|
2
|
+
import { ensureAuthSchema } from './auth-d1.js';
|
|
3
|
+
import { resolveAuthDb, type AuthDb } from './auth-db-adapter.js';
|
|
4
|
+
import { sendToDatabaseLiveDO } from './database-live-emitter.js';
|
|
5
|
+
import { createPushProvider } from './push-provider.js';
|
|
6
|
+
import { getDevicesForUser } from './push-token.js';
|
|
7
|
+
import type { Env } from '../types.js';
|
|
8
|
+
|
|
9
|
+
type PushTokenStore = KVNamespace | { kv: KVNamespace; authDb?: AuthDb | null };
|
|
10
|
+
|
|
11
|
+
async function resolvePushTokenStore(env: Env): Promise<PushTokenStore | null> {
|
|
12
|
+
if (!env.KV) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const authDb = resolveAuthDb(env as unknown as Record<string, unknown>);
|
|
18
|
+
await ensureAuthSchema(authDb);
|
|
19
|
+
return { kv: env.KV, authDb };
|
|
20
|
+
} catch {
|
|
21
|
+
return env.KV;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function buildTableHookRuntimeServices(
|
|
26
|
+
config: EdgeBaseConfig,
|
|
27
|
+
env: Env,
|
|
28
|
+
): Pick<HookCtx, 'databaseLive' | 'push'> {
|
|
29
|
+
return {
|
|
30
|
+
databaseLive: {
|
|
31
|
+
async broadcast(channel: string, event: string, data: unknown): Promise<void> {
|
|
32
|
+
await sendToDatabaseLiveDO(
|
|
33
|
+
env,
|
|
34
|
+
{ channel, event, payload: data ?? {} },
|
|
35
|
+
'/internal/broadcast',
|
|
36
|
+
);
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
push: {
|
|
40
|
+
async send(userId: string, payload: { title?: string; body: string }): Promise<void> {
|
|
41
|
+
try {
|
|
42
|
+
const tokenStore = await resolvePushTokenStore(env);
|
|
43
|
+
if (!tokenStore) return;
|
|
44
|
+
|
|
45
|
+
const provider = createPushProvider(config.push, env);
|
|
46
|
+
if (!provider) return;
|
|
47
|
+
|
|
48
|
+
const devices = await getDevicesForUser(tokenStore, userId);
|
|
49
|
+
if (devices.length === 0) return;
|
|
50
|
+
|
|
51
|
+
await Promise.allSettled(
|
|
52
|
+
devices.map((device) =>
|
|
53
|
+
provider.send({ token: device.token, platform: device.platform, payload }),
|
|
54
|
+
),
|
|
55
|
+
);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.warn('[EdgeBase] table hook push.send failed:', error);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
package/src/routes/admin.ts
CHANGED
|
@@ -23,7 +23,16 @@ import {
|
|
|
23
23
|
import { hashPassword, verifyPassword } from '../lib/password.js';
|
|
24
24
|
import { generateId } from '../lib/uuid.js';
|
|
25
25
|
import { validateKey, buildConstraintCtx, extractBearerToken, resolveServiceKeyCandidate } from '../lib/service-key.js';
|
|
26
|
-
import {
|
|
26
|
+
import {
|
|
27
|
+
formatDbTargetValidationIssue,
|
|
28
|
+
getD1BindingName,
|
|
29
|
+
getDbDoName,
|
|
30
|
+
isDynamicDbBlock,
|
|
31
|
+
normalizeDbInstanceId,
|
|
32
|
+
parseConfig,
|
|
33
|
+
resolveDbTarget,
|
|
34
|
+
shouldRouteToD1,
|
|
35
|
+
} from '../lib/do-router.js';
|
|
27
36
|
import { handleD1Request, d1BatchImport } from '../lib/d1-handler.js';
|
|
28
37
|
import { fetchDOWithRetry } from '../lib/do-retry.js';
|
|
29
38
|
import { dumpNamespaceTables } from '../lib/namespace-dump.js';
|
|
@@ -734,19 +743,6 @@ function getTableDO(env: Env, tableName: string, config: ReturnType<typeof parse
|
|
|
734
743
|
return { stub: env.DATABASE.get(env.DATABASE.idFromName(doName)), doName };
|
|
735
744
|
}
|
|
736
745
|
|
|
737
|
-
function isDynamicDbBlock(
|
|
738
|
-
dbBlock: {
|
|
739
|
-
instance?: boolean;
|
|
740
|
-
access?: {
|
|
741
|
-
canCreate?: unknown;
|
|
742
|
-
access?: unknown;
|
|
743
|
-
};
|
|
744
|
-
} | undefined,
|
|
745
|
-
): boolean {
|
|
746
|
-
if (!dbBlock) return false;
|
|
747
|
-
return !!(dbBlock.instance || dbBlock.access?.canCreate || dbBlock.access?.access);
|
|
748
|
-
}
|
|
749
|
-
|
|
750
746
|
function getEffectiveDbProvider(namespace: string, config: ReturnType<typeof parseConfig>): 'do' | 'd1' | 'postgres' | 'neon' {
|
|
751
747
|
const dbBlock = config.databases?.[namespace];
|
|
752
748
|
if (!dbBlock) return 'do';
|
|
@@ -763,10 +759,7 @@ function getEffectiveDbProvider(namespace: string, config: ReturnType<typeof par
|
|
|
763
759
|
}
|
|
764
760
|
|
|
765
761
|
function getRequestedInstanceId(c: { req: { query: (name: string) => string | undefined } }): string | undefined {
|
|
766
|
-
|
|
767
|
-
if (!raw) return undefined;
|
|
768
|
-
const trimmed = raw.trim();
|
|
769
|
-
return trimmed.length > 0 ? trimmed : undefined;
|
|
762
|
+
return normalizeDbInstanceId(c.req.query('instanceId'));
|
|
770
763
|
}
|
|
771
764
|
|
|
772
765
|
function validateAdminTableInstanceId(
|
|
@@ -774,28 +767,27 @@ function validateAdminTableInstanceId(
|
|
|
774
767
|
config: ReturnType<typeof parseConfig>,
|
|
775
768
|
instanceId: string | undefined,
|
|
776
769
|
): Response | null {
|
|
777
|
-
const
|
|
778
|
-
if (
|
|
779
|
-
if (dynamic) {
|
|
780
|
-
return new Response(
|
|
781
|
-
JSON.stringify({
|
|
782
|
-
code: 400,
|
|
783
|
-
message: `instanceId is required for dynamic namespace '${namespace}'`,
|
|
784
|
-
}),
|
|
785
|
-
{
|
|
786
|
-
status: 400,
|
|
787
|
-
headers: { 'Content-Type': 'application/json' },
|
|
788
|
-
},
|
|
789
|
-
);
|
|
790
|
-
}
|
|
770
|
+
const target = resolveDbTarget(config, namespace, instanceId);
|
|
771
|
+
if (target.ok) {
|
|
791
772
|
return null;
|
|
792
773
|
}
|
|
793
|
-
|
|
794
|
-
|
|
774
|
+
if (target.status !== 400) {
|
|
775
|
+
return new Response(
|
|
776
|
+
JSON.stringify({
|
|
777
|
+
code: target.status,
|
|
778
|
+
message: formatDbTargetValidationIssue(target.issue, namespace),
|
|
779
|
+
}),
|
|
780
|
+
{
|
|
781
|
+
status: target.status,
|
|
782
|
+
headers: { 'Content-Type': 'application/json' },
|
|
783
|
+
},
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
if (target.issue === 'instance_id_invalid') {
|
|
795
787
|
return new Response(
|
|
796
788
|
JSON.stringify({
|
|
797
789
|
code: 400,
|
|
798
|
-
message:
|
|
790
|
+
message: formatDbTargetValidationIssue(target.issue, namespace),
|
|
799
791
|
}),
|
|
800
792
|
{
|
|
801
793
|
status: 400,
|
|
@@ -803,8 +795,16 @@ function validateAdminTableInstanceId(
|
|
|
803
795
|
},
|
|
804
796
|
);
|
|
805
797
|
}
|
|
806
|
-
|
|
807
|
-
|
|
798
|
+
return new Response(
|
|
799
|
+
JSON.stringify({
|
|
800
|
+
code: 400,
|
|
801
|
+
message: formatDbTargetValidationIssue(target.issue, namespace),
|
|
802
|
+
}),
|
|
803
|
+
{
|
|
804
|
+
status: 400,
|
|
805
|
+
headers: { 'Content-Type': 'application/json' },
|
|
806
|
+
},
|
|
807
|
+
);
|
|
808
808
|
}
|
|
809
809
|
|
|
810
810
|
async function restoreAdminNamespaceTables(
|
|
@@ -974,7 +974,7 @@ api.openapi(adminGetTableRecords, async (c) => {
|
|
|
974
974
|
const name = c.req.param('name')!;
|
|
975
975
|
const config = parseConfig(c.env);
|
|
976
976
|
const namespace = findNamespaceForTable(name, config);
|
|
977
|
-
const instanceId =
|
|
977
|
+
const instanceId = getRequestedInstanceId(c);
|
|
978
978
|
const instanceError = validateAdminTableInstanceId(namespace, config, instanceId);
|
|
979
979
|
if (instanceError) return instanceError;
|
|
980
980
|
|
|
@@ -1027,7 +1027,7 @@ api.openapi(adminCreateTableRecord, async (c) => {
|
|
|
1027
1027
|
const name = c.req.param('name')!;
|
|
1028
1028
|
const config = parseConfig(c.env);
|
|
1029
1029
|
const namespace = findNamespaceForTable(name, config);
|
|
1030
|
-
const instanceId =
|
|
1030
|
+
const instanceId = getRequestedInstanceId(c);
|
|
1031
1031
|
const instanceError = validateAdminTableInstanceId(namespace, config, instanceId);
|
|
1032
1032
|
if (instanceError) return instanceError;
|
|
1033
1033
|
|
|
@@ -1081,7 +1081,7 @@ api.openapi(adminUpdateTableRecord, async (c) => {
|
|
|
1081
1081
|
const id = c.req.param('id')!;
|
|
1082
1082
|
const config = parseConfig(c.env);
|
|
1083
1083
|
const namespace = findNamespaceForTable(name, config);
|
|
1084
|
-
const instanceId =
|
|
1084
|
+
const instanceId = getRequestedInstanceId(c);
|
|
1085
1085
|
const instanceError = validateAdminTableInstanceId(namespace, config, instanceId);
|
|
1086
1086
|
if (instanceError) return instanceError;
|
|
1087
1087
|
|
|
@@ -1129,7 +1129,7 @@ api.openapi(adminDeleteTableRecord, async (c) => {
|
|
|
1129
1129
|
const id = c.req.param('id')!;
|
|
1130
1130
|
const config = parseConfig(c.env);
|
|
1131
1131
|
const namespace = findNamespaceForTable(name, config);
|
|
1132
|
-
const instanceId =
|
|
1132
|
+
const instanceId = getRequestedInstanceId(c);
|
|
1133
1133
|
const instanceError = validateAdminTableInstanceId(namespace, config, instanceId);
|
|
1134
1134
|
if (instanceError) return instanceError;
|
|
1135
1135
|
|
|
@@ -9,7 +9,12 @@ import {
|
|
|
9
9
|
DATABASE_LIVE_HUB_DO_NAME,
|
|
10
10
|
isDbLiveChannel,
|
|
11
11
|
} from '../lib/database-live-emitter.js';
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
formatDbTargetValidationIssue,
|
|
14
|
+
isDynamicDbBlock,
|
|
15
|
+
parseConfig,
|
|
16
|
+
resolveDbTarget,
|
|
17
|
+
} from '../lib/do-router.js';
|
|
13
18
|
import { validateKey, buildConstraintCtx } from '../lib/service-key.js';
|
|
14
19
|
import { getTrustedClientIp } from '../lib/client-ip.js';
|
|
15
20
|
import {
|
|
@@ -50,18 +55,107 @@ const dbConnectDiagnosticSchema = z.object({
|
|
|
50
55
|
maxPending: z.number().optional(),
|
|
51
56
|
});
|
|
52
57
|
|
|
53
|
-
function
|
|
58
|
+
function resolveStructuredDatabaseLiveChannel(
|
|
59
|
+
config: ReturnType<typeof parseConfig>,
|
|
60
|
+
query: {
|
|
61
|
+
namespace?: string;
|
|
62
|
+
instanceId?: string;
|
|
63
|
+
table?: string;
|
|
64
|
+
docId?: string;
|
|
65
|
+
},
|
|
66
|
+
): { ok: true; channel: string } | { ok: false; message: string } {
|
|
67
|
+
if (!query.namespace || !query.table) {
|
|
68
|
+
return { ok: false, message: 'Database subscription target required' };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const target = resolveDbTarget(config, query.namespace, query.instanceId);
|
|
72
|
+
if (!target.ok) {
|
|
73
|
+
return {
|
|
74
|
+
ok: false,
|
|
75
|
+
message: formatDbTargetValidationIssue(target.issue, query.namespace),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (!target.value.dbBlock.tables?.[query.table]) {
|
|
79
|
+
return {
|
|
80
|
+
ok: false,
|
|
81
|
+
message: `Table '${query.table}' not found in database '${query.namespace}'`,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const channel = buildDbLiveChannel(
|
|
86
|
+
query.namespace,
|
|
87
|
+
query.table,
|
|
88
|
+
target.value.instanceId,
|
|
89
|
+
query.docId,
|
|
90
|
+
);
|
|
91
|
+
if (!isDbLiveChannel(channel)) {
|
|
92
|
+
return { ok: false, message: 'Invalid database subscription target' };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return { ok: true, channel };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function resolveDatabaseLiveChannel(
|
|
99
|
+
config: ReturnType<typeof parseConfig>,
|
|
100
|
+
query: {
|
|
54
101
|
channel?: string;
|
|
55
102
|
namespace?: string;
|
|
56
103
|
instanceId?: string;
|
|
57
104
|
table?: string;
|
|
58
105
|
docId?: string;
|
|
59
|
-
}): string |
|
|
106
|
+
}): { ok: true; channel: string } | { ok: false; message: string } {
|
|
60
107
|
if (query.channel) {
|
|
61
|
-
|
|
108
|
+
if (!isDbLiveChannel(query.channel)) {
|
|
109
|
+
return {
|
|
110
|
+
ok: false,
|
|
111
|
+
message: `Database live only supports DB channels: ${query.channel}`,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const parts = query.channel.split(':');
|
|
116
|
+
const namespace = parts[1];
|
|
117
|
+
if (!namespace) {
|
|
118
|
+
return { ok: false, message: 'Database subscription target required' };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const dbBlock = config.databases?.[namespace];
|
|
122
|
+
if (!dbBlock) {
|
|
123
|
+
return {
|
|
124
|
+
ok: false,
|
|
125
|
+
message: formatDbTargetValidationIssue('namespace_not_found', namespace),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const dynamic = isDynamicDbBlock(dbBlock);
|
|
130
|
+
if (dynamic && parts.length < 4) {
|
|
131
|
+
return {
|
|
132
|
+
ok: false,
|
|
133
|
+
message: formatDbTargetValidationIssue('instance_id_required', namespace),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
if (!dynamic && parts.length > 4) {
|
|
137
|
+
return {
|
|
138
|
+
ok: false,
|
|
139
|
+
message: formatDbTargetValidationIssue('instance_id_not_allowed', namespace),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const structured = dynamic
|
|
144
|
+
? {
|
|
145
|
+
namespace,
|
|
146
|
+
instanceId: parts[2],
|
|
147
|
+
table: parts[3],
|
|
148
|
+
docId: parts[4],
|
|
149
|
+
}
|
|
150
|
+
: {
|
|
151
|
+
namespace,
|
|
152
|
+
table: parts[2],
|
|
153
|
+
docId: parts[3],
|
|
154
|
+
};
|
|
155
|
+
return resolveStructuredDatabaseLiveChannel(config, structured);
|
|
62
156
|
}
|
|
63
|
-
|
|
64
|
-
return
|
|
157
|
+
|
|
158
|
+
return resolveStructuredDatabaseLiveChannel(config, query);
|
|
65
159
|
}
|
|
66
160
|
|
|
67
161
|
function getPendingKey(ip: string): string {
|
|
@@ -85,7 +179,8 @@ const checkDatabaseConnection = createRoute({
|
|
|
85
179
|
});
|
|
86
180
|
|
|
87
181
|
databaseLiveRoute.openapi(checkDatabaseConnection, async (c) => {
|
|
88
|
-
const
|
|
182
|
+
const config = parseConfig(c.env);
|
|
183
|
+
const channelResult = resolveDatabaseLiveChannel(config, {
|
|
89
184
|
channel: c.req.query('channel') ?? undefined,
|
|
90
185
|
namespace: c.req.query('namespace') ?? undefined,
|
|
91
186
|
instanceId: c.req.query('instanceId') ?? undefined,
|
|
@@ -93,14 +188,15 @@ databaseLiveRoute.openapi(checkDatabaseConnection, async (c) => {
|
|
|
93
188
|
docId: c.req.query('docId') ?? undefined,
|
|
94
189
|
});
|
|
95
190
|
|
|
96
|
-
if (!
|
|
191
|
+
if (!channelResult.ok) {
|
|
97
192
|
return c.json({
|
|
98
193
|
ok: false,
|
|
99
194
|
type: 'db_connect_invalid_request',
|
|
100
195
|
category: 'request',
|
|
101
|
-
message:
|
|
196
|
+
message: channelResult.message,
|
|
102
197
|
}, 400);
|
|
103
198
|
}
|
|
199
|
+
const channel = channelResult.channel;
|
|
104
200
|
|
|
105
201
|
const ip = getTrustedClientIp(c.env, c.req) ?? 'unknown';
|
|
106
202
|
const kvKey = getPendingKey(ip);
|
|
@@ -224,16 +320,18 @@ databaseLiveRoute.openapi(connectDatabaseSubscription, async (c) => {
|
|
|
224
320
|
return c.json({ code: 400, message: 'Expected WebSocket upgrade' }, 400);
|
|
225
321
|
}
|
|
226
322
|
|
|
227
|
-
const
|
|
323
|
+
const config = parseConfig(c.env);
|
|
324
|
+
const channelResult = resolveDatabaseLiveChannel(config, {
|
|
228
325
|
channel: c.req.query('channel') ?? undefined,
|
|
229
326
|
namespace: c.req.query('namespace') ?? undefined,
|
|
230
327
|
instanceId: c.req.query('instanceId') ?? undefined,
|
|
231
328
|
table: c.req.query('table') ?? undefined,
|
|
232
329
|
docId: c.req.query('docId') ?? undefined,
|
|
233
330
|
});
|
|
234
|
-
if (!
|
|
235
|
-
return c.json({ code: 400, message:
|
|
331
|
+
if (!channelResult.ok) {
|
|
332
|
+
return c.json({ code: 400, message: channelResult.message }, 400);
|
|
236
333
|
}
|
|
334
|
+
const channel = channelResult.channel;
|
|
237
335
|
|
|
238
336
|
const ip = getTrustedClientIp(c.env, c.req) ?? 'unknown';
|
|
239
337
|
const kvKey = getPendingKey(ip);
|
package/src/routes/sql.ts
CHANGED
|
@@ -23,7 +23,11 @@
|
|
|
23
23
|
* { namespace: 'workspace', id: 'ws-456', sql: 'SELECT * FROM documents', params: [] }
|
|
24
24
|
*/
|
|
25
25
|
import { OpenAPIHono, createRoute, type HonoEnv } from '../lib/hono.js';
|
|
26
|
-
import {
|
|
26
|
+
import {
|
|
27
|
+
formatDbTargetValidationIssue,
|
|
28
|
+
parseConfig,
|
|
29
|
+
resolveDbTarget,
|
|
30
|
+
} from '../lib/do-router.js';
|
|
27
31
|
import { validateKey, buildConstraintCtx } from '../lib/service-key.js';
|
|
28
32
|
import {
|
|
29
33
|
zodDefaultHook,
|
|
@@ -65,6 +69,10 @@ const executeSql = createRoute({
|
|
|
65
69
|
description: 'Forbidden',
|
|
66
70
|
content: { 'application/json': { schema: errorResponseSchema } },
|
|
67
71
|
},
|
|
72
|
+
404: {
|
|
73
|
+
description: 'Namespace not found',
|
|
74
|
+
content: { 'application/json': { schema: errorResponseSchema } },
|
|
75
|
+
},
|
|
68
76
|
},
|
|
69
77
|
});
|
|
70
78
|
|
|
@@ -84,30 +92,27 @@ sqlRoute.openapi(executeSql, async (c) => {
|
|
|
84
92
|
if (id !== undefined && id !== null && typeof id !== 'string') {
|
|
85
93
|
return c.json({ code: 400, message: 'id must be a string' }, 400);
|
|
86
94
|
}
|
|
87
|
-
if (id && id.includes(':')) {
|
|
88
|
-
return c.json({ code: 400, message: "id must not contain ':' (§2)" }, 400);
|
|
89
|
-
}
|
|
90
95
|
if (!sql || typeof sql !== 'string') {
|
|
91
96
|
return c.json({ code: 400, message: 'sql is required' }, 400);
|
|
92
97
|
}
|
|
93
98
|
|
|
94
99
|
// Validate namespace is declared in databases config (§1)
|
|
95
100
|
const config = parseConfig(c.env);
|
|
96
|
-
const
|
|
97
|
-
if (!
|
|
98
|
-
return c.json({ code: 404, message: `Namespace '${namespace}' not found in config` }, 404);
|
|
99
|
-
}
|
|
100
|
-
const isDynamicNamespace = !!(
|
|
101
|
-
dbBlock.instance ||
|
|
102
|
-
dbBlock.access?.canCreate ||
|
|
103
|
-
dbBlock.access?.access
|
|
104
|
-
);
|
|
105
|
-
if (isDynamicNamespace && !id) {
|
|
101
|
+
const target = resolveDbTarget(config, namespace, id);
|
|
102
|
+
if (!target.ok) {
|
|
106
103
|
return c.json(
|
|
107
|
-
{
|
|
108
|
-
|
|
104
|
+
{
|
|
105
|
+
code: target.status,
|
|
106
|
+
message: formatDbTargetValidationIssue(target.issue, namespace, {
|
|
107
|
+
namespaceLabel: 'Namespace',
|
|
108
|
+
instanceIdLabel: 'id',
|
|
109
|
+
includeSectionRef: target.issue === 'instance_id_invalid',
|
|
110
|
+
}),
|
|
111
|
+
},
|
|
112
|
+
target.status,
|
|
109
113
|
);
|
|
110
114
|
}
|
|
115
|
+
const { instanceId } = target.value;
|
|
111
116
|
|
|
112
117
|
// Service Key required AND validated
|
|
113
118
|
const { result: skResult } = validateKey(
|
|
@@ -133,7 +138,7 @@ sqlRoute.openapi(executeSql, async (c) => {
|
|
|
133
138
|
databaseNamespace: c.env.DATABASE,
|
|
134
139
|
},
|
|
135
140
|
namespace,
|
|
136
|
-
|
|
141
|
+
instanceId,
|
|
137
142
|
sql,
|
|
138
143
|
params ?? [],
|
|
139
144
|
);
|
package/src/routes/tables.ts
CHANGED
|
@@ -22,7 +22,13 @@
|
|
|
22
22
|
*/
|
|
23
23
|
import { OpenAPIHono, createRoute, z, type HonoEnv } from '../lib/hono.js';
|
|
24
24
|
import type { Context } from 'hono';
|
|
25
|
-
import {
|
|
25
|
+
import {
|
|
26
|
+
formatDbTargetValidationIssue,
|
|
27
|
+
getDbDoName,
|
|
28
|
+
parseConfig,
|
|
29
|
+
resolveDbTarget,
|
|
30
|
+
shouldRouteToD1,
|
|
31
|
+
} from '../lib/do-router.js';
|
|
26
32
|
import { fetchDOWithRetry } from '../lib/do-retry.js';
|
|
27
33
|
import {
|
|
28
34
|
queryParamsSchema, listResponseSchema, recordResponseSchema,
|
|
@@ -562,7 +568,8 @@ tablesRoute.openapi(dbDeleteRecord, async (c) => {
|
|
|
562
568
|
* - provider='do' (default): forwards to DatabaseDO instance
|
|
563
569
|
* - provider='neon'|'postgres': handles in Worker via postgres-handler
|
|
564
570
|
*
|
|
565
|
-
* Handles §36 canCreate 2-RTT flow for dynamic DOs
|
|
571
|
+
* Handles §36 canCreate 2-RTT flow for dynamic DOs and auto-retries bootstrap
|
|
572
|
+
* for single-instance provider='do' namespaces.
|
|
566
573
|
*/
|
|
567
574
|
async function routeToDO(
|
|
568
575
|
c: Context<HonoEnv>,
|
|
@@ -574,14 +581,17 @@ async function routeToDO(
|
|
|
574
581
|
const tableName = decodeURIComponent(_tableName);
|
|
575
582
|
// Check provider — route to D1 or PostgreSQL handler if not DO
|
|
576
583
|
const config = parseConfig(c.env);
|
|
577
|
-
const
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
584
|
+
const target = resolveDbTarget(config, namespace, instanceId);
|
|
585
|
+
if (!target.ok) {
|
|
586
|
+
return c.json({
|
|
587
|
+
code: target.status,
|
|
588
|
+
message: formatDbTargetValidationIssue(target.issue, namespace),
|
|
589
|
+
}, target.status);
|
|
581
590
|
}
|
|
591
|
+
const { dbBlock, dynamic: dynamicDbBlock, instanceId: normalizedInstanceId } = target.value;
|
|
582
592
|
|
|
583
593
|
// D1 route: single-instance namespaces without dynamic instanceId
|
|
584
|
-
if (!
|
|
594
|
+
if (!normalizedInstanceId && shouldRouteToD1(namespace, config)) {
|
|
585
595
|
return handleD1Request(c as unknown as Context<HonoEnv>, namespace, tableName, doPath);
|
|
586
596
|
}
|
|
587
597
|
|
|
@@ -590,9 +600,10 @@ async function routeToDO(
|
|
|
590
600
|
if (provider === 'neon' || provider === 'postgres') {
|
|
591
601
|
return handlePgRequest(c as unknown as Context<HonoEnv>, namespace, tableName, doPath);
|
|
592
602
|
}
|
|
603
|
+
const requiresCreateAuthorization = dynamicDbBlock;
|
|
593
604
|
|
|
594
605
|
// Build DO name: 'shared' | 'workspace:ws-456' (§2)
|
|
595
|
-
const doName = getDbDoName(namespace,
|
|
606
|
+
const doName = getDbDoName(namespace, normalizedInstanceId);
|
|
596
607
|
|
|
597
608
|
const doId = c.env.DATABASE.idFromName(doName);
|
|
598
609
|
const stub = c.env.DATABASE.get(doId);
|
|
@@ -648,26 +659,33 @@ async function routeToDO(
|
|
|
648
659
|
body: bodyText,
|
|
649
660
|
}, { safeToRetry });
|
|
650
661
|
|
|
651
|
-
// §36: Handle needsCreate 2-RTT flow for dynamic DOs
|
|
652
|
-
|
|
662
|
+
// §36: Handle needsCreate 2-RTT flow for dynamic DOs and bootstrap retry for
|
|
663
|
+
// single-instance provider='do' namespaces.
|
|
664
|
+
if (res.status === 201) {
|
|
653
665
|
const body = await res.clone().json().catch(() => null) as
|
|
654
666
|
| { needsCreate?: boolean; namespace?: string; id?: string }
|
|
655
667
|
| null;
|
|
656
668
|
if (body?.needsCreate) {
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
669
|
+
let allowed = !requiresCreateAuthorization;
|
|
670
|
+
|
|
671
|
+
if (!allowed) {
|
|
672
|
+
// Evaluate DbLevelRules.canCreate(auth, id) in Worker (#133 §36)
|
|
673
|
+
const config = parseConfig(c.env);
|
|
674
|
+
const currentDbBlock = config.databases?.[namespace];
|
|
675
|
+
const canCreateFn = currentDbBlock?.access?.canCreate;
|
|
676
|
+
|
|
677
|
+
// Internal/admin DB proxy calls already bypass row-level rules.
|
|
678
|
+
// Dynamic DB bootstrap must honor that bypass too, otherwise
|
|
679
|
+
// context.admin.db(namespace, id).table(...).insert() fails on first write.
|
|
680
|
+
allowed = isServiceKey;
|
|
681
|
+
if (!allowed && canCreateFn) {
|
|
682
|
+
try {
|
|
683
|
+
allowed = await Promise.resolve(
|
|
684
|
+
canCreateFn(auth ?? null, body.id ?? normalizedInstanceId ?? namespace),
|
|
685
|
+
);
|
|
686
|
+
} catch {
|
|
687
|
+
allowed = false; // fail-closed
|
|
688
|
+
}
|
|
671
689
|
}
|
|
672
690
|
}
|
|
673
691
|
|
|
@@ -681,11 +699,6 @@ async function routeToDO(
|
|
|
681
699
|
// Authorized — retry with X-DO-Create-Authorized header
|
|
682
700
|
const retryHeaders = new Headers(headers);
|
|
683
701
|
retryHeaders.set('X-DO-Create-Authorized', '1');
|
|
684
|
-
const retryInit: RequestInit = { method: c.req.raw.method, headers: retryHeaders };
|
|
685
|
-
// BUG-008 fix: use pre-read body text (stream already consumed above)
|
|
686
|
-
if (c.req.raw.method !== 'GET' && c.req.raw.method !== 'HEAD') {
|
|
687
|
-
retryInit.body = bodyText ?? null;
|
|
688
|
-
}
|
|
689
702
|
// needsCreate 2-RTT: DO is empty at this point, safe to retry
|
|
690
703
|
return fetchDOWithRetry(stub, doUrl, {
|
|
691
704
|
method: c.req.raw.method,
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a as r}from"../chunks/byv2rTy8.js";import{w as t}from"../chunks/DiyBpamp.js";export{t as load_css,r as start};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{_ as m}from"../chunks/BxoNtYHK.js";export{m as component};
|