@posthog/core 1.71.3 → 1.71.4
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"featureFlagLocalEvaluation.d.ts","sourceRoot":"","sources":["../src/featureFlagLocalEvaluation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAEzD,MAAM,MAAM,wBAAwB,GAAG,QAAQ,CAAA;AAE/C,MAAM,MAAM,mBAAmB,GAAG;IAChC,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,wBAAwB,CAAA;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,8BAA8B,GAAG,QAAQ,GAAG,mBAAmB,CAAA;AAE3E,MAAM,MAAM,+BAA+B,GAAG;IAC5C,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;IACxC,mBAAmB,CAAC,EAAE,8BAA8B,CAAA;IACpD,mGAAmG;IACnG,uBAAuB,CAAC,EAAE,MAAM,CAAA;IAChC,8GAA8G;IAC9G,eAAe,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAA;CACtC,CAAA;AAED,MAAM,MAAM,kBAAkB,GAAG;IAC/B,GAAG,EAAE,MAAM,CAAA;IACX,kBAAkB,EAAE,MAAM,CAAA;CAC3B,CAAA;AAED,MAAM,MAAM,6BAA6B,GAAG;IAC1C,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAyDD;;;;GAIG;AACH,MAAM,MAAM,oBAAoB,GAC5B,uBAAuB,GACvB,wBAAwB,GACxB,iBAAiB,GACjB,uBAAuB,CAAA;AAE3B,qBAAa,sBAAuB,SAAQ,KAAK;IAC/C,QAAQ,CAAC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B,QAAQ,EAAE,SAAS,kBAAkB,EAAE,GACtC,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAK7B;AAED,wBAAgB,yBAAyB,CACvC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,GAAG,SAAS,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,EAC3E,SAAS,EAAE,gBAAgB,GAAG,IAAI,GAAG,SAAS,GAC7C,QAAQ,GAAG,IAAI,CAMjB"}
|
|
@@ -227,13 +227,65 @@ function exactMatchString(value) {
|
|
|
227
227
|
return serializeJsonValue(value);
|
|
228
228
|
}
|
|
229
229
|
const LITERAL_ESCAPED_PUNCTUATION = /[\x20-\x2f:;=?@\x5b-\x60\x7b-\x7e]/;
|
|
230
|
+
const MAX_UNICODE_CLASS_COPIES = 100;
|
|
231
|
+
const MAX_ATOM_COPIES = 10000;
|
|
232
|
+
const MAX_GROUP_DEPTH = 32;
|
|
233
|
+
function newRegexTerm(atoms, unicodeClasses, consumes) {
|
|
234
|
+
return {
|
|
235
|
+
atoms,
|
|
236
|
+
unicodeClasses,
|
|
237
|
+
consumes,
|
|
238
|
+
variable: false,
|
|
239
|
+
zeroWidthPart: false
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
function addRegexTerm(group, term, copies = 1) {
|
|
243
|
+
group.atoms += term.atoms * copies;
|
|
244
|
+
group.unicodeClasses += term.unicodeClasses * copies;
|
|
245
|
+
group.consumes ||= term.consumes;
|
|
246
|
+
group.variable ||= term.variable;
|
|
247
|
+
group.zeroWidthPart ||= term.zeroWidthPart;
|
|
248
|
+
return group.atoms <= MAX_ATOM_COPIES && group.unicodeClasses <= MAX_UNICODE_CLASS_COPIES;
|
|
249
|
+
}
|
|
250
|
+
function readRegexRepetition(pattern, index) {
|
|
251
|
+
const character = pattern[index];
|
|
252
|
+
if ('*' === character) return {
|
|
253
|
+
length: 1,
|
|
254
|
+
min: 0,
|
|
255
|
+
max: 1 / 0
|
|
256
|
+
};
|
|
257
|
+
if ('+' === character) return {
|
|
258
|
+
length: 1,
|
|
259
|
+
min: 1,
|
|
260
|
+
max: 1 / 0
|
|
261
|
+
};
|
|
262
|
+
if ('?' === character) return {
|
|
263
|
+
length: 1,
|
|
264
|
+
min: 0,
|
|
265
|
+
max: 1
|
|
266
|
+
};
|
|
267
|
+
const counts = /^\{(\d+)(?:,(\d*))?\}/.exec(pattern.slice(index));
|
|
268
|
+
if (!counts || counts.slice(1).some((count)=>Number(count) > 0xffffffff)) return null;
|
|
269
|
+
const min = Number(counts[1]);
|
|
270
|
+
const max = void 0 === counts[2] ? min : '' === counts[2] ? 1 / 0 : Number(counts[2]);
|
|
271
|
+
return {
|
|
272
|
+
length: counts[0].length,
|
|
273
|
+
min,
|
|
274
|
+
max
|
|
275
|
+
};
|
|
276
|
+
}
|
|
230
277
|
function getPortableRegexClasses(pattern) {
|
|
231
278
|
const classes = [];
|
|
279
|
+
const groups = [
|
|
280
|
+
newRegexTerm(0, 0, false)
|
|
281
|
+
];
|
|
282
|
+
let term = null;
|
|
232
283
|
let classStart = -1;
|
|
233
|
-
let
|
|
284
|
+
let classHasUnicode = false;
|
|
234
285
|
for(let index = 0; index < pattern.length; index++){
|
|
235
286
|
const character = pattern[index];
|
|
236
287
|
if (character.charCodeAt(0) > 0x7f) return null;
|
|
288
|
+
const group = groups[groups.length - 1];
|
|
237
289
|
if ('\\' === character) {
|
|
238
290
|
const escaped = pattern[++index];
|
|
239
291
|
if (void 0 === escaped) break;
|
|
@@ -241,49 +293,77 @@ function getPortableRegexClasses(pattern) {
|
|
|
241
293
|
if (!/^[\da-f]{2}$/i.test(pattern.slice(index + 1, index + 3))) return null;
|
|
242
294
|
index += 2;
|
|
243
295
|
} else if (!'dDsSwWbBfnrtv'.includes(escaped) && !LITERAL_ESCAPED_PUNCTUATION.test(escaped)) return null;
|
|
244
|
-
|
|
245
|
-
|
|
296
|
+
const isUnicodeClass = 'dDsSwW'.includes(escaped);
|
|
297
|
+
if (-1 !== classStart) {
|
|
298
|
+
classHasUnicode ||= isUnicodeClass;
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if ('bB'.includes(escaped)) return null;
|
|
302
|
+
term = newRegexTerm(1, isUnicodeClass ? 1 : 0, true);
|
|
303
|
+
if (!addRegexTerm(group, term)) return null;
|
|
246
304
|
continue;
|
|
247
305
|
}
|
|
248
|
-
if (
|
|
249
|
-
if (
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
} else if (-1 !== classStart) {
|
|
259
|
-
if ([
|
|
306
|
+
if (-1 !== classStart) {
|
|
307
|
+
if ('[' === character) return null;
|
|
308
|
+
if (']' === character) {
|
|
309
|
+
const contentStart = classStart + ('^' === pattern[classStart + 1] ? 2 : 1);
|
|
310
|
+
if (index === contentStart) return null;
|
|
311
|
+
classes.push(pattern.slice(classStart, index + 1));
|
|
312
|
+
classStart = -1;
|
|
313
|
+
term = newRegexTerm(1, classHasUnicode ? 1 : 0, true);
|
|
314
|
+
if (!addRegexTerm(group, term)) return null;
|
|
315
|
+
} else if ([
|
|
260
316
|
'&&',
|
|
261
317
|
'--',
|
|
262
318
|
'~~'
|
|
263
319
|
].includes(pattern.slice(index, index + 2))) return null;
|
|
264
320
|
continue;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
if (
|
|
271
|
-
''
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
321
|
+
}
|
|
322
|
+
if ('[' === character) {
|
|
323
|
+
classStart = index;
|
|
324
|
+
classHasUnicode = '^' === pattern[index + 1];
|
|
325
|
+
} else if ('(' === character) {
|
|
326
|
+
if ('?' === pattern[index + 1]) {
|
|
327
|
+
if (':' !== pattern[index + 2]) return null;
|
|
328
|
+
index += 2;
|
|
329
|
+
}
|
|
330
|
+
if (groups.length > MAX_GROUP_DEPTH) return null;
|
|
331
|
+
groups.push(newRegexTerm(0, 0, false));
|
|
332
|
+
term = null;
|
|
333
|
+
} else if (')' === character) {
|
|
334
|
+
if (groups.length > 1) {
|
|
335
|
+
term = groups.pop();
|
|
336
|
+
term.zeroWidthPart ||= !term.consumes;
|
|
337
|
+
if (!addRegexTerm(groups[groups.length - 1], term)) return null;
|
|
338
|
+
} else if (null === term || !term.zeroWidthPart) term = newRegexTerm(0, 0, true);
|
|
339
|
+
} else if ('|' === character) term = null;
|
|
340
|
+
else if ('*+?{'.includes(character)) {
|
|
341
|
+
const repetition = readRegexRepetition(pattern, index);
|
|
342
|
+
if (!repetition) return null;
|
|
343
|
+
if ('+' === pattern[index + repetition.length] || '{' === pattern[index + repetition.length]) return null;
|
|
344
|
+
index += repetition.length - 1;
|
|
345
|
+
if (null === term) {
|
|
346
|
+
if ('{' === character) return null;
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (term.zeroWidthPart) return null;
|
|
350
|
+
if (repetition.max > 1 && term.variable) return null;
|
|
351
|
+
const copies = 1 / 0 === repetition.max ? repetition.min + 1 : repetition.max;
|
|
352
|
+
if (!addRegexTerm(group, term, Math.max(copies, 1) - 1)) return null;
|
|
353
|
+
group.variable ||= repetition.min !== repetition.max;
|
|
354
|
+
term = null;
|
|
355
|
+
} else if ('^' === character || '$' === character) {
|
|
356
|
+
term = newRegexTerm(0, 0, false);
|
|
357
|
+
term.zeroWidthPart = true;
|
|
358
|
+
addRegexTerm(group, term);
|
|
359
|
+
} else {
|
|
360
|
+
if ('}' === character && ('+' === pattern[index + 1] || '{' === pattern[index + 1])) return null;
|
|
361
|
+
term = newRegexTerm(1, '.' === character ? 1 : 0, true);
|
|
362
|
+
if (!addRegexTerm(group, term)) return null;
|
|
363
|
+
}
|
|
285
364
|
}
|
|
286
|
-
|
|
365
|
+
const total = newRegexTerm(0, 0, false);
|
|
366
|
+
return groups.every((group)=>addRegexTerm(total, group)) ? classes : null;
|
|
287
367
|
}
|
|
288
368
|
function regexRequiresServerEvaluation(message) {
|
|
289
369
|
const error = new InconclusiveMatchError(message);
|
|
@@ -185,13 +185,65 @@ function exactMatchString(value) {
|
|
|
185
185
|
return serializeJsonValue(value);
|
|
186
186
|
}
|
|
187
187
|
const LITERAL_ESCAPED_PUNCTUATION = /[\x20-\x2f:;=?@\x5b-\x60\x7b-\x7e]/;
|
|
188
|
+
const MAX_UNICODE_CLASS_COPIES = 100;
|
|
189
|
+
const MAX_ATOM_COPIES = 10000;
|
|
190
|
+
const MAX_GROUP_DEPTH = 32;
|
|
191
|
+
function newRegexTerm(atoms, unicodeClasses, consumes) {
|
|
192
|
+
return {
|
|
193
|
+
atoms,
|
|
194
|
+
unicodeClasses,
|
|
195
|
+
consumes,
|
|
196
|
+
variable: false,
|
|
197
|
+
zeroWidthPart: false
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function addRegexTerm(group, term, copies = 1) {
|
|
201
|
+
group.atoms += term.atoms * copies;
|
|
202
|
+
group.unicodeClasses += term.unicodeClasses * copies;
|
|
203
|
+
group.consumes ||= term.consumes;
|
|
204
|
+
group.variable ||= term.variable;
|
|
205
|
+
group.zeroWidthPart ||= term.zeroWidthPart;
|
|
206
|
+
return group.atoms <= MAX_ATOM_COPIES && group.unicodeClasses <= MAX_UNICODE_CLASS_COPIES;
|
|
207
|
+
}
|
|
208
|
+
function readRegexRepetition(pattern, index) {
|
|
209
|
+
const character = pattern[index];
|
|
210
|
+
if ('*' === character) return {
|
|
211
|
+
length: 1,
|
|
212
|
+
min: 0,
|
|
213
|
+
max: 1 / 0
|
|
214
|
+
};
|
|
215
|
+
if ('+' === character) return {
|
|
216
|
+
length: 1,
|
|
217
|
+
min: 1,
|
|
218
|
+
max: 1 / 0
|
|
219
|
+
};
|
|
220
|
+
if ('?' === character) return {
|
|
221
|
+
length: 1,
|
|
222
|
+
min: 0,
|
|
223
|
+
max: 1
|
|
224
|
+
};
|
|
225
|
+
const counts = /^\{(\d+)(?:,(\d*))?\}/.exec(pattern.slice(index));
|
|
226
|
+
if (!counts || counts.slice(1).some((count)=>Number(count) > 0xffffffff)) return null;
|
|
227
|
+
const min = Number(counts[1]);
|
|
228
|
+
const max = void 0 === counts[2] ? min : '' === counts[2] ? 1 / 0 : Number(counts[2]);
|
|
229
|
+
return {
|
|
230
|
+
length: counts[0].length,
|
|
231
|
+
min,
|
|
232
|
+
max
|
|
233
|
+
};
|
|
234
|
+
}
|
|
188
235
|
function getPortableRegexClasses(pattern) {
|
|
189
236
|
const classes = [];
|
|
237
|
+
const groups = [
|
|
238
|
+
newRegexTerm(0, 0, false)
|
|
239
|
+
];
|
|
240
|
+
let term = null;
|
|
190
241
|
let classStart = -1;
|
|
191
|
-
let
|
|
242
|
+
let classHasUnicode = false;
|
|
192
243
|
for(let index = 0; index < pattern.length; index++){
|
|
193
244
|
const character = pattern[index];
|
|
194
245
|
if (character.charCodeAt(0) > 0x7f) return null;
|
|
246
|
+
const group = groups[groups.length - 1];
|
|
195
247
|
if ('\\' === character) {
|
|
196
248
|
const escaped = pattern[++index];
|
|
197
249
|
if (void 0 === escaped) break;
|
|
@@ -199,49 +251,77 @@ function getPortableRegexClasses(pattern) {
|
|
|
199
251
|
if (!/^[\da-f]{2}$/i.test(pattern.slice(index + 1, index + 3))) return null;
|
|
200
252
|
index += 2;
|
|
201
253
|
} else if (!'dDsSwWbBfnrtv'.includes(escaped) && !LITERAL_ESCAPED_PUNCTUATION.test(escaped)) return null;
|
|
202
|
-
|
|
203
|
-
|
|
254
|
+
const isUnicodeClass = 'dDsSwW'.includes(escaped);
|
|
255
|
+
if (-1 !== classStart) {
|
|
256
|
+
classHasUnicode ||= isUnicodeClass;
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if ('bB'.includes(escaped)) return null;
|
|
260
|
+
term = newRegexTerm(1, isUnicodeClass ? 1 : 0, true);
|
|
261
|
+
if (!addRegexTerm(group, term)) return null;
|
|
204
262
|
continue;
|
|
205
263
|
}
|
|
206
|
-
if (
|
|
207
|
-
if (
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
} else if (-1 !== classStart) {
|
|
217
|
-
if ([
|
|
264
|
+
if (-1 !== classStart) {
|
|
265
|
+
if ('[' === character) return null;
|
|
266
|
+
if (']' === character) {
|
|
267
|
+
const contentStart = classStart + ('^' === pattern[classStart + 1] ? 2 : 1);
|
|
268
|
+
if (index === contentStart) return null;
|
|
269
|
+
classes.push(pattern.slice(classStart, index + 1));
|
|
270
|
+
classStart = -1;
|
|
271
|
+
term = newRegexTerm(1, classHasUnicode ? 1 : 0, true);
|
|
272
|
+
if (!addRegexTerm(group, term)) return null;
|
|
273
|
+
} else if ([
|
|
218
274
|
'&&',
|
|
219
275
|
'--',
|
|
220
276
|
'~~'
|
|
221
277
|
].includes(pattern.slice(index, index + 2))) return null;
|
|
222
278
|
continue;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if (
|
|
229
|
-
''
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
279
|
+
}
|
|
280
|
+
if ('[' === character) {
|
|
281
|
+
classStart = index;
|
|
282
|
+
classHasUnicode = '^' === pattern[index + 1];
|
|
283
|
+
} else if ('(' === character) {
|
|
284
|
+
if ('?' === pattern[index + 1]) {
|
|
285
|
+
if (':' !== pattern[index + 2]) return null;
|
|
286
|
+
index += 2;
|
|
287
|
+
}
|
|
288
|
+
if (groups.length > MAX_GROUP_DEPTH) return null;
|
|
289
|
+
groups.push(newRegexTerm(0, 0, false));
|
|
290
|
+
term = null;
|
|
291
|
+
} else if (')' === character) {
|
|
292
|
+
if (groups.length > 1) {
|
|
293
|
+
term = groups.pop();
|
|
294
|
+
term.zeroWidthPart ||= !term.consumes;
|
|
295
|
+
if (!addRegexTerm(groups[groups.length - 1], term)) return null;
|
|
296
|
+
} else if (null === term || !term.zeroWidthPart) term = newRegexTerm(0, 0, true);
|
|
297
|
+
} else if ('|' === character) term = null;
|
|
298
|
+
else if ('*+?{'.includes(character)) {
|
|
299
|
+
const repetition = readRegexRepetition(pattern, index);
|
|
300
|
+
if (!repetition) return null;
|
|
301
|
+
if ('+' === pattern[index + repetition.length] || '{' === pattern[index + repetition.length]) return null;
|
|
302
|
+
index += repetition.length - 1;
|
|
303
|
+
if (null === term) {
|
|
304
|
+
if ('{' === character) return null;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
if (term.zeroWidthPart) return null;
|
|
308
|
+
if (repetition.max > 1 && term.variable) return null;
|
|
309
|
+
const copies = 1 / 0 === repetition.max ? repetition.min + 1 : repetition.max;
|
|
310
|
+
if (!addRegexTerm(group, term, Math.max(copies, 1) - 1)) return null;
|
|
311
|
+
group.variable ||= repetition.min !== repetition.max;
|
|
312
|
+
term = null;
|
|
313
|
+
} else if ('^' === character || '$' === character) {
|
|
314
|
+
term = newRegexTerm(0, 0, false);
|
|
315
|
+
term.zeroWidthPart = true;
|
|
316
|
+
addRegexTerm(group, term);
|
|
317
|
+
} else {
|
|
318
|
+
if ('}' === character && ('+' === pattern[index + 1] || '{' === pattern[index + 1])) return null;
|
|
319
|
+
term = newRegexTerm(1, '.' === character ? 1 : 0, true);
|
|
320
|
+
if (!addRegexTerm(group, term)) return null;
|
|
321
|
+
}
|
|
243
322
|
}
|
|
244
|
-
|
|
323
|
+
const total = newRegexTerm(0, 0, false);
|
|
324
|
+
return groups.every((group)=>addRegexTerm(total, group)) ? classes : null;
|
|
245
325
|
}
|
|
246
326
|
function regexRequiresServerEvaluation(message) {
|
|
247
327
|
const error = new InconclusiveMatchError(message);
|
package/package.json
CHANGED
|
@@ -290,13 +290,72 @@ function exactMatchString(value: unknown): string {
|
|
|
290
290
|
// Both engines treat escaped spaces and ASCII punctuation as literals, except Rust's \< and \> word boundaries.
|
|
291
291
|
const LITERAL_ESCAPED_PUNCTUATION = /[\x20-\x2f:;=?@\x5b-\x60\x7b-\x7e]/
|
|
292
292
|
|
|
293
|
+
// The flags service compiles a pattern in the subset with Rust's `regex` crate, whose program must
|
|
294
|
+
// fit in 10 MiB, so it rejects a pattern that JavaScript accepts once it holds too many atoms. A
|
|
295
|
+
// Unicode class is the costliest atom: the service compiles at most 209 copies of `\w`, about
|
|
296
|
+
// 2,000 of `\d` and 10,000 of `.`, against 145,000 ASCII classes or 327,000 literals. These bounds
|
|
297
|
+
// keep under half of the `\w` limit, and leave room for the ASCII atoms beside them.
|
|
298
|
+
const MAX_UNICODE_CLASS_COPIES = 100
|
|
299
|
+
const MAX_ATOM_COPIES = 10_000
|
|
300
|
+
// The service rejects groups nested 64 deep. JavaScript accepts them.
|
|
301
|
+
const MAX_GROUP_DEPTH = 32
|
|
302
|
+
|
|
303
|
+
// An atom, an anchor, or a group with everything in it.
|
|
304
|
+
type RegexTerm = {
|
|
305
|
+
// The copies of atoms, and of atoms that are Unicode classes, that it compiles to.
|
|
306
|
+
atoms: number
|
|
307
|
+
unicodeClasses: number
|
|
308
|
+
// Whether it can consume input, and whether it holds a repetition of variable length.
|
|
309
|
+
consumes: boolean
|
|
310
|
+
variable: boolean
|
|
311
|
+
// Whether it holds an anchor or a group that consumes nothing, which repeating it would quantify.
|
|
312
|
+
zeroWidthPart: boolean
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function newRegexTerm(atoms: number, unicodeClasses: number, consumes: boolean): RegexTerm {
|
|
316
|
+
return { atoms, unicodeClasses, consumes, variable: false, zeroWidthPart: false }
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Adds `copies` copies of the term to the group, and returns whether the group still fits the bounds.
|
|
320
|
+
function addRegexTerm(group: RegexTerm, term: RegexTerm, copies: number = 1): boolean {
|
|
321
|
+
group.atoms += term.atoms * copies
|
|
322
|
+
group.unicodeClasses += term.unicodeClasses * copies
|
|
323
|
+
group.consumes ||= term.consumes
|
|
324
|
+
group.variable ||= term.variable
|
|
325
|
+
group.zeroWidthPart ||= term.zeroWidthPart
|
|
326
|
+
return group.atoms <= MAX_ATOM_COPIES && group.unicodeClasses <= MAX_UNICODE_CLASS_COPIES
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function readRegexRepetition(pattern: string, index: number): { length: number; min: number; max: number } | null {
|
|
330
|
+
const character = pattern[index]
|
|
331
|
+
if (character === '*') return { length: 1, min: 0, max: Infinity }
|
|
332
|
+
if (character === '+') return { length: 1, min: 1, max: Infinity }
|
|
333
|
+
if (character === '?') return { length: 1, min: 0, max: 1 }
|
|
334
|
+
const counts = /^\{(\d+)(?:,(\d*))?\}/.exec(pattern.slice(index))
|
|
335
|
+
// JavaScript reads a malformed repetition such as `a{,2}` as literal text, and accepts counts
|
|
336
|
+
// above 4,294,967,295, which the flags service rejects.
|
|
337
|
+
if (!counts || counts.slice(1).some((count) => Number(count) > 0xffffffff)) return null
|
|
338
|
+
const min = Number(counts[1])
|
|
339
|
+
const max = counts[2] === undefined ? min : counts[2] === '' ? Infinity : Number(counts[2])
|
|
340
|
+
return { length: counts[0].length, min, max }
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* The character classes of a pattern in the portable subset, for a stricter syntax check, or null
|
|
345
|
+
* when the flags service and JavaScript could disagree on the pattern.
|
|
346
|
+
*/
|
|
293
347
|
function getPortableRegexClasses(pattern: string): string[] | null {
|
|
294
348
|
const classes: string[] = []
|
|
349
|
+
// The innermost open group is last. A group is added to its parent when it closes.
|
|
350
|
+
const groups: RegexTerm[] = [newRegexTerm(0, 0, false)]
|
|
351
|
+
// What a repetition here would repeat, or null where there is nothing to repeat.
|
|
352
|
+
let term: RegexTerm | null = null
|
|
295
353
|
let classStart = -1
|
|
296
|
-
let
|
|
354
|
+
let classHasUnicode = false
|
|
297
355
|
for (let index = 0; index < pattern.length; index++) {
|
|
298
356
|
const character = pattern[index]
|
|
299
357
|
if (character.charCodeAt(0) > 0x7f) return null
|
|
358
|
+
const group = groups[groups.length - 1]
|
|
300
359
|
if (character === '\\') {
|
|
301
360
|
const escaped = pattern[++index]
|
|
302
361
|
if (escaped === undefined) break
|
|
@@ -306,48 +365,94 @@ function getPortableRegexClasses(pattern: string): string[] | null {
|
|
|
306
365
|
} else if (!'dDsSwWbBfnrtv'.includes(escaped) && !LITERAL_ESCAPED_PUNCTUATION.test(escaped)) {
|
|
307
366
|
return null
|
|
308
367
|
}
|
|
368
|
+
const isUnicodeClass = 'dDsSwW'.includes(escaped)
|
|
369
|
+
if (classStart !== -1) {
|
|
370
|
+
classHasUnicode ||= isUnicodeClass
|
|
371
|
+
continue
|
|
372
|
+
}
|
|
309
373
|
// Word boundaries can exhaust the service's backtracking limit even for short ASCII values.
|
|
310
|
-
if (
|
|
311
|
-
|
|
374
|
+
if ('bB'.includes(escaped)) return null
|
|
375
|
+
term = newRegexTerm(1, isUnicodeClass ? 1 : 0, true)
|
|
376
|
+
if (!addRegexTerm(group, term)) return null
|
|
377
|
+
continue
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (classStart !== -1) {
|
|
381
|
+
if (character === '[') return null
|
|
382
|
+
if (character === ']') {
|
|
383
|
+
const contentStart = classStart + (pattern[classStart + 1] === '^' ? 2 : 1)
|
|
384
|
+
if (index === contentStart) return null
|
|
385
|
+
classes.push(pattern.slice(classStart, index + 1))
|
|
386
|
+
classStart = -1
|
|
387
|
+
// A negated class holds most of Unicode, like a class that holds a shorthand.
|
|
388
|
+
term = newRegexTerm(1, classHasUnicode ? 1 : 0, true)
|
|
389
|
+
if (!addRegexTerm(group, term)) return null
|
|
390
|
+
} else if (['&&', '--', '~~'].includes(pattern.slice(index, index + 2))) {
|
|
391
|
+
return null
|
|
392
|
+
}
|
|
312
393
|
continue
|
|
313
394
|
}
|
|
314
395
|
|
|
315
396
|
if (character === '[') {
|
|
316
|
-
if (classStart !== -1) return null
|
|
317
397
|
classStart = index
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
if (index ===
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
!repetition ||
|
|
337
|
-
['', '|', '('].includes(previousToken) ||
|
|
338
|
-
repetition.slice(1).some((count) => Number(count) > 0xffffffff)
|
|
339
|
-
) {
|
|
340
|
-
return null
|
|
398
|
+
classHasUnicode = pattern[index + 1] === '^'
|
|
399
|
+
} else if (character === '(') {
|
|
400
|
+
if (pattern[index + 1] === '?') {
|
|
401
|
+
if (pattern[index + 2] !== ':') return null
|
|
402
|
+
index += 2
|
|
403
|
+
}
|
|
404
|
+
if (groups.length > MAX_GROUP_DEPTH) return null
|
|
405
|
+
groups.push(newRegexTerm(0, 0, false))
|
|
406
|
+
term = null
|
|
407
|
+
} else if (character === ')') {
|
|
408
|
+
if (groups.length > 1) {
|
|
409
|
+
term = groups.pop() as RegexTerm
|
|
410
|
+
term.zeroWidthPart ||= !term.consumes
|
|
411
|
+
if (!addRegexTerm(groups[groups.length - 1], term)) return null
|
|
412
|
+
} else if (term === null || !term.zeroWidthPart) {
|
|
413
|
+
// Both engines reject an unmatched parenthesis. One after a term that a repetition would
|
|
414
|
+
// defer keeps that term.
|
|
415
|
+
term = newRegexTerm(0, 0, true)
|
|
341
416
|
}
|
|
342
|
-
} else if (
|
|
343
|
-
|
|
417
|
+
} else if (character === '|') {
|
|
418
|
+
term = null
|
|
419
|
+
} else if ('*+?{'.includes(character)) {
|
|
420
|
+
const repetition = readRegexRepetition(pattern, index)
|
|
421
|
+
if (!repetition) return null
|
|
422
|
+
// Rust accepts possessive and stacked repetitions such as `a++` and `a*{2}`. JavaScript rejects them.
|
|
423
|
+
if (pattern[index + repetition.length] === '+' || pattern[index + repetition.length] === '{') return null
|
|
424
|
+
index += repetition.length - 1
|
|
425
|
+
if (term === null) {
|
|
426
|
+
// Rust reads `{n}` with nothing before it as literal text. JavaScript rejects it.
|
|
427
|
+
if (character === '{') return null
|
|
428
|
+
// Otherwise this makes a repetition lazy, or both engines reject it.
|
|
429
|
+
continue
|
|
430
|
+
}
|
|
431
|
+
// JavaScript rejects a repeated anchor such as `^?`. Rust rejects a repeated empty group such
|
|
432
|
+
// as `(?:)*`. Repeating a group that holds either, such as `(?:^)*`, quantifies an assertion.
|
|
433
|
+
if (term.zeroWidthPart) return null
|
|
434
|
+
// A repeated group that holds a variable-length repetition, such as `(a+)+`, can backtrack
|
|
435
|
+
// exponentially in JavaScript, while the service matches it in linear time.
|
|
436
|
+
if (repetition.max > 1 && term.variable) return null
|
|
437
|
+
// A repetition compiles to as many copies as its maximum, or one more than its minimum.
|
|
438
|
+
const copies = repetition.max === Infinity ? repetition.min + 1 : repetition.max
|
|
439
|
+
if (!addRegexTerm(group, term, Math.max(copies, 1) - 1)) return null
|
|
440
|
+
group.variable ||= repetition.min !== repetition.max
|
|
441
|
+
term = null
|
|
442
|
+
} else if (character === '^' || character === '$') {
|
|
443
|
+
term = newRegexTerm(0, 0, false)
|
|
444
|
+
term.zeroWidthPart = true
|
|
445
|
+
addRegexTerm(group, term)
|
|
446
|
+
} else {
|
|
447
|
+
// A literal `}` before `+` or `{` reads like the end of a stacked repetition, so it defers too.
|
|
448
|
+
if (character === '}' && (pattern[index + 1] === '+' || pattern[index + 1] === '{')) return null
|
|
449
|
+
term = newRegexTerm(1, character === '.' ? 1 : 0, true)
|
|
450
|
+
if (!addRegexTerm(group, term)) return null
|
|
344
451
|
}
|
|
345
|
-
// JavaScript rejects a repeated anchor such as `^?`. Rust accepts it.
|
|
346
|
-
// Rust rejects a repeated empty group such as `(?:)*`. JavaScript accepts it.
|
|
347
|
-
if ('*+?{'.includes(character) && ['^', '$', 'empty group'].includes(previousToken)) return null
|
|
348
|
-
previousToken = character === ')' && ['(', 'empty group'].includes(previousToken) ? 'empty group' : character
|
|
349
452
|
}
|
|
350
|
-
|
|
453
|
+
// Groups still open belong to no parent yet. Both engines reject them, but count them anyway.
|
|
454
|
+
const total = newRegexTerm(0, 0, false)
|
|
455
|
+
return groups.every((group) => addRegexTerm(total, group)) ? classes : null
|
|
351
456
|
}
|
|
352
457
|
|
|
353
458
|
// Adapters must preserve regex uncertainty across variant rules without changing missing-context fallback.
|
|
@@ -360,7 +465,8 @@ function regexRequiresServerEvaluation(message: string): InconclusiveMatchError
|
|
|
360
465
|
function matchFeatureFlagRegex(pattern: string, value: string): boolean | null {
|
|
361
466
|
if (pattern === '') return true
|
|
362
467
|
|
|
363
|
-
// Rust uses Unicode scalar semantics and
|
|
468
|
+
// Rust uses Unicode scalar semantics, additional syntax and other limits. Keep local matching to
|
|
469
|
+
// the shared ASCII subset that both engines compile and match within their limits.
|
|
364
470
|
const characterClasses = getPortableRegexClasses(pattern)
|
|
365
471
|
if (characterClasses === null) {
|
|
366
472
|
throw regexRequiresServerEvaluation('Regex syntax requires server evaluation')
|