-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
QueueClient.ts
1209 lines (1125 loc) · 45.2 KB
/
QueueClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import type { TokenCredential } from "@azure/core-auth";
import { isTokenCredential } from "@azure/core-auth";
import { isNode } from "@azure/core-util";
import type {
EnqueuedMessage,
DequeuedMessageItem,
MessagesDequeueHeaders,
MessagesEnqueueHeaders,
MessagesPeekHeaders,
MessageIdUpdateResponse,
MessageIdDeleteResponse,
MessagesClearResponse,
PeekedMessageItem,
QueueCreateHeaders,
QueueDeleteResponse,
QueueGetAccessPolicyHeaders,
QueueGetPropertiesResponse,
QueueSetAccessPolicyResponse,
QueueSetMetadataResponse,
SignedIdentifierModel,
QueueCreateResponse,
QueueDeleteHeaders,
QueueSetMetadataHeaders,
QueueGetPropertiesHeaders,
QueueSetAccessPolicyHeaders,
MessagesClearHeaders,
MessageIdDeleteHeaders,
MessageIdUpdateHeaders,
} from "./generatedModels";
import type { AbortSignalLike } from "@azure/abort-controller";
import type { Messages, MessageId, Queue } from "./generated/src/operationsInterfaces";
import type { StoragePipelineOptions, Pipeline } from "./Pipeline";
import { newPipeline, isPipelineLike } from "./Pipeline";
import type { CommonOptions } from "./StorageClient";
import { StorageClient, getStorageClientContext } from "./StorageClient";
import type { WithResponse } from "./utils/utils.common";
import {
appendToURLPath,
extractConnectionStringParts,
isIpEndpointStyle,
truncatedISO8061Date,
appendToURLQuery,
assertResponse,
} from "./utils/utils.common";
import { StorageSharedKeyCredential } from "../../storage-blob/src/credentials/StorageSharedKeyCredential";
import { AnonymousCredential } from "../../storage-blob/src/credentials/AnonymousCredential";
import { tracingClient } from "./utils/tracing";
import type { Metadata } from "./models";
import {
generateQueueSASQueryParameters,
generateQueueSASQueryParametersInternal,
} from "./QueueSASSignatureValues";
import type { SasIPRange } from "./SasIPRange";
import type { QueueSASPermissions } from "./QueueSASPermissions";
import type { SASProtocol } from "./SASQueryParameters";
import { getDefaultProxySettings } from "@azure/core-rest-pipeline";
/**
* Options to configure {@link QueueClient.create} operation
*/
export interface QueueCreateOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
/**
* A collection of key-value string pair to associate with the queue object.
* The keys need to be lower-case.
*/
metadata?: Metadata;
}
/**
* Options to configure {@link QueueClient.exists} operation
*/
export interface QueueExistsOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure {@link QueueClient.getProperties} operation
*/
export interface QueueGetPropertiesOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure {@link QueueClient.delete} operation
*/
export interface QueueDeleteOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure {@link QueueClient.getAccessPolicy} operation
*/
export interface QueueGetAccessPolicyOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure {@link QueueClient.setAccessPolicy} operation
*/
export interface QueueSetAccessPolicyOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure {@link QueueClient.setMetadata} operation
*/
export interface QueueSetMetadataOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/**
* Signed identifier.
*/
export interface SignedIdentifier {
/**
* a unique id
*/
id: string;
/**
* Access Policy
*/
accessPolicy: {
/**
* the date-time the policy is active.
*/
startsOn?: Date;
/**
* the date-time the policy expires.
*/
expiresOn?: Date;
/**
* the permissions for the acl policy
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/set-queue-acl
*/
permissions?: string;
};
}
/**
* Contains response data for the {@link QueueClient.getAccessPolicy} operation.
*/
export declare type QueueGetAccessPolicyResponse = WithResponse<
{
signedIdentifiers: SignedIdentifier[];
} & QueueGetAccessPolicyHeaders,
QueueGetAccessPolicyHeaders,
SignedIdentifierModel[]
>;
/**
* Options to configure {@link QueueClient.clearMessages} operation
*/
export interface QueueClearMessagesOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/** Optional parameters. */
export interface MessagesEnqueueOptionalParams extends CommonOptions {
/** The The timeout parameter is expressed in seconds. For more information, see <a href="https://docs.microsoft.com/en-us/rest/api/storageservices/setting-timeouts-for-queue-service-operations">Setting Timeouts for Queue Service Operations.</a> */
timeoutInSeconds?: number;
/** Provides a client-generated, opaque value with a 1 KB character limit that is recorded in the analytics logs when storage analytics logging is enabled. */
requestId?: string;
/** Optional. If specified, the request must be made using an x-ms-version of 2011-08-18 or later. If not specified, the default value is 0. Specifies the new visibility timeout value, in seconds, relative to server time. The new value must be larger than or equal to 0, and cannot be larger than 7 days. The visibility timeout of a message cannot be set to a value later than the expiry time. visibilitytimeout should be set to a value smaller than the time-to-live value. */
visibilityTimeout?: number;
/** Optional. Specifies the time-to-live interval for the message, in seconds. Prior to version 2017-07-29, the maximum time-to-live allowed is 7 days. For version 2017-07-29 or later, the maximum time-to-live can be any positive number, as well as -1 indicating that the message does not expire. If this parameter is omitted, the default time-to-live is 7 days. */
messageTimeToLive?: number;
}
/**
* Options to configure {@link QueueClient.sendMessage} operation
*/
export interface QueueSendMessageOptions extends MessagesEnqueueOptionalParams, CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/** Optional parameters. */
export interface MessagesDequeueOptionalParams extends CommonOptions {
/** The The timeout parameter is expressed in seconds. For more information, see <a href="https://docs.microsoft.com/en-us/rest/api/storageservices/setting-timeouts-for-queue-service-operations">Setting Timeouts for Queue Service Operations.</a> */
timeoutInSeconds?: number;
/** Provides a client-generated, opaque value with a 1 KB character limit that is recorded in the analytics logs when storage analytics logging is enabled. */
requestId?: string;
/** Optional. A nonzero integer value that specifies the number of messages to retrieve from the queue, up to a maximum of 32. If fewer are visible, the visible messages are returned. By default, a single message is retrieved from the queue with this operation. */
numberOfMessages?: number;
/** Optional. Specifies the new visibility timeout value, in seconds, relative to server time. The default value is 30 seconds. A specified value must be larger than or equal to 1 second, and cannot be larger than 7 days, or larger than 2 hours on REST protocol versions prior to version 2011-08-18. The visibility timeout of a message can be set to a value later than the expiry time. */
visibilityTimeout?: number;
}
/**
* Options to configure {@link QueueClient.receiveMessages} operation
*/
export interface QueueReceiveMessageOptions extends MessagesDequeueOptionalParams, CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/** Optional parameters. */
export interface MessagesPeekOptionalParams extends CommonOptions {
/** The The timeout parameter is expressed in seconds. For more information, see <a href="https://docs.microsoft.com/en-us/rest/api/storageservices/setting-timeouts-for-queue-service-operations">Setting Timeouts for Queue Service Operations.</a> */
timeoutInSeconds?: number;
/** Provides a client-generated, opaque value with a 1 KB character limit that is recorded in the analytics logs when storage analytics logging is enabled. */
requestId?: string;
/** Optional. A nonzero integer value that specifies the number of messages to retrieve from the queue, up to a maximum of 32. If fewer are visible, the visible messages are returned. By default, a single message is retrieved from the queue with this operation. */
numberOfMessages?: number;
}
/**
* Options to configure {@link QueueClient.peekMessages} operation
*/
export interface QueuePeekMessagesOptions extends MessagesPeekOptionalParams, CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/**
* Contains the response data for the {@link QueueClient.sendMessage} operation.
*/
export declare type QueueSendMessageResponse = WithResponse<
{
/**
* The ID of the sent Message.
*/
messageId: string;
/**
* This value is required to delete the Message.
* If deletion fails using this popreceipt then the message has been received
* by another client.
*/
popReceipt: string;
/**
* The time that the message was inserted into the
* Queue.
*/
insertedOn: Date;
/**
* The time that the message will expire and be
* automatically deleted.
*/
expiresOn: Date;
/**
* The time that the message will again become
* visible in the Queue.
*/
nextVisibleOn: Date;
} & MessagesEnqueueHeaders,
MessagesEnqueueHeaders,
EnqueuedMessage[]
>;
/**
* The object returned in the `receivedMessageItems` array when calling {@link QueueClient.receiveMessages}.
*
* See: {@link QueueReceiveMessageResponse}
*/
export declare type ReceivedMessageItem = DequeuedMessageItem;
/**
* Contains the response data for the {@link QueueClient.receiveMessages} operation.
*/
export declare type QueueReceiveMessageResponse = WithResponse<
{
receivedMessageItems: ReceivedMessageItem[];
} & MessagesDequeueHeaders,
MessagesDequeueHeaders,
ReceivedMessageItem[]
>;
/**
* Contains the response data for the {@link QueueClient.peekMessages} operation.
*/
export declare type QueuePeekMessagesResponse = WithResponse<
{
peekedMessageItems: PeekedMessageItem[];
} & MessagesPeekHeaders,
MessagesPeekHeaders,
PeekedMessageItem[]
>;
/**
* Options to configure the {@link QueueClient.deleteMessage} operation
*/
export interface QueueDeleteMessageOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/**
* Contains response data for the {@link QueueClient.updateMessage} operation.
*/
export declare type QueueUpdateMessageResponse = MessageIdUpdateResponse;
/**
* Contains response data for the {@link QueueClient.deleteMessage} operation.
*/
export declare type QueueDeleteMessageResponse = MessageIdDeleteResponse;
/**
* Contains response data for the {@link QueueClient.clearMessages} operation.
*/
export declare type QueueClearMessagesResponse = MessagesClearResponse;
/**
* Options to configure {@link QueueClient.updateMessage} operation
*/
export interface QueueUpdateMessageOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*/
abortSignal?: AbortSignalLike;
}
/**
* Contains response data for the {@link QueueClient.createIfNotExists} operation.
*/
export interface QueueCreateIfNotExistsResponse extends QueueCreateResponse {
/**
* Indicate whether the queue is successfully created. Is false when the queue is not changed as it already exists.
*/
succeeded: boolean;
}
/**
* Contains response data for the {@link QueueClient.deleteIfExists} operation.
*/
export interface QueueDeleteIfExistsResponse extends QueueDeleteResponse {
/**
* Indicate whether the queue is successfully deleted. Is false if the queue does not exist in the first place.
*/
succeeded: boolean;
}
/**
* Options to configure {@link QueueClient.generateSasUrl} operation.
*/
export interface QueueGenerateSasUrlOptions {
/**
* The version of the service this SAS will target. If not specified, it will default to the version targeted by the
* library.
*/
version?: string;
/**
* Optional. SAS protocols, HTTPS only or HTTPSandHTTP
*/
protocol?: SASProtocol;
/**
* Optional. When the SAS will take effect.
*/
startsOn?: Date;
/**
* Optional only when identifier is provided. The time after which the SAS will no longer work.
*/
expiresOn?: Date;
/**
* Optional only when identifier is provided.
* Please refer to {@link QueueSASPermissions} for help constructing the permissions string.
*/
permissions?: QueueSASPermissions;
/**
* Optional. IP ranges allowed in this SAS.
*/
ipRange?: SasIPRange;
/**
* Optional. The name of the access policy on the queue this SAS references if any.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/establishing-a-stored-access-policy
*/
identifier?: string;
}
/**
* A QueueClient represents a URL to an Azure Storage Queue's messages allowing you to manipulate its messages.
*/
export class QueueClient extends StorageClient {
/**
* messagesContext provided by protocol layer.
*/
private messagesContext: Messages;
/**
* queueContext provided by protocol layer.
*/
private queueContext: Queue;
private _name: string;
private _messagesUrl: string;
/**
* The name of the queue.
*/
public get name(): string {
return this._name;
}
/**
* Creates an instance of QueueClient.
*
* @param connectionString - Account connection string or a SAS connection string of an Azure storage account.
* [ Note - Account connection string can only be used in NODE.JS runtime. ]
* Account connection string example -
* `DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=accountKey;EndpointSuffix=core.windows.net`
* SAS connection string example -
* `BlobEndpoint=https://myaccount.blob.core.windows.net/;QueueEndpoint=https://myaccount.queue.core.windows.net/;FileEndpoint=https://myaccount.file.core.windows.net/;TableEndpoint=https://myaccount.table.core.windows.net/;SharedAccessSignature=sasString`
* @param queueName - Queue name.
* @param options - Options to configure the HTTP pipeline.
*/
// Legacy, no way to fix the eslint error without breaking. Disable the rule for this line.
/* eslint-disable-next-line @azure/azure-sdk/ts-naming-options */
constructor(connectionString: string, queueName: string, options?: StoragePipelineOptions);
/**
* Creates an instance of QueueClient.
*
* @param url - A URL string pointing to Azure Storage queue, such as
* "https://myaccount.queue.core.windows.net/myqueue". You can
* append a SAS if using AnonymousCredential, such as
* "https://myaccount.queue.core.windows.net/myqueue?sasString".
* @param credential - Such as AnonymousCredential, StorageSharedKeyCredential or any credential from the `@azure/identity` package to authenticate requests to the service. You can also provide an object that implements the TokenCredential interface. If not specified, AnonymousCredential is used.
* @param options - Options to configure the HTTP pipeline.
*/
constructor(
url: string,
credential?: StorageSharedKeyCredential | AnonymousCredential | TokenCredential,
// Legacy, no way to fix the eslint error without breaking. Disable the rule for this line.
/* eslint-disable-next-line @azure/azure-sdk/ts-naming-options */
options?: StoragePipelineOptions,
);
/**
* Creates an instance of QueueClient.
*
* @param url - A URL string pointing to Azure Storage queue, such as
* "https://myaccount.queue.core.windows.net/myqueue". You can
* append a SAS if using AnonymousCredential, such as
* "https://myaccount.queue.core.windows.net/myqueue?sasString".
* @param pipeline - Call newPipeline() to create a default
* pipeline, or provide a customized pipeline.
*/
constructor(url: string, pipeline: Pipeline);
constructor(
urlOrConnectionString: string,
credentialOrPipelineOrQueueName?:
| StorageSharedKeyCredential
| AnonymousCredential
| TokenCredential
| Pipeline
| string,
// Legacy, no way to fix the eslint error without breaking. Disable the rule for this line.
/* eslint-disable-next-line @azure/azure-sdk/ts-naming-options */
options?: StoragePipelineOptions,
) {
options = options || {};
let pipeline: Pipeline;
let url: string;
if (isPipelineLike(credentialOrPipelineOrQueueName)) {
// (url: string, pipeline: Pipeline)
url = urlOrConnectionString;
pipeline = credentialOrPipelineOrQueueName;
} else if (
(isNode && credentialOrPipelineOrQueueName instanceof StorageSharedKeyCredential) ||
credentialOrPipelineOrQueueName instanceof AnonymousCredential ||
isTokenCredential(credentialOrPipelineOrQueueName)
) {
// (url: string, credential?: StorageSharedKeyCredential | AnonymousCredential | TokenCredential, options?: StoragePipelineOptions)
url = urlOrConnectionString;
pipeline = newPipeline(credentialOrPipelineOrQueueName, options);
} else if (
!credentialOrPipelineOrQueueName &&
typeof credentialOrPipelineOrQueueName !== "string"
) {
// (url: string, credential?: StorageSharedKeyCredential | AnonymousCredential | TokenCredential, options?: StoragePipelineOptions)
// The second parameter is undefined. Use anonymous credential.
url = urlOrConnectionString;
pipeline = newPipeline(new AnonymousCredential(), options);
} else if (
credentialOrPipelineOrQueueName &&
typeof credentialOrPipelineOrQueueName === "string"
) {
// (connectionString: string, containerName: string, queueName: string, options?: StoragePipelineOptions)
const extractedCreds = extractConnectionStringParts(urlOrConnectionString);
if (extractedCreds.kind === "AccountConnString") {
if (isNode) {
const queueName = credentialOrPipelineOrQueueName;
const sharedKeyCredential = new StorageSharedKeyCredential(
extractedCreds.accountName,
extractedCreds.accountKey,
);
url = appendToURLPath(extractedCreds.url, queueName);
if (!options.proxyOptions) {
options.proxyOptions = getDefaultProxySettings(extractedCreds.proxyUri);
}
pipeline = newPipeline(sharedKeyCredential, options);
} else {
throw new Error("Account connection string is only supported in Node.js environment");
}
} else if (extractedCreds.kind === "SASConnString") {
const queueName = credentialOrPipelineOrQueueName;
url = appendToURLPath(extractedCreds.url, queueName) + "?" + extractedCreds.accountSas;
pipeline = newPipeline(new AnonymousCredential(), options);
} else {
throw new Error(
"Connection string must be either an Account connection string or a SAS connection string",
);
}
} else {
throw new Error("Expecting non-empty strings for queueName parameter");
}
super(url, pipeline);
this._name = this.getQueueNameFromUrl();
this.queueContext = this.storageClientContext.queue;
// MessagesContext
// Build the url with "messages"
const partsOfUrl = this.url.split("?");
this._messagesUrl = partsOfUrl[1]
? appendToURLPath(partsOfUrl[0], "messages") + "?" + partsOfUrl[1]
: appendToURLPath(partsOfUrl[0], "messages");
this.messagesContext = getStorageClientContext(this._messagesUrl, this.pipeline).messages;
}
private getMessageIdContext(messageId: string): MessageId {
// Build the url with messageId
const partsOfUrl = this._messagesUrl.split("?");
const urlWithMessageId = partsOfUrl[1]
? appendToURLPath(partsOfUrl[0], messageId) + "?" + partsOfUrl[1]
: appendToURLPath(partsOfUrl[0], messageId);
return getStorageClientContext(urlWithMessageId, this.pipeline).messageId;
}
/**
* Creates a new queue under the specified account.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/create-queue4
*
* @param options - Options to Queue create operation.
* @returns Response data for the Queue create operation.
*
* Example usage:
*
* ```js
* const queueClient = queueServiceClient.getQueueClient("<new queue name>");
* const createQueueResponse = await queueClient.create();
* ```
*/
public async create(options: QueueCreateOptions = {}): Promise<QueueCreateResponse> {
return tracingClient.withSpan("QueueClient-create", options, async (updatedOptions) => {
return assertResponse<QueueCreateHeaders, QueueCreateHeaders>(
await this.queueContext.create(updatedOptions),
);
});
}
/**
* Creates a new queue under the specified account if it doesn't already exist.
* If the queue already exists, it is not changed.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/create-queue4
*
* @param options -
*/
public async createIfNotExists(
options: QueueCreateOptions = {},
): Promise<QueueCreateIfNotExistsResponse> {
return tracingClient.withSpan(
"QueueClient-createIfNotExists",
options,
async (updatedOptions) => {
try {
const response = await this.create(updatedOptions);
// When a queue with the specified name already exists, the Queue service checks the metadata associated with the existing queue.
// If the existing metadata is identical to the metadata specified on the Create Queue request, status code 204 (No Content) is returned.
// If the existing metadata does not match, the operation fails and status code 409 (Conflict) is returned.
if (response._response.status === 204) {
return {
succeeded: false,
...response,
};
}
return {
succeeded: true,
...response,
};
} catch (e: any) {
if (e.details?.errorCode === "QueueAlreadyExists") {
return {
succeeded: false,
...e.response?.parsedHeaders,
_response: e.response,
};
}
throw e;
}
},
);
}
/**
* Deletes the specified queue permanently if it exists.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/delete-queue3
*
* @param options -
*/
public async deleteIfExists(
options: QueueDeleteOptions = {},
): Promise<QueueDeleteIfExistsResponse> {
return tracingClient.withSpan("QueueClient-deleteIfExists", options, async (updatedOptions) => {
try {
const res = await this.delete(updatedOptions);
return {
succeeded: true,
...res,
};
} catch (e: any) {
if (e.details?.errorCode === "QueueNotFound") {
return {
succeeded: false,
...e.response?.parsedHeaders,
_response: e.response,
};
}
throw e;
}
});
}
/**
* Deletes the specified queue permanently.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/delete-queue3
*
* @param options - Options to Queue delete operation.
* @returns Response data for the Queue delete operation.
*
* Example usage:
*
* ```js
* const deleteQueueResponse = await queueClient.delete();
* console.log(
* "Delete queue successfully, service assigned request Id:", deleteQueueResponse.requestId
* );
* ```
*/
public async delete(options: QueueDeleteOptions = {}): Promise<QueueDeleteResponse> {
return tracingClient.withSpan("QueueClient-delete", options, async (updatedOptions) => {
return assertResponse<QueueDeleteHeaders, QueueDeleteHeaders>(
await this.queueContext.delete({
abortSignal: options.abortSignal,
tracingOptions: updatedOptions.tracingOptions,
}),
);
});
}
/**
* Returns true if the specified queue exists; false otherwise.
*
* NOTE: use this function with care since an existing queue might be deleted by other clients or
* applications. Vice versa new queues might be added by other clients or applications after this
* function completes.
*
* @param options - options to Exists operation.
*/
public async exists(options: QueueExistsOptions = {}): Promise<boolean> {
return tracingClient.withSpan("QueueClient-exists", options, async (updatedOptions) => {
try {
await this.getProperties(updatedOptions);
return true;
} catch (e: any) {
if (e.statusCode === 404) {
return false;
}
throw e;
}
});
}
/**
* Gets all user-defined metadata and system properties for the specified
* queue. Metadata is associated with the queue as name-values pairs.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/get-queue-metadata
*
* WARNING: The `metadata` object returned in the response will have its keys in lowercase, even if
* they originally contained uppercase characters. This differs from the metadata keys returned by
* the `listQueues` method of {@link QueueServiceClient} using the `includeMetadata` option, which
* will retain their original casing.
*
* @param options - Options to Queue get properties operation.
* @returns Response data for the Queue get properties operation.
*/
public async getProperties(
options: QueueGetPropertiesOptions = {},
): Promise<QueueGetPropertiesResponse> {
return tracingClient.withSpan("QueueClient-getProperties", options, async (updatedOptions) => {
return assertResponse<QueueGetPropertiesHeaders, QueueGetPropertiesHeaders>(
await this.queueContext.getProperties(updatedOptions),
);
});
}
/**
* Sets one or more user-defined name-value pairs for the specified queue.
*
* If no option provided, or no metadata defined in the option parameter, the queue
* metadata will be removed.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/set-queue-metadata
*
* @param metadata - If no metadata provided, all existing metadata will be removed.
* @param options - Options to Queue set metadata operation.
* @returns Response data for the Queue set metadata operation.
*/
public async setMetadata(
metadata?: Metadata,
options: QueueSetMetadataOptions = {},
): Promise<QueueSetMetadataResponse> {
return tracingClient.withSpan("QueueClient-setMetadata", options, async (updatedOptions) => {
return assertResponse<QueueSetMetadataHeaders, QueueSetMetadataHeaders>(
await this.queueContext.setMetadata({
...updatedOptions,
metadata,
}),
);
});
}
/**
* Gets details about any stored access policies specified on the queue that may be used with Shared Access Signatures.
*
* WARNING: JavaScript Date will potential lost precision when parsing start and expiry string.
* For example, new Date("2018-12-31T03:44:23.8827891Z").toISOString() will get "2018-12-31T03:44:23.882Z".
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/get-queue-acl
*
* @param options - Options to Queue get access policy operation.
* @returns Response data for the Queue get access policy operation.
*/
public async getAccessPolicy(
options: QueueGetAccessPolicyOptions = {},
): Promise<QueueGetAccessPolicyResponse> {
return tracingClient.withSpan(
"QueueClient-getAccessPolicy",
options,
async (updatedOptions) => {
const response = assertResponse<
QueueGetAccessPolicyHeaders & SignedIdentifierModel[],
QueueGetAccessPolicyHeaders,
SignedIdentifierModel[]
>(
await this.queueContext.getAccessPolicy({
abortSignal: options.abortSignal,
tracingOptions: updatedOptions.tracingOptions,
}),
);
const res: QueueGetAccessPolicyResponse = {
_response: response._response,
date: response.date,
requestId: response.requestId,
clientRequestId: response.clientRequestId,
signedIdentifiers: [],
version: response.version,
errorCode: response.errorCode,
};
for (const identifier of response) {
let accessPolicy: any = undefined;
if (identifier.accessPolicy) {
accessPolicy = {
permissions: identifier.accessPolicy.permissions,
};
if (identifier.accessPolicy.expiresOn) {
accessPolicy.expiresOn = new Date(identifier.accessPolicy.expiresOn);
}
if (identifier.accessPolicy.startsOn) {
accessPolicy.startsOn = new Date(identifier.accessPolicy.startsOn);
}
}
res.signedIdentifiers.push({
accessPolicy,
id: identifier.id,
});
}
return res;
},
);
}
/**
* Sets stored access policies for the queue that may be used with Shared Access Signatures.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/set-queue-acl
*
* @param queueAcl -
* @param options - Options to Queue set access policy operation.
* @returns Response data for the Queue set access policy operation.
*/
public async setAccessPolicy(
queueAcl?: SignedIdentifier[],
options: QueueSetAccessPolicyOptions = {},
): Promise<QueueSetAccessPolicyResponse> {
return tracingClient.withSpan(
"QueueClient-setAccessPolicy",
options,
async (updatedOptions) => {
const acl: SignedIdentifierModel[] = [];
for (const identifier of queueAcl || []) {
acl.push({
accessPolicy: {
expiresOn: identifier.accessPolicy.expiresOn
? truncatedISO8061Date(identifier.accessPolicy.expiresOn)
: undefined,
permissions: identifier.accessPolicy.permissions,
startsOn: identifier.accessPolicy.startsOn
? truncatedISO8061Date(identifier.accessPolicy.startsOn)
: undefined,
},
id: identifier.id,
});
}
return assertResponse<QueueSetAccessPolicyHeaders, QueueSetAccessPolicyHeaders>(
await this.queueContext.setAccessPolicy({
...updatedOptions,
queueAcl: acl,
}),
);
},
);
}
/**
* Clear deletes all messages from a queue.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/clear-messages
*
* @param options - Options to clear messages operation.
* @returns Response data for the clear messages operation.
*/
public async clearMessages(
options: QueueClearMessagesOptions = {},
): Promise<QueueClearMessagesResponse> {
return tracingClient.withSpan("QueueClient-clearMessages", options, async (updatedOptions) => {
return assertResponse<MessagesClearHeaders, MessagesClearHeaders>(
await this.messagesContext.clear(updatedOptions),
);
});
}
/**
* sendMessage adds a new message to the back of a queue. The visibility timeout specifies how long
* the message should be invisible to Dequeue and Peek operations.
* The message content is up to 64KB in size, and must be in a format that can be included in an XML request with UTF-8 encoding.
* To include markup in the message, the contents of the message must either be XML-escaped or Base64-encode.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/put-message
*
* @param messageText - Text of the message to send
* @param options - Options to send messages operation.
* @returns Response data for the send messages operation.
*
* Example usage:
*
* ```js
* const sendMessageResponse = await queueClient.sendMessage("Hello World!");
* console.log(
* "Sent message successfully, service assigned message Id:", sendMessageResponse.messageId,
* "service assigned request Id:", sendMessageResponse.requestId
* );
* ```
*/
public async sendMessage(
messageText: string,
options: QueueSendMessageOptions = {},
): Promise<QueueSendMessageResponse> {
return tracingClient.withSpan("QueueClient-sendMessage", options, async (updatedOptions) => {
const response = assertResponse<
MessagesEnqueueHeaders & EnqueuedMessage[],
MessagesEnqueueHeaders,
EnqueuedMessage[]
>(
await this.messagesContext.enqueue(
{
messageText: messageText,
},
updatedOptions,
),
);
const item = response[0];
return {
_response: response._response,
date: response.date,
requestId: response.requestId,
clientRequestId: response.clientRequestId,
version: response.version,
errorCode: response.errorCode,
messageId: item.messageId,
popReceipt: item.popReceipt,
nextVisibleOn: item.nextVisibleOn,
insertedOn: item.insertedOn,
expiresOn: item.expiresOn,
};
});
}
/**
* receiveMessages retrieves one or more messages from the front of the queue.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/get-messages
*
* @param options - Options to receive messages operation.
* @returns Response data for the receive messages operation.
*
* Example usage:
*
* ```js
* const response = await queueClient.receiveMessages();
* if (response.receivedMessageItems.length == 1) {
* const receivedMessageItem = response.receivedMessageItems[0];
* console.log("Processing & deleting message with content:", receivedMessageItem.messageText);
* const deleteMessageResponse = await queueClient.deleteMessage(
* receivedMessageItem.messageId,
* receivedMessageItem.popReceipt
* );
* console.log(
* "Delete message successfully, service assigned request Id:",
* deleteMessageResponse.requestId
* );
* }
* ```
*/
public async receiveMessages(
options: QueueReceiveMessageOptions = {},
): Promise<QueueReceiveMessageResponse> {
return tracingClient.withSpan(
"QueueClient-receiveMessages",
options,
async (updatedOptions) => {
const response = assertResponse<
MessagesDequeueHeaders & DequeuedMessageItem[],
MessagesDequeueHeaders,
DequeuedMessageItem[]
>(await this.messagesContext.dequeue(updatedOptions));
const res: QueueReceiveMessageResponse = {
_response: response._response,
date: response.date,
requestId: response.requestId,
clientRequestId: response.clientRequestId,