@vercel/queue 0.1.7 → 0.2.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/index.mjs CHANGED
@@ -597,7 +597,13 @@ function parseBinaryHeaders(headers) {
597
597
  `Missing required CloudEvent headers: ${missingFields.join(", ")}`
598
598
  );
599
599
  }
600
- const region = getHeader(headers, "ce-vqsregion") ?? void 0;
600
+ const rawRegion = getHeader(headers, "ce-vqsregion") ?? void 0;
601
+ if (rawRegion !== void 0 && !/^[a-z]{2,5}[0-9]{1,2}$/.test(rawRegion)) {
602
+ throw new Error(
603
+ `Invalid ce-vqsregion header: ${JSON.stringify(rawRegion)}. Region must match /^[a-z]{2,5}[0-9]{1,2}$/ (e.g. "iad1", "lhr1").`
604
+ );
605
+ }
606
+ const region = rawRegion;
601
607
  const base = {
602
608
  queueName,
603
609
  consumerGroup,
@@ -941,6 +947,44 @@ Add a trigger to vercel.json:
941
947
  );
942
948
  }
943
949
  }
950
+ function registerDevConsumer(options) {
951
+ const {
952
+ topic,
953
+ client,
954
+ handler,
955
+ consumerGroup = "dev-consumer",
956
+ visibilityTimeoutSeconds,
957
+ retry
958
+ } = options;
959
+ const entry = {
960
+ consumerGroup,
961
+ handler,
962
+ client,
963
+ options: visibilityTimeoutSeconds !== void 0 || retry !== void 0 ? { visibilityTimeoutSeconds, retry } : void 0
964
+ };
965
+ const registry = getHandlerRegistry();
966
+ const existing = registry.get(topic) ?? [];
967
+ const existingIndex = existing.findIndex(
968
+ (e) => e.consumerGroup === consumerGroup
969
+ );
970
+ if (existingIndex >= 0) {
971
+ existing[existingIndex] = entry;
972
+ } else {
973
+ existing.push(entry);
974
+ }
975
+ registry.set(topic, existing);
976
+ return () => {
977
+ const current = registry.get(topic);
978
+ if (!current) return;
979
+ const filtered = current.filter((e) => e !== entry);
980
+ if (filtered.length === current.length) return;
981
+ if (filtered.length === 0) {
982
+ registry.delete(topic);
983
+ } else {
984
+ registry.set(topic, filtered);
985
+ }
986
+ };
987
+ }
944
988
  function lookupHandlers(topicName) {
945
989
  const registry = getHandlerRegistry();
946
990
  const result = [];
@@ -1448,6 +1492,14 @@ function parseQueueHeaders(headers) {
1448
1492
  receiptHandle
1449
1493
  };
1450
1494
  }
1495
+ var REGION_PATTERN = /^[a-z]{2,5}[0-9]{1,2}$/;
1496
+ function validateRegion(region) {
1497
+ if (!REGION_PATTERN.test(region)) {
1498
+ throw new Error(
1499
+ `Invalid region code: ${JSON.stringify(region)}. Region must match the pattern /^[a-z]{2,5}[0-9]{1,2}$/ (e.g. "iad1", "lhr1").`
1500
+ );
1501
+ }
1502
+ }
1451
1503
  var DEFAULT_BASE_URL_RESOLVER = (region) => new URL(`https://${region}.vercel-queue.com`);
1452
1504
  function resolveBaseUrl(region, resolver) {
1453
1505
  return (resolver ?? DEFAULT_BASE_URL_RESOLVER)(region);
@@ -1465,6 +1517,7 @@ var ApiClient = class _ApiClient {
1465
1517
  baseUrlResolver;
1466
1518
  dispatcher;
1467
1519
  constructor(options) {
1520
+ validateRegion(options.region);
1468
1521
  this.region = options.region;
1469
1522
  this.baseUrlResolver = options.resolveBaseUrl;
1470
1523
  this.baseUrl = resolveBaseUrl(this.region, this.baseUrlResolver);
@@ -1579,7 +1632,7 @@ Cause: ${cause}`
1579
1632
  }
1580
1633
  console.debug("[VQS Debug] Request:", JSON.stringify(logData, null, 2));
1581
1634
  }
1582
- init.headers.set("User-Agent", `@vercel/queue/${"0.1.7"}`);
1635
+ init.headers.set("User-Agent", `@vercel/queue/${"0.2.1"}`);
1583
1636
  init.headers.set("Vqs-Client-Ts", (/* @__PURE__ */ new Date()).toISOString());
1584
1637
  const fetchInit = this.dispatcher ? { ...init, dispatcher: this.dispatcher } : init;
1585
1638
  const response = await fetch(url, fetchInit);
@@ -2268,6 +2321,7 @@ export {
2268
2321
  handleCallback2 as handleCallback,
2269
2322
  parseCallback,
2270
2323
  parseRawCallback,
2324
+ registerDevConsumer,
2271
2325
  send
2272
2326
  };
2273
2327
  //# sourceMappingURL=index.mjs.map