unikvs 0.0.12 → 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 (82) hide show
  1. package/README.md +2 -2
  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 +34 -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 +169 -115
  16. package/src/_streams.schemas.ts +21 -0
  17. package/src/_streams.ts +32 -6
  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 -6
  23. package/src/_value-schemas.ts +36 -206
  24. package/src/_value-validation.ts +57 -0
  25. package/src/errors.ts +391 -148
  26. package/src/index.ts +46 -25
  27. package/src/{value-stream.types.ts → types.ts} +1 -1
  28. package/src/unikvs-branch.ts +445 -0
  29. package/src/unikvs-builder.ts +706 -0
  30. package/src/unikvs.ts +1423 -1007
  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-variables.d.ts +0 -13
  37. package/dist/src/_merge-variables.d.ts.map +0 -1
  38. package/dist/src/_merge-variables.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 -103
  42. package/dist/src/_streams.d.ts +0 -20
  43. package/dist/src/_streams.d.ts.map +0 -1
  44. package/dist/src/_streams.js +0 -36
  45. package/dist/src/_to-value-stream.d.ts +0 -12
  46. package/dist/src/_to-value-stream.d.ts.map +0 -1
  47. package/dist/src/_to-value-stream.js +0 -102
  48. package/dist/src/_transformer.d.ts +0 -13
  49. package/dist/src/_transformer.d.ts.map +0 -1
  50. package/dist/src/_transformer.js +0 -60
  51. package/dist/src/_valibot.d.ts +0 -15
  52. package/dist/src/_valibot.d.ts.map +0 -1
  53. package/dist/src/_valibot.js +0 -23
  54. package/dist/src/_value-schemas.d.ts +0 -179
  55. package/dist/src/_value-schemas.d.ts.map +0 -1
  56. package/dist/src/_value-schemas.js +0 -150
  57. package/dist/src/errors.d.ts +0 -306
  58. package/dist/src/errors.d.ts.map +0 -1
  59. package/dist/src/errors.js +0 -234
  60. package/dist/src/index.d.ts +0 -10
  61. package/dist/src/index.d.ts.map +0 -1
  62. package/dist/src/index.js +0 -3
  63. package/dist/src/unikvs-config.d.ts +0 -174
  64. package/dist/src/unikvs-config.d.ts.map +0 -1
  65. package/dist/src/unikvs-config.js +0 -100
  66. package/dist/src/unikvs.d.ts +0 -272
  67. package/dist/src/unikvs.d.ts.map +0 -1
  68. package/dist/src/unikvs.js +0 -928
  69. package/dist/src/utils.types.d.ts +0 -5
  70. package/dist/src/utils.types.d.ts.map +0 -1
  71. package/dist/src/utils.types.js +0 -1
  72. package/dist/src/value-stream.types.d.ts +0 -10
  73. package/dist/src/value-stream.types.d.ts.map +0 -1
  74. package/dist/src/value-stream.types.js +0 -1
  75. package/dist/src/variables.types.d.ts +0 -11
  76. package/dist/src/variables.types.d.ts.map +0 -1
  77. package/dist/src/variables.types.js +0 -1
  78. package/src/_merge-variables.ts +0 -19
  79. package/src/_transformer.ts +0 -89
  80. package/src/unikvs-config.ts +0 -401
  81. package/src/utils.types.ts +0 -4
  82. package/src/variables.types.ts +0 -13
@@ -1,55 +1,13 @@
1
- import * as v from "./_valibot.js";
1
+ import type { GenericSchema } from "./_valibot.js";
2
+ import { union, safeParse } from "./_valibot.js";
2
3
  import { InvalidInputError } from "./errors.js";
4
+ import type { PlainValue, StreamValue } from "./value-schemas.js";
3
5
 
4
- // -------------------------------------------------------------------------------------------------
6
+ // -------------------------------------------------------------------------------------------------。
5
7
  //
6
- // 値の型
8
+ // 値の型。
7
9
  //
8
- // -------------------------------------------------------------------------------------------------
9
-
10
- /**
11
- * プレーンな値を識別するための固有のシンボルです。
12
- */
13
- declare const PLAIN_VALUE: unique symbol;
14
-
15
- /**
16
- * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
17
- *
18
- * @template TData 入出力されるデータの型です。
19
- * @template TInput `set` が受け付ける入力データの型です。
20
- */
21
- export type PlainValue<TData = any, TInput = TData> = [
22
- Type: typeof PLAIN_VALUE,
23
- Data: TData,
24
- Input: TInput,
25
- ];
26
-
27
- /**
28
- * ストリーム形式の値を識別するための固有のシンボルです。
29
- */
30
- declare const STREAM_VALUE: unique symbol;
31
-
32
- /**
33
- * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
34
- *
35
- * @template TChunkData 入出力されるチャンクデータの型です。
36
- * @template TChunkInput `set` が受け付ける入力チャンクデータの型です。
37
- */
38
- export type StreamValue<TChunkData = any, TChunkInput = TChunkData> = [
39
- Type: typeof STREAM_VALUE,
40
- ChunkData: TChunkData,
41
- ChunkInput: TChunkInput,
42
- ];
43
-
44
- /**
45
- * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
46
- *
47
- * @template TDataAndChunkData データまたはチャンクデータの型です。
48
- * @template TInput `set` が受け付ける入力データまたは入力チャンクデータの型です。
49
- */
50
- export type Value<TDataAndChunkData = any, TInput = TDataAndChunkData> =
51
- | PlainValue<TDataAndChunkData, TInput>
52
- | StreamValue<TDataAndChunkData, TInput>;
10
+ // -------------------------------------------------------------------------------------------------。
53
11
 
54
12
  /**
55
13
  * PlainValue 型から内部のデータ型を抽出します。
@@ -83,18 +41,14 @@ export type $InferStreamValueChunkData<TStreamValue> =
83
41
  export type $InferStreamValueChunkInput<TStreamValue> =
84
42
  TStreamValue extends StreamValue<any, infer TChunkInput> ? TChunkInput : never;
85
43
 
86
- // -------------------------------------------------------------------------------------------------
87
- //
88
- // 値のスキーマ
89
- //
90
- // -------------------------------------------------------------------------------------------------
91
-
92
44
  /**
93
45
  * スキーマ付きの値であることを識別するためのブランドです。
94
46
  *
95
47
  * 同じライブラリーが複数読み込まれた環境でも認識できるようにグローバルシンボルを使用します。
96
48
  */
97
- const VALUE_SCHEMA_BRAND: unique symbol = Symbol.for("unikvs.valueSchema");
49
+ export const VALUE_SCHEMA_BRAND: unique symbol = /* #__PURE__ */ Symbol.for(
50
+ "unikvs:VALUE_SCHEMA_BRAND",
51
+ );
98
52
 
99
53
  /**
100
54
  * スキーマ付きの値が扱うデータの種類です。
@@ -117,168 +71,35 @@ export type ValueSchemaInfo = {
117
71
  /**
118
72
  * データの検証に使用するスキーマです。
119
73
  */
120
- readonly schema: v.GenericSchema;
121
- };
122
-
123
- /**
124
- * スキーマ付きの値を表す内部オブジェクトです。
125
- */
126
- type ValueSchemaDescriptor = ValueSchemaInfo & {
127
- readonly [VALUE_SCHEMA_BRAND]: true;
74
+ readonly schema: GenericSchema;
128
75
  };
129
76
 
130
- /**
131
- * 値が Valibot のスキーマであるかどうかを判定します。
132
- */
133
- function isGenericSchema(input: unknown): input is v.GenericSchema {
134
- return (
135
- typeof input === "object" &&
136
- input !== null &&
137
- typeof (input as { readonly ["~run"]?: unknown })["~run"] === "function"
138
- );
139
- }
140
-
141
- /**
142
- * 値がスキーマ情報であるかどうかを判定します。
143
- */
144
- function isValueSchemaInfo(input: unknown): input is ValueSchemaInfo {
145
- return (
146
- typeof input === "object" &&
147
- input !== null &&
148
- (input as { readonly [VALUE_SCHEMA_BRAND]?: unknown })[VALUE_SCHEMA_BRAND] === true
149
- );
150
- }
151
-
152
- /**
153
- * スキーマ情報を内部オブジェクトとして生成します。
154
- */
155
- function createValueSchemaDescriptor(
156
- kind: ValueSchemaKind,
157
- schema: v.GenericSchema,
158
- ): ValueSchemaDescriptor {
159
- return { [VALUE_SCHEMA_BRAND]: true, kind, schema };
160
- }
161
-
162
- /**
163
- * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
164
- *
165
- * プレーンな値のスキーマを定義します。
166
- *
167
- * @template TSchema スキーマの型です。
168
- * @param schema 値の検証に使用する Valibot スキーマです。
169
- * @returns プレーンな値であることを示す型情報を返します。
170
- */
171
- export function PlainValue<const TSchema extends v.GenericSchema>(
172
- schema: TSchema,
173
- ): PlainValue<v.InferOutput<TSchema>, v.InferInput<TSchema>> {
174
- return createValueSchemaDescriptor("plain", schema) as unknown as PlainValue<
175
- v.InferOutput<TSchema>,
176
- v.InferInput<TSchema>
177
- >;
178
- }
179
-
180
- /**
181
- * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
182
- *
183
- * ストリーム形式の値のスキーマを定義します。
184
- *
185
- * @template TSchema スキーマの型です。
186
- * @param schema チャンクデータの検証に使用する Valibot スキーマです。
187
- * @returns ストリーム形式の値であることを示す型情報を返します。
188
- */
189
- export function StreamValue<const TSchema extends v.GenericSchema>(
190
- schema: TSchema,
191
- ): StreamValue<v.InferOutput<TSchema>, v.InferInput<TSchema>> {
192
- return createValueSchemaDescriptor("stream", schema) as unknown as StreamValue<
193
- v.InferOutput<TSchema>,
194
- v.InferInput<TSchema>
195
- >;
196
- }
197
-
198
- /**
199
- * [API Reference](https://tai-kun.github.io/unikvs/packages/unikvs#value-types)
200
- *
201
- * プレーンな値とストリーム形式の値の両方で使えるスキーマを定義します。
202
- *
203
- * @template TSchema スキーマの型です。
204
- * @param schema データとチャンクデータの検証に使用する Valibot スキーマです。
205
- * @returns どちらの形式でも使える値であることを示す型情報を返します。
206
- */
207
- export function Value<const TSchema extends v.GenericSchema>(
208
- schema: TSchema,
209
- ): Value<v.InferOutput<TSchema>, v.InferInput<TSchema>> {
210
- return createValueSchemaDescriptor("any", schema) as unknown as Value<
211
- v.InferOutput<TSchema>,
212
- v.InferInput<TSchema>
213
- >;
214
- }
215
-
216
- // -------------------------------------------------------------------------------------------------
77
+ // -------------------------------------------------------------------------------------------------。
217
78
  //
218
- // スキーマ定義
79
+ // スキーマ定義。
219
80
  //
220
- // -------------------------------------------------------------------------------------------------
221
-
222
- /**
223
- * オブジェクト形式のスキーマ定義です。キーをそのままキーとして扱います。
224
- */
225
- export type UniKvsSchemaRecord = { readonly [key: string]: Value<any, any> };
226
-
227
- /**
228
- * 配列形式のスキーマ定義です。キーのスキーマと値のスキーマの組を並べます。
229
- */
230
- export type UniKvsSchemaEntries = readonly (readonly [v.GenericSchema, Value<any, any>])[];
231
-
232
- /**
233
- * `UniKvs.config()` の `schema` オプションに指定できるスキーマ定義です。
234
- */
235
- export type UniKvsSchema = UniKvsSchemaRecord | UniKvsSchemaEntries;
236
-
237
- /**
238
- * スキーマ定義からキーと値のマッピング型を推論します。
239
- *
240
- * @template TSchema 対象となるスキーマ定義の型です。
241
- */
242
- export type $InferKeyValueMapping<TSchema extends UniKvsSchema> =
243
- TSchema extends readonly (readonly [v.GenericSchema, infer TValue extends Value<any, any>])[]
244
- ? { readonly [key: string]: TValue }
245
- : TSchema extends { readonly [key: string]: Value<any, any> }
246
- ? { readonly [TKey in keyof TSchema]: TSchema[TKey] }
247
- : never;
81
+ // -------------------------------------------------------------------------------------------------。
248
82
 
249
83
  /**
250
84
  * 検証済みのオブジェクト形式のスキーマ定義です。
251
85
  */
252
- export type UniKvsSchemaInfoRecord = { readonly [key: string]: ValueSchemaInfo };
86
+ type UniKvsSchemaInfoRecord = Readonly<Record<string, ValueSchemaInfo>>;
253
87
 
254
88
  /**
255
89
  * 検証済みの配列形式のスキーマ定義です。
256
90
  */
257
- export type UniKvsSchemaInfoEntries = readonly (readonly [v.GenericSchema, ValueSchemaInfo])[];
91
+ type UniKvsSchemaInfoEntries = readonly (readonly [GenericSchema, ValueSchemaInfo])[];
258
92
 
259
93
  /**
260
94
  * 検証済みのスキーマ定義です。
261
95
  */
262
- export type UniKvsSchemaInfo = UniKvsSchemaInfoRecord | UniKvsSchemaInfoEntries;
96
+ type UniKvsSchemaInfo = UniKvsSchemaInfoRecord | UniKvsSchemaInfoEntries;
263
97
 
264
- /**
265
- * `schema` オプションの検証に使用するスキーマです。
266
- */
267
- export const UniKvsSchemaSchema = v.union([
268
- v.array(
269
- v.tuple([
270
- v.custom<v.GenericSchema>(isGenericSchema),
271
- v.custom<ValueSchemaInfo>(isValueSchemaInfo),
272
- ]),
273
- ),
274
- v.record(v.string(), v.custom<ValueSchemaInfo>(isValueSchemaInfo)),
275
- ]);
276
-
277
- // -------------------------------------------------------------------------------------------------
98
+ // -------------------------------------------------------------------------------------------------。
278
99
  //
279
- // スキーマリゾルバー
100
+ // スキーマリゾルバー。
280
101
  //
281
- // -------------------------------------------------------------------------------------------------
102
+ // -------------------------------------------------------------------------------------------------。
282
103
 
283
104
  /**
284
105
  * キーに対応する値のスキーマ情報を解決するインターフェースです。
@@ -288,7 +109,9 @@ export interface IValueSchemaResolver {
288
109
  * キーに対応する値のスキーマ情報を返します。
289
110
  *
290
111
  * @param key 解決するキーです。
112
+ *
291
113
  * @returns 対応するスキーマ情報を返します。対応する定義がない場合は `undefined` を返します。
114
+ *
292
115
  * @throws キーがどのキースキーマにも一致しない場合に `InvalidInputError` を投げます。
293
116
  */
294
117
  resolve(key: string): ValueSchemaInfo | undefined;
@@ -317,24 +140,30 @@ class RecordValueSchemaResolver implements IValueSchemaResolver {
317
140
  class EntriesValueSchemaResolver implements IValueSchemaResolver {
318
141
  readonly #entries: UniKvsSchemaInfoEntries;
319
142
 
320
- readonly #keySchema: v.GenericSchema | undefined;
143
+ readonly #keySchema: GenericSchema | undefined;
321
144
 
322
145
  public constructor(entries: UniKvsSchemaInfoEntries) {
323
146
  this.#entries = entries;
324
- this.#keySchema =
325
- entries.length > 0 ? v.union(entries.map(([keySchema]) => keySchema)) : undefined;
147
+ if (entries.length > 0) {
148
+ this.#keySchema = union(entries.map(([keySchema]) => keySchema));
149
+ }
326
150
  }
327
151
 
328
152
  public resolve(key: string): ValueSchemaInfo | undefined {
153
+ let resolved: ValueSchemaInfo | undefined;
154
+
329
155
  for (const [keySchema, valueSchema] of this.#entries) {
330
- const result = v.safeParse(keySchema, key);
156
+ const result = safeParse(keySchema, key);
157
+
331
158
  if (result.success) {
332
- return valueSchema;
159
+ resolved = valueSchema;
160
+ break;
333
161
  }
334
162
  }
335
163
 
336
- if (this.#keySchema) {
337
- const result = v.safeParse(this.#keySchema, key);
164
+ if (resolved === undefined && this.#keySchema) {
165
+ const result = safeParse(this.#keySchema, key);
166
+
338
167
  if (!result.success) {
339
168
  throw new InvalidInputError({
340
169
  value: key,
@@ -343,7 +172,7 @@ class EntriesValueSchemaResolver implements IValueSchemaResolver {
343
172
  }
344
173
  }
345
174
 
346
- return undefined;
175
+ return resolved;
347
176
  }
348
177
  }
349
178
 
@@ -351,6 +180,7 @@ class EntriesValueSchemaResolver implements IValueSchemaResolver {
351
180
  * 検証済みのスキーマ定義からスキーマリゾルバーを生成します。
352
181
  *
353
182
  * @param schema 検証済みのスキーマ定義です。
183
+ *
354
184
  * @returns スキーマリゾルバーを返します。
355
185
  */
356
186
  export function createValueSchemaResolver(schema: UniKvsSchemaInfo): IValueSchemaResolver {
@@ -0,0 +1,57 @@
1
+ import { isReadableStream } from "./_streams.js";
2
+ import { pipe, check, unknown, parseInput } from "./_valibot.js";
3
+ import type { ValueSchemaInfo } from "./_value-schemas.js";
4
+
5
+ /**
6
+ * プレーン値専用のスキーマに ReadableStream が渡されたことを報告するためのスキーマです。
7
+ */
8
+ const PlainValueInputSchema = pipe(
9
+ unknown(),
10
+ check(
11
+ (input) => !isReadableStream(input),
12
+ "Expected a plain value, but received a ReadableStream",
13
+ ),
14
+ );
15
+
16
+ /**
17
+ * ストリーム専用のスキーマにプレーン値が渡されたことを報告するためのスキーマです。
18
+ */
19
+ const StreamValueInputSchema = pipe(
20
+ unknown(),
21
+ check(isReadableStream, "Expected a ReadableStream, but received a plain value"),
22
+ );
23
+
24
+ /**
25
+ * スキーマ情報に基づいて `set` の入力値を検証します。
26
+ *
27
+ * プレーン値はその場で検証し、ストリームはチャンクごとに検証するストリームに変換します。
28
+ *
29
+ * @param info キーに対応するスキーマ情報です。
30
+ * @param value 検証する入力値です。
31
+ *
32
+ * @returns 検証済みの入力値を返します。
33
+ */
34
+ export function parseSetValue(info: ValueSchemaInfo, value: unknown): unknown {
35
+ if (isReadableStream(value)) {
36
+ if (info.kind === "plain") {
37
+ // 必ず検証に失敗するため、InvalidInputError が投げられます。
38
+ parseInput(PlainValueInputSchema, value);
39
+ }
40
+
41
+ // 入力チャンクを検証し、変換後の値を後続のパイプラインへ流します。
42
+ return value.pipeThrough(
43
+ new TransformStream({
44
+ transform(chunk, controller) {
45
+ controller.enqueue(parseInput(info.schema, chunk));
46
+ },
47
+ }),
48
+ );
49
+ }
50
+
51
+ if (info.kind === "stream") {
52
+ // 必ず検証に失敗するため、InvalidInputError が投げられます。
53
+ parseInput(StreamValueInputSchema, value);
54
+ }
55
+
56
+ return parseInput(info.schema, value);
57
+ }