@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.js CHANGED
@@ -3,6 +3,7 @@ import { Diagnostics, ast, childName, enumPropName, extractRefName, macroDiscrim
3
3
  import { access, readFile } from "node:fs/promises";
4
4
  import { upgrade } from "@scalar/openapi-upgrader";
5
5
  import { bundle } from "api-ref-bundler";
6
+ import { CORE_SCHEMA, load } from "js-yaml";
6
7
  import * as yaml from "yaml";
7
8
  import { parse } from "yaml";
8
9
  //#endregion
@@ -13,7 +14,7 @@ var compatibility = {
13
14
  };
14
15
  //#endregion
15
16
  //#region package.json
16
- var version = "5.12.4";
17
+ var version = "5.12.5";
17
18
  //#endregion
18
19
  //#region src/constants.ts
19
20
  /**
@@ -221,12 +222,23 @@ function patchDiscriminatorNode(node, entry) {
221
222
  *
222
223
  * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
223
224
  */
224
- function toCamelOrPascal(text, pascal) {
225
+ function convert(text, pascal) {
225
226
  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) => {
226
227
  if (word.length > 1 && word === word.toUpperCase()) return word;
227
228
  return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
228
229
  }).join("").replace(/[^a-zA-Z0-9]/g, "");
229
230
  }
231
+ const cache = /* @__PURE__ */ new Map();
232
+ const cacheLimit = 1e4;
233
+ function toCamelOrPascal(text, pascal) {
234
+ const key = `${pascal ? "P" : "c"}${text}`;
235
+ const cached = cache.get(key);
236
+ if (cached !== void 0) return cached;
237
+ const result = convert(text, pascal);
238
+ if (cache.size >= cacheLimit) cache.clear();
239
+ cache.set(key, result);
240
+ return result;
241
+ }
230
242
  /**
231
243
  * Converts `text` to PascalCase.
232
244
  *
@@ -395,29 +407,28 @@ async function readSource(sourcePath) {
395
407
  /**
396
408
  * Reads and parses one source file or URL referenced during bundling: YAML/JSON is parsed into an
397
409
  * object, Markdown is returned as-is (bundled inline rather than dereferenced).
398
- *
399
- * JSON is valid YAML, so `yaml`'s `parse` handles both, but its general-purpose parser (comments,
400
- * anchors, block scalars, multi-document streams) does much more work than `JSON.parse` needs to.
401
- * `JSON.parse` runs first and fails fast on the first non-JSON character, so a real YAML document
402
- * falls through to `parse` at negligible cost.
403
410
  */
404
411
  async function resolveSource(sourcePath) {
405
412
  const data = await readSource(sourcePath);
406
413
  if (sourcePath.toLowerCase().endsWith(".md")) return data;
407
- try {
408
- return JSON.parse(data);
409
- } catch {
410
- return parseYaml(data, {
411
- origin: sourcePath,
412
- help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page."
413
- });
414
- }
414
+ return parseYaml(data, {
415
+ origin: sourcePath,
416
+ help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page."
417
+ });
415
418
  }
416
419
  /**
417
420
  * Parses a YAML (or JSON) spec. A syntax error throws `KUBB_INVALID_DOCUMENT` naming `origin`,
418
421
  * with the parser message, which keeps the line and column.
422
+ *
423
+ * JSON is valid YAML, so one parser handles both. `js-yaml` parses about ten times faster than
424
+ * `yaml`, and unlike `JSON.parse` it rejects duplicate keys instead of keeping the last one. Its
425
+ * core schema keeps timestamps as strings and `<<` as a plain key, as `yaml` does for YAML 1.2.
419
426
  */
420
427
  function parseYaml(data, { origin, help }) {
428
+ try {
429
+ const document = load(data, { schema: CORE_SCHEMA });
430
+ if (document !== void 0) return document;
431
+ } catch {}
421
432
  try {
422
433
  return parse(data);
423
434
  } catch (error) {
@@ -2296,7 +2307,7 @@ function visit(node, pointer) {
2296
2307
  }
2297
2308
  //#endregion
2298
2309
  //#region src/sourceLocator.ts
2299
- const cache = /* @__PURE__ */ new Map();
2310
+ let cached;
2300
2311
  const toRange = ([start, end]) => ({
2301
2312
  start,
2302
2313
  end
@@ -2339,11 +2350,12 @@ function findRefUsage(document, ref) {
2339
2350
  */
2340
2351
  const locate = (source, location) => {
2341
2352
  if (!("pointer" in location)) return void 0;
2342
- if (!cache.has(source)) {
2343
- cache.clear();
2344
- cache.set(source, yaml.parseDocument(source));
2345
- }
2346
- const document = cache.get(source);
2353
+ if (cached?.source === source) cached.source = source;
2354
+ else cached = {
2355
+ source,
2356
+ document: yaml.parseDocument(source)
2357
+ };
2358
+ const { document } = cached;
2347
2359
  return findPointer(document, location.pointer) ?? ("ref" in location && location.ref ? findRefUsage(document, location.ref) : void 0);
2348
2360
  };
2349
2361
  //#endregion