@libreflare/worker-runtime 0.10.3 → 0.10.5
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 +87 -37
- package/dist/rule-evaluator/access-values.d.ts +42 -27
- package/dist/rule-evaluator/access-values.d.ts.map +1 -1
- package/dist/rule-evaluator/access-values.js +123 -52
- package/dist/rule-evaluator/access-values.js.map +1 -1
- package/dist/rule-evaluator/ast.d.ts +6 -0
- package/dist/rule-evaluator/ast.d.ts.map +1 -1
- package/dist/rule-evaluator/ast.js.map +1 -1
- package/dist/rule-evaluator/byte-strings.d.ts.map +1 -1
- package/dist/rule-evaluator/byte-strings.js +27 -11
- package/dist/rule-evaluator/byte-strings.js.map +1 -1
- package/dist/rule-evaluator/constants.d.ts +1 -1
- package/dist/rule-evaluator/constants.d.ts.map +1 -1
- package/dist/rule-evaluator/constants.js +3 -0
- package/dist/rule-evaluator/constants.js.map +1 -1
- package/dist/rule-evaluator/country-codes.generated.js +1 -1
- package/dist/rule-evaluator/diagnostics.d.ts +1 -1
- package/dist/rule-evaluator/diagnostics.d.ts.map +1 -1
- package/dist/rule-evaluator/diagnostics.js +8 -0
- package/dist/rule-evaluator/diagnostics.js.map +1 -1
- package/dist/rule-evaluator/edge-field-gaps.d.ts +6 -4
- package/dist/rule-evaluator/edge-field-gaps.d.ts.map +1 -1
- package/dist/rule-evaluator/edge-field-gaps.js +14 -39
- package/dist/rule-evaluator/edge-field-gaps.js.map +1 -1
- package/dist/rule-evaluator/edge-record.js +2 -0
- package/dist/rule-evaluator/edge-record.js.map +1 -1
- package/dist/rule-evaluator/engine-observations.d.ts +4 -4
- package/dist/rule-evaluator/engine-observations.d.ts.map +1 -1
- package/dist/rule-evaluator/engine-observations.generated.d.ts +496 -3
- package/dist/rule-evaluator/engine-observations.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/engine-observations.generated.js +389 -248
- package/dist/rule-evaluator/engine-observations.generated.js.map +1 -1
- package/dist/rule-evaluator/engine-observations.js +1 -0
- package/dist/rule-evaluator/engine-observations.js.map +1 -1
- package/dist/rule-evaluator/explain.js +10 -7
- package/dist/rule-evaluator/explain.js.map +1 -1
- package/dist/rule-evaluator/field-availability.d.ts +6 -1
- package/dist/rule-evaluator/field-availability.d.ts.map +1 -1
- package/dist/rule-evaluator/field-availability.js +16 -4
- package/dist/rule-evaluator/field-availability.js.map +1 -1
- package/dist/rule-evaluator/field-descriptions.generated.d.ts +147 -5
- package/dist/rule-evaluator/field-descriptions.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/field-descriptions.generated.js +345 -5
- package/dist/rule-evaluator/field-descriptions.generated.js.map +1 -1
- package/dist/rule-evaluator/fields.d.ts +0 -1
- package/dist/rule-evaluator/fields.d.ts.map +1 -1
- package/dist/rule-evaluator/fields.js +67 -8
- package/dist/rule-evaluator/fields.js.map +1 -1
- package/dist/rule-evaluator/function-availability.d.ts +7 -0
- package/dist/rule-evaluator/function-availability.d.ts.map +1 -1
- package/dist/rule-evaluator/function-availability.js +38 -0
- package/dist/rule-evaluator/function-availability.js.map +1 -1
- package/dist/rule-evaluator/function-descriptions.d.ts +10 -2
- package/dist/rule-evaluator/function-descriptions.d.ts.map +1 -1
- package/dist/rule-evaluator/function-descriptions.generated.d.ts +55 -4
- package/dist/rule-evaluator/function-descriptions.generated.d.ts.map +1 -1
- package/dist/rule-evaluator/function-descriptions.generated.js +351 -1
- package/dist/rule-evaluator/function-descriptions.generated.js.map +1 -1
- package/dist/rule-evaluator/function-descriptions.js +3 -1
- package/dist/rule-evaluator/function-descriptions.js.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.d.ts +18 -0
- package/dist/rule-evaluator/function-signature-validation.d.ts.map +1 -1
- package/dist/rule-evaluator/function-signature-validation.js +146 -13
- package/dist/rule-evaluator/function-signature-validation.js.map +1 -1
- package/dist/rule-evaluator/functions.d.ts +10 -6
- package/dist/rule-evaluator/functions.d.ts.map +1 -1
- package/dist/rule-evaluator/functions.js +469 -172
- package/dist/rule-evaluator/functions.js.map +1 -1
- package/dist/rule-evaluator/operators.d.ts +0 -6
- package/dist/rule-evaluator/operators.d.ts.map +1 -1
- package/dist/rule-evaluator/operators.js +4 -52
- package/dist/rule-evaluator/operators.js.map +1 -1
- package/dist/rule-evaluator/parser.d.ts.map +1 -1
- package/dist/rule-evaluator/parser.js +4 -1
- package/dist/rule-evaluator/parser.js.map +1 -1
- package/dist/rule-evaluator/quantifier.d.ts +17 -2
- package/dist/rule-evaluator/quantifier.d.ts.map +1 -1
- package/dist/rule-evaluator/quantifier.js +90 -7
- package/dist/rule-evaluator/quantifier.js.map +1 -1
- package/dist/rule-evaluator/replacement.d.ts +37 -0
- package/dist/rule-evaluator/replacement.d.ts.map +1 -0
- package/dist/rule-evaluator/replacement.js +54 -0
- package/dist/rule-evaluator/replacement.js.map +1 -0
- package/dist/rule-evaluator/rule-language-types.d.ts.map +1 -1
- package/dist/rule-evaluator/rule-language-types.js +5 -0
- package/dist/rule-evaluator/rule-language-types.js.map +1 -1
- package/dist/rule-evaluator/validation.d.ts +1 -1
- package/dist/rule-evaluator/validation.d.ts.map +1 -1
- package/dist/rule-evaluator/validation.js +160 -106
- package/dist/rule-evaluator/validation.js.map +1 -1
- package/dist/rule-evaluator/wildcard.d.ts +65 -0
- package/dist/rule-evaluator/wildcard.d.ts.map +1 -0
- package/dist/rule-evaluator/wildcard.js +168 -0
- package/dist/rule-evaluator/wildcard.js.map +1 -0
- package/package.json +2 -2
|
@@ -1,17 +1,38 @@
|
|
|
1
|
-
import { EQUALITY_ORDERING_OPERATORS, isConditionArgument, isDynamicValueNode, unhandledNode, } from './ast.js';
|
|
2
|
-
import { applyRuntimeAccessPath, isUnknownElement, PartialArrayValue, RuleMapValue, } from './access-values.js';
|
|
1
|
+
import { EQUALITY_ORDERING_OPERATORS, isConditionArgument, isDynamicValueNode, isExpressionNode, unhandledNode, } from './ast.js';
|
|
2
|
+
import { applyRuntimeAccessPath, inPlaceOf, isUnknownElement, listedElement, OptionalElement, PartialArrayValue, RuleMapValue, } from './access-values.js';
|
|
3
3
|
import { byteLength, bytesToText, canonicalText, textToBytes } from './byte-strings.js';
|
|
4
4
|
import { formatIpAddress, ipAddressOctets } from './custom-lists.js';
|
|
5
5
|
import { resolveBoolField, resolveFieldNode } from './fields.js';
|
|
6
6
|
import { hashInRange } from './hash-in-range.js';
|
|
7
|
-
import { applyOp, compareOperands, findMatchingSetMember, literalValue, reachableSetMembers
|
|
7
|
+
import { applyOp, compareOperands, findMatchingSetMember, literalValue, reachableSetMembers } from './operators.js';
|
|
8
8
|
import { andOutcome, incomparable, isUnknown, known, negateOutcome, orOutcome, outcomeValue, unavailable, valueOutcome, xorOutcome, } from './outcome.js';
|
|
9
|
-
import { conditionArray, extractQuantifierPredicate, givesNilForNilArgument, keepsPlacesWhenMapped, quantifierPredicateConditions, readQuantifierPredicate, unalignedArray, } from './quantifier.js';
|
|
9
|
+
import { conditionArray, extractQuantifierPredicate, givesNilForNilArgument, keepsPlacesWhenMapped, pairedArgumentArrays, quantifierPredicateConditions, readQuantifierPredicate, unalignedArray, } from './quantifier.js';
|
|
10
10
|
import { removeQueryArgs } from './query-args.js';
|
|
11
|
+
import { expandReplacement, MAX_REPLACEMENT_REFERENCES, measureReplacement } from './replacement.js';
|
|
11
12
|
import { urlDecode } from './url-decode.js';
|
|
13
|
+
import { compileWildcard, wildcardCaptures } from './wildcard.js';
|
|
12
14
|
import { collectionElementDataType, compareRuleExpressionDataTypes, formatRuleExpressionDataType, evaluatedValueType, inferFunctionInvocation, isArrayConditionArgument, } from './rule-language-types.js';
|
|
15
|
+
/**
|
|
16
|
+
* The most a call's result holds, over all its elements when it maps; see
|
|
17
|
+
* elementSize(). A request's URL and headers fit, while join(), replacements
|
|
18
|
+
* and a mapped concat() can make many megabytes of them, which Cloudflare
|
|
19
|
+
* gives whole. An array keeps the elements that fit, and the others are
|
|
20
|
+
* unknown. It bounds memory rather than accounting for it: what a call holds
|
|
21
|
+
* while it runs can be a few times as much.
|
|
22
|
+
*/
|
|
23
|
+
const MAX_RESULT_SIZE = 512 * 1024;
|
|
24
|
+
/** The fewest characters an array element counts as: even an empty one takes memory, and an unknown one more. */
|
|
25
|
+
const MIN_ELEMENT_SIZE = 8;
|
|
26
|
+
/** The most elements concat() of arrays joins, whatever they hold, as its result shares their text. */
|
|
27
|
+
const MAX_CONCAT_ELEMENTS = MAX_RESULT_SIZE / MIN_ELEMENT_SIZE;
|
|
28
|
+
const RESULT_SIZE_REASON = `its result holds more than ${MAX_RESULT_SIZE} characters, counting each array element as at least ${MIN_ELEMENT_SIZE}`;
|
|
29
|
+
/** A result that holds more, which is not built; see tooLargeResult(). */
|
|
30
|
+
const TOO_LARGE = Symbol('too large');
|
|
31
|
+
/** How much the pieces of a result may hold where joining them can unite their bytes; see surelyTooLarge(). */
|
|
32
|
+
const MAX_JOINED_SIZE = MAX_RESULT_SIZE * 3;
|
|
13
33
|
const UNRELIABLE_FUNCTION_REASONS = {
|
|
14
34
|
bit_slice: 'packet bit slicing is not available in Worker request context',
|
|
35
|
+
hmac_sha256: 'synchronous HMAC-SHA-256 is not available in this evaluator',
|
|
15
36
|
is_jwt_present: 'Cloudflare API JWT token configurations are not available in Worker request context',
|
|
16
37
|
is_jwt_valid: 'Cloudflare API JWT token configurations are not available in Worker request context',
|
|
17
38
|
is_timed_hmac_valid_v0: 'synchronous HMAC validation is not available in this evaluator',
|
|
@@ -38,8 +59,8 @@ function evaluateRuntimeOperand(node, ctx) {
|
|
|
38
59
|
* Narrows a value to a rule-language value. Only coalesce(), has_key(), and
|
|
39
60
|
* postfix access read maps, and validation rejects maps anywhere else, so a
|
|
40
61
|
* map here is unknown. So is a partly known array, which only any(), all(),
|
|
41
|
-
* has_value(), len(), concat(), mapped functions and postfix access
|
|
42
|
-
* part.
|
|
62
|
+
* has_value(), len(), concat(), filter(), mapped functions and postfix access
|
|
63
|
+
* read in part.
|
|
43
64
|
*/
|
|
44
65
|
function ruleValue(evaluated) {
|
|
45
66
|
if (isUnknown(evaluated))
|
|
@@ -94,15 +115,21 @@ export function evaluateComparison(node, ctx) {
|
|
|
94
115
|
*/
|
|
95
116
|
export function evaluateCollection(fieldArg, ctx) {
|
|
96
117
|
const evaluated = evaluateRuntimeOperand(fieldArg, ctx);
|
|
97
|
-
const array = isUnknown(evaluated) ? evaluated :
|
|
118
|
+
const array = isUnknown(evaluated) ? evaluated : arrayValue(evaluated.value);
|
|
98
119
|
if (isUnknown(array))
|
|
99
120
|
return array;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return known(
|
|
121
|
+
if (!(array.value instanceof PartialArrayValue))
|
|
122
|
+
return known({ values: array.value.filter(isCollectionValue) });
|
|
123
|
+
const { elements, more, shortest } = array.value;
|
|
124
|
+
const values = array.value.knownValues();
|
|
125
|
+
const optional = elements.flatMap(listed => listed instanceof OptionalElement && !isUnknownElement(listed.element) ? [listed.element.value] : []);
|
|
126
|
+
return known({
|
|
127
|
+
values: values.filter(isCollectionValue),
|
|
128
|
+
unknown: array.value.firstUnknown(),
|
|
129
|
+
optional: optional.filter(isCollectionValue),
|
|
130
|
+
mayHoldUnknown: more !== undefined || values.length + optional.length < elements.length,
|
|
131
|
+
mayBeEmpty: shortest === 0,
|
|
132
|
+
});
|
|
106
133
|
}
|
|
107
134
|
function isCollectionValue(value) {
|
|
108
135
|
return typeof value === 'string' || typeof value === 'number';
|
|
@@ -194,30 +221,31 @@ function evaluateQuantifier(node, ctx) {
|
|
|
194
221
|
const reading = readQuantifierPredicate(node);
|
|
195
222
|
return reading ? evaluatePairedQuantifier(node, reading, ctx).outcome : false;
|
|
196
223
|
}
|
|
197
|
-
/** A value
|
|
198
|
-
function
|
|
224
|
+
/** A value read as an array: nil has no elements, a scalar is one, and a map's are unknown. */
|
|
225
|
+
function arrayValue(value) {
|
|
199
226
|
if (value instanceof RuleMapValue)
|
|
200
227
|
return unavailable('a map value');
|
|
201
|
-
if (value instanceof PartialArrayValue)
|
|
202
|
-
return known({ elements: value.elements, more: value.more });
|
|
203
228
|
if (value === null)
|
|
204
|
-
return known(
|
|
205
|
-
return known(
|
|
229
|
+
return known([]);
|
|
230
|
+
return known(value instanceof PartialArrayValue || Array.isArray(value) ? value : [value]);
|
|
206
231
|
}
|
|
207
|
-
const NO_PAIRS = { arrays: new Map(),
|
|
232
|
+
const NO_PAIRS = { arrays: new Map(), end: 0, tailFrom: 0 };
|
|
208
233
|
/**
|
|
209
234
|
* Evaluates the collections of a predicate's conditions; see PairedArrays.
|
|
210
235
|
* An empty one leaves no pairs, whatever the others hold, so the ones after
|
|
211
236
|
* it are not read. `valueCall` is a quantifier's predicate read as a value,
|
|
212
|
-
* which is no array when it gives nil.
|
|
237
|
+
* which is no array when it gives nil. `source` is an array that pairs with
|
|
238
|
+
* them.
|
|
213
239
|
*/
|
|
214
|
-
function pairArrays(owner, predicate, ctx, valueCall) {
|
|
240
|
+
function pairArrays(owner, predicate, ctx, valueCall, source) {
|
|
241
|
+
const paired = source?.array;
|
|
242
|
+
if (paired && paired.length === 0 && !paired.more)
|
|
243
|
+
return NO_PAIRS;
|
|
215
244
|
const arrays = new Map();
|
|
216
|
-
let
|
|
217
|
-
let
|
|
218
|
-
let
|
|
219
|
-
let
|
|
220
|
-
let shortestMore;
|
|
245
|
+
let end = paired && !paired.more ? paired.length : Infinity;
|
|
246
|
+
let longest = paired ? paired.length : 0;
|
|
247
|
+
let shortest = paired;
|
|
248
|
+
let partial = paired?.unknownLength !== undefined;
|
|
221
249
|
for (const { node: condition } of quantifierPredicateConditions(predicate)) {
|
|
222
250
|
// Conditions that validation accepts have a result for each element at each comparison and call (see
|
|
223
251
|
// isArrayCondition()), so their kind tells them.
|
|
@@ -229,35 +257,48 @@ function pairArrays(owner, predicate, ctx, valueCall) {
|
|
|
229
257
|
giveWarnings(ctx, warnings);
|
|
230
258
|
return { ...NO_PAIRS, nilCall: valueCall };
|
|
231
259
|
}
|
|
232
|
-
const
|
|
260
|
+
const compare = condition.kind === 'Comparison' ? comparatorFor(owner, condition, ctx) : isTrueValue;
|
|
261
|
+
const array = pairedArray(evaluated, warnings, compare, ctx);
|
|
233
262
|
if (array.length === 0 && !array.more)
|
|
234
263
|
return NO_PAIRS;
|
|
235
264
|
arrays.set(condition, array);
|
|
236
|
-
if (array.
|
|
237
|
-
|
|
238
|
-
shortestMore = array.more;
|
|
239
|
-
}
|
|
265
|
+
if (!shortest || array.shortest < shortest.shortest)
|
|
266
|
+
shortest = array;
|
|
240
267
|
longest = Math.max(longest, array.length);
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
else
|
|
268
|
+
partial ||= array.unknownLength !== undefined;
|
|
269
|
+
if (!array.more)
|
|
244
270
|
end = Math.min(end, array.length);
|
|
245
271
|
}
|
|
246
272
|
// Validation rejects a predicate that compares no collection.
|
|
247
|
-
if (arrays.size === 0)
|
|
273
|
+
if (!shortest || arrays.size === 0)
|
|
248
274
|
return NO_PAIRS;
|
|
249
|
-
|
|
275
|
+
if (!partial)
|
|
276
|
+
return { arrays, end, tailFrom: end };
|
|
277
|
+
const all = paired ? [paired, ...arrays.values()] : [...arrays.values()];
|
|
278
|
+
const tail = end === Infinity ? all[0].more : undefined;
|
|
250
279
|
if (end === Infinity)
|
|
251
280
|
end = longest;
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
array.elements = Array(array.length).fill(array.more);
|
|
258
|
-
}
|
|
281
|
+
if (describeSameItems(arrays, source)) {
|
|
282
|
+
// Each index pairs the elements of one item, which the collections have or lack together.
|
|
283
|
+
const [{ elements, length, shortest: certain }] = all;
|
|
284
|
+
const missing = certain < length ? (index) => missingReason(elements[index]) : undefined;
|
|
285
|
+
return { arrays, end, missing, tail, tailFrom: Math.min(...all.map(array => array.moreFrom ?? array.length)) };
|
|
259
286
|
}
|
|
260
|
-
|
|
287
|
+
// Past its leading elements, a collection that may lack or hold others has its elements at positions it cannot
|
|
288
|
+
// tell, but for the last, which is the only one that can be at the last position when it lists them all.
|
|
289
|
+
for (const array of all) {
|
|
290
|
+
const { leading = array.length, more, unknownLength } = array;
|
|
291
|
+
if (!unknownLength)
|
|
292
|
+
continue;
|
|
293
|
+
const last = more ? -1 : array.length - 1;
|
|
294
|
+
array.elements = array.elements.map((listed, index) => index < leading || index === last ? listed : unknownLength);
|
|
295
|
+
}
|
|
296
|
+
const { shortest: certain, unknownLength } = shortest;
|
|
297
|
+
return { arrays, end, missing: index => index < certain ? undefined : unknownLength, tail, tailFrom: end };
|
|
298
|
+
}
|
|
299
|
+
/** Why an array may lack an element it lists, when it may. */
|
|
300
|
+
function missingReason(listed) {
|
|
301
|
+
return listed instanceof OptionalElement ? listed.reason : undefined;
|
|
261
302
|
}
|
|
262
303
|
/**
|
|
263
304
|
* Evaluates any() or all() as Cloudflare does, over the pairs of its
|
|
@@ -267,12 +308,12 @@ function pairArrays(owner, predicate, ctx, valueCall) {
|
|
|
267
308
|
export function evaluatePairedQuantifier(node, reading, ctx) {
|
|
268
309
|
const settling = node.name === 'any';
|
|
269
310
|
const { predicate } = reading;
|
|
270
|
-
const { arrays,
|
|
311
|
+
const { arrays, end, missing, tail, nilCall } = pairArrays(node, predicate, ctx, reading.kind === 'value' ? reading.predicate : undefined);
|
|
271
312
|
// A call read as a value that gives nil is no array, which neither any() nor all() holds for; see
|
|
272
313
|
// QuantifierPredicateReading.
|
|
273
314
|
if (nilCall)
|
|
274
315
|
return { outcome: false, paired: 0, mayLackSome: false, mayHaveMore: false, nilCall };
|
|
275
|
-
const result = (outcome) => ({ outcome, paired: end, mayLackSome: end
|
|
316
|
+
const result = (outcome) => ({ outcome, paired: end, mayLackSome: firstMissing(end, missing) !== undefined, mayHaveMore: tail !== undefined });
|
|
276
317
|
// The unknown outcomes of known elements, from every position, and the first outcome that reads an unknown
|
|
277
318
|
// element, which is combined after them, so that an unknown comparison value, which applies at every
|
|
278
319
|
// element, names the cause before one element does.
|
|
@@ -280,14 +321,12 @@ export function evaluatePairedQuantifier(node, reading, ctx) {
|
|
|
280
321
|
let pending;
|
|
281
322
|
for (let index = 0; index < end; index++) {
|
|
282
323
|
let outcome = pairedOutcome(predicate, index, arrays);
|
|
283
|
-
if (
|
|
284
|
-
//
|
|
285
|
-
|
|
286
|
-
if (
|
|
287
|
-
outcome
|
|
288
|
-
|
|
289
|
-
else if (outcome === settling) {
|
|
290
|
-
return result(outcome);
|
|
324
|
+
if (outcome === settling) {
|
|
325
|
+
// One that may be missing settles the result only if it is there.
|
|
326
|
+
const reason = missing?.(index);
|
|
327
|
+
if (!reason)
|
|
328
|
+
return result(outcome);
|
|
329
|
+
outcome = reason;
|
|
291
330
|
}
|
|
292
331
|
if (typeof outcome === 'function')
|
|
293
332
|
pending ??= outcome;
|
|
@@ -305,71 +344,108 @@ export function evaluatePairedQuantifier(node, reading, ctx) {
|
|
|
305
344
|
/**
|
|
306
345
|
* Evaluates a function argument read as a condition with a result for each
|
|
307
346
|
* element: the array of its results, at the pairs of its collections (see
|
|
308
|
-
* PairedArrays), with none over nil. An unknown result keeps its place
|
|
309
|
-
* when the shortest collection may hold more, so may the array, which lists
|
|
310
|
-
* the results that may follow when it knows them all.
|
|
347
|
+
* PairedArrays), with none over nil. An unknown result keeps its place.
|
|
311
348
|
*/
|
|
312
349
|
function evaluateConditionArray(node, ctx) {
|
|
313
|
-
const
|
|
314
|
-
const
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
results.push(resultElement(node, index, arrays, ctx));
|
|
321
|
-
return known(PartialArrayValue.from(results.slice(0, certain), moreResults(pairing), tail ? undefined : results.slice(certain)));
|
|
350
|
+
const { arrays, end, missing, tail, tailFrom } = pairArrays(node, node, ctx);
|
|
351
|
+
const results = resultElements(node, end, arrays, ctx);
|
|
352
|
+
return known(PartialArrayValue.from(missing ? results.map((result, index) => optionalElement(result, missing(index))) : results, tail, tailFrom));
|
|
353
|
+
}
|
|
354
|
+
/** An element that the array may lack for a reason, when there is one. */
|
|
355
|
+
function optionalElement(element, reason) {
|
|
356
|
+
return reason ? new OptionalElement(element, reason) : element;
|
|
322
357
|
}
|
|
323
358
|
/** The number of results evaluateConditionArray() gives, which reads none of them. */
|
|
324
359
|
function countConditionResults(node, ctx) {
|
|
325
|
-
const
|
|
326
|
-
return
|
|
360
|
+
const { end, missing, tail } = pairArrays(node, node, ctx);
|
|
361
|
+
return (firstMissing(end, missing) ?? tail)?.() ?? known(end);
|
|
327
362
|
}
|
|
328
|
-
/** Why
|
|
329
|
-
function
|
|
330
|
-
|
|
363
|
+
/** Why the first pair that may be missing may be, when one may. */
|
|
364
|
+
function firstMissing(end, missing) {
|
|
365
|
+
if (!missing)
|
|
366
|
+
return undefined;
|
|
367
|
+
for (let index = 0; index < end; index++) {
|
|
368
|
+
const reason = missing(index);
|
|
369
|
+
if (reason)
|
|
370
|
+
return reason;
|
|
371
|
+
}
|
|
372
|
+
return undefined;
|
|
331
373
|
}
|
|
332
374
|
/**
|
|
333
|
-
* The
|
|
334
|
-
* not known settles nothing, and tells why only when
|
|
335
|
-
* element does.
|
|
375
|
+
* The results at the first `count` indexes as elements of the array of
|
|
376
|
+
* results. One that is not known settles nothing, and tells why only when
|
|
377
|
+
* read, as an unknown element does. Results unknown for one cause, as where
|
|
378
|
+
* they compare with an unknown value, share an element, as there can be
|
|
379
|
+
* thousands.
|
|
336
380
|
*/
|
|
337
|
-
function
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
|
|
381
|
+
function resultElements(node, count, arrays, ctx) {
|
|
382
|
+
const results = [];
|
|
383
|
+
let last;
|
|
384
|
+
for (let index = 0; index < count; index++) {
|
|
385
|
+
const { result: outcome, warnings } = collectWarnings(ctx, () => pairedOutcome(node, index, arrays));
|
|
386
|
+
if (typeof outcome === 'boolean') {
|
|
387
|
+
giveWarnings(ctx, warnings);
|
|
388
|
+
results.push(known(outcome));
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
const cause = typeof outcome === 'function' ? outcome : outcome.cause;
|
|
392
|
+
if (last?.cause !== cause || !sameWarnings(last.warnings, warnings)) {
|
|
393
|
+
const read = () => outcomeValue(typeof outcome === 'function' ? outcome() : outcome);
|
|
394
|
+
last = { cause, warnings, element: deferWarnings(ctx, warnings, read) };
|
|
395
|
+
}
|
|
396
|
+
results.push(last.element);
|
|
341
397
|
}
|
|
342
|
-
|
|
343
|
-
|
|
398
|
+
return results;
|
|
399
|
+
}
|
|
400
|
+
function sameWarnings(left, right) {
|
|
401
|
+
return left.length === right.length && left.every((warning, index) => warning === right[index]);
|
|
344
402
|
}
|
|
345
403
|
/**
|
|
346
404
|
* Whether a predicate's collections describe the same item at each index:
|
|
347
|
-
* they come from one list (see pairedArrayList()), and
|
|
348
|
-
*
|
|
349
|
-
* a Set-Cookie value repeated past a cut header list,
|
|
405
|
+
* they come from one list (see pairedArrayList()), and list as many elements,
|
|
406
|
+
* and may lack the same ones. An element dropped, as a mapped call's nil
|
|
407
|
+
* result, or added, as a Set-Cookie value repeated past a cut header list,
|
|
408
|
+
* changes how many there are.
|
|
350
409
|
*/
|
|
351
|
-
function describeSameItems(arrays,
|
|
352
|
-
|
|
353
|
-
|
|
410
|
+
function describeSameItems(arrays, source) {
|
|
411
|
+
if (arrays.size === 1 && !source)
|
|
412
|
+
return true;
|
|
413
|
+
const nodes = [...arrays.keys()].map(conditionArray);
|
|
414
|
+
const all = [...arrays.values()];
|
|
415
|
+
if (source) {
|
|
416
|
+
nodes.push(...pairedArgumentArrays(source.node));
|
|
417
|
+
all.push(source.array);
|
|
418
|
+
}
|
|
419
|
+
const [first] = all;
|
|
420
|
+
for (const { elements, length, shortest } of all) {
|
|
421
|
+
if (length !== first.length || shortest !== first.shortest)
|
|
422
|
+
return false;
|
|
423
|
+
if (shortest === length)
|
|
424
|
+
continue;
|
|
425
|
+
if (elements.some((listed, index) => (listed instanceof OptionalElement) !== (first.elements[index] instanceof OptionalElement)))
|
|
354
426
|
return false;
|
|
355
427
|
}
|
|
356
|
-
return !unalignedArray(
|
|
357
|
-
}
|
|
358
|
-
/**
|
|
359
|
-
function
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
428
|
+
return !unalignedArray(nodes);
|
|
429
|
+
}
|
|
430
|
+
/** Whether a call's result holds at an element: where it is true. */
|
|
431
|
+
function isTrueValue(value) {
|
|
432
|
+
return value === true;
|
|
433
|
+
}
|
|
434
|
+
/** A condition's array, or a PairedSource's, from its evaluated collection and the warnings evaluating it gave. */
|
|
435
|
+
function pairedArray(evaluated, warnings, compare, ctx) {
|
|
436
|
+
const array = isUnknown(evaluated) ? evaluated : arrayValue(evaluated.value);
|
|
437
|
+
if (isUnknown(array)) {
|
|
438
|
+
// A collection unknown whole tells why only when read, since the other collections may settle the result.
|
|
439
|
+
const more = deferWarnings(ctx, warnings, () => array);
|
|
440
|
+
return { values: undefined, elements: [], length: 0, shortest: 0, more, unknownLength: more, compare };
|
|
365
441
|
}
|
|
366
|
-
const array = isUnknown(evaluated) ? evaluated : arrayElements(evaluated.value);
|
|
367
|
-
// A collection unknown whole tells why only when read, since the other collections may settle the result.
|
|
368
|
-
if (isUnknown(array))
|
|
369
|
-
return { values: undefined, elements: [], length: 0, more: deferWarnings(ctx, warnings, () => array), compare };
|
|
370
442
|
giveWarnings(ctx, warnings);
|
|
371
|
-
|
|
372
|
-
|
|
443
|
+
if (!(array.value instanceof PartialArrayValue)) {
|
|
444
|
+
const { length } = array.value;
|
|
445
|
+
return { values: array.value, elements: [], length, shortest: length, compare };
|
|
446
|
+
}
|
|
447
|
+
const { elements, shortest, more, moreFrom, leading, unknownLength } = array.value;
|
|
448
|
+
return { values: undefined, elements, length: elements.length, shortest, more, moreFrom, leading, unknownLength, compare };
|
|
373
449
|
}
|
|
374
450
|
/**
|
|
375
451
|
* Runs `compute` with warnings of its own, to give when its result is read:
|
|
@@ -427,9 +503,13 @@ function elementOutcome(array, index) {
|
|
|
427
503
|
// A collection known whole holds no more, so it reaches every position read.
|
|
428
504
|
if (array.values)
|
|
429
505
|
return array.compare(array.values[index]);
|
|
430
|
-
const element = array
|
|
506
|
+
const element = elementAt(array, index);
|
|
431
507
|
return isUnknownElement(element) ? element : array.compare(element.value);
|
|
432
508
|
}
|
|
509
|
+
/** The element at an index of a collection known in part, unknown where it lists none. */
|
|
510
|
+
function elementAt(array, index) {
|
|
511
|
+
return index < array.length ? listedElement(array.elements[index]) : array.more;
|
|
512
|
+
}
|
|
433
513
|
/** Combines operands at an index, stopping at the first known one that decides the result, if any does. */
|
|
434
514
|
function combinePairedOutcomes(operands, deciding, combine, index, arrays) {
|
|
435
515
|
const outcomes = [];
|
|
@@ -495,10 +575,41 @@ export function evaluateFuncValue(node, ctx) {
|
|
|
495
575
|
const evaluated = invocation.mapping === 'mapped'
|
|
496
576
|
? evaluateFuncArray(node, ctx, invocation)
|
|
497
577
|
: evaluateDirectFuncValue(node, ctx, invocation);
|
|
578
|
+
if (evaluated === TOO_LARGE)
|
|
579
|
+
return tooLargeResult(ctx, node.name);
|
|
498
580
|
if (isUnknown(evaluated))
|
|
499
581
|
return evaluated;
|
|
582
|
+
// Only concat() and mapped calls can make an array too large, and they count it as they get its elements.
|
|
583
|
+
if (typeof evaluated.value === 'string' && evaluated.value.length > MAX_RESULT_SIZE)
|
|
584
|
+
return tooLargeResult(ctx, node.name);
|
|
500
585
|
return applyRuntimeAccessPath(evaluated.value, node.accessPath);
|
|
501
586
|
}
|
|
587
|
+
/** How many characters an array element counts as. An unknown one is not read, as reading it warns. */
|
|
588
|
+
function elementSize(element) {
|
|
589
|
+
return isUnknownElement(element) ? MIN_ELEMENT_SIZE : valueSize(element.value);
|
|
590
|
+
}
|
|
591
|
+
function valueSize(value) {
|
|
592
|
+
let size = 0;
|
|
593
|
+
if (typeof value === 'string')
|
|
594
|
+
size = value.length;
|
|
595
|
+
else if (Array.isArray(value))
|
|
596
|
+
for (const element of value)
|
|
597
|
+
size += valueSize(element);
|
|
598
|
+
return Math.max(size, MIN_ELEMENT_SIZE);
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Whether a result joined from pieces that hold `size` characters in all is
|
|
602
|
+
* surely too large. Joining text can unite bytes into characters, which leaves
|
|
603
|
+
* a third of their number at least, so where a piece is text with such bytes,
|
|
604
|
+
* only a result past MAX_JOINED_SIZE surely is.
|
|
605
|
+
*/
|
|
606
|
+
function surelyTooLarge(size, ...pieces) {
|
|
607
|
+
if (size <= MAX_RESULT_SIZE)
|
|
608
|
+
return false;
|
|
609
|
+
if (size > MAX_JOINED_SIZE)
|
|
610
|
+
return true;
|
|
611
|
+
return !pieces.some(list => list.some(piece => typeof piece === 'string' && !piece.isWellFormed()));
|
|
612
|
+
}
|
|
502
613
|
/**
|
|
503
614
|
* Evaluates a call on its arguments, or one that maps on one element:
|
|
504
615
|
* `element` is then its first argument, and `later` keeps the arguments
|
|
@@ -519,16 +630,12 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
519
630
|
case 'any':
|
|
520
631
|
return outcomeValue(evaluateQuantifier(node, ctx));
|
|
521
632
|
case 'concat': {
|
|
522
|
-
const parts =
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
if (isUnknown(part))
|
|
526
|
-
return part;
|
|
527
|
-
parts.push(part.value);
|
|
528
|
-
}
|
|
633
|
+
const parts = readConcatArguments(args.length, runtimeArgument, read, () => tooLargeResult(ctx, name));
|
|
634
|
+
if (parts === TOO_LARGE || isUnknown(parts))
|
|
635
|
+
return parts;
|
|
529
636
|
// An array known in part keeps its known elements, as any() and all() read them.
|
|
530
|
-
if (parts.some(part => part instanceof PartialArrayValue))
|
|
531
|
-
return concatElements(parts, invocation.argumentTypes);
|
|
637
|
+
if (parts.value.some(part => part instanceof PartialArrayValue))
|
|
638
|
+
return concatElements(parts.value, invocation.argumentTypes);
|
|
532
639
|
break;
|
|
533
640
|
}
|
|
534
641
|
case 'coalesce':
|
|
@@ -539,6 +646,8 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
539
646
|
return value;
|
|
540
647
|
}
|
|
541
648
|
return known(null);
|
|
649
|
+
case 'filter':
|
|
650
|
+
return later ? filterValues(element, argument(1)) : evaluateFilter(node, ctx);
|
|
542
651
|
case 'has_key': {
|
|
543
652
|
// A nil key gives nil without reading the map, as Cloudflare documents.
|
|
544
653
|
const key = argument(1);
|
|
@@ -557,7 +666,7 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
557
666
|
return collection;
|
|
558
667
|
// A partly known array's known elements can settle it, as they do any().
|
|
559
668
|
const partial = collection.value instanceof PartialArrayValue ? collection.value : undefined;
|
|
560
|
-
const values = partial ? known(partial.knownValues()
|
|
669
|
+
const values = partial ? known(partial.knownValues()) : ruleValue(collection);
|
|
561
670
|
if (isUnknown(values))
|
|
562
671
|
return values;
|
|
563
672
|
const value = argument(1);
|
|
@@ -577,9 +686,9 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
577
686
|
const source = runtimeArgument(0);
|
|
578
687
|
if (isUnknown(source))
|
|
579
688
|
return source;
|
|
580
|
-
// Unknown elements keep their places, so only
|
|
689
|
+
// Unknown elements keep their places, so only elements it may lack or not list leave the length unknown.
|
|
581
690
|
if (source.value instanceof PartialArrayValue)
|
|
582
|
-
return source.value.
|
|
691
|
+
return source.value.unknownLength?.() ?? known(source.value.elements.length);
|
|
583
692
|
const value = ruleValue(source);
|
|
584
693
|
return isUnknown(value) ? value : known(lengthOfValue(value.value));
|
|
585
694
|
}
|
|
@@ -588,6 +697,7 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
588
697
|
case 'lookup_json_string':
|
|
589
698
|
return lookupJsonValue('string', args.length, argument);
|
|
590
699
|
case 'bit_slice':
|
|
700
|
+
case 'hmac_sha256':
|
|
591
701
|
case 'is_jwt_present':
|
|
592
702
|
case 'is_jwt_valid':
|
|
593
703
|
case 'is_timed_hmac_valid_v0':
|
|
@@ -607,7 +717,9 @@ function evaluateDirectFuncValue(node, ctx, invocation, later, element = null) {
|
|
|
607
717
|
values.push(value.value);
|
|
608
718
|
}
|
|
609
719
|
const value = functionResult(name, values, invocation);
|
|
610
|
-
|
|
720
|
+
if (value === undefined)
|
|
721
|
+
return unavailable(functionSource(name));
|
|
722
|
+
return value === TOO_LARGE ? value : known(canonicalResult(value));
|
|
611
723
|
}
|
|
612
724
|
/**
|
|
613
725
|
* Puts each string of a result in the one form byte-strings.ts gives its
|
|
@@ -646,6 +758,8 @@ function functionResult(name, values, invocation) {
|
|
|
646
758
|
return decodeBase64Value(at(0));
|
|
647
759
|
case 'encode_base64':
|
|
648
760
|
return encodeBase64Value(at(0), at(1));
|
|
761
|
+
case 'encode_hex':
|
|
762
|
+
return encodeHexValue(at(0));
|
|
649
763
|
case 'ends_with':
|
|
650
764
|
case 'starts_with':
|
|
651
765
|
return matchesAffix(name, at(0), at(1));
|
|
@@ -685,10 +799,13 @@ function evaluateFuncArray(node, ctx, invocation) {
|
|
|
685
799
|
// Over nil, such as a missing header's values, the call gives nil, not an empty array.
|
|
686
800
|
if (!isUnknown(source) && source.value === null)
|
|
687
801
|
return source;
|
|
688
|
-
const array = isUnknown(source) ? source :
|
|
802
|
+
const array = isUnknown(source) ? source : arrayValue(source.value);
|
|
689
803
|
if (isUnknown(array))
|
|
690
804
|
return array;
|
|
691
|
-
const {
|
|
805
|
+
const { value } = array;
|
|
806
|
+
const { elements, more, moreFrom } = value instanceof PartialArrayValue
|
|
807
|
+
? value
|
|
808
|
+
: { elements: value.map(element => known(element)), more: undefined, moreFrom: value.length };
|
|
692
809
|
const later = [];
|
|
693
810
|
// A nil later argument gives every element nil, unknown ones too, so the call gives an empty array.
|
|
694
811
|
if ((elements.length > 0 || more) && givesNilForNilArgument(node.name)) {
|
|
@@ -700,27 +817,80 @@ function evaluateFuncArray(node, ctx, invocation) {
|
|
|
700
817
|
return known([]);
|
|
701
818
|
}
|
|
702
819
|
}
|
|
820
|
+
// What a nil second argument gives is not known, so neither is the array's length; see NEVER_NIL_FUNCTIONS.
|
|
821
|
+
if ((elements.length > 0 || more) && node.name === 'hmac_sha256') {
|
|
822
|
+
const value = (later[1] = evaluateArgValue(node.args[1], ctx));
|
|
823
|
+
if (isUnknown(value))
|
|
824
|
+
return value;
|
|
825
|
+
if (value.value === null)
|
|
826
|
+
return unreliableFunction(ctx, node.name);
|
|
827
|
+
}
|
|
703
828
|
let keepsPlaces = keepsPlacesWhenMapped(node.name);
|
|
829
|
+
const tooLarge = () => tooLargeResult(ctx, node.name);
|
|
830
|
+
const evaluate = (element) => {
|
|
831
|
+
const direct = evaluateDirectFuncValue(node, ctx, invocation, later, element);
|
|
832
|
+
return direct === TOO_LARGE ? direct : ruleValue(direct);
|
|
833
|
+
};
|
|
834
|
+
// Elements it may lack leave the others their whole budget, and those with an unknown result warn only when
|
|
835
|
+
// read. The first may have read a later argument for all, so they share their warnings.
|
|
836
|
+
let size = 0;
|
|
837
|
+
let optionalSize = 0;
|
|
838
|
+
let optionalWarnings;
|
|
839
|
+
let resultsFrom = 0;
|
|
704
840
|
const results = [];
|
|
705
|
-
for (
|
|
841
|
+
for (let index = 0; index < elements.length; index++) {
|
|
842
|
+
const listed = elements[index];
|
|
843
|
+
const optional = listed instanceof OptionalElement;
|
|
844
|
+
const element = listedElement(listed);
|
|
845
|
+
// The results that fit can still settle what reads them, so the ones past them are unknown elements.
|
|
846
|
+
let result = tooLarge;
|
|
706
847
|
if (isUnknownElement(element)) {
|
|
707
|
-
|
|
708
|
-
continue;
|
|
848
|
+
result = element;
|
|
709
849
|
}
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
850
|
+
else if ((optional ? optionalSize : size) <= MAX_RESULT_SIZE) {
|
|
851
|
+
let evaluated;
|
|
852
|
+
if (optional) {
|
|
853
|
+
const collected = collectWarnings(ctx, () => evaluate(element.value));
|
|
854
|
+
evaluated = collected.result;
|
|
855
|
+
optionalWarnings ??= new Set();
|
|
856
|
+
for (const warning of collected.warnings)
|
|
857
|
+
optionalWarnings.add(warning);
|
|
858
|
+
}
|
|
859
|
+
else {
|
|
860
|
+
evaluated = evaluate(element.value);
|
|
861
|
+
}
|
|
862
|
+
if (evaluated !== TOO_LARGE) {
|
|
863
|
+
if (isUnknown(evaluated)) {
|
|
864
|
+
const unknown = evaluated;
|
|
865
|
+
result = deferWarnings(ctx, optionalWarnings ?? [], () => unknown);
|
|
866
|
+
if (!optional)
|
|
867
|
+
return result();
|
|
868
|
+
}
|
|
869
|
+
else if (evaluated.value === null) {
|
|
870
|
+
// A listed function that gives nil after all may for an unknown element too.
|
|
871
|
+
keepsPlaces = false;
|
|
872
|
+
continue;
|
|
873
|
+
}
|
|
874
|
+
else {
|
|
875
|
+
result = evaluated;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
const total = optional ? (optionalSize += elementSize(result)) : (size += elementSize(result));
|
|
880
|
+
if (total > MAX_RESULT_SIZE)
|
|
881
|
+
result = tooLarge;
|
|
882
|
+
results.push(inPlaceOf(listed, result));
|
|
883
|
+
if (index < moreFrom)
|
|
884
|
+
resultsFrom = results.length;
|
|
718
885
|
}
|
|
719
886
|
if (keepsPlaces)
|
|
720
|
-
return known(PartialArrayValue.from(results, more));
|
|
721
|
-
// An unknown element may give nil, which drops it
|
|
722
|
-
const
|
|
723
|
-
|
|
887
|
+
return known(PartialArrayValue.from(results, more, resultsFrom));
|
|
888
|
+
// An unknown element may give nil, which drops it.
|
|
889
|
+
const droppable = results.map(listed => {
|
|
890
|
+
const element = listedElement(listed);
|
|
891
|
+
return isUnknownElement(element) ? new OptionalElement(element, element) : listed;
|
|
892
|
+
});
|
|
893
|
+
return known(PartialArrayValue.from(droppable, more, resultsFrom));
|
|
724
894
|
}
|
|
725
895
|
function evaluateArgValue(arg, ctx) {
|
|
726
896
|
// An omitted optional argument is null, which functions treat as absent.
|
|
@@ -796,6 +966,47 @@ function nonValueArgumentSource(arg) {
|
|
|
796
966
|
return `list '$${arg.name}'`;
|
|
797
967
|
return 'an inline list';
|
|
798
968
|
}
|
|
969
|
+
/**
|
|
970
|
+
* Evaluates filter(): its array's elements at the positions where its
|
|
971
|
+
* conditions hold, as Cloudflare pairs them by index and stops at the shorter
|
|
972
|
+
* array. An unknown condition may keep its element or not, so the result may
|
|
973
|
+
* lack it, as it may one whose position an array may lack. Validation rejects
|
|
974
|
+
* arguments that may be nil.
|
|
975
|
+
*/
|
|
976
|
+
function evaluateFilter(node, ctx) {
|
|
977
|
+
const [arrayNode, predicate] = node.args;
|
|
978
|
+
// No field is an array of Booleans, so only a condition or a call gives one.
|
|
979
|
+
if (!isExpressionNode(predicate))
|
|
980
|
+
return unavailable(functionSource(node.name));
|
|
981
|
+
const { result: evaluated, warnings } = collectWarnings(ctx, () => evaluateArgValue(arrayNode, ctx));
|
|
982
|
+
const array = pairedArray(evaluated, warnings, isTrueValue, ctx);
|
|
983
|
+
const { arrays, end, missing, tail, tailFrom } = pairArrays(predicate, predicate, ctx, undefined, { node: arrayNode, array });
|
|
984
|
+
const kept = [];
|
|
985
|
+
let keptFrom = 0;
|
|
986
|
+
resultElements(predicate, end, arrays, ctx).forEach((result, index) => {
|
|
987
|
+
const unknown = isUnknownElement(result);
|
|
988
|
+
if (!unknown && result.value !== true)
|
|
989
|
+
return;
|
|
990
|
+
const element = array.values ? known(array.values[index]) : elementAt(array, index);
|
|
991
|
+
kept.push(optionalElement(element, unknown ? result : missing?.(index)));
|
|
992
|
+
if (index < tailFrom)
|
|
993
|
+
keptFrom = kept.length;
|
|
994
|
+
});
|
|
995
|
+
return known(PartialArrayValue.from(kept, tail, keptFrom));
|
|
996
|
+
}
|
|
997
|
+
/**
|
|
998
|
+
* Evaluates filter() on one element of the arrays it maps over. An unknown
|
|
999
|
+
* condition leaves the whole call unknown, its length too, as an unknown later
|
|
1000
|
+
* argument leaves any call that maps, even where the element does not reach
|
|
1001
|
+
* it: no list lines up with each of the arrays, so such a call gets a warning
|
|
1002
|
+
* anyway.
|
|
1003
|
+
*/
|
|
1004
|
+
function filterValues(array, conditions) {
|
|
1005
|
+
if (isUnknown(conditions))
|
|
1006
|
+
return conditions;
|
|
1007
|
+
const { value } = conditions;
|
|
1008
|
+
return known(Array.isArray(array) && Array.isArray(value) ? array.filter((_, index) => value[index] === true) : []);
|
|
1009
|
+
}
|
|
799
1010
|
/** Returns nil (null) for a nil collection or value, as Cloudflare documents. */
|
|
800
1011
|
function evaluateHasValue(collection, value) {
|
|
801
1012
|
if (collection === null || value === null)
|
|
@@ -812,6 +1023,63 @@ function matchesAffix(name, value, affix) {
|
|
|
812
1023
|
? String(value).startsWith(String(affix))
|
|
813
1024
|
: String(value).endsWith(String(affix));
|
|
814
1025
|
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Reads the arguments of concat() until one is unknown, or the result is
|
|
1028
|
+
* surely too large; arrays end after the elements that fit. Those that arrays
|
|
1029
|
+
* may lack have a budget of their own, as sharing the others' would make the
|
|
1030
|
+
* result depend on the order of the arguments, and arrays do not list those
|
|
1031
|
+
* past it: the result can do without. `read`, which keeps the arguments, loses
|
|
1032
|
+
* them too.
|
|
1033
|
+
*/
|
|
1034
|
+
function readConcatArguments(count, argument, read, tooLarge) {
|
|
1035
|
+
const parts = [];
|
|
1036
|
+
let size = 0;
|
|
1037
|
+
let limit = MAX_RESULT_SIZE;
|
|
1038
|
+
let elementCount = 0;
|
|
1039
|
+
let optionalCount = 0;
|
|
1040
|
+
for (let index = 0; index < count; index++) {
|
|
1041
|
+
const part = argument(index);
|
|
1042
|
+
if (isUnknown(part))
|
|
1043
|
+
return part;
|
|
1044
|
+
let { value } = part;
|
|
1045
|
+
if (value instanceof PartialArrayValue) {
|
|
1046
|
+
optionalCount += value.elements.length - value.shortest;
|
|
1047
|
+
if (optionalCount > MAX_CONCAT_ELEMENTS) {
|
|
1048
|
+
value = value.withoutOptional();
|
|
1049
|
+
read[index] = known(value);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
if (Array.isArray(value) || value instanceof PartialArrayValue) {
|
|
1053
|
+
const room = MAX_CONCAT_ELEMENTS - elementCount;
|
|
1054
|
+
elementCount += value instanceof PartialArrayValue ? value.shortest : value.length;
|
|
1055
|
+
if (elementCount > MAX_CONCAT_ELEMENTS) {
|
|
1056
|
+
// The elements that fit can still settle any() and has_value().
|
|
1057
|
+
parts.push(firstElements(value, room, tooLarge));
|
|
1058
|
+
return known(parts);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
parts.push(value);
|
|
1062
|
+
if (typeof value === 'string')
|
|
1063
|
+
size += value.length;
|
|
1064
|
+
if (size <= limit)
|
|
1065
|
+
continue;
|
|
1066
|
+
if (surelyTooLarge(size, parts))
|
|
1067
|
+
return TOO_LARGE;
|
|
1068
|
+
// A part has bytes that can unite, so the parts are not scanned again.
|
|
1069
|
+
limit = MAX_JOINED_SIZE;
|
|
1070
|
+
}
|
|
1071
|
+
return known(parts);
|
|
1072
|
+
}
|
|
1073
|
+
/** An array up to its first `count` elements, of those it surely has, as one that `more` says holds more. */
|
|
1074
|
+
function firstElements(array, count, more) {
|
|
1075
|
+
if (Array.isArray(array))
|
|
1076
|
+
return PartialArrayValue.from(array.slice(0, count).map(value => known(value)), more, count);
|
|
1077
|
+
let end = 0;
|
|
1078
|
+
for (let left = count; left > 0; end++)
|
|
1079
|
+
if (!(array.elements[end] instanceof OptionalElement))
|
|
1080
|
+
left--;
|
|
1081
|
+
return PartialArrayValue.from(array.elements.slice(0, end), more, Math.min(array.moreFrom, end));
|
|
1082
|
+
}
|
|
815
1083
|
function concatValues(values, argumentTypes) {
|
|
816
1084
|
if (values.length < 2 || concatTypesDiffer(argumentTypes))
|
|
817
1085
|
return null;
|
|
@@ -835,30 +1103,25 @@ function concatTypesDiffer(argumentTypes) {
|
|
|
835
1103
|
}
|
|
836
1104
|
/**
|
|
837
1105
|
* concat() over arrays some of which are known only in part: the result
|
|
838
|
-
*
|
|
839
|
-
*
|
|
840
|
-
*
|
|
841
|
-
* value that is no array gives nil, as in concatValues(); a map is unknown,
|
|
842
|
-
* as in ruleValue().
|
|
1106
|
+
* lists each one's elements in turn, and may lack or hold others where one
|
|
1107
|
+
* may. A value that is no array gives nil, as in concatValues(); a map is
|
|
1108
|
+
* unknown, as in ruleValue().
|
|
843
1109
|
*/
|
|
844
1110
|
function concatElements(parts, argumentTypes) {
|
|
845
|
-
if (
|
|
1111
|
+
if (argumentTypes.length < 2 || concatTypesDiffer(argumentTypes))
|
|
846
1112
|
return known(null);
|
|
847
1113
|
const elements = [];
|
|
848
1114
|
let more;
|
|
849
|
-
let
|
|
1115
|
+
let moreFrom = 0;
|
|
850
1116
|
for (const part of parts) {
|
|
851
1117
|
if (part instanceof RuleMapValue)
|
|
852
1118
|
return unavailable('a map value');
|
|
853
1119
|
if (part instanceof PartialArrayValue) {
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
1120
|
+
if (!more)
|
|
1121
|
+
moreFrom = elements.length + part.moreFrom;
|
|
1122
|
+
for (const element of part.elements)
|
|
1123
|
+
elements.push(element);
|
|
857
1124
|
more ??= part.more;
|
|
858
|
-
if (part.optional)
|
|
859
|
-
optional?.push(...part.optional);
|
|
860
|
-
else
|
|
861
|
-
optional = undefined;
|
|
862
1125
|
}
|
|
863
1126
|
else if (Array.isArray(part)) {
|
|
864
1127
|
for (const value of part)
|
|
@@ -868,7 +1131,7 @@ function concatElements(parts, argumentTypes) {
|
|
|
868
1131
|
return known(null);
|
|
869
1132
|
}
|
|
870
1133
|
}
|
|
871
|
-
return known(PartialArrayValue.from(elements, more,
|
|
1134
|
+
return known(PartialArrayValue.from(elements, more, moreFrom));
|
|
872
1135
|
}
|
|
873
1136
|
/** Cloudflare counts the bytes of a string. */
|
|
874
1137
|
function lengthOfValue(value) {
|
|
@@ -896,13 +1159,39 @@ function encodeBase64Value(value, flagsValue) {
|
|
|
896
1159
|
if (value === null)
|
|
897
1160
|
return null;
|
|
898
1161
|
const flags = flagsValue === null ? '' : String(flagsValue);
|
|
899
|
-
|
|
1162
|
+
const bytes = bytesToEncode(String(value), 4 / 3);
|
|
1163
|
+
if (bytes === TOO_LARGE)
|
|
1164
|
+
return bytes;
|
|
1165
|
+
let encoded = bytesToBase64(bytes);
|
|
900
1166
|
if (flags.includes('u'))
|
|
901
1167
|
encoded = encoded.replaceAll('+', '-').replaceAll('/', '_');
|
|
902
1168
|
if (!flags.includes('p'))
|
|
903
1169
|
encoded = encoded.replace(/=+$/, '');
|
|
904
1170
|
return encoded;
|
|
905
1171
|
}
|
|
1172
|
+
/** The bytes of a text, unless an encoding that writes `ratio` characters for each would hold too much. */
|
|
1173
|
+
function bytesToEncode(text, ratio) {
|
|
1174
|
+
// Each character is at least one byte.
|
|
1175
|
+
if (text.length * ratio > MAX_RESULT_SIZE)
|
|
1176
|
+
return TOO_LARGE;
|
|
1177
|
+
const bytes = textToBytes(text);
|
|
1178
|
+
return Math.ceil(bytes.length * ratio) > MAX_RESULT_SIZE ? TOO_LARGE : bytes;
|
|
1179
|
+
}
|
|
1180
|
+
const HEX_DIGITS = new TextEncoder().encode('0123456789abcdef');
|
|
1181
|
+
/** Writes each byte as two lowercase hex digits. */
|
|
1182
|
+
function encodeHexValue(value) {
|
|
1183
|
+
if (value === null)
|
|
1184
|
+
return null;
|
|
1185
|
+
const bytes = bytesToEncode(String(value), 2);
|
|
1186
|
+
if (bytes === TOO_LARGE)
|
|
1187
|
+
return bytes;
|
|
1188
|
+
const digits = new Uint8Array(bytes.length * 2);
|
|
1189
|
+
for (let index = 0; index < bytes.length; index++) {
|
|
1190
|
+
digits[index * 2] = HEX_DIGITS[bytes[index] >> 4];
|
|
1191
|
+
digits[index * 2 + 1] = HEX_DIGITS[bytes[index] & 15];
|
|
1192
|
+
}
|
|
1193
|
+
return bytesToText(digits);
|
|
1194
|
+
}
|
|
906
1195
|
/**
|
|
907
1196
|
* Decodes standard Base64 as Cloudflare does, observed with live requests: it
|
|
908
1197
|
* skips whitespace and takes input with or without padding. Other input is
|
|
@@ -931,10 +1220,15 @@ function bytesToBase64(bytes) {
|
|
|
931
1220
|
binary += String.fromCharCode(byte);
|
|
932
1221
|
return globalThis.btoa(binary);
|
|
933
1222
|
}
|
|
934
|
-
function joinValues(items,
|
|
935
|
-
if (!Array.isArray(items) ||
|
|
1223
|
+
function joinValues(items, separatorValue) {
|
|
1224
|
+
if (!Array.isArray(items) || separatorValue === null)
|
|
936
1225
|
return null;
|
|
937
|
-
|
|
1226
|
+
const texts = items.map(item => String(item));
|
|
1227
|
+
const separator = String(separatorValue);
|
|
1228
|
+
let size = separator.length * Math.max(texts.length - 1, 0);
|
|
1229
|
+
for (const text of texts)
|
|
1230
|
+
size += text.length;
|
|
1231
|
+
return surelyTooLarge(size, texts, [separator]) ? TOO_LARGE : texts.join(separator);
|
|
938
1232
|
}
|
|
939
1233
|
/**
|
|
940
1234
|
* Cloudflare accepts one seed, and a nil seed gives nil. The bounds are integer
|
|
@@ -1038,16 +1332,27 @@ function jsonMember(container, key) {
|
|
|
1038
1332
|
function regexReplace(sourceValue, patternValue, replacementValue) {
|
|
1039
1333
|
if (sourceValue === null || patternValue === null || replacementValue === null)
|
|
1040
1334
|
return null;
|
|
1335
|
+
const source = String(sourceValue);
|
|
1336
|
+
let match;
|
|
1041
1337
|
try {
|
|
1042
|
-
|
|
1043
|
-
return String(sourceValue).replace(regex, (...matchParts) => {
|
|
1044
|
-
const captures = matchParts.slice(1, -2);
|
|
1045
|
-
return replaceRuleCaptureReferences(String(replacementValue), captures);
|
|
1046
|
-
});
|
|
1338
|
+
match = new RegExp(String(patternValue)).exec(source);
|
|
1047
1339
|
}
|
|
1048
1340
|
catch {
|
|
1049
|
-
return
|
|
1341
|
+
return source;
|
|
1050
1342
|
}
|
|
1343
|
+
return match ? replaceMatch(source, match.index, match, String(replacementValue)) : source;
|
|
1344
|
+
}
|
|
1345
|
+
/** Writes a replacement in place of a source's match, which is the first of the captures. */
|
|
1346
|
+
function replaceMatch(source, start, captures, replacement) {
|
|
1347
|
+
const before = source.slice(0, start);
|
|
1348
|
+
const after = source.slice(start + (captures[0]?.length ?? 0));
|
|
1349
|
+
// The size is counted only where it could be too large.
|
|
1350
|
+
if (replacement.length + source.length * (MAX_REPLACEMENT_REFERENCES + 1) > MAX_RESULT_SIZE) {
|
|
1351
|
+
const { length, written } = measureReplacement(replacement, captures);
|
|
1352
|
+
if (surelyTooLarge(before.length + length + after.length, [before, replacement, after], written))
|
|
1353
|
+
return TOO_LARGE;
|
|
1354
|
+
}
|
|
1355
|
+
return before + expandReplacement(replacement, captures) + after;
|
|
1051
1356
|
}
|
|
1052
1357
|
/** Removes each listed byte wherever it appears, as Cloudflare does, so a result can split a character. */
|
|
1053
1358
|
function removeBytes(sourceValue, removeValue) {
|
|
@@ -1122,20 +1427,9 @@ function wildcardReplaceValue(sourceValue, patternValue, replacementValue, flags
|
|
|
1122
1427
|
return wildcardReplace(String(sourceValue), String(patternValue), String(replacementValue), flagsValue === null ? '' : String(flagsValue));
|
|
1123
1428
|
}
|
|
1124
1429
|
function wildcardReplace(source, pattern, replacement, flags) {
|
|
1125
|
-
const
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
const match = regex.exec(source);
|
|
1129
|
-
if (!match)
|
|
1130
|
-
return source;
|
|
1131
|
-
return replaceRuleCaptureReferences(replacement, match.slice(1));
|
|
1132
|
-
}
|
|
1133
|
-
function replaceRuleCaptureReferences(replacement, captures) {
|
|
1134
|
-
return replacement.replace(/\$\$|\$\{([1-8])\}/g, (full, index) => {
|
|
1135
|
-
if (full === '$$')
|
|
1136
|
-
return '$';
|
|
1137
|
-
return captures[Number.parseInt(index ?? '0', 10) - 1]?.toString() ?? '';
|
|
1138
|
-
});
|
|
1430
|
+
const compiled = compileWildcard(pattern, flags.includes('s'));
|
|
1431
|
+
const captures = compiled && wildcardCaptures(compiled, source);
|
|
1432
|
+
return captures ? replaceMatch(source, 0, captures, replacement) : source;
|
|
1139
1433
|
}
|
|
1140
1434
|
/** Returns the network address, printed as Cloudflare prints IP addresses. */
|
|
1141
1435
|
function evaluateCidr(addressValue, ipv4BitsValue, ipv6BitsValue) {
|
|
@@ -1155,6 +1449,9 @@ function evaluateCidr(addressValue, ipv4BitsValue, ipv6BitsValue) {
|
|
|
1155
1449
|
}
|
|
1156
1450
|
return formatIpAddress(octets);
|
|
1157
1451
|
}
|
|
1452
|
+
function tooLargeResult(ctx, name) {
|
|
1453
|
+
return unreliableFunction(ctx, name, RESULT_SIZE_REASON);
|
|
1454
|
+
}
|
|
1158
1455
|
/** A function result this evaluator cannot reproduce is unknown. */
|
|
1159
1456
|
function unreliableFunction(ctx, name, reason = getUnreliableRuleExpressionFunctionReason(name)) {
|
|
1160
1457
|
if (reason)
|