@vercel/build-utils 15.2.1 → 15.3.0-snapshot-801db9d533c3

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.
@@ -23,6 +23,10 @@ export interface PrerenderInitialMetadata {
23
23
  htmlSize?: number;
24
24
  }
25
25
  export type PrerenderOnMiss = 'sync' | 'dynamic';
26
+ export type PrerenderExperimentalBypassFor = Array<HasField[number] | {
27
+ type: 'method';
28
+ value: NonNullable<HasField[number]['value']>;
29
+ }>;
26
30
  interface PrerenderOptions {
27
31
  expiration: number | false;
28
32
  staleExpiration?: number;
@@ -36,7 +40,7 @@ interface PrerenderOptions {
36
40
  initialStatus?: number;
37
41
  passQuery?: boolean;
38
42
  sourcePath?: string;
39
- experimentalBypassFor?: HasField;
43
+ experimentalBypassFor?: PrerenderExperimentalBypassFor;
40
44
  experimentalStreamingLambdaPath?: string;
41
45
  chain?: Chain;
42
46
  exposeErrBody?: boolean;
@@ -67,7 +71,7 @@ export declare class Prerender {
67
71
  initialStatus?: number;
68
72
  passQuery?: boolean;
69
73
  sourcePath?: string;
70
- experimentalBypassFor?: HasField;
74
+ experimentalBypassFor?: PrerenderExperimentalBypassFor;
71
75
  experimentalStreamingLambdaPath?: string;
72
76
  chain?: Chain;
73
77
  exposeErrBody?: boolean;
package/dist/prerender.js CHANGED
@@ -82,12 +82,19 @@ class Prerender {
82
82
  }
83
83
  if (experimentalBypassFor !== void 0) {
84
84
  if (!Array.isArray(experimentalBypassFor) || experimentalBypassFor.some(
85
- (field) => typeof field !== "object" || typeof field.type !== "string" || field.type === "host" && "key" in field || field.type !== "host" && typeof field.key !== "string" || field.value !== void 0 && typeof field.value !== "string" && (typeof field.value !== "object" || field.value === null || Array.isArray(field.value))
85
+ (field) => typeof field !== "object" || typeof field.type !== "string" || (field.type === "host" || field.type === "method") && "key" in field || field.type !== "host" && field.type !== "method" && typeof field.key !== "string" || field.value !== void 0 && typeof field.value !== "string" && (typeof field.value !== "object" || field.value === null || Array.isArray(field.value))
86
86
  )) {
87
87
  throw new Error(
88
88
  "The `experimentalBypassFor` argument for `Prerender` must be Array of objects with fields `type`, `key` and optionally `value`."
89
89
  );
90
90
  }
91
+ if (experimentalBypassFor.some(
92
+ (field) => field.type === "method" && field.value === void 0
93
+ )) {
94
+ throw new Error(
95
+ "The `value` field is required for `experimentalBypassFor` rules with type `method`."
96
+ );
97
+ }
91
98
  this.experimentalBypassFor = experimentalBypassFor;
92
99
  }
93
100
  if (typeof fallback === "undefined") {
@@ -0,0 +1,33 @@
1
+ import type { Meta } from './types';
2
+ export interface StaticPublishFilter {
3
+ /** Resolves to `false` for a source file that must not be published. */
4
+ canPublish: (sourcePath: string) => Promise<boolean>;
5
+ /**
6
+ * Glob `ignore` patterns, relative to the publish directory, for protected
7
+ * roots inside it, so a glob doesn't walk them. They only match paths
8
+ * lexically, so keep checking `canPublish` for symlinks. Calling this counts
9
+ * those roots as skipped in `report`.
10
+ */
11
+ getIgnorePatterns: () => string[];
12
+ /**
13
+ * Prints a summary of the files skipped under other services' roots. Call
14
+ * once, after collecting the static output.
15
+ */
16
+ report: (result: {
17
+ published: number;
18
+ }) => void;
19
+ }
20
+ /**
21
+ * Prevents automatic static publishing from including the sources of daemons
22
+ * and of the services in `meta.servicesContext` other than `serviceName`.
23
+ *
24
+ * - A protected root inside the publish directory is skipped entirely. A
25
+ * protected root equal to the publish directory fails the build.
26
+ * - A publish directory strictly inside a protected root is a shared context:
27
+ * its own files may be published, except the root's entrypoint.
28
+ */
29
+ export declare function createStaticPublishFilter({ meta, publishPath, serviceName, }: {
30
+ meta: Meta | undefined;
31
+ publishPath: string;
32
+ serviceName?: string;
33
+ }): Promise<StaticPublishFilter>;
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var static_publish_filter_exports = {};
20
+ __export(static_publish_filter_exports, {
21
+ createStaticPublishFilter: () => createStaticPublishFilter
22
+ });
23
+ module.exports = __toCommonJS(static_publish_filter_exports);
24
+ var import_promises = require("fs/promises");
25
+ var import_path = require("path");
26
+ var import_errors = require("./errors");
27
+ function contains(directory, file) {
28
+ const path = (0, import_path.relative)(directory, file);
29
+ return path === "" || !(0, import_path.isAbsolute)(path) && path !== ".." && !path.startsWith(`..${import_path.sep}`);
30
+ }
31
+ async function realpathOrSelf(path) {
32
+ return (0, import_promises.realpath)(path).catch((error) => {
33
+ if (error.code === "ENOENT")
34
+ return path;
35
+ throw error;
36
+ });
37
+ }
38
+ const DAEMON_WARNING = "Warning: Skipping static files under daemon roots. To publish them intentionally, copy them into a separate build output directory.";
39
+ function displayPath(projectRoot, path) {
40
+ return (0, import_path.relative)(projectRoot, path).split(import_path.sep).join("/") || ".";
41
+ }
42
+ async function createFilter({
43
+ projectRoot,
44
+ roots,
45
+ publishPath,
46
+ serviceName
47
+ }) {
48
+ const publish = (0, import_path.resolve)(publishPath);
49
+ const realPublish = await realpathOrSelf(publish);
50
+ const publisher = serviceName ? `Service "${serviceName}"` : "The app";
51
+ for (const root of roots) {
52
+ if (publish !== root.root && realPublish !== root.realRoot)
53
+ continue;
54
+ if (root.kind === "daemon") {
55
+ throw new import_errors.NowBuildError({
56
+ code: "INVALID_DAEMON_ROOT",
57
+ message: `Can't build daemon "${root.name}": ${publisher} publishes "${(0, import_path.relative)(projectRoot, publish) || "."}" as static files, but that is the daemon's root directory. Choose a separate output directory.`
58
+ });
59
+ }
60
+ throw new import_errors.NowBuildError({
61
+ code: "INVALID_SERVICE_ROOT",
62
+ message: `${publisher} publishes "${displayPath(projectRoot, publish)}" as static files, but that is the root directory of service "${root.name}". Set outputDirectory to a directory that contains only static files.`
63
+ });
64
+ }
65
+ const isExcludedBy = (root, source, realSource) => source === root.entrypoint || realSource === root.realEntrypoint || // Directory symlinks can expose protected sources the glob doesn't list.
66
+ contains(source, root.root) || contains(realSource, root.realRoot) || contains(root.root, source) && (!contains(root.root, publish) || !contains(publish, source)) || contains(root.realRoot, realSource) && (!contains(root.realRoot, realPublish) || !contains(realPublish, realSource));
67
+ let warnedDaemons = false;
68
+ const excludedServices = /* @__PURE__ */ new Set();
69
+ const markExcluded = (root) => {
70
+ if (root.kind === "service") {
71
+ excludedServices.add(root);
72
+ } else if (!warnedDaemons) {
73
+ console.warn(DAEMON_WARNING);
74
+ warnedDaemons = true;
75
+ }
76
+ };
77
+ const canPublish = async (sourcePath) => {
78
+ const source = (0, import_path.resolve)(sourcePath);
79
+ const realSource = await (0, import_promises.realpath)(source);
80
+ let excluded = false;
81
+ for (const root of roots) {
82
+ if (!isExcludedBy(root, source, realSource))
83
+ continue;
84
+ excluded = true;
85
+ markExcluded(root);
86
+ }
87
+ return !excluded;
88
+ };
89
+ const ignoredRoots = (await Promise.all(
90
+ roots.map(async (root) => {
91
+ const patterns = [
92
+ ignorePattern(publish, root.root),
93
+ ignorePattern(realPublish, root.realRoot)
94
+ ].filter((pattern) => pattern !== void 0);
95
+ if (patterns.length === 0)
96
+ return void 0;
97
+ const exists = await (0, import_promises.lstat)(root.root).then(
98
+ () => true,
99
+ () => false
100
+ );
101
+ return exists ? { root, patterns } : void 0;
102
+ })
103
+ )).filter(
104
+ (entry) => entry !== void 0
105
+ );
106
+ const getIgnorePatterns = () => {
107
+ for (const { root } of ignoredRoots)
108
+ markExcluded(root);
109
+ return [...new Set(ignoredRoots.flatMap(({ patterns }) => patterns))];
110
+ };
111
+ const report = ({ published }) => {
112
+ if (excludedServices.size === 0)
113
+ return;
114
+ const services = roots.filter((root) => excludedServices.has(root)).map((root) => `"${root.name}" (${displayPath(projectRoot, root.root)})`).join(", ");
115
+ const publishDir = displayPath(projectRoot, publish);
116
+ if (published === 0) {
117
+ console.warn(
118
+ `Warning: ${publisher} publishes no static files: every file in "${publishDir}" is under the root of another service: ${services}. Set outputDirectory to a directory that contains only static files.`
119
+ );
120
+ } else {
121
+ console.warn(
122
+ `Warning: ${publisher} skipped static files in "${publishDir}" that are under the roots of other services: ${services}. To publish them, copy them into a separate output directory.`
123
+ );
124
+ }
125
+ };
126
+ return { canPublish, getIgnorePatterns, report };
127
+ }
128
+ const GLOB_MAGIC = /[*?[\]{}()!+@\\]/;
129
+ function ignorePattern(publish, root) {
130
+ if (!contains(publish, root))
131
+ return void 0;
132
+ const path = (0, import_path.relative)(publish, root).split(import_path.sep).join("/");
133
+ if (GLOB_MAGIC.test(path))
134
+ return void 0;
135
+ return path === "" ? "**" : `${path}/**`;
136
+ }
137
+ async function getDaemonRoots(context) {
138
+ if (!context)
139
+ return [];
140
+ return Promise.all(
141
+ Object.entries(context.daemons).map(async ([name, daemon]) => {
142
+ const root = (0, import_path.resolve)(context.projectRoot, daemon.root);
143
+ const entrypoint = (0, import_path.resolve)(root, daemon.entrypoint);
144
+ const [realRoot, realEntrypoint] = await Promise.all([
145
+ (0, import_promises.realpath)(root),
146
+ (0, import_promises.realpath)(entrypoint)
147
+ ]);
148
+ return {
149
+ kind: "daemon",
150
+ name,
151
+ root,
152
+ realRoot,
153
+ entrypoint,
154
+ realEntrypoint
155
+ };
156
+ })
157
+ );
158
+ }
159
+ async function getServiceRoots(context, serviceName) {
160
+ if (!context)
161
+ return [];
162
+ return Promise.all(
163
+ context.services.filter((service) => service.name !== serviceName).map(async (service) => {
164
+ const root = (0, import_path.resolve)(context.projectRoot, service.root);
165
+ const entrypoint = service.entrypoint === void 0 ? void 0 : (0, import_path.resolve)(root, service.entrypoint);
166
+ const [realRoot, realEntrypoint] = await Promise.all([
167
+ realpathOrSelf(root),
168
+ entrypoint === void 0 ? void 0 : realpathOrSelf(entrypoint)
169
+ ]);
170
+ return {
171
+ kind: "service",
172
+ name: service.name,
173
+ root,
174
+ realRoot,
175
+ entrypoint,
176
+ realEntrypoint
177
+ };
178
+ })
179
+ );
180
+ }
181
+ async function createStaticPublishFilter({
182
+ meta,
183
+ publishPath,
184
+ serviceName
185
+ }) {
186
+ const [daemonRoots, serviceRoots] = await Promise.all([
187
+ getDaemonRoots(meta?.daemonContext),
188
+ getServiceRoots(meta?.servicesContext, serviceName)
189
+ ]);
190
+ const roots = [...daemonRoots, ...serviceRoots];
191
+ if (roots.length === 0) {
192
+ return {
193
+ canPublish: async () => true,
194
+ getIgnorePatterns: () => [],
195
+ report: () => {
196
+ }
197
+ };
198
+ }
199
+ return createFilter({
200
+ projectRoot: meta?.daemonContext?.projectRoot ?? meta?.servicesContext?.projectRoot ?? (0, import_path.resolve)(publishPath),
201
+ roots,
202
+ publishPath,
203
+ serviceName
204
+ });
205
+ }
206
+ // Annotate the CommonJS export names for ESM import in node:
207
+ 0 && (module.exports = {
208
+ createStaticPublishFilter
209
+ });
package/dist/types.d.ts CHANGED
@@ -82,6 +82,8 @@ export interface Config {
82
82
  /** Set when this build produces the named daemon. */
83
83
  daemon?: DaemonConfig & {
84
84
  name: string;
85
+ /** Dockerfile stage from an `entrypoint` like `Dockerfile#worker`. */
86
+ buildTarget?: string;
85
87
  };
86
88
  [key: string]: unknown;
87
89
  }
@@ -91,6 +93,24 @@ export interface Meta {
91
93
  projectRoot: string;
92
94
  daemons: Record<string, DaemonConfig>;
93
95
  };
96
+ /**
97
+ * Services whose sources static publishing must not include: every service
98
+ * of a multi-service project and command schedule services. Static
99
+ * publishing skips files under the roots of services other than the one
100
+ * being built.
101
+ */
102
+ servicesContext?: {
103
+ projectRoot: string;
104
+ /**
105
+ * Roots are relative to `projectRoot`, entrypoints to their root. An
106
+ * entrypoint stays private even inside a publish directory in its root.
107
+ */
108
+ services: Array<{
109
+ name: string;
110
+ root: string;
111
+ entrypoint?: string;
112
+ }>;
113
+ };
94
114
  isDev?: boolean;
95
115
  devCacheDir?: string;
96
116
  skipDownload?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/build-utils",
3
- "version": "15.2.1",
3
+ "version": "15.3.0-snapshot-801db9d533c3",
4
4
  "license": "Apache-2.0",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.js",
@@ -1,10 +0,0 @@
1
- import type { Meta } from './types';
2
- /**
3
- * Prevents automatic static publishing from including daemon sources.
4
- * A separate public directory is allowed inside a shared build context.
5
- */
6
- export declare function createDaemonStaticFileFilter({ context, publishPath, serviceName, }: {
7
- context: Meta['daemonContext'];
8
- publishPath: string;
9
- serviceName?: string;
10
- }): Promise<(sourcePath: string) => Promise<boolean>>;
@@ -1,86 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var daemon_static_publishing_exports = {};
20
- __export(daemon_static_publishing_exports, {
21
- createDaemonStaticFileFilter: () => createDaemonStaticFileFilter
22
- });
23
- module.exports = __toCommonJS(daemon_static_publishing_exports);
24
- var import_promises = require("fs/promises");
25
- var import_path = require("path");
26
- var import_errors = require("./errors");
27
- function contains(directory, file) {
28
- const path = (0, import_path.relative)(directory, file);
29
- return path === "" || !(0, import_path.isAbsolute)(path) && path !== ".." && !path.startsWith(`..${import_path.sep}`);
30
- }
31
- async function createDaemonStaticFileFilter({
32
- context,
33
- publishPath,
34
- serviceName
35
- }) {
36
- if (!context || Object.keys(context.daemons).length === 0)
37
- return async () => true;
38
- const publish = (0, import_path.resolve)(publishPath);
39
- const realPublish = await (0, import_promises.realpath)(publish).catch(
40
- (error) => {
41
- if (error.code === "ENOENT")
42
- return publish;
43
- throw error;
44
- }
45
- );
46
- const daemons = await Promise.all(
47
- Object.entries(context.daemons).map(async ([name, daemon]) => {
48
- const root = (0, import_path.resolve)(context.projectRoot, daemon.root);
49
- const dockerfile = (0, import_path.resolve)(root, daemon.entrypoint);
50
- const [realRoot, realDockerfile] = await Promise.all([
51
- (0, import_promises.realpath)(root),
52
- (0, import_promises.realpath)(dockerfile)
53
- ]);
54
- return { name, root, dockerfile, realRoot, realDockerfile };
55
- })
56
- );
57
- for (const daemon of daemons) {
58
- if (publish === daemon.root || realPublish === daemon.realRoot) {
59
- const publisher = serviceName ? `Service "${serviceName}"` : "The app";
60
- throw new import_errors.NowBuildError({
61
- code: "INVALID_DAEMON_ROOT",
62
- message: `Can't build daemon "${daemon.name}": ${publisher} publishes "${(0, import_path.relative)(context.projectRoot, publish) || "."}" as static files, but that is the daemon's root directory. Choose a separate output directory.`
63
- });
64
- }
65
- }
66
- let warned = false;
67
- return async (sourcePath) => {
68
- const source = (0, import_path.resolve)(sourcePath);
69
- const realSource = await (0, import_promises.realpath)(source);
70
- const excluded = daemons.some(
71
- (daemon) => source === daemon.dockerfile || realSource === daemon.realDockerfile || // Directory symlinks can expose daemon sources the glob doesn't list.
72
- contains(source, daemon.root) || contains(realSource, daemon.realRoot) || contains(daemon.root, source) && (!contains(daemon.root, publish) || !contains(publish, source)) || contains(daemon.realRoot, realSource) && (!contains(daemon.realRoot, realPublish) || !contains(realPublish, realSource))
73
- );
74
- if (excluded && !warned) {
75
- console.warn(
76
- "Warning: Skipping static files under daemon roots. To publish them intentionally, copy them into a separate build output directory."
77
- );
78
- warned = true;
79
- }
80
- return !excluded;
81
- };
82
- }
83
- // Annotate the CommonJS export names for ESM import in node:
84
- 0 && (module.exports = {
85
- createDaemonStaticFileFilter
86
- });