Skip to content

Commit

Permalink
refactor: Rework PackageVersionRecordsController to store package fil…
Browse files Browse the repository at this point in the history
…es in file records
  • Loading branch information
KallynGowdy committed Nov 21, 2024
1 parent fe36d75 commit fe047fa
Show file tree
Hide file tree
Showing 11 changed files with 1,229 additions and 498 deletions.
1 change: 1 addition & 0 deletions src/aux-common/rpc/ErrorCodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe('getStatusCode()', () => {
['hume_api_error', 500] as const,
['invalid_webhook_target', 501] as const,
['took_too_long', 504] as const,
['parent_not_found', 400] as const,
];

it.each(cases)('should map error code %s to %s', (code, expectedStatus) => {
Expand Down
3 changes: 2 additions & 1 deletion src/aux-common/rpc/ErrorCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export type KnownErrorCodes =
| 'session_is_not_revokable'
| 'hume_api_error'
| 'invalid_webhook_target'
| 'took_too_long';
| 'took_too_long'
| 'parent_not_found';

/**
* Gets the status code that should be used for the given response.
Expand Down
6 changes: 6 additions & 0 deletions src/aux-records/FileRecordsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export class FileRecordsController {
'The file has already been uploaded to ' +
fileResult.url,
existingFileUrl: fileResult.url,
existingFileName: fileResult.fileName,
};
}
}
Expand Down Expand Up @@ -913,6 +914,11 @@ export interface RecordFileFailure {
* The URL that the file is available at if it has already been uploaded.
*/
existingFileUrl?: string;

/**
* The name of the file that was attempted to be recorded.
*/
existingFileName?: string;
}

/**
Expand Down
19 changes: 9 additions & 10 deletions src/aux-records/crud/sub/MemorySubCrudRecordsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class MemorySubCrudRecordsStore<
if (!existing.item) {
return {
success: false,
errorCode: 'item_not_found',
errorCode: 'data_not_found',
errorMessage: 'Item not found',
};
}
Expand Down Expand Up @@ -144,15 +144,10 @@ export class MemorySubCrudRecordsStore<
item.key
);
if (!existing.item) {
await this.createItem(recordName, item as T);
return;
return await this.createItem(recordName, item as T);
}

await this.updateItem(recordName, item);

return {
success: true,
};
return await this.updateItem(recordName, item);
}

async deleteItem(
Expand All @@ -162,12 +157,16 @@ export class MemorySubCrudRecordsStore<
): Promise<CrudResult> {
const bucket = this._itemBuckets.get(recordName);
if (!bucket) {
return;
return {
success: true,
};
}

const arr = bucket.get(address);
if (!arr) {
return;
return {
success: true,
};
}

const index = arr.findIndex((i) => isEqual(this.getKey(i), key));
Expand Down
63 changes: 56 additions & 7 deletions src/aux-records/crud/sub/SubCrudRecordsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import {
import { ZodIssue } from 'zod';
import { traced } from '../../tracing/TracingDecorators';
import { SpanStatusCode, trace } from '@opentelemetry/api';
import { SubCrudRecord, SubCrudRecordsStore } from './SubCrudRecordsStore';
import {
CrudResult,
SubCrudRecord,
SubCrudRecordsStore,
} from './SubCrudRecordsStore';
import { CrudRecord, CrudRecordsStore } from '../CrudRecordsStore';
import {
CheckSubscriptionMetricsResult,
Expand Down Expand Up @@ -242,12 +246,15 @@ export abstract class SubCrudRecordsController<
return subscriptionResult;
}

await this._store.putItem(recordName, item);
return {
success: true,
const result = await this._putItem(
action,
recordName,
address: item.address,
};
item,
contextResult.context,
authorization,
request
);
return result;
} catch (err) {
const span = trace.getActiveSpan();
span?.recordException(err);
Expand All @@ -262,6 +269,36 @@ export abstract class SubCrudRecordsController<
}
}

/**
* Updates or creates the item in the given record.
* @param action The action that is being performed.
* @param recordName The name of the record.
* @param item The item that should be updated or created.
* @param context The authorization context.
* @param authorization The authorization for the user and instances.
* @param request The request.
*/
protected async _putItem(
action: 'update' | 'create',
recordName: string,
item: T,
context: AuthorizationContext,
authorization: AuthorizeUserAndInstancesForResourcesSuccess,
request: CrudRecordItemRequest<T>
): Promise<CrudRecordItemResult> {
const crudResult = await this._store.putItem(recordName, item);

if (crudResult.success === false) {
return crudResult;
}

return {
success: true,
recordName,
address: item.address,
};
}

/**
* Gets the item with the given address from the given record.
* @param request The request to get the item.
Expand Down Expand Up @@ -317,9 +354,21 @@ export abstract class SubCrudRecordsController<
return authorization;
}

const item = result.item;
const metricsResult = await this._checkSubscriptionMetrics(
'read',
context.context,
authorization,
item
);

if (metricsResult.success === false) {
return metricsResult;
}

return {
success: true,
item: this._convertItemToResult(result.item, context.context),
item: this._convertItemToResult(item, context.context),
};
} catch (err) {
const span = trace.getActiveSpan();
Expand Down
Loading

0 comments on commit fe047fa

Please sign in to comment.