unikvs 0.0.13 → 0.0.15

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/dist/index.js CHANGED
@@ -24,7 +24,9 @@ var _MiddlewareNextCalledMultipleTimesError;
24
24
  var _StreamEncodingNotSupportedError;
25
25
  var _StreamDecodingNotSupportedError;
26
26
  var _PluginOperationAggregateError;
27
+ var _InvalidForkError;
27
28
  var _MissingStorageError;
29
+ var _UnterminatedPipelineError;
28
30
  /**
29
31
  * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#errors)
30
32
  */
@@ -276,6 +278,17 @@ setErrorMessage(PluginOperationAggregateError, ({ action, errors, plugin }) => `
276
278
  /**
277
279
  * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#errors)
278
280
  */
281
+ var InvalidForkError = class extends ErrorBase {
282
+ constructor(options) {
283
+ super("A forked branch does not reach any storage", options);
284
+ }
285
+ };
286
+ _InvalidForkError = InvalidForkError;
287
+ _InvalidForkError.prototype.name = "UniKvsInvalidForkError";
288
+ setErrorMessage(InvalidForkError, "ストレージに到達しない分岐があります", "ja");
289
+ /**
290
+ * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#errors)
291
+ */
279
292
  var MissingStorageError = class extends ErrorBase {
280
293
  constructor(options) {
281
294
  super("At least one storage is required to use UniKvs", options);
@@ -284,6 +297,17 @@ var MissingStorageError = class extends ErrorBase {
284
297
  _MissingStorageError = MissingStorageError;
285
298
  _MissingStorageError.prototype.name = "UniKvsMissingStorageError";
286
299
  setErrorMessage(MissingStorageError, "UniKvs を使用するためには最低 1 つのストレージが必要です", "ja");
300
+ /**
301
+ * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#errors)
302
+ */
303
+ var UnterminatedPipelineError = class extends ErrorBase {
304
+ constructor(options) {
305
+ super("The pipeline does not end with a storage", options);
306
+ }
307
+ };
308
+ _UnterminatedPipelineError = UnterminatedPipelineError;
309
+ _UnterminatedPipelineError.prototype.name = "UniKvsUnterminatedPipelineError";
310
+ setErrorMessage(UnterminatedPipelineError, "パイプラインがストレージで終わっていません。use() の後に persist() または fork() を呼び出してください", "ja");
287
311
  //#endregion
288
312
  //#region \0@oxc-project+runtime@0.154.0/helpers/esm/checkPrivateRedeclaration.js
289
313
  function _checkPrivateRedeclaration(e, t) {
@@ -572,7 +596,8 @@ var _lifecycle$1 = /* @__PURE__ */ new WeakMap();
572
596
  */
573
597
  var UniKvsMiddleware = class UniKvsMiddleware {
574
598
  constructor(middleware) {
575
- _classPrivateFieldInitSpec(this, _mw, void _initProto$1(this));
599
+ this.name = void _initProto$1(this);
600
+ _classPrivateFieldInitSpec(this, _mw, void 0);
576
601
  _classPrivateFieldInitSpec(this, _lifecycle$1, void 0);
577
602
  _classPrivateFieldSet2(_mw, this, middleware);
578
603
  _classPrivateFieldSet2(_lifecycle$1, this, new PluginLifecycle(middleware));
@@ -924,7 +949,8 @@ var _lifecycle = /* @__PURE__ */ new WeakMap();
924
949
  */
925
950
  var UniKvsStorage = class UniKvsStorage {
926
951
  constructor(storage) {
927
- _classPrivateFieldInitSpec(this, _io, void _initProto(this));
952
+ this.name = void _initProto(this);
953
+ _classPrivateFieldInitSpec(this, _io, void 0);
928
954
  _classPrivateFieldInitSpec(this, _lifecycle, void 0);
929
955
  _classPrivateFieldSet2(_io, this, storage);
930
956
  _classPrivateFieldSet2(_lifecycle, this, new PluginLifecycle(storage));
@@ -1084,34 +1110,23 @@ _UniKvsStorage = UniKvsStorage;
1084
1110
  */
1085
1111
  const branchStates = /* #__PURE__ */ new WeakMap();
1086
1112
  /**
1087
- * 分岐先のスナップショットを返します。
1113
+ * 分岐先を親の接頭辞付きで追加します。
1088
1114
  *
1089
- * 親の `fork` で接頭辞付加するために使用します。
1090
1115
  * `new Branch()` で作成された分岐以外の値が渡されたときは例外を投げます。
1091
1116
  * 外部からは使用しません。
1092
1117
  *
1093
- * @param branch スナップショットを取得する分岐です。
1094
- *
1095
- * @returns 分岐先のコピーを返します。
1096
- */
1097
- function getBranchDestinationsForFork(branch) {
1098
- const state = branchStates.get(branch);
1099
- if (!state) throw new TypeError("fork() expects a branch created by new Branch().");
1100
- return state.destinations.map((dest) => ({
1101
- storage: dest.storage,
1102
- middlewares: [...dest.middlewares]
1103
- }));
1104
- }
1105
- /**
1106
- * 分岐先を親の接頭辞付きで追加します。
1107
- *
1108
1118
  * @param destinations 追加先の分岐先列です。
1109
1119
  * @param prefix 親のミドルウェア列です。
1110
1120
  * @param branch 追加する分岐です。
1121
+ *
1122
+ * @throws 分岐がストレージに到達しない場合に `InvalidForkError` を投げます。
1111
1123
  */
1112
1124
  function appendForkDestinations(destinations, prefix, branch) {
1125
+ const state = branchStates.get(branch);
1126
+ if (!state) throw new TypeError("fork() expects a branch created by new Branch().");
1127
+ if (state.destinations.length === 0) throw new InvalidForkError();
1113
1128
  const prefixSnapshot = [...prefix];
1114
- for (const dest of getBranchDestinationsForFork(branch)) destinations.push({
1129
+ for (const dest of state.destinations) destinations.push({
1115
1130
  storage: dest.storage,
1116
1131
  middlewares: [...prefixSnapshot, ...dest.middlewares]
1117
1132
  });
@@ -1250,6 +1265,14 @@ var PipelineContext = class {
1250
1265
  _classPrivateFieldGet2(_readonly, this).add(name);
1251
1266
  return this;
1252
1267
  }
1268
+ /**
1269
+ * 現在の実行時変数の浅いコピーを作成します。
1270
+ *
1271
+ * @returns 実行時変数のスナップショットを返します。
1272
+ */
1273
+ snapshot() {
1274
+ return new Map(_classPrivateFieldGet2(_data, this));
1275
+ }
1253
1276
  };
1254
1277
  var MapKeyContext = class extends PipelineContext {
1255
1278
  /**
@@ -3014,8 +3037,8 @@ var UniKvs = class {
3014
3037
  let ownsStream = stream !== null;
3015
3038
  try {
3016
3039
  try {
3017
- var _usingCtx6 = _usingCtx();
3018
- _usingCtx6.u(await asyncmux.readonly(this, signal));
3040
+ var _usingCtx3 = _usingCtx();
3041
+ _usingCtx3.u(await asyncmux.readonly(this, signal));
3019
3042
  if (_classPrivateFieldGet2(_con, this) === null) throw new UniKvsIsNotOpenError();
3020
3043
  const useContext = new SetContext({
3021
3044
  key,
@@ -3029,9 +3052,9 @@ var UniKvs = class {
3029
3052
  await _assertClassBrand(_UniKvs_brand, this, _setStream).call(this, io, signal, key, store, stream);
3030
3053
  } else await _assertClassBrand(_UniKvs_brand, this, _setPlain).call(this, io, signal, key, store, value);
3031
3054
  } catch (_) {
3032
- _usingCtx6.e = _;
3055
+ _usingCtx3.e = _;
3033
3056
  } finally {
3034
- _usingCtx6.d();
3057
+ _usingCtx3.d();
3035
3058
  }
3036
3059
  } catch (ex) {
3037
3060
  if (ownsStream && stream !== null) try {
@@ -3042,7 +3065,7 @@ var UniKvs = class {
3042
3065
  }
3043
3066
  async get(...args) {
3044
3067
  try {
3045
- var _usingCtx7 = _usingCtx();
3068
+ var _usingCtx4 = _usingCtx();
3046
3069
  if (_classPrivateFieldGet2(_con, this) === null) throw new UniKvsIsNotOpenError();
3047
3070
  const [options] = parseInput(GetArgsSchema, args);
3048
3071
  const { key, vars: varsOption, repair = false, signal: signalOption, throwOnRepairDenied = false } = options;
@@ -3050,7 +3073,7 @@ var UniKvs = class {
3050
3073
  const { ac, io } = _classPrivateFieldGet2(_con, this);
3051
3074
  const signal = combineSignals([ac.signal, signalOption]);
3052
3075
  const store = _assertClassBrand(_UniKvs_brand, this, _createOperationStore).call(this, varsOption);
3053
- _usingCtx7.u(await asyncmux.readonly(this, signal));
3076
+ _usingCtx4.u(await asyncmux.readonly(this, signal));
3054
3077
  if (_classPrivateFieldGet2(_con, this) === null) throw new UniKvsIsNotOpenError();
3055
3078
  const leafNames = _classPrivateFieldGet2(_pipeline, this).leaves.map((leaf) => leaf.name);
3056
3079
  const useContext = new GetContext({
@@ -3072,8 +3095,8 @@ var UniKvs = class {
3072
3095
  const missing = [];
3073
3096
  const errors = [];
3074
3097
  try {
3075
- var _usingCtx8 = _usingCtx();
3076
- _usingCtx8.u(await (repair ? io.lock({
3098
+ var _usingCtx5 = _usingCtx();
3099
+ _usingCtx5.u(await (repair ? io.lock({
3077
3100
  key,
3078
3101
  signal
3079
3102
  }) : io.rLock({
@@ -3126,9 +3149,9 @@ var UniKvs = class {
3126
3149
  if (hit === null) throw _assertClassBrand(_UniKvs_brand, this, _notFound).call(this, key, errors);
3127
3150
  if (repair) data = await _assertClassBrand(_UniKvs_brand, this, _repairPlain).call(this, data, hit, missing, store, key, signal, throwOnRepairDenied);
3128
3151
  } catch (_) {
3129
- _usingCtx8.e = _;
3152
+ _usingCtx5.e = _;
3130
3153
  } finally {
3131
- _usingCtx8.d();
3154
+ _usingCtx5.d();
3132
3155
  }
3133
3156
  if (!repair) data = await _classPrivateFieldGet2(_pipeline, this).decode(hit.leaf, {
3134
3157
  key,
@@ -3138,9 +3161,9 @@ var UniKvs = class {
3138
3161
  if (valueSchema) data = parseOutput(valueSchema.schema, data);
3139
3162
  return data;
3140
3163
  } catch (_) {
3141
- _usingCtx7.e = _;
3164
+ _usingCtx4.e = _;
3142
3165
  } finally {
3143
- _usingCtx7.d();
3166
+ _usingCtx4.d();
3144
3167
  }
3145
3168
  }
3146
3169
  async stream(...args) {
@@ -3152,8 +3175,8 @@ var UniKvs = class {
3152
3175
  const signal = combineSignals([ac.signal, signalOption]);
3153
3176
  const store = _assertClassBrand(_UniKvs_brand, this, _createOperationStore).call(this, varsOption);
3154
3177
  try {
3155
- var _usingCtx9 = _usingCtx();
3156
- _usingCtx9.u(await asyncmux.readonly(this, signal));
3178
+ var _usingCtx6 = _usingCtx();
3179
+ _usingCtx6.u(await asyncmux.readonly(this, signal));
3157
3180
  if (_classPrivateFieldGet2(_con, this) === null) throw new UniKvsIsNotOpenError();
3158
3181
  const leafNames = _classPrivateFieldGet2(_pipeline, this).leaves.map((leaf) => leaf.name);
3159
3182
  const useContext = new StreamContext({
@@ -3356,14 +3379,14 @@ var UniKvs = class {
3356
3379
  throw ex;
3357
3380
  }
3358
3381
  } catch (_) {
3359
- _usingCtx9.e = _;
3382
+ _usingCtx6.e = _;
3360
3383
  } finally {
3361
- _usingCtx9.d();
3384
+ _usingCtx6.d();
3362
3385
  }
3363
3386
  }
3364
3387
  async has(...args) {
3365
3388
  try {
3366
- var _usingCtx10 = _usingCtx();
3389
+ var _usingCtx7 = _usingCtx();
3367
3390
  if (_classPrivateFieldGet2(_con, this) === null) throw new UniKvsIsNotOpenError();
3368
3391
  const [options] = parseInput(HasArgsSchema, args);
3369
3392
  const { key, vars: varsOption, signal: signalOption } = options;
@@ -3371,7 +3394,7 @@ var UniKvs = class {
3371
3394
  const { ac, io } = _classPrivateFieldGet2(_con, this);
3372
3395
  const signal = combineSignals([ac.signal, signalOption]);
3373
3396
  const store = _assertClassBrand(_UniKvs_brand, this, _createOperationStore).call(this, varsOption);
3374
- _usingCtx10.u(await asyncmux.readonly(this, signal));
3397
+ _usingCtx7.u(await asyncmux.readonly(this, signal));
3375
3398
  if (_classPrivateFieldGet2(_con, this) === null) throw new UniKvsIsNotOpenError();
3376
3399
  const useContext = new HasContext({
3377
3400
  key,
@@ -3380,7 +3403,7 @@ var UniKvs = class {
3380
3403
  });
3381
3404
  await _assertClassBrand(_UniKvs_brand, this, _use).call(this, "read", useContext);
3382
3405
  throwIfAborted(signal);
3383
- _usingCtx10.u(await io.rLock({
3406
+ _usingCtx7.u(await io.rLock({
3384
3407
  key,
3385
3408
  signal
3386
3409
  }));
@@ -3412,14 +3435,14 @@ var UniKvs = class {
3412
3435
  if (errors.length > 0) throw _assertClassBrand(_UniKvs_brand, this, _notFound).call(this, key, errors);
3413
3436
  return false;
3414
3437
  } catch (_) {
3415
- _usingCtx10.e = _;
3438
+ _usingCtx7.e = _;
3416
3439
  } finally {
3417
- _usingCtx10.d();
3440
+ _usingCtx7.d();
3418
3441
  }
3419
3442
  }
3420
3443
  async delete(...args) {
3421
3444
  try {
3422
- var _usingCtx11 = _usingCtx();
3445
+ var _usingCtx8 = _usingCtx();
3423
3446
  if (_classPrivateFieldGet2(_con, this) === null) throw new UniKvsIsNotOpenError();
3424
3447
  const [options] = parseInput(DeleteArgsSchema, args);
3425
3448
  const { key, vars: varsOption, signal: signalOption } = options;
@@ -3427,7 +3450,7 @@ var UniKvs = class {
3427
3450
  const { ac, io } = _classPrivateFieldGet2(_con, this);
3428
3451
  const signal = combineSignals([ac.signal, signalOption]);
3429
3452
  const store = _assertClassBrand(_UniKvs_brand, this, _createOperationStore).call(this, varsOption);
3430
- _usingCtx11.u(await asyncmux.readonly(this, signal));
3453
+ _usingCtx8.u(await asyncmux.readonly(this, signal));
3431
3454
  if (_classPrivateFieldGet2(_con, this) === null) throw new UniKvsIsNotOpenError();
3432
3455
  const useContext = new DeleteContext({
3433
3456
  key,
@@ -3435,7 +3458,7 @@ var UniKvs = class {
3435
3458
  signal
3436
3459
  });
3437
3460
  await _assertClassBrand(_UniKvs_brand, this, _use).call(this, "delete", useContext);
3438
- _usingCtx11.u(await io.lock({
3461
+ _usingCtx8.u(await io.lock({
3439
3462
  key,
3440
3463
  signal
3441
3464
  }));
@@ -3473,23 +3496,23 @@ var UniKvs = class {
3473
3496
  plugin: "storage"
3474
3497
  });
3475
3498
  } catch (_) {
3476
- _usingCtx11.e = _;
3499
+ _usingCtx8.e = _;
3477
3500
  } finally {
3478
- _usingCtx11.d();
3501
+ _usingCtx8.d();
3479
3502
  }
3480
3503
  }
3481
3504
  async clear(...args) {
3482
3505
  try {
3483
- var _usingCtx12 = _usingCtx();
3506
+ var _usingCtx9 = _usingCtx();
3484
3507
  if (_classPrivateFieldGet2(_con, this) === null) throw new UniKvsIsNotOpenError();
3485
3508
  const [options = {}] = parseInput(ClearArgsSchema, args);
3486
3509
  const { vars: varsOption, signal: signalOption } = options;
3487
3510
  const { ac, io } = _classPrivateFieldGet2(_con, this);
3488
3511
  const signal = combineSignals([ac.signal, signalOption]);
3489
3512
  const store = _assertClassBrand(_UniKvs_brand, this, _createOperationStore).call(this, varsOption);
3490
- _usingCtx12.u(await asyncmux.readonly(this, signal));
3513
+ _usingCtx9.u(await asyncmux.readonly(this, signal));
3491
3514
  if (_classPrivateFieldGet2(_con, this) === null) throw new UniKvsIsNotOpenError();
3492
- _usingCtx12.u(await io.lock({ signal }));
3515
+ _usingCtx9.u(await io.lock({ signal }));
3493
3516
  const useContext = new ClearContext({
3494
3517
  store,
3495
3518
  signal
@@ -3518,9 +3541,9 @@ var UniKvs = class {
3518
3541
  plugin: "storage"
3519
3542
  });
3520
3543
  } catch (_) {
3521
- _usingCtx12.e = _;
3544
+ _usingCtx9.e = _;
3522
3545
  } finally {
3523
- _usingCtx12.d();
3546
+ _usingCtx9.d();
3524
3547
  }
3525
3548
  }
3526
3549
  };
@@ -3533,7 +3556,7 @@ var UniKvs = class {
3533
3556
  */
3534
3557
  function _createOperationVars(vars) {
3535
3558
  const map = new Map(_classPrivateFieldGet2(_baseVars, this));
3536
- if (vars) for (const [k, v] of vars) map.set(k, v);
3559
+ if (vars) for (const [k, value] of vars) map.set(k, value);
3537
3560
  return map;
3538
3561
  }
3539
3562
  /**
@@ -3585,11 +3608,11 @@ function _notFound(key, errors) {
3585
3608
  }
3586
3609
  async function _close(operationVars, signal, con, progress) {
3587
3610
  try {
3588
- var _usingCtx3 = _usingCtx();
3589
- _usingCtx3.u(await asyncmux(this, signal));
3611
+ var _usingCtx10 = _usingCtx();
3612
+ _usingCtx10.u(await asyncmux(this, signal));
3590
3613
  if (_classPrivateFieldGet2(_con, this) !== con) throw new UniKvsIsNotOpenError();
3591
3614
  const { io } = _classPrivateFieldGet2(_con, this);
3592
- _usingCtx3.u(await io.lock({ signal }));
3615
+ _usingCtx10.u(await io.lock({ signal }));
3593
3616
  const closeFns = [];
3594
3617
  for (const [index, leaf] of _classPrivateFieldGet2(_pipeline, this).leaves.entries()) {
3595
3618
  const createContext = () => new CloseContext({
@@ -3656,9 +3679,9 @@ async function _close(operationVars, signal, con, progress) {
3656
3679
  }
3657
3680
  _classPrivateFieldSet2(_con, this, null);
3658
3681
  } catch (_) {
3659
- _usingCtx3.e = _;
3682
+ _usingCtx10.e = _;
3660
3683
  } finally {
3661
- _usingCtx3.d();
3684
+ _usingCtx10.d();
3662
3685
  }
3663
3686
  }
3664
3687
  /**
@@ -3778,13 +3801,13 @@ async function _use(action, c) {
3778
3801
  */
3779
3802
  async function _setPlain(io, signal, key, store, value) {
3780
3803
  try {
3781
- var _usingCtx4 = _usingCtx();
3804
+ var _usingCtx11 = _usingCtx();
3782
3805
  const targets = await _classPrivateFieldGet2(_pipeline, this).encode({
3783
3806
  key,
3784
3807
  store,
3785
3808
  signal
3786
3809
  }, value);
3787
- _usingCtx4.u(await io.lock({
3810
+ _usingCtx11.u(await io.lock({
3788
3811
  key,
3789
3812
  signal
3790
3813
  }));
@@ -3809,9 +3832,9 @@ async function _setPlain(io, signal, key, store, value) {
3809
3832
  }));
3810
3833
  await _assertClassBrand(_UniKvs_brand, this, _finishWrites).call(this, targets, store, failed, errors, signal);
3811
3834
  } catch (_) {
3812
- _usingCtx4.e = _;
3835
+ _usingCtx11.e = _;
3813
3836
  } finally {
3814
- _usingCtx4.d();
3837
+ _usingCtx11.d();
3815
3838
  }
3816
3839
  }
3817
3840
  /**
@@ -3825,7 +3848,7 @@ async function _setPlain(io, signal, key, store, value) {
3825
3848
  */
3826
3849
  async function _setStream(io, signal, key, store, value) {
3827
3850
  try {
3828
- var _usingCtx5 = _usingCtx();
3851
+ var _usingCtx12 = _usingCtx();
3829
3852
  const streams = /* @__PURE__ */ new Set([value]);
3830
3853
  let tree;
3831
3854
  try {
@@ -3838,7 +3861,7 @@ async function _setStream(io, signal, key, store, value) {
3838
3861
  await cancelStreams(streams, ex);
3839
3862
  throw ex;
3840
3863
  }
3841
- _usingCtx5.u(await io.lock({
3864
+ _usingCtx12.u(await io.lock({
3842
3865
  key,
3843
3866
  signal
3844
3867
  }).catch(async (ex) => {
@@ -3870,9 +3893,9 @@ async function _setStream(io, signal, key, store, value) {
3870
3893
  }));
3871
3894
  await _assertClassBrand(_UniKvs_brand, this, _finishWrites).call(this, tree.targets, store, failed, errors, signal);
3872
3895
  } catch (_) {
3873
- _usingCtx5.e = _;
3896
+ _usingCtx12.e = _;
3874
3897
  } finally {
3875
- _usingCtx5.d();
3898
+ _usingCtx12.d();
3876
3899
  }
3877
3900
  }
3878
3901
  /**
@@ -3936,6 +3959,7 @@ const UniKvsBuilderOptionsSchema = object({
3936
3959
  const UniKvsBuilderArgsSchema = tuple([optional(UniKvsBuilderOptionsSchema)]);
3937
3960
  var _destinations = /* @__PURE__ */ new WeakMap();
3938
3961
  var _middlewares = /* @__PURE__ */ new WeakMap();
3962
+ var _hasUnboundMiddlewares = /* @__PURE__ */ new WeakMap();
3939
3963
  var _valueSchemaResolver = /* @__PURE__ */ new WeakMap();
3940
3964
  var _vars = /* @__PURE__ */ new WeakMap();
3941
3965
  /**
@@ -3945,22 +3969,26 @@ var UniKvsBuilder = class {
3945
3969
  constructor(...args) {
3946
3970
  _classPrivateFieldInitSpec(this, _destinations, void 0);
3947
3971
  _classPrivateFieldInitSpec(this, _middlewares, void 0);
3972
+ _classPrivateFieldInitSpec(this, _hasUnboundMiddlewares, void 0);
3948
3973
  _classPrivateFieldInitSpec(this, _valueSchemaResolver, void 0);
3949
3974
  _classPrivateFieldInitSpec(this, _vars, void 0);
3950
3975
  const [options = {}] = parseInput(UniKvsBuilderArgsSchema, args);
3951
3976
  const { vars, schema } = options;
3952
3977
  _classPrivateFieldSet2(_destinations, this, []);
3953
3978
  _classPrivateFieldSet2(_middlewares, this, []);
3979
+ _classPrivateFieldSet2(_hasUnboundMiddlewares, this, false);
3954
3980
  _classPrivateFieldSet2(_valueSchemaResolver, this, null);
3955
3981
  _classPrivateFieldSet2(_vars, this, new Map(vars));
3956
3982
  if (schema) _classPrivateFieldSet2(_valueSchemaResolver, this, createValueSchemaResolver(schema));
3957
3983
  }
3958
3984
  fork(branch) {
3959
3985
  appendForkDestinations(_classPrivateFieldGet2(_destinations, this), _classPrivateFieldGet2(_middlewares, this), branch);
3986
+ _classPrivateFieldSet2(_hasUnboundMiddlewares, this, false);
3960
3987
  return this;
3961
3988
  }
3962
3989
  use(middleware) {
3963
3990
  _classPrivateFieldGet2(_middlewares, this).push(UniKvsMiddleware.from(middleware));
3991
+ _classPrivateFieldSet2(_hasUnboundMiddlewares, this, true);
3964
3992
  return this;
3965
3993
  }
3966
3994
  persist(storage) {
@@ -3968,6 +3996,7 @@ var UniKvsBuilder = class {
3968
3996
  storage: UniKvsStorage.from(storage),
3969
3997
  middlewares: [..._classPrivateFieldGet2(_middlewares, this)]
3970
3998
  });
3999
+ _classPrivateFieldSet2(_hasUnboundMiddlewares, this, false);
3971
4000
  return this;
3972
4001
  }
3973
4002
  setVars(...args) {
@@ -3991,9 +4020,23 @@ var UniKvsBuilder = class {
3991
4020
  this.setVars(target);
3992
4021
  return this;
3993
4022
  }
3994
- build() {
4023
+ isValid() {
4024
+ return _classPrivateFieldGet2(_destinations, this).length > 0 && !_classPrivateFieldGet2(_hasUnboundMiddlewares, this);
4025
+ }
4026
+ build(options = {}) {
3995
4027
  if (_classPrivateFieldGet2(_destinations, this).length === 0) throw new MissingStorageError();
3996
- return new UniKvs([..._classPrivateFieldGet2(_vars, this).entries()], PipelineGraph.compile([..._classPrivateFieldGet2(_destinations, this)]), _classPrivateFieldGet2(_valueSchemaResolver, this));
4028
+ if (_classPrivateFieldGet2(_hasUnboundMiddlewares, this)) throw new UnterminatedPipelineError();
4029
+ const beforeAll = options.beforeAll?.map((mw) => UniKvsMiddleware.from(mw)) ?? [];
4030
+ const beforeStorage = options.beforeStorage?.map((mw) => UniKvsMiddleware.from(mw)) ?? [];
4031
+ const destinations = beforeAll.length === 0 && beforeStorage.length === 0 ? _classPrivateFieldGet2(_destinations, this) : _classPrivateFieldGet2(_destinations, this).map((dest) => ({
4032
+ storage: dest.storage,
4033
+ middlewares: [
4034
+ ...beforeAll,
4035
+ ...dest.middlewares,
4036
+ ...beforeStorage
4037
+ ]
4038
+ }));
4039
+ return new UniKvs([..._classPrivateFieldGet2(_vars, this).entries()], PipelineGraph.compile(destinations), _classPrivateFieldGet2(_valueSchemaResolver, this));
3997
4040
  }
3998
4041
  };
3999
4042
  //#endregion
@@ -4027,6 +4070,6 @@ function Value(schema) {
4027
4070
  return createValueSchemaDescriptor("any", schema);
4028
4071
  }
4029
4072
  //#endregion
4030
- export { UniKvsBranch as Branch, UniKvsBranch, UniKvsBuilder as Builder, UniKvsBuilder, InvalidInputError, InvalidOutputError, InvalidUsageErrorBase, KeyNotFoundError, MiddlewareDestinationFrozenError, MiddlewareDestinationMissingError, MiddlewareIsNotOpenError, MiddlewareNextCalledMultipleTimesError, MiddlewareNextNotAwaitedError, MiddlewareNextNotCalledError, MissingStorageError, MultipartWriteNotSupportedError, PlainValue, PluginOperationAggregateError, ReadableStreamNotSupportedError, StorageIsNotOpenError, StreamDecodingNotSupportedError, StreamEncodingNotSupportedError, StreamValue, UniKvs, UniKvsIsNotOpenError, UniKvsIsOpenError, Value, WritableStreamNotSupportedError };
4073
+ export { UniKvsBranch as Branch, UniKvsBranch, UniKvsBuilder as Builder, UniKvsBuilder, InvalidForkError, InvalidInputError, InvalidOutputError, InvalidUsageErrorBase, KeyNotFoundError, MiddlewareDestinationFrozenError, MiddlewareDestinationMissingError, MiddlewareIsNotOpenError, MiddlewareNextCalledMultipleTimesError, MiddlewareNextNotAwaitedError, MiddlewareNextNotCalledError, MissingStorageError, MultipartWriteNotSupportedError, PlainValue, PluginOperationAggregateError, ReadableStreamNotSupportedError, StorageIsNotOpenError, StreamDecodingNotSupportedError, StreamEncodingNotSupportedError, StreamValue, UniKvs, UniKvsIsNotOpenError, UniKvsIsOpenError, UnterminatedPipelineError, Value, WritableStreamNotSupportedError };
4031
4074
 
4032
4075
  //# sourceMappingURL=index.js.map