fastly.js 0.9.1 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/browser.js CHANGED
@@ -1546,14 +1546,269 @@ function collectCallStatements(statements) {
1546
1546
  return calls;
1547
1547
  }
1548
1548
 
1549
- // src/vcl-strings.ts
1549
+ // src/vcl-regex.ts
1550
1550
  var utf8Encoder = new TextEncoder;
1551
- var utf8Decoder = new TextDecoder;
1551
+ var utf8Decoder = new TextDecoder("utf-8", { ignoreBOM: true });
1552
+ var ASCII = /^[\x00-\x7f]*$/;
1553
+ function toBytes(s) {
1554
+ if (ASCII.test(s))
1555
+ return s;
1556
+ const bytes = utf8Encoder.encode(s);
1557
+ let out = "";
1558
+ for (let i = 0;i < bytes.length; i++)
1559
+ out += String.fromCharCode(bytes[i]);
1560
+ return out;
1561
+ }
1562
+ function fromBytes(s) {
1563
+ if (ASCII.test(s))
1564
+ return s;
1565
+ const bytes = new Uint8Array(s.length);
1566
+ for (let i = 0;i < s.length; i++)
1567
+ bytes[i] = s.charCodeAt(i);
1568
+ return utf8Decoder.decode(bytes);
1569
+ }
1570
+ var DIGIT = [[48, 57]];
1571
+ var UPPER = [[65, 90]];
1572
+ var LOWER = [[97, 122]];
1573
+ var ALPHA = [...UPPER, ...LOWER];
1574
+ var ALNUM = [...DIGIT, ...ALPHA];
1575
+ var WORD = [...ALNUM, [95, 95]];
1576
+ var BLANK = [
1577
+ [9, 9],
1578
+ [32, 32]
1579
+ ];
1580
+ var SPACE = [
1581
+ [9, 13],
1582
+ [32, 32]
1583
+ ];
1584
+ var SETS = {
1585
+ d: DIGIT,
1586
+ w: WORD,
1587
+ s: SPACE,
1588
+ h: [...BLANK, [160, 160]],
1589
+ v: [
1590
+ [10, 13],
1591
+ [133, 133]
1592
+ ]
1593
+ };
1594
+ var POSIX = {
1595
+ alpha: ALPHA,
1596
+ digit: DIGIT,
1597
+ alnum: ALNUM,
1598
+ upper: UPPER,
1599
+ lower: LOWER,
1600
+ space: SPACE,
1601
+ blank: BLANK,
1602
+ punct: [
1603
+ [33, 47],
1604
+ [58, 64],
1605
+ [91, 96],
1606
+ [123, 126]
1607
+ ],
1608
+ xdigit: [...DIGIT, [65, 70], [97, 102]],
1609
+ cntrl: [
1610
+ [0, 31],
1611
+ [127, 127]
1612
+ ],
1613
+ print: [[32, 126]],
1614
+ graph: [[33, 126]],
1615
+ word: WORD,
1616
+ ascii: [[0, 127]]
1617
+ };
1618
+ var ANCHORS = {
1619
+ A: "^",
1620
+ z: "$",
1621
+ Z: "(?=\\n?$)",
1622
+ b: "\\b",
1623
+ B: "\\B",
1624
+ R: "(?:\\r\\n|[\\n\\x0b\\f\\r\\x85])"
1625
+ };
1626
+ var SIMPLE_ESCAPES = {
1627
+ n: 10,
1628
+ r: 13,
1629
+ t: 9,
1630
+ f: 12,
1631
+ e: 27,
1632
+ a: 7,
1633
+ b: 8
1634
+ };
1635
+ function complement(ranges) {
1636
+ const out = [];
1637
+ let next = 0;
1638
+ for (const [lo, hi] of [...ranges].sort((a, b) => a[0] - b[0])) {
1639
+ if (lo > next)
1640
+ out.push([next, lo - 1]);
1641
+ next = Math.max(next, hi + 1);
1642
+ }
1643
+ if (next <= 255)
1644
+ out.push([next, 255]);
1645
+ return out;
1646
+ }
1647
+ function withOtherCase(ranges) {
1648
+ const out = [...ranges];
1649
+ for (const [lo, hi] of ranges) {
1650
+ for (const [a, z, shift] of [
1651
+ [65, 90, 32],
1652
+ [97, 122, -32]
1653
+ ]) {
1654
+ const l = Math.max(lo, a);
1655
+ const h = Math.min(hi, z);
1656
+ if (l <= h)
1657
+ out.push([l + shift, h + shift]);
1658
+ }
1659
+ }
1660
+ return out;
1661
+ }
1662
+ function classSource(ranges, negate, caseless) {
1663
+ const hex = (c) => `\\x${c.toString(16).padStart(2, "0")}`;
1664
+ const body = (caseless ? withOtherCase(ranges) : ranges).map(([lo, hi]) => lo === hi ? hex(lo) : `${hex(lo)}-${hex(hi)}`).join("");
1665
+ return `[${negate ? "^" : ""}${body}]`;
1666
+ }
1667
+ var isSingleChar = (r) => r.length === 1 && r[0][0] === r[0][1];
1668
+ function escapeRanges(p, i) {
1669
+ const c = p[i];
1670
+ const set = SETS[c.toLowerCase()];
1671
+ if (set)
1672
+ return [c === c.toLowerCase() ? set : complement(set), i + 1];
1673
+ let code = SIMPLE_ESCAPES[c];
1674
+ let next = i + 1;
1675
+ const braced = /^x\{([0-9a-fA-F]+)\}/.exec(p.slice(i));
1676
+ if (braced) {
1677
+ code = parseInt(braced[1], 16);
1678
+ next = i + braced[0].length;
1679
+ } else if (c === "x" && p[i + 1] !== "{" || c === "0") {
1680
+ const digits = (c === "x" ? /^[0-9a-f]{0,2}/i : /^[0-7]{0,2}/).exec(p.slice(i + 1))[0];
1681
+ code = digits ? parseInt(digits, c === "x" ? 16 : 8) : 0;
1682
+ next += digits.length;
1683
+ } else if (!/[a-z0-9]/i.test(c)) {
1684
+ code = c.charCodeAt(0);
1685
+ }
1686
+ if (code === undefined || code > 255) {
1687
+ throw new Error(`Unsupported regex escape \\${c}`);
1688
+ }
1689
+ return [[[code, code]], next];
1690
+ }
1691
+ function classItem(p, i) {
1692
+ return p[i] === "\\" ? escapeRanges(p, i + 1) : [[[p.charCodeAt(i), p.charCodeAt(i)]], i + 1];
1693
+ }
1694
+ function classAt(p, i, caseless) {
1695
+ const negate = p[i] === "^";
1696
+ if (negate)
1697
+ i++;
1698
+ const ranges = [];
1699
+ do {
1700
+ if (i >= p.length)
1701
+ throw new Error("Missing ] in regex character class");
1702
+ const posix = /^\[:(\^?)([a-z]+):\]/.exec(p.slice(i));
1703
+ if (posix) {
1704
+ const name = caseless && (posix[2] === "upper" || posix[2] === "lower") ? "alpha" : posix[2];
1705
+ const set = POSIX[name];
1706
+ if (!set)
1707
+ throw new Error(`Unknown POSIX class ${posix[0]}`);
1708
+ ranges.push(...posix[1] ? complement(set) : set);
1709
+ i += posix[0].length;
1710
+ continue;
1711
+ }
1712
+ let item;
1713
+ [item, i] = classItem(p, i);
1714
+ if (p[i] === "-" && p[i + 1] !== "]" && i + 1 < p.length) {
1715
+ const [end, next] = classItem(p, i + 1);
1716
+ if (!isSingleChar(item) || !isSingleChar(end) || item[0][0] > end[0][0]) {
1717
+ throw new Error("Invalid range in regex character class");
1718
+ }
1719
+ item = [[item[0][0], end[0][0]]];
1720
+ i = next;
1721
+ }
1722
+ ranges.push(...item);
1723
+ } while (p[i] !== "]");
1724
+ return [classSource(ranges, negate, caseless), i + 1];
1725
+ }
1726
+ function translate(p) {
1727
+ let out = "";
1728
+ let opts = "";
1729
+ const saved = [];
1730
+ let i = 0;
1731
+ while (i < p.length) {
1732
+ const c = p[i++];
1733
+ let m;
1734
+ if (c === "[") {
1735
+ let cls;
1736
+ [cls, i] = classAt(p, i, opts.includes("i"));
1737
+ out += cls;
1738
+ } else if (c === "(") {
1739
+ saved.push(opts);
1740
+ const rest = p.slice(i);
1741
+ if (m = /^\?#[^)]*\)/.exec(rest)) {
1742
+ saved.pop();
1743
+ } else if (m = /^\?P=(\w+)\)/.exec(rest)) {
1744
+ saved.pop();
1745
+ out += `\\k<${m[1]}>`;
1746
+ } else if (m = /^\?(?:P?<(\w+)>|'(\w+)')/.exec(rest)) {
1747
+ out += `(?<${m[1] ?? m[2]}>`;
1748
+ } else if (m = /^(?:\?[:=!]|\?<[=!]|(?!\?))/.exec(rest)) {
1749
+ out += `(${m[0]}`;
1750
+ } else if (m = /^\?([a-zA-Z]*)(?:-([a-zA-Z]*))?([:)])/.exec(rest)) {
1751
+ const [, on = "", off = "", end] = m;
1752
+ for (const f of on + off) {
1753
+ if (!"ims".includes(f))
1754
+ throw new Error(`Unsupported regex option (?${f})`);
1755
+ }
1756
+ if (end === ")")
1757
+ saved.pop();
1758
+ else
1759
+ out += "(?:";
1760
+ opts = [...opts + on].filter((f) => !off.includes(f)).join("");
1761
+ } else {
1762
+ throw new Error(`Unsupported regex group (${rest.slice(0, 2)}`);
1763
+ }
1764
+ i += m[0].length;
1765
+ } else if (c === ")") {
1766
+ opts = saved.pop() ?? opts;
1767
+ out += ")";
1768
+ } else if (c === ".") {
1769
+ out += opts.includes("s") ? "[\\s\\S]" : "[^\\n]";
1770
+ } else if (c === "^") {
1771
+ out += opts.includes("m") ? "(?:^|(?<=\\n)(?=[\\s\\S]))" : "^";
1772
+ } else if (c === "$") {
1773
+ out += opts.includes("m") ? "(?=\\n|$)" : "(?=\\n?$)";
1774
+ } else if (c !== "\\") {
1775
+ out += opts.includes("i") && /[a-z]/i.test(c) ? `[${c.toLowerCase()}${c.toUpperCase()}]` : c;
1776
+ } else if (p[i] === undefined) {
1777
+ throw new Error("Regex ends with a \\");
1778
+ } else if (ANCHORS[p[i]] !== undefined) {
1779
+ out += ANCHORS[p[i++]];
1780
+ } else if (m = /^(?:[1-9]\d*|k<\w+>)/.exec(p.slice(i))) {
1781
+ out += `(?:\\${m[0]})`;
1782
+ i += m[0].length;
1783
+ } else {
1784
+ let ranges;
1785
+ [ranges, i] = escapeRanges(p, i);
1786
+ out += classSource(ranges, false, opts.includes("i"));
1787
+ }
1788
+ }
1789
+ return out;
1790
+ }
1791
+ var cache = new Map;
1792
+ function compileRegex(pattern, flags = "") {
1793
+ const key = `${flags}/${pattern}`;
1794
+ let re = cache.get(key);
1795
+ if (re === undefined) {
1796
+ re = new RegExp(translate(toBytes(pattern)), flags);
1797
+ if (cache.size >= 1000)
1798
+ cache.clear();
1799
+ cache.set(key, re);
1800
+ }
1801
+ return re;
1802
+ }
1803
+
1804
+ // src/vcl-strings.ts
1805
+ var utf8Encoder2 = new TextEncoder;
1806
+ var utf8Decoder2 = new TextDecoder;
1552
1807
  function utf8Bytes(s) {
1553
- return utf8Encoder.encode(s);
1808
+ return utf8Encoder2.encode(s);
1554
1809
  }
1555
1810
  function decodeBytes(bytes) {
1556
- return utf8Decoder.decode(bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes));
1811
+ return utf8Decoder2.decode(bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes));
1557
1812
  }
1558
1813
  function isAlnumByte(b) {
1559
1814
  return b >= 65 && b <= 90 || b >= 97 && b <= 122 || b >= 48 && b <= 57;
@@ -1928,33 +2183,34 @@ function expandReplacement(_input, replacement, match) {
1928
2183
  }
1929
2184
  function regsub(str, pattern, replacement) {
1930
2185
  try {
1931
- const s = String(str);
1932
- const re = new RegExp(pattern);
1933
- const match = re.exec(s);
2186
+ const s = toBytes(String(str));
2187
+ const match = compileRegex(String(pattern)).exec(s);
1934
2188
  if (!match)
1935
- return s;
1936
- return s.slice(0, match.index) + expandReplacement(s, replacement, match) + s.slice(match.index + match[0].length);
2189
+ return String(str);
2190
+ return fromBytes(s.slice(0, match.index) + expandReplacement(s, toBytes(String(replacement)), match) + s.slice(match.index + match[0].length));
1937
2191
  } catch {
1938
2192
  return str;
1939
2193
  }
1940
2194
  }
1941
2195
  function regsuball(str, pattern, replacement) {
1942
2196
  try {
1943
- const s = String(str);
1944
- const re = new RegExp(pattern, "g");
2197
+ const s = toBytes(String(str));
2198
+ const re = compileRegex(String(pattern), "g");
2199
+ re.lastIndex = 0;
2200
+ const sub = toBytes(String(replacement));
1945
2201
  let result = "";
1946
2202
  let lastEnd = 0;
1947
2203
  let match;
1948
2204
  while ((match = re.exec(s)) !== null) {
1949
2205
  result += s.slice(lastEnd, match.index);
1950
- result += expandReplacement(s, replacement, match);
2206
+ result += expandReplacement(s, sub, match);
1951
2207
  lastEnd = match.index + match[0].length;
1952
2208
  if (match[0].length === 0) {
1953
2209
  re.lastIndex++;
1954
2210
  }
1955
2211
  }
1956
2212
  result += s.slice(lastEnd);
1957
- return result;
2213
+ return fromBytes(result);
1958
2214
  } catch {
1959
2215
  return str;
1960
2216
  }
@@ -3277,6 +3533,21 @@ function fastlyHash(key, seed, from, to) {
3277
3533
  return null;
3278
3534
  }
3279
3535
  var MAX_RTIME_SECONDS = 9223372036.854776;
3536
+ function urlComponents(url) {
3537
+ const pathEnd = url.search(/[?#]/);
3538
+ const path = pathEnd < 0 ? url : url.substring(0, pathEnd);
3539
+ const qs = url[pathEnd] === "?" ? url.substring(pathEnd + 1).split("#")[0] : "";
3540
+ const lastSlash = path.lastIndexOf("/");
3541
+ const basename2 = path.substring(lastSlash + 1);
3542
+ const dot = basename2.lastIndexOf(".");
3543
+ return {
3544
+ path,
3545
+ qs,
3546
+ dirname: lastSlash > 0 ? path.substring(0, lastSlash) : "/",
3547
+ basename: basename2,
3548
+ ext: dot >= 0 ? basename2.substring(dot + 1) : ""
3549
+ };
3550
+ }
3280
3551
  function serializeHeaders(headers) {
3281
3552
  if (!headers)
3282
3553
  return "";
@@ -4769,11 +5040,7 @@ class VCLCompiler {
4769
5040
  return raw === undefined ? VCLString.notset() : firstHeaderFragment(raw);
4770
5041
  }
4771
5042
  if (parts.length === 3 && idPart0 === "re" && idPart1 === "group") {
4772
- const groupNumber = parseInt(idPart2, 10);
4773
- if (!Number.isNaN(groupNumber) && context.re?.groups?.[groupNumber] !== undefined) {
4774
- return context.re.groups[groupNumber];
4775
- }
4776
- return VCLString.notset();
5043
+ return context.re?.groups?.[idPart2] ?? VCLString.notset();
4777
5044
  }
4778
5045
  if (parts.length >= 2 && idPart0 === "var") {
4779
5046
  const varName = parts.slice(1).join(".");
@@ -4797,35 +5064,16 @@ class VCLCompiler {
4797
5064
  return context.obj.response ?? "";
4798
5065
  if (name === "req.url")
4799
5066
  return context.req.url;
4800
- if (name === "req.url.path") {
4801
- const url = context.req.url || "";
4802
- const qIdx = url.indexOf("?");
4803
- return qIdx >= 0 ? url.substring(0, qIdx) : url;
4804
- }
4805
- if (name === "req.url.qs") {
4806
- const url = context.req.url || "";
4807
- const qIdx = url.indexOf("?");
4808
- return qIdx >= 0 ? url.substring(qIdx + 1) : "";
4809
- }
4810
- if (name === "req.url.basename") {
4811
- const path = this.resolveVariable("req.url.path", context);
4812
- if (path === "")
4813
- return ".";
4814
- const lastSlash = path.lastIndexOf("/");
4815
- return lastSlash >= 0 ? path.substring(lastSlash + 1) : path;
4816
- }
4817
- if (name === "req.url.dirname") {
4818
- const path = this.resolveVariable("req.url.path", context);
4819
- if (path === "")
4820
- return ".";
4821
- const lastSlash = path.lastIndexOf("/");
4822
- return lastSlash >= 0 ? path.substring(0, lastSlash + 1) : "/";
4823
- }
4824
- if (name === "req.url.ext") {
4825
- const basename2 = this.resolveVariable("req.url.basename", context);
4826
- const dotIdx = basename2.lastIndexOf(".");
4827
- return dotIdx >= 0 ? basename2.substring(dotIdx + 1) : "";
4828
- }
5067
+ if (name === "req.url.path")
5068
+ return urlComponents(context.req.url).path;
5069
+ if (name === "req.url.qs")
5070
+ return urlComponents(context.req.url).qs;
5071
+ if (name === "req.url.basename")
5072
+ return urlComponents(context.req.url).basename;
5073
+ if (name === "req.url.dirname")
5074
+ return urlComponents(context.req.url).dirname;
5075
+ if (name === "req.url.ext")
5076
+ return urlComponents(context.req.url).ext;
4829
5077
  if (name === "req.method" || name === "req.request")
4830
5078
  return context.req.method;
4831
5079
  if (name === "req.backend")
@@ -4933,35 +5181,16 @@ class VCLCompiler {
4933
5181
  return context.bereq.method;
4934
5182
  if (name === "bereq.proto")
4935
5183
  return ctx.bereq?.proto || "HTTP/1.1";
4936
- if (name === "bereq.url.path") {
4937
- const url = context.bereq.url || "";
4938
- const qIdx = url.indexOf("?");
4939
- return qIdx >= 0 ? url.substring(0, qIdx) : url;
4940
- }
4941
- if (name === "bereq.url.qs") {
4942
- const url = context.bereq.url || "";
4943
- const qIdx = url.indexOf("?");
4944
- return qIdx >= 0 ? url.substring(qIdx + 1) : "";
4945
- }
4946
- if (name === "bereq.url.basename") {
4947
- const path = this.resolveVariable("bereq.url.path", context);
4948
- if (path === "")
4949
- return ".";
4950
- const lastSlash = path.lastIndexOf("/");
4951
- return lastSlash >= 0 ? path.substring(lastSlash + 1) : path;
4952
- }
4953
- if (name === "bereq.url.dirname") {
4954
- const path = this.resolveVariable("bereq.url.path", context);
4955
- if (path === "")
4956
- return ".";
4957
- const lastSlash = path.lastIndexOf("/");
4958
- return lastSlash >= 0 ? path.substring(0, lastSlash + 1) : "/";
4959
- }
4960
- if (name === "bereq.url.ext") {
4961
- const basename2 = this.resolveVariable("bereq.url.basename", context);
4962
- const dotIdx = basename2.lastIndexOf(".");
4963
- return dotIdx >= 0 ? basename2.substring(dotIdx + 1) : "";
4964
- }
5184
+ if (name === "bereq.url.path")
5185
+ return urlComponents(context.bereq.url).path;
5186
+ if (name === "bereq.url.qs")
5187
+ return urlComponents(context.bereq.url).qs;
5188
+ if (name === "bereq.url.basename")
5189
+ return urlComponents(context.bereq.url).basename;
5190
+ if (name === "bereq.url.dirname")
5191
+ return urlComponents(context.bereq.url).dirname;
5192
+ if (name === "bereq.url.ext")
5193
+ return urlComponents(context.bereq.url).ext;
4965
5194
  if (name === "bereq.headers")
4966
5195
  return serializeHeaders(context.bereq.http);
4967
5196
  if (name === "bereq.max_reuse_idle_time")
@@ -5534,13 +5763,20 @@ class VCLCompiler {
5534
5763
  }
5535
5764
  regexMatch(left, right, context, negate) {
5536
5765
  try {
5537
- const regex = right instanceof RegExp ? right : new RegExp(String(right));
5538
- const match = String(left).match(regex);
5766
+ const regex = right instanceof RegExp ? right : compileRegex(String(right));
5767
+ const match = regex.exec(toBytes(String(left)));
5768
+ context.re = undefined;
5539
5769
  if (match) {
5540
- const groups = {};
5770
+ const groups = Object.create(null);
5541
5771
  match.forEach((val, i) => {
5542
- groups[i] = val ?? "";
5772
+ if (val !== undefined)
5773
+ groups[i] = fromBytes(val);
5543
5774
  });
5775
+ for (const name in match.groups) {
5776
+ const val = match.groups[name];
5777
+ if (val !== undefined)
5778
+ groups[name] = fromBytes(val);
5779
+ }
5544
5780
  context.re = { groups };
5545
5781
  return !negate;
5546
5782
  }
@@ -8781,13 +9017,13 @@ function globToRegExp(pattern) {
8781
9017
  var MATCHER_CACHE_MAX = 1000;
8782
9018
  var GLOB_MATCHER_CACHE = new Map;
8783
9019
  var REGEX_MATCHER_CACHE = new Map;
8784
- function cached(cache, pattern, build) {
8785
- let fn = cache.get(pattern);
9020
+ function cached(cache2, pattern, build) {
9021
+ let fn = cache2.get(pattern);
8786
9022
  if (fn === undefined) {
8787
9023
  fn = build(pattern);
8788
- if (cache.size >= MATCHER_CACHE_MAX)
8789
- cache.clear();
8790
- cache.set(pattern, fn);
9024
+ if (cache2.size >= MATCHER_CACHE_MAX)
9025
+ cache2.clear();
9026
+ cache2.set(pattern, fn);
8791
9027
  }
8792
9028
  return fn;
8793
9029
  }
@@ -8800,30 +9036,11 @@ function globMatcher(pattern) {
8800
9036
  return re.test(name);
8801
9037
  };
8802
9038
  }
8803
- function pcreToRegExp(pattern) {
8804
- let p = String(pattern);
8805
- let flags = "";
8806
- for (;; ) {
8807
- const m = /^\(\?([a-zA-Z]+)\)/.exec(p);
8808
- if (!m)
8809
- break;
8810
- for (const ch of m[1]) {
8811
- if (ch === "i" || ch === "m" || ch === "s") {
8812
- if (!flags.includes(ch))
8813
- flags += ch;
8814
- } else {
8815
- throw new Error(`Unsupported inline regex flag: ${ch}`);
8816
- }
8817
- }
8818
- p = p.substring(m[0].length);
8819
- }
8820
- return new RegExp(p, `${flags}g`);
8821
- }
8822
9039
  function regexMatcher(pattern) {
8823
- const re = pcreToRegExp(pattern);
9040
+ const re = compileRegex(pattern, "g");
8824
9041
  return (name) => {
8825
9042
  re.lastIndex = 0;
8826
- for (const m of name.matchAll(re)) {
9043
+ for (const m of toBytes(name).matchAll(re)) {
8827
9044
  if (m[0].length > 0)
8828
9045
  return true;
8829
9046
  }
@@ -9266,7 +9483,7 @@ function createTableModule() {
9266
9483
  if (value === undefined)
9267
9484
  return null;
9268
9485
  try {
9269
- return new RegExp(String(value));
9486
+ return compileRegex(String(value));
9270
9487
  } catch {
9271
9488
  return null;
9272
9489
  }
@@ -9598,22 +9815,8 @@ function createVCLContext(platform = getPlatform()) {
9598
9815
  suffixof: (str, suffix) => String(str).endsWith(String(suffix)),
9599
9816
  replace: (str, search, replacement) => String(str).replace(String(search), String(replacement)),
9600
9817
  replaceall: (str, search, replacement) => String(str).split(String(search)).join(String(replacement)),
9601
- regsub: (str, regex, replacement) => {
9602
- try {
9603
- return String(str).replace(new RegExp(regex), replacement);
9604
- } catch (e) {
9605
- logError(`Invalid regex: ${regex}`, e);
9606
- return str;
9607
- }
9608
- },
9609
- regsuball: (str, regex, replacement) => {
9610
- try {
9611
- return String(str).replace(new RegExp(regex, "g"), replacement);
9612
- } catch (e) {
9613
- logError(`Invalid regex: ${regex}`, e);
9614
- return str;
9615
- }
9616
- },
9818
+ regsub,
9819
+ regsuball,
9617
9820
  integer: (value) => parseInt(String(value), 10) || 0,
9618
9821
  real: (value) => parseFloat(String(value)) || 0,
9619
9822
  math: {
@@ -10027,7 +10230,7 @@ Connection: close\r
10027
10230
  },
10028
10231
  lookup_regex: (tableName, key, defaultValue = "") => {
10029
10232
  const table = context.tables[tableName];
10030
- const defaultRegex = () => defaultValue ? new RegExp(defaultValue) : /(?:)/;
10233
+ const defaultRegex = () => defaultValue ? compileRegex(defaultValue) : /(?:)/;
10031
10234
  if (!table || !(key in table.entries))
10032
10235
  return defaultRegex();
10033
10236
  const value = table.entries[key];
@@ -10035,7 +10238,7 @@ Connection: close\r
10035
10238
  return value;
10036
10239
  if (typeof value === "string") {
10037
10240
  try {
10038
- return new RegExp(value);
10241
+ return compileRegex(value);
10039
10242
  } catch (e) {
10040
10243
  logError(`Invalid regex pattern: ${value}`, e);
10041
10244
  return defaultRegex();
@@ -10252,7 +10455,7 @@ function executeVCL(subroutines, subroutineName, context) {
10252
10455
  var DEFAULT_TTL = 300;
10253
10456
  var DEFAULT_GRACE = 3600;
10254
10457
  var DEFAULT_STALE_WHILE_REVALIDATE = 10;
10255
- function toBytes(body) {
10458
+ function toBytes2(body) {
10256
10459
  if (typeof body === "string")
10257
10460
  return new TextEncoder().encode(body);
10258
10461
  if (body instanceof ArrayBuffer)
@@ -10262,13 +10465,51 @@ function toBytes(body) {
10262
10465
  function computeCacheKey(context) {
10263
10466
  return context.hashData && context.hashData.length > 0 ? context.hashData.join(":") : `${context.req.url}:${context.req.http.host || ""}`;
10264
10467
  }
10468
+ var DROPPED_ON_PASS = new Set([
10469
+ "alt-svc",
10470
+ "connection",
10471
+ "fastly-dsr-request",
10472
+ "fastly-shield-rp",
10473
+ "keep-alive",
10474
+ "te",
10475
+ "trailer",
10476
+ "upgrade"
10477
+ ]);
10478
+ var DROPPED_ON_POST_MISS = new Set([
10479
+ ...DROPPED_ON_PASS,
10480
+ "accept-ranges",
10481
+ "cache-control",
10482
+ "cdn-cache-control",
10483
+ "content-range",
10484
+ "fastly-cache-control",
10485
+ "proxy-authenticate",
10486
+ "proxy-authorization",
10487
+ "range"
10488
+ ]);
10489
+ var DROPPED_ON_MISS = new Set([
10490
+ ...DROPPED_ON_POST_MISS,
10491
+ "content-length",
10492
+ "if-modified-since",
10493
+ "if-none-match",
10494
+ "if-range",
10495
+ "transfer-encoding"
10496
+ ]);
10497
+ function initBackendRequest(context, mode) {
10498
+ const post = context.req.method === "POST";
10499
+ const dropped = mode === "pass" ? DROPPED_ON_PASS : post ? DROPPED_ON_POST_MISS : DROPPED_ON_MISS;
10500
+ context.bereq = {
10501
+ url: context.req.url,
10502
+ method: mode === "miss" && !post ? "GET" : context.req.method,
10503
+ http: Object.fromEntries(Object.entries(context.req.http).filter(([name]) => !dropped.has(name.toLowerCase())))
10504
+ };
10505
+ }
10265
10506
  function errorResult(context, key, restarts) {
10266
10507
  return {
10267
10508
  response: {
10268
10509
  status: context.obj.status || 500,
10269
10510
  statusText: "",
10270
10511
  headers: { ...context.obj.http },
10271
- body: toBytes(context.obj.response ?? "")
10512
+ body: toBytes2(context.obj.response ?? "")
10272
10513
  },
10273
10514
  action: "error",
10274
10515
  restarts,
@@ -10291,7 +10532,7 @@ async function runPipeline(opts) {
10291
10532
  }
10292
10533
  }
10293
10534
  async function runPass(opts) {
10294
- const { subroutines, context, cache, getBackendResponse } = opts;
10535
+ const { subroutines, context, cache: cache2, getBackendResponse } = opts;
10295
10536
  const now = () => context.platform.now();
10296
10537
  context.hashData = [];
10297
10538
  context.beresp.backend = undefined;
@@ -10325,7 +10566,7 @@ async function runPass(opts) {
10325
10566
  if (action === "lookup") {
10326
10567
  executeVCL(subroutines, "vcl_hash", context);
10327
10568
  key = computeCacheKey(context);
10328
- const cached2 = cache.get(key);
10569
+ const cached2 = cache2.get(key);
10329
10570
  if (cached2) {
10330
10571
  const at = now();
10331
10572
  const isFresh = at < cached2.expires;
@@ -10346,7 +10587,7 @@ async function runPass(opts) {
10346
10587
  return "restart";
10347
10588
  executeVCL(subroutines, "vcl_log", context);
10348
10589
  if (isStale)
10349
- cache.delete(key);
10590
+ cache2.delete(key);
10350
10591
  return {
10351
10592
  response: {
10352
10593
  status: context.resp.status,
@@ -10360,15 +10601,22 @@ async function runPass(opts) {
10360
10601
  };
10361
10602
  }
10362
10603
  } else {
10363
- cache.delete(key);
10604
+ cache2.delete(key);
10605
+ initBackendRequest(context, "miss");
10364
10606
  action = executeVCL(subroutines, "vcl_miss", context) || "fetch";
10365
10607
  }
10366
10608
  } else {
10609
+ initBackendRequest(context, "miss");
10367
10610
  action = executeVCL(subroutines, "vcl_miss", context) || "fetch";
10368
10611
  }
10369
- } else if (action === "pass") {
10612
+ }
10613
+ if (action === "pass") {
10614
+ key = "";
10615
+ initBackendRequest(context, "pass");
10370
10616
  action = executeVCL(subroutines, "vcl_pass", context) || "fetch";
10371
10617
  }
10618
+ if (action === "error")
10619
+ return errorResult(context, key, restarts);
10372
10620
  const passing = key === "";
10373
10621
  let backendResponse;
10374
10622
  try {
@@ -10413,7 +10661,7 @@ async function runPass(opts) {
10413
10661
  context.beresp.grace = DEFAULT_GRACE;
10414
10662
  context.beresp.stale_while_revalidate = DEFAULT_STALE_WHILE_REVALIDATE;
10415
10663
  }
10416
- const body = toBytes(backendResponse.body);
10664
+ const body = toBytes2(backendResponse.body);
10417
10665
  context.resp.status = context.beresp.status;
10418
10666
  context.resp.statusText = context.beresp.statusText;
10419
10667
  context.resp.http = { ...context.beresp.http };
@@ -10426,7 +10674,7 @@ async function runPass(opts) {
10426
10674
  const ttlMs = context.beresp.ttl * 1000;
10427
10675
  const graceMs = (context.beresp.grace || 0) * 1000;
10428
10676
  const swrMs = (context.beresp.stale_while_revalidate || 0) * 1000;
10429
- cache.set(key, {
10677
+ cache2.set(key, {
10430
10678
  resp: {
10431
10679
  status: context.resp.status,
10432
10680
  statusText: context.resp.statusText,