This commit is contained in:
2025-08-18 23:06:34 +08:00
parent 0bc04fb659
commit ed18af0cad
1926 changed files with 275098 additions and 0 deletions

67
package/node_modules/treeverse/lib/breadth.js generated vendored Normal file
View File

@@ -0,0 +1,67 @@
// Perform a breadth-first walk of a tree, either logical or physical
// This one only visits, it doesn't leave. That's because
// in a breadth-first traversal, children may be visited long
// after their parent, so the "exit" pass ends up being just
// another breadth-first walk.
//
// Breadth-first traversals are good for either creating a tree (ie,
// reifying a dep graph based on a package.json without a node_modules
// or package-lock), or mutating it in-place. For a map-reduce type of
// walk, it doesn't make a lot of sense, and is very expensive.
const breadth = ({
visit,
filter = () => true,
getChildren,
tree,
}) => {
const queue = []
const seen = new Map()
const next = () => {
while (queue.length) {
const node = queue.shift()
const res = visitNode(node)
if (isPromise(res)) {
return res.then(() => next())
}
}
return seen.get(tree)
}
const visitNode = (visitTree) => {
if (seen.has(visitTree)) {
return seen.get(visitTree)
}
seen.set(visitTree, null)
const res = visit ? visit(visitTree) : visitTree
if (isPromise(res)) {
const fullResult = res.then(resThen => {
seen.set(visitTree, resThen)
return kidNodes(visitTree)
})
seen.set(visitTree, fullResult)
return fullResult
} else {
seen.set(visitTree, res)
return kidNodes(visitTree)
}
}
const kidNodes = (kidTree) => {
const kids = getChildren(kidTree, seen.get(kidTree))
return isPromise(kids) ? kids.then(processKids) : processKids(kids)
}
const processKids = (kids) => {
kids = (kids || []).filter(filter)
queue.push(...kids)
}
queue.push(tree)
return next()
}
const isPromise = p => p && typeof p.then === 'function'
module.exports = breadth

88
package/node_modules/treeverse/lib/depth-descent.js generated vendored Normal file
View File

@@ -0,0 +1,88 @@
// Perform a depth-first walk of a tree, ONLY doing the descent (visit)
//
// This uses a stack rather than recursion, so that it can handle deeply
// nested trees without call stack overflows. (My kingdom for proper TCO!)
//
// This is only used for cases where leave() is not specified.
//
// a
// +-- b
// | +-- 1
// | +-- 2
// +-- c
// +-- 3
// +-- 4
//
// Expect:
// visit a
// visit b
// visit 1
// visit 2
// visit c
// visit 3
// visit 4
//
// stack.push(tree)
// while stack not empty
// pop T from stack
// VISIT(T)
// get children C of T
// push each C onto stack
const depth = ({
visit,
filter,
getChildren,
tree,
}) => {
const stack = []
const seen = new Map()
const next = () => {
while (stack.length) {
const node = stack.pop()
const res = visitNode(node)
if (isPromise(res)) {
return res.then(() => next())
}
}
return seen.get(tree)
}
const visitNode = (visitTree) => {
if (seen.has(visitTree)) {
return seen.get(visitTree)
}
seen.set(visitTree, null)
const res = visit ? visit(visitTree) : visitTree
if (isPromise(res)) {
const fullResult = res.then(resThen => {
seen.set(visitTree, resThen)
return kidNodes(visitTree)
})
seen.set(visitTree, fullResult)
return fullResult
} else {
seen.set(visitTree, res)
return kidNodes(visitTree)
}
}
const kidNodes = (kidTree) => {
const kids = getChildren(kidTree, seen.get(kidTree))
return isPromise(kids) ? kids.then(processKids) : processKids(kids)
}
const processKids = (kids) => {
kids = (kids || []).filter(filter)
stack.push(...kids)
}
stack.push(tree)
return next()
}
const isPromise = p => p && typeof p.then === 'function'
module.exports = depth

76
package/node_modules/treeverse/lib/depth.js generated vendored Normal file
View File

@@ -0,0 +1,76 @@
// Perform a depth-first walk of a tree.
//
// `visit(node)` is called when the node is first encountered.
// `leave(node, children)` is called when all of the node's children
// have been left or (in the case of cyclic graphs) visited.
//
// Only one of visit or leave is required. (Technically both are optional,
// but if you don't provide at least one, the tree is just walked without
// doing anything, which is a bit pointless.) If visit is provided, and
// leave is not, then this is a root->leaf traversal. If leave is provided,
// and visit is not, then it's leaf->root. Both can be provided for a
// map-reduce operation.
//
// If either visit or leave return a Promise for any node, then the
// walk returns a Promise.
const depthDescent = require('./depth-descent.js')
const depth = ({
visit,
leave,
filter = () => true,
seen = new Map(),
getChildren,
tree,
}) => {
if (!leave) {
return depthDescent({ visit, filter, getChildren, tree })
}
if (seen.has(tree)) {
return seen.get(tree)
}
seen.set(tree, null)
const visitNode = () => {
const res = visit ? visit(tree) : tree
if (isPromise(res)) {
const fullResult = res.then(resThen => {
seen.set(tree, resThen)
return kidNodes()
})
seen.set(tree, fullResult)
return fullResult
} else {
seen.set(tree, res)
return kidNodes()
}
}
const kidNodes = () => {
const kids = getChildren(tree, seen.get(tree))
return isPromise(kids) ? kids.then(processKids) : processKids(kids)
}
const processKids = nodes => {
const kids = (nodes || []).filter(filter).map(kid =>
depth({ visit, leave, filter, seen, getChildren, tree: kid }))
return kids.some(isPromise)
? Promise.all(kids).then(leaveNode)
: leaveNode(kids)
}
const leaveNode = kids => {
const res = leave(seen.get(tree), kids)
seen.set(tree, res)
// if it's a promise at this point, the caller deals with it
return res
}
return visitNode()
}
const isPromise = p => p && typeof p.then === 'function'
module.exports = depth

4
package/node_modules/treeverse/lib/index.js generated vendored Normal file
View File

@@ -0,0 +1,4 @@
module.exports = {
breadth: require('./breadth.js'),
depth: require('./depth.js'),
}