
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
@ansi-tools/parser
Advanced tools
Tokenize and parse strings containing ANSI escape sequences and control codes
Parser for ANSI escape sequences.
\x1b or \u001b) and 8-bit (\u009b) introducers\033) and shorthand \e introducers (only escaped)\x1b\\, \x07)Used by ansi.tools.
npm install @ansi-tools/parser
import { parse } from "@ansi-tools/parser";
const input = "\x1b[31mHello\x1b[0m World";
for (const code of parse(input)) {
console.log(code);
}
There is a difference between escaped and unescaped input. Only with an escaped
input string the raw input and the positions can be preserved in the tokens and
control codes. See the example below for the default and the /escaped import.
The default and unescaped tokenization is roughly ~30% faster. Use this if you just need the control codes.
import { parse } from "@ansi-tools/parser";
parse(`\x1b[31mHello\x1b[0m`);
// result:
[
{
type: "CSI",
pos: 0,
raw: "\u001b[31m",
command: "m",
params: ["31"],
},
{
type: "TEXT",
pos: 5,
raw: "Hello",
},
{
type: "CSI",
pos: 10,
raw: "\u001b[0m",
command: "m",
params: ["0"],
},
];
import { parse } from "@ansi-tools/parser/escaped";
parse(String.raw`\x1b[31mHello\x1b[0m`);
// result:
[
{
type: "CSI",
pos: 0,
raw: "\\x1b[31m",
command: "m",
params: ["31"],
},
{
type: "TEXT",
pos: 8,
raw: "Hello",
},
{
type: "CSI",
pos: 13,
raw: "\\x1b[0m",
command: "m",
params: ["0"],
},
];
The tokenizer and generators are also available, for both the default and the
/escaped versions.
import { tokenize } from "@ansi-tools/parser";
const input = "\x1b[31m";
for (const token of tokenize(input)) {
console.log(token);
}
import { tokenizer, parser } from "@ansi-tools/parser";
const input = "\x1b[31mHello\x1b[0m";
const tokens = tokenizer(input);
const codes = parser(tokens);
for (const code of codes) {
console.log(code);
}
function tokenize(input: string): TOKEN[];
function parse(input: string): CODE[];
function* tokenizer(input: string): Generator<TOKEN>;
function* parser(tokens: Generator<TOKEN>): Generator<CODE>;
type TOKEN = {
type: "INTRODUCER" | "DATA" | "FINAL" | "TEXT";
pos: number;
raw: string;
code?: string;
intermediate?: string;
};
type CONTROL_CODE = {
type: "CSI" | "DCS" | "DEC" | "ESC" | "OSC" | "SGR" | "STRING" | "PRIVATE";
command: string;
raw: string;
params: string[];
pos: number;
};
type TEXT = {
type: "TEXT";
raw: string;
pos: number;
};
type CODE = CONTROL_CODE | TEXT;
ISC
FAQs
Tokenize and parse strings containing ANSI escape sequences and control codes
The npm package @ansi-tools/parser receives a total of 37 weekly downloads. As such, @ansi-tools/parser popularity was classified as not popular.
We found that @ansi-tools/parser demonstrated a healthy version release cadence and project activity because the last version was released less than 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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.