@vercel/queue 0.0.0-alpha.34 → 0.0.0-alpha.35

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.
@@ -62,6 +62,12 @@ var JsonTransport = class {
62
62
  contentType = "application/json";
63
63
  replacer;
64
64
  reviver;
65
+ /**
66
+ * Create a new JsonTransport.
67
+ * @param options - Optional JSON serialization options
68
+ * @param options.replacer - Custom replacer for JSON.stringify
69
+ * @param options.reviver - Custom reviver for JSON.parse
70
+ */
65
71
  constructor(options = {}) {
66
72
  this.replacer = options.replacer;
67
73
  this.reviver = options.reviver;
@@ -141,7 +147,9 @@ var MessageAlreadyProcessedError = class extends Error {
141
147
  }
142
148
  };
143
149
  var ConcurrencyLimitError = class extends Error {
150
+ /** Current number of in-flight messages for this consumer group. */
144
151
  currentInflight;
152
+ /** Maximum allowed concurrent messages (as configured). */
145
153
  maxConcurrency;
146
154
  constructor(message = "Concurrency limit exceeded", currentInflight, maxConcurrency) {
147
155
  super(message);
@@ -528,6 +536,25 @@ var QueueClient = class {
528
536
  }
529
537
  return response;
530
538
  }
539
+ /**
540
+ * Send a message to a topic.
541
+ *
542
+ * @param options - Message options including queue name, payload, and optional settings
543
+ * @param options.queueName - Topic name (pattern: `[A-Za-z0-9_-]+`)
544
+ * @param options.payload - Message payload
545
+ * @param options.idempotencyKey - Optional deduplication key (dedup window: min(retention, 24h))
546
+ * @param options.retentionSeconds - Message TTL (default: 86400, min: 60, max: 86400)
547
+ * @param options.delaySeconds - Delivery delay (default: 0, max: retentionSeconds)
548
+ * @param transport - Serializer for the payload
549
+ * @returns Promise with the generated messageId
550
+ * @throws {DuplicateMessageError} When idempotency key was already used
551
+ * @throws {ConsumerDiscoveryError} When consumer discovery fails
552
+ * @throws {ConsumerRegistryNotConfiguredError} When registry not configured
553
+ * @throws {BadRequestError} When parameters are invalid
554
+ * @throws {UnauthorizedError} When authentication fails
555
+ * @throws {ForbiddenError} When access is denied
556
+ * @throws {InternalServerError} When server encounters an error
557
+ */
531
558
  async sendMessage(options, transport) {
532
559
  const {
533
560
  queueName,
@@ -590,6 +617,25 @@ var QueueClient = class {
590
617
  const responseData = await response.json();
591
618
  return responseData;
592
619
  }
620
+ /**
621
+ * Receive messages from a topic as an async generator.
622
+ *
623
+ * @param options - Receive options
624
+ * @param options.queueName - Topic name (pattern: `[A-Za-z0-9_-]+`)
625
+ * @param options.consumerGroup - Consumer group name (pattern: `[A-Za-z0-9_-]+`)
626
+ * @param options.visibilityTimeoutSeconds - Lock duration (default: 30, min: 0, max: 3600)
627
+ * @param options.limit - Max messages to retrieve (default: 1, min: 1, max: 10)
628
+ * @param options.maxConcurrency - Max in-flight messages (default: unlimited, min: 1)
629
+ * @param transport - Deserializer for message payloads
630
+ * @yields Message objects with payload, messageId, receiptHandle, etc.
631
+ * @throws {QueueEmptyError} When no messages available
632
+ * @throws {InvalidLimitError} When limit is outside 1-10 range
633
+ * @throws {ConcurrencyLimitError} When maxConcurrency exceeded
634
+ * @throws {BadRequestError} When parameters are invalid
635
+ * @throws {UnauthorizedError} When authentication fails
636
+ * @throws {ForbiddenError} When access is denied
637
+ * @throws {InternalServerError} When server encounters an error
638
+ */
593
639
  async *receiveMessages(options, transport) {
594
640
  const {
595
641
  queueName,
@@ -675,6 +721,26 @@ var QueueClient = class {
675
721
  }
676
722
  }
677
723
  }
724
+ /**
725
+ * Receive a specific message by its ID.
726
+ *
727
+ * @param options - Receive options
728
+ * @param options.queueName - Topic name (pattern: `[A-Za-z0-9_-]+`)
729
+ * @param options.consumerGroup - Consumer group name (pattern: `[A-Za-z0-9_-]+`)
730
+ * @param options.messageId - Message ID to retrieve
731
+ * @param options.visibilityTimeoutSeconds - Lock duration (default: 30, min: 0, max: 3600)
732
+ * @param options.maxConcurrency - Max in-flight messages (default: unlimited, min: 1)
733
+ * @param transport - Deserializer for the message payload
734
+ * @returns Promise with the message
735
+ * @throws {MessageNotFoundError} When message doesn't exist
736
+ * @throws {MessageNotAvailableError} When message is in wrong state or was a duplicate
737
+ * @throws {MessageAlreadyProcessedError} When message was already processed
738
+ * @throws {ConcurrencyLimitError} When maxConcurrency exceeded
739
+ * @throws {BadRequestError} When parameters are invalid
740
+ * @throws {UnauthorizedError} When authentication fails
741
+ * @throws {ForbiddenError} When access is denied
742
+ * @throws {InternalServerError} When server encounters an error
743
+ */
678
744
  async receiveMessageById(options, transport) {
679
745
  const {
680
746
  queueName,
@@ -769,6 +835,21 @@ var QueueClient = class {
769
835
  }
770
836
  throw new MessageNotFoundError(messageId);
771
837
  }
838
+ /**
839
+ * Delete (acknowledge) a message after successful processing.
840
+ *
841
+ * @param options - Delete options
842
+ * @param options.queueName - Topic name
843
+ * @param options.consumerGroup - Consumer group name
844
+ * @param options.receiptHandle - Receipt handle from the received message (must use same deployment ID as receive)
845
+ * @returns Promise indicating deletion success
846
+ * @throws {MessageNotFoundError} When receipt handle not found
847
+ * @throws {MessageNotAvailableError} When receipt handle invalid or message already processed
848
+ * @throws {BadRequestError} When parameters are invalid
849
+ * @throws {UnauthorizedError} When authentication fails
850
+ * @throws {ForbiddenError} When access is denied
851
+ * @throws {InternalServerError} When server encounters an error
852
+ */
772
853
  async deleteMessage(options) {
773
854
  const { queueName, consumerGroup, receiptHandle } = options;
774
855
  const headers = new Headers({
@@ -813,6 +894,23 @@ var QueueClient = class {
813
894
  }
814
895
  return { deleted: true };
815
896
  }
897
+ /**
898
+ * Extend or change the visibility timeout of a message.
899
+ * Used to prevent message redelivery while still processing.
900
+ *
901
+ * @param options - Visibility options
902
+ * @param options.queueName - Topic name
903
+ * @param options.consumerGroup - Consumer group name
904
+ * @param options.receiptHandle - Receipt handle from the received message (must use same deployment ID as receive)
905
+ * @param options.visibilityTimeoutSeconds - New timeout (min: 0, max: 3600, cannot exceed message expiration)
906
+ * @returns Promise indicating success
907
+ * @throws {MessageNotFoundError} When receipt handle not found
908
+ * @throws {MessageNotAvailableError} When receipt handle invalid or message already processed
909
+ * @throws {BadRequestError} When parameters are invalid
910
+ * @throws {UnauthorizedError} When authentication fails
911
+ * @throws {ForbiddenError} When access is denied
912
+ * @throws {InternalServerError} When server encounters an error
913
+ */
816
914
  async changeVisibility(options) {
817
915
  const {
818
916
  queueName,
@@ -935,36 +1033,26 @@ var ConsumerGroup = class {
935
1033
  refreshInterval;
936
1034
  transport;
937
1035
  /**
938
- * Create a new ConsumerGroup instance
939
- * @param client QueueClient instance to use for API calls
940
- * @param topicName Name of the topic to consume from
941
- * @param consumerGroupName Name of the consumer group
942
- * @param options Optional configuration
1036
+ * Create a new ConsumerGroup instance.
1037
+ *
1038
+ * @param client - QueueClient instance to use for API calls
1039
+ * @param topicName - Name of the topic to consume from (pattern: `[A-Za-z0-9_-]+`)
1040
+ * @param consumerGroupName - Name of the consumer group (pattern: `[A-Za-z0-9_-]+`)
1041
+ * @param options - Optional configuration
1042
+ * @param options.transport - Payload serializer (default: JsonTransport)
1043
+ * @param options.visibilityTimeoutSeconds - Message lock duration (default: 30, max: 3600)
1044
+ * @param options.visibilityRefreshInterval - Lock refresh interval in seconds (default: visibilityTimeout / 3)
943
1045
  */
944
1046
  constructor(client, topicName, consumerGroupName, options = {}) {
945
1047
  this.client = client;
946
1048
  this.topicName = topicName;
947
1049
  this.consumerGroupName = consumerGroupName;
948
- this.visibilityTimeout = options.visibilityTimeoutSeconds || 30;
949
- this.refreshInterval = options.refreshInterval || 10;
1050
+ this.visibilityTimeout = options.visibilityTimeoutSeconds ?? 30;
1051
+ this.refreshInterval = options.visibilityRefreshInterval ?? Math.floor(this.visibilityTimeout / 3);
950
1052
  this.transport = options.transport || new JsonTransport();
951
1053
  }
952
1054
  /**
953
1055
  * Starts a background loop that periodically extends the visibility timeout for a message.
954
- * This prevents the message from becoming visible to other consumers while it's being processed.
955
- *
956
- * The extension loop runs every `refreshInterval` seconds and updates the message's
957
- * visibility timeout to `visibilityTimeout` seconds from the current time.
958
- *
959
- * @param receiptHandle - The receipt handle that proves ownership of the message
960
- * @returns A function that when called will stop the extension loop
961
- *
962
- * @remarks
963
- * - The first extension attempt occurs after `refreshInterval` seconds, not immediately
964
- * - If an extension fails, the loop terminates with an error logged to console
965
- * - The returned stop function is idempotent - calling it multiple times is safe
966
- * - By default, the stop function returns immediately without waiting for in-flight
967
- * - Pass `true` to the stop function to wait for any in-flight extension to complete
968
1056
  */
969
1057
  startVisibilityExtension(receiptHandle) {
970
1058
  let isRunning = true;
@@ -1236,7 +1324,7 @@ async function parseCallback(request) {
1236
1324
  messageId
1237
1325
  };
1238
1326
  }
1239
- function createCallbackHandler(handlers, client) {
1327
+ function createCallbackHandler(handlers, client, visibilityTimeoutSeconds) {
1240
1328
  for (const topicPattern in handlers) {
1241
1329
  if (topicPattern.includes("*")) {
1242
1330
  if (!validateWildcardPattern(topicPattern)) {
@@ -1272,7 +1360,10 @@ function createCallbackHandler(handlers, client) {
1272
1360
  );
1273
1361
  }
1274
1362
  const topic = new Topic(client, queueName);
1275
- const cg = topic.consumerGroup(consumerGroup);
1363
+ const cg = topic.consumerGroup(
1364
+ consumerGroup,
1365
+ visibilityTimeoutSeconds !== void 0 ? { visibilityTimeoutSeconds } : void 0
1366
+ );
1276
1367
  await cg.consume(consumerGroupHandler, { messageId });
1277
1368
  return Response.json({ status: "success" });
1278
1369
  } catch (error) {
@@ -1288,8 +1379,12 @@ function createCallbackHandler(handlers, client) {
1288
1379
  };
1289
1380
  return routeHandler;
1290
1381
  }
1291
- function handleCallback(handlers, client) {
1292
- return createCallbackHandler(handlers, client || new QueueClient());
1382
+ function handleCallback(handlers, options) {
1383
+ return createCallbackHandler(
1384
+ handlers,
1385
+ options?.client || new QueueClient(),
1386
+ options?.visibilityTimeoutSeconds
1387
+ );
1293
1388
  }
1294
1389
 
1295
1390
  // src/nextjs-pages.ts