@kubb/adapter-oas 5.12.4 → 5.12.5

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
@@ -26,6 +26,7 @@ let _kubb_kit = require("@kubb/kit");
26
26
  let node_fs_promises = require("node:fs/promises");
27
27
  let _scalar_openapi_upgrader = require("@scalar/openapi-upgrader");
28
28
  let api_ref_bundler = require("api-ref-bundler");
29
+ let js_yaml = require("js-yaml");
29
30
  let yaml = require("yaml");
30
31
  yaml = __toESM(yaml, 1);
31
32
  //#region kubb-extension.json
@@ -35,7 +36,7 @@ var compatibility = {
35
36
  };
36
37
  //#endregion
37
38
  //#region package.json
38
- var version = "5.12.4";
39
+ var version = "5.12.5";
39
40
  //#endregion
40
41
  //#region src/constants.ts
41
42
  /**
@@ -243,12 +244,23 @@ function patchDiscriminatorNode(node, entry) {
243
244
  *
244
245
  * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
245
246
  */
246
- function toCamelOrPascal(text, pascal) {
247
+ function convert(text, pascal) {
247
248
  return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
248
249
  if (word.length > 1 && word === word.toUpperCase()) return word;
249
250
  return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
250
251
  }).join("").replace(/[^a-zA-Z0-9]/g, "");
251
252
  }
253
+ const cache = /* @__PURE__ */ new Map();
254
+ const cacheLimit = 1e4;
255
+ function toCamelOrPascal(text, pascal) {
256
+ const key = `${pascal ? "P" : "c"}${text}`;
257
+ const cached = cache.get(key);
258
+ if (cached !== void 0) return cached;
259
+ const result = convert(text, pascal);
260
+ if (cache.size >= cacheLimit) cache.clear();
261
+ cache.set(key, result);
262
+ return result;
263
+ }
252
264
  /**
253
265
  * Converts `text` to PascalCase.
254
266
  *
@@ -417,29 +429,28 @@ async function readSource(sourcePath) {
417
429
  /**
418
430
  * Reads and parses one source file or URL referenced during bundling: YAML/JSON is parsed into an
419
431
  * object, Markdown is returned as-is (bundled inline rather than dereferenced).
420
- *
421
- * JSON is valid YAML, so `yaml`'s `parse` handles both, but its general-purpose parser (comments,
422
- * anchors, block scalars, multi-document streams) does much more work than `JSON.parse` needs to.
423
- * `JSON.parse` runs first and fails fast on the first non-JSON character, so a real YAML document
424
- * falls through to `parse` at negligible cost.
425
432
  */
426
433
  async function resolveSource(sourcePath) {
427
434
  const data = await readSource(sourcePath);
428
435
  if (sourcePath.toLowerCase().endsWith(".md")) return data;
429
- try {
430
- return JSON.parse(data);
431
- } catch {
432
- return parseYaml(data, {
433
- origin: sourcePath,
434
- help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page."
435
- });
436
- }
436
+ return parseYaml(data, {
437
+ origin: sourcePath,
438
+ help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page."
439
+ });
437
440
  }
438
441
  /**
439
442
  * Parses a YAML (or JSON) spec. A syntax error throws `KUBB_INVALID_DOCUMENT` naming `origin`,
440
443
  * with the parser message, which keeps the line and column.
444
+ *
445
+ * JSON is valid YAML, so one parser handles both. `js-yaml` parses about ten times faster than
446
+ * `yaml`, and unlike `JSON.parse` it rejects duplicate keys instead of keeping the last one. Its
447
+ * core schema keeps timestamps as strings and `<<` as a plain key, as `yaml` does for YAML 1.2.
441
448
  */
442
449
  function parseYaml(data, { origin, help }) {
450
+ try {
451
+ const document = (0, js_yaml.load)(data, { schema: js_yaml.CORE_SCHEMA });
452
+ if (document !== void 0) return document;
453
+ } catch {}
443
454
  try {
444
455
  return (0, yaml.parse)(data);
445
456
  } catch (error) {
@@ -2318,7 +2329,7 @@ function visit(node, pointer) {
2318
2329
  }
2319
2330
  //#endregion
2320
2331
  //#region src/sourceLocator.ts
2321
- const cache = /* @__PURE__ */ new Map();
2332
+ let cached;
2322
2333
  const toRange = ([start, end]) => ({
2323
2334
  start,
2324
2335
  end
@@ -2361,11 +2372,12 @@ function findRefUsage(document, ref) {
2361
2372
  */
2362
2373
  const locate = (source, location) => {
2363
2374
  if (!("pointer" in location)) return void 0;
2364
- if (!cache.has(source)) {
2365
- cache.clear();
2366
- cache.set(source, yaml.parseDocument(source));
2367
- }
2368
- const document = cache.get(source);
2375
+ if (cached?.source === source) cached.source = source;
2376
+ else cached = {
2377
+ source,
2378
+ document: yaml.parseDocument(source)
2379
+ };
2380
+ const { document } = cached;
2369
2381
  return findPointer(document, location.pointer) ?? ("ref" in location && location.ref ? findRefUsage(document, location.ref) : void 0);
2370
2382
  };
2371
2383
  //#endregion