Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
The 'tsort' npm package is a simple implementation of topological sorting. It is used to order elements based on their dependencies, ensuring that each element appears before any elements that depend on it. This is particularly useful in scenarios like task scheduling, resolving module dependencies, and organizing data with hierarchical relationships.
Topological Sorting
This feature allows you to perform topological sorting on a set of elements with dependencies. In the code sample, 'a' depends on 'b', 'b' depends on 'c', and 'a' also depends on 'c'. The sorted output ensures that each element appears before any elements that depend on it.
const tsort = require('tsort');
const graph = tsort();
graph.add('a', 'b');
graph.add('b', 'c');
graph.add('a', 'c');
const sorted = graph.sort();
console.log(sorted); // Output: ['a', 'b', 'c']
The 'toposort' package provides similar functionality for topological sorting. It allows you to sort elements based on their dependencies. Compared to 'tsort', 'toposort' offers a more modern API and better performance for larger datasets.
The 'graphlib' package is a more comprehensive library for working with graphs, including topological sorting. It provides a rich set of features for creating, manipulating, and analyzing graphs. While 'tsort' focuses solely on topological sorting, 'graphlib' offers a broader range of graph-related functionalities.
The 'dependency-graph' package is designed to manage and resolve dependencies between nodes. It includes topological sorting as one of its features. Compared to 'tsort', 'dependency-graph' provides additional capabilities for managing dependencies and detecting cycles.
npm install tsort
var tsort = require('tsort');
// create an empty graph
var graph = tsort();
// add nodes
graph.add('a', 'b');
graph.add('b', 'c');
graph.add('0', 'a');
// outputs: [ '0', 'a', 'b', 'c' ]
console.dir(graph.sort());
// can add more than one node
graph.add('1', '2', '3', 'a');
// outputs: [ '0', '1', '2', '3', 'a', 'b', 'c' ]
console.dir(graph.sort());
// can add in array form
graph.add(['1', '1.5']);
graph.add(['1.5', 'a']);
// outputs: [ '0', '1', '2', '3', '1.5', 'a', 'b', 'c' ]
console.dir(graph.sort());
// detects cycles
graph.add('first', 'second');
graph.add('second', 'third', 'first');
// throws: Error: There is a cycle in the graph. It is not possible to derive a topological sort.
graph.sort();
#license MIT
FAQs
Topological sort on directed graphs.
The npm package tsort receives a total of 179,301 weekly downloads. As such, tsort popularity was classified as popular.
We found that tsort demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.