@rebon/cli-darwin-arm64 0.17.3 → 0.19.0
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/bin/compose-runtime/package.json +17 -0
- package/bin/compose-runtime/payload/compose/credentials-runtime.js +38 -0
- package/bin/compose-runtime/payload/compose/llm-runtime.js +330 -0
- package/bin/compose-runtime/payload/compose/loop-assembly.js +149 -0
- package/bin/compose-runtime/payload/compose/serve-dispatch.js +82 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-anonymous-user-id.js +10 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-credentials.js +13 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-launch-environment.js +21 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-llm.js +313 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-settings.js +43 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-tools.js +20 -0
- package/bin/compose-runtime/payload/compose/shims/dsh-web.js +10 -0
- package/bin/compose-runtime/payload/compose/shims/llm-adapter.js +32 -0
- package/bin/compose-runtime/payload/compose/shims/zod-lite.js +49 -0
- package/bin/compose-runtime/payload/compose/systemprompt-runtime.js +74 -0
- package/bin/compose-runtime/payload/compose/tools-runtime.js +228 -0
- package/bin/compose-runtime/payload/compose/web-runtime.js +204 -0
- package/bin/compose-runtime/payload/vendor/cordis/index.js +1530 -0
- package/bin/compose-runtime/payload/vendor/cosmokit/LICENSE +21 -0
- package/bin/compose-runtime/payload/vendor/cosmokit/index.mjs +357 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-agent +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-agent-loop +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-llm-core +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-llm-deepseek +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-logger-console +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-scope +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-session +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-system-prompt +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-timeout +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-timer +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-tool-todo +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-tool-web +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-tools-schema +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/LICENSE-web-search-exa +21 -0
- package/bin/compose-runtime/payload/vendor/dsh/agent-loop.js +1193 -0
- package/bin/compose-runtime/payload/vendor/dsh/agent.js +714 -0
- package/bin/compose-runtime/payload/vendor/dsh/llm-core.js +269 -0
- package/bin/compose-runtime/payload/vendor/dsh/llm-deepseek.js +672 -0
- package/bin/compose-runtime/payload/vendor/dsh/logger-console.js +83 -0
- package/bin/compose-runtime/payload/vendor/dsh/scope.js +287 -0
- package/bin/compose-runtime/payload/vendor/dsh/session.js +1668 -0
- package/bin/compose-runtime/payload/vendor/dsh/system-prompt.js +309 -0
- package/bin/compose-runtime/payload/vendor/dsh/timeout.js +100 -0
- package/bin/compose-runtime/payload/vendor/dsh/timer.js +128 -0
- package/bin/compose-runtime/payload/vendor/dsh/tool-todo.js +143 -0
- package/bin/compose-runtime/payload/vendor/dsh/tool-web.js +1527 -0
- package/bin/compose-runtime/payload/vendor/dsh/tools-schema.js +849 -0
- package/bin/compose-runtime/payload/vendor/dsh/web-search-exa.js +122 -0
- package/bin/compose-runtime/payload/vendor/eventsource-parser/LICENSE +21 -0
- package/bin/compose-runtime/payload/vendor/eventsource-parser/index.js +177 -0
- package/bin/compose-runtime/payload/vendor/eventsource-parser/stream.js +48 -0
- package/bin/compose-runtime/payload/vendor/schemastery/index.mjs +656 -0
- package/bin/compose-runtime/payload-manifests.json +48 -0
- package/bin/compose-runtime/src/bridge.mjs +142 -0
- package/bin/compose-runtime/src/credentials-runtime.mjs +50 -0
- package/bin/compose-runtime/src/eventsource-stream.mjs +48 -0
- package/bin/compose-runtime/src/index.mjs +7 -0
- package/bin/compose-runtime/src/llm-runtime.mjs +326 -0
- package/bin/compose-runtime/src/loader.mjs +78 -0
- package/bin/compose-runtime/src/loop-assembly.mjs +237 -0
- package/bin/compose-runtime/src/payload.mjs +49 -0
- package/bin/compose-runtime/src/plugin.mjs +59 -0
- package/bin/compose-runtime/src/realm.mjs +223 -0
- package/bin/compose-runtime/src/registry.mjs +191 -0
- package/bin/compose-runtime/src/resolve.mjs +115 -0
- package/bin/compose-runtime/src/systemprompt-runtime.mjs +67 -0
- package/bin/compose-runtime/src/tools-runtime.mjs +239 -0
- package/bin/compose-runtime/src/web-runtime.mjs +209 -0
- package/bin/plugin-host/package.json +11 -0
- package/bin/plugin-host/src/bridge.mjs +64 -0
- package/bin/plugin-host/src/cli.mjs +105 -0
- package/bin/plugin-host/src/framing.mjs +113 -0
- package/bin/plugin-host/src/host.mjs +624 -0
- package/bin/plugin-host/src/json.mjs +196 -0
- package/bin/plugin-host/src/lifecycle.mjs +114 -0
- package/bin/plugin-host/src/loader.mjs +142 -0
- package/bin/plugin-host/src/methods.mjs +256 -0
- package/bin/plugin-host/src/protocol.mjs +129 -0
- package/bin/plugin-host/src/sdk.mjs +7 -0
- package/bin/rebon +0 -0
- package/bin/rebon-boa-helper +0 -0
- package/package.json +4 -1
|
@@ -0,0 +1,1530 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/events.ts
|
|
5
|
+
import { defineProperty as defineProperty2 } from "cosmokit";
|
|
6
|
+
|
|
7
|
+
// src/utils.ts
|
|
8
|
+
import { defineProperty } from "cosmokit";
|
|
9
|
+
var DisposableList = class {
|
|
10
|
+
static {
|
|
11
|
+
__name(this, "DisposableList");
|
|
12
|
+
}
|
|
13
|
+
sn = 0;
|
|
14
|
+
map = /* @__PURE__ */ new Map();
|
|
15
|
+
weak = /* @__PURE__ */ new WeakMap();
|
|
16
|
+
get length() {
|
|
17
|
+
return this.map.size;
|
|
18
|
+
}
|
|
19
|
+
push(value) {
|
|
20
|
+
const sn = ++this.sn;
|
|
21
|
+
this.map.set(sn, value);
|
|
22
|
+
this.weak.set(value, sn);
|
|
23
|
+
return () => this.map.delete(sn);
|
|
24
|
+
}
|
|
25
|
+
delete(value) {
|
|
26
|
+
const sn = this.weak.get(value);
|
|
27
|
+
if (!sn) return false;
|
|
28
|
+
return this.map.delete(sn);
|
|
29
|
+
}
|
|
30
|
+
clear() {
|
|
31
|
+
const values = [...this.map.values()];
|
|
32
|
+
this.map.clear();
|
|
33
|
+
return values.reverse();
|
|
34
|
+
}
|
|
35
|
+
[Symbol.iterator]() {
|
|
36
|
+
return this.map.values();
|
|
37
|
+
}
|
|
38
|
+
[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
|
|
39
|
+
return [...this];
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
var symbols = {
|
|
43
|
+
// internal symbols
|
|
44
|
+
shadow: /* @__PURE__ */ Symbol.for("cordis.shadow"),
|
|
45
|
+
caller: /* @__PURE__ */ Symbol.for("cordis.caller"),
|
|
46
|
+
receiver: /* @__PURE__ */ Symbol.for("cordis.receiver"),
|
|
47
|
+
original: /* @__PURE__ */ Symbol.for("cordis.original"),
|
|
48
|
+
metadata: /* @__PURE__ */ Symbol.for("cordis.metadata"),
|
|
49
|
+
initHooks: /* @__PURE__ */ Symbol.for("cordis.initHooks"),
|
|
50
|
+
checkProto: /* @__PURE__ */ Symbol.for("cordis.checkProto"),
|
|
51
|
+
// context symbols
|
|
52
|
+
effect: /* @__PURE__ */ Symbol.for("cordis.effect"),
|
|
53
|
+
filter: /* @__PURE__ */ Symbol.for("cordis.filter"),
|
|
54
|
+
isolate: /* @__PURE__ */ Symbol.for("cordis.isolate"),
|
|
55
|
+
intercept: /* @__PURE__ */ Symbol.for("cordis.intercept"),
|
|
56
|
+
// service symbols
|
|
57
|
+
init: /* @__PURE__ */ Symbol.for("cordis.init"),
|
|
58
|
+
check: /* @__PURE__ */ Symbol.for("cordis.check"),
|
|
59
|
+
config: /* @__PURE__ */ Symbol.for("cordis.config"),
|
|
60
|
+
invoke: /* @__PURE__ */ Symbol.for("cordis.invoke"),
|
|
61
|
+
extend: /* @__PURE__ */ Symbol.for("cordis.extend"),
|
|
62
|
+
tracker: /* @__PURE__ */ Symbol.for("cordis.tracker"),
|
|
63
|
+
resolveConfig: /* @__PURE__ */ Symbol.for("cordis.resolveConfig")
|
|
64
|
+
};
|
|
65
|
+
var GeneratorFunction = function* () {
|
|
66
|
+
}.constructor;
|
|
67
|
+
var AsyncGeneratorFunction = async function* () {
|
|
68
|
+
}.constructor;
|
|
69
|
+
function isConstructor(func) {
|
|
70
|
+
if (!func.prototype) return false;
|
|
71
|
+
if (func instanceof GeneratorFunction) return false;
|
|
72
|
+
if (AsyncGeneratorFunction !== Function && func instanceof AsyncGeneratorFunction) return false;
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
__name(isConstructor, "isConstructor");
|
|
76
|
+
function joinPrototype(proto1, proto2) {
|
|
77
|
+
if (proto1 === Object.prototype) return proto2;
|
|
78
|
+
const result = Object.create(joinPrototype(Object.getPrototypeOf(proto1), proto2));
|
|
79
|
+
for (const key of Reflect.ownKeys(proto1)) {
|
|
80
|
+
Object.defineProperty(result, key, Object.getOwnPropertyDescriptor(proto1, key));
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
__name(joinPrototype, "joinPrototype");
|
|
85
|
+
function isObject(value) {
|
|
86
|
+
return value && (typeof value === "object" || typeof value === "function");
|
|
87
|
+
}
|
|
88
|
+
__name(isObject, "isObject");
|
|
89
|
+
function getPropertyDescriptor(target, prop) {
|
|
90
|
+
let proto = target;
|
|
91
|
+
while (proto) {
|
|
92
|
+
const desc = Reflect.getOwnPropertyDescriptor(proto, prop);
|
|
93
|
+
if (desc) return desc;
|
|
94
|
+
proto = Object.getPrototypeOf(proto);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
__name(getPropertyDescriptor, "getPropertyDescriptor");
|
|
98
|
+
function getTraceable(ctx, value) {
|
|
99
|
+
if (!isObject(value)) return value;
|
|
100
|
+
if (Object.hasOwn(value, symbols.shadow)) {
|
|
101
|
+
return Object.getPrototypeOf(value);
|
|
102
|
+
}
|
|
103
|
+
const tracker = value[symbols.tracker];
|
|
104
|
+
if (!tracker) return value;
|
|
105
|
+
return createTraceable(ctx, value, tracker);
|
|
106
|
+
}
|
|
107
|
+
__name(getTraceable, "getTraceable");
|
|
108
|
+
function withProps(target, props) {
|
|
109
|
+
if (!props) return target;
|
|
110
|
+
return new Proxy(target, {
|
|
111
|
+
get: /* @__PURE__ */ __name((target2, prop, receiver) => {
|
|
112
|
+
if (prop in props && prop !== "constructor") return Reflect.get(props, prop, receiver);
|
|
113
|
+
return Reflect.get(target2, prop, receiver);
|
|
114
|
+
}, "get"),
|
|
115
|
+
set: /* @__PURE__ */ __name((target2, prop, value, receiver) => {
|
|
116
|
+
if (prop in props && prop !== "constructor") return Reflect.set(props, prop, value, receiver);
|
|
117
|
+
return Reflect.set(target2, prop, value, receiver);
|
|
118
|
+
}, "set")
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
__name(withProps, "withProps");
|
|
122
|
+
function withProp(target, prop, value) {
|
|
123
|
+
return withProps(target, Object.defineProperty(/* @__PURE__ */ Object.create(null), prop, {
|
|
124
|
+
value,
|
|
125
|
+
writable: false
|
|
126
|
+
}));
|
|
127
|
+
}
|
|
128
|
+
__name(withProp, "withProp");
|
|
129
|
+
function createShadow(ctx, target, property, receiver) {
|
|
130
|
+
if (!property) return receiver;
|
|
131
|
+
const origin = getPropertyDescriptor(target, property)?.value;
|
|
132
|
+
if (!origin) return receiver;
|
|
133
|
+
return withProp(receiver, property, ctx.extend({ [symbols.shadow]: origin }));
|
|
134
|
+
}
|
|
135
|
+
__name(createShadow, "createShadow");
|
|
136
|
+
function createShadowMethod(ctx, value, outer, shadow) {
|
|
137
|
+
return new Proxy(value, {
|
|
138
|
+
apply: /* @__PURE__ */ __name((target, thisArg, args) => {
|
|
139
|
+
if (thisArg === outer) thisArg = shadow;
|
|
140
|
+
return getTraceable(ctx, Reflect.apply(target, thisArg, args));
|
|
141
|
+
}, "apply")
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
__name(createShadowMethod, "createShadowMethod");
|
|
145
|
+
function createTraceable(ctx, value, tracker) {
|
|
146
|
+
const caller = ctx[symbols.shadow] ?? ctx;
|
|
147
|
+
if (ctx[symbols.shadow]) {
|
|
148
|
+
ctx = Object.getPrototypeOf(ctx);
|
|
149
|
+
}
|
|
150
|
+
const proxy = new Proxy(value, {
|
|
151
|
+
get: /* @__PURE__ */ __name((target, prop, receiver) => {
|
|
152
|
+
if (prop === symbols.original) return target;
|
|
153
|
+
if (prop === symbols.caller) return caller;
|
|
154
|
+
if (prop === tracker.property) return ctx;
|
|
155
|
+
if (typeof prop === "symbol") {
|
|
156
|
+
return Reflect.get(target, prop, receiver);
|
|
157
|
+
}
|
|
158
|
+
if (tracker.associate && ctx.reflect.props[`${tracker.associate}.${prop}`]) {
|
|
159
|
+
return Reflect.get(ctx, `${tracker.associate}.${prop}`, withProp(ctx, symbols.receiver, receiver));
|
|
160
|
+
}
|
|
161
|
+
let shadow, innerValue;
|
|
162
|
+
const desc = getPropertyDescriptor(target, prop);
|
|
163
|
+
if (desc && "value" in desc) {
|
|
164
|
+
innerValue = desc.value;
|
|
165
|
+
} else {
|
|
166
|
+
shadow = createShadow(ctx, target, tracker.property, receiver);
|
|
167
|
+
innerValue = Reflect.get(target, prop, shadow);
|
|
168
|
+
}
|
|
169
|
+
const innerTracker = innerValue?.[symbols.tracker];
|
|
170
|
+
if (innerTracker) {
|
|
171
|
+
return createTraceable(ctx, innerValue, innerTracker);
|
|
172
|
+
} else if (!tracker.noShadow && typeof innerValue === "function") {
|
|
173
|
+
shadow ??= createShadow(ctx, target, tracker.property, receiver);
|
|
174
|
+
return createShadowMethod(ctx, innerValue, receiver, shadow);
|
|
175
|
+
} else {
|
|
176
|
+
return innerValue;
|
|
177
|
+
}
|
|
178
|
+
}, "get"),
|
|
179
|
+
set: /* @__PURE__ */ __name((target, prop, value2, receiver) => {
|
|
180
|
+
if (prop === symbols.original) return false;
|
|
181
|
+
if (prop === symbols.caller) return false;
|
|
182
|
+
if (prop === tracker.property) return false;
|
|
183
|
+
if (typeof prop === "symbol") {
|
|
184
|
+
return Reflect.set(target, prop, value2, receiver);
|
|
185
|
+
}
|
|
186
|
+
if (tracker.associate && ctx.reflect.props[`${tracker.associate}.${prop}`]) {
|
|
187
|
+
return Reflect.set(ctx, `${tracker.associate}.${prop}`, value2, withProp(ctx, symbols.receiver, receiver));
|
|
188
|
+
}
|
|
189
|
+
const shadow = createShadow(ctx, target, tracker.property, receiver);
|
|
190
|
+
return Reflect.set(target, prop, value2, shadow);
|
|
191
|
+
}, "set"),
|
|
192
|
+
apply: /* @__PURE__ */ __name((target, thisArg, args) => {
|
|
193
|
+
const receiver = tracker.noShadow ? proxy : createShadow(ctx, target, tracker.property, proxy);
|
|
194
|
+
return applyTraceable(receiver, target, thisArg, args);
|
|
195
|
+
}, "apply")
|
|
196
|
+
});
|
|
197
|
+
return proxy;
|
|
198
|
+
}
|
|
199
|
+
__name(createTraceable, "createTraceable");
|
|
200
|
+
function applyTraceable(proxy, value, thisArg, args) {
|
|
201
|
+
if (!value[symbols.invoke]) return Reflect.apply(value, thisArg, args);
|
|
202
|
+
return value[symbols.invoke].apply(proxy, args);
|
|
203
|
+
}
|
|
204
|
+
__name(applyTraceable, "applyTraceable");
|
|
205
|
+
function createCallable(name, proto, tracker) {
|
|
206
|
+
const self = /* @__PURE__ */ __name(function(...args) {
|
|
207
|
+
const proxy = createTraceable(self["ctx"], self, tracker);
|
|
208
|
+
return Reflect.apply(proxy, this, args);
|
|
209
|
+
}, "self");
|
|
210
|
+
defineProperty(self, "name", name);
|
|
211
|
+
return Object.setPrototypeOf(self, proto);
|
|
212
|
+
}
|
|
213
|
+
__name(createCallable, "createCallable");
|
|
214
|
+
function handleError(info, reason, getOuterStack) {
|
|
215
|
+
const innerLines = info.error.stack.split("\n");
|
|
216
|
+
if (typeof reason?.stack !== "string") {
|
|
217
|
+
const outerError = new Error(reason);
|
|
218
|
+
const lines2 = outerError.stack.split("\n");
|
|
219
|
+
lines2.splice(1, Infinity, ...getOuterStack());
|
|
220
|
+
outerError.stack = lines2.join("\n");
|
|
221
|
+
throw outerError;
|
|
222
|
+
}
|
|
223
|
+
const lines = reason.stack.split("\n");
|
|
224
|
+
let index = lines.indexOf(innerLines[2]);
|
|
225
|
+
if (index === -1) throw reason;
|
|
226
|
+
index -= info.offset;
|
|
227
|
+
while (index > 0) {
|
|
228
|
+
if (!lines[index - 1].endsWith(" (<anonymous>)")) break;
|
|
229
|
+
index -= 1;
|
|
230
|
+
}
|
|
231
|
+
lines.splice(index, Infinity, ...getOuterStack());
|
|
232
|
+
reason.stack = lines.join("\n");
|
|
233
|
+
throw reason;
|
|
234
|
+
}
|
|
235
|
+
__name(handleError, "handleError");
|
|
236
|
+
function composeError(callback, getOuterStack = buildOuterStack()) {
|
|
237
|
+
const info = { offset: 1, error: new Error() };
|
|
238
|
+
try {
|
|
239
|
+
const result = callback(info);
|
|
240
|
+
if (isObject(result) && "then" in result) {
|
|
241
|
+
return result.then(void 0, (reason) => handleError(info, reason, getOuterStack));
|
|
242
|
+
} else {
|
|
243
|
+
return result;
|
|
244
|
+
}
|
|
245
|
+
} catch (reason) {
|
|
246
|
+
handleError(info, reason, getOuterStack);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
__name(composeError, "composeError");
|
|
250
|
+
function buildOuterStack(offset = 0) {
|
|
251
|
+
const outerError = new Error();
|
|
252
|
+
return () => outerError.stack.split("\n").slice(3 + offset);
|
|
253
|
+
}
|
|
254
|
+
__name(buildOuterStack, "buildOuterStack");
|
|
255
|
+
|
|
256
|
+
// src/events.ts
|
|
257
|
+
function isBailed(value) {
|
|
258
|
+
return value !== null && value !== false && value !== void 0;
|
|
259
|
+
}
|
|
260
|
+
__name(isBailed, "isBailed");
|
|
261
|
+
var EventsService = class {
|
|
262
|
+
constructor(ctx) {
|
|
263
|
+
this.ctx = ctx;
|
|
264
|
+
defineProperty2(this, symbols.tracker, {
|
|
265
|
+
property: "ctx",
|
|
266
|
+
noShadow: true
|
|
267
|
+
});
|
|
268
|
+
this.on("internal/listener", function(name, listener, options) {
|
|
269
|
+
if (name === "internal/update" && !options.global) {
|
|
270
|
+
const hooks = this.fiber._hooks["internal/update"] ??= new DisposableList();
|
|
271
|
+
const method = options.prepend ? "unshift" : "push";
|
|
272
|
+
return hooks[method](listener);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
this.on("internal/update", function(config, noSave, next) {
|
|
276
|
+
const cbs = [...this._hooks["internal/update"] || []];
|
|
277
|
+
const _next = /* @__PURE__ */ __name(() => {
|
|
278
|
+
const cb = cbs.shift() ?? next;
|
|
279
|
+
return cb.call(this, config, noSave, _next);
|
|
280
|
+
}, "_next");
|
|
281
|
+
return _next();
|
|
282
|
+
}, { global: true, prepend: true });
|
|
283
|
+
}
|
|
284
|
+
ctx;
|
|
285
|
+
static {
|
|
286
|
+
__name(this, "EventsService");
|
|
287
|
+
}
|
|
288
|
+
_hooks = {};
|
|
289
|
+
_resolve(type, args) {
|
|
290
|
+
const thisArg = typeof args[0] === "object" || typeof args[0] === "function" ? args.shift() : null;
|
|
291
|
+
const name = args.shift();
|
|
292
|
+
if (!name.startsWith("internal/") && this._hooks["internal/dispatch"]?.length) {
|
|
293
|
+
this.emit("internal/dispatch", type, name, args, thisArg);
|
|
294
|
+
}
|
|
295
|
+
const filter = thisArg?.[Context.filter];
|
|
296
|
+
return [thisArg, (this._hooks[name] || []).filter((hook) => hook.global || !filter || filter.call(thisArg, hook.ctx)).map((hook) => hook.callback)];
|
|
297
|
+
}
|
|
298
|
+
/** @deprecated */
|
|
299
|
+
dispatch(type, args) {
|
|
300
|
+
const [thisArg, callbacks] = this._resolve(type, args);
|
|
301
|
+
return callbacks.map((callback) => callback.bind(thisArg));
|
|
302
|
+
}
|
|
303
|
+
async parallel(...args) {
|
|
304
|
+
const [thisArg, callbacks] = this._resolve("emit", args);
|
|
305
|
+
const results = await Promise.allSettled(callbacks.map(async (callback) => Reflect.apply(callback, thisArg, args)));
|
|
306
|
+
const errors = results.filter((result) => result.status === "rejected");
|
|
307
|
+
if (errors.length) throw new AggregateError(errors.map((error) => error.reason));
|
|
308
|
+
}
|
|
309
|
+
emit(...args) {
|
|
310
|
+
const [thisArg, callbacks] = this._resolve("emit", args);
|
|
311
|
+
for (const callback of callbacks) Reflect.apply(callback, thisArg, args);
|
|
312
|
+
}
|
|
313
|
+
async serial(...args) {
|
|
314
|
+
const [thisArg, callbacks] = this._resolve("serial", args);
|
|
315
|
+
for (const callback of callbacks) {
|
|
316
|
+
const result = await Reflect.apply(callback, thisArg, args);
|
|
317
|
+
if (isBailed(result)) return result;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
bail(...args) {
|
|
321
|
+
const [thisArg, callbacks] = this._resolve("bail", args);
|
|
322
|
+
for (const callback of callbacks) {
|
|
323
|
+
const result = Reflect.apply(callback, thisArg, args);
|
|
324
|
+
if (isBailed(result)) return result;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
waterfall(...args) {
|
|
328
|
+
const [thisArg, callbacks] = this._resolve("waterfall", args);
|
|
329
|
+
const inner = args.pop();
|
|
330
|
+
const next = /* @__PURE__ */ __name(() => {
|
|
331
|
+
const callback = callbacks.shift();
|
|
332
|
+
return callback ? Reflect.apply(callback, thisArg, args) : inner(...args);
|
|
333
|
+
}, "next");
|
|
334
|
+
args.push(next);
|
|
335
|
+
return next();
|
|
336
|
+
}
|
|
337
|
+
register(label, hooks, callback, options) {
|
|
338
|
+
const method = options.prepend ? "unshift" : "push";
|
|
339
|
+
return this.ctx.fiber.effect(() => {
|
|
340
|
+
hooks[method]({ ctx: this.ctx, callback, ...options });
|
|
341
|
+
return () => this.unregister(hooks, callback);
|
|
342
|
+
}, label);
|
|
343
|
+
}
|
|
344
|
+
unregister(hooks, callback) {
|
|
345
|
+
const index = hooks.findIndex((hook) => hook.callback === callback);
|
|
346
|
+
if (index >= 0) {
|
|
347
|
+
hooks.splice(index, 1);
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
on(name, listener, options) {
|
|
352
|
+
if (typeof options !== "object") {
|
|
353
|
+
options = { prepend: options };
|
|
354
|
+
}
|
|
355
|
+
this.ctx.fiber.assertActive();
|
|
356
|
+
listener = this.ctx.reflect.bind(listener);
|
|
357
|
+
const result = this.bail(this.ctx, "internal/listener", name, listener, options);
|
|
358
|
+
if (result) return result;
|
|
359
|
+
const hooks = this._hooks[name] ||= [];
|
|
360
|
+
const label = `ctx.on(${typeof name === "string" ? JSON.stringify(name) : name.toString()})`;
|
|
361
|
+
return this.register(label, hooks, listener, options);
|
|
362
|
+
}
|
|
363
|
+
once(name, listener, options) {
|
|
364
|
+
const dispose = this.on(name, function(...args) {
|
|
365
|
+
dispose();
|
|
366
|
+
return listener.apply(this, args);
|
|
367
|
+
}, options);
|
|
368
|
+
return dispose;
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
// src/logger.ts
|
|
373
|
+
import { defineProperty as defineProperty3, hyphenate } from "cosmokit";
|
|
374
|
+
var LoggerLevel = /* @__PURE__ */ ((LoggerLevel2) => {
|
|
375
|
+
LoggerLevel2[LoggerLevel2["ERROR"] = 0] = "ERROR";
|
|
376
|
+
LoggerLevel2[LoggerLevel2["WARN"] = 1] = "WARN";
|
|
377
|
+
LoggerLevel2[LoggerLevel2["INFO"] = 2] = "INFO";
|
|
378
|
+
LoggerLevel2[LoggerLevel2["DEBUG"] = 3] = "DEBUG";
|
|
379
|
+
return LoggerLevel2;
|
|
380
|
+
})(LoggerLevel || {});
|
|
381
|
+
var defaultFormatters = {
|
|
382
|
+
s: /* @__PURE__ */ __name((value) => String(value), "s"),
|
|
383
|
+
d: /* @__PURE__ */ __name((value) => Math.trunc(Number(value)), "d"),
|
|
384
|
+
i: /* @__PURE__ */ __name((value) => Math.trunc(Number(value)), "i"),
|
|
385
|
+
f: /* @__PURE__ */ __name((value) => Number(value), "f"),
|
|
386
|
+
o: /* @__PURE__ */ __name((value) => JSON.stringify(value), "o"),
|
|
387
|
+
O: /* @__PURE__ */ __name((value) => JSON.stringify(value), "O"),
|
|
388
|
+
c: /* @__PURE__ */ __name(() => "", "c"),
|
|
389
|
+
C: /* @__PURE__ */ __name((value, exporter, message) => {
|
|
390
|
+
return Logger.color(exporter, Logger.code(message.name, exporter.colors), value);
|
|
391
|
+
}, "C")
|
|
392
|
+
};
|
|
393
|
+
function isAggregateError(error) {
|
|
394
|
+
return error instanceof Error && Array.isArray(error["errors"]);
|
|
395
|
+
}
|
|
396
|
+
__name(isAggregateError, "isAggregateError");
|
|
397
|
+
var Logger = class {
|
|
398
|
+
constructor(options, service) {
|
|
399
|
+
this.service = service;
|
|
400
|
+
Object.assign(this, options);
|
|
401
|
+
this.error = this._method("error", 0 /* ERROR */);
|
|
402
|
+
this.info = this._method("info", 2 /* INFO */);
|
|
403
|
+
this.warn = this._method("warn", 1 /* WARN */);
|
|
404
|
+
this.debug = this._method("debug", 3 /* DEBUG */);
|
|
405
|
+
}
|
|
406
|
+
service;
|
|
407
|
+
static {
|
|
408
|
+
__name(this, "Logger");
|
|
409
|
+
}
|
|
410
|
+
static color(exporter, code, value, decoration = "") {
|
|
411
|
+
if (!exporter.colors) return "" + value;
|
|
412
|
+
return `\x1B[3${code < 8 ? code : "8;5;" + code}${exporter.colors >= 2 ? decoration : ""}m${value}\x1B[0m`;
|
|
413
|
+
}
|
|
414
|
+
static code(name, level) {
|
|
415
|
+
let hash = 0;
|
|
416
|
+
for (let i = 0; i < name.length; i++) {
|
|
417
|
+
hash = (hash << 3) - hash + name.charCodeAt(i) + 13;
|
|
418
|
+
hash |= 0;
|
|
419
|
+
}
|
|
420
|
+
const colors = !level ? [] : level >= 2 ? c256 : c16;
|
|
421
|
+
return colors[Math.abs(hash) % colors.length];
|
|
422
|
+
}
|
|
423
|
+
static format(exporter, message) {
|
|
424
|
+
const args = message.args.slice();
|
|
425
|
+
if (args[0] instanceof Error) {
|
|
426
|
+
args[0] = args[0].stack || args[0].message;
|
|
427
|
+
args.unshift("%s");
|
|
428
|
+
} else if (typeof args[0] !== "string") {
|
|
429
|
+
args.unshift("%o");
|
|
430
|
+
}
|
|
431
|
+
let format = args.shift();
|
|
432
|
+
format = format.replace(/%([a-zA-Z%])/g, (match, char) => {
|
|
433
|
+
if (match === "%%") return "%";
|
|
434
|
+
const formatter = exporter.formatters?.[char] ?? defaultFormatters[char];
|
|
435
|
+
if (typeof formatter === "function") {
|
|
436
|
+
const value = args.shift();
|
|
437
|
+
return formatter(value, exporter, message);
|
|
438
|
+
}
|
|
439
|
+
return match;
|
|
440
|
+
});
|
|
441
|
+
const oFormatter = exporter.formatters?.o ?? defaultFormatters.o;
|
|
442
|
+
for (let arg of args) {
|
|
443
|
+
if (typeof arg === "object" && arg) {
|
|
444
|
+
arg = oFormatter(arg, exporter, message);
|
|
445
|
+
}
|
|
446
|
+
format += " " + arg;
|
|
447
|
+
}
|
|
448
|
+
const { maxLength = 10240 } = exporter;
|
|
449
|
+
return format.split(/\r?\n/g).map((line) => {
|
|
450
|
+
return line.slice(0, maxLength) + (line.length > maxLength ? "..." : "");
|
|
451
|
+
}).join("\n");
|
|
452
|
+
}
|
|
453
|
+
_method(type, level) {
|
|
454
|
+
return (...args) => {
|
|
455
|
+
if (args.length === 1 && args[0] instanceof Error) {
|
|
456
|
+
if (args[0].cause) {
|
|
457
|
+
this[type](args[0].cause);
|
|
458
|
+
} else if (isAggregateError(args[0])) {
|
|
459
|
+
args[0].errors.forEach((error) => this[type](error));
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const sn = ++this.service._snMessage;
|
|
464
|
+
const ts = Date.now();
|
|
465
|
+
for (const exporter of this.service.exporters.values()) {
|
|
466
|
+
const targetLevel = exporter.levels?.[this.name] ?? exporter.levels?.default ?? this.level ?? 2 /* INFO */;
|
|
467
|
+
if (targetLevel < level) continue;
|
|
468
|
+
const message = { sn, ts, type, level, name: this.name, ...this.meta, args };
|
|
469
|
+
exporter.export(message);
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
var c16 = [6, 2, 3, 4, 5, 1];
|
|
475
|
+
var c256 = [
|
|
476
|
+
20,
|
|
477
|
+
21,
|
|
478
|
+
26,
|
|
479
|
+
27,
|
|
480
|
+
32,
|
|
481
|
+
33,
|
|
482
|
+
38,
|
|
483
|
+
39,
|
|
484
|
+
40,
|
|
485
|
+
41,
|
|
486
|
+
42,
|
|
487
|
+
43,
|
|
488
|
+
44,
|
|
489
|
+
45,
|
|
490
|
+
56,
|
|
491
|
+
57,
|
|
492
|
+
62,
|
|
493
|
+
63,
|
|
494
|
+
68,
|
|
495
|
+
69,
|
|
496
|
+
74,
|
|
497
|
+
75,
|
|
498
|
+
76,
|
|
499
|
+
77,
|
|
500
|
+
78,
|
|
501
|
+
79,
|
|
502
|
+
80,
|
|
503
|
+
81,
|
|
504
|
+
92,
|
|
505
|
+
93,
|
|
506
|
+
98,
|
|
507
|
+
99,
|
|
508
|
+
112,
|
|
509
|
+
113,
|
|
510
|
+
129,
|
|
511
|
+
134,
|
|
512
|
+
135,
|
|
513
|
+
148,
|
|
514
|
+
149,
|
|
515
|
+
160,
|
|
516
|
+
161,
|
|
517
|
+
162,
|
|
518
|
+
163,
|
|
519
|
+
164,
|
|
520
|
+
165,
|
|
521
|
+
166,
|
|
522
|
+
167,
|
|
523
|
+
168,
|
|
524
|
+
169,
|
|
525
|
+
170,
|
|
526
|
+
171,
|
|
527
|
+
172,
|
|
528
|
+
173,
|
|
529
|
+
178,
|
|
530
|
+
179,
|
|
531
|
+
184,
|
|
532
|
+
185,
|
|
533
|
+
196,
|
|
534
|
+
197,
|
|
535
|
+
198,
|
|
536
|
+
199,
|
|
537
|
+
200,
|
|
538
|
+
201,
|
|
539
|
+
202,
|
|
540
|
+
203,
|
|
541
|
+
204,
|
|
542
|
+
205,
|
|
543
|
+
206,
|
|
544
|
+
207,
|
|
545
|
+
208,
|
|
546
|
+
209,
|
|
547
|
+
214,
|
|
548
|
+
215,
|
|
549
|
+
220,
|
|
550
|
+
221
|
|
551
|
+
];
|
|
552
|
+
var LoggerService = class _LoggerService {
|
|
553
|
+
static {
|
|
554
|
+
__name(this, "LoggerService");
|
|
555
|
+
}
|
|
556
|
+
bufferSize = 1e3;
|
|
557
|
+
buffer = [];
|
|
558
|
+
ctx;
|
|
559
|
+
_snMessage = 0;
|
|
560
|
+
_snExporter = 0;
|
|
561
|
+
exporters = /* @__PURE__ */ new Map();
|
|
562
|
+
constructor(ctx) {
|
|
563
|
+
const tracker = {
|
|
564
|
+
property: "ctx",
|
|
565
|
+
noShadow: true
|
|
566
|
+
};
|
|
567
|
+
const self = createCallable("logger", joinPrototype(Object.getPrototypeOf(this), Function.prototype), tracker);
|
|
568
|
+
Object.assign(self, this);
|
|
569
|
+
self.ctx = ctx;
|
|
570
|
+
defineProperty3(self, symbols.tracker, tracker);
|
|
571
|
+
self.exporter({
|
|
572
|
+
colors: 3,
|
|
573
|
+
export: /* @__PURE__ */ __name((message) => {
|
|
574
|
+
self.buffer.push(message);
|
|
575
|
+
const overflow = self.buffer.length - self.bufferSize;
|
|
576
|
+
if (overflow === 1) {
|
|
577
|
+
self.buffer.shift();
|
|
578
|
+
} else if (overflow > 1) {
|
|
579
|
+
self.buffer.splice(0, overflow);
|
|
580
|
+
}
|
|
581
|
+
}, "export")
|
|
582
|
+
});
|
|
583
|
+
return self;
|
|
584
|
+
}
|
|
585
|
+
exporter(exporter) {
|
|
586
|
+
return this.ctx.effect(() => {
|
|
587
|
+
const id = ++this._snExporter;
|
|
588
|
+
this.exporters.set(id, exporter);
|
|
589
|
+
return () => this.exporters.delete(id);
|
|
590
|
+
}, "ctx.logger.exporter()");
|
|
591
|
+
}
|
|
592
|
+
_resolveConfig() {
|
|
593
|
+
let intercept = this.ctx[symbols.intercept];
|
|
594
|
+
const configs = [];
|
|
595
|
+
while ("logger" in intercept) {
|
|
596
|
+
if (Object.hasOwn(intercept, "logger")) {
|
|
597
|
+
configs.unshift(intercept["logger"]);
|
|
598
|
+
}
|
|
599
|
+
intercept = Object.getPrototypeOf(intercept);
|
|
600
|
+
}
|
|
601
|
+
return Object.assign({}, ...configs);
|
|
602
|
+
}
|
|
603
|
+
[symbols.invoke](name) {
|
|
604
|
+
const config = this._resolveConfig();
|
|
605
|
+
const caller = this[symbols.caller];
|
|
606
|
+
const fiber = (caller ?? this.ctx).fiber;
|
|
607
|
+
name ??= config.name;
|
|
608
|
+
name ??= hyphenate(fiber.name);
|
|
609
|
+
return new Logger({
|
|
610
|
+
name,
|
|
611
|
+
level: config.level,
|
|
612
|
+
meta: { fiber: new WeakRef(fiber) }
|
|
613
|
+
}, this);
|
|
614
|
+
}
|
|
615
|
+
static {
|
|
616
|
+
for (const type of ["error", "info", "warn", "debug"]) {
|
|
617
|
+
;
|
|
618
|
+
_LoggerService.prototype[type] = function(...args) {
|
|
619
|
+
return this()[type](...args);
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
// src/reflect.ts
|
|
626
|
+
import { defineProperty as defineProperty5, isNullable as isNullable2 } from "cosmokit";
|
|
627
|
+
|
|
628
|
+
// src/fiber.ts
|
|
629
|
+
import { defineProperty as defineProperty4, isNullable } from "cosmokit";
|
|
630
|
+
var kValidationError = /* @__PURE__ */ Symbol.for("ValidationError");
|
|
631
|
+
var ValidationError = class extends TypeError {
|
|
632
|
+
static {
|
|
633
|
+
__name(this, "ValidationError");
|
|
634
|
+
}
|
|
635
|
+
name = "ValidationError";
|
|
636
|
+
constructor(issues) {
|
|
637
|
+
super(`invalid config:
|
|
638
|
+
` + issues.map((issue) => {
|
|
639
|
+
if (issue.path) {
|
|
640
|
+
return ` - ${issue.message} (at ${issue.path.join(".")})`;
|
|
641
|
+
} else {
|
|
642
|
+
return ` - ${issue.message}`;
|
|
643
|
+
}
|
|
644
|
+
}).join("\n"));
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
Object.defineProperty(ValidationError.prototype, kValidationError, {
|
|
648
|
+
value: true
|
|
649
|
+
});
|
|
650
|
+
function resolveConfig(runtime, config) {
|
|
651
|
+
if (!runtime.Config) return config;
|
|
652
|
+
const result = runtime.Config["~standard"].validate(config);
|
|
653
|
+
if ("then" in result) {
|
|
654
|
+
throw new TypeError("Async config validation is not supported");
|
|
655
|
+
}
|
|
656
|
+
if (result.issues) {
|
|
657
|
+
throw new ValidationError(result.issues);
|
|
658
|
+
} else {
|
|
659
|
+
return result.value;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
__name(resolveConfig, "resolveConfig");
|
|
663
|
+
var FiberState = /* @__PURE__ */ ((FiberState2) => {
|
|
664
|
+
FiberState2[FiberState2["PENDING"] = 0] = "PENDING";
|
|
665
|
+
FiberState2[FiberState2["LOADING"] = 1] = "LOADING";
|
|
666
|
+
FiberState2[FiberState2["ACTIVE"] = 2] = "ACTIVE";
|
|
667
|
+
FiberState2[FiberState2["FAILED"] = 3] = "FAILED";
|
|
668
|
+
FiberState2[FiberState2["DISPOSED"] = 4] = "DISPOSED";
|
|
669
|
+
FiberState2[FiberState2["UNLOADING"] = 5] = "UNLOADING";
|
|
670
|
+
return FiberState2;
|
|
671
|
+
})(FiberState || {});
|
|
672
|
+
var CordisError = class _CordisError extends Error {
|
|
673
|
+
constructor(code, message) {
|
|
674
|
+
super(message ?? _CordisError.Code[code]);
|
|
675
|
+
this.code = code;
|
|
676
|
+
}
|
|
677
|
+
code;
|
|
678
|
+
static {
|
|
679
|
+
__name(this, "CordisError");
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
((CordisError2) => {
|
|
683
|
+
CordisError2.Code = {
|
|
684
|
+
INACTIVE_EFFECT: "cannot create effect on inactive context"
|
|
685
|
+
};
|
|
686
|
+
})(CordisError || (CordisError = {}));
|
|
687
|
+
var INACTIVE = "__INACTIVE__";
|
|
688
|
+
var Fiber = class {
|
|
689
|
+
constructor(parent, config, inject, runtime, getOuterStack) {
|
|
690
|
+
this.parent = parent;
|
|
691
|
+
this.inject = inject;
|
|
692
|
+
this.runtime = runtime;
|
|
693
|
+
const collect = /* @__PURE__ */ __name((dispose) => {
|
|
694
|
+
this._disposables.push(dispose);
|
|
695
|
+
}, "collect");
|
|
696
|
+
if (runtime) {
|
|
697
|
+
this.uid = parent.registry.counter;
|
|
698
|
+
this.ctx = this.context = parent.extend({ fiber: this });
|
|
699
|
+
const injectEntries = Object.entries(this.inject);
|
|
700
|
+
if (injectEntries.length) {
|
|
701
|
+
this.ctx[Context.intercept] = Object.create(parent[Context.intercept]);
|
|
702
|
+
for (const [name, config2] of injectEntries) {
|
|
703
|
+
if (isNullable(config2)) continue;
|
|
704
|
+
this.ctx[Context.intercept][name] = config2;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
this._runner = {
|
|
708
|
+
epoch: INACTIVE,
|
|
709
|
+
getOuterStack,
|
|
710
|
+
execute: /* @__PURE__ */ __name(function() {
|
|
711
|
+
if (isConstructor(runtime.callback)) {
|
|
712
|
+
const instance = new runtime.callback(this.ctx, this.config);
|
|
713
|
+
for (const hook of instance?.[symbols.initHooks] ?? []) {
|
|
714
|
+
hook();
|
|
715
|
+
}
|
|
716
|
+
return instance?.[symbols.init]?.();
|
|
717
|
+
} else {
|
|
718
|
+
return runtime.callback(this.ctx, this.config);
|
|
719
|
+
}
|
|
720
|
+
}, "execute"),
|
|
721
|
+
collect
|
|
722
|
+
};
|
|
723
|
+
this.context.emit("internal/plugin", this);
|
|
724
|
+
for (const name of Object.keys(this.inject)) {
|
|
725
|
+
this._checkImpl(name);
|
|
726
|
+
}
|
|
727
|
+
this.dispose = parent.fiber.effect(() => {
|
|
728
|
+
const remove = runtime.fibers.push(this);
|
|
729
|
+
try {
|
|
730
|
+
this.config = resolveConfig(runtime, config);
|
|
731
|
+
this._refresh();
|
|
732
|
+
} catch (error) {
|
|
733
|
+
this.ctx.logger.error(error);
|
|
734
|
+
this._error = error;
|
|
735
|
+
}
|
|
736
|
+
return async () => {
|
|
737
|
+
this.uid = null;
|
|
738
|
+
this.context.emit("internal/plugin", this);
|
|
739
|
+
if (this.ctx.registry.has(runtime.callback)) {
|
|
740
|
+
remove();
|
|
741
|
+
if (!runtime.fibers.length) {
|
|
742
|
+
this.ctx.registry.delete(runtime.callback);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
this._setEpoch(INACTIVE);
|
|
746
|
+
while (this.inertia) {
|
|
747
|
+
await this.inertia;
|
|
748
|
+
}
|
|
749
|
+
};
|
|
750
|
+
}, "ctx.plugin()");
|
|
751
|
+
} else {
|
|
752
|
+
this.uid = 0;
|
|
753
|
+
this.ctx = this.context = parent;
|
|
754
|
+
this.state = 2 /* ACTIVE */;
|
|
755
|
+
this.store = /* @__PURE__ */ Object.create(null);
|
|
756
|
+
this._runner = {
|
|
757
|
+
epoch: "",
|
|
758
|
+
getOuterStack,
|
|
759
|
+
execute: /* @__PURE__ */ __name(() => {
|
|
760
|
+
}, "execute"),
|
|
761
|
+
collect
|
|
762
|
+
};
|
|
763
|
+
this.dispose = () => this.restart();
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
parent;
|
|
767
|
+
inject;
|
|
768
|
+
runtime;
|
|
769
|
+
static {
|
|
770
|
+
__name(this, "Fiber");
|
|
771
|
+
}
|
|
772
|
+
uid;
|
|
773
|
+
ctx;
|
|
774
|
+
config;
|
|
775
|
+
state = 0 /* PENDING */;
|
|
776
|
+
dispose;
|
|
777
|
+
store;
|
|
778
|
+
inertia;
|
|
779
|
+
_hooks = /* @__PURE__ */ Object.create(null);
|
|
780
|
+
_disposables = new DisposableList();
|
|
781
|
+
// Same as `this.ctx`, but with a more specific type.
|
|
782
|
+
context;
|
|
783
|
+
_error;
|
|
784
|
+
_runner;
|
|
785
|
+
_store = /* @__PURE__ */ Object.create(null);
|
|
786
|
+
get name() {
|
|
787
|
+
let fiber = this;
|
|
788
|
+
do {
|
|
789
|
+
if (fiber.runtime?.name) return fiber.runtime.name;
|
|
790
|
+
fiber = fiber.parent.fiber;
|
|
791
|
+
} while (fiber !== fiber.parent.fiber);
|
|
792
|
+
return "root";
|
|
793
|
+
}
|
|
794
|
+
assertActive() {
|
|
795
|
+
if (this.uid !== null) return;
|
|
796
|
+
throw new CordisError("INACTIVE_EFFECT");
|
|
797
|
+
}
|
|
798
|
+
_execute(runner) {
|
|
799
|
+
const oldEpoch = runner.epoch;
|
|
800
|
+
return composeError((info) => {
|
|
801
|
+
const safeCollect = /* @__PURE__ */ __name((dispose) => {
|
|
802
|
+
if (typeof dispose === "function") {
|
|
803
|
+
runner.collect(dispose);
|
|
804
|
+
} else if (!isNullable(dispose)) {
|
|
805
|
+
throw new TypeError("Invalid effect");
|
|
806
|
+
}
|
|
807
|
+
}, "safeCollect");
|
|
808
|
+
const effect = runner.execute.call(this);
|
|
809
|
+
if (typeof effect === "function") {
|
|
810
|
+
return runner.collect(effect);
|
|
811
|
+
} else if (isNullable(effect)) {
|
|
812
|
+
} else if (!isObject(effect)) {
|
|
813
|
+
throw new TypeError("Invalid effect");
|
|
814
|
+
} else if ("then" in effect) {
|
|
815
|
+
return effect.then(safeCollect);
|
|
816
|
+
} else if (Symbol.iterator in effect) {
|
|
817
|
+
info.error = new Error();
|
|
818
|
+
const iter = effect[Symbol.iterator]();
|
|
819
|
+
while (true) {
|
|
820
|
+
const result = iter.next();
|
|
821
|
+
safeCollect(result.value);
|
|
822
|
+
if (result.done) return;
|
|
823
|
+
}
|
|
824
|
+
} else if (Symbol.asyncIterator in effect) {
|
|
825
|
+
const iter = effect[Symbol.asyncIterator]();
|
|
826
|
+
return (async () => {
|
|
827
|
+
await Promise.resolve();
|
|
828
|
+
info.error = new Error();
|
|
829
|
+
while (true) {
|
|
830
|
+
if (runner.epoch !== oldEpoch) return;
|
|
831
|
+
const result = await iter.next();
|
|
832
|
+
safeCollect(result.value);
|
|
833
|
+
if (result.done) return;
|
|
834
|
+
}
|
|
835
|
+
})();
|
|
836
|
+
} else {
|
|
837
|
+
throw new TypeError("Invalid effect");
|
|
838
|
+
}
|
|
839
|
+
}, runner.getOuterStack);
|
|
840
|
+
}
|
|
841
|
+
effect(execute, label = "anonymous") {
|
|
842
|
+
this.assertActive();
|
|
843
|
+
const disposables = [];
|
|
844
|
+
const dispose = /* @__PURE__ */ __name(() => {
|
|
845
|
+
let task2;
|
|
846
|
+
for (const dispose2 of disposables.splice(0).reverse()) {
|
|
847
|
+
if (task2) {
|
|
848
|
+
task2 = task2.then(dispose2);
|
|
849
|
+
} else {
|
|
850
|
+
const result = dispose2();
|
|
851
|
+
if (isObject(result) && "then" in result) {
|
|
852
|
+
task2 = result;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
return task2;
|
|
857
|
+
}, "dispose");
|
|
858
|
+
const meta = { label, children: [] };
|
|
859
|
+
const runner = {
|
|
860
|
+
execute,
|
|
861
|
+
epoch: true,
|
|
862
|
+
collect: /* @__PURE__ */ __name((dispose2) => {
|
|
863
|
+
disposables.push(dispose2);
|
|
864
|
+
this._disposables.delete(dispose2);
|
|
865
|
+
if (dispose2[symbols.effect]) {
|
|
866
|
+
meta.children.push(dispose2[symbols.effect]);
|
|
867
|
+
}
|
|
868
|
+
}, "collect"),
|
|
869
|
+
getOuterStack: buildOuterStack()
|
|
870
|
+
};
|
|
871
|
+
let task;
|
|
872
|
+
try {
|
|
873
|
+
task = this._execute(runner);
|
|
874
|
+
} catch (reason) {
|
|
875
|
+
dispose();
|
|
876
|
+
throw reason;
|
|
877
|
+
}
|
|
878
|
+
task?.catch(dispose).catch((error) => this.ctx.logger.error(error));
|
|
879
|
+
const wrapper = defineProperty4(() => {
|
|
880
|
+
if (!runner.epoch) return;
|
|
881
|
+
runner.epoch = false;
|
|
882
|
+
return task ? task.then(dispose) : dispose();
|
|
883
|
+
}, symbols.effect, meta);
|
|
884
|
+
const disposeAsync = /* @__PURE__ */ __name(() => {
|
|
885
|
+
if (!runner.epoch) return;
|
|
886
|
+
runner.epoch = false;
|
|
887
|
+
return dispose();
|
|
888
|
+
}, "disposeAsync");
|
|
889
|
+
wrapper.then = async (onFulfilled, onRejected) => {
|
|
890
|
+
return Promise.resolve(task).then(() => disposeAsync).then(onFulfilled, onRejected);
|
|
891
|
+
};
|
|
892
|
+
disposables.push(this._disposables.push(wrapper));
|
|
893
|
+
return wrapper;
|
|
894
|
+
}
|
|
895
|
+
getEffects() {
|
|
896
|
+
return [...this._disposables].map((dispose) => dispose[symbols.effect]).filter(Boolean);
|
|
897
|
+
}
|
|
898
|
+
_getState() {
|
|
899
|
+
if (this.uid === null) return 4 /* DISPOSED */;
|
|
900
|
+
if (this._error) return 3 /* FAILED */;
|
|
901
|
+
if (this._runner.epoch !== INACTIVE) return 2 /* ACTIVE */;
|
|
902
|
+
return 0 /* PENDING */;
|
|
903
|
+
}
|
|
904
|
+
_updateState(callback) {
|
|
905
|
+
const oldState = this.state;
|
|
906
|
+
this.state = callback() ?? this._getState();
|
|
907
|
+
if (oldState === this.state) return;
|
|
908
|
+
this.context.emit("internal/status", this, oldState);
|
|
909
|
+
if (oldState !== 2 /* ACTIVE */ && this.state !== 2 /* ACTIVE */) return;
|
|
910
|
+
for (const key of Reflect.ownKeys(this.ctx.reflect.store)) {
|
|
911
|
+
const impl = this.ctx.reflect.store[key];
|
|
912
|
+
if (impl.fiber !== this) continue;
|
|
913
|
+
this.ctx.reflect.notify([impl.name]);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
_checkImpl(name) {
|
|
917
|
+
const impl = this.ctx.reflect._getImpl(name, true);
|
|
918
|
+
if (!impl) return delete this._store[name];
|
|
919
|
+
try {
|
|
920
|
+
if (impl.check && !impl.check.call(getTraceable(this.ctx, impl.value))) {
|
|
921
|
+
return delete this._store[name];
|
|
922
|
+
}
|
|
923
|
+
} catch (error) {
|
|
924
|
+
impl.fiber.ctx.logger.error(error);
|
|
925
|
+
return delete this._store[name];
|
|
926
|
+
}
|
|
927
|
+
this._store[name] = impl;
|
|
928
|
+
}
|
|
929
|
+
_refresh() {
|
|
930
|
+
let epoch = false;
|
|
931
|
+
epoch = "";
|
|
932
|
+
for (const name of Object.keys(this.inject)) {
|
|
933
|
+
const impl = this._store[name];
|
|
934
|
+
if (!impl) {
|
|
935
|
+
epoch = INACTIVE;
|
|
936
|
+
break;
|
|
937
|
+
}
|
|
938
|
+
epoch += ":" + impl.fiber.uid;
|
|
939
|
+
}
|
|
940
|
+
this._setEpoch(epoch);
|
|
941
|
+
}
|
|
942
|
+
_setEpoch(epoch) {
|
|
943
|
+
const oldEpoch = this._runner.epoch;
|
|
944
|
+
if (epoch === oldEpoch) return;
|
|
945
|
+
this._runner.epoch = epoch;
|
|
946
|
+
if (this.inertia) return;
|
|
947
|
+
this._updateState(() => {
|
|
948
|
+
if (epoch !== INACTIVE && oldEpoch === INACTIVE) {
|
|
949
|
+
this.inertia = this._reload();
|
|
950
|
+
return 1 /* LOADING */;
|
|
951
|
+
} else {
|
|
952
|
+
this.inertia = this._unload();
|
|
953
|
+
return 5 /* UNLOADING */;
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
async _reload() {
|
|
958
|
+
this.store = { ...this._store };
|
|
959
|
+
const oldEpoch = this._runner.epoch;
|
|
960
|
+
try {
|
|
961
|
+
await Promise.resolve();
|
|
962
|
+
await this._execute(this._runner);
|
|
963
|
+
} catch (reason) {
|
|
964
|
+
this.ctx.logger.error(reason);
|
|
965
|
+
this._error = reason;
|
|
966
|
+
this._runner.epoch = INACTIVE;
|
|
967
|
+
}
|
|
968
|
+
this._updateState(() => {
|
|
969
|
+
if (this._runner.epoch === oldEpoch) {
|
|
970
|
+
this.inertia = void 0;
|
|
971
|
+
} else {
|
|
972
|
+
this.inertia = this._unload();
|
|
973
|
+
return 5 /* UNLOADING */;
|
|
974
|
+
}
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
async _unload() {
|
|
978
|
+
await Promise.all(this._disposables.clear().map(async (dispose) => {
|
|
979
|
+
try {
|
|
980
|
+
await composeError(async (info) => {
|
|
981
|
+
await Promise.resolve();
|
|
982
|
+
info.error = new Error();
|
|
983
|
+
await dispose();
|
|
984
|
+
}, this._runner.getOuterStack);
|
|
985
|
+
} catch (reason) {
|
|
986
|
+
this.ctx.logger.error(reason);
|
|
987
|
+
}
|
|
988
|
+
}));
|
|
989
|
+
this.store = void 0;
|
|
990
|
+
this._updateState(() => {
|
|
991
|
+
if (this._runner.epoch === INACTIVE) {
|
|
992
|
+
this.inertia = void 0;
|
|
993
|
+
} else {
|
|
994
|
+
this.inertia = this._reload();
|
|
995
|
+
return 1 /* LOADING */;
|
|
996
|
+
}
|
|
997
|
+
});
|
|
998
|
+
}
|
|
999
|
+
async await() {
|
|
1000
|
+
while (this.inertia) {
|
|
1001
|
+
await this.inertia;
|
|
1002
|
+
}
|
|
1003
|
+
if (this._error) throw this._error;
|
|
1004
|
+
return this;
|
|
1005
|
+
}
|
|
1006
|
+
async restart() {
|
|
1007
|
+
const fiber = this.ctx.fiber;
|
|
1008
|
+
fiber.assertActive();
|
|
1009
|
+
fiber._setEpoch(INACTIVE);
|
|
1010
|
+
fiber._refresh();
|
|
1011
|
+
await fiber.await();
|
|
1012
|
+
}
|
|
1013
|
+
update(config, noSave = false) {
|
|
1014
|
+
const fiber = this.ctx.fiber;
|
|
1015
|
+
fiber.assertActive();
|
|
1016
|
+
config = resolveConfig(fiber.runtime, config);
|
|
1017
|
+
fiber.context.waterfall(fiber, "internal/update", config, noSave, () => {
|
|
1018
|
+
fiber.config = config;
|
|
1019
|
+
fiber._error = void 0;
|
|
1020
|
+
return fiber.restart();
|
|
1021
|
+
});
|
|
1022
|
+
}
|
|
1023
|
+
};
|
|
1024
|
+
|
|
1025
|
+
// src/reflect.ts
|
|
1026
|
+
function enhanceError(error) {
|
|
1027
|
+
const lines = error.stack.split("\n");
|
|
1028
|
+
lines.splice(0, 2, `Error: ${error.message}`);
|
|
1029
|
+
error.stack = lines.join("\n");
|
|
1030
|
+
return error;
|
|
1031
|
+
}
|
|
1032
|
+
__name(enhanceError, "enhanceError");
|
|
1033
|
+
var RESERVED_WORDS = ["prototype", "then"];
|
|
1034
|
+
function isSpecialProperty(prop) {
|
|
1035
|
+
return typeof prop === "symbol" || RESERVED_WORDS.includes(prop) || parseInt(prop).toString() === prop || prop.startsWith("_");
|
|
1036
|
+
}
|
|
1037
|
+
__name(isSpecialProperty, "isSpecialProperty");
|
|
1038
|
+
var ReflectService = class {
|
|
1039
|
+
constructor(ctx) {
|
|
1040
|
+
this.ctx = ctx;
|
|
1041
|
+
defineProperty5(this, symbols.tracker, {
|
|
1042
|
+
property: "ctx",
|
|
1043
|
+
noShadow: true
|
|
1044
|
+
});
|
|
1045
|
+
this.mixin("reflect", ["get", "set", "provide", "accessor", "mixin"]);
|
|
1046
|
+
this.mixin("fiber", ["runtime", "effect"]);
|
|
1047
|
+
this.mixin("registry", ["inject", "plugin"]);
|
|
1048
|
+
this.mixin("events", ["on", "once", "parallel", "emit", "serial", "bail", "waterfall"]);
|
|
1049
|
+
}
|
|
1050
|
+
ctx;
|
|
1051
|
+
static {
|
|
1052
|
+
__name(this, "ReflectService");
|
|
1053
|
+
}
|
|
1054
|
+
static handler = {
|
|
1055
|
+
get: /* @__PURE__ */ __name((target, prop, ctx) => {
|
|
1056
|
+
if (isSpecialProperty(prop)) {
|
|
1057
|
+
return Reflect.get(target, prop, ctx);
|
|
1058
|
+
}
|
|
1059
|
+
if (Reflect.has(target, prop)) {
|
|
1060
|
+
return getTraceable(ctx, Reflect.get(target, prop, ctx));
|
|
1061
|
+
}
|
|
1062
|
+
const error = new Error(`cannot get property "${prop}" without inject`);
|
|
1063
|
+
try {
|
|
1064
|
+
const def = target.reflect.props[prop];
|
|
1065
|
+
if (def?.type === "accessor") {
|
|
1066
|
+
return def.get.call(ctx, ctx[symbols.receiver], error);
|
|
1067
|
+
}
|
|
1068
|
+
if (!ctx.fiber.runtime) return ctx.reflect.get(prop, false);
|
|
1069
|
+
return ctx.events.waterfall("internal/get", ctx, prop, error, () => {
|
|
1070
|
+
const key = target[symbols.isolate][prop];
|
|
1071
|
+
let fiber = (ctx[symbols.shadow] ?? ctx).fiber;
|
|
1072
|
+
while (true) {
|
|
1073
|
+
const impl = fiber.store?.[prop];
|
|
1074
|
+
if (impl) return getTraceable(ctx, impl.value);
|
|
1075
|
+
if (prop in fiber.inject) {
|
|
1076
|
+
error.message = `cannot get required service "${prop}" in inactive context`;
|
|
1077
|
+
throw error;
|
|
1078
|
+
}
|
|
1079
|
+
if (!fiber.runtime) throw error;
|
|
1080
|
+
if (fiber.parent[symbols.isolate][prop] !== key) throw error;
|
|
1081
|
+
fiber = fiber.parent.fiber;
|
|
1082
|
+
}
|
|
1083
|
+
});
|
|
1084
|
+
} catch (e) {
|
|
1085
|
+
throw e === error ? enhanceError(e) : e;
|
|
1086
|
+
}
|
|
1087
|
+
}, "get"),
|
|
1088
|
+
set: /* @__PURE__ */ __name((target, prop, value, ctx) => {
|
|
1089
|
+
if (isSpecialProperty(prop)) {
|
|
1090
|
+
return Reflect.set(target, prop, value, ctx);
|
|
1091
|
+
}
|
|
1092
|
+
const error = new Error(`cannot set property "${prop}" without provide`);
|
|
1093
|
+
const def = target.reflect.props[prop];
|
|
1094
|
+
if (!def) {
|
|
1095
|
+
if (!ctx.fiber.runtime) return Reflect.set(target, prop, value, ctx);
|
|
1096
|
+
throw enhanceError(error);
|
|
1097
|
+
}
|
|
1098
|
+
try {
|
|
1099
|
+
if (def.type === "accessor") {
|
|
1100
|
+
if (!def.set) return false;
|
|
1101
|
+
return def.set.call(ctx, value, ctx[symbols.receiver], error);
|
|
1102
|
+
}
|
|
1103
|
+
return ctx.events.waterfall("internal/set", ctx, prop, value, error, () => {
|
|
1104
|
+
return ctx.reflect.set(prop, value, error);
|
|
1105
|
+
});
|
|
1106
|
+
} catch (e) {
|
|
1107
|
+
throw e === error ? enhanceError(e) : e;
|
|
1108
|
+
}
|
|
1109
|
+
}, "set"),
|
|
1110
|
+
has: /* @__PURE__ */ __name((target, prop) => {
|
|
1111
|
+
if (isSpecialProperty(prop)) {
|
|
1112
|
+
return Reflect.has(target, prop);
|
|
1113
|
+
}
|
|
1114
|
+
if (Reflect.has(target, prop)) return true;
|
|
1115
|
+
return !!target.reflect.props[prop];
|
|
1116
|
+
}, "has")
|
|
1117
|
+
};
|
|
1118
|
+
store = /* @__PURE__ */ Object.create(null);
|
|
1119
|
+
props = /* @__PURE__ */ Object.create(null);
|
|
1120
|
+
get(name, strict = true) {
|
|
1121
|
+
return getTraceable(this.ctx, this._getImpl(name, strict)?.value);
|
|
1122
|
+
}
|
|
1123
|
+
_getImpl(name, strict = true) {
|
|
1124
|
+
const key = this.ctx[symbols.isolate][name];
|
|
1125
|
+
const impl = key && this.store[key];
|
|
1126
|
+
if (!impl) return;
|
|
1127
|
+
if (strict && impl.fiber.state !== 2 /* ACTIVE */) return;
|
|
1128
|
+
return impl;
|
|
1129
|
+
}
|
|
1130
|
+
set(name, value, error) {
|
|
1131
|
+
const key = this.ctx[symbols.isolate][name];
|
|
1132
|
+
const impl = this.store[key];
|
|
1133
|
+
if (!impl) {
|
|
1134
|
+
throw new Error(`cannot set property "${name}" without provide`);
|
|
1135
|
+
}
|
|
1136
|
+
if (impl.fiber !== this.ctx.fiber) {
|
|
1137
|
+
throw new Error(`cannot set property "${name}" in multiple fibers`);
|
|
1138
|
+
}
|
|
1139
|
+
impl.value = value;
|
|
1140
|
+
return true;
|
|
1141
|
+
}
|
|
1142
|
+
provide(name, value, check) {
|
|
1143
|
+
return this.ctx.fiber.effect(() => {
|
|
1144
|
+
if (!this.props[name]) {
|
|
1145
|
+
this.props[name] ??= { type: "service" };
|
|
1146
|
+
} else if (this.props[name].type !== "service") {
|
|
1147
|
+
throw new Error(`property "${name}" is already declared as ${this.props[name].type}`);
|
|
1148
|
+
}
|
|
1149
|
+
this.props[name] = { type: "service" };
|
|
1150
|
+
this.ctx.root[symbols.isolate][name] ??= Symbol(name);
|
|
1151
|
+
const key = this.ctx[symbols.isolate][name];
|
|
1152
|
+
const impl = { name, value, fiber: this.ctx.fiber, check };
|
|
1153
|
+
if (this.store[key]) {
|
|
1154
|
+
throw new Error(`service "${name}" has been registered at <${this.store[key].fiber.name}>`);
|
|
1155
|
+
}
|
|
1156
|
+
this.store[key] = impl;
|
|
1157
|
+
this.ctx.fiber.store[name] = impl;
|
|
1158
|
+
if (this.ctx.fiber.state === 2 /* ACTIVE */) {
|
|
1159
|
+
this.notify([name]);
|
|
1160
|
+
}
|
|
1161
|
+
return async () => {
|
|
1162
|
+
delete this.store[key];
|
|
1163
|
+
const fibers = this.notify([name]);
|
|
1164
|
+
await Promise.allSettled(fibers.map((fiber) => fiber.await()));
|
|
1165
|
+
delete this.ctx.fiber.store[name];
|
|
1166
|
+
};
|
|
1167
|
+
}, `ctx.provide(${JSON.stringify(name)})`);
|
|
1168
|
+
}
|
|
1169
|
+
notify(names, filter = (ctx, name) => ctx[symbols.isolate][name] === this.ctx[symbols.isolate][name]) {
|
|
1170
|
+
const fibers = [];
|
|
1171
|
+
for (const runtime of this.ctx.registry.values()) {
|
|
1172
|
+
for (const fiber of runtime.fibers) {
|
|
1173
|
+
let hasUpdate = false;
|
|
1174
|
+
for (const name of names) {
|
|
1175
|
+
if (!(name in fiber.inject)) continue;
|
|
1176
|
+
if (!filter(fiber.ctx, name)) continue;
|
|
1177
|
+
hasUpdate = true;
|
|
1178
|
+
fiber._checkImpl(name);
|
|
1179
|
+
}
|
|
1180
|
+
if (!hasUpdate) continue;
|
|
1181
|
+
fiber._refresh();
|
|
1182
|
+
fibers.push(fiber);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
for (const name of names) {
|
|
1186
|
+
const self = Object.create(this.ctx);
|
|
1187
|
+
self[symbols.filter] = (target) => filter(target, name);
|
|
1188
|
+
this.ctx.events.emit(self, "internal/service", name, this._getImpl(name, false)?.value);
|
|
1189
|
+
}
|
|
1190
|
+
return fibers;
|
|
1191
|
+
}
|
|
1192
|
+
accessor(name, options) {
|
|
1193
|
+
return this.ctx.fiber.effect(() => {
|
|
1194
|
+
if (name in this.props) {
|
|
1195
|
+
throw new Error(`property "${name}" is already declared as ${this.props[name].type}`);
|
|
1196
|
+
}
|
|
1197
|
+
this.props[name] = { type: "accessor", ...options };
|
|
1198
|
+
return () => delete this.props[name];
|
|
1199
|
+
}, `ctx.accessor(${JSON.stringify(name)})`);
|
|
1200
|
+
}
|
|
1201
|
+
mixin(source, mixins) {
|
|
1202
|
+
const self = this;
|
|
1203
|
+
return this.ctx.fiber.effect(function* () {
|
|
1204
|
+
const entries = Array.isArray(mixins) ? mixins.map((key) => [key, key]) : Object.entries(mixins);
|
|
1205
|
+
const getTarget = /* @__PURE__ */ __name((ctx, error) => {
|
|
1206
|
+
return ctx[source];
|
|
1207
|
+
}, "getTarget");
|
|
1208
|
+
for (const [key, value] of entries) {
|
|
1209
|
+
yield self.accessor(value, {
|
|
1210
|
+
get(receiver, error) {
|
|
1211
|
+
const service = getTarget(this, error);
|
|
1212
|
+
if (isNullable2(service)) return service;
|
|
1213
|
+
const mixin = receiver ? withProps(receiver, service) : service;
|
|
1214
|
+
const value2 = Reflect.get(service, key, mixin);
|
|
1215
|
+
if (typeof value2 !== "function") return value2;
|
|
1216
|
+
return value2.bind(mixin ?? service);
|
|
1217
|
+
},
|
|
1218
|
+
set(value2, receiver, error) {
|
|
1219
|
+
const service = getTarget(this, error);
|
|
1220
|
+
const mixin = receiver ? withProps(receiver, service) : service;
|
|
1221
|
+
return Reflect.set(service, key, value2, mixin);
|
|
1222
|
+
}
|
|
1223
|
+
});
|
|
1224
|
+
}
|
|
1225
|
+
}, `ctx.mixin(${JSON.stringify(source)})`);
|
|
1226
|
+
}
|
|
1227
|
+
trace(value) {
|
|
1228
|
+
return getTraceable(this.ctx, value);
|
|
1229
|
+
}
|
|
1230
|
+
bind(callback) {
|
|
1231
|
+
return new Proxy(callback, {
|
|
1232
|
+
apply: /* @__PURE__ */ __name((target, thisArg, args) => {
|
|
1233
|
+
return Reflect.apply(target, this.trace(thisArg), args.map((arg) => this.trace(arg)));
|
|
1234
|
+
}, "apply"),
|
|
1235
|
+
construct: /* @__PURE__ */ __name((target, args, newTarget) => {
|
|
1236
|
+
return Reflect.construct(target, args.map((arg) => this.trace(arg)), newTarget);
|
|
1237
|
+
}, "construct")
|
|
1238
|
+
});
|
|
1239
|
+
}
|
|
1240
|
+
};
|
|
1241
|
+
|
|
1242
|
+
// src/registry.ts
|
|
1243
|
+
import { defineProperty as defineProperty6 } from "cosmokit";
|
|
1244
|
+
function isApplicable(object) {
|
|
1245
|
+
return object && typeof object === "object" && typeof object.apply === "function";
|
|
1246
|
+
}
|
|
1247
|
+
__name(isApplicable, "isApplicable");
|
|
1248
|
+
function Inject(name, config) {
|
|
1249
|
+
return function(value, decorator) {
|
|
1250
|
+
if (decorator.kind === "class") {
|
|
1251
|
+
if (!Object.hasOwn(value, "inject")) {
|
|
1252
|
+
defineProperty6(value, "inject", Object.create(Object.getPrototypeOf(value).inject ?? null));
|
|
1253
|
+
defineProperty6(value.inject, symbols.checkProto, true);
|
|
1254
|
+
}
|
|
1255
|
+
value.inject[name] = config;
|
|
1256
|
+
} else if (decorator.kind === "method") {
|
|
1257
|
+
const inject = (value[symbols.metadata] ??= {}).inject ??= /* @__PURE__ */ Object.create(null);
|
|
1258
|
+
inject[name] = config;
|
|
1259
|
+
decorator.addInitializer(function() {
|
|
1260
|
+
const property = this[symbols.tracker]?.property;
|
|
1261
|
+
(this[symbols.initHooks] ??= []).push(() => {
|
|
1262
|
+
this.ctx.inject(inject, (ctx) => {
|
|
1263
|
+
return value.call(property ? withProps(this, { [property]: ctx }) : this);
|
|
1264
|
+
});
|
|
1265
|
+
});
|
|
1266
|
+
});
|
|
1267
|
+
} else {
|
|
1268
|
+
throw new Error("@Inject() can only be used on class or class methods");
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
}
|
|
1272
|
+
__name(Inject, "Inject");
|
|
1273
|
+
((Inject2) => {
|
|
1274
|
+
function resolve(inject, result = /* @__PURE__ */ Object.create(null)) {
|
|
1275
|
+
if (!inject) return result;
|
|
1276
|
+
if (Array.isArray(inject)) {
|
|
1277
|
+
for (const name of inject) {
|
|
1278
|
+
result[name] = null;
|
|
1279
|
+
}
|
|
1280
|
+
} else if (Reflect.has(inject, symbols.checkProto)) {
|
|
1281
|
+
Object.assign(result, resolve(Object.getPrototypeOf(inject)));
|
|
1282
|
+
for (const name of Object.keys(inject)) {
|
|
1283
|
+
result[name] = inject[name] ?? null;
|
|
1284
|
+
}
|
|
1285
|
+
} else {
|
|
1286
|
+
for (const name of Object.keys(inject)) {
|
|
1287
|
+
result[name] = inject[name] ?? null;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
return result;
|
|
1291
|
+
}
|
|
1292
|
+
Inject2.resolve = resolve;
|
|
1293
|
+
__name(resolve, "resolve");
|
|
1294
|
+
})(Inject || (Inject = {}));
|
|
1295
|
+
var RegistryService = class {
|
|
1296
|
+
constructor(ctx) {
|
|
1297
|
+
this.ctx = ctx;
|
|
1298
|
+
defineProperty6(this, symbols.tracker, {
|
|
1299
|
+
property: "ctx",
|
|
1300
|
+
noShadow: true
|
|
1301
|
+
});
|
|
1302
|
+
}
|
|
1303
|
+
ctx;
|
|
1304
|
+
static {
|
|
1305
|
+
__name(this, "RegistryService");
|
|
1306
|
+
}
|
|
1307
|
+
_counter = 0;
|
|
1308
|
+
_internal = /* @__PURE__ */ new Map();
|
|
1309
|
+
get counter() {
|
|
1310
|
+
return ++this._counter;
|
|
1311
|
+
}
|
|
1312
|
+
get size() {
|
|
1313
|
+
return this._internal.size;
|
|
1314
|
+
}
|
|
1315
|
+
resolve(plugin) {
|
|
1316
|
+
try {
|
|
1317
|
+
if (typeof plugin === "function") return plugin;
|
|
1318
|
+
if (isApplicable(plugin)) return plugin.apply;
|
|
1319
|
+
} catch {
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
get(plugin) {
|
|
1323
|
+
const key = this.resolve(plugin);
|
|
1324
|
+
return key && this._internal.get(key);
|
|
1325
|
+
}
|
|
1326
|
+
has(plugin) {
|
|
1327
|
+
const key = this.resolve(plugin);
|
|
1328
|
+
return !!key && this._internal.has(key);
|
|
1329
|
+
}
|
|
1330
|
+
delete(plugin) {
|
|
1331
|
+
const key = this.resolve(plugin);
|
|
1332
|
+
const runtime = key && this._internal.get(key);
|
|
1333
|
+
if (!runtime) return;
|
|
1334
|
+
this._internal.delete(key);
|
|
1335
|
+
for (const fiber of runtime.fibers) {
|
|
1336
|
+
fiber.dispose();
|
|
1337
|
+
}
|
|
1338
|
+
return runtime;
|
|
1339
|
+
}
|
|
1340
|
+
keys() {
|
|
1341
|
+
return this._internal.keys();
|
|
1342
|
+
}
|
|
1343
|
+
values() {
|
|
1344
|
+
return this._internal.values();
|
|
1345
|
+
}
|
|
1346
|
+
entries() {
|
|
1347
|
+
return this._internal.entries();
|
|
1348
|
+
}
|
|
1349
|
+
forEach(callback) {
|
|
1350
|
+
return this._internal.forEach(callback);
|
|
1351
|
+
}
|
|
1352
|
+
inject(inject, callback) {
|
|
1353
|
+
return this.plugin({ inject, apply: callback, name: callback.name });
|
|
1354
|
+
}
|
|
1355
|
+
plugin(plugin, config, getOuterStack = buildOuterStack()) {
|
|
1356
|
+
const callback = this.resolve(plugin);
|
|
1357
|
+
if (!callback) throw new Error('invalid plugin, expect function or object with an "apply" method, received ' + typeof plugin);
|
|
1358
|
+
this.ctx.fiber.assertActive();
|
|
1359
|
+
let runtime = this._internal.get(callback);
|
|
1360
|
+
if (!runtime) {
|
|
1361
|
+
let name = plugin.name;
|
|
1362
|
+
if (name === "apply") name = void 0;
|
|
1363
|
+
runtime = { name, callback, fibers: new DisposableList(), Config: plugin.Config };
|
|
1364
|
+
this._internal.set(callback, runtime);
|
|
1365
|
+
}
|
|
1366
|
+
const fiber = new Fiber(this.ctx, config, Inject.resolve(plugin.inject), runtime, getOuterStack);
|
|
1367
|
+
const wrapped = Object.create(fiber);
|
|
1368
|
+
wrapped.then = (onFulfilled, onRejected) => {
|
|
1369
|
+
return fiber.await().then(onFulfilled, onRejected);
|
|
1370
|
+
};
|
|
1371
|
+
return wrapped;
|
|
1372
|
+
}
|
|
1373
|
+
};
|
|
1374
|
+
|
|
1375
|
+
// src/context.ts
|
|
1376
|
+
var Context = class _Context {
|
|
1377
|
+
static {
|
|
1378
|
+
__name(this, "Context");
|
|
1379
|
+
}
|
|
1380
|
+
static effect = symbols.effect;
|
|
1381
|
+
static filter = symbols.filter;
|
|
1382
|
+
static isolate = symbols.isolate;
|
|
1383
|
+
static intercept = symbols.intercept;
|
|
1384
|
+
static is(value) {
|
|
1385
|
+
return !!value?.[_Context.is];
|
|
1386
|
+
}
|
|
1387
|
+
static {
|
|
1388
|
+
_Context.is[Symbol.toPrimitive] = () => /* @__PURE__ */ Symbol.for("cordis.is");
|
|
1389
|
+
_Context.prototype[_Context.is] = true;
|
|
1390
|
+
}
|
|
1391
|
+
constructor() {
|
|
1392
|
+
this[symbols.isolate] = /* @__PURE__ */ Object.create(null);
|
|
1393
|
+
this[symbols.intercept] = /* @__PURE__ */ Object.create(null);
|
|
1394
|
+
const self = new Proxy(this, ReflectService.handler);
|
|
1395
|
+
this.root = self;
|
|
1396
|
+
this.baseUrl = void 0;
|
|
1397
|
+
this.fiber = new Fiber(self, {}, /* @__PURE__ */ Object.create(null), null, () => []);
|
|
1398
|
+
this.reflect = new ReflectService(self);
|
|
1399
|
+
this.registry = new RegistryService(self);
|
|
1400
|
+
this.events = new EventsService(self);
|
|
1401
|
+
this.logger = new LoggerService(self);
|
|
1402
|
+
this.fiber._disposables.clear();
|
|
1403
|
+
return self;
|
|
1404
|
+
}
|
|
1405
|
+
[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
|
|
1406
|
+
return `Context <${this.fiber.name}>`;
|
|
1407
|
+
}
|
|
1408
|
+
extend(meta = {}) {
|
|
1409
|
+
const shadow = Reflect.getOwnPropertyDescriptor(this, symbols.shadow)?.value;
|
|
1410
|
+
const self = Object.create(getTraceable(this, this));
|
|
1411
|
+
for (const prop of Reflect.ownKeys(meta)) {
|
|
1412
|
+
Object.defineProperty(self, prop, Reflect.getOwnPropertyDescriptor(meta, prop));
|
|
1413
|
+
}
|
|
1414
|
+
if (!shadow) return self;
|
|
1415
|
+
return Object.assign(Object.create(self), { [symbols.shadow]: shadow });
|
|
1416
|
+
}
|
|
1417
|
+
isolate(name, label) {
|
|
1418
|
+
const shadow = Object.create(this[symbols.isolate]);
|
|
1419
|
+
shadow[name] = label ?? Symbol(name);
|
|
1420
|
+
return this.extend({ [symbols.isolate]: shadow });
|
|
1421
|
+
}
|
|
1422
|
+
intercept(name, config) {
|
|
1423
|
+
const intercept = Object.create(this[symbols.intercept]);
|
|
1424
|
+
intercept[name] = config;
|
|
1425
|
+
return this.extend({ [symbols.intercept]: intercept });
|
|
1426
|
+
}
|
|
1427
|
+
};
|
|
1428
|
+
|
|
1429
|
+
// src/service.ts
|
|
1430
|
+
import { defineProperty as defineProperty7 } from "cosmokit";
|
|
1431
|
+
var Service = class _Service {
|
|
1432
|
+
constructor(ctx, name) {
|
|
1433
|
+
this.ctx = ctx;
|
|
1434
|
+
name ??= this.constructor["provide"];
|
|
1435
|
+
let self = this;
|
|
1436
|
+
const tracker = {
|
|
1437
|
+
associate: name,
|
|
1438
|
+
property: "ctx"
|
|
1439
|
+
};
|
|
1440
|
+
if (self[symbols.invoke]) {
|
|
1441
|
+
self = createCallable(name, joinPrototype(Object.getPrototypeOf(this), Function.prototype), tracker);
|
|
1442
|
+
}
|
|
1443
|
+
self.ctx = ctx;
|
|
1444
|
+
self.name = name;
|
|
1445
|
+
defineProperty7(self, symbols.tracker, tracker);
|
|
1446
|
+
self.ctx.reflect.provide(name, self, this[symbols.check]);
|
|
1447
|
+
return self;
|
|
1448
|
+
}
|
|
1449
|
+
ctx;
|
|
1450
|
+
static {
|
|
1451
|
+
__name(this, "Service");
|
|
1452
|
+
}
|
|
1453
|
+
static init = symbols.init;
|
|
1454
|
+
static check = symbols.check;
|
|
1455
|
+
static config = symbols.config;
|
|
1456
|
+
static invoke = symbols.invoke;
|
|
1457
|
+
static extend = symbols.extend;
|
|
1458
|
+
static tracker = symbols.tracker;
|
|
1459
|
+
static resolveConfig = symbols.resolveConfig;
|
|
1460
|
+
name;
|
|
1461
|
+
[symbols.filter](ctx) {
|
|
1462
|
+
return ctx[symbols.isolate][this.name] === this.ctx[symbols.isolate][this.name];
|
|
1463
|
+
}
|
|
1464
|
+
[symbols.extend](props) {
|
|
1465
|
+
let self;
|
|
1466
|
+
if (this[_Service.invoke]) {
|
|
1467
|
+
self = createCallable(this.name, this, this[symbols.tracker]);
|
|
1468
|
+
} else {
|
|
1469
|
+
self = Object.create(this);
|
|
1470
|
+
}
|
|
1471
|
+
return Object.assign(self, props);
|
|
1472
|
+
}
|
|
1473
|
+
[symbols.resolveConfig](base, head) {
|
|
1474
|
+
let intercept = this.ctx[Context.intercept];
|
|
1475
|
+
const configs = [];
|
|
1476
|
+
while (this.name in intercept) {
|
|
1477
|
+
if (Object.hasOwn(intercept, this.name)) {
|
|
1478
|
+
configs.unshift(intercept[this.name]);
|
|
1479
|
+
}
|
|
1480
|
+
intercept = Object.getPrototypeOf(intercept);
|
|
1481
|
+
}
|
|
1482
|
+
if (base) configs.unshift(base);
|
|
1483
|
+
if (head) configs.push(head);
|
|
1484
|
+
if (this["Config"]?.merge) {
|
|
1485
|
+
return this["Config"].merge(...configs);
|
|
1486
|
+
} else {
|
|
1487
|
+
return Object.assign({}, ...configs);
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
static [Symbol.hasInstance](instance) {
|
|
1491
|
+
if (!instance) return false;
|
|
1492
|
+
let constructor = instance.constructor;
|
|
1493
|
+
while (constructor) {
|
|
1494
|
+
constructor = constructor.prototype?.constructor;
|
|
1495
|
+
if (constructor === this) return true;
|
|
1496
|
+
constructor &&= Object.getPrototypeOf(constructor);
|
|
1497
|
+
}
|
|
1498
|
+
return false;
|
|
1499
|
+
}
|
|
1500
|
+
};
|
|
1501
|
+
export {
|
|
1502
|
+
Context,
|
|
1503
|
+
CordisError,
|
|
1504
|
+
DisposableList,
|
|
1505
|
+
EventsService,
|
|
1506
|
+
Fiber,
|
|
1507
|
+
FiberState,
|
|
1508
|
+
Inject,
|
|
1509
|
+
Logger,
|
|
1510
|
+
LoggerLevel,
|
|
1511
|
+
LoggerService,
|
|
1512
|
+
RegistryService,
|
|
1513
|
+
Service,
|
|
1514
|
+
ValidationError,
|
|
1515
|
+
buildOuterStack,
|
|
1516
|
+
c16,
|
|
1517
|
+
c256,
|
|
1518
|
+
composeError,
|
|
1519
|
+
createCallable,
|
|
1520
|
+
defaultFormatters,
|
|
1521
|
+
getPropertyDescriptor,
|
|
1522
|
+
getTraceable,
|
|
1523
|
+
isBailed,
|
|
1524
|
+
isConstructor,
|
|
1525
|
+
isObject,
|
|
1526
|
+
joinPrototype,
|
|
1527
|
+
resolveConfig,
|
|
1528
|
+
symbols,
|
|
1529
|
+
withProps
|
|
1530
|
+
};
|