diff --git a/src/client/handlers/VaultsSecretsList.ts b/src/client/handlers/VaultsSecretsList.ts index 501d2cbc7..2243d04ee 100644 --- a/src/client/handlers/VaultsSecretsList.ts +++ b/src/client/handlers/VaultsSecretsList.ts @@ -1,27 +1,29 @@ import type { DB } from '@matrixai/db'; +import type { + ClientRPCRequestParams, + ClientRPCResponseResult, + SecretFilesMessage, + SecretFiles, +} from '../types'; import type VaultManager from '../../vaults/VaultManager'; -import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types'; -import type { SecretFilesMessage, SecretFile } from '../types'; -import type { StatEncoded } from '../../vaults/types'; import path from 'path'; import { ServerHandler } from '@matrixai/rpc'; -import { generateStats } from '../../vaults/fileTree'; import * as vaultsUtils from '../../vaults/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as clientErrors from '../errors'; -class VaultsSecretsGetFileTree extends ServerHandler< +class VaultsSecretsList extends ServerHandler< { vaultManager: VaultManager; db: DB; }, ClientRPCRequestParams, - ClientRPCResponseResult + ClientRPCResponseResult > { public async *handle( input: ClientRPCRequestParams, _cancel: any, - ): AsyncGenerator, void, void> { + ): AsyncGenerator, void, void> { const { vaultManager, db } = this.container; const vaultId = await db.withTransactionF(async (tran) => { const vaultIdFromName = await vaultManager.getVaultId( @@ -36,51 +38,30 @@ class VaultsSecretsGetFileTree extends ServerHandler< yield* vaultManager.withVaultsG([vaultId], (vault) => { return vault.readG(async function* (fs): AsyncGenerator< - SecretFile, + SecretFiles, void, void > { + let files: Array; try { // @ts-ignore: While the types don't fully match, it matches enough for our usage. - let files: Array = await fs.promises.readdir(input.path); - files = files.map((file) => path.join(input.path, file)); - - for await (const file of files) { - try { - const stat = await fs.promises.lstat(file); - const type = stat.isFile() ? 'FILE' : 'DIRECTORY'; - let stats: StatEncoded | undefined; - - if (input.yieldStats) { - stats = generateStats(stat); - if (stats.isSymbolicLink) { - // @ts-ignore: Again, the types don't fully match, but it works. - stats.symbolicLinkTarget = await fs.promises.readlink(file); - } - } else { - stats = undefined; - } - yield { - path: file, - type: type, - stat: stats, - }; - } catch (e) { - throw new clientErrors.ErrorClientFSReadFailed( - `Failed to read file: ${file}`, - { cause: e }, - ); - } - } + files = await fs.promises.readdir(input.path); } catch (e) { throw new clientErrors.ErrorClientFSReadFailed( - `Failed to read directory: ${input.path}`, + `No matches exist for path: ${input.path}`, { cause: e }, ); } + files = files.map((file) => path.join(input.path, 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 }; + } }); }); } } -export default VaultsSecretsGetFileTree; +export default VaultsSecretsList; diff --git a/src/client/types.ts b/src/client/types.ts index 08c1467fe..9a7faf3b4 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -13,12 +13,7 @@ import type { VaultIdEncoded, } from '../ids'; import type { GestaltAction } from '../gestalts/types'; -import type { - CommitId, - StatEncoded, - VaultAction, - VaultName, -} from '../vaults/types'; +import type { CommitId, 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'; @@ -307,7 +302,6 @@ type VaultsLatestVersionMessage = { // Secrets type SecretFilesMessage = VaultIdentifierMessage & { path: string; - yieldStats: boolean; }; type SecretNameMessage = { @@ -336,10 +330,9 @@ type SecretRenameMessage = SecretIdentifierMessage & { newSecretName: string; }; -type SecretFile = { +type SecretFiles = { path: string; type: 'FILE' | 'DIRECTORY'; - stat?: StatEncoded; }; // Stat is the 'JSON.stringify version of the file stat @@ -433,7 +426,7 @@ export type { SecretMkdirMessage, SecretDirMessage, SecretRenameMessage, - SecretFile, + SecretFiles, SecretStatMessage, SignatureMessage, OverrideRPClientType, diff --git a/src/vaults/fileTree.ts b/src/vaults/fileTree.ts index f9a5de850..b008aaec3 100644 --- a/src/vaults/fileTree.ts +++ b/src/vaults/fileTree.ts @@ -370,11 +370,7 @@ function serializerStreamFactory( } return new ReadableStream({ start: (controller) => { - controller.enqueue( - generateGenericHeader({ - type: HeaderType.TREE, - }), - ); + controller.enqueue(generateGenericHeader({ type: HeaderType.TREE })); }, pull: async (controller) => { try { diff --git a/tests/client/handlers/vaults.test.ts b/tests/client/handlers/vaults.test.ts index f88dc9f2c..df452131c 100644 --- a/tests/client/handlers/vaults.test.ts +++ b/tests/client/handlers/vaults.test.ts @@ -1593,7 +1593,6 @@ describe('vaultsSecretsNewDir and vaultsSecretsList', () => { const files = await rpcClient.methods.vaultsSecretsList({ nameOrId: vaultsIdEncoded, path: 'doesntExist', - yieldStats: false, }); try { for await (const _ of files); // Consume values @@ -1605,7 +1604,6 @@ describe('vaultsSecretsNewDir and vaultsSecretsList', () => { const secrets = await rpcClient.methods.vaultsSecretsList({ nameOrId: vaultsIdEncoded, path: 'secretDir', - yieldStats: false, }); // Extract secret file paths diff --git a/tests/vaults/VaultOps.test.ts b/tests/vaults/VaultOps.test.ts index 98e9e446b..0d04713e8 100644 --- a/tests/vaults/VaultOps.test.ts +++ b/tests/vaults/VaultOps.test.ts @@ -16,7 +16,6 @@ import * as vaultOps from '@/vaults/VaultOps'; import * as vaultsErrors from '@/vaults/errors'; import * as vaultsUtils from '@/vaults/utils'; import * as keysUtils from '@/keys/utils'; -import * as utils from '@/utils'; import * as testNodesUtils from '../nodes/utils'; describe('VaultOps', () => { @@ -590,13 +589,11 @@ describe('VaultOps', () => { }); const data: Array = []; const parserTransform = fileTree.parserTransformStreamFactory(); - const serializedGen = fileTree.serializerStreamFactory( + const serializedStream = fileTree.serializerStreamFactory( fs, fileTreeGen, true, ); - const serializedStream = - utils.asyncGeneratorToReadableStream(serializedGen); const outputStream = serializedStream.pipeThrough(parserTransform); for await (const output of outputStream) { data.push(output); diff --git a/tests/vaults/fileTree.test.ts b/tests/vaults/fileTree.test.ts index 32da7340b..f47831c72 100644 --- a/tests/vaults/fileTree.test.ts +++ b/tests/vaults/fileTree.test.ts @@ -6,7 +6,6 @@ import { ReadableStream } from 'stream/web'; import { test } from '@fast-check/jest'; import fc from 'fast-check'; import * as fileTree from '@/vaults/fileTree'; -import * as utils from '@/utils'; import * as vaultsTestUtils from './utils'; describe('fileTree', () => { @@ -497,13 +496,11 @@ describe('fileTree', () => { }); const data: Array = []; const parserTransform = fileTree.parserTransformStreamFactory(); - const serializedGen = fileTree.serializerStreamFactory( + const serializedStream = fileTree.serializerStreamFactory( fs, fileTreeGen, false, ); - const serializedStream = - utils.asyncGeneratorToReadableStream(serializedGen); const outputStream = serializedStream.pipeThrough(parserTransform); for await (const output of outputStream) { data.push(output); @@ -545,13 +542,11 @@ describe('fileTree', () => { 5, 7, 11, 13, ]); const parserTransform = fileTree.parserTransformStreamFactory(); - const serializedGen = fileTree.serializerStreamFactory( + const serializedStream = fileTree.serializerStreamFactory( fs, fileTreeGen, false, ); - const serializedStream = - utils.asyncGeneratorToReadableStream(serializedGen); const outputStream = serializedStream .pipeThrough(snipperTransform) .pipeThrough(parserTransform); @@ -664,13 +659,11 @@ describe('fileTree', () => { }); const data: Array = []; const parserTransform = fileTree.parserTransformStreamFactory(); - const serializedGen = fileTree.serializerStreamFactory( + const serializedStream = fileTree.serializerStreamFactory( fs, fileTreeGen, true, ); - const serializedStream = - utils.asyncGeneratorToReadableStream(serializedGen); const outputStream = serializedStream.pipeThrough(parserTransform); for await (const output of outputStream) { data.push(output);