/
QueueClient.ts
1136 lines (1080 loc) · 37.7 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. All rights reserved.
// Licensed under the MIT License.
import {
HttpResponse,
TokenCredential,
isTokenCredential,
isNode,
getDefaultProxySettings,
URLBuilder
} from "@azure/core-http";
import { CanonicalCode } from "@opentelemetry/types";
import {
EnqueuedMessage,
DequeuedMessageItem,
MessagesDequeueHeaders,
MessagesDequeueOptionalParams,
MessagesEnqueueHeaders,
MessagesEnqueueOptionalParams,
MessagesPeekHeaders,
MessagesPeekOptionalParams,
MessageIdUpdateResponse,
MessageIdDeleteResponse,
MessagesClearResponse,
PeekedMessageItem,
QueueCreateResponse,
QueueDeleteResponse,
QueueGetAccessPolicyHeaders,
QueueGetPropertiesResponse,
QueueSetAccessPolicyResponse,
QueueSetMetadataResponse,
SignedIdentifierModel
} from "./generatedModels";
import { AbortSignalLike } from "@azure/abort-controller";
import { Messages, MessageId, Queue } from "./generated/src/operations";
import { newPipeline, StoragePipelineOptions, Pipeline } from "./Pipeline";
import { StorageClient, CommonOptions } from "./StorageClient";
import {
appendToURLPath,
extractConnectionStringParts,
truncatedISO8061Date,
getStorageClientContext
} from "./utils/utils.common";
import { StorageSharedKeyCredential } from "./credentials/StorageSharedKeyCredential";
import { AnonymousCredential } from "./credentials/AnonymousCredential";
import { createSpan } from "./utils/tracing";
import { Metadata } from "./models";
import { getCachedDefaultHttpClient } from "./utils/cache";
/**
* Options to configure {@link QueueClient.create} operation
*
* @export
* @interface QueueCreateOptions
*/
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`.
*
* @type {AbortSignalLike}
* @memberof QueueCreateOptions
*/
abortSignal?: AbortSignalLike;
/**
* A collection of key-value string pair to associate with the queue object.
* The keys need to be lower-case.
*
* @type {Metadata}
* @memberof QueueCreateOptions
*/
metadata?: Metadata;
}
/**
* Options to configure {@link QueueClient.getProperties} operation
*
* @export
* @interface QueueGetPropertiesOptions
*/
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`.
*
* @type {AbortSignalLike}
* @memberof QueueGetPropertiesOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure {@link QueueClient.delete} operation
*
* @export
* @interface QueueDeleteOptions
*/
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`.
*
* @type {AbortSignalLike}
* @memberof QueueDeleteOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure {@link QueueClient.getAccessPolicy} operation
*
* @export
* @interface QueueGetAccessPolicyOptions
*/
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`.
*
* @type {AbortSignalLike}
* @memberof QueueGetAccessPolicyOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure {@link QueueClient.setAccessPolicy} operation
*
* @export
* @interface QueueSetAccessPolicyOptions
*/
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`.
*
* @type {AbortSignalLike}
* @memberof QueueSetAccessPolicyOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure {@link QueueClient.setMetadata} operation
*
* @export
* @interface QueueSetMetadataOptions
*/
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`.
*
* @type {AbortSignalLike}
* @memberof QueueSetMetadataOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Signed identifier.
*
* @export
* @interface SignedIdentifier
*/
export interface SignedIdentifier {
/**
* @member {string} id a unique id
*/
id: string;
/**
* @member {AccessPolicy} accessPolicy
*/
accessPolicy: {
/**
* @member {Date} startsOn the date-time the policy is active.
*/
startsOn: Date;
/**
* @member {string} expiresOn the date-time the policy expires.
*/
expiresOn: Date;
/**
* @member {string} permission 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 = {
signedIdentifiers: SignedIdentifier[];
} & QueueGetAccessPolicyHeaders & {
/**
* The underlying HTTP response.
*/
_response: HttpResponse & {
/**
* The parsed HTTP response headers.
*/
parsedHeaders: QueueGetAccessPolicyHeaders;
/**
* The response body as text (string format)
*/
bodyAsText: string;
/**
* The response body as parsed JSON or XML
*/
parsedBody: SignedIdentifierModel[];
};
};
/**
* Options to configure {@link QueueClient.clearMessages} operation
*
* @export
* @interface QueueClearMessagesOptions
*/
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`.
*
* @type {AbortSignalLike}
* @memberof QueueClearMessagesOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure {@link QueueClient.sendMessage} operation
*
* @export
* @interface QueueSendMessageOptions
* @extends {MessagesEnqueueOptionalParams}
*/
export interface QueueSendMessageOptions extends MessagesEnqueueOptionalParams, CommonOptions {}
/**
* Options to configure {@link QueueClient.receiveMessages} operation
*
* @export
* @interface QueueReceiveMessageOptions
* @extends {MessagesDequeueOptionalParams}
*/
export interface QueueReceiveMessageOptions extends MessagesDequeueOptionalParams, CommonOptions {}
/**
* Options to configure {@link QueueClient.peekMessages} operation
*
* @export
* @interface QueuePeekMessagesOptions
* @extends {MessagesPeekOptionalParams}
*/
export interface QueuePeekMessagesOptions extends MessagesPeekOptionalParams, CommonOptions {}
/**
* Contains the response data for the {@link QueueClient.sendMessage} operation.
*/
export declare type QueueSendMessageResponse = {
/**
* @member {string} messageId The ID of the sent Message.
*/
messageId: string;
/**
* @member {string} popReceipt 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;
/**
* @member {Date} insertedOn The time that the message was inserted into the
* Queue.
*/
insertedOn: Date;
/**
* @member {Date} expiresOn The time that the message will expire and be
* automatically deleted.
*/
expiresOn: Date;
/**
* @member {Date} nextVisibleOn The time that the message will again become
* visible in the Queue.
*/
nextVisibleOn: Date;
} & MessagesEnqueueHeaders & {
/**
* The underlying HTTP response.
*/
_response: HttpResponse & {
/**
* The parsed HTTP response headers.
*/
parsedHeaders: MessagesEnqueueHeaders;
/**
* The response body as text (string format)
*/
bodyAsText: string;
/**
* The response body as parsed JSON or XML
*/
parsedBody: 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 = {
receivedMessageItems: ReceivedMessageItem[];
} & MessagesDequeueHeaders & {
/**
* The underlying HTTP response.
*/
_response: HttpResponse & {
/**
* The parsed HTTP response headers.
*/
parsedHeaders: MessagesDequeueHeaders;
/**
* The response body as text (string format)
*/
bodyAsText: string;
/**
* The response body as parsed JSON or XML
*/
parsedBody: ReceivedMessageItem[];
};
};
/**
* Contains the response data for the {@link QueueClient.peekMessages} operation.
*/
export declare type QueuePeekMessagesResponse = {
peekedMessageItems: PeekedMessageItem[];
} & MessagesPeekHeaders & {
/**
* The underlying HTTP response.
*/
_response: HttpResponse & {
/**
* The parsed HTTP response headers.
*/
parsedHeaders: MessagesPeekHeaders;
/**
* The response body as text (string format)
*/
bodyAsText: string;
/**
* The response body as parsed JSON or XML
*/
parsedBody: PeekedMessageItem[];
};
};
/**
* Options to configure the {@link QueueClient.deleteMessage} operation
*
* @export
* @interface QueueDeleteMessageOptions
*/
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`.
*
* @type {AbortSignalLike}
* @memberof QueueDeleteMessageOptions
*/
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
*/
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`.
*
* @type {AbortSignalLike}
* @memberof QueueUpdateMessageOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* A QueueClient represents a URL to an Azure Storage Queue's messages allowing you to manipulate its messages.
*
* @export
* @class QueueClient
*/
export class QueueClient extends StorageClient {
/**
* messagesContext provided by protocol layer.
*
* @private
* @type {Messages}
* @memberof QueueClient
*/
private messagesContext: Messages;
/**
* queueContext provided by protocol layer.
*
* @private
* @type {Queue}
* @memberof QueueClient
*/
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 {string} 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 {string} queueName Queue name.
* @param {StoragePipelineOptions} [options] Options to configure the HTTP pipeline.
* @memberof QueueClient
*/
constructor(connectionString: string, queueName: string, options?: StoragePipelineOptions);
/**
* Creates an instance of QueueClient.
*
* @param {string} 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 {StorageSharedKeyCredential | AnonymousCredential | TokenCredential} 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 {StoragePipelineOptions} [options] Options to configure the HTTP pipeline.
* @memberof QueueClient
*/
constructor(
url: string,
credential?: StorageSharedKeyCredential | AnonymousCredential | TokenCredential,
options?: StoragePipelineOptions
);
/**
* Creates an instance of QueueClient.
*
* @param {string} 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} pipeline Call newPipeline() to create a default
* pipeline, or provide a customized pipeline.
* @memberof QueueClient
*/
constructor(url: string, pipeline: Pipeline);
constructor(
urlOrConnectionString: string,
credentialOrPipelineOrQueueName?:
| StorageSharedKeyCredential
| AnonymousCredential
| TokenCredential
| Pipeline
| string,
options?: StoragePipelineOptions
) {
// when options.httpClient is not specified, passing in a DefaultHttpClient instance to
// avoid each client creating its own http client.
const newOptions: StoragePipelineOptions = {
httpClient: getCachedDefaultHttpClient(),
...options
};
let pipeline: Pipeline;
let url: string;
if (credentialOrPipelineOrQueueName instanceof Pipeline) {
// (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, newOptions);
} else if (
!credentialOrPipelineOrQueueName &&
typeof credentialOrPipelineOrQueueName !== "string"
) {
// (url: string, credential?: StorageSharedKeyCredential | AnonymousCredential | TokenCredential, options?: StoragePipelineOptions)
// The second paramter is undefined. Use anonymous credential.
url = urlOrConnectionString;
pipeline = newPipeline(new AnonymousCredential(), newOptions);
} 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);
newOptions.proxyOptions = getDefaultProxySettings(extractedCreds.proxyUri);
pipeline = newPipeline(sharedKeyCredential, newOptions);
} 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(), newOptions);
} 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 = new Queue(this.storageClientContext);
// 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 = new Messages(getStorageClientContext(this._messagesUrl, this.pipeline));
}
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 new MessageId(getStorageClientContext(urlWithMessageId, this.pipeline));
}
/**
* Creates a new queue under the specified account.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/create-queue4
*
* @param {QueueCreateOptions} [options] Options to Queue create operation.
* @returns {Promise<QueueCreateResponse>} Response data for the Queue create operation.
* @memberof QueueClient
*
* Example usage:
*
* ```js
* const queueClient = queueServiceClient.getQueueClient("<new queue name>");
* const createQueueResponse = await queueClient.create();
* ```
*/
public async create(options: QueueCreateOptions = {}): Promise<QueueCreateResponse> {
const { span, spanOptions } = createSpan("QueueClient-create", options.tracingOptions);
try {
return await this.queueContext.create({
...options,
abortSignal: options.abortSignal,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Deletes the specified queue permanently.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/delete-queue3
*
* @param {QueueDeleteOptions} [options] Options to Queue delete operation.
* @returns {Promise<QueueDeleteResponse>} Response data for the Queue delete operation.
* @memberof QueueClient
*
* 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> {
const { span, spanOptions } = createSpan("QueueClient-delete", options.tracingOptions);
try {
return await this.queueContext.deleteMethod({
abortSignal: options.abortSignal,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* 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 {QueueGetPropertiesOptions} [options] Options to Queue get properties operation.
* @returns {Promise<QueueGetPropertiesResponse>} Response data for the Queue get properties operation.
* @memberof QueueClient
*/
public async getProperties(
options: QueueGetPropertiesOptions = {}
): Promise<QueueGetPropertiesResponse> {
const { span, spanOptions } = createSpan("QueueClient-getProperties", options.tracingOptions);
try {
return await this.queueContext.getProperties({
abortSignal: options.abortSignal,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* 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} [metadata] If no metadata provided, all existing metadata will be removed.
* @param {QueueSetMetadataOptions} [options] Options to Queue set metadata operation.
* @returns {Promise<QueueSetMetadataResponse>} Response data for the Queue set metadata operation.
* @memberof QueueClient
*/
public async setMetadata(
metadata?: Metadata,
options: QueueSetMetadataOptions = {}
): Promise<QueueSetMetadataResponse> {
const { span, spanOptions } = createSpan("QueueClient-setMetadata", options.tracingOptions);
try {
return await this.queueContext.setMetadata({
abortSignal: options.abortSignal,
metadata,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* 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 {QueueGetAccessPolicyOptions} [options] Options to Queue get access policy operation.
* @returns {Promise<QueueGetAccessPolicyResponse>} Response data for the Queue get access policy operation.
* @memberof QueueClient
*/
public async getAccessPolicy(
options: QueueGetAccessPolicyOptions = {}
): Promise<QueueGetAccessPolicyResponse> {
const { span, spanOptions } = createSpan("QueueClient-getAccessPolicy", options.tracingOptions);
try {
const response = await this.queueContext.getAccessPolicy({
abortSignal: options.abortSignal,
spanOptions
});
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) {
res.signedIdentifiers.push({
accessPolicy: {
expiresOn: new Date(identifier.accessPolicy.expiresOn),
permissions: identifier.accessPolicy.permissions,
startsOn: new Date(identifier.accessPolicy.startsOn)
},
id: identifier.id
});
}
return res;
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* 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 {SignedIdentifier[]} [queueAcl]
* @param {QueueSetAccessPolicyOptions} [options] Options to Queue set access policy operation.
* @returns {Promise<QueueSetAccessPolicyResponse>} Response data for the Queue set access policy operation.
* @memberof QueueClient
*/
public async setAccessPolicy(
queueAcl?: SignedIdentifier[],
options: QueueSetAccessPolicyOptions = {}
): Promise<QueueSetAccessPolicyResponse> {
const { span, spanOptions } = createSpan("QueueClient-setAccessPolicy", options.tracingOptions);
try {
const acl: SignedIdentifierModel[] = [];
for (const identifier of queueAcl || []) {
acl.push({
accessPolicy: {
expiresOn: truncatedISO8061Date(identifier.accessPolicy.expiresOn),
permissions: identifier.accessPolicy.permissions,
startsOn: truncatedISO8061Date(identifier.accessPolicy.startsOn)
},
id: identifier.id
});
}
return await this.queueContext.setAccessPolicy({
abortSignal: options.abortSignal,
queueAcl: acl,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Clear deletes all messages from a queue.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/clear-messages
*
* @param {QueueClearMessagesOptions} [options] Options to clear messages operation.
* @returns {Promise<QueueClearMessagesResponse>} Response data for the clear messages operation.
* @memberof QueueClient
*/
public async clearMessages(
options: QueueClearMessagesOptions = {}
): Promise<QueueClearMessagesResponse> {
const { span, spanOptions } = createSpan("QueueClient-clearMessages", options.tracingOptions);
try {
return await this.messagesContext.clear({
abortSignal: options.abortSignal,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* 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 {string} messageText Text of the message to send
* @param {QueueSendMessageOptions} [options] Options to send messages operation.
* @returns {Promise<QueueSendMessageResponse>} Response data for the send messages operation.
* @memberof QueueClient
*
* 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> {
const { span, spanOptions } = createSpan("QueueClient-sendMessage", options.tracingOptions);
try {
const response = await this.messagesContext.enqueue(
{
messageText: messageText
},
{
abortSignal: options.abortSignal,
...options,
spanOptions
}
);
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
};
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* 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 {QueueReceiveMessageOptions} [options] Options to receive messages operation.
* @returns {Promise<QueueReceiveMessageResponse>} Response data for the receive messages operation.
* @memberof QueueClient
*
* 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 succesfully, service assigned request Id:",
* deleteMessageResponse.requestId
* );
* }
* ```
*/
public async receiveMessages(
options: QueueReceiveMessageOptions = {}
): Promise<QueueReceiveMessageResponse> {
const { span, spanOptions } = createSpan("QueueClient-receiveMessages", options.tracingOptions);
try {
const response = await this.messagesContext.dequeue({
abortSignal: options.abortSignal,
...options,
spanOptions
});
const res: QueueReceiveMessageResponse = {
_response: response._response,
date: response.date,
requestId: response.requestId,
clientRequestId: response.clientRequestId,
receivedMessageItems: [],
version: response.version,
errorCode: response.errorCode
};
for (const item of response) {
res.receivedMessageItems.push(item);
}
return res;
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* peekMessages retrieves one or more messages from the front of the queue but does not alter the visibility of the message.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/peek-messages
*
* @param {QueuePeekMessagesOptions} [options] Options to peek messages operation.
* @returns {QueuePeekMessagesResponse>} Response data for the peek messages operation.
* @memberof QueueClient
*
* Example usage:
*
* ```js
* const peekMessagesResponse = await queueClient.peekMessages();
* console.log("The peeked message is:", peekMessagesResponse.peekedMessageItems[0].messageText);
* ```
*/
public async peekMessages(
options: QueuePeekMessagesOptions = {}
): Promise<QueuePeekMessagesResponse> {
const { span, spanOptions } = createSpan("QueueClient-peekMessages", options.tracingOptions);
try {
const response = await this.messagesContext.peek({
abortSignal: options.abortSignal,
...options,
spanOptions
});
const res: QueuePeekMessagesResponse = {
_response: response._response,
date: response.date,