@pnpm/fs.indexed-pkg-importer 1100.0.30 → 1100.0.32
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 +29 -0
- package/lib/copyInternalSymlink.d.ts +6 -0
- package/lib/copyInternalSymlink.js +61 -0
- package/lib/dirUtils.d.ts +5 -0
- package/lib/dirUtils.js +72 -0
- package/lib/fileMapUtils.d.ts +11 -0
- package/lib/fileMapUtils.js +72 -0
- package/lib/filesHaveEqualContents.d.ts +1 -0
- package/lib/filesHaveEqualContents.js +32 -0
- package/lib/importIndexedDir.d.ts +8 -0
- package/lib/importIndexedDir.js +65 -443
- package/lib/index.js +205 -91
- package/lib/removeQuarantine.js +2 -1
- package/lib/repairIndexedDir.d.ts +8 -0
- package/lib/repairIndexedDir.js +134 -0
- package/package.json +7 -6
package/lib/importIndexedDir.js
CHANGED
|
@@ -1,247 +1,111 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
3
|
+
import { isError } from '@pnpm/error';
|
|
4
|
+
import { withFileLockRetry } from '@pnpm/fs.graceful-fs';
|
|
5
|
+
import { globalWarn, logger } from '@pnpm/logger';
|
|
6
6
|
import { rimrafSync } from '@zkochan/rimraf';
|
|
7
|
-
import fsx from 'fs-extra';
|
|
8
7
|
import { makeEmptyDirSync } from 'make-empty-dir';
|
|
9
8
|
import { fastPathTemp as pathTemp } from 'path-temp';
|
|
10
9
|
import { renameOverwriteSync } from 'rename-overwrite';
|
|
11
|
-
import
|
|
10
|
+
import { copyInternalSymlink } from './copyInternalSymlink.js';
|
|
11
|
+
import { makeFileMapDirs } from './dirUtils.js';
|
|
12
|
+
import { getUniqueFileMap, moveOrMergeModulesDirs, sanitizeFilenames, } from './fileMapUtils.js';
|
|
13
|
+
import { allFilesMatch, allSymlinksMatch, repairIndexedDir, } from './repairIndexedDir.js';
|
|
12
14
|
const filenameConflictsLogger = logger('_filename-conflicts');
|
|
13
|
-
const FILE_COMPARE_BUFFER_SIZE = 64 * 1024;
|
|
14
15
|
export function importIndexedDir(importer, newDir, filenames, opts) {
|
|
15
16
|
const dirImport = { importer, newDir, filenames, opts };
|
|
16
|
-
// Content-addressed target (e.g. global virtual store): the path is shared
|
|
17
|
-
// across projects, so concurrent importers are expected and the directory
|
|
18
|
-
// must never be removed or swapped out from under them. It is populated in
|
|
19
|
-
// place, adopted when it already matches, and otherwise repaired entry by
|
|
20
|
-
// entry. Repairing in place leaves a nested node_modules/ where it is, so
|
|
21
|
-
// keepModulesDir has nothing to preserve here either.
|
|
22
|
-
//
|
|
23
|
-
// A local directory is copied into the target at install time, so its
|
|
24
|
-
// contents can change without the lockfile changing and the path pins
|
|
25
|
-
// nothing. Such a target is rebuilt below rather than adopted or repaired.
|
|
26
17
|
if (opts.safeToSkip && opts.resolvedFrom !== 'local-dir') {
|
|
27
18
|
importIntoSharedDir(dirImport);
|
|
28
19
|
return;
|
|
29
20
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
// handling (EEXIST dedup, ENOENT sanitized-filename retry, etc.) and
|
|
35
|
-
// atomically swaps in a complete directory.
|
|
36
|
-
// keepModulesDir needs the staging path to preserve the existing node_modules.
|
|
37
|
-
if (!opts.keepModulesDir && tryExclusiveImport(importer, newDir, filenames, symlinkDirs(opts, filenames, { writtenDir: newDir, finalDir: newDir }))) {
|
|
21
|
+
if (!opts.keepModulesDir && tryExclusiveImport(importer, newDir, filenames, {
|
|
22
|
+
links: symlinkDirs(opts, filenames, { writtenDir: newDir, finalDir: newDir }),
|
|
23
|
+
symlinks: opts.symlinks,
|
|
24
|
+
})) {
|
|
38
25
|
return;
|
|
39
26
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
27
|
+
importWithStaging(dirImport);
|
|
28
|
+
}
|
|
29
|
+
function importWithStaging(dirImport) {
|
|
30
|
+
const { importer, newDir, filenames, opts } = dirImport;
|
|
43
31
|
const stage = pathTemp(newDir);
|
|
44
32
|
try {
|
|
45
33
|
makeEmptyDirSync(stage, { recursive: true });
|
|
46
|
-
tryImportIndexedDir({ importFile: importer.importFile, importFileAtomic: importer.importFile }, stage, filenames,
|
|
34
|
+
tryImportIndexedDir({ importFile: importer.importFile, importFileAtomic: importer.importFile }, stage, filenames, {
|
|
35
|
+
links: symlinkDirs(opts, filenames, { writtenDir: stage, finalDir: newDir }),
|
|
36
|
+
symlinks: opts.symlinks,
|
|
37
|
+
});
|
|
47
38
|
if (opts.keepModulesDir) {
|
|
48
|
-
// Keeping node_modules is needed only when the hoisted node linker is used.
|
|
49
39
|
moveOrMergeModulesDirs(path.join(newDir, 'node_modules'), path.join(stage, 'node_modules'));
|
|
50
40
|
}
|
|
51
41
|
}
|
|
52
42
|
catch (err) {
|
|
53
|
-
|
|
54
|
-
rimrafSync(stage);
|
|
55
|
-
}
|
|
56
|
-
catch { } // eslint-disable-line:no-empty
|
|
43
|
+
cleanStageDir(stage);
|
|
57
44
|
if (retryWithFixedFileMap(err, dirImport))
|
|
58
45
|
return;
|
|
59
46
|
throw err;
|
|
60
47
|
}
|
|
48
|
+
commitStagedDir(stage, newDir);
|
|
49
|
+
}
|
|
50
|
+
function commitStagedDir(stage, newDir) {
|
|
61
51
|
try {
|
|
62
|
-
// rename-overwrite retries Windows lock errors (EPERM, EBUSY) but not the
|
|
63
|
-
// EACCES that the same locks produce on a Windows drive mounted into WSL.
|
|
64
52
|
withFileLockRetry(() => {
|
|
65
53
|
renameOverwriteSync(stage, newDir);
|
|
66
54
|
});
|
|
67
55
|
}
|
|
68
56
|
catch (renameErr) {
|
|
69
|
-
|
|
70
|
-
rimrafSync(stage);
|
|
71
|
-
}
|
|
72
|
-
catch { } // eslint-disable-line:no-empty
|
|
57
|
+
cleanStageDir(stage);
|
|
73
58
|
throw renameErr;
|
|
74
59
|
}
|
|
75
60
|
}
|
|
76
|
-
|
|
77
|
-
// the installs running in them. Nothing here removes a dirent: the winner of
|
|
78
|
-
// the exclusive mkdir writes the package, and everyone else adopts the result
|
|
79
|
-
// when it already matches and otherwise replaces only the entries that do not,
|
|
80
|
-
// so concurrent importers converge on the same tree.
|
|
81
|
-
//
|
|
82
|
-
// Replacing rather than adopting is what keeps a shared slot repairable. The
|
|
83
|
-
// linking tiers report EEXIST for a dirent that is already there, so an import
|
|
84
|
-
// that adopts what it finds keeps a file truncated by an interrupted copy, and
|
|
85
|
-
// then puts the completion marker on top of it — after which the directory
|
|
86
|
-
// looks finished to every later install and is never repaired.
|
|
87
|
-
function importIntoSharedDir(dirImport) {
|
|
88
|
-
const { importer, newDir, filenames } = dirImport;
|
|
89
|
-
fs.mkdirSync(path.dirname(newDir), { recursive: true });
|
|
90
|
-
let created = false;
|
|
61
|
+
function cleanStageDir(stage) {
|
|
91
62
|
try {
|
|
92
|
-
|
|
93
|
-
created = true;
|
|
94
|
-
}
|
|
95
|
-
catch (err) {
|
|
96
|
-
if (!util.types.isNativeError(err) || !('code' in err) || err.code !== 'EEXIST')
|
|
97
|
-
throw err;
|
|
98
|
-
}
|
|
99
|
-
if (created) {
|
|
100
|
-
try {
|
|
101
|
-
tryImportIndexedDir(importer, newDir, filenames);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
catch (err) {
|
|
105
|
-
if (retryWithFixedFileMap(err, dirImport))
|
|
106
|
-
return;
|
|
107
|
-
// Our own write stopped partway. Another importer may already be reading
|
|
108
|
-
// what did land, so finish the directory in place instead of staging a
|
|
109
|
-
// replacement for it.
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (allFilesMatch(newDir, filenames))
|
|
113
|
-
return;
|
|
114
|
-
try {
|
|
115
|
-
repairIndexedDir(dirImport);
|
|
116
|
-
}
|
|
117
|
-
catch (err) {
|
|
118
|
-
if (retryWithFixedFileMap(err, dirImport))
|
|
119
|
-
return;
|
|
120
|
-
throw err;
|
|
63
|
+
rimrafSync(stage);
|
|
121
64
|
}
|
|
65
|
+
catch { }
|
|
122
66
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
// belongs to whoever put it there, and a slot other installs are reading is
|
|
126
|
-
// not somewhere to delete from speculatively.
|
|
127
|
-
function repairIndexedDir({ importer, newDir, filenames }) {
|
|
128
|
-
makeFileMapDirs(newDir, filenames, { clearBlockers: true });
|
|
129
|
-
let packageJsonSrc;
|
|
130
|
-
for (const [f, src] of filenames) {
|
|
131
|
-
if (f === 'package.json') {
|
|
132
|
-
packageJsonSrc = src;
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
135
|
-
replaceFileIfDifferent(importer.importFile, src, path.join(newDir, f));
|
|
136
|
-
}
|
|
137
|
-
if (packageJsonSrc !== undefined) {
|
|
138
|
-
replaceFileIfDifferent(importer.importFile, packageJsonSrc, path.join(newDir, 'package.json'));
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
// Swap the file in through a temp sibling: a reader sees either the old dirent
|
|
142
|
-
// or the new one, and the rename replaces what the linking tiers would have
|
|
143
|
-
// refused to overwrite.
|
|
144
|
-
function replaceFileIfDifferent(importFile, src, dest) {
|
|
145
|
-
if (mismatchReason(dest, src) === undefined)
|
|
146
|
-
return;
|
|
147
|
-
const tmp = pathTemp(dest);
|
|
148
|
-
try {
|
|
149
|
-
importFile(src, tmp);
|
|
150
|
-
}
|
|
151
|
-
catch (err) {
|
|
152
|
-
try {
|
|
153
|
-
fs.unlinkSync(tmp);
|
|
154
|
-
}
|
|
155
|
-
catch { } // eslint-disable-line:no-empty
|
|
156
|
-
throw err;
|
|
157
|
-
}
|
|
67
|
+
function tryExclusiveMkdir(newDir) {
|
|
68
|
+
fs.mkdirSync(path.dirname(newDir), { recursive: true });
|
|
158
69
|
try {
|
|
159
|
-
|
|
160
|
-
|
|
70
|
+
fs.mkdirSync(newDir);
|
|
71
|
+
return true;
|
|
161
72
|
}
|
|
162
73
|
catch (err) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
catch { } // eslint-disable-line:no-empty
|
|
167
|
-
if (mismatchReason(dest, src) === undefined)
|
|
168
|
-
return;
|
|
74
|
+
if (isError(err) && 'code' in err && err.code === 'EEXIST')
|
|
75
|
+
return false;
|
|
169
76
|
throw err;
|
|
170
77
|
}
|
|
171
78
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
// healing a slot together race over these paths, and one that finishes the
|
|
177
|
-
// same work first leaves exactly what the removal was for, whether it merely
|
|
178
|
-
// unlinked the blocker or replaced it outright. The inspection retries for
|
|
179
|
-
// the same reason the others here do.
|
|
180
|
-
function dirFitsAt(dir) {
|
|
79
|
+
function tryDirectSharedImport(dirImport) {
|
|
80
|
+
const { importer, newDir, filenames, opts } = dirImport;
|
|
81
|
+
if (!tryExclusiveMkdir(newDir))
|
|
82
|
+
return false;
|
|
181
83
|
try {
|
|
182
|
-
|
|
84
|
+
tryImportIndexedDir(importer, newDir, filenames, { symlinks: opts.symlinks });
|
|
85
|
+
return true;
|
|
183
86
|
}
|
|
184
87
|
catch (err) {
|
|
185
|
-
return
|
|
88
|
+
return retryWithFixedFileMap(err, dirImport);
|
|
186
89
|
}
|
|
187
90
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
function clearDirBlockingFile(dest) {
|
|
195
|
-
let stats;
|
|
91
|
+
function importIntoSharedDir(dirImport) {
|
|
92
|
+
if (tryDirectSharedImport(dirImport))
|
|
93
|
+
return;
|
|
94
|
+
const { newDir, filenames, opts } = dirImport;
|
|
95
|
+
if (allFilesMatch(newDir, filenames) && allSymlinksMatch(newDir, opts.symlinks))
|
|
96
|
+
return;
|
|
196
97
|
try {
|
|
197
|
-
|
|
98
|
+
repairIndexedDir(dirImport);
|
|
198
99
|
}
|
|
199
100
|
catch (err) {
|
|
200
|
-
if (
|
|
101
|
+
if (retryWithFixedFileMap(err, dirImport))
|
|
201
102
|
return;
|
|
202
103
|
throw err;
|
|
203
104
|
}
|
|
204
|
-
if (stats.isDirectory()) {
|
|
205
|
-
rimrafSync(dest);
|
|
206
|
-
}
|
|
207
105
|
}
|
|
208
|
-
// A file where the package needs a directory turns up only in a damaged tree,
|
|
209
|
-
// and would fail the mkdir below. Walking top-down means a segment whose
|
|
210
|
-
// parent is itself a file is never reached: the parent goes first, and
|
|
211
|
-
// everything under a missing segment is missing too.
|
|
212
|
-
function clearDirentBlockingDir(newDir, relativeDir) {
|
|
213
|
-
let dir = newDir;
|
|
214
|
-
for (const segment of relativeDir.split(/[\\/]/)) {
|
|
215
|
-
dir = path.join(dir, segment);
|
|
216
|
-
let stats;
|
|
217
|
-
try {
|
|
218
|
-
stats = lstatWithRetry(dir);
|
|
219
|
-
}
|
|
220
|
-
catch (err) {
|
|
221
|
-
if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')
|
|
222
|
-
return;
|
|
223
|
-
throw err;
|
|
224
|
-
}
|
|
225
|
-
if (stats.isDirectory())
|
|
226
|
-
continue;
|
|
227
|
-
try {
|
|
228
|
-
unlinkWithRetry(dir);
|
|
229
|
-
}
|
|
230
|
-
catch (err) {
|
|
231
|
-
if (!dirFitsAt(dir))
|
|
232
|
-
throw err;
|
|
233
|
-
}
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
// The two failures an indexed file map can cause on its own: names that
|
|
238
|
-
// collide on a case-insensitive filesystem, and names the filesystem rejects
|
|
239
|
-
// outright. Both are recovered by rewriting the map and importing again.
|
|
240
|
-
// Returns false for anything else, which the caller must treat as a real
|
|
241
|
-
// failure.
|
|
242
106
|
function retryWithFixedFileMap(err, dirImport) {
|
|
243
107
|
const { importer, newDir, filenames, opts } = dirImport;
|
|
244
|
-
if (!
|
|
108
|
+
if (!isError(err) || !('code' in err))
|
|
245
109
|
return false;
|
|
246
110
|
if (err.code === 'EEXIST') {
|
|
247
111
|
const { uniqueFileMap, conflictingFileNames } = getUniqueFileMap(filenames);
|
|
@@ -263,118 +127,21 @@ function retryWithFixedFileMap(err, dirImport) {
|
|
|
263
127
|
}
|
|
264
128
|
return false;
|
|
265
129
|
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
// (non-atomic) write is safe. If newDir already exists — a concurrent importer
|
|
270
|
-
// or a stale partial directory from an interrupted import — this returns false
|
|
271
|
-
// so the caller falls back to the staging path, which builds a complete
|
|
272
|
-
// directory in a private temp dir and atomically renames it into place.
|
|
273
|
-
//
|
|
274
|
-
// Destructively emptying a directory another process may be populating could
|
|
275
|
-
// otherwise leave a partial package behind: if the surviving files include the
|
|
276
|
-
// package.json completion marker, every later install treats the broken
|
|
277
|
-
// directory as complete and never repairs it.
|
|
278
|
-
function tryExclusiveImport(importer, newDir, filenames, links) {
|
|
279
|
-
fs.mkdirSync(path.dirname(newDir), { recursive: true });
|
|
280
|
-
try {
|
|
281
|
-
fs.mkdirSync(newDir);
|
|
282
|
-
}
|
|
283
|
-
catch (err) {
|
|
284
|
-
if (util.types.isNativeError(err) && 'code' in err && err.code === 'EEXIST')
|
|
285
|
-
return false;
|
|
286
|
-
throw err;
|
|
287
|
-
}
|
|
288
|
-
// We exclusively created newDir, so no other process writes into it directly
|
|
289
|
-
// (concurrent importers see EEXIST above and take the staging path). The
|
|
290
|
-
// directory is ours: on failure we remove our partial result before falling
|
|
291
|
-
// back to staging, so the next attempt — including this process's own method
|
|
292
|
-
// fallbacks (clone → hardlink → copy) — can fast-path again.
|
|
130
|
+
function tryExclusiveImport(importer, newDir, filenames, entries) {
|
|
131
|
+
if (!tryExclusiveMkdir(newDir))
|
|
132
|
+
return false;
|
|
293
133
|
try {
|
|
294
|
-
tryImportIndexedDir(importer, newDir, filenames,
|
|
134
|
+
tryImportIndexedDir(importer, newDir, filenames, entries);
|
|
295
135
|
return true;
|
|
296
136
|
}
|
|
297
137
|
catch {
|
|
298
138
|
try {
|
|
299
139
|
rimrafSync(newDir);
|
|
300
140
|
}
|
|
301
|
-
catch { }
|
|
141
|
+
catch { }
|
|
302
142
|
return false;
|
|
303
143
|
}
|
|
304
144
|
}
|
|
305
|
-
function allFilesMatch(dir, filenames) {
|
|
306
|
-
// The completion marker is written last, so its absence settles the common
|
|
307
|
-
// case — a directory another importer is still filling — in one stat,
|
|
308
|
-
// wherever the map happens to hold it.
|
|
309
|
-
const markerSrc = filenames.get('package.json');
|
|
310
|
-
if (markerSrc !== undefined && !fileMatches(dir, 'package.json', markerSrc))
|
|
311
|
-
return false;
|
|
312
|
-
for (const [f, src] of filenames) {
|
|
313
|
-
if (f === 'package.json')
|
|
314
|
-
continue;
|
|
315
|
-
if (!fileMatches(dir, f, src))
|
|
316
|
-
return false;
|
|
317
|
-
}
|
|
318
|
-
return true;
|
|
319
|
-
}
|
|
320
|
-
function fileMatches(dir, f, src) {
|
|
321
|
-
const reason = mismatchReason(path.join(dir, f), src);
|
|
322
|
-
if (reason === undefined)
|
|
323
|
-
return true;
|
|
324
|
-
globalInfo(`Re-importing "${dir}" because file "${f}" ${reason}`);
|
|
325
|
-
return false;
|
|
326
|
-
}
|
|
327
|
-
// Why `target` is not the store file at `src`, or undefined when it already
|
|
328
|
-
// is. Files imported by hardlink or reflink share the store inode, which
|
|
329
|
-
// settles it without a read; the copy tier compares size first, then content.
|
|
330
|
-
function mismatchReason(target, src) {
|
|
331
|
-
try {
|
|
332
|
-
const targetStat = fs.lstatSync(target, { bigint: true });
|
|
333
|
-
if (!targetStat.isFile())
|
|
334
|
-
return 'is not a regular file';
|
|
335
|
-
const srcStat = gfs.statSync(src, { bigint: true });
|
|
336
|
-
const hasSameFileIdentity = targetStat.ino !== 0n &&
|
|
337
|
-
targetStat.dev !== 0n &&
|
|
338
|
-
targetStat.ino === srcStat.ino &&
|
|
339
|
-
targetStat.dev === srcStat.dev;
|
|
340
|
-
if (hasSameFileIdentity)
|
|
341
|
-
return undefined;
|
|
342
|
-
if (targetStat.size !== srcStat.size)
|
|
343
|
-
return 'has a different size';
|
|
344
|
-
if (!filesHaveEqualContents(target, src))
|
|
345
|
-
return 'has different content';
|
|
346
|
-
return undefined;
|
|
347
|
-
}
|
|
348
|
-
catch {
|
|
349
|
-
return 'is missing or unreadable';
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
function filesHaveEqualContents(left, right) {
|
|
353
|
-
const leftBuffer = Buffer.allocUnsafe(FILE_COMPARE_BUFFER_SIZE);
|
|
354
|
-
const rightBuffer = Buffer.allocUnsafe(FILE_COMPARE_BUFFER_SIZE);
|
|
355
|
-
const leftFd = fs.openSync(left, 'r');
|
|
356
|
-
try {
|
|
357
|
-
const rightFd = fs.openSync(right, 'r');
|
|
358
|
-
try {
|
|
359
|
-
for (;;) {
|
|
360
|
-
const leftBytes = fs.readSync(leftFd, leftBuffer, 0, leftBuffer.length, null);
|
|
361
|
-
const rightBytes = fs.readSync(rightFd, rightBuffer, 0, rightBuffer.length, null);
|
|
362
|
-
if (leftBytes !== rightBytes)
|
|
363
|
-
return false;
|
|
364
|
-
if (leftBytes === 0)
|
|
365
|
-
return true;
|
|
366
|
-
if (!leftBuffer.subarray(0, leftBytes).equals(rightBuffer.subarray(0, rightBytes)))
|
|
367
|
-
return false;
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
finally {
|
|
371
|
-
fs.closeSync(rightFd);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
finally {
|
|
375
|
-
fs.closeSync(leftFd);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
145
|
function retryWithSanitizedFilenames({ importer, newDir, filenames, opts }) {
|
|
379
146
|
const { sanitizedFilenames, invalidFilenames } = sanitizeFilenames(filenames);
|
|
380
147
|
if (invalidFilenames.length === 0)
|
|
@@ -386,44 +153,31 @@ They were renamed.`);
|
|
|
386
153
|
importIndexedDir(importer, newDir, sanitizedFilenames, opts);
|
|
387
154
|
return true;
|
|
388
155
|
}
|
|
389
|
-
function
|
|
390
|
-
const sanitizedFilenames = new Map();
|
|
391
|
-
const invalidFilenames = [];
|
|
392
|
-
for (const [filename, src] of filenames) {
|
|
393
|
-
const sanitizedFilename = filename.split('/').map((f) => sanitizeFilename(f)).join('/');
|
|
394
|
-
if (sanitizedFilename !== filename) {
|
|
395
|
-
invalidFilenames.push(filename);
|
|
396
|
-
}
|
|
397
|
-
sanitizedFilenames.set(sanitizedFilename, src);
|
|
398
|
-
}
|
|
399
|
-
return { sanitizedFilenames, invalidFilenames };
|
|
400
|
-
}
|
|
401
|
-
function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames, links) {
|
|
156
|
+
function tryImportIndexedDir({ importFile, importFileAtomic }, newDir, filenames, { links, symlinks } = {}) {
|
|
402
157
|
makeFileMapDirs(newDir, filenames);
|
|
403
|
-
// Write package.json last so it acts as a completion marker.
|
|
404
|
-
// pkgExistsAtTargetDir() checks for package.json to decide if a package
|
|
405
|
-
// is already imported — writing it last ensures a crash mid-import won't
|
|
406
|
-
// leave a partially-populated directory that appears fully imported.
|
|
407
158
|
let packageJsonSrc;
|
|
408
|
-
for (const [
|
|
409
|
-
if (
|
|
159
|
+
for (const [relativePath, src] of filenames) {
|
|
160
|
+
if (relativePath === 'package.json') {
|
|
410
161
|
packageJsonSrc = src;
|
|
411
162
|
continue;
|
|
412
163
|
}
|
|
413
|
-
importEntry(importFile, src, path.join(newDir,
|
|
164
|
+
importEntry(importFile, src, path.join(newDir, relativePath), links);
|
|
165
|
+
}
|
|
166
|
+
for (const [relativePath, target] of symlinks ?? []) {
|
|
167
|
+
const dest = path.join(newDir, relativePath);
|
|
168
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
169
|
+
fs.symlinkSync(target, dest);
|
|
414
170
|
}
|
|
415
171
|
if (packageJsonSrc !== undefined) {
|
|
416
172
|
importEntry(importFileAtomic, packageJsonSrc, path.join(newDir, 'package.json'), links);
|
|
417
173
|
}
|
|
418
174
|
}
|
|
419
|
-
// Only a local directory has symlinks to preserve. The store holds regular
|
|
420
|
-
// files, so other imports skip the lstat per file.
|
|
421
175
|
function symlinkDirs(opts, filenames, dirs) {
|
|
422
176
|
if (opts.resolvedFrom !== 'local-dir')
|
|
423
177
|
return undefined;
|
|
424
178
|
const imported = new Set(['']);
|
|
425
|
-
for (const
|
|
426
|
-
for (let entry =
|
|
179
|
+
for (const relativePath of filenames.keys()) {
|
|
180
|
+
for (let entry = relativePath; entry !== '' && !imported.has(entry); entry = path.posix.dirname(entry).replace(/^\.$/, '')) {
|
|
427
181
|
imported.add(entry);
|
|
428
182
|
}
|
|
429
183
|
}
|
|
@@ -434,136 +188,4 @@ function importEntry(importFile, src, dest, links) {
|
|
|
434
188
|
return;
|
|
435
189
|
importFile(src, dest);
|
|
436
190
|
}
|
|
437
|
-
// Recreate the symlink at src as dest when its target stays inside the
|
|
438
|
-
// package, and report whether the entry is handled. A link that points outside
|
|
439
|
-
// the package, or at a file the import leaves out, is imported as the file it
|
|
440
|
-
// points to. A link to a directory the import leaves out is left out too.
|
|
441
|
-
function copyInternalSymlink(src, dest, links) {
|
|
442
|
-
let target = fs.readlinkSync(src);
|
|
443
|
-
if (path.isAbsolute(target)) {
|
|
444
|
-
// The link text can reach the package through another path than src,
|
|
445
|
-
// such as a symlinked workspace, so compare real paths.
|
|
446
|
-
target = path.relative(realpathOrSelf(path.dirname(src)), realpathOrSelf(target));
|
|
447
|
-
}
|
|
448
|
-
const resolved = path.resolve(path.dirname(dest), target);
|
|
449
|
-
if (escapesDir(links.writtenDir, resolved))
|
|
450
|
-
return false;
|
|
451
|
-
if (!links.imported.has(path.relative(links.writtenDir, resolved).split(path.sep).join('/'))) {
|
|
452
|
-
return isDirectory(src);
|
|
453
|
-
}
|
|
454
|
-
if (process.platform !== 'win32') {
|
|
455
|
-
fs.symlinkSync(target, dest);
|
|
456
|
-
return true;
|
|
457
|
-
}
|
|
458
|
-
const isDir = isDirectory(src);
|
|
459
|
-
try {
|
|
460
|
-
fs.symlinkSync(target, dest, isDir ? 'dir' : 'file');
|
|
461
|
-
}
|
|
462
|
-
catch (err) {
|
|
463
|
-
// Creating a symlink on Windows needs a privilege that a junction does
|
|
464
|
-
// not, and a file has no junction to fall back to.
|
|
465
|
-
if (!util.types.isNativeError(err) || !('code' in err) || err.code !== 'EPERM')
|
|
466
|
-
throw err;
|
|
467
|
-
if (!isDir)
|
|
468
|
-
return false;
|
|
469
|
-
fs.symlinkSync(path.join(links.finalDir, path.relative(links.writtenDir, resolved)), dest, 'junction');
|
|
470
|
-
}
|
|
471
|
-
return true;
|
|
472
|
-
}
|
|
473
|
-
function realpathOrSelf(file) {
|
|
474
|
-
try {
|
|
475
|
-
return fs.realpathSync(file);
|
|
476
|
-
}
|
|
477
|
-
catch (err) {
|
|
478
|
-
if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')
|
|
479
|
-
return file;
|
|
480
|
-
throw err;
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
function isDirectory(file) {
|
|
484
|
-
try {
|
|
485
|
-
return fs.statSync(file).isDirectory();
|
|
486
|
-
}
|
|
487
|
-
catch (err) {
|
|
488
|
-
if (util.types.isNativeError(err) && 'code' in err && err.code === 'ENOENT')
|
|
489
|
-
return false;
|
|
490
|
-
throw err;
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
function escapesDir(rootDir, targetPath) {
|
|
494
|
-
const rel = path.relative(rootDir, targetPath);
|
|
495
|
-
return rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel);
|
|
496
|
-
}
|
|
497
|
-
// Sorting shortest-first means the recursive mkdir for a deeper directory
|
|
498
|
-
// always finds its ancestor already on disk.
|
|
499
|
-
function makeFileMapDirs(newDir, filenames, opts) {
|
|
500
|
-
const allDirs = new Set();
|
|
501
|
-
for (const f of filenames.keys()) {
|
|
502
|
-
const dir = path.dirname(f);
|
|
503
|
-
if (dir === '.')
|
|
504
|
-
continue;
|
|
505
|
-
allDirs.add(dir);
|
|
506
|
-
}
|
|
507
|
-
for (const dir of Array.from(allDirs).sort((d1, d2) => d1.length - d2.length)) {
|
|
508
|
-
if (opts?.clearBlockers) {
|
|
509
|
-
clearDirentBlockingDir(newDir, dir);
|
|
510
|
-
}
|
|
511
|
-
fs.mkdirSync(path.join(newDir, dir), { recursive: true });
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
function getUniqueFileMap(fileMap) {
|
|
515
|
-
const lowercaseFiles = new Map();
|
|
516
|
-
const conflictingFileNames = new Map();
|
|
517
|
-
const uniqueFileMap = new Map();
|
|
518
|
-
for (const filename of Array.from(fileMap.keys()).sort()) {
|
|
519
|
-
const lowercaseFilename = filename.toLowerCase();
|
|
520
|
-
if (lowercaseFiles.has(lowercaseFilename)) {
|
|
521
|
-
conflictingFileNames.set(filename, lowercaseFiles.get(lowercaseFilename));
|
|
522
|
-
continue;
|
|
523
|
-
}
|
|
524
|
-
lowercaseFiles.set(lowercaseFilename, filename);
|
|
525
|
-
uniqueFileMap.set(filename, fileMap.get(filename));
|
|
526
|
-
}
|
|
527
|
-
return {
|
|
528
|
-
conflictingFileNames,
|
|
529
|
-
uniqueFileMap,
|
|
530
|
-
};
|
|
531
|
-
}
|
|
532
|
-
function moveOrMergeModulesDirs(src, dest) {
|
|
533
|
-
try {
|
|
534
|
-
renameEvenAcrossDevices(src, dest);
|
|
535
|
-
}
|
|
536
|
-
catch (err) {
|
|
537
|
-
switch (util.types.isNativeError(err) && 'code' in err && err.code) {
|
|
538
|
-
case 'ENOENT':
|
|
539
|
-
// If src directory doesn't exist, there is nothing to do
|
|
540
|
-
return;
|
|
541
|
-
case 'ENOTEMPTY':
|
|
542
|
-
case 'EPERM': // This error code is thrown on Windows
|
|
543
|
-
// The newly added dependency might have node_modules if it has bundled dependencies.
|
|
544
|
-
mergeModulesDirs(src, dest);
|
|
545
|
-
return;
|
|
546
|
-
default:
|
|
547
|
-
throw err;
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
function renameEvenAcrossDevices(src, dest) {
|
|
552
|
-
try {
|
|
553
|
-
gfs.renameSync(src, dest);
|
|
554
|
-
}
|
|
555
|
-
catch (err) {
|
|
556
|
-
if (!(util.types.isNativeError(err) && 'code' in err && err.code === 'EXDEV'))
|
|
557
|
-
throw err;
|
|
558
|
-
fsx.copySync(src, dest);
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
function mergeModulesDirs(src, dest) {
|
|
562
|
-
const srcFiles = fs.readdirSync(src);
|
|
563
|
-
const destFiles = new Set(fs.readdirSync(dest));
|
|
564
|
-
const filesToMove = srcFiles.filter((file) => !destFiles.has(file));
|
|
565
|
-
for (const file of filesToMove) {
|
|
566
|
-
renameEvenAcrossDevices(path.join(src, file), path.join(dest, file));
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
191
|
//# sourceMappingURL=importIndexedDir.js.map
|