Skip to content

Commit

Permalink
feat: stopped using globWalk for reading and listing files
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Aug 20, 2024
1 parent 1fc3ba0 commit a5fe659
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
35 changes: 19 additions & 16 deletions src/client/handlers/VaultsSecretsGetFileTree.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { DB } from '@matrixai/db';
import type VaultManager from '../../vaults/VaultManager';
import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types';
import type { TreeNode } from '../../vaults/types';
import type { SecretFilesMessage } from '../types';
import type { SecretFilesMessage, VaultFileNode } from '../types';
import path from 'path';
import { ServerHandler } from '@matrixai/rpc';
import { fileTree } from '../../vaults';
import { generateStats } from '../../vaults/fileTree';
import * as vaultsUtils from '../../vaults/utils';
import * as vaultsErrors from '../../vaults/errors';

Expand All @@ -14,12 +14,12 @@ class VaultsSecretsGetFileTree extends ServerHandler<
db: DB;
},
ClientRPCRequestParams<SecretFilesMessage>,
ClientRPCResponseResult<TreeNode>
ClientRPCResponseResult<VaultFileNode>
> {
public async *handle(
input: ClientRPCRequestParams<SecretFilesMessage>,
_cancel: any,
): AsyncGenerator<ClientRPCResponseResult<TreeNode>, void, void> {
): AsyncGenerator<ClientRPCResponseResult<VaultFileNode>, void, void> {
const { vaultManager, db } = this.container;
const vaultId = await db.withTransactionF(async (tran) => {
const vaultIdFromName = await vaultManager.getVaultId(
Expand All @@ -34,20 +34,23 @@ class VaultsSecretsGetFileTree extends ServerHandler<

yield* vaultManager.withVaultsG([vaultId], (vault) => {
return vault.readG(async function* (fs): AsyncGenerator<
TreeNode,
VaultFileNode,
void,
void
> {
yield* fileTree.globWalk({
fs: fs,
basePath: '.',
pattern: input.pattern,
yieldStats: input.yieldStats,
yieldRoot: input.yieldRoot,
yieldFiles: input.yieldFiles,
yieldParents: input.yieldParents,
yieldDirectories: input.yieldDirectories,
});
// @ts-ignore: While the types don't fully match, it matches enough for our usage.
let files: Array<string> = await fs.promises.readdir(input.pattern);
files = files.map((file) => path.join(input.pattern, file));

for await (const file of files) {
const stat = await fs.promises.stat(file);
const type = stat.isFile() ? 'FILE' : 'DIRECTORY';
yield {
path: file,
type: type,
stat: input.yieldStats ? generateStats(stat) : undefined,
};
}
});
});
}
Expand Down
21 changes: 13 additions & 8 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import type {
VaultIdEncoded,
} from '../ids';
import type { GestaltAction } from '../gestalts/types';
import type { CommitId, VaultAction, VaultName } from '../vaults/types';
import type {
CommitId,
StatEncoded,
VaultAction,
VaultName,
} from '../vaults/types';
import type { CertificatePEM, JWKEncrypted, PublicKeyJWK } from '../keys/types';
import type { Notification } from '../notifications/types';
import type { ProviderToken } from '../identities/types';
Expand Down Expand Up @@ -300,18 +305,11 @@ type VaultsLatestVersionMessage = {
};

// Secrets

type SecretFilesMessage = VaultIdentifierMessage & {
pattern: string;
yieldStats: boolean;
yieldRoot: boolean;
yieldFiles: boolean;
yieldParents: boolean;
yieldDirectories: boolean;
};

// NOTE: we used to use SecretNameMessage before. do we need to keep it? look into this.

type SecretNameMessage = {
secretName: string;
};
Expand All @@ -338,6 +336,12 @@ type SecretRenameMessage = SecretIdentifierMessage & {
newSecretName: string;
};

type VaultFileNode = {
path: string;
type: 'FILE' | 'DIRECTORY';
stat?: StatEncoded;
};

// Stat is the 'JSON.stringify version of the file stat
type SecretStatMessage = {
stat: {
Expand Down Expand Up @@ -429,6 +433,7 @@ export type {
SecretMkdirMessage,
SecretDirMessage,
SecretRenameMessage,
VaultFileNode,
SecretStatMessage,
SignatureMessage,
OverrideRPClientType,
Expand Down
13 changes: 4 additions & 9 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1591,23 +1591,18 @@ describe('vaultsSecretsNewDir and vaultsSecretsList', () => {
// List secrets with names of directories
const secrets = await rpcClient.methods.vaultsSecretsGetFileTree({
nameOrId: vaultsIdEncoded,
pattern: '**/*',
pattern: 'secretDir',
yieldStats: false,
yieldRoot: false,
yieldFiles: true,
yieldParents: false,
yieldDirectories: true,
});

// Extract secret file paths
const parsedFiles: Array<string> = [];
for await (const file of secrets) {
parsedFiles.push(file.path);
}
expect(parsedFiles).toIncludeAllMembers([
...secretList.map((secret) => path.join('secretDir', secret)),
'secretDir',
]);
expect(parsedFiles).toIncludeAllMembers(
secretList.map((secret) => path.join('secretDir', secret)),
);
});
});
describe('vaultsSecretsRename', () => {
Expand Down

0 comments on commit a5fe659

Please sign in to comment.