@tstdl/base 0.91.12 → 0.91.14

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 (64) hide show
  1. package/package.json +4 -4
  2. package/pdf/pdf.service.js +1 -1
  3. package/process/spawn.d.ts +15 -0
  4. package/process/spawn.js +55 -0
  5. package/schema/schema.d.ts +1 -0
  6. package/schema/schema.error.d.ts +1 -0
  7. package/schema/schema.error.js +2 -0
  8. package/schema/schemas/any.d.ts +1 -0
  9. package/schema/schemas/any.js +1 -0
  10. package/schema/schemas/array.d.ts +1 -0
  11. package/schema/schemas/array.js +2 -0
  12. package/schema/schemas/bigint.d.ts +1 -0
  13. package/schema/schemas/bigint.js +1 -0
  14. package/schema/schemas/boolean.d.ts +1 -0
  15. package/schema/schemas/boolean.js +1 -0
  16. package/schema/schemas/date.d.ts +1 -0
  17. package/schema/schemas/date.js +1 -0
  18. package/schema/schemas/defaulted.d.ts +1 -0
  19. package/schema/schemas/defaulted.js +2 -0
  20. package/schema/schemas/enumeration.d.ts +1 -0
  21. package/schema/schemas/enumeration.js +2 -0
  22. package/schema/schemas/function.d.ts +1 -0
  23. package/schema/schemas/function.js +1 -0
  24. package/schema/schemas/instance.d.ts +1 -0
  25. package/schema/schemas/instance.js +2 -0
  26. package/schema/schemas/literal.d.ts +1 -0
  27. package/schema/schemas/literal.js +2 -0
  28. package/schema/schemas/never.d.ts +1 -0
  29. package/schema/schemas/never.js +1 -0
  30. package/schema/schemas/nullable.d.ts +1 -0
  31. package/schema/schemas/nullable.js +2 -0
  32. package/schema/schemas/number.d.ts +1 -0
  33. package/schema/schemas/number.js +1 -0
  34. package/schema/schemas/object.d.ts +2 -0
  35. package/schema/schemas/object.js +4 -0
  36. package/schema/schemas/one-or-many.d.ts +1 -0
  37. package/schema/schemas/one-or-many.js +2 -0
  38. package/schema/schemas/optional.d.ts +1 -0
  39. package/schema/schemas/optional.js +2 -0
  40. package/schema/schemas/readable-stream.d.ts +1 -0
  41. package/schema/schemas/readable-stream.js +1 -0
  42. package/schema/schemas/regexp.d.ts +1 -0
  43. package/schema/schemas/regexp.js +1 -0
  44. package/schema/schemas/string.d.ts +1 -0
  45. package/schema/schemas/string.js +1 -0
  46. package/schema/schemas/symbol.d.ts +1 -0
  47. package/schema/schemas/symbol.js +1 -0
  48. package/schema/schemas/transform.d.ts +3 -2
  49. package/schema/schemas/transform.js +4 -1
  50. package/schema/schemas/uint8-array.d.ts +1 -0
  51. package/schema/schemas/uint8-array.js +1 -0
  52. package/schema/schemas/union.d.ts +1 -0
  53. package/schema/schemas/union.js +13 -4
  54. package/schema/schemas/unknown.d.ts +1 -0
  55. package/schema/schemas/unknown.js +1 -0
  56. package/schema/testable.js +6 -0
  57. package/utils/encoding.d.ts +2 -2
  58. package/utils/encoding.js +4 -4
  59. package/utils/stream/index.d.ts +1 -0
  60. package/utils/stream/index.js +1 -0
  61. package/utils/stream/stream-reader.d.ts +1 -1
  62. package/utils/stream/stream-reader.js +2 -1
  63. package/utils/stream/to-readable-stream.d.ts +1 -0
  64. package/utils/stream/to-readable-stream.js +8 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.91.12",
3
+ "version": "0.91.14",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -111,7 +111,7 @@
111
111
  "luxon": "^3.5",
112
112
  "reflect-metadata": "^0.2",
113
113
  "rxjs": "^7.8",
114
- "type-fest": "4.24"
114
+ "type-fest": "4.26"
115
115
  },
116
116
  "devDependencies": {
117
117
  "@mxssfd/typedoc-theme": "1.1",
@@ -134,8 +134,8 @@
134
134
  "typescript": "5.5"
135
135
  },
136
136
  "peerDependencies": {
137
- "@elastic/elasticsearch": "^8.14",
138
- "@koa/router": "^12.0",
137
+ "@elastic/elasticsearch": "^8.15",
138
+ "@koa/router": "^13.0",
139
139
  "@tstdl/angular": "^0.91",
140
140
  "@zxcvbn-ts/core": "^3.0",
141
141
  "@zxcvbn-ts/language-common": "^3.0",
@@ -61,8 +61,8 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
61
61
  });
62
62
  import { execFile as execFileCallback } from 'node:child_process';
63
63
  import { access, readFile, unlink, writeFile } from 'node:fs/promises';
64
- import { promisify } from 'node:util';
65
64
  import { tmpdir } from 'node:os';
65
+ import { promisify } from 'node:util';
66
66
  import { BrowserContextController } from '../browser/browser-context-controller.js';
67
67
  import { BrowserController } from '../browser/browser-controller.js';
68
68
  import { PdfRenderOptions } from '../browser/pdf-options.js';
@@ -0,0 +1,15 @@
1
+ import type { ChildProcessWithoutNullStreams } from 'node:child_process';
2
+ export type SpawnCommandResult = TransformStream<Uint8Array, Uint8Array> & {
3
+ process: ChildProcessWithoutNullStreams;
4
+ stderr: ReadableStream<Uint8Array>;
5
+ write(chunk: ReadableStream<Uint8Array> | Uint8Array | string): Promise<void>;
6
+ readOutputBytes(): Promise<Uint8Array>;
7
+ readOutput(): Promise<string>;
8
+ readErrBytes(): Promise<Uint8Array>;
9
+ readErr(): Promise<string>;
10
+ wait(): Promise<{
11
+ code: number | null;
12
+ signal: string | null;
13
+ }>;
14
+ };
15
+ export declare function spawnCommand(command: string, args: string[]): Promise<SpawnCommandResult>;
@@ -0,0 +1,55 @@
1
+ import { dynamicImport } from '../import.js';
2
+ import { decodeTextStream, encodeUtf8Stream } from '../utils/encoding.js';
3
+ import { readBinaryStream, readTextStream } from '../utils/stream/stream-reader.js';
4
+ import { toReadableStream } from '../utils/stream/to-readable-stream.js';
5
+ import { isReadableStream, isString, isUint8Array } from '../utils/type-guards.js';
6
+ export async function spawnCommand(command, args) {
7
+ const { spawn } = await dynamicImport('node:child_process');
8
+ const { Readable, Writable } = await dynamicImport('node:stream');
9
+ const process = spawn(command, args, { stdio: 'pipe' });
10
+ await Promise.race([
11
+ new Promise((resolve) => process.on('spawn', resolve)),
12
+ new Promise((_, reject) => process.on('error', reject))
13
+ ]);
14
+ const readable = Readable.toWeb(process.stdout);
15
+ const writable = Writable.toWeb(process.stdin);
16
+ const stderr = Readable.toWeb(process.stderr);
17
+ async function write(data) {
18
+ if (isReadableStream(data)) {
19
+ await data.pipeTo(writable);
20
+ }
21
+ else if (isUint8Array(data)) {
22
+ await toReadableStream(data).pipeTo(writable);
23
+ }
24
+ else if (isString(data)) {
25
+ await toReadableStream(data).pipeThrough(encodeUtf8Stream()).pipeTo(writable);
26
+ }
27
+ }
28
+ async function readOutputBytes() {
29
+ return readBinaryStream(readable);
30
+ }
31
+ async function readOutput() {
32
+ return readTextStream(readable.pipeThrough(decodeTextStream()));
33
+ }
34
+ async function readErrBytes() {
35
+ return readBinaryStream(stderr);
36
+ }
37
+ async function readErr() {
38
+ return readTextStream(stderr.pipeThrough(decodeTextStream()));
39
+ }
40
+ const signalPromise = new Promise((resolve) => process.on('close', (code, signal) => resolve({ code, signal })));
41
+ return {
42
+ process,
43
+ readable,
44
+ writable,
45
+ stderr,
46
+ write,
47
+ readOutputBytes,
48
+ readOutput,
49
+ readErrBytes,
50
+ readErr,
51
+ async wait() {
52
+ return signalPromise;
53
+ },
54
+ };
55
+ }
@@ -23,6 +23,7 @@ export type SchemaTestable<T = unknown> = Schema<T> | AbstractConstructor<T> | N
23
23
  export type SchemaOutput<T extends SchemaTestable> = T extends SchemaTestable<infer U> ? U : never;
24
24
  export declare const OPTIONAL: unique symbol;
25
25
  export declare abstract class Schema<T = unknown> {
26
+ abstract readonly name: string;
26
27
  readonly [OPTIONAL]: boolean;
27
28
  /**
28
29
  * Test an unknown value to see whether it corresponds to the schema.
@@ -13,6 +13,7 @@ export declare class SchemaError extends CustomError implements ErrorExtraInfo {
13
13
  readonly path: string;
14
14
  readonly details?: UndefinableJson;
15
15
  readonly inner?: OneOrMany<SchemaError>;
16
+ readonly innerMessages?: string[];
16
17
  constructor(message: string, options: SchemaErrorOptions, cause?: any);
17
18
  static expectedButGot(expected: OneOrMany<string | AbstractConstructor>, got: string, path: string | JsonPath, options?: TypedOmit<SchemaErrorOptions, 'path'> & {
18
19
  customMessage?: string;
@@ -6,6 +6,7 @@ export class SchemaError extends CustomError {
6
6
  path;
7
7
  details;
8
8
  inner;
9
+ innerMessages;
9
10
  constructor(message, options, cause) {
10
11
  super({ message, cause: cause ?? options.cause, fast: options.fast });
11
12
  this.path = isString(options.path) ? options.path : options.path.path;
@@ -15,6 +16,7 @@ export class SchemaError extends CustomError {
15
16
  ? options.inner[0]
16
17
  : options.inner
17
18
  : options.inner;
19
+ this.innerMessages = toArray(options.inner).flatMap((inner) => [`${inner.path}: ${inner.message}`, ...(inner.innerMessages ?? [])]);
18
20
  }
19
21
  if (isNotNullOrUndefined(options.details)) {
20
22
  this.details = options.details;
@@ -1,6 +1,7 @@
1
1
  import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
2
2
  import { Schema, type SchemaTestResult } from '../schema.js';
3
3
  export declare class AnySchema extends Schema<any> {
4
+ readonly name = "any";
4
5
  _test(value: any): SchemaTestResult<any>;
5
6
  }
6
7
  export declare function any(): AnySchema;
@@ -1,6 +1,7 @@
1
1
  import { Property } from '../decorators/index.js';
2
2
  import { Schema } from '../schema.js';
3
3
  export class AnySchema extends Schema {
4
+ name = 'any';
4
5
  _test(value) {
5
6
  return { valid: true, value };
6
7
  }
@@ -3,6 +3,7 @@ import type { TypedOmit } from '../../types.js';
3
3
  import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
4
4
  import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
5
5
  export declare class ArraySchema<T> extends Schema<T[]> {
6
+ readonly name: string;
6
7
  readonly itemSchema: Schema<T>;
7
8
  constructor(itemSchema: SchemaTestable<T>);
8
9
  _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T[]>;
@@ -5,10 +5,12 @@ import { Property } from '../decorators/index.js';
5
5
  import { Schema } from '../schema.js';
6
6
  import { schemaTestableToSchema } from '../testable.js';
7
7
  export class ArraySchema extends Schema {
8
+ name;
8
9
  itemSchema;
9
10
  constructor(itemSchema) {
10
11
  super();
11
12
  this.itemSchema = schemaTestableToSchema(itemSchema);
13
+ this.name = `Array[${this.itemSchema.name}]`;
12
14
  }
13
15
  _test(value, path, options) {
14
16
  if (!isArray(value)) {
@@ -3,6 +3,7 @@ import { type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
3
3
  import type { SchemaPropertyDecorator } from '../decorators/types.js';
4
4
  import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
5
5
  export declare class BigIntSchema extends Schema<bigint> {
6
+ readonly name = "bigint";
6
7
  _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<bigint>;
7
8
  }
8
9
  export declare function bigint(): BigIntSchema;
@@ -4,6 +4,7 @@ import { typeOf } from '../../utils/type-of.js';
4
4
  import { Property } from '../decorators/index.js';
5
5
  import { Schema } from '../schema.js';
6
6
  export class BigIntSchema extends Schema {
7
+ name = 'bigint';
7
8
  _test(value, path, options) {
8
9
  if (isBigInt(value)) {
9
10
  return { valid: true, value };
@@ -4,6 +4,7 @@ import type { SchemaTestOptions, SchemaTestResult } from '../schema.js';
4
4
  import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
5
5
  export type BooleanSchemaOptions = SimpleSchemaOptions;
6
6
  export declare class BooleanSchema extends SimpleSchema<boolean> {
7
+ readonly name = "boolean";
7
8
  constructor(options?: BooleanSchemaOptions);
8
9
  _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<boolean>;
9
10
  }
@@ -4,6 +4,7 @@ import { typeOf } from '../../utils/type-of.js';
4
4
  import { Property } from '../decorators/index.js';
5
5
  import { SimpleSchema } from './simple.js';
6
6
  export class BooleanSchema extends SimpleSchema {
7
+ name = 'boolean';
7
8
  constructor(options) {
8
9
  super('boolean', isBoolean, options, {
9
10
  coercers: {
@@ -4,6 +4,7 @@ export type DateSchemaOptions = SimpleSchemaOptions & {
4
4
  integer?: boolean;
5
5
  };
6
6
  export declare class DateSchema extends SimpleSchema<globalThis.Date> {
7
+ readonly name = "date";
7
8
  constructor(options?: DateSchemaOptions);
8
9
  }
9
10
  export declare function date(options?: DateSchemaOptions): DateSchema;
@@ -3,6 +3,7 @@ import { Property } from '../decorators/index.js';
3
3
  import { SchemaError } from '../schema.error.js';
4
4
  import { SimpleSchema } from './simple.js';
5
5
  export class DateSchema extends SimpleSchema {
6
+ name = 'date';
6
7
  constructor(options) {
7
8
  super('date', isValidDate, options, {
8
9
  coercers: {
@@ -2,6 +2,7 @@ import type { JsonPath } from '../../json-path/json-path.js';
2
2
  import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
3
3
  import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
4
4
  export declare class DefaultSchema<T, D> extends Schema<T | D> {
5
+ readonly name: string;
5
6
  readonly schema: Schema<T>;
6
7
  readonly defaultValue: D;
7
8
  constructor(schema: SchemaTestable<T>, defaultValue: D);
@@ -3,12 +3,14 @@ import { Property } from '../decorators/index.js';
3
3
  import { Schema } from '../schema.js';
4
4
  import { schemaTestableToSchema } from '../testable.js';
5
5
  export class DefaultSchema extends Schema {
6
+ name;
6
7
  schema;
7
8
  defaultValue;
8
9
  constructor(schema, defaultValue) {
9
10
  super();
10
11
  this.schema = schemaTestableToSchema(schema);
11
12
  this.defaultValue = defaultValue;
13
+ this.name = `Defaulted[${this.schema.name}]`;
12
14
  }
13
15
  _test(value, path, options) {
14
16
  if (isNullOrUndefined(value)) {
@@ -4,6 +4,7 @@ import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
4
4
  export type EnumerationSchemaOptions = SimpleSchemaOptions;
5
5
  export declare class EnumerationSchema<T extends EnumerationType> extends SimpleSchema<EnumerationValue<T>> {
6
6
  #private;
7
+ readonly name: string;
7
8
  readonly enumeration: EnumerationType;
8
9
  constructor(enumeration: T, options?: EnumerationSchemaOptions);
9
10
  }
@@ -4,6 +4,7 @@ import { Property } from '../decorators/index.js';
4
4
  import { SimpleSchema } from './simple.js';
5
5
  export class EnumerationSchema extends SimpleSchema {
6
6
  #allowedValuesSet;
7
+ name;
7
8
  enumeration;
8
9
  constructor(enumeration, options) {
9
10
  const allowedValues = isArray(enumeration) ? enumeration : enumValues(enumeration);
@@ -17,6 +18,7 @@ export class EnumerationSchema extends SimpleSchema {
17
18
  });
18
19
  this.enumeration = enumeration;
19
20
  this.#allowedValuesSet = new Set(allowedValues);
21
+ this.name = `Enumeration[${allowedValuesString}]`;
20
22
  }
21
23
  }
22
24
  export function enumeration(enumeration, options) {
@@ -2,6 +2,7 @@ import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } fro
2
2
  import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
3
3
  export type FunctionSchemaOptions = SimpleSchemaOptions;
4
4
  export declare class FunctionSchema extends SimpleSchema<Function> {
5
+ readonly name = "function";
5
6
  constructor(options?: FunctionSchemaOptions);
6
7
  }
7
8
  export declare function func(): FunctionSchema;
@@ -2,6 +2,7 @@ import { isFunction } from '../../utils/type-guards.js';
2
2
  import { Property } from '../decorators/index.js';
3
3
  import { SimpleSchema } from './simple.js';
4
4
  export class FunctionSchema extends SimpleSchema {
5
+ name = 'function';
5
6
  constructor(options) {
6
7
  super('function', isFunction, options);
7
8
  }
@@ -3,6 +3,7 @@ import type { AbstractConstructor } from '../../types.js';
3
3
  import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
4
4
  import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
5
5
  export declare class InstanceSchema<T extends AbstractConstructor> extends Schema<InstanceType<T>> {
6
+ readonly name: string;
6
7
  readonly type: T;
7
8
  constructor(type: T);
8
9
  _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<InstanceType<T>>;
@@ -3,10 +3,12 @@ import { Property } from '../decorators/index.js';
3
3
  import { SchemaError } from '../schema.error.js';
4
4
  import { Schema } from '../schema.js';
5
5
  export class InstanceSchema extends Schema {
6
+ name;
6
7
  type;
7
8
  constructor(type) {
8
9
  super();
9
10
  this.type = type;
11
+ this.name = `Instance[${type.name}]`;
10
12
  }
11
13
  _test(value, path, options) {
12
14
  if (value instanceof this.type) {
@@ -2,6 +2,7 @@ import type { JsonPath } from '../../json-path/json-path.js';
2
2
  import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
3
3
  import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
4
4
  export declare class LiteralSchema<const T> extends Schema<T> {
5
+ readonly name: string;
5
6
  readonly value: T;
6
7
  constructor(value: T);
7
8
  _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T>;
@@ -4,10 +4,12 @@ import { Property } from '../decorators/index.js';
4
4
  import { SchemaError } from '../schema.error.js';
5
5
  import { Schema } from '../schema.js';
6
6
  export class LiteralSchema extends Schema {
7
+ name;
7
8
  value;
8
9
  constructor(value) {
9
10
  super();
10
11
  this.value = value;
12
+ this.name = `Literal[${String(value)}]`;
11
13
  }
12
14
  _test(value, path, options) {
13
15
  if (value === this.value) {
@@ -1,6 +1,7 @@
1
1
  import type { JsonPath } from '../../json-path/json-path.js';
2
2
  import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
3
3
  export declare class NeverSchema extends Schema<never> {
4
+ readonly name = "never";
4
5
  _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<never>;
5
6
  }
6
7
  export declare function never(): NeverSchema;
@@ -2,6 +2,7 @@ import { SchemaError } from '../../schema/schema.error.js';
2
2
  import { typeOf } from '../../utils/type-of.js';
3
3
  import { Schema } from '../schema.js';
4
4
  export class NeverSchema extends Schema {
5
+ name = 'never';
5
6
  _test(value, path, options) {
6
7
  return { valid: false, error: SchemaError.expectedButGot('never', typeOf(value), path, { fast: options.fastErrors }) };
7
8
  }
@@ -3,6 +3,7 @@ import type { TypedOmit } from '../../types.js';
3
3
  import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
4
4
  import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
5
5
  export declare class NullableSchema<T> extends Schema<T | null> {
6
+ readonly name: string;
6
7
  readonly schema: Schema<T>;
7
8
  constructor(schema: SchemaTestable<T>);
8
9
  _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T | null>;
@@ -3,6 +3,7 @@ import { createSchemaPropertyDecorator } from '../decorators/index.js';
3
3
  import { Schema } from '../schema.js';
4
4
  import { schemaTestableToSchema } from '../testable.js';
5
5
  export class NullableSchema extends Schema {
6
+ name;
6
7
  schema;
7
8
  constructor(schema) {
8
9
  if ((schema instanceof NullableSchema) && (schema == schema.schema)) {
@@ -10,6 +11,7 @@ export class NullableSchema extends Schema {
10
11
  }
11
12
  super();
12
13
  this.schema = schemaTestableToSchema(schema);
14
+ this.name = `Nullable[${this.schema.name}]`;
13
15
  }
14
16
  _test(value, path, options) {
15
17
  if (isNull(value)) {
@@ -4,6 +4,7 @@ export type NumberSchemaOptions = SimpleSchemaOptions & {
4
4
  integer?: boolean;
5
5
  };
6
6
  export declare class NumberSchema extends SimpleSchema<number> {
7
+ readonly name = "number";
7
8
  constructor(options?: NumberSchemaOptions);
8
9
  }
9
10
  export declare function number(options?: NumberSchemaOptions): NumberSchema;
@@ -3,6 +3,7 @@ import { Property } from '../decorators/index.js';
3
3
  import { SchemaError } from '../schema.error.js';
4
4
  import { SimpleSchema } from './simple.js';
5
5
  export class NumberSchema extends SimpleSchema {
6
+ name = 'number';
6
7
  constructor(options) {
7
8
  super('number', isNumber, options, {
8
9
  coercers: {
@@ -16,6 +16,7 @@ export type NormalizedObjectSchemaProperties<T extends Record> = {
16
16
  [P in keyof T]-?: Schema<T[P]>;
17
17
  };
18
18
  export type ObjectSchemaOptions<T extends Record = Record, K extends PropertyKey = PropertyKey, V = unknown> = {
19
+ name?: string;
19
20
  mask?: boolean | null;
20
21
  unknownProperties?: SchemaTestable<V> | null;
21
22
  unknownPropertiesKey?: SchemaTestable<K> | null;
@@ -34,6 +35,7 @@ export declare const tryGetSchemaFromReflection: typeof _tryGetSchemaFromReflect
34
35
  export declare class ObjectSchema<T extends Record = Record> extends Schema<T> {
35
36
  private readonly propertyKeys;
36
37
  private readonly allowUnknownProperties;
38
+ readonly name: string;
37
39
  readonly properties: NormalizedObjectSchemaProperties<T>;
38
40
  readonly mask: boolean | null;
39
41
  readonly unknownProperties: Schema | null;
@@ -16,6 +16,7 @@ export const tryGetSchemaFromReflection = memoizeSingle(_tryGetSchemaFromReflect
16
16
  export class ObjectSchema extends Schema {
17
17
  propertyKeys;
18
18
  allowUnknownProperties;
19
+ name;
19
20
  properties;
20
21
  mask;
21
22
  unknownProperties;
@@ -30,6 +31,7 @@ export class ObjectSchema extends Schema {
30
31
  this.factory = options.factory ?? null;
31
32
  this.allowUnknownProperties = isNotNull(this.unknownProperties) || isNotNull(this.unknownPropertiesKey);
32
33
  this.propertyKeys = new Set(objectKeys(properties));
34
+ this.name = options.name ?? 'Object';
33
35
  }
34
36
  _test(value, path, options) {
35
37
  if (!isObject(value)) {
@@ -82,6 +84,7 @@ export function record(key, value, options) {
82
84
  export function assign(...schemasOrTypes) {
83
85
  const schemas = schemasOrTypes.map(getObjectSchema);
84
86
  return object(schemas.reduce((result, schema) => ({ ...result, ...schema.properties }), {}), {
87
+ name: schemas.at(-1)?.name,
85
88
  mask: schemas.findLast((schema) => isNotNull(schema.mask))?.mask,
86
89
  unknownProperties: schemas.findLast((schema) => isNotNull(schema.unknownProperties))?.unknownProperties,
87
90
  unknownPropertiesKey: schemas.findLast((schema) => isNotNull(schema.unknownPropertiesKey))?.unknownPropertiesKey
@@ -137,6 +140,7 @@ function _tryGetSchemaFromReflection(type) {
137
140
  return schemaTestableToSchema(typeData.schema);
138
141
  }
139
142
  const schema = object(getObjectSchemaPropertiesFromReflection(metadata, type), {
143
+ name: type.name,
140
144
  factory: isDefined(typeData.factory) ? typeData.factory : { type: type },
141
145
  mask: typeData.mask,
142
146
  unknownProperties: typeData.unknownProperties,
@@ -4,6 +4,7 @@ import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } fro
4
4
  import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
5
5
  export type OneOrMany<T> = OneOrManyType<T>;
6
6
  export declare class OneOrManySchema<T> extends Schema<T | T[]> {
7
+ readonly name: string;
7
8
  readonly schema: Schema<T | T[]>;
8
9
  constructor(schema: SchemaTestable<T>);
9
10
  _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<T | T[]>;
@@ -4,11 +4,13 @@ import { schemaTestableToSchema } from '../testable.js';
4
4
  import { array } from './array.js';
5
5
  import { union } from './union.js';
6
6
  export class OneOrManySchema extends Schema {
7
+ name;
7
8
  schema;
8
9
  constructor(schema) {
9
10
  super();
10
11
  const oneSchema = schemaTestableToSchema(schema);
11
12
  this.schema = union(oneSchema, array(oneSchema));
13
+ this.name = `OneOrMany[${oneSchema.name}]`;
12
14
  }
13
15
  _test(value, path, options) {
14
16
  return this.schema._test(value, path, options);
@@ -3,6 +3,7 @@ import type { TypedOmit } from '../../types.js';
3
3
  import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
4
4
  import { type OPTIONAL, Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
5
5
  export declare class OptionalSchema<T> extends Schema<T | undefined> {
6
+ readonly name: string;
6
7
  readonly [OPTIONAL]: true;
7
8
  readonly schema: Schema<T>;
8
9
  constructor(schema: SchemaTestable<T>);
@@ -3,6 +3,7 @@ import { createSchemaPropertyDecorator } from '../decorators/index.js';
3
3
  import { Schema } from '../schema.js';
4
4
  import { schemaTestableToSchema } from '../testable.js';
5
5
  export class OptionalSchema extends Schema {
6
+ name;
6
7
  schema;
7
8
  constructor(schema) {
8
9
  if ((schema instanceof OptionalSchema) && (schema == schema.schema)) {
@@ -10,6 +11,7 @@ export class OptionalSchema extends Schema {
10
11
  }
11
12
  super();
12
13
  this.schema = schemaTestableToSchema(schema);
14
+ this.name = `Optional[${this.schema.name}]`;
13
15
  }
14
16
  _test(value, path, options) {
15
17
  if (isUndefined(value)) {
@@ -2,6 +2,7 @@ import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } fro
2
2
  import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
3
3
  export type ReadableStreamSchemaOptions = SimpleSchemaOptions;
4
4
  export declare class ReadableStreamSchema extends SimpleSchema<ReadableStream> {
5
+ readonly name = "ReadableStream";
5
6
  constructor(options?: ReadableStreamSchemaOptions);
6
7
  }
7
8
  export declare function readableStream(options?: ReadableStreamSchemaOptions): ReadableStreamSchema;
@@ -2,6 +2,7 @@ import { isReadableStream } from '../../utils/type-guards.js';
2
2
  import { Property } from '../decorators/index.js';
3
3
  import { SimpleSchema } from './simple.js';
4
4
  export class ReadableStreamSchema extends SimpleSchema {
5
+ name = 'ReadableStream';
5
6
  constructor(options) {
6
7
  super('ReadableStream', isReadableStream, options);
7
8
  }
@@ -2,6 +2,7 @@ import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } fro
2
2
  import { SimpleSchema, type SimpleSchemaOptions } from './simple.js';
3
3
  export type RegExpSchemaOptions = SimpleSchemaOptions;
4
4
  export declare class RegExpSchema extends SimpleSchema<RegExp> {
5
+ readonly name = "RegExp";
5
6
  constructor(options?: RegExpSchemaOptions);
6
7
  }
7
8
  export declare function regExp(options?: RegExpSchemaOptions): RegExpSchema;
@@ -3,6 +3,7 @@ import { Property } from '../decorators/index.js';
3
3
  import { SchemaError } from '../schema.error.js';
4
4
  import { SimpleSchema } from './simple.js';
5
5
  export class RegExpSchema extends SimpleSchema {
6
+ name = 'RegExp';
6
7
  constructor(options) {
7
8
  super('RegExp', isRegExp, options, {
8
9
  coercers: {
@@ -4,6 +4,7 @@ export type StringSchemaOptions = SimpleSchemaOptions & {
4
4
  pattern?: RegExp | string;
5
5
  };
6
6
  export declare class StringSchema extends SimpleSchema<string> {
7
+ readonly name = "string";
7
8
  constructor(options?: StringSchemaOptions);
8
9
  }
9
10
  export declare function string(options?: StringSchemaOptions): StringSchema;
@@ -2,6 +2,7 @@ import { isDefined, isRegExp, isString } from '../../utils/type-guards.js';
2
2
  import { Property } from '../decorators/index.js';
3
3
  import { SimpleSchema } from './simple.js';
4
4
  export class StringSchema extends SimpleSchema {
5
+ name = 'string';
5
6
  constructor(options) {
6
7
  const pattern = isDefined(options?.pattern) ? isString(options.pattern) ? RegExp(options.pattern, 'u') : isRegExp(options.pattern) ? options.pattern : undefined : undefined;
7
8
  super('string', isString, options, {
@@ -2,6 +2,7 @@ import type { JsonPath } from '../../json-path/json-path.js';
2
2
  import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
3
3
  import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
4
4
  export declare class SymbolSchema extends Schema<symbol> {
5
+ readonly name = "symbol";
5
6
  _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<symbol>;
6
7
  }
7
8
  export declare function symbol(): SymbolSchema;
@@ -4,6 +4,7 @@ import { typeOf } from '../../utils/type-of.js';
4
4
  import { Property } from '../decorators/index.js';
5
5
  import { Schema } from '../schema.js';
6
6
  export class SymbolSchema extends Schema {
7
+ name = 'symbol';
7
8
  _test(value, path, options) {
8
9
  if (isSymbol(value)) {
9
10
  return { valid: true, value };
@@ -1,9 +1,10 @@
1
1
  import type { JsonPath } from '../../json-path/json-path.js';
2
- import { Schema, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
2
+ import { Schema, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
3
3
  export declare class TransformSchema<I, O> extends Schema<O> {
4
+ readonly name: string;
4
5
  readonly schema: Schema<I>;
5
6
  readonly transformFn: (value: I) => O;
6
- constructor(schema: Schema<I>, transformFn: (value: I) => O);
7
+ constructor(schema: SchemaTestable<I>, transformFn: (value: I) => O);
7
8
  _test(value: any, path: JsonPath, options: SchemaTestOptions): SchemaTestResult<O>;
8
9
  }
9
10
  export declare function transform<I, O>(schema: Schema<I>, transformFn: (value: I) => O): TransformSchema<I, O>;
@@ -1,11 +1,14 @@
1
1
  import { Schema } from '../schema.js';
2
+ import { schemaTestableToSchema } from '../testable.js';
2
3
  export class TransformSchema extends Schema {
4
+ name;
3
5
  schema;
4
6
  transformFn;
5
7
  constructor(schema, transformFn) {
6
8
  super();
7
- this.schema = schema;
9
+ this.schema = schemaTestableToSchema(schema);
8
10
  this.transformFn = transformFn;
11
+ this.name = `Transform[${schema.name}]`;
9
12
  }
10
13
  _test(value, path, options) {
11
14
  const result = this.schema._test(value, path, options);
@@ -7,6 +7,7 @@ export type Uint8ArraySchemaOptions = SimpleSchemaOptions & {
7
7
  maximumLength?: number;
8
8
  };
9
9
  export declare class Uint8ArraySchema extends SimpleSchema<Uint8Array> {
10
+ readonly name = "Uint8Array";
10
11
  readonly options: Uint8ArraySchemaOptions;
11
12
  constructor(options?: Uint8ArraySchemaOptions);
12
13
  }
@@ -2,6 +2,7 @@ import { isDefined, isUint8Array } from '../../utils/type-guards.js';
2
2
  import { Property } from '../decorators/index.js';
3
3
  import { SimpleSchema } from './simple.js';
4
4
  export class Uint8ArraySchema extends SimpleSchema {
5
+ name = 'Uint8Array';
5
6
  options;
6
7
  constructor(options) {
7
8
  super('Uint8Array', isUint8Array, options, {
@@ -3,6 +3,7 @@ import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } fro
3
3
  import { Schema, type SchemaOutput, type SchemaTestable, type SchemaTestOptions, type SchemaTestResult } from '../schema.js';
4
4
  type UnionSchemaType<T extends [SchemaTestable, ...SchemaTestable[]]> = T[number] extends SchemaTestable<infer V> ? V : never;
5
5
  export declare class UnionSchema<T extends [SchemaTestable, ...SchemaTestable[]]> extends Schema<UnionSchemaType<T>> {
6
+ readonly name: string;
6
7
  readonly schemas: {
7
8
  [P in keyof T]: T[P] extends Schema ? T[P] : Schema<SchemaOutput<T[P]>>;
8
9
  };
@@ -1,22 +1,31 @@
1
1
  import { Property } from '../decorators/index.js';
2
+ import { SchemaError } from '../schema.error.js';
2
3
  import { Schema } from '../schema.js';
3
4
  import { isSchemaTestable, schemaTestableToSchema } from '../testable.js';
4
5
  export class UnionSchema extends Schema {
6
+ name;
5
7
  schemas;
6
8
  constructor(schemas) {
7
9
  super();
8
- this.schemas = schemas.map((schema) => schemaTestableToSchema(schema));
10
+ this.schemas = schemas.flatMap((testable) => {
11
+ const schema = schemaTestableToSchema(testable);
12
+ if (schema instanceof UnionSchema) {
13
+ return schema.schemas;
14
+ }
15
+ return schema;
16
+ });
17
+ this.name = `Union[${this.schemas.map((schema) => schema.name).join(', ')}]`;
9
18
  }
10
19
  _test(value, path, options) {
11
- let firstInvalidResult;
20
+ const errors = [];
12
21
  for (const schema of this.schemas) {
13
22
  const result = schema._test(value, path, options);
14
23
  if (result.valid) {
15
24
  return result;
16
25
  }
17
- firstInvalidResult ??= result;
26
+ errors.push(result.error);
18
27
  }
19
- return firstInvalidResult;
28
+ return { valid: false, error: new SchemaError(`None of the schemas [${this.schemas.map((schema) => schema.name).join(', ')}] matched. See inner for details.`, { path, inner: errors, fast: options.fastErrors }) };
20
29
  }
21
30
  }
22
31
  export function union(...schemas) {
@@ -1,6 +1,7 @@
1
1
  import { type SchemaPropertyDecorator, type SchemaPropertyDecoratorOptions } from '../decorators/index.js';
2
2
  import { Schema, type SchemaTestResult } from '../schema.js';
3
3
  export declare class UnknownSchema extends Schema<unknown> {
4
+ readonly name = "unknown";
4
5
  _test(value: unknown): SchemaTestResult<unknown>;
5
6
  }
6
7
  export declare function unknown(): UnknownSchema;
@@ -1,6 +1,7 @@
1
1
  import { Property } from '../decorators/index.js';
2
2
  import { Schema } from '../schema.js';
3
3
  export class UnknownSchema extends Schema {
4
+ name = 'unknown';
4
5
  _test(value) {
5
6
  return { valid: true, value };
6
7
  }
@@ -5,8 +5,10 @@ import { boolean } from './schemas/boolean.js';
5
5
  import { func } from './schemas/function.js';
6
6
  import { number } from './schemas/number.js';
7
7
  import { getSchemaFromReflection } from './schemas/object.js';
8
+ import { readableStream } from './schemas/readable-stream.js';
8
9
  import { string } from './schemas/string.js';
9
10
  import { symbol } from './schemas/symbol.js';
11
+ import { uint8Array } from './schemas/uint8-array.js';
10
12
  export function schemaTestableToSchema(testable) {
11
13
  if (testable instanceof Schema) {
12
14
  return testable;
@@ -24,6 +26,10 @@ export function schemaTestableToSchema(testable) {
24
26
  return symbol();
25
27
  case Function:
26
28
  return func();
29
+ case Uint8Array:
30
+ return uint8Array();
31
+ case ReadableStream:
32
+ return readableStream();
27
33
  default:
28
34
  return getSchemaFromReflection(testable);
29
35
  }
@@ -15,13 +15,13 @@ export declare function encodeUtf8Stream(): TransformStream<string, Uint8Array>;
15
15
  * @param encoding encoding, defaults to utf8
16
16
  * @returns decoded string
17
17
  */
18
- export declare function decodeText(buffer: BinaryData, encoding?: string): string;
18
+ export declare function decodeText(buffer: BinaryData, encoding?: string, options?: TextDecoderOptions): string;
19
19
  /**
20
20
  * Transforms binary stream to string stream
21
21
  * @param encoding encoding, defaults to utf8
22
22
  * @returns stream of decoded string
23
23
  */
24
- export declare function decodeTextStream(encoding?: string): TransformStream<BinaryData, string>;
24
+ export declare function decodeTextStream(encoding?: string, options?: TextDecoderOptions): TransformStream<BinaryData, string>;
25
25
  /**
26
26
  * Encodes buffer to hex
27
27
  * @param buffer buffer to encode
package/utils/encoding.js CHANGED
@@ -25,8 +25,8 @@ export function encodeUtf8Stream() {
25
25
  * @param encoding encoding, defaults to utf8
26
26
  * @returns decoded string
27
27
  */
28
- export function decodeText(buffer, encoding) {
29
- const decoder = new TextDecoder(encoding, { fatal: true });
28
+ export function decodeText(buffer, encoding, options) {
29
+ const decoder = new TextDecoder(encoding, options);
30
30
  return decoder.decode(buffer);
31
31
  }
32
32
  /**
@@ -34,8 +34,8 @@ export function decodeText(buffer, encoding) {
34
34
  * @param encoding encoding, defaults to utf8
35
35
  * @returns stream of decoded string
36
36
  */
37
- export function decodeTextStream(encoding) {
38
- return new TextDecoderStream(encoding, { fatal: true });
37
+ export function decodeTextStream(encoding, options) {
38
+ return new TextDecoderStream(encoding, options);
39
39
  }
40
40
  /**
41
41
  * Encodes buffer to hex
@@ -6,3 +6,4 @@ export * from './slice.js';
6
6
  export * from './stream-helper-types.js';
7
7
  export * from './stream-reader.js';
8
8
  export * from './to-bytes-stream.js';
9
+ export * from './to-readable-stream.js';
@@ -6,3 +6,4 @@ export * from './slice.js';
6
6
  export * from './stream-helper-types.js';
7
7
  export * from './stream-reader.js';
8
8
  export * from './to-bytes-stream.js';
9
+ export * from './to-readable-stream.js';
@@ -7,4 +7,4 @@ export type ReadBinaryStreamOptions = {
7
7
  onLengthSubceed?: 'error' | 'close' | 'leave-open';
8
8
  };
9
9
  export declare function readBinaryStream(iterableOrStream: AnyIterable<ArrayBufferView> | ReadableStream<ArrayBufferView>, { length, onLengthExceed, onLengthSubceed }?: ReadBinaryStreamOptions): Promise<Uint8Array>;
10
- export declare function readTextStream(iterableOrStream: AnyIterable<string> | ReadableStream<string>): Promise<string>;
10
+ export declare function readTextStream(iterableOrStream: AsyncIterable<string> | ReadableStream<string>): Promise<string>;
@@ -1,6 +1,7 @@
1
1
  import { BadRequestError } from '../../errors/bad-request.error.js';
2
2
  import { MaxBytesExceededError } from '../../errors/max-bytes-exceeded.error.js';
3
3
  import { NotSupportedError } from '../../errors/not-supported.error.js';
4
+ import { isAsyncIterable } from '../async-iterable-helpers/is-async-iterable.js';
4
5
  import { concatArrayBufferViews } from '../binary.js';
5
6
  import { isDefined, isReadableStream } from '../type-guards.js';
6
7
  import { getReadableStreamFromIterable, getReadableStreamIterable } from './readable-stream-adapter.js';
@@ -77,7 +78,7 @@ export async function readBinaryStream(iterableOrStream, { length, onLengthExcee
77
78
  return concatArrayBufferViews(views, Uint8Array, totalLength);
78
79
  }
79
80
  export async function readTextStream(iterableOrStream) {
80
- const iterable = isReadableStream(iterableOrStream) ? getReadableStreamIterable(iterableOrStream) : iterableOrStream;
81
+ const iterable = isAsyncIterable(iterableOrStream) ? iterableOrStream : getReadableStreamIterable(iterableOrStream);
81
82
  let text = '';
82
83
  for await (const chunk of iterable) {
83
84
  text += chunk;
@@ -0,0 +1 @@
1
+ export declare function toReadableStream<T>(value: T): ReadableStream<T>;
@@ -0,0 +1,8 @@
1
+ export function toReadableStream(value) {
2
+ return new ReadableStream({
3
+ start(controller) {
4
+ controller.enqueue(value);
5
+ controller.close();
6
+ }
7
+ });
8
+ }