@adep/runtime 0.3.15 → 0.3.17

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.
@@ -82,12 +82,35 @@ export declare function isDynamicImportRejection(error: unknown): boolean;
82
82
  */
83
83
  export declare function containsDynamicImport(source: string): boolean;
84
84
  /** 构造一个文件模块:返回其 exports(含入口的 default 处理器)。
85
- * 导出:CF-036 状态对象加载器(state-objects/loader.ts)复用同一精简 require 沙箱。 */
86
- export declare function loadModule(path: string, files: Readonly<Record<string, string>>, sandboxGlobals: Record<string, unknown>, cache: Map<string, Record<string, unknown>>, resolveBare?: BareSpecifierResolver, bareCache?: Map<string, unknown>, strictPaths?: boolean): Record<string, unknown>;
85
+ * 导出:CF-036 状态对象加载器(state-objects/loader.ts)复用同一精简 require 沙箱。
86
+ * hints(W2,可选):按路径下发的预编译产物——命中跳过转译/变换与完整 parse,
87
+ * cachedData 被拒静默回退全量编译;context 与模块实例仍每调用新建(隔离红线不变)。 */
88
+ export declare function loadModule(path: string, files: Readonly<Record<string, string>>, sandboxGlobals: Record<string, unknown>, cache: Map<string, Record<string, unknown>>, resolveBare?: BareSpecifierResolver, bareCache?: Map<string, unknown>, strictPaths?: boolean, hints?: Readonly<Record<string, {
89
+ js: string;
90
+ cachedData?: Uint8Array;
91
+ }>>): Record<string, unknown>;
87
92
  export interface LoadFunctionResult {
88
93
  /** 入口的 default 处理器(假设为 async (ctx) => result)。 */
89
94
  handler: (ctx: unknown) => unknown;
90
95
  }
96
+ /**
97
+ * 单文件预编译产物(W2 执行热路径缓存):transpile + 导出变换 + 沙箱 wrapper 包装 +
98
+ * `vm.Script` 预编译(`produceCachedData`)。产物(js 字符串 + cachedData Buffer)可经
99
+ * 结构化克隆跨线程下发,worker 侧用 `new vm.Script(js, { cachedData })` 跳过完整 parse。
100
+ * cachedData 与 Node/V8 版本相关 ⇒ 消费侧必须检查 `cachedDataRejected` 并回退全量编译。
101
+ */
102
+ export interface CompiledModuleCode {
103
+ /** 含 wrapper 前缀的完整脚本源码(与沙箱编译形态逐字节一致)。 */
104
+ js: string;
105
+ /** V8 code cache;宿主环境不支持时为 null(消费侧按无缓存处理)。 */
106
+ cachedData: Buffer | null;
107
+ }
108
+ export declare function compileModuleCode(source: string, filename: string): CompiledModuleCode;
91
109
  /** 装配入口函数:多文件 + 精简 require + 白名单全局;resolveBare 提供裸模块解析(FN-009,可选)。
92
- * strictPaths(CORE-154 `--parity`)= true 时 `../` 越根引用报错(缺省 false 行为不变)。 */
93
- export declare function loadFunctionModule(files: Readonly<Record<string, string>>, entry: string, sandboxGlobals: Record<string, unknown>, resolveBare?: BareSpecifierResolver, strictPaths?: boolean): LoadFunctionResult;
110
+ * strictPaths(CORE-154 `--parity`)= true 时 `../` 越根引用报错(缺省 false 行为不变)。
111
+ * hints(W2,可选):按文件路径下发的预编译产物;命中即跳过转译/变换与完整 parse,
112
+ * `cachedDataRejected` 时静默回退全量编译——语义逐字节不变(见 CompileHint 契约注释)。 */
113
+ export declare function loadFunctionModule(files: Readonly<Record<string, string>>, entry: string, sandboxGlobals: Record<string, unknown>, resolveBare?: BareSpecifierResolver, strictPaths?: boolean, hints?: Readonly<Record<string, {
114
+ js: string;
115
+ cachedData?: Uint8Array;
116
+ }>>): LoadFunctionResult;
package/dist/index.js CHANGED
@@ -47,10 +47,15 @@ var REALTIME_CAPABILITY_CODES = {
47
47
  subscriptionNotFound: "REALTIME_SUBSCRIPTION_NOT_FOUND",
48
48
  tooManySubscriptions: "REALTIME_TOO_MANY_SUBSCRIPTIONS",
49
49
  /** 线上独有:订阅授权策略(RT-002 `SubscriptionPolicy`)拒绝该 channel。 */
50
- channelDenied: "REALTIME_CHANNEL_DENIED"
50
+ channelDenied: "REALTIME_CHANNEL_DENIED",
51
+ /** 线上独有(RT-008):签票入参不合法(subject 缺失 / channels 非字符串数组 / TTL 越界)。 */
52
+ invalidArg: "REALTIME_INVALID_ARG",
53
+ /** 线上独有(RT-008):函数级签票超频(按项目固定窗口限流)。 */
54
+ ticketRateLimited: "REALTIME_TICKET_RATE_LIMITED"
51
55
  };
52
56
  var REALTIME_CAPABILITY_METHODS = [
53
57
  "publish",
58
+ "issueTicket",
54
59
  "subscribe",
55
60
  "receive",
56
61
  "unsubscribe"
@@ -311,7 +316,9 @@ var WorkerFunctionExecutor = class {
311
316
  ...input.env === void 0 ? {} : { env: input.env },
312
317
  ...deps === void 0 ? {} : { deps },
313
318
  ...this.parity ? { parity: true } : {},
314
- ...effectiveFetchConfig === void 0 ? {} : { fetch: effectiveFetchConfig }
319
+ ...effectiveFetchConfig === void 0 ? {} : { fetch: effectiveFetchConfig },
320
+ // W2:预编译提示随 workerData 下发(结构化克隆可跨线程;缺省不传 = 全量编译路径)。
321
+ ...input.compileHints === void 0 ? {} : { compileHints: input.compileHints }
315
322
  },
316
323
  resourceLimits: { maxOldGenerationSizeMb: memoryLimitMb }
317
324
  });
@@ -3078,6 +3085,33 @@ function createSimRealtimeCapability() {
3078
3085
  const [channel, data] = args;
3079
3086
  return sim.publish(channel, data);
3080
3087
  }
3088
+ case "issueTicket": {
3089
+ const [input] = args;
3090
+ if (typeof input !== "object" || input === null) {
3091
+ throw realtimeError(
3092
+ REALTIME_CODES.invalidArg,
3093
+ "\u7B7E\u53D1\u7968\u636E\u9700\u8981\u4E00\u4E2A\u8F93\u5165\u5BF9\u8C61 { subject, channels, ttlSeconds? }"
3094
+ );
3095
+ }
3096
+ const subject = input.subject;
3097
+ const channels = input.channels;
3098
+ if (typeof subject !== "string" || subject.length === 0) {
3099
+ throw realtimeError(
3100
+ REALTIME_CODES.invalidArg,
3101
+ "\u7B7E\u53D1\u7968\u636E\u5FC5\u987B\u5E26 subject\uFF08\u5E94\u7528\u4FA7\u7528\u6237\u6807\u8BC6\uFF09"
3102
+ );
3103
+ }
3104
+ if (!Array.isArray(channels) || channels.length === 0 || channels.some((c) => typeof c !== "string")) {
3105
+ throw realtimeError(
3106
+ REALTIME_CODES.invalidArg,
3107
+ "\u7B7E\u53D1\u7968\u636E\u5FC5\u987B\u5E26\u975E\u7A7A channels \u5B57\u7B26\u4E32\u6570\u7EC4\uFF08\u5E94\u7528\u7528\u6237\u7684\u7CBE\u786E\u8BA2\u9605\u767D\u540D\u5355\uFF09"
3108
+ );
3109
+ }
3110
+ throw realtimeError(
3111
+ REALTIME_CODES.unsupportedArg,
3112
+ "\u672C\u5730 sim \u4E0D\u7B7E\u53D1\u5B9E\u65F6\u7968\u636E\uFF08\u65E0 WS \u63E1\u624B\u9762\uFF0C\u7968\u636E\u65E0\u5904\u53EF\u7528\uFF09\uFF1B\u8BF7\u5728\u7EBF\u4E0A\u7684\u51FD\u6570\u6267\u884C\u9762\u4F7F\u7528 cloud.realtime.issueTicket"
3113
+ );
3114
+ }
3081
3115
  case "subscribe": {
3082
3116
  if (buffers.size >= MAX_SUBSCRIPTIONS) {
3083
3117
  throw realtimeError(
@@ -3297,13 +3331,32 @@ var StateObjectRuntime = class {
3297
3331
  }
3298
3332
  /**
3299
3333
  * alarm 轮询:把到期对象唤醒并调 onAlarm(CF-037)。
3334
+ * - 到期集合 = 后端落库到期记录 ∪ **驻留实例的内存 alarmAt(驻留时以内存值为准)**:
3335
+ * `setAlarm`/`clearAlarm` 只写内存、`alarmAt` 随 `flush()` 落库——若 pump 只读后端,
3336
+ * 「setAlarm 后没 flush」会静默丢调度(rift-brawl 实锤:先 flush 再 setAlarm,闹钟永不触发),
3337
+ * 反向「clearAlarm 后没 flush」会因 DB 残留到期值误触发。重启续接仍以落库值为准
3338
+ * (实例不驻留时后端值是唯一事实;要重启不丢 alarm,方法内仍须 flush——见 features/state-objects.md)。
3300
3339
  * - onAlarm 后统一持久化(flush):onAlarm 内改的 state 与 setAlarm 重调度 / clearAlarm
3301
3340
  * 的新 alarmAt 都落库;
3302
3341
  * - 单对象 onAlarm 抛错**不阻塞**同 tick 其余对象:失败对象不落库、alarmAt 保留
3303
3342
  * → 下轮 dueAlarms 仍命中重试;错误折叠进 failed(不泄漏内部栈)。
3304
3343
  */
3305
3344
  async pump(now) {
3306
- const due = await this.backend.dueAlarms(now);
3345
+ const dueMap = /* @__PURE__ */ new Map();
3346
+ for (const d of await this.backend.dueAlarms(now)) {
3347
+ dueMap.set(this.key(d.type, d.id), d);
3348
+ }
3349
+ for (const [k, inst] of this.instances) {
3350
+ const typeName = inst.constructor.typeName;
3351
+ if (typeName === void 0) continue;
3352
+ const raw = inst;
3353
+ if (raw.alarmAt === null || raw.alarmAt > now) {
3354
+ dueMap.delete(k);
3355
+ continue;
3356
+ }
3357
+ dueMap.set(k, { type: typeName, id: inst.id, alarmAt: raw.alarmAt });
3358
+ }
3359
+ const due = [...dueMap.values()].toSorted((a, b) => a.alarmAt - b.alarmAt);
3307
3360
  const fired = [];
3308
3361
  const failed = [];
3309
3362
  for (const d of due) {
@@ -41,7 +41,11 @@ export declare const REALTIME_CAPABILITY_CODES: {
41
41
  readonly tooManySubscriptions: "REALTIME_TOO_MANY_SUBSCRIPTIONS";
42
42
  /** 线上独有:订阅授权策略(RT-002 `SubscriptionPolicy`)拒绝该 channel。 */
43
43
  readonly channelDenied: "REALTIME_CHANNEL_DENIED";
44
+ /** 线上独有(RT-008):签票入参不合法(subject 缺失 / channels 非字符串数组 / TTL 越界)。 */
45
+ readonly invalidArg: "REALTIME_INVALID_ARG";
46
+ /** 线上独有(RT-008):函数级签票超频(按项目固定窗口限流)。 */
47
+ readonly ticketRateLimited: "REALTIME_TICKET_RATE_LIMITED";
44
48
  };
45
49
  /** 命令通道可派发的 realtime 方法名(未知方法一律 fail loud)。 */
46
- export declare const REALTIME_CAPABILITY_METHODS: readonly ["publish", "subscribe", "receive", "unsubscribe"];
50
+ export declare const REALTIME_CAPABILITY_METHODS: readonly ["publish", "issueTicket", "subscribe", "receive", "unsubscribe"];
47
51
  export type RealtimeCapabilityMethod = (typeof REALTIME_CAPABILITY_METHODS)[number];
@@ -39,6 +39,8 @@ export declare const REALTIME_CODES: {
39
39
  readonly subscriptionNotFound: "REALTIME_SUBSCRIPTION_NOT_FOUND";
40
40
  readonly tooManySubscriptions: "REALTIME_TOO_MANY_SUBSCRIPTIONS";
41
41
  readonly channelDenied: "REALTIME_CHANNEL_DENIED";
42
+ readonly invalidArg: "REALTIME_INVALID_ARG";
43
+ readonly ticketRateLimited: "REALTIME_TICKET_RATE_LIMITED";
42
44
  };
43
45
  /** `subscribe` 的回执(函数侧只拿到订阅号,拿不到监听器 / 总线内部对象)。 */
44
46
  export interface SimRealtimeSubscription {
@@ -101,6 +101,11 @@ export declare class StateObjectRuntime {
101
101
  flush<State extends Record<string, unknown>>(inst: StateObject<State>): Promise<void>;
102
102
  /**
103
103
  * alarm 轮询:把到期对象唤醒并调 onAlarm(CF-037)。
104
+ * - 到期集合 = 后端落库到期记录 ∪ **驻留实例的内存 alarmAt(驻留时以内存值为准)**:
105
+ * `setAlarm`/`clearAlarm` 只写内存、`alarmAt` 随 `flush()` 落库——若 pump 只读后端,
106
+ * 「setAlarm 后没 flush」会静默丢调度(rift-brawl 实锤:先 flush 再 setAlarm,闹钟永不触发),
107
+ * 反向「clearAlarm 后没 flush」会因 DB 残留到期值误触发。重启续接仍以落库值为准
108
+ * (实例不驻留时后端值是唯一事实;要重启不丢 alarm,方法内仍须 flush——见 features/state-objects.md)。
104
109
  * - onAlarm 后统一持久化(flush):onAlarm 内改的 state 与 setAlarm 重调度 / clearAlarm
105
110
  * 的新 alarmAt 都落库;
106
111
  * - 单对象 onAlarm 抛错**不阻塞**同 tick 其余对象:失败对象不落库、alarmAt 保留
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  },
8
8
  "homepage": "https://adep.top",
9
9
  "bugs": "https://gitcode.com/yunqiang_wu/ai-yunke/issues",
10
- "version": "0.3.15",
10
+ "version": "0.3.17",
11
11
  "private": false,
12
12
  "type": "module",
13
13
  "sideEffects": false,
@@ -32,7 +32,7 @@
32
32
  "dist"
33
33
  ],
34
34
  "dependencies": {
35
- "@adep/types": "0.4.4"
35
+ "@adep/types": "0.4.6"
36
36
  },
37
37
  "scripts": {
38
38
  "test": "vitest run",