@wrongstack/primitives 0.316.2 → 0.317.2

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/dist/index.js CHANGED
@@ -12,28 +12,41 @@ var DANGEROUS_PATTERNS = [
12
12
  /\(\?<?[!=][^)]*[+*][^)]*\)/
13
13
  ];
14
14
  function hasAmbiguousQuantifiedAlternation(pattern) {
15
+ let outerInClass = false;
15
16
  for (let i = 0; i < pattern.length; i++) {
16
- if (pattern[i] !== "(") continue;
17
- if (i > 0 && pattern[i - 1] === "\\") continue;
17
+ const ch = pattern[i];
18
+ if (ch === "\\") {
19
+ i++;
20
+ continue;
21
+ }
22
+ if (outerInClass) {
23
+ if (ch === "]") outerInClass = false;
24
+ continue;
25
+ }
26
+ if (ch === "[") {
27
+ outerInClass = true;
28
+ continue;
29
+ }
30
+ if (ch !== "(") continue;
18
31
  let depth = 0;
19
32
  let inClass = false;
20
33
  let j = i;
21
34
  for (; j < pattern.length; j++) {
22
- const ch = pattern[j];
23
- if (ch === "\\") {
35
+ const ch2 = pattern[j];
36
+ if (ch2 === "\\") {
24
37
  j++;
25
38
  continue;
26
39
  }
27
40
  if (inClass) {
28
- if (ch === "]") inClass = false;
41
+ if (ch2 === "]") inClass = false;
29
42
  continue;
30
43
  }
31
- if (ch === "[") {
44
+ if (ch2 === "[") {
32
45
  inClass = true;
33
46
  continue;
34
47
  }
35
- if (ch === "(") depth++;
36
- else if (ch === ")") {
48
+ if (ch2 === "(") depth++;
49
+ else if (ch2 === ")") {
37
50
  depth--;
38
51
  if (depth === 0) break;
39
52
  }
@@ -48,30 +61,30 @@ function hasAmbiguousQuantifiedAlternation(pattern) {
48
61
  let d = 0;
49
62
  let cls = false;
50
63
  for (let k = 0; k < inner.length; k++) {
51
- const ch = inner[k];
52
- if (ch === "\\") {
53
- current += ch + (inner[k + 1] ?? "");
64
+ const ch2 = inner[k];
65
+ if (ch2 === "\\") {
66
+ current += ch2 + (inner[k + 1] ?? "");
54
67
  k++;
55
68
  continue;
56
69
  }
57
70
  if (cls) {
58
- if (ch === "]") cls = false;
59
- current += ch;
71
+ if (ch2 === "]") cls = false;
72
+ current += ch2;
60
73
  continue;
61
74
  }
62
- if (ch === "[") {
75
+ if (ch2 === "[") {
63
76
  cls = true;
64
- current += ch;
77
+ current += ch2;
65
78
  continue;
66
79
  }
67
- if (ch === "(") d++;
68
- if (ch === ")") d--;
69
- if (ch === "|" && d === 0) {
80
+ if (ch2 === "(") d++;
81
+ if (ch2 === ")") d--;
82
+ if (ch2 === "|" && d === 0) {
70
83
  branches.push(current);
71
84
  current = "";
72
85
  continue;
73
86
  }
74
- current += ch;
87
+ current += ch2;
75
88
  }
76
89
  branches.push(current);
77
90
  if (branches.length < 2) continue;
@@ -86,10 +99,30 @@ function hasAmbiguousQuantifiedAlternation(pattern) {
86
99
  }
87
100
  return false;
88
101
  }
89
- function compileUserRegex(pattern, flags) {
102
+ var COMPILED_CACHE = /* @__PURE__ */ new Map();
103
+ var CACHE_MAX_SIZE = 500;
104
+ function compileUserRegex(pattern, flags = "") {
90
105
  if (typeof pattern !== "string") {
91
106
  return { ok: false, reason: "pattern must be a string" };
92
107
  }
108
+ const cacheKey = `${flags}\0${pattern}`;
109
+ const cached = COMPILED_CACHE.get(cacheKey);
110
+ if (cached !== void 0) {
111
+ return cached.ok ? { ok: true, regex: new RegExp(pattern, flags) } : cached;
112
+ }
113
+ if (COMPILED_CACHE.size >= CACHE_MAX_SIZE) {
114
+ let evicted = 0;
115
+ const target = Math.floor(CACHE_MAX_SIZE / 4);
116
+ for (const key of COMPILED_CACHE.keys()) {
117
+ COMPILED_CACHE.delete(key);
118
+ if (++evicted >= target) break;
119
+ }
120
+ }
121
+ const result = compileUncached(pattern, flags);
122
+ COMPILED_CACHE.set(cacheKey, result);
123
+ return result.ok ? { ok: true, regex: new RegExp(pattern, flags) } : result;
124
+ }
125
+ function compileUncached(pattern, flags) {
93
126
  if (pattern.length === 0) {
94
127
  return { ok: false, reason: "pattern is empty" };
95
128
  }
@@ -144,7 +177,7 @@ function systemSessionId(actor) {
144
177
  return `${SYSTEM_SESSION_PREFIX}${actor}`;
145
178
  }
146
179
  function isSystemSessionId(sessionId) {
147
- return sessionId.startsWith(SYSTEM_SESSION_PREFIX);
180
+ return typeof sessionId === "string" && sessionId.startsWith(SYSTEM_SESSION_PREFIX);
148
181
  }
149
182
 
150
183
  // src/time.ts
@@ -30,7 +30,7 @@ export interface CompileFail {
30
30
  reason: string;
31
31
  }
32
32
  export type CompileUserRegexResult = CompileResult | CompileFail;
33
- export declare function compileUserRegex(pattern: string, flags: string): CompileResult | CompileFail;
33
+ export declare function compileUserRegex(pattern: string, flags?: string): CompileResult | CompileFail;
34
34
  /**
35
35
  * Maximum subject length handed to a user-supplied regex before matching.
36
36
  * A linear-time pattern over a multi-megabyte line is still a stall; tools
@@ -25,5 +25,5 @@ export declare const SYSTEM_SESSION_PREFIX: 'system:';
25
25
  */
26
26
  export declare function systemSessionId(actor: string): string;
27
27
  /** Whether this id names a daemon rather than a user session. */
28
- export declare function isSystemSessionId(sessionId: string): boolean;
28
+ export declare function isSystemSessionId(sessionId: string | null | undefined): boolean;
29
29
  //# sourceMappingURL=session-id.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wrongstack/primitives",
3
- "version": "0.316.2",
3
+ "version": "0.317.2",
4
4
  "license": "MIT",
5
5
  "description": "Dependency-leaf primitives shared across WrongStack packages (regex guard, slugs, timestamps).",
6
6
  "repository": {