@adep/runtime 0.2.2 → 0.2.4

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.js CHANGED
@@ -1207,6 +1207,19 @@ function assertSafeStoragePath(path) {
1207
1207
  }
1208
1208
  return path;
1209
1209
  }
1210
+ function assertStoragePrefix(prefix) {
1211
+ if (prefix !== void 0 && prefix !== "") {
1212
+ const segments = prefix.split("/");
1213
+ if (prefix.startsWith("/") || /[\\]/.test(prefix) || segments.some((segment) => segment === "..")) {
1214
+ throw new StorageError(
1215
+ 400,
1216
+ STORAGE_CODES.invalidPath,
1217
+ `\u975E\u6CD5\u524D\u7F00 "${prefix}"\uFF08\u7981\u6B62 / \u5F00\u5934\u3001\u53CD\u659C\u6760\u6216 .. \u7A7F\u8D8A\uFF09`
1218
+ );
1219
+ }
1220
+ }
1221
+ return prefix ?? "";
1222
+ }
1210
1223
 
1211
1224
  // packages/runtime/src/storage/hmac-sha256.ts
1212
1225
  var K = new Uint32Array([
@@ -1614,16 +1627,7 @@ function createLocalStorageDriver(options) {
1614
1627
  await unlink(metaPath(projectId, path)).catch(() => void 0);
1615
1628
  },
1616
1629
  async list(projectId, prefix) {
1617
- if (prefix !== void 0 && prefix !== "") {
1618
- const segments = prefix.split("/");
1619
- if (prefix.startsWith("/") || /[\\]/.test(prefix) || segments.some((segment) => segment === "..")) {
1620
- throw new StorageError(
1621
- 400,
1622
- STORAGE_CODES.invalidPath,
1623
- `\u975E\u6CD5\u524D\u7F00 "${prefix}"\uFF08\u7981\u6B62 / \u5F00\u5934\u3001\u53CD\u659C\u6760\u6216 .. \u7A7F\u8D8A\uFF09`
1624
- );
1625
- }
1626
- }
1630
+ assertStoragePrefix(prefix);
1627
1631
  const root = bucketDir(projectId);
1628
1632
  const metas = [];
1629
1633
  const walk = async (dir, relative) => {
@@ -1803,19 +1807,6 @@ var mapError = async (res, message) => {
1803
1807
  `\u5BF9\u8C61\u5B58\u50A8\u540E\u7AEF\u9519\u8BEF\uFF08HTTP ${res.status} ${code || ""}\uFF09\uFF1A${message}`
1804
1808
  );
1805
1809
  };
1806
- var assertPrefix = (prefix) => {
1807
- if (prefix !== void 0 && prefix !== "") {
1808
- const segments = prefix.split("/");
1809
- if (prefix.startsWith("/") || /[\\]/.test(prefix) || segments.some((segment) => segment === "..")) {
1810
- throw new StorageError(
1811
- 400,
1812
- STORAGE_CODES.invalidPath,
1813
- `\u975E\u6CD5\u524D\u7F00 "${prefix}"\uFF08\u7981\u6B62 / \u5F00\u5934\u3001\u53CD\u659C\u6760\u6216 .. \u7A7F\u8D8A\uFF09`
1814
- );
1815
- }
1816
- }
1817
- return prefix ?? "";
1818
- };
1819
1810
  function createS3StorageDriver(options) {
1820
1811
  const region = options.region ?? DEFAULT_REGION;
1821
1812
  const quotaBytes = options.quotaBytes ?? PROJECT_QUOTA_BYTES;
@@ -1976,7 +1967,7 @@ function createS3StorageDriver(options) {
1976
1967
  if (!res.ok) await mapError(res, `\u6587\u4EF6 "${path}" \u4E0D\u5B58\u5728\u6216\u4E0D\u53EF\u5220\u9664`);
1977
1968
  },
1978
1969
  async list(projectId, prefix) {
1979
- const normalized = assertPrefix(prefix);
1970
+ const normalized = assertStoragePrefix(prefix);
1980
1971
  const keys = await listAllKeys(`${projectId}/${normalized}`);
1981
1972
  const metas = await mapLimit(keys, HEAD_CONCURRENCY, async (entry) => {
1982
1973
  const path = entry.key.slice(projectId.length + 1);
@@ -2027,6 +2018,7 @@ function stripQuotes(raw) {
2027
2018
  const t = raw.trim();
2028
2019
  if (/^'.*'$/.test(t)) return t.slice(1, -1);
2029
2020
  if (/^".*"$/.test(t)) return t.slice(1, -1);
2021
+ if (/^NULL$/i.test(t)) return null;
2030
2022
  return t;
2031
2023
  }
2032
2024
  function parseCreateTable(ddl) {
@@ -2047,50 +2039,109 @@ function parseCreateTable(ddl) {
2047
2039
  }
2048
2040
  return { name, columns };
2049
2041
  }
2050
- function parseWhereClauses(whereRaw, cursor) {
2051
- const clauses = [];
2052
- for (const part of whereRaw.split(/\s+AND\s+/i)) {
2053
- const t = part.trim();
2054
- if (t.length === 0) continue;
2055
- if (/\bIS\s+NULL\b/i.test(t)) {
2056
- const col2 = ident(t.split(/\s+IS\s+NULL\b/i)[0] ?? "");
2057
- clauses.push({ column: col2, operator: "IS NULL", value: null, list: false });
2058
- continue;
2059
- }
2060
- if (/\bIS\s+NOT\s+NULL\b/i.test(t)) {
2061
- const col2 = ident(t.split(/\s+IS\s+NOT\s+NULL\b/i)[0] ?? "");
2062
- clauses.push({ column: col2, operator: "IS NOT NULL", value: null, list: false });
2063
- continue;
2064
- }
2065
- const opMatch = /^\s*((?:"[^"]+")|([A-Za-z_][A-Za-z0-9_]*))\s*(=|!=|>=|<=|>|<|like|in|not\s+in)\s*(.+)$/i.exec(
2066
- t
2067
- );
2068
- if (opMatch === null) continue;
2069
- const col = ident(opMatch[1] ?? "");
2070
- const op = (opMatch[3] ?? opMatch[2] ?? "").toLowerCase();
2071
- const rhs = (opMatch[4] ?? "").trim();
2072
- if (op === "in" || op === "not in") {
2073
- const inner = rhs.replace(/^\(|\)$/g, "");
2074
- const items = splitListItems(inner);
2075
- const list = items.map((item) => {
2076
- if (item.trim() === "?") return cursor.take();
2077
- return stripQuotes(item);
2078
- });
2079
- 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;
2080
2065
  continue;
2081
2066
  }
2082
- let value;
2083
- if (rhs === "?") {
2084
- value = cursor.take();
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;
2085
2073
  } else {
2086
- value = stripQuotes(rhs);
2074
+ buffer += ch;
2087
2075
  }
2088
- clauses.push({ column: col, operator: op, value, list: false });
2089
2076
  }
2090
- return clauses;
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);
2091
2142
  }
2092
- function matchWhere(clauses, row) {
2093
- 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)));
2094
2145
  }
2095
2146
  function splitListItems(inner) {
2096
2147
  const items = [];
@@ -2110,6 +2161,168 @@ function splitListItems(inner) {
2110
2161
  items.push(buffer);
2111
2162
  return items;
2112
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
+ }
2113
2326
  function matchValue(value, clause) {
2114
2327
  const op = clause.operator;
2115
2328
  if (op === "IS NULL") return value === null || value === void 0;
@@ -2237,6 +2450,17 @@ var SimSqlEngine = class {
2237
2450
  }
2238
2451
  return { changes: 0 };
2239
2452
  }
2453
+ if (/^CREATE\s+(?:UNIQUE\s+)?INDEX\s+(?:IF\s+NOT\s+EXISTS\s+)?/i.test(stmt)) {
2454
+ const idx = /^CREATE\s+(?:UNIQUE\s+)?INDEX\s+(?:IF\s+NOT\s+EXISTS\s+)?((?:"[^"]+")|([A-Za-z_][A-Za-z0-9_]*))\s+ON\s+((?:"[^"]+")|([A-Za-z_][A-Za-z0-9_]*))/i.exec(
2455
+ stmt
2456
+ );
2457
+ if (idx === null) throw new SimDbError("DB_UNSAFE_OP", `\u65E0\u6CD5\u89E3\u6790 CREATE INDEX \u8BED\u53E5`);
2458
+ const tableName = ident(idx[3]);
2459
+ if (this.tables[tableName] === void 0) {
2460
+ throw new SimDbError("DB_UNSAFE_OP", `CREATE INDEX \u76EE\u6807\u8868\u4E0D\u5B58\u5728\uFF1A${tableName}`);
2461
+ }
2462
+ return { changes: 0 };
2463
+ }
2240
2464
  if (/^INSERT\s+INTO/i.test(stmt)) return this.execInsert(stmt, params);
2241
2465
  if (/^UPDATE\s+/i.test(stmt)) return this.execUpdate(stmt, params);
2242
2466
  if (/^DELETE\s+FROM/i.test(stmt)) return this.execDelete(stmt, params);
@@ -2252,16 +2476,36 @@ var SimSqlEngine = class {
2252
2476
  const valueBody = match[4].trim();
2253
2477
  const cursor = new ParamCursor(params);
2254
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
+ );
2255
2486
  let inserted = 0;
2256
- for (const group of splitListItems(valueBody)) {
2257
- const inner = group.trim().replace(/^\(|\)$/g, "");
2258
- const values = splitListItems(inner).map(
2487
+ for (const tuple of tuples) {
2488
+ const values = splitListItems(tuple).map(
2259
2489
  (item) => item.trim() === "?" ? cursor.take() : stripQuotes(item)
2260
2490
  );
2261
2491
  const row = {};
2262
2492
  cols.forEach((col, i) => {
2263
2493
  row[col] = values[i] ?? null;
2264
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
+ }
2265
2509
  this.applyAutoincrement(table, row);
2266
2510
  table.rows.push(row);
2267
2511
  inserted += 1;
@@ -2330,6 +2574,20 @@ var SimSqlEngine = class {
2330
2574
  return project(this.catalog(), selectRaw);
2331
2575
  }
2332
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
+ }
2333
2591
  const whereMatch = /\bWHERE\b/i.exec(tail);
2334
2592
  const orderMatch = /\bORDER\s+BY\b/i.exec(tail);
2335
2593
  const limitMatch = /\bLIMIT\b/i.exec(tail);
@@ -2338,7 +2596,6 @@ var SimSqlEngine = class {
2338
2596
  whereMatch.index + whereMatch[0].length,
2339
2597
  indexAfter(whereMatch.index, [orderMatch, limitMatch, offsetMatch], tail)
2340
2598
  );
2341
- let rows = table.rows;
2342
2599
  if (whereRaw.trim().length > 0) {
2343
2600
  const clauses = parseWhereClauses(whereRaw, cursor);
2344
2601
  rows = rows.filter((row) => matchWhere(clauses, row));
@@ -2348,9 +2605,9 @@ var SimSqlEngine = class {
2348
2605
  orderMatch.index + orderMatch[0].length,
2349
2606
  indexAfter(orderMatch.index, [limitMatch, offsetMatch], tail)
2350
2607
  );
2351
- 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);
2352
2609
  if (oc !== null) {
2353
- const col = ident(oc[1]);
2610
+ const col = unqualifyColumn(ident(oc[1]));
2354
2611
  const dir = oc[3].toLowerCase();
2355
2612
  rows = [...rows].toSorted((a, b) => {
2356
2613
  const av = a[col];
@@ -2395,6 +2652,15 @@ function indexAfter(start, matches, tail) {
2395
2652
  const end = candidates.length === 0 ? -1 : Math.min(...candidates);
2396
2653
  return end === -1 ? tail.length : end;
2397
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
+ }
2398
2664
  function project(rows, selectRaw) {
2399
2665
  if (/^count\s*\(\s*\*/i.test(selectRaw) || /^count\s*\(\s*1\)/i.test(selectRaw)) {
2400
2666
  const alias = selectRaw.match(/\bAS\s+([A-Za-z_][A-Za-z0-9_]*)/i)?.[1] ?? "n";
@@ -2405,10 +2671,21 @@ function project(rows, selectRaw) {
2405
2671
  return [{ [constMatch[2]]: Number(constMatch[1]) }];
2406
2672
  }
2407
2673
  if (selectRaw.trim() === "*") return rows;
2408
- const cols = selectRaw.split(",").map((s) => ident(s.trim()));
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
+ }
2409
2686
  return rows.map((row) => {
2410
2687
  const out = {};
2411
- for (const col of cols) out[col] = row[col];
2688
+ for (const item of items) out[item.alias] = valueOf(row, item);
2412
2689
  return out;
2413
2690
  });
2414
2691
  }
@@ -13,4 +13,5 @@ export interface S3StorageOptions {
13
13
  /** BILL-005:项目级配额回调(billing 域注入)。 */
14
14
  quotaCheck?: (projectId: string, projectedBytes: number) => Promise<void>;
15
15
  }
16
+ /** 前缀合法性校验(list/usage 用):实现已上浮到 driver.ts 的 `assertStoragePrefix`(与 local 同一份)。 */
16
17
  export declare function createS3StorageDriver(options: S3StorageOptions): StorageDriver;
@@ -64,3 +64,12 @@ export declare const STORAGE_CODES: {
64
64
  * 合法路径为单层或多层相对段(如 `a`、`a/b.png`),每段不能是 `.`/`..`、空或含路径分隔符。
65
65
  */
66
66
  export declare function assertSafeStoragePath(path: string): string;
67
+ /**
68
+ * `list` / `usage` 的**前缀**校验:前缀不是完整路径,尾部空段(`a/` 表示目录 a 下全部)合法,
69
+ * 故不做 `assertSafeStoragePath` 的全段校验,只拦穿越风险(`/` 开头、反斜杠、`..` 段)。
70
+ *
71
+ * 去重背景:local / s3 / 浏览器 sim 三个驱动此前各写一份**逐字相同**的 12 行实现
72
+ * (s3 侧已抽成局部 `assertPrefix`,另两处没有复用)。这是路径穿越防线,任何一份被单独
73
+ * 放宽就是漏洞——必须只有一份。
74
+ */
75
+ export declare function assertStoragePrefix(prefix: string | undefined): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adep/runtime",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -25,7 +25,7 @@
25
25
  "dist"
26
26
  ],
27
27
  "dependencies": {
28
- "@adep/types": "0.2.2"
28
+ "@adep/types": "0.2.3"
29
29
  },
30
30
  "scripts": {
31
31
  "test": "vitest run",