Skip to content

Commit

Permalink
Fix/status list (#237)
Browse files Browse the repository at this point in the history
Signed-off-by: Mirko Mollik <mirko.mollik@fit.fraunhofer.de>
  • Loading branch information
cre8 authored May 21, 2024
1 parent 92f2235 commit 0329a6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
20 changes: 12 additions & 8 deletions packages/jwt-status-list/src/status-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { BitsPerStatus } from './types';
* StatusListManager is a class that manages a list of statuses with variable bit size.
*/
export class StatusList {
private statusList: number[];
private _statusList: number[];
private bitsPerStatus: BitsPerStatus;
private totalStatuses: number;

Expand All @@ -26,11 +26,18 @@ export class StatusList {
);
}
}
this.statusList = statusList;
this._statusList = statusList;
this.bitsPerStatus = bitsPerStatus;
this.totalStatuses = statusList.length;
}

/**
* Get the status list.
*/
get statusList(): number[] {
return this._statusList;
}

/**
* Get the number of statuses.
* @returns
Expand All @@ -42,13 +49,12 @@ export class StatusList {
/**
* Get the status at a specific index.
* @param index
* @returns
*/
getStatus(index: number): number {
if (index < 0 || index >= this.totalStatuses) {
throw new Error('Index out of bounds');
}
return this.statusList[index];
return this._statusList[index];
}

/**
Expand All @@ -60,12 +66,11 @@ export class StatusList {
if (index < 0 || index >= this.totalStatuses) {
throw new Error('Index out of bounds');
}
this.statusList[index] = value;
this._statusList[index] = value;
}

/**
* Compress the status list.
* @returns
*/
compressStatusList(): string {
const byteArray = this.encodeStatusList();
Expand All @@ -77,7 +82,6 @@ export class StatusList {
* Decompress the compressed status list and return a new StatusList instance.
* @param compressed
* @param bitsPerStatus
* @returns
*/
static decompressStatusList(
compressed: string,
Expand Down Expand Up @@ -113,7 +117,7 @@ export class StatusList {
let bitIndex = 0;
let currentByte = '';
for (let i = 0; i < this.totalStatuses; i++) {
const status = this.statusList[i];
const status = this._statusList[i];
// Place bits from status into currentByte, starting from the most significant bit.
currentByte = status.toString(2).padStart(numBits, '0') + currentByte;
bitIndex += numBits;
Expand Down
9 changes: 7 additions & 2 deletions packages/jwt-status-list/src/test/status-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ describe('StatusList', () => {
const manager = new StatusList(status, 1);
const encoded = manager.compressStatusList();
expect(encoded).toBe('eNrbuRgAAhcBXQ');
const l = StatusList.decompressStatusList(encoded, 1);
const list = StatusList.decompressStatusList(encoded, 1);
for (let i = 0; i < status.length; i++) {
expect(l.getStatus(i)).toBe(status[i]);
expect(list.getStatus(i)).toBe(status[i]);
}

//get the whole list and check if it is equal
for (let i = 0; i < list.statusList.length; i++) {
expect(list.statusList[i]).toBe(status[i]);
}
});

Expand Down

0 comments on commit 0329a6b

Please sign in to comment.