brass-runtime 1.13.8 → 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.
Files changed (49) hide show
  1. package/README.md +6 -3
  2. package/dist/agent/cli/main.cjs +44 -43
  3. package/dist/agent/cli/main.js +5 -4
  4. package/dist/agent/cli/main.mjs +5 -4
  5. package/dist/agent/index.cjs +4 -3
  6. package/dist/agent/index.d.ts +1 -1
  7. package/dist/agent/index.js +3 -2
  8. package/dist/agent/index.mjs +3 -2
  9. package/dist/{chunk-3R7ZYRK2.mjs → chunk-3QMOKAS5.js} +9 -7
  10. package/dist/{chunk-ATHSSDUF.js → chunk-4NHES7VK.mjs} +113 -31
  11. package/dist/chunk-AR22SXML.js +1043 -0
  12. package/dist/chunk-BDF4AMWX.mjs +3773 -0
  13. package/dist/chunk-BDYEENHT.js +224 -0
  14. package/dist/chunk-BMH5AV44.js +3773 -0
  15. package/dist/chunk-ELOOF35R.mjs +131 -0
  16. package/dist/chunk-JFPU5GQI.mjs +1043 -0
  17. package/dist/{chunk-INZBKOHY.js → chunk-K6M7MDZ4.mjs} +9 -7
  18. package/dist/chunk-MS34J5LY.cjs +224 -0
  19. package/dist/{chunk-XNOTJSMZ.mjs → chunk-PPUXIH5R.js} +113 -31
  20. package/dist/chunk-R3R2FVLG.cjs +131 -0
  21. package/dist/{chunk-ZTDK2DLG.cjs → chunk-STVLQ3XD.cjs} +169 -87
  22. package/dist/chunk-TGIFUAK4.cjs +3773 -0
  23. package/dist/chunk-TO7IKXYT.js +131 -0
  24. package/dist/chunk-UMAZLXAB.mjs +224 -0
  25. package/dist/{chunk-XDINDYNA.cjs → chunk-VEZNF5GZ.cjs} +136 -134
  26. package/dist/chunk-XPZNXSVN.cjs +1043 -0
  27. package/dist/core/index.cjs +216 -0
  28. package/dist/core/index.d.ts +673 -0
  29. package/dist/core/index.js +216 -0
  30. package/dist/core/index.mjs +216 -0
  31. package/dist/{effect-ISvXPLgc.d.ts → effect-CMOQKX8y.d.ts} +202 -31
  32. package/dist/http/index.cjs +3177 -187
  33. package/dist/http/index.d.ts +1692 -9
  34. package/dist/http/index.js +3164 -174
  35. package/dist/http/index.mjs +3164 -174
  36. package/dist/index.cjs +936 -219
  37. package/dist/index.d.ts +313 -36
  38. package/dist/index.js +830 -113
  39. package/dist/index.mjs +830 -113
  40. package/dist/{stream-BvukHxCv.d.ts → stream-FQm9h4Mg.d.ts} +12 -4
  41. package/dist/tracing-DNT9jEbr.d.ts +106 -0
  42. package/package.json +11 -3
  43. package/wasm/pkg/brass_runtime_wasm_engine.d.ts +95 -16
  44. package/wasm/pkg/brass_runtime_wasm_engine.js +715 -15
  45. package/wasm/pkg/brass_runtime_wasm_engine_bg.wasm +0 -0
  46. package/wasm/pkg/brass_runtime_wasm_engine_bg.wasm.d.ts +78 -7
  47. package/dist/chunk-2P4PD6D7.cjs +0 -2557
  48. package/dist/chunk-7F2R7A2V.mjs +0 -2557
  49. package/dist/chunk-L6KKKM66.js +0 -2557
@@ -1,8 +1,11 @@
1
+ import {
2
+ raceWith
3
+ } from "./chunk-ELOOF35R.mjs";
1
4
  import {
2
5
  Cause,
3
6
  Exit,
4
7
  Scope,
5
- async,
8
+ asyncEffect,
6
9
  asyncFail,
7
10
  asyncFlatMap,
8
11
  asyncFold,
@@ -16,13 +19,12 @@ import {
16
19
  mapError,
17
20
  none,
18
21
  orElseOptional,
19
- raceWith,
20
22
  some,
21
23
  succeed,
22
24
  sync,
23
25
  unsafeGetCurrentRuntime,
24
26
  unsafeRunFoldWithEnv
25
- } from "./chunk-L6KKKM66.js";
27
+ } from "./chunk-BDF4AMWX.mjs";
26
28
 
27
29
  // src/core/stream/stream.ts
28
30
  var widenOpt = (opt) => opt._tag === "None" ? none : some(opt.value);
@@ -73,7 +75,7 @@ function streamToRaceWithHandler(winnerSide, leftStream, rightStream, flip, id)
73
75
  return asyncSucceed([a, next]);
74
76
  }
75
77
  if (exit.cause._tag === "Interrupt") {
76
- return async((_env, cb) => {
78
+ return asyncEffect((_env, cb) => {
77
79
  cb(Exit.failCause(Cause.interrupt()));
78
80
  });
79
81
  }
@@ -88,7 +90,7 @@ function makeMergePull(onLeft, onRight, flip, mergePullId) {
88
90
  const id = ++mergePullId;
89
91
  const onLeftHandler = streamToRaceWithHandler("L", onLeft, onRight, flip, id);
90
92
  const onRightHandler = streamToRaceWithHandler("R", onLeft, onRight, flip, id);
91
- return async((_env, cb) => {
93
+ return asyncEffect((_env, cb) => {
92
94
  const runtime = unsafeGetCurrentRuntime();
93
95
  const scope = new Scope(runtime);
94
96
  const handler = raceWith(
@@ -111,6 +113,12 @@ function uncons(self) {
111
113
  switch (self._tag) {
112
114
  case "Empty":
113
115
  return fail(none);
116
+ case "FromArray": {
117
+ const arr = self.values;
118
+ if (arr.length === 0) return fail(none);
119
+ const tail = arr.length === 1 ? EMPTY_STREAM : { _tag: "FromArray", values: arr.slice(1) };
120
+ return succeed([arr[0], tail]);
121
+ }
114
122
  case "Emit":
115
123
  return map(
116
124
  mapError(self.value, (e) => some(e)),
@@ -143,7 +151,7 @@ function uncons(self) {
143
151
  case "Merge":
144
152
  return makeMergePull(self.left, self.right, self.flip, 0);
145
153
  case "Scoped":
146
- return async((env, cb) => {
154
+ return asyncEffect((env, cb) => {
147
155
  const runtime = unsafeGetCurrentRuntime();
148
156
  const scope = new Scope(runtime);
149
157
  const fiber = getCurrentFiber();
@@ -164,7 +172,7 @@ function uncons(self) {
164
172
  scope.close(exit);
165
173
  };
166
174
  const wrap = (s) => fromPull(
167
- async((env2, cb2) => {
175
+ asyncEffect((env2, cb2) => {
168
176
  const pull = uncons(s);
169
177
  unsafeRunFoldWithEnv(
170
178
  pull,
@@ -192,7 +200,7 @@ function uncons(self) {
192
200
  });
193
201
  });
194
202
  case "Managed":
195
- return async((env, cb) => {
203
+ return asyncEffect((env, cb) => {
196
204
  const runtime = unsafeGetCurrentRuntime();
197
205
  const scope = new Scope(runtime);
198
206
  getCurrentFiber()?.addFinalizer((exit) => {
@@ -220,16 +228,19 @@ function uncons(self) {
220
228
  const { stream: inner, release } = ex.value;
221
229
  scope.addFinalizer((exit) => release(exit));
222
230
  const wrap = (s) => fromPull(
223
- async((env2, cb2) => {
224
- uncons(s)(env2, (ex2) => {
225
- if (ex2._tag === "Failure") {
231
+ asyncEffect((env2, cb2) => {
232
+ unsafeRunFoldWithEnv(
233
+ uncons(s),
234
+ env2,
235
+ (cause) => {
236
+ const ex2 = Exit.failCause(cause);
226
237
  closeWith(ex2);
227
238
  cb2(ex2);
228
- return;
239
+ },
240
+ ([a, tail]) => {
241
+ cb2(Exit.succeed([a, wrap(tail)]));
229
242
  }
230
- const [a, tail] = ex2.value;
231
- cb2(Exit.succeed([a, wrap(tail)]));
232
- });
243
+ );
233
244
  })
234
245
  );
235
246
  unsafeGetCurrentRuntime().fork(uncons(wrap(inner))).join(cb);
@@ -244,6 +255,8 @@ function mapStream(self, f) {
244
255
  switch (self._tag) {
245
256
  case "Empty":
246
257
  return emptyStream();
258
+ case "FromArray":
259
+ return { _tag: "FromArray", values: self.values.map(f) };
247
260
  case "Emit":
248
261
  return emitStream(map(self.value, f));
249
262
  case "FromPull":
@@ -327,58 +340,127 @@ function foreachStream(stream, f) {
327
340
  );
328
341
  }
329
342
  function fromArray(values) {
330
- let s = emptyStream();
331
- for (let i = values.length - 1; i >= 0; i--) {
332
- const head = emitStream(succeed(values[i]));
333
- s = concatStream(head, s);
334
- }
335
- return s;
343
+ if (values.length === 0) return emptyStream();
344
+ return { _tag: "FromArray", values };
336
345
  }
337
346
  function collectStream(stream) {
347
+ const syncResult = drainStreamSyncFull(stream);
348
+ if (syncResult !== null) {
349
+ return asyncSucceed(syncResult);
350
+ }
338
351
  const loop = (cur, acc) => asyncFold(
339
352
  uncons(cur),
340
353
  (opt) => {
341
354
  if (opt._tag === "None") return succeed(acc);
342
355
  return fail(opt);
343
356
  },
344
- ([a, tail]) => loop(tail, [...acc, a])
357
+ ([a, tail]) => {
358
+ acc.push(a);
359
+ return loop(tail, acc);
360
+ }
345
361
  );
346
362
  return mapError(loop(stream, []), (opt) => {
347
363
  if (opt._tag === "Some") return opt.value;
348
364
  throw new Error("unreachable: stream end handled as success");
349
365
  });
350
366
  }
351
- function readerStream(reader, normalizeError) {
352
- const pull = async((_, cb) => {
353
- reader.read().then(({ done, value }) => {
354
- if (done) {
355
- cb({ _tag: "Failure", cause: { _tag: "Fail", error: none } });
367
+ function drainStreamSyncFull(stream) {
368
+ const result = [];
369
+ let cur = stream;
370
+ while (true) {
371
+ switch (cur._tag) {
372
+ case "Empty":
373
+ return result;
374
+ case "FromArray": {
375
+ const arr = cur.values;
376
+ for (let i = 0; i < arr.length; i++) {
377
+ result.push(arr[i]);
378
+ }
379
+ return result;
380
+ }
381
+ case "Emit": {
382
+ const zio = cur.value;
383
+ if (zio._tag === "Succeed") {
384
+ result.push(zio.value);
385
+ return result;
386
+ }
387
+ return null;
388
+ }
389
+ case "Concat": {
390
+ const leftItems = drainStreamSyncFull(cur.left);
391
+ if (leftItems === null) return null;
392
+ for (let i = 0; i < leftItems.length; i++) {
393
+ result.push(leftItems[i]);
394
+ }
395
+ cur = cur.right;
396
+ break;
397
+ }
398
+ default:
399
+ return null;
400
+ }
401
+ }
402
+ }
403
+ function readerStream(reader, normalizeError, signal) {
404
+ const pull = asyncEffect((_, cb) => {
405
+ let done = false;
406
+ const cleanup = () => signal?.removeEventListener("abort", abort);
407
+ const finish = (exit) => {
408
+ if (done) return;
409
+ done = true;
410
+ cleanup();
411
+ cb(exit);
412
+ };
413
+ const abort = () => {
414
+ try {
415
+ reader.cancel();
416
+ } catch {
417
+ }
418
+ const error = typeof DOMException === "function" ? new DOMException("aborted", "AbortError") : new Error("aborted");
419
+ finish({ _tag: "Failure", cause: { _tag: "Fail", error: some(normalizeError(error)) } });
420
+ };
421
+ if (signal?.aborted) {
422
+ abort();
423
+ return;
424
+ }
425
+ signal?.addEventListener("abort", abort, { once: true });
426
+ reader.read().then(({ done: done2, value }) => {
427
+ if (done2) {
428
+ finish({ _tag: "Failure", cause: { _tag: "Fail", error: none } });
356
429
  return;
357
430
  }
358
- cb({
431
+ finish({
359
432
  _tag: "Success",
360
433
  value: [value, fromPull(pull)]
361
434
  });
362
435
  }).catch((e) => {
363
- cb({ _tag: "Failure", cause: { _tag: "Fail", error: some(normalizeError(e)) } });
436
+ finish({ _tag: "Failure", cause: { _tag: "Fail", error: some(normalizeError(e)) } });
364
437
  });
438
+ return () => {
439
+ cleanup();
440
+ try {
441
+ reader.cancel();
442
+ } catch {
443
+ }
444
+ };
365
445
  });
366
446
  return fromPull(pull);
367
447
  }
368
- function streamFromReadableStream(body, normalizeError) {
448
+ function streamFromReadableStream(body, normalizeError, options = {}) {
369
449
  if (!body) return emptyStream();
370
450
  let reader;
371
451
  return unwrapScoped(
372
452
  // acquire: produce un ZStream
373
453
  sync(() => {
374
454
  reader = body.getReader();
375
- return readerStream(reader, normalizeError);
455
+ return readerStream(reader, normalizeError, options.signal);
376
456
  }),
377
457
  // release: se corre en fin / error / interrupción
378
458
  () => asyncSync(() => {
379
459
  try {
380
460
  reader?.cancel();
381
461
  } catch {
462
+ } finally {
463
+ options.onRelease?.();
382
464
  }
383
465
  })
384
466
  );