brass-runtime 1.15.0 → 1.16.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/README.md +409 -137
- package/dist/agent/cli/main.cjs +40 -35
- package/dist/agent/cli/main.js +9 -4
- package/dist/agent/cli/main.mjs +9 -4
- package/dist/agent/index.cjs +8 -4
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/index.js +7 -3
- package/dist/agent/index.mjs +7 -3
- package/dist/{chunk-PPUXIH5R.js → chunk-2WC63LJK.mjs} +11 -7
- package/dist/chunk-3RG5ZIWI.js +10 -0
- package/dist/chunk-45F7OKGT.cjs +104 -0
- package/dist/chunk-5YOQOXEQ.cjs +2491 -0
- package/dist/{chunk-STVLQ3XD.cjs → chunk-7HUOJA4W.cjs} +78 -74
- package/dist/{chunk-BMH5AV44.js → chunk-7LVI2GIN.js} +251 -370
- package/dist/chunk-7TL2LHQJ.js +2491 -0
- package/dist/chunk-7V4KY4RL.mjs +104 -0
- package/dist/chunk-7XOPAB5Q.js +2143 -0
- package/dist/chunk-CCKHV5BT.mjs +193 -0
- package/dist/{chunk-AR22SXML.js → chunk-CY33PGEX.mjs} +488 -421
- package/dist/chunk-DJQ7OMMB.cjs +144 -0
- package/dist/chunk-F5EUMJL7.mjs +2143 -0
- package/dist/chunk-FM4W4QPL.js +193 -0
- package/dist/{chunk-TO7IKXYT.js → chunk-G3XGCZDQ.js} +1 -1
- package/dist/{chunk-BDF4AMWX.mjs → chunk-G6IQOE4P.mjs} +251 -370
- package/dist/chunk-GOV47PPB.mjs +552 -0
- package/dist/chunk-H55LI6WY.js +93 -0
- package/dist/chunk-IJT6RRQ5.cjs +93 -0
- package/dist/{chunk-ELOOF35R.mjs → chunk-J3H54ZRV.mjs} +1 -1
- package/dist/chunk-JF4XXPZ5.cjs +552 -0
- package/dist/chunk-JNFRRJYH.cjs +2143 -0
- package/dist/chunk-JX3LZQJH.cjs +354 -0
- package/dist/chunk-K2T3DV26.mjs +93 -0
- package/dist/chunk-KCPT2D6G.js +552 -0
- package/dist/chunk-MWXMNYJS.cjs +1110 -0
- package/dist/{chunk-VEZNF5GZ.cjs → chunk-N6VHMOWB.cjs} +130 -126
- package/dist/{chunk-3QMOKAS5.js → chunk-NC5SDRYE.js} +9 -5
- package/dist/chunk-NOYZIMUJ.mjs +144 -0
- package/dist/{chunk-R3R2FVLG.cjs → chunk-NYL4D7SK.cjs} +5 -5
- package/dist/chunk-OBGZSXTJ.cjs +10 -0
- package/dist/{chunk-4NHES7VK.mjs → chunk-OOGJ73B6.js} +11 -7
- package/dist/chunk-PNVFW245.js +144 -0
- package/dist/chunk-PRWCB3QL.mjs +2491 -0
- package/dist/{chunk-JFPU5GQI.mjs → chunk-QY5FKYEQ.js} +488 -421
- package/dist/chunk-ROJC3NBJ.js +104 -0
- package/dist/chunk-SPUEME2B.cjs +343 -0
- package/dist/chunk-TDVMADDN.js +343 -0
- package/dist/chunk-TVN5I4U6.cjs +193 -0
- package/dist/chunk-U5KWK3PX.mjs +343 -0
- package/dist/chunk-VFIUZG7J.mjs +354 -0
- package/dist/{chunk-TGIFUAK4.cjs → chunk-WQ5QNU5R.cjs} +459 -578
- package/dist/chunk-XDZOO4L5.js +354 -0
- package/dist/chunk-Y6FXYEAI.mjs +10 -0
- package/dist/{chunk-K6M7MDZ4.mjs → chunk-ZGLD4TVZ.mjs} +9 -5
- package/dist/client-CtFmoDvM.d.ts +645 -0
- package/dist/core/index.cjs +72 -4
- package/dist/core/index.d.ts +92 -198
- package/dist/core/index.js +106 -38
- package/dist/core/index.mjs +106 -38
- package/dist/{effect-CMOQKX8y.d.ts → effect-CGNl5Rqp.d.ts} +107 -1
- package/dist/effectRunner-3ZHAD3LE.cjs +8 -0
- package/dist/effectRunner-A4CHJXJI.js +8 -0
- package/dist/effectRunner-OPUF6QRN.mjs +8 -0
- package/dist/http/index.cjs +2189 -1271
- package/dist/http/index.d.ts +830 -270
- package/dist/http/index.js +2008 -1090
- package/dist/http/index.mjs +2008 -1090
- package/dist/http/testing.cjs +159 -0
- package/dist/http/testing.d.ts +42 -0
- package/dist/http/testing.js +159 -0
- package/dist/http/testing.mjs +159 -0
- package/dist/index.cjs +246 -178
- package/dist/index.d.ts +9 -35
- package/dist/index.js +120 -52
- package/dist/index.mjs +120 -52
- package/dist/observability/index.cjs +677 -0
- package/dist/observability/index.d.ts +79 -0
- package/dist/observability/index.js +677 -0
- package/dist/observability/index.mjs +677 -0
- package/dist/schedule-Fque9Abz.d.ts +70 -0
- package/dist/schema/index.cjs +25 -0
- package/dist/schema/index.d.ts +177 -0
- package/dist/schema/index.js +25 -0
- package/dist/schema/index.mjs +25 -0
- package/dist/server-C8hDXA74.d.ts +674 -0
- package/dist/{stream-FQm9h4Mg.d.ts → stream-dvSs0QS5.d.ts} +1 -1
- package/dist/tracer-B5tRH9H7.d.ts +230 -0
- package/dist/tracing-Dt9S_6V8.d.ts +148 -0
- package/package.json +27 -1
- package/dist/chunk-BDYEENHT.js +0 -224
- package/dist/chunk-MS34J5LY.cjs +0 -224
- package/dist/chunk-UMAZLXAB.mjs +0 -224
- package/dist/chunk-XPZNXSVN.cjs +0 -1043
- package/dist/tracing-DNT9jEbr.d.ts +0 -106
|
@@ -0,0 +1,1110 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3;
|
|
2
|
+
|
|
3
|
+
var _chunkJX3LZQJHcjs = require('./chunk-JX3LZQJH.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunkWQ5QNU5Rcjs = require('./chunk-WQ5QNU5R.cjs');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
var _chunkDJQ7OMMBcjs = require('./chunk-DJQ7OMMB.cjs');
|
|
18
|
+
|
|
19
|
+
// src/core/types/cancel.ts
|
|
20
|
+
function makeCancelToken() {
|
|
21
|
+
let cancelled = false;
|
|
22
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
23
|
+
const cancel = () => {
|
|
24
|
+
if (cancelled) return;
|
|
25
|
+
cancelled = true;
|
|
26
|
+
listeners.forEach((f) => f());
|
|
27
|
+
listeners.clear();
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
isCancelled: () => cancelled,
|
|
31
|
+
onCancel: (f) => {
|
|
32
|
+
if (cancelled) {
|
|
33
|
+
try {
|
|
34
|
+
f();
|
|
35
|
+
} catch (e2) {
|
|
36
|
+
}
|
|
37
|
+
return () => {
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
listeners.add(f);
|
|
41
|
+
return () => {
|
|
42
|
+
listeners.delete(f);
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
cancel
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function linkAbortController(token, ac) {
|
|
49
|
+
return token.onCancel(() => ac.abort());
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/core/runtime/linkedQueue.ts
|
|
53
|
+
var LinkedQueue = (_class = class {constructor() { _class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this); }
|
|
54
|
+
__init() {this.head = null}
|
|
55
|
+
__init2() {this.tail = null}
|
|
56
|
+
__init3() {this.len = 0}
|
|
57
|
+
get length() {
|
|
58
|
+
return this.len;
|
|
59
|
+
}
|
|
60
|
+
isEmpty() {
|
|
61
|
+
return this.len === 0;
|
|
62
|
+
}
|
|
63
|
+
push(value) {
|
|
64
|
+
const node = { value, next: null, prev: this.tail, removed: false };
|
|
65
|
+
if (this.tail) this.tail.next = node;
|
|
66
|
+
else this.head = node;
|
|
67
|
+
this.tail = node;
|
|
68
|
+
this.len++;
|
|
69
|
+
return node;
|
|
70
|
+
}
|
|
71
|
+
shift() {
|
|
72
|
+
const h = this.head;
|
|
73
|
+
if (!h) return void 0;
|
|
74
|
+
this.unlink(h);
|
|
75
|
+
return h.value;
|
|
76
|
+
}
|
|
77
|
+
remove(node) {
|
|
78
|
+
if (node.removed) return;
|
|
79
|
+
this.unlink(node);
|
|
80
|
+
}
|
|
81
|
+
unlink(node) {
|
|
82
|
+
node.removed = true;
|
|
83
|
+
const { prev, next } = node;
|
|
84
|
+
if (prev) prev.next = next;
|
|
85
|
+
else this.head = next;
|
|
86
|
+
if (next) next.prev = prev;
|
|
87
|
+
else this.tail = prev;
|
|
88
|
+
node.next = null;
|
|
89
|
+
node.prev = null;
|
|
90
|
+
this.len--;
|
|
91
|
+
}
|
|
92
|
+
}, _class);
|
|
93
|
+
|
|
94
|
+
// src/core/runtime/semaphore.ts
|
|
95
|
+
function makeSemaphore(n) {
|
|
96
|
+
const capacity = Math.max(1, Math.floor(n));
|
|
97
|
+
let available = capacity;
|
|
98
|
+
let totalAcquired = 0;
|
|
99
|
+
let totalReleased = 0;
|
|
100
|
+
const waiters = new LinkedQueue();
|
|
101
|
+
const acquire = () => {
|
|
102
|
+
return _chunkDJQ7OMMBcjs.asyncEffect.call(void 0, (_env, cb) => {
|
|
103
|
+
if (available > 0) {
|
|
104
|
+
available--;
|
|
105
|
+
totalAcquired++;
|
|
106
|
+
cb({ _tag: "Success", value: void 0 });
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const node = waiters.push(cb);
|
|
110
|
+
return () => {
|
|
111
|
+
waiters.remove(node);
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
const release = () => {
|
|
116
|
+
totalReleased++;
|
|
117
|
+
if (waiters.length > 0) {
|
|
118
|
+
const waiter = waiters.shift();
|
|
119
|
+
totalAcquired++;
|
|
120
|
+
waiter({ _tag: "Success", value: void 0 });
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
available++;
|
|
124
|
+
};
|
|
125
|
+
const acquirePermit = () => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0,
|
|
126
|
+
acquire(),
|
|
127
|
+
() => _chunkDJQ7OMMBcjs.asyncEffect.call(void 0, (_env, cb) => {
|
|
128
|
+
let released = false;
|
|
129
|
+
const releaseOnce = () => {
|
|
130
|
+
if (released) return;
|
|
131
|
+
released = true;
|
|
132
|
+
release();
|
|
133
|
+
};
|
|
134
|
+
const fiber = _chunkWQ5QNU5Rcjs.getCurrentFiber.call(void 0, );
|
|
135
|
+
_optionalChain([fiber, 'optionalAccess', _2 => _2.addFinalizer, 'call', _3 => _3(() => {
|
|
136
|
+
releaseOnce();
|
|
137
|
+
})]);
|
|
138
|
+
cb({ _tag: "Success", value: releaseOnce });
|
|
139
|
+
})
|
|
140
|
+
);
|
|
141
|
+
const withPermit = (effect) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0,
|
|
142
|
+
acquirePermit(),
|
|
143
|
+
(releaseOnce) => _chunkDJQ7OMMBcjs.asyncFold.call(void 0,
|
|
144
|
+
effect,
|
|
145
|
+
(error) => {
|
|
146
|
+
releaseOnce();
|
|
147
|
+
return _chunkDJQ7OMMBcjs.asyncFail.call(void 0, error);
|
|
148
|
+
},
|
|
149
|
+
(value) => {
|
|
150
|
+
releaseOnce();
|
|
151
|
+
return _chunkDJQ7OMMBcjs.asyncSucceed.call(void 0, value);
|
|
152
|
+
}
|
|
153
|
+
)
|
|
154
|
+
);
|
|
155
|
+
return {
|
|
156
|
+
capacity,
|
|
157
|
+
available: () => available,
|
|
158
|
+
waiting: () => waiters.length,
|
|
159
|
+
withPermit,
|
|
160
|
+
acquire,
|
|
161
|
+
release
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/core/runtime/ref.ts
|
|
166
|
+
function makeRef(initial) {
|
|
167
|
+
let value = initial;
|
|
168
|
+
return {
|
|
169
|
+
get: () => _chunkDJQ7OMMBcjs.asyncSync.call(void 0, () => value),
|
|
170
|
+
set: (v) => _chunkDJQ7OMMBcjs.asyncSync.call(void 0, () => {
|
|
171
|
+
value = v;
|
|
172
|
+
}),
|
|
173
|
+
update: (f) => _chunkDJQ7OMMBcjs.asyncSync.call(void 0, () => {
|
|
174
|
+
value = f(value);
|
|
175
|
+
return value;
|
|
176
|
+
}),
|
|
177
|
+
modify: (f) => _chunkDJQ7OMMBcjs.asyncSync.call(void 0, () => {
|
|
178
|
+
const [result, next] = f(value);
|
|
179
|
+
value = next;
|
|
180
|
+
return result;
|
|
181
|
+
}),
|
|
182
|
+
unsafeGet: () => value
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function derivedRef(parent, get, set) {
|
|
186
|
+
return {
|
|
187
|
+
get: () => _chunkDJQ7OMMBcjs.asyncSync.call(void 0, () => get(parent.unsafeGet())),
|
|
188
|
+
set: (b) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0, parent.get(), (current) => parent.set(set(current, b))),
|
|
189
|
+
update: (f) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0, parent.get(), (parentVal) => {
|
|
190
|
+
const newB = f(get(parentVal));
|
|
191
|
+
return _chunkDJQ7OMMBcjs.asyncMap.call(void 0, parent.set(set(parentVal, newB)), () => newB);
|
|
192
|
+
}),
|
|
193
|
+
modify: (f) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0, parent.get(), (parentVal) => {
|
|
194
|
+
const [result, newB] = f(get(parentVal));
|
|
195
|
+
return _chunkDJQ7OMMBcjs.asyncMap.call(void 0, parent.set(set(parentVal, newB)), () => result);
|
|
196
|
+
}),
|
|
197
|
+
unsafeGet: () => get(parent.unsafeGet())
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/core/runtime/shutdown.ts
|
|
202
|
+
async function gracefulShutdown(runtime, config = {}) {
|
|
203
|
+
const timeoutMs = _nullishCoalesce(config.timeoutMs, () => ( 3e4));
|
|
204
|
+
const startedAt = Date.now();
|
|
205
|
+
_optionalChain([config, 'access', _4 => _4.onStart, 'optionalCall', _5 => _5()]);
|
|
206
|
+
let timedOut = false;
|
|
207
|
+
const shutdownPromise = (async () => {
|
|
208
|
+
try {
|
|
209
|
+
await runtime.shutdown();
|
|
210
|
+
} catch (e3) {
|
|
211
|
+
}
|
|
212
|
+
})();
|
|
213
|
+
const timeoutPromise = new Promise((resolve) => {
|
|
214
|
+
setTimeout(() => {
|
|
215
|
+
timedOut = true;
|
|
216
|
+
resolve();
|
|
217
|
+
}, timeoutMs);
|
|
218
|
+
});
|
|
219
|
+
await Promise.race([shutdownPromise, timeoutPromise]);
|
|
220
|
+
const completedAt = Date.now();
|
|
221
|
+
const stats = {
|
|
222
|
+
startedAt,
|
|
223
|
+
completedAt,
|
|
224
|
+
elapsedMs: completedAt - startedAt,
|
|
225
|
+
timedOut
|
|
226
|
+
};
|
|
227
|
+
if (timedOut) {
|
|
228
|
+
_optionalChain([config, 'access', _6 => _6.onTimeout, 'optionalCall', _7 => _7(stats)]);
|
|
229
|
+
} else {
|
|
230
|
+
_optionalChain([config, 'access', _8 => _8.onComplete, 'optionalCall', _9 => _9(stats)]);
|
|
231
|
+
}
|
|
232
|
+
return stats;
|
|
233
|
+
}
|
|
234
|
+
function registerShutdownHooks(runtime, config = {}) {
|
|
235
|
+
let shuttingDown = false;
|
|
236
|
+
const handler = (signal) => {
|
|
237
|
+
if (shuttingDown) {
|
|
238
|
+
process.exit(1);
|
|
239
|
+
}
|
|
240
|
+
shuttingDown = true;
|
|
241
|
+
console.log(`
|
|
242
|
+
[brass-runtime] Received ${signal}, shutting down gracefully...`);
|
|
243
|
+
gracefulShutdown(runtime, {
|
|
244
|
+
...config,
|
|
245
|
+
onComplete: (stats) => {
|
|
246
|
+
_optionalChain([config, 'access', _10 => _10.onComplete, 'optionalCall', _11 => _11(stats)]);
|
|
247
|
+
if (!config.onComplete) {
|
|
248
|
+
console.log(`[brass-runtime] Shutdown complete (${stats.elapsedMs}ms)`);
|
|
249
|
+
process.exit(0);
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
onTimeout: (stats) => {
|
|
253
|
+
_optionalChain([config, 'access', _12 => _12.onTimeout, 'optionalCall', _13 => _13(stats)]);
|
|
254
|
+
if (!config.onTimeout) {
|
|
255
|
+
console.log(`[brass-runtime] Shutdown timed out after ${stats.elapsedMs}ms, forcing exit`);
|
|
256
|
+
process.exit(1);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
process.on("SIGTERM", () => handler("SIGTERM"));
|
|
262
|
+
process.on("SIGINT", () => handler("SIGINT"));
|
|
263
|
+
return () => {
|
|
264
|
+
process.removeAllListeners("SIGTERM");
|
|
265
|
+
process.removeAllListeners("SIGINT");
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// src/core/runtime/testing.ts
|
|
270
|
+
function makeTestRuntime(env, options) {
|
|
271
|
+
const runtime = _chunkWQ5QNU5Rcjs.Runtime.make(_nullishCoalesce(env, () => ( {})));
|
|
272
|
+
const run = (effect) => runtime.toPromise(effect);
|
|
273
|
+
const runExit = (effect) => new Promise((resolve) => {
|
|
274
|
+
runtime.unsafeRunAsync(effect, resolve);
|
|
275
|
+
});
|
|
276
|
+
return { runtime, run, runExit };
|
|
277
|
+
}
|
|
278
|
+
async function assertSucceeds(effect, expected, runtime) {
|
|
279
|
+
const rt = _nullishCoalesce(runtime, () => ( _chunkWQ5QNU5Rcjs.Runtime.make({})));
|
|
280
|
+
const exit = await new Promise((resolve) => {
|
|
281
|
+
rt.unsafeRunAsync(effect, resolve);
|
|
282
|
+
});
|
|
283
|
+
if (exit._tag !== "Success") {
|
|
284
|
+
throw new Error(`Expected success with ${JSON.stringify(expected)}, got failure: ${JSON.stringify(exit.cause)}`);
|
|
285
|
+
}
|
|
286
|
+
if (JSON.stringify(exit.value) !== JSON.stringify(expected)) {
|
|
287
|
+
throw new Error(`Expected ${JSON.stringify(expected)}, got ${JSON.stringify(exit.value)}`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
async function assertFails(effect, expectedError, runtime) {
|
|
291
|
+
const rt = _nullishCoalesce(runtime, () => ( _chunkWQ5QNU5Rcjs.Runtime.make({})));
|
|
292
|
+
const exit = await new Promise((resolve) => {
|
|
293
|
+
rt.unsafeRunAsync(effect, resolve);
|
|
294
|
+
});
|
|
295
|
+
if (exit._tag !== "Failure") {
|
|
296
|
+
throw new Error(`Expected failure with ${JSON.stringify(expectedError)}, got success: ${JSON.stringify(exit.value)}`);
|
|
297
|
+
}
|
|
298
|
+
const error = exit.cause.error;
|
|
299
|
+
if (JSON.stringify(error) !== JSON.stringify(expectedError)) {
|
|
300
|
+
throw new Error(`Expected error ${JSON.stringify(expectedError)}, got ${JSON.stringify(error)}`);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async function assertFailsWith(effect, predicate, runtime) {
|
|
304
|
+
const rt = _nullishCoalesce(runtime, () => ( _chunkWQ5QNU5Rcjs.Runtime.make({})));
|
|
305
|
+
const exit = await new Promise((resolve) => {
|
|
306
|
+
rt.unsafeRunAsync(effect, resolve);
|
|
307
|
+
});
|
|
308
|
+
if (exit._tag !== "Failure") {
|
|
309
|
+
throw new Error(`Expected failure, got success: ${JSON.stringify(exit.value)}`);
|
|
310
|
+
}
|
|
311
|
+
const error = exit.cause.error;
|
|
312
|
+
if (!predicate(error)) {
|
|
313
|
+
throw new Error(`Error did not match predicate: ${JSON.stringify(error)}`);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
async function assertCompletesWithin(effect, maxMs, runtime) {
|
|
317
|
+
const rt = _nullishCoalesce(runtime, () => ( _chunkWQ5QNU5Rcjs.Runtime.make({})));
|
|
318
|
+
const start = performance.now();
|
|
319
|
+
const result = await rt.toPromise(effect);
|
|
320
|
+
const elapsed = performance.now() - start;
|
|
321
|
+
if (elapsed > maxMs) {
|
|
322
|
+
throw new Error(`Effect took ${elapsed.toFixed(1)}ms, expected < ${maxMs}ms`);
|
|
323
|
+
}
|
|
324
|
+
return result;
|
|
325
|
+
}
|
|
326
|
+
function flakyEffect(failCount, successValue, errorValue) {
|
|
327
|
+
let calls = 0;
|
|
328
|
+
return _chunkDJQ7OMMBcjs.asyncEffect.call(void 0, (_env, cb) => {
|
|
329
|
+
calls++;
|
|
330
|
+
if (calls <= failCount) {
|
|
331
|
+
cb({ _tag: "Failure", cause: { _tag: "Fail", error: errorValue } });
|
|
332
|
+
} else {
|
|
333
|
+
cb({ _tag: "Success", value: successValue });
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
function delayedEffect(ms, value) {
|
|
338
|
+
return _chunkDJQ7OMMBcjs.asyncEffect.call(void 0, (_env, cb) => {
|
|
339
|
+
const id = setTimeout(() => cb({ _tag: "Success", value }), ms);
|
|
340
|
+
return () => clearTimeout(id);
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
function neverEffect() {
|
|
344
|
+
return _chunkDJQ7OMMBcjs.asyncEffect.call(void 0, () => {
|
|
345
|
+
return () => {
|
|
346
|
+
};
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// src/core/runtime/layer.ts
|
|
351
|
+
function layer(acquire, release) {
|
|
352
|
+
return {
|
|
353
|
+
_tag: "Layer",
|
|
354
|
+
build: (_deps) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0, acquire(), (service) => _chunkDJQ7OMMBcjs.asyncSucceed.call(void 0, {
|
|
355
|
+
service,
|
|
356
|
+
release: release ? () => release(service) : () => _chunkDJQ7OMMBcjs.unit.call(void 0, )
|
|
357
|
+
}))
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
function layerFrom() {
|
|
361
|
+
return (acquire, release) => ({
|
|
362
|
+
_tag: "Layer",
|
|
363
|
+
build: (deps) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0, acquire(deps), (service) => _chunkDJQ7OMMBcjs.asyncSucceed.call(void 0, {
|
|
364
|
+
service,
|
|
365
|
+
release: release ? () => release(service) : () => _chunkDJQ7OMMBcjs.unit.call(void 0, )
|
|
366
|
+
}))
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
function layerSucceed(value) {
|
|
370
|
+
return {
|
|
371
|
+
_tag: "Layer",
|
|
372
|
+
build: () => _chunkDJQ7OMMBcjs.asyncSucceed.call(void 0, { service: value, release: () => _chunkDJQ7OMMBcjs.unit.call(void 0, ) })
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
function layerFail(error) {
|
|
376
|
+
return {
|
|
377
|
+
_tag: "Layer",
|
|
378
|
+
build: () => _chunkDJQ7OMMBcjs.asyncFail.call(void 0, error)
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
function compose(from, to) {
|
|
382
|
+
return {
|
|
383
|
+
_tag: "Layer",
|
|
384
|
+
build: (deps) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0,
|
|
385
|
+
from.build(deps),
|
|
386
|
+
({ service: mid, release: releaseMid }) => _chunkDJQ7OMMBcjs.asyncFold.call(void 0,
|
|
387
|
+
to.build(mid),
|
|
388
|
+
(error) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0, releaseMid(), () => _chunkDJQ7OMMBcjs.asyncFail.call(void 0, error)),
|
|
389
|
+
({ service: out, release: releaseOut }) => _chunkDJQ7OMMBcjs.asyncSucceed.call(void 0, {
|
|
390
|
+
service: out,
|
|
391
|
+
release: () => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0, releaseOut(), () => releaseMid())
|
|
392
|
+
})
|
|
393
|
+
)
|
|
394
|
+
)
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
function merge(left, right) {
|
|
398
|
+
return {
|
|
399
|
+
_tag: "Layer",
|
|
400
|
+
build: (deps) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0,
|
|
401
|
+
left.build(deps),
|
|
402
|
+
({ service: a, release: releaseA }) => _chunkDJQ7OMMBcjs.asyncFold.call(void 0,
|
|
403
|
+
right.build(deps),
|
|
404
|
+
(error) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0, releaseA(), () => _chunkDJQ7OMMBcjs.asyncFail.call(void 0, error)),
|
|
405
|
+
({ service: b, release: releaseB }) => _chunkDJQ7OMMBcjs.asyncSucceed.call(void 0, {
|
|
406
|
+
service: { ...a, ...b },
|
|
407
|
+
release: () => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0, releaseB(), () => releaseA())
|
|
408
|
+
})
|
|
409
|
+
)
|
|
410
|
+
)
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
function mapLayer(l, f) {
|
|
414
|
+
return {
|
|
415
|
+
_tag: "Layer",
|
|
416
|
+
build: (deps) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0,
|
|
417
|
+
l.build(deps),
|
|
418
|
+
({ service, release }) => _chunkDJQ7OMMBcjs.asyncSucceed.call(void 0, { service: f(service), release })
|
|
419
|
+
)
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
function provideLayer(l, use, deps) {
|
|
423
|
+
return _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0,
|
|
424
|
+
l.build(_nullishCoalesce(deps, () => ( {}))),
|
|
425
|
+
({ service, release }) => _chunkDJQ7OMMBcjs.asyncFold.call(void 0,
|
|
426
|
+
use(service),
|
|
427
|
+
(error) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0, release(), () => _chunkDJQ7OMMBcjs.asyncFail.call(void 0, error)),
|
|
428
|
+
(value) => _chunkDJQ7OMMBcjs.asyncFlatMap.call(void 0, release(), () => _chunkDJQ7OMMBcjs.asyncSucceed.call(void 0, value))
|
|
429
|
+
)
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// src/core/runtime/workerPool.ts
|
|
434
|
+
function makeWorkerPool(config = {}) {
|
|
435
|
+
const size = _nullishCoalesce(config.size, () => ( 4));
|
|
436
|
+
const maxQueue = _nullishCoalesce(config.maxQueue, () => ( 1e3));
|
|
437
|
+
const taskTimeoutMs = _nullishCoalesce(config.taskTimeoutMs, () => ( 3e4));
|
|
438
|
+
let closed = false;
|
|
439
|
+
let busy = 0;
|
|
440
|
+
let completed = 0;
|
|
441
|
+
let failed = 0;
|
|
442
|
+
let timedOut = 0;
|
|
443
|
+
const queue = [];
|
|
444
|
+
const processNext = () => {
|
|
445
|
+
if (queue.length === 0 || busy >= size) return;
|
|
446
|
+
const task = queue.shift();
|
|
447
|
+
busy++;
|
|
448
|
+
setImmediate(() => {
|
|
449
|
+
if (task.timeoutId) clearTimeout(task.timeoutId);
|
|
450
|
+
try {
|
|
451
|
+
const result = task.fn();
|
|
452
|
+
busy--;
|
|
453
|
+
completed++;
|
|
454
|
+
task.resolve(result);
|
|
455
|
+
processNext();
|
|
456
|
+
} catch (e) {
|
|
457
|
+
busy--;
|
|
458
|
+
failed++;
|
|
459
|
+
task.reject({ _tag: "WorkerTaskError", message: String(e) });
|
|
460
|
+
processNext();
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
};
|
|
464
|
+
const execute = (fn) => {
|
|
465
|
+
if (closed) return _chunkDJQ7OMMBcjs.asyncFail.call(void 0, { _tag: "WorkerPoolClosed" });
|
|
466
|
+
if (queue.length >= maxQueue) {
|
|
467
|
+
return _chunkDJQ7OMMBcjs.asyncFail.call(void 0, { _tag: "WorkerPoolFull", queued: queue.length });
|
|
468
|
+
}
|
|
469
|
+
return _chunkDJQ7OMMBcjs.asyncEffect.call(void 0, (_env, cb) => {
|
|
470
|
+
const task = {
|
|
471
|
+
fn,
|
|
472
|
+
resolve: (value) => cb({ _tag: "Success", value }),
|
|
473
|
+
reject: (error) => cb({ _tag: "Failure", cause: { _tag: "Fail", error } })
|
|
474
|
+
};
|
|
475
|
+
task.timeoutId = setTimeout(() => {
|
|
476
|
+
const idx = queue.indexOf(task);
|
|
477
|
+
if (idx >= 0) {
|
|
478
|
+
queue.splice(idx, 1);
|
|
479
|
+
timedOut++;
|
|
480
|
+
task.reject({ _tag: "WorkerTaskTimeout", ms: taskTimeoutMs });
|
|
481
|
+
}
|
|
482
|
+
}, taskTimeoutMs);
|
|
483
|
+
queue.push(task);
|
|
484
|
+
processNext();
|
|
485
|
+
return () => {
|
|
486
|
+
const idx = queue.indexOf(task);
|
|
487
|
+
if (idx >= 0) {
|
|
488
|
+
queue.splice(idx, 1);
|
|
489
|
+
if (task.timeoutId) clearTimeout(task.timeoutId);
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
});
|
|
493
|
+
};
|
|
494
|
+
const run = (taskSource, args = []) => {
|
|
495
|
+
return execute(() => {
|
|
496
|
+
const fn = new Function(...args.map((_, i) => `arg${i}`), taskSource);
|
|
497
|
+
return fn(...args);
|
|
498
|
+
});
|
|
499
|
+
};
|
|
500
|
+
return {
|
|
501
|
+
execute,
|
|
502
|
+
run,
|
|
503
|
+
stats: () => ({
|
|
504
|
+
size,
|
|
505
|
+
busy,
|
|
506
|
+
idle: size - busy,
|
|
507
|
+
queued: queue.length,
|
|
508
|
+
completed,
|
|
509
|
+
failed,
|
|
510
|
+
timedOut
|
|
511
|
+
}),
|
|
512
|
+
shutdown: async () => {
|
|
513
|
+
closed = true;
|
|
514
|
+
while (queue.length > 0) {
|
|
515
|
+
const task = queue.shift();
|
|
516
|
+
if (task.timeoutId) clearTimeout(task.timeoutId);
|
|
517
|
+
task.reject({ _tag: "WorkerPoolClosed" });
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// src/core/runtime/tracing.ts
|
|
524
|
+
var idCounter = 0;
|
|
525
|
+
function generateId() {
|
|
526
|
+
return (++idCounter).toString(16).padStart(16, "0");
|
|
527
|
+
}
|
|
528
|
+
function makeTracer(config) {
|
|
529
|
+
const completedSpans = [];
|
|
530
|
+
const sampleRate = _nullishCoalesce(config.sampleRate, () => ( 1));
|
|
531
|
+
const shouldSample = () => {
|
|
532
|
+
if (sampleRate >= 1) return true;
|
|
533
|
+
if (sampleRate <= 0) return false;
|
|
534
|
+
return Math.random() < sampleRate;
|
|
535
|
+
};
|
|
536
|
+
const span = (name, effect, attributes) => {
|
|
537
|
+
if (!shouldSample()) return effect;
|
|
538
|
+
const spanObj = {
|
|
539
|
+
name,
|
|
540
|
+
context: {
|
|
541
|
+
traceId: generateId(),
|
|
542
|
+
spanId: generateId()
|
|
543
|
+
},
|
|
544
|
+
startTime: performance.now(),
|
|
545
|
+
status: "unset",
|
|
546
|
+
attributes: { "service.name": config.serviceName, ...attributes },
|
|
547
|
+
events: []
|
|
548
|
+
};
|
|
549
|
+
return _chunkDJQ7OMMBcjs.asyncFold.call(void 0,
|
|
550
|
+
effect,
|
|
551
|
+
(error) => {
|
|
552
|
+
spanObj.endTime = performance.now();
|
|
553
|
+
spanObj.status = "error";
|
|
554
|
+
spanObj.events.push({
|
|
555
|
+
name: "error",
|
|
556
|
+
time: performance.now(),
|
|
557
|
+
attributes: { "error.message": String(error) }
|
|
558
|
+
});
|
|
559
|
+
completedSpans.push(spanObj);
|
|
560
|
+
_optionalChain([config, 'access', _14 => _14.onSpanEnd, 'optionalCall', _15 => _15(spanObj)]);
|
|
561
|
+
return _chunkDJQ7OMMBcjs.asyncFail.call(void 0, error);
|
|
562
|
+
},
|
|
563
|
+
(value) => {
|
|
564
|
+
spanObj.endTime = performance.now();
|
|
565
|
+
spanObj.status = "ok";
|
|
566
|
+
completedSpans.push(spanObj);
|
|
567
|
+
_optionalChain([config, 'access', _16 => _16.onSpanEnd, 'optionalCall', _17 => _17(spanObj)]);
|
|
568
|
+
return _chunkDJQ7OMMBcjs.asyncSucceed.call(void 0, value);
|
|
569
|
+
}
|
|
570
|
+
);
|
|
571
|
+
};
|
|
572
|
+
return {
|
|
573
|
+
span,
|
|
574
|
+
spans: () => completedSpans,
|
|
575
|
+
clear: () => {
|
|
576
|
+
completedSpans.length = 0;
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// src/core/runtime/loggerSink.ts
|
|
582
|
+
function consoleJsonLogger() {
|
|
583
|
+
return {
|
|
584
|
+
emit(ev, ctx) {
|
|
585
|
+
if (ev.type !== "log") return;
|
|
586
|
+
const wallTs = Date.now();
|
|
587
|
+
const out = {
|
|
588
|
+
level: ev.level,
|
|
589
|
+
msg: ev.message,
|
|
590
|
+
wallTs,
|
|
591
|
+
fiberId: ctx.fiberId,
|
|
592
|
+
scopeId: ctx.scopeId,
|
|
593
|
+
traceId: ctx.traceId,
|
|
594
|
+
spanId: ctx.spanId,
|
|
595
|
+
parentSpanId: ctx.parentSpanId,
|
|
596
|
+
..._nullishCoalesce(ev.fields, () => ( {}))
|
|
597
|
+
};
|
|
598
|
+
if (ev.level === "error") console.error(JSON.stringify(out));
|
|
599
|
+
else console.log(JSON.stringify(out));
|
|
600
|
+
}
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// src/core/runtime/registry.ts
|
|
605
|
+
var RuntimeRegistry = (_class2 = class {constructor() { _class2.prototype.__init4.call(this);_class2.prototype.__init5.call(this);_class2.prototype.__init6.call(this);_class2.prototype.__init7.call(this);_class2.prototype.__init8.call(this); }
|
|
606
|
+
__init4() {this.fibers = /* @__PURE__ */ new Map()}
|
|
607
|
+
__init5() {this.scopes = /* @__PURE__ */ new Map()}
|
|
608
|
+
__init6() {this.seq = 1}
|
|
609
|
+
__init7() {this.recent = []}
|
|
610
|
+
__init8() {this.recentCap = 2e3}
|
|
611
|
+
emit(ev, ctx) {
|
|
612
|
+
const rec = _chunkJX3LZQJHcjs.makeRuntimeEventRecord.call(void 0, ev, ctx, this.seq++);
|
|
613
|
+
this.recent.push(rec);
|
|
614
|
+
if (this.recent.length > this.recentCap) this.recent.shift();
|
|
615
|
+
switch (rec.type) {
|
|
616
|
+
case "fiber.start": {
|
|
617
|
+
const id = rec.fiberId;
|
|
618
|
+
this.fibers.set(id, {
|
|
619
|
+
fiberId: id,
|
|
620
|
+
parentFiberId: rec.parentFiberId,
|
|
621
|
+
name: rec.name,
|
|
622
|
+
runState: "Running",
|
|
623
|
+
status: "Running",
|
|
624
|
+
createdAt: rec.wallTs,
|
|
625
|
+
lastActiveAt: rec.wallTs,
|
|
626
|
+
scopeId: rec.scopeId,
|
|
627
|
+
traceId: rec.traceId,
|
|
628
|
+
spanId: rec.spanId
|
|
629
|
+
});
|
|
630
|
+
break;
|
|
631
|
+
}
|
|
632
|
+
case "fiber.suspend": {
|
|
633
|
+
const f = this.fibers.get(rec.fiberId);
|
|
634
|
+
if (f) {
|
|
635
|
+
f.runState = "Suspended";
|
|
636
|
+
f.lastActiveAt = rec.wallTs;
|
|
637
|
+
f.awaiting = { reason: _nullishCoalesce(rec.reason, () => ( "unknown")) };
|
|
638
|
+
}
|
|
639
|
+
break;
|
|
640
|
+
}
|
|
641
|
+
case "fiber.resume": {
|
|
642
|
+
const f = this.fibers.get(rec.fiberId);
|
|
643
|
+
if (f) {
|
|
644
|
+
f.runState = "Running";
|
|
645
|
+
f.lastActiveAt = rec.wallTs;
|
|
646
|
+
f.awaiting = void 0;
|
|
647
|
+
}
|
|
648
|
+
break;
|
|
649
|
+
}
|
|
650
|
+
case "fiber.end": {
|
|
651
|
+
const f = this.fibers.get(rec.fiberId);
|
|
652
|
+
if (f) {
|
|
653
|
+
f.runState = "Done";
|
|
654
|
+
f.lastActiveAt = rec.wallTs;
|
|
655
|
+
f.status = rec.status === "interrupted" ? "Interrupted" : "Done";
|
|
656
|
+
f.lastEnd = { status: rec.status, error: rec.error };
|
|
657
|
+
}
|
|
658
|
+
break;
|
|
659
|
+
}
|
|
660
|
+
case "scope.open": {
|
|
661
|
+
const sid = rec.scopeId;
|
|
662
|
+
this.scopes.set(sid, {
|
|
663
|
+
scopeId: sid,
|
|
664
|
+
parentScopeId: rec.parentScopeId,
|
|
665
|
+
ownerFiberId: rec.fiberId,
|
|
666
|
+
openAt: rec.wallTs,
|
|
667
|
+
finalizers: []
|
|
668
|
+
});
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
671
|
+
case "scope.close": {
|
|
672
|
+
const s = this.scopes.get(rec.scopeId);
|
|
673
|
+
if (s) s.closedAt = rec.wallTs;
|
|
674
|
+
break;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
getRecentEvents() {
|
|
679
|
+
return this.recent.slice();
|
|
680
|
+
}
|
|
681
|
+
}, _class2);
|
|
682
|
+
|
|
683
|
+
// src/core/runtime/dump.ts
|
|
684
|
+
function dumpAllFibers(reg) {
|
|
685
|
+
const fibers = Array.from(reg.fibers.values());
|
|
686
|
+
fibers.sort((a, b) => a.runState === b.runState ? b.lastActiveAt - a.lastActiveAt : a.runState.localeCompare(b.runState));
|
|
687
|
+
const lines = [];
|
|
688
|
+
lines.push(`=== Fiber Dump (${(/* @__PURE__ */ new Date()).toISOString()}) ===`);
|
|
689
|
+
for (const f of fibers) {
|
|
690
|
+
lines.push(
|
|
691
|
+
`fiber#${f.fiberId} ${_nullishCoalesce(f.name, () => ( ""))} run=${f.runState} status=${f.status} scope=${_nullishCoalesce(f.scopeId, () => ( "-"))} trace=${_nullishCoalesce(f.traceId, () => ( "-"))} span=${_nullishCoalesce(f.spanId, () => ( "-"))} last=${new Date(f.lastActiveAt).toISOString()}`
|
|
692
|
+
);
|
|
693
|
+
if (f.awaiting) lines.push(` awaiting: ${f.awaiting.reason}${f.awaiting.detail ? ` (${f.awaiting.detail})` : ""}`);
|
|
694
|
+
if (_optionalChain([f, 'access', _18 => _18.lastEnd, 'optionalAccess', _19 => _19.error])) lines.push(` end.error: ${f.lastEnd.error}`);
|
|
695
|
+
}
|
|
696
|
+
lines.push(`=== Recent Events ===`);
|
|
697
|
+
for (const ev of reg.getRecentEvents().slice(-80)) {
|
|
698
|
+
lines.push(
|
|
699
|
+
`${ev.seq} ${new Date(ev.wallTs).toISOString()} ${ev.type} fiber=${_nullishCoalesce(ev.fiberId, () => ( "-"))} scope=${_nullishCoalesce(ev.scopeId, () => ( "-"))} trace=${_nullishCoalesce(ev.traceId, () => ( "-"))}`
|
|
700
|
+
);
|
|
701
|
+
}
|
|
702
|
+
return lines.join("\n");
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// src/core/runtime/tracer.ts
|
|
706
|
+
var defaultTracer = {
|
|
707
|
+
newTraceId: () => crypto.randomUUID(),
|
|
708
|
+
newSpanId: () => crypto.randomUUID()
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
// src/core/types/typedError.ts
|
|
712
|
+
function catchTag(effect, tag, handler) {
|
|
713
|
+
return _chunkDJQ7OMMBcjs.asyncFold.call(void 0,
|
|
714
|
+
effect,
|
|
715
|
+
(error) => {
|
|
716
|
+
if (typeof error === "object" && error !== null && "_tag" in error && error._tag === tag) {
|
|
717
|
+
return handler(error);
|
|
718
|
+
}
|
|
719
|
+
return _chunkDJQ7OMMBcjs.asyncFail.call(void 0, error);
|
|
720
|
+
},
|
|
721
|
+
(value) => _chunkDJQ7OMMBcjs.asyncSucceed.call(void 0, value)
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
function catchTags(effect, handlers) {
|
|
725
|
+
return _chunkDJQ7OMMBcjs.asyncFold.call(void 0,
|
|
726
|
+
effect,
|
|
727
|
+
(error) => {
|
|
728
|
+
if (typeof error === "object" && error !== null && "_tag" in error) {
|
|
729
|
+
const handler = handlers[error._tag];
|
|
730
|
+
if (handler) return handler(error);
|
|
731
|
+
}
|
|
732
|
+
return _chunkDJQ7OMMBcjs.asyncFail.call(void 0, error);
|
|
733
|
+
},
|
|
734
|
+
(value) => _chunkDJQ7OMMBcjs.asyncSucceed.call(void 0, value)
|
|
735
|
+
);
|
|
736
|
+
}
|
|
737
|
+
function mapError(effect, f) {
|
|
738
|
+
return _chunkDJQ7OMMBcjs.asyncFold.call(void 0, effect, (error) => _chunkDJQ7OMMBcjs.asyncFail.call(void 0, f(error)), _chunkDJQ7OMMBcjs.asyncSucceed);
|
|
739
|
+
}
|
|
740
|
+
function tagError(effect, tag, enrich) {
|
|
741
|
+
return _chunkDJQ7OMMBcjs.asyncFold.call(void 0,
|
|
742
|
+
effect,
|
|
743
|
+
(error) => {
|
|
744
|
+
const fields = enrich ? enrich(error) : {};
|
|
745
|
+
return _chunkDJQ7OMMBcjs.asyncFail.call(void 0, { _tag: tag, ...fields });
|
|
746
|
+
},
|
|
747
|
+
_chunkDJQ7OMMBcjs.asyncSucceed
|
|
748
|
+
);
|
|
749
|
+
}
|
|
750
|
+
function orElse(effect, fallback) {
|
|
751
|
+
return _chunkDJQ7OMMBcjs.asyncFold.call(void 0, effect, fallback, _chunkDJQ7OMMBcjs.asyncSucceed);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
// src/core/runtime/supervisor.ts
|
|
755
|
+
var nextSupervisorId = 1;
|
|
756
|
+
var nextChildId = 1;
|
|
757
|
+
var Supervisor = (_class3 = class {
|
|
758
|
+
constructor(runtime, config = {}) {;_class3.prototype.__init9.call(this);_class3.prototype.__init10.call(this);_class3.prototype.__init11.call(this);
|
|
759
|
+
this.runtime = runtime;
|
|
760
|
+
this.strategy = _nullishCoalesce(config.strategy, () => ( "one-for-one"));
|
|
761
|
+
this.restart = resolveRestartPolicy(_nullishCoalesce(config.restart, () => ( "on-failure")));
|
|
762
|
+
this.escalation = _nullishCoalesce(config.escalation, () => ( "shutdown"));
|
|
763
|
+
this.clock = _nullishCoalesce(config.clock, () => ( Date.now));
|
|
764
|
+
this.onEvent = config.onEvent;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
__init9() {this.id = nextSupervisorId++}
|
|
768
|
+
__init10() {this.records = /* @__PURE__ */ new Map()}
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
__init11() {this.closed = false}
|
|
775
|
+
start(spec) {
|
|
776
|
+
if (this.closed) throw new Error("Supervisor is shut down");
|
|
777
|
+
const record = {
|
|
778
|
+
id: nextChildId++,
|
|
779
|
+
name: spec.name,
|
|
780
|
+
spec,
|
|
781
|
+
restart: resolveRestartPolicy(_nullishCoalesce(spec.restart, () => ( this.restart))),
|
|
782
|
+
status: "running",
|
|
783
|
+
restartCount: 0,
|
|
784
|
+
restartTimes: [],
|
|
785
|
+
generation: 0,
|
|
786
|
+
plannedRestart: false,
|
|
787
|
+
joiners: []
|
|
788
|
+
};
|
|
789
|
+
this.records.set(record.id, record);
|
|
790
|
+
this.launch(record);
|
|
791
|
+
return this.handle(record);
|
|
792
|
+
}
|
|
793
|
+
startAll(specs) {
|
|
794
|
+
return specs.map((spec) => this.start(spec));
|
|
795
|
+
}
|
|
796
|
+
shutdown() {
|
|
797
|
+
return _chunkDJQ7OMMBcjs.asyncEffect.call(void 0, (_env, cb) => {
|
|
798
|
+
this.closed = true;
|
|
799
|
+
this.emit({ type: "shutdown", supervisorId: this.id });
|
|
800
|
+
const records = [...this.records.values()];
|
|
801
|
+
let remaining = records.filter((record) => record.current !== void 0).length;
|
|
802
|
+
for (const record of records) {
|
|
803
|
+
if (record.timer) {
|
|
804
|
+
clearTimeout(record.timer);
|
|
805
|
+
record.timer = void 0;
|
|
806
|
+
}
|
|
807
|
+
record.plannedRestart = false;
|
|
808
|
+
if (record.current) {
|
|
809
|
+
record.status = "interrupted";
|
|
810
|
+
record.current.interrupt();
|
|
811
|
+
} else if (!record.terminalExit) {
|
|
812
|
+
this.finish(record, interruptExit());
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
if (remaining === 0) {
|
|
816
|
+
cb({ _tag: "Success", value: void 0 });
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
for (const record of records) {
|
|
820
|
+
_optionalChain([record, 'access', _20 => _20.current, 'optionalAccess', _21 => _21.join, 'call', _22 => _22(() => {
|
|
821
|
+
remaining--;
|
|
822
|
+
if (remaining === 0) cb({ _tag: "Success", value: void 0 });
|
|
823
|
+
})]);
|
|
824
|
+
}
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
handle(record) {
|
|
828
|
+
return {
|
|
829
|
+
id: record.id,
|
|
830
|
+
name: record.name,
|
|
831
|
+
current: () => record.current,
|
|
832
|
+
status: () => record.status,
|
|
833
|
+
restartCount: () => record.restartCount,
|
|
834
|
+
interrupt: () => {
|
|
835
|
+
record.plannedRestart = false;
|
|
836
|
+
if (record.timer) {
|
|
837
|
+
clearTimeout(record.timer);
|
|
838
|
+
record.timer = void 0;
|
|
839
|
+
}
|
|
840
|
+
if (record.current) {
|
|
841
|
+
record.current.interrupt();
|
|
842
|
+
} else if (!record.terminalExit) {
|
|
843
|
+
this.finish(record, interruptExit());
|
|
844
|
+
}
|
|
845
|
+
},
|
|
846
|
+
join: (cb) => {
|
|
847
|
+
if (record.terminalExit) cb(record.terminalExit);
|
|
848
|
+
else record.joiners.push(cb);
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
launch(record) {
|
|
853
|
+
if (this.closed) return;
|
|
854
|
+
record.generation++;
|
|
855
|
+
record.status = "running";
|
|
856
|
+
record.plannedRestart = false;
|
|
857
|
+
record.terminalExit = void 0;
|
|
858
|
+
const generation = record.generation;
|
|
859
|
+
const effect = typeof record.spec.effect === "function" ? record.spec.effect() : record.spec.effect;
|
|
860
|
+
const fiber = this.runtime.fork(effect);
|
|
861
|
+
record.current = fiber;
|
|
862
|
+
this.emit({
|
|
863
|
+
type: "child-start",
|
|
864
|
+
supervisorId: this.id,
|
|
865
|
+
childId: record.id,
|
|
866
|
+
name: record.name,
|
|
867
|
+
status: "running",
|
|
868
|
+
restartCount: record.restartCount
|
|
869
|
+
});
|
|
870
|
+
fiber.join((exit) => this.onChildExit(record, generation, exit));
|
|
871
|
+
}
|
|
872
|
+
onChildExit(record, generation, exit) {
|
|
873
|
+
if (generation !== record.generation) return;
|
|
874
|
+
record.current = void 0;
|
|
875
|
+
if (this.closed) {
|
|
876
|
+
this.finish(record, exit);
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
if (record.plannedRestart) {
|
|
880
|
+
this.scheduleRestart(record, exit, "all-for-one");
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
this.emit({
|
|
884
|
+
type: "child-end",
|
|
885
|
+
supervisorId: this.id,
|
|
886
|
+
childId: record.id,
|
|
887
|
+
name: record.name,
|
|
888
|
+
status: statusFromExit(exit),
|
|
889
|
+
restartCount: record.restartCount,
|
|
890
|
+
exit
|
|
891
|
+
});
|
|
892
|
+
if (!shouldRestart(record.restart.mode, exit)) {
|
|
893
|
+
this.finish(record, exit);
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
if (this.strategy === "all-for-one") {
|
|
897
|
+
this.restartAll(record, exit);
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
this.scheduleRestart(record, exit, "one-for-one");
|
|
901
|
+
}
|
|
902
|
+
restartAll(failed, exit) {
|
|
903
|
+
for (const record of this.records.values()) {
|
|
904
|
+
if (record.id === failed.id) continue;
|
|
905
|
+
if (!record.current) continue;
|
|
906
|
+
record.plannedRestart = true;
|
|
907
|
+
record.status = "restarting";
|
|
908
|
+
record.current.interrupt();
|
|
909
|
+
}
|
|
910
|
+
this.scheduleRestart(failed, exit, "all-for-one");
|
|
911
|
+
}
|
|
912
|
+
scheduleRestart(record, exit, reason) {
|
|
913
|
+
const context = this.restartContext(record, exit);
|
|
914
|
+
const delay = this.nextDelay(record, context);
|
|
915
|
+
if (delay === void 0) {
|
|
916
|
+
this.escalate(record, exit, "restart policy exhausted");
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
record.status = "restarting";
|
|
920
|
+
record.restartCount++;
|
|
921
|
+
this.emit({
|
|
922
|
+
type: "child-restart",
|
|
923
|
+
supervisorId: this.id,
|
|
924
|
+
childId: record.id,
|
|
925
|
+
name: record.name,
|
|
926
|
+
status: "restarting",
|
|
927
|
+
restartCount: record.restartCount,
|
|
928
|
+
delayMs: delay,
|
|
929
|
+
exit,
|
|
930
|
+
reason
|
|
931
|
+
});
|
|
932
|
+
record.timer = setTimeout(() => {
|
|
933
|
+
record.timer = void 0;
|
|
934
|
+
this.launch(record);
|
|
935
|
+
}, delay);
|
|
936
|
+
}
|
|
937
|
+
nextDelay(record, context) {
|
|
938
|
+
const now = this.clock();
|
|
939
|
+
const windowStart = record.restart.withinMs === void 0 ? Number.NEGATIVE_INFINITY : now - record.restart.withinMs;
|
|
940
|
+
record.restartTimes = record.restartTimes.filter((ts) => ts >= windowStart);
|
|
941
|
+
if (record.restart.maxRestarts !== void 0 && record.restartTimes.length >= record.restart.maxRestarts) {
|
|
942
|
+
return void 0;
|
|
943
|
+
}
|
|
944
|
+
record.restartTimes.push(now);
|
|
945
|
+
if (record.restart.schedule) {
|
|
946
|
+
const state = _nullishCoalesce(record.scheduleState, () => ( record.restart.schedule.initial()));
|
|
947
|
+
const [decision, nextState] = record.restart.schedule.step(state, context);
|
|
948
|
+
record.scheduleState = nextState;
|
|
949
|
+
return decision.continue ? Math.max(0, Math.floor(decision.delayMs)) : void 0;
|
|
950
|
+
}
|
|
951
|
+
const raw = typeof record.restart.delayMs === "function" ? record.restart.delayMs(context) : _nullishCoalesce(record.restart.delayMs, () => ( 0));
|
|
952
|
+
return Number.isFinite(raw) ? Math.max(0, Math.floor(raw)) : 0;
|
|
953
|
+
}
|
|
954
|
+
escalate(record, exit, reason) {
|
|
955
|
+
this.emit({
|
|
956
|
+
type: "child-escalate",
|
|
957
|
+
supervisorId: this.id,
|
|
958
|
+
childId: record.id,
|
|
959
|
+
name: record.name,
|
|
960
|
+
status: statusFromExit(exit),
|
|
961
|
+
restartCount: record.restartCount,
|
|
962
|
+
exit,
|
|
963
|
+
reason
|
|
964
|
+
});
|
|
965
|
+
if (this.escalation === "shutdown") {
|
|
966
|
+
for (const child of this.records.values()) {
|
|
967
|
+
if (child.id !== record.id) _optionalChain([child, 'access', _23 => _23.current, 'optionalAccess', _24 => _24.interrupt, 'call', _25 => _25()]);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
this.finish(record, exit);
|
|
971
|
+
}
|
|
972
|
+
finish(record, exit) {
|
|
973
|
+
record.status = statusFromExit(exit);
|
|
974
|
+
record.terminalExit = exit;
|
|
975
|
+
const joiners = record.joiners.splice(0);
|
|
976
|
+
for (const joiner of joiners) joiner(exit);
|
|
977
|
+
}
|
|
978
|
+
restartContext(record, exit) {
|
|
979
|
+
return {
|
|
980
|
+
supervisorId: this.id,
|
|
981
|
+
childId: record.id,
|
|
982
|
+
name: record.name,
|
|
983
|
+
restartCount: record.restartCount,
|
|
984
|
+
exit
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
emit(event) {
|
|
988
|
+
_optionalChain([this, 'access', _26 => _26.onEvent, 'optionalCall', _27 => _27(event)]);
|
|
989
|
+
this.runtime.emit(toRuntimeEvent(event));
|
|
990
|
+
}
|
|
991
|
+
}, _class3);
|
|
992
|
+
function makeSupervisor(runtime, config = {}) {
|
|
993
|
+
return new Supervisor(runtime, config);
|
|
994
|
+
}
|
|
995
|
+
function supervise(runtime, spec, config = {}) {
|
|
996
|
+
return new Supervisor(runtime, config).start(spec);
|
|
997
|
+
}
|
|
998
|
+
function joinSupervised(fiber) {
|
|
999
|
+
return _chunkDJQ7OMMBcjs.asyncEffect.call(void 0, (_env, cb) => fiber.join(cb));
|
|
1000
|
+
}
|
|
1001
|
+
function resolveRestartPolicy(policy) {
|
|
1002
|
+
if (typeof policy === "string") return { mode: policy };
|
|
1003
|
+
return {
|
|
1004
|
+
mode: _nullishCoalesce(policy.mode, () => ( "on-failure")),
|
|
1005
|
+
maxRestarts: policy.maxRestarts,
|
|
1006
|
+
withinMs: policy.withinMs,
|
|
1007
|
+
delayMs: policy.delayMs,
|
|
1008
|
+
schedule: policy.schedule
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
function shouldRestart(mode, exit) {
|
|
1012
|
+
if (exit._tag === "Failure" && exit.cause._tag === "Interrupt") return false;
|
|
1013
|
+
if (mode === "never") return false;
|
|
1014
|
+
if (mode === "always") return true;
|
|
1015
|
+
return exit._tag === "Failure";
|
|
1016
|
+
}
|
|
1017
|
+
function statusFromExit(exit) {
|
|
1018
|
+
if (exit._tag === "Success") return "succeeded";
|
|
1019
|
+
return exit.cause._tag === "Interrupt" ? "interrupted" : "failed";
|
|
1020
|
+
}
|
|
1021
|
+
function toRuntimeEvent(event) {
|
|
1022
|
+
switch (event.type) {
|
|
1023
|
+
case "child-start":
|
|
1024
|
+
return {
|
|
1025
|
+
type: "supervisor.child.start",
|
|
1026
|
+
supervisorId: event.supervisorId,
|
|
1027
|
+
childId: event.childId,
|
|
1028
|
+
name: event.name,
|
|
1029
|
+
restartCount: _nullishCoalesce(event.restartCount, () => ( 0))
|
|
1030
|
+
};
|
|
1031
|
+
case "child-end":
|
|
1032
|
+
return {
|
|
1033
|
+
type: "supervisor.child.end",
|
|
1034
|
+
supervisorId: event.supervisorId,
|
|
1035
|
+
childId: event.childId,
|
|
1036
|
+
name: event.name,
|
|
1037
|
+
status: event.status === "succeeded" ? "success" : event.status === "interrupted" ? "interrupted" : "failure",
|
|
1038
|
+
error: _optionalChain([event, 'access', _28 => _28.exit, 'optionalAccess', _29 => _29._tag]) === "Failure" ? event.exit.cause : void 0
|
|
1039
|
+
};
|
|
1040
|
+
case "child-restart":
|
|
1041
|
+
return {
|
|
1042
|
+
type: "supervisor.child.restart",
|
|
1043
|
+
supervisorId: event.supervisorId,
|
|
1044
|
+
childId: event.childId,
|
|
1045
|
+
name: event.name,
|
|
1046
|
+
restartCount: _nullishCoalesce(event.restartCount, () => ( 0)),
|
|
1047
|
+
delayMs: _nullishCoalesce(event.delayMs, () => ( 0)),
|
|
1048
|
+
reason: event.reason
|
|
1049
|
+
};
|
|
1050
|
+
case "child-escalate":
|
|
1051
|
+
return {
|
|
1052
|
+
type: "supervisor.child.escalate",
|
|
1053
|
+
supervisorId: event.supervisorId,
|
|
1054
|
+
childId: event.childId,
|
|
1055
|
+
name: event.name,
|
|
1056
|
+
reason: event.reason,
|
|
1057
|
+
error: _optionalChain([event, 'access', _30 => _30.exit, 'optionalAccess', _31 => _31._tag]) === "Failure" ? event.exit.cause : void 0
|
|
1058
|
+
};
|
|
1059
|
+
case "shutdown":
|
|
1060
|
+
return {
|
|
1061
|
+
type: "supervisor.shutdown",
|
|
1062
|
+
supervisorId: event.supervisorId
|
|
1063
|
+
};
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
function interruptExit() {
|
|
1067
|
+
return { _tag: "Failure", cause: { _tag: "Interrupt" } };
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
|
|
1074
|
+
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
|
|
1093
|
+
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
|
|
1097
|
+
|
|
1098
|
+
|
|
1099
|
+
|
|
1100
|
+
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
|
|
1105
|
+
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
|
|
1109
|
+
|
|
1110
|
+
exports.makeCancelToken = makeCancelToken; exports.linkAbortController = linkAbortController; exports.LinkedQueue = LinkedQueue; exports.makeSemaphore = makeSemaphore; exports.makeRef = makeRef; exports.derivedRef = derivedRef; exports.gracefulShutdown = gracefulShutdown; exports.registerShutdownHooks = registerShutdownHooks; exports.makeTestRuntime = makeTestRuntime; exports.assertSucceeds = assertSucceeds; exports.assertFails = assertFails; exports.assertFailsWith = assertFailsWith; exports.assertCompletesWithin = assertCompletesWithin; exports.flakyEffect = flakyEffect; exports.delayedEffect = delayedEffect; exports.neverEffect = neverEffect; exports.layer = layer; exports.layerFrom = layerFrom; exports.layerSucceed = layerSucceed; exports.layerFail = layerFail; exports.compose = compose; exports.merge = merge; exports.mapLayer = mapLayer; exports.provideLayer = provideLayer; exports.makeWorkerPool = makeWorkerPool; exports.makeTracer = makeTracer; exports.consoleJsonLogger = consoleJsonLogger; exports.RuntimeRegistry = RuntimeRegistry; exports.dumpAllFibers = dumpAllFibers; exports.defaultTracer = defaultTracer; exports.catchTag = catchTag; exports.catchTags = catchTags; exports.mapError = mapError; exports.tagError = tagError; exports.orElse = orElse; exports.Supervisor = Supervisor; exports.makeSupervisor = makeSupervisor; exports.supervise = supervise; exports.joinSupervised = joinSupervised;
|