{"id":48913837,"url":"https://github.com/stella/aho-corasick","last_synced_at":"2026-04-17T01:01:51.528Z","repository":{"id":351864526,"uuid":"1182691099","full_name":"stella/aho-corasick","owner":"stella","description":"Exact many-pattern string search for Node.js/Bun via Rust aho-corasick. Fast string, buffer, and stream APIs.","archived":false,"fork":false,"pushed_at":"2026-04-16T21:14:16.000Z","size":3672,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-16T21:27:01.822Z","etag":null,"topics":["aho-corasick","multi-pattern-search","napi-rs","string-matching"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stella.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-15T21:06:17.000Z","updated_at":"2026-04-16T21:14:24.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/stella/aho-corasick","commit_stats":null,"previous_names":["stella/aho-corasick"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/stella/aho-corasick","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stella%2Faho-corasick","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stella%2Faho-corasick/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stella%2Faho-corasick/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stella%2Faho-corasick/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stella","download_url":"https://codeload.github.com/stella/aho-corasick/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stella%2Faho-corasick/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31909237,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T18:22:33.417Z","status":"ssl_error","status_checked_at":"2026-04-16T18:21:47.142Z","response_time":69,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aho-corasick","multi-pattern-search","napi-rs","string-matching"],"created_at":"2026-04-17T01:01:08.185Z","updated_at":"2026-04-17T01:01:51.517Z","avatar_url":"https://github.com/stella.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\".github/assets/banner.png\" alt=\"Stella\" width=\"100%\" /\u003e\n\u003c/p\u003e\n\n# @stll/aho-corasick\n\n[NAPI-RS](https://napi.rs/) bindings to the Rust\n[aho-corasick](https://github.com/BurntSushi/aho-corasick)\ncrate for Node.js and Bun.\n\nMulti-pattern string searching in linear time.\nBuilt on\n[BurntSushi's aho-corasick](https://github.com/BurntSushi/aho-corasick)\n(the same engine that powers\n[ripgrep](https://github.com/BurntSushi/ripgrep)),\nexposed to JavaScript via\n[NAPI-RS](https://github.com/napi-rs/napi-rs).\n\n## Install\n\n```bash\nnpm install @stll/aho-corasick\n# or\nbun add @stll/aho-corasick\n```\n\nThe companion `@stll/aho-corasick-wasm` package is\navailable for browser builds.\n\nIf you use the browser package with Vite, import the\nbundled plugin so the generated WASM loader is not\npre-bundled into broken asset URLs:\n\n```typescript\nimport { defineConfig } from \"vite\";\nimport stllAhoCorasickWasm from \"@stll/aho-corasick-wasm/vite\";\n\nexport default defineConfig({\n  plugins: [stllAhoCorasickWasm()],\n});\n```\n\nGitHub releases include npm tarballs, an SBOM, and\nthird-party notices.\n\nPrebuilts are available for:\n\n| Platform      | Architecture |\n| ------------- | ------------ |\n| macOS         | x64, arm64   |\n| Linux (glibc) | x64, arm64   |\n| WASM          | browser      |\n\n## Usage\n\n```typescript\nimport { AhoCorasick } from \"@stll/aho-corasick\";\n\nconst ac = new AhoCorasick([\"foo\", \"bar\", \"baz\"]);\n\n// Check for any match\nac.isMatch(\"hello foo world\"); // true\n\n// Find all non-overlapping matches\nac.findIter(\"foo bar baz\");\n// [\n//   { pattern: 0, start: 0, end: 3, text: \"foo\" },\n//   { pattern: 1, start: 4, end: 7, text: \"bar\" },\n//   { pattern: 2, start: 8, end: 11, text: \"baz\" },\n// ]\n\n// Find overlapping matches\nac.findOverlappingIter(\"foobar\");\n\n// Replace matches\nac.replaceAll(\"foo bar\", [\"FOO\", \"BAR\", \"BAZ\"]);\n// \"FOO BAR\"\n```\n\n### Options\n\n```typescript\nconst ac = new AhoCorasick(patterns, {\n  // Match semantics (default: \"leftmost-first\")\n  matchKind: \"leftmost-longest\",\n  // Unicode simple case folding (default: false)\n  caseInsensitive: true,\n  // Only match whole words (default: false)\n  // Unicode-aware; CJK always passes\n  wholeWords: true,\n});\n```\n\n### Streaming\n\nFor processing large files or streams chunk by\nchunk:\n\n```typescript\nimport { StreamMatcher } from \"@stll/aho-corasick\";\n\nconst sm = new StreamMatcher([\"needle\", \"haystack\"]);\n\nfor await (const chunk of readableStream) {\n  const matches = sm.write(chunk);\n  for (const m of matches) {\n    console.log(\n      `Pattern ${m.pattern} ` + `at ${m.start}..${m.end}`,\n    );\n  }\n}\n\nsm.flush(); // finalize stream state\nsm.reset(); // reuse for another stream\n```\n\n`StreamMatcher` automatically handles overlap\nbetween chunks so that matches spanning chunk\nboundaries are found.\n\n### Groups\n\nTo organize patterns into named groups (e.g., for\nentity recognition), use the `pattern` index as a\nlookup key into a parallel array:\n\n```typescript\nconst GROUPS = {\n  LEGAL_FORM: [\"s.r.o.\", \"GmbH\", \"LLC\"],\n  CURRENCY: [\"EUR\", \"USD\", \"CZK\"],\n};\n\nconst patterns: string[] = [];\nconst tag: string[] = [];\n\nfor (const [group, terms] of Object.entries(GROUPS)) {\n  for (const term of terms) {\n    patterns.push(term);\n    tag.push(group);\n  }\n}\n\nconst ac = new AhoCorasick(patterns, {\n  wholeWords: true,\n});\n\nfor (const m of ac.findIter(text)) {\n  console.log(m.text, tag[m.pattern]);\n  // \"GmbH\" \"LEGAL_FORM\"\n  // \"EUR\"  \"CURRENCY\"\n}\n```\n\n`tag[m.pattern]` is a single array index lookup\n(O(1), no hashing).\n\n## Benchmarks\n\nThe repository includes a checked-in benchmark harness\nfor ASCII, Unicode, and WASM cases. The inputs are\npublic and the scripts are reproducible from the\nrepo. Run it locally:\n\n```bash\nbun run bench:install\nbun run bench:download\nbun run bench:all\n```\n\nThe harness compares multiple JS/TS implementations on\npublic corpora and verifies equal match counts across\nlibraries. The speed suite is anchored in the\nmany-pattern exact-search workloads where\nAho-Corasick is meant to win, rather than single-call\ntoy regex cases.\n\nRepresentative baseline from the checked-in public\nharness on this machine:\n- runtime: Bun `1.3.12`\n- platform: macOS `26.4.1` (`Darwin arm64`)\n\n| Scenario                            | `@stll/aho-corasick` | Best compared JS/TS result | Relative |\n| ----------------------------------- | -------------------- | -------------------------- | -------- |\n| `bible.txt`, `4.0 MB`, `20` terms   | `2.27 ms`            | `69.25 ms`                 | `30.5x`  |\n| `world192.txt`, `2.5 MB`, `20` terms| `1.55 ms`            | `41.88 ms`                 | `26.9x`  |\n| `E.coli`, `4.6 MB`, `16` codons     | `0.83 ms`            | `11.00 ms`                 | `13.3x`  |\n| `bible.txt`, single-pattern baseline| `0.67 ms`            | `20.10 ms`                 | `29.9x`  |\n\n\u003cdetails\u003e\n\u003csummary\u003eAlternatives tested\u003c/summary\u003e\n\n- [modern-ahocorasick](https://www.npmjs.com/package/modern-ahocorasick) — pure JS, ESM/CJS\n- [ahocorasick](https://www.npmjs.com/package/ahocorasick) — pure JS\n- [@monyone/aho-corasick](https://www.npmjs.com/package/@monyone/aho-corasick) — pure TS\n- [@tanishiking/aho-corasick](https://www.npmjs.com/package/@tanishiking/aho-corasick) — pure TS\n\n\u003c/details\u003e\n\n## API\n\n### `AhoCorasick`\n\n| Method                                | Returns       | Description                                         |\n| ------------------------------------- | ------------- | --------------------------------------------------- |\n| `new AhoCorasick(patterns, options?)` | instance      | Build automaton                                     |\n| `.findIter(haystack)`                 | `Match[]`     | Non-overlapping matches                             |\n| `.findOverlappingIter(haystack)`      | `Match[]`     | All overlapping matches                             |\n| `.isMatch(haystack)`                  | `boolean`     | Any pattern matches?                                |\n| `.replaceAll(haystack, replacements)` | `string`      | Replace matched patterns                            |\n| `.findIterBuf(haystack)`              | `ByteMatch[]` | Matches in a `Buffer` / `Uint8Array` (byte offsets) |\n| `.isMatchBuf(haystack)`               | `boolean`     | Any pattern matches in a `Buffer` / `Uint8Array`?   |\n| `.patternCount`                       | `number`      | Number of patterns                                  |\n\n### `StreamMatcher`\n\n| Method                                  | Returns       | Description                                |\n| --------------------------------------- | ------------- | ------------------------------------------ |\n| `new StreamMatcher(patterns, options?)` | instance      | Build streaming matcher                    |\n| `.write(chunk)`                         | `ByteMatch[]` | Feed chunk, get global byte-offset matches |\n| `.flush()`                              | `ByteMatch[]` | Finalize stream                            |\n| `.reset()`                              | `void`        | Reset for reuse                            |\n\n### Types\n\n```typescript\ntype MatchKind = \"leftmost-first\" | \"leftmost-longest\";\n\ntype Options = {\n  matchKind?: MatchKind;\n  caseInsensitive?: boolean;\n  wholeWords?: boolean;\n  dfa?: boolean;\n};\n\n// Returned by string methods (findIter, etc.)\ntype Match = {\n  pattern: number; // index into patterns array\n  start: number; // UTF-16 code unit offset\n  end: number; // exclusive\n  text: string; // matched substring\n};\n\n// Returned by Buffer/streaming methods\ntype ByteMatch = {\n  pattern: number; // index into patterns array\n  start: number; // byte offset\n  end: number; // exclusive\n};\n```\n\n### Error handling\n\n- `new AhoCorasick([...])` throws if the\n  automaton cannot be built (e.g. patterns exceed\n  internal size limits).\n- `replaceAll(haystack, replacements)` throws if\n  `replacements.length !== patternCount`.\n- Empty patterns arrays are valid; all search\n  methods return no matches.\n\n## Limitations\n\n- **Case insensitivity uses Unicode simple case\n  folding, not locale-specific collation.** It\n  handles one-to-one folds like Turkish `İ -\u003e i`\n  and `ẞ -\u003e ß`, but it does not perform full\n  multi-character expansions such as `ß -\u003e ss`.\n- **WASM requires `SharedArrayBuffer`.** Browser\n  builds need `Cross-Origin-Opener-Policy: same-origin`\n  and `Cross-Origin-Embedder-Policy: require-corp`\n  headers. Edge runtimes without WASM support\n  (some Cloudflare Workers configurations) are\n  not supported.\n\n## Acknowledgements\n\nThis package is a thin binding layer. The hard work\nis done by:\n\n- [**aho-corasick**](https://github.com/BurntSushi/aho-corasick)\n  by Andrew Gallant (BurntSushi) — the Rust\n  implementation of the Aho-Corasick algorithm.\n  MIT licensed.\n- [**NAPI-RS**](https://github.com/napi-rs/napi-rs)\n  — the Rust framework for building Node.js native\n  addons. MIT licensed.\n\n## Development\n\n```bash\n# Install dependencies\nbun install\n\n# Build native module (requires Rust toolchain)\nbun run build\n\n# Run tests (113 tests, including Unicode edge cases)\nbun test\n\n# Download benchmark corpora\nbun run bench:download\n\n# Install benchmark dependencies (alternatives)\nbun run bench:install\n\n# Run benchmarks\nbun run bench:speed       # Canterbury corpus\nbun run bench:unicode     # Leipzig corpora\nbun run bench:correctness # cross-library comparison\nbun run bench:all         # all three\n\n# Lint \u0026 format\nbun run lint\nbun run format\n```\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstella%2Faho-corasick","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstella%2Faho-corasick","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstella%2Faho-corasick/lists"}