Public Beta
Not set

Experience

Changes how pages explain things, never what you can do.

More

Experience

Changes how pages explain things, never what you can do.

Sign inGet started

Test Report Analyst v0.1.2 · generic

Archive SHA-256 fba54e81797c6ddd3ec2a06040e9ec6a873d453b25506d5d5076e01979293da3 · 66.2 KB · built Dec 1, 2026

src/glob.js

// Glob patterns, matched without regular expressions built from user input (no ReDoS):
//   *  any characters except "/"      ?  one character except "/"
//   ** (a whole segment) zero or more folders     {a,b}  alternatives (not nested, at most 32 expansions)
// Matching is case-insensitive on every platform, so a recipe behaves the same on Windows, macOS and Linux.
import { ToolError } from "./errors.js";

const MAX_EXPANSIONS = 32;

function expandBraces(p, field) {
  const open = p.indexOf("{");
  if (open < 0) {
    if (p.includes("}")) throw new ToolError("invalid_recipe", `${field}: unmatched "}" in pattern ${p}.`);
    return [p];
  }
  const close = p.indexOf("}", open);
  if (close < 0) throw new ToolError("invalid_recipe", `${field}: unmatched "{" in pattern ${p}.`);
  const inner = p.slice(open + 1, close);
  if (inner.includes("{")) throw new ToolError("invalid_recipe", `${field}: nested {} alternatives are not supported (${p}).`);
  const out = [];
  for (const alt of inner.split(",")) {
    for (const rest of expandBraces(p.slice(0, open) + alt + p.slice(close + 1), field)) {
      out.push(rest);
      if (out.length > MAX_EXPANSIONS) throw new ToolError("invalid_recipe", `${field}: pattern ${p} expands to more than ${MAX_EXPANSIONS} alternatives.`);
    }
  }
  return out;
}

/** Wildcard match of one name (no "/"): two-pointer algorithm, O(n*m) worst case. */
export function matchSegment(pat, name) {
  const p = pat.toLowerCase();
  const s = name.toLowerCase();
  let pi = 0;
  let si = 0;
  let star = -1;
  let mark = 0;
  while (si < s.length) {
    if (pi < p.length && (p[pi] === "?" || p[pi] === s[si])) {
      pi++;
      si++;
    } else if (pi < p.length && p[pi] === "*") {
      star = pi++;
      mark = si;
    } else if (star >= 0) {
      pi = star + 1;
      si = ++mark;
    } else return false;
  }
  while (pi < p.length && p[pi] === "*") pi++;
  return pi === p.length;
}

function matchSegments(pats, segs) {
  // memo[i][j]: pats[i..] matches segs[j..]
  const memo = new Map();
  const go = (i, j) => {
    const key = i * 1000 + j;
    if (memo.has(key)) return memo.get(key);
    let r;
    if (i === pats.length) r = j === segs.length;
    else if (pats[i] === "**") r = go(i + 1, j) || (j < segs.length && go(i, j + 1));
    else r = j < segs.length && matchSegment(pats[i], segs[j]) && go(i + 1, j + 1);
    memo.set(key, r);
    return r;
  };
  return go(0, 0);
}

const hasWild = (seg) => /[*?{}]/.test(seg);

/**
 * Compiles a normalised relative pattern ("exports/*.csv", "notes/**\/*.md").
 * Returns { base, recursive, maxDepth, match(relPath) } where base is the folder to start listing from.
 */
export function compileGlob(pattern, field = "glob") {
  const alts = expandBraces(pattern, field).map((a) => a.split("/"));
  for (const segs of alts) for (const s of segs) if (s.includes("**") && s !== "**") throw new ToolError("invalid_recipe", `${field}: "**" must be a whole path segment (for example notes/**/*.md).`);
  // Static prefix shared by every alternative.
  const first = alts[0];
  let k = 0;
  while (k < first.length - 1 && !hasWild(first[k]) && alts.every((a) => a.length - 1 > k && a[k].toLowerCase() === first[k].toLowerCase())) k++;
  const base = first.slice(0, k).join("/") || ".";
  const rest = alts.map((a) => a.slice(k));
  const recursive = rest.some((r) => r.includes("**"));
  const maxDepth = recursive ? Infinity : Math.max(...rest.map((r) => r.length));
  return {
    pattern,
    base,
    recursive,
    maxDepth,
    /** relPath is relative to the workspace root. */
    match(relPath) {
      const segs = relPath.split("/");
      return alts.some((a) => matchSegments(a, segs));
    },
  };
}

export const isGlob = (p) => typeof p === "string" && /[*?{}]/.test(p);

Find an agent

Do not include passwords, keys, customer data or other confidential details.
Rule-based matching is free during the public beta (fair use).