unikvs 0.0.16 → 0.0.17

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
@@ -728,6 +728,427 @@ _UniKvsMiddleware = UniKvsMiddleware;
728
728
  ]
729
729
  ]).e;
730
730
  //#endregion
731
+ //#region ../src/_contexts.ts
732
+ /**
733
+ * 実行時変数の初期値をエントリー列に正規化します。
734
+ *
735
+ * レコードは `Object.entries` で展開します。
736
+ * 反復子はそのまま返します。
737
+ *
738
+ * @param vars 初期値です。
739
+ *
740
+ * @returns エントリー列を返します。
741
+ */
742
+ function normalizeVariablesInit(vars) {
743
+ if (!vars) return [];
744
+ if (Symbol.iterator in vars) return vars;
745
+ return Object.entries(vars);
746
+ }
747
+ /**
748
+ * 新しい空の変数ストアを作成します。
749
+ *
750
+ * @param vars 初期値です。
751
+ *
752
+ * @returns 空ストアを返します。
753
+ */
754
+ function createVariablesStore(vars = []) {
755
+ return {
756
+ data: new Map(vars),
757
+ readonly: /* @__PURE__ */ new Set()
758
+ };
759
+ }
760
+ var _store = /* @__PURE__ */ new WeakMap();
761
+ /**
762
+ * 実行時変数を保持する基底コンテキストです。
763
+ */
764
+ var PipelineContext = class {
765
+ /**
766
+ * コンテキストを作成します。
767
+ *
768
+ * @param init 初期値です。
769
+ */
770
+ constructor(init) {
771
+ _classPrivateFieldInitSpec(this, _store, void 0);
772
+ const { store } = init;
773
+ _classPrivateFieldSet2(_store, this, store ?? createVariablesStore());
774
+ const vars = normalizeVariablesInit(init.vars);
775
+ for (const [name, value] of vars) _classPrivateFieldGet2(_store, this).data.set(name, value);
776
+ }
777
+ /**
778
+ * 共有する実行時変数のストアを取得します。
779
+ *
780
+ * @returns 実行時変数のストアを返します。
781
+ */
782
+ get store() {
783
+ return _classPrivateFieldGet2(_store, this);
784
+ }
785
+ /**
786
+ * 変数の値を読み取ります。
787
+ *
788
+ * @param name 変数名です。
789
+ *
790
+ * @returns 変数の値を返します。
791
+ */
792
+ get(name) {
793
+ return _classPrivateFieldGet2(_store, this).data.get(name);
794
+ }
795
+ /**
796
+ * 変数の値を書き込みます。
797
+ *
798
+ * @param name 変数名です。
799
+ * @param value 設定する値です。
800
+ *
801
+ * @returns 自身を返します。
802
+ */
803
+ set(name, value) {
804
+ if (_classPrivateFieldGet2(_store, this).readonly.has(name)) throw new ReadonlyVariableError({ name });
805
+ _classPrivateFieldGet2(_store, this).data.set(name, value);
806
+ return this;
807
+ }
808
+ /**
809
+ * 変数の値を設定し、読み取り専用にします。
810
+ *
811
+ * @param name 変数名です。
812
+ * @param value 設定する値です。
813
+ *
814
+ * @returns 自身を返します。
815
+ */
816
+ setReadonly(name, value) {
817
+ if (_classPrivateFieldGet2(_store, this).readonly.has(name)) throw new ReadonlyVariableError({ name });
818
+ _classPrivateFieldGet2(_store, this).data.set(name, value);
819
+ _classPrivateFieldGet2(_store, this).readonly.add(name);
820
+ return this;
821
+ }
822
+ };
823
+ /**
824
+ * コンテキストが共有する実行時変数のストアを取得します。
825
+ *
826
+ * 外部実装のコンテキストは共有ストアを公開しないため、独立したストアを返します。
827
+ *
828
+ * @param context 対象のコンテキストです。
829
+ *
830
+ * @returns 実行時変数のストアを返します。
831
+ */
832
+ function getVariablesStore(context) {
833
+ return context instanceof PipelineContext ? context.store : createVariablesStore();
834
+ }
835
+ var MapKeyContext = class extends PipelineContext {
836
+ /**
837
+ * コンテキストを作成します。
838
+ *
839
+ * @param init 初期値です。
840
+ */
841
+ constructor(init) {
842
+ const { key, vars, store } = init;
843
+ super({
844
+ vars,
845
+ store
846
+ });
847
+ this.key = key;
848
+ }
849
+ };
850
+ var ActionContext = class extends PipelineContext {
851
+ /**
852
+ * コンテキストを作成します。
853
+ *
854
+ * @param init 初期値です。
855
+ */
856
+ constructor(init) {
857
+ const { vars, store, action, signal } = init;
858
+ super({
859
+ vars,
860
+ store
861
+ });
862
+ this.action = action;
863
+ this.signal = signal;
864
+ }
865
+ };
866
+ var LifecycleContext = class extends ActionContext {};
867
+ /**
868
+ * 開始処理のコンテキストです。
869
+ */
870
+ var OpenContext = class extends LifecycleContext {
871
+ /**
872
+ * コンテキストを作成します。
873
+ *
874
+ * @param init 初期値です。
875
+ */
876
+ constructor(init) {
877
+ super({
878
+ ...init,
879
+ action: "open"
880
+ });
881
+ }
882
+ };
883
+ /**
884
+ * 終了処理のコンテキストです。
885
+ */
886
+ var CloseContext = class extends LifecycleContext {
887
+ /**
888
+ * コンテキストを作成します。
889
+ *
890
+ * @param init 初期値です。
891
+ */
892
+ constructor(init) {
893
+ super({
894
+ ...init,
895
+ action: "close"
896
+ });
897
+ }
898
+ };
899
+ var KeyedActionContext = class extends ActionContext {
900
+ /**
901
+ * コンテキストを作成します。
902
+ *
903
+ * @param init 初期値です。
904
+ */
905
+ constructor(init) {
906
+ const { key, ...superInit } = init;
907
+ super(superInit);
908
+ this.key = key;
909
+ }
910
+ };
911
+ /**
912
+ * 書き込み処理のコンテキストです。
913
+ */
914
+ var SetContext = class extends KeyedActionContext {
915
+ /**
916
+ * コンテキストを作成します。
917
+ *
918
+ * @param init 初期値です。
919
+ */
920
+ constructor(init) {
921
+ super({
922
+ ...init,
923
+ action: "set"
924
+ });
925
+ }
926
+ };
927
+ /**
928
+ * 読み取り処理のコンテキストです。
929
+ */
930
+ var GetContext = class extends KeyedActionContext {
931
+ /**
932
+ * コンテキストを作成します。
933
+ *
934
+ * @param init 初期値です。
935
+ */
936
+ constructor(init) {
937
+ const { repair = {
938
+ enabled: false,
939
+ targets: []
940
+ }, ...superInit } = init;
941
+ super({
942
+ ...superInit,
943
+ action: "get"
944
+ });
945
+ this.repair = repair;
946
+ }
947
+ };
948
+ /**
949
+ * ストリーム処理のコンテキストです。
950
+ */
951
+ var StreamContext = class extends KeyedActionContext {
952
+ /**
953
+ * コンテキストを作成します。
954
+ *
955
+ * @param init 初期値です。
956
+ */
957
+ constructor(init) {
958
+ const { repair = {
959
+ enabled: false,
960
+ targets: []
961
+ }, ...superInit } = init;
962
+ super({
963
+ ...superInit,
964
+ action: "stream"
965
+ });
966
+ this.repair = repair;
967
+ }
968
+ };
969
+ /**
970
+ * 存在確認のコンテキストです。
971
+ */
972
+ var HasContext = class extends KeyedActionContext {
973
+ /**
974
+ * コンテキストを作成します。
975
+ *
976
+ * @param init 初期値です。
977
+ */
978
+ constructor(init) {
979
+ super({
980
+ ...init,
981
+ action: "has"
982
+ });
983
+ }
984
+ };
985
+ /**
986
+ * 削除処理のコンテキストです。
987
+ */
988
+ var DeleteContext = class extends KeyedActionContext {
989
+ /**
990
+ * コンテキストを作成します。
991
+ *
992
+ * @param init 初期値です。
993
+ */
994
+ constructor(init) {
995
+ super({
996
+ ...init,
997
+ action: "delete"
998
+ });
999
+ }
1000
+ };
1001
+ /**
1002
+ * コピー・移動で共通のコンテキストです。
1003
+ */
1004
+ var TransferContext = class extends KeyedActionContext {
1005
+ /**
1006
+ * コンテキストを作成します。
1007
+ *
1008
+ * @param init 初期値です。
1009
+ */
1010
+ constructor(init) {
1011
+ const { destinationKey, ...superInit } = init;
1012
+ super(superInit);
1013
+ this.destinationKey = destinationKey;
1014
+ }
1015
+ };
1016
+ /**
1017
+ * コピー処理のコンテキストです。
1018
+ */
1019
+ var CopyContext = class extends TransferContext {
1020
+ /**
1021
+ * コンテキストを作成します。
1022
+ *
1023
+ * @param init 初期値です。
1024
+ */
1025
+ constructor(init) {
1026
+ super({
1027
+ ...init,
1028
+ action: "copy"
1029
+ });
1030
+ }
1031
+ };
1032
+ /**
1033
+ * 移動処理のコンテキストです。
1034
+ */
1035
+ var MoveContext = class extends TransferContext {
1036
+ /**
1037
+ * コンテキストを作成します。
1038
+ *
1039
+ * @param init 初期値です。
1040
+ */
1041
+ constructor(init) {
1042
+ super({
1043
+ ...init,
1044
+ action: "move"
1045
+ });
1046
+ }
1047
+ };
1048
+ /**
1049
+ * 全削除のコンテキストです。
1050
+ */
1051
+ var ClearContext = class extends ActionContext {
1052
+ /**
1053
+ * コンテキストを作成します。
1054
+ *
1055
+ * @param init 初期値です。
1056
+ */
1057
+ constructor(init) {
1058
+ super({
1059
+ ...init,
1060
+ action: "clear"
1061
+ });
1062
+ }
1063
+ };
1064
+ /**
1065
+ * 値変換の基底コンテキストです。
1066
+ */
1067
+ var TransformContext = class extends KeyedActionContext {
1068
+ /**
1069
+ * コンテキストを作成します。
1070
+ *
1071
+ * @param init 初期値です。
1072
+ */
1073
+ constructor(init) {
1074
+ const { source, ...superInit } = init;
1075
+ super(superInit);
1076
+ this.source = source;
1077
+ }
1078
+ };
1079
+ /**
1080
+ * データ変換の基底コンテキストです。
1081
+ */
1082
+ var TransformDataContext = class extends TransformContext {};
1083
+ /**
1084
+ * 符号化のコンテキストです。
1085
+ */
1086
+ var EncodeContext = class extends TransformDataContext {
1087
+ /**
1088
+ * コンテキストを作成します。
1089
+ *
1090
+ * @param init 初期値です。
1091
+ */
1092
+ constructor(init) {
1093
+ super({
1094
+ ...init,
1095
+ action: "set"
1096
+ });
1097
+ }
1098
+ };
1099
+ /**
1100
+ * 復号のコンテキストです。
1101
+ */
1102
+ var DecodeContext = class extends TransformDataContext {
1103
+ /**
1104
+ * コンテキストを作成します。
1105
+ *
1106
+ * @param init 初期値です。
1107
+ */
1108
+ constructor(init) {
1109
+ super({
1110
+ ...init,
1111
+ action: "get"
1112
+ });
1113
+ }
1114
+ };
1115
+ /**
1116
+ * ストリーム変換の基底コンテキストです。
1117
+ */
1118
+ var TransformStreamContext = class extends TransformContext {};
1119
+ /**
1120
+ * ストリーム符号化のコンテキストです。
1121
+ */
1122
+ var EncodeStreamContext = class extends TransformStreamContext {
1123
+ /**
1124
+ * コンテキストを作成します。
1125
+ *
1126
+ * @param init 初期値です。
1127
+ */
1128
+ constructor(init) {
1129
+ super({
1130
+ ...init,
1131
+ action: "set"
1132
+ });
1133
+ }
1134
+ };
1135
+ /**
1136
+ * ストリーム復号のコンテキストです。
1137
+ */
1138
+ var DecodeStreamContext = class extends TransformStreamContext {
1139
+ /**
1140
+ * コンテキストを作成します。
1141
+ *
1142
+ * @param init 初期値です。
1143
+ */
1144
+ constructor(init) {
1145
+ super({
1146
+ ...init,
1147
+ action: "stream"
1148
+ });
1149
+ }
1150
+ };
1151
+ //#endregion
731
1152
  //#region ../src/_streams.ts
732
1153
  /**
733
1154
  * 値が `ReadableStream` のインターフェースを満たすかどうかを判定します。
@@ -937,677 +1358,287 @@ function _checkInRHS(e) {
937
1358
  return e;
938
1359
  }
939
1360
  /**
940
- * 同じオブジェクトに対して同じ `UniKvsStorage` インスタンスを返すためのメモです。
941
- */
942
- const instances = /* #__PURE__ */ new WeakMap();
943
- var _io = /* @__PURE__ */ new WeakMap();
944
- var _lifecycle = /* @__PURE__ */ new WeakMap();
945
- /**
946
- * `IStorage` を `UniKvs` 内部から扱うためのラッパーです。
947
- *
948
- * ストレージを安全に使うための検証などの処理が集約されています。
949
- */
950
- var UniKvsStorage = class UniKvsStorage {
951
- constructor(storage) {
952
- this.name = void _initProto(this);
953
- _classPrivateFieldInitSpec(this, _io, void 0);
954
- _classPrivateFieldInitSpec(this, _lifecycle, void 0);
955
- _classPrivateFieldSet2(_io, this, storage);
956
- _classPrivateFieldSet2(_lifecycle, this, new PluginLifecycle(storage));
957
- this.name = storage.name;
958
- }
959
- /**
960
- * `IStorage` から `UniKvs` 内部から扱うためのストレージを作成します。
961
- *
962
- * @param storage 元となるストレージです。
963
- *
964
- * @returns 検証などの機能が集約されたストレージです。
965
- */
966
- static from(storage) {
967
- return callFnOnce(instances, storage, () => new UniKvsStorage(storage));
968
- }
969
- /**
970
- * ストレージを開きます。
971
- *
972
- * @param c オープンコンテキストです。
973
- */
974
- async open(c) {
975
- await _classPrivateFieldGet2(_lifecycle, this).open(c);
976
- }
977
- /**
978
- * ストレージを閉じます。
979
- *
980
- * @param c クローズコンテキストです。
981
- */
982
- async close(c) {
983
- await _classPrivateFieldGet2(_lifecycle, this).close(c);
984
- }
985
- /**
986
- * 失敗した実 `close` だけを再試行します。
987
- *
988
- * @param c クローズコンテキストです。
989
- */
990
- async retryClose(c) {
991
- await _classPrivateFieldGet2(_lifecycle, this).retryClose(c);
992
- }
993
- /**
994
- * データを書き込みます。
995
- *
996
- * @param c 書き込みコンテキストです。
997
- * @param data 書き込むデータです。
998
- */
999
- async write(c, data) {
1000
- if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1001
- await _classPrivateFieldGet2(_io, this).write(c, data);
1002
- }
1003
- /**
1004
- * データを読み取ります。
1005
- *
1006
- * @param c 読み取りコンテキストです。
1007
- *
1008
- * @returns 読み取ったデータを返します。
1009
- */
1010
- async read(c) {
1011
- if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1012
- return await _classPrivateFieldGet2(_io, this).read(c);
1013
- }
1014
- /**
1015
- * データの存在を確認します。
1016
- *
1017
- * @param c 存在確認コンテキストです。
1018
- *
1019
- * @returns 存在する場合は `true` を返します。
1020
- */
1021
- async exists(c) {
1022
- if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1023
- return await _classPrivateFieldGet2(_io, this).exists(c);
1024
- }
1025
- /**
1026
- * データを削除します。
1027
- *
1028
- * @param c 削除コンテキストです。
1029
- */
1030
- async delete(c) {
1031
- if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1032
- await _classPrivateFieldGet2(_io, this).delete(c);
1033
- }
1034
- /**
1035
- * データをすべて削除します。
1036
- *
1037
- * @param c クリアコンテキストです。
1038
- */
1039
- async clear(c) {
1040
- if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1041
- await _classPrivateFieldGet2(_io, this).clear(c);
1042
- }
1043
- /**
1044
- * データを別のキーへ複製します。
1045
- *
1046
- * @param c コピー元とコピー先のキーを持つコンテキストです。
1047
- */
1048
- async copy(c) {
1049
- if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1050
- await _classPrivateFieldGet2(_io, this).copy(c);
1051
- }
1052
- /**
1053
- * データを別のキーへ移動します。
1054
- *
1055
- * @param c 移動元と移動先のキーを持つコンテキストです。
1056
- */
1057
- async move(c) {
1058
- if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1059
- await _classPrivateFieldGet2(_io, this).move(c);
1060
- }
1061
- /**
1062
- * 読み取り用のストリームを取得します。
1063
- *
1064
- * @param c ストリーム取得コンテキストです。
1065
- *
1066
- * @returns 読み取り用のストリームを返します。
1067
- */
1068
- async getReadable(c) {
1069
- if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1070
- if (_classPrivateFieldGet2(_io, this).getReadable === void 0) throw new ReadableStreamNotSupportedError({ name: this.name });
1071
- const output = await _classPrivateFieldGet2(_io, this).getReadable(c);
1072
- return parseOutput(ReadableStreamSchema, output);
1073
- }
1074
- /**
1075
- * 書き込み用のストリームを取得します。
1076
- *
1077
- * @param c ストリーム取得コンテキストです。
1078
- *
1079
- * @returns 書き込み用のストリームを返します。
1080
- */
1081
- async getWritable(c) {
1082
- if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1083
- if (_classPrivateFieldGet2(_io, this).getWritable === void 0) throw new WritableStreamNotSupportedError({ name: this.name });
1084
- const output = await _classPrivateFieldGet2(_io, this).getWritable(c);
1085
- return parseOutput(WritableStreamSchema, output);
1086
- }
1087
- /**
1088
- * 他のストレージへの書き込み失敗を通知します。
1089
- *
1090
- * @param c 書き込み失敗通知コンテキストです。
1091
- * @param err 集約エラーです。
1092
- */
1093
- async onOtherWriteError(c, err) {
1094
- if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1095
- if (_classPrivateFieldGet2(_io, this).onOtherWriteError === void 0) return;
1096
- await _classPrivateFieldGet2(_io, this).onOtherWriteError(c, [{
1097
- errors: err.meta.errors.map((error) => ({
1098
- plugin: "storage",
1099
- reason: error.reason
1100
- })),
1101
- plugin: "storage"
1102
- }]);
1103
- }
1104
- };
1105
- _UniKvsStorage = UniKvsStorage;
1106
- [_initProto] = _applyDecs(_UniKvsStorage, [], [
1107
- [
1108
- asyncmux,
1109
- 2,
1110
- "open"
1111
- ],
1112
- [
1113
- asyncmux,
1114
- 2,
1115
- "close"
1116
- ],
1117
- [
1118
- asyncmux,
1119
- 2,
1120
- "retryClose"
1121
- ]
1122
- ]).e;
1123
- //#endregion
1124
- //#region ../src/_unikvs-branch.ts
1125
- /**
1126
- * インスタンスと内部状態の対応表です。
1127
- * コンパイラーによる `#` フィールドのダウンレベルと同様に、`WeakMap` をブランド兼ストレージとして使います。
1128
- */
1129
- const branchStates = /* #__PURE__ */ new WeakMap();
1130
- /**
1131
- * 分岐先を親の接頭辞付きで追加します。
1132
- *
1133
- * `new Branch()` で作成された分岐以外の値が渡されたときは例外を投げます。
1134
- * 外部からは使用しません。
1135
- *
1136
- * @param destinations 追加先の分岐先列です。
1137
- * @param prefix 親のミドルウェア列です。
1138
- * @param branch 追加する分岐です。
1139
- *
1140
- * @throws 分岐がストレージに到達しない場合に `InvalidForkError` を投げます。
1141
- */
1142
- function appendForkDestinations(destinations, prefix, branch) {
1143
- const state = branchStates.get(branch);
1144
- if (!state) throw new TypeError("fork() expects a branch created by new Branch().");
1145
- if (state.destinations.length === 0) throw new InvalidForkError();
1146
- const prefixSnapshot = [...prefix];
1147
- for (const dest of state.destinations) destinations.push({
1148
- storage: dest.storage,
1149
- middlewares: [...prefixSnapshot, ...dest.middlewares]
1150
- });
1151
- }
1152
- //#endregion
1153
- //#region ../src/unikvs-branch.ts
1154
- var _UniKvsBranch_brand = /* @__PURE__ */ new WeakSet();
1155
- /**
1156
- * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#builder)
1157
- */
1158
- var UniKvsBranch = class {
1159
- constructor() {
1160
- _classPrivateMethodInitSpec(this, _UniKvsBranch_brand);
1161
- branchStates.set(this, {
1162
- middlewares: [],
1163
- destinations: []
1164
- });
1165
- }
1166
- fork(branch) {
1167
- const state = _assertClassBrand(_UniKvsBranch_brand, this, _branchState).call(this);
1168
- appendForkDestinations(state.destinations, state.middlewares, branch);
1169
- return this;
1170
- }
1171
- use(middleware) {
1172
- _assertClassBrand(_UniKvsBranch_brand, this, _branchState).call(this).middlewares.push(UniKvsMiddleware.from(middleware));
1173
- return this;
1174
- }
1175
- persist(storage) {
1176
- const state = _assertClassBrand(_UniKvsBranch_brand, this, _branchState).call(this);
1177
- state.destinations.push({
1178
- storage: UniKvsStorage.from(storage),
1179
- middlewares: [...state.middlewares]
1180
- });
1181
- return this;
1182
- }
1183
- };
1184
- /**
1185
- * この分岐の構築状態を取得します。
1186
- *
1187
- * @returns 構築状態を返します。
1188
- */
1189
- function _branchState() {
1190
- const state = branchStates.get(this);
1191
- if (state === void 0) throw new Error("分岐の構築状態が見つかりません。");
1192
- return state;
1193
- }
1194
- //#endregion
1195
- //#region ../src/_contexts.ts
1196
- /**
1197
- * 実行時変数の初期値をエントリー列に正規化します。
1198
- *
1199
- * レコードは `Object.entries` で展開します。
1200
- * 反復子はそのまま返します。
1201
- *
1202
- * @param vars 初期値です。
1203
- *
1204
- * @returns エントリー列を返します。
1361
+ * 同じオブジェクトに対して同じ `UniKvsStorage` インスタンスを返すためのメモです。
1205
1362
  */
1206
- function normalizeVariablesInit(vars) {
1207
- if (!vars) return [];
1208
- if (Symbol.iterator in vars) return vars;
1209
- return Object.entries(vars);
1210
- }
1363
+ const instances = /* #__PURE__ */ new WeakMap();
1364
+ var _io = /* @__PURE__ */ new WeakMap();
1365
+ var _lifecycle = /* @__PURE__ */ new WeakMap();
1211
1366
  /**
1212
- * 新しい空の変数ストアを作成します。
1213
- *
1214
- * @param vars 初期値です。
1367
+ * `IStorage` を `UniKvs` 内部から扱うためのラッパーです。
1215
1368
  *
1216
- * @returns 空ストアを返します。
1217
- */
1218
- function createVariablesStore(vars = []) {
1219
- return {
1220
- data: new Map(vars),
1221
- readonly: /* @__PURE__ */ new Set()
1222
- };
1223
- }
1224
- var _data = /* @__PURE__ */ new WeakMap();
1225
- var _readonly = /* @__PURE__ */ new WeakMap();
1226
- /**
1227
- * 実行時変数を保持する基底コンテキストです。
1369
+ * ストレージを安全に使うための検証などの処理が集約されています。
1228
1370
  */
1229
- var PipelineContext = class {
1230
- /**
1231
- * コンテキストを作成します。
1232
- *
1233
- * @param init 初期値です。
1234
- */
1235
- constructor(init) {
1236
- _classPrivateFieldInitSpec(this, _data, void 0);
1237
- _classPrivateFieldInitSpec(this, _readonly, void 0);
1238
- const { store } = init;
1239
- if (store) {
1240
- _classPrivateFieldSet2(_data, this, store.data);
1241
- _classPrivateFieldSet2(_readonly, this, store.readonly);
1242
- } else {
1243
- _classPrivateFieldSet2(_data, this, /* @__PURE__ */ new Map());
1244
- _classPrivateFieldSet2(_readonly, this, /* @__PURE__ */ new Set());
1245
- }
1246
- const vars = normalizeVariablesInit(init.vars);
1247
- for (const [name, value] of vars) _classPrivateFieldGet2(_data, this).set(name, value);
1248
- }
1249
- /**
1250
- * 変数の値を読み取ります。
1251
- *
1252
- * @param name 変数名です。
1253
- *
1254
- * @returns 変数の値を返します。
1255
- */
1256
- get(name) {
1257
- return _classPrivateFieldGet2(_data, this).get(name);
1258
- }
1259
- /**
1260
- * 変数の値を書き込みます。
1261
- *
1262
- * @param name 変数名です。
1263
- * @param value 設定する値です。
1264
- *
1265
- * @returns 自身を返します。
1266
- */
1267
- set(name, value) {
1268
- if (_classPrivateFieldGet2(_readonly, this).has(name)) throw new ReadonlyVariableError({ name });
1269
- _classPrivateFieldGet2(_data, this).set(name, value);
1270
- return this;
1271
- }
1272
- /**
1273
- * 変数の値を設定し、読み取り専用にします。
1274
- *
1275
- * @param name 変数名です。
1276
- * @param value 設定する値です。
1277
- *
1278
- * @returns 自身を返します。
1279
- */
1280
- setReadonly(name, value) {
1281
- if (_classPrivateFieldGet2(_readonly, this).has(name)) throw new ReadonlyVariableError({ name });
1282
- _classPrivateFieldGet2(_data, this).set(name, value);
1283
- _classPrivateFieldGet2(_readonly, this).add(name);
1284
- return this;
1285
- }
1286
- /**
1287
- * 現在の実行時変数の浅いコピーを作成します。
1288
- *
1289
- * @returns 実行時変数のスナップショットを返します。
1290
- */
1291
- snapshot() {
1292
- return new Map(_classPrivateFieldGet2(_data, this));
1293
- }
1294
- };
1295
- var MapKeyContext = class extends PipelineContext {
1296
- /**
1297
- * コンテキストを作成します。
1298
- *
1299
- * @param init 初期値です。
1300
- */
1301
- constructor(init) {
1302
- const { key, vars, store } = init;
1303
- super({
1304
- vars,
1305
- store
1306
- });
1307
- this.key = key;
1371
+ var UniKvsStorage = class UniKvsStorage {
1372
+ constructor(storage) {
1373
+ this.name = void _initProto(this);
1374
+ _classPrivateFieldInitSpec(this, _io, void 0);
1375
+ _classPrivateFieldInitSpec(this, _lifecycle, void 0);
1376
+ _classPrivateFieldSet2(_io, this, storage);
1377
+ _classPrivateFieldSet2(_lifecycle, this, new PluginLifecycle(storage));
1378
+ this.name = storage.name;
1308
1379
  }
1309
- };
1310
- var ActionContext = class extends PipelineContext {
1311
1380
  /**
1312
- * コンテキストを作成します。
1381
+ * `IStorage` から `UniKvs` 内部から扱うためのストレージを作成します。
1313
1382
  *
1314
- * @param init 初期値です。
1315
- */
1316
- constructor(init) {
1317
- const { vars, store, action, signal } = init;
1318
- super({
1319
- vars,
1320
- store
1321
- });
1322
- this.action = action;
1323
- this.signal = signal;
1324
- }
1325
- };
1326
- var LifecycleContext = class extends ActionContext {};
1327
- /**
1328
- * 開始処理のコンテキストです。
1329
- */
1330
- var OpenContext = class extends LifecycleContext {
1331
- /**
1332
- * コンテキストを作成します。
1383
+ * @param storage 元となるストレージです。
1333
1384
  *
1334
- * @param init 初期値です。
1385
+ * @returns 検証などの機能が集約されたストレージです。
1335
1386
  */
1336
- constructor(init) {
1337
- super({
1338
- ...init,
1339
- action: "open"
1340
- });
1387
+ static from(storage) {
1388
+ return callFnOnce(instances, storage, () => new UniKvsStorage(storage));
1341
1389
  }
1342
- };
1343
- /**
1344
- * 終了処理のコンテキストです。
1345
- */
1346
- var CloseContext = class extends LifecycleContext {
1347
1390
  /**
1348
- * コンテキストを作成します。
1391
+ * ストレージを開きます。
1349
1392
  *
1350
- * @param init 初期値です。
1393
+ * @param c オープンコンテキストです。
1351
1394
  */
1352
- constructor(init) {
1353
- super({
1354
- ...init,
1355
- action: "close"
1356
- });
1395
+ async open(c) {
1396
+ await _classPrivateFieldGet2(_lifecycle, this).open(c);
1357
1397
  }
1358
- };
1359
- var KeyedActionContext = class extends ActionContext {
1360
1398
  /**
1361
- * コンテキストを作成します。
1399
+ * ストレージを閉じます。
1362
1400
  *
1363
- * @param init 初期値です。
1401
+ * @param c クローズコンテキストです。
1364
1402
  */
1365
- constructor(init) {
1366
- const { key, ...superInit } = init;
1367
- super(superInit);
1368
- this.key = key;
1403
+ async close(c) {
1404
+ await _classPrivateFieldGet2(_lifecycle, this).close(c);
1369
1405
  }
1370
- };
1371
- /**
1372
- * 書き込み処理のコンテキストです。
1373
- */
1374
- var SetContext = class extends KeyedActionContext {
1375
1406
  /**
1376
- * コンテキストを作成します。
1407
+ * 失敗した実 `close` だけを再試行します。
1377
1408
  *
1378
- * @param init 初期値です。
1409
+ * @param c クローズコンテキストです。
1379
1410
  */
1380
- constructor(init) {
1381
- super({
1382
- ...init,
1383
- action: "set"
1384
- });
1411
+ async retryClose(c) {
1412
+ await _classPrivateFieldGet2(_lifecycle, this).retryClose(c);
1385
1413
  }
1386
- };
1387
- /**
1388
- * 読み取り処理のコンテキストです。
1389
- */
1390
- var GetContext = class extends KeyedActionContext {
1391
1414
  /**
1392
- * コンテキストを作成します。
1415
+ * データを書き込みます。
1393
1416
  *
1394
- * @param init 初期値です。
1417
+ * @param c 書き込みコンテキストです。
1418
+ * @param data 書き込むデータです。
1395
1419
  */
1396
- constructor(init) {
1397
- const { repair = {
1398
- enabled: false,
1399
- targets: []
1400
- }, ...superInit } = init;
1401
- super({
1402
- ...superInit,
1403
- action: "get"
1404
- });
1405
- this.repair = repair;
1420
+ async write(c, data) {
1421
+ if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1422
+ await _classPrivateFieldGet2(_io, this).write(c, data);
1406
1423
  }
1407
- };
1408
- /**
1409
- * ストリーム処理のコンテキストです。
1410
- */
1411
- var StreamContext = class extends KeyedActionContext {
1412
1424
  /**
1413
- * コンテキストを作成します。
1425
+ * データを読み取ります。
1414
1426
  *
1415
- * @param init 初期値です。
1416
- */
1417
- constructor(init) {
1418
- const { repair = {
1419
- enabled: false,
1420
- targets: []
1421
- }, ...superInit } = init;
1422
- super({
1423
- ...superInit,
1424
- action: "stream"
1425
- });
1426
- this.repair = repair;
1427
- }
1428
- };
1429
- /**
1430
- * 存在確認のコンテキストです。
1431
- */
1432
- var HasContext = class extends KeyedActionContext {
1433
- /**
1434
- * コンテキストを作成します。
1427
+ * @param c 読み取りコンテキストです。
1435
1428
  *
1436
- * @param init 初期値です。
1429
+ * @returns 読み取ったデータを返します。
1437
1430
  */
1438
- constructor(init) {
1439
- super({
1440
- ...init,
1441
- action: "has"
1442
- });
1431
+ async read(c) {
1432
+ if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1433
+ return await _classPrivateFieldGet2(_io, this).read(c);
1443
1434
  }
1444
- };
1445
- /**
1446
- * 削除処理のコンテキストです。
1447
- */
1448
- var DeleteContext = class extends KeyedActionContext {
1449
1435
  /**
1450
- * コンテキストを作成します。
1436
+ * データの存在を確認します。
1451
1437
  *
1452
- * @param init 初期値です。
1453
- */
1454
- constructor(init) {
1455
- super({
1456
- ...init,
1457
- action: "delete"
1458
- });
1438
+ * @param c 存在確認コンテキストです。
1439
+ *
1440
+ * @returns 存在する場合は `true` を返します。
1441
+ */
1442
+ async exists(c) {
1443
+ if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1444
+ return await _classPrivateFieldGet2(_io, this).exists(c);
1459
1445
  }
1460
- };
1461
- /**
1462
- * コピー・移動で共通のコンテキストです。
1463
- */
1464
- var TransferContext = class extends KeyedActionContext {
1465
1446
  /**
1466
- * コンテキストを作成します。
1447
+ * データを削除します。
1467
1448
  *
1468
- * @param init 初期値です。
1449
+ * @param c 削除コンテキストです。
1469
1450
  */
1470
- constructor(init) {
1471
- const { destinationKey, ...superInit } = init;
1472
- super(superInit);
1473
- this.destinationKey = destinationKey;
1451
+ async delete(c) {
1452
+ if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1453
+ await _classPrivateFieldGet2(_io, this).delete(c);
1474
1454
  }
1475
- };
1476
- /**
1477
- * コピー処理のコンテキストです。
1478
- */
1479
- var CopyContext = class extends TransferContext {
1480
1455
  /**
1481
- * コンテキストを作成します。
1456
+ * データをすべて削除します。
1482
1457
  *
1483
- * @param init 初期値です。
1458
+ * @param c クリアコンテキストです。
1484
1459
  */
1485
- constructor(init) {
1486
- super({
1487
- ...init,
1488
- action: "copy"
1489
- });
1460
+ async clear(c) {
1461
+ if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1462
+ await _classPrivateFieldGet2(_io, this).clear(c);
1490
1463
  }
1491
- };
1492
- /**
1493
- * 移動処理のコンテキストです。
1494
- */
1495
- var MoveContext = class extends TransferContext {
1496
1464
  /**
1497
- * コンテキストを作成します。
1465
+ * データを別のキーへ複製します。
1498
1466
  *
1499
- * @param init 初期値です。
1467
+ * @param c コピー元とコピー先のキーを持つコンテキストです。
1500
1468
  */
1501
- constructor(init) {
1502
- super({
1503
- ...init,
1504
- action: "move"
1505
- });
1469
+ async copy(c) {
1470
+ if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1471
+ if (_classPrivateFieldGet2(_io, this).copy === void 0) {
1472
+ const data = await _classPrivateFieldGet2(_io, this).read(c);
1473
+ if (c.key !== c.destinationKey) await _classPrivateFieldGet2(_io, this).write(new CopyContext({
1474
+ key: c.destinationKey,
1475
+ store: getVariablesStore(c),
1476
+ signal: c.signal,
1477
+ destinationKey: c.destinationKey
1478
+ }), data);
1479
+ return;
1480
+ }
1481
+ await _classPrivateFieldGet2(_io, this).copy(c);
1506
1482
  }
1507
- };
1508
- /**
1509
- * 全削除のコンテキストです。
1510
- */
1511
- var ClearContext = class extends ActionContext {
1512
1483
  /**
1513
- * コンテキストを作成します。
1484
+ * データを別のキーへ移動します。
1514
1485
  *
1515
- * @param init 初期値です。
1486
+ * @param c 移動元と移動先のキーを持つコンテキストです。
1516
1487
  */
1517
- constructor(init) {
1518
- super({
1519
- ...init,
1520
- action: "clear"
1521
- });
1488
+ async move(c) {
1489
+ if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1490
+ if (_classPrivateFieldGet2(_io, this).move === void 0) {
1491
+ if (c.key === c.destinationKey) {
1492
+ await _classPrivateFieldGet2(_io, this).read(c);
1493
+ return;
1494
+ }
1495
+ if (_classPrivateFieldGet2(_io, this).copy === void 0) {
1496
+ const data = await _classPrivateFieldGet2(_io, this).read(c);
1497
+ await _classPrivateFieldGet2(_io, this).write(new MoveContext({
1498
+ key: c.destinationKey,
1499
+ store: getVariablesStore(c),
1500
+ signal: c.signal,
1501
+ destinationKey: c.destinationKey
1502
+ }), data);
1503
+ } else await _classPrivateFieldGet2(_io, this).copy(c);
1504
+ await _classPrivateFieldGet2(_io, this).delete(c);
1505
+ return;
1506
+ }
1507
+ await _classPrivateFieldGet2(_io, this).move(c);
1522
1508
  }
1523
- };
1524
- /**
1525
- * 値変換の基底コンテキストです。
1526
- */
1527
- var TransformContext = class extends KeyedActionContext {
1528
1509
  /**
1529
- * コンテキストを作成します。
1510
+ * 読み取り用のストリームを取得します。
1530
1511
  *
1531
- * @param init 初期値です。
1512
+ * @param c ストリーム取得コンテキストです。
1513
+ *
1514
+ * @returns 読み取り用のストリームを返します。
1532
1515
  */
1533
- constructor(init) {
1534
- const { source, ...superInit } = init;
1535
- super(superInit);
1536
- this.source = source;
1516
+ async getReadable(c) {
1517
+ if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1518
+ if (_classPrivateFieldGet2(_io, this).getReadable === void 0) throw new ReadableStreamNotSupportedError({ name: this.name });
1519
+ const output = await _classPrivateFieldGet2(_io, this).getReadable(c);
1520
+ return parseOutput(ReadableStreamSchema, output);
1537
1521
  }
1538
- };
1539
- /**
1540
- * データ変換の基底コンテキストです。
1541
- */
1542
- var TransformDataContext = class extends TransformContext {};
1543
- /**
1544
- * 符号化のコンテキストです。
1545
- */
1546
- var EncodeContext = class extends TransformDataContext {
1547
1522
  /**
1548
- * コンテキストを作成します。
1523
+ * 書き込み用のストリームを取得します。
1549
1524
  *
1550
- * @param init 初期値です。
1525
+ * @param c ストリーム取得コンテキストです。
1526
+ *
1527
+ * @returns 書き込み用のストリームを返します。
1551
1528
  */
1552
- constructor(init) {
1553
- super({
1554
- ...init,
1555
- action: "set"
1556
- });
1529
+ async getWritable(c) {
1530
+ if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1531
+ if (_classPrivateFieldGet2(_io, this).getWritable === void 0) throw new WritableStreamNotSupportedError({ name: this.name });
1532
+ const output = await _classPrivateFieldGet2(_io, this).getWritable(c);
1533
+ return parseOutput(WritableStreamSchema, output);
1557
1534
  }
1558
- };
1559
- /**
1560
- * 復号のコンテキストです。
1561
- */
1562
- var DecodeContext = class extends TransformDataContext {
1563
1535
  /**
1564
- * コンテキストを作成します。
1536
+ * 他のストレージへの書き込み失敗を通知します。
1565
1537
  *
1566
- * @param init 初期値です。
1538
+ * @param c 書き込み失敗通知コンテキストです。
1539
+ * @param err 集約エラーです。
1567
1540
  */
1568
- constructor(init) {
1569
- super({
1570
- ...init,
1571
- action: "get"
1572
- });
1541
+ async onOtherWriteError(c, err) {
1542
+ if (!_classPrivateFieldGet2(_io, this).isOpen) throw new StorageIsNotOpenError({ name: this.name });
1543
+ if (_classPrivateFieldGet2(_io, this).onOtherWriteError === void 0) return;
1544
+ await _classPrivateFieldGet2(_io, this).onOtherWriteError(c, [{
1545
+ errors: err.meta.errors.map((error) => ({
1546
+ plugin: "storage",
1547
+ reason: error.reason
1548
+ })),
1549
+ plugin: "storage"
1550
+ }]);
1573
1551
  }
1574
1552
  };
1553
+ _UniKvsStorage = UniKvsStorage;
1554
+ [_initProto] = _applyDecs(_UniKvsStorage, [], [
1555
+ [
1556
+ asyncmux,
1557
+ 2,
1558
+ "open"
1559
+ ],
1560
+ [
1561
+ asyncmux,
1562
+ 2,
1563
+ "close"
1564
+ ],
1565
+ [
1566
+ asyncmux,
1567
+ 2,
1568
+ "retryClose"
1569
+ ]
1570
+ ]).e;
1571
+ //#endregion
1572
+ //#region ../src/_unikvs-branch.ts
1575
1573
  /**
1576
- * ストリーム変換の基底コンテキストです。
1574
+ * インスタンスと内部状態の対応表です。
1575
+ * コンパイラーによる `#` フィールドのダウンレベルと同様に、`WeakMap` をブランド兼ストレージとして使います。
1577
1576
  */
1578
- var TransformStreamContext = class extends TransformContext {};
1577
+ const branchStates = /* #__PURE__ */ new WeakMap();
1579
1578
  /**
1580
- * ストリーム符号化のコンテキストです。
1579
+ * 分岐先を親の接頭辞付きで追加します。
1580
+ *
1581
+ * `new Branch()` で作成された分岐以外の値が渡されたときは例外を投げます。
1582
+ * 外部からは使用しません。
1583
+ *
1584
+ * @param destinations 追加先の分岐先列です。
1585
+ * @param prefix 親のミドルウェア列です。
1586
+ * @param branch 追加する分岐です。
1587
+ *
1588
+ * @throws 分岐がストレージに到達しない場合に `InvalidForkError` を投げます。
1581
1589
  */
1582
- var EncodeStreamContext = class extends TransformStreamContext {
1583
- /**
1584
- * コンテキストを作成します。
1585
- *
1586
- * @param init 初期値です。
1587
- */
1588
- constructor(init) {
1589
- super({
1590
- ...init,
1591
- action: "set"
1592
- });
1593
- }
1594
- };
1590
+ function appendForkDestinations(destinations, prefix, branch) {
1591
+ const state = branchStates.get(branch);
1592
+ if (!state) throw new TypeError("fork() expects a branch created by new Branch().");
1593
+ if (state.destinations.length === 0) throw new InvalidForkError();
1594
+ const prefixSnapshot = [...prefix];
1595
+ for (const dest of state.destinations) destinations.push({
1596
+ storage: dest.storage,
1597
+ middlewares: [...prefixSnapshot, ...dest.middlewares]
1598
+ });
1599
+ }
1600
+ //#endregion
1601
+ //#region ../src/unikvs-branch.ts
1602
+ var _UniKvsBranch_brand = /* @__PURE__ */ new WeakSet();
1595
1603
  /**
1596
- * ストリーム復号のコンテキストです。
1604
+ * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#builder)
1597
1605
  */
1598
- var DecodeStreamContext = class extends TransformStreamContext {
1599
- /**
1600
- * コンテキストを作成します。
1601
- *
1602
- * @param init 初期値です。
1603
- */
1604
- constructor(init) {
1605
- super({
1606
- ...init,
1607
- action: "stream"
1606
+ var UniKvsBranch = class {
1607
+ constructor() {
1608
+ _classPrivateMethodInitSpec(this, _UniKvsBranch_brand);
1609
+ branchStates.set(this, {
1610
+ middlewares: [],
1611
+ destinations: []
1612
+ });
1613
+ }
1614
+ fork(branch) {
1615
+ const state = _assertClassBrand(_UniKvsBranch_brand, this, _branchState).call(this);
1616
+ appendForkDestinations(state.destinations, state.middlewares, branch);
1617
+ return this;
1618
+ }
1619
+ use(middleware) {
1620
+ _assertClassBrand(_UniKvsBranch_brand, this, _branchState).call(this).middlewares.push(UniKvsMiddleware.from(middleware));
1621
+ return this;
1622
+ }
1623
+ persist(storage) {
1624
+ const state = _assertClassBrand(_UniKvsBranch_brand, this, _branchState).call(this);
1625
+ state.destinations.push({
1626
+ storage: UniKvsStorage.from(storage),
1627
+ middlewares: [...state.middlewares]
1608
1628
  });
1629
+ return this;
1609
1630
  }
1610
1631
  };
1632
+ /**
1633
+ * この分岐の構築状態を取得します。
1634
+ *
1635
+ * @returns 構築状態を返します。
1636
+ */
1637
+ function _branchState() {
1638
+ const state = branchStates.get(this);
1639
+ if (state === void 0) throw new Error("分岐の構築状態が見つかりません。");
1640
+ return state;
1641
+ }
1611
1642
  //#endregion
1612
1643
  //#region ../src/_logger.ts
1613
1644
  const logger = getLogger("unikvs");