@vercel/fs-detectors 7.13.0 → 7.14.0-snapshot-c9dfd83f2293

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.
@@ -1,6 +1,6 @@
1
1
  import type { Route } from '@vercel/routing-utils';
2
2
  import { type Framework } from '@vercel/frameworks';
3
- import type { PackageJson, Builder, BuilderFunctions, ProjectSettings } from '@vercel/build-utils';
3
+ import type { PackageJson, Builder, BuilderFunctions, ProjectSettings, ProxyConfig } from '@vercel/build-utils';
4
4
  /**
5
5
  * Pattern for finding all supported middleware files.
6
6
  */
@@ -25,10 +25,7 @@ export interface ErrorResponse {
25
25
  action?: string;
26
26
  link?: string;
27
27
  }
28
- export interface ProxyConfig {
29
- entrypoint: string;
30
- matcher?: string | string[];
31
- }
28
+ export type { ProxyConfig };
32
29
  export interface Options {
33
30
  tag?: string;
34
31
  functions?: BuilderFunctions;
@@ -68,6 +65,11 @@ export declare function detectBuilders(files: string[], pkg?: PackageJson | unde
68
65
  rewriteRoutes: Route[] | null;
69
66
  errorRoutes: Route[] | null;
70
67
  }>;
68
+ export interface ParsedProxyEntrypoint {
69
+ file: string;
70
+ variable?: string;
71
+ }
72
+ export declare function parseProxyEntrypoint(entrypoint: string): ParsedProxyEntrypoint;
71
73
  export interface ResolvedProxy {
72
74
  /**
73
75
  * `vercel-config`: the `proxy` property in the Vercel config (`vercel.json`, etc.).
@@ -84,17 +86,23 @@ export interface ResolvedProxy {
84
86
  */
85
87
  export declare function resolveProxy(files: string[], options: Options): ResolvedProxy | null;
86
88
  export declare function getProxyBuilder(proxy: ProxyConfig, tag?: string, functions?: BuilderFunctions): Builder;
89
+ export interface ProxyValidationOptions {
90
+ /** Config path named in error messages, eg. `proxy` or `services.api.proxy`. */
91
+ field: string;
92
+ /** Whether the proxy is project level or service level. */
93
+ level: 'project' | 'service';
94
+ }
87
95
  /**
88
96
  * Validates a `proxy` config value in isolation, without knowledge of the
89
97
  * project's files or framework. Also used by the CLI to validate
90
98
  * `vercel.json` before builders are detected.
91
99
  */
92
- export declare function validateProxyConfig(proxy: ProxyConfig): ErrorResponse | null;
100
+ export declare function validateProxyConfig(proxy: ProxyConfig, { field, level }: ProxyValidationOptions): ErrorResponse | null;
93
101
  /**
94
102
  * Validates the `proxy` config on its own.
95
103
  */
96
- export declare function validateProxy(options: Pick<Options, 'proxy'>, files: string[], _framework: string | null | undefined): ErrorResponse | null;
104
+ export declare function validateProxy(options: Pick<Options, 'proxy'>, files: string[], _framework: string | null | undefined, validationOptions: ProxyValidationOptions): ErrorResponse | null;
97
105
  /**
98
106
  * Validates a resolved proxy against the rest of the project.
99
107
  */
100
- export declare function validateResolvedProxy(proxy: ResolvedProxy, framework: string | null | undefined): ErrorResponse | null;
108
+ export declare function validateResolvedProxy(proxy: Pick<ResolvedProxy, 'source' | 'entrypoint' | 'file'>, framework: string | null | undefined, { field }: ProxyValidationOptions): ErrorResponse | null;
@@ -328,6 +328,7 @@ __export(detect_builders_exports, {
328
328
  detectBuilders: () => detectBuilders,
329
329
  detectOutputDirectory: () => detectOutputDirectory,
330
330
  getProxyBuilder: () => getProxyBuilder,
331
+ parseProxyEntrypoint: () => parseProxyEntrypoint,
331
332
  resolveProxy: () => resolveProxy,
332
333
  sortFiles: () => sortFiles,
333
334
  validateProxy: () => validateProxy,
@@ -458,9 +459,18 @@ function detectOutputDirectory(builders) {
458
459
  async function detectBuilders(files, pkg, options = {}) {
459
460
  const { projectSettings = {} } = options;
460
461
  const { framework } = projectSettings;
461
- const proxyConfigError = validateProxy(options, files, framework);
462
+ const proxyValidationOptions = {
463
+ field: "proxy",
464
+ level: "project"
465
+ };
466
+ const proxyConfigError = validateProxy(
467
+ options,
468
+ files,
469
+ framework,
470
+ proxyValidationOptions
471
+ );
462
472
  const proxy = proxyConfigError ? null : resolveProxy(files, options);
463
- const proxyError = proxyConfigError ?? (proxy && validateResolvedProxy(proxy, framework));
473
+ const proxyError = proxyConfigError ?? (proxy && validateResolvedProxy(proxy, framework, proxyValidationOptions));
464
474
  if (proxyError) {
465
475
  return {
466
476
  builders: null,
@@ -868,11 +878,11 @@ function getApiMatches() {
868
878
  { src: "api/**/*.rs", use: `@vercel/rust`, config }
869
879
  ];
870
880
  }
871
- function validateProxyConfig(proxy) {
881
+ function validateProxyConfig(proxy, { field, level }) {
872
882
  if (typeof proxy !== "object" || typeof proxy.entrypoint !== "string" || !proxy.entrypoint) {
873
883
  return {
874
884
  code: "invalid_proxy",
875
- message: "The `proxy` property must contain an `entrypoint` string."
885
+ message: `The \`${field}\` property must contain an \`entrypoint\` string.`
876
886
  };
877
887
  }
878
888
  const { file, variable } = parseProxyEntrypoint(proxy.entrypoint);
@@ -882,19 +892,19 @@ function validateProxyConfig(proxy) {
882
892
  if (file.startsWith("/") || file.includes("\\") || segments.includes(".") || segments.includes("..") || /[?#\u0000-\u001f]/.test(file)) {
883
893
  return {
884
894
  code: "invalid_proxy_entrypoint",
885
- message: "The `proxy.entrypoint` path must be relative to the project root and cannot contain traversal, query, fragment, or control characters."
895
+ message: `The \`${field}.entrypoint\` path must be relative to the ${level} root and cannot contain traversal, query, fragment, or control characters.`
886
896
  };
887
897
  }
888
898
  if (!isPythonForm && !isJsForm) {
889
899
  return {
890
900
  code: "invalid_proxy_entrypoint",
891
- message: "The `proxy.entrypoint` must be a `.js` or `.ts` file path, or a Python `module:attr`."
901
+ message: `The \`${field}.entrypoint\` must be a \`.js\` or \`.ts\` file path, or a Python \`module:attr\`.`
892
902
  };
893
903
  }
894
904
  if (isPythonForm && !variable) {
895
905
  return {
896
906
  code: "invalid_proxy_entrypoint",
897
- message: "Python proxy entrypoints must use `module:attr` syntax, not a plain `.py` path."
907
+ message: `The \`${field}.entrypoint\` must use Python \`module:attr\` syntax, not a plain \`.py\` path.`
898
908
  };
899
909
  }
900
910
  if (proxy.matcher !== void 0) {
@@ -904,18 +914,19 @@ function validateProxyConfig(proxy) {
904
914
  )) {
905
915
  return {
906
916
  code: "invalid_proxy_matcher",
907
- message: "The `proxy.matcher` value must be a path matcher starting with `/`, or an array of path matchers starting with `/`."
917
+ message: `The \`${field}.matcher\` value must be a path matcher starting with \`/\`, or an array of path matchers starting with \`/\`.`
908
918
  };
909
919
  }
910
920
  }
911
921
  return null;
912
922
  }
913
- function validateProxy(options, files, _framework) {
923
+ function validateProxy(options, files, _framework, validationOptions) {
914
924
  const { proxy } = options;
915
925
  if (!proxy) {
916
926
  return null;
917
927
  }
918
- const configError = validateProxyConfig(proxy);
928
+ const { field } = validationOptions;
929
+ const configError = validateProxyConfig(proxy, validationOptions);
919
930
  if (configError) {
920
931
  return configError;
921
932
  }
@@ -923,24 +934,24 @@ function validateProxy(options, files, _framework) {
923
934
  if (!files.includes(file)) {
924
935
  return {
925
936
  code: "proxy_entrypoint_not_found",
926
- message: `The proxy entrypoint \`${proxy.entrypoint}\` does not exist.`
937
+ message: `The \`${field}.entrypoint\` \`${proxy.entrypoint}\` does not exist.`
927
938
  };
928
939
  }
929
940
  return null;
930
941
  }
931
- function validateResolvedProxy(proxy, framework) {
942
+ function validateResolvedProxy(proxy, framework, { field }) {
932
943
  const appRuntime = (0, import_framework.inferBuilderRuntimeFromFramework)(framework);
933
944
  const proxyRuntime = import_constants.ENTRYPOINT_EXTENSIONS[(0, import_path.extname)(proxy.file)];
934
945
  if (appRuntime !== void 0 && proxyRuntime !== void 0 && appRuntime !== proxyRuntime) {
935
946
  return {
936
947
  code: "proxy_runtime_mismatch",
937
- message: proxy.source === "vercel-config" ? `The proxy entrypoint \`${proxy.entrypoint}\` uses the ${proxyRuntime} runtime, but the app uses the ${appRuntime} runtime. The proxy and app must use the same runtime.` : `The middleware \`${proxy.file}\` uses the ${proxyRuntime} runtime, but the app uses the ${appRuntime} runtime. The middleware and app must use the same runtime.`
948
+ message: proxy.source === "vercel-config" ? `The \`${field}.entrypoint\` \`${proxy.entrypoint}\` uses the ${proxyRuntime} runtime, but the app uses the ${appRuntime} runtime. The proxy and app must use the same runtime.` : `The middleware \`${proxy.file}\` uses the ${proxyRuntime} runtime, but the app uses the ${appRuntime} runtime. The middleware and app must use the same runtime.`
938
949
  };
939
950
  }
940
951
  if (proxy.source === "vercel-config" && (framework === "nextjs" || framework === "astro")) {
941
952
  return {
942
953
  code: "proxy_framework_conflict",
943
- message: `The \`proxy\` property cannot be used with ${framework === "nextjs" ? "Next.js" : "Astro"} because the framework builds its own routing middleware.`
954
+ message: `The \`${field}\` property cannot be used with ${framework === "nextjs" ? "Next.js" : "Astro"} because the framework builds its own routing middleware.`
944
955
  };
945
956
  }
946
957
  return null;
@@ -1441,6 +1452,7 @@ function sortFilesBySegmentCount(fileA, fileB) {
1441
1452
  detectBuilders,
1442
1453
  detectOutputDirectory,
1443
1454
  getProxyBuilder,
1455
+ parseProxyEntrypoint,
1444
1456
  resolveProxy,
1445
1457
  sortFiles,
1446
1458
  validateProxy,
@@ -34,6 +34,7 @@ var import_buildpack_runtimes = require("../buildpack-runtimes");
34
34
  var import_resolve = require("./resolve");
35
35
  var import_utils = require("./utils");
36
36
  var import_detect_builders = require("../detect-builders");
37
+ var import_detect_builders2 = require("../detect-builders");
37
38
  const frameworksBySlug = new Map(import_frameworks.frameworkList.map((f) => [f.slug, f]));
38
39
  const MAX_SERVICE_NAME_LENGTH = 64;
39
40
  const SERVICE_NAME_REGEX = /^[a-z]([a-z_-]*[a-z])?$/;
@@ -107,6 +108,15 @@ async function resolveContainerServiceV2(name, config, normalizedRoot, serviceFs
107
108
  }
108
109
  }
109
110
  const builderSrc = isRoot ? dockerfile : import_path.posix.join(normalizedRoot, dockerfile);
111
+ const proxyError = await validateServiceProxy(
112
+ name,
113
+ config,
114
+ serviceFs,
115
+ void 0
116
+ );
117
+ if (proxyError) {
118
+ return { error: proxyError };
119
+ }
110
120
  const builderConfig = { zeroConfig: true };
111
121
  if (!isRoot) {
112
122
  builderConfig.workspace = normalizedRoot;
@@ -133,6 +143,7 @@ async function resolveContainerServiceV2(name, config, normalizedRoot, serviceFs
133
143
  },
134
144
  bindings: config.bindings,
135
145
  functions: config.functions,
146
+ proxy: config.proxy,
136
147
  headers: config.headers,
137
148
  redirects: config.redirects,
138
149
  rewrites: config.rewrites,
@@ -142,6 +153,28 @@ async function resolveContainerServiceV2(name, config, normalizedRoot, serviceFs
142
153
  }
143
154
  };
144
155
  }
156
+ async function validateServiceProxy(name, config, serviceFs, framework) {
157
+ const { proxy } = config;
158
+ if (!proxy) {
159
+ return null;
160
+ }
161
+ const validationOptions = {
162
+ field: `services.${name}.proxy`,
163
+ level: "service"
164
+ };
165
+ const { file } = (0, import_detect_builders2.parseProxyEntrypoint)(proxy.entrypoint);
166
+ const files = await (0, import_utils.hasFile)(serviceFs, file) ? [file] : [];
167
+ const error = (0, import_detect_builders2.validateProxy)({ proxy }, files, framework, validationOptions) ?? (0, import_detect_builders2.validateResolvedProxy)(
168
+ { source: "vercel-config", entrypoint: proxy.entrypoint, file },
169
+ framework,
170
+ validationOptions
171
+ );
172
+ return error ? {
173
+ code: error.code.toUpperCase(),
174
+ message: error.message,
175
+ serviceName: name
176
+ } : null;
177
+ }
145
178
  function validateServiceConfigV2(name, config) {
146
179
  if (!isValidServiceName(name)) {
147
180
  return {
@@ -239,6 +272,11 @@ function validateImageServiceConfigV2(name, config) {
239
272
  `Service "${name}" sets "image", so it can't set "functions". The functions configuration isn't supported for image services.`
240
273
  );
241
274
  }
275
+ if (config.proxy !== void 0) {
276
+ return invalid(
277
+ `Service "${name}" sets "image", so it can't set "proxy". A proxy isn't supported for image services.`
278
+ );
279
+ }
242
280
  return null;
243
281
  }
244
282
  function resolveImageServiceV2(name, config, normalizedRoot) {
@@ -445,6 +483,15 @@ async function resolveConfiguredServiceV2(name, config, fs) {
445
483
  builderConfig.handlerFunction = moduleAttr.attrName;
446
484
  }
447
485
  const runtime = import_constants.STATIC_BUILDERS.has(builderUse) ? void 0 : inferredRuntime;
486
+ const proxyError = await validateServiceProxy(
487
+ name,
488
+ config,
489
+ serviceFs,
490
+ framework
491
+ );
492
+ if (proxyError) {
493
+ return { error: proxyError };
494
+ }
448
495
  return {
449
496
  service: {
450
497
  schema: "experimentalServicesV2",
@@ -465,6 +512,7 @@ async function resolveConfiguredServiceV2(name, config, fs) {
465
512
  outputDirectory: config.outputDirectory,
466
513
  bindings: config.bindings,
467
514
  functions: config.functions,
515
+ proxy: config.proxy,
468
516
  headers: config.headers,
469
517
  redirects: config.redirects,
470
518
  rewrites: config.rewrites,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/fs-detectors",
3
- "version": "7.13.0",
3
+ "version": "7.14.0-snapshot-c9dfd83f2293",
4
4
  "description": "Vercel filesystem detectors",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -20,9 +20,9 @@
20
20
  "minimatch": "3.1.2",
21
21
  "semver": "6.3.1",
22
22
  "smol-toml": "1.5.2",
23
- "@vercel/build-utils": "15.6.0",
24
- "@vercel/frameworks": "3.35.1",
23
+ "@vercel/build-utils": "15.7.0-snapshot-c9dfd83f2293",
25
24
  "@vercel/error-utils": "2.2.1",
25
+ "@vercel/frameworks": "3.35.1",
26
26
  "@vercel/python-analysis": "0.15.4",
27
27
  "@vercel/routing-utils": "6.6.1"
28
28
  },
@@ -33,7 +33,7 @@
33
33
  "@types/node": "20.11.0",
34
34
  "@types/semver": "7.3.10",
35
35
  "vitest": "4.1.10",
36
- "@vercel-internals/runtimes": "1.2.13"
36
+ "@vercel-internals/runtimes": "1.2.14"
37
37
  },
38
38
  "scripts": {
39
39
  "build": "node build.mjs",