Socket
Socket
Sign inDemoInstall

metro-file-map

Package Overview
Dependencies
Maintainers
2
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

metro-file-map - npm Package Compare versions

Comparing version 0.80.10 to 0.80.11

2

package.json
{
"name": "metro-file-map",
"version": "0.80.10",
"version": "0.80.11",
"description": "[Experimental] - 🚇 File crawling, watching and mapping for Metro",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -165,3 +165,2 @@ /**

getModuleName(file: Path): string | null;
getRealPath(file: Path): string | null;
getSerializableSnapshot(): FileData;

@@ -171,2 +170,38 @@ getSha1(file: Path): string | null;

/**
* Given a start path (which need not exist), a subpath and type, and
* optionally a 'breakOnSegment', performs the following:
*
* X = mixedStartPath
* do
* if basename(X) === opts.breakOnSegment
* return null
* if X + subpath exists and has type opts.subpathType
* return {
* absolutePath: realpath(X + subpath)
* containerRelativePath: relative(mixedStartPath, X)
* }
* X = dirname(X)
* while X !== dirname(X)
*
* If opts.invalidatedBy is given, collects all absolute, real paths that if
* added or removed may invalidate this result.
*
* Useful for finding the closest package scope (subpath: package.json,
* type f, breakOnSegment: node_modules) or closest potential package root
* (subpath: node_modules/pkg, type: d) in Node.js resolution.
*/
hierarchicalLookup(
mixedStartPath: string,
subpath: string,
opts: {
breakOnSegment: string | null | undefined;
invalidatedBy: Set<string> | null | undefined;
subpathType: 'f' | 'd';
},
): {
absolutePath: string;
containerRelativePath: string;
} | null;
/**
* Analogous to posix lstat. If the file at `file` is a symlink, return

@@ -177,2 +212,8 @@ * information about the symlink without following it.

/**
* Return information about the given path, whether a directory or file.
* Always follow symlinks, and return a real path if it exists.
*/
lookup(mixedPath: Path): LookupResult;
matchFiles(opts: {

@@ -196,2 +237,25 @@ /* Filter relative paths against a pattern. */

export type LookupResult =
| {
// The node is missing from the FileSystem implementation (note this
// could indicate an unwatched path, or a directory containing no watched
// files).
exists: false;
// The real, normal, absolute paths of any symlinks traversed.
links: ReadonlySet<string>;
// The real, normal, absolute path of the first path segment
// encountered that does not exist, or cannot be navigated through.
missing: string;
}
| {
exists: true;
// The real, normal, absolute paths of any symlinks traversed.
links: ReadonlySet<string>;
// The real, normal, absolute path of the file or directory.
realPath: string;
// Currently lookup always follows symlinks, so can only return
// directories or regular files, but this may be extended.
type: 'd' | 'f';
};
export interface HasteMap {

@@ -198,0 +262,0 @@ getModule(

@@ -7,2 +7,3 @@ "use strict";

exports.RootPathUtils = void 0;
var _invariant = _interopRequireDefault(require("invariant"));
var path = _interopRequireWildcard(require("path"));

@@ -49,2 +50,5 @@ function _getRequireWildcardCache(nodeInterop) {

}
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj };
}
const UP_FRAGMENT_SEP = ".." + path.sep;

@@ -79,2 +83,5 @@ const SEP_UP_FRAGMENT = path.sep + "..";

}
getParts() {
return this.#rootParts;
}
absoluteToNormal(absolutePath) {

@@ -102,5 +109,12 @@ let endOfMatchingPrefix = 0;

upIndirectionsToPrepend
) ?? path.relative(this.#rootDir, absolutePath)
)?.collapsedPath ?? this.#slowAbsoluteToNormal(absolutePath)
);
}
#slowAbsoluteToNormal(absolutePath) {
const endsWithSep = absolutePath.endsWith(path.sep);
const result = path.relative(this.#rootDir, absolutePath);
return endsWithSep && !result.endsWith(path.sep)
? result + path.sep
: result;
}
normalToAbsolute(normalPath) {

@@ -128,18 +142,50 @@ let left = this.#rootDir;

return (
this.#tryCollapseIndirectionsInSuffix(relativePath, 0, 0) ??
this.#tryCollapseIndirectionsInSuffix(relativePath, 0, 0)
?.collapsedPath ??
path.relative(this.#rootDir, path.join(this.#rootDir, relativePath))
);
}
getAncestorOfRootIdx(normalPath) {
if (normalPath === "") {
return 0;
}
if (normalPath === "..") {
return 1;
}
if (normalPath.endsWith(SEP_UP_FRAGMENT)) {
return (normalPath.length + 1) / 3;
}
return null;
}
joinNormalToRelative(normalPath, relativePath) {
if (normalPath === "") {
return relativePath;
return {
collapsedSegments: 0,
normalPath: relativePath,
};
}
if (relativePath === "") {
return normalPath;
return {
collapsedSegments: 0,
normalPath,
};
}
const left = normalPath + path.sep;
const rawPath = left + relativePath;
if (normalPath === ".." || normalPath.endsWith(SEP_UP_FRAGMENT)) {
return this.relativeToNormal(normalPath + path.sep + relativePath);
const collapsed = this.#tryCollapseIndirectionsInSuffix(rawPath, 0, 0);
(0, _invariant.default)(collapsed != null, "Failed to collapse");
return {
collapsedSegments: collapsed.collapsedSegments,
normalPath: collapsed.collapsedPath,
};
}
return normalPath + path.sep + relativePath;
return {
collapsedSegments: 0,
normalPath: rawPath,
};
}
relative(from, to) {
return path.relative(from, to);
}
#tryCollapseIndirectionsInSuffix(

@@ -151,2 +197,3 @@ fullPath,

let totalUpIndirections = implicitUpIndirections;
let collapsedSegments = 0;
for (let pos = startOfRelativePart; ; pos += UP_FRAGMENT_SEP_LENGTH) {

@@ -164,2 +211,3 @@ const nextIndirection = fullPath.indexOf(CURRENT_FRAGMENT, pos);

pos += segmentToMaybeCollapse.length + 1;
collapsedSegments++;
totalUpIndirections--;

@@ -170,13 +218,36 @@ } else {

}
const right = fullPath.slice(pos);
if (pos >= fullPath.length) {
return {
collapsedPath:
totalUpIndirections > 0
? UP_FRAGMENT_SEP.repeat(totalUpIndirections - 1) +
".." +
fullPath.slice(pos - 1)
: "",
collapsedSegments,
};
}
const right = pos > 0 ? fullPath.slice(pos) : fullPath;
if (
right === "" ||
(right === ".." && totalUpIndirections >= this.#rootParts.length - 1)
right === ".." &&
totalUpIndirections >= this.#rootParts.length - 1
) {
return UP_FRAGMENT_SEP.repeat(totalUpIndirections).slice(0, -1);
return {
collapsedPath: UP_FRAGMENT_SEP.repeat(totalUpIndirections).slice(
0,
-1
),
collapsedSegments,
};
}
if (totalUpIndirections === 0) {
return right;
return {
collapsedPath: right,
collapsedSegments,
};
}
return UP_FRAGMENT_SEP.repeat(totalUpIndirections) + right;
return {
collapsedPath: UP_FRAGMENT_SEP.repeat(totalUpIndirections) + right,
collapsedSegments,
};
}

@@ -183,0 +254,0 @@ if (totalUpIndirections < this.#rootParts.length - 1) {

@@ -14,2 +14,8 @@ "use strict";

}
function isDirectory(node) {
return node instanceof Map;
}
function isRegularFile(node) {
return node[_constants.default.SYMLINK] === 0;
}
class TreeFS {

@@ -66,6 +72,3 @@ #cachedNormalSymlinkTargets = new WeakMap();

if (newMetadata) {
if (
(newMetadata[_constants.default.SYMLINK] === 0) !==
(metadata[_constants.default.SYMLINK] === 0)
) {
if (isRegularFile(newMetadata) !== isRegularFile(metadata)) {
continue;

@@ -124,8 +127,3 @@ }

const { canonicalPath, node } = result;
const type =
node instanceof Map
? "d"
: node[_constants.default.SYMLINK] === 0
? "f"
: "l";
const type = isDirectory(node) ? "d" : isRegularFile(node) ? "f" : "l";
(0, _invariant.default)(

@@ -160,3 +158,3 @@ type !== "l",

}
const fileType = fileMetadata[_constants.default.SYMLINK] === 0 ? "f" : "l";
const fileType = isRegularFile(fileMetadata) ? "f" : "l";
const modifiedTime = fileMetadata[_constants.default.MTIME];

@@ -187,3 +185,3 @@ return {

} = contextRootResult;
if (!(contextRoot instanceof Map)) {
if (!isDirectory(contextRoot)) {
return;

@@ -264,4 +262,10 @@ }

});
if (!(lookup?.node instanceof Map)) {
if (!lookup.exists) {
throw new Error(
`TreeFS: Unexpected error adding ${normalPath}.\nMissing: ` +
lookup.canonicalMissingPath
);
}
if (!isDirectory(lookup.node)) {
throw new Error(
`TreeFS: Could not add directory ${dirname}, adding ${normalPath}. ` +

@@ -286,3 +290,3 @@ `${dirname} already exists in the file map as a file.`

const { parentNode, canonicalPath, node } = result;
if (node instanceof Map && node.size > 0) {
if (isDirectory(node) && node.size > 0) {
throw new Error(

@@ -298,3 +302,3 @@ `TreeFS: remove called on a non-empty directory: ${mixedPath}`

}
return node instanceof Map ? null : node;
return isDirectory(node) ? null : node;
}

@@ -310,5 +314,7 @@ _lookupByNormalPath(

let seen;
let fromIdx = 0;
let parentNode = this.#rootNode;
let ancestorOfRootIdx = null;
let fromIdx = opts.start?.pathIdx ?? 0;
let parentNode = opts.start?.node ?? this.#rootNode;
let ancestorOfRootIdx = opts.start?.ancestorOfRootIdx ?? 0;
const collectAncestors = opts.collectAncestors;
let unseenPathFromIdx = 0;
while (targetNormalPath.length > fromIdx) {

@@ -320,2 +326,3 @@ const nextSepIdx = targetNormalPath.indexOf(_path.default.sep, fromIdx);

: targetNormalPath.slice(fromIdx, nextSepIdx);
const isUnseen = fromIdx >= unseenPathFromIdx;
fromIdx = !isLastSegment ? nextSepIdx + 1 : targetNormalPath.length;

@@ -326,5 +333,4 @@ if (segmentName === ".") {

let segmentNode = parentNode.get(segmentName);
if (segmentName === "..") {
ancestorOfRootIdx =
ancestorOfRootIdx == null ? 1 : ancestorOfRootIdx + 1;
if (segmentName === ".." && ancestorOfRootIdx != null) {
ancestorOfRootIdx++;
} else if (segmentNode != null) {

@@ -340,2 +346,3 @@ ancestorOfRootIdx = null;

exists: false,
missingSegmentName: segmentName,
};

@@ -349,10 +356,14 @@ }

if (
isLastSegment &&
(segmentNode instanceof Map ||
segmentNode[_constants.default.SYMLINK] === 0 ||
opts.followLeaf === false)
(nextSepIdx === targetNormalPath.length - 1 &&
isDirectory(segmentNode)) ||
(isLastSegment &&
(isDirectory(segmentNode) ||
isRegularFile(segmentNode) ||
opts.followLeaf === false))
) {
return {
ancestorOfRootIdx,
canonicalPath: targetNormalPath,
canonicalPath: isLastSegment
? targetNormalPath
: targetNormalPath.slice(0, -1),
exists: true,

@@ -363,4 +374,15 @@ node: segmentNode,

}
if (segmentNode instanceof Map) {
if (isDirectory(segmentNode)) {
parentNode = segmentNode;
if (collectAncestors && isUnseen) {
const currentPath = isLastSegment
? targetNormalPath
: targetNormalPath.slice(0, fromIdx - 1);
collectAncestors.push({
ancestorOfRootIdx,
node: segmentNode,
normalPath: currentPath,
segmentName,
});
}
} else {

@@ -370,6 +392,7 @@ const currentPath = isLastSegment

: targetNormalPath.slice(0, fromIdx - 1);
if (segmentNode[_constants.default.SYMLINK] === 0) {
if (isRegularFile(segmentNode)) {
return {
canonicalMissingPath: currentPath,
exists: false,
missingSegmentName: segmentName,
};

@@ -386,8 +409,45 @@ }

}
targetNormalPath = isLastSegment
? normalSymlinkTarget
: this.#pathUtils.joinNormalToRelative(
normalSymlinkTarget,
targetNormalPath.slice(fromIdx)
);
const remainingTargetPath = isLastSegment
? ""
: targetNormalPath.slice(fromIdx);
const joinedResult = this.#pathUtils.joinNormalToRelative(
normalSymlinkTarget.normalPath,
remainingTargetPath
);
targetNormalPath = joinedResult.normalPath;
if (
collectAncestors &&
!isLastSegment &&
(normalSymlinkTarget.ancestorOfRootIdx === 0 ||
joinedResult.collapsedSegments > 0)
) {
let node = this.#rootNode;
let collapsedPath = "";
const reverseAncestors = [];
for (
let i = 0;
i <= joinedResult.collapsedSegments && isDirectory(node);
i++
) {
if (
i > 0 ||
normalSymlinkTarget.ancestorOfRootIdx === 0 ||
joinedResult.collapsedSegments > 0
) {
reverseAncestors.push({
ancestorOfRootIdx: i,
node,
normalPath: collapsedPath,
segmentName: this.#pathUtils.getBasenameOfNthAncestor(i),
});
}
node = node.get("..") ?? new Map();
collapsedPath =
collapsedPath === ""
? ".."
: collapsedPath + _path.default.sep + "..";
}
collectAncestors.push(...reverseAncestors.reverse());
}
unseenPathFromIdx = normalSymlinkTarget.normalPath.length;
if (seen == null) {

@@ -400,2 +460,3 @@ seen = new Set([requestedNormalPath]);

exists: false,
missingSegmentName: segmentName,
};

@@ -406,2 +467,3 @@ }

parentNode = this.#rootNode;
ancestorOfRootIdx = 0;
}

@@ -414,3 +476,3 @@ }

return {
ancestorOfRootIdx: null,
ancestorOfRootIdx: 0,
canonicalPath: targetNormalPath,

@@ -422,2 +484,170 @@ exists: true,

}
hierarchicalLookup(mixedStartPath, subpath, opts) {
const ancestorsOfInput = [];
const normalPath = this._normalizePath(mixedStartPath);
const invalidatedBy = opts.invalidatedBy;
const closestLookup = this._lookupByNormalPath(normalPath, {
collectAncestors: ancestorsOfInput,
collectLinkPaths: invalidatedBy,
});
if (closestLookup.exists && isDirectory(closestLookup.node)) {
const maybeAbsolutePathMatch = this.#checkCandidateHasSubpath(
closestLookup.canonicalPath,
subpath,
opts.subpathType,
invalidatedBy,
null
);
if (maybeAbsolutePathMatch != null) {
return {
absolutePath: maybeAbsolutePathMatch,
containerRelativePath: "",
};
}
} else {
if (
invalidatedBy &&
(!closestLookup.exists || !isDirectory(closestLookup.node))
) {
invalidatedBy.add(
this.#pathUtils.normalToAbsolute(
closestLookup.exists
? closestLookup.canonicalPath
: closestLookup.canonicalMissingPath
)
);
}
if (
opts.breakOnSegment != null &&
!closestLookup.exists &&
closestLookup.missingSegmentName === opts.breakOnSegment
) {
return null;
}
}
let commonRoot = this.#rootNode;
let commonRootDepth = 0;
if (closestLookup.exists && closestLookup.ancestorOfRootIdx != null) {
commonRootDepth = closestLookup.ancestorOfRootIdx;
(0, _invariant.default)(
isDirectory(closestLookup.node),
"ancestors of the root must be directories"
);
commonRoot = closestLookup.node;
} else {
for (const ancestor of ancestorsOfInput) {
if (ancestor.ancestorOfRootIdx == null) {
break;
}
commonRootDepth = ancestor.ancestorOfRootIdx;
commonRoot = ancestor.node;
}
}
for (
let candidateIdx = ancestorsOfInput.length - 1;
candidateIdx >= commonRootDepth;
--candidateIdx
) {
const candidate = ancestorsOfInput[candidateIdx];
if (candidate.segmentName === opts.breakOnSegment) {
return null;
}
const maybeAbsolutePathMatch = this.#checkCandidateHasSubpath(
candidate.normalPath,
subpath,
opts.subpathType,
invalidatedBy,
{
ancestorOfRootIdx: candidate.ancestorOfRootIdx,
node: candidate.node,
pathIdx:
candidate.normalPath.length > 0
? candidate.normalPath.length + 1
: 0,
}
);
if (maybeAbsolutePathMatch != null) {
let prefixLength = commonRootDepth * 3;
for (let i = commonRootDepth; i <= candidateIdx; i++) {
prefixLength = normalPath.indexOf(
_path.default.sep,
prefixLength + 1
);
}
const containerRelativePath = normalPath.slice(prefixLength + 1);
return {
absolutePath: maybeAbsolutePathMatch,
containerRelativePath,
};
}
}
let candidateNormalPath =
commonRootDepth > 0 ? normalPath.slice(0, 3 * commonRootDepth - 1) : "";
const remainingNormalPath = normalPath.slice(commonRootDepth * 3);
let nextNode = commonRoot;
let depthBelowCommonRoot = 0;
while (isDirectory(nextNode)) {
const maybeAbsolutePathMatch = this.#checkCandidateHasSubpath(
candidateNormalPath,
subpath,
opts.subpathType,
invalidatedBy,
null
);
if (maybeAbsolutePathMatch != null) {
const rootDirParts = this.#pathUtils.getParts();
const relativeParts =
depthBelowCommonRoot > 0
? rootDirParts.slice(
-(depthBelowCommonRoot + commonRootDepth),
commonRootDepth > 0 ? -commonRootDepth : undefined
)
: [];
if (remainingNormalPath !== "") {
relativeParts.push(remainingNormalPath);
}
return {
absolutePath: maybeAbsolutePathMatch,
containerRelativePath: relativeParts.join(_path.default.sep),
};
}
depthBelowCommonRoot++;
candidateNormalPath =
candidateNormalPath === ""
? ".."
: candidateNormalPath + _path.default.sep + "..";
nextNode = nextNode.get("..");
}
return null;
}
#checkCandidateHasSubpath(
normalCandidatePath,
subpath,
subpathType,
invalidatedBy,
start
) {
const lookupResult = this._lookupByNormalPath(
this.#pathUtils.joinNormalToRelative(normalCandidatePath, subpath)
.normalPath,
{
collectLinkPaths: invalidatedBy,
}
);
if (
lookupResult.exists &&
isDirectory(lookupResult.node) === (subpathType === "d")
) {
return this.#pathUtils.normalToAbsolute(lookupResult.canonicalPath);
} else if (invalidatedBy) {
invalidatedBy.add(
this.#pathUtils.normalToAbsolute(
lookupResult.exists
? lookupResult.canonicalPath
: lookupResult.canonicalMissingPath
)
);
}
return null;
}
*metadataIterator(opts) {

@@ -430,3 +660,3 @@ yield* this._metadataIterator(this.#rootNode, opts);

!opts.includeNodeModules &&
node instanceof Map &&
isDirectory(node) &&
name === "node_modules"

@@ -438,8 +668,5 @@ ) {

prefix === "" ? name : prefix + _path.default.sep + name;
if (node instanceof Map) {
if (isDirectory(node)) {
yield* this._metadataIterator(node, opts, prefixedName);
} else if (
node[_constants.default.SYMLINK] === 0 ||
opts.includeSymlinks
) {
} else if (isRegularFile(node) || opts.includeSymlinks) {
yield {

@@ -459,3 +686,3 @@ canonicalPath: prefixedName,

*#directoryNodeIterator(node, parent, ancestorOfRootIdx) {
if (ancestorOfRootIdx != null && parent) {
if (ancestorOfRootIdx != null && ancestorOfRootIdx > 0 && parent) {
yield [

@@ -487,4 +714,4 @@ this.#pathUtils.getBasenameOfNthAncestor(ancestorOfRootIdx - 1),

const nodePath = prefixWithSep + name;
if (!(node instanceof Map)) {
if (node[_constants.default.SYMLINK] === 0) {
if (!isDirectory(node)) {
if (isRegularFile(node)) {
yield nodePath;

@@ -507,3 +734,3 @@ } else {

const target = resolved.node;
if (!(target instanceof Map)) {
if (!isDirectory(target)) {
yield nodePath;

@@ -529,3 +756,3 @@ } else if (

iterationRootParentNode,
ancestorOfRootIdx != null && ancestorOfRootIdx > 1
ancestorOfRootIdx != null && ancestorOfRootIdx > 0
? ancestorOfRootIdx - 1

@@ -541,5 +768,5 @@ : null,

_resolveSymlinkTargetToNormalPath(symlinkNode, canonicalPathOfSymlink) {
let normalSymlinkTarget = this.#cachedNormalSymlinkTargets.get(symlinkNode);
if (normalSymlinkTarget != null) {
return normalSymlinkTarget;
const cachedResult = this.#cachedNormalSymlinkTargets.get(symlinkNode);
if (cachedResult != null) {
return cachedResult;
}

@@ -557,8 +784,13 @@ const literalSymlinkTarget = symlinkNode[_constants.default.SYMLINK];

);
normalSymlinkTarget = _path.default.relative(
const normalSymlinkTarget = _path.default.relative(
this.#rootDir,
absoluteSymlinkTarget
);
this.#cachedNormalSymlinkTargets.set(symlinkNode, normalSymlinkTarget);
return normalSymlinkTarget;
const result = {
ancestorOfRootIdx:
this.#pathUtils.getAncestorOfRootIdx(normalSymlinkTarget),
normalPath: normalSymlinkTarget,
};
this.#cachedNormalSymlinkTargets.set(symlinkNode, result);
return result;
}

@@ -575,3 +807,3 @@ _getFileData(

});
if (!result.exists || result.node instanceof Map) {
if (!result.exists || isDirectory(result.node)) {
return null;

@@ -584,3 +816,3 @@ }

for (const [name, node] of root) {
if (node instanceof Map) {
if (isDirectory(node)) {
clone.set(name, this._cloneTree(node));

@@ -587,0 +819,0 @@ } else {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc