@kubb/adapter-oas 5.18.7 → 5.18.8

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/dist/index.cjs CHANGED
@@ -1,42 +1,27 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  // \0rolldown/runtime.js
3
- var __create = Object.create;
4
3
  var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getProtoOf = Object.getPrototypeOf;
8
- var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
- key = keys[i];
12
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
- get: ((k) => from[k]).bind(null, key),
14
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
- });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
20
- value: mod,
21
- enumerable: true
22
- }) : target, mod));
4
+ var __name = (target, value) => __defProp(target, "name", {
5
+ value,
6
+ configurable: true
7
+ });
23
8
  let _kubb_core = require("@kubb/core");
24
9
  let _kubb_kit = require("@kubb/kit");
25
10
  let node_path = require("node:path");
26
11
  let node_url = require("node:url");
12
+ let node_fs = require("node:fs");
27
13
  let node_fs_promises = require("node:fs/promises");
28
14
  let _scalar_openapi_upgrader = require("@scalar/openapi-upgrader");
29
15
  let api_ref_bundler = require("api-ref-bundler");
30
16
  let js_yaml = require("js-yaml");
31
17
  let yaml = require("yaml");
32
- yaml = __toESM(yaml, 1);
33
18
  // kubb-extension.json
34
19
  var compatibility = {
35
20
  "kubb": ">=5.8.3",
36
21
  "node": ">=22.22"
37
22
  };
38
23
  // package.json
39
- var version = "5.18.7";
24
+ var version = "5.18.8";
40
25
  // src/constants.ts
41
26
  /**
42
27
  * Default parser options applied when no explicit options are provided.
@@ -383,8 +368,44 @@ async function exists(path) {
383
368
  */
384
369
  async function read(path) {
385
370
  if (runtime.isBun) return Bun.file(path).text();
386
- return (0, node_fs_promises.readFile)(path, { encoding: "utf8" });
371
+ return new Promise((resolvePromise, reject) => (0, node_fs.readFile)(path, { encoding: "utf8" }, (error, data) => error ? reject(error) : resolvePromise(data)));
387
372
  }
373
+ // ../../internals/utils/src/promise.ts
374
+ /**
375
+ * Returns the value cached under `key` in `store`, computing and storing it on the first call.
376
+ * Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
377
+ * keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
378
+ *
379
+ * @example
380
+ * `const refs = memo(refCache, node, () => collectRefs(node))`
381
+ */
382
+ function memo(store, key, compute) {
383
+ const cached = store.get(key);
384
+ if (cached !== void 0 || store.has(key)) return cached;
385
+ const value = compute();
386
+ store.set(key, value);
387
+ return value;
388
+ }
389
+ function createStore({ type }) {
390
+ if (type === "weak") return /* @__PURE__ */ new WeakMap();
391
+ const entry = /* @__PURE__ */ new Map();
392
+ if (type === "map") return entry;
393
+ const set = (key, value) => {
394
+ entry.clear();
395
+ entry.set(key, value);
396
+ };
397
+ const touch = (key) => {
398
+ if (!entry.has(key)) return false;
399
+ set(key, entry.get(key));
400
+ return true;
401
+ };
402
+ return {
403
+ has: touch,
404
+ get: (key) => touch(key) ? entry.get(key) : void 0,
405
+ set
406
+ };
407
+ }
408
+ memo.create = createStore;
388
409
  // src/oas.ts
389
410
  /**
390
411
  * Returns `true` when a schema should be treated as nullable.
@@ -630,6 +651,11 @@ const NAMED_ENTRY_KEYS = /* @__PURE__ */ new Set([
630
651
  "schemas",
631
652
  "dependentSchemas"
632
653
  ]);
654
+ const TARGET_KEYS = [
655
+ "$id",
656
+ "$anchor",
657
+ "$dynamicAnchor"
658
+ ];
633
659
  function walk(node, pointer, visit, parentKey) {
634
660
  if (!node || typeof node !== "object") return;
635
661
  if (!Array.isArray(node)) visit(node, pointer);
@@ -639,6 +665,37 @@ function walk(node, pointer, visit, parentKey) {
639
665
  walk(value, `${pointer}/${escapePointerToken(key)}`, visit, Array.isArray(node) ? parentKey : key);
640
666
  }
641
667
  }
668
+ /** Answers, in one walk and without building JSON pointers, what the loading steps after it need to know. */
669
+ function scanDocument(document) {
670
+ const scan = {
671
+ externalRef: false,
672
+ schemaIds: false,
673
+ pathRefs: false
674
+ };
675
+ const visit = ({ node, readsIds, parentKey }) => {
676
+ if (!node || typeof node !== "object") return;
677
+ const isArray = Array.isArray(node);
678
+ const record = node;
679
+ const ref = record["$ref"];
680
+ if (typeof ref === "string" && !ref.startsWith("#")) scan.externalRef = true;
681
+ if (typeof ref === "string" && ref.startsWith("#/paths/")) scan.pathRefs = true;
682
+ if (readsIds && !isArray && TARGET_KEYS.some((key) => typeof record[key] === "string")) scan.schemaIds = true;
683
+ const named = parentKey !== void 0 && NAMED_ENTRY_KEYS.has(parentKey);
684
+ for (const key in record) {
685
+ const childReadsIds = readsIds && (named || !(DATA_KEYS.has(key) || key.startsWith("x-")));
686
+ visit({
687
+ node: record[key],
688
+ readsIds: childReadsIds,
689
+ parentKey: isArray ? parentKey : key
690
+ });
691
+ }
692
+ };
693
+ visit({
694
+ node: document,
695
+ readsIds: true
696
+ });
697
+ return scan;
698
+ }
642
699
  /**
643
700
  * The declaration in the nearest enclosing schema, else the only one, else `undefined`.
644
701
  */
@@ -673,6 +730,183 @@ function resolveSchemaIds(document) {
673
730
  }
674
731
  });
675
732
  }
733
+ // src/sourceLocator.ts
734
+ const flowGap = /(?:[\s,]|#[^\n]*)*/y;
735
+ const indexes = memo.create({ type: "last" });
736
+ function toKeyText(text) {
737
+ if (!/^[-+.~\d]|^(?:null|true|false)$/i.test(text)) return text;
738
+ try {
739
+ const value = (0, js_yaml.load)(text, { schema: js_yaml.CORE_SCHEMA });
740
+ return value === null || typeof value === "number" || typeof value === "boolean" ? String(value) : text;
741
+ } catch {
742
+ return text;
743
+ }
744
+ }
745
+ function indexEvents(events) {
746
+ const types = new Uint8Array(events.length);
747
+ const styles = new Uint8Array(events.length);
748
+ const starts = new Int32Array(events.length);
749
+ const ends = new Int32Array(events.length);
750
+ for (let at = 0, last = 0; at < events.length; last = Math.max(last, starts[at], ends[at]), at++) {
751
+ const event = events[at];
752
+ types[at] = event.type;
753
+ if (event.type === js_yaml.EVENT_ID.SCALAR) {
754
+ const quote = event.style === js_yaml.SCALAR_STYLE.SINGLE_QUOTED || event.style === js_yaml.SCALAR_STYLE.DOUBLE_QUOTED ? 1 : 0;
755
+ const empty = event.valueStart < 0;
756
+ styles[at] = event.style;
757
+ starts[at] = empty ? last : event.valueStart - quote;
758
+ ends[at] = empty ? last : event.valueEnd + quote;
759
+ }
760
+ if (event.type === js_yaml.EVENT_ID.ALIAS) {
761
+ starts[at] = event.anchorStart - 1;
762
+ ends[at] = event.anchorEnd;
763
+ }
764
+ if (event.type === js_yaml.EVENT_ID.MAPPING || event.type === js_yaml.EVENT_ID.SEQUENCE) {
765
+ styles[at] = event.style;
766
+ starts[at] = event.start;
767
+ }
768
+ }
769
+ return {
770
+ types,
771
+ styles,
772
+ starts,
773
+ ends,
774
+ collections: /* @__PURE__ */ new Map()
775
+ };
776
+ }
777
+ function createLocator({ index, source }) {
778
+ const { types, styles, starts, ends, collections } = index;
779
+ const isCollection = (at) => types[at] === js_yaml.EVENT_ID.MAPPING || types[at] === js_yaml.EVENT_ID.SEQUENCE;
780
+ const readText = (at) => {
781
+ const text = source.slice(starts[at], ends[at]);
782
+ if (styles[at] === js_yaml.SCALAR_STYLE.SINGLE_QUOTED) return text.slice(1, -1).replaceAll("''", "'");
783
+ if (styles[at] !== js_yaml.SCALAR_STYLE.DOUBLE_QUOTED) return text;
784
+ try {
785
+ return JSON.parse(text);
786
+ } catch {
787
+ return String((0, js_yaml.load)(text, { schema: js_yaml.CORE_SCHEMA }));
788
+ }
789
+ };
790
+ const readNode = (at) => {
791
+ if (!isCollection(at)) return {
792
+ start: starts[at],
793
+ end: ends[at],
794
+ next: at + 1
795
+ };
796
+ let next = at + 1;
797
+ let end = starts[at];
798
+ while (types[next] !== js_yaml.EVENT_ID.POP) ({end, next} = readNode(next));
799
+ if (styles[at] === js_yaml.COLLECTION_STYLE.FLOW) {
800
+ flowGap.lastIndex = end;
801
+ flowGap.exec(source);
802
+ end = flowGap.lastIndex + 1;
803
+ }
804
+ return {
805
+ start: starts[at],
806
+ end,
807
+ next: next + 1
808
+ };
809
+ };
810
+ const readChildren = (collection) => {
811
+ const keys = /* @__PURE__ */ new Map();
812
+ const items = [];
813
+ const isMapping = types[collection] === js_yaml.EVENT_ID.MAPPING;
814
+ for (let at = collection + 1; types[at] !== js_yaml.EVENT_ID.POP;) {
815
+ const key = isMapping ? at : void 0;
816
+ if (key !== void 0) at = readNode(at).next;
817
+ const { start, end, next } = readNode(at);
818
+ if (key === void 0) items.push({
819
+ index: at,
820
+ range: {
821
+ start,
822
+ end
823
+ }
824
+ });
825
+ const raw = key !== void 0 && types[key] === js_yaml.EVENT_ID.SCALAR ? readText(key) : void 0;
826
+ const text = raw !== void 0 && styles[key] === js_yaml.SCALAR_STYLE.PLAIN ? toKeyText(raw) : raw;
827
+ if (text !== void 0 && !keys.has(text)) keys.set(text, {
828
+ index: at,
829
+ range: {
830
+ start: starts[key],
831
+ end: ends[key]
832
+ }
833
+ });
834
+ at = next;
835
+ }
836
+ return {
837
+ keys,
838
+ items
839
+ };
840
+ };
841
+ const walk = ({ at, tokens: [token, ...rest], range }) => {
842
+ if (token === void 0) return range;
843
+ if (!isCollection(at)) return void 0;
844
+ const { keys, items } = memo(collections, at, () => readChildren(at));
845
+ const child = keys.get(token) ?? items[Number(token)];
846
+ return child ? walk({
847
+ at: child.index,
848
+ tokens: rest,
849
+ range: child.range
850
+ }) : void 0;
851
+ };
852
+ return {
853
+ find(pointer) {
854
+ return walk({
855
+ at: 1,
856
+ tokens: pointer.replace(/^#/, "").split("/").slice(1).map(unescapePointerToken),
857
+ range: void 0
858
+ });
859
+ },
860
+ findRef(ref) {
861
+ if (!index.refs) {
862
+ index.refs = /* @__PURE__ */ new Map();
863
+ const frames = [];
864
+ for (let at = 1; at + 1 < types.length; at++) {
865
+ if (types[at] === js_yaml.EVENT_ID.POP) {
866
+ frames.pop();
867
+ continue;
868
+ }
869
+ const top = frames.length - 1;
870
+ const isKey = frames[top] === 0;
871
+ if (frames[top] >= 0) frames[top] = 1 - frames[top];
872
+ if (isCollection(at)) frames.push(types[at] === js_yaml.EVENT_ID.MAPPING ? 0 : -1);
873
+ if (!isKey || types[at] !== js_yaml.EVENT_ID.SCALAR || types[at + 1] !== js_yaml.EVENT_ID.SCALAR || readText(at) !== "$ref") continue;
874
+ const text = readText(at + 1);
875
+ if (!index.refs.has(text)) index.refs.set(text, {
876
+ start: starts[at + 1],
877
+ end: ends[at + 1]
878
+ });
879
+ }
880
+ }
881
+ return index.refs.get(ref);
882
+ }
883
+ };
884
+ }
885
+ function parse$1(source) {
886
+ try {
887
+ return indexEvents((0, js_yaml.parseEvents)(source, { maxDepth: Number.POSITIVE_INFINITY }));
888
+ } catch {
889
+ return;
890
+ }
891
+ }
892
+ __name(parse$1, "parse");
893
+ function rememberEvents({ source, events }) {
894
+ indexes.set(source, indexEvents(events));
895
+ }
896
+ /**
897
+ * Maps OpenAPI JSON pointers and unresolved references to ranges in YAML or JSON source.
898
+ */
899
+ const locate = (source, location) => {
900
+ if (!("pointer" in location)) return void 0;
901
+ const index = memo(indexes, source, () => parse$1(source));
902
+ if (!index) return void 0;
903
+ const { find, findRef } = createLocator({
904
+ index,
905
+ source
906
+ });
907
+ const ref = "ref" in location ? location.ref : void 0;
908
+ return find(location.pointer) ?? (ref ? findRef(ref) : void 0);
909
+ };
676
910
  // src/load/source.ts
677
911
  const urlRegExp = /^https?:\/+/i;
678
912
  /**
@@ -727,14 +961,15 @@ async function readSource(sourcePath) {
727
961
  * Reads and parses one source file or URL referenced during bundling: YAML/JSON is parsed into an
728
962
  * object, Markdown is returned as-is (bundled inline rather than dereferenced).
729
963
  */
730
- async function resolveSource(sourcePath) {
964
+ async function resolveSource(sourcePath, { remember } = {}) {
731
965
  const data = await readSource(sourcePath);
732
966
  if (sourcePath.toLowerCase().endsWith(".md")) return data;
733
967
  const pathname = urlRegExp.test(sourcePath) ? new URL(sourcePath).pathname : sourcePath;
734
968
  return parseYaml(data, {
735
969
  origin: sourcePath,
736
970
  json: !/\.ya?ml$/i.test(pathname),
737
- help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page."
971
+ help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page.",
972
+ remember
738
973
  });
739
974
  }
740
975
  /**
@@ -750,13 +985,23 @@ async function resolveSource(sourcePath) {
750
985
  * keeping the last one. Its core schema keeps timestamps as strings and `<<` as a plain key, as
751
986
  * `yaml` does for YAML 1.2.
752
987
  */
753
- function parseYaml(data, { origin, help, json = true }) {
988
+ function parseYaml(data, { origin, help, json = true, remember = false }) {
754
989
  if (json && /^\s*[[{]/.test(data)) try {
755
990
  return JSON.parse(data);
756
991
  } catch {}
757
992
  try {
758
- const document = (0, js_yaml.load)(data, { schema: js_yaml.CORE_SCHEMA });
759
- if (document !== void 0) return document;
993
+ const events = (0, js_yaml.parseEvents)(data, {});
994
+ const documents = (0, js_yaml.constructFromEvents)(events, {
995
+ source: data,
996
+ schema: js_yaml.CORE_SCHEMA
997
+ });
998
+ if (documents.length === 1 && documents[0] !== void 0) {
999
+ if (remember) rememberEvents({
1000
+ source: data,
1001
+ events
1002
+ });
1003
+ return documents[0];
1004
+ }
760
1005
  } catch {}
761
1006
  try {
762
1007
  return (0, yaml.parse)(data);
@@ -788,18 +1033,15 @@ async function assertInputExists(input) {
788
1033
  });
789
1034
  }
790
1035
  // src/load/normalize.ts
1036
+ const scans = /* @__PURE__ */ new WeakMap();
791
1037
  /**
792
- * True when `node` contains a `$ref` pointing outside the current document (a relative path,
793
- * absolute path, or URL). An internal `#/...` fragment does not count.
794
- *
795
- * `Object.values` reads array elements and object property values alike, so the same recursion
796
- * walks both without a separate array branch.
1038
+ * Rewrites `$id` and `$anchor` refs in place, then returns the scan of the result.
797
1039
  */
798
- function hasExternalRef(node) {
799
- if (!node || typeof node !== "object") return false;
800
- const ref = node.$ref;
801
- if (typeof ref === "string" && !ref.startsWith("#")) return true;
802
- return Object.values(node).some(hasExternalRef);
1040
+ function resolveTargets(document) {
1041
+ const scan = scanDocument(document);
1042
+ if (!scan.schemaIds) return scan;
1043
+ resolveSchemaIds(document);
1044
+ return scanDocument(document);
803
1045
  }
804
1046
  /**
805
1047
  * Replaces each `$ref` into `#/paths/...` with a copy of its target, in place.
@@ -891,13 +1133,16 @@ async function bundleDocument(pathOrUrl, { document, sources = [] } = {}) {
891
1133
  const cached = cache.get(key);
892
1134
  if (cached) return cached;
893
1135
  sources.push(key);
894
- const result = resolveSource(sourcePath);
1136
+ const result = resolveSource(sourcePath, { remember: key === toKey(pathOrUrl) });
895
1137
  cache.set(key, result);
896
1138
  return result;
897
1139
  };
898
1140
  const root = await resolver(pathOrUrl);
899
- resolveSchemaIds(root);
900
- if (typeof root === "object" && root !== null && !hasExternalRef(root)) return root;
1141
+ const scan = resolveTargets(root);
1142
+ if (typeof root === "object" && root !== null && !scan.externalRef) {
1143
+ scans.set(root, scan);
1144
+ return root;
1145
+ }
901
1146
  entry = getBaseUri(pathOrUrl, root);
902
1147
  entryDocument = root;
903
1148
  return await (0, api_ref_bundler.bundle)(entry, resolver, { hooks: { onError: (message, { path, state }) => {
@@ -940,11 +1185,13 @@ async function parseFromConfig(source, sources = []) {
940
1185
  origin: "the inline `input` string",
941
1186
  help: "Fix the syntax at the reported line. Kubb reads `input` as an inline spec, not a file path, because it starts with `{`, `[`, `openapi:`, or `swagger:`, or spans several lines."
942
1187
  }) : structuredClone(source.data);
943
- if (source.root && hasExternalRef(data)) return bundleDocument((0, node_path.join)(source.root, inlineInputName), {
1188
+ const isObject = Boolean(data) && typeof data === "object";
1189
+ if (source.root && isObject) return bundleDocument((0, node_path.join)(source.root, inlineInputName), {
944
1190
  document: data,
945
1191
  sources
946
1192
  });
947
- resolveSchemaIds(data);
1193
+ const scan = resolveTargets(data);
1194
+ if (isObject) scans.set(data, scan);
948
1195
  return data;
949
1196
  }
950
1197
  await assertInputExists(source.path);
@@ -957,7 +1204,7 @@ async function parseFromConfig(source, sources = []) {
957
1204
  if (typeof version === "number") document[key] = Number.isInteger(version) ? version.toFixed(1) : String(version);
958
1205
  }
959
1206
  }
960
- if (raw && typeof raw === "object" && "swagger" in raw) {
1207
+ if (raw && typeof raw === "object" && "swagger" in raw && (scans.get(raw)?.pathRefs ?? true)) {
961
1208
  const refs = createRefs(raw);
962
1209
  inlinePathRefs(raw, (ref) => refs.resolve(ref, { report: false }));
963
1210
  }
@@ -3624,162 +3871,6 @@ function refPromotedEnums(node, promoted) {
3624
3871
  });
3625
3872
  } });
3626
3873
  }
3627
- // src/sourceLocator.ts
3628
- let cached;
3629
- const toRange = ([start, end]) => ({
3630
- start,
3631
- end
3632
- });
3633
- /** Walks a JSON pointer through maps and sequences; the range is the last key or item it reached. */
3634
- function findPointer(document, pointer) {
3635
- return pointer.replace(/^#/, "").split("/").slice(1).map(unescapePointerToken).reduce((at, token) => {
3636
- if (yaml.isMap(at?.node)) {
3637
- const pair = at.node.items.find((item) => yaml.isScalar(item.key) && String(item.key.value) === token);
3638
- return pair && yaml.isScalar(pair.key) && pair.key.range ? {
3639
- node: pair.value,
3640
- range: toRange(pair.key.range)
3641
- } : void 0;
3642
- }
3643
- if (yaml.isSeq(at?.node)) {
3644
- const item = at.node.items[Number(token)];
3645
- return yaml.isNode(item) && item.range ? {
3646
- node: item,
3647
- range: toRange(item.range)
3648
- } : void 0;
3649
- }
3650
- }, {
3651
- node: document.contents,
3652
- range: void 0
3653
- })?.range;
3654
- }
3655
- /** The first `$ref` whose value is `ref`. */
3656
- function findRefUsage(document, ref) {
3657
- const found = [];
3658
- yaml.visit(document, { Pair(_, { key, value }) {
3659
- if (!yaml.isScalar(key) || key.value !== "$ref" || !yaml.isScalar(value) || value.value !== ref || !value.range) return;
3660
- found.push(toRange(value.range));
3661
- return yaml.visit.BREAK;
3662
- } });
3663
- return found[0];
3664
- }
3665
- /**
3666
- * One pass over JSON text, so a located diagnostic in a large JSON spec skips `yaml.parseDocument`.
3667
- * Holds the range of every key and array item by pointer, and the first usage of every `$ref`, as
3668
- * `findPointer` and `findRefUsage` return them. Throws where the text breaks JSON syntax, such as a
3669
- * comment, an unquoted key, or an unquoted `$ref` value. Other bare values, such as `object`, pass.
3670
- */
3671
- function indexJson(source) {
3672
- const pointers = /* @__PURE__ */ new Map();
3673
- const refs = /* @__PURE__ */ new Map();
3674
- let at = source.indexOf("{");
3675
- const fail = () => {
3676
- throw new SyntaxError(`Unexpected character at ${at}`);
3677
- };
3678
- const skipSpace = () => {
3679
- for (let code = source.charCodeAt(at); code === 32 || code === 9 || code === 10 || code === 13; code = source.charCodeAt(++at));
3680
- };
3681
- const readString = () => {
3682
- if (source[at] !== "\"") fail();
3683
- const start = at;
3684
- for (at++; source.charCodeAt(at) !== 34; at++) {
3685
- if (at >= source.length) fail();
3686
- if (source.charCodeAt(at) === 92) at++;
3687
- }
3688
- const raw = source.slice(start + 1, at++);
3689
- return {
3690
- text: raw.includes("\\") ? JSON.parse(source.slice(start, at)) : raw,
3691
- start,
3692
- end: at
3693
- };
3694
- };
3695
- const expect = (char) => {
3696
- skipSpace();
3697
- if (source[at++] !== char) fail();
3698
- skipSpace();
3699
- };
3700
- const walk = (pointer) => {
3701
- const open = source[at];
3702
- if (open === "\"") {
3703
- readString();
3704
- return;
3705
- }
3706
- if (open !== "{" && open !== "[") {
3707
- const start = at;
3708
- while (at < source.length && !/[\s,\]}]/.test(source[at])) at++;
3709
- if (at === start) fail();
3710
- return;
3711
- }
3712
- const close = open === "{" ? "}" : "]";
3713
- at++;
3714
- for (let index = 0;; index++) {
3715
- skipSpace();
3716
- if (source[at] === close) break;
3717
- if (index > 0) expect(",");
3718
- if (open === "[") {
3719
- const start = at;
3720
- const next = `${pointer}/${index}`;
3721
- walk(next);
3722
- if (!pointers.has(next)) pointers.set(next, {
3723
- start,
3724
- end: at
3725
- });
3726
- continue;
3727
- }
3728
- const key = readString();
3729
- const next = `${pointer}/${escapePointerToken(key.text)}`;
3730
- if (!pointers.has(next)) pointers.set(next, {
3731
- start: key.start,
3732
- end: key.end
3733
- });
3734
- expect(":");
3735
- if (key.text !== "$ref" || source[at] === "{" || source[at] === "[") {
3736
- walk(next);
3737
- continue;
3738
- }
3739
- const value = readString();
3740
- if (!refs.has(value.text)) refs.set(value.text, {
3741
- start: value.start,
3742
- end: value.end
3743
- });
3744
- }
3745
- at++;
3746
- };
3747
- walk("#");
3748
- return {
3749
- pointers,
3750
- refs
3751
- };
3752
- }
3753
- /** Indexes plain JSON text; anything else goes to the YAML parser. */
3754
- function parse(source) {
3755
- if (/^[ \t\n\r]*\{/.test(source)) try {
3756
- return {
3757
- source,
3758
- json: indexJson(source)
3759
- };
3760
- } catch {}
3761
- return {
3762
- source,
3763
- document: yaml.parseDocument(source)
3764
- };
3765
- }
3766
- /**
3767
- * Maps OpenAPI JSON pointers and unresolved references to ranges in YAML or JSON source.
3768
- */
3769
- const locate = (source, location) => {
3770
- if (!("pointer" in location)) return void 0;
3771
- cached = cached?.source === source ? {
3772
- ...cached,
3773
- source
3774
- } : parse(source);
3775
- const ref = "ref" in location ? location.ref : void 0;
3776
- if ("json" in cached) {
3777
- const { pointers, refs } = cached.json;
3778
- return pointers.get(location.pointer) ?? (ref ? refs.get(ref) : void 0);
3779
- }
3780
- const { document } = cached;
3781
- return findPointer(document, location.pointer) ?? (ref ? findRefUsage(document, ref) : void 0);
3782
- };
3783
3874
  // src/adapter.ts
3784
3875
  /**
3785
3876
  * The `name` of `@kubb/adapter-oas`, used to identify this adapter in a Kubb config.