@lunora/server 0.0.0 → 1.0.0-alpha.2
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/LunoraEnvError-DjFkpkSP.mjs +187 -0
- package/dist/packem_shared/LunoraError-DhggBJZF.mjs +51 -0
- package/dist/packem_shared/PRESENCE_DEFAULT_TTL_MS-BZYd5-uo.mjs +114 -0
- package/dist/packem_shared/asBucketStorage-Cnxd9y2q.mjs +11 -0
- package/dist/packem_shared/bindOrm-DCuyr46L.mjs +71 -0
- package/dist/packem_shared/composePluginMiddleware-Ck5_TUO8.mjs +100 -0
- package/dist/packem_shared/createPolicyDsl-De67zPDS.mjs +29 -0
- package/dist/packem_shared/defineAggregateIndex-DxSso0rH.mjs +236 -0
- package/dist/packem_shared/defineMigration-CAJLr6fx.mjs +8 -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-Jc84C_hK.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-BDKRbMCA.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 +1051 -0
- package/dist/types.d.ts +1051 -0
- package/dist/types.mjs +31 -0
- package/package.json +59 -17
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { isOrWrapsFromValidator } from '@lunora/values';
|
|
2
|
+
import { mergeSchemaExtension } from './composePluginMiddleware-Ck5_TUO8.mjs';
|
|
3
|
+
|
|
4
|
+
const relationBuilder = {
|
|
5
|
+
many: (table, options) => {
|
|
6
|
+
return { field: options.field, kind: "many", references: options.references ?? "_id", table };
|
|
7
|
+
},
|
|
8
|
+
one: (table, options) => {
|
|
9
|
+
return { field: options.field, kind: "one", onDelete: options.onDelete, references: options.references ?? "_id", table };
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const makeTrigger = (timing, op, handler) => {
|
|
13
|
+
return { handler, op, timing };
|
|
14
|
+
};
|
|
15
|
+
const createTriggerBuilder = () => {
|
|
16
|
+
return {
|
|
17
|
+
afterDelete: (handler) => makeTrigger("after", "delete", handler),
|
|
18
|
+
afterInsert: (handler) => makeTrigger("after", "insert", handler),
|
|
19
|
+
afterUpdate: (handler) => makeTrigger("after", "update", handler),
|
|
20
|
+
beforeDelete: (handler) => makeTrigger("before", "delete", handler),
|
|
21
|
+
beforeInsert: (handler) => makeTrigger("before", "insert", handler),
|
|
22
|
+
beforeUpdate: (handler) => makeTrigger("before", "update", handler)
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
const defineTable = (shape) => {
|
|
26
|
+
for (const [columnName, validator] of Object.entries(shape)) {
|
|
27
|
+
if (isOrWrapsFromValidator(validator)) {
|
|
28
|
+
throw new Error(`defineTable: column "${columnName}" uses v.from() which is args-only — table columns need a concrete v.* type`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const aggregateIndexes = [];
|
|
32
|
+
const indexes = [];
|
|
33
|
+
const rankIndexes = [];
|
|
34
|
+
const relations = {};
|
|
35
|
+
const searchIndexes = [];
|
|
36
|
+
const triggers = {};
|
|
37
|
+
const triggerBuilder = createTriggerBuilder();
|
|
38
|
+
const vectorIndexes = [];
|
|
39
|
+
let shardMode = { kind: "root" };
|
|
40
|
+
let isExternallyManaged = false;
|
|
41
|
+
let isPublic = false;
|
|
42
|
+
const builder = {
|
|
43
|
+
aggregateIndex(name, options) {
|
|
44
|
+
const op = options?.op ?? "count";
|
|
45
|
+
if (op !== "count" && !options?.field) {
|
|
46
|
+
throw new Error(`aggregateIndex "${name}": op "${op}" requires a "field"`);
|
|
47
|
+
}
|
|
48
|
+
aggregateIndexes.push({
|
|
49
|
+
by: options?.by,
|
|
50
|
+
field: options?.field,
|
|
51
|
+
name,
|
|
52
|
+
// `on` is filled in by `defineSchema` once the table is keyed; we
|
|
53
|
+
// stash the placeholder so the AggregateIndexDefinition shape stays
|
|
54
|
+
// straightforward for D1/DO consumers (who read `on`).
|
|
55
|
+
on: "",
|
|
56
|
+
op,
|
|
57
|
+
where: options?.where
|
|
58
|
+
});
|
|
59
|
+
return builder;
|
|
60
|
+
},
|
|
61
|
+
get aggregateIndexes() {
|
|
62
|
+
return aggregateIndexes;
|
|
63
|
+
},
|
|
64
|
+
externallyManaged() {
|
|
65
|
+
isExternallyManaged = true;
|
|
66
|
+
return builder;
|
|
67
|
+
},
|
|
68
|
+
global(options) {
|
|
69
|
+
shardMode = { backend: options?.backend ?? "d1", kind: "global" };
|
|
70
|
+
return builder;
|
|
71
|
+
},
|
|
72
|
+
get isExternallyManaged() {
|
|
73
|
+
return isExternallyManaged;
|
|
74
|
+
},
|
|
75
|
+
get isPublic() {
|
|
76
|
+
return isPublic;
|
|
77
|
+
},
|
|
78
|
+
index(name, fields, options) {
|
|
79
|
+
indexes.push({ fields, name, unique: options?.unique ?? false });
|
|
80
|
+
return builder;
|
|
81
|
+
},
|
|
82
|
+
get indexes() {
|
|
83
|
+
return indexes;
|
|
84
|
+
},
|
|
85
|
+
public() {
|
|
86
|
+
isPublic = true;
|
|
87
|
+
return builder;
|
|
88
|
+
},
|
|
89
|
+
rankIndex(name, options) {
|
|
90
|
+
if (!options.sortBy || options.sortBy.length === 0) {
|
|
91
|
+
throw new Error(`rankIndex "${name}": "sortBy" is required and must list at least one key`);
|
|
92
|
+
}
|
|
93
|
+
const sortBy = options.sortBy.map((key) => {
|
|
94
|
+
return {
|
|
95
|
+
direction: key.direction ?? "asc",
|
|
96
|
+
field: key.field
|
|
97
|
+
};
|
|
98
|
+
});
|
|
99
|
+
rankIndexes.push({
|
|
100
|
+
name,
|
|
101
|
+
// `on` is filled in by `defineSchema` once the table is keyed —
|
|
102
|
+
// same pattern as `aggregateIndex`.
|
|
103
|
+
on: "",
|
|
104
|
+
partitionBy: options.partitionBy,
|
|
105
|
+
sortBy,
|
|
106
|
+
where: options.where
|
|
107
|
+
});
|
|
108
|
+
return builder;
|
|
109
|
+
},
|
|
110
|
+
get rankIndexes() {
|
|
111
|
+
return rankIndexes;
|
|
112
|
+
},
|
|
113
|
+
get relationMap() {
|
|
114
|
+
return relations;
|
|
115
|
+
},
|
|
116
|
+
relations(build) {
|
|
117
|
+
Object.assign(relations, build(relationBuilder));
|
|
118
|
+
return builder;
|
|
119
|
+
},
|
|
120
|
+
searchIndex(name, options) {
|
|
121
|
+
searchIndexes.push({ field: options.field, filterFields: options.filterFields, name });
|
|
122
|
+
return builder;
|
|
123
|
+
},
|
|
124
|
+
get searchIndexes() {
|
|
125
|
+
return searchIndexes;
|
|
126
|
+
},
|
|
127
|
+
shape,
|
|
128
|
+
shardBy(field) {
|
|
129
|
+
shardMode = { field, kind: "shardBy" };
|
|
130
|
+
return builder;
|
|
131
|
+
},
|
|
132
|
+
get shardMode() {
|
|
133
|
+
return shardMode;
|
|
134
|
+
},
|
|
135
|
+
get triggerMap() {
|
|
136
|
+
return triggers;
|
|
137
|
+
},
|
|
138
|
+
triggers(build) {
|
|
139
|
+
Object.assign(triggers, build(triggerBuilder));
|
|
140
|
+
return builder;
|
|
141
|
+
},
|
|
142
|
+
get vectorIndexes() {
|
|
143
|
+
return vectorIndexes;
|
|
144
|
+
},
|
|
145
|
+
vectorize(field, options) {
|
|
146
|
+
vectorIndexes.push({
|
|
147
|
+
dimensions: options.dimensions,
|
|
148
|
+
embed: options.embed,
|
|
149
|
+
field,
|
|
150
|
+
metadata: options.metadata,
|
|
151
|
+
metric: options.metric,
|
|
152
|
+
name: options.index
|
|
153
|
+
});
|
|
154
|
+
return builder;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
return builder;
|
|
158
|
+
};
|
|
159
|
+
const defineVectorIndex = (options) => {
|
|
160
|
+
return {
|
|
161
|
+
dimensions: options.dimensions,
|
|
162
|
+
embed: options.embed,
|
|
163
|
+
kind: "vectorIndex",
|
|
164
|
+
metadata: options.metadata,
|
|
165
|
+
metric: options.metric,
|
|
166
|
+
select: options.source.select,
|
|
167
|
+
table: options.source.table
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
const defineAggregateIndex = (name, options) => {
|
|
171
|
+
const op = options.op ?? "count";
|
|
172
|
+
if (op !== "count" && !options.field) {
|
|
173
|
+
throw new Error(`aggregateIndex "${name}": op "${op}" requires a "field"`);
|
|
174
|
+
}
|
|
175
|
+
return { by: options.by, field: options.field, name, on: options.on, op, where: options.where };
|
|
176
|
+
};
|
|
177
|
+
const defineRankIndex = (name, options) => {
|
|
178
|
+
if (!options.sortBy || options.sortBy.length === 0) {
|
|
179
|
+
throw new Error(`rankIndex "${name}": "sortBy" is required and must list at least one key`);
|
|
180
|
+
}
|
|
181
|
+
const sortBy = options.sortBy.map((key) => {
|
|
182
|
+
return {
|
|
183
|
+
direction: key.direction ?? "asc",
|
|
184
|
+
field: key.field
|
|
185
|
+
};
|
|
186
|
+
});
|
|
187
|
+
return { name, on: options.table, partitionBy: options.partitionBy, sortBy, where: options.where };
|
|
188
|
+
};
|
|
189
|
+
const withExtend = (schema) => {
|
|
190
|
+
return {
|
|
191
|
+
...schema,
|
|
192
|
+
extend(extension) {
|
|
193
|
+
return withExtend(mergeSchemaExtension(schema, extension));
|
|
194
|
+
},
|
|
195
|
+
rls(mode) {
|
|
196
|
+
return withExtend({ ...schema, rlsMode: mode });
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
const fillIndexTableNames = (tables) => {
|
|
201
|
+
for (const [tableName, table] of Object.entries(tables)) {
|
|
202
|
+
for (const index of table.aggregateIndexes) {
|
|
203
|
+
if (index.on === "") {
|
|
204
|
+
index.on = tableName;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (const index of table.rankIndexes) {
|
|
208
|
+
if (index.on === "") {
|
|
209
|
+
index.on = tableName;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
const attachStandaloneIndexes = (tables, aggregateIndexes, rankIndexes) => {
|
|
215
|
+
for (const index of Object.values(aggregateIndexes)) {
|
|
216
|
+
const table = tables[index.on];
|
|
217
|
+
if (!table) {
|
|
218
|
+
throw new Error(`defineAggregateIndex "${index.name}": unknown table "${index.on}"`);
|
|
219
|
+
}
|
|
220
|
+
table.aggregateIndexes.push(index);
|
|
221
|
+
}
|
|
222
|
+
for (const index of Object.values(rankIndexes)) {
|
|
223
|
+
const table = tables[index.on];
|
|
224
|
+
if (!table) {
|
|
225
|
+
throw new Error(`defineRankIndex "${index.name}": unknown table "${index.on}"`);
|
|
226
|
+
}
|
|
227
|
+
table.rankIndexes.push(index);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
const defineSchema = (tables, vectorIndexes = {}, aggregateIndexes = {}, rankIndexes = {}) => {
|
|
231
|
+
fillIndexTableNames(tables);
|
|
232
|
+
attachStandaloneIndexes(tables, aggregateIndexes, rankIndexes);
|
|
233
|
+
return withExtend({ tables, vectorIndexes });
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
export { defineAggregateIndex, defineRankIndex, defineSchema, defineTable, defineVectorIndex };
|
|
@@ -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 };
|