@libreflare/worker-runtime 0.9.5 → 0.10.1

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.
Files changed (62) hide show
  1. package/README.md +310 -9
  2. package/dist/log-handler.d.ts.map +1 -1
  3. package/dist/log-handler.js +3 -4
  4. package/dist/log-handler.js.map +1 -1
  5. package/dist/rule-evaluator/accept-encoding.d.ts.map +1 -1
  6. package/dist/rule-evaluator/accept-encoding.js +3 -0
  7. package/dist/rule-evaluator/accept-encoding.js.map +1 -1
  8. package/dist/rule-evaluator/access-values.d.ts +5 -3
  9. package/dist/rule-evaluator/access-values.d.ts.map +1 -1
  10. package/dist/rule-evaluator/access-values.js +9 -3
  11. package/dist/rule-evaluator/access-values.js.map +1 -1
  12. package/dist/rule-evaluator/country-codes.generated.js +1 -1
  13. package/dist/rule-evaluator/edge-evaluation.d.ts +138 -0
  14. package/dist/rule-evaluator/edge-evaluation.d.ts.map +1 -0
  15. package/dist/rule-evaluator/edge-evaluation.js +335 -0
  16. package/dist/rule-evaluator/edge-evaluation.js.map +1 -0
  17. package/dist/rule-evaluator/edge-record.d.ts +189 -0
  18. package/dist/rule-evaluator/edge-record.d.ts.map +1 -0
  19. package/dist/rule-evaluator/edge-record.js +779 -0
  20. package/dist/rule-evaluator/edge-record.js.map +1 -0
  21. package/dist/rule-evaluator/evaluator.d.ts +21 -0
  22. package/dist/rule-evaluator/evaluator.d.ts.map +1 -1
  23. package/dist/rule-evaluator/evaluator.js +60 -3
  24. package/dist/rule-evaluator/evaluator.js.map +1 -1
  25. package/dist/rule-evaluator/explain.d.ts +6 -0
  26. package/dist/rule-evaluator/explain.d.ts.map +1 -1
  27. package/dist/rule-evaluator/explain.js +54 -20
  28. package/dist/rule-evaluator/explain.js.map +1 -1
  29. package/dist/rule-evaluator/fields.d.ts +50 -6
  30. package/dist/rule-evaluator/fields.d.ts.map +1 -1
  31. package/dist/rule-evaluator/fields.js +72 -50
  32. package/dist/rule-evaluator/fields.js.map +1 -1
  33. package/dist/rule-evaluator/functions.d.ts +18 -0
  34. package/dist/rule-evaluator/functions.d.ts.map +1 -1
  35. package/dist/rule-evaluator/functions.js +58 -4
  36. package/dist/rule-evaluator/functions.js.map +1 -1
  37. package/dist/rule-evaluator/log-sample.d.ts +23 -0
  38. package/dist/rule-evaluator/log-sample.d.ts.map +1 -1
  39. package/dist/rule-evaluator/log-sample.js +5 -3
  40. package/dist/rule-evaluator/log-sample.js.map +1 -1
  41. package/dist/rule-evaluator/match-chain.d.ts +8 -0
  42. package/dist/rule-evaluator/match-chain.d.ts.map +1 -1
  43. package/dist/rule-evaluator/match-chain.js +16 -0
  44. package/dist/rule-evaluator/match-chain.js.map +1 -1
  45. package/dist/rule-evaluator/operators.d.ts +5 -0
  46. package/dist/rule-evaluator/operators.d.ts.map +1 -1
  47. package/dist/rule-evaluator/operators.js +52 -0
  48. package/dist/rule-evaluator/operators.js.map +1 -1
  49. package/dist/rule-evaluator/query-args.d.ts +2 -2
  50. package/dist/rule-evaluator/query-args.d.ts.map +1 -1
  51. package/dist/rule-evaluator/query-args.js +11 -4
  52. package/dist/rule-evaluator/query-args.js.map +1 -1
  53. package/dist/rule-evaluator/validation.d.ts.map +1 -1
  54. package/dist/rule-evaluator/validation.js +30 -19
  55. package/dist/rule-evaluator/validation.js.map +1 -1
  56. package/dist/rule-evaluator.d.ts +5 -0
  57. package/dist/rule-evaluator.d.ts.map +1 -1
  58. package/dist/rule-evaluator.js +3 -0
  59. package/dist/rule-evaluator.js.map +1 -1
  60. package/dist/tools/explain-rule-miss.js +260 -10
  61. package/dist/tools/explain-rule-miss.js.map +1 -1
  62. package/package.json +3 -3
@@ -0,0 +1,189 @@
1
+ import type { RuleExpressionCustomLists } from './custom-lists.js';
2
+ import { type EdgeResolverCtx } from './fields.js';
3
+ import { type SampleLogRecord, type SampleRequestCfProperties } from './log-sample.js';
4
+ import { type QueryArg } from './query-args.js';
5
+ /**
6
+ * Edge records are Cloudflare Logpush http_requests records as Libreflare's
7
+ * edge log mapping stores them (Vector's `edge_logs` transform). They hold
8
+ * some of the values the rules engine read, and
9
+ * test/fixtures/edge-record-probes.json compares each with the engine's. A
10
+ * field the record does not hold is unknown, never empty.
11
+ */
12
+ /** How the zone normalizes URLs, which the non-raw path fields read. */
13
+ export type UrlNormalization = 'off' | 'cloudflare' | 'rfc3986';
14
+ /** The request headers and cookies a zone's custom-field rules log. */
15
+ export interface EdgeLoggedFields {
16
+ /** Request headers logged in raw form, by name in any case. */
17
+ requestHeaders: readonly string[];
18
+ /**
19
+ * Request headers logged in transformed form, by name in any case. Their
20
+ * values are what the origin received, which a Transform Rule may have
21
+ * set, so they are unknown.
22
+ */
23
+ transformedRequestHeaders?: readonly string[];
24
+ /** Cookies logged, by name as configured; Cloudflare matches cookie names case-sensitively. */
25
+ cookies: readonly string[];
26
+ }
27
+ export interface EdgeRecordOptions {
28
+ /** The zone's logged fields at the record's time. Without them, the record's own headers and cookies stand in. */
29
+ loggedFields?: EdgeLoggedFields;
30
+ /** The zone's URL normalization. Without it, a path that a normalization would change is unknown. */
31
+ urlNormalization?: UrlNormalization;
32
+ /**
33
+ * Whether the record keeps the request header and cookie values sent
34
+ * empty, as the edge log mapping passes them on before VictoriaLogs drops
35
+ * them. A logged header or cookie the record lacks was then not sent.
36
+ * User-Agent, Referer and X-Requested-With, which the mapping takes from
37
+ * Logpush's standard fields, stay not sent or sent empty.
38
+ */
39
+ keepsEmptyValues?: boolean;
40
+ }
41
+ /** A record's time, from `_time`, which Cloudflare's EdgeStartTimestamp sets. */
42
+ export interface EdgeRecordTime {
43
+ /** Milliseconds since the epoch. */
44
+ readonly epochMs: number;
45
+ /** As http.request.timestamp.sec reads it. */
46
+ readonly sec: number;
47
+ /** As http.request.timestamp.msec reads it: the millisecond within the second. */
48
+ readonly msec: number;
49
+ }
50
+ /**
51
+ * An entry of the record's MatchedRules, which has one per product. The
52
+ * transform entry lists the matched rules of every transform ruleset, URL
53
+ * rewrites first, then request and response header rules, each ruleset's in
54
+ * rule order. Its ruleset ID and version are the first matched ruleset's.
55
+ */
56
+ export interface EdgeMatchedRuleset {
57
+ readonly rulesetId: string;
58
+ readonly rulesetVersion?: string;
59
+ readonly ruleIds: readonly string[];
60
+ }
61
+ /** An edge record read for evaluation; see readEdgeRecord(). */
62
+ export interface EdgeRecord {
63
+ readonly rayId: string;
64
+ readonly time: EdgeRecordTime | undefined;
65
+ /**
66
+ * The Custom Rules the record lists, each once, in order, or every rule it
67
+ * lists when `ruleSourcesAligned` is false.
68
+ */
69
+ readonly customRuleIds: readonly string[];
70
+ /**
71
+ * Whether the record's rule sources line up with its rule IDs, which tell
72
+ * the Custom Rules apart. The mapping's compact() drops an empty entry from
73
+ * either array, and then they do not.
74
+ */
75
+ readonly ruleSourcesAligned: boolean;
76
+ /**
77
+ * The rules that matched, by product: the transform rules, URL rewrites
78
+ * among them. MatchedRules lists no Custom Rules. The mapping drops it when
79
+ * empty.
80
+ */
81
+ readonly matchedRules: readonly EdgeMatchedRuleset[];
82
+ /** Whether the record's own headers and cookies stood in for logged fields that were not given. */
83
+ readonly assumedLoggedFields: boolean;
84
+ /** See EdgeRecordOptions.keepsEmptyValues. */
85
+ readonly keepsEmptyValues: boolean;
86
+ readonly urlNormalization: UrlNormalization | undefined;
87
+ /**
88
+ * The paths the non-raw path fields may read: one, or more when the
89
+ * normalization was not given or the order of its steps changes the path,
90
+ * or null when Cloudflare's normalization of the path is unknown.
91
+ */
92
+ readonly normalizedPaths: readonly string[] | null;
93
+ readonly method: string | undefined;
94
+ readonly scheme: string;
95
+ /** As http.host reads it: the host in lowercase, without a port or a trailing dot. */
96
+ readonly hostname: string;
97
+ /** The scheme and host as http.request.full_uri begins, without a port. */
98
+ readonly origin: string;
99
+ /** The path and query as the client sent them, which the raw fields read. */
100
+ readonly rawPath: string;
101
+ /** The query from its `?`, or empty. */
102
+ readonly search: string;
103
+ /** The query's arguments, which the query fields read. */
104
+ readonly queryArgs: readonly QueryArg[];
105
+ /** The query's arguments by name, with each name's values in order. */
106
+ readonly queryArgMap: ReadonlyMap<string, readonly string[]>;
107
+ readonly clientIp: string | undefined;
108
+ /** The client's source port, TCP or UDP. */
109
+ readonly clientPort: number | undefined;
110
+ /** The client connection's TCP round-trip time in whole milliseconds, 0 over HTTP/3. */
111
+ readonly tcpRttMs: number | undefined;
112
+ readonly zoneName: string | undefined;
113
+ readonly cf: SampleRequestCfProperties;
114
+ /** The request headers logged raw that the record holds, by lowercase name, with their first value. */
115
+ readonly headers: ReadonlyMap<string, string>;
116
+ /** The request headers logged raw, by lowercase name, but for those in `ambiguousHeaders`. */
117
+ readonly loggedHeaders: ReadonlySet<string>;
118
+ /** The request headers logged transformed, by lowercase name, which are unknown. */
119
+ readonly transformedHeaders: ReadonlySet<string>;
120
+ /**
121
+ * Logged request headers whose values are unknown because, flattened, a
122
+ * header named `cookie.<name>` and a logged cookie share a key.
123
+ */
124
+ readonly ambiguousHeaders: ReadonlySet<string>;
125
+ /** The logged cookies the record holds, by configured name. */
126
+ readonly cookies: ReadonlyMap<string, string>;
127
+ /** The logged cookies, by configured name. */
128
+ readonly loggedCookies: ReadonlySet<string>;
129
+ /**
130
+ * Logged cookies whose values are unknown: the record keeps one value
131
+ * under the lowercase name of those that differ only in case, and
132
+ * flattened, a logged header can share a cookie's key.
133
+ */
134
+ readonly ambiguousCookies: ReadonlySet<string>;
135
+ /**
136
+ * The record's cookie keys that the logged cookies do not name and that
137
+ * may stand for names with uppercase letters, since Logpush keys a cookie
138
+ * by its name in lowercase. A cookie of such a name is unknown.
139
+ */
140
+ readonly unnamedCookies: ReadonlySet<string>;
141
+ /** Whether the record shows that a Cookie header was sent. */
142
+ readonly cookieSent: boolean;
143
+ }
144
+ /** A record the evaluator cannot read, as opposed to a failure while evaluating one. */
145
+ export declare class EdgeRecordError extends Error {
146
+ readonly name = "EdgeRecordError";
147
+ }
148
+ /** Whether a sample is a mapped edge record, which has a Ray ID under `edge`, nested or flattened. */
149
+ export declare function isEdgeRecord(sample: SampleLogRecord): boolean;
150
+ /**
151
+ * Reads a mapped edge record, nested as the mapping writes it or flattened as
152
+ * VictoriaLogs returns it. Logpush logs only the first value of a header sent
153
+ * more than once, and the evaluator takes each logged header and cookie as
154
+ * sent once.
155
+ *
156
+ * @throws {EdgeRecordError} When the record has no `edge.ray_id`, or no http
157
+ * or https URL in `edge.url`.
158
+ */
159
+ export declare function readEdgeRecord(sample: SampleLogRecord, options?: EdgeRecordOptions): EdgeRecord;
160
+ /**
161
+ * The paths a mapped edge record's non-raw path fields may read with a URL
162
+ * normalization; see EdgeRecord.normalizedPaths.
163
+ *
164
+ * @throws {EdgeRecordError} When the record has no http or https URL in `edge.url`.
165
+ */
166
+ export declare function edgeRecordPaths(sample: SampleLogRecord, urlNormalization?: UrlNormalization): readonly string[] | null;
167
+ /** A mapped edge record's time, read without the rest of the record. */
168
+ export declare function edgeRecordTime(sample: SampleLogRecord): EdgeRecordTime | undefined;
169
+ /**
170
+ * An RFC 3339 time, such as `_time` or a rule version's time, or undefined
171
+ * for text that is not one, such as a month 13. Fractions below a
172
+ * millisecond are cut, and a leap second reads as the next minute's first,
173
+ * as in Unix time.
174
+ */
175
+ export declare function parseEdgeTime(text: string): EdgeRecordTime | undefined;
176
+ /** Options for reading fields from an edge record. */
177
+ export interface EdgeResolverOptions {
178
+ /** A route's prefix, which the non-raw path fields read before the visitor's path, as the route's rewrite puts it. */
179
+ routePrefix?: string;
180
+ /**
181
+ * A rule listed after a route's rewrite that may have set the path, which
182
+ * leaves the non-raw path fields unknown.
183
+ */
184
+ pathRewrittenBy?: string;
185
+ customLists?: RuleExpressionCustomLists;
186
+ }
187
+ /** A resolver context that reads an edge record. */
188
+ export declare function edgeResolverCtx(record: EdgeRecord, options: EdgeResolverOptions, warnings: Set<string>): EdgeResolverCtx;
189
+ //# sourceMappingURL=edge-record.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edge-record.d.ts","sourceRoot":"","sources":["../../src/rule-evaluator/edge-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