re2js 2.8.2 → 2.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -5
- package/build/index.cjs +8 -5
- package/build/index.d.cts +5 -2
- package/build/index.d.ts +5 -2
- package/build/index.js +8 -5
- package/build/index.umd.js +8 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -264,7 +264,7 @@ This is incredibly powerful for profanity filters, routing engines, or log parse
|
|
|
264
264
|
```js
|
|
265
265
|
import { RE2Set } from 're2js'
|
|
266
266
|
|
|
267
|
-
// Create a new set. You can optionally pass an anchor
|
|
267
|
+
// Create a new set. You can optionally pass an anchor, public RE2JS flags, and a max memory limit for the DFA.
|
|
268
268
|
// Default anchor: RE2Set.UNANCHORED
|
|
269
269
|
const set = new RE2Set()
|
|
270
270
|
|
|
@@ -286,17 +286,22 @@ console.log(set.match('All systems operational.'))
|
|
|
286
286
|
// Outputs: []
|
|
287
287
|
```
|
|
288
288
|
|
|
289
|
-
#### Anchoring a Set
|
|
289
|
+
#### Anchoring a Set and Memory Limits
|
|
290
290
|
|
|
291
291
|
You can strictly anchor the entire set by passing an anchor constant to the constructor (`RE2Set.UNANCHORED`, `RE2Set.ANCHOR_START`, or `RE2Set.ANCHOR_BOTH`).
|
|
292
292
|
|
|
293
|
-
|
|
293
|
+
You can pass standard public `RE2JS` flags (like `CASE_INSENSITIVE` or `LOOKBEHINDS`) as the second argument to apply them to all patterns in the set.
|
|
294
|
+
|
|
295
|
+
Additionally, the third argument allows you to specify a `maxMem` limit (in bytes) to prevent the underlying DFA from exploding and consuming too much memory on highly ambiguous patterns.
|
|
296
|
+
The default is 8 * 1024 * 1024 (8MB).
|
|
294
297
|
|
|
295
298
|
```js
|
|
296
299
|
import { RE2Set, RE2JS } from 're2js'
|
|
297
300
|
|
|
298
|
-
// Anchor the set to match the entire string,
|
|
299
|
-
|
|
301
|
+
// Anchor the set to match the entire string, make it case-insensitive,
|
|
302
|
+
// and limit the DFA memory usage to ~4MB (default is 8MB).
|
|
303
|
+
const maxMem = 4 * 1024 * 1024;
|
|
304
|
+
const set = new RE2Set(RE2Set.ANCHOR_BOTH, RE2JS.CASE_INSENSITIVE, maxMem)
|
|
300
305
|
|
|
301
306
|
set.add('foo') // ID: 0
|
|
302
307
|
set.add('bar') // ID: 1
|
package/build/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* re2js
|
|
3
3
|
* RE2JS is the JavaScript port of RE2, a regular expression engine that provides linear time matching
|
|
4
4
|
*
|
|
5
|
-
* @version v2.8.
|
|
5
|
+
* @version v2.8.3
|
|
6
6
|
* @author Oleksii Vasyliev
|
|
7
7
|
* @homepage https://github.com/le0pard/re2js#readme
|
|
8
8
|
* @repository github:le0pard/re2js
|
|
@@ -2208,12 +2208,13 @@ var DFAState = class {
|
|
|
2208
2208
|
};
|
|
2209
2209
|
var DFA = class DFA {
|
|
2210
2210
|
static MAX_CACHE_CLEARS = 5;
|
|
2211
|
-
|
|
2211
|
+
static STATE_MEMORY_ESTIMATE = 838;
|
|
2212
|
+
constructor(prog, maxMem = 8388608) {
|
|
2212
2213
|
this.prog = prog;
|
|
2213
2214
|
this.stateCache = /* @__PURE__ */ new Map();
|
|
2214
2215
|
this.stateCount = 0;
|
|
2215
2216
|
this.startState = null;
|
|
2216
|
-
this.stateLimit =
|
|
2217
|
+
this.stateLimit = Math.max(1, Math.floor(maxMem / DFA.STATE_MEMORY_ESTIMATE));
|
|
2217
2218
|
this.cacheClears = 0;
|
|
2218
2219
|
this.failed = false;
|
|
2219
2220
|
this.clock = 0;
|
|
@@ -5900,10 +5901,12 @@ var RE2Set = class RE2Set {
|
|
|
5900
5901
|
* Constructs a new RE2Set with the specified anchor mode and flags.
|
|
5901
5902
|
* @param {number} [anchor=RE2Set.UNANCHORED] - The anchoring mode (e.g., RE2Set.UNANCHORED).
|
|
5902
5903
|
* @param {number} [flags=0] - The public flags to apply to all patterns in the set.
|
|
5904
|
+
* @param {number} [maxMem=8388608] - The maximum memory in bytes to use for the DFA (default 8MB).
|
|
5903
5905
|
*/
|
|
5904
|
-
constructor(anchor = RE2Set.UNANCHORED, flags = 0) {
|
|
5906
|
+
constructor(anchor = RE2Set.UNANCHORED, flags = 0, maxMem = 8388608) {
|
|
5905
5907
|
this.anchor = anchor;
|
|
5906
5908
|
this.jsFlags = flags;
|
|
5909
|
+
this.maxMem = maxMem;
|
|
5907
5910
|
let re2Flags = RE2Flags.PERL;
|
|
5908
5911
|
if ((flags & PublicFlags.DISABLE_UNICODE_GROUPS) !== 0) re2Flags &= ~RE2Flags.UNICODE_GROUPS;
|
|
5909
5912
|
if ((flags & PublicFlags.LOOKBEHINDS) !== 0) re2Flags |= RE2Flags.LOOKBEHIND;
|
|
@@ -5938,7 +5941,7 @@ var RE2Set = class RE2Set {
|
|
|
5938
5941
|
compile() {
|
|
5939
5942
|
if (this.prog) return;
|
|
5940
5943
|
this.prog = Compiler.compileSet(this.regexps);
|
|
5941
|
-
this.dfa = new DFA(this.prog);
|
|
5944
|
+
this.dfa = new DFA(this.prog, this.maxMem);
|
|
5942
5945
|
this.dummyRe2 = {
|
|
5943
5946
|
prog: this.prog,
|
|
5944
5947
|
cond: this.prog.startCond(),
|
package/build/index.d.cts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
declare class DFA {
|
|
4
4
|
static MAX_CACHE_CLEARS: number;
|
|
5
|
-
|
|
5
|
+
static STATE_MEMORY_ESTIMATE: number;
|
|
6
|
+
constructor(prog: any, maxMem?: number);
|
|
6
7
|
prog: any;
|
|
7
8
|
stateCache: Map<any, any>;
|
|
8
9
|
stateCount: number;
|
|
@@ -53,10 +54,12 @@ export class RE2Set {
|
|
|
53
54
|
* Constructs a new RE2Set with the specified anchor mode and flags.
|
|
54
55
|
* @param {number} [anchor=RE2Set.UNANCHORED] - The anchoring mode (e.g., RE2Set.UNANCHORED).
|
|
55
56
|
* @param {number} [flags=0] - The public flags to apply to all patterns in the set.
|
|
57
|
+
* @param {number} [maxMem=8388608] - The maximum memory in bytes to use for the DFA (default 8MB).
|
|
56
58
|
*/
|
|
57
|
-
constructor(anchor?: number, flags?: number);
|
|
59
|
+
constructor(anchor?: number, flags?: number, maxMem?: number);
|
|
58
60
|
anchor: number;
|
|
59
61
|
jsFlags: number;
|
|
62
|
+
maxMem: number;
|
|
60
63
|
re2Flags: number;
|
|
61
64
|
regexps: any[];
|
|
62
65
|
prog: Prog;
|
package/build/index.d.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
declare class DFA {
|
|
4
4
|
static MAX_CACHE_CLEARS: number;
|
|
5
|
-
|
|
5
|
+
static STATE_MEMORY_ESTIMATE: number;
|
|
6
|
+
constructor(prog: any, maxMem?: number);
|
|
6
7
|
prog: any;
|
|
7
8
|
stateCache: Map<any, any>;
|
|
8
9
|
stateCount: number;
|
|
@@ -53,10 +54,12 @@ export class RE2Set {
|
|
|
53
54
|
* Constructs a new RE2Set with the specified anchor mode and flags.
|
|
54
55
|
* @param {number} [anchor=RE2Set.UNANCHORED] - The anchoring mode (e.g., RE2Set.UNANCHORED).
|
|
55
56
|
* @param {number} [flags=0] - The public flags to apply to all patterns in the set.
|
|
57
|
+
* @param {number} [maxMem=8388608] - The maximum memory in bytes to use for the DFA (default 8MB).
|
|
56
58
|
*/
|
|
57
|
-
constructor(anchor?: number, flags?: number);
|
|
59
|
+
constructor(anchor?: number, flags?: number, maxMem?: number);
|
|
58
60
|
anchor: number;
|
|
59
61
|
jsFlags: number;
|
|
62
|
+
maxMem: number;
|
|
60
63
|
re2Flags: number;
|
|
61
64
|
regexps: any[];
|
|
62
65
|
prog: Prog;
|
package/build/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* re2js
|
|
3
3
|
* RE2JS is the JavaScript port of RE2, a regular expression engine that provides linear time matching
|
|
4
4
|
*
|
|
5
|
-
* @version v2.8.
|
|
5
|
+
* @version v2.8.3
|
|
6
6
|
* @author Oleksii Vasyliev
|
|
7
7
|
* @homepage https://github.com/le0pard/re2js#readme
|
|
8
8
|
* @repository github:le0pard/re2js
|
|
@@ -2207,12 +2207,13 @@ var DFAState = class {
|
|
|
2207
2207
|
};
|
|
2208
2208
|
var DFA = class DFA {
|
|
2209
2209
|
static MAX_CACHE_CLEARS = 5;
|
|
2210
|
-
|
|
2210
|
+
static STATE_MEMORY_ESTIMATE = 838;
|
|
2211
|
+
constructor(prog, maxMem = 8388608) {
|
|
2211
2212
|
this.prog = prog;
|
|
2212
2213
|
this.stateCache = /* @__PURE__ */ new Map();
|
|
2213
2214
|
this.stateCount = 0;
|
|
2214
2215
|
this.startState = null;
|
|
2215
|
-
this.stateLimit =
|
|
2216
|
+
this.stateLimit = Math.max(1, Math.floor(maxMem / DFA.STATE_MEMORY_ESTIMATE));
|
|
2216
2217
|
this.cacheClears = 0;
|
|
2217
2218
|
this.failed = false;
|
|
2218
2219
|
this.clock = 0;
|
|
@@ -5899,10 +5900,12 @@ var RE2Set = class RE2Set {
|
|
|
5899
5900
|
* Constructs a new RE2Set with the specified anchor mode and flags.
|
|
5900
5901
|
* @param {number} [anchor=RE2Set.UNANCHORED] - The anchoring mode (e.g., RE2Set.UNANCHORED).
|
|
5901
5902
|
* @param {number} [flags=0] - The public flags to apply to all patterns in the set.
|
|
5903
|
+
* @param {number} [maxMem=8388608] - The maximum memory in bytes to use for the DFA (default 8MB).
|
|
5902
5904
|
*/
|
|
5903
|
-
constructor(anchor = RE2Set.UNANCHORED, flags = 0) {
|
|
5905
|
+
constructor(anchor = RE2Set.UNANCHORED, flags = 0, maxMem = 8388608) {
|
|
5904
5906
|
this.anchor = anchor;
|
|
5905
5907
|
this.jsFlags = flags;
|
|
5908
|
+
this.maxMem = maxMem;
|
|
5906
5909
|
let re2Flags = RE2Flags.PERL;
|
|
5907
5910
|
if ((flags & PublicFlags.DISABLE_UNICODE_GROUPS) !== 0) re2Flags &= ~RE2Flags.UNICODE_GROUPS;
|
|
5908
5911
|
if ((flags & PublicFlags.LOOKBEHINDS) !== 0) re2Flags |= RE2Flags.LOOKBEHIND;
|
|
@@ -5937,7 +5940,7 @@ var RE2Set = class RE2Set {
|
|
|
5937
5940
|
compile() {
|
|
5938
5941
|
if (this.prog) return;
|
|
5939
5942
|
this.prog = Compiler.compileSet(this.regexps);
|
|
5940
|
-
this.dfa = new DFA(this.prog);
|
|
5943
|
+
this.dfa = new DFA(this.prog, this.maxMem);
|
|
5941
5944
|
this.dummyRe2 = {
|
|
5942
5945
|
prog: this.prog,
|
|
5943
5946
|
cond: this.prog.startCond(),
|
package/build/index.umd.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* re2js
|
|
3
3
|
* RE2JS is the JavaScript port of RE2, a regular expression engine that provides linear time matching
|
|
4
4
|
*
|
|
5
|
-
* @version v2.8.
|
|
5
|
+
* @version v2.8.3
|
|
6
6
|
* @author Oleksii Vasyliev
|
|
7
7
|
* @homepage https://github.com/le0pard/re2js#readme
|
|
8
8
|
* @repository github:le0pard/re2js
|
|
@@ -2211,12 +2211,13 @@
|
|
|
2211
2211
|
};
|
|
2212
2212
|
var DFA = class DFA {
|
|
2213
2213
|
static MAX_CACHE_CLEARS = 5;
|
|
2214
|
-
|
|
2214
|
+
static STATE_MEMORY_ESTIMATE = 838;
|
|
2215
|
+
constructor(prog, maxMem = 8388608) {
|
|
2215
2216
|
this.prog = prog;
|
|
2216
2217
|
this.stateCache = /* @__PURE__ */ new Map();
|
|
2217
2218
|
this.stateCount = 0;
|
|
2218
2219
|
this.startState = null;
|
|
2219
|
-
this.stateLimit =
|
|
2220
|
+
this.stateLimit = Math.max(1, Math.floor(maxMem / DFA.STATE_MEMORY_ESTIMATE));
|
|
2220
2221
|
this.cacheClears = 0;
|
|
2221
2222
|
this.failed = false;
|
|
2222
2223
|
this.clock = 0;
|
|
@@ -5903,10 +5904,12 @@
|
|
|
5903
5904
|
* Constructs a new RE2Set with the specified anchor mode and flags.
|
|
5904
5905
|
* @param {number} [anchor=RE2Set.UNANCHORED] - The anchoring mode (e.g., RE2Set.UNANCHORED).
|
|
5905
5906
|
* @param {number} [flags=0] - The public flags to apply to all patterns in the set.
|
|
5907
|
+
* @param {number} [maxMem=8388608] - The maximum memory in bytes to use for the DFA (default 8MB).
|
|
5906
5908
|
*/
|
|
5907
|
-
constructor(anchor = RE2Set.UNANCHORED, flags = 0) {
|
|
5909
|
+
constructor(anchor = RE2Set.UNANCHORED, flags = 0, maxMem = 8388608) {
|
|
5908
5910
|
this.anchor = anchor;
|
|
5909
5911
|
this.jsFlags = flags;
|
|
5912
|
+
this.maxMem = maxMem;
|
|
5910
5913
|
let re2Flags = RE2Flags.PERL;
|
|
5911
5914
|
if ((flags & PublicFlags.DISABLE_UNICODE_GROUPS) !== 0) re2Flags &= ~RE2Flags.UNICODE_GROUPS;
|
|
5912
5915
|
if ((flags & PublicFlags.LOOKBEHINDS) !== 0) re2Flags |= RE2Flags.LOOKBEHIND;
|
|
@@ -5941,7 +5944,7 @@
|
|
|
5941
5944
|
compile() {
|
|
5942
5945
|
if (this.prog) return;
|
|
5943
5946
|
this.prog = Compiler.compileSet(this.regexps);
|
|
5944
|
-
this.dfa = new DFA(this.prog);
|
|
5947
|
+
this.dfa = new DFA(this.prog, this.maxMem);
|
|
5945
5948
|
this.dummyRe2 = {
|
|
5946
5949
|
prog: this.prog,
|
|
5947
5950
|
cond: this.prog.startCond(),
|