Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTEGRATION [PR#1894 > development/8.1] ARSN-201 type check versioning #1895

Merged
merged 13 commits into from
May 19, 2022
Merged
10 changes: 1 addition & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export * as constants from './lib/constants';
export * as https from './lib/https';
export * as metrics from './lib/metrics';
export * as network from './lib/network';
export * as versioning from './lib/versioning';
export * as stream from './lib/stream';
export * as errorUtils from './lib/errorUtils';
export { default as shuffle } from './lib/shuffle';
Expand Down Expand Up @@ -67,15 +68,6 @@ export const testing = {
matrix: require('./lib/testing/matrix.js'),
};

export const versioning = {
VersioningConstants: require('./lib/versioning/constants.js').VersioningConstants,
Version: require('./lib/versioning/Version.js').Version,
VersionID: require('./lib/versioning/VersionID.js'),
WriteGatheringManager: require('./lib/versioning/WriteGatheringManager.js'),
WriteCache: require('./lib/versioning/WriteCache.js'),
VersioningRequestProcessor: require('./lib/versioning/VersioningRequestProcessor.js'),
};

export const s3routes = {
routes: require('./lib/s3routes/routes'),
routesUtils: require('./lib/s3routes/routesUtils'),
Expand Down
111 changes: 62 additions & 49 deletions lib/versioning/Version.js → lib/versioning/Version.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'; // eslint-disable-line strict

const VID_SEP = require('./constants').VersioningConstants.VersionId.Separator;
import { VersioningConstants } from './constants';

const VID_SEP = VersioningConstants.VersionId.Separator;
/**
* Class for manipulating an object version.
* The format of a version: { isNull, isDeleteMarker, versionId, otherInfo }
Expand All @@ -11,36 +10,48 @@ const VID_SEP = require('./constants').VersioningConstants.VersionId.Separator;
* with the full stringify/parse cycle, which is too low a number for
* use with a production setup with Metadata).
*/
class Version {
export class Version {
version: {
isNull?: boolean;
isDeleteMarker?: boolean;
versionId?: string;
isPHD?: boolean;
};

/**
* Create a new version instantiation from its data object.
* @param {object} version - the data object to instantiate
* @param {boolean} version.isNull - is a null version
* @param {boolean} version.isDeleteMarker - is a delete marker
* @param {string} version.versionId - the version id
* @param version - the data object to instantiate
* @param version.isNull - is a null version
* @param version.isDeleteMarker - is a delete marker
* @param version.versionId - the version id
* @constructor
*/
constructor(version) {
constructor(version?: {
isNull?: boolean;
isDeleteMarker?: boolean;
versionId?: string;
isPHD?: boolean;
}) {
this.version = version || {};
}

/**
* Parse the version information from a string.
*
* @param {string} value - the string to parse
* @return {Version} - the version deserialized from the input string
* @param value - the string to parse
* @return - the version deserialized from the input string
*/
static from(value) {
static from(value: string) {
return new Version(value ? JSON.parse(value) : undefined);
}

/**
* [MetaData Internal] Check if a version is a place holder for deletion.
*
* @param {string} value - version to check
* @return {boolean} - whether this is a PHD version
* @param value - version to check
* @return - whether this is a PHD version
*/
static isPHD(value) {
static isPHD(value: string) {
// check if the input is a valid version
if (!value) {
return false;
Expand All @@ -53,7 +64,8 @@ class Version {
// parse the value if it has the keyword 'isPHD'
try {
return Version.from(value).isPHDVersion();
} catch (exception) { // eslint-disable-line strict
} catch (exception) {
// eslint-disable-line strict
return false; // nice, Vault
}
}
Expand All @@ -64,100 +76,104 @@ class Version {
* to indicate that the master version of an object has been deleted and it
* needs to be updated by the latest version if any.
*
* @param {string} versionId - versionId of the PHD version
* @return {string} - the serialized version
* @param versionId - versionId of the PHD version
* @return - the serialized version
*/
static generatePHDVersion(versionId) {
static generatePHDVersion(versionId: string) {
return `{ "isPHD": true, "versionId": "${versionId}" }`;
}

/**
* Put versionId into an object in the (cheap) way of string manipulation,
* instead of the more expensive alternative parsing and stringification.
*
* @param {string} value - stringification of the object to append versionId
* @param {string} versionId - the versionId to append
* @return {string} - the object with versionId appended
* @param value - stringification of the object to append versionId
* @param versionId - the versionId to append
* @return - the object with versionId appended
*/
static appendVersionId(value, versionId) {
static appendVersionId(value: string, versionId: string): string {
// assuming value has the format of '{...}'
let index = value.length - 2;
while (value.charAt(index--) === ' ');
const comma = value.charAt(index + 1) !== '{';
return `${value.slice(0, value.length - 1)}` + // eslint-disable-line
(comma ? ',' : '') + `"versionId":"${versionId}"}`;
return (
`${value.slice(0, value.length - 1)}` + // eslint-disable-line
(comma ? ',' : '') +
`"versionId":"${versionId}"}`
);
}

/**
* [MetaData Internal] Check if a version is a place holder for deletion.
*
* @return {boolean} - whether this is a PHD version
*/
isPHDVersion() {
isPHDVersion(): boolean {
return this.version.isPHD || false;
}

/**
* Check if a version is a null version.
*
* @return {boolean} - stating if the value is a null version
* @return - stating if the value is a null version
*/
isNullVersion() {
return this.version.isNull;
isNullVersion(): boolean {
return this.version.isNull ?? false;
}

/**
* Check if a stringified object is a delete marker.
*
* @param {string} value - the stringified object to check
* @return {boolean} - if the object is a delete marker
* @param value - the stringified object to check
* @return - if the object is a delete marker
*/
static isDeleteMarker(value) {
static isDeleteMarker(value: string): boolean {
const index = value.indexOf('isDeleteMarker');
if (index < 0) {
return false;
}
// parse the value
try {
return Version.from(value).isDeleteMarkerVersion();
} catch (exception) { // eslint-disable-line strict
} catch (exception) {
// eslint-disable-line strict
return false;
}
}

/**
* Check if a version is a delete marker.
*
* @return {boolean} - stating if the value is a delete marker
* @return - stating if the value is a delete marker
*/
isDeleteMarkerVersion() {
return this.version.isDeleteMarker;
isDeleteMarkerVersion(): boolean {
return this.version.isDeleteMarker ?? false;
}

/**
* Get the versionId of the version.
*
* @return {string} - the versionId
* @return - the versionId
*/
getVersionId() {
getVersionId(): string | undefined {
return this.version.versionId;
}

/**
* Set the versionId of the version.
*
* @param {string} versionId - the versionId
* @return {Version} - the updated version
* @param versionId - the versionId
* @return - the updated version
*/
setVersionId(versionId) {
setVersionId(versionId: string) {
this.version.versionId = versionId;
return this;
}

/**
* Mark a version as a delete marker.
*
* @return {Version} - the updated version
* @return - the updated version
*/
setDeleteMarker() {
this.version.isDeleteMarker = true;
Expand All @@ -167,7 +183,7 @@ class Version {
/**
* Mark a version as a null version.
*
* @return {Version} - the updated version
* @return - the updated version
*/
setNullVersion() {
this.version.isNull = true;
Expand All @@ -177,16 +193,13 @@ class Version {
/**
* Serialize the version.
*
* @return {string} - the serialized version
* @return - the serialized version
*/
toString() {
toString(): string {
return JSON.stringify(this.version);
}
}

function isMasterKey(key) {
export function isMasterKey(key: string) {
return !key.includes(VID_SEP);
}


module.exports = { Version, isMasterKey };
Loading