brass-runtime 1.14.0 → 1.15.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 +6 -3
- package/dist/agent/cli/main.cjs +44 -43
- package/dist/agent/cli/main.js +5 -4
- package/dist/agent/cli/main.mjs +5 -4
- package/dist/agent/index.cjs +4 -3
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/index.js +3 -2
- package/dist/agent/index.mjs +3 -2
- package/dist/{chunk-WJESVBWN.js → chunk-3QMOKAS5.js} +9 -7
- package/dist/{chunk-BMRF4FN6.js → chunk-4NHES7VK.mjs} +59 -237
- package/dist/chunk-AR22SXML.js +1043 -0
- package/dist/{chunk-4N2JEK4H.mjs → chunk-BDF4AMWX.mjs} +27 -151
- package/dist/chunk-BDYEENHT.js +224 -0
- package/dist/{chunk-JT7D6M5H.js → chunk-BMH5AV44.js} +27 -151
- package/dist/chunk-ELOOF35R.mjs +131 -0
- package/dist/chunk-JFPU5GQI.mjs +1043 -0
- package/dist/{chunk-MQF7HZ7Y.mjs → chunk-K6M7MDZ4.mjs} +9 -7
- package/dist/chunk-MS34J5LY.cjs +224 -0
- package/dist/{chunk-UWMMYKVK.mjs → chunk-PPUXIH5R.js} +59 -237
- package/dist/chunk-R3R2FVLG.cjs +131 -0
- package/dist/chunk-STVLQ3XD.cjs +489 -0
- package/dist/{chunk-BKBFSOGT.cjs → chunk-TGIFUAK4.cjs} +26 -150
- package/dist/chunk-TO7IKXYT.js +131 -0
- package/dist/chunk-UMAZLXAB.mjs +224 -0
- package/dist/{chunk-XTMZTVIT.cjs → chunk-VEZNF5GZ.cjs} +136 -134
- package/dist/chunk-XPZNXSVN.cjs +1043 -0
- package/dist/core/index.cjs +216 -0
- package/dist/core/index.d.ts +673 -0
- package/dist/core/index.js +216 -0
- package/dist/core/index.mjs +216 -0
- package/dist/{effect-DM56H743.d.ts → effect-CMOQKX8y.d.ts} +12 -11
- package/dist/http/index.cjs +2557 -235
- package/dist/http/index.d.ts +1514 -4
- package/dist/http/index.js +2549 -227
- package/dist/http/index.mjs +2549 -227
- package/dist/index.cjs +237 -1168
- package/dist/index.d.ts +7 -673
- package/dist/index.js +77 -1008
- package/dist/index.mjs +77 -1008
- package/dist/stream-FQm9h4Mg.d.ts +74 -0
- package/dist/tracing-DNT9jEbr.d.ts +106 -0
- package/package.json +11 -3
- package/dist/chunk-SKVY72E5.cjs +0 -667
- package/dist/stream-Oqe6WeLE.d.ts +0 -173
|
@@ -0,0 +1,1043 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sleep
|
|
3
|
+
} from "./chunk-UMAZLXAB.mjs";
|
|
4
|
+
import {
|
|
5
|
+
Cause,
|
|
6
|
+
Exit,
|
|
7
|
+
Runtime,
|
|
8
|
+
Scope,
|
|
9
|
+
asyncEffect,
|
|
10
|
+
asyncFail,
|
|
11
|
+
asyncFlatMap,
|
|
12
|
+
asyncFold,
|
|
13
|
+
asyncMap,
|
|
14
|
+
asyncSucceed,
|
|
15
|
+
asyncSync,
|
|
16
|
+
unit,
|
|
17
|
+
unsafeGetCurrentRuntime
|
|
18
|
+
} from "./chunk-BDF4AMWX.mjs";
|
|
19
|
+
|
|
20
|
+
// src/core/types/cancel.ts
|
|
21
|
+
function makeCancelToken() {
|
|
22
|
+
let cancelled = false;
|
|
23
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
24
|
+
const cancel = () => {
|
|
25
|
+
if (cancelled) return;
|
|
26
|
+
cancelled = true;
|
|
27
|
+
listeners.forEach((f) => f());
|
|
28
|
+
listeners.clear();
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
isCancelled: () => cancelled,
|
|
32
|
+
onCancel: (f) => {
|
|
33
|
+
if (cancelled) {
|
|
34
|
+
try {
|
|
35
|
+
f();
|
|
36
|
+
} catch {
|
|
37
|
+
}
|
|
38
|
+
return () => {
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
listeners.add(f);
|
|
42
|
+
return () => {
|
|
43
|
+
listeners.delete(f);
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
cancel
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function linkAbortController(token, ac) {
|
|
50
|
+
return token.onCancel(() => ac.abort());
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/core/runtime/resource.ts
|
|
54
|
+
function bracket(acquire, use, release) {
|
|
55
|
+
return asyncEffect((env, cb) => {
|
|
56
|
+
const runtime = unsafeGetCurrentRuntime();
|
|
57
|
+
const scope = new Scope(runtime);
|
|
58
|
+
const acquireFiber = scope.fork(acquire);
|
|
59
|
+
acquireFiber.join((acquireExit) => {
|
|
60
|
+
if (acquireExit._tag === "Failure") {
|
|
61
|
+
scope.close(acquireExit);
|
|
62
|
+
cb(acquireExit);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const resource = acquireExit.value;
|
|
66
|
+
const useFiber = scope.fork(use(resource));
|
|
67
|
+
useFiber.join((useExit) => {
|
|
68
|
+
const releaseEffect = safeRelease(release, resource, useExit);
|
|
69
|
+
const releaseFiber = runtime.fork(releaseEffect);
|
|
70
|
+
releaseFiber.join(() => {
|
|
71
|
+
scope.close(useExit);
|
|
72
|
+
cb(useExit);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
return () => {
|
|
77
|
+
scope.close(Exit.failCause(Cause.interrupt()));
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function safeRelease(release, resource, exit) {
|
|
82
|
+
return asyncFold(
|
|
83
|
+
(() => {
|
|
84
|
+
try {
|
|
85
|
+
return release(resource, exit);
|
|
86
|
+
} catch {
|
|
87
|
+
return unit();
|
|
88
|
+
}
|
|
89
|
+
})(),
|
|
90
|
+
() => unit(),
|
|
91
|
+
() => unit()
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
function ensuring(effect, finalizer) {
|
|
95
|
+
return asyncEffect((env, cb) => {
|
|
96
|
+
const runtime = unsafeGetCurrentRuntime();
|
|
97
|
+
const fiber = runtime.fork(effect);
|
|
98
|
+
fiber.join((exit) => {
|
|
99
|
+
const fin = asyncFold(
|
|
100
|
+
(() => {
|
|
101
|
+
try {
|
|
102
|
+
return finalizer(exit);
|
|
103
|
+
} catch {
|
|
104
|
+
return unit();
|
|
105
|
+
}
|
|
106
|
+
})(),
|
|
107
|
+
() => unit(),
|
|
108
|
+
() => unit()
|
|
109
|
+
);
|
|
110
|
+
runtime.fork(fin).join(() => {
|
|
111
|
+
cb(exit);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
return () => {
|
|
115
|
+
fiber.interrupt();
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function managed(acquire, release) {
|
|
120
|
+
return {
|
|
121
|
+
_tag: "Managed",
|
|
122
|
+
acquire,
|
|
123
|
+
release: (resource, exit) => release(resource, exit)
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function useManaged(m, body) {
|
|
127
|
+
return bracket(m.acquire, body, m.release);
|
|
128
|
+
}
|
|
129
|
+
function managedAll(manageds) {
|
|
130
|
+
const acquire = asyncEffect((env, cb) => {
|
|
131
|
+
const runtime = unsafeGetCurrentRuntime();
|
|
132
|
+
const resources = [];
|
|
133
|
+
let i = 0;
|
|
134
|
+
const acquireNext = () => {
|
|
135
|
+
if (i >= manageds.length) {
|
|
136
|
+
cb({ _tag: "Success", value: resources });
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const m = manageds[i];
|
|
140
|
+
const fiber = runtime.fork(m.acquire);
|
|
141
|
+
fiber.join((exit) => {
|
|
142
|
+
if (exit._tag === "Failure") {
|
|
143
|
+
releaseAcquired(runtime, manageds, resources, exit).then(() => {
|
|
144
|
+
cb(exit);
|
|
145
|
+
});
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
resources.push(exit.value);
|
|
149
|
+
i++;
|
|
150
|
+
acquireNext();
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
acquireNext();
|
|
154
|
+
});
|
|
155
|
+
const release = (resources, exit) => {
|
|
156
|
+
return asyncEffect((_env, cb) => {
|
|
157
|
+
const runtime = unsafeGetCurrentRuntime();
|
|
158
|
+
releaseAcquired(runtime, manageds, resources, exit).then(() => {
|
|
159
|
+
cb({ _tag: "Success", value: void 0 });
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
return { _tag: "Managed", acquire, release };
|
|
164
|
+
}
|
|
165
|
+
async function releaseAcquired(runtime, manageds, resources, exit) {
|
|
166
|
+
for (let i = resources.length - 1; i >= 0; i--) {
|
|
167
|
+
try {
|
|
168
|
+
const m = manageds[i];
|
|
169
|
+
await new Promise((resolve) => {
|
|
170
|
+
const releaseEff = asyncFold(
|
|
171
|
+
m.release(resources[i], exit),
|
|
172
|
+
() => unit(),
|
|
173
|
+
() => unit()
|
|
174
|
+
);
|
|
175
|
+
runtime.fork(releaseEff).join(() => resolve());
|
|
176
|
+
});
|
|
177
|
+
} catch {
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/core/runtime/linkedQueue.ts
|
|
183
|
+
var LinkedQueue = class {
|
|
184
|
+
head = null;
|
|
185
|
+
tail = null;
|
|
186
|
+
len = 0;
|
|
187
|
+
get length() {
|
|
188
|
+
return this.len;
|
|
189
|
+
}
|
|
190
|
+
isEmpty() {
|
|
191
|
+
return this.len === 0;
|
|
192
|
+
}
|
|
193
|
+
push(value) {
|
|
194
|
+
const node = { value, next: null, prev: this.tail, removed: false };
|
|
195
|
+
if (this.tail) this.tail.next = node;
|
|
196
|
+
else this.head = node;
|
|
197
|
+
this.tail = node;
|
|
198
|
+
this.len++;
|
|
199
|
+
return node;
|
|
200
|
+
}
|
|
201
|
+
shift() {
|
|
202
|
+
const h = this.head;
|
|
203
|
+
if (!h) return void 0;
|
|
204
|
+
this.unlink(h);
|
|
205
|
+
return h.value;
|
|
206
|
+
}
|
|
207
|
+
remove(node) {
|
|
208
|
+
if (node.removed) return;
|
|
209
|
+
this.unlink(node);
|
|
210
|
+
}
|
|
211
|
+
unlink(node) {
|
|
212
|
+
node.removed = true;
|
|
213
|
+
const { prev, next } = node;
|
|
214
|
+
if (prev) prev.next = next;
|
|
215
|
+
else this.head = next;
|
|
216
|
+
if (next) next.prev = prev;
|
|
217
|
+
else this.tail = prev;
|
|
218
|
+
node.next = null;
|
|
219
|
+
node.prev = null;
|
|
220
|
+
this.len--;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// src/core/runtime/semaphore.ts
|
|
225
|
+
function makeSemaphore(n) {
|
|
226
|
+
const capacity = Math.max(1, Math.floor(n));
|
|
227
|
+
let available = capacity;
|
|
228
|
+
let totalAcquired = 0;
|
|
229
|
+
let totalReleased = 0;
|
|
230
|
+
const waiters = new LinkedQueue();
|
|
231
|
+
const acquire = () => {
|
|
232
|
+
return asyncEffect((_env, cb) => {
|
|
233
|
+
if (available > 0) {
|
|
234
|
+
available--;
|
|
235
|
+
totalAcquired++;
|
|
236
|
+
cb({ _tag: "Success", value: void 0 });
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const node = waiters.push(cb);
|
|
240
|
+
return () => {
|
|
241
|
+
waiters.remove(node);
|
|
242
|
+
};
|
|
243
|
+
});
|
|
244
|
+
};
|
|
245
|
+
const release = () => {
|
|
246
|
+
totalReleased++;
|
|
247
|
+
if (waiters.length > 0) {
|
|
248
|
+
const waiter = waiters.shift();
|
|
249
|
+
totalAcquired++;
|
|
250
|
+
waiter({ _tag: "Success", value: void 0 });
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
available++;
|
|
254
|
+
};
|
|
255
|
+
const withPermit = (effect) => {
|
|
256
|
+
return asyncFlatMap(
|
|
257
|
+
acquire(),
|
|
258
|
+
() => asyncEffect((_env, cb) => {
|
|
259
|
+
const runtime = unsafeGetCurrentRuntime();
|
|
260
|
+
const fiber = runtime.fork(effect);
|
|
261
|
+
let released = false;
|
|
262
|
+
const releaseOnce = () => {
|
|
263
|
+
if (released) return;
|
|
264
|
+
released = true;
|
|
265
|
+
release();
|
|
266
|
+
};
|
|
267
|
+
fiber.join((exit) => {
|
|
268
|
+
releaseOnce();
|
|
269
|
+
cb(exit);
|
|
270
|
+
});
|
|
271
|
+
return () => {
|
|
272
|
+
fiber.interrupt();
|
|
273
|
+
releaseOnce();
|
|
274
|
+
};
|
|
275
|
+
})
|
|
276
|
+
);
|
|
277
|
+
};
|
|
278
|
+
return {
|
|
279
|
+
capacity,
|
|
280
|
+
available: () => available,
|
|
281
|
+
waiting: () => waiters.length,
|
|
282
|
+
withPermit,
|
|
283
|
+
acquire,
|
|
284
|
+
release
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// src/core/runtime/ref.ts
|
|
289
|
+
function makeRef(initial) {
|
|
290
|
+
let value = initial;
|
|
291
|
+
return {
|
|
292
|
+
get: () => asyncSync(() => value),
|
|
293
|
+
set: (v) => asyncSync(() => {
|
|
294
|
+
value = v;
|
|
295
|
+
}),
|
|
296
|
+
update: (f) => asyncSync(() => {
|
|
297
|
+
value = f(value);
|
|
298
|
+
return value;
|
|
299
|
+
}),
|
|
300
|
+
modify: (f) => asyncSync(() => {
|
|
301
|
+
const [result, next] = f(value);
|
|
302
|
+
value = next;
|
|
303
|
+
return result;
|
|
304
|
+
}),
|
|
305
|
+
unsafeGet: () => value
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
function derivedRef(parent, get, set) {
|
|
309
|
+
return {
|
|
310
|
+
get: () => asyncSync(() => get(parent.unsafeGet())),
|
|
311
|
+
set: (b) => asyncFlatMap(parent.get(), (current) => parent.set(set(current, b))),
|
|
312
|
+
update: (f) => asyncFlatMap(parent.get(), (parentVal) => {
|
|
313
|
+
const newB = f(get(parentVal));
|
|
314
|
+
return asyncMap(parent.set(set(parentVal, newB)), () => newB);
|
|
315
|
+
}),
|
|
316
|
+
modify: (f) => asyncFlatMap(parent.get(), (parentVal) => {
|
|
317
|
+
const [result, newB] = f(get(parentVal));
|
|
318
|
+
return asyncMap(parent.set(set(parentVal, newB)), () => result);
|
|
319
|
+
}),
|
|
320
|
+
unsafeGet: () => get(parent.unsafeGet())
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// src/core/runtime/schedule.ts
|
|
325
|
+
function recurs(n) {
|
|
326
|
+
return {
|
|
327
|
+
_tag: "Schedule",
|
|
328
|
+
initial: () => 0,
|
|
329
|
+
step: (count, _input) => {
|
|
330
|
+
const next = count + 1;
|
|
331
|
+
return [{ continue: next < n, delayMs: 0 }, next, next];
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
function fixed(delayMs) {
|
|
336
|
+
return {
|
|
337
|
+
_tag: "Schedule",
|
|
338
|
+
initial: () => 0,
|
|
339
|
+
step: (count, _input) => {
|
|
340
|
+
return [{ continue: true, delayMs }, count + 1, count + 1];
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
function exponential(baseMs, maxMs = Infinity) {
|
|
345
|
+
return {
|
|
346
|
+
_tag: "Schedule",
|
|
347
|
+
initial: () => 0,
|
|
348
|
+
step: (count, _input) => {
|
|
349
|
+
const delay = Math.min(baseMs * Math.pow(2, count), maxMs);
|
|
350
|
+
return [{ continue: true, delayMs: delay }, count + 1, count + 1];
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
function jittered(baseMs, maxMs = Infinity) {
|
|
355
|
+
return {
|
|
356
|
+
_tag: "Schedule",
|
|
357
|
+
initial: () => 0,
|
|
358
|
+
step: (count, _input) => {
|
|
359
|
+
const cap = Math.min(baseMs * Math.pow(2, count), maxMs);
|
|
360
|
+
const delay = Math.floor(Math.random() * cap);
|
|
361
|
+
return [{ continue: true, delayMs: delay }, count + 1, count + 1];
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
function elapsed(maxMs) {
|
|
366
|
+
return {
|
|
367
|
+
_tag: "Schedule",
|
|
368
|
+
initial: () => performance.now(),
|
|
369
|
+
step: (startedAt, _input) => {
|
|
370
|
+
const el = performance.now() - startedAt;
|
|
371
|
+
return [{ continue: el < maxMs, delayMs: 0 }, startedAt, el];
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
function whileInput(pred) {
|
|
376
|
+
return {
|
|
377
|
+
_tag: "Schedule",
|
|
378
|
+
initial: () => void 0,
|
|
379
|
+
step: (_state, input) => {
|
|
380
|
+
return [{ continue: pred(input), delayMs: 0 }, void 0, input];
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
function take(schedule, n) {
|
|
385
|
+
return {
|
|
386
|
+
_tag: "Schedule",
|
|
387
|
+
initial: () => ({ inner: schedule.initial(), count: 0 }),
|
|
388
|
+
step: (state, input) => {
|
|
389
|
+
if (state.count >= n) return [{ continue: false, delayMs: 0 }, state, void 0];
|
|
390
|
+
const [decision, nextInner, output] = schedule.step(state.inner, input);
|
|
391
|
+
const nextState = { inner: nextInner, count: state.count + 1 };
|
|
392
|
+
return [{ continue: decision.continue && state.count + 1 < n, delayMs: decision.delayMs }, nextState, output];
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
function andThen(first, second) {
|
|
397
|
+
return {
|
|
398
|
+
_tag: "Schedule",
|
|
399
|
+
initial: () => ({ phase: "first", inner: first.initial() }),
|
|
400
|
+
step: (state, input) => {
|
|
401
|
+
if (state.phase === "first") {
|
|
402
|
+
const [decision2, nextInner2, output2] = first.step(state.inner, input);
|
|
403
|
+
if (decision2.continue) {
|
|
404
|
+
return [decision2, { phase: "first", inner: nextInner2 }, output2];
|
|
405
|
+
}
|
|
406
|
+
return [{ continue: true, delayMs: decision2.delayMs }, { phase: "second", inner: second.initial() }, output2];
|
|
407
|
+
}
|
|
408
|
+
const [decision, nextInner, output] = second.step(state.inner, input);
|
|
409
|
+
return [decision, { phase: "second", inner: nextInner }, output];
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
function intersect(left, right) {
|
|
414
|
+
return {
|
|
415
|
+
_tag: "Schedule",
|
|
416
|
+
initial: () => ({ left: left.initial(), right: right.initial() }),
|
|
417
|
+
step: (state, input) => {
|
|
418
|
+
const [ld, ls, lo] = left.step(state.left, input);
|
|
419
|
+
const [rd, rs, ro] = right.step(state.right, input);
|
|
420
|
+
const cont = ld.continue && rd.continue;
|
|
421
|
+
const delay = Math.max(ld.delayMs, rd.delayMs);
|
|
422
|
+
return [{ continue: cont, delayMs: delay }, { left: ls, right: rs }, [lo, ro]];
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
function union(left, right) {
|
|
427
|
+
return {
|
|
428
|
+
_tag: "Schedule",
|
|
429
|
+
initial: () => ({ left: left.initial(), right: right.initial() }),
|
|
430
|
+
step: (state, input) => {
|
|
431
|
+
const [ld, ls, lo] = left.step(state.left, input);
|
|
432
|
+
const [rd, rs, ro] = right.step(state.right, input);
|
|
433
|
+
const cont = ld.continue || rd.continue;
|
|
434
|
+
const delay = Math.min(ld.delayMs, rd.delayMs);
|
|
435
|
+
return [{ continue: cont, delayMs: delay }, { left: ls, right: rs }, [lo, ro]];
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
function retryWithSchedule(effect, schedule) {
|
|
440
|
+
const loop = (state) => asyncFold(
|
|
441
|
+
effect,
|
|
442
|
+
(error) => {
|
|
443
|
+
const [decision, nextState, _output] = schedule.step(state, error);
|
|
444
|
+
if (!decision.continue) return asyncFail(error);
|
|
445
|
+
if (decision.delayMs <= 0) return loop(nextState);
|
|
446
|
+
return asyncFlatMap(sleep(decision.delayMs), () => loop(nextState));
|
|
447
|
+
},
|
|
448
|
+
(value) => asyncSucceed(value)
|
|
449
|
+
);
|
|
450
|
+
return loop(schedule.initial());
|
|
451
|
+
}
|
|
452
|
+
function repeatWithSchedule(effect, schedule) {
|
|
453
|
+
const loop = (state, lastValue) => {
|
|
454
|
+
const [decision, nextState, _output] = schedule.step(state, lastValue);
|
|
455
|
+
if (!decision.continue) return asyncSucceed(lastValue);
|
|
456
|
+
if (decision.delayMs <= 0) {
|
|
457
|
+
return asyncFold(
|
|
458
|
+
effect,
|
|
459
|
+
(error) => asyncFail(error),
|
|
460
|
+
(value) => loop(nextState, value)
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
return asyncFlatMap(
|
|
464
|
+
sleep(decision.delayMs),
|
|
465
|
+
() => asyncFold(
|
|
466
|
+
effect,
|
|
467
|
+
(error) => asyncFail(error),
|
|
468
|
+
(value) => loop(nextState, value)
|
|
469
|
+
)
|
|
470
|
+
);
|
|
471
|
+
};
|
|
472
|
+
return asyncFold(
|
|
473
|
+
effect,
|
|
474
|
+
(error) => asyncFail(error),
|
|
475
|
+
(value) => loop(schedule.initial(), value)
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// src/core/runtime/shutdown.ts
|
|
480
|
+
async function gracefulShutdown(runtime, config = {}) {
|
|
481
|
+
const timeoutMs = config.timeoutMs ?? 3e4;
|
|
482
|
+
const startedAt = Date.now();
|
|
483
|
+
config.onStart?.();
|
|
484
|
+
let timedOut = false;
|
|
485
|
+
const shutdownPromise = (async () => {
|
|
486
|
+
try {
|
|
487
|
+
await runtime.shutdown();
|
|
488
|
+
} catch {
|
|
489
|
+
}
|
|
490
|
+
})();
|
|
491
|
+
const timeoutPromise = new Promise((resolve) => {
|
|
492
|
+
setTimeout(() => {
|
|
493
|
+
timedOut = true;
|
|
494
|
+
resolve();
|
|
495
|
+
}, timeoutMs);
|
|
496
|
+
});
|
|
497
|
+
await Promise.race([shutdownPromise, timeoutPromise]);
|
|
498
|
+
const completedAt = Date.now();
|
|
499
|
+
const stats = {
|
|
500
|
+
startedAt,
|
|
501
|
+
completedAt,
|
|
502
|
+
elapsedMs: completedAt - startedAt,
|
|
503
|
+
timedOut
|
|
504
|
+
};
|
|
505
|
+
if (timedOut) {
|
|
506
|
+
config.onTimeout?.(stats);
|
|
507
|
+
} else {
|
|
508
|
+
config.onComplete?.(stats);
|
|
509
|
+
}
|
|
510
|
+
return stats;
|
|
511
|
+
}
|
|
512
|
+
function registerShutdownHooks(runtime, config = {}) {
|
|
513
|
+
let shuttingDown = false;
|
|
514
|
+
const handler = (signal) => {
|
|
515
|
+
if (shuttingDown) {
|
|
516
|
+
process.exit(1);
|
|
517
|
+
}
|
|
518
|
+
shuttingDown = true;
|
|
519
|
+
console.log(`
|
|
520
|
+
[brass-runtime] Received ${signal}, shutting down gracefully...`);
|
|
521
|
+
gracefulShutdown(runtime, {
|
|
522
|
+
...config,
|
|
523
|
+
onComplete: (stats) => {
|
|
524
|
+
config.onComplete?.(stats);
|
|
525
|
+
if (!config.onComplete) {
|
|
526
|
+
console.log(`[brass-runtime] Shutdown complete (${stats.elapsedMs}ms)`);
|
|
527
|
+
process.exit(0);
|
|
528
|
+
}
|
|
529
|
+
},
|
|
530
|
+
onTimeout: (stats) => {
|
|
531
|
+
config.onTimeout?.(stats);
|
|
532
|
+
if (!config.onTimeout) {
|
|
533
|
+
console.log(`[brass-runtime] Shutdown timed out after ${stats.elapsedMs}ms, forcing exit`);
|
|
534
|
+
process.exit(1);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
};
|
|
539
|
+
process.on("SIGTERM", () => handler("SIGTERM"));
|
|
540
|
+
process.on("SIGINT", () => handler("SIGINT"));
|
|
541
|
+
return () => {
|
|
542
|
+
process.removeAllListeners("SIGTERM");
|
|
543
|
+
process.removeAllListeners("SIGINT");
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// src/core/runtime/testing.ts
|
|
548
|
+
function makeTestRuntime(env, options) {
|
|
549
|
+
const runtime = Runtime.make(env ?? {});
|
|
550
|
+
const run = (effect) => runtime.toPromise(effect);
|
|
551
|
+
const runExit = (effect) => new Promise((resolve) => {
|
|
552
|
+
runtime.unsafeRunAsync(effect, resolve);
|
|
553
|
+
});
|
|
554
|
+
return { runtime, run, runExit };
|
|
555
|
+
}
|
|
556
|
+
async function assertSucceeds(effect, expected, runtime) {
|
|
557
|
+
const rt = runtime ?? Runtime.make({});
|
|
558
|
+
const exit = await new Promise((resolve) => {
|
|
559
|
+
rt.unsafeRunAsync(effect, resolve);
|
|
560
|
+
});
|
|
561
|
+
if (exit._tag !== "Success") {
|
|
562
|
+
throw new Error(`Expected success with ${JSON.stringify(expected)}, got failure: ${JSON.stringify(exit.cause)}`);
|
|
563
|
+
}
|
|
564
|
+
if (JSON.stringify(exit.value) !== JSON.stringify(expected)) {
|
|
565
|
+
throw new Error(`Expected ${JSON.stringify(expected)}, got ${JSON.stringify(exit.value)}`);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
async function assertFails(effect, expectedError, runtime) {
|
|
569
|
+
const rt = runtime ?? Runtime.make({});
|
|
570
|
+
const exit = await new Promise((resolve) => {
|
|
571
|
+
rt.unsafeRunAsync(effect, resolve);
|
|
572
|
+
});
|
|
573
|
+
if (exit._tag !== "Failure") {
|
|
574
|
+
throw new Error(`Expected failure with ${JSON.stringify(expectedError)}, got success: ${JSON.stringify(exit.value)}`);
|
|
575
|
+
}
|
|
576
|
+
const error = exit.cause.error;
|
|
577
|
+
if (JSON.stringify(error) !== JSON.stringify(expectedError)) {
|
|
578
|
+
throw new Error(`Expected error ${JSON.stringify(expectedError)}, got ${JSON.stringify(error)}`);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
async function assertFailsWith(effect, predicate, runtime) {
|
|
582
|
+
const rt = runtime ?? Runtime.make({});
|
|
583
|
+
const exit = await new Promise((resolve) => {
|
|
584
|
+
rt.unsafeRunAsync(effect, resolve);
|
|
585
|
+
});
|
|
586
|
+
if (exit._tag !== "Failure") {
|
|
587
|
+
throw new Error(`Expected failure, got success: ${JSON.stringify(exit.value)}`);
|
|
588
|
+
}
|
|
589
|
+
const error = exit.cause.error;
|
|
590
|
+
if (!predicate(error)) {
|
|
591
|
+
throw new Error(`Error did not match predicate: ${JSON.stringify(error)}`);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
async function assertCompletesWithin(effect, maxMs, runtime) {
|
|
595
|
+
const rt = runtime ?? Runtime.make({});
|
|
596
|
+
const start = performance.now();
|
|
597
|
+
const result = await rt.toPromise(effect);
|
|
598
|
+
const elapsed2 = performance.now() - start;
|
|
599
|
+
if (elapsed2 > maxMs) {
|
|
600
|
+
throw new Error(`Effect took ${elapsed2.toFixed(1)}ms, expected < ${maxMs}ms`);
|
|
601
|
+
}
|
|
602
|
+
return result;
|
|
603
|
+
}
|
|
604
|
+
function flakyEffect(failCount, successValue, errorValue) {
|
|
605
|
+
let calls = 0;
|
|
606
|
+
return asyncEffect((_env, cb) => {
|
|
607
|
+
calls++;
|
|
608
|
+
if (calls <= failCount) {
|
|
609
|
+
cb({ _tag: "Failure", cause: { _tag: "Fail", error: errorValue } });
|
|
610
|
+
} else {
|
|
611
|
+
cb({ _tag: "Success", value: successValue });
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
function delayedEffect(ms, value) {
|
|
616
|
+
return asyncEffect((_env, cb) => {
|
|
617
|
+
const id = setTimeout(() => cb({ _tag: "Success", value }), ms);
|
|
618
|
+
return () => clearTimeout(id);
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
function neverEffect() {
|
|
622
|
+
return asyncEffect(() => {
|
|
623
|
+
return () => {
|
|
624
|
+
};
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
// src/core/runtime/layer.ts
|
|
629
|
+
function layer(acquire, release) {
|
|
630
|
+
return {
|
|
631
|
+
_tag: "Layer",
|
|
632
|
+
build: (_deps) => asyncFlatMap(acquire(), (service) => asyncSucceed({
|
|
633
|
+
service,
|
|
634
|
+
release: release ? () => release(service) : () => unit()
|
|
635
|
+
}))
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
function layerFrom() {
|
|
639
|
+
return (acquire, release) => ({
|
|
640
|
+
_tag: "Layer",
|
|
641
|
+
build: (deps) => asyncFlatMap(acquire(deps), (service) => asyncSucceed({
|
|
642
|
+
service,
|
|
643
|
+
release: release ? () => release(service) : () => unit()
|
|
644
|
+
}))
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
function layerSucceed(value) {
|
|
648
|
+
return {
|
|
649
|
+
_tag: "Layer",
|
|
650
|
+
build: () => asyncSucceed({ service: value, release: () => unit() })
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
function layerFail(error) {
|
|
654
|
+
return {
|
|
655
|
+
_tag: "Layer",
|
|
656
|
+
build: () => asyncFail(error)
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
function compose(from, to) {
|
|
660
|
+
return {
|
|
661
|
+
_tag: "Layer",
|
|
662
|
+
build: (deps) => asyncFlatMap(
|
|
663
|
+
from.build(deps),
|
|
664
|
+
({ service: mid, release: releaseMid }) => asyncFold(
|
|
665
|
+
to.build(mid),
|
|
666
|
+
(error) => asyncFlatMap(releaseMid(), () => asyncFail(error)),
|
|
667
|
+
({ service: out, release: releaseOut }) => asyncSucceed({
|
|
668
|
+
service: out,
|
|
669
|
+
release: () => asyncFlatMap(releaseOut(), () => releaseMid())
|
|
670
|
+
})
|
|
671
|
+
)
|
|
672
|
+
)
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
function merge(left, right) {
|
|
676
|
+
return {
|
|
677
|
+
_tag: "Layer",
|
|
678
|
+
build: (deps) => asyncFlatMap(
|
|
679
|
+
left.build(deps),
|
|
680
|
+
({ service: a, release: releaseA }) => asyncFold(
|
|
681
|
+
right.build(deps),
|
|
682
|
+
(error) => asyncFlatMap(releaseA(), () => asyncFail(error)),
|
|
683
|
+
({ service: b, release: releaseB }) => asyncSucceed({
|
|
684
|
+
service: { ...a, ...b },
|
|
685
|
+
release: () => asyncFlatMap(releaseB(), () => releaseA())
|
|
686
|
+
})
|
|
687
|
+
)
|
|
688
|
+
)
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
function mapLayer(l, f) {
|
|
692
|
+
return {
|
|
693
|
+
_tag: "Layer",
|
|
694
|
+
build: (deps) => asyncFlatMap(
|
|
695
|
+
l.build(deps),
|
|
696
|
+
({ service, release }) => asyncSucceed({ service: f(service), release })
|
|
697
|
+
)
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
function provideLayer(l, use, deps) {
|
|
701
|
+
return asyncFlatMap(
|
|
702
|
+
l.build(deps ?? {}),
|
|
703
|
+
({ service, release }) => asyncFold(
|
|
704
|
+
use(service),
|
|
705
|
+
(error) => asyncFlatMap(release(), () => asyncFail(error)),
|
|
706
|
+
(value) => asyncFlatMap(release(), () => asyncSucceed(value))
|
|
707
|
+
)
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// src/core/runtime/workerPool.ts
|
|
712
|
+
function makeWorkerPool(config = {}) {
|
|
713
|
+
const size = config.size ?? 4;
|
|
714
|
+
const maxQueue = config.maxQueue ?? 1e3;
|
|
715
|
+
const taskTimeoutMs = config.taskTimeoutMs ?? 3e4;
|
|
716
|
+
let closed = false;
|
|
717
|
+
let busy = 0;
|
|
718
|
+
let completed = 0;
|
|
719
|
+
let failed = 0;
|
|
720
|
+
let timedOut = 0;
|
|
721
|
+
const queue = [];
|
|
722
|
+
const processNext = () => {
|
|
723
|
+
if (queue.length === 0 || busy >= size) return;
|
|
724
|
+
const task = queue.shift();
|
|
725
|
+
busy++;
|
|
726
|
+
setImmediate(() => {
|
|
727
|
+
if (task.timeoutId) clearTimeout(task.timeoutId);
|
|
728
|
+
try {
|
|
729
|
+
const result = task.fn();
|
|
730
|
+
busy--;
|
|
731
|
+
completed++;
|
|
732
|
+
task.resolve(result);
|
|
733
|
+
processNext();
|
|
734
|
+
} catch (e) {
|
|
735
|
+
busy--;
|
|
736
|
+
failed++;
|
|
737
|
+
task.reject({ _tag: "WorkerTaskError", message: String(e) });
|
|
738
|
+
processNext();
|
|
739
|
+
}
|
|
740
|
+
});
|
|
741
|
+
};
|
|
742
|
+
const execute = (fn) => {
|
|
743
|
+
if (closed) return asyncFail({ _tag: "WorkerPoolClosed" });
|
|
744
|
+
if (queue.length >= maxQueue) {
|
|
745
|
+
return asyncFail({ _tag: "WorkerPoolFull", queued: queue.length });
|
|
746
|
+
}
|
|
747
|
+
return asyncEffect((_env, cb) => {
|
|
748
|
+
const task = {
|
|
749
|
+
fn,
|
|
750
|
+
resolve: (value) => cb({ _tag: "Success", value }),
|
|
751
|
+
reject: (error) => cb({ _tag: "Failure", cause: { _tag: "Fail", error } })
|
|
752
|
+
};
|
|
753
|
+
task.timeoutId = setTimeout(() => {
|
|
754
|
+
const idx = queue.indexOf(task);
|
|
755
|
+
if (idx >= 0) {
|
|
756
|
+
queue.splice(idx, 1);
|
|
757
|
+
timedOut++;
|
|
758
|
+
task.reject({ _tag: "WorkerTaskTimeout", ms: taskTimeoutMs });
|
|
759
|
+
}
|
|
760
|
+
}, taskTimeoutMs);
|
|
761
|
+
queue.push(task);
|
|
762
|
+
processNext();
|
|
763
|
+
return () => {
|
|
764
|
+
const idx = queue.indexOf(task);
|
|
765
|
+
if (idx >= 0) {
|
|
766
|
+
queue.splice(idx, 1);
|
|
767
|
+
if (task.timeoutId) clearTimeout(task.timeoutId);
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
});
|
|
771
|
+
};
|
|
772
|
+
const run = (taskSource, args = []) => {
|
|
773
|
+
return execute(() => {
|
|
774
|
+
const fn = new Function(...args.map((_, i) => `arg${i}`), taskSource);
|
|
775
|
+
return fn(...args);
|
|
776
|
+
});
|
|
777
|
+
};
|
|
778
|
+
return {
|
|
779
|
+
execute,
|
|
780
|
+
run,
|
|
781
|
+
stats: () => ({
|
|
782
|
+
size,
|
|
783
|
+
busy,
|
|
784
|
+
idle: size - busy,
|
|
785
|
+
queued: queue.length,
|
|
786
|
+
completed,
|
|
787
|
+
failed,
|
|
788
|
+
timedOut
|
|
789
|
+
}),
|
|
790
|
+
shutdown: async () => {
|
|
791
|
+
closed = true;
|
|
792
|
+
while (queue.length > 0) {
|
|
793
|
+
const task = queue.shift();
|
|
794
|
+
if (task.timeoutId) clearTimeout(task.timeoutId);
|
|
795
|
+
task.reject({ _tag: "WorkerPoolClosed" });
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
// src/core/runtime/tracing.ts
|
|
802
|
+
var idCounter = 0;
|
|
803
|
+
function generateId() {
|
|
804
|
+
return (++idCounter).toString(16).padStart(16, "0");
|
|
805
|
+
}
|
|
806
|
+
function makeTracer(config) {
|
|
807
|
+
const completedSpans = [];
|
|
808
|
+
const sampleRate = config.sampleRate ?? 1;
|
|
809
|
+
const shouldSample = () => {
|
|
810
|
+
if (sampleRate >= 1) return true;
|
|
811
|
+
if (sampleRate <= 0) return false;
|
|
812
|
+
return Math.random() < sampleRate;
|
|
813
|
+
};
|
|
814
|
+
const span = (name, effect, attributes) => {
|
|
815
|
+
if (!shouldSample()) return effect;
|
|
816
|
+
const spanObj = {
|
|
817
|
+
name,
|
|
818
|
+
context: {
|
|
819
|
+
traceId: generateId(),
|
|
820
|
+
spanId: generateId()
|
|
821
|
+
},
|
|
822
|
+
startTime: performance.now(),
|
|
823
|
+
status: "unset",
|
|
824
|
+
attributes: { "service.name": config.serviceName, ...attributes },
|
|
825
|
+
events: []
|
|
826
|
+
};
|
|
827
|
+
return asyncFold(
|
|
828
|
+
effect,
|
|
829
|
+
(error) => {
|
|
830
|
+
spanObj.endTime = performance.now();
|
|
831
|
+
spanObj.status = "error";
|
|
832
|
+
spanObj.events.push({
|
|
833
|
+
name: "error",
|
|
834
|
+
time: performance.now(),
|
|
835
|
+
attributes: { "error.message": String(error) }
|
|
836
|
+
});
|
|
837
|
+
completedSpans.push(spanObj);
|
|
838
|
+
config.onSpanEnd?.(spanObj);
|
|
839
|
+
return asyncFail(error);
|
|
840
|
+
},
|
|
841
|
+
(value) => {
|
|
842
|
+
spanObj.endTime = performance.now();
|
|
843
|
+
spanObj.status = "ok";
|
|
844
|
+
completedSpans.push(spanObj);
|
|
845
|
+
config.onSpanEnd?.(spanObj);
|
|
846
|
+
return asyncSucceed(value);
|
|
847
|
+
}
|
|
848
|
+
);
|
|
849
|
+
};
|
|
850
|
+
return {
|
|
851
|
+
span,
|
|
852
|
+
spans: () => completedSpans,
|
|
853
|
+
clear: () => {
|
|
854
|
+
completedSpans.length = 0;
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
// src/core/runtime/metrics.ts
|
|
860
|
+
var DEFAULT_BOUNDARIES = [1, 5, 10, 25, 50, 100, 250, 500, 1e3, 5e3, 1e4];
|
|
861
|
+
function makeMetrics() {
|
|
862
|
+
const counters = /* @__PURE__ */ new Map();
|
|
863
|
+
const gauges = /* @__PURE__ */ new Map();
|
|
864
|
+
const histograms = /* @__PURE__ */ new Map();
|
|
865
|
+
const key = (name, labels) => labels ? `${name}|${Object.entries(labels).sort().map(([k, v]) => `${k}=${v}`).join(",")}` : name;
|
|
866
|
+
const counter = (name, labels = {}) => {
|
|
867
|
+
const k = key(name, labels);
|
|
868
|
+
if (!counters.has(k)) counters.set(k, { labels, value: 0 });
|
|
869
|
+
const entry = counters.get(k);
|
|
870
|
+
return {
|
|
871
|
+
increment: (n = 1) => {
|
|
872
|
+
entry.value += Math.max(0, n);
|
|
873
|
+
},
|
|
874
|
+
value: () => entry.value
|
|
875
|
+
};
|
|
876
|
+
};
|
|
877
|
+
const gauge = (name, labels = {}) => {
|
|
878
|
+
const k = key(name, labels);
|
|
879
|
+
if (!gauges.has(k)) gauges.set(k, { labels, value: 0 });
|
|
880
|
+
const entry = gauges.get(k);
|
|
881
|
+
return {
|
|
882
|
+
set: (v) => {
|
|
883
|
+
entry.value = v;
|
|
884
|
+
},
|
|
885
|
+
increment: (n = 1) => {
|
|
886
|
+
entry.value += n;
|
|
887
|
+
},
|
|
888
|
+
decrement: (n = 1) => {
|
|
889
|
+
entry.value -= n;
|
|
890
|
+
},
|
|
891
|
+
value: () => entry.value
|
|
892
|
+
};
|
|
893
|
+
};
|
|
894
|
+
const histogram = (name, boundaries = DEFAULT_BOUNDARIES, labels = {}) => {
|
|
895
|
+
const k = key(name, labels);
|
|
896
|
+
if (!histograms.has(k)) {
|
|
897
|
+
const sorted = [...boundaries].sort((a, b) => a - b);
|
|
898
|
+
histograms.set(k, {
|
|
899
|
+
labels,
|
|
900
|
+
boundaries: sorted,
|
|
901
|
+
data: { boundaries: sorted, counts: new Array(sorted.length + 1).fill(0), sum: 0, count: 0, min: Infinity, max: -Infinity }
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
const entry = histograms.get(k);
|
|
905
|
+
return {
|
|
906
|
+
observe: (value) => {
|
|
907
|
+
entry.data.sum += value;
|
|
908
|
+
entry.data.count++;
|
|
909
|
+
entry.data.min = Math.min(entry.data.min, value);
|
|
910
|
+
entry.data.max = Math.max(entry.data.max, value);
|
|
911
|
+
let placed = false;
|
|
912
|
+
for (let i = 0; i < entry.boundaries.length; i++) {
|
|
913
|
+
if (value <= entry.boundaries[i]) {
|
|
914
|
+
entry.data.counts[i]++;
|
|
915
|
+
placed = true;
|
|
916
|
+
break;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
if (!placed) entry.data.counts[entry.boundaries.length]++;
|
|
920
|
+
},
|
|
921
|
+
buckets: () => ({ ...entry.data }),
|
|
922
|
+
percentile: (p) => {
|
|
923
|
+
const target = Math.ceil(entry.data.count * (p / 100));
|
|
924
|
+
let cumulative = 0;
|
|
925
|
+
for (let i = 0; i < entry.boundaries.length; i++) {
|
|
926
|
+
cumulative += entry.data.counts[i];
|
|
927
|
+
if (cumulative >= target) return entry.boundaries[i];
|
|
928
|
+
}
|
|
929
|
+
return entry.data.max;
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
};
|
|
933
|
+
return {
|
|
934
|
+
counter,
|
|
935
|
+
gauge,
|
|
936
|
+
histogram,
|
|
937
|
+
snapshot: () => ({
|
|
938
|
+
counters: Array.from(counters.entries()).map(([k, v]) => ({ name: k.split("|")[0], labels: v.labels, value: v.value })),
|
|
939
|
+
gauges: Array.from(gauges.entries()).map(([k, v]) => ({ name: k.split("|")[0], labels: v.labels, value: v.value })),
|
|
940
|
+
histograms: Array.from(histograms.entries()).map(([k, v]) => ({ name: k.split("|")[0], labels: v.labels, buckets: v.data }))
|
|
941
|
+
}),
|
|
942
|
+
reset: () => {
|
|
943
|
+
counters.clear();
|
|
944
|
+
gauges.clear();
|
|
945
|
+
histograms.clear();
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
// src/core/types/typedError.ts
|
|
951
|
+
function catchTag(effect, tag, handler) {
|
|
952
|
+
return asyncFold(
|
|
953
|
+
effect,
|
|
954
|
+
(error) => {
|
|
955
|
+
if (typeof error === "object" && error !== null && "_tag" in error && error._tag === tag) {
|
|
956
|
+
return handler(error);
|
|
957
|
+
}
|
|
958
|
+
return asyncFail(error);
|
|
959
|
+
},
|
|
960
|
+
(value) => asyncSucceed(value)
|
|
961
|
+
);
|
|
962
|
+
}
|
|
963
|
+
function catchTags(effect, handlers) {
|
|
964
|
+
return asyncFold(
|
|
965
|
+
effect,
|
|
966
|
+
(error) => {
|
|
967
|
+
if (typeof error === "object" && error !== null && "_tag" in error) {
|
|
968
|
+
const handler = handlers[error._tag];
|
|
969
|
+
if (handler) return handler(error);
|
|
970
|
+
}
|
|
971
|
+
return asyncFail(error);
|
|
972
|
+
},
|
|
973
|
+
(value) => asyncSucceed(value)
|
|
974
|
+
);
|
|
975
|
+
}
|
|
976
|
+
function mapError(effect, f) {
|
|
977
|
+
return asyncFold(effect, (error) => asyncFail(f(error)), asyncSucceed);
|
|
978
|
+
}
|
|
979
|
+
function tagError(effect, tag, enrich) {
|
|
980
|
+
return asyncFold(
|
|
981
|
+
effect,
|
|
982
|
+
(error) => {
|
|
983
|
+
const fields = enrich ? enrich(error) : {};
|
|
984
|
+
return asyncFail({ _tag: tag, ...fields });
|
|
985
|
+
},
|
|
986
|
+
asyncSucceed
|
|
987
|
+
);
|
|
988
|
+
}
|
|
989
|
+
function orElse(effect, fallback) {
|
|
990
|
+
return asyncFold(effect, fallback, asyncSucceed);
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
export {
|
|
994
|
+
makeCancelToken,
|
|
995
|
+
linkAbortController,
|
|
996
|
+
bracket,
|
|
997
|
+
ensuring,
|
|
998
|
+
managed,
|
|
999
|
+
useManaged,
|
|
1000
|
+
managedAll,
|
|
1001
|
+
LinkedQueue,
|
|
1002
|
+
makeSemaphore,
|
|
1003
|
+
makeRef,
|
|
1004
|
+
derivedRef,
|
|
1005
|
+
recurs,
|
|
1006
|
+
fixed,
|
|
1007
|
+
exponential,
|
|
1008
|
+
jittered,
|
|
1009
|
+
elapsed,
|
|
1010
|
+
whileInput,
|
|
1011
|
+
take,
|
|
1012
|
+
andThen,
|
|
1013
|
+
intersect,
|
|
1014
|
+
union,
|
|
1015
|
+
retryWithSchedule,
|
|
1016
|
+
repeatWithSchedule,
|
|
1017
|
+
gracefulShutdown,
|
|
1018
|
+
registerShutdownHooks,
|
|
1019
|
+
makeTestRuntime,
|
|
1020
|
+
assertSucceeds,
|
|
1021
|
+
assertFails,
|
|
1022
|
+
assertFailsWith,
|
|
1023
|
+
assertCompletesWithin,
|
|
1024
|
+
flakyEffect,
|
|
1025
|
+
delayedEffect,
|
|
1026
|
+
neverEffect,
|
|
1027
|
+
layer,
|
|
1028
|
+
layerFrom,
|
|
1029
|
+
layerSucceed,
|
|
1030
|
+
layerFail,
|
|
1031
|
+
compose,
|
|
1032
|
+
merge,
|
|
1033
|
+
mapLayer,
|
|
1034
|
+
provideLayer,
|
|
1035
|
+
makeWorkerPool,
|
|
1036
|
+
makeTracer,
|
|
1037
|
+
makeMetrics,
|
|
1038
|
+
catchTag,
|
|
1039
|
+
catchTags,
|
|
1040
|
+
mapError,
|
|
1041
|
+
tagError,
|
|
1042
|
+
orElse
|
|
1043
|
+
};
|