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.
- package/README.md +3 -0
- package/dist/index.d.ts +1553 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4032 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -34
- package/src/_contexts.ts +1089 -0
- package/src/_logger.ts +5 -3
- package/src/_middleware.ts +207 -0
- package/src/_pipeline-graph.helpers.ts +183 -0
- package/src/_pipeline-graph.ts +1307 -0
- package/src/_pipeline-graph.types.ts +303 -0
- package/src/_pipeline-leaf-resolver.ts +70 -0
- package/src/_plugin-lifecycle.ts +201 -0
- package/src/_storage.ts +173 -96
- package/src/_streams.schemas.ts +21 -0
- package/src/_streams.ts +73 -0
- package/src/_to-value-stream.ts +22 -11
- package/src/_types.ts +237 -0
- package/src/_unikvs-branch.ts +94 -0
- package/src/_unikvs.schemas.ts +451 -0
- package/src/_valibot.ts +17 -3
- package/src/_value-schemas.ts +192 -0
- package/src/_value-validation.ts +57 -0
- package/src/errors.ts +446 -252
- package/src/index.ts +46 -26
- package/src/types.ts +13 -0
- package/src/unikvs-branch.ts +445 -0
- package/src/unikvs-builder.ts +706 -0
- package/src/unikvs.ts +1456 -971
- package/src/unikvs.types.ts +194 -0
- package/src/value-schemas.ts +98 -0
- package/dist/src/_logger.d.ts +0 -3
- package/dist/src/_logger.d.ts.map +0 -1
- package/dist/src/_logger.js +0 -3
- package/dist/src/_merge-context.d.ts +0 -13
- package/dist/src/_merge-context.d.ts.map +0 -1
- package/dist/src/_merge-context.js +0 -12
- package/dist/src/_storage.d.ts +0 -18
- package/dist/src/_storage.d.ts.map +0 -1
- package/dist/src/_storage.js +0 -94
- package/dist/src/_to-value-stream.d.ts +0 -12
- package/dist/src/_to-value-stream.d.ts.map +0 -1
- package/dist/src/_to-value-stream.js +0 -102
- package/dist/src/_transformer.d.ts +0 -13
- package/dist/src/_transformer.d.ts.map +0 -1
- package/dist/src/_transformer.js +0 -60
- package/dist/src/_valibot.d.ts +0 -15
- package/dist/src/_valibot.d.ts.map +0 -1
- package/dist/src/_valibot.js +0 -23
- package/dist/src/context.types.d.ts +0 -17
- package/dist/src/context.types.d.ts.map +0 -1
- package/dist/src/context.types.js +0 -1
- package/dist/src/errors.d.ts +0 -340
- package/dist/src/errors.d.ts.map +0 -1
- package/dist/src/errors.js +0 -250
- package/dist/src/index.d.ts +0 -10
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -3
- package/dist/src/unikvs-config.d.ts +0 -255
- package/dist/src/unikvs-config.d.ts.map +0 -1
- package/dist/src/unikvs-config.js +0 -105
- package/dist/src/unikvs.d.ts +0 -354
- package/dist/src/unikvs.d.ts.map +0 -1
- package/dist/src/unikvs.js +0 -867
- package/dist/src/utils.types.d.ts +0 -7
- package/dist/src/utils.types.d.ts.map +0 -1
- package/dist/src/utils.types.js +0 -1
- package/dist/src/value-stream.types.d.ts +0 -16
- package/dist/src/value-stream.types.d.ts.map +0 -1
- package/dist/src/value-stream.types.js +0 -1
- package/src/_merge-context.ts +0 -19
- package/src/_transformer.ts +0 -89
- package/src/context.types.ts +0 -19
- package/src/unikvs-config.ts +0 -426
- package/src/utils.types.ts +0 -6
- package/src/value-stream.types.ts +0 -19
|
@@ -0,0 +1,706 @@
|
|
|
1
|
+
import type { IStorage, IMiddleware } from "@unikvs/core";
|
|
2
|
+
|
|
3
|
+
import type { VariablesInit } from "./_contexts.js";
|
|
4
|
+
import UniKvsMiddleware from "./_middleware.js";
|
|
5
|
+
import { PipelineGraph } from "./_pipeline-graph.js";
|
|
6
|
+
import UniKvsStorage from "./_storage.js";
|
|
7
|
+
import type {
|
|
8
|
+
ValueOf,
|
|
9
|
+
$CheckForkDecodeData,
|
|
10
|
+
$CheckForkEncodeData,
|
|
11
|
+
$CheckForkDecodeChunk,
|
|
12
|
+
$CheckForkEncodeChunk,
|
|
13
|
+
$InferDecodeDataInput,
|
|
14
|
+
$InferReadChunkOutput,
|
|
15
|
+
$InferWriteChunkInput,
|
|
16
|
+
$InferDecodeChunkInput,
|
|
17
|
+
$InferEncodeDataOutput,
|
|
18
|
+
$InferEncodeChunkOutput,
|
|
19
|
+
} from "./_types.js";
|
|
20
|
+
import { appendForkDestinations } from "./_unikvs-branch.js";
|
|
21
|
+
import { VariablesArgsSchema, VariablesInitSchema } from "./_unikvs.schemas.js";
|
|
22
|
+
import type { GenericSchema } from "./_valibot.js";
|
|
23
|
+
import {
|
|
24
|
+
array,
|
|
25
|
+
tuple,
|
|
26
|
+
union,
|
|
27
|
+
custom,
|
|
28
|
+
object,
|
|
29
|
+
record,
|
|
30
|
+
string,
|
|
31
|
+
optional,
|
|
32
|
+
parseInput,
|
|
33
|
+
} from "./_valibot.js";
|
|
34
|
+
import {
|
|
35
|
+
type ValueSchemaInfo,
|
|
36
|
+
type $InferPlainValueData,
|
|
37
|
+
type IValueSchemaResolver,
|
|
38
|
+
type $InferStreamValueChunkData,
|
|
39
|
+
VALUE_SCHEMA_BRAND,
|
|
40
|
+
createValueSchemaResolver,
|
|
41
|
+
} from "./_value-schemas.js";
|
|
42
|
+
import { MissingStorageError } from "./errors.js";
|
|
43
|
+
import type { IUniKvsBranch, KeyValueMapping } from "./unikvs-branch.js";
|
|
44
|
+
import UniKvs from "./unikvs.js";
|
|
45
|
+
import type { Value } from "./value-schemas.js";
|
|
46
|
+
|
|
47
|
+
type KeyofKVMap<TKeyValueMapping extends KeyValueMapping = KeyValueMapping> = Extract<
|
|
48
|
+
keyof TKeyValueMapping,
|
|
49
|
+
string
|
|
50
|
+
>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#builder)
|
|
54
|
+
*/
|
|
55
|
+
export interface IUniKvsBuilder<
|
|
56
|
+
TKeyValueMapping extends KeyValueMapping = KeyValueMapping,
|
|
57
|
+
TUniKvsDataInput = ValueOf<{
|
|
58
|
+
[TKey in KeyofKVMap<TKeyValueMapping>]: $InferPlainValueData<TKeyValueMapping[TKey]>;
|
|
59
|
+
}>,
|
|
60
|
+
TUniKvsDataOutput = TUniKvsDataInput,
|
|
61
|
+
TUniKvsChunkInput = ValueOf<{
|
|
62
|
+
[TKey in KeyofKVMap<TKeyValueMapping>]: $InferStreamValueChunkData<TKeyValueMapping[TKey]>;
|
|
63
|
+
}>,
|
|
64
|
+
TUniKvsChunkOutput = TUniKvsChunkInput,
|
|
65
|
+
TLastMiddlewareDecodeDataInput = TUniKvsDataOutput,
|
|
66
|
+
TLastMiddlewareEncodeDataOutput = TUniKvsDataInput,
|
|
67
|
+
TLastMiddlewareDecodeChunkInput = TUniKvsChunkOutput,
|
|
68
|
+
TLastMiddlewareEncodeChunkOutput = TUniKvsChunkInput,
|
|
69
|
+
> {
|
|
70
|
+
fork<
|
|
71
|
+
TBranchChunkInput,
|
|
72
|
+
TBranchChunkOutput,
|
|
73
|
+
TBranchHeadEncodeDataInput,
|
|
74
|
+
TBranchHeadDecodeDataOutput,
|
|
75
|
+
TBranchHeadEncodeChunkInput,
|
|
76
|
+
TBranchHeadDecodeChunkOutput,
|
|
77
|
+
TBranchLastDecodeDataInput,
|
|
78
|
+
TBranchLastEncodeDataOutput,
|
|
79
|
+
TBranchLastDecodeChunkInput,
|
|
80
|
+
TBranchLastEncodeChunkOutput,
|
|
81
|
+
TBranchHasPersist extends boolean,
|
|
82
|
+
>(
|
|
83
|
+
branch: IUniKvsBranch<
|
|
84
|
+
any,
|
|
85
|
+
TBranchChunkInput,
|
|
86
|
+
TBranchChunkOutput,
|
|
87
|
+
TBranchHeadEncodeDataInput,
|
|
88
|
+
TBranchHeadDecodeDataOutput,
|
|
89
|
+
TBranchHeadEncodeChunkInput,
|
|
90
|
+
TBranchHeadDecodeChunkOutput,
|
|
91
|
+
TBranchLastDecodeDataInput,
|
|
92
|
+
TBranchLastEncodeDataOutput,
|
|
93
|
+
TBranchLastDecodeChunkInput,
|
|
94
|
+
TBranchLastEncodeChunkOutput,
|
|
95
|
+
TBranchHasPersist
|
|
96
|
+
> &
|
|
97
|
+
(TBranchHasPersist extends true
|
|
98
|
+
? $CheckForkEncodeData<TLastMiddlewareEncodeDataOutput, TBranchHeadEncodeDataInput>
|
|
99
|
+
: unknown) &
|
|
100
|
+
(TBranchHasPersist extends true
|
|
101
|
+
? $CheckForkDecodeData<TLastMiddlewareDecodeDataInput, TBranchHeadDecodeDataOutput>
|
|
102
|
+
: unknown) &
|
|
103
|
+
(TBranchHasPersist extends true
|
|
104
|
+
? $CheckForkEncodeChunk<TLastMiddlewareEncodeChunkOutput, TBranchHeadEncodeChunkInput>
|
|
105
|
+
: unknown) &
|
|
106
|
+
(TBranchHasPersist extends true
|
|
107
|
+
? $CheckForkDecodeChunk<TLastMiddlewareDecodeChunkInput, TBranchHeadDecodeChunkOutput>
|
|
108
|
+
: unknown),
|
|
109
|
+
): IUniKvsBuilder<
|
|
110
|
+
TKeyValueMapping,
|
|
111
|
+
TUniKvsDataInput,
|
|
112
|
+
TUniKvsDataOutput,
|
|
113
|
+
TBranchHasPersist extends true
|
|
114
|
+
? [TUniKvsChunkInput] extends [never]
|
|
115
|
+
? never
|
|
116
|
+
: [TBranchChunkInput] extends [never]
|
|
117
|
+
? never
|
|
118
|
+
: TUniKvsChunkInput
|
|
119
|
+
: TUniKvsChunkInput,
|
|
120
|
+
TBranchHasPersist extends true
|
|
121
|
+
? [TUniKvsChunkOutput] extends [never]
|
|
122
|
+
? never
|
|
123
|
+
: [TBranchChunkOutput] extends [never]
|
|
124
|
+
? never
|
|
125
|
+
: TUniKvsChunkOutput
|
|
126
|
+
: TUniKvsChunkOutput,
|
|
127
|
+
TLastMiddlewareDecodeDataInput,
|
|
128
|
+
TLastMiddlewareEncodeDataOutput,
|
|
129
|
+
TLastMiddlewareDecodeChunkInput,
|
|
130
|
+
TLastMiddlewareEncodeChunkOutput
|
|
131
|
+
>;
|
|
132
|
+
|
|
133
|
+
use<
|
|
134
|
+
TMiddleware extends IMiddleware<
|
|
135
|
+
[TLastMiddlewareEncodeDataOutput] extends [never] ? any : TLastMiddlewareEncodeDataOutput,
|
|
136
|
+
any,
|
|
137
|
+
[TLastMiddlewareEncodeChunkOutput] extends [never] ? any : TLastMiddlewareEncodeChunkOutput
|
|
138
|
+
>,
|
|
139
|
+
>(
|
|
140
|
+
middleware: TMiddleware,
|
|
141
|
+
): IUniKvsBuilder<
|
|
142
|
+
TKeyValueMapping,
|
|
143
|
+
TUniKvsDataInput,
|
|
144
|
+
TUniKvsDataOutput,
|
|
145
|
+
[TUniKvsChunkInput] extends [never]
|
|
146
|
+
? never
|
|
147
|
+
: [$InferDecodeChunkInput<TMiddleware>] extends [never]
|
|
148
|
+
? never
|
|
149
|
+
: TUniKvsChunkInput,
|
|
150
|
+
[TUniKvsChunkOutput] extends [never]
|
|
151
|
+
? never
|
|
152
|
+
: [$InferEncodeChunkOutput<TMiddleware>] extends [never]
|
|
153
|
+
? never
|
|
154
|
+
: TUniKvsChunkOutput,
|
|
155
|
+
$InferDecodeDataInput<TMiddleware>,
|
|
156
|
+
$InferEncodeDataOutput<TMiddleware>,
|
|
157
|
+
$InferDecodeChunkInput<TMiddleware>,
|
|
158
|
+
$InferEncodeChunkOutput<TMiddleware>
|
|
159
|
+
>;
|
|
160
|
+
|
|
161
|
+
persist<
|
|
162
|
+
TStorage extends IStorage<
|
|
163
|
+
[TLastMiddlewareEncodeDataOutput] extends [never] ? any : TLastMiddlewareEncodeDataOutput,
|
|
164
|
+
[TLastMiddlewareDecodeDataInput] extends [never] ? any : TLastMiddlewareDecodeDataInput,
|
|
165
|
+
[TLastMiddlewareEncodeChunkOutput] extends [never] ? any : TLastMiddlewareEncodeChunkOutput,
|
|
166
|
+
[TLastMiddlewareDecodeChunkInput] extends [never] ? any : TLastMiddlewareDecodeChunkInput
|
|
167
|
+
>,
|
|
168
|
+
>(
|
|
169
|
+
storage: TStorage,
|
|
170
|
+
): IUniKvsBuilderFinalizer<
|
|
171
|
+
TKeyValueMapping,
|
|
172
|
+
TLastMiddlewareEncodeDataOutput,
|
|
173
|
+
TLastMiddlewareDecodeDataInput,
|
|
174
|
+
[TLastMiddlewareEncodeChunkOutput] extends [never]
|
|
175
|
+
? never
|
|
176
|
+
: [$InferWriteChunkInput<TStorage>] extends [never]
|
|
177
|
+
? never
|
|
178
|
+
: TLastMiddlewareEncodeChunkOutput,
|
|
179
|
+
[TLastMiddlewareDecodeChunkInput] extends [never]
|
|
180
|
+
? never
|
|
181
|
+
: [$InferReadChunkOutput<TStorage>] extends [never]
|
|
182
|
+
? never
|
|
183
|
+
: TLastMiddlewareDecodeChunkInput,
|
|
184
|
+
TLastMiddlewareDecodeDataInput,
|
|
185
|
+
TLastMiddlewareEncodeDataOutput,
|
|
186
|
+
TLastMiddlewareDecodeChunkInput,
|
|
187
|
+
TLastMiddlewareEncodeChunkOutput
|
|
188
|
+
>;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#base-vars-set-vars)
|
|
192
|
+
*/
|
|
193
|
+
setVars(vars: VariablesInit): this;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#base-vars-assign-vars)
|
|
197
|
+
*/
|
|
198
|
+
assignVars(vars: VariablesInit): this;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#base-vars-defaults-vars)
|
|
202
|
+
*/
|
|
203
|
+
defaultsVars(vars: VariablesInit): this;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#builder)
|
|
208
|
+
*/
|
|
209
|
+
export interface IUniKvsBuilderFinalizer<
|
|
210
|
+
TKeyValueMapping extends KeyValueMapping = KeyValueMapping,
|
|
211
|
+
TWriteDataInput = any,
|
|
212
|
+
TReadDataOutput = any,
|
|
213
|
+
TWriteChunkInput = any,
|
|
214
|
+
TReadChunkOutput = any,
|
|
215
|
+
TLastMiddlewareDecodeDataInput = TReadDataOutput,
|
|
216
|
+
TLastMiddlewareEncodeDataOutput = TWriteDataInput,
|
|
217
|
+
TLastMiddlewareDecodeChunkInput = TReadChunkOutput,
|
|
218
|
+
TLastMiddlewareEncodeChunkOutput = TWriteChunkInput,
|
|
219
|
+
> {
|
|
220
|
+
fork<
|
|
221
|
+
TBranchChunkInput,
|
|
222
|
+
TBranchChunkOutput,
|
|
223
|
+
TBranchHeadEncodeDataInput,
|
|
224
|
+
TBranchHeadDecodeDataOutput,
|
|
225
|
+
TBranchHeadEncodeChunkInput,
|
|
226
|
+
TBranchHeadDecodeChunkOutput,
|
|
227
|
+
TBranchLastDecodeDataInput,
|
|
228
|
+
TBranchLastEncodeDataOutput,
|
|
229
|
+
TBranchLastDecodeChunkInput,
|
|
230
|
+
TBranchLastEncodeChunkOutput,
|
|
231
|
+
TBranchHasPersist extends boolean,
|
|
232
|
+
>(
|
|
233
|
+
branch: IUniKvsBranch<
|
|
234
|
+
any,
|
|
235
|
+
TBranchChunkInput,
|
|
236
|
+
TBranchChunkOutput,
|
|
237
|
+
TBranchHeadEncodeDataInput,
|
|
238
|
+
TBranchHeadDecodeDataOutput,
|
|
239
|
+
TBranchHeadEncodeChunkInput,
|
|
240
|
+
TBranchHeadDecodeChunkOutput,
|
|
241
|
+
TBranchLastDecodeDataInput,
|
|
242
|
+
TBranchLastEncodeDataOutput,
|
|
243
|
+
TBranchLastDecodeChunkInput,
|
|
244
|
+
TBranchLastEncodeChunkOutput,
|
|
245
|
+
TBranchHasPersist
|
|
246
|
+
> &
|
|
247
|
+
(TBranchHasPersist extends true
|
|
248
|
+
? $CheckForkEncodeData<TLastMiddlewareEncodeDataOutput, TBranchHeadEncodeDataInput>
|
|
249
|
+
: unknown) &
|
|
250
|
+
(TBranchHasPersist extends true
|
|
251
|
+
? $CheckForkDecodeData<TLastMiddlewareDecodeDataInput, TBranchHeadDecodeDataOutput>
|
|
252
|
+
: unknown) &
|
|
253
|
+
(TBranchHasPersist extends true
|
|
254
|
+
? $CheckForkEncodeChunk<TLastMiddlewareEncodeChunkOutput, TBranchHeadEncodeChunkInput>
|
|
255
|
+
: unknown) &
|
|
256
|
+
(TBranchHasPersist extends true
|
|
257
|
+
? $CheckForkDecodeChunk<TLastMiddlewareDecodeChunkInput, TBranchHeadDecodeChunkOutput>
|
|
258
|
+
: unknown),
|
|
259
|
+
): IUniKvsBuilderFinalizer<
|
|
260
|
+
TKeyValueMapping,
|
|
261
|
+
TWriteDataInput,
|
|
262
|
+
TReadDataOutput,
|
|
263
|
+
TBranchHasPersist extends true
|
|
264
|
+
? [TWriteChunkInput] extends [never]
|
|
265
|
+
? never
|
|
266
|
+
: [TBranchChunkInput] extends [never]
|
|
267
|
+
? never
|
|
268
|
+
: TWriteChunkInput
|
|
269
|
+
: TWriteChunkInput,
|
|
270
|
+
TBranchHasPersist extends true
|
|
271
|
+
? [TReadChunkOutput] extends [never]
|
|
272
|
+
? never
|
|
273
|
+
: [TBranchChunkOutput] extends [never]
|
|
274
|
+
? never
|
|
275
|
+
: TReadChunkOutput
|
|
276
|
+
: TReadChunkOutput,
|
|
277
|
+
TLastMiddlewareDecodeDataInput,
|
|
278
|
+
TLastMiddlewareEncodeDataOutput,
|
|
279
|
+
TLastMiddlewareDecodeChunkInput,
|
|
280
|
+
TLastMiddlewareEncodeChunkOutput
|
|
281
|
+
>;
|
|
282
|
+
|
|
283
|
+
use<
|
|
284
|
+
TMiddleware extends IMiddleware<
|
|
285
|
+
[TLastMiddlewareEncodeDataOutput] extends [never] ? any : TLastMiddlewareEncodeDataOutput,
|
|
286
|
+
any,
|
|
287
|
+
[TLastMiddlewareEncodeChunkOutput] extends [never] ? any : TLastMiddlewareEncodeChunkOutput
|
|
288
|
+
>,
|
|
289
|
+
>(
|
|
290
|
+
middleware: TMiddleware,
|
|
291
|
+
): IUniKvsBuilderFinalizer<
|
|
292
|
+
TKeyValueMapping,
|
|
293
|
+
TWriteDataInput,
|
|
294
|
+
TReadDataOutput,
|
|
295
|
+
TWriteChunkInput,
|
|
296
|
+
TReadChunkOutput,
|
|
297
|
+
$InferDecodeDataInput<TMiddleware>,
|
|
298
|
+
$InferEncodeDataOutput<TMiddleware>,
|
|
299
|
+
$InferDecodeChunkInput<TMiddleware>,
|
|
300
|
+
$InferEncodeChunkOutput<TMiddleware>
|
|
301
|
+
>;
|
|
302
|
+
|
|
303
|
+
persist<
|
|
304
|
+
TStorage extends IStorage<
|
|
305
|
+
[TLastMiddlewareEncodeDataOutput] extends [never] ? any : TLastMiddlewareEncodeDataOutput,
|
|
306
|
+
[TLastMiddlewareDecodeDataInput] extends [never] ? any : TLastMiddlewareDecodeDataInput,
|
|
307
|
+
[TLastMiddlewareEncodeChunkOutput] extends [never] ? any : TLastMiddlewareEncodeChunkOutput,
|
|
308
|
+
[TLastMiddlewareDecodeChunkInput] extends [never] ? any : TLastMiddlewareDecodeChunkInput
|
|
309
|
+
>,
|
|
310
|
+
>(
|
|
311
|
+
storage: TStorage,
|
|
312
|
+
): IUniKvsBuilderFinalizer<
|
|
313
|
+
TKeyValueMapping,
|
|
314
|
+
TWriteDataInput,
|
|
315
|
+
TReadDataOutput,
|
|
316
|
+
[TWriteChunkInput] extends [never]
|
|
317
|
+
? never
|
|
318
|
+
: [$InferWriteChunkInput<TStorage>] extends [never]
|
|
319
|
+
? never
|
|
320
|
+
: TWriteChunkInput,
|
|
321
|
+
[TReadChunkOutput] extends [never]
|
|
322
|
+
? never
|
|
323
|
+
: [$InferReadChunkOutput<TStorage>] extends [never]
|
|
324
|
+
? never
|
|
325
|
+
: TReadChunkOutput,
|
|
326
|
+
TLastMiddlewareDecodeDataInput,
|
|
327
|
+
TLastMiddlewareEncodeDataOutput,
|
|
328
|
+
TLastMiddlewareDecodeChunkInput,
|
|
329
|
+
TLastMiddlewareEncodeChunkOutput
|
|
330
|
+
>;
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#base-vars-set-vars)
|
|
334
|
+
*/
|
|
335
|
+
setVars(vars: VariablesInit): this;
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#base-vars-assign-vars)
|
|
339
|
+
*/
|
|
340
|
+
assignVars(vars: VariablesInit): this;
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#base-vars-defaults-vars)
|
|
344
|
+
*/
|
|
345
|
+
defaultsVars(vars: VariablesInit): this;
|
|
346
|
+
|
|
347
|
+
build(): UniKvs<TKeyValueMapping>;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#builder)
|
|
352
|
+
*/
|
|
353
|
+
export type UniKvsBuilderOptions = {
|
|
354
|
+
/**
|
|
355
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#base-vars)
|
|
356
|
+
*/
|
|
357
|
+
readonly vars?: VariablesInit | undefined;
|
|
358
|
+
readonly schema?: UniKvsSchema | undefined;
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* オブジェクト形式のスキーマ定義です。キーをそのままキーとして扱います。
|
|
363
|
+
*/
|
|
364
|
+
export type UniKvsSchemaRecord = Readonly<Record<string, Value>>;
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* 配列形式のスキーマ定義です。キーのスキーマと値のスキーマの組を並べます。
|
|
368
|
+
*/
|
|
369
|
+
export type UniKvsSchemaEntries = readonly (readonly [GenericSchema, Value])[];
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* `new Builder()` の `schema` オプションに指定できるスキーマ定義です。
|
|
373
|
+
*/
|
|
374
|
+
export type UniKvsSchema = UniKvsSchemaRecord | UniKvsSchemaEntries;
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* スキーマ定義からキーと値のマッピング型を推論します。
|
|
378
|
+
*
|
|
379
|
+
* @template TSchema 対象となるスキーマ定義の型です。
|
|
380
|
+
*/
|
|
381
|
+
export type $InferKeyValueMapping<TSchema extends UniKvsSchema> =
|
|
382
|
+
TSchema extends readonly (readonly [GenericSchema, infer TValue extends Value])[]
|
|
383
|
+
? Readonly<Record<string, TValue>>
|
|
384
|
+
: TSchema extends Readonly<Record<string, Value>>
|
|
385
|
+
? { readonly [TKey in keyof TSchema]: TSchema[TKey] }
|
|
386
|
+
: never;
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* 値が Valibot のスキーマであるかどうかを判定します。
|
|
390
|
+
*/
|
|
391
|
+
function isGenericSchema(input: unknown): input is GenericSchema {
|
|
392
|
+
return (
|
|
393
|
+
typeof input === "object" &&
|
|
394
|
+
input !== null &&
|
|
395
|
+
// GenericSchema は関数 "~run" を持ちます。
|
|
396
|
+
"~run" in input &&
|
|
397
|
+
typeof input["~run"] === "function"
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* 値がスキーマ情報であるかどうかを判定します。
|
|
403
|
+
*/
|
|
404
|
+
function isValueSchemaInfo(input: unknown): input is ValueSchemaInfo {
|
|
405
|
+
return (
|
|
406
|
+
typeof input === "object" &&
|
|
407
|
+
input !== null &&
|
|
408
|
+
VALUE_SCHEMA_BRAND in input &&
|
|
409
|
+
input[VALUE_SCHEMA_BRAND] === true
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const UniKvsBuilderOptionsSchema = object({
|
|
414
|
+
/**
|
|
415
|
+
* キーと値のスキーマ定義です。
|
|
416
|
+
*/
|
|
417
|
+
schema: optional(
|
|
418
|
+
union([
|
|
419
|
+
array(
|
|
420
|
+
tuple([custom<GenericSchema>(isGenericSchema), custom<ValueSchemaInfo>(isValueSchemaInfo)]),
|
|
421
|
+
),
|
|
422
|
+
record(string(), custom<ValueSchemaInfo>(isValueSchemaInfo)),
|
|
423
|
+
]),
|
|
424
|
+
),
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* 基本となる実行時変数です。
|
|
428
|
+
*/
|
|
429
|
+
vars: optional(VariablesInitSchema),
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
const UniKvsBuilderArgsSchema = tuple([optional(UniKvsBuilderOptionsSchema)]);
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#builder)
|
|
436
|
+
*/
|
|
437
|
+
export default class UniKvsBuilder<
|
|
438
|
+
const TInit extends KeyValueMapping | UniKvsSchema = KeyValueMapping,
|
|
439
|
+
TKeyValueMapping extends KeyValueMapping = $InferKeyValueMapping<TInit>,
|
|
440
|
+
TUniKvsDataInput = ValueOf<{
|
|
441
|
+
[TKey in KeyofKVMap<TKeyValueMapping>]: $InferPlainValueData<TKeyValueMapping[TKey]>;
|
|
442
|
+
}>,
|
|
443
|
+
TUniKvsDataOutput = TUniKvsDataInput,
|
|
444
|
+
TUniKvsChunkInput = ValueOf<{
|
|
445
|
+
[TKey in KeyofKVMap<TKeyValueMapping>]: $InferStreamValueChunkData<TKeyValueMapping[TKey]>;
|
|
446
|
+
}>,
|
|
447
|
+
TUniKvsChunkOutput = TUniKvsChunkInput,
|
|
448
|
+
TLastMiddlewareDecodeDataInput = TUniKvsDataOutput,
|
|
449
|
+
TLastMiddlewareEncodeDataOutput = TUniKvsDataInput,
|
|
450
|
+
TLastMiddlewareDecodeChunkInput = TUniKvsChunkOutput,
|
|
451
|
+
TLastMiddlewareEncodeChunkOutput = TUniKvsChunkInput,
|
|
452
|
+
> implements IUniKvsBuilder<
|
|
453
|
+
TKeyValueMapping,
|
|
454
|
+
TUniKvsDataInput,
|
|
455
|
+
TUniKvsDataOutput,
|
|
456
|
+
TUniKvsChunkInput,
|
|
457
|
+
TUniKvsChunkOutput,
|
|
458
|
+
TLastMiddlewareDecodeDataInput,
|
|
459
|
+
TLastMiddlewareEncodeDataOutput,
|
|
460
|
+
TLastMiddlewareDecodeChunkInput,
|
|
461
|
+
TLastMiddlewareEncodeChunkOutput
|
|
462
|
+
> {
|
|
463
|
+
/**
|
|
464
|
+
* 登録済みストレージと各登録時点のパイプラインのスナップショットです。
|
|
465
|
+
*/
|
|
466
|
+
readonly #destinations: {
|
|
467
|
+
/**
|
|
468
|
+
* データの永続化先です。
|
|
469
|
+
*/
|
|
470
|
+
readonly storage: UniKvsStorage;
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* ストレージに対するパイプラインです。
|
|
474
|
+
* 登録順に保持します。
|
|
475
|
+
*/
|
|
476
|
+
readonly middlewares: readonly UniKvsMiddleware[];
|
|
477
|
+
}[];
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* 現在のメインのパイプラインです。
|
|
481
|
+
* 登録順に保持します。
|
|
482
|
+
*/
|
|
483
|
+
readonly #middlewares: UniKvsMiddleware[];
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* キーに対応する値のスキーマ情報を解決するリゾルバーです。
|
|
487
|
+
*/
|
|
488
|
+
readonly #valueSchemaResolver: IValueSchemaResolver | null;
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* 基本となる実行時変数です。
|
|
492
|
+
*
|
|
493
|
+
* 各操作の `vars` と結合して使用されます。
|
|
494
|
+
*/
|
|
495
|
+
readonly #vars: Map<string, unknown>;
|
|
496
|
+
|
|
497
|
+
public constructor(options?: Omit<UniKvsBuilderOptions, "schema"> & { readonly schema?: TInit });
|
|
498
|
+
public constructor(...args: unknown[]) {
|
|
499
|
+
const [options = {}] = parseInput(UniKvsBuilderArgsSchema, args);
|
|
500
|
+
const { vars, schema } = options;
|
|
501
|
+
|
|
502
|
+
this.#destinations = [];
|
|
503
|
+
this.#middlewares = [];
|
|
504
|
+
this.#valueSchemaResolver = null;
|
|
505
|
+
this.#vars = new Map(vars);
|
|
506
|
+
if (schema) {
|
|
507
|
+
this.#valueSchemaResolver = createValueSchemaResolver(schema);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
fork<
|
|
512
|
+
TBranchChunkInput,
|
|
513
|
+
TBranchChunkOutput,
|
|
514
|
+
TBranchHeadEncodeDataInput,
|
|
515
|
+
TBranchHeadDecodeDataOutput,
|
|
516
|
+
TBranchHeadEncodeChunkInput,
|
|
517
|
+
TBranchHeadDecodeChunkOutput,
|
|
518
|
+
TBranchLastDecodeDataInput,
|
|
519
|
+
TBranchLastEncodeDataOutput,
|
|
520
|
+
TBranchLastDecodeChunkInput,
|
|
521
|
+
TBranchLastEncodeChunkOutput,
|
|
522
|
+
TBranchHasPersist extends boolean,
|
|
523
|
+
>(
|
|
524
|
+
branch: IUniKvsBranch<
|
|
525
|
+
any,
|
|
526
|
+
TBranchChunkInput,
|
|
527
|
+
TBranchChunkOutput,
|
|
528
|
+
TBranchHeadEncodeDataInput,
|
|
529
|
+
TBranchHeadDecodeDataOutput,
|
|
530
|
+
TBranchHeadEncodeChunkInput,
|
|
531
|
+
TBranchHeadDecodeChunkOutput,
|
|
532
|
+
TBranchLastDecodeDataInput,
|
|
533
|
+
TBranchLastEncodeDataOutput,
|
|
534
|
+
TBranchLastDecodeChunkInput,
|
|
535
|
+
TBranchLastEncodeChunkOutput,
|
|
536
|
+
TBranchHasPersist
|
|
537
|
+
> &
|
|
538
|
+
(TBranchHasPersist extends true
|
|
539
|
+
? $CheckForkEncodeData<TLastMiddlewareEncodeDataOutput, TBranchHeadEncodeDataInput>
|
|
540
|
+
: unknown) &
|
|
541
|
+
(TBranchHasPersist extends true
|
|
542
|
+
? $CheckForkDecodeData<TLastMiddlewareDecodeDataInput, TBranchHeadDecodeDataOutput>
|
|
543
|
+
: unknown) &
|
|
544
|
+
(TBranchHasPersist extends true
|
|
545
|
+
? $CheckForkEncodeChunk<TLastMiddlewareEncodeChunkOutput, TBranchHeadEncodeChunkInput>
|
|
546
|
+
: unknown) &
|
|
547
|
+
(TBranchHasPersist extends true
|
|
548
|
+
? $CheckForkDecodeChunk<TLastMiddlewareDecodeChunkInput, TBranchHeadDecodeChunkOutput>
|
|
549
|
+
: unknown),
|
|
550
|
+
): IUniKvsBuilder<
|
|
551
|
+
TKeyValueMapping,
|
|
552
|
+
TUniKvsDataInput,
|
|
553
|
+
TUniKvsDataOutput,
|
|
554
|
+
TBranchHasPersist extends true
|
|
555
|
+
? [TUniKvsChunkInput] extends [never]
|
|
556
|
+
? never
|
|
557
|
+
: [TBranchChunkInput] extends [never]
|
|
558
|
+
? never
|
|
559
|
+
: TUniKvsChunkInput
|
|
560
|
+
: TUniKvsChunkInput,
|
|
561
|
+
TBranchHasPersist extends true
|
|
562
|
+
? [TUniKvsChunkOutput] extends [never]
|
|
563
|
+
? never
|
|
564
|
+
: [TBranchChunkOutput] extends [never]
|
|
565
|
+
? never
|
|
566
|
+
: TUniKvsChunkOutput
|
|
567
|
+
: TUniKvsChunkOutput,
|
|
568
|
+
TLastMiddlewareDecodeDataInput,
|
|
569
|
+
TLastMiddlewareEncodeDataOutput,
|
|
570
|
+
TLastMiddlewareDecodeChunkInput,
|
|
571
|
+
TLastMiddlewareEncodeChunkOutput
|
|
572
|
+
>;
|
|
573
|
+
public fork(branch: IUniKvsBranch): IUniKvsBuilder {
|
|
574
|
+
appendForkDestinations(this.#destinations, this.#middlewares, branch);
|
|
575
|
+
|
|
576
|
+
return this;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
use<
|
|
580
|
+
TMiddleware extends IMiddleware<
|
|
581
|
+
[TLastMiddlewareEncodeDataOutput] extends [never] ? any : TLastMiddlewareEncodeDataOutput,
|
|
582
|
+
any,
|
|
583
|
+
[TLastMiddlewareEncodeChunkOutput] extends [never] ? any : TLastMiddlewareEncodeChunkOutput
|
|
584
|
+
>,
|
|
585
|
+
>(
|
|
586
|
+
middleware: TMiddleware,
|
|
587
|
+
): IUniKvsBuilder<
|
|
588
|
+
TKeyValueMapping,
|
|
589
|
+
TUniKvsDataInput,
|
|
590
|
+
TUniKvsDataOutput,
|
|
591
|
+
[TUniKvsChunkInput] extends [never]
|
|
592
|
+
? never
|
|
593
|
+
: [$InferDecodeChunkInput<TMiddleware>] extends [never]
|
|
594
|
+
? never
|
|
595
|
+
: TUniKvsChunkInput,
|
|
596
|
+
[TUniKvsChunkOutput] extends [never]
|
|
597
|
+
? never
|
|
598
|
+
: [$InferEncodeChunkOutput<TMiddleware>] extends [never]
|
|
599
|
+
? never
|
|
600
|
+
: TUniKvsChunkOutput,
|
|
601
|
+
$InferDecodeDataInput<TMiddleware>,
|
|
602
|
+
$InferEncodeDataOutput<TMiddleware>,
|
|
603
|
+
$InferDecodeChunkInput<TMiddleware>,
|
|
604
|
+
$InferEncodeChunkOutput<TMiddleware>
|
|
605
|
+
>;
|
|
606
|
+
public use(middleware: IMiddleware): IUniKvsBuilder {
|
|
607
|
+
this.#middlewares.push(UniKvsMiddleware.from(middleware));
|
|
608
|
+
|
|
609
|
+
return this;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
persist<
|
|
613
|
+
TStorage extends IStorage<
|
|
614
|
+
[TLastMiddlewareEncodeDataOutput] extends [never] ? any : TLastMiddlewareEncodeDataOutput,
|
|
615
|
+
[TLastMiddlewareDecodeDataInput] extends [never] ? any : TLastMiddlewareDecodeDataInput,
|
|
616
|
+
[TLastMiddlewareEncodeChunkOutput] extends [never] ? any : TLastMiddlewareEncodeChunkOutput,
|
|
617
|
+
[TLastMiddlewareDecodeChunkInput] extends [never] ? any : TLastMiddlewareDecodeChunkInput
|
|
618
|
+
>,
|
|
619
|
+
>(
|
|
620
|
+
storage: TStorage,
|
|
621
|
+
): IUniKvsBuilderFinalizer<
|
|
622
|
+
TKeyValueMapping,
|
|
623
|
+
TLastMiddlewareEncodeDataOutput,
|
|
624
|
+
TLastMiddlewareDecodeDataInput,
|
|
625
|
+
[TLastMiddlewareEncodeChunkOutput] extends [never]
|
|
626
|
+
? never
|
|
627
|
+
: [$InferWriteChunkInput<TStorage>] extends [never]
|
|
628
|
+
? never
|
|
629
|
+
: TLastMiddlewareEncodeChunkOutput,
|
|
630
|
+
[TLastMiddlewareDecodeChunkInput] extends [never]
|
|
631
|
+
? never
|
|
632
|
+
: [$InferReadChunkOutput<TStorage>] extends [never]
|
|
633
|
+
? never
|
|
634
|
+
: TLastMiddlewareDecodeChunkInput,
|
|
635
|
+
TLastMiddlewareDecodeDataInput,
|
|
636
|
+
TLastMiddlewareEncodeDataOutput,
|
|
637
|
+
TLastMiddlewareDecodeChunkInput,
|
|
638
|
+
TLastMiddlewareEncodeChunkOutput
|
|
639
|
+
>;
|
|
640
|
+
public persist(storage: IStorage): IUniKvsBuilder {
|
|
641
|
+
this.#destinations.push({
|
|
642
|
+
storage: UniKvsStorage.from(storage),
|
|
643
|
+
// 登録時点のチェーンをスナップショットして保持するため、以降の use はこの登録に影響しません。
|
|
644
|
+
middlewares: [...this.#middlewares],
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
return this;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
public setVars(vars: VariablesInit): this;
|
|
651
|
+
public setVars(...args: unknown[]): this {
|
|
652
|
+
const [vars] = parseInput(VariablesArgsSchema, args);
|
|
653
|
+
|
|
654
|
+
this.#vars.clear();
|
|
655
|
+
for (const [name, value] of vars) {
|
|
656
|
+
this.#vars.set(name, value);
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
return this;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
public assignVars(vars: VariablesInit): this;
|
|
663
|
+
public assignVars(...args: unknown[]): this {
|
|
664
|
+
const [vars] = parseInput(VariablesArgsSchema, args);
|
|
665
|
+
|
|
666
|
+
const target = this.#vars;
|
|
667
|
+
const source = vars;
|
|
668
|
+
|
|
669
|
+
for (const [name, value] of source) {
|
|
670
|
+
target.set(name, value);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
return this;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
public defaultsVars(vars: VariablesInit): this;
|
|
677
|
+
public defaultsVars(...args: unknown[]): this {
|
|
678
|
+
const [vars] = parseInput(VariablesArgsSchema, args);
|
|
679
|
+
|
|
680
|
+
const target = new Map(vars);
|
|
681
|
+
const source = this.#vars;
|
|
682
|
+
|
|
683
|
+
for (const [name, value] of source) {
|
|
684
|
+
target.set(name, value);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
this.setVars(target);
|
|
688
|
+
|
|
689
|
+
return this;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
public build(): UniKvs<TKeyValueMapping> {
|
|
693
|
+
// ストレージが空の場合は UniKvs として機能できないため、例外を投げます。
|
|
694
|
+
if (this.#destinations.length === 0) {
|
|
695
|
+
throw new MissingStorageError();
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
return new UniKvs(
|
|
699
|
+
[...this.#vars.entries()],
|
|
700
|
+
// ビルダーを再利用できるよう、build のたびに trie をコンパイルします。コンパイル後の登録内容は変更しません。
|
|
701
|
+
// 同じミドルウェアインスタンスに対するラッパーは共有します。
|
|
702
|
+
PipelineGraph.compile([...this.#destinations]),
|
|
703
|
+
this.#valueSchemaResolver,
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
}
|