@travetto/runtime 5.0.16 → 6.0.0-rc.0

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 CHANGED
@@ -65,7 +65,7 @@ class $Runtime {
65
65
  /** Get import for function */
66
66
  getImport(fn: Function): string;
67
67
  /** Import from a given path */
68
- importFrom<T = unknown>(imp?: string): Promise<T>;
68
+ async importFrom<T = unknown>(imp?: string): Promise<T>;
69
69
  }
70
70
  ```
71
71
 
@@ -73,7 +73,7 @@ class $Runtime {
73
73
  For the framework to work properly, metadata needs to be collected about files, classes and functions to uniquely identify them, with support for detecting changes during live reloads. To achieve this, every `class` is decorated with metadata, including methods, line numbers, and ultimately a unique id stored at `Ⲑid`.
74
74
 
75
75
  ## Environment Support
76
- The functionality we support for testing and retrieving environment information for known environment variables. They can be accessed directly on the [Env](https://github.com/travetto/travetto/tree/main/module/runtime/src/env.ts#L111) object, and will return a scoped [EnvProp](https://github.com/travetto/travetto/tree/main/module/runtime/src/env.ts#L8), that is compatible with the property definition. E.g. only showing boolean related fields when the underlying flag supports `true` or `false`
76
+ The functionality we support for testing and retrieving environment information for known environment variables. They can be accessed directly on the [Env](https://github.com/travetto/travetto/tree/main/module/runtime/src/env.ts#L114) object, and will return a scoped [EnvProp](https://github.com/travetto/travetto/tree/main/module/runtime/src/env.ts#L8), that is compatible with the property definition. E.g. only showing boolean related fields when the underlying flag supports `true` or `false`
77
77
 
78
78
  **Code: Base Known Environment Flags**
79
79
  ```typescript
@@ -140,11 +140,12 @@ For a given [EnvProp](https://github.com/travetto/travetto/tree/main/module/runt
140
140
  **Code: EnvProp Shape**
141
141
  ```typescript
142
142
  export class EnvProp<T> {
143
- constructor(public readonly key: string) { }
143
+ readonly key: string;
144
+ constructor(key: string) { this.key = key; }
144
145
  /** Remove value */
145
146
  clear(): void;
146
147
  /** Export value */
147
- export(val: T | undefined): Record<string, string>;
148
+ export(val?: T | undefined | null): Record<string, string>;
148
149
  /** Read value as string */
149
150
  get val(): string | undefined;
150
151
  /** Read value as list */
@@ -259,7 +260,7 @@ The primary access patterns for resources, is to directly request a file, and to
259
260
 
260
261
  The [FileLoader](https://github.com/travetto/travetto/tree/main/module/runtime/src/file-loader.ts#L11) allows for accessing information about the resources, and subsequently reading the file as text/binary or to access the resource as a `Readable` stream. If a file is not found, it will throw an [AppError](https://github.com/travetto/travetto/tree/main/module/runtime/src/error.ts#L26) with a category of 'notfound'.
261
262
 
262
- The [FileLoader](https://github.com/travetto/travetto/tree/main/module/runtime/src/file-loader.ts#L11) also supports tying itself to [Env](https://github.com/travetto/travetto/tree/main/module/runtime/src/env.ts#L111)'s `TRV_RESOURCES` information on where to attempt to find a requested resource.
263
+ The [FileLoader](https://github.com/travetto/travetto/tree/main/module/runtime/src/file-loader.ts#L11) also supports tying itself to [Env](https://github.com/travetto/travetto/tree/main/module/runtime/src/env.ts#L114)'s `TRV_RESOURCES` information on where to attempt to find a requested resource.
263
264
 
264
265
  ## Common Utilities
265
266
  Common utilities used throughout the framework. Currently [Util](https://github.com/travetto/travetto/tree/main/module/runtime/src/util.ts#L17) includes:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/runtime",
3
- "version": "5.0.16",
3
+ "version": "6.0.0-rc.0",
4
4
  "description": "Runtime for travetto applications.",
5
5
  "keywords": [
6
6
  "console-manager",
@@ -25,12 +25,12 @@
25
25
  "directory": "module/runtime"
26
26
  },
27
27
  "dependencies": {
28
- "@travetto/manifest": "^5.0.12",
28
+ "@travetto/manifest": "^6.0.0-rc.0",
29
29
  "@types/debug": "^4.1.12",
30
30
  "debug": "^4.4.0"
31
31
  },
32
32
  "peerDependencies": {
33
- "@travetto/transformer": "^5.0.13"
33
+ "@travetto/transformer": "^6.0.0-rc.0"
34
34
  },
35
35
  "peerDependenciesMeta": {
36
36
  "@travetto/transformer": {
package/src/context.ts CHANGED
@@ -7,6 +7,7 @@ import { type ManifestIndex, type ManifestContext, ManifestModuleUtil } from '@t
7
7
  import { Env } from './env';
8
8
  import { RuntimeIndex } from './manifest-index';
9
9
  import { describeFunction } from './function';
10
+ import { castTo } from './types';
10
11
 
11
12
  /** Constrained version of {@type ManifestContext} */
12
13
  class $Runtime {
@@ -107,7 +108,7 @@ class $Runtime {
107
108
  }
108
109
 
109
110
  /** Import from a given path */
110
- importFrom<T = unknown>(imp?: string): Promise<T> {
111
+ async importFrom<T = unknown>(imp?: string): Promise<T> {
111
112
  const file = path.resolve(this.#idx.mainModule.sourcePath, imp!);
112
113
  if (existsSync(file)) {
113
114
  imp = this.#idx.getFromSource(file)?.import;
@@ -131,7 +132,16 @@ class $Runtime {
131
132
  }
132
133
 
133
134
  imp = ManifestModuleUtil.withOutputExtension(imp);
134
- return import(imp);
135
+ const res = await import(imp);
136
+ if (res?.default?.default) {
137
+ // Unpack default.default, typescript does this in a way that requires recreating the whole object
138
+ const def = res?.default?.default;
139
+ return Object.defineProperties(castTo({}), {
140
+ ...Object.getOwnPropertyDescriptors(res),
141
+ default: { get: () => def, configurable: false }
142
+ });
143
+ }
144
+ return res;
135
145
  }
136
146
  }
137
147
 
package/src/env.ts CHANGED
@@ -6,7 +6,8 @@ const IS_FALSE = /^(false|no|off|0)$/i;
6
6
  export interface EnvData { }
7
7
 
8
8
  export class EnvProp<T> {
9
- constructor(public readonly key: string) { }
9
+ readonly key: string;
10
+ constructor(key: string) { this.key = key; }
10
11
 
11
12
  /** Set value according to prop type */
12
13
  set(val: T | undefined | null): void {
@@ -23,9 +24,11 @@ export class EnvProp<T> {
23
24
  }
24
25
 
25
26
  /** Export value */
26
- export(val: T | undefined): Record<string, string> {
27
+ export(val?: T | undefined | null): Record<string, string> {
27
28
  let out: string;
28
- if (val === undefined || val === '' || val === null) {
29
+ if (arguments.length === 0) { // If nothing passed in
30
+ out = `${this.val}`;
31
+ } else if (val === undefined || val === null) {
29
32
  out = '';
30
33
  } else if (Array.isArray(val)) {
31
34
  out = val.join(',');
package/src/util.ts CHANGED
@@ -133,4 +133,35 @@ export class Util {
133
133
  return () => true;
134
134
  }
135
135
  }
136
+
137
+ /**
138
+ * Encode JSON value as base64 encoded string
139
+ */
140
+ static encodeSafeJSON<T>(value: T | undefined): string | undefined {
141
+ if (value === undefined) {
142
+ return;
143
+ }
144
+ const res = JSON.stringify(value);
145
+ return Buffer.from(res, 'utf8').toString('base64');
146
+ }
147
+
148
+ /**
149
+ * Decode JSON value from base64 encoded string
150
+ */
151
+ static decodeSafeJSON<T>(input: string): T;
152
+ static decodeSafeJSON<T>(input?: string | undefined): T | undefined;
153
+ static decodeSafeJSON<T>(input?: string | undefined): T | undefined {
154
+ if (!input) {
155
+ return undefined;
156
+ }
157
+
158
+ let decoded = Buffer.from(input, 'base64').toString('utf8');
159
+
160
+ // Read from encoded if it happens
161
+ if (decoded.startsWith('%')) {
162
+ decoded = decodeURIComponent(decoded);
163
+ }
164
+
165
+ return JSON.parse(decoded, undefined);
166
+ }
136
167
  }