Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/resafe/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ export function check(regex: string | RegExp, options: Options = {}): Result {
const radius = Number(result.radius.toFixed(4))

if (!result.safe && !options.silent) {
log.error("Unsafe Regex!", `/${pattern}/`, [
`Spectral radius: ${radius} (threshold: ${options.threshold ?? 1.0})`,
"? Consider simplifying quantifiers",
])
log.error("Unsafe Regex!", {
property: { name: "regex", value: `/${pattern}/`, color: (f) => f.red() },
lines: [
`Spectral radius: ${radius} (threshold: ${options.threshold ?? 1.0})`,
"? Consider simplifying quantifiers",
]
})
}

if (!result.safe && options.throwErr) {
throw new Error(
`Unsafe regex (spectral radius ${radius})`,
)
throw new Error(`Unsafe regex (spectral radius ${radius})`)
}

return result
Expand Down
78 changes: 52 additions & 26 deletions packages/resafe/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class Formatter {
this.parts += `${ESC}${code}m`
return this
}

static create(text: string) {
return new Formatter(text)
}

bold() {
return this.code("1")
}
Expand All @@ -29,9 +31,21 @@ class Formatter {
red() {
return this.code("38;2;255;85;85")
}
yellow() {
return this.code("38;2;255;200;50")
}
blue() {
return this.code("38;2;100;149;237")
}
green() {
return this.code("38;2;85;255;85")
}
pastelRedBg() {
return this.code("48;2;255;85;85")
}
pastelYellowBg() {
return this.code("48;2;255;200;50")
}

toString() {
return `${this.parts}${this.text}${ESC}0m`
Expand All @@ -43,34 +57,46 @@ class Formatter {

const fmt = (text: string) => Formatter.create(text)

export const log = {
error: (msg: string, regex?: string, extra?: string[]) => {
let firstLine = `${fmt(" RESAFE ").bold().pastelRedBg().white()} ${fmt(msg).white()}`
if (regex) {
firstLine += ` ${fmt("regex").red()}${fmt("=").darkGray()}${fmt(regex).white()}`
}
stdout.write(`${firstLine}\n`)
if (extra) log.quote(extra)
},
export type LogProperty = {
name: string
value: string
color?: (fmt: Formatter) => Formatter
}

warn: (msg: string, regex?: string, extra?: string[]) => {
let firstLine = `${fmt(" RESAFE ").bold().pastelRedBg().white()} ${fmt(msg).white()}`
if (regex) {
firstLine += ` ${fmt("regex").red()}${fmt("=").darkGray()}${fmt(regex).white()}`
}
stdout.write(`${firstLine}\n`)
if (extra) log.quote(extra)
},
export type LogOptions = {
property?: LogProperty
lines?: string[]
}

function formatLogLine(
prefixBg: (f: Formatter) => Formatter,
msg: string,
options?: LogOptions,
) {
const prefix = prefixBg(fmt(" RESAFE ").bold()).white()
let line = `${prefix} ${fmt(msg).white()}`

if (options?.property) {
const prop = options.property
const nameFmt = prop.color
? prop.color(fmt(prop.name))
: fmt(prop.name).bold()
line += ` ${nameFmt}${fmt("=").darkGray()}${fmt(prop.value).white()}`
}

hint: (msg: string | string[]) => {
const lines = Array.isArray(msg) ? msg : [msg]
log.quote(lines)
},
stdout.write(`${line}\n`)

quote: (lines: string[]) => {
lines.forEach((line) => {
stdout.write(` ${fmt("│").darkGray()} ${fmt(line).white()}\n`)
})
if (options?.lines) {
for (const l of options.lines) {
stdout.write(` ${fmt("│").darkGray()} ${fmt(l).white()}\n`)
}
stdout.write("\n")
},
}
}

export const log = {
error: (msg: string, options?: LogOptions) =>
formatLogLine((f) => f.pastelRedBg(), msg, options),
warn: (msg: string, options?: LogOptions) =>
formatLogLine((f) => f.pastelYellowBg(), msg, options),
}