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