Skip to content

Commit ed2c6ac

Browse files
feat: added comments
1 parent b601546 commit ed2c6ac

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

packages/idempotency/src/EnvironmentVariablesService.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
class EnvironmentVariablesService {
22
private lambdaFunctionNameVariable = 'AWS_LAMBDA_FUNCTION_NAME';
33

4+
/**
5+
* retrieve the value of an environment variable
6+
*
7+
* @param name the name of the environment variable
8+
* @returns the value of the environment variable
9+
*/
410
public get(name: string): string {
511
return process.env[name]?.trim() || '';
612
}
7-
13+
/**
14+
* retrieve the name of the Lambda function
15+
*
16+
* @returns the Lambda function name
17+
*/
818
public getLambdaFunctionName(): string{
919
return this.get(this.lambdaFunctionNameVariable);
1020
}

packages/idempotency/src/persistence/PersistenceLayer.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ abstract class PersistenceLayer implements PersistenceLayerInterface {
2929
this.functionName = this.getEnvVarsService().getLambdaFunctionName() + '.' + functionName;
3030
}
3131

32+
/**
33+
* Deletes a record from the persistence store for the persistence key generated from the data passed in.
34+
*
35+
* @param data - the data payload that will be hashed to create the hash portion of the idempotency key
36+
*/
3237
public async deleteRecord(data: unknown): Promise<void> {
3338
const idempotencyRecord: IdempotencyRecord =
3439
new IdempotencyRecord(this.getHashedIdempotencyKey(data),
@@ -41,7 +46,11 @@ abstract class PersistenceLayer implements PersistenceLayerInterface {
4146

4247
this._deleteRecord(idempotencyRecord);
4348
}
44-
49+
/**
50+
* Retrieves idempotency key for the provided data and fetches data for that key from the persistence store
51+
*
52+
* @param data - the data payload that will be hashed to create the hash portion of the idempotency key
53+
*/
4554
public async getRecord(data: unknown): Promise<IdempotencyRecord> {
4655
const idempotencyKey: string = this.getHashedIdempotencyKey(data);
4756

@@ -66,6 +75,13 @@ abstract class PersistenceLayer implements PersistenceLayerInterface {
6675
return this._putRecord(idempotencyRecord);
6776
}
6877

78+
/**
79+
* Saves a record of the function completing successfully. This will create a record with a COMPLETED status
80+
* and will save the result of the completed function in the idempotency record.
81+
*
82+
* @param data - the data payload that will be hashed to create the hash portion of the idempotency key
83+
* @param result - the result of the successfully completed function
84+
*/
6985
public async saveSuccess(data: unknown, result: unknown): Promise<void> {
7086
const idempotencyRecord: IdempotencyRecord =
7187
new IdempotencyRecord(this.getHashedIdempotencyKey(data),
@@ -85,6 +101,12 @@ abstract class PersistenceLayer implements PersistenceLayerInterface {
85101
protected abstract _putRecord(record: IdempotencyRecord): Promise<void>;
86102
protected abstract _updateRecord(record: IdempotencyRecord): Promise<void>;
87103

104+
/**
105+
* Generates a hash of the data and returns the digest of that hash
106+
*
107+
* @param data the data payload that will generate the hash
108+
* @returns the digest of the generated hash
109+
*/
88110
private generateHash(data: string): string{
89111
const hash: Hash = createHash(this.hashFunction);
90112
hash.update(data);
@@ -100,12 +122,23 @@ abstract class PersistenceLayer implements PersistenceLayerInterface {
100122
return this.envVarsService;
101123
}
102124

125+
/**
126+
* Creates the expiry timestamp for the idempotency record
127+
*
128+
* @returns the expiry time for the record expressed as number of seconds past the UNIX epoch
129+
*/
103130
private getExpiryTimestamp(): number {
104131
const currentTime: number = Date.now() / 1000;
105132

106133
return currentTime + this.expiresAfterSeconds;
107134
}
108135

136+
/**
137+
* Generates the idempotency key used to identify records in the persistence store.
138+
*
139+
* @param data the data payload that will be hashed to create the hash portion of the idempotency key
140+
* @returns the idempotency key
141+
*/
109142
private getHashedIdempotencyKey(data: unknown): string {
110143
if (!data){
111144
console.warn('No data found for idempotency key');

0 commit comments

Comments
 (0)