azurite
Version:
An open source Azure Storage API compatible server
78 lines • 2.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.EdmGuid = void 0;
const constants_1 = require("../utils/constants");
const EntityProperty_1 = require("./EntityProperty");
class EdmGuid {
/**
* Stores value as base64
* @param value
* @returns
*/
static validate(value) {
if (typeof value !== "string") {
throw TypeError(`Not a valid EdmGuid string.`);
}
// TODO: Check GUID string format
// we need to store GUID in base64 to avoid finding with a string query
const guidBuff = Buffer.from(value);
return guidBuff.toString("base64");
}
constructor(value) {
this.value = value;
this.typedValue = EdmGuid.validate(value);
}
toJsonPropertyValuePair(name) {
return [name, this.typedValue];
}
/**
* We store GUIDs as base64 encoded strings to stop them being found
* by simple string searches.
* We must support backwards compatability, so cover both cases.
* @param name
* @returns
*/
toJsonPropertyValueString(name) {
if (EdmGuid.isBase64Encoded(this.value)) {
const binData = Buffer.from(this.value, "base64");
const decoded = binData.toString("utf8");
return `"${name}":${JSON.stringify(decoded)}`;
}
return `"${name}":${JSON.stringify(this.value)}`;
}
static isBase64Encoded(value) {
const stringValue = value;
const matches = stringValue.match(/^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{1}=)?$/);
return (matches !== null &&
matches?.length === 3 &&
(matches[2] === undefined || matches[2].length === 4));
}
toJsonPropertyTypePair(name, annotationLevel, isSystemProperty) {
if (isSystemProperty) {
throw RangeError(`EdmGuid type shouldn't be a system property.`);
}
if (annotationLevel === EntityProperty_1.AnnotationLevel.MINIMAL ||
annotationLevel === EntityProperty_1.AnnotationLevel.FULL) {
return [`${name}${constants_1.ODATA_TYPE}`, "Edm.Guid"];
}
}
/**
* Will return "<propname>@odata.type":"Edm.guid"
*
* @param {string} name
* @param {AnnotationLevel} annotationLevel
* @param {boolean} isSystemProperty
* @return {*} {(string | undefined)}
* @memberof EdmGuid
*/
toJsonPropertyTypeString(name, annotationLevel, isSystemProperty) {
const res = this.toJsonPropertyTypePair(name, annotationLevel, isSystemProperty);
if (!res) {
return;
}
const [key, value] = res;
return `"${key}":"${value}"`;
}
}
exports.EdmGuid = EdmGuid;
//# sourceMappingURL=EdmGuid.js.map
;