@amritk/resolve-refs 0.1.0 → 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.
@@ -1 +1 @@
1
- {"version":3,"file":"resolve-refs-from-file.d.ts","sourceRoot":"","sources":["../src/resolve-refs-from-file.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAgB,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AA+C1E,iFAAiF;AACjF,eAAO,MAAM,gBAAgB,QAAO,IAEnC,CAAA;AAmKD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,aAAoB,MAAM,YAAW,cAAc,KAAQ,OAAO,CAAC,aAAa,CAW/G,CAAA"}
1
+ {"version":3,"file":"resolve-refs-from-file.d.ts","sourceRoot":"","sources":["../src/resolve-refs-from-file.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAgB,cAAc,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AA0C1E,iFAAiF;AACjF,eAAO,MAAM,gBAAgB,QAAO,IAEnC,CAAA;AAqKD;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,aAAoB,MAAM,YAAW,cAAc,KAAQ,OAAO,CAAC,aAAa,CAW/G,CAAA"}
@@ -28,10 +28,6 @@ const splitRef = (ref) => {
28
28
  pointer: hashIdx === -1 ? '' : ref.slice(hashIdx + 1),
29
29
  };
30
30
  };
31
- // mjst deals only in JSON Schema documents, so every document — local or
32
- // remote — is parsed as JSON. (The Loupe linter's sibling resolver additionally
33
- // accepts YAML; this one stays JSON-only to keep the package dependency-free.)
34
- const parseContent = (content) => JSON.parse(content);
35
31
  // --- Remote document cache -------------------------------------------------
36
32
  //
37
33
  // Fetched remote documents are cached in memory for the lifetime of the
@@ -91,7 +87,8 @@ const loadDoc = async (location, docCache, options, errors) => {
91
87
  const response = await fetch(location);
92
88
  if (!response.ok)
93
89
  throw new Error(`HTTP ${response.status} ${response.statusText}`);
94
- const doc = parseContent(await response.text());
90
+ const parse = options.parse ?? ((c) => JSON.parse(c));
91
+ const doc = parse(await response.text(), location);
95
92
  remoteCache.set(location, doc);
96
93
  docCache.set(location, doc);
97
94
  return true;
@@ -103,7 +100,8 @@ const loadDoc = async (location, docCache, options, errors) => {
103
100
  }
104
101
  }
105
102
  try {
106
- docCache.set(location, parseContent(readFileSync(location, 'utf8')));
103
+ const parse = options.parse ?? ((c) => JSON.parse(c));
104
+ docCache.set(location, parse(readFileSync(location, 'utf8'), location));
107
105
  return true;
108
106
  }
109
107
  catch (err) {
package/dist/types.d.ts CHANGED
@@ -33,5 +33,22 @@ export type ResolveOptions = {
33
33
  * endpoint). An explicit `allowedHosts` entry always bypasses this guard.
34
34
  */
35
35
  allowPrivateHosts?: boolean;
36
+ /**
37
+ * Custom content parser. Receives the raw text of every loaded document and
38
+ * its absolute location (file path or URL). Defaults to `JSON.parse`.
39
+ *
40
+ * Pass a YAML-aware function to support `.yaml`/`.yml` documents without
41
+ * adding a dependency to this package:
42
+ *
43
+ * ```ts
44
+ * import { parse as parseYaml } from 'yaml'
45
+ *
46
+ * resolveRefsFromFile(path, {
47
+ * parse: (content, location) =>
48
+ * /\.ya?ml$/i.test(location) ? parseYaml(content) : JSON.parse(content),
49
+ * })
50
+ * ```
51
+ */
52
+ parse?: (content: string, location: string) => unknown;
36
53
  };
37
54
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAA;AAE1C,iFAAiF;AACjF,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,QAAQ,CAAA;CACf,CAAA;AAED,gFAAgF;AAChF,MAAM,MAAM,aAAa,GAAG;IAC1B,kEAAkE;IAClE,QAAQ,EAAE,OAAO,CAAA;IACjB,MAAM,EAAE,YAAY,EAAE,CAAA;CACvB,CAAA;AAED,mEAAmE;AACnE,MAAM,MAAM,cAAc,GAAG;IAC3B;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;IACvB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAA;CAC5B,CAAA"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAA;AAE1C,iFAAiF;AACjF,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,QAAQ,CAAA;CACf,CAAA;AAED,gFAAgF;AAChF,MAAM,MAAM,aAAa,GAAG;IAC1B,kEAAkE;IAClE,QAAQ,EAAE,OAAO,CAAA;IACjB,MAAM,EAAE,YAAY,EAAE,CAAA;CACvB,CAAA;AAED,mEAAmE;AACnE,MAAM,MAAM,cAAc,GAAG;IAC3B;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;IACvB;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B;;;;;;;;;;;;;;;OAeG;IACH,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAA;CACvD,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/resolve-refs",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Resolve and inline JSON Schema / OpenAPI $refs — internal, cross-file, and remote — with session caching and a default-deny SSRF guard.",
5
5
  "module": "./dist/index.js",
6
6
  "type": "module",
@@ -82,6 +82,22 @@ describe('resolve-refs-from-file', () => {
82
82
  expect(fetchSpy).not.toHaveBeenCalled()
83
83
  })
84
84
 
85
+ it('uses a custom parse callback to load non-JSON (e.g. YAML) documents', async () => {
86
+ // Real YAML that JSON.parse would reject — a custom callback handles it.
87
+ writeFileSync(join(dir, 'contact.yaml'), 'type: object\nproperties:\n name:\n type: string\n')
88
+ writeFileSync(join(dir, 'api.json'), JSON.stringify({ contact: { $ref: './contact.yaml' } }))
89
+
90
+ const parse = (content: string, location: string): unknown => {
91
+ if (/\.ya?ml$/i.test(location)) return { type: 'object', properties: { name: { type: 'string' } } }
92
+ return JSON.parse(content) as unknown
93
+ }
94
+
95
+ const { resolved, errors } = await resolveRefsFromFile(join(dir, 'api.json'), { parse })
96
+
97
+ expect(errors).toEqual([])
98
+ expect(resolved).toMatchObject({ contact: { type: 'object', properties: { name: { type: 'string' } } } })
99
+ })
100
+
85
101
  it('fetches an allow-listed remote $ref and caches it for the session', async () => {
86
102
  const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(
87
103
  new Response(JSON.stringify({ Foo: { type: 'string' } }), {
@@ -35,11 +35,6 @@ const splitRef = (ref: string): { filePart: string; pointer: string } => {
35
35
  }
36
36
  }
37
37
 
38
- // mjst deals only in JSON Schema documents, so every document — local or
39
- // remote — is parsed as JSON. (The Loupe linter's sibling resolver additionally
40
- // accepts YAML; this one stays JSON-only to keep the package dependency-free.)
41
- const parseContent = (content: string): unknown => JSON.parse(content) as unknown
42
-
43
38
  // --- Remote document cache -------------------------------------------------
44
39
  //
45
40
  // Fetched remote documents are cached in memory for the lifetime of the
@@ -107,7 +102,8 @@ const loadDoc = async (
107
102
  try {
108
103
  const response = await fetch(location)
109
104
  if (!response.ok) throw new Error(`HTTP ${response.status} ${response.statusText}`)
110
- const doc = parseContent(await response.text())
105
+ const parse = options.parse ?? ((c: string) => JSON.parse(c) as unknown)
106
+ const doc = parse(await response.text(), location)
111
107
  remoteCache.set(location, doc)
112
108
  docCache.set(location, doc)
113
109
  return true
@@ -119,7 +115,8 @@ const loadDoc = async (
119
115
  }
120
116
 
121
117
  try {
122
- docCache.set(location, parseContent(readFileSync(location, 'utf8')))
118
+ const parse = options.parse ?? ((c: string) => JSON.parse(c) as unknown)
119
+ docCache.set(location, parse(readFileSync(location, 'utf8'), location))
123
120
  return true
124
121
  } catch (err) {
125
122
  errors.push({ message: String(err), path: [] })
@@ -25,6 +25,23 @@ describe('resolve-refs', () => {
25
25
  expect(head.properties.next).toEqual({})
26
26
  })
27
27
 
28
+ it('breaks a mutual cycle (A → B → A) without infinite recursion', () => {
29
+ const { resolved } = resolveRefs({
30
+ $defs: {
31
+ a: { type: 'object', properties: { b: { $ref: '#/$defs/b' } } },
32
+ b: { type: 'object', properties: { a: { $ref: '#/$defs/a' } } },
33
+ },
34
+ properties: { root: { $ref: '#/$defs/a' } },
35
+ })
36
+
37
+ // Resolution terminates; `a` is in the cache by the time `properties.root` is
38
+ // processed, so we get the partially-resolved shape where the back-reference
39
+ // leg terminated at `{}` rather than looping forever.
40
+ const root = (resolved as { properties: { root: { type: string; properties: { b: unknown } } } }).properties.root
41
+ expect(root.type).toBe('object')
42
+ expect(root.properties.b).toEqual({})
43
+ })
44
+
28
45
  it('leaves external (non-#) refs untouched', () => {
29
46
  const { resolved } = resolveRefs({ properties: { pet: { $ref: 'pet.json#/Pet' } } })
30
47
 
package/src/types.ts CHANGED
@@ -36,4 +36,21 @@ export type ResolveOptions = {
36
36
  * endpoint). An explicit `allowedHosts` entry always bypasses this guard.
37
37
  */
38
38
  allowPrivateHosts?: boolean
39
+ /**
40
+ * Custom content parser. Receives the raw text of every loaded document and
41
+ * its absolute location (file path or URL). Defaults to `JSON.parse`.
42
+ *
43
+ * Pass a YAML-aware function to support `.yaml`/`.yml` documents without
44
+ * adding a dependency to this package:
45
+ *
46
+ * ```ts
47
+ * import { parse as parseYaml } from 'yaml'
48
+ *
49
+ * resolveRefsFromFile(path, {
50
+ * parse: (content, location) =>
51
+ * /\.ya?ml$/i.test(location) ? parseYaml(content) : JSON.parse(content),
52
+ * })
53
+ * ```
54
+ */
55
+ parse?: (content: string, location: string) => unknown
39
56
  }