{"id":19910983,"url":"https://github.com/streetsidesoftware/regexp-worker","last_synced_at":"2025-05-03T03:30:52.216Z","repository":{"id":36977530,"uuid":"266325517","full_name":"streetsidesoftware/regexp-worker","owner":"streetsidesoftware","description":"Runs regular expressions on a background thread.","archived":false,"fork":false,"pushed_at":"2025-04-28T02:08:44.000Z","size":1692,"stargazers_count":4,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-29T03:07:42.756Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/streetsidesoftware.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["streetsidesoftware"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2020-05-23T11:48:17.000Z","updated_at":"2025-04-20T14:25:13.000Z","dependencies_parsed_at":"2023-09-24T13:42:32.471Z","dependency_job_id":"e1758978-6678-41fb-b361-c3ad37850fb3","html_url":"https://github.com/streetsidesoftware/regexp-worker","commit_stats":{"total_commits":371,"total_committers":4,"mean_commits":92.75,"dds":"0.49056603773584906","last_synced_commit":"5c602d4ada554e779f36b8ec2b1ae3fe3b67907b"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streetsidesoftware%2Fregexp-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streetsidesoftware%2Fregexp-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streetsidesoftware%2Fregexp-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/streetsidesoftware%2Fregexp-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/streetsidesoftware","download_url":"https://codeload.github.com/streetsidesoftware/regexp-worker/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252137512,"owners_count":21700227,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-11-12T21:22:45.952Z","updated_at":"2025-05-03T03:30:51.953Z","avatar_url":"https://github.com/streetsidesoftware.png","language":"TypeScript","funding_links":["https://github.com/sponsors/streetsidesoftware"],"categories":[],"sub_categories":[],"readme":"# Regular Expression Worker\n\nExecute Regular Expression Matches on a Node [Worker Thread](https://nodejs.org/api/worker_threads.html).\n\nRegular Expressions can suffer from [Catastrophic Backtracking](https://www.regular-expressions.info/catastrophic.html). A very simple expression like `/(x+x+)+y/` can cause your JavaScript application to freeze. This library allows you to run these expressions on another thread. If they take to long to complete, they are terminated, protecting your application from locking up.\n\n## Installation\n```\nnpm install regexp-worker\n```\n\n## Basic Usage\n\nIn the example below:\n1. a new Worker thread is created\n1. the regular expression is executed on the thread\n1. the result is returned\n1. the thread is stopped\n\nFor the occasional request, this is the easiest way, but the Worker startup and shutdown is expensive.\n\n### Find the words in some text\n\n```typescript\nimport { execRegExpOnWorker } from 'regexp-worker'\n//...\nconst response = await execRegExpOnWorker(/\\b\\w+/g, 'Good Morning')\nconsole.log(response.matches.map(m =\u003e m[0]))\n```\n\nResult:\n```\n  console.log\n    [ 'Good', 'Morning' ]\n\n```\n\n### Find the word breaks in some text\n```typescript\nimport { execRegExpOnWorker } from 'regexp-worker'\n//...\nconst response = await execRegExpOnWorker(/\\b/g, 'Good Morning');\nconsole.log(response.matches.map(m =\u003e m.index))\n```\nResult:\n```\n  console.log\n    [ 0, 4, 5, 12 ]\n```\n\n### Format of the response\n\n```typescript\ninterface ExecRegExpResult {\n    elapsedTimeMs: number;\n    matches: RegExpExecArray[];\n}\n```\nWhere `RegExpExecArray` is [RegExp.prototype.exec() result](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#Description).\n\n\n## Creating a `RegExpWorker` Instance\n\nTo reduce the cost of starting and stopping the Worker, it is possible to create a `RegExpWorker` instance.\nThis instance allows you to make multiple requests using the same worker. The request are queued and handled\none at a time. If a request takes too long, it is terminated and the promise is rejected with an `ErrorCanceledRequest`.\n\n```js\nimport { RegExpWorker } from 'regexp-worker'\n\n// ...\nconst defaultTimeOutMs = 10\nconst worker = new RegExpWorker(defaultTimeOutMs);\n\n// Find all words in some text\nlet words = await worker.execRegExp(/\\b\\w+/g, 'Lots of text ...')\n\n// Find all numbers in some text\nlet numbers = await worker.execRegExp(/\\b\\d+/g, 'Lots of text ...')\n\n// Find 3 letter word pairs\nlet moreTimeMs = 100\nlet numbers = await worker.execRegExp(/\\b\\w{3}\\s+\\w{3}/g, 'Lots of text ...', moreTimeMs)\n\n// ...\n\n// It is a good idea to dispose of the worker before shutdown.\n// The worker thread will stop on its own if left idle for more than 200ms.\nworker.dispose();\n```\n\n## Handling Timeouts\n\nIf the request times out, the promise will be rejected with:\n\n```js\ninterface TimeoutError {\n    message: string;\n    elapsedTimeMs: number;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstreetsidesoftware%2Fregexp-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstreetsidesoftware%2Fregexp-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstreetsidesoftware%2Fregexp-worker/lists"}