@adep/runtime 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/dist/index.d.ts +1 -1
- package/dist/index.js +353 -47
- package/dist/shared/function-source.d.ts +101 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -39,7 +39,7 @@ export { SimSqlEngine, SimDbError } from './sim/sql-engine';
|
|
|
39
39
|
export type { SimTable, EngineStorage } from './sim/sql-engine';
|
|
40
40
|
export { SimRealtime, createSimRealtimeCapability, REALTIME_CODES } from './sim/realtime';
|
|
41
41
|
export type { SimRealtimeSubscription } from './sim/realtime';
|
|
42
|
-
export { resolveFunctionSource, parseSnapshot } from './shared/function-source';
|
|
42
|
+
export { resolveFunctionSource, parseSnapshot, resolveExecutableFiles, mergeSharedSource, withProjectSharedSource, type ProjectSharedSourceDb, type ExecutableSourceDb, type ExecutableSourceFunction, } from './shared/function-source';
|
|
43
43
|
export { StateObjectRuntime, StateObject, StateObjectError, createProjectDriverStateBackend, STATE_OBJECTS_TABLE, DemoCounter, DemoChatRoom, } from './state-objects';
|
|
44
44
|
export type { StateObjectBackend, StateObjectClass, StateObjectErrorCode, DemoCounterState, DemoChatRoomState, } from './state-objects';
|
|
45
45
|
export { STATE_PATH, parseStatePath, parseStateBody } from './state-objects/http';
|
package/dist/index.js
CHANGED
|
@@ -2018,6 +2018,7 @@ function stripQuotes(raw) {
|
|
|
2018
2018
|
const t = raw.trim();
|
|
2019
2019
|
if (/^'.*'$/.test(t)) return t.slice(1, -1);
|
|
2020
2020
|
if (/^".*"$/.test(t)) return t.slice(1, -1);
|
|
2021
|
+
if (/^NULL$/i.test(t)) return null;
|
|
2021
2022
|
return t;
|
|
2022
2023
|
}
|
|
2023
2024
|
function parseCreateTable(ddl) {
|
|
@@ -2038,50 +2039,109 @@ function parseCreateTable(ddl) {
|
|
|
2038
2039
|
}
|
|
2039
2040
|
return { name, columns };
|
|
2040
2041
|
}
|
|
2041
|
-
function
|
|
2042
|
-
const
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
const inner = rhs.replace(/^\(|\)$/g, "");
|
|
2065
|
-
const items = splitListItems(inner);
|
|
2066
|
-
const list = items.map((item) => {
|
|
2067
|
-
if (item.trim() === "?") return cursor.take();
|
|
2068
|
-
return stripQuotes(item);
|
|
2069
|
-
});
|
|
2070
|
-
clauses.push({ column: col, operator: op, value: list, list: true });
|
|
2042
|
+
function splitTopLevel(raw, token) {
|
|
2043
|
+
const parts = [];
|
|
2044
|
+
let depth = 0;
|
|
2045
|
+
let inQuote = false;
|
|
2046
|
+
let buffer = "";
|
|
2047
|
+
const n = raw.length;
|
|
2048
|
+
const isTokenAt = (i) => {
|
|
2049
|
+
const word = raw.slice(i, i + token.length);
|
|
2050
|
+
if (word.toUpperCase() !== token) return false;
|
|
2051
|
+
const before = i === 0 ? "" : raw[i - 1] ?? "";
|
|
2052
|
+
const after = raw[i + token.length] ?? "";
|
|
2053
|
+
return !/[A-Za-z0-9_.]/.test(before) && !/[A-Za-z0-9_.]/.test(after);
|
|
2054
|
+
};
|
|
2055
|
+
for (let i = 0; i < n; i += 1) {
|
|
2056
|
+
const ch = raw[i] ?? "";
|
|
2057
|
+
if (ch === "'") {
|
|
2058
|
+
if (inQuote && raw[i + 1] === "'") {
|
|
2059
|
+
buffer += ch + (raw[i + 1] ?? "");
|
|
2060
|
+
i += 1;
|
|
2061
|
+
continue;
|
|
2062
|
+
}
|
|
2063
|
+
inQuote = !inQuote;
|
|
2064
|
+
buffer += ch;
|
|
2071
2065
|
continue;
|
|
2072
2066
|
}
|
|
2073
|
-
|
|
2074
|
-
if (
|
|
2075
|
-
|
|
2067
|
+
if (!inQuote && ch === "(") depth += 1;
|
|
2068
|
+
else if (!inQuote && ch === ")") depth -= 1;
|
|
2069
|
+
if (!inQuote && depth === 0 && isTokenAt(i)) {
|
|
2070
|
+
parts.push(buffer);
|
|
2071
|
+
buffer = "";
|
|
2072
|
+
i += token.length - 1;
|
|
2076
2073
|
} else {
|
|
2077
|
-
|
|
2074
|
+
buffer += ch;
|
|
2078
2075
|
}
|
|
2079
|
-
clauses.push({ column: col, operator: op, value, list: false });
|
|
2080
2076
|
}
|
|
2081
|
-
|
|
2077
|
+
parts.push(buffer);
|
|
2078
|
+
return parts;
|
|
2079
|
+
}
|
|
2080
|
+
function unqualifyColumn(raw) {
|
|
2081
|
+
const t = raw.trim();
|
|
2082
|
+
const dot = t.lastIndexOf(".");
|
|
2083
|
+
if (dot > 0 && /^[A-Za-z_][A-Za-z0-9_]*\./.test(t)) return t.slice(dot + 1);
|
|
2084
|
+
return t;
|
|
2085
|
+
}
|
|
2086
|
+
function parseLeftJoins(tail) {
|
|
2087
|
+
const joins = [];
|
|
2088
|
+
const re = /LEFT\s+JOIN\s+((?:"[^"]+")|([A-Za-z_][A-Za-z0-9_]*))\s+([A-Za-z_][A-Za-z0-9_]*)\s+ON\s+([A-Za-z_][A-Za-z0-9_.]*)\s*=\s*([A-Za-z_][A-Za-z0-9_.]*)/gi;
|
|
2089
|
+
let m;
|
|
2090
|
+
while ((m = re.exec(tail)) !== null) {
|
|
2091
|
+
joins.push({
|
|
2092
|
+
table: ident(m[1] ?? ""),
|
|
2093
|
+
alias: m[3] ?? "",
|
|
2094
|
+
leftCol: unqualifyColumn(m[4] ?? ""),
|
|
2095
|
+
rightCol: unqualifyColumn(m[5] ?? "")
|
|
2096
|
+
});
|
|
2097
|
+
}
|
|
2098
|
+
return joins;
|
|
2099
|
+
}
|
|
2100
|
+
function parseWhereClauses(whereRaw, cursor) {
|
|
2101
|
+
const parseAndGroup = (andRaw) => {
|
|
2102
|
+
const clauses = [];
|
|
2103
|
+
for (const part of splitTopLevel(andRaw, "AND")) {
|
|
2104
|
+
const t = part.trim();
|
|
2105
|
+
if (t.length === 0) continue;
|
|
2106
|
+
if (/\bIS\s+NULL\b/i.test(t)) {
|
|
2107
|
+
clauses.push({ column: unqualifyColumn(ident(t.split(/\s+IS\s+NULL\b/i)[0] ?? "")), operator: "IS NULL", value: null, list: false });
|
|
2108
|
+
continue;
|
|
2109
|
+
}
|
|
2110
|
+
if (/\bIS\s+NOT\s+NULL\b/i.test(t)) {
|
|
2111
|
+
clauses.push({ column: unqualifyColumn(ident(t.split(/\s+IS\s+NOT\s+NULL\b/i)[0] ?? "")), operator: "IS NOT NULL", value: null, list: false });
|
|
2112
|
+
continue;
|
|
2113
|
+
}
|
|
2114
|
+
const opMatch = /^\s*((?:"[^"]+")|([A-Za-z_][A-Za-z0-9_.]*))\s*(=|!=|>=|<=|>|<|like|in|not\s+in)\s*(.+)$/i.exec(
|
|
2115
|
+
t
|
|
2116
|
+
);
|
|
2117
|
+
if (opMatch === null) continue;
|
|
2118
|
+
const col = unqualifyColumn(ident(opMatch[1] ?? ""));
|
|
2119
|
+
const op = (opMatch[3] ?? opMatch[2] ?? "").toLowerCase();
|
|
2120
|
+
const rhs = (opMatch[4] ?? "").trim();
|
|
2121
|
+
if (op === "in" || op === "not in") {
|
|
2122
|
+
const inner = rhs.replace(/^\(|\)$/g, "");
|
|
2123
|
+
const items = splitListItems(inner);
|
|
2124
|
+
const list = items.map((item) => {
|
|
2125
|
+
if (item.trim() === "?") return cursor.take();
|
|
2126
|
+
return stripQuotes(item);
|
|
2127
|
+
});
|
|
2128
|
+
clauses.push({ column: col, operator: op, value: list, list: true });
|
|
2129
|
+
continue;
|
|
2130
|
+
}
|
|
2131
|
+
let value;
|
|
2132
|
+
if (rhs === "?") {
|
|
2133
|
+
value = cursor.take();
|
|
2134
|
+
} else {
|
|
2135
|
+
value = stripQuotes(rhs);
|
|
2136
|
+
}
|
|
2137
|
+
clauses.push({ column: col, operator: op, value, list: false });
|
|
2138
|
+
}
|
|
2139
|
+
return clauses;
|
|
2140
|
+
};
|
|
2141
|
+
return splitTopLevel(whereRaw, "OR").map((group) => group.trim()).filter((group) => group.length > 0).map(parseAndGroup);
|
|
2082
2142
|
}
|
|
2083
|
-
function matchWhere(
|
|
2084
|
-
return clauses.every((c) => matchValue(row[c.column], c));
|
|
2143
|
+
function matchWhere(groups, row) {
|
|
2144
|
+
return groups.some((clauses) => clauses.every((c) => matchValue(row[c.column], c)));
|
|
2085
2145
|
}
|
|
2086
2146
|
function splitListItems(inner) {
|
|
2087
2147
|
const items = [];
|
|
@@ -2101,6 +2161,168 @@ function splitListItems(inner) {
|
|
|
2101
2161
|
items.push(buffer);
|
|
2102
2162
|
return items;
|
|
2103
2163
|
}
|
|
2164
|
+
var ExcludedRef = class {
|
|
2165
|
+
constructor(column) {
|
|
2166
|
+
this.column = column;
|
|
2167
|
+
}
|
|
2168
|
+
};
|
|
2169
|
+
var ColumnRef = class {
|
|
2170
|
+
constructor(column) {
|
|
2171
|
+
this.column = column;
|
|
2172
|
+
}
|
|
2173
|
+
};
|
|
2174
|
+
var ExprRef = class {
|
|
2175
|
+
constructor(source) {
|
|
2176
|
+
this.source = source;
|
|
2177
|
+
}
|
|
2178
|
+
};
|
|
2179
|
+
function resolveSetValue(value, existing, row) {
|
|
2180
|
+
if (value instanceof ExcludedRef) return row[value.column];
|
|
2181
|
+
if (value instanceof ColumnRef) return existing[value.column];
|
|
2182
|
+
if (value instanceof ExprRef) return evalArithmetic(value.source, existing, row);
|
|
2183
|
+
return value;
|
|
2184
|
+
}
|
|
2185
|
+
function evalArithmetic(src, existing, row) {
|
|
2186
|
+
const num = (v) => {
|
|
2187
|
+
const n2 = Number(v);
|
|
2188
|
+
return Number.isFinite(n2) ? n2 : 0;
|
|
2189
|
+
};
|
|
2190
|
+
let i = 0;
|
|
2191
|
+
const n = src.length;
|
|
2192
|
+
const skip = () => {
|
|
2193
|
+
while (i < n && /\s/.test(src[i] ?? "")) i += 1;
|
|
2194
|
+
};
|
|
2195
|
+
const peek = () => {
|
|
2196
|
+
skip();
|
|
2197
|
+
return i < n ? src[i] ?? "" : null;
|
|
2198
|
+
};
|
|
2199
|
+
const parseIdent = () => {
|
|
2200
|
+
skip();
|
|
2201
|
+
const m = /^([A-Za-z_][A-Za-z0-9_]*)/.exec(src.slice(i));
|
|
2202
|
+
if (m === null) return null;
|
|
2203
|
+
i += (m[1] ?? "").length;
|
|
2204
|
+
skip();
|
|
2205
|
+
let col = ident(m[1] ?? "");
|
|
2206
|
+
let excluded = false;
|
|
2207
|
+
if (src[i] === ".") {
|
|
2208
|
+
const m2 = /^\.([A-Za-z_][A-Za-z0-9_]*)/.exec(src.slice(i));
|
|
2209
|
+
if (m2 !== null) {
|
|
2210
|
+
i += (m2[0] ?? "").length;
|
|
2211
|
+
if (col.toLowerCase() === "excluded") excluded = true;
|
|
2212
|
+
col = ident(m2[1] ?? "");
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
return { col, excluded };
|
|
2216
|
+
};
|
|
2217
|
+
const parseFactor = () => {
|
|
2218
|
+
const ch = peek();
|
|
2219
|
+
if (ch === "(") {
|
|
2220
|
+
i += 1;
|
|
2221
|
+
const v = parseExpr();
|
|
2222
|
+
skip();
|
|
2223
|
+
if (src[i] === ")") i += 1;
|
|
2224
|
+
return v;
|
|
2225
|
+
}
|
|
2226
|
+
const numMatch = /^(\d+(?:\.\d+)?)/.exec(src.slice(i));
|
|
2227
|
+
if (numMatch !== null) {
|
|
2228
|
+
i += (numMatch[1] ?? "").length;
|
|
2229
|
+
return Number(numMatch[1]);
|
|
2230
|
+
}
|
|
2231
|
+
const id = parseIdent();
|
|
2232
|
+
if (id !== null) {
|
|
2233
|
+
const v = id.excluded ? row[id.col] : existing[id.col];
|
|
2234
|
+
return num(v);
|
|
2235
|
+
}
|
|
2236
|
+
return 0;
|
|
2237
|
+
};
|
|
2238
|
+
const parseTerm = () => {
|
|
2239
|
+
let v = parseFactor();
|
|
2240
|
+
for (; ; ) {
|
|
2241
|
+
const ch = peek();
|
|
2242
|
+
if (ch !== "*" && ch !== "/") return v;
|
|
2243
|
+
i += 1;
|
|
2244
|
+
const rhs = parseFactor();
|
|
2245
|
+
v = ch === "*" ? v * rhs : rhs === 0 ? v : v / rhs;
|
|
2246
|
+
}
|
|
2247
|
+
};
|
|
2248
|
+
const parseExpr = () => {
|
|
2249
|
+
let v = parseTerm();
|
|
2250
|
+
for (; ; ) {
|
|
2251
|
+
const ch = peek();
|
|
2252
|
+
if (ch !== "+" && ch !== "-") return v;
|
|
2253
|
+
i += 1;
|
|
2254
|
+
const rhs = parseTerm();
|
|
2255
|
+
v = ch === "+" ? v + rhs : v - rhs;
|
|
2256
|
+
}
|
|
2257
|
+
};
|
|
2258
|
+
return parseExpr();
|
|
2259
|
+
}
|
|
2260
|
+
function splitInsertTuples(body) {
|
|
2261
|
+
const tuples = [];
|
|
2262
|
+
let i = 0;
|
|
2263
|
+
const n = body.length;
|
|
2264
|
+
while (i < n) {
|
|
2265
|
+
while (i < n && (body[i] === " " || body[i] === " " || body[i] === "\n" || body[i] === "\r" || body[i] === ",")) {
|
|
2266
|
+
i += 1;
|
|
2267
|
+
}
|
|
2268
|
+
if (i >= n) break;
|
|
2269
|
+
if (body[i] !== "(") {
|
|
2270
|
+
return { tuples, conflict: body.slice(i).trim() };
|
|
2271
|
+
}
|
|
2272
|
+
let depth = 0;
|
|
2273
|
+
let j = i;
|
|
2274
|
+
for (; j < n; j += 1) {
|
|
2275
|
+
if (body[j] === "(") depth += 1;
|
|
2276
|
+
else if (body[j] === ")") {
|
|
2277
|
+
depth -= 1;
|
|
2278
|
+
if (depth === 0) break;
|
|
2279
|
+
}
|
|
2280
|
+
}
|
|
2281
|
+
if (j >= n) return { tuples, conflict: null };
|
|
2282
|
+
tuples.push(body.slice(i + 1, j));
|
|
2283
|
+
i = j + 1;
|
|
2284
|
+
}
|
|
2285
|
+
return { tuples, conflict: null };
|
|
2286
|
+
}
|
|
2287
|
+
function parseConflictClause(clause, cursor, fallbackCols) {
|
|
2288
|
+
const m = /^ON\s+CONFLICT(?:\s*\(([^)]*)\))?\s+DO\s+(UPDATE\s+SET\s+([\s\S]+)|NOTHING\s*)$/i.exec(
|
|
2289
|
+
clause.trim()
|
|
2290
|
+
);
|
|
2291
|
+
if (m === null) {
|
|
2292
|
+
throw new SimDbError("DB_UNSAFE_OP", `\u65E0\u6CD5\u89E3\u6790 ON CONFLICT \u5B50\u53E5\uFF1A${clause.trim().slice(0, 60)}\u2026`);
|
|
2293
|
+
}
|
|
2294
|
+
const colsRaw = m[1];
|
|
2295
|
+
const cols = colsRaw === void 0 ? fallbackCols : colsRaw.split(",").map(ident);
|
|
2296
|
+
if (cols.length === 0) throw new SimDbError("DB_UNSAFE_OP", "ON CONFLICT \u51B2\u7A81\u5217\u4E3A\u7A7A");
|
|
2297
|
+
const action = m[2] ?? "";
|
|
2298
|
+
if (/^NOTHING/i.test(action)) return { cols, sets: [] };
|
|
2299
|
+
const setRaw = m[3] ?? "";
|
|
2300
|
+
const sets = splitListItems(setRaw).map((part) => {
|
|
2301
|
+
const eq = /^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*([\s\S]+)$/i.exec(part.trim());
|
|
2302
|
+
if (eq === null) {
|
|
2303
|
+
throw new SimDbError("DB_UNSAFE_OP", `\u65E0\u6CD5\u89E3\u6790 SET \u8D4B\u503C\uFF1A${part.trim().slice(0, 60)}\u2026`);
|
|
2304
|
+
}
|
|
2305
|
+
const col = ident(eq[1] ?? "");
|
|
2306
|
+
const rhs = (eq[2] ?? "").trim();
|
|
2307
|
+
let value;
|
|
2308
|
+
if (rhs === "?") {
|
|
2309
|
+
value = cursor.take();
|
|
2310
|
+
} else {
|
|
2311
|
+
const ex = /^excluded\.([A-Za-z_][A-Za-z0-9_]*)$/i.exec(rhs);
|
|
2312
|
+
if (ex !== null) {
|
|
2313
|
+
value = new ExcludedRef(ident(ex[1] ?? ""));
|
|
2314
|
+
} else if (/^([A-Za-z_][A-Za-z0-9_]*)$/.test(rhs) && !/^NULL$/i.test(rhs)) {
|
|
2315
|
+
value = new ColumnRef(ident(rhs));
|
|
2316
|
+
} else if (/^'.*'$/.test(rhs) || /^".*"$/.test(rhs) || /^NULL$/i.test(rhs)) {
|
|
2317
|
+
value = stripQuotes(rhs);
|
|
2318
|
+
} else {
|
|
2319
|
+
value = new ExprRef(rhs);
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
return { col, value };
|
|
2323
|
+
});
|
|
2324
|
+
return { cols, sets };
|
|
2325
|
+
}
|
|
2104
2326
|
function matchValue(value, clause) {
|
|
2105
2327
|
const op = clause.operator;
|
|
2106
2328
|
if (op === "IS NULL") return value === null || value === void 0;
|
|
@@ -2254,16 +2476,36 @@ var SimSqlEngine = class {
|
|
|
2254
2476
|
const valueBody = match[4].trim();
|
|
2255
2477
|
const cursor = new ParamCursor(params);
|
|
2256
2478
|
const table = this.ensureTable(tableName);
|
|
2479
|
+
const { tuples, conflict } = splitInsertTuples(valueBody);
|
|
2480
|
+
if (tuples.length === 0) throw new SimDbError("DB_UNSAFE_OP", "INSERT \u7F3A\u5C11 VALUES \u5143\u7EC4");
|
|
2481
|
+
const conflictSpec = conflict === null ? null : parseConflictClause(
|
|
2482
|
+
conflict,
|
|
2483
|
+
cursor,
|
|
2484
|
+
table.columns.filter((c) => c.primaryKey).map((c) => c.name)
|
|
2485
|
+
);
|
|
2257
2486
|
let inserted = 0;
|
|
2258
|
-
for (const
|
|
2259
|
-
const
|
|
2260
|
-
const values = splitListItems(inner).map(
|
|
2487
|
+
for (const tuple of tuples) {
|
|
2488
|
+
const values = splitListItems(tuple).map(
|
|
2261
2489
|
(item) => item.trim() === "?" ? cursor.take() : stripQuotes(item)
|
|
2262
2490
|
);
|
|
2263
2491
|
const row = {};
|
|
2264
2492
|
cols.forEach((col, i) => {
|
|
2265
2493
|
row[col] = values[i] ?? null;
|
|
2266
2494
|
});
|
|
2495
|
+
if (conflictSpec !== null) {
|
|
2496
|
+
const existing = table.rows.find(
|
|
2497
|
+
(r) => conflictSpec.cols.every((c) => r[c] === row[c])
|
|
2498
|
+
);
|
|
2499
|
+
if (existing !== void 0) {
|
|
2500
|
+
const oldRow = { ...existing };
|
|
2501
|
+
for (const set of conflictSpec.sets) {
|
|
2502
|
+
existing[set.col] = resolveSetValue(set.value, oldRow, row);
|
|
2503
|
+
}
|
|
2504
|
+
inserted += 1;
|
|
2505
|
+
void this.persist();
|
|
2506
|
+
continue;
|
|
2507
|
+
}
|
|
2508
|
+
}
|
|
2267
2509
|
this.applyAutoincrement(table, row);
|
|
2268
2510
|
table.rows.push(row);
|
|
2269
2511
|
inserted += 1;
|
|
@@ -2332,6 +2574,20 @@ var SimSqlEngine = class {
|
|
|
2332
2574
|
return project(this.catalog(), selectRaw);
|
|
2333
2575
|
}
|
|
2334
2576
|
const table = this.ensureTable(tableName);
|
|
2577
|
+
let rows = table.rows;
|
|
2578
|
+
const joins = parseLeftJoins(tail);
|
|
2579
|
+
if (joins.length > 0) {
|
|
2580
|
+
rows = table.rows.map((row) => {
|
|
2581
|
+
const merged = { ...row };
|
|
2582
|
+
for (const j of joins) {
|
|
2583
|
+
const joined = this.tables[j.table]?.rows.find((jr) => jr[j.leftCol] === merged[j.rightCol]);
|
|
2584
|
+
if (joined !== void 0) {
|
|
2585
|
+
for (const [k, v] of Object.entries(joined)) merged[`${j.alias}.${k}`] = v;
|
|
2586
|
+
}
|
|
2587
|
+
}
|
|
2588
|
+
return merged;
|
|
2589
|
+
});
|
|
2590
|
+
}
|
|
2335
2591
|
const whereMatch = /\bWHERE\b/i.exec(tail);
|
|
2336
2592
|
const orderMatch = /\bORDER\s+BY\b/i.exec(tail);
|
|
2337
2593
|
const limitMatch = /\bLIMIT\b/i.exec(tail);
|
|
@@ -2340,7 +2596,6 @@ var SimSqlEngine = class {
|
|
|
2340
2596
|
whereMatch.index + whereMatch[0].length,
|
|
2341
2597
|
indexAfter(whereMatch.index, [orderMatch, limitMatch, offsetMatch], tail)
|
|
2342
2598
|
);
|
|
2343
|
-
let rows = table.rows;
|
|
2344
2599
|
if (whereRaw.trim().length > 0) {
|
|
2345
2600
|
const clauses = parseWhereClauses(whereRaw, cursor);
|
|
2346
2601
|
rows = rows.filter((row) => matchWhere(clauses, row));
|
|
@@ -2350,9 +2605,9 @@ var SimSqlEngine = class {
|
|
|
2350
2605
|
orderMatch.index + orderMatch[0].length,
|
|
2351
2606
|
indexAfter(orderMatch.index, [limitMatch, offsetMatch], tail)
|
|
2352
2607
|
);
|
|
2353
|
-
const oc = /^\s*((?:"[^"]+")|([A-Za-z_][A-Za-z0-9_]*))\s+(asc|desc)/i.exec(orderRaw);
|
|
2608
|
+
const oc = /^\s*((?:"[^"]+")|([A-Za-z_][A-Za-z0-9_.]*))\s+(asc|desc)/i.exec(orderRaw);
|
|
2354
2609
|
if (oc !== null) {
|
|
2355
|
-
const col = ident(oc[1]);
|
|
2610
|
+
const col = unqualifyColumn(ident(oc[1]));
|
|
2356
2611
|
const dir = oc[3].toLowerCase();
|
|
2357
2612
|
rows = [...rows].toSorted((a, b) => {
|
|
2358
2613
|
const av = a[col];
|
|
@@ -2397,6 +2652,15 @@ function indexAfter(start, matches, tail) {
|
|
|
2397
2652
|
const end = candidates.length === 0 ? -1 : Math.min(...candidates);
|
|
2398
2653
|
return end === -1 ? tail.length : end;
|
|
2399
2654
|
}
|
|
2655
|
+
function parseSelectItem(raw) {
|
|
2656
|
+
const t = raw.trim();
|
|
2657
|
+
const as = /\s+AS\s+([A-Za-z_][A-Za-z0-9_]*)$/i.exec(t);
|
|
2658
|
+
let name = as === null ? t : t.slice(0, as.index).trim();
|
|
2659
|
+
if (unqualifyColumn(name) === "*") return { name: "", qualified: "", alias: "", star: true };
|
|
2660
|
+
const qualified = /^[A-Za-z_][A-Za-z0-9_]*\.[A-Za-z_][A-Za-z0-9_]*$/.test(name) ? ident(name) : "";
|
|
2661
|
+
const plain = unqualifyColumn(ident(name));
|
|
2662
|
+
return { name: plain, qualified, alias: as === null ? plain : ident(as[1] ?? ""), star: false };
|
|
2663
|
+
}
|
|
2400
2664
|
function project(rows, selectRaw) {
|
|
2401
2665
|
if (/^count\s*\(\s*\*/i.test(selectRaw) || /^count\s*\(\s*1\)/i.test(selectRaw)) {
|
|
2402
2666
|
const alias = selectRaw.match(/\bAS\s+([A-Za-z_][A-Za-z0-9_]*)/i)?.[1] ?? "n";
|
|
@@ -2407,10 +2671,21 @@ function project(rows, selectRaw) {
|
|
|
2407
2671
|
return [{ [constMatch[2]]: Number(constMatch[1]) }];
|
|
2408
2672
|
}
|
|
2409
2673
|
if (selectRaw.trim() === "*") return rows;
|
|
2410
|
-
const
|
|
2674
|
+
const items = selectRaw.split(",").map(parseSelectItem);
|
|
2675
|
+
const valueOf = (row, item) => item.qualified !== "" && row[item.qualified] !== void 0 ? row[item.qualified] : row[item.name];
|
|
2676
|
+
if (items.some((item) => item.star)) {
|
|
2677
|
+
return rows.map((row) => {
|
|
2678
|
+
const out = { ...row };
|
|
2679
|
+
for (const item of items) {
|
|
2680
|
+
if (item.star) continue;
|
|
2681
|
+
out[item.alias] = valueOf(row, item);
|
|
2682
|
+
}
|
|
2683
|
+
return out;
|
|
2684
|
+
});
|
|
2685
|
+
}
|
|
2411
2686
|
return rows.map((row) => {
|
|
2412
2687
|
const out = {};
|
|
2413
|
-
for (const
|
|
2688
|
+
for (const item of items) out[item.alias] = valueOf(row, item);
|
|
2414
2689
|
return out;
|
|
2415
2690
|
});
|
|
2416
2691
|
}
|
|
@@ -2552,6 +2827,34 @@ async function resolveFunctionSource(db, fn) {
|
|
|
2552
2827
|
function parseSnapshot(code) {
|
|
2553
2828
|
return JSON.parse(code);
|
|
2554
2829
|
}
|
|
2830
|
+
var SHARED_SOURCE_ROOT = "functions/_shared/";
|
|
2831
|
+
var FUNCTION_SHARED_DIR = "_shared/";
|
|
2832
|
+
async function resolveExecutableFiles(db, fn) {
|
|
2833
|
+
const base = await resolveFunctionSource(db, fn);
|
|
2834
|
+
if (base === null) return null;
|
|
2835
|
+
const isPublished = fn.publishedVersion !== null && fn.publishedVersion !== void 0;
|
|
2836
|
+
if (isPublished) return base;
|
|
2837
|
+
return withProjectSharedSource(db, fn.projectId, base);
|
|
2838
|
+
}
|
|
2839
|
+
async function readProjectShared(db, projectId) {
|
|
2840
|
+
if (projectId === void 0 || projectId === "" || db.projectSharedFiles === void 0) {
|
|
2841
|
+
return [];
|
|
2842
|
+
}
|
|
2843
|
+
return db.projectSharedFiles.listByProject(projectId);
|
|
2844
|
+
}
|
|
2845
|
+
async function withProjectSharedSource(db, projectId, files) {
|
|
2846
|
+
return mergeSharedSource(files, await readProjectShared(db, projectId));
|
|
2847
|
+
}
|
|
2848
|
+
function mergeSharedSource(files, shared) {
|
|
2849
|
+
const merged = { ...files };
|
|
2850
|
+
for (const file of shared) {
|
|
2851
|
+
if (!file.path.startsWith(SHARED_SOURCE_ROOT)) continue;
|
|
2852
|
+
const rel = file.path.slice(SHARED_SOURCE_ROOT.length);
|
|
2853
|
+
if (rel === "") continue;
|
|
2854
|
+
merged[`${FUNCTION_SHARED_DIR}${rel}`] = file.content;
|
|
2855
|
+
}
|
|
2856
|
+
return merged;
|
|
2857
|
+
}
|
|
2555
2858
|
|
|
2556
2859
|
// packages/runtime/src/state-objects/runtime.ts
|
|
2557
2860
|
var StateObject = class {
|
|
@@ -2857,11 +3160,14 @@ export {
|
|
|
2857
3160
|
createStorageCapability,
|
|
2858
3161
|
hmacSha256,
|
|
2859
3162
|
hmacSha256Hex,
|
|
3163
|
+
mergeSharedSource,
|
|
2860
3164
|
parseSnapshot,
|
|
2861
3165
|
parseStateBody,
|
|
2862
3166
|
parseStatePath,
|
|
3167
|
+
resolveExecutableFiles,
|
|
2863
3168
|
resolveFunctionSource,
|
|
2864
3169
|
sha256,
|
|
2865
3170
|
signDownloadUrl,
|
|
2866
|
-
verifyDownloadSignature
|
|
3171
|
+
verifyDownloadSignature,
|
|
3172
|
+
withProjectSharedSource
|
|
2867
3173
|
};
|
|
@@ -6,6 +6,14 @@
|
|
|
6
6
|
* 不 import `PlatformDb` / functions 域内部——消费方(gateway / mcp)传入自己的 db 即结构兼容。
|
|
7
7
|
*
|
|
8
8
|
* 纯函数 + 结构接口,零运行时依赖(规范 §4,shared 纯性)。
|
|
9
|
+
*
|
|
10
|
+
* ## 与 `resolveExecutableFiles` 的分工(FN-024)
|
|
11
|
+
*
|
|
12
|
+
* 本函数回答「**这份函数当前服务的源码是哪一份**」——版本切换语义只在这里,一处。
|
|
13
|
+
* `resolveExecutableFiles` 回答「**要拿去执行/发布的自包含文件集是什么**」——它在上面多叠一步
|
|
14
|
+
* 「草稿路径补项目共享源码」。两者分开是因为它们的变化原因不同:前者随发布/回滚变,
|
|
15
|
+
* 后者随共享源码模型变(SRC-004 / FN-024)。合成一个函数会让"版本切换"这条最敏感的语义
|
|
16
|
+
* 与"共享源码从哪来"耦合在一起,改任一边都要重新论证另一边。
|
|
9
17
|
*/
|
|
10
18
|
/** `resolveFunctionSource` 需要的最小 DB 读取面:只声明用到的两个仓库方法。结构兼容 `PlatformDb`。 */
|
|
11
19
|
export interface FunctionSourceDb {
|
|
@@ -42,3 +50,96 @@ export declare function resolveFunctionSource(db: FunctionSourceDb, fn: SourceFu
|
|
|
42
50
|
* 独立于此文件避免引入 functions 域(快照序列化/哈希在 functions/publish/snapshot)。
|
|
43
51
|
*/
|
|
44
52
|
export declare function parseSnapshot(code: string): Record<string, string>;
|
|
53
|
+
/**
|
|
54
|
+
* 「读项目共享源码」这一件事的最小读取面(SRC-004)。
|
|
55
|
+
*
|
|
56
|
+
* **可选**:缺省时调用方退化成"用草稿里现有的一切"(未装配 / 旧调用方 / 单测不受影响)。
|
|
57
|
+
* 单独抽出来是因为不是每个消费方都需要版本切换(如状态对象声明扫描只看开发期草稿),
|
|
58
|
+
* 它们只该依赖这一小块,而不是整套 `ExecutableSourceDb`。
|
|
59
|
+
*/
|
|
60
|
+
export interface ProjectSharedSourceDb {
|
|
61
|
+
readonly projectSharedFiles?: {
|
|
62
|
+
listByProject(projectId: string): Promise<ReadonlyArray<{
|
|
63
|
+
path: string;
|
|
64
|
+
content: string;
|
|
65
|
+
}>>;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/** `resolveExecutableFiles` 额外需要的最小读取面:项目级共享源码(SRC-004)。 */
|
|
69
|
+
export interface ExecutableSourceDb extends FunctionSourceDb, ProjectSharedSourceDb {
|
|
70
|
+
}
|
|
71
|
+
/** `resolveExecutableFiles` 需要的函数投影:在 `SourceFunction` 之上补 `projectId`。 */
|
|
72
|
+
export interface ExecutableSourceFunction extends SourceFunction {
|
|
73
|
+
/** 函数所属项目(读共享面用)。缺省时不合并共享源码。 */
|
|
74
|
+
projectId?: string;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 解析函数**可直接执行 / 打包**的自包含文件集(FN-024:全平台唯一收口点)。
|
|
78
|
+
*
|
|
79
|
+
* ## 为什么需要它
|
|
80
|
+
*
|
|
81
|
+
* SRC-004 之前,平台把项目级共享源码扇出成"每函数一份物化副本",于是**每个消费点都自己假设
|
|
82
|
+
* 草稿里已经有 `_shared/**` 了**——而这个假设没有任何地方强制(`writeShared` 没装配、或某条
|
|
83
|
+
* 写入路径绕过共享面路由时,副本就是漂的)。FN-024 停止扇出后,把这份假设换成这一处显式收口:
|
|
84
|
+
* 「要拿去执行的东西」只有一个来源。
|
|
85
|
+
*
|
|
86
|
+
* ## 两条分支的语义(**不可互换**)
|
|
87
|
+
*
|
|
88
|
+
* - **已发布**:直接返回快照文件集,**不合并共享面**。版本是不可变快照,必须继续服务它被发布那一刻
|
|
89
|
+
* 的代码——否则「回滚」不再可靠(回滚到 v1 却跑着新共享代码),且历史版本会随共享源码漂移。
|
|
90
|
+
* 前提是新发布的版本已经自带 `_shared/**`(见 `functions/publish/domain.ts` 的同一收口调用)。
|
|
91
|
+
* - **草稿**:以**项目共享面覆盖** `_shared/**`(同路径的以共享面为准),**并保留草稿自带的其它文件**。
|
|
92
|
+
* 这就是 SRC-004 扇出契约的那条规则("只覆盖 `_shared/**`"),没有新增语义。
|
|
93
|
+
*
|
|
94
|
+
* ## 为什么**只覆盖、不剥除**(一条被 e2e 打出来的结论)
|
|
95
|
+
*
|
|
96
|
+
* 曾经的写法是"先剥掉草稿里所有 `_shared/**`,再叠上共享面"——理由是"共享源码从 5 个减到 3 个时
|
|
97
|
+
* 旧副本不该变成幽灵"。那个理由本身成立,但**前提错了**:它假设"共享面是项目共享源码的完整表示",
|
|
98
|
+
* 而全平台至今不成立——`adep deploy`(CLI)与应用层(`apps/game`)都是**自描述草稿**:把
|
|
99
|
+
* `functions/_shared/**` 直接写在函数草稿里发布,**从不写项目共享面**(协议统一见 CLI 单独任务单)。
|
|
100
|
+
*
|
|
101
|
+
* 于是"剥除"对这类项目等于**删掉它唯一的共享源码**:函数随即在沙箱里解析不到 `./_shared/x`。
|
|
102
|
+
* 实测代价:`apps/game/e2e` 七条用例全部 500(`resolveExecutableFiles` 把一个带 3 份
|
|
103
|
+
* `_shared/**` 的草稿塌成只剩 `index.ts`)。而"幽灵副本"的危害是有限且自愈的(见下)。
|
|
104
|
+
*
|
|
105
|
+
* 所以判据是:**草稿自己声明的东西一律保留,共享面只获得"同路径覆盖权"**。两条已知残余都留给
|
|
106
|
+
* CLI 协议统一那单(那时草稿不再携带 `_shared/**`,"剥除"这件事本身就不需要了):
|
|
107
|
+
* 1. 存量双写草稿(SRC-004 期扇出留下)里,被共享面删掉的路径仍可见——直到该函数被 IDE 保存一次
|
|
108
|
+
* (IDE 写的是纯入口草稿:`functions/routes.ts` 的整组替换);
|
|
109
|
+
* 2. CLI 本地 `_shared/**` 与项目共享面**同名不同内容**时,共享面赢(即"本地改了但没同步就 deploy"
|
|
110
|
+
* 会跑到共享面的旧内容上)。两者都必须由协议统一解决,不能靠读取侧猜。
|
|
111
|
+
*
|
|
112
|
+
* ## 拿不到共享面读取面时**不动**草稿
|
|
113
|
+
*
|
|
114
|
+
* 读取面未装配 / 函数没 `projectId` → 视为空共享面 → 连覆盖都不做,原样返回
|
|
115
|
+
* (与 `resolveFunctionSource` 逐字相同,旧调用方零回归)。
|
|
116
|
+
*
|
|
117
|
+
* 反过来,**共享面为空不代表"项目没有共享源码"**——它也可能只是"这个项目还没进共享模型"
|
|
118
|
+
* (CLI / 应用层部署的项目就是这一种)。两者在数据上无从区分,而**按后者处理才是安全的**:
|
|
119
|
+
* 保留草稿自带的东西。宁可让一个已删文件多活一会儿,也不能让一类项目整体跑不起来。
|
|
120
|
+
*
|
|
121
|
+
* 函数自建的同名目录(`lib/x.ts`)与入口 `index.ts` **一律原样保留**:覆盖范围只认 `_shared/`。
|
|
122
|
+
*/
|
|
123
|
+
export declare function resolveExecutableFiles(db: ExecutableSourceDb, fn: ExecutableSourceFunction): Promise<Record<string, string> | null>;
|
|
124
|
+
/**
|
|
125
|
+
* 把一组**草稿文件**补成含项目共享源码的自包含文件集(收口点的可复用原语)。
|
|
126
|
+
*
|
|
127
|
+
* 与 `resolveExecutableFiles` 的分工:那个还负责"读哪一份源码"(版本切换);本函数只负责
|
|
128
|
+
* "把共享源码补进来"这一步。分开是因为**并非所有消费方都需要版本切换**——例如状态对象声明扫描
|
|
129
|
+
* (`functions/state-objects/host.ts`)看的是开发期草稿里的声明文件,与"当前服务哪个版本"无关,
|
|
130
|
+
* 它只需要这一步。
|
|
131
|
+
*
|
|
132
|
+
* @returns 共享面同路径的以共享面为准;入参自带的其它文件(含共享面没有的 `_shared/**`)原样保留。
|
|
133
|
+
*/
|
|
134
|
+
export declare function withProjectSharedSource(db: ProjectSharedSourceDb, projectId: string | undefined, files: Record<string, string>): Promise<Record<string, string>>;
|
|
135
|
+
/**
|
|
136
|
+
* 把共享面文件按**函数内相对路径**叠进文件集(`functions/_shared/x.ts` → `_shared/x.ts`)。
|
|
137
|
+
*
|
|
138
|
+
* 与 `server/domains/functions/shared-source/domain.ts::toFunctionRelativePath` 同规则;
|
|
139
|
+
* 这里不能 import 它(server 域 → shared 是反向依赖,规范 §4 禁),故就地实现一份**最短规则**,
|
|
140
|
+
* 并由 `shared/__tests__/function-source.test.ts` 的对照断言钉住两边一致。
|
|
141
|
+
*/
|
|
142
|
+
export declare function mergeSharedSource(files: Record<string, string>, shared: ReadonlyArray<{
|
|
143
|
+
path: string;
|
|
144
|
+
content: string;
|
|
145
|
+
}>): Record<string, string>;
|