@amritk/helpers 0.15.3 → 0.16.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.
Files changed (41) hide show
  1. package/dist/assert-id-scopes.js +9 -8
  2. package/dist/assign-key.d.ts +17 -0
  3. package/dist/assign-key.js +10 -0
  4. package/dist/build-anchor-map.js +8 -8
  5. package/dist/build-dynamic-ref-map.js +10 -10
  6. package/dist/build-resource-registry.d.ts +54 -0
  7. package/dist/build-resource-registry.js +35 -12
  8. package/dist/derive-root-type-name.js +9 -5
  9. package/dist/escape-regex-pattern.js +42 -35
  10. package/dist/extract-refs.js +11 -6
  11. package/dist/fold-nullable.js +17 -10
  12. package/dist/generate-index-barrel.js +7 -11
  13. package/dist/generate-type-definition.js +156 -138
  14. package/dist/graft-external-schemas.js +3 -2
  15. package/dist/has-ref.js +1 -1
  16. package/dist/max-schema-depth.d.ts +8 -1
  17. package/dist/max-schema-depth.js +3 -3
  18. package/dist/mjst-extension.js +3 -2
  19. package/dist/normalize-ref-scopes.js +12 -6
  20. package/dist/parse-documentation.js +1 -1
  21. package/dist/prune-external-schemas.js +10 -8
  22. package/dist/quote-js-string.js +3 -1
  23. package/dist/read-key.d.ts +25 -0
  24. package/dist/read-key.js +6 -0
  25. package/dist/ref-to-name.d.ts +41 -0
  26. package/dist/ref-to-name.js +11 -5
  27. package/dist/resolve-dynamic-refs.js +19 -12
  28. package/dist/resolve-ref.js +2 -1
  29. package/dist/safe-accessor.d.ts +1 -1
  30. package/dist/safe-accessor.js +1 -1
  31. package/dist/schema-guards.js +34 -34
  32. package/dist/string-length-check.js +2 -2
  33. package/dist/unknown-key-check.d.ts +14 -3
  34. package/dist/unknown-key-check.js +2 -2
  35. package/dist/upgrade-draft07-schema.js +49 -34
  36. package/dist/validate-record.d.ts +6 -1
  37. package/dist/validate-record.js +1 -1
  38. package/dist/walk-ref-graph.js +75 -33
  39. package/package.json +1 -1
  40. package/src/has-ref.ts +1 -1
  41. package/src/validate-record.ts +7 -2
@@ -1,26 +1,33 @@
1
- import { DATA_KEYWORDS } from "./build-resource-registry.js";
1
+ import { assignKey } from "./assign-key.js";
2
+ import { entersSchemaMap, isDataPosition } from "./build-resource-registry.js";
2
3
  import { assertSchemaDepth } from "./max-schema-depth.js";
4
+ import { readKey } from "./read-key.js";
3
5
  import { refToFilename, toKebabCase } from "./ref-to-filename.js";
4
- const isDraft07Schema = (schema) => typeof schema["$schema"] === "string" && schema["$schema"].includes("draft-07");
5
- const rewriteRefs = (obj, refMap, selfRef, depth = 0) => {
6
+ const isDraft07Schema = (schema) => {
7
+ const declared = readKey(schema, "$schema");
8
+ return typeof declared === "string" && declared.includes("draft-07");
9
+ };
10
+ const rewriteRefs = (obj, refMap, selfRef, depth = 0, inSchemaMap = false) => {
6
11
  assertSchemaDepth(depth, "upgradeDraft07Schema");
7
12
  if (typeof obj !== "object" || obj === null)
8
13
  return obj;
9
14
  if (Array.isArray(obj))
10
- return obj.map((item) => rewriteRefs(item, refMap, selfRef, depth + 1));
15
+ return obj.map((item) => rewriteRefs(item, refMap, selfRef, depth + 1, inSchemaMap));
11
16
  const record = obj;
12
17
  const result = {};
13
18
  for (const [key, value] of Object.entries(record)) {
14
- if (key === "$ref" && typeof value === "string") {
19
+ if (isDataPosition(key, inSchemaMap)) {
20
+ assignKey(result, key, value);
21
+ } else if (!inSchemaMap && key === "$ref" && typeof value === "string") {
15
22
  if (refMap.has(value)) {
16
- result[key] = refMap.get(value);
23
+ assignKey(result, key, refMap.get(value));
17
24
  } else if (value === "#" && selfRef) {
18
- result[key] = selfRef;
25
+ assignKey(result, key, selfRef);
19
26
  } else {
20
- result[key] = value;
27
+ assignKey(result, key, value);
21
28
  }
22
29
  } else {
23
- result[key] = rewriteRefs(value, refMap, selfRef, depth + 1);
30
+ assignKey(result, key, rewriteRefs(value, refMap, selfRef, depth + 1, entersSchemaMap(key, inSchemaMap)));
24
31
  }
25
32
  }
26
33
  return result;
@@ -29,13 +36,13 @@ const hoistNestedDefs = (defs) => {
29
36
  const hoisted = {};
30
37
  for (const [parentName, parentSchema] of Object.entries(defs)) {
31
38
  if (typeof parentSchema !== "object" || parentSchema === null) {
32
- hoisted[parentName] = parentSchema;
39
+ assignKey(hoisted, parentName, parentSchema);
33
40
  continue;
34
41
  }
35
42
  const parentObj = parentSchema;
36
- const nestedDefs = parentObj["$defs"];
43
+ const nestedDefs = readKey(parentObj, "$defs");
37
44
  if (!nestedDefs || typeof nestedDefs !== "object") {
38
- hoisted[parentName] = parentSchema;
45
+ assignKey(hoisted, parentName, parentSchema);
39
46
  continue;
40
47
  }
41
48
  const parentPrefix = parentName.startsWith("http://") || parentName.startsWith("https://") ? refToFilename(parentName) : parentName;
@@ -46,10 +53,10 @@ const hoistNestedDefs = (defs) => {
46
53
  }
47
54
  const selfRef = `#/$defs/${parentPrefix}`;
48
55
  const rewrittenParent = rewriteRefs(parentObj, localToHoisted, selfRef);
49
- hoisted[parentName] = rewrittenParent;
56
+ assignKey(hoisted, parentName, rewrittenParent);
50
57
  for (const [localName, localSchema] of Object.entries(nestedDefs)) {
51
58
  const hoistedName = `${parentPrefix}-${toKebabCase(localName)}`;
52
- hoisted[hoistedName] = rewriteRefs(localSchema, localToHoisted, selfRef);
59
+ assignKey(hoisted, hoistedName, rewriteRefs(localSchema, localToHoisted, selfRef));
53
60
  }
54
61
  }
55
62
  return hoisted;
@@ -61,14 +68,14 @@ const upgradeDraft07Schema = (schema) => {
61
68
  const rawDefs = definitions ?? {};
62
69
  const renamedDefs = {};
63
70
  for (const [key, value] of Object.entries(rawDefs)) {
64
- renamedDefs[key] = renameNestedDefs(value);
71
+ assignKey(renamedDefs, key, renameNestedDefs(value));
65
72
  }
66
73
  const hoistedDefs = hoistNestedDefs(renamedDefs);
67
74
  for (const key of Object.keys(hoistedDefs)) {
68
75
  if (key.startsWith("http://") || key.startsWith("https://")) {
69
76
  const shortName = refToFilename(key);
70
- if (shortName && !(shortName in hoistedDefs)) {
71
- hoistedDefs[shortName] = hoistedDefs[key];
77
+ if (shortName && !Object.hasOwn(hoistedDefs, shortName)) {
78
+ assignKey(hoistedDefs, shortName, hoistedDefs[key]);
72
79
  }
73
80
  }
74
81
  }
@@ -77,40 +84,48 @@ const upgradeDraft07Schema = (schema) => {
77
84
  $defs: hoistedDefs
78
85
  };
79
86
  };
80
- const rewriteDefinitionsRefs = (obj, depth = 0) => {
87
+ const rewriteDefinitionsRefs = (obj, depth = 0, inSchemaMap = false) => {
81
88
  assertSchemaDepth(depth, "upgradeDraft07Schema");
82
89
  if (typeof obj !== "object" || obj === null)
83
90
  return obj;
84
91
  if (Array.isArray(obj))
85
- return obj.map((item) => rewriteDefinitionsRefs(item, depth + 1));
92
+ return obj.map((item) => rewriteDefinitionsRefs(item, depth + 1, inSchemaMap));
86
93
  const record = obj;
87
94
  const result = {};
88
- for (const [key, value] of Object.entries(record)) {
89
- const rewritten = key === "$ref" && typeof value === "string" && value.startsWith("#/definitions/") ? value.replace("#/definitions/", "#/$defs/") : DATA_KEYWORDS.has(key) || startsNewResource(value) ? value : rewriteDefinitionsRefs(value, depth + 1);
90
- if (key === "__proto__") {
91
- Object.defineProperty(result, key, { value: rewritten, writable: true, enumerable: true, configurable: true });
92
- } else {
93
- result[key] = rewritten;
94
- }
95
+ for (const [key, value] of Object.entries(record))
96
+ assignKey(result, key, value);
97
+ for (const key of Object.keys(record)) {
98
+ if (isDataPosition(key, inSchemaMap))
99
+ continue;
100
+ const value = record[key];
101
+ const rewritten = !inSchemaMap && key === "$ref" && typeof value === "string" && value.startsWith("#/definitions/") ? value.replace("#/definitions/", "#/$defs/") : startsNewResource(value) ? value : rewriteDefinitionsRefs(value, depth + 1, entersSchemaMap(key, inSchemaMap));
102
+ assignKey(result, key, rewritten);
95
103
  }
96
104
  return result;
97
105
  };
98
106
  const startsNewResource = (value) => typeof value === "object" && value !== null && !Array.isArray(value) && typeof value["$id"] === "string";
99
- const renameNestedDefs = (obj, depth = 0) => {
107
+ const renameNestedDefs = (obj, depth = 0, inSchemaMap = false) => {
100
108
  assertSchemaDepth(depth, "upgradeDraft07Schema");
101
109
  if (typeof obj !== "object" || obj === null)
102
110
  return obj;
103
111
  if (Array.isArray(obj))
104
- return obj.map((item) => renameNestedDefs(item, depth + 1));
112
+ return obj.map((item) => renameNestedDefs(item, depth + 1, inSchemaMap));
105
113
  const record = obj;
106
114
  const result = {};
107
- for (const [key, value] of Object.entries(record)) {
108
- if (key === "$ref" && typeof value === "string" && value.startsWith("#/definitions/")) {
109
- result[key] = value.replace("#/definitions/", "#/$defs/");
110
- } else {
111
- const outKey = key === "definitions" ? "$defs" : key;
112
- result[outKey] = renameNestedDefs(value, depth + 1);
115
+ for (const [key, value] of Object.entries(record))
116
+ assignKey(result, key, value);
117
+ for (const key of Object.keys(record)) {
118
+ if (isDataPosition(key, inSchemaMap))
119
+ continue;
120
+ const value = record[key];
121
+ if (!inSchemaMap && key === "$ref" && typeof value === "string" && value.startsWith("#/definitions/")) {
122
+ assignKey(result, key, value.replace("#/definitions/", "#/$defs/"));
123
+ continue;
113
124
  }
125
+ const outKey = !inSchemaMap && key === "definitions" ? "$defs" : key;
126
+ if (outKey !== key)
127
+ delete result[key];
128
+ assignKey(result, outKey, renameNestedDefs(value, depth + 1, entersSchemaMap(key, inSchemaMap)));
114
129
  }
115
130
  return result;
116
131
  };
@@ -1,5 +1,10 @@
1
1
  /**
2
2
  * Parses the values of a record with a parser function.
3
- * Uses for...in instead of Object.entries() to avoid allocating an intermediate array.
3
+ *
4
+ * Own keys only. This runs on untrusted input at runtime, and a `for…in` walks
5
+ * the prototype chain — so with a polluted `Object.prototype` the parsed record
6
+ * came back carrying properties the input never had. `Object.keys` allocates an
7
+ * array where `for…in` did not; that is the cheaper half of the trade, and it
8
+ * is the form the interpreter's own benchmark measures fastest anyway.
4
9
  */
5
10
  export declare const validateRecord: (input: unknown, parser: (input: unknown) => unknown) => Record<string, unknown>;
@@ -5,7 +5,7 @@ const validateRecord = (input, parser) => {
5
5
  }
6
6
  const record = input;
7
7
  const result = {};
8
- for (const key in record) {
8
+ for (const key of Object.keys(record)) {
9
9
  const value = parser(record[key]);
10
10
  if (key === "__proto__") {
11
11
  Object.defineProperty(result, key, { value, writable: true, enumerable: true, configurable: true });
@@ -1,9 +1,13 @@
1
1
  import { assertIdScopes } from "./assert-id-scopes.js";
2
+ import { assignKey } from "./assign-key.js";
2
3
  import { buildDynamicRefMap } from "./build-dynamic-ref-map.js";
4
+ import { entersSchemaMap, isDataPosition } from "./build-resource-registry.js";
3
5
  import { extractRefs } from "./extract-refs.js";
4
6
  import { graftExternalSchemas } from "./graft-external-schemas.js";
7
+ import { assertSchemaDepth } from "./max-schema-depth.js";
5
8
  import { normalizeRefScopes } from "./normalize-ref-scopes.js";
6
9
  import { pruneExternalSchemas } from "./prune-external-schemas.js";
10
+ import { readKey } from "./read-key.js";
7
11
  import { refToFilename } from "./ref-to-filename.js";
8
12
  import { refToName } from "./ref-to-name.js";
9
13
  import { resolveDynamicRefs } from "./resolve-dynamic-refs.js";
@@ -35,40 +39,47 @@ const getAliasRootRef = (schema) => {
35
39
  if (typeof schema !== "object" || schema === null)
36
40
  return null;
37
41
  const record = schema;
38
- if (typeof record["$ref"] !== "string")
42
+ const ref = readKey(record, "$ref");
43
+ if (typeof ref !== "string")
39
44
  return null;
40
- return SHAPE_KEYWORDS.some((key) => key in record) ? null : record["$ref"];
45
+ return SHAPE_KEYWORDS.some((key) => Object.hasOwn(record, key)) ? null : ref;
41
46
  };
42
47
  const DEFINITION_MAP_KEYS = ["$defs", "definitions"];
43
- const expandBooleanDefinitions = (value) => {
48
+ const expandBooleanDefinitions = (value, depth, inSchemaMap = false) => {
49
+ assertSchemaDepth(depth, "walkRefGraph");
44
50
  if (typeof value !== "object" || value === null)
45
51
  return value;
46
52
  if (Array.isArray(value)) {
47
- const mapped = value.map(expandBooleanDefinitions);
53
+ const mapped = value.map((item) => expandBooleanDefinitions(item, depth + 1, inSchemaMap));
48
54
  return mapped.some((item, index) => item !== value[index]) ? mapped : value;
49
55
  }
50
56
  const record = value;
51
57
  let changed = false;
52
58
  const result = {};
53
- for (const [key, sub] of Object.entries(record)) {
54
- const next = DEFINITION_MAP_KEYS.includes(key) ? expandDefinitionMap(sub) : expandBooleanDefinitions(sub);
55
- if (next !== sub)
59
+ for (const [key, sub] of Object.entries(record))
60
+ assignKey(result, key, sub);
61
+ for (const key of Object.keys(record)) {
62
+ if (isDataPosition(key, inSchemaMap))
63
+ continue;
64
+ const next = !inSchemaMap && DEFINITION_MAP_KEYS.includes(key) ? expandDefinitionMap(record[key], depth + 1) : expandBooleanDefinitions(record[key], depth + 1, entersSchemaMap(key, inSchemaMap));
65
+ if (next !== record[key])
56
66
  changed = true;
57
- result[key] = next;
67
+ assignKey(result, key, next);
58
68
  }
59
69
  return changed ? result : value;
60
70
  };
61
- const expandDefinitionMap = (definitions) => {
71
+ const expandDefinitionMap = (definitions, depth) => {
72
+ assertSchemaDepth(depth, "walkRefGraph");
62
73
  if (typeof definitions !== "object" || definitions === null || Array.isArray(definitions))
63
74
  return definitions;
64
75
  const record = definitions;
65
76
  let changed = false;
66
77
  const result = {};
67
78
  for (const [name, definition] of Object.entries(record)) {
68
- const next = definition === true ? {} : definition === false ? { not: {} } : expandBooleanDefinitions(definition);
79
+ const next = definition === true ? {} : definition === false ? { not: {} } : expandBooleanDefinitions(definition, depth + 1);
69
80
  if (next !== definition)
70
81
  changed = true;
71
- result[name] = next;
82
+ assignKey(result, name, next);
72
83
  }
73
84
  return changed ? result : definitions;
74
85
  };
@@ -89,7 +100,7 @@ const getRootCache = (rootSchema, schemas) => {
89
100
  return cached;
90
101
  const grafted = registry === void 0 ? void 0 : graftExternalSchemas(rootSchema, registry);
91
102
  const combined = grafted?.document ?? rootSchema;
92
- const expanded = expandBooleanDefinitions(upgradeDraft07Schema(combined));
103
+ const expanded = expandBooleanDefinitions(upgradeDraft07Schema(combined), 0);
93
104
  assertIdScopes(expanded);
94
105
  const normalized = normalizeRefScopes(expanded);
95
106
  const upgraded = grafted === void 0 ? normalized : pruneExternalSchemas(normalized, grafted.names);
@@ -109,35 +120,60 @@ const getRootCache = (rootSchema, schemas) => {
109
120
  }
110
121
  return cache;
111
122
  };
112
- const referencesRoot = (value) => {
123
+ const referencesRoot = (value, depth, inSchemaMap = false) => {
124
+ assertSchemaDepth(depth, "walkRefGraph");
113
125
  if (typeof value !== "object" || value === null)
114
126
  return false;
115
127
  if (Array.isArray(value))
116
- return value.some(referencesRoot);
128
+ return value.some((item) => referencesRoot(item, depth + 1, inSchemaMap));
117
129
  const record = value;
118
- if (record["$ref"] === ROOT_POINTER || record["$ref"] === "#/")
119
- return true;
120
- for (const key in record)
121
- if (referencesRoot(record[key]))
130
+ if (!inSchemaMap) {
131
+ const ref = readKey(record, "$ref");
132
+ if (ref === ROOT_POINTER || ref === "#/")
122
133
  return true;
134
+ }
135
+ for (const key of Object.keys(record)) {
136
+ if (isDataPosition(key, inSchemaMap))
137
+ continue;
138
+ if (referencesRoot(record[key], depth + 1, entersSchemaMap(key, inSchemaMap)))
139
+ return true;
140
+ }
123
141
  return false;
124
142
  };
125
- const rewriteRootRefs = (value, rootSelfRef) => {
143
+ const copyValue = (value, depth) => {
144
+ assertSchemaDepth(depth, "walkRefGraph");
126
145
  if (typeof value !== "object" || value === null)
127
146
  return value;
128
147
  if (Array.isArray(value))
129
- return value.map((item) => rewriteRootRefs(item, rootSelfRef));
148
+ return value.map((item) => copyValue(item, depth + 1));
130
149
  const out = {};
131
150
  for (const [key, sub] of Object.entries(value)) {
132
- out[key] = key === "$ref" && (sub === ROOT_POINTER || sub === "#/") ? rootSelfRef : rewriteRootRefs(sub, rootSelfRef);
151
+ assignKey(out, key, copyValue(sub, depth + 1));
152
+ }
153
+ return out;
154
+ };
155
+ const rewriteRootRefs = (value, rootSelfRef, depth, inSchemaMap = false) => {
156
+ assertSchemaDepth(depth, "walkRefGraph");
157
+ if (typeof value !== "object" || value === null)
158
+ return value;
159
+ if (Array.isArray(value))
160
+ return value.map((item) => rewriteRootRefs(item, rootSelfRef, depth + 1, inSchemaMap));
161
+ const out = {};
162
+ const record = value;
163
+ for (const key of Object.keys(record)) {
164
+ if (isDataPosition(key, inSchemaMap)) {
165
+ assignKey(out, key, copyValue(record[key], depth + 1));
166
+ continue;
167
+ }
168
+ assignKey(out, key, !inSchemaMap && key === "$ref" && (record[key] === ROOT_POINTER || record[key] === "#/") ? rootSelfRef : rewriteRootRefs(record[key], rootSelfRef, depth + 1, entersSchemaMap(key, inSchemaMap)));
133
169
  }
134
170
  return out;
135
171
  };
136
- const rootAnchoredCache = (base, rootFilename, rootSelfRef) => {
172
+ const rootAnchoredCache = (base, rootFilename, rootSelfRef, selfReferenced) => {
137
173
  const cached = base.rootAnchored.get(rootFilename);
138
174
  if (cached)
139
175
  return cached;
140
- const rewritten = referencesRoot(base.upgraded) ? rewriteRootRefs(base.upgraded, rootSelfRef) : base.upgraded;
176
+ const rewritten = selfReferenced ? rewriteRootRefs(base.upgraded, rootSelfRef, 0) : base.upgraded;
141
177
  const { $defs, ...rootBody } = rewritten;
142
178
  const defs = typeof $defs === "object" && $defs !== null ? $defs : {};
143
179
  if (Object.hasOwn(defs, rootFilename)) {
@@ -156,20 +192,26 @@ const rootAnchoredCache = (base, rootFilename, rootSelfRef) => {
156
192
  base.rootAnchored.set(rootFilename, derived);
157
193
  return derived;
158
194
  };
159
- const dynamicPointerRefs = (value, into = /* @__PURE__ */ new Set()) => {
195
+ const dynamicPointerRefs = (value, depth, into = /* @__PURE__ */ new Set(), inSchemaMap = false) => {
196
+ assertSchemaDepth(depth, "walkRefGraph");
160
197
  if (typeof value !== "object" || value === null)
161
198
  return into;
162
199
  if (Array.isArray(value)) {
163
200
  for (const item of value)
164
- dynamicPointerRefs(item, into);
201
+ dynamicPointerRefs(item, depth + 1, into, inSchemaMap);
165
202
  return into;
166
203
  }
167
204
  const record = value;
168
- const ref = record["$dynamicRef"];
169
- if (typeof ref === "string" && ref.startsWith("#/"))
170
- into.add(ref);
171
- for (const key in record)
172
- dynamicPointerRefs(record[key], into);
205
+ if (!inSchemaMap) {
206
+ const ref = readKey(record, "$dynamicRef");
207
+ if (typeof ref === "string" && ref.startsWith("#/"))
208
+ into.add(ref);
209
+ }
210
+ for (const key of Object.keys(record)) {
211
+ if (isDataPosition(key, inSchemaMap))
212
+ continue;
213
+ dynamicPointerRefs(record[key], depth + 1, into, entersSchemaMap(key, inSchemaMap));
214
+ }
173
215
  return into;
174
216
  };
175
217
  const cachedResolveRef = (cache, ref) => {
@@ -195,12 +237,12 @@ const walkRefGraph = (rootSchema, rootTypeName, options, visit) => {
195
237
  const rootFilename = rootTypeName.toLowerCase();
196
238
  const rootSelfRef = `#/$defs/${rootFilename}`;
197
239
  const rootIsAnchored = Object.values(baseCache.dynamicRefMap).includes(ROOT_POINTER);
198
- const rootIsSelfReferenced = referencesRoot(baseCache.upgraded);
240
+ const rootIsSelfReferenced = referencesRoot(baseCache.upgraded, 0);
199
241
  const needsRootAlias = rootIsAnchored || rootIsSelfReferenced;
200
242
  if (needsRootAlias && refToName(rootSelfRef, typeSuffix) !== rootTypeName) {
201
243
  throw new Error(`The root schema is referenced by ${rootIsAnchored ? "a $dynamicAnchor" : 'a $ref to "#"'}, which is generated as the root's own file \u2014 but the root type name "${rootTypeName}" does not survive the round trip through ref naming (refs to it would be named "${refToName(rootSelfRef, typeSuffix)}"). Use a single-word root type name with no type suffix, or move the recursion onto a $defs entry.`);
202
244
  }
203
- const cache = needsRootAlias ? rootAnchoredCache(baseCache, rootFilename, rootSelfRef) : baseCache;
245
+ const cache = needsRootAlias ? rootAnchoredCache(baseCache, rootFilename, rootSelfRef, rootIsSelfReferenced) : baseCache;
204
246
  const { upgraded, dynamicRefMap } = cache;
205
247
  const processedRefs = /* @__PURE__ */ new Set();
206
248
  const filenameOwners = /* @__PURE__ */ new Map();
@@ -236,7 +278,7 @@ const walkRefGraph = (rootSchema, rootTypeName, options, visit) => {
236
278
  const queue = [
237
279
  ...cachedExtractRefs(cache, upgraded),
238
280
  ...Object.values(dynamicRefMap),
239
- ...[...dynamicPointerRefs(upgraded)].filter((ref) => cachedResolveRef(cache, ref) !== void 0)
281
+ ...[...dynamicPointerRefs(upgraded, 0)].filter((ref) => cachedResolveRef(cache, ref) !== void 0)
240
282
  ];
241
283
  for (let head = 0; head < queue.length; head++) {
242
284
  const ref = queue[head];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/helpers",
3
- "version": "0.15.3",
3
+ "version": "0.16.0",
4
4
  "description": "Shared utilities for the mjst code generation ecosystem.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
package/src/has-ref.ts CHANGED
@@ -4,7 +4,7 @@ export const hasRef = (value: unknown): value is { $ref: string } & Record<strin
4
4
  typeof value === 'object' &&
5
5
  value !== null &&
6
6
  !Array.isArray(value) &&
7
- '$ref' in value &&
7
+ Object.hasOwn(value, '$ref') &&
8
8
  typeof (value as { $ref: unknown }).$ref === 'string'
9
9
  )
10
10
  }
@@ -2,7 +2,12 @@ import { isObject } from './is-object'
2
2
 
3
3
  /**
4
4
  * Parses the values of a record with a parser function.
5
- * Uses for...in instead of Object.entries() to avoid allocating an intermediate array.
5
+ *
6
+ * Own keys only. This runs on untrusted input at runtime, and a `for…in` walks
7
+ * the prototype chain — so with a polluted `Object.prototype` the parsed record
8
+ * came back carrying properties the input never had. `Object.keys` allocates an
9
+ * array where `for…in` did not; that is the cheaper half of the trade, and it
10
+ * is the form the interpreter's own benchmark measures fastest anyway.
6
11
  */
7
12
  export const validateRecord = (input: unknown, parser: (input: unknown) => unknown) => {
8
13
  if (!isObject(input)) {
@@ -11,7 +16,7 @@ export const validateRecord = (input: unknown, parser: (input: unknown) => unkno
11
16
 
12
17
  const record = input as Record<string, unknown>
13
18
  const result: Record<string, unknown> = {}
14
- for (const key in record) {
19
+ for (const key of Object.keys(record)) {
15
20
  const value = parser(record[key])
16
21
  // A plain assignment of `__proto__` invokes the prototype setter and
17
22
  // corrupts `result`'s prototype (a prototype-pollution vector for untrusted