|
| 1 | +import type { ContextTimed } from '@matrixai/contexts'; |
| 2 | +import type { DB } from '@matrixai/db'; |
| 3 | +import type { ResourceAcquire } from '@matrixai/resources'; |
| 4 | +import type { JSONValue } from '@matrixai/rpc'; |
| 5 | +import type { |
| 6 | + ClientRPCRequestParams, |
| 7 | + ClientRPCResponseResult, |
| 8 | + SecretIdentifierMessageTagged, |
| 9 | + SuccessOrErrorMessageTagged, |
| 10 | + VaultNamesHeaderMessageTagged, |
| 11 | +} from '../types'; |
| 12 | +import type VaultManager from '../../vaults/VaultManager'; |
| 13 | +import type { FileSystemWritable } from '../../vaults/types'; |
| 14 | +import { withG } from '@matrixai/resources'; |
| 15 | +import { DuplexHandler } from '@matrixai/rpc'; |
| 16 | +import * as vaultsUtils from '../../vaults/utils'; |
| 17 | +import * as vaultsErrors from '../../vaults/errors'; |
| 18 | +import * as clientErrors from '../errors'; |
| 19 | + |
| 20 | +class VaultsSecretsTouch extends DuplexHandler< |
| 21 | + { |
| 22 | + db: DB; |
| 23 | + vaultManager: VaultManager; |
| 24 | + }, |
| 25 | + ClientRPCRequestParams< |
| 26 | + VaultNamesHeaderMessageTagged | SecretIdentifierMessageTagged |
| 27 | + >, |
| 28 | + ClientRPCResponseResult<SuccessOrErrorMessageTagged> |
| 29 | +> { |
| 30 | + public handle = async function* ( |
| 31 | + input: AsyncIterableIterator< |
| 32 | + ClientRPCRequestParams< |
| 33 | + VaultNamesHeaderMessageTagged | SecretIdentifierMessageTagged |
| 34 | + > |
| 35 | + >, |
| 36 | + _cancel: (reason?: any) => void, |
| 37 | + _meta: Record<string, JSONValue>, |
| 38 | + ctx: ContextTimed, |
| 39 | + ): AsyncGenerator<ClientRPCResponseResult<SuccessOrErrorMessageTagged>> { |
| 40 | + const { db, vaultManager }: { db: DB; vaultManager: VaultManager } = |
| 41 | + this.container; |
| 42 | + // Extract the header message from the iterator |
| 43 | + const headerMessagePair = await input.next(); |
| 44 | + const headerMessage: |
| 45 | + | VaultNamesHeaderMessageTagged |
| 46 | + | SecretIdentifierMessageTagged = headerMessagePair.value; |
| 47 | + // Testing if the header is of the expected format |
| 48 | + if ( |
| 49 | + headerMessagePair.done || |
| 50 | + headerMessage.type !== 'VaultNamesHeaderMessage' |
| 51 | + ) { |
| 52 | + throw new clientErrors.ErrorClientInvalidHeader(); |
| 53 | + } |
| 54 | + // Create an array of write acquires |
| 55 | + const vaultAcquires = await db.withTransactionF(async (tran) => { |
| 56 | + const vaultAcquires: Array<ResourceAcquire<FileSystemWritable>> = []; |
| 57 | + for (const vaultName of headerMessage.vaultNames) { |
| 58 | + ctx.signal.throwIfAborted(); |
| 59 | + const vaultIdFromName = await vaultManager.getVaultId(vaultName, tran); |
| 60 | + const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(vaultName); |
| 61 | + if (vaultId == null) { |
| 62 | + throw new vaultsErrors.ErrorVaultsVaultUndefined( |
| 63 | + `Vault "${vaultName}" does not exist`, |
| 64 | + ); |
| 65 | + } |
| 66 | + // The resource acquisition will automatically create a transaction and |
| 67 | + // release it when cleaning up. |
| 68 | + const acquire = await vaultManager.withVaults( |
| 69 | + [vaultId], |
| 70 | + async (vault) => vault.acquireWrite(undefined, ctx), |
| 71 | + ); |
| 72 | + vaultAcquires.push(acquire); |
| 73 | + } |
| 74 | + return vaultAcquires; |
| 75 | + }); |
| 76 | + // Acquire all locks in parallel and perform all operations at once |
| 77 | + yield* withG( |
| 78 | + vaultAcquires, |
| 79 | + async function* (efses): AsyncGenerator<SuccessOrErrorMessageTagged> { |
| 80 | + // Creating the vault name to efs map for easy access |
| 81 | + const vaultMap = new Map<string, FileSystemWritable>(); |
| 82 | + for (let i = 0; i < efses.length; i++) { |
| 83 | + vaultMap.set(headerMessage!.vaultNames[i], efses[i]); |
| 84 | + } |
| 85 | + let loopRan = false; |
| 86 | + for await (const message of input) { |
| 87 | + ctx.signal.throwIfAborted(); |
| 88 | + loopRan = true; |
| 89 | + // Header messages should not be seen anymore |
| 90 | + if (message.type === 'VaultNamesHeaderMessage') { |
| 91 | + throw new clientErrors.ErrorClientProtocolError( |
| 92 | + 'The header message cannot be sent multiple times', |
| 93 | + ); |
| 94 | + } |
| 95 | + const efs = vaultMap.get(message.nameOrId); |
| 96 | + if (efs == null) { |
| 97 | + throw new vaultsErrors.ErrorVaultsVaultUndefined( |
| 98 | + `Vault ${message.nameOrId} was not present in the header message`, |
| 99 | + ); |
| 100 | + } |
| 101 | + try { |
| 102 | + // If the file exists, update its timestamps. Otherwise, create the |
| 103 | + // file. Note that this can throw errors, which are handled later. |
| 104 | + if (await efs.exists(message.secretName)) { |
| 105 | + const now = new Date(); |
| 106 | + await efs.utimes(message.secretName, now, now); |
| 107 | + } else { |
| 108 | + await efs.writeFile(message.secretName); |
| 109 | + } |
| 110 | + yield { |
| 111 | + type: 'SuccessMessage', |
| 112 | + success: true, |
| 113 | + }; |
| 114 | + } catch (e) { |
| 115 | + switch (e.code) { |
| 116 | + case 'ENOENT': |
| 117 | + yield { |
| 118 | + type: 'ErrorMessage', |
| 119 | + code: e.code, |
| 120 | + reason: message.secretName, |
| 121 | + }; |
| 122 | + break; |
| 123 | + default: |
| 124 | + throw e; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + // Content messages must follow header messages |
| 129 | + if (!loopRan) { |
| 130 | + throw new clientErrors.ErrorClientProtocolError( |
| 131 | + 'No content messages followed header message', |
| 132 | + ); |
| 133 | + } |
| 134 | + }, |
| 135 | + ); |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +export default VaultsSecretsTouch; |
0 commit comments