fastly.js 0.9.0 → 0.9.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.
package/dist/browser.js CHANGED
@@ -271,6 +271,19 @@ function vclToString(v) {
271
271
  }
272
272
  return String(v);
273
273
  }
274
+ var LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
275
+ function isValidUtf8(s) {
276
+ return !LONE_SURROGATE.test(s);
277
+ }
278
+
279
+ class VCLFailure {
280
+ value;
281
+ error;
282
+ constructor(value, error) {
283
+ this.value = value;
284
+ this.error = error;
285
+ }
286
+ }
274
287
  function isNotSet(v) {
275
288
  return v instanceof VCLString && v.isNotSet;
276
289
  }
@@ -976,6 +989,7 @@ var BUILTIN_SIGNATURES = {
976
989
  ],
977
990
  ret: "STRING"
978
991
  },
992
+ "utf8.translate": { args: [["STRING", "STRING", "STRING"]], ret: "STRING" },
979
993
  "uuid.dns": { args: [], ret: "STRING" },
980
994
  "uuid.is_valid": { args: [["STRING"]], ret: "BOOL" },
981
995
  "uuid.is_version3": { args: [["STRING"]], ret: "BOOL" },
@@ -1834,10 +1848,9 @@ var JSON_ESCAPE_MAP = new Map([
1834
1848
  [12, "\\f"],
1835
1849
  [13, "\\r"]
1836
1850
  ]);
1837
- var LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
1838
1851
  function json_escape(str) {
1839
1852
  const s = String(str);
1840
- if (LONE_SURROGATE.test(s))
1853
+ if (!isValidUtf8(s))
1841
1854
  return "";
1842
1855
  let out = "";
1843
1856
  for (const ch of s) {
@@ -2025,6 +2038,9 @@ function strstr(haystack, needle) {
2025
2038
  }
2026
2039
  function strpad(s, width, pad) {
2027
2040
  const str = String(s);
2041
+ if (!Number.isFinite(width) || width <= -Number.MAX_SAFE_INTEGER) {
2042
+ return new VCLFailure("", "EDOM");
2043
+ }
2028
2044
  const w = Math.abs(Math.trunc(width));
2029
2045
  const sLen = utf8Bytes(str).length;
2030
2046
  if (sLen >= w)
@@ -4354,6 +4370,11 @@ class VCLCompiler {
4354
4370
  }
4355
4371
  evaluateFunctionCall(expression, context) {
4356
4372
  const result = this.evaluateFunctionCallInner(expression, context);
4373
+ if (result instanceof VCLFailure) {
4374
+ if (context.fastly)
4375
+ context.fastly.error = result.error;
4376
+ return coerceBuiltinReturn(expression.name, result.value);
4377
+ }
4357
4378
  const rule = NULL_RESULT_RULES[expression.name];
4358
4379
  if (rule && (result === null || result === undefined)) {
4359
4380
  if (context.fastly)
@@ -6223,6 +6244,112 @@ function createMathModule() {
6223
6244
  };
6224
6245
  }
6225
6246
 
6247
+ // src/vcl-utf8.ts
6248
+ function isNullArg(value) {
6249
+ return value === null || value === undefined || isNotSet(value);
6250
+ }
6251
+ var lastSets = null;
6252
+ var lastTable = new Map;
6253
+ function translationTable(set1, set2) {
6254
+ const key = `${set1}\x00${set2}`;
6255
+ if (key === lastSets)
6256
+ return lastTable;
6257
+ const from = [...set1];
6258
+ const to = [...set2];
6259
+ const table = new Map;
6260
+ for (let i = 0;i < from.length; i++) {
6261
+ const character = from[i];
6262
+ if (!table.has(character))
6263
+ table.set(character, to[Math.min(i, to.length - 1)]);
6264
+ }
6265
+ lastSets = key;
6266
+ lastTable = table;
6267
+ return table;
6268
+ }
6269
+ function translateSetsError(set1, set2) {
6270
+ const count1 = [...set1].length;
6271
+ const count2 = [...set2].length;
6272
+ if (count1 === 0)
6273
+ return "Characters required in set1";
6274
+ if (count2 === 0)
6275
+ return "Characters required in set2";
6276
+ if (count2 > count1) {
6277
+ return `Excess characters in set2: Expected ${count1} or fewer, got ${count2}`;
6278
+ }
6279
+ return null;
6280
+ }
6281
+ var UTF8Module = {
6282
+ is_valid: (s) => !isNullArg(s) && isValidUtf8(toRawString(s)),
6283
+ codepoint_count: (s) => {
6284
+ const str = toRawString(s);
6285
+ return isValidUtf8(str) ? [...str].length : 0;
6286
+ },
6287
+ substr: (s, offset, length) => {
6288
+ if (isNullArg(s))
6289
+ return null;
6290
+ const str = toRawString(s);
6291
+ if (!isValidUtf8(str))
6292
+ return null;
6293
+ const runes = [...str];
6294
+ const count = runes.length;
6295
+ let start = Math.trunc(Number(offset));
6296
+ if (!Number.isFinite(start))
6297
+ return null;
6298
+ if (start < 0)
6299
+ start += count;
6300
+ if (start < 0 || start > count)
6301
+ return null;
6302
+ let len;
6303
+ if (length === undefined) {
6304
+ len = count - start;
6305
+ } else {
6306
+ len = Math.trunc(Number(length));
6307
+ if (!Number.isFinite(len))
6308
+ return null;
6309
+ if (len < 0)
6310
+ len += count - start;
6311
+ }
6312
+ if (len < 0)
6313
+ len = 0;
6314
+ return runes.slice(start, start + len).join("");
6315
+ },
6316
+ strpad: (s, width, pad) => {
6317
+ const str = toRawString(s);
6318
+ const padStr = toRawString(pad);
6319
+ if (padStr === "")
6320
+ return str;
6321
+ const w = Math.trunc(Number(width));
6322
+ if (!Number.isFinite(w) || w <= -Number.MAX_SAFE_INTEGER)
6323
+ return new VCLFailure("", "EDOM");
6324
+ if (!isValidUtf8(padStr) || !isValidUtf8(str))
6325
+ return new VCLFailure(null, "EUTF8");
6326
+ const runes = [...str];
6327
+ const padRunes = [...padStr];
6328
+ const paddedCount = Math.abs(w);
6329
+ if (runes.length >= paddedCount)
6330
+ return str;
6331
+ const paddingCount = paddedCount - runes.length;
6332
+ const padding = [];
6333
+ for (let i = 0;i < paddingCount; i++)
6334
+ padding.push(padRunes[i % padRunes.length]);
6335
+ return w < 0 ? str + padding.join("") : padding.join("") + str;
6336
+ },
6337
+ translate: (input, set1, set2) => {
6338
+ const str = toRawString(input);
6339
+ if (!isValidUtf8(str))
6340
+ return new VCLFailure(null, "EUTF8");
6341
+ const from = toRawString(set1);
6342
+ const to = toRawString(set2);
6343
+ if (from === "" || to === "")
6344
+ return str;
6345
+ const table = translationTable(from, to);
6346
+ let out = "";
6347
+ for (const rune of str)
6348
+ out += table.get(rune) ?? rune;
6349
+ return out;
6350
+ }
6351
+ };
6352
+
6226
6353
  // src/vcl-parser-impl.ts
6227
6354
  var INT64_MAX = 2n ** 63n - 1n;
6228
6355
  var INT64_MIN_MAGNITUDE = 2n ** 63n;
@@ -6272,6 +6399,20 @@ var RETURN_ACTIONS = new Set([
6272
6399
  "hit_for_pass",
6273
6400
  "upgrade"
6274
6401
  ]);
6402
+ function constantString(expression) {
6403
+ if (expression.type === "StringLiteral")
6404
+ return expression.value;
6405
+ if (expression.type === "BinaryExpression") {
6406
+ if (expression.operator !== "+" && expression.operator !== " ")
6407
+ return null;
6408
+ const left = constantString(expression.left);
6409
+ if (left === null)
6410
+ return null;
6411
+ const right = constantString(expression.right);
6412
+ return right === null ? null : left + right;
6413
+ }
6414
+ return null;
6415
+ }
6275
6416
 
6276
6417
  class VCLParser {
6277
6418
  tokens = [];
@@ -7810,9 +7951,9 @@ class VCLParser {
7810
7951
  previous() {
7811
7952
  return this.tokens[this.current - 1];
7812
7953
  }
7813
- error(message) {
7814
- const token = this.peek();
7815
- throw new Error(`${message} at line ${token.line}, column ${token.column}`);
7954
+ error(message, at) {
7955
+ const where = at?.location ?? this.peek();
7956
+ throw new Error(`${message} at line ${where.line}, column ${where.column}`);
7816
7957
  }
7817
7958
  parseFunctionCall(token) {
7818
7959
  const name = token.value;
@@ -7824,6 +7965,9 @@ class VCLParser {
7824
7965
  } while (this.match("PUNCTUATION" /* PUNCTUATION */, ","));
7825
7966
  }
7826
7967
  this.consume("PUNCTUATION" /* PUNCTUATION */, "Expected ')' after function arguments");
7968
+ if (name === "utf8.translate" && args.length === 3) {
7969
+ this.checkTranslateSets(args[1], args[2]);
7970
+ }
7827
7971
  let result = {
7828
7972
  type: "FunctionCall",
7829
7973
  name,
@@ -7848,11 +7992,23 @@ class VCLParser {
7848
7992
  }
7849
7993
  return result;
7850
7994
  }
7995
+ checkTranslateSets(set1Expression, set2Expression) {
7996
+ const set1 = constantString(set1Expression);
7997
+ if (set1 === null)
7998
+ this.error("Expected a constant string for set1", set1Expression);
7999
+ const set2 = constantString(set2Expression);
8000
+ if (set2 === null)
8001
+ this.error("Expected a constant string for set2", set2Expression);
8002
+ const problem = translateSetsError(set1, set2);
8003
+ if (problem !== null) {
8004
+ this.error(problem, problem.includes("set1") ? set1Expression : set2Expression);
8005
+ }
8006
+ }
7851
8007
  }
7852
8008
 
7853
8009
  // src/vcl-parser.ts
7854
8010
  var UTF8_ENCODER = new TextEncoder;
7855
- var UTF8_STRICT_DECODER = new TextDecoder("utf-8", { fatal: true });
8011
+ var UTF8_STRICT_DECODER = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
7856
8012
  var LONG_STRING_OPEN_RE = /\{([A-Za-z0-9_]*)"/y;
7857
8013
  var RTIME_UNITS = new Set(["ms", "s", "m", "h", "d", "w", "y"]);
7858
8014
  var UNIT_BOUNDARY = /[a-zA-Z0-9_.:-]/;
@@ -8090,14 +8246,8 @@ class VCLLexer {
8090
8246
  const raw = this.input.substring(this.position, rawEnd);
8091
8247
  let content;
8092
8248
  if (!raw.includes("%")) {
8093
- for (const ch of raw) {
8094
- if (ch === `
8095
- `) {
8096
- this.line++;
8097
- this.column = 1;
8098
- }
8099
- this.advance();
8100
- }
8249
+ while (this.position < rawEnd)
8250
+ this.advanceTrackingLine();
8101
8251
  if (this.position < this.input.length)
8102
8252
  this.advance();
8103
8253
  content = raw;
@@ -8419,23 +8569,6 @@ function bytesToString(bytes) {
8419
8569
  }
8420
8570
  return out;
8421
8571
  }
8422
- function isValidUtf8(s) {
8423
- for (let i = 0;i < s.length; i++) {
8424
- const cu = s.charCodeAt(i);
8425
- if (cu >= 55296 && cu <= 56319) {
8426
- const lo = i + 1 < s.length ? s.charCodeAt(i + 1) : 0;
8427
- if (lo >= 56320 && lo <= 57343) {
8428
- i++;
8429
- continue;
8430
- }
8431
- return false;
8432
- }
8433
- if (cu >= 56320 && cu <= 57343) {
8434
- return false;
8435
- }
8436
- }
8437
- return true;
8438
- }
8439
8572
  function percentUnescape(s, plusAsSpace) {
8440
8573
  const bytes = stringToBytes(s);
8441
8574
  const out = [];
@@ -9144,103 +9277,6 @@ function createTableModule() {
9144
9277
  };
9145
9278
  }
9146
9279
 
9147
- // src/vcl-utf8.ts
9148
- var UTF8Module = {
9149
- is_valid: (s) => {
9150
- try {
9151
- const buf = Buffer.from(String(s), "utf-8");
9152
- return buf.toString("utf-8") === String(s);
9153
- } catch {
9154
- return false;
9155
- }
9156
- },
9157
- codepoint_count: (s) => {
9158
- const str = String(s);
9159
- try {
9160
- const buf = Buffer.from(str, "utf-8");
9161
- if (buf.toString("utf-8") !== str)
9162
- return 0;
9163
- } catch {
9164
- return 0;
9165
- }
9166
- return [...str].length;
9167
- },
9168
- substr: (s, offset, length) => {
9169
- const str = String(s);
9170
- try {
9171
- const buf = Buffer.from(str, "utf-8");
9172
- if (buf.toString("utf-8") !== str)
9173
- return null;
9174
- } catch {
9175
- return null;
9176
- }
9177
- const input = [...str];
9178
- const off = Math.trunc(Number(offset));
9179
- let start;
9180
- if (off < 0) {
9181
- start = input.length + off;
9182
- if (start < 0)
9183
- return "";
9184
- } else {
9185
- start = off;
9186
- }
9187
- let end;
9188
- if (length === undefined || length === null) {
9189
- end = input.length;
9190
- } else {
9191
- const len = Math.trunc(Number(length));
9192
- if (len < 0) {
9193
- end = input.length + len;
9194
- } else {
9195
- end = start + len;
9196
- if (end < 0)
9197
- return "";
9198
- }
9199
- }
9200
- if (end > input.length)
9201
- end = input.length;
9202
- if (start > input.length)
9203
- return "";
9204
- if (end <= start)
9205
- return "";
9206
- return input.slice(start, end).join("");
9207
- },
9208
- strpad: (s, width, pad) => {
9209
- const str = String(s);
9210
- const padStr = String(pad);
9211
- if (padStr === "")
9212
- return str;
9213
- const w = Math.trunc(Number(width));
9214
- if (!Number.isFinite(w) || w <= -Number.MAX_SAFE_INTEGER)
9215
- return "";
9216
- try {
9217
- const buf1 = Buffer.from(padStr, "utf-8");
9218
- if (buf1.toString("utf-8") !== padStr)
9219
- return null;
9220
- const buf2 = Buffer.from(str, "utf-8");
9221
- if (buf2.toString("utf-8") !== str)
9222
- return null;
9223
- } catch {
9224
- return null;
9225
- }
9226
- const origRunes = [...str];
9227
- const padRunes = [...padStr];
9228
- const origCount = origRunes.length;
9229
- const paddedCount = Math.abs(w);
9230
- if (origCount >= paddedCount)
9231
- return str;
9232
- const paddingCount = paddedCount - origCount;
9233
- const padding = [];
9234
- for (let i = 0;i < paddingCount; i++) {
9235
- padding.push(padRunes[i % padRunes.length]);
9236
- }
9237
- if (w < 0) {
9238
- return str + padding.join("");
9239
- }
9240
- return padding.join("") + str;
9241
- }
9242
- };
9243
-
9244
9280
  // src/vcl-uuid.ts
9245
9281
  import {
9246
9282
  parse,
@@ -10065,7 +10101,8 @@ Connection: close\r
10065
10101
  is_valid: UTF8Module.is_valid,
10066
10102
  codepoint_count: UTF8Module.codepoint_count,
10067
10103
  substr: UTF8Module.substr,
10068
- strpad: UTF8Module.strpad
10104
+ strpad: UTF8Module.strpad,
10105
+ translate: UTF8Module.translate
10069
10106
  };
10070
10107
  context.uuid = createUUIDModule(platform);
10071
10108
  const simpleHash = (str) => {
package/dist/index.js CHANGED
@@ -261,6 +261,19 @@ function vclToString(v) {
261
261
  }
262
262
  return String(v);
263
263
  }
264
+ var LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
265
+ function isValidUtf8(s) {
266
+ return !LONE_SURROGATE.test(s);
267
+ }
268
+
269
+ class VCLFailure {
270
+ value;
271
+ error;
272
+ constructor(value, error) {
273
+ this.value = value;
274
+ this.error = error;
275
+ }
276
+ }
264
277
  function isNotSet(v) {
265
278
  return v instanceof VCLString && v.isNotSet;
266
279
  }
@@ -966,6 +979,7 @@ var BUILTIN_SIGNATURES = {
966
979
  ],
967
980
  ret: "STRING"
968
981
  },
982
+ "utf8.translate": { args: [["STRING", "STRING", "STRING"]], ret: "STRING" },
969
983
  "uuid.dns": { args: [], ret: "STRING" },
970
984
  "uuid.is_valid": { args: [["STRING"]], ret: "BOOL" },
971
985
  "uuid.is_version3": { args: [["STRING"]], ret: "BOOL" },
@@ -1824,10 +1838,9 @@ var JSON_ESCAPE_MAP = new Map([
1824
1838
  [12, "\\f"],
1825
1839
  [13, "\\r"]
1826
1840
  ]);
1827
- var LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
1828
1841
  function json_escape(str) {
1829
1842
  const s = String(str);
1830
- if (LONE_SURROGATE.test(s))
1843
+ if (!isValidUtf8(s))
1831
1844
  return "";
1832
1845
  let out = "";
1833
1846
  for (const ch of s) {
@@ -2015,6 +2028,9 @@ function strstr(haystack, needle) {
2015
2028
  }
2016
2029
  function strpad(s, width, pad) {
2017
2030
  const str = String(s);
2031
+ if (!Number.isFinite(width) || width <= -Number.MAX_SAFE_INTEGER) {
2032
+ return new VCLFailure("", "EDOM");
2033
+ }
2018
2034
  const w = Math.abs(Math.trunc(width));
2019
2035
  const sLen = utf8Bytes(str).length;
2020
2036
  if (sLen >= w)
@@ -4344,6 +4360,11 @@ class VCLCompiler {
4344
4360
  }
4345
4361
  evaluateFunctionCall(expression, context) {
4346
4362
  const result = this.evaluateFunctionCallInner(expression, context);
4363
+ if (result instanceof VCLFailure) {
4364
+ if (context.fastly)
4365
+ context.fastly.error = result.error;
4366
+ return coerceBuiltinReturn(expression.name, result.value);
4367
+ }
4347
4368
  const rule = NULL_RESULT_RULES[expression.name];
4348
4369
  if (rule && (result === null || result === undefined)) {
4349
4370
  if (context.fastly)
@@ -6213,6 +6234,112 @@ function createMathModule() {
6213
6234
  };
6214
6235
  }
6215
6236
 
6237
+ // src/vcl-utf8.ts
6238
+ function isNullArg(value) {
6239
+ return value === null || value === undefined || isNotSet(value);
6240
+ }
6241
+ var lastSets = null;
6242
+ var lastTable = new Map;
6243
+ function translationTable(set1, set2) {
6244
+ const key = `${set1}\x00${set2}`;
6245
+ if (key === lastSets)
6246
+ return lastTable;
6247
+ const from = [...set1];
6248
+ const to = [...set2];
6249
+ const table = new Map;
6250
+ for (let i = 0;i < from.length; i++) {
6251
+ const character = from[i];
6252
+ if (!table.has(character))
6253
+ table.set(character, to[Math.min(i, to.length - 1)]);
6254
+ }
6255
+ lastSets = key;
6256
+ lastTable = table;
6257
+ return table;
6258
+ }
6259
+ function translateSetsError(set1, set2) {
6260
+ const count1 = [...set1].length;
6261
+ const count2 = [...set2].length;
6262
+ if (count1 === 0)
6263
+ return "Characters required in set1";
6264
+ if (count2 === 0)
6265
+ return "Characters required in set2";
6266
+ if (count2 > count1) {
6267
+ return `Excess characters in set2: Expected ${count1} or fewer, got ${count2}`;
6268
+ }
6269
+ return null;
6270
+ }
6271
+ var UTF8Module = {
6272
+ is_valid: (s) => !isNullArg(s) && isValidUtf8(toRawString(s)),
6273
+ codepoint_count: (s) => {
6274
+ const str = toRawString(s);
6275
+ return isValidUtf8(str) ? [...str].length : 0;
6276
+ },
6277
+ substr: (s, offset, length) => {
6278
+ if (isNullArg(s))
6279
+ return null;
6280
+ const str = toRawString(s);
6281
+ if (!isValidUtf8(str))
6282
+ return null;
6283
+ const runes = [...str];
6284
+ const count = runes.length;
6285
+ let start = Math.trunc(Number(offset));
6286
+ if (!Number.isFinite(start))
6287
+ return null;
6288
+ if (start < 0)
6289
+ start += count;
6290
+ if (start < 0 || start > count)
6291
+ return null;
6292
+ let len;
6293
+ if (length === undefined) {
6294
+ len = count - start;
6295
+ } else {
6296
+ len = Math.trunc(Number(length));
6297
+ if (!Number.isFinite(len))
6298
+ return null;
6299
+ if (len < 0)
6300
+ len += count - start;
6301
+ }
6302
+ if (len < 0)
6303
+ len = 0;
6304
+ return runes.slice(start, start + len).join("");
6305
+ },
6306
+ strpad: (s, width, pad) => {
6307
+ const str = toRawString(s);
6308
+ const padStr = toRawString(pad);
6309
+ if (padStr === "")
6310
+ return str;
6311
+ const w = Math.trunc(Number(width));
6312
+ if (!Number.isFinite(w) || w <= -Number.MAX_SAFE_INTEGER)
6313
+ return new VCLFailure("", "EDOM");
6314
+ if (!isValidUtf8(padStr) || !isValidUtf8(str))
6315
+ return new VCLFailure(null, "EUTF8");
6316
+ const runes = [...str];
6317
+ const padRunes = [...padStr];
6318
+ const paddedCount = Math.abs(w);
6319
+ if (runes.length >= paddedCount)
6320
+ return str;
6321
+ const paddingCount = paddedCount - runes.length;
6322
+ const padding = [];
6323
+ for (let i = 0;i < paddingCount; i++)
6324
+ padding.push(padRunes[i % padRunes.length]);
6325
+ return w < 0 ? str + padding.join("") : padding.join("") + str;
6326
+ },
6327
+ translate: (input, set1, set2) => {
6328
+ const str = toRawString(input);
6329
+ if (!isValidUtf8(str))
6330
+ return new VCLFailure(null, "EUTF8");
6331
+ const from = toRawString(set1);
6332
+ const to = toRawString(set2);
6333
+ if (from === "" || to === "")
6334
+ return str;
6335
+ const table = translationTable(from, to);
6336
+ let out = "";
6337
+ for (const rune of str)
6338
+ out += table.get(rune) ?? rune;
6339
+ return out;
6340
+ }
6341
+ };
6342
+
6216
6343
  // src/vcl-parser-impl.ts
6217
6344
  var INT64_MAX = 2n ** 63n - 1n;
6218
6345
  var INT64_MIN_MAGNITUDE = 2n ** 63n;
@@ -6262,6 +6389,20 @@ var RETURN_ACTIONS = new Set([
6262
6389
  "hit_for_pass",
6263
6390
  "upgrade"
6264
6391
  ]);
6392
+ function constantString(expression) {
6393
+ if (expression.type === "StringLiteral")
6394
+ return expression.value;
6395
+ if (expression.type === "BinaryExpression") {
6396
+ if (expression.operator !== "+" && expression.operator !== " ")
6397
+ return null;
6398
+ const left = constantString(expression.left);
6399
+ if (left === null)
6400
+ return null;
6401
+ const right = constantString(expression.right);
6402
+ return right === null ? null : left + right;
6403
+ }
6404
+ return null;
6405
+ }
6265
6406
 
6266
6407
  class VCLParser {
6267
6408
  tokens = [];
@@ -7800,9 +7941,9 @@ class VCLParser {
7800
7941
  previous() {
7801
7942
  return this.tokens[this.current - 1];
7802
7943
  }
7803
- error(message) {
7804
- const token = this.peek();
7805
- throw new Error(`${message} at line ${token.line}, column ${token.column}`);
7944
+ error(message, at) {
7945
+ const where = at?.location ?? this.peek();
7946
+ throw new Error(`${message} at line ${where.line}, column ${where.column}`);
7806
7947
  }
7807
7948
  parseFunctionCall(token) {
7808
7949
  const name = token.value;
@@ -7814,6 +7955,9 @@ class VCLParser {
7814
7955
  } while (this.match("PUNCTUATION" /* PUNCTUATION */, ","));
7815
7956
  }
7816
7957
  this.consume("PUNCTUATION" /* PUNCTUATION */, "Expected ')' after function arguments");
7958
+ if (name === "utf8.translate" && args.length === 3) {
7959
+ this.checkTranslateSets(args[1], args[2]);
7960
+ }
7817
7961
  let result = {
7818
7962
  type: "FunctionCall",
7819
7963
  name,
@@ -7838,11 +7982,23 @@ class VCLParser {
7838
7982
  }
7839
7983
  return result;
7840
7984
  }
7985
+ checkTranslateSets(set1Expression, set2Expression) {
7986
+ const set1 = constantString(set1Expression);
7987
+ if (set1 === null)
7988
+ this.error("Expected a constant string for set1", set1Expression);
7989
+ const set2 = constantString(set2Expression);
7990
+ if (set2 === null)
7991
+ this.error("Expected a constant string for set2", set2Expression);
7992
+ const problem = translateSetsError(set1, set2);
7993
+ if (problem !== null) {
7994
+ this.error(problem, problem.includes("set1") ? set1Expression : set2Expression);
7995
+ }
7996
+ }
7841
7997
  }
7842
7998
 
7843
7999
  // src/vcl-parser.ts
7844
8000
  var UTF8_ENCODER = new TextEncoder;
7845
- var UTF8_STRICT_DECODER = new TextDecoder("utf-8", { fatal: true });
8001
+ var UTF8_STRICT_DECODER = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
7846
8002
  var LONG_STRING_OPEN_RE = /\{([A-Za-z0-9_]*)"/y;
7847
8003
  var RTIME_UNITS = new Set(["ms", "s", "m", "h", "d", "w", "y"]);
7848
8004
  var UNIT_BOUNDARY = /[a-zA-Z0-9_.:-]/;
@@ -8080,14 +8236,8 @@ class VCLLexer {
8080
8236
  const raw = this.input.substring(this.position, rawEnd);
8081
8237
  let content;
8082
8238
  if (!raw.includes("%")) {
8083
- for (const ch of raw) {
8084
- if (ch === `
8085
- `) {
8086
- this.line++;
8087
- this.column = 1;
8088
- }
8089
- this.advance();
8090
- }
8239
+ while (this.position < rawEnd)
8240
+ this.advanceTrackingLine();
8091
8241
  if (this.position < this.input.length)
8092
8242
  this.advance();
8093
8243
  content = raw;
@@ -8409,23 +8559,6 @@ function bytesToString(bytes) {
8409
8559
  }
8410
8560
  return out;
8411
8561
  }
8412
- function isValidUtf8(s) {
8413
- for (let i = 0;i < s.length; i++) {
8414
- const cu = s.charCodeAt(i);
8415
- if (cu >= 55296 && cu <= 56319) {
8416
- const lo = i + 1 < s.length ? s.charCodeAt(i + 1) : 0;
8417
- if (lo >= 56320 && lo <= 57343) {
8418
- i++;
8419
- continue;
8420
- }
8421
- return false;
8422
- }
8423
- if (cu >= 56320 && cu <= 57343) {
8424
- return false;
8425
- }
8426
- }
8427
- return true;
8428
- }
8429
8562
  function percentUnescape(s, plusAsSpace) {
8430
8563
  const bytes = stringToBytes(s);
8431
8564
  const out = [];
@@ -9134,103 +9267,6 @@ function createTableModule() {
9134
9267
  };
9135
9268
  }
9136
9269
 
9137
- // src/vcl-utf8.ts
9138
- var UTF8Module = {
9139
- is_valid: (s) => {
9140
- try {
9141
- const buf = Buffer.from(String(s), "utf-8");
9142
- return buf.toString("utf-8") === String(s);
9143
- } catch {
9144
- return false;
9145
- }
9146
- },
9147
- codepoint_count: (s) => {
9148
- const str = String(s);
9149
- try {
9150
- const buf = Buffer.from(str, "utf-8");
9151
- if (buf.toString("utf-8") !== str)
9152
- return 0;
9153
- } catch {
9154
- return 0;
9155
- }
9156
- return [...str].length;
9157
- },
9158
- substr: (s, offset, length) => {
9159
- const str = String(s);
9160
- try {
9161
- const buf = Buffer.from(str, "utf-8");
9162
- if (buf.toString("utf-8") !== str)
9163
- return null;
9164
- } catch {
9165
- return null;
9166
- }
9167
- const input = [...str];
9168
- const off = Math.trunc(Number(offset));
9169
- let start;
9170
- if (off < 0) {
9171
- start = input.length + off;
9172
- if (start < 0)
9173
- return "";
9174
- } else {
9175
- start = off;
9176
- }
9177
- let end;
9178
- if (length === undefined || length === null) {
9179
- end = input.length;
9180
- } else {
9181
- const len = Math.trunc(Number(length));
9182
- if (len < 0) {
9183
- end = input.length + len;
9184
- } else {
9185
- end = start + len;
9186
- if (end < 0)
9187
- return "";
9188
- }
9189
- }
9190
- if (end > input.length)
9191
- end = input.length;
9192
- if (start > input.length)
9193
- return "";
9194
- if (end <= start)
9195
- return "";
9196
- return input.slice(start, end).join("");
9197
- },
9198
- strpad: (s, width, pad) => {
9199
- const str = String(s);
9200
- const padStr = String(pad);
9201
- if (padStr === "")
9202
- return str;
9203
- const w = Math.trunc(Number(width));
9204
- if (!Number.isFinite(w) || w <= -Number.MAX_SAFE_INTEGER)
9205
- return "";
9206
- try {
9207
- const buf1 = Buffer.from(padStr, "utf-8");
9208
- if (buf1.toString("utf-8") !== padStr)
9209
- return null;
9210
- const buf2 = Buffer.from(str, "utf-8");
9211
- if (buf2.toString("utf-8") !== str)
9212
- return null;
9213
- } catch {
9214
- return null;
9215
- }
9216
- const origRunes = [...str];
9217
- const padRunes = [...padStr];
9218
- const origCount = origRunes.length;
9219
- const paddedCount = Math.abs(w);
9220
- if (origCount >= paddedCount)
9221
- return str;
9222
- const paddingCount = paddedCount - origCount;
9223
- const padding = [];
9224
- for (let i = 0;i < paddingCount; i++) {
9225
- padding.push(padRunes[i % padRunes.length]);
9226
- }
9227
- if (w < 0) {
9228
- return str + padding.join("");
9229
- }
9230
- return padding.join("") + str;
9231
- }
9232
- };
9233
-
9234
9270
  // src/vcl-uuid.ts
9235
9271
  import {
9236
9272
  parse,
@@ -10055,7 +10091,8 @@ Connection: close\r
10055
10091
  is_valid: UTF8Module.is_valid,
10056
10092
  codepoint_count: UTF8Module.codepoint_count,
10057
10093
  substr: UTF8Module.substr,
10058
- strpad: UTF8Module.strpad
10094
+ strpad: UTF8Module.strpad,
10095
+ translate: UTF8Module.translate
10059
10096
  };
10060
10097
  context.uuid = createUUIDModule(platform);
10061
10098
  const simpleHash = (str) => {
@@ -1,5 +1,6 @@
1
1
  import { type VCLPlatform } from "./platform.js";
2
2
  import type { VCLProgram } from "./vcl-parser.js";
3
+ import { VCLFailure } from "./vcl-value.js";
3
4
  export interface VCLBackend {
4
5
  name: string;
5
6
  host: string;
@@ -174,10 +175,11 @@ export interface VCLContext {
174
175
  [key: string]: any;
175
176
  };
176
177
  utf8?: {
177
- is_valid: (s: string) => boolean;
178
- codepoint_count: (s: string) => number;
179
- substr: (s: string, offset: number, length?: number) => string | null;
180
- strpad: (s: string, width: number, pad: string) => string | null;
178
+ is_valid: (s: unknown) => boolean;
179
+ codepoint_count: (s: unknown) => number;
180
+ substr: (s: unknown, offset: number, length?: number) => string | null;
181
+ strpad: (s: unknown, width: number, pad: unknown) => string | VCLFailure;
182
+ translate: (input: unknown, set1: unknown, set2: unknown) => string | VCLFailure;
181
183
  };
182
184
  waf?: Record<string, any>;
183
185
  error?: (status: number, message: string) => string;
@@ -274,7 +276,7 @@ export interface VCLContext {
274
276
  };
275
277
  strrev?: (s: string) => string;
276
278
  strrep?: (s: string, count: number) => string;
277
- strpad?: (s: string, width: number, pad: string) => string;
279
+ strpad?: (s: string, width: number, pad: string) => string | VCLFailure;
278
280
  strcasecmp?: (s1: string, s2: string) => number;
279
281
  replace_prefix?: (s: string, prefix: string, replacement: string) => string;
280
282
  replace_suffix?: (s: string, suffix: string, replacement: string) => string;
@@ -123,4 +123,9 @@ export declare class VCLParser {
123
123
  private previous;
124
124
  private error;
125
125
  private parseFunctionCall;
126
+ /**
127
+ * Fastly builds the translation table at compile time, so both sets must be
128
+ * constant strings. vcl-utf8 owns the rules about their contents.
129
+ */
130
+ private checkTranslateSets;
126
131
  }
package/dist/vcl-std.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { type VCLFailure } from "./vcl-value.js";
1
2
  export interface StdModule {
2
3
  strlen: (s: string) => number;
3
4
  tolower: (s: string) => string;
@@ -5,7 +6,7 @@ export interface StdModule {
5
6
  strstr: (haystack: string, needle: string) => string | null;
6
7
  strrev: (s: string) => string | null;
7
8
  strrep: (s: string, count: number) => string;
8
- strpad: (s: string, width: number, pad: string) => string;
9
+ strpad: (s: string, width: number, pad: string) => string | VCLFailure;
9
10
  strcasecmp: (s1: string, s2: string) => boolean;
10
11
  prefixof: (s: string, prefix: string) => boolean;
11
12
  suffixof: (s: string, suffix: string) => boolean;
@@ -1,3 +1,4 @@
1
+ import { VCLFailure } from "./vcl-value.js";
1
2
  export declare function isAlnumByte(b: number): boolean;
2
3
  /** Value of a hex digit byte, or -1 when the byte is not a hex digit. */
3
4
  export declare function hexVal(b: number): number;
@@ -17,7 +18,7 @@ export declare function substr(str: string, offset: number, length?: number): st
17
18
  export declare function subfield(header: string, name: string, separator?: string): string | null;
18
19
  export declare function strcasecmp(a: string, b: string): boolean;
19
20
  export declare function strstr(haystack: string, needle: string): string | null;
20
- export declare function strpad(s: string, width: number, pad: string): string;
21
+ export declare function strpad(s: string, width: number, pad: string): string | VCLFailure;
21
22
  export declare function strrep(s: string, count: number): string;
22
23
  export declare function strrev(s: string): string | null;
23
24
  export declare function prefixof(s: string, prefix: string): boolean;
@@ -39,9 +40,3 @@ export declare function urlNormalize(input: string): string;
39
40
  export declare function boltsort_sort(url: string): string;
40
41
  export declare function setcookie_get_value_by_name(setCookieHeader: string, name: string): string | null;
41
42
  export declare function setcookie_delete_by_name(setCookieHeader: string, name: string): string;
42
- export declare const Utf8Module: {
43
- is_valid: (str: string) => boolean;
44
- codepoint_count: (str: string) => number;
45
- substr: (str: string, offset: number, length?: number) => string;
46
- strpad: (str: string, width: number, pad: string) => string;
47
- };
@@ -1,6 +1,29 @@
1
+ import { VCLFailure } from "./vcl-value.js";
2
+ /**
3
+ * The rules Fastly applies to the two character sets when it compiles a
4
+ * utf8.translate call. Returns the error message, or null when the sets are good.
5
+ */
6
+ export declare function translateSetsError(set1: string, set2: string): string | null;
1
7
  export declare const UTF8Module: {
2
- is_valid: (s: string) => boolean;
3
- codepoint_count: (s: string) => number;
4
- substr: (s: string, offset: number, length?: number) => string | null;
5
- strpad: (s: string, width: number, pad: string) => string | null;
8
+ is_valid: (s: unknown) => boolean;
9
+ codepoint_count: (s: unknown) => number;
10
+ /**
11
+ * A negative offset counts back from the end, and so does a negative length.
12
+ * An offset outside the string gives a not-set STRING.
13
+ */
14
+ substr: (s: unknown, offset: number, length?: number) => string | null;
15
+ /**
16
+ * A positive width pads on the left, a negative width on the right. The pad
17
+ * string repeats until the result is wide enough.
18
+ *
19
+ * An empty pad string returns the input. Fastly makes this test first, before
20
+ * every other rule.
21
+ */
22
+ strpad: (s: unknown, width: number, pad: unknown) => string | VCLFailure;
23
+ /**
24
+ * Each character of set1 becomes the character at the same position in set2.
25
+ * Extra characters of set1 all become the last character of set2. Every other
26
+ * character stays as it is.
27
+ */
28
+ translate: (input: unknown, set1: unknown, set2: unknown) => string | VCLFailure;
6
29
  };
@@ -60,6 +60,17 @@ export declare const HEADER_FRAGMENT_SEPARATOR = "\n";
60
60
  export declare function firstHeaderFragment(value: string): string;
61
61
  /** Convert any runtime value to its VCL string form. */
62
62
  export declare function vclToString(v: any): string;
63
+ /** True when the string is valid UTF-8. */
64
+ export declare function isValidUtf8(s: string): boolean;
65
+ /**
66
+ * A builtin that fails with a fastly.error returns this. The value is what the
67
+ * caller reads, and null means a not-set STRING.
68
+ */
69
+ export declare class VCLFailure {
70
+ readonly value: string | null;
71
+ readonly error: string;
72
+ constructor(value: string | null, error: string);
73
+ }
63
74
  /** Check if a value is a NOTSET VCLString */
64
75
  export declare function isNotSet(v: any): v is VCLString;
65
76
  /** Get the raw string value (empty string for NOTSET) */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fastly.js",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "A reimplementation of the Fastly platform in TypeScript: parse and execute Fastly VCL locally, with the full request pipeline, caching, ESI, and the VCL standard library.",
5
5
  "author": "Frank Denis <github@pureftpd.org>",
6
6
  "license": "MIT",