@lunora/server 0.0.0 → 1.0.0-alpha.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/LICENSE.md +105 -0
- package/README.md +130 -9
- package/__assets__/package-og.svg +14 -0
- package/dist/data-model.d.mts +328 -0
- package/dist/data-model.d.ts +328 -0
- package/dist/data-model.mjs +1 -0
- package/dist/drizzle.d.mts +1 -0
- package/dist/drizzle.d.ts +1 -0
- package/dist/drizzle.mjs +1 -0
- package/dist/index.d.mts +1741 -0
- package/dist/index.d.ts +1741 -0
- package/dist/index.mjs +24 -0
- package/dist/packem_shared/LunoraError-DhggBJZF.mjs +51 -0
- package/dist/packem_shared/asBucketStorage-Cnxd9y2q.mjs +11 -0
- package/dist/packem_shared/bindTableFacade-DCuyr46L.mjs +71 -0
- package/dist/packem_shared/defineAggregateIndex-DzqxtAyV.mjs +236 -0
- package/dist/packem_shared/defineEnv-DjFkpkSP.mjs +187 -0
- package/dist/packem_shared/defineMigration-CAJLr6fx.mjs +8 -0
- package/dist/packem_shared/definePolicy-De67zPDS.mjs +29 -0
- package/dist/packem_shared/definePresence-D5LtwGl0.mjs +114 -0
- package/dist/packem_shared/defineSchemaExtension-Ck5_TUO8.mjs +100 -0
- package/dist/packem_shared/defineStorageRule-qu0mpilX.mjs +20 -0
- package/dist/packem_shared/httpAction-B7FYUEgr.mjs +340 -0
- package/dist/packem_shared/initLunora-CATvPsVt.mjs +86 -0
- package/dist/packem_shared/mask-CkZJHHMM.mjs +211 -0
- package/dist/packem_shared/onConnect-CIPXKPyw.mjs +13 -0
- package/dist/packem_shared/protectPublic-BjFkQ_Or.mjs +15 -0
- package/dist/packem_shared/rls-Zhf5wEeJ.mjs +551 -0
- package/dist/packem_shared/run-middleware-CYQOuoV6.mjs +18 -0
- package/dist/packem_shared/storageRules-4a30FSpI.mjs +88 -0
- package/dist/packem_shared/types.d-BDY0FYHK.d.ts +135 -0
- package/dist/packem_shared/types.d-DmvyEMD6.d.mts +135 -0
- package/dist/rls/testing.d.mts +63 -0
- package/dist/rls/testing.d.ts +63 -0
- package/dist/rls/testing.mjs +49 -0
- package/dist/types.d.mts +1029 -0
- package/dist/types.d.ts +1029 -0
- package/dist/types.mjs +31 -0
- package/package.json +59 -17
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { v } from '@lunora/values';
|
|
2
|
+
import { initLunora } from './initLunora-CATvPsVt.mjs';
|
|
3
|
+
import { LunoraError } from './LunoraError-DhggBJZF.mjs';
|
|
4
|
+
import { onDisconnect } from './onConnect-CIPXKPyw.mjs';
|
|
5
|
+
import { defineSchemaExtension, defineComponent } from './defineSchemaExtension-Ck5_TUO8.mjs';
|
|
6
|
+
import { defineTable } from './defineAggregateIndex-DzqxtAyV.mjs';
|
|
7
|
+
|
|
8
|
+
const DEFAULT_TTL_MS = 3e4;
|
|
9
|
+
const MAX_DATA_BYTES = 4096;
|
|
10
|
+
const PRESENCE_KEY = "presence";
|
|
11
|
+
const PRESENCE_BARE_TABLE = "present";
|
|
12
|
+
const PRESENCE_TABLE = `${PRESENCE_KEY}_${PRESENCE_BARE_TABLE}`;
|
|
13
|
+
const presenceExtension = defineSchemaExtension(PRESENCE_KEY, {
|
|
14
|
+
tables: {
|
|
15
|
+
[PRESENCE_BARE_TABLE]: defineTable({
|
|
16
|
+
data: v.optional(v.record(v.string(), v.any())),
|
|
17
|
+
lastSeen: v.number(),
|
|
18
|
+
roomId: v.string(),
|
|
19
|
+
sessionId: v.string(),
|
|
20
|
+
userId: v.optional(v.string())
|
|
21
|
+
}).index("byRoomSession", ["roomId", "sessionId"]).index("byRoom", ["roomId"])
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const { mutation, query } = initLunora.dataModel().create();
|
|
25
|
+
const definePresence = (options = {}) => {
|
|
26
|
+
const ttlMs = options.ttlMs ?? DEFAULT_TTL_MS;
|
|
27
|
+
const disconnectGraceMs = Math.max(0, Math.min(options.disconnectGraceMs ?? 0, ttlMs));
|
|
28
|
+
const heartbeat = mutation.input({
|
|
29
|
+
data: v.optional(v.record(v.string(), v.any())),
|
|
30
|
+
roomId: v.string(),
|
|
31
|
+
sessionId: v.string()
|
|
32
|
+
}).mutation(async ({ args, ctx: context }) => {
|
|
33
|
+
const lastSeen = Date.now();
|
|
34
|
+
const userId = context.auth.userId ?? void 0;
|
|
35
|
+
if (args.data !== void 0 && JSON.stringify(args.data).length > MAX_DATA_BYTES) {
|
|
36
|
+
throw new LunoraError("BAD_REQUEST", `presence data exceeds the ${String(MAX_DATA_BYTES)}-byte limit`);
|
|
37
|
+
}
|
|
38
|
+
const existing = await context.db.query(PRESENCE_TABLE).withIndex("byRoomSession", (q) => q.eq("roomId", args.roomId).eq("sessionId", args.sessionId)).first();
|
|
39
|
+
if (existing && (existing["userId"] ?? void 0) !== userId) {
|
|
40
|
+
throw new LunoraError("FORBIDDEN", "presence heartbeat denied: this (roomId, sessionId) is held by another identity");
|
|
41
|
+
}
|
|
42
|
+
const row = {
|
|
43
|
+
lastSeen,
|
|
44
|
+
roomId: args.roomId,
|
|
45
|
+
sessionId: args.sessionId,
|
|
46
|
+
...args.data === void 0 ? {} : { data: args.data },
|
|
47
|
+
...userId === void 0 ? {} : { userId }
|
|
48
|
+
};
|
|
49
|
+
await (existing ? context.db.patch(existing["_id"], row) : context.db.insert(PRESENCE_TABLE, row));
|
|
50
|
+
return { lastSeen };
|
|
51
|
+
});
|
|
52
|
+
const listPresent = query.input({ roomId: v.string() }).query(async ({ args, ctx: context }) => {
|
|
53
|
+
const cutoff = Date.now() - ttlMs;
|
|
54
|
+
const rows = await context.db.query(PRESENCE_TABLE).withIndex("byRoom", (q) => q.eq("roomId", args.roomId)).collect();
|
|
55
|
+
const live = rows.filter((row) => row["lastSeen"] > cutoff).toSorted((a, b) => b["lastSeen"] - a["lastSeen"]);
|
|
56
|
+
const seenUsers = /* @__PURE__ */ new Set();
|
|
57
|
+
const members = [];
|
|
58
|
+
for (const row of live) {
|
|
59
|
+
const userId = row["userId"];
|
|
60
|
+
if (userId !== void 0) {
|
|
61
|
+
if (seenUsers.has(userId)) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
seenUsers.add(userId);
|
|
65
|
+
}
|
|
66
|
+
const member = {
|
|
67
|
+
lastSeen: row["lastSeen"],
|
|
68
|
+
roomId: row["roomId"]
|
|
69
|
+
};
|
|
70
|
+
if (userId !== void 0) {
|
|
71
|
+
member.userId = userId;
|
|
72
|
+
}
|
|
73
|
+
if (row["data"] !== void 0) {
|
|
74
|
+
member.data = row["data"];
|
|
75
|
+
}
|
|
76
|
+
members.push(member);
|
|
77
|
+
}
|
|
78
|
+
return members;
|
|
79
|
+
});
|
|
80
|
+
const sweep = mutation.input({ roomId: v.string() }).mutation(async ({ args, ctx: context }) => {
|
|
81
|
+
const cutoff = Date.now() - ttlMs;
|
|
82
|
+
const stale = await context.db.query(PRESENCE_TABLE).withIndex("byRoom", (q) => q.eq("roomId", args.roomId)).filter((row) => row["lastSeen"] <= cutoff).collect();
|
|
83
|
+
await Promise.all(stale.map((row) => context.db.delete(row["_id"])));
|
|
84
|
+
return { deleted: stale.length };
|
|
85
|
+
});
|
|
86
|
+
const internalSweep = { ...sweep, visibility: "internal" };
|
|
87
|
+
const disconnect = onDisconnect(async (context, event) => {
|
|
88
|
+
const roomId = event.context?.["roomId"];
|
|
89
|
+
const sessionId = event.context?.["sessionId"];
|
|
90
|
+
if (typeof roomId !== "string" || typeof sessionId !== "string") {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const existing = await context.db.query(PRESENCE_TABLE).withIndex("byRoomSession", (q) => q.eq("roomId", roomId).eq("sessionId", sessionId)).first();
|
|
94
|
+
if (!existing) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const verifiedUserId = event.userId ?? void 0;
|
|
98
|
+
if ((existing["userId"] ?? void 0) !== verifiedUserId) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (disconnectGraceMs === 0) {
|
|
102
|
+
await context.db.delete(existing["_id"]);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const agedLastSeen = Math.min(existing["lastSeen"], Date.now() + disconnectGraceMs - ttlMs);
|
|
106
|
+
await context.db.patch(existing["_id"], { lastSeen: agedLastSeen });
|
|
107
|
+
});
|
|
108
|
+
return defineComponent(PRESENCE_KEY, {
|
|
109
|
+
extension: presenceExtension,
|
|
110
|
+
functions: { disconnect, heartbeat, listPresent, sweep: internalSweep }
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export { DEFAULT_TTL_MS as PRESENCE_DEFAULT_TTL_MS, PRESENCE_TABLE, definePresence, presenceExtension };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { r as runMiddlewareChain } from './run-middleware-CYQOuoV6.mjs';
|
|
2
|
+
|
|
3
|
+
const prefixTableName = (key, bareName) => `${key}_${bareName}`;
|
|
4
|
+
const rewriteReference = (target, key, bareNames) => bareNames.has(target) ? prefixTableName(key, target) : target;
|
|
5
|
+
const rewriteTableReferences = (table, key, bareNames, ownBareName) => {
|
|
6
|
+
const ownPrefixed = prefixTableName(key, ownBareName);
|
|
7
|
+
const rewriteOn = (on) => on === "" ? ownPrefixed : rewriteReference(on, key, bareNames);
|
|
8
|
+
const relationMap = {};
|
|
9
|
+
for (const [accessor, relation] of Object.entries(table.relationMap)) {
|
|
10
|
+
relationMap[accessor] = { ...relation, table: rewriteReference(relation.table, key, bareNames) };
|
|
11
|
+
}
|
|
12
|
+
const aggregateIndexes = table.aggregateIndexes.map((index) => {
|
|
13
|
+
return { ...index, on: rewriteOn(index.on) };
|
|
14
|
+
});
|
|
15
|
+
const rankIndexes = table.rankIndexes.map((index) => {
|
|
16
|
+
return { ...index, on: rewriteOn(index.on) };
|
|
17
|
+
});
|
|
18
|
+
return { ...table, aggregateIndexes, rankIndexes, relationMap };
|
|
19
|
+
};
|
|
20
|
+
const defineSchemaExtension = (key, options) => {
|
|
21
|
+
if (!key) {
|
|
22
|
+
throw new Error("defineSchemaExtension: `key` is required and must be a non-empty string");
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
key,
|
|
26
|
+
tables: options.tables,
|
|
27
|
+
...options.vectorIndexes ? { vectorIndexes: options.vectorIndexes } : {}
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
const definePlugin = (key, options) => {
|
|
31
|
+
if (!key) {
|
|
32
|
+
throw new Error("definePlugin: `key` is required and must be a non-empty string");
|
|
33
|
+
}
|
|
34
|
+
if (options.extension && options.extension.key !== key) {
|
|
35
|
+
throw new Error(`definePlugin("${key}"): extension key "${options.extension.key}" does not match plugin key`);
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
key,
|
|
39
|
+
...options.extension ? { extension: options.extension } : {},
|
|
40
|
+
...options.middleware ? { middleware: options.middleware } : {}
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
const defineComponent = (key, options) => {
|
|
44
|
+
const plugin = definePlugin(key, {
|
|
45
|
+
...options.extension ? { extension: options.extension } : {},
|
|
46
|
+
...options.middleware ? { middleware: options.middleware } : {}
|
|
47
|
+
});
|
|
48
|
+
return {
|
|
49
|
+
...plugin,
|
|
50
|
+
functions: options.functions ?? {}
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
const mergeSchemaExtension = (base, extension) => {
|
|
54
|
+
const { key } = extension;
|
|
55
|
+
const bareNames = new Set(Object.keys(extension.tables));
|
|
56
|
+
const merged = { ...base.tables };
|
|
57
|
+
for (const [bareName, table] of Object.entries(extension.tables)) {
|
|
58
|
+
const prefixed = prefixTableName(key, bareName);
|
|
59
|
+
if (Object.hasOwn(merged, prefixed)) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`defineSchema(...).extend("${key}"): table "${prefixed}" already exists in the base schema — another extension with the same key already contributed it`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
merged[prefixed] = rewriteTableReferences(table, key, bareNames, bareName);
|
|
65
|
+
}
|
|
66
|
+
const mergedVectorIndexes = { ...base.vectorIndexes };
|
|
67
|
+
if (extension.vectorIndexes) {
|
|
68
|
+
for (const [bareIndexName, index] of Object.entries(extension.vectorIndexes)) {
|
|
69
|
+
const prefixed = prefixTableName(key, bareIndexName);
|
|
70
|
+
if (Object.hasOwn(mergedVectorIndexes, prefixed)) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
`defineSchema(...).extend("${key}"): vector index "${prefixed}" already exists in the base schema — another extension with the same key already contributed it`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
mergedVectorIndexes[prefixed] = { ...index, table: rewriteReference(index.table, key, bareNames) };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
// Preserve secure-by-default RLS across `.extend(...)` — a plugin's
|
|
80
|
+
// tables join an `.rls("required")` schema as protected-by-default too.
|
|
81
|
+
rlsMode: base.rlsMode,
|
|
82
|
+
tables: merged,
|
|
83
|
+
vectorIndexes: mergedVectorIndexes
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
const installPlugins = (base, plugins) => {
|
|
87
|
+
let schema = base;
|
|
88
|
+
for (const plugin of plugins) {
|
|
89
|
+
if (plugin.extension) {
|
|
90
|
+
schema = mergeSchemaExtension(schema, plugin.extension);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return schema;
|
|
94
|
+
};
|
|
95
|
+
const composePluginMiddleware = (plugins) => {
|
|
96
|
+
const middlewares = plugins.map((plugin) => plugin.middleware).filter((middleware) => middleware !== void 0);
|
|
97
|
+
return (async ({ ctx, next }) => runMiddlewareChain(middlewares, ctx, (context) => next({ ctx: context })));
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export { composePluginMiddleware, defineComponent, definePlugin, defineSchemaExtension, installPlugins, mergeSchemaExtension };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const defineStorageRule = (input) => {
|
|
2
|
+
return { bucket: input.bucket, on: input.on, prefix: input.prefix, when: input.when };
|
|
3
|
+
};
|
|
4
|
+
const defineStorageRules = (rules) => {
|
|
5
|
+
const seenWhenByKey = /* @__PURE__ */ new Map();
|
|
6
|
+
for (const rule of rules) {
|
|
7
|
+
const key = JSON.stringify([rule.bucket, rule.on, rule.prefix]);
|
|
8
|
+
const whens = seenWhenByKey.get(key) ?? /* @__PURE__ */ new Set();
|
|
9
|
+
if (whens.has(rule.when)) {
|
|
10
|
+
throw new Error(
|
|
11
|
+
`defineStorageRules: duplicate rule for (bucket "${rule.bucket}", on "${rule.on}"${rule.prefix === void 0 ? "" : `, prefix "${rule.prefix}"`}) — the same decision function is registered more than once. Multiple distinct rules per (bucket, on) are allowed (they OR); remove the duplicate.`
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
whens.add(rule.when);
|
|
15
|
+
seenWhenByKey.set(key, whens);
|
|
16
|
+
}
|
|
17
|
+
return rules;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export { defineStorageRule, defineStorageRules };
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { parseValidatorMap, ValidationError } from '@lunora/values';
|
|
2
|
+
import { Hono } from 'hono';
|
|
3
|
+
import { LunoraError } from './LunoraError-DhggBJZF.mjs';
|
|
4
|
+
|
|
5
|
+
const httpAction = (handler) => async (c) => handler(c.get("lunora"), c.req.raw);
|
|
6
|
+
const httpRouter = () => {
|
|
7
|
+
const app = new Hono();
|
|
8
|
+
app.use("*", async (c, next) => {
|
|
9
|
+
const injected = c.env.__lunoraCtx;
|
|
10
|
+
if (!injected) {
|
|
11
|
+
throw new LunoraError(
|
|
12
|
+
"INTERNAL_SERVER_ERROR",
|
|
13
|
+
"HttpActionCtx was not injected — mount httpRouter() on createWorker(), which supplies it per request."
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
c.set("lunora", injected);
|
|
17
|
+
await next();
|
|
18
|
+
});
|
|
19
|
+
return app;
|
|
20
|
+
};
|
|
21
|
+
const unwrapOptional = (validator) => validator.kind === "optional" ? validator._meta?.inner ?? validator : validator;
|
|
22
|
+
const coerceScalar = (kind, raw) => {
|
|
23
|
+
switch (kind) {
|
|
24
|
+
case "bigint": {
|
|
25
|
+
try {
|
|
26
|
+
return BigInt(raw);
|
|
27
|
+
} catch {
|
|
28
|
+
return raw;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
case "boolean": {
|
|
32
|
+
if (raw === "true" || raw === "1") {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
if (raw === "false" || raw === "0") {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return raw;
|
|
39
|
+
}
|
|
40
|
+
case "number": {
|
|
41
|
+
return raw === "" ? Number.NaN : Number(raw);
|
|
42
|
+
}
|
|
43
|
+
default: {
|
|
44
|
+
return raw;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const coerceSearchParameter = (validator, c, key) => {
|
|
49
|
+
const effective = unwrapOptional(validator);
|
|
50
|
+
if (effective.kind === "array") {
|
|
51
|
+
const values = c.req.queries(key);
|
|
52
|
+
if (values === void 0) {
|
|
53
|
+
return void 0;
|
|
54
|
+
}
|
|
55
|
+
const element = effective._meta?.inner;
|
|
56
|
+
return values.map((raw2) => coerceScalar(element?.kind ?? "string", raw2));
|
|
57
|
+
}
|
|
58
|
+
const raw = c.req.query(key);
|
|
59
|
+
return raw === void 0 ? void 0 : coerceScalar(effective.kind, raw);
|
|
60
|
+
};
|
|
61
|
+
const parseSearchParams = (validators, c) => {
|
|
62
|
+
const raw = {};
|
|
63
|
+
for (const key of Object.keys(validators)) {
|
|
64
|
+
const validator = validators[key];
|
|
65
|
+
if (!validator) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
raw[key] = coerceSearchParameter(validator, c, key);
|
|
69
|
+
}
|
|
70
|
+
return parseValidatorMap(validators, raw, "searchParams");
|
|
71
|
+
};
|
|
72
|
+
const parseParams = (validators, c) => {
|
|
73
|
+
const provided = c.req.param();
|
|
74
|
+
const raw = {};
|
|
75
|
+
for (const key of Object.keys(validators)) {
|
|
76
|
+
const validator = validators[key];
|
|
77
|
+
if (!validator) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const value = provided[key];
|
|
81
|
+
raw[key] = value === void 0 ? void 0 : coerceScalar(unwrapOptional(validator).kind, value);
|
|
82
|
+
}
|
|
83
|
+
return parseValidatorMap(validators, raw, "params");
|
|
84
|
+
};
|
|
85
|
+
const parseBody = async (validators, c) => {
|
|
86
|
+
let json;
|
|
87
|
+
try {
|
|
88
|
+
json = await c.req.json();
|
|
89
|
+
} catch {
|
|
90
|
+
throw new LunoraError("BAD_REQUEST", "Invalid JSON body");
|
|
91
|
+
}
|
|
92
|
+
if (typeof json !== "object" || json === null || Array.isArray(json)) {
|
|
93
|
+
throw new LunoraError("BAD_REQUEST", "Expected a JSON object body");
|
|
94
|
+
}
|
|
95
|
+
return parseValidatorMap(validators, json, "body");
|
|
96
|
+
};
|
|
97
|
+
const applyOutput = (output, result) => {
|
|
98
|
+
try {
|
|
99
|
+
return output.parse(result);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (error instanceof ValidationError) {
|
|
102
|
+
throw new LunoraError("INTERNAL_SERVER_ERROR", `Response did not match the declared output schema: ${error.message}`);
|
|
103
|
+
}
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const errorResponse = (error) => {
|
|
108
|
+
if (error instanceof ValidationError) {
|
|
109
|
+
return Response.json({ code: "BAD_REQUEST", error: error.message }, { status: 400 });
|
|
110
|
+
}
|
|
111
|
+
if (error instanceof LunoraError) {
|
|
112
|
+
return Response.json({ code: error.code, error: error.message }, { status: error.status });
|
|
113
|
+
}
|
|
114
|
+
throw error;
|
|
115
|
+
};
|
|
116
|
+
const buildRouteHandler = (state, userHandler) => async (c) => {
|
|
117
|
+
try {
|
|
118
|
+
const context = c.get("lunora");
|
|
119
|
+
const searchParams = Object.keys(state.searchParams).length > 0 ? parseSearchParams(state.searchParams, c) : {};
|
|
120
|
+
const params = Object.keys(state.params).length > 0 ? parseParams(state.params, c) : {};
|
|
121
|
+
const body = Object.keys(state.body).length > 0 ? await parseBody(state.body, c) : {};
|
|
122
|
+
const result = await userHandler({ body, ctx: context, params, searchParams });
|
|
123
|
+
const payload = state.output ? applyOutput(state.output, result) : result;
|
|
124
|
+
return payload === void 0 ? new Response(null, { status: 204 }) : Response.json(payload);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
return errorResponse(error);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const isLunoraErrorLike = (error) => {
|
|
130
|
+
if (!error || typeof error !== "object") {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
const candidate = error;
|
|
134
|
+
return candidate.name === "LunoraError" && typeof candidate.code === "string" && typeof candidate.message === "string";
|
|
135
|
+
};
|
|
136
|
+
const sseFrame = (chunk, event) => {
|
|
137
|
+
const data = JSON.stringify(chunk);
|
|
138
|
+
const prefix = event ? `event: ${event}
|
|
139
|
+
` : "";
|
|
140
|
+
return `${prefix}data: ${data}
|
|
141
|
+
|
|
142
|
+
`;
|
|
143
|
+
};
|
|
144
|
+
const buildStreamHandler = (state, userHandler) => (
|
|
145
|
+
// eslint-disable-next-line @typescript-eslint/require-await -- LunoraRouteHandler is contractually `(c) => Promise<Response>`; this handler returns synchronously (all awaits live inside the ReadableStream pump), so `async` is required by the type, not the body.
|
|
146
|
+
async (c) => {
|
|
147
|
+
let searchParams;
|
|
148
|
+
let params;
|
|
149
|
+
try {
|
|
150
|
+
searchParams = Object.keys(state.searchParams).length > 0 ? parseSearchParams(state.searchParams, c) : {};
|
|
151
|
+
params = Object.keys(state.params).length > 0 ? parseParams(state.params, c) : {};
|
|
152
|
+
} catch (error) {
|
|
153
|
+
return errorResponse(error);
|
|
154
|
+
}
|
|
155
|
+
const context = c.get("lunora");
|
|
156
|
+
const request = c.req.raw;
|
|
157
|
+
const encoder = new TextEncoder();
|
|
158
|
+
const ac = new AbortController();
|
|
159
|
+
if (request.signal.aborted) {
|
|
160
|
+
ac.abort();
|
|
161
|
+
return new Response(
|
|
162
|
+
new ReadableStream({
|
|
163
|
+
start: (controller) => {
|
|
164
|
+
controller.close();
|
|
165
|
+
}
|
|
166
|
+
}),
|
|
167
|
+
{
|
|
168
|
+
headers: {
|
|
169
|
+
"cache-control": "no-cache, no-transform",
|
|
170
|
+
"content-type": "text/event-stream; charset=utf-8",
|
|
171
|
+
"x-accel-buffering": "no"
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
const onAbort = () => {
|
|
177
|
+
ac.abort();
|
|
178
|
+
};
|
|
179
|
+
request.signal.addEventListener("abort", onAbort, { once: true });
|
|
180
|
+
const stream = new ReadableStream({
|
|
181
|
+
cancel() {
|
|
182
|
+
request.signal.removeEventListener("abort", onAbort);
|
|
183
|
+
ac.abort();
|
|
184
|
+
},
|
|
185
|
+
async start(controller) {
|
|
186
|
+
try {
|
|
187
|
+
const iterator = userHandler({ ctx: context, params, request, searchParams, signal: ac.signal });
|
|
188
|
+
for await (const chunk of iterator) {
|
|
189
|
+
if (ac.signal.aborted) {
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
controller.enqueue(encoder.encode(sseFrame(chunk)));
|
|
193
|
+
}
|
|
194
|
+
controller.enqueue(encoder.encode(sseFrame({}, "complete")));
|
|
195
|
+
} catch (error) {
|
|
196
|
+
let payload;
|
|
197
|
+
if (isLunoraErrorLike(error)) {
|
|
198
|
+
payload = { code: error.code, message: error.message };
|
|
199
|
+
} else {
|
|
200
|
+
console.error("[lunora] unhandled stream handler error:", error);
|
|
201
|
+
payload = { code: "INTERNAL_SERVER_ERROR", message: "Internal error" };
|
|
202
|
+
}
|
|
203
|
+
controller.enqueue(encoder.encode(sseFrame(payload, "error")));
|
|
204
|
+
} finally {
|
|
205
|
+
request.signal.removeEventListener("abort", onAbort);
|
|
206
|
+
controller.close();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
return new Response(stream, {
|
|
211
|
+
headers: {
|
|
212
|
+
"cache-control": "no-cache, no-transform",
|
|
213
|
+
"content-type": "text/event-stream; charset=utf-8",
|
|
214
|
+
// Hint to proxies (including Cloudflare's own buffering layer)
|
|
215
|
+
// that this response must not be coalesced.
|
|
216
|
+
"x-accel-buffering": "no"
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
);
|
|
221
|
+
const makeRouteBuilder = (state) => {
|
|
222
|
+
return {
|
|
223
|
+
body: (validators) => makeRouteBuilder({ ...state, body: { ...state.body, ...validators } }),
|
|
224
|
+
handler: (userHandler) => buildRouteHandler(state, userHandler),
|
|
225
|
+
output: (validator) => makeRouteBuilder({ ...state, output: validator }),
|
|
226
|
+
params: (validators) => makeRouteBuilder({ ...state, params: { ...state.params, ...validators } }),
|
|
227
|
+
searchParams: (validators) => makeRouteBuilder({ ...state, searchParams: { ...state.searchParams, ...validators } }),
|
|
228
|
+
stream: (userHandler) => buildStreamHandler(state, userHandler)
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
const makeRouteFactory = (method) => (path) => makeRouteBuilder({ body: {}, method, params: {}, path, searchParams: {} });
|
|
232
|
+
const httpRoute = {
|
|
233
|
+
delete: makeRouteFactory("DELETE"),
|
|
234
|
+
get: makeRouteFactory("GET"),
|
|
235
|
+
head: makeRouteFactory("HEAD"),
|
|
236
|
+
options: makeRouteFactory("OPTIONS"),
|
|
237
|
+
patch: makeRouteFactory("PATCH"),
|
|
238
|
+
post: makeRouteFactory("POST"),
|
|
239
|
+
put: makeRouteFactory("PUT")
|
|
240
|
+
};
|
|
241
|
+
const SINGLE_BYTE_RANGE_RE = /^bytes=(\d*)-(\d*)$/;
|
|
242
|
+
const toHttpEtag = (etag) => {
|
|
243
|
+
if (etag.startsWith('"') || etag.startsWith('W/"')) {
|
|
244
|
+
return etag;
|
|
245
|
+
}
|
|
246
|
+
return `"${etag}"`;
|
|
247
|
+
};
|
|
248
|
+
const isSafeHeaderValue = (value) => {
|
|
249
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
250
|
+
const code = value.codePointAt(index);
|
|
251
|
+
if (code === 13 || code === 10 || code === 0) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return true;
|
|
256
|
+
};
|
|
257
|
+
const parseRange = (header, size) => {
|
|
258
|
+
if (header === null) {
|
|
259
|
+
return { kind: "full" };
|
|
260
|
+
}
|
|
261
|
+
const match = SINGLE_BYTE_RANGE_RE.exec(header.trim());
|
|
262
|
+
if (!match) {
|
|
263
|
+
return { kind: "full" };
|
|
264
|
+
}
|
|
265
|
+
const startRaw = match[1] ?? "";
|
|
266
|
+
const endRaw = match[2] ?? "";
|
|
267
|
+
if (startRaw === "" && endRaw === "") {
|
|
268
|
+
return { kind: "full" };
|
|
269
|
+
}
|
|
270
|
+
let start;
|
|
271
|
+
let end;
|
|
272
|
+
if (startRaw === "") {
|
|
273
|
+
const suffix = Number(endRaw);
|
|
274
|
+
if (suffix === 0) {
|
|
275
|
+
return { kind: "unsatisfiable" };
|
|
276
|
+
}
|
|
277
|
+
start = Math.max(0, size - suffix);
|
|
278
|
+
end = size - 1;
|
|
279
|
+
} else {
|
|
280
|
+
start = Number(startRaw);
|
|
281
|
+
end = endRaw === "" ? size - 1 : Math.min(Number(endRaw), size - 1);
|
|
282
|
+
}
|
|
283
|
+
if (start > end || start >= size) {
|
|
284
|
+
return { kind: "unsatisfiable" };
|
|
285
|
+
}
|
|
286
|
+
return { end, kind: "partial", start };
|
|
287
|
+
};
|
|
288
|
+
const serveStorageObject = async (context, key, request) => {
|
|
289
|
+
const object = await context.storage.download(key);
|
|
290
|
+
if (!object) {
|
|
291
|
+
return new Response("Not Found", { status: 404 });
|
|
292
|
+
}
|
|
293
|
+
const rawContentType = object.httpMetadata?.contentType;
|
|
294
|
+
const contentType = rawContentType !== void 0 && isSafeHeaderValue(rawContentType) ? rawContentType : "application/octet-stream";
|
|
295
|
+
const baseHeaders = {
|
|
296
|
+
"accept-ranges": "bytes",
|
|
297
|
+
"content-type": contentType,
|
|
298
|
+
etag: toHttpEtag(object.etag)
|
|
299
|
+
};
|
|
300
|
+
if (object.sha256Base64 !== void 0) {
|
|
301
|
+
baseHeaders["repr-digest"] = `sha-256=:${object.sha256Base64}:`;
|
|
302
|
+
}
|
|
303
|
+
const range = parseRange(request.headers.get("range"), object.size);
|
|
304
|
+
if (range.kind === "unsatisfiable") {
|
|
305
|
+
object.body?.cancel().catch(() => {
|
|
306
|
+
});
|
|
307
|
+
return new Response("Range Not Satisfiable", {
|
|
308
|
+
headers: {
|
|
309
|
+
"accept-ranges": "bytes",
|
|
310
|
+
"content-range": `bytes */${String(object.size)}`,
|
|
311
|
+
"content-type": "text/plain; charset=utf-8",
|
|
312
|
+
etag: toHttpEtag(object.etag)
|
|
313
|
+
},
|
|
314
|
+
status: 416
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
if (range.kind === "full") {
|
|
318
|
+
return new Response(object.body, {
|
|
319
|
+
headers: { ...baseHeaders, "content-length": String(object.size) },
|
|
320
|
+
status: 200
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
object.body?.cancel().catch(() => {
|
|
324
|
+
});
|
|
325
|
+
const length = range.end - range.start + 1;
|
|
326
|
+
const slice = await context.storage.download(key, { range: { length, offset: range.start } });
|
|
327
|
+
if (!slice) {
|
|
328
|
+
return new Response("Not Found", { status: 404 });
|
|
329
|
+
}
|
|
330
|
+
return new Response(slice.body, {
|
|
331
|
+
headers: {
|
|
332
|
+
...baseHeaders,
|
|
333
|
+
"content-length": String(length),
|
|
334
|
+
"content-range": `bytes ${String(range.start)}-${String(range.end)}/${String(object.size)}`
|
|
335
|
+
},
|
|
336
|
+
status: 206
|
|
337
|
+
});
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
export { httpAction, httpRoute, httpRouter, serveStorageObject };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { parseValidatorMap } from '@lunora/values';
|
|
2
|
+
import { r as runMiddlewareChain } from './run-middleware-CYQOuoV6.mjs';
|
|
3
|
+
|
|
4
|
+
const validateArgs = (validators, args) => parseValidatorMap(validators, args, "args");
|
|
5
|
+
|
|
6
|
+
const runMiddleware = (middlewares, baseContext) => runMiddlewareChain(middlewares, baseContext, (context) => context);
|
|
7
|
+
const makeHandler = (args, middlewares, userHandler, output) => async (context, rawArgs) => {
|
|
8
|
+
const parsed = validateArgs(args, rawArgs);
|
|
9
|
+
const resolvedContext = await runMiddleware(middlewares, context);
|
|
10
|
+
const result = await userHandler({ args: parsed, ctx: resolvedContext });
|
|
11
|
+
return output ? output.parse(result) : result;
|
|
12
|
+
};
|
|
13
|
+
const makeStreamHandler = (args, middlewares, userHandler) => (context, rawArgs, signal) => {
|
|
14
|
+
const parsed = validateArgs(args, rawArgs);
|
|
15
|
+
return (async function* drive() {
|
|
16
|
+
const resolvedContext = await runMiddleware(middlewares, context);
|
|
17
|
+
const source = userHandler({ args: parsed, ctx: resolvedContext, signal });
|
|
18
|
+
const iterator = source[Symbol.asyncIterator]();
|
|
19
|
+
try {
|
|
20
|
+
while (true) {
|
|
21
|
+
if (signal.aborted) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const next = await iterator.next();
|
|
25
|
+
if (next.done) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (signal.aborted) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
yield next.value;
|
|
32
|
+
}
|
|
33
|
+
} finally {
|
|
34
|
+
await iterator.return?.();
|
|
35
|
+
}
|
|
36
|
+
})();
|
|
37
|
+
};
|
|
38
|
+
const makeBuilder = (kind, state, visibility) => {
|
|
39
|
+
return {
|
|
40
|
+
__lunoraProcedure: kind,
|
|
41
|
+
...visibility ? { __lunoraVisibility: visibility } : {},
|
|
42
|
+
input: (validators) => makeBuilder(kind, { ...state, args: { ...state.args, ...validators } }, visibility),
|
|
43
|
+
[kind]: (userHandler) => {
|
|
44
|
+
return {
|
|
45
|
+
args: state.args,
|
|
46
|
+
handler: makeHandler(state.args, state.middlewares, userHandler, state.output),
|
|
47
|
+
kind,
|
|
48
|
+
...visibility ? { visibility } : {}
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
output: (validator) => makeBuilder(kind, { ...state, output: validator }, visibility),
|
|
52
|
+
// `.stream()` is meaningful only on query builders. It's harmless to expose
|
|
53
|
+
// on every builder shape (callers can't hit it from action/mutation builders
|
|
54
|
+
// anyway since the type system narrows it away), but emitting it
|
|
55
|
+
// unconditionally keeps the runtime free of per-kind branching.
|
|
56
|
+
...kind === "query" ? {
|
|
57
|
+
stream: (userHandler) => {
|
|
58
|
+
return {
|
|
59
|
+
args: state.args,
|
|
60
|
+
handler: makeStreamHandler(state.args, state.middlewares, userHandler),
|
|
61
|
+
kind: "stream",
|
|
62
|
+
...visibility ? { visibility } : {}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
} : {},
|
|
66
|
+
use: (middleware) => makeBuilder(kind, { ...state, middlewares: [...state.middlewares, middleware] }, visibility)
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
const initLunora = {
|
|
70
|
+
dataModel: () => {
|
|
71
|
+
return {
|
|
72
|
+
create: (_options) => {
|
|
73
|
+
return {
|
|
74
|
+
action: makeBuilder("action", { args: {}, middlewares: [] }),
|
|
75
|
+
internalAction: makeBuilder("action", { args: {}, middlewares: [] }, "internal"),
|
|
76
|
+
internalMutation: makeBuilder("mutation", { args: {}, middlewares: [] }, "internal"),
|
|
77
|
+
internalQuery: makeBuilder("query", { args: {}, middlewares: [] }, "internal"),
|
|
78
|
+
mutation: makeBuilder("mutation", { args: {}, middlewares: [] }),
|
|
79
|
+
query: makeBuilder("query", { args: {}, middlewares: [] })
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export { initLunora };
|