{"name":"super-regex","version":"1.1.0","description":"Make a regular expression time out if it takes too long to execute","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/super-regex.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":{"types":"./index.d.ts","default":"./index.js"},"sideEffects":false,"engines":{"node":">=18"},"scripts":{"test":"xo && node --test && tsd","benchmark":"node benchmark/index.js"},"keywords":["regex","regexp","regular","expression","timeout","time","out","cancel","expire","abort","redos","security","script","execute","async","asynchronous","worker","non-blocking"],"dependencies":{"function-timeout":"^1.0.1","make-asynchronous":"^1.0.1","time-span":"^5.1.0"},"devDependencies":{"tsd":"^0.31.0","xo":"^0.58.0"},"gitHead":"50f4b43a9e141b86ff7025c407596b5fed8ed965","types":"./index.d.ts","_id":"super-regex@1.1.0","bugs":{"url":"https://github.com/sindresorhus/super-regex/issues"},"homepage":"https://github.com/sindresorhus/super-regex#readme","_nodeVersion":"20.19.5","_npmVersion":"11.6.1","dist":{"integrity":"sha512-WHkws2ZflZe41zj6AolvvmaTrWds/VuyeYr9iPVv/oQeaIoVxMKaushfFWpOGDT+GuBrM/sVqF8KUCYQlSSTdQ==","shasum":"14b69b6374f7b3338db52ecd511dae97c27acf75","tarball":"http://registry-npm.ywwl.com/super-regex/download/super-regex-1.1.0.tgz","fileCount":5,"unpackedSize":18640,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIEpgoChx51tNsOnKdkJ8BgvmpapJrZHceJrt0LPsC6NyAiEAy20IxA+K3/2zhDAXnpCE+kKP4ONW+9WeVELsuBqLHa4="}],"size":4942,"noattachment":false,"key":"/super-regex/-/super-regex-1.1.0.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/super-regex_1.1.0_1762248612588_0.37291710401512645"},"_hasShrinkwrap":false,"readme":"# super-regex\n\n> Make a regular expression time out if it takes too long to execute\n\nThis can be used to prevent [ReDoS vulnerabilities](https://en.wikipedia.org/wiki/ReDoS) when running a regular expression against untrusted user input.\n\nThis package also has a better API than the built-in regular expression methods. For example, none of the methods mutate the regex.\n\n**Synchronous methods** (`isMatch`, `firstMatch`, `matches`) use a timeout mechanism that only works in Node.js. In the browser, they will not time out.\n\n**Asynchronous methods** (`isMatchAsync`, `firstMatchAsync`, `matchesAsync`) run the regex in a worker thread and support timeout in both Node.js and browsers. They are especially useful for preventing ReDoS attacks in browser environments and for non-blocking execution in servers.\n\n## Install\n\n```sh\nnpm install super-regex\n```\n\n## Usage\n\n```js\nimport {isMatch} from 'super-regex';\n\nconsole.log(isMatch(/\\d+/, getUserInput(), {timeout: 1000}));\n```\n\n```js\nimport {isMatchAsync} from 'super-regex';\n\nconsole.log(await isMatchAsync(/\\d+/, getUserInput(), {timeout: 1000}));\n```\n\n## API\n\n### isMatch(regex, string, options?)\n\nReturns a boolean for whether the given `regex` matches the given `string`.\n\nIf the regex takes longer to match than the given timeout, it returns `false`.\n\n*This method is similar to [`RegExp#test`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test), but differs in that the given `regex` is [never mutated, even when it has the `/g` flag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test#using_test_on_a_regex_with_the_global_flag).*\n\n### firstMatch(regex, string, options?)\n\nReturns the first `Match` or `undefined` if there was no match.\n\nIf the regex takes longer to match than the given timeout, it returns `undefined`.\n\n### matches(regex, string, options?)\n\nReturns an iterable of `Match`es.\n\nIf the regex takes longer to match than the given timeout, it returns an empty array.\n\n**The `regex` must have the `/g` flag.**\n\n### isMatchAsync(regex, string, options?)\n\nReturns a promise that resolves to a boolean for whether the given `regex` matches the given `string`.\n\nIf the regex takes longer to match than the given timeout, it returns `false`.\n\nThis method runs the regex in a worker thread, which allows it to time out in both Node.js and browsers. This is especially useful for preventing ReDoS attacks in browser environments.\n\n*This method is similar to [`RegExp#test`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test), but differs in that the given `regex` is [never mutated, even when it has the `/g` flag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test#using_test_on_a_regex_with_the_global_flag).*\n\n```js\nimport {isMatchAsync} from 'super-regex';\n\nconsole.log(await isMatchAsync(/\\d+/, getUserInput(), {timeout: 1000}));\n```\n\n### firstMatchAsync(regex, string, options?)\n\nReturns a promise that resolves to the first match or `undefined` if there was no match.\n\nIf the regex takes longer to match than the given timeout, it returns `undefined`.\n\nThis method runs the regex in a worker thread, which allows it to time out in both Node.js and browsers. This is especially useful for preventing ReDoS attacks in browser environments.\n\n```js\nimport {firstMatchAsync} from 'super-regex';\n\nconsole.log(await firstMatchAsync(/\\d+/, getUserInput(), {timeout: 1000}));\n```\n\n### matchesAsync(regex, string, options?)\n\nReturns an async iterable of matches.\n\nIf the regex takes longer to match than the given timeout, it returns an empty iterable.\n\nThis method runs the regex in a worker thread, which allows it to time out in both Node.js and browsers. This is especially useful for preventing ReDoS attacks in browser environments.\n\n**The `regex` must have the `/g` flag.**\n\n```js\nimport {matchesAsync} from 'super-regex';\n\nfor await (const match of matchesAsync(/\\d+/g, getUserInput(), {timeout: 1000})) {\n\tconsole.log(match);\n}\n```\n\n#### options\n\nType: `object`\n\n##### timeout?\n\nType: `number` *(integer)*\n\nThe time in milliseconds to wait before timing out.\n\n##### throwOnTimeout?\n\nType: `boolean`\\\nDefault: `false`\n\nThrow a timeout error instead of returning a default value when the timeout is reached.\n\nThis lets you distinguish between “no match” and “timeout”.\n\nBy default, when a timeout occurs:\n- `isMatch()` returns `false`\n- `firstMatch()` returns `undefined`\n- `matches()` returns an empty array\n- `isMatchAsync()` returns `false`\n- `firstMatchAsync()` returns `undefined`\n- `matchesAsync()` returns an empty iterable\n\n##### matchTimeout?\n\nType: `number` *(integer)*\n\nOnly works in `matches()`.\n\nThe time in milliseconds to wait before timing out when searching for each match.\n\n### Match\n\n```ts\n{\n\tmatch: string;\n\tindex: number;\n\tgroups: string[];\n\tnamedGroups: {string: string}; // object with string values\n\tinput: string;\n}\n```\n\n## Related\n\n- [function-timeout](https://github.com/sindresorhus/function-timeout) - Make a synchronous function have a timeout\n","publish_time":1762248612779,"_cnpm_publish_time":1762248612779,"dist-tags":{"latest":"1.1.0"}}