@@ -5,20 +5,19 @@ import type {
55 ClientRPCRequestParams ,
66 ClientRPCResponseResult ,
77 SecretIdentifierMessage ,
8- SecretContentMessage ,
8+ SecretContentOrErrorMessage ,
99} from '../types.js' ;
1010import type VaultManager from '../../vaults/VaultManager.js' ;
1111import { DuplexHandler } from '@matrixai/rpc' ;
1212import * as vaultsUtils from '../../vaults/utils.js' ;
13- import * as vaultsErrors from '../../vaults/errors.js' ;
1413
1514class VaultsSecretsEnv extends DuplexHandler <
1615 {
1716 db : DB ;
1817 vaultManager : VaultManager ;
1918 } ,
2019 ClientRPCRequestParams < SecretIdentifierMessage > ,
21- ClientRPCResponseResult < SecretContentMessage >
20+ ClientRPCResponseResult < SecretContentOrErrorMessage >
2221> {
2322 public handle = async function * (
2423 input : AsyncIterableIterator <
@@ -27,64 +26,73 @@ class VaultsSecretsEnv extends DuplexHandler<
2726 _cancel : ( reason ?: any ) => void ,
2827 _meta : Record < string , JSONValue > | undefined ,
2928 ctx : ContextTimed ,
30- ) : AsyncGenerator < ClientRPCResponseResult < SecretContentMessage > > {
29+ ) : AsyncGenerator <
30+ ClientRPCResponseResult < SecretContentOrErrorMessage > ,
31+ void ,
32+ void
33+ > {
3134 const { db, vaultManager } : { db : DB ; vaultManager : VaultManager } =
3235 this . container ;
3336 return yield * db . withTransactionG ( async function * ( tran ) : AsyncGenerator <
34- ClientRPCResponseResult < SecretContentMessage >
37+ ClientRPCResponseResult < SecretContentOrErrorMessage > ,
38+ void ,
39+ void
3540 > {
3641 for await ( const secretIdentifierMessage of input ) {
3742 const { nameOrId, secretName } = secretIdentifierMessage ;
3843 const vaultIdFromName = await vaultManager . getVaultId ( nameOrId , tran ) ;
3944 const vaultId = vaultIdFromName ?? vaultsUtils . decodeVaultId ( nameOrId ) ;
4045 if ( vaultId == null ) {
41- throw new vaultsErrors . ErrorVaultsVaultUndefined (
42- `Vault "${ nameOrId } " does not exist` ,
43- ) ;
46+ yield {
47+ type : 'ErrorMessage' ,
48+ code : 'EINVAL' ,
49+ reason : `Vault "${ nameOrId } " does not exist` ,
50+ data : { secretName : undefined , nameOrId } ,
51+ } ;
52+ continue ;
4453 }
45- const secrets = await vaultManager . withVaults (
54+ yield * vaultManager . withVaultsG (
4655 [ vaultId ] ,
47- async ( vault ) => {
48- const results : Array < {
49- filePath : string ;
50- value : string ;
51- } > = [ ] ;
52- return await vault . readF ( async ( fs ) => {
56+ async function * (
57+ vault ,
58+ ) : AsyncGenerator < SecretContentOrErrorMessage , void , void > {
59+ yield * vault . readG ( async function * ( efs ) : AsyncGenerator <
60+ SecretContentOrErrorMessage ,
61+ void ,
62+ void
63+ > {
5364 try {
5465 for await ( const filePath of vaultsUtils . walkFs (
55- fs ,
66+ efs ,
5667 secretName ,
5768 ) ) {
5869 ctx . signal . throwIfAborted ( ) ;
59- const fileContents = await fs . readFile ( filePath ) ;
60- results . push ( {
61- filePath : filePath ,
62- value : fileContents . toString ( ) ,
63- } ) ;
70+ const fileContents = await efs . readFile ( filePath ) ;
71+ yield {
72+ type : 'SuccessMessage' ,
73+ success : true ,
74+ nameOrId : nameOrId ,
75+ secretName : filePath ,
76+ secretContent : fileContents . toString ( ) ,
77+ } ;
6478 }
6579 } catch ( e ) {
6680 if ( e . code === 'ENOENT' ) {
67- throw new vaultsErrors . ErrorSecretsSecretUndefined (
68- `Secret with name: ${ secretName } does not exist` ,
69- { cause : e } ,
70- ) ;
81+ yield {
82+ type : 'ErrorMessage' ,
83+ code : e . code ,
84+ reason : `Secret "${ secretName } " does not exist` ,
85+ data : { secretName, nameOrId } ,
86+ } ;
87+ } else {
88+ throw e ;
7189 }
72- throw e ;
7390 }
74- return results ;
7591 } ) ;
7692 } ,
7793 tran ,
7894 ctx ,
7995 ) ;
80- for ( const { filePath, value } of secrets ) {
81- ctx . signal . throwIfAborted ( ) ;
82- yield {
83- nameOrId : nameOrId ,
84- secretName : filePath ,
85- secretContent : value ,
86- } ;
87- }
8896 }
8997 } ) ;
9098 } ;
0 commit comments