@adep/runtime 0.2.4 → 0.2.6

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 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
@@ -1292,7 +1292,7 @@ var rotr = (x, n) => x >>> n | x << 32 - n;
1292
1292
  function compress(h, block, w) {
1293
1293
  for (let i = 0; i < 16; i += 1) {
1294
1294
  const j = i * 4;
1295
- w[i] = (block[j] ?? 0) << 24 | (block[j + 1] ?? 0) << 16 | (block[j + 2] ?? 0) << 8 | (block[j + 3] ?? 0);
1295
+ w[i] = block[j] << 24 | block[j + 1] << 16 | block[j + 2] << 8 | block[j + 3];
1296
1296
  }
1297
1297
  for (let i = 16; i < 64; i += 1) {
1298
1298
  const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ w[i - 15] >>> 3;
@@ -2014,6 +2014,10 @@ function toNumber(raw) {
2014
2014
  const n = Number(t);
2015
2015
  return Number.isNaN(n) ? null : n;
2016
2016
  }
2017
+ function arithmeticOperand(v) {
2018
+ const n = Number(v);
2019
+ return Number.isFinite(n) ? n : 0;
2020
+ }
2017
2021
  function stripQuotes(raw) {
2018
2022
  const t = raw.trim();
2019
2023
  if (/^'.*'$/.test(t)) return t.slice(1, -1);
@@ -2183,10 +2187,6 @@ function resolveSetValue(value, existing, row) {
2183
2187
  return value;
2184
2188
  }
2185
2189
  function evalArithmetic(src, existing, row) {
2186
- const num = (v) => {
2187
- const n2 = Number(v);
2188
- return Number.isFinite(n2) ? n2 : 0;
2189
- };
2190
2190
  let i = 0;
2191
2191
  const n = src.length;
2192
2192
  const skip = () => {
@@ -2231,7 +2231,7 @@ function evalArithmetic(src, existing, row) {
2231
2231
  const id = parseIdent();
2232
2232
  if (id !== null) {
2233
2233
  const v = id.excluded ? row[id.col] : existing[id.col];
2234
- return num(v);
2234
+ return arithmeticOperand(v);
2235
2235
  }
2236
2236
  return 0;
2237
2237
  };
@@ -2661,6 +2661,9 @@ function parseSelectItem(raw) {
2661
2661
  const plain = unqualifyColumn(ident(name));
2662
2662
  return { name: plain, qualified, alias: as === null ? plain : ident(as[1] ?? ""), star: false };
2663
2663
  }
2664
+ function projectValueOf(row, item) {
2665
+ return item.qualified !== "" && row[item.qualified] !== void 0 ? row[item.qualified] : row[item.name];
2666
+ }
2664
2667
  function project(rows, selectRaw) {
2665
2668
  if (/^count\s*\(\s*\*/i.test(selectRaw) || /^count\s*\(\s*1\)/i.test(selectRaw)) {
2666
2669
  const alias = selectRaw.match(/\bAS\s+([A-Za-z_][A-Za-z0-9_]*)/i)?.[1] ?? "n";
@@ -2672,20 +2675,19 @@ function project(rows, selectRaw) {
2672
2675
  }
2673
2676
  if (selectRaw.trim() === "*") return rows;
2674
2677
  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
2678
  if (items.some((item) => item.star)) {
2677
2679
  return rows.map((row) => {
2678
2680
  const out = { ...row };
2679
2681
  for (const item of items) {
2680
2682
  if (item.star) continue;
2681
- out[item.alias] = valueOf(row, item);
2683
+ out[item.alias] = projectValueOf(row, item);
2682
2684
  }
2683
2685
  return out;
2684
2686
  });
2685
2687
  }
2686
2688
  return rows.map((row) => {
2687
2689
  const out = {};
2688
- for (const item of items) out[item.alias] = valueOf(row, item);
2690
+ for (const item of items) out[item.alias] = projectValueOf(row, item);
2689
2691
  return out;
2690
2692
  });
2691
2693
  }
@@ -2827,6 +2829,34 @@ async function resolveFunctionSource(db, fn) {
2827
2829
  function parseSnapshot(code) {
2828
2830
  return JSON.parse(code);
2829
2831
  }
2832
+ var SHARED_SOURCE_ROOT = "functions/_shared/";
2833
+ var FUNCTION_SHARED_DIR = "_shared/";
2834
+ async function resolveExecutableFiles(db, fn) {
2835
+ const base = await resolveFunctionSource(db, fn);
2836
+ if (base === null) return null;
2837
+ const isPublished = fn.publishedVersion !== null && fn.publishedVersion !== void 0;
2838
+ if (isPublished) return base;
2839
+ return withProjectSharedSource(db, fn.projectId, base);
2840
+ }
2841
+ async function readProjectShared(db, projectId) {
2842
+ if (projectId === void 0 || projectId === "" || db.projectSharedFiles === void 0) {
2843
+ return [];
2844
+ }
2845
+ return db.projectSharedFiles.listByProject(projectId);
2846
+ }
2847
+ async function withProjectSharedSource(db, projectId, files) {
2848
+ return mergeSharedSource(files, await readProjectShared(db, projectId));
2849
+ }
2850
+ function mergeSharedSource(files, shared) {
2851
+ const merged = { ...files };
2852
+ for (const file of shared) {
2853
+ if (!file.path.startsWith(SHARED_SOURCE_ROOT)) continue;
2854
+ const rel = file.path.slice(SHARED_SOURCE_ROOT.length);
2855
+ if (rel === "") continue;
2856
+ merged[`${FUNCTION_SHARED_DIR}${rel}`] = file.content;
2857
+ }
2858
+ return merged;
2859
+ }
2830
2860
 
2831
2861
  // packages/runtime/src/state-objects/runtime.ts
2832
2862
  var StateObject = class {
@@ -3132,11 +3162,14 @@ export {
3132
3162
  createStorageCapability,
3133
3163
  hmacSha256,
3134
3164
  hmacSha256Hex,
3165
+ mergeSharedSource,
3135
3166
  parseSnapshot,
3136
3167
  parseStateBody,
3137
3168
  parseStatePath,
3169
+ resolveExecutableFiles,
3138
3170
  resolveFunctionSource,
3139
3171
  sha256,
3140
3172
  signDownloadUrl,
3141
- verifyDownloadSignature
3173
+ verifyDownloadSignature,
3174
+ withProjectSharedSource
3142
3175
  };
@@ -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>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adep/runtime",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "sideEffects": false,