1
0

matcher.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * Matcher shared by both hook dialects. Claude treats alphanumeric/underscore/
  3. * pipe patterns as literal alternatives and other patterns as regex; Codex
  4. * treats every non-empty pattern as an unanchored regex. Missing, empty, and
  5. * `*` match all. Runtime matching contains invalid regexes as non-matches;
  6. * config parsers use {@link matcherDiagnostic} to reject them with a diagnostic.
  7. * @module @deepseek-ai/dsh-hook-protocol/matcher
  8. */
  9. import type { MatcherMode } from './types.ts'
  10. /** True for an absent / empty / `'*'` pattern — the match-all sentinels. */
  11. function isMatchAll(matcher: string | undefined): boolean {
  12. return matcher === undefined || matcher === '' || matcher === '*'
  13. }
  14. /** A Claude-literal pattern is purely word chars + `|` (the regex-vs-literal discriminator). */
  15. const CLAUDE_LITERAL = /^[A-Za-z0-9_|]+$/
  16. /** Compile an unanchored matcher regex; invalid patterns return `undefined`. */
  17. function compileRegex(pattern: string): RegExp | undefined {
  18. try {
  19. return new RegExp(pattern)
  20. } catch (_syntaxError) {
  21. // RegExp construction is the try's only operation, so malformed pattern
  22. // syntax is the only expected failure.
  23. return undefined
  24. }
  25. }
  26. /**
  27. * Validate one matcher before a bridge accepts its config group.
  28. * @param matcher - configured pattern; match-all sentinels are valid.
  29. * @param mode - dialect deciding whether a word-and-pipe pattern is literal.
  30. * @returns `undefined` for a valid matcher, otherwise a stable diagnostic.
  31. */
  32. export function matcherDiagnostic(matcher: string | undefined, mode: MatcherMode): string | undefined {
  33. if (isMatchAll(matcher)) return undefined
  34. const pattern = matcher as string
  35. if (mode === 'claude-code' && CLAUDE_LITERAL.test(pattern)) return undefined
  36. return compileRegex(pattern) === undefined
  37. ? `invalid ${mode} regex matcher ${JSON.stringify(pattern)}`
  38. : undefined
  39. }
  40. /**
  41. * Whether `matcher` selects `query` under the given dialect. Claude literal
  42. * patterns exact-match pipe-separated alternatives; all other patterns are
  43. * unanchored regexes. Invalid regexes return `false` rather than throwing;
  44. * bridge config parsers surface them through {@link matcherDiagnostic} before use.
  45. * @param matcher - the configured pattern; absent/empty/`'*'` are the match-all sentinels.
  46. * @param query - the candidate value (a tool name, a session source, …).
  47. * @param mode - the dialect deciding literal-vs-regex interpretation of the pattern.
  48. * @returns `true` when the pattern selects the query; `false` on a non-match or an invalid
  49. * regex.
  50. */
  51. export function matchesMatcher(matcher: string | undefined, query: string, mode: MatcherMode): boolean {
  52. if (isMatchAll(matcher)) return true
  53. // matcher is a non-empty string past the match-all guard.
  54. const pattern = matcher as string
  55. if (mode === 'claude-code' && CLAUDE_LITERAL.test(pattern)) {
  56. return pattern.split('|').includes(query)
  57. }
  58. return compileRegex(pattern)?.test(query) ?? false
  59. }