@vercel/build-utils 7.0.0 → 7.1.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/file-ref.js CHANGED
@@ -1,85 +1,105 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
4
11
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const assert_1 = __importDefault(require("assert"));
7
- const node_fetch_1 = __importDefault(require("node-fetch"));
8
- const multistream_1 = __importDefault(require("multistream"));
9
- const async_retry_1 = __importDefault(require("async-retry"));
10
- const async_sema_1 = __importDefault(require("async-sema"));
11
- const semaToDownloadFromS3 = new async_sema_1.default(5);
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var file_ref_exports = {};
30
+ __export(file_ref_exports, {
31
+ default: () => FileRef
32
+ });
33
+ module.exports = __toCommonJS(file_ref_exports);
34
+ var import_assert = __toESM(require("assert"));
35
+ var import_node_fetch = __toESM(require("node-fetch"));
36
+ var import_multistream = __toESM(require("multistream"));
37
+ var import_async_retry = __toESM(require("async-retry"));
38
+ var import_async_sema = __toESM(require("async-sema"));
39
+ const semaToDownloadFromS3 = new import_async_sema.default(5);
12
40
  class BailableError extends Error {
13
- constructor(...args) {
14
- super(...args);
15
- this.bail = false;
16
- }
41
+ constructor(...args) {
42
+ super(...args);
43
+ this.bail = false;
44
+ }
17
45
  }
18
46
  class FileRef {
19
- constructor({ mode = 0o100644, digest, contentType, mutable = false, }) {
20
- (0, assert_1.default)(typeof mode === 'number');
21
- (0, assert_1.default)(typeof digest === 'string');
22
- this.type = 'FileRef';
23
- this.mode = mode;
24
- this.digest = digest;
25
- this.contentType = contentType;
26
- this.mutable = mutable;
27
- }
28
- async toStreamAsync() {
29
- let url = '';
30
- // sha:24be087eef9fac01d61b30a725c1a10d7b45a256
31
- const [digestType, digestHash] = this.digest.split(':');
32
- if (digestType === 'sha') {
33
- // This CloudFront URL edge caches the `now-files` S3 bucket to prevent
34
- // overloading it. Mutable files cannot be cached.
35
- // `https://now-files.s3.amazonaws.com/${digestHash}`
36
- url = this.mutable
37
- ? `https://now-files.s3.amazonaws.com/${digestHash}`
38
- : `https://dmmcy0pwk6bqi.cloudfront.net/${digestHash}`;
39
- }
40
- else if (digestType === 'sha+ephemeral') {
41
- // This URL is currently only used for cache files that constantly
42
- // change. We shouldn't cache it on CloudFront because it'd always be a
43
- // MISS.
44
- url = `https://now-ephemeral-files.s3.amazonaws.com/${digestHash}`;
45
- }
46
- else {
47
- throw new Error('Expected digest to be sha');
48
- }
49
- await semaToDownloadFromS3.acquire();
50
- // console.time(`downloading ${url}`);
51
- try {
52
- return await (0, async_retry_1.default)(async () => {
53
- const resp = await (0, node_fetch_1.default)(url);
54
- if (!resp.ok) {
55
- const error = new BailableError(`download: ${resp.status} ${resp.statusText} for ${url}`);
56
- if (resp.status === 403)
57
- error.bail = true;
58
- throw error;
59
- }
60
- return resp.body;
61
- }, { factor: 1, retries: 3 });
62
- }
63
- finally {
64
- // console.timeEnd(`downloading ${url}`);
65
- semaToDownloadFromS3.release();
66
- }
47
+ constructor({
48
+ mode = 33188,
49
+ digest,
50
+ contentType,
51
+ mutable = false
52
+ }) {
53
+ (0, import_assert.default)(typeof mode === "number");
54
+ (0, import_assert.default)(typeof digest === "string");
55
+ this.type = "FileRef";
56
+ this.mode = mode;
57
+ this.digest = digest;
58
+ this.contentType = contentType;
59
+ this.mutable = mutable;
60
+ }
61
+ async toStreamAsync() {
62
+ let url = "";
63
+ const [digestType, digestHash] = this.digest.split(":");
64
+ if (digestType === "sha") {
65
+ url = this.mutable ? `https://now-files.s3.amazonaws.com/${digestHash}` : `https://dmmcy0pwk6bqi.cloudfront.net/${digestHash}`;
66
+ } else if (digestType === "sha+ephemeral") {
67
+ url = `https://now-ephemeral-files.s3.amazonaws.com/${digestHash}`;
68
+ } else {
69
+ throw new Error("Expected digest to be sha");
67
70
  }
68
- toStream() {
69
- let flag = false;
70
- // eslint-disable-next-line consistent-return
71
- return (0, multistream_1.default)(cb => {
72
- if (flag)
73
- return cb(null, null);
74
- flag = true;
75
- this.toStreamAsync()
76
- .then(stream => {
77
- cb(null, stream);
78
- })
79
- .catch(error => {
80
- cb(error, null);
81
- });
82
- });
71
+ await semaToDownloadFromS3.acquire();
72
+ try {
73
+ return await (0, import_async_retry.default)(
74
+ async () => {
75
+ const resp = await (0, import_node_fetch.default)(url);
76
+ if (!resp.ok) {
77
+ const error = new BailableError(
78
+ `download: ${resp.status} ${resp.statusText} for ${url}`
79
+ );
80
+ if (resp.status === 403)
81
+ error.bail = true;
82
+ throw error;
83
+ }
84
+ return resp.body;
85
+ },
86
+ { factor: 1, retries: 3 }
87
+ );
88
+ } finally {
89
+ semaToDownloadFromS3.release();
83
90
  }
91
+ }
92
+ toStream() {
93
+ let flag = false;
94
+ return (0, import_multistream.default)((cb) => {
95
+ if (flag)
96
+ return cb(null, null);
97
+ flag = true;
98
+ this.toStreamAsync().then((stream) => {
99
+ cb(null, stream);
100
+ }).catch((error) => {
101
+ cb(error, null);
102
+ });
103
+ });
104
+ }
84
105
  }
85
- exports.default = FileRef;
@@ -1,106 +1,136 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
4
11
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.downloadFile = exports.isSymbolicLink = exports.isDirectory = void 0;
7
- const path_1 = __importDefault(require("path"));
8
- const debug_1 = __importDefault(require("../debug"));
9
- const file_fs_ref_1 = __importDefault(require("../file-fs-ref"));
10
- const fs_extra_1 = require("fs-extra");
11
- const stream_to_buffer_1 = __importDefault(require("./stream-to-buffer"));
12
- const S_IFDIR = 16384; /* 0040000 directory */
13
- const S_IFLNK = 40960; /* 0120000 symbolic link */
14
- const S_IFMT = 61440; /* 0170000 type of file */
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var download_exports = {};
30
+ __export(download_exports, {
31
+ default: () => download,
32
+ downloadFile: () => downloadFile,
33
+ isDirectory: () => isDirectory,
34
+ isSymbolicLink: () => isSymbolicLink
35
+ });
36
+ module.exports = __toCommonJS(download_exports);
37
+ var import_path = __toESM(require("path"));
38
+ var import_debug = __toESM(require("../debug"));
39
+ var import_file_fs_ref = __toESM(require("../file-fs-ref"));
40
+ var import_fs_extra = require("fs-extra");
41
+ var import_stream_to_buffer = __toESM(require("./stream-to-buffer"));
42
+ const S_IFDIR = 16384;
43
+ const S_IFLNK = 40960;
44
+ const S_IFMT = 61440;
15
45
  function isDirectory(mode) {
16
- return (mode & S_IFMT) === S_IFDIR;
46
+ return (mode & S_IFMT) === S_IFDIR;
17
47
  }
18
- exports.isDirectory = isDirectory;
19
48
  function isSymbolicLink(mode) {
20
- return (mode & S_IFMT) === S_IFLNK;
49
+ return (mode & S_IFMT) === S_IFLNK;
21
50
  }
22
- exports.isSymbolicLink = isSymbolicLink;
23
51
  async function prepareSymlinkTarget(file, fsPath) {
24
- const mkdirPromise = (0, fs_extra_1.mkdirp)(path_1.default.dirname(fsPath));
25
- if (file.type === 'FileFsRef') {
26
- const [target] = await Promise.all([(0, fs_extra_1.readlink)(file.fsPath), mkdirPromise]);
27
- return target;
28
- }
29
- if (file.type === 'FileRef' || file.type === 'FileBlob') {
30
- const targetPathBufferPromise = (0, stream_to_buffer_1.default)(await file.toStreamAsync());
31
- const [targetPathBuffer] = await Promise.all([
32
- targetPathBufferPromise,
33
- mkdirPromise,
34
- ]);
35
- return targetPathBuffer.toString('utf8');
36
- }
37
- throw new Error(`file.type "${file.type}" not supported for symlink`);
52
+ const mkdirPromise = (0, import_fs_extra.mkdirp)(import_path.default.dirname(fsPath));
53
+ if (file.type === "FileFsRef") {
54
+ const [target] = await Promise.all([(0, import_fs_extra.readlink)(file.fsPath), mkdirPromise]);
55
+ return target;
56
+ }
57
+ if (file.type === "FileRef" || file.type === "FileBlob") {
58
+ const targetPathBufferPromise = (0, import_stream_to_buffer.default)(await file.toStreamAsync());
59
+ const [targetPathBuffer] = await Promise.all([
60
+ targetPathBufferPromise,
61
+ mkdirPromise
62
+ ]);
63
+ return targetPathBuffer.toString("utf8");
64
+ }
65
+ throw new Error(
66
+ `file.type "${file.type}" not supported for symlink`
67
+ );
38
68
  }
39
69
  async function downloadFile(file, fsPath) {
40
- const { mode } = file;
41
- if (isDirectory(mode)) {
42
- await (0, fs_extra_1.mkdirp)(fsPath);
43
- await (0, fs_extra_1.chmod)(fsPath, mode);
44
- return file_fs_ref_1.default.fromFsPath({ mode, fsPath });
45
- }
46
- // If the source is a symlink, try to create it instead of copying the file.
47
- // Note: creating symlinks on Windows requires admin priviliges or symlinks
48
- // enabled in the group policy. We may want to improve the error message.
49
- if (isSymbolicLink(mode)) {
50
- const target = await prepareSymlinkTarget(file, fsPath);
51
- await (0, fs_extra_1.symlink)(target, fsPath);
52
- return file_fs_ref_1.default.fromFsPath({ mode, fsPath });
53
- }
54
- const stream = file.toStream();
55
- return file_fs_ref_1.default.fromStream({ mode, stream, fsPath });
70
+ const { mode } = file;
71
+ if (isDirectory(mode)) {
72
+ await (0, import_fs_extra.mkdirp)(fsPath);
73
+ await (0, import_fs_extra.chmod)(fsPath, mode);
74
+ return import_file_fs_ref.default.fromFsPath({ mode, fsPath });
75
+ }
76
+ if (isSymbolicLink(mode)) {
77
+ const target = await prepareSymlinkTarget(file, fsPath);
78
+ await (0, import_fs_extra.symlink)(target, fsPath);
79
+ return import_file_fs_ref.default.fromFsPath({ mode, fsPath });
80
+ }
81
+ const stream = file.toStream();
82
+ return import_file_fs_ref.default.fromStream({ mode, stream, fsPath });
56
83
  }
57
- exports.downloadFile = downloadFile;
58
84
  async function removeFile(basePath, fileMatched) {
59
- const file = path_1.default.join(basePath, fileMatched);
60
- await (0, fs_extra_1.remove)(file);
85
+ const file = import_path.default.join(basePath, fileMatched);
86
+ await (0, import_fs_extra.remove)(file);
61
87
  }
62
88
  async function download(files, basePath, meta) {
63
- const { isDev = false, skipDownload = false, filesChanged = null, filesRemoved = null, } = meta || {};
64
- if (isDev || skipDownload) {
65
- // In `vercel dev`, the `download()` function is a no-op because
66
- // the `basePath` matches the `cwd` of the dev server, so the
67
- // source files are already available.
68
- return files;
69
- }
70
- (0, debug_1.default)('Downloading deployment source files...');
71
- const start = Date.now();
72
- const files2 = {};
73
- const filenames = Object.keys(files);
74
- await Promise.all(filenames.map(async (name) => {
75
- // If the file does not exist anymore, remove it.
76
- if (Array.isArray(filesRemoved) && filesRemoved.includes(name)) {
77
- await removeFile(basePath, name);
78
- return;
79
- }
80
- // If a file didn't change, do not re-download it.
81
- if (Array.isArray(filesChanged) && !filesChanged.includes(name)) {
82
- return;
83
- }
84
- // Some builders resolve symlinks and return both
85
- // a file, node_modules/<symlink>/package.json, and
86
- // node_modules/<symlink>, a symlink.
87
- // Removing the file matches how the yazl lambda zip
88
- // behaves so we can use download() with `vercel build`.
89
- const parts = name.split('/');
90
- for (let i = 1; i < parts.length; i++) {
91
- const dir = parts.slice(0, i).join('/');
92
- const parent = files[dir];
93
- if (parent && isSymbolicLink(parent.mode)) {
94
- console.warn(`Warning: file "${name}" is within a symlinked directory "${dir}" and will be ignored`);
95
- return;
96
- }
89
+ const {
90
+ isDev = false,
91
+ skipDownload = false,
92
+ filesChanged = null,
93
+ filesRemoved = null
94
+ } = meta || {};
95
+ if (isDev || skipDownload) {
96
+ return files;
97
+ }
98
+ (0, import_debug.default)("Downloading deployment source files...");
99
+ const start = Date.now();
100
+ const files2 = {};
101
+ const filenames = Object.keys(files);
102
+ await Promise.all(
103
+ filenames.map(async (name) => {
104
+ if (Array.isArray(filesRemoved) && filesRemoved.includes(name)) {
105
+ await removeFile(basePath, name);
106
+ return;
107
+ }
108
+ if (Array.isArray(filesChanged) && !filesChanged.includes(name)) {
109
+ return;
110
+ }
111
+ const parts = name.split("/");
112
+ for (let i = 1; i < parts.length; i++) {
113
+ const dir = parts.slice(0, i).join("/");
114
+ const parent = files[dir];
115
+ if (parent && isSymbolicLink(parent.mode)) {
116
+ console.warn(
117
+ `Warning: file "${name}" is within a symlinked directory "${dir}" and will be ignored`
118
+ );
119
+ return;
97
120
  }
98
- const file = files[name];
99
- const fsPath = path_1.default.join(basePath, name);
100
- files2[name] = await downloadFile(file, fsPath);
101
- }));
102
- const duration = Date.now() - start;
103
- (0, debug_1.default)(`Downloaded ${filenames.length} source files: ${duration}ms`);
104
- return files2;
121
+ }
122
+ const file = files[name];
123
+ const fsPath = import_path.default.join(basePath, name);
124
+ files2[name] = await downloadFile(file, fsPath);
125
+ })
126
+ );
127
+ const duration = Date.now() - start;
128
+ (0, import_debug.default)(`Downloaded ${filenames.length} source files: ${duration}ms`);
129
+ return files2;
105
130
  }
106
- exports.default = download;
131
+ // Annotate the CommonJS export names for ESM import in node:
132
+ 0 && (module.exports = {
133
+ downloadFile,
134
+ isDirectory,
135
+ isSymbolicLink
136
+ });
@@ -1,12 +1,32 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const path_1 = require("path");
4
- const os_1 = require("os");
5
- const fs_extra_1 = require("fs-extra");
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 get_writable_directory_exports = {};
20
+ __export(get_writable_directory_exports, {
21
+ default: () => getWritableDirectory
22
+ });
23
+ module.exports = __toCommonJS(get_writable_directory_exports);
24
+ var import_path = require("path");
25
+ var import_os = require("os");
26
+ var import_fs_extra = require("fs-extra");
6
27
  async function getWritableDirectory() {
7
- const name = Math.floor(Math.random() * 0x7fffffff).toString(16);
8
- const directory = (0, path_1.join)((0, os_1.tmpdir)(), name);
9
- await (0, fs_extra_1.mkdirp)(directory);
10
- return directory;
28
+ const name = Math.floor(Math.random() * 2147483647).toString(16);
29
+ const directory = (0, import_path.join)((0, import_os.tmpdir)(), name);
30
+ await (0, import_fs_extra.mkdirp)(directory);
31
+ return directory;
11
32
  }
12
- exports.default = getWritableDirectory;
package/dist/fs/glob.js CHANGED
@@ -1,84 +1,111 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
4
11
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const path_1 = __importDefault(require("path"));
7
- const assert_1 = __importDefault(require("assert"));
8
- const glob_1 = __importDefault(require("glob"));
9
- const util_1 = require("util");
10
- const fs_extra_1 = require("fs-extra");
11
- const normalize_path_1 = require("./normalize-path");
12
- const file_fs_ref_1 = __importDefault(require("../file-fs-ref"));
13
- const vanillaGlob = (0, util_1.promisify)(glob_1.default);
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var glob_exports = {};
30
+ __export(glob_exports, {
31
+ default: () => glob
32
+ });
33
+ module.exports = __toCommonJS(glob_exports);
34
+ var import_path = __toESM(require("path"));
35
+ var import_assert = __toESM(require("assert"));
36
+ var import_glob = __toESM(require("glob"));
37
+ var import_util = require("util");
38
+ var import_fs_extra = require("fs-extra");
39
+ var import_normalize_path = require("./normalize-path");
40
+ var import_file_fs_ref = __toESM(require("../file-fs-ref"));
41
+ const vanillaGlob = (0, import_util.promisify)(import_glob.default);
14
42
  async function glob(pattern, opts, mountpoint) {
15
- const options = typeof opts === 'string' ? { cwd: opts } : opts;
16
- if (!options.cwd) {
17
- throw new Error('Second argument (basePath) must be specified for names of resulting files');
18
- }
19
- if (!path_1.default.isAbsolute(options.cwd)) {
20
- throw new Error(`basePath/cwd must be an absolute path (${options.cwd})`);
43
+ const options = typeof opts === "string" ? { cwd: opts } : opts;
44
+ if (!options.cwd) {
45
+ throw new Error(
46
+ "Second argument (basePath) must be specified for names of resulting files"
47
+ );
48
+ }
49
+ if (!import_path.default.isAbsolute(options.cwd)) {
50
+ throw new Error(`basePath/cwd must be an absolute path (${options.cwd})`);
51
+ }
52
+ const results = {};
53
+ const statCache = {};
54
+ const symlinks = {};
55
+ const files = await vanillaGlob(pattern, {
56
+ ...options,
57
+ symlinks,
58
+ statCache,
59
+ stat: true,
60
+ dot: true
61
+ });
62
+ const dirs = /* @__PURE__ */ new Set();
63
+ const dirsWithEntries = /* @__PURE__ */ new Set();
64
+ for (const relativePath of files) {
65
+ const absPath = import_path.default.join(options.cwd, relativePath);
66
+ const fsPath = (0, import_normalize_path.normalizePath)(absPath);
67
+ let stat = statCache[fsPath];
68
+ (0, import_assert.default)(
69
+ stat,
70
+ `statCache does not contain value for ${relativePath} (resolved to ${fsPath})`
71
+ );
72
+ const isSymlink = symlinks[fsPath];
73
+ if (options.follow && (isSymlink || (await (0, import_fs_extra.lstat)(fsPath)).isSymbolicLink())) {
74
+ const target = await (0, import_fs_extra.readlink)(absPath);
75
+ const absTarget = import_path.default.resolve(import_path.default.dirname(absPath), target);
76
+ if (import_path.default.relative(options.cwd, absTarget).startsWith(`..${import_path.default.sep}`)) {
77
+ continue;
78
+ }
21
79
  }
22
- const results = {};
23
- const statCache = {};
24
- const symlinks = {};
25
- const files = await vanillaGlob(pattern, {
26
- ...options,
27
- symlinks,
28
- statCache,
29
- stat: true,
30
- dot: true,
31
- });
32
- const dirs = new Set();
33
- const dirsWithEntries = new Set();
34
- for (const relativePath of files) {
35
- const absPath = path_1.default.join(options.cwd, relativePath);
36
- const fsPath = (0, normalize_path_1.normalizePath)(absPath);
37
- let stat = statCache[fsPath];
38
- (0, assert_1.default)(stat, `statCache does not contain value for ${relativePath} (resolved to ${fsPath})`);
39
- const isSymlink = symlinks[fsPath];
40
- // When `follow` mode is enabled, ensure that the entry is not a symlink
41
- // that points to outside of `cwd`
42
- if (options.follow &&
43
- (isSymlink || (await (0, fs_extra_1.lstat)(fsPath)).isSymbolicLink())) {
44
- const target = await (0, fs_extra_1.readlink)(absPath);
45
- const absTarget = path_1.default.resolve(path_1.default.dirname(absPath), target);
46
- if (path_1.default.relative(options.cwd, absTarget).startsWith(`..${path_1.default.sep}`)) {
47
- continue;
48
- }
49
- }
50
- if (isSymlink || stat.isFile() || stat.isDirectory()) {
51
- if (isSymlink) {
52
- stat = await (0, fs_extra_1.lstat)(absPath);
53
- }
54
- // Some bookkeeping to track which directories already have entries within
55
- const dirname = path_1.default.dirname(relativePath);
56
- dirsWithEntries.add(dirname);
57
- if (stat.isDirectory()) {
58
- dirs.add(relativePath);
59
- continue;
60
- }
61
- let finalPath = relativePath;
62
- if (mountpoint) {
63
- finalPath = path_1.default.join(mountpoint, finalPath);
64
- }
65
- results[finalPath] = new file_fs_ref_1.default({ mode: stat.mode, fsPath });
66
- }
80
+ if (isSymlink || stat.isFile() || stat.isDirectory()) {
81
+ if (isSymlink) {
82
+ stat = await (0, import_fs_extra.lstat)(absPath);
83
+ }
84
+ const dirname = import_path.default.dirname(relativePath);
85
+ dirsWithEntries.add(dirname);
86
+ if (stat.isDirectory()) {
87
+ dirs.add(relativePath);
88
+ continue;
89
+ }
90
+ let finalPath = relativePath;
91
+ if (mountpoint) {
92
+ finalPath = import_path.default.join(mountpoint, finalPath);
93
+ }
94
+ results[finalPath] = new import_file_fs_ref.default({ mode: stat.mode, fsPath });
67
95
  }
68
- // Add empty directory entries
69
- if (options.includeDirectories) {
70
- for (const relativePath of dirs) {
71
- if (dirsWithEntries.has(relativePath))
72
- continue;
73
- let finalPath = relativePath;
74
- if (mountpoint) {
75
- finalPath = path_1.default.join(mountpoint, finalPath);
76
- }
77
- const fsPath = (0, normalize_path_1.normalizePath)(path_1.default.join(options.cwd, relativePath));
78
- const stat = statCache[fsPath];
79
- results[finalPath] = new file_fs_ref_1.default({ mode: stat.mode, fsPath });
80
- }
96
+ }
97
+ if (options.includeDirectories) {
98
+ for (const relativePath of dirs) {
99
+ if (dirsWithEntries.has(relativePath))
100
+ continue;
101
+ let finalPath = relativePath;
102
+ if (mountpoint) {
103
+ finalPath = import_path.default.join(mountpoint, finalPath);
104
+ }
105
+ const fsPath = (0, import_normalize_path.normalizePath)(import_path.default.join(options.cwd, relativePath));
106
+ const stat = statCache[fsPath];
107
+ results[finalPath] = new import_file_fs_ref.default({ mode: stat.mode, fsPath });
81
108
  }
82
- return results;
109
+ }
110
+ return results;
83
111
  }
84
- exports.default = glob;