@skmtc/core 0.49.0 → 0.49.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.
@@ -88,7 +88,13 @@ _ResultsLog_results = new WeakMap(), _ResultsLog_instances = new WeakSet(), _Res
88
88
  };
89
89
  /** Writes an own, enumerable property, even for keys such as `__proto__`. */
90
90
  const defineOwn = (node, key, value) => {
91
- Object.defineProperty(node, key, { value, enumerable: true, writable: true, configurable: true });
91
+ // An assignment to `__proto__` would set the prototype, so that key is defined.
92
+ if (key === '__proto__') {
93
+ Object.defineProperty(node, key, { value, enumerable: true, writable: true, configurable: true });
94
+ }
95
+ else {
96
+ node[key] = value;
97
+ }
92
98
  };
93
99
  /**
94
100
  * Severity of each result type. A higher number replaces a lower one
@@ -71,6 +71,7 @@ export const toRefPathSegments = (refPath) => {
71
71
  const body = refPath.startsWith('#/') ? refPath.slice(2) : refPath.slice(1);
72
72
  return body === '' ? [] : body.split('/').map(unescapeSegment);
73
73
  };
74
+ const COMPONENTS_PATH = '#/components/';
74
75
  /**
75
76
  * The `components` bucket a `refPath` is, such as `schemas` for
76
77
  * `#/components/schemas`, or `undefined` for any other path. A ref whose path
@@ -83,6 +84,13 @@ export const toRefPathBucket = (refPath) => {
83
84
  if (isRemoteRefPath(refPath)) {
84
85
  return undefined;
85
86
  }
87
+ // The usual path, `#/components/<bucket>`, with nothing to split or unescape.
88
+ if (refPath.startsWith(COMPONENTS_PATH)) {
89
+ const bucket = refPath.slice(COMPONENTS_PATH.length);
90
+ if (!bucket.includes('/') && !bucket.includes('~')) {
91
+ return isBucket(bucket) ? bucket : undefined;
92
+ }
93
+ }
86
94
  const [first, bucket, ...rest] = toRefPathSegments(refPath);
87
95
  return first === 'components' && rest.length === 0 && isBucket(bucket) ? bucket : undefined;
88
96
  };
@@ -153,6 +161,9 @@ export const toRawRefTarget = (document, { refName, refPath }) => isRemoteRefPat
153
161
  return isPlainObject(value) && Object.hasOwn(value, segment) ? value[segment] : undefined;
154
162
  }, document);
155
163
  const toPercentDecoded = (segment) => {
164
+ if (!segment.includes('%')) {
165
+ return segment;
166
+ }
156
167
  try {
157
168
  return decodeURIComponent(segment);
158
169
  }
@@ -4,34 +4,45 @@ import { isRef, toRawRefTarget, toRefParts, toRefPathBucket, toRefPointer, toSch
4
4
  const DATA_KEYWORDS = ['default', 'example', 'examples', 'enum', 'const'];
5
5
  /** Keywords whose value is a map of names to schemas: a name in it is not a keyword. */
6
6
  const SCHEMA_MAP_KEYWORDS = ['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas'];
7
- /** Every `$ref` object anywhere inside a raw schema. */
8
- const toSchemaRefs = (value) => {
7
+ /** Every `$ref` object anywhere inside a raw schema, added to `found` in document order. */
8
+ const toSchemaRefs = (value, found = []) => {
9
9
  if (Array.isArray(value)) {
10
- return value.flatMap(toSchemaRefs);
10
+ value.forEach(item => toSchemaRefs(item, found));
11
+ return found;
11
12
  }
12
13
  if (!isPlainObject(value)) {
13
- return [];
14
+ return found;
14
15
  }
15
- const nested = Object.entries(value).flatMap(([key, child]) => {
16
+ if (isRef(value)) {
17
+ found.push(value);
18
+ }
19
+ Object.entries(value).forEach(([key, child]) => {
16
20
  if (DATA_KEYWORDS.includes(key) || key.startsWith('x-')) {
17
- return [];
21
+ return;
22
+ }
23
+ if (SCHEMA_MAP_KEYWORDS.includes(key) && isPlainObject(child)) {
24
+ Object.values(child).forEach(member => toSchemaRefs(member, found));
25
+ }
26
+ else {
27
+ toSchemaRefs(child, found);
18
28
  }
19
- return SCHEMA_MAP_KEYWORDS.includes(key) && isPlainObject(child)
20
- ? Object.values(child).flatMap(toSchemaRefs)
21
- : toSchemaRefs(child);
22
29
  });
23
- return isRef(value) ? [value, ...nested] : nested;
30
+ return found;
24
31
  };
25
- /** Every `$ref` object anywhere in a raw document. */
26
- const toDocumentRefs = (value) => {
32
+ /** Every `$ref` object anywhere in a raw document, added to `found` in document order. */
33
+ const toDocumentRefs = (value, found = []) => {
27
34
  if (Array.isArray(value)) {
28
- return value.flatMap(toDocumentRefs);
35
+ value.forEach(item => toDocumentRefs(item, found));
36
+ return found;
29
37
  }
30
38
  if (!isPlainObject(value)) {
31
- return [];
39
+ return found;
40
+ }
41
+ if (isRef(value)) {
42
+ found.push(value);
32
43
  }
33
- const nested = Object.values(value).flatMap(toDocumentRefs);
34
- return isRef(value) ? [value, ...nested] : nested;
44
+ Object.values(value).forEach(child => toDocumentRefs(child, found));
45
+ return found;
35
46
  };
36
47
  /** Whether a ref can point at a schema: not a remote one, and not one to another kind of component. */
37
48
  const canNameSchema = ({ refName, refPath }) => {
@@ -14,14 +14,14 @@
14
14
  * `~` → `~0` (must be replaced first to avoid double-escaping)
15
15
  * `/` → `~1`
16
16
  */
17
- export const escapeSegment = (segment) => segment.replaceAll('~', '~0').replaceAll('/', '~1');
17
+ export const escapeSegment = (segment) => segment.includes('~') || segment.includes('/') ? segment.replaceAll('~', '~0').replaceAll('/', '~1') : segment;
18
18
  /**
19
19
  * Unescape a single segment per RFC 6901 §4.
20
20
  *
21
21
  * `~1` → `/` (must be replaced first)
22
22
  * `~0` → `~`
23
23
  */
24
- export const unescapeSegment = (segment) => segment.replaceAll('~1', '/').replaceAll('~0', '~');
24
+ export const unescapeSegment = (segment) => segment.includes('~') ? segment.replaceAll('~1', '/').replaceAll('~0', '~') : segment;
25
25
  /**
26
26
  * Build a JSON Pointer from an array of unescaped segments.
27
27
  * Returns the URI fragment form: `#/<seg1>/<seg2>/...`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skmtc/core",
3
- "version": "0.49.0",
3
+ "version": "0.49.1",
4
4
  "description": "Skmtc is a declarative code generation framework",
5
5
  "keywords": [
6
6
  "openapi",
@@ -92,7 +92,13 @@ _ResultsLog_results = new WeakMap(), _ResultsLog_instances = new WeakSet(), _Res
92
92
  };
93
93
  /** Writes an own, enumerable property, even for keys such as `__proto__`. */
94
94
  const defineOwn = (node, key, value) => {
95
- Object.defineProperty(node, key, { value, enumerable: true, writable: true, configurable: true });
95
+ // An assignment to `__proto__` would set the prototype, so that key is defined.
96
+ if (key === '__proto__') {
97
+ Object.defineProperty(node, key, { value, enumerable: true, writable: true, configurable: true });
98
+ }
99
+ else {
100
+ node[key] = value;
101
+ }
96
102
  };
97
103
  /**
98
104
  * Severity of each result type. A higher number replaces a lower one
@@ -78,6 +78,7 @@ const toRefPathSegments = (refPath) => {
78
78
  return body === '' ? [] : body.split('/').map(JsonPointer_js_1.unescapeSegment);
79
79
  };
80
80
  exports.toRefPathSegments = toRefPathSegments;
81
+ const COMPONENTS_PATH = '#/components/';
81
82
  /**
82
83
  * The `components` bucket a `refPath` is, such as `schemas` for
83
84
  * `#/components/schemas`, or `undefined` for any other path. A ref whose path
@@ -90,6 +91,13 @@ const toRefPathBucket = (refPath) => {
90
91
  if ((0, exports.isRemoteRefPath)(refPath)) {
91
92
  return undefined;
92
93
  }
94
+ // The usual path, `#/components/<bucket>`, with nothing to split or unescape.
95
+ if (refPath.startsWith(COMPONENTS_PATH)) {
96
+ const bucket = refPath.slice(COMPONENTS_PATH.length);
97
+ if (!bucket.includes('/') && !bucket.includes('~')) {
98
+ return isBucket(bucket) ? bucket : undefined;
99
+ }
100
+ }
93
101
  const [first, bucket, ...rest] = (0, exports.toRefPathSegments)(refPath);
94
102
  return first === 'components' && rest.length === 0 && isBucket(bucket) ? bucket : undefined;
95
103
  };
@@ -169,6 +177,9 @@ const toRawRefTarget = (document, { refName, refPath }) => (0, exports.isRemoteR
169
177
  }, document);
170
178
  exports.toRawRefTarget = toRawRefTarget;
171
179
  const toPercentDecoded = (segment) => {
180
+ if (!segment.includes('%')) {
181
+ return segment;
182
+ }
172
183
  try {
173
184
  return decodeURIComponent(segment);
174
185
  }
@@ -7,34 +7,45 @@ const refFns_js_1 = require("../helpers/refFns.js");
7
7
  const DATA_KEYWORDS = ['default', 'example', 'examples', 'enum', 'const'];
8
8
  /** Keywords whose value is a map of names to schemas: a name in it is not a keyword. */
9
9
  const SCHEMA_MAP_KEYWORDS = ['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas'];
10
- /** Every `$ref` object anywhere inside a raw schema. */
11
- const toSchemaRefs = (value) => {
10
+ /** Every `$ref` object anywhere inside a raw schema, added to `found` in document order. */
11
+ const toSchemaRefs = (value, found = []) => {
12
12
  if (Array.isArray(value)) {
13
- return value.flatMap(toSchemaRefs);
13
+ value.forEach(item => toSchemaRefs(item, found));
14
+ return found;
14
15
  }
15
16
  if (!(0, isPlainObject_js_1.isPlainObject)(value)) {
16
- return [];
17
+ return found;
17
18
  }
18
- const nested = Object.entries(value).flatMap(([key, child]) => {
19
+ if ((0, refFns_js_1.isRef)(value)) {
20
+ found.push(value);
21
+ }
22
+ Object.entries(value).forEach(([key, child]) => {
19
23
  if (DATA_KEYWORDS.includes(key) || key.startsWith('x-')) {
20
- return [];
24
+ return;
25
+ }
26
+ if (SCHEMA_MAP_KEYWORDS.includes(key) && (0, isPlainObject_js_1.isPlainObject)(child)) {
27
+ Object.values(child).forEach(member => toSchemaRefs(member, found));
28
+ }
29
+ else {
30
+ toSchemaRefs(child, found);
21
31
  }
22
- return SCHEMA_MAP_KEYWORDS.includes(key) && (0, isPlainObject_js_1.isPlainObject)(child)
23
- ? Object.values(child).flatMap(toSchemaRefs)
24
- : toSchemaRefs(child);
25
32
  });
26
- return (0, refFns_js_1.isRef)(value) ? [value, ...nested] : nested;
33
+ return found;
27
34
  };
28
- /** Every `$ref` object anywhere in a raw document. */
29
- const toDocumentRefs = (value) => {
35
+ /** Every `$ref` object anywhere in a raw document, added to `found` in document order. */
36
+ const toDocumentRefs = (value, found = []) => {
30
37
  if (Array.isArray(value)) {
31
- return value.flatMap(toDocumentRefs);
38
+ value.forEach(item => toDocumentRefs(item, found));
39
+ return found;
32
40
  }
33
41
  if (!(0, isPlainObject_js_1.isPlainObject)(value)) {
34
- return [];
42
+ return found;
43
+ }
44
+ if ((0, refFns_js_1.isRef)(value)) {
45
+ found.push(value);
35
46
  }
36
- const nested = Object.values(value).flatMap(toDocumentRefs);
37
- return (0, refFns_js_1.isRef)(value) ? [value, ...nested] : nested;
47
+ Object.values(value).forEach(child => toDocumentRefs(child, found));
48
+ return found;
38
49
  };
39
50
  /** Whether a ref can point at a schema: not a remote one, and not one to another kind of component. */
40
51
  const canNameSchema = ({ refName, refPath }) => {
@@ -17,7 +17,7 @@ exports.isJsonPointer = exports.append = exports.fromJsonPointer = exports.toJso
17
17
  * `~` → `~0` (must be replaced first to avoid double-escaping)
18
18
  * `/` → `~1`
19
19
  */
20
- const escapeSegment = (segment) => segment.replaceAll('~', '~0').replaceAll('/', '~1');
20
+ const escapeSegment = (segment) => segment.includes('~') || segment.includes('/') ? segment.replaceAll('~', '~0').replaceAll('/', '~1') : segment;
21
21
  exports.escapeSegment = escapeSegment;
22
22
  /**
23
23
  * Unescape a single segment per RFC 6901 §4.
@@ -25,7 +25,7 @@ exports.escapeSegment = escapeSegment;
25
25
  * `~1` → `/` (must be replaced first)
26
26
  * `~0` → `~`
27
27
  */
28
- const unescapeSegment = (segment) => segment.replaceAll('~1', '/').replaceAll('~0', '~');
28
+ const unescapeSegment = (segment) => segment.includes('~') ? segment.replaceAll('~1', '/').replaceAll('~0', '~') : segment;
29
29
  exports.unescapeSegment = unescapeSegment;
30
30
  /**
31
31
  * Build a JSON Pointer from an array of unescaped segments.