Skip to content

Commit

Permalink
chore: cleaned up tests and VaultsSecretsRemove
Browse files Browse the repository at this point in the history
fix: expand header message out of the function

fix: input can be a AsyncIterableIterator
  • Loading branch information
aryanjassal committed Dec 6, 2024
1 parent eb813db commit 6e322ce
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 32 deletions.
4 changes: 3 additions & 1 deletion src/client/handlers/VaultsSecretsCat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class VaultsSecretsCat extends DuplexHandler<
ClientRPCResponseResult<ContentOrErrorMessage>
> {
public handle = async function* (
input: AsyncIterable<ClientRPCRequestParams<SecretIdentifierMessage>>,
input: AsyncIterableIterator<
ClientRPCRequestParams<SecretIdentifierMessage>
>,
): AsyncGenerator<ClientRPCResponseResult<ContentOrErrorMessage>> {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand Down
4 changes: 2 additions & 2 deletions src/client/handlers/VaultsSecretsEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DuplexHandler } from '@matrixai/rpc';
import * as vaultsUtils from '../../vaults/utils';
import * as vaultsErrors from '../../vaults/errors';

class VaultsSecretsList extends DuplexHandler<
class VaultsSecretsEnv extends DuplexHandler<
{
db: DB;
vaultManager: VaultManager;
Expand Down Expand Up @@ -86,4 +86,4 @@ class VaultsSecretsList extends DuplexHandler<
};
}

export default VaultsSecretsList;
export default VaultsSecretsEnv;
2 changes: 1 addition & 1 deletion src/client/handlers/VaultsSecretsMkdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class VaultsSecretsMkdir extends DuplexHandler<
ClientRPCResponseResult<SuccessOrErrorMessage>
> {
public handle = async function* (
input: AsyncIterable<ClientRPCRequestParams<SecretDirMessage>>,
input: AsyncIterableIterator<ClientRPCRequestParams<SecretDirMessage>>,
): AsyncGenerator<ClientRPCResponseResult<SuccessOrErrorMessage>> {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand Down
34 changes: 20 additions & 14 deletions src/client/handlers/VaultsSecretsRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,27 @@ class VaultsSecretsRemove extends DuplexHandler<
ClientRPCResponseResult<SuccessOrErrorMessage>
> {
public handle = async function* (
input: AsyncIterable<
input: AsyncIterableIterator<
ClientRPCRequestParams<
SecretsRemoveHeaderMessage | SecretIdentifierMessageTagged
>
>,
): AsyncGenerator<ClientRPCResponseResult<SuccessOrErrorMessage>> {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
const vaultAcquires: Array<ResourceAcquire<FileSystemWritable>> = [];
// Extracts the header message from the iterator
const headerMessage = await (async () => {
const iterator = input[Symbol.asyncIterator]();
const header = (await iterator.next()).value;
if (header.type === 'VaultNamesHeaderMessage') {
if (header == null) throw new clientErrors.ErrorClientInvalidHeader();
return header;
}
})();
// Extract the header message from the iterator
const headerMessage:
| SecretsRemoveHeaderMessage
| SecretIdentifierMessageTagged = (await input.next()).value;
if (
headerMessage == null ||
headerMessage.type !== 'VaultNamesHeaderMessage'
) {
throw new clientErrors.ErrorClientInvalidHeader();
}
// Create an array of write acquires
await db.withTransactionF(async (tran) => {
const vaultAcquires = await db.withTransactionF(async (tran) => {
const vaultAcquires: Array<ResourceAcquire<FileSystemWritable>> = [];
for (const vaultName of headerMessage.vaultNames) {
const vaultIdFromName = await vaultManager.getVaultId(vaultName, tran);
const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(vaultName);
Expand All @@ -60,6 +61,7 @@ class VaultsSecretsRemove extends DuplexHandler<
);
vaultAcquires.push(acquire);
}
return vaultAcquires;
});
// Acquire all locks in parallel and perform all operations at once
yield* withG(
Expand All @@ -72,7 +74,11 @@ class VaultsSecretsRemove extends DuplexHandler<
}
for await (const message of input) {
// Ignoring any header messages
if (message.type !== 'SecretIdentifierMessage') continue;
if (message.type === 'VaultNamesHeaderMessage') {
throw new clientErrors.ErrorClientInvalidHeader(
'The header message cannot be sent multiple times',
);
}
const efs = vaultMap.get(message.nameOrId);
if (efs == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined(
Expand All @@ -98,7 +104,7 @@ class VaultsSecretsRemove extends DuplexHandler<
e.code === 'ENOTEMPTY' ||
e.code === 'EINVAL'
) {
// INVAL can be triggered if removing the root of the
// EINVAL can be triggered if removing the root of the
// vault is attempted.
yield {
type: 'error',
Expand Down
2 changes: 1 addition & 1 deletion src/git/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ async function listObjects({
}
return;
default:
utils.never();
utils.never('Invalid type');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function getDefaultNodePath(): string | undefined {
return p;
}

function never(message?: string): never {
function never(message: string): never {
throw new utilsErrors.ErrorUtilsUndefinedBehaviour(message);
}

Expand Down
2 changes: 1 addition & 1 deletion src/vaults/VaultInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ class VaultInternal {
}
// The returned transaction can be undefined, too. We won't handle those
// cases.
if (tran == null) utils.never();
if (tran == null) utils.never('Acquired transactions cannot be null');
await tran.lock(
[...this.vaultMetadataDbPath, VaultInternal.dirtyKey].join(''),
);
Expand Down
35 changes: 24 additions & 11 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,7 @@ describe('vaultsSecretsMkdir', () => {
const vaultName = 'test-vault';
const vaultId = await vaultManager.createVault(vaultName);
const dirPath = 'dir/dir1/dir2';
// Attempt to make directories
const response = await rpcClient.methods.vaultsSecretsMkdir();
const writer = response.writable.getWriter();
await writer.write({
Expand All @@ -1461,7 +1462,7 @@ describe('vaultsSecretsMkdir', () => {
metadata: { options: { recursive: true } },
});
await writer.close();

// Check if the operation concluded as expected
for await (const data of response.readable) {
expect(data.type).toEqual('success');
}
Expand All @@ -1476,13 +1477,15 @@ describe('vaultsSecretsMkdir', () => {
const vaultId = await vaultManager.createVault(vaultName);
const encodeVaultId = vaultsUtils.encodeVaultId(vaultId);
const dirPath = 'dir/dir1/dir2';
// Attempt to make directories
const response = await rpcClient.methods.vaultsSecretsMkdir();
const writer = response.writable.getWriter();
await writer.write({ nameOrId: encodeVaultId, dirName: dirPath });
await writer.close();
// Check if the operation concluded as expected
for await (const data of response.readable) {
expect(data.type).toEqual('error');
if (data.type !== 'error') utils.never();
if (data.type !== 'error') utils.never("Type is asserted to be 'error'");
expect(data.code).toEqual('ENOENT');
expect(data.reason).toEqual(dirPath);
}
Expand Down Expand Up @@ -1543,17 +1546,21 @@ describe('vaultsSecretsMkdir', () => {
// Attempt to make directories
const response = await rpcClient.methods.vaultsSecretsMkdir();
const writer = response.writable.getWriter();
await writer.write({ nameOrId: vaultIdEncoded1, dirName: dirPath1 });
await writer.write({ nameOrId: vaultIdEncoded2, dirName: dirPath2 });
await writer.write({ nameOrId: vaultIdEncoded1, dirName: dirPath3 });
await writer.write({ nameOrId: vaultIdEncoded2, dirName: dirPath2 });
await writer.write({ nameOrId: vaultIdEncoded1, dirName: dirPath1 });
await writer.close();
// Check if the operation concluded as expected
let successCount = 0;
for await (const data of response.readable) {
if (data.type === 'error') {
expect(data.code).toEqual('ENOENT');
expect(data.reason).toEqual(dirPath3);
} else {
successCount++;
}
}
expect(successCount).toEqual(2);
await vaultManager.withVaults(
[vaultId1, vaultId2],
async (vault1, vault2) => {
Expand Down Expand Up @@ -1585,7 +1592,7 @@ describe('vaultsSecretsMkdir', () => {
// Check if the operation concluded as expected
for await (const data of response.readable) {
expect(data.type).toEqual('error');
if (data.type !== 'error') utils.never();
if (data.type !== 'error') utils.never("Type is asserted to be 'error'");
expect(data.code).toEqual('EEXIST');
expect(data.reason).toEqual(dirPath);
}
Expand Down Expand Up @@ -1728,7 +1735,9 @@ describe('vaultsSecretsCat', () => {
// Read response
for await (const data of response.readable) {
expect(data.type).toEqual('success');
if (data.type !== 'success') utils.never();
if (data.type !== 'success') {
utils.never("Type is asserted to be 'success'");
}
expect(data.secretContent).toEqual(secretContent);
}
});
Expand All @@ -1747,7 +1756,7 @@ describe('vaultsSecretsCat', () => {
// Read response
for await (const data of response.readable) {
expect(data.type).toEqual('error');
if (data.type !== 'error') utils.never();
if (data.type !== 'error') utils.never("Type is asserted to be 'error'");
expect(data.code).toEqual('ENOENT');
expect(data.reason).toEqual(secretName);
}
Expand All @@ -1773,7 +1782,7 @@ describe('vaultsSecretsCat', () => {
// Read response
for await (const data of response.readable) {
expect(data.type).toEqual('error');
if (data.type !== 'error') utils.never();
if (data.type !== 'error') utils.never("Type is asserted to be 'error'");
expect(data.code).toEqual('EISDIR');
expect(data.reason).toEqual(secretName);
}
Expand Down Expand Up @@ -1803,7 +1812,9 @@ describe('vaultsSecretsCat', () => {
let totalContent = '';
for await (const data of response.readable) {
expect(data.type).toEqual('success');
if (data.type !== 'success') utils.never();
if (data.type !== 'success') {
utils.never("Type is asserted to be 'success'");
}
totalContent += data.secretContent;
}
expect(totalContent).toEqual(`${secretContent1}${secretContent2}`);
Expand Down Expand Up @@ -1845,7 +1856,9 @@ describe('vaultsSecretsCat', () => {
let totalContent = '';
for await (const data of response.readable) {
expect(data.type).toEqual('success');
if (data.type !== 'success') utils.never();
if (data.type !== 'success') {
utils.never("Type is asserted to be 'success'");
}
totalContent += data.secretContent;
}
expect(totalContent).toEqual(
Expand Down Expand Up @@ -2397,7 +2410,7 @@ describe('vaultsSecretsRemove', () => {
for await (const data of response.readable) {
loopRun = true;
expect(data.type).toStrictEqual('error');
if (data.type !== 'error') utils.never();
if (data.type !== 'error') utils.never("Type is asserted to be 'error'");
expect(data.code).toStrictEqual('EINVAL');
}
// Check
Expand Down

0 comments on commit 6e322ce

Please sign in to comment.