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
|
+
"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;
|
|
2
|
+
|
|
3
|
+
var _chunkMS34J5LYcjs = require('./chunk-MS34J5LY.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
var _chunkTGIFUAK4cjs = require('./chunk-TGIFUAK4.cjs');
|
|
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 (e2) {
|
|
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 _chunkTGIFUAK4cjs.asyncEffect.call(void 0, (env, cb) => {
|
|
56
|
+
const runtime = _chunkTGIFUAK4cjs.unsafeGetCurrentRuntime.call(void 0, );
|
|
57
|
+
const scope = new (0, _chunkTGIFUAK4cjs.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(_chunkTGIFUAK4cjs.Exit.failCause(_chunkTGIFUAK4cjs.Cause.interrupt()));
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
function safeRelease(release, resource, exit) {
|
|
82
|
+
return _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
83
|
+
(() => {
|
|
84
|
+
try {
|
|
85
|
+
return release(resource, exit);
|
|
86
|
+
} catch (e3) {
|
|
87
|
+
return _chunkTGIFUAK4cjs.unit.call(void 0, );
|
|
88
|
+
}
|
|
89
|
+
})(),
|
|
90
|
+
() => _chunkTGIFUAK4cjs.unit.call(void 0, ),
|
|
91
|
+
() => _chunkTGIFUAK4cjs.unit.call(void 0, )
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
function ensuring(effect, finalizer) {
|
|
95
|
+
return _chunkTGIFUAK4cjs.asyncEffect.call(void 0, (env, cb) => {
|
|
96
|
+
const runtime = _chunkTGIFUAK4cjs.unsafeGetCurrentRuntime.call(void 0, );
|
|
97
|
+
const fiber = runtime.fork(effect);
|
|
98
|
+
fiber.join((exit) => {
|
|
99
|
+
const fin = _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
100
|
+
(() => {
|
|
101
|
+
try {
|
|
102
|
+
return finalizer(exit);
|
|
103
|
+
} catch (e4) {
|
|
104
|
+
return _chunkTGIFUAK4cjs.unit.call(void 0, );
|
|
105
|
+
}
|
|
106
|
+
})(),
|
|
107
|
+
() => _chunkTGIFUAK4cjs.unit.call(void 0, ),
|
|
108
|
+
() => _chunkTGIFUAK4cjs.unit.call(void 0, )
|
|
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 = _chunkTGIFUAK4cjs.asyncEffect.call(void 0, (env, cb) => {
|
|
131
|
+
const runtime = _chunkTGIFUAK4cjs.unsafeGetCurrentRuntime.call(void 0, );
|
|
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 _chunkTGIFUAK4cjs.asyncEffect.call(void 0, (_env, cb) => {
|
|
157
|
+
const runtime = _chunkTGIFUAK4cjs.unsafeGetCurrentRuntime.call(void 0, );
|
|
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 = _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
171
|
+
m.release(resources[i], exit),
|
|
172
|
+
() => _chunkTGIFUAK4cjs.unit.call(void 0, ),
|
|
173
|
+
() => _chunkTGIFUAK4cjs.unit.call(void 0, )
|
|
174
|
+
);
|
|
175
|
+
runtime.fork(releaseEff).join(() => resolve());
|
|
176
|
+
});
|
|
177
|
+
} catch (e5) {
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/core/runtime/linkedQueue.ts
|
|
183
|
+
var LinkedQueue = (_class = class {constructor() { _class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this); }
|
|
184
|
+
__init() {this.head = null}
|
|
185
|
+
__init2() {this.tail = null}
|
|
186
|
+
__init3() {this.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
|
+
}, _class);
|
|
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 _chunkTGIFUAK4cjs.asyncEffect.call(void 0, (_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 _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0,
|
|
257
|
+
acquire(),
|
|
258
|
+
() => _chunkTGIFUAK4cjs.asyncEffect.call(void 0, (_env, cb) => {
|
|
259
|
+
const runtime = _chunkTGIFUAK4cjs.unsafeGetCurrentRuntime.call(void 0, );
|
|
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: () => _chunkTGIFUAK4cjs.asyncSync.call(void 0, () => value),
|
|
293
|
+
set: (v) => _chunkTGIFUAK4cjs.asyncSync.call(void 0, () => {
|
|
294
|
+
value = v;
|
|
295
|
+
}),
|
|
296
|
+
update: (f) => _chunkTGIFUAK4cjs.asyncSync.call(void 0, () => {
|
|
297
|
+
value = f(value);
|
|
298
|
+
return value;
|
|
299
|
+
}),
|
|
300
|
+
modify: (f) => _chunkTGIFUAK4cjs.asyncSync.call(void 0, () => {
|
|
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: () => _chunkTGIFUAK4cjs.asyncSync.call(void 0, () => get(parent.unsafeGet())),
|
|
311
|
+
set: (b) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, parent.get(), (current) => parent.set(set(current, b))),
|
|
312
|
+
update: (f) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, parent.get(), (parentVal) => {
|
|
313
|
+
const newB = f(get(parentVal));
|
|
314
|
+
return _chunkTGIFUAK4cjs.asyncMap.call(void 0, parent.set(set(parentVal, newB)), () => newB);
|
|
315
|
+
}),
|
|
316
|
+
modify: (f) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, parent.get(), (parentVal) => {
|
|
317
|
+
const [result, newB] = f(get(parentVal));
|
|
318
|
+
return _chunkTGIFUAK4cjs.asyncMap.call(void 0, 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) => _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
441
|
+
effect,
|
|
442
|
+
(error) => {
|
|
443
|
+
const [decision, nextState, _output] = schedule.step(state, error);
|
|
444
|
+
if (!decision.continue) return _chunkTGIFUAK4cjs.asyncFail.call(void 0, error);
|
|
445
|
+
if (decision.delayMs <= 0) return loop(nextState);
|
|
446
|
+
return _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, _chunkMS34J5LYcjs.sleep.call(void 0, decision.delayMs), () => loop(nextState));
|
|
447
|
+
},
|
|
448
|
+
(value) => _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, 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 _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, lastValue);
|
|
456
|
+
if (decision.delayMs <= 0) {
|
|
457
|
+
return _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
458
|
+
effect,
|
|
459
|
+
(error) => _chunkTGIFUAK4cjs.asyncFail.call(void 0, error),
|
|
460
|
+
(value) => loop(nextState, value)
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
return _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0,
|
|
464
|
+
_chunkMS34J5LYcjs.sleep.call(void 0, decision.delayMs),
|
|
465
|
+
() => _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
466
|
+
effect,
|
|
467
|
+
(error) => _chunkTGIFUAK4cjs.asyncFail.call(void 0, error),
|
|
468
|
+
(value) => loop(nextState, value)
|
|
469
|
+
)
|
|
470
|
+
);
|
|
471
|
+
};
|
|
472
|
+
return _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
473
|
+
effect,
|
|
474
|
+
(error) => _chunkTGIFUAK4cjs.asyncFail.call(void 0, 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 = _nullishCoalesce(config.timeoutMs, () => ( 3e4));
|
|
482
|
+
const startedAt = Date.now();
|
|
483
|
+
_optionalChain([config, 'access', _2 => _2.onStart, 'optionalCall', _3 => _3()]);
|
|
484
|
+
let timedOut = false;
|
|
485
|
+
const shutdownPromise = (async () => {
|
|
486
|
+
try {
|
|
487
|
+
await runtime.shutdown();
|
|
488
|
+
} catch (e6) {
|
|
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
|
+
_optionalChain([config, 'access', _4 => _4.onTimeout, 'optionalCall', _5 => _5(stats)]);
|
|
507
|
+
} else {
|
|
508
|
+
_optionalChain([config, 'access', _6 => _6.onComplete, 'optionalCall', _7 => _7(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
|
+
_optionalChain([config, 'access', _8 => _8.onComplete, 'optionalCall', _9 => _9(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
|
+
_optionalChain([config, 'access', _10 => _10.onTimeout, 'optionalCall', _11 => _11(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 = _chunkTGIFUAK4cjs.Runtime.make(_nullishCoalesce(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 = _nullishCoalesce(runtime, () => ( _chunkTGIFUAK4cjs.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 = _nullishCoalesce(runtime, () => ( _chunkTGIFUAK4cjs.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 = _nullishCoalesce(runtime, () => ( _chunkTGIFUAK4cjs.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 = _nullishCoalesce(runtime, () => ( _chunkTGIFUAK4cjs.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 _chunkTGIFUAK4cjs.asyncEffect.call(void 0, (_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 _chunkTGIFUAK4cjs.asyncEffect.call(void 0, (_env, cb) => {
|
|
617
|
+
const id = setTimeout(() => cb({ _tag: "Success", value }), ms);
|
|
618
|
+
return () => clearTimeout(id);
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
function neverEffect() {
|
|
622
|
+
return _chunkTGIFUAK4cjs.asyncEffect.call(void 0, () => {
|
|
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) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, acquire(), (service) => _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, {
|
|
633
|
+
service,
|
|
634
|
+
release: release ? () => release(service) : () => _chunkTGIFUAK4cjs.unit.call(void 0, )
|
|
635
|
+
}))
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
function layerFrom() {
|
|
639
|
+
return (acquire, release) => ({
|
|
640
|
+
_tag: "Layer",
|
|
641
|
+
build: (deps) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, acquire(deps), (service) => _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, {
|
|
642
|
+
service,
|
|
643
|
+
release: release ? () => release(service) : () => _chunkTGIFUAK4cjs.unit.call(void 0, )
|
|
644
|
+
}))
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
function layerSucceed(value) {
|
|
648
|
+
return {
|
|
649
|
+
_tag: "Layer",
|
|
650
|
+
build: () => _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, { service: value, release: () => _chunkTGIFUAK4cjs.unit.call(void 0, ) })
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
function layerFail(error) {
|
|
654
|
+
return {
|
|
655
|
+
_tag: "Layer",
|
|
656
|
+
build: () => _chunkTGIFUAK4cjs.asyncFail.call(void 0, error)
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
function compose(from, to) {
|
|
660
|
+
return {
|
|
661
|
+
_tag: "Layer",
|
|
662
|
+
build: (deps) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0,
|
|
663
|
+
from.build(deps),
|
|
664
|
+
({ service: mid, release: releaseMid }) => _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
665
|
+
to.build(mid),
|
|
666
|
+
(error) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, releaseMid(), () => _chunkTGIFUAK4cjs.asyncFail.call(void 0, error)),
|
|
667
|
+
({ service: out, release: releaseOut }) => _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, {
|
|
668
|
+
service: out,
|
|
669
|
+
release: () => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, releaseOut(), () => releaseMid())
|
|
670
|
+
})
|
|
671
|
+
)
|
|
672
|
+
)
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
function merge(left, right) {
|
|
676
|
+
return {
|
|
677
|
+
_tag: "Layer",
|
|
678
|
+
build: (deps) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0,
|
|
679
|
+
left.build(deps),
|
|
680
|
+
({ service: a, release: releaseA }) => _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
681
|
+
right.build(deps),
|
|
682
|
+
(error) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, releaseA(), () => _chunkTGIFUAK4cjs.asyncFail.call(void 0, error)),
|
|
683
|
+
({ service: b, release: releaseB }) => _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, {
|
|
684
|
+
service: { ...a, ...b },
|
|
685
|
+
release: () => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, releaseB(), () => releaseA())
|
|
686
|
+
})
|
|
687
|
+
)
|
|
688
|
+
)
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
function mapLayer(l, f) {
|
|
692
|
+
return {
|
|
693
|
+
_tag: "Layer",
|
|
694
|
+
build: (deps) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0,
|
|
695
|
+
l.build(deps),
|
|
696
|
+
({ service, release }) => _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, { service: f(service), release })
|
|
697
|
+
)
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
function provideLayer(l, use, deps) {
|
|
701
|
+
return _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0,
|
|
702
|
+
l.build(_nullishCoalesce(deps, () => ( {}))),
|
|
703
|
+
({ service, release }) => _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
704
|
+
use(service),
|
|
705
|
+
(error) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, release(), () => _chunkTGIFUAK4cjs.asyncFail.call(void 0, error)),
|
|
706
|
+
(value) => _chunkTGIFUAK4cjs.asyncFlatMap.call(void 0, release(), () => _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, value))
|
|
707
|
+
)
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// src/core/runtime/workerPool.ts
|
|
712
|
+
function makeWorkerPool(config = {}) {
|
|
713
|
+
const size = _nullishCoalesce(config.size, () => ( 4));
|
|
714
|
+
const maxQueue = _nullishCoalesce(config.maxQueue, () => ( 1e3));
|
|
715
|
+
const taskTimeoutMs = _nullishCoalesce(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 _chunkTGIFUAK4cjs.asyncFail.call(void 0, { _tag: "WorkerPoolClosed" });
|
|
744
|
+
if (queue.length >= maxQueue) {
|
|
745
|
+
return _chunkTGIFUAK4cjs.asyncFail.call(void 0, { _tag: "WorkerPoolFull", queued: queue.length });
|
|
746
|
+
}
|
|
747
|
+
return _chunkTGIFUAK4cjs.asyncEffect.call(void 0, (_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 = _nullishCoalesce(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 _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
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
|
+
_optionalChain([config, 'access', _12 => _12.onSpanEnd, 'optionalCall', _13 => _13(spanObj)]);
|
|
839
|
+
return _chunkTGIFUAK4cjs.asyncFail.call(void 0, error);
|
|
840
|
+
},
|
|
841
|
+
(value) => {
|
|
842
|
+
spanObj.endTime = performance.now();
|
|
843
|
+
spanObj.status = "ok";
|
|
844
|
+
completedSpans.push(spanObj);
|
|
845
|
+
_optionalChain([config, 'access', _14 => _14.onSpanEnd, 'optionalCall', _15 => _15(spanObj)]);
|
|
846
|
+
return _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, 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 _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
953
|
+
effect,
|
|
954
|
+
(error) => {
|
|
955
|
+
if (typeof error === "object" && error !== null && "_tag" in error && error._tag === tag) {
|
|
956
|
+
return handler(error);
|
|
957
|
+
}
|
|
958
|
+
return _chunkTGIFUAK4cjs.asyncFail.call(void 0, error);
|
|
959
|
+
},
|
|
960
|
+
(value) => _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, value)
|
|
961
|
+
);
|
|
962
|
+
}
|
|
963
|
+
function catchTags(effect, handlers) {
|
|
964
|
+
return _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
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 _chunkTGIFUAK4cjs.asyncFail.call(void 0, error);
|
|
972
|
+
},
|
|
973
|
+
(value) => _chunkTGIFUAK4cjs.asyncSucceed.call(void 0, value)
|
|
974
|
+
);
|
|
975
|
+
}
|
|
976
|
+
function mapError(effect, f) {
|
|
977
|
+
return _chunkTGIFUAK4cjs.asyncFold.call(void 0, effect, (error) => _chunkTGIFUAK4cjs.asyncFail.call(void 0, f(error)), _chunkTGIFUAK4cjs.asyncSucceed);
|
|
978
|
+
}
|
|
979
|
+
function tagError(effect, tag, enrich) {
|
|
980
|
+
return _chunkTGIFUAK4cjs.asyncFold.call(void 0,
|
|
981
|
+
effect,
|
|
982
|
+
(error) => {
|
|
983
|
+
const fields = enrich ? enrich(error) : {};
|
|
984
|
+
return _chunkTGIFUAK4cjs.asyncFail.call(void 0, { _tag: tag, ...fields });
|
|
985
|
+
},
|
|
986
|
+
_chunkTGIFUAK4cjs.asyncSucceed
|
|
987
|
+
);
|
|
988
|
+
}
|
|
989
|
+
function orElse(effect, fallback) {
|
|
990
|
+
return _chunkTGIFUAK4cjs.asyncFold.call(void 0, effect, fallback, _chunkTGIFUAK4cjs.asyncSucceed);
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
|
|
1025
|
+
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
exports.makeCancelToken = makeCancelToken; exports.linkAbortController = linkAbortController; exports.bracket = bracket; exports.ensuring = ensuring; exports.managed = managed; exports.useManaged = useManaged; exports.managedAll = managedAll; exports.LinkedQueue = LinkedQueue; exports.makeSemaphore = makeSemaphore; exports.makeRef = makeRef; exports.derivedRef = derivedRef; exports.recurs = recurs; exports.fixed = fixed; exports.exponential = exponential; exports.jittered = jittered; exports.elapsed = elapsed; exports.whileInput = whileInput; exports.take = take; exports.andThen = andThen; exports.intersect = intersect; exports.union = union; exports.retryWithSchedule = retryWithSchedule; exports.repeatWithSchedule = repeatWithSchedule; 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.makeMetrics = makeMetrics; exports.catchTag = catchTag; exports.catchTags = catchTags; exports.mapError = mapError; exports.tagError = tagError; exports.orElse = orElse;
|