@scalar/mock-server 0.21.7 → 0.21.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/CHANGELOG.md +20 -0
- package/dist/create-mock-server.js +1 -1
- package/dist/create-openapi-mock-server.d.ts +5 -2
- package/dist/create-openapi-mock-server.d.ts.map +1 -1
- package/dist/create-openapi-mock-server.js +67 -20
- package/dist/routes/mock-any-response.d.ts.map +1 -1
- package/dist/routes/mock-any-response.js +22 -20
- package/dist/utils/build-handler-context.d.ts.map +1 -1
- package/dist/utils/build-handler-context.js +9 -9
- package/dist/utils/deserialize-parameter.d.ts +15 -0
- package/dist/utils/deserialize-parameter.d.ts.map +1 -1
- package/dist/utils/deserialize-parameter.js +36 -7
- package/dist/utils/expand-server-variables.d.ts +6 -0
- package/dist/utils/expand-server-variables.d.ts.map +1 -0
- package/dist/utils/expand-server-variables.js +44 -0
- package/dist/utils/get-response-header-example.d.ts +13 -0
- package/dist/utils/get-response-header-example.d.ts.map +1 -0
- package/dist/utils/get-response-header-example.js +94 -0
- package/dist/utils/is-json-content-type.d.ts +4 -0
- package/dist/utils/is-json-content-type.d.ts.map +1 -0
- package/dist/utils/is-json-content-type.js +2 -0
- package/dist/utils/process-document.d.ts +2 -0
- package/dist/utils/process-document.d.ts.map +1 -1
- package/dist/utils/process-document.js +65 -9
- package/dist/utils/resolve-server-base-paths.d.ts +17 -0
- package/dist/utils/resolve-server-base-paths.d.ts.map +1 -0
- package/dist/utils/resolve-server-base-paths.js +85 -0
- package/dist/utils/resolve-server-pathnames.d.ts.map +1 -1
- package/dist/utils/resolve-server-pathnames.js +9 -12
- package/dist/utils/serialize-linkset.d.ts +3 -0
- package/dist/utils/serialize-linkset.d.ts.map +1 -0
- package/dist/utils/serialize-linkset.js +77 -0
- package/dist/utils/validate-request.d.ts.map +1 -1
- package/dist/utils/validate-request.js +165 -96
- package/package.json +10 -10
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process-document.d.ts","sourceRoot":"","sources":["../../src/utils/process-document.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"process-document.d.ts","sourceRoot":"","sources":["../../src/utils/process-document.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAErE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAA;AAOlE,8FAA8F;AAC9F,MAAM,MAAM,iBAAiB,GACzB;IACE,IAAI,EAAE,SAAS,CAAA;IACf,QAAQ,EAAE,WAAW,CAAC,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAA;IACrD,sBAAsB,CAAC,EAAE,MAAM,CAAA;IAC/B,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,mBAAmB,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAC7C,GACD;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,EAAE,gBAAgB,CAAA;CAAE,CAAA;AA8DpD;;;;GAIG;AACH,eAAO,MAAM,eAAe,GAC1B,UAAU,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EACtD,SAAS,MAAM,KACd,OAAO,CAAC,iBAAiB,CAgG3B,CAAA"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import { cwd } from 'node:process';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { getOwnEntry } from '@scalar/helpers/object/get-own-entry';
|
|
4
|
+
import { isObject, isObjectLike } from '@scalar/helpers/object/is-object';
|
|
5
|
+
import { safeRun } from '@scalar/helpers/types/safe-run';
|
|
6
|
+
import { bundle, resolveReferencePath } from '@scalar/json-magic/bundle';
|
|
5
7
|
import { fetchUrls, parseJson, parseYaml, readFiles } from '@scalar/json-magic/bundle/plugins/node';
|
|
6
8
|
import { isFilePath } from '@scalar/json-magic/helpers/is-file-path';
|
|
7
9
|
import { isHttpUrl } from '@scalar/json-magic/helpers/is-http-url';
|
|
@@ -11,6 +13,57 @@ import { openApiDocument, resolveOpenApiDocument } from '@scalar/workspace-store
|
|
|
11
13
|
import { isPluginDocument } from '@scalar/workspace-store/schemas/plugin-document';
|
|
12
14
|
import { isAsyncApiDocument } from '@scalar/workspace-store/schemas/type-guards';
|
|
13
15
|
import { cloneDocument } from './clone-document.js';
|
|
16
|
+
/** Track external server lists by identity so their source URL stays with the resolved object. */
|
|
17
|
+
const indexServerRetrievalUris = (documents, mappings, baseUri) => {
|
|
18
|
+
const origins = new WeakMap();
|
|
19
|
+
const visited = new WeakSet();
|
|
20
|
+
const indexBundle = (documents, mappings, baseUri, parentUri) => {
|
|
21
|
+
if (!isObject(documents)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const sources = isObject(mappings) ? mappings : undefined;
|
|
25
|
+
for (const [key, document] of Object.entries(documents)) {
|
|
26
|
+
const source = getOwnEntry(sources, key);
|
|
27
|
+
const resolved = safeRun(() => {
|
|
28
|
+
if (typeof source !== 'string') {
|
|
29
|
+
return parentUri;
|
|
30
|
+
}
|
|
31
|
+
if (baseUri) {
|
|
32
|
+
return resolveReferencePath(baseUri, source);
|
|
33
|
+
}
|
|
34
|
+
return URL.canParse(source) ? source : parentUri;
|
|
35
|
+
});
|
|
36
|
+
const retrievalUri = resolved.ok ? resolved.data : parentUri;
|
|
37
|
+
const stack = [document];
|
|
38
|
+
while (stack.length > 0) {
|
|
39
|
+
const node = stack.pop();
|
|
40
|
+
if (!isObjectLike(node) || visited.has(node)) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
visited.add(node);
|
|
44
|
+
if (retrievalUri && Array.isArray(node.servers)) {
|
|
45
|
+
origins.set(node.servers, retrievalUri);
|
|
46
|
+
}
|
|
47
|
+
for (const [key, child] of Object.entries(node)) {
|
|
48
|
+
if (node === document && key === 'x-ext' && isObject(child)) {
|
|
49
|
+
// Imported bundles record their URL mappings relative to their own base URI.
|
|
50
|
+
// An absolute document identity can resolve relative mappings even without a retrieval URI.
|
|
51
|
+
const self = node.$self;
|
|
52
|
+
const identityBase = retrievalUri ?? (typeof self === 'string' && URL.canParse(self) ? self : undefined);
|
|
53
|
+
const identity = identityBase ? safeRun(() => resolveOpenApiDocument(node, identityBase)) : undefined;
|
|
54
|
+
const nestedBase = (identity?.ok ? identity.data?.baseUri : undefined) ?? retrievalUri;
|
|
55
|
+
indexBundle(child, node['x-ext-urls'], nestedBase, retrievalUri);
|
|
56
|
+
}
|
|
57
|
+
else if (retrievalUri) {
|
|
58
|
+
stack.push(child);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
indexBundle(documents, mappings, baseUri);
|
|
65
|
+
return origins;
|
|
66
|
+
};
|
|
14
67
|
/**
|
|
15
68
|
* Bundle API descriptions once, retaining AsyncAPI and upgrading compatible OpenAPI input.
|
|
16
69
|
*
|
|
@@ -25,6 +78,7 @@ export const processDocument = async (document, origin) => {
|
|
|
25
78
|
// Return a minimal valid OpenAPI 3.2 document
|
|
26
79
|
return {
|
|
27
80
|
type: 'openapi',
|
|
81
|
+
serverRetrievalUris: new WeakMap(),
|
|
28
82
|
document: {
|
|
29
83
|
openapi: '3.2.0',
|
|
30
84
|
info: {
|
|
@@ -44,8 +98,7 @@ export const processDocument = async (document, origin) => {
|
|
|
44
98
|
try {
|
|
45
99
|
// Bundle external references with Node.js plugins
|
|
46
100
|
// Include parseJson and parseYaml to handle string inputs
|
|
47
|
-
//
|
|
48
|
-
// `x-ext-urls` — so an object input is copied first and the caller's document is left as passed.
|
|
101
|
+
// Bundling writes external documents and their URL mappings, so copy object inputs first.
|
|
49
102
|
bundled = await bundle(typeof document === 'string' ? document : cloneDocument(document), {
|
|
50
103
|
origin,
|
|
51
104
|
plugins: [
|
|
@@ -56,6 +109,7 @@ export const processDocument = async (document, origin) => {
|
|
|
56
109
|
fetchUrls({ blockPrivateNetworks: true }),
|
|
57
110
|
],
|
|
58
111
|
treeShake: false,
|
|
112
|
+
urlMap: true,
|
|
59
113
|
});
|
|
60
114
|
}
|
|
61
115
|
catch (error) {
|
|
@@ -67,17 +121,17 @@ export const processDocument = async (document, origin) => {
|
|
|
67
121
|
if (isPluginDocument(bundled)) {
|
|
68
122
|
throw new Error('Plugin documents are not supported by the mock server');
|
|
69
123
|
}
|
|
124
|
+
// Keep retrieval metadata out of the exported API description.
|
|
125
|
+
const externalDocumentUrls = bundled['x-ext-urls'];
|
|
126
|
+
delete bundled['x-ext-urls'];
|
|
70
127
|
if (isAsyncApiDocument(bundled)) {
|
|
71
128
|
return { type: 'asyncapi', document: createMagicProxy(bundled) };
|
|
72
129
|
}
|
|
73
130
|
// Upgrading must not activate a $self field authored in an older OpenAPI version.
|
|
74
131
|
const retrievalUri = origin ?? (typeof document === 'string' && (isFilePath(document) || isHttpUrl(document)) ? document : '/');
|
|
75
132
|
const documentUri = resolveOpenApiDocument(bundled, retrievalUri)?.baseUri;
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
: typeof bundled.swagger === 'string'
|
|
79
|
-
? bundled.swagger
|
|
80
|
-
: undefined;
|
|
133
|
+
const swaggerVersion = typeof bundled.swagger === 'string' ? bundled.swagger : undefined;
|
|
134
|
+
const originalOpenapiVersion = typeof bundled.openapi === 'string' ? bundled.openapi : swaggerVersion;
|
|
81
135
|
let upgraded;
|
|
82
136
|
try {
|
|
83
137
|
upgraded = upgrade(bundled, '3.2', { onIncompatible: 'collect' }).document;
|
|
@@ -94,5 +148,7 @@ export const processDocument = async (document, origin) => {
|
|
|
94
148
|
type: 'openapi',
|
|
95
149
|
document: createMagicProxy(upgraded, { documentUri }),
|
|
96
150
|
originalOpenapiVersion,
|
|
151
|
+
retrievalUri,
|
|
152
|
+
serverRetrievalUris: indexServerRetrievalUris(upgraded['x-ext'], externalDocumentUrls, documentUri ?? retrievalUri),
|
|
97
153
|
};
|
|
98
154
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { OpenAPIV3_1 } from '@scalar/openapi-types';
|
|
2
|
+
/** Maximum server URL combinations processed per OpenAPI description. */
|
|
3
|
+
export declare const MAX_SERVER_URL_EXPANSIONS = 4096;
|
|
4
|
+
/**
|
|
5
|
+
* Resolve the base paths the servers of an operation put in front of its path keys.
|
|
6
|
+
*
|
|
7
|
+
* Every combination of enum values of the server variables is served, and the default is used when
|
|
8
|
+
* a variable has no enum. A server whose URL keeps a variable without an enum or default adds no
|
|
9
|
+
* base path, because its path cannot be known, and neither does a URL that does not parse. Relative
|
|
10
|
+
* URLs use the document's HTTP retrieval URL when known and the mock root otherwise. The root path
|
|
11
|
+
* is left out, since every operation is already served there. Returns undefined when the shared
|
|
12
|
+
* document budget is exhausted.
|
|
13
|
+
*/
|
|
14
|
+
export declare const resolveServerBasePaths: (servers?: OpenAPIV3_1.ServerObject[], retrievalUri?: string, budget?: {
|
|
15
|
+
remaining: number;
|
|
16
|
+
}) => string[] | undefined;
|
|
17
|
+
//# sourceMappingURL=resolve-server-base-paths.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-server-base-paths.d.ts","sourceRoot":"","sources":["../../src/utils/resolve-server-base-paths.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAyBxD,yEAAyE;AACzE,eAAO,MAAM,yBAAyB,OAAQ,CAAA;AAyB9C;;;;;;;;;GASG;AACH,eAAO,MAAM,sBAAsB,GACjC,UAAS,WAAW,CAAC,YAAY,EAAO,EACxC,qBAAiB,EACjB;;CAAiD,KAChD,MAAM,EAAE,GAAG,SAyCb,CAAA"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { getOwnEntry } from '@scalar/helpers/object/get-own-entry';
|
|
2
|
+
import { isObject } from '@scalar/helpers/object/is-object';
|
|
3
|
+
import { REGEX } from '@scalar/helpers/regex/regex-helpers';
|
|
4
|
+
import { ensureProtocol } from '@scalar/helpers/url/ensure-protocol';
|
|
5
|
+
import { isRelativePath } from '@scalar/helpers/url/is-relative-path';
|
|
6
|
+
import { isHttpUrl } from '@scalar/json-magic/helpers/is-http-url';
|
|
7
|
+
import { getPath } from 'hono/utils/url';
|
|
8
|
+
import { expandServerVariables } from './expand-server-variables.js';
|
|
9
|
+
/** Collect usable values from an OpenAPI server variable, which may come from an unvalidated document. */
|
|
10
|
+
const getVariableValues = function* (variable) {
|
|
11
|
+
if (!isObject(variable)) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (Array.isArray(variable.enum) && variable.enum.length > 0) {
|
|
15
|
+
for (const value of variable.enum) {
|
|
16
|
+
if (typeof value === 'string') {
|
|
17
|
+
yield value;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (typeof variable.default === 'string') {
|
|
23
|
+
yield variable.default;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
/** Maximum server URL combinations processed per OpenAPI description. */
|
|
27
|
+
export const MAX_SERVER_URL_EXPANSIONS = 4_096;
|
|
28
|
+
/** Resolve a server URL to the URL it names, reading a domain without a scheme as a host like the API client does. */
|
|
29
|
+
const toAbsoluteUrl = (url, baseUrl) => {
|
|
30
|
+
if (/^[a-z][a-z\d+.-]*:\/\//i.test(url)) {
|
|
31
|
+
return new URL(url);
|
|
32
|
+
}
|
|
33
|
+
const hasBareHostWithPort = /^[a-z\d-]+\.[a-z\d.-]+:\d+(?:[/?#]|$)/i.test(url);
|
|
34
|
+
if (!hasBareHostWithPort && isRelativePath(url)) {
|
|
35
|
+
// Preserve the path interpretation for scheme-less inputs the client treats as relative.
|
|
36
|
+
return new URL(url.startsWith('/') || url === '' ? url : `./${url}`, baseUrl);
|
|
37
|
+
}
|
|
38
|
+
return new URL(ensureProtocol(url));
|
|
39
|
+
};
|
|
40
|
+
/** Collapse path slashes before URL parsing resolves dot segments, while preserving the authority. */
|
|
41
|
+
const normalizePathSlashes = (url) => url.replace(/^([a-z][a-z\d+.-]*:\/\/[^/?#]*|\/\/[^/?#]+)?([^?#]*)/i, (_, authority, pathname) => (authority ?? '') + pathname.replace(REGEX.MULTIPLE_SLASHES, '/'));
|
|
42
|
+
/**
|
|
43
|
+
* Resolve the base paths the servers of an operation put in front of its path keys.
|
|
44
|
+
*
|
|
45
|
+
* Every combination of enum values of the server variables is served, and the default is used when
|
|
46
|
+
* a variable has no enum. A server whose URL keeps a variable without an enum or default adds no
|
|
47
|
+
* base path, because its path cannot be known, and neither does a URL that does not parse. Relative
|
|
48
|
+
* URLs use the document's HTTP retrieval URL when known and the mock root otherwise. The root path
|
|
49
|
+
* is left out, since every operation is already served there. Returns undefined when the shared
|
|
50
|
+
* document budget is exhausted.
|
|
51
|
+
*/
|
|
52
|
+
export const resolveServerBasePaths = (servers = [], retrievalUri = '', budget = { remaining: MAX_SERVER_URL_EXPANSIONS }) => {
|
|
53
|
+
const baseUrl = isHttpUrl(retrievalUri) ? retrievalUri : 'http://localhost';
|
|
54
|
+
const pathnames = new Set();
|
|
55
|
+
for (const server of servers) {
|
|
56
|
+
if (typeof server?.url !== 'string') {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const variables = isObject(server.variables) ? server.variables : undefined;
|
|
60
|
+
const isAbsolutePath = server.url.startsWith('/') && !server.url.startsWith('//');
|
|
61
|
+
const expansion = expandServerVariables(server.url, (name) => getVariableValues(getOwnEntry(variables, name)), budget.remaining);
|
|
62
|
+
if (!expansion) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
budget.remaining -= expansion.combinationCount;
|
|
66
|
+
for (const url of expansion.urls) {
|
|
67
|
+
try {
|
|
68
|
+
// A leading slash added by a variable must not turn an absolute-path reference into a host.
|
|
69
|
+
const serverUrl = isAbsolutePath ? url.replace(/^\/+/, '/') : url;
|
|
70
|
+
const resolved = toAbsoluteUrl(normalizePathSlashes(serverUrl), baseUrl);
|
|
71
|
+
const pathname = resolved.pathname.replace(REGEX.MULTIPLE_SLASHES, '/').replace(/\/$/, '');
|
|
72
|
+
if (pathname.startsWith('/')) {
|
|
73
|
+
pathnames.add(pathname);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// A server URL that does not parse cannot be requested, so it adds no route.
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// Hostname combinations can share a pathname, and different encodings can decode to the same route.
|
|
82
|
+
// Braces left after decoding would be treated as path templates.
|
|
83
|
+
const basePaths = [...pathnames].map((pathname) => getPath(new Request(`http://localhost${pathname}`)));
|
|
84
|
+
return [...new Set(basePaths.filter((path) => !/[{}]/.test(path)))];
|
|
85
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve-server-pathnames.d.ts","sourceRoot":"","sources":["../../src/utils/resolve-server-pathnames.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAA;
|
|
1
|
+
{"version":3,"file":"resolve-server-pathnames.d.ts","sourceRoot":"","sources":["../../src/utils/resolve-server-pathnames.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAA;AAKtE,+FAA+F;AAC/F,eAAO,MAAM,sBAAsB,GAAI,QAAQ,oBAAoB,KAAG,MAAM,EAS5D,CAAA"}
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import { getResolvedRef } from '@scalar/workspace-store/helpers/get-resolved-ref';
|
|
2
|
+
import { expandServerVariables } from './expand-server-variables.js';
|
|
2
3
|
/** Expand server pathname variables without expanding channel parameters or host variables. */
|
|
3
|
-
export const resolveServerPathnames = (server) => {
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return paths;
|
|
12
|
-
return [...new Set(paths.flatMap((path) => values.map((value) => path.replaceAll(`{${name}}`, () => value))))];
|
|
13
|
-
}, [pathname]);
|
|
14
|
-
};
|
|
4
|
+
export const resolveServerPathnames = (server) => expandServerVariables(server.pathname ?? '', (name) => {
|
|
5
|
+
const variable = getResolvedRef(server.variables?.[name]);
|
|
6
|
+
if (variable?.enum?.length) {
|
|
7
|
+
return variable.enum;
|
|
8
|
+
}
|
|
9
|
+
// Unconstrained variables remain dynamic route parameters.
|
|
10
|
+
return variable?.default !== undefined ? [variable.default] : undefined;
|
|
11
|
+
})?.urls ?? [];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialize-linkset.d.ts","sourceRoot":"","sources":["../../src/utils/serialize-linkset.ts"],"names":[],"mappings":"AAgFA,0FAA0F;AAC1F,eAAO,MAAM,gBAAgB,GAAI,OAAO,OAAO,KAAG,MAAM,GAAG,SAG1D,CAAA"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { HTTP_TOKEN } from '@scalar/helpers/http/http-token';
|
|
2
|
+
import { quoteHttpString } from '@scalar/helpers/http/quote-http-string';
|
|
3
|
+
import { isObject } from '@scalar/helpers/object/is-object';
|
|
4
|
+
/** Extended attributes use RFC 8187 syntax, including for UTF-8 titles. */
|
|
5
|
+
const encodeExtendedAttribute = (attribute) => {
|
|
6
|
+
if (!isObject(attribute) || typeof attribute.value !== 'string') {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
const language = attribute.language ?? '';
|
|
10
|
+
if (typeof language !== 'string' || /[^a-z0-9-]/i.test(language)) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
const text = encodeURIComponent(attribute.value).replace(/['()*]/g, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
14
|
+
return `UTF-8'${language}'${text}`;
|
|
15
|
+
};
|
|
16
|
+
const serializeTarget = (target, relation, anchor) => {
|
|
17
|
+
if (!isObject(target)) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
const { href, ...attributes } = target;
|
|
21
|
+
if (typeof href !== 'string' || /[^\x21-\x7e]|[<>"\\]/.test(href)) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
const parameters = [`rel=${quoteHttpString(relation)}`];
|
|
25
|
+
if (anchor !== undefined) {
|
|
26
|
+
parameters.push(`anchor=${quoteHttpString(anchor)}`);
|
|
27
|
+
}
|
|
28
|
+
for (const [name, attribute] of Object.entries(attributes)) {
|
|
29
|
+
if (HTTP_TOKEN.exec(name)?.[0] !== name || ['rel', 'anchor'].includes(name.toLowerCase())) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
for (const item of Array.isArray(attribute) ? attribute : [attribute]) {
|
|
33
|
+
const text = name.endsWith('*')
|
|
34
|
+
? encodeExtendedAttribute(item)
|
|
35
|
+
: typeof item === 'string' && quoteHttpString(item);
|
|
36
|
+
if (typeof text !== 'string') {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
parameters.push(`${name}=${text}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return `<${href}>; ${parameters.join('; ')}`;
|
|
43
|
+
};
|
|
44
|
+
/** Render RFC 9264 link contexts in HTTP Link field syntax. */
|
|
45
|
+
const serializeContexts = (value) => {
|
|
46
|
+
if (!isObject(value) || !Array.isArray(value.linkset)) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const links = [];
|
|
50
|
+
for (const context of value.linkset) {
|
|
51
|
+
if (!isObject(context)) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
const { anchor, ...relations } = context;
|
|
55
|
+
if (anchor !== undefined && typeof anchor !== 'string') {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
for (const [relation, targets] of Object.entries(relations)) {
|
|
59
|
+
if (!Array.isArray(targets)) {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
for (const target of targets) {
|
|
63
|
+
const link = serializeTarget(target, relation, anchor);
|
|
64
|
+
if (link === undefined) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
links.push(link);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return links.join(', ');
|
|
72
|
+
};
|
|
73
|
+
/** Standalone linkset documents allow line breaks that HTTP field values cannot carry. */
|
|
74
|
+
export const serializeLinkset = (value) => {
|
|
75
|
+
const text = typeof value === 'string' ? value.replace(/\r?\n/g, ' ') : serializeContexts(value);
|
|
76
|
+
return text !== undefined && !/[^\t\x20-\x7e]/.test(text) ? text : undefined;
|
|
77
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate-request.d.ts","sourceRoot":"","sources":["../../src/utils/validate-request.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validate-request.d.ts","sourceRoot":"","sources":["../../src/utils/validate-request.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAe,MAAM,uBAAuB,CAAA;AAMrE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAA;AAkZ7C;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,GAC1B,WAAW,WAAW,CAAC,eAAe,EACtC,qBAAqB,WAAW,CAAC,cAAc,CAAC,YAAY,CAAC,KAC5D,iBAmMF,CAAA"}
|