azurite
Version:
An open source Azure Storage API compatible server
133 lines • 3.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Provides access to headers for batch requests.
* As requests in an entity group transaction have different headers per
* transaction, and these need to be handled separately to the
* outer request envelope.
*
* @export
* @class BatchRequestHeaders
*/
class BatchRequestHeaders {
constructor(headers) {
this.headerItems = {};
this.headerCount = 0;
this.rawHeaders = headers;
this.createDictFromRawHeaders();
}
/**
* Returns the raw headers as a string array
*
* @return {*}
* @memberof BatchRequestHeaders
*/
getRawHeaders() {
return this.rawHeaders;
}
/**
* Checks for existence of a header
*
* @param {string} key
* @return {*} {boolean}
* @memberof BatchRequestHeaders
*/
containsHeader(key) {
return this.headerItems.hasOwnProperty(key);
}
/**
* The count of headers
*
* @return {*} {number}
* @memberof BatchRequestHeaders
*/
count() {
return this.headerCount;
}
/**
* Add a header to the header items
*
* @param {string} key
* @param {string} value
* @memberof BatchRequestHeaders
*/
add(key, value) {
if (!this.headerItems.hasOwnProperty(key))
this.headerCount++;
this.headerItems[key] = value;
}
/**
* Remove a header from the header items
*
* @param {string} key
* @return {*} {string}
* @memberof BatchRequestHeaders
*/
remove(key) {
const val = this.headerItems[key];
delete this.headerItems[key];
this.headerCount--;
return val;
}
/**
* Returns the header value based on a lower case lookup of the key
*
* @param {string} key
* @return {*} {string}
* @memberof BatchRequestHeaders
*/
header(key) {
return this.headerItems[key.toLocaleLowerCase()];
}
/**
* The header keys as a string array
*
* @return {*} {string[]}
* @memberof BatchRequestHeaders
*/
headerKeys() {
const headers = [];
for (const prop in this.headerItems) {
if (this.headerItems.hasOwnProperty(prop)) {
headers.push(prop);
}
}
return headers;
}
/**
* Header values as a string array
*
* @return {*} {string[]}
* @memberof BatchRequestHeaders
*/
headerValues() {
const values = [];
for (const prop in this.headerItems) {
if (this.headerItems.hasOwnProperty(prop)) {
values.push(this.headerItems[prop]);
}
}
return values;
}
/**
* Creates the dictionary to allow key value lookups on the headers
*
* @private
* @memberof BatchRequestHeaders
*/
createDictFromRawHeaders() {
this.rawHeaders.forEach((rawheader) => {
if (rawheader != null) {
const headerMatch = rawheader.match(/(\S+)(:\s?)(\S+)/);
if (headerMatch == null && rawheader.length > 2) {
this.add(rawheader, "");
}
else if (headerMatch != null) {
this.add(headerMatch[1].toLocaleLowerCase(), headerMatch[3]);
}
}
});
}
}
exports.default = BatchRequestHeaders;
//# sourceMappingURL=BatchRequestHeaders.js.map