@travetto/runtime 8.0.0-alpha.9 → 8.0.1

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/src/context.ts CHANGED
@@ -1,17 +1,16 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
 
4
- import { type ManifestIndex, type ManifestContext, ManifestModuleUtil } from '@travetto/manifest';
4
+ import { type ManifestContext, type ManifestIndex, ManifestModuleUtil } from '@travetto/manifest';
5
5
 
6
6
  import { Env } from './env.ts';
7
- import { RuntimeIndex } from './manifest-index.ts';
8
7
  import { describeFunction } from './function.ts';
9
- import type { Role } from './trv';
10
8
  import { JSONUtil } from './json.ts';
9
+ import { RuntimeIndex } from './manifest-index.ts';
10
+ import type { Role } from './trv.ts';
11
11
 
12
12
  /** Constrained version of {@type ManifestContext} */
13
13
  class $Runtime {
14
-
15
14
  #idx: ManifestIndex;
16
15
  #resourceOverrides?: Record<string, string>;
17
16
 
@@ -23,14 +22,13 @@ class $Runtime {
23
22
  get #moduleAliases(): Record<string, string> {
24
23
  return {
25
24
  '@': this.#idx.mainModule.sourcePath,
26
- '@@': this.#idx.manifest.workspace.path,
25
+ '@@': this.#idx.manifest.workspace.path
27
26
  };
28
27
  }
29
28
 
30
29
  /** The role we are running as */
31
30
  get role(): Role {
32
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
33
- return Env.TRV_ROLE.value as Role ?? 'std';
31
+ return (Env.TRV_ROLE.value as Role) ?? 'std';
34
32
  }
35
33
 
36
34
  /** Are we in production mode */
@@ -45,7 +43,7 @@ class $Runtime {
45
43
 
46
44
  /** Get debug value */
47
45
  get debug(): false | string {
48
- return Env.DEBUG.isFalse ? false : (Env.DEBUG.value || false);
46
+ return Env.DEBUG.isFalse ? false : Env.DEBUG.value || false;
49
47
  }
50
48
 
51
49
  /** Manifest main */
@@ -80,7 +78,7 @@ class $Runtime {
80
78
 
81
79
  /** Produce a workspace path for tooling, with '@' being replaced by node_module/name folder */
82
80
  toolPath(...parts: string[]): string {
83
- parts = parts.flatMap(part => part === '@' ? ['node_modules', this.#idx.manifest.main.name] : [part]);
81
+ parts = parts.flatMap(part => (part === '@' ? ['node_modules', this.#idx.manifest.main.name] : [part]));
84
82
  return path.resolve(this.workspace.path, this.#idx.manifest.build.toolFolder, ...parts);
85
83
  }
86
84
 
@@ -95,12 +93,14 @@ class $Runtime {
95
93
 
96
94
  /** Resolve resource paths */
97
95
  resourcePaths(paths: string[] = []): string[] {
98
- return [...new Set([...paths, ...Env.TRV_RESOURCES.list ?? [], '@#resources', '@@#resources'].map(module => this.modulePath(module)))];
96
+ return [
97
+ ...new Set([...paths, ...(Env.TRV_RESOURCES.list ?? []), '@#resources', '@@#resources'].map(module => this.modulePath(module)))
98
+ ];
99
99
  }
100
100
 
101
101
  /** Get source for function */
102
102
  getSourceFile(handle: Function): string {
103
- return this.#idx.getFromImport(this.getImport(handle))?.sourceFile!;
103
+ return this.#idx.getFromImport(this.getImport(handle))?.sourceFile ?? undefined!;
104
104
  }
105
105
 
106
106
  /** Get import for function */
@@ -146,11 +146,14 @@ class $Runtime {
146
146
  }
147
147
  }
148
148
  switch (this.workspace.manager) {
149
- case 'npm': return `npm install ${production ? '' : '--save-dev '}${pkg}`;
150
- case 'yarn': return `yarn add ${production ? '' : '--dev '}${pkg}`;
151
- case 'pnpm': return `pnpm add ${production ? '' : '--dev '}${pkg}`;
149
+ case 'npm':
150
+ return `npm install ${production ? '' : '--save-dev '}${pkg}`;
151
+ case 'yarn':
152
+ return `yarn add ${production ? '' : '--dev '}${pkg}`;
153
+ case 'pnpm':
154
+ return `pnpm add ${production ? '' : '--dev '}${pkg}`;
152
155
  }
153
156
  }
154
157
  }
155
158
 
156
- export const Runtime = new $Runtime(RuntimeIndex, Env.TRV_RESOURCE_OVERRIDES.object);
159
+ export const Runtime = new $Runtime(RuntimeIndex, Env.TRV_RESOURCE_OVERRIDES.object);
package/src/debug.ts CHANGED
@@ -7,7 +7,6 @@ import type { ClassInstance } from './types.ts';
7
7
  */
8
8
  export function DebugBreak(): MethodDecorator {
9
9
  return (instance: ClassInstance, property: string | symbol, descriptor: PropertyDescriptor) => descriptor;
10
-
11
10
  }
12
11
 
13
12
  /**
@@ -15,4 +14,4 @@ export function DebugBreak(): MethodDecorator {
15
14
  */
16
15
  export function tryDebugger(): boolean {
17
16
  return Env.TRV_DEBUG_BREAK.isTrue;
18
- }
17
+ }
package/src/env.ts CHANGED
@@ -3,11 +3,13 @@ import { castKey, castTo } from './types.ts';
3
3
  const IS_TRUE = /^(true|yes|on|1)$/i;
4
4
  const IS_FALSE = /^(false|no|off|0)$/i;
5
5
 
6
- export interface EnvData { }
6
+ export interface EnvData {}
7
7
 
8
8
  export class EnvProp<T> {
9
9
  readonly key: string;
10
- constructor(key: string) { this.key = key; }
10
+ constructor(key: string) {
11
+ this.key = key;
12
+ }
11
13
 
12
14
  /** Set value according to type */
13
15
  set(value: T | undefined | null): void {
@@ -26,14 +28,18 @@ export class EnvProp<T> {
26
28
  /** Export value */
27
29
  export(value?: T | undefined | null): Record<string, string> {
28
30
  let out: string;
29
- if (arguments.length === 0) { // If nothing passed in
31
+ // biome-ignore lint/complexity/noArguments: We want to use arguments here to detect if nothing was truly passed
32
+ if (arguments.length === 0) {
33
+ // If nothing passed in
30
34
  out = `${this.value}`;
31
35
  } else if (value === undefined || value === null) {
32
36
  out = '';
33
37
  } else if (Array.isArray(value)) {
34
38
  out = value.join(',');
35
39
  } else if (typeof value === 'object') {
36
- out = Object.entries(value).map(([key, keyValue]) => `${key}=${keyValue}`).join(',');
40
+ out = Object.entries(value)
41
+ .map(([key, keyValue]) => `${key}=${keyValue}`)
42
+ .join(',');
37
43
  } else {
38
44
  out = `${value}`;
39
45
  }
@@ -41,13 +47,19 @@ export class EnvProp<T> {
41
47
  }
42
48
 
43
49
  /** Read value as string */
44
- get value(): string | undefined { return process.env[this.key] || undefined; }
50
+ get value(): string | undefined {
51
+ return process.env[this.key] || undefined;
52
+ }
45
53
 
46
54
  /** Read value as list */
47
55
  get list(): string[] | undefined {
48
56
  const value = this.value;
49
- return (value === undefined || value === '') ?
50
- undefined : value.split(/[, ]+/g).map(item => item.trim()).filter(item => !!item);
57
+ return value === undefined || value === ''
58
+ ? undefined
59
+ : value
60
+ .split(/[, ]+/g)
61
+ .map(item => item.trim())
62
+ .filter(item => !!item);
51
63
  }
52
64
 
53
65
  /** Read value as object */
@@ -58,7 +70,7 @@ export class EnvProp<T> {
58
70
 
59
71
  /** Add values to list */
60
72
  add(...items: string[]): void {
61
- process.env[this.key] = [... new Set([...this.list ?? [], ...items])].join(',');
73
+ process.env[this.key] = [...new Set([...(this.list ?? []), ...items])].join(',');
62
74
  }
63
75
 
64
76
  /** Read value as int */
@@ -70,7 +82,7 @@ export class EnvProp<T> {
70
82
  /** Read value as boolean */
71
83
  get bool(): boolean | undefined {
72
84
  const value = this.value;
73
- return (value === undefined || value === '') ? undefined : IS_TRUE.test(value);
85
+ return value === undefined || value === '' ? undefined : IS_TRUE.test(value);
74
86
  }
75
87
 
76
88
  /** Determine if the underlying value is truthy */
@@ -91,24 +103,32 @@ export class EnvProp<T> {
91
103
  }
92
104
 
93
105
  type EnvDataCombinedType = {
94
- [K in keyof EnvData]: Pick<EnvProp<EnvData[K]>, 'key' | 'export' | 'value' | 'set' | 'clear' | 'isSet' |
95
- (EnvData[K] extends unknown[] ? 'list' | 'add' : never) |
96
- (Extract<EnvData[K], object> extends never ? never : 'object') |
97
- (Extract<EnvData[K], number> extends never ? never : 'int') |
98
- (Extract<EnvData[K], boolean> extends never ? never : 'bool' | 'isTrue' | 'isFalse')
99
- >
106
+ [K in keyof EnvData]: Pick<
107
+ EnvProp<EnvData[K]>,
108
+ | 'key'
109
+ | 'export'
110
+ | 'value'
111
+ | 'set'
112
+ | 'clear'
113
+ | 'isSet'
114
+ | (EnvData[K] extends unknown[] ? 'list' | 'add' : never)
115
+ | (Extract<EnvData[K], object> extends never ? never : 'object')
116
+ | (Extract<EnvData[K], number> extends never ? never : 'int')
117
+ | (Extract<EnvData[K], boolean> extends never ? never : 'bool' | 'isTrue' | 'isFalse')
118
+ >;
100
119
  };
101
120
 
102
121
  function delegate<T extends object>(base: T): EnvDataCombinedType & T {
103
122
  return new Proxy(castTo(base), {
104
123
  get(target, property): unknown {
105
- return typeof property !== 'string' ? undefined :
106
- (property in base ? base[castKey(property)] :
107
- target[castKey<typeof target>(property)] ??= castTo(new EnvProp(property))
108
- );
124
+ return typeof property !== 'string'
125
+ ? undefined
126
+ : property in base
127
+ ? base[castKey(property)]
128
+ : (target[castKey<typeof target>(property)] ??= castTo(new EnvProp(property)));
109
129
  }
110
130
  });
111
131
  }
112
132
 
113
133
  /** Basic utils for reading known environment variables */
114
- export const Env = delegate({});
134
+ export const Env = delegate({});
package/src/error.ts CHANGED
@@ -1,13 +1,6 @@
1
1
  import { castTo } from './types.ts';
2
2
 
3
- export type ErrorCategory =
4
- 'general' |
5
- 'notfound' |
6
- 'data' |
7
- 'permissions' |
8
- 'authentication' |
9
- 'timeout' |
10
- 'unavailable';
3
+ export type ErrorCategory = 'general' | 'notfound' | 'data' | 'permissions' | 'authentication' | 'timeout' | 'unavailable';
11
4
 
12
5
  export type RuntimeErrorOptions<T> = Omit<Partial<RuntimeError>, 'details'> & (T extends undefined ? { details?: T } : { details: T });
13
6
 
@@ -15,7 +8,6 @@ export type RuntimeErrorOptions<T> = Omit<Partial<RuntimeError>, 'details'> & (T
15
8
  * Framework error class, with the aim of being extensible
16
9
  */
17
10
  export class RuntimeError<T = Record<string, unknown> | undefined> extends Error {
18
-
19
11
  static defaultCategory: ErrorCategory = 'general';
20
12
 
21
13
  type: string;
@@ -28,14 +20,11 @@ export class RuntimeError<T = Record<string, unknown> | undefined> extends Error
28
20
  *
29
21
  * @param message The error message
30
22
  */
31
- constructor(
32
- ...[message, options]:
33
- T extends undefined ? ([string] | [string, RuntimeErrorOptions<T>]) : [string, RuntimeErrorOptions<T>]
34
- ) {
23
+ constructor(...[message, options]: T extends undefined ? [string] | [string, RuntimeErrorOptions<T>] : [string, RuntimeErrorOptions<T>]) {
35
24
  super(message, options?.cause ? { cause: options.cause } : undefined);
36
25
  this.type = options?.type ?? this.constructor.name;
37
- this.details = options?.details!;
26
+ this.details = options?.details ?? undefined!;
38
27
  this.category = options?.category ?? castTo<typeof RuntimeError>(this.constructor).defaultCategory ?? 'general';
39
28
  this.at = new Date(options?.at ?? Date.now());
40
29
  }
41
- }
30
+ }
package/src/exec.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { type ChildProcess, spawn, type SpawnOptions } from 'node:child_process';
1
+ import { type ChildProcess, type SpawnOptions, spawn } from 'node:child_process';
2
2
 
3
- import { castTo } from './types.ts';
4
- import { RuntimeIndex } from './manifest-index.ts';
5
- import { BinaryUtil, type BinaryArray } from './binary.ts';
3
+ import { type BinaryArray, BinaryUtil } from './binary.ts';
6
4
  import { CodecUtil } from './codec.ts';
5
+ import { RuntimeIndex } from './manifest-index.ts';
6
+ import { castTo } from './types.ts';
7
7
 
8
8
  const ResultSymbol = Symbol();
9
9
 
@@ -39,6 +39,17 @@ type ExecutionBaseResult = Omit<ExecutionResult, 'stdout' | 'stderr'>;
39
39
  * Standard utilities for managing executions
40
40
  */
41
41
  export class ExecUtil {
42
+ /** Read stream as string from a result */
43
+ static toString(result: ExecutionResult<BinaryArray | string>, stream: 'stdout' | 'stderr' | 'any'): string {
44
+ if (stream === 'any') {
45
+ return this.toString(result, 'stderr') || this.toString(result, 'stdout');
46
+ }
47
+ const value = result[stream];
48
+ if (typeof value === 'string') {
49
+ return value.trim();
50
+ }
51
+ return CodecUtil.toUTF8String(value).trim();
52
+ }
42
53
 
43
54
  /**
44
55
  * Take a child process, and some additional options, and produce a promise that
@@ -49,11 +60,14 @@ export class ExecUtil {
49
60
  * @param options The options to use to enhance the process
50
61
  */
51
62
  static getResult(subProcess: ChildProcess): Promise<ExecutionResult<string>>;
52
- static getResult(subProcess: ChildProcess, options: { catch?: boolean, binary?: false }): Promise<ExecutionResult<string>>;
53
- static getResult(subProcess: ChildProcess, options: { catch?: boolean, binary: true }): Promise<ExecutionResult<BinaryArray>>;
54
- static getResult<T extends string | BinaryArray>(subProcess: ChildProcess, options: { catch?: boolean, binary?: boolean } = {}): Promise<ExecutionResult<T>> {
63
+ static getResult(subProcess: ChildProcess, options: { catch?: boolean; binary?: false }): Promise<ExecutionResult<string>>;
64
+ static getResult(subProcess: ChildProcess, options: { catch?: boolean; binary: true }): Promise<ExecutionResult<BinaryArray>>;
65
+ static getResult<T extends string | BinaryArray>(
66
+ subProcess: ChildProcess,
67
+ options: { catch?: boolean; binary?: boolean } = {}
68
+ ): Promise<ExecutionResult<T>> {
55
69
  const typed: ChildProcess & { [ResultSymbol]?: Promise<ExecutionResult> } = subProcess;
56
- const result = typed[ResultSymbol] ??= new Promise<ExecutionResult>(resolve => {
70
+ const result = (typed[ResultSymbol] ??= new Promise<ExecutionResult>(resolve => {
57
71
  const stdout: BinaryArray[] = [];
58
72
  const stderr: BinaryArray[] = [];
59
73
  let done = false;
@@ -65,7 +79,7 @@ export class ExecUtil {
65
79
 
66
80
  const buffers = {
67
81
  stdout: BinaryUtil.combineBinaryArrays(stdout),
68
- stderr: BinaryUtil.combineBinaryArrays(stderr),
82
+ stderr: BinaryUtil.combineBinaryArrays(stderr)
69
83
  };
70
84
 
71
85
  const final = {
@@ -74,37 +88,37 @@ export class ExecUtil {
74
88
  ...finalResult
75
89
  };
76
90
 
77
- resolve(!final.valid ?
78
- { ...final, message: `${final.message || final.stderr || final.stdout || 'failed'}` } :
79
- final
80
- );
91
+ resolve(!final.valid ? { ...final, message: `${final.message || final.stderr || final.stdout || 'failed'}` } : final);
81
92
  };
82
93
 
83
94
  subProcess.stdout?.on('data', data => stdout.push(CodecUtil.readChunk(data, subProcess.stdout?.readableEncoding)));
84
95
  subProcess.stderr?.on('data', data => stderr.push(CodecUtil.readChunk(data, subProcess.stderr?.readableEncoding)));
85
96
 
86
- subProcess.on('error', (error: Error) =>
87
- finish({ code: 1, message: error.message, valid: false }));
97
+ subProcess.on('error', (error: Error) => finish({ code: 1, message: error.message, valid: false }));
88
98
 
89
- subProcess.on('close', (code: number) =>
90
- finish({ code, valid: code === null || code === 0 }));
99
+ subProcess.on('close', (code: number) => finish({ code, valid: code === null || code === 0 }));
91
100
 
92
- if (subProcess.exitCode !== null) { // We are already done
101
+ if (subProcess.exitCode !== null) {
102
+ // We are already done
93
103
  finish({ code: subProcess.exitCode, valid: subProcess.exitCode === 0 });
94
104
  }
95
- });
96
-
97
- return castTo(options.catch ? result : result.then(executionResult => {
98
- if (executionResult.valid) {
99
- return executionResult;
100
- } else {
101
- throw new Error(executionResult.message);
102
- }
103
105
  }));
106
+
107
+ return castTo(
108
+ options.catch
109
+ ? result
110
+ : result.then(executionResult => {
111
+ if (executionResult.valid) {
112
+ return executionResult;
113
+ } else {
114
+ throw new Error(executionResult.message);
115
+ }
116
+ })
117
+ );
104
118
  }
105
119
 
106
120
  /** Spawn a package command */
107
121
  static spawnPackageCommand(cmd: string, args: string[], config: SpawnOptions = {}): ChildProcess {
108
122
  return spawn(process.argv0, [RuntimeIndex.resolvePackageCommand(cmd), ...args], config);
109
123
  }
110
- }
124
+ }
@@ -2,14 +2,13 @@ import { createReadStream } from 'node:fs';
2
2
  import fs from 'node:fs/promises';
3
3
  import path from 'node:path';
4
4
 
5
+ import { type BinaryArray, type BinaryStream, BinaryUtil } from './binary.ts';
5
6
  import { RuntimeError } from './error.ts';
6
- import { BinaryUtil, type BinaryArray, type BinaryStream } from './binary.ts';
7
7
 
8
8
  /**
9
9
  * File loader that will search for files across the provided search paths
10
10
  */
11
11
  export class FileLoader {
12
-
13
12
  #searchPaths: readonly string[];
14
13
 
15
14
  constructor(paths: string[]) {
@@ -72,4 +71,4 @@ export class FileLoader {
72
71
  const buffer = BinaryUtil.binaryArrayToBuffer(await this.readBinaryArray(relativePath));
73
72
  return new File([buffer], path.basename(relativePath));
74
73
  }
75
- }
74
+ }
package/src/function.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ManifestModuleUtil } from '@travetto/manifest';
2
2
 
3
- export type FunctionMetadataTag = { hash: number, lines: [start: number, end: number, bodyStart?: number] };
3
+ export type FunctionMetadataTag = { hash: number; lines: [start: number, end: number, bodyStart?: number] };
4
4
  export type FunctionMetadata = FunctionMetadataTag & {
5
5
  id: string;
6
6
  import: string;
@@ -26,13 +26,16 @@ const pending = new Set<Function>([]);
26
26
  * @private
27
27
  */
28
28
  export function registerFunction(
29
- input: Function, [module, relativePath]: [string, string], tag: FunctionMetadataTag,
30
- methods?: Record<string, FunctionMetadataTag>, abstract?: boolean,
29
+ input: Function,
30
+ [module, relativePath]: [string, string],
31
+ tag: FunctionMetadataTag,
32
+ methods?: Record<string, FunctionMetadataTag>,
33
+ abstract?: boolean
31
34
  ): void {
32
35
  const modulePath = ManifestModuleUtil.withoutSourceExtension(relativePath);
33
36
 
34
37
  const metadata: FunctionMetadata = {
35
- id: (input.name ? `${module}:${modulePath}#${input.name}` : `${module}:${modulePath}`),
38
+ id: input.name ? `${module}:${modulePath}#${input.name}` : `${module}:${modulePath}`,
36
39
  import: `${module}/${relativePath}`,
37
40
  module,
38
41
  modulePath,
@@ -65,5 +68,10 @@ export function describeFunction(input?: Function): FunctionMetadata | undefined
65
68
 
66
69
  const foreignTypeRegistry = new Map<string, Function>();
67
70
  export function foreignType(id: string): Function {
68
- return foreignTypeRegistry.getOrInsert(id, class { static Ⲑid = id; });
69
- }
71
+ return foreignTypeRegistry.getOrInsert(
72
+ id,
73
+ class {
74
+ static Ⲑid = id;
75
+ }
76
+ );
77
+ }
package/src/global.d.ts CHANGED
@@ -2,10 +2,6 @@ import './types';
2
2
 
3
3
  declare const write: unique symbol;
4
4
 
5
- declare global {
6
- var devProcessWarningExclusions: ((message: string, category: string) => boolean)[] | undefined;
7
- }
8
-
9
5
  declare global {
10
6
  // https://github.com/microsoft/TypeScript/issues/59012
11
7
  interface WritableStreamDefaultWriter<W = any> {
@@ -20,28 +16,28 @@ declare global {
20
16
  /**
21
17
  * @concrete node:buffer#Blob
22
18
  */
23
- interface Blob { }
19
+ interface Blob {}
24
20
 
25
21
  /**
26
22
  * @concrete node:buffer#File
27
23
  */
28
- interface File { }
24
+ interface File {}
29
25
 
30
26
  /**
31
27
  * @concrete node:stream/web#ReadableStream
32
28
  */
33
- interface ReadableStream { }
29
+ interface ReadableStream {}
34
30
 
35
31
  /**
36
32
  * @concrete node:buffer#Buffer
37
33
  */
38
- interface Buffer { }
34
+ interface Buffer {}
39
35
 
40
36
  namespace NodeJS {
41
37
  /**
42
38
  * @concrete node:stream#Readable
43
39
  */
44
- interface ReadableStream { }
40
+ interface ReadableStream {}
45
41
  }
46
42
  }
47
43
 
@@ -49,26 +45,38 @@ declare module 'buffer' {
49
45
  /**
50
46
  * @concrete node:buffer#Blob
51
47
  */
52
- interface Blob { }
48
+ interface Blob {}
53
49
 
54
50
  /**
55
51
  * @concrete node:buffer#File
56
52
  */
57
- interface File { }
53
+ interface File {}
54
+ }
55
+
56
+ declare module 'node:buffer' {
57
+ /**
58
+ * @concrete node:buffer#Blob
59
+ */
60
+ interface Blob {}
61
+
62
+ /**
63
+ * @concrete node:buffer#File
64
+ */
65
+ interface File {}
58
66
  }
59
67
 
60
68
  declare module 'stream' {
61
69
  /**
62
70
  * @concrete node:stream#Readable
63
71
  */
64
- interface Readable { }
72
+ interface Readable {}
65
73
  }
66
74
 
67
75
  declare module 'stream/web' {
68
76
  /**
69
77
  * @concrete node:stream/web#ReadableStream
70
78
  */
71
- interface ReadableStream { }
79
+ interface ReadableStream {}
72
80
  }
73
81
 
74
82
  // Remove once node 26 types are released
@@ -76,4 +84,4 @@ declare module 'node:fs' {
76
84
  interface StatOptions {
77
85
  throwIfNoEntry?: boolean;
78
86
  }
79
- }
87
+ }
package/src/json.ts CHANGED
@@ -3,27 +3,33 @@ import { AssertionError } from 'node:assert';
3
3
  import type { BinaryArray } from './binary.ts';
4
4
  import { CodecUtil } from './codec.ts';
5
5
  import { RuntimeError, type RuntimeErrorOptions } from './error.ts';
6
- import { castTo, type Any } from './types.ts';
6
+ import { type Any, castTo } from './types.ts';
7
7
 
8
8
  const VALID_JSON_ERROR_TYPES = ['runtime', 'plain', 'assert'] as const;
9
9
  const VALID_JSON_ERROR_TYPE_SET = new Set<unknown>(VALID_JSON_ERROR_TYPES);
10
10
 
11
11
  type JSONTransformer = (this: unknown, key: string, value: unknown) => unknown;
12
- type JSONOutputConfig = { indent?: number, replacer?: JSONTransformer };
12
+ type JSONOutputConfig = { indent?: number; replacer?: JSONTransformer };
13
13
  type JSONInputConfig = { reviver?: JSONTransformer };
14
14
  type JSONCloneConfig = JSONOutputConfig & JSONInputConfig;
15
- type JSONError = { $trv: (typeof VALID_JSON_ERROR_TYPES)[number], name?: string } & Partial<RuntimeErrorOptions<Record<string, unknown>>>;
15
+ type JSONError = { $trv: (typeof VALID_JSON_ERROR_TYPES)[number]; name?: string } & Partial<RuntimeErrorOptions<Record<string, unknown>>>;
16
16
 
17
17
  Object.defineProperty(BigInt.prototype, 'toJSON', {
18
- value() { return `${this}n`; },
18
+ value() {
19
+ return `${this}n`;
20
+ }
19
21
  });
20
22
 
21
23
  Object.defineProperty(Error.prototype, 'toJSON', {
22
- value() { return JSONUtil.errorToJSONError(this); },
24
+ value() {
25
+ return JSONUtil.errorToJSONError(this);
26
+ }
23
27
  });
24
28
 
25
29
  Object.defineProperty(RuntimeError.prototype, 'toJSON', {
26
- value() { return JSONUtil.errorToJSONError(this); },
30
+ value() {
31
+ return JSONUtil.errorToJSONError(this);
32
+ }
27
33
  });
28
34
 
29
35
  const ISO_8601_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?$/;
@@ -31,7 +37,6 @@ const BIGINT_REGEX = /^-?\d+n$/;
31
37
 
32
38
  /** Utilities for JSON */
33
39
  export class JSONUtil {
34
-
35
40
  static includeStackTraces = false;
36
41
 
37
42
  static TRANSMIT_REVIVER: JSONTransformer = function (this: unknown, key: string, value: unknown): unknown {
@@ -47,7 +52,6 @@ export class JSONUtil {
47
52
  return value;
48
53
  };
49
54
 
50
-
51
55
  static isJSONError(value: unknown): value is JSONError {
52
56
  return typeof value === 'object' && value !== null && '$trv' in value && VALID_JSON_ERROR_TYPE_SET.has(value.$trv);
53
57
  }
@@ -57,12 +61,20 @@ export class JSONUtil {
57
61
  const { $trv, message, stack, name, ...rest } = error;
58
62
  let response: Error;
59
63
  switch ($trv) {
60
- case 'runtime': response = new RuntimeError(message!, castTo<Any>(rest)); break;
61
- case 'assert': response = new AssertionError({ message, ...rest }); break;
62
- case 'plain': response = new Error(message!); break;
64
+ case 'runtime':
65
+ response = new RuntimeError(message!, castTo<Any>(rest));
66
+ break;
67
+ case 'assert':
68
+ response = new AssertionError({ message, ...rest });
69
+ break;
70
+ case 'plain':
71
+ response = new Error(message!);
72
+ break;
63
73
  }
64
74
  response.stack = stack;
65
- if (name) { response.name = name; }
75
+ if (name) {
76
+ response.name = name;
77
+ }
66
78
  return response;
67
79
  }
68
80
 
@@ -73,21 +85,29 @@ export class JSONUtil {
73
85
  includeStack ??= JSONUtil.includeStackTraces;
74
86
  let $trv: JSONError['$trv'];
75
87
  switch (true) {
76
- case error instanceof RuntimeError: $trv = 'runtime'; break;
77
- case error instanceof AssertionError: $trv = 'assert'; break;
78
- default: $trv = 'plain'; break;
88
+ case error instanceof RuntimeError:
89
+ $trv = 'runtime';
90
+ break;
91
+ case error instanceof AssertionError:
92
+ $trv = 'assert';
93
+ break;
94
+ default:
95
+ $trv = 'plain';
96
+ break;
79
97
  }
80
98
  return {
81
99
  $trv,
82
100
  message: error.message,
83
101
  ...(error.cause ? { cause: `${error.cause}` } : undefined),
84
102
  ...(includeStack ? { stack: error.stack } : undefined),
85
- ...(error instanceof RuntimeError ? {
86
- category: error.category,
87
- type: error.type,
88
- at: error.at,
89
- ...(error.details ? { details: error.details } : undefined!),
90
- } : {})
103
+ ...(error instanceof RuntimeError
104
+ ? {
105
+ category: error.category,
106
+ type: error.type,
107
+ at: error.at,
108
+ ...(error.details ? { details: error.details } : undefined!)
109
+ }
110
+ : {})
91
111
  };
92
112
  }
93
113
 
@@ -147,4 +167,4 @@ export class JSONUtil {
147
167
  static cloneFromTransmit<T, R = T>(input: T): R {
148
168
  return JSONUtil.clone<T, R>(input, { reviver: JSONUtil.TRANSMIT_REVIVER });
149
169
  }
150
- }
170
+ }
@@ -1,4 +1,4 @@
1
1
  import { ManifestIndex } from '@travetto/manifest';
2
2
 
3
3
  /** Runtime manifest index */
4
- export const RuntimeIndex = new ManifestIndex();
4
+ export const RuntimeIndex = new ManifestIndex();