@safeaccess/inline 0.1.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/.gitattributes +16 -0
- package/.gitkeep +0 -0
- package/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/README.md +454 -0
- package/benchmarks/get.bench.ts +26 -0
- package/benchmarks/parse.bench.ts +41 -0
- package/dist/accessors/abstract-accessor.d.ts +213 -0
- package/dist/accessors/abstract-accessor.js +294 -0
- package/dist/accessors/formats/any-accessor.d.ts +35 -0
- package/dist/accessors/formats/any-accessor.js +44 -0
- package/dist/accessors/formats/array-accessor.d.ts +26 -0
- package/dist/accessors/formats/array-accessor.js +39 -0
- package/dist/accessors/formats/env-accessor.d.ts +27 -0
- package/dist/accessors/formats/env-accessor.js +64 -0
- package/dist/accessors/formats/ini-accessor.d.ts +41 -0
- package/dist/accessors/formats/ini-accessor.js +109 -0
- package/dist/accessors/formats/json-accessor.d.ts +26 -0
- package/dist/accessors/formats/json-accessor.js +56 -0
- package/dist/accessors/formats/ndjson-accessor.d.ts +28 -0
- package/dist/accessors/formats/ndjson-accessor.js +71 -0
- package/dist/accessors/formats/object-accessor.d.ts +48 -0
- package/dist/accessors/formats/object-accessor.js +90 -0
- package/dist/accessors/formats/xml-accessor.d.ts +27 -0
- package/dist/accessors/formats/xml-accessor.js +52 -0
- package/dist/accessors/formats/yaml-accessor.d.ts +29 -0
- package/dist/accessors/formats/yaml-accessor.js +46 -0
- package/dist/contracts/accessors-interface.d.ts +11 -0
- package/dist/contracts/accessors-interface.js +1 -0
- package/dist/contracts/factory-accessors-interface.d.ts +16 -0
- package/dist/contracts/factory-accessors-interface.js +1 -0
- package/dist/contracts/parse-integration-interface.d.ts +31 -0
- package/dist/contracts/parse-integration-interface.js +1 -0
- package/dist/contracts/path-cache-interface.d.ts +40 -0
- package/dist/contracts/path-cache-interface.js +1 -0
- package/dist/contracts/readable-accessors-interface.d.ts +79 -0
- package/dist/contracts/readable-accessors-interface.js +1 -0
- package/dist/contracts/security-guard-interface.d.ts +40 -0
- package/dist/contracts/security-guard-interface.js +1 -0
- package/dist/contracts/security-parser-interface.d.ts +67 -0
- package/dist/contracts/security-parser-interface.js +1 -0
- package/dist/contracts/writable-accessors-interface.d.ts +65 -0
- package/dist/contracts/writable-accessors-interface.js +1 -0
- package/dist/core/dot-notation-parser.d.ts +204 -0
- package/dist/core/dot-notation-parser.js +343 -0
- package/dist/exceptions/accessor-exception.d.ts +13 -0
- package/dist/exceptions/accessor-exception.js +16 -0
- package/dist/exceptions/invalid-format-exception.d.ts +14 -0
- package/dist/exceptions/invalid-format-exception.js +17 -0
- package/dist/exceptions/parser-exception.d.ts +14 -0
- package/dist/exceptions/parser-exception.js +17 -0
- package/dist/exceptions/path-not-found-exception.d.ts +14 -0
- package/dist/exceptions/path-not-found-exception.js +17 -0
- package/dist/exceptions/readonly-violation-exception.d.ts +15 -0
- package/dist/exceptions/readonly-violation-exception.js +18 -0
- package/dist/exceptions/security-exception.d.ts +18 -0
- package/dist/exceptions/security-exception.js +21 -0
- package/dist/exceptions/unsupported-type-exception.d.ts +14 -0
- package/dist/exceptions/unsupported-type-exception.js +17 -0
- package/dist/exceptions/yaml-parse-exception.d.ts +17 -0
- package/dist/exceptions/yaml-parse-exception.js +20 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +30 -0
- package/dist/inline.d.ts +402 -0
- package/dist/inline.js +512 -0
- package/dist/parser/xml-parser.d.ts +46 -0
- package/dist/parser/xml-parser.js +288 -0
- package/dist/parser/yaml-parser.d.ts +94 -0
- package/dist/parser/yaml-parser.js +286 -0
- package/dist/security/forbidden-keys.d.ts +34 -0
- package/dist/security/forbidden-keys.js +80 -0
- package/dist/security/security-guard.d.ts +94 -0
- package/dist/security/security-guard.js +172 -0
- package/dist/security/security-parser.d.ts +130 -0
- package/dist/security/security-parser.js +192 -0
- package/dist/type-format.d.ts +28 -0
- package/dist/type-format.js +29 -0
- package/eslint.config.js +1 -0
- package/package.json +39 -0
- package/src/accessors/abstract-accessor.ts +353 -0
- package/src/accessors/formats/any-accessor.ts +51 -0
- package/src/accessors/formats/array-accessor.ts +45 -0
- package/src/accessors/formats/env-accessor.ts +79 -0
- package/src/accessors/formats/ini-accessor.ts +124 -0
- package/src/accessors/formats/json-accessor.ts +66 -0
- package/src/accessors/formats/ndjson-accessor.ts +82 -0
- package/src/accessors/formats/object-accessor.ts +100 -0
- package/src/accessors/formats/xml-accessor.ts +58 -0
- package/src/accessors/formats/yaml-accessor.ts +52 -0
- package/src/contracts/accessors-interface.ts +12 -0
- package/src/contracts/factory-accessors-interface.ts +16 -0
- package/src/contracts/parse-integration-interface.ts +32 -0
- package/src/contracts/path-cache-interface.ts +43 -0
- package/src/contracts/readable-accessors-interface.ts +88 -0
- package/src/contracts/security-guard-interface.ts +43 -0
- package/src/contracts/security-parser-interface.ts +74 -0
- package/src/contracts/writable-accessors-interface.ts +70 -0
- package/src/core/dot-notation-parser.ts +419 -0
- package/src/exceptions/accessor-exception.ts +16 -0
- package/src/exceptions/invalid-format-exception.ts +18 -0
- package/src/exceptions/parser-exception.ts +18 -0
- package/src/exceptions/path-not-found-exception.ts +18 -0
- package/src/exceptions/readonly-violation-exception.ts +19 -0
- package/src/exceptions/security-exception.ts +22 -0
- package/src/exceptions/unsupported-type-exception.ts +18 -0
- package/src/exceptions/yaml-parse-exception.ts +21 -0
- package/src/index.ts +46 -0
- package/src/inline.ts +570 -0
- package/src/parser/xml-parser.ts +334 -0
- package/src/parser/yaml-parser.ts +368 -0
- package/src/security/forbidden-keys.ts +81 -0
- package/src/security/security-guard.ts +195 -0
- package/src/security/security-parser.ts +233 -0
- package/src/type-format.ts +28 -0
- package/stryker.config.json +24 -0
- package/tests/accessors/accessors.test.ts +1017 -0
- package/tests/accessors/json-accessor.test.ts +171 -0
- package/tests/core/dot-notation-parser.test.ts +587 -0
- package/tests/exceptions/parser-exception.test.ts +31 -0
- package/tests/inline.test.ts +445 -0
- package/tests/mocks/fake-parse-integration.ts +24 -0
- package/tests/mocks/fake-path-cache.ts +31 -0
- package/tests/parity.test.ts +164 -0
- package/tests/parser/xml-parser.test.ts +618 -0
- package/tests/parser/yaml-parser.test.ts +463 -0
- package/tests/security/security-guard.test.ts +646 -0
- package/tests/security/security-parser.test.ts +391 -0
- package/tsconfig.json +16 -0
- package/vitest.config.ts +19 -0
package/src/inline.ts
ADDED
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
import { TypeFormat } from './type-format.js';
|
|
2
|
+
import { DotNotationParser } from './core/dot-notation-parser.js';
|
|
3
|
+
import { SecurityGuard } from './security/security-guard.js';
|
|
4
|
+
import { SecurityParser } from './security/security-parser.js';
|
|
5
|
+
import type { SecurityGuardInterface } from './contracts/security-guard-interface.js';
|
|
6
|
+
import type { SecurityParserInterface } from './contracts/security-parser-interface.js';
|
|
7
|
+
import type { AccessorsInterface } from './contracts/accessors-interface.js';
|
|
8
|
+
import type { ParseIntegrationInterface } from './contracts/parse-integration-interface.js';
|
|
9
|
+
import type { PathCacheInterface } from './contracts/path-cache-interface.js';
|
|
10
|
+
import { AbstractAccessor } from './accessors/abstract-accessor.js';
|
|
11
|
+
import { ArrayAccessor } from './accessors/formats/array-accessor.js';
|
|
12
|
+
import { ObjectAccessor } from './accessors/formats/object-accessor.js';
|
|
13
|
+
import { JsonAccessor } from './accessors/formats/json-accessor.js';
|
|
14
|
+
import { XmlAccessor } from './accessors/formats/xml-accessor.js';
|
|
15
|
+
import { YamlAccessor } from './accessors/formats/yaml-accessor.js';
|
|
16
|
+
import { IniAccessor } from './accessors/formats/ini-accessor.js';
|
|
17
|
+
import { EnvAccessor } from './accessors/formats/env-accessor.js';
|
|
18
|
+
import { NdjsonAccessor } from './accessors/formats/ndjson-accessor.js';
|
|
19
|
+
import { AnyAccessor } from './accessors/formats/any-accessor.js';
|
|
20
|
+
import { UnsupportedTypeException } from './exceptions/unsupported-type-exception.js';
|
|
21
|
+
import { InvalidFormatException } from './exceptions/invalid-format-exception.js';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Facade for creating typed data accessors fluently.
|
|
25
|
+
*
|
|
26
|
+
* All static factory methods return a strongly-typed accessor instance.
|
|
27
|
+
* Use the builder methods (`withSecurityGuard`, `withSecurityParser`) to
|
|
28
|
+
* customize the security configuration before creating an accessor.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* const accessor = Inline.fromJson('{"name":"Alice"}');
|
|
32
|
+
* accessor.get('name'); // 'Alice'
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const accessor = Inline.from(TypeFormat.Yaml, 'name: Alice');
|
|
36
|
+
* accessor.get('name'); // 'Alice'
|
|
37
|
+
*/
|
|
38
|
+
export class Inline {
|
|
39
|
+
private readonly guard: SecurityGuardInterface;
|
|
40
|
+
private readonly secParser: SecurityParserInterface;
|
|
41
|
+
private readonly pathCache: PathCacheInterface | null;
|
|
42
|
+
private readonly integration: ParseIntegrationInterface | null;
|
|
43
|
+
private readonly strictMode: boolean | null;
|
|
44
|
+
|
|
45
|
+
private constructor(
|
|
46
|
+
guard: SecurityGuardInterface,
|
|
47
|
+
secParser: SecurityParserInterface,
|
|
48
|
+
pathCache: PathCacheInterface | null = null,
|
|
49
|
+
integration: ParseIntegrationInterface | null = null,
|
|
50
|
+
strictMode: boolean | null = null,
|
|
51
|
+
) {
|
|
52
|
+
this.guard = guard;
|
|
53
|
+
this.secParser = secParser;
|
|
54
|
+
this.pathCache = pathCache;
|
|
55
|
+
this.integration = integration;
|
|
56
|
+
this.strictMode = strictMode;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private static defaultInstance(): Inline {
|
|
60
|
+
return new Inline(new SecurityGuard(), new SecurityParser());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private makeParser(): DotNotationParser {
|
|
64
|
+
return new DotNotationParser(this.guard, this.secParser, this.pathCache ?? undefined);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Apply configured strict mode to a new accessor before hydration.
|
|
69
|
+
*
|
|
70
|
+
* @param accessor - Unhydrated accessor instance.
|
|
71
|
+
* @returns Same accessor with strict mode applied if configured.
|
|
72
|
+
*/
|
|
73
|
+
private prepare<T extends AbstractAccessor>(accessor: T): T {
|
|
74
|
+
if (this.strictMode !== null) {
|
|
75
|
+
return accessor.strict(this.strictMode) as T;
|
|
76
|
+
}
|
|
77
|
+
return accessor;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Return a new Inline instance with a custom SecurityGuard, preserving other settings.
|
|
82
|
+
*
|
|
83
|
+
* @param guard - Custom security guard implementation.
|
|
84
|
+
* @returns New Inline builder instance.
|
|
85
|
+
*/
|
|
86
|
+
withSecurityGuard(guard: SecurityGuardInterface): Inline {
|
|
87
|
+
return new Inline(guard, this.secParser, this.pathCache, this.integration, this.strictMode);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Return a new Inline instance with a custom SecurityParser, preserving other settings.
|
|
92
|
+
*
|
|
93
|
+
* @param parser - Custom security parser implementation.
|
|
94
|
+
* @returns New Inline builder instance.
|
|
95
|
+
*/
|
|
96
|
+
withSecurityParser(parser: SecurityParserInterface): Inline {
|
|
97
|
+
return new Inline(this.guard, parser, this.pathCache, this.integration, this.strictMode);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Return a new Inline instance with a custom path cache, preserving other settings.
|
|
102
|
+
*
|
|
103
|
+
* @param cache - Custom path cache implementation.
|
|
104
|
+
* @returns New Inline builder instance.
|
|
105
|
+
*/
|
|
106
|
+
withPathCache(cache: PathCacheInterface): Inline {
|
|
107
|
+
return new Inline(this.guard, this.secParser, cache, this.integration, this.strictMode);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Return a new Inline instance with a custom parser integration, preserving other settings.
|
|
112
|
+
*
|
|
113
|
+
* @param integration - Custom format integration implementation.
|
|
114
|
+
* @returns New Inline builder instance.
|
|
115
|
+
*/
|
|
116
|
+
withParserIntegration(integration: ParseIntegrationInterface): Inline {
|
|
117
|
+
return new Inline(this.guard, this.secParser, this.pathCache, integration, this.strictMode);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Return a new Inline instance with the given strict mode, preserving other settings.
|
|
122
|
+
*
|
|
123
|
+
* @param strict - Whether to enable strict security validation.
|
|
124
|
+
* @returns New Inline builder instance.
|
|
125
|
+
*
|
|
126
|
+
* @security Passing `false` disables all SecurityGuard and SecurityParser
|
|
127
|
+
* validation. Only use with fully trusted, application-controlled input.
|
|
128
|
+
*/
|
|
129
|
+
withStrictMode(strict: boolean): Inline {
|
|
130
|
+
return new Inline(this.guard, this.secParser, this.pathCache, this.integration, strict);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Return a new Inline instance with a custom SecurityGuard.
|
|
135
|
+
*
|
|
136
|
+
* @param guard - Custom security guard implementation.
|
|
137
|
+
* @returns New Inline builder instance.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* Inline.withSecurityGuard(new SecurityGuard(10, ['extraKey'])).fromJson('{}');
|
|
141
|
+
*/
|
|
142
|
+
static withSecurityGuard(guard: SecurityGuardInterface): Inline {
|
|
143
|
+
return new Inline(guard, new SecurityParser());
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Return a new Inline instance with a custom SecurityParser.
|
|
148
|
+
*
|
|
149
|
+
* @param parser - Custom security parser implementation.
|
|
150
|
+
* @returns New Inline builder instance.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* Inline.withSecurityParser(new SecurityParser({ maxDepth: 10 })).fromJson('{}');
|
|
154
|
+
*/
|
|
155
|
+
static withSecurityParser(parser: SecurityParserInterface): Inline {
|
|
156
|
+
return new Inline(new SecurityGuard(), parser);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Return a new Inline instance with a custom path cache.
|
|
161
|
+
*
|
|
162
|
+
* @param cache - Custom path cache implementation.
|
|
163
|
+
* @returns New Inline builder instance.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* const cache: PathCacheInterface = { get: () => null, set: () => {}, has: () => false, clear: () => {} };
|
|
167
|
+
* Inline.withPathCache(cache).fromJson('{"key":"value"}');
|
|
168
|
+
*/
|
|
169
|
+
static withPathCache(cache: PathCacheInterface): Inline {
|
|
170
|
+
return new Inline(new SecurityGuard(), new SecurityParser(), cache);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Return a new Inline instance with a custom parser integration for `fromAny()`.
|
|
175
|
+
*
|
|
176
|
+
* @param integration - Custom format integration implementation.
|
|
177
|
+
* @returns New Inline builder instance.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* Inline.withParserIntegration(new MyCsvIntegration()).fromAny(csvString);
|
|
181
|
+
*/
|
|
182
|
+
static withParserIntegration(integration: ParseIntegrationInterface): Inline {
|
|
183
|
+
return new Inline(new SecurityGuard(), new SecurityParser(), null, integration);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Return a new Inline instance with the given strict mode.
|
|
188
|
+
*
|
|
189
|
+
* @param strict - Whether to enable strict security validation.
|
|
190
|
+
* @returns New Inline builder instance.
|
|
191
|
+
*
|
|
192
|
+
* @security Passing `false` disables all SecurityGuard and SecurityParser
|
|
193
|
+
* validation. Only use with fully trusted, application-controlled input.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* Inline.withStrictMode(false).fromJson(hugePayload).get('key');
|
|
197
|
+
*/
|
|
198
|
+
static withStrictMode(strict: boolean): Inline {
|
|
199
|
+
return new Inline(new SecurityGuard(), new SecurityParser(), null, null, strict);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Create an ArrayAccessor from a plain object or array.
|
|
204
|
+
*
|
|
205
|
+
* @param data - Plain object or array input.
|
|
206
|
+
* @returns Populated ArrayAccessor instance.
|
|
207
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* inline.fromArray({ name: 'Alice' }).get('name'); // 'Alice'
|
|
211
|
+
*/
|
|
212
|
+
fromArray(data: Record<string, unknown> | unknown[]): ArrayAccessor {
|
|
213
|
+
return this.prepare(new ArrayAccessor(this.makeParser())).from(data);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Create an ObjectAccessor from a JavaScript object.
|
|
218
|
+
*
|
|
219
|
+
* @param data - Object input (plain object, class instance, etc.).
|
|
220
|
+
* @returns Populated ObjectAccessor instance.
|
|
221
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* inline.fromObject({ user: { name: 'Alice' } }).get('user.name');
|
|
225
|
+
*/
|
|
226
|
+
fromObject(data: object): ObjectAccessor {
|
|
227
|
+
return this.prepare(new ObjectAccessor(this.makeParser())).from(data);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Create a JsonAccessor from a JSON string.
|
|
232
|
+
*
|
|
233
|
+
* @param data - Raw JSON string.
|
|
234
|
+
* @returns Populated JsonAccessor instance.
|
|
235
|
+
* @throws {InvalidFormatException} When the JSON is malformed.
|
|
236
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* inline.fromJson('{"key":"value"}').get('key'); // 'value'
|
|
240
|
+
*/
|
|
241
|
+
fromJson(data: string): JsonAccessor {
|
|
242
|
+
return this.prepare(new JsonAccessor(this.makeParser())).from(data);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Create an XmlAccessor from an XML string.
|
|
247
|
+
*
|
|
248
|
+
* @param data - Raw XML string.
|
|
249
|
+
* @returns Populated XmlAccessor instance.
|
|
250
|
+
* @throws {InvalidFormatException} When the XML is malformed.
|
|
251
|
+
* @throws {SecurityException} When DOCTYPE is detected.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* inline.fromXml('<root><key>value</key></root>').get('key');
|
|
255
|
+
*/
|
|
256
|
+
fromXml(data: string): XmlAccessor {
|
|
257
|
+
return this.prepare(new XmlAccessor(this.makeParser())).from(data);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Create a YamlAccessor from a YAML string.
|
|
262
|
+
*
|
|
263
|
+
* @param data - Raw YAML string.
|
|
264
|
+
* @returns Populated YamlAccessor instance.
|
|
265
|
+
* @throws {YamlParseException} When the YAML is malformed or contains unsafe constructs.
|
|
266
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* inline.fromYaml('name: Alice').get('name'); // 'Alice'
|
|
270
|
+
*/
|
|
271
|
+
fromYaml(data: string): YamlAccessor {
|
|
272
|
+
return this.prepare(new YamlAccessor(this.makeParser())).from(data);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Create an IniAccessor from an INI string.
|
|
277
|
+
*
|
|
278
|
+
* @param data - Raw INI string.
|
|
279
|
+
* @returns Populated IniAccessor instance.
|
|
280
|
+
* @throws {InvalidFormatException} When the input is not a string.
|
|
281
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* inline.fromIni('[section]\nkey=value').get('section.key'); // 'value'
|
|
285
|
+
*/
|
|
286
|
+
fromIni(data: string): IniAccessor {
|
|
287
|
+
return this.prepare(new IniAccessor(this.makeParser())).from(data);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Create an EnvAccessor from a dotenv-formatted string.
|
|
292
|
+
*
|
|
293
|
+
* @param data - Raw dotenv string.
|
|
294
|
+
* @returns Populated EnvAccessor instance.
|
|
295
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* inline.fromEnv('APP_NAME=MyApp').get('APP_NAME'); // 'MyApp'
|
|
299
|
+
*/
|
|
300
|
+
fromEnv(data: string): EnvAccessor {
|
|
301
|
+
return this.prepare(new EnvAccessor(this.makeParser())).from(data);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Create an NdjsonAccessor from a newline-delimited JSON string.
|
|
306
|
+
*
|
|
307
|
+
* @param data - Raw NDJSON string.
|
|
308
|
+
* @returns Populated NdjsonAccessor instance.
|
|
309
|
+
* @throws {InvalidFormatException} When any JSON line is malformed.
|
|
310
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* inline.fromNdjson('{"id":1}\n{"id":2}').get('0.id'); // 1
|
|
314
|
+
*/
|
|
315
|
+
fromNdjson(data: string): NdjsonAccessor {
|
|
316
|
+
return this.prepare(new NdjsonAccessor(this.makeParser())).from(data);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Create an AnyAccessor from raw data using a custom integration.
|
|
321
|
+
*
|
|
322
|
+
* Uses the integration provided via `withParserIntegration()` by default,
|
|
323
|
+
* or the one passed as the second argument for a one-off override.
|
|
324
|
+
*
|
|
325
|
+
* @param data - Raw input data in any format supported by the integration.
|
|
326
|
+
* @param integration - Override integration for this call (optional).
|
|
327
|
+
* @returns Populated AnyAccessor instance.
|
|
328
|
+
* @throws {InvalidFormatException} When no integration is available or it rejects the format.
|
|
329
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* Inline.withParserIntegration(new CsvIntegration()).fromAny(csvString);
|
|
333
|
+
*/
|
|
334
|
+
fromAny(data: unknown, integration?: ParseIntegrationInterface): AnyAccessor {
|
|
335
|
+
const resolved = integration ?? this.integration;
|
|
336
|
+
if (resolved === null) {
|
|
337
|
+
throw new InvalidFormatException(
|
|
338
|
+
'AnyAccessor requires a ParseIntegrationInterface — use Inline.withParserIntegration(integration).fromAny(data).',
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
return this.prepare(new AnyAccessor(this.makeParser(), resolved)).from(data);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Create a typed accessor by its constructor.
|
|
346
|
+
*
|
|
347
|
+
* @param AccessorConstructor - The accessor class to instantiate.
|
|
348
|
+
* @param data - Raw data to hydrate the accessor with.
|
|
349
|
+
* @returns Populated accessor instance.
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
* Inline.make(JsonAccessor, '{"key":"value"}').get('key'); // 'value'
|
|
353
|
+
*/
|
|
354
|
+
make<T extends AccessorsInterface>(
|
|
355
|
+
AccessorConstructor: new (parser: DotNotationParser) => T,
|
|
356
|
+
data: unknown,
|
|
357
|
+
): T {
|
|
358
|
+
const accessor = new AccessorConstructor(this.makeParser());
|
|
359
|
+
if (this.strictMode !== null && accessor instanceof AbstractAccessor) {
|
|
360
|
+
return (accessor.strict(this.strictMode) as T).from(data);
|
|
361
|
+
}
|
|
362
|
+
return accessor.from(data);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Create an accessor for the given TypeFormat and raw data.
|
|
367
|
+
*
|
|
368
|
+
* @param typeFormat - The format to parse as.
|
|
369
|
+
* @param data - Raw input data.
|
|
370
|
+
* @returns Populated accessor instance.
|
|
371
|
+
* @throws {InvalidFormatException} When the data is malformed for the target format.
|
|
372
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
373
|
+
* @throws {UnsupportedTypeException} When the TypeFormat is not supported.
|
|
374
|
+
*/
|
|
375
|
+
from(typeFormat: TypeFormat, data: unknown): AccessorsInterface {
|
|
376
|
+
switch (typeFormat) {
|
|
377
|
+
case TypeFormat.Array:
|
|
378
|
+
return this.fromArray(data as Record<string, unknown> | unknown[]);
|
|
379
|
+
case TypeFormat.Object:
|
|
380
|
+
return this.fromObject(data as object);
|
|
381
|
+
case TypeFormat.Json:
|
|
382
|
+
return this.fromJson(data as string);
|
|
383
|
+
case TypeFormat.Xml:
|
|
384
|
+
return this.fromXml(data as string);
|
|
385
|
+
case TypeFormat.Yaml:
|
|
386
|
+
return this.fromYaml(data as string);
|
|
387
|
+
case TypeFormat.Ini:
|
|
388
|
+
return this.fromIni(data as string);
|
|
389
|
+
case TypeFormat.Env:
|
|
390
|
+
return this.fromEnv(data as string);
|
|
391
|
+
case TypeFormat.Ndjson:
|
|
392
|
+
return this.fromNdjson(data as string);
|
|
393
|
+
case TypeFormat.Any:
|
|
394
|
+
return this.fromAny(data);
|
|
395
|
+
default: {
|
|
396
|
+
const exhaustive: never = typeFormat;
|
|
397
|
+
throw new UnsupportedTypeException(
|
|
398
|
+
`TypeFormat '${String(exhaustive)}' is not supported.`,
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Create an ArrayAccessor from a plain object or array.
|
|
406
|
+
*
|
|
407
|
+
* @param data - Plain object or array input.
|
|
408
|
+
* @returns Populated ArrayAccessor instance.
|
|
409
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
410
|
+
*
|
|
411
|
+
* @example
|
|
412
|
+
* Inline.fromArray({ name: 'Alice' }).get('name'); // 'Alice'
|
|
413
|
+
*/
|
|
414
|
+
static fromArray(data: Record<string, unknown> | unknown[]): ArrayAccessor {
|
|
415
|
+
return Inline.defaultInstance().fromArray(data);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Create an ObjectAccessor from a JavaScript object.
|
|
420
|
+
*
|
|
421
|
+
* @param data - Object input (plain object, class instance, etc.).
|
|
422
|
+
* @returns Populated ObjectAccessor instance.
|
|
423
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* Inline.fromObject({ user: { name: 'Alice' } }).get('user.name');
|
|
427
|
+
*/
|
|
428
|
+
static fromObject(data: object): ObjectAccessor {
|
|
429
|
+
return Inline.defaultInstance().fromObject(data);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Create a JsonAccessor from a JSON string.
|
|
434
|
+
*
|
|
435
|
+
* @param data - Raw JSON string.
|
|
436
|
+
* @returns Populated JsonAccessor instance.
|
|
437
|
+
* @throws {InvalidFormatException} When the JSON is malformed.
|
|
438
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* Inline.fromJson('{"key":"value"}').get('key'); // 'value'
|
|
442
|
+
*/
|
|
443
|
+
static fromJson(data: string): JsonAccessor {
|
|
444
|
+
return Inline.defaultInstance().fromJson(data);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Create an XmlAccessor from an XML string.
|
|
449
|
+
*
|
|
450
|
+
* @param data - Raw XML string.
|
|
451
|
+
* @returns Populated XmlAccessor instance.
|
|
452
|
+
* @throws {InvalidFormatException} When the XML is malformed.
|
|
453
|
+
* @throws {SecurityException} When DOCTYPE is detected.
|
|
454
|
+
*
|
|
455
|
+
* @example
|
|
456
|
+
* Inline.fromXml('<root><key>value</key></root>').get('key');
|
|
457
|
+
*/
|
|
458
|
+
static fromXml(data: string): XmlAccessor {
|
|
459
|
+
return Inline.defaultInstance().fromXml(data);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Create a YamlAccessor from a YAML string.
|
|
464
|
+
*
|
|
465
|
+
* @param data - Raw YAML string.
|
|
466
|
+
* @returns Populated YamlAccessor instance.
|
|
467
|
+
* @throws {YamlParseException} When the YAML is malformed or contains unsafe constructs.
|
|
468
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
469
|
+
*
|
|
470
|
+
* @example
|
|
471
|
+
* Inline.fromYaml('name: Alice\nage: 30').get('name'); // 'Alice'
|
|
472
|
+
*/
|
|
473
|
+
static fromYaml(data: string): YamlAccessor {
|
|
474
|
+
return Inline.defaultInstance().fromYaml(data);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Create an IniAccessor from an INI string.
|
|
479
|
+
*
|
|
480
|
+
* @param data - Raw INI string.
|
|
481
|
+
* @returns Populated IniAccessor instance.
|
|
482
|
+
* @throws {InvalidFormatException} When the input is not a string.
|
|
483
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* Inline.fromIni('[section]\nkey=value').get('section.key'); // 'value'
|
|
487
|
+
*/
|
|
488
|
+
static fromIni(data: string): IniAccessor {
|
|
489
|
+
return Inline.defaultInstance().fromIni(data);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Create an EnvAccessor from a dotenv-formatted string.
|
|
494
|
+
*
|
|
495
|
+
* @param data - Raw dotenv string.
|
|
496
|
+
* @returns Populated EnvAccessor instance.
|
|
497
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* Inline.fromEnv('APP_NAME=MyApp\nDEBUG=true').get('APP_NAME'); // 'MyApp'
|
|
501
|
+
*/
|
|
502
|
+
static fromEnv(data: string): EnvAccessor {
|
|
503
|
+
return Inline.defaultInstance().fromEnv(data);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Create an NdjsonAccessor from a newline-delimited JSON string.
|
|
508
|
+
*
|
|
509
|
+
* @param data - Raw NDJSON string.
|
|
510
|
+
* @returns Populated NdjsonAccessor instance.
|
|
511
|
+
* @throws {InvalidFormatException} When any JSON line is malformed.
|
|
512
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
513
|
+
*
|
|
514
|
+
* @example
|
|
515
|
+
* Inline.fromNdjson('{"id":1}\n{"id":2}').get('0.id'); // 1
|
|
516
|
+
*/
|
|
517
|
+
static fromNdjson(data: string): NdjsonAccessor {
|
|
518
|
+
return Inline.defaultInstance().fromNdjson(data);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Create an accessor for the given TypeFormat and raw data.
|
|
523
|
+
*
|
|
524
|
+
* @param typeFormat - The format to parse as.
|
|
525
|
+
* @param data - Raw input data.
|
|
526
|
+
* @returns Populated accessor instance.
|
|
527
|
+
* @throws {InvalidFormatException} When the data is malformed for the target format.
|
|
528
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
529
|
+
* @throws {UnsupportedTypeException} When the TypeFormat is not supported.
|
|
530
|
+
*
|
|
531
|
+
* @example
|
|
532
|
+
* Inline.from(TypeFormat.Json, '{"key":"value"}').get('key'); // 'value'
|
|
533
|
+
*/
|
|
534
|
+
static from(typeFormat: TypeFormat, data: unknown): AccessorsInterface {
|
|
535
|
+
return Inline.defaultInstance().from(typeFormat, data);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Create an AnyAccessor from raw data using a custom integration.
|
|
540
|
+
*
|
|
541
|
+
* @param data - Raw input data.
|
|
542
|
+
* @param integration - Integration that detects and parses the format (optional if set via `withParserIntegration`).
|
|
543
|
+
* @returns Populated AnyAccessor instance.
|
|
544
|
+
* @throws {InvalidFormatException} When no integration is available or it rejects the format.
|
|
545
|
+
* @throws {SecurityException} When security constraints are violated.
|
|
546
|
+
*
|
|
547
|
+
* @example
|
|
548
|
+
* Inline.fromAny(csvString, new CsvIntegration()).get('0.name');
|
|
549
|
+
*/
|
|
550
|
+
static fromAny(data: unknown, integration?: ParseIntegrationInterface): AnyAccessor {
|
|
551
|
+
return Inline.defaultInstance().fromAny(data, integration);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Create a typed accessor by its constructor.
|
|
556
|
+
*
|
|
557
|
+
* @param AccessorConstructor - The accessor class to instantiate.
|
|
558
|
+
* @param data - Raw data to hydrate the accessor with.
|
|
559
|
+
* @returns Populated accessor instance.
|
|
560
|
+
*
|
|
561
|
+
* @example
|
|
562
|
+
* Inline.make(JsonAccessor, '{"key":"value"}').get('key'); // 'value'
|
|
563
|
+
*/
|
|
564
|
+
static make<T extends AccessorsInterface>(
|
|
565
|
+
AccessorConstructor: new (parser: DotNotationParser) => T,
|
|
566
|
+
data: unknown,
|
|
567
|
+
): T {
|
|
568
|
+
return Inline.defaultInstance().make(AccessorConstructor, data);
|
|
569
|
+
}
|
|
570
|
+
}
|