@kubb/adapter-oas 5.19.0 → 5.19.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.
- package/dist/index.cjs +55 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +56 -13
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { defineExtension } from "@kubb/core";
|
|
2
2
|
import { Diagnostics, ast, childName, enumPrimitive, enumPropName, macroDedupeUnion, macroDiscriminatorEnum, macroEnumName, mergeAdjacentObjectsLazy } from "@kubb/kit";
|
|
3
|
-
import {
|
|
3
|
+
import { access, realpath } from "node:fs/promises";
|
|
4
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
4
5
|
import { fileURLToPath } from "node:url";
|
|
5
6
|
import { readFile } from "node:fs";
|
|
6
|
-
import { access } from "node:fs/promises";
|
|
7
7
|
import { upgrade } from "@scalar/openapi-upgrader";
|
|
8
8
|
import { bundle } from "api-ref-bundler";
|
|
9
9
|
import { COLLECTION_STYLE, CORE_SCHEMA, EVENT_ID, SCALAR_STYLE, constructFromEvents, load, parseEvents } from "js-yaml";
|
|
@@ -14,7 +14,7 @@ var compatibility = {
|
|
|
14
14
|
"node": ">=22.22"
|
|
15
15
|
};
|
|
16
16
|
// package.json
|
|
17
|
-
var version = "5.19.
|
|
17
|
+
var version = "5.19.1";
|
|
18
18
|
// src/constants.ts
|
|
19
19
|
/**
|
|
20
20
|
* Default parser options applied when no explicit options are provided.
|
|
@@ -398,6 +398,27 @@ async function read(path) {
|
|
|
398
398
|
if (runtime.isBun) return Bun.file(path).text();
|
|
399
399
|
return new Promise((resolvePromise, reject) => readFile(path, { encoding: "utf8" }, (error, data) => error ? reject(error) : resolvePromise(data)));
|
|
400
400
|
}
|
|
401
|
+
/**
|
|
402
|
+
* Resolves to `true` when `path` is `parent` itself or nested inside it. Both sides are resolved
|
|
403
|
+
* to absolute paths first, so relative and `..`-containing inputs compare correctly.
|
|
404
|
+
*
|
|
405
|
+
* Guards a destructive or an out-of-tree operation: before wiping an output directory, check that
|
|
406
|
+
* it does not contain the project root, and before loading a path a caller supplied, check that it
|
|
407
|
+
* did not escape the directory it is allowed to read from.
|
|
408
|
+
*
|
|
409
|
+
* @example
|
|
410
|
+
* isPathInside('./src/gen', '.') // true — nested inside the root
|
|
411
|
+
* isPathInside('.', '.') // true — the same directory counts as inside
|
|
412
|
+
* isPathInside('.', './src/gen') // false — the root is not inside its own output
|
|
413
|
+
* isPathInside('../other', '.') // false — escapes the root
|
|
414
|
+
*/
|
|
415
|
+
function isPathInside(path, parent) {
|
|
416
|
+
const resolvedPath = resolve(path);
|
|
417
|
+
const resolvedParent = resolve(parent);
|
|
418
|
+
if (resolvedPath === resolvedParent) return true;
|
|
419
|
+
const rel = relative(resolvedParent, resolvedPath);
|
|
420
|
+
return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel);
|
|
421
|
+
}
|
|
401
422
|
// src/oas.ts
|
|
402
423
|
/**
|
|
403
424
|
* Returns `true` when a schema should be treated as nullable.
|
|
@@ -1089,6 +1110,16 @@ function getBaseUri(pathOrUrl, root) {
|
|
|
1089
1110
|
}
|
|
1090
1111
|
}
|
|
1091
1112
|
/**
|
|
1113
|
+
* Whether `filePath` is inside one of `folders`. Links are followed first, so a link inside a
|
|
1114
|
+
* folder cannot point outside it.
|
|
1115
|
+
*/
|
|
1116
|
+
async function isInsideFolders(filePath, folders) {
|
|
1117
|
+
const real = (item) => realpath(resolve(item)).catch(() => resolve(item));
|
|
1118
|
+
const target = await real(filePath);
|
|
1119
|
+
const realFolders = await Promise.all(folders.map(real));
|
|
1120
|
+
return [...folders, ...realFolders].some((folder) => isPathInside(target, folder));
|
|
1121
|
+
}
|
|
1122
|
+
/**
|
|
1092
1123
|
* Bundles a multi-file OpenAPI document into a single document via `api-ref-bundler`.
|
|
1093
1124
|
*
|
|
1094
1125
|
* External file schemas are hoisted into named `components.schemas` entries, so a property
|
|
@@ -1105,6 +1136,7 @@ function getBaseUri(pathOrUrl, root) {
|
|
|
1105
1136
|
* against the OpenAPI 3.2 `$self` when the document sets one. Every other file or URL read,
|
|
1106
1137
|
* including one that fails, lands in `sources`. A `$ref` that cannot be read reports
|
|
1107
1138
|
* `KUBB_REF_NOT_FOUND` at the key that holds it, as a warning when it sits inside an `x-` extension.
|
|
1139
|
+
* So does any local file from a URL input, and a local file outside every `fileRefs` folder.
|
|
1108
1140
|
*
|
|
1109
1141
|
* @example Local file
|
|
1110
1142
|
* `const document = await bundleDocument('./openapi.yaml')`
|
|
@@ -1112,14 +1144,20 @@ function getBaseUri(pathOrUrl, root) {
|
|
|
1112
1144
|
* @example Remote URL
|
|
1113
1145
|
* `const document = await bundleDocument('https://example.com/openapi.yaml')`
|
|
1114
1146
|
*/
|
|
1115
|
-
async function bundleDocument(pathOrUrl, { document, sources = [] } = {}) {
|
|
1147
|
+
async function bundleDocument(pathOrUrl, { document, sources = [], fileRefs } = {}) {
|
|
1116
1148
|
const cache = memo.create();
|
|
1117
1149
|
const toKey = (sourcePath) => urlRegExp.test(sourcePath) ? new URL(sourcePath).href : sourcePath;
|
|
1150
|
+
const folders = urlRegExp.test(pathOrUrl) ? [] : fileRefs;
|
|
1151
|
+
const refused = /* @__PURE__ */ new Set();
|
|
1118
1152
|
let entry = pathOrUrl;
|
|
1119
1153
|
let entryDocument = document;
|
|
1120
|
-
const resolver = (sourcePath) => {
|
|
1121
|
-
if (entryDocument !== void 0 && toKey(sourcePath) === toKey(entry)) return
|
|
1154
|
+
const resolver = async (sourcePath) => {
|
|
1155
|
+
if (entryDocument !== void 0 && toKey(sourcePath) === toKey(entry)) return entryDocument;
|
|
1122
1156
|
const key = toKey(sourcePath);
|
|
1157
|
+
if (key !== toKey(pathOrUrl) && !urlRegExp.test(sourcePath) && folders && !await isInsideFolders(sourcePath, folders)) {
|
|
1158
|
+
refused.add(sourcePath);
|
|
1159
|
+
throw new Error(`Refused ${sourcePath}`);
|
|
1160
|
+
}
|
|
1123
1161
|
return memo(cache, key, () => {
|
|
1124
1162
|
sources.push(key);
|
|
1125
1163
|
return resolveSource(sourcePath, { remember: key === toKey(pathOrUrl) });
|
|
@@ -1135,11 +1173,12 @@ async function bundleDocument(pathOrUrl, { document, sources = [] } = {}) {
|
|
|
1135
1173
|
entryDocument = root;
|
|
1136
1174
|
return await bundle(entry, resolver, { hooks: { onError: (message, { path, state }) => {
|
|
1137
1175
|
const pointer = toKey(state.baseFile) === toKey(entry) ? `#/${path.map((token) => escapePointerToken(String(token))).join("/")}` : void 0;
|
|
1176
|
+
const refusedPath = [...refused].find((sourcePath) => message.includes(sourcePath));
|
|
1138
1177
|
const diagnostic = {
|
|
1139
1178
|
code: Diagnostics.code.refNotFound,
|
|
1140
1179
|
severity: isExtensionPath(path) ? "warning" : "error",
|
|
1141
|
-
message: `Could not bundle an external \`$ref\`. ${message}.`,
|
|
1142
|
-
help: "Check that the file or URL the `$ref` points at exists and is readable, or fix the `$ref`.",
|
|
1180
|
+
message: refusedPath ? `Refused an external \`$ref\` to the local file ${refusedPath}.` : `Could not bundle an external \`$ref\`. ${message}.`,
|
|
1181
|
+
help: refusedPath ? folders?.length ? "This run only reads local files inside the folders its host allows, such as the project. Move the file there or inline the schema. On your own agent, `--allow-file-refs` lifts the limit." : "This input reads no local file. Inline the schema, or serve the file from a URL." : "Check that the file or URL the `$ref` points at exists and is readable, or fix the `$ref`.",
|
|
1143
1182
|
...pointer && { location: {
|
|
1144
1183
|
kind: "schema",
|
|
1145
1184
|
pointer
|
|
@@ -1166,7 +1205,7 @@ const inlineInputName = "<inline input>";
|
|
|
1166
1205
|
* const document = await parseFromConfig({ type: 'data', data: '{"openapi":"3.0.0",...}' })
|
|
1167
1206
|
* ```
|
|
1168
1207
|
*/
|
|
1169
|
-
async function parseFromConfig(source, sources = []) {
|
|
1208
|
+
async function parseFromConfig(source, sources = [], { fileRefs } = {}) {
|
|
1170
1209
|
const raw = await (async () => {
|
|
1171
1210
|
if (source.type === "data") {
|
|
1172
1211
|
const data = typeof source.data === "string" ? parseYaml(source.data, {
|
|
@@ -1176,14 +1215,18 @@ async function parseFromConfig(source, sources = []) {
|
|
|
1176
1215
|
const isObject = Boolean(data) && typeof data === "object";
|
|
1177
1216
|
if (source.root && isObject) return bundleDocument(join(source.root, inlineInputName), {
|
|
1178
1217
|
document: data,
|
|
1179
|
-
sources
|
|
1218
|
+
sources,
|
|
1219
|
+
fileRefs
|
|
1180
1220
|
});
|
|
1181
1221
|
const scan = resolveTargets(data);
|
|
1182
1222
|
if (isObject) scans.set(data, scan);
|
|
1183
1223
|
return data;
|
|
1184
1224
|
}
|
|
1185
1225
|
await assertInputExists(source.path);
|
|
1186
|
-
return bundleDocument(source.path, {
|
|
1226
|
+
return bundleDocument(source.path, {
|
|
1227
|
+
sources,
|
|
1228
|
+
fileRefs
|
|
1229
|
+
});
|
|
1187
1230
|
})();
|
|
1188
1231
|
if (raw && typeof raw === "object") {
|
|
1189
1232
|
const document = raw;
|
|
@@ -4037,7 +4080,7 @@ const adapterOas = defineExtension({
|
|
|
4037
4080
|
async settle() {
|
|
4038
4081
|
await Promise.all(pendingValidations.splice(0));
|
|
4039
4082
|
},
|
|
4040
|
-
async parse(source, { signal } = {}) {
|
|
4083
|
+
async parse(source, { signal, fileRefs } = {}) {
|
|
4041
4084
|
signal?.throwIfAborted();
|
|
4042
4085
|
const cached = inputCache.get(source);
|
|
4043
4086
|
if (cached) {
|
|
@@ -4047,7 +4090,7 @@ const adapterOas = defineExtension({
|
|
|
4047
4090
|
const read = [];
|
|
4048
4091
|
sources = read;
|
|
4049
4092
|
const promise = (async () => {
|
|
4050
|
-
const document = await parseFromConfig(source, read);
|
|
4093
|
+
const document = await parseFromConfig(source, read, { fileRefs });
|
|
4051
4094
|
signal?.throwIfAborted();
|
|
4052
4095
|
assertDocument(document);
|
|
4053
4096
|
if (validate) pendingValidations.push(validateDocumentInWorker(document));
|