unikvs 0.0.10 → 0.0.13

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 (77) hide show
  1. package/README.md +3 -0
  2. package/dist/index.d.ts +1553 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +4032 -0
  5. package/dist/index.js.map +1 -0
  6. package/package.json +35 -34
  7. package/src/_contexts.ts +1089 -0
  8. package/src/_logger.ts +5 -3
  9. package/src/_middleware.ts +207 -0
  10. package/src/_pipeline-graph.helpers.ts +183 -0
  11. package/src/_pipeline-graph.ts +1307 -0
  12. package/src/_pipeline-graph.types.ts +303 -0
  13. package/src/_pipeline-leaf-resolver.ts +70 -0
  14. package/src/_plugin-lifecycle.ts +201 -0
  15. package/src/_storage.ts +173 -96
  16. package/src/_streams.schemas.ts +21 -0
  17. package/src/_streams.ts +73 -0
  18. package/src/_to-value-stream.ts +22 -11
  19. package/src/_types.ts +237 -0
  20. package/src/_unikvs-branch.ts +94 -0
  21. package/src/_unikvs.schemas.ts +451 -0
  22. package/src/_valibot.ts +17 -3
  23. package/src/_value-schemas.ts +192 -0
  24. package/src/_value-validation.ts +57 -0
  25. package/src/errors.ts +446 -252
  26. package/src/index.ts +46 -26
  27. package/src/types.ts +13 -0
  28. package/src/unikvs-branch.ts +445 -0
  29. package/src/unikvs-builder.ts +706 -0
  30. package/src/unikvs.ts +1456 -971
  31. package/src/unikvs.types.ts +194 -0
  32. package/src/value-schemas.ts +98 -0
  33. package/dist/src/_logger.d.ts +0 -3
  34. package/dist/src/_logger.d.ts.map +0 -1
  35. package/dist/src/_logger.js +0 -3
  36. package/dist/src/_merge-context.d.ts +0 -13
  37. package/dist/src/_merge-context.d.ts.map +0 -1
  38. package/dist/src/_merge-context.js +0 -12
  39. package/dist/src/_storage.d.ts +0 -18
  40. package/dist/src/_storage.d.ts.map +0 -1
  41. package/dist/src/_storage.js +0 -94
  42. package/dist/src/_to-value-stream.d.ts +0 -12
  43. package/dist/src/_to-value-stream.d.ts.map +0 -1
  44. package/dist/src/_to-value-stream.js +0 -102
  45. package/dist/src/_transformer.d.ts +0 -13
  46. package/dist/src/_transformer.d.ts.map +0 -1
  47. package/dist/src/_transformer.js +0 -60
  48. package/dist/src/_valibot.d.ts +0 -15
  49. package/dist/src/_valibot.d.ts.map +0 -1
  50. package/dist/src/_valibot.js +0 -23
  51. package/dist/src/context.types.d.ts +0 -17
  52. package/dist/src/context.types.d.ts.map +0 -1
  53. package/dist/src/context.types.js +0 -1
  54. package/dist/src/errors.d.ts +0 -340
  55. package/dist/src/errors.d.ts.map +0 -1
  56. package/dist/src/errors.js +0 -250
  57. package/dist/src/index.d.ts +0 -10
  58. package/dist/src/index.d.ts.map +0 -1
  59. package/dist/src/index.js +0 -3
  60. package/dist/src/unikvs-config.d.ts +0 -255
  61. package/dist/src/unikvs-config.d.ts.map +0 -1
  62. package/dist/src/unikvs-config.js +0 -105
  63. package/dist/src/unikvs.d.ts +0 -354
  64. package/dist/src/unikvs.d.ts.map +0 -1
  65. package/dist/src/unikvs.js +0 -867
  66. package/dist/src/utils.types.d.ts +0 -7
  67. package/dist/src/utils.types.d.ts.map +0 -1
  68. package/dist/src/utils.types.js +0 -1
  69. package/dist/src/value-stream.types.d.ts +0 -16
  70. package/dist/src/value-stream.types.d.ts.map +0 -1
  71. package/dist/src/value-stream.types.js +0 -1
  72. package/src/_merge-context.ts +0 -19
  73. package/src/_transformer.ts +0 -89
  74. package/src/context.types.ts +0 -19
  75. package/src/unikvs-config.ts +0 -426
  76. package/src/utils.types.ts +0 -6
  77. package/src/value-stream.types.ts +0 -19
package/src/_types.ts ADDED
@@ -0,0 +1,237 @@
1
+ import type {
2
+ IStorage,
3
+ IMiddleware,
4
+ IReadableStreamStorage,
5
+ IWritableStreamStorage,
6
+ } from "@unikvs/core";
7
+
8
+ export type ValueOf<T> = T[keyof T];
9
+
10
+ /**
11
+ * ミドルウェアから、エンコード時の入力データ (生データ) の型を推論するユーティリティー型です。
12
+ *
13
+ * @template TMiddleware 対象となるミドルウェアの型です。
14
+ */
15
+ export type $InferEncodeDataInput<TMiddleware extends IMiddleware> =
16
+ TMiddleware extends IMiddleware<infer TEncodeDataInput> ? TEncodeDataInput : never;
17
+
18
+ /**
19
+ * ミドルウェアから、エンコード時の出力データ (符号化済み) の型を推論するユーティリティー型です。
20
+ *
21
+ * @template TMiddleware 対象となるミドルウェアの型です。
22
+ */
23
+ export type $InferEncodeDataOutput<TMiddleware extends IMiddleware> =
24
+ TMiddleware extends IMiddleware<any, infer TEncodeDataOutput> ? TEncodeDataOutput : never;
25
+
26
+ /**
27
+ * ミドルウェアから、デコード時の入力データ (符号化済み) の型を推論するユーティリティー型です。
28
+ * 分岐先頭の親出力受容検査に使用します。
29
+ *
30
+ * @template TMiddleware 対象となるミドルウェアの型です。
31
+ */
32
+ export type $InferDecodeDataInput<TMiddleware extends IMiddleware> =
33
+ TMiddleware extends IMiddleware<any, infer TDecodeDataInput> ? TDecodeDataInput : never;
34
+
35
+ /**
36
+ * ミドルウェアから、デコード時の出力データ (生データ) の型を推論するユーティリティー型です。
37
+ * 分岐先頭の親入力受容検査に使用します。
38
+ *
39
+ * @template TMiddleware 対象となるミドルウェアの型です。
40
+ */
41
+ export type $InferDecodeDataOutput<TMiddleware extends IMiddleware> =
42
+ TMiddleware extends IMiddleware<infer TDecodeDataOutput> ? TDecodeDataOutput : never;
43
+
44
+ /**
45
+ * ミドルウェアから、デコードストリームの入力チャンクデータの型を推論するユーティリティー型です。
46
+ *
47
+ * @template TMiddleware 対象となるミドルウェアの型です。
48
+ */
49
+ export type $InferDecodeChunkInput<TMiddleware extends IMiddleware> =
50
+ TMiddleware extends IMiddleware<any, any, any, infer TDecodeChunkInput>
51
+ ? TDecodeChunkInput
52
+ : never;
53
+
54
+ /**
55
+ * ミドルウェアから、エンコードストリームの出力チャンクデータの型を推論するユーティリティー型です。
56
+ *
57
+ * @template TMiddleware 対象となるミドルウェアの型です。
58
+ */
59
+ export type $InferEncodeChunkOutput<TMiddleware extends IMiddleware> =
60
+ TMiddleware extends IMiddleware<any, any, any, infer TEncodeChunkOutput>
61
+ ? TEncodeChunkOutput
62
+ : never;
63
+
64
+ /**
65
+ * ミドルウェアから、エンコードストリームの入力チャンクデータの型を推論するユーティリティー型です。
66
+ * 分岐先頭のチャンク検査に使用します。
67
+ *
68
+ * @template TMiddleware 対象となるミドルウェアの型です。
69
+ */
70
+ export type $InferEncodeChunkInput<TMiddleware extends IMiddleware> =
71
+ TMiddleware extends IMiddleware<any, any, infer TEncodeChunkInput> ? TEncodeChunkInput : never;
72
+
73
+ /**
74
+ * ミドルウェアから、デコードストリームの出力チャンクデータの型を推論するユーティリティー型です。
75
+ * 分岐先頭のチャンク検査に使用します。
76
+ *
77
+ * @template TMiddleware 対象となるミドルウェアの型です。
78
+ */
79
+ export type $InferDecodeChunkOutput<TMiddleware extends IMiddleware> =
80
+ TMiddleware extends IMiddleware<any, any, infer TDecodeChunkOutput> ? TDecodeChunkOutput : never;
81
+
82
+ /**
83
+ * ストレージから、書き込みデータの入力型を推論するユーティリティー型です。
84
+ * 分岐先頭の親出力受容検査に使用します。
85
+ *
86
+ * @template TStorage 対象となるストレージの型です。
87
+ */
88
+ export type $InferWriteDataInput<TStorage extends IStorage> =
89
+ TStorage extends IStorage<infer TWriteDataInput, any, any, any> ? TWriteDataInput : never;
90
+
91
+ /**
92
+ * ストレージから、読み込みデータの出力型を推論するユーティリティー型です。
93
+ * 分岐先頭の親入力受容検査に使用します。
94
+ *
95
+ * @template TStorage 対象となるストレージの型です。
96
+ */
97
+ export type $InferReadDataOutput<TStorage extends IStorage> =
98
+ TStorage extends IStorage<any, infer TReadDataOutput, any, any> ? TReadDataOutput : never;
99
+
100
+ /**
101
+ * ストレージから、書き込みストリームの入力チャンクデータの型を推論するユーティリティー型です。
102
+ *
103
+ * @template TStorage 対象となるストレージの型です。
104
+ */
105
+ export type $InferWriteChunkInput<TStorage extends IStorage> =
106
+ TStorage extends IWritableStreamStorage<infer TWriteChunkInput> ? TWriteChunkInput : never;
107
+
108
+ /**
109
+ * ストレージから、読み込みストリームの出力チャンクデータの型を推論するユーティリティー型です。
110
+ *
111
+ * @template TStorage 対象となるストレージの型です。
112
+ */
113
+ export type $InferReadChunkOutput<TStorage extends IStorage> =
114
+ TStorage extends IReadableStreamStorage<infer TReadChunkOutput> ? TReadChunkOutput : never;
115
+
116
+ /**
117
+ * `any` かどうかを判定するユーティリティー型です。
118
+ * 分岐先頭の空判定で `any` を `unknown` と区別するために使用します。
119
+ *
120
+ * @template T 判定する型です。
121
+ */
122
+ export type $IsAny<T> = 0 extends 1 & T ? true : false;
123
+
124
+ /**
125
+ * `unknown` かどうかを判定するユーティリティー型です。
126
+ * 空ブランチの先頭マーカー検出に使用します。
127
+ *
128
+ * @template T 判定する型です。
129
+ */
130
+ export type $IsUnknown<T> =
131
+ $IsAny<T> extends true
132
+ ? false
133
+ : [T] extends [unknown]
134
+ ? [unknown] extends [T]
135
+ ? true
136
+ : false
137
+ : false;
138
+
139
+ /**
140
+ * `never` かどうかを判定するユーティリティー型です。
141
+ * チャンクの非対応判定に使用します。
142
+ *
143
+ * @template T 判定する型です。
144
+ */
145
+ export type $IsNever<T> = [T] extends [never] ? true : false;
146
+
147
+ /**
148
+ * 親出力が分岐先頭のエンコード入力を受容できるかを検査するユーティリティー型です。
149
+ * 不適合時は `never` を返し、`fork` 引数を `never` 化して型エラーを発生させます。
150
+ * 先頭のみを検査します。
151
+ * 中間以降は検査しません。
152
+ *
153
+ * @template TParentOutput 親の現出力型です。
154
+ * @template TBranchHeadInput 分岐先頭の入力型です。
155
+ */
156
+ export type $CheckForkEncodeData<TParentOutput, TBranchHeadInput> =
157
+ $IsUnknown<TBranchHeadInput> extends true
158
+ ? unknown
159
+ : $IsAny<TBranchHeadInput> extends true
160
+ ? unknown
161
+ : $IsAny<TParentOutput> extends true
162
+ ? unknown
163
+ : [TParentOutput] extends [TBranchHeadInput]
164
+ ? unknown
165
+ : never;
166
+
167
+ /**
168
+ * 分岐先頭のデコード出力が親入力を受容できるかを検査するユーティリティー型です。
169
+ * 不適合時は `never` を返します。
170
+ * 先頭のみを検査します。
171
+ * 中間以降は検査しません。
172
+ *
173
+ * @template TParentInput 親の現入力型です。
174
+ * @template TBranchHeadOutput 分岐先頭の出力型です。
175
+ */
176
+ export type $CheckForkDecodeData<TParentInput, TBranchHeadOutput> =
177
+ $IsUnknown<TBranchHeadOutput> extends true
178
+ ? unknown
179
+ : $IsAny<TBranchHeadOutput> extends true
180
+ ? unknown
181
+ : $IsAny<TParentInput> extends true
182
+ ? unknown
183
+ : [TBranchHeadOutput] extends [TParentInput]
184
+ ? unknown
185
+ : never;
186
+
187
+ /**
188
+ * 親チャンク出力が分岐先頭チャンク入力を受容できるかを検査するユーティリティー型です。
189
+ * ストリーム非対応の `never` は許容します。
190
+ * 先頭のみを検査します。
191
+ * 中間以降は検査しません。
192
+ *
193
+ * @template TParentOutput 親の現チャンク出力型です。
194
+ * @template TBranchHeadInput 分岐先頭のチャンク入力型です。
195
+ */
196
+ export type $CheckForkEncodeChunk<TParentOutput, TBranchHeadInput> =
197
+ $IsUnknown<TBranchHeadInput> extends true
198
+ ? unknown
199
+ : $IsAny<TBranchHeadInput> extends true
200
+ ? unknown
201
+ : $IsAny<TParentOutput> extends true
202
+ ? unknown
203
+ : $IsUnknown<TParentOutput> extends true
204
+ ? unknown
205
+ : $IsNever<TParentOutput> extends true
206
+ ? unknown
207
+ : $IsNever<TBranchHeadInput> extends true
208
+ ? unknown
209
+ : [TParentOutput] extends [TBranchHeadInput]
210
+ ? unknown
211
+ : never;
212
+
213
+ /**
214
+ * 分岐先頭チャンク出力が親チャンク入力を受容できるかを検査するユーティリティー型です。
215
+ * ストリーム非対応の `never` は許容します。
216
+ * 先頭のみを検査します。
217
+ * 中間以降は検査しません。
218
+ *
219
+ * @template TParentInput 親の現チャンク入力型です。
220
+ * @template TBranchHeadOutput 分岐先頭のチャンク出力型です。
221
+ */
222
+ export type $CheckForkDecodeChunk<TParentInput, TBranchHeadOutput> =
223
+ $IsUnknown<TBranchHeadOutput> extends true
224
+ ? unknown
225
+ : $IsAny<TBranchHeadOutput> extends true
226
+ ? unknown
227
+ : $IsAny<TParentInput> extends true
228
+ ? unknown
229
+ : $IsUnknown<TParentInput> extends true
230
+ ? unknown
231
+ : $IsNever<TParentInput> extends true
232
+ ? unknown
233
+ : $IsNever<TBranchHeadOutput> extends true
234
+ ? unknown
235
+ : [TBranchHeadOutput] extends [TParentInput]
236
+ ? unknown
237
+ : never;
@@ -0,0 +1,94 @@
1
+ import type UniKvsMiddleware from "./_middleware.js";
2
+ import type UniKvsStorage from "./_storage.js";
3
+ import type { IUniKvsBranch } from "./unikvs-branch.js";
4
+
5
+ /**
6
+ * `fork` で親に引き継がれる分岐先の情報です。
7
+ */
8
+ type BranchDestination = {
9
+ /**
10
+ * データの永続化先です。
11
+ */
12
+ readonly storage: UniKvsStorage;
13
+
14
+ /**
15
+ * ストレージに対するパイプラインです。
16
+ * 登録順に保持します。
17
+ */
18
+ readonly middlewares: readonly UniKvsMiddleware[];
19
+ };
20
+
21
+ /**
22
+ * `UniKvsBranch` のインスタンスごとの内部状態です。
23
+ * ネイティブの `#` フィールドの代わりに `WeakMap` に保持することで、別モジュールの `UniKvsBuilder` から内部アクセサー経由で参照できます。
24
+ * `branchStates` 自体は外部に公開しません。
25
+ */
26
+ export type BranchState = {
27
+ /**
28
+ * 自前の相対パイプラインです。
29
+ * 登録順に保持します。
30
+ */
31
+ readonly middlewares: UniKvsMiddleware[];
32
+
33
+ /**
34
+ * 自前の分岐先と各登録時点の相対パイプラインのスナップショットです。
35
+ */
36
+ readonly destinations: BranchDestination[];
37
+ };
38
+
39
+ /**
40
+ * インスタンスと内部状態の対応表です。
41
+ * コンパイラーによる `#` フィールドのダウンレベルと同様に、`WeakMap` をブランド兼ストレージとして使います。
42
+ */
43
+ export const branchStates: WeakMap<IUniKvsBranch, BranchState> = /* #__PURE__ */ new WeakMap<
44
+ IUniKvsBranch,
45
+ BranchState
46
+ >();
47
+
48
+ /**
49
+ * 分岐先のスナップショットを返します。
50
+ *
51
+ * 親の `fork` で接頭辞付加するために使用します。
52
+ * `new Branch()` で作成された分岐以外の値が渡されたときは例外を投げます。
53
+ * 外部からは使用しません。
54
+ *
55
+ * @param branch スナップショットを取得する分岐です。
56
+ *
57
+ * @returns 分岐先のコピーを返します。
58
+ */
59
+ export function getBranchDestinationsForFork(branch: IUniKvsBranch): BranchDestination[] {
60
+ const state = branchStates.get(branch);
61
+
62
+ if (!state) {
63
+ throw new TypeError("fork() expects a branch created by new Branch().");
64
+ }
65
+
66
+ // コピーを返します。親の fork で接頭辞を付けても、元の分岐は変更されません。
67
+ return state.destinations.map((dest) => ({
68
+ storage: dest.storage,
69
+ middlewares: [...dest.middlewares],
70
+ }));
71
+ }
72
+
73
+ /**
74
+ * 分岐先を親の接頭辞付きで追加します。
75
+ *
76
+ * @param destinations 追加先の分岐先列です。
77
+ * @param prefix 親のミドルウェア列です。
78
+ * @param branch 追加する分岐です。
79
+ */
80
+ export function appendForkDestinations(
81
+ destinations: BranchDestination[],
82
+ prefix: readonly UniKvsMiddleware[],
83
+ branch: IUniKvsBranch,
84
+ ): void {
85
+ const prefixSnapshot = [...prefix];
86
+
87
+ for (const dest of getBranchDestinationsForFork(branch)) {
88
+ destinations.push({
89
+ storage: dest.storage,
90
+ // fork 時点の親チェーンを接頭辞として各分岐先の先頭に付け、ブランチのミドルウェアを結合します。
91
+ middlewares: [...prefixSnapshot, ...dest.middlewares],
92
+ });
93
+ }
94
+ }