-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: switched over the rpc handler from rawhandler to stremhandler
- Loading branch information
1 parent
b86dfd4
commit 1fc3ba0
Showing
8 changed files
with
64 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import { RawCaller } from '@matrixai/rpc'; | ||
import type { HandlerTypes } from '@matrixai/rpc'; | ||
import type VaultsSecretsGetFileTree from '../handlers/VaultsSecretsGetFileTree'; | ||
import { ServerCaller } from '@matrixai/rpc'; | ||
|
||
const vaultsSecretsGetFileTree = new RawCaller(); | ||
type CallerTypes = HandlerTypes<VaultsSecretsGetFileTree>; | ||
|
||
const vaultsSecretsGetFileTree = new ServerCaller< | ||
CallerTypes['input'], | ||
CallerTypes['output'] | ||
>(); | ||
|
||
export default vaultsSecretsGetFileTree; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,140 +1,56 @@ | ||
import type { DB } from '@matrixai/db'; | ||
import type { JSONObject, JSONRPCRequest } from '@matrixai/rpc'; | ||
import type VaultManager from '../../vaults/VaultManager'; | ||
import { ReadableStream } from 'stream/web'; | ||
import { RawHandler } from '@matrixai/rpc'; | ||
import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types'; | ||
import type { TreeNode } from '../../vaults/types'; | ||
import type { SecretFilesMessage } from '../types'; | ||
import { ServerHandler } from '@matrixai/rpc'; | ||
import { fileTree } from '../../vaults'; | ||
import * as vaultsUtils from '../../vaults/utils'; | ||
import * as vaultsErrors from '../../vaults/errors'; | ||
import * as utils from '../../utils'; | ||
import { fileTree } from '../../vaults'; | ||
import { validateSync } from '../../validation'; | ||
import * as validationErrors from '../../validation/errors'; | ||
|
||
class VaultsSecretsGetFileTree extends RawHandler<{ | ||
vaultManager: VaultManager; | ||
db: DB; | ||
}> { | ||
public handle = async ( | ||
input: [JSONRPCRequest, ReadableStream<Uint8Array>], | ||
class VaultsSecretsGetFileTree extends ServerHandler< | ||
{ | ||
vaultManager: VaultManager; | ||
db: DB; | ||
}, | ||
ClientRPCRequestParams<SecretFilesMessage>, | ||
ClientRPCResponseResult<TreeNode> | ||
> { | ||
public async *handle( | ||
input: ClientRPCRequestParams<SecretFilesMessage>, | ||
_cancel: any, | ||
): Promise<[JSONObject, ReadableStream<Uint8Array>]> => { | ||
): AsyncGenerator<ClientRPCResponseResult<TreeNode>, void, void> { | ||
const { vaultManager, db } = this.container; | ||
const [headerMessage, _] = input; | ||
|
||
const params = headerMessage.params; | ||
if (params == null || !utils.isObject(params)) utils.never(); | ||
|
||
// TEST: testing needed | ||
function stringParser(value: any): string { | ||
if (typeof value !== 'string') { | ||
throw new validationErrors.ErrorParse( | ||
'Provided value must be a string', | ||
); | ||
} | ||
return value as string; | ||
} | ||
|
||
function globPatternParser(value: any): string { | ||
const allowedCharacters = /^[\w\s\-./*?[\]{}]*$/; | ||
const invalidCharacters = /[\0:]/; | ||
if (!value) { | ||
throw new validationErrors.ErrorParse('Glob pattern must not be empty'); | ||
} | ||
if (typeof value !== 'string') { | ||
throw new validationErrors.ErrorParse( | ||
'Glob pattern must be must be a string', | ||
); | ||
} | ||
if (!allowedCharacters.test(value) || invalidCharacters.test(value)) { | ||
throw new validationErrors.ErrorParse('Glob pattern is not valid'); | ||
} | ||
return value as string; | ||
} | ||
|
||
function booleanParser(value: any): boolean { | ||
if (typeof value !== 'boolean') { | ||
throw new validationErrors.ErrorParse( | ||
'Provided value must be a string', | ||
); | ||
} | ||
return value as boolean; | ||
} | ||
|
||
const { | ||
vaultNameOrId, | ||
pattern, | ||
yieldStats, | ||
yieldRoot, | ||
yieldFiles, | ||
yieldParents, | ||
yieldDirectories, | ||
}: { | ||
vaultNameOrId: string; | ||
pattern: string; | ||
yieldStats: boolean; | ||
yieldRoot: boolean; | ||
yieldFiles: boolean; | ||
yieldParents: boolean; | ||
yieldDirectories: boolean; | ||
} = validateSync( | ||
(keyPath, value) => { | ||
return utils.matchSync(keyPath)( | ||
[['vaultNameOrId'], () => stringParser(value)], | ||
[['pattern'], () => globPatternParser(value)], | ||
[ | ||
[ | ||
'yieldStats', | ||
'yieldRoot', | ||
'yieldFiles', | ||
'yieldParents', | ||
'yieldDirectories', | ||
], | ||
() => booleanParser(value), | ||
], | ||
() => value, | ||
); | ||
}, | ||
{ | ||
vaultNameOrId: params.vaultNameOrId, | ||
pattern: params.pattern, | ||
yieldStats: params.yieldStats, | ||
yieldRoot: params.yieldRoot, | ||
yieldFiles: params.yieldFiles, | ||
yieldParents: params.yieldParents, | ||
yieldDirectories: params.yieldDirectories, | ||
}, | ||
); | ||
|
||
const vaultId = await db.withTransactionF(async (tran) => { | ||
const vaultIdFromName = await vaultManager.getVaultId( | ||
vaultNameOrId, | ||
input.nameOrId, | ||
tran, | ||
); | ||
const vaultId = | ||
vaultIdFromName ?? vaultsUtils.decodeVaultId(vaultNameOrId); | ||
vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId); | ||
if (vaultId == null) throw new vaultsErrors.ErrorVaultsVaultUndefined(); | ||
return vaultId; | ||
}); | ||
|
||
const filesData = vaultManager.withVaultsG([vaultId], (vault) => { | ||
return vault.readG((fs): AsyncGenerator<Uint8Array, void, void> => { | ||
const fileTreeGen = fileTree.globWalk({ | ||
yield* vaultManager.withVaultsG([vaultId], (vault) => { | ||
return vault.readG(async function* (fs): AsyncGenerator< | ||
TreeNode, | ||
void, | ||
void | ||
> { | ||
yield* fileTree.globWalk({ | ||
fs: fs, | ||
basePath: '.', | ||
pattern: pattern, | ||
yieldStats: yieldStats, | ||
yieldRoot: yieldRoot, | ||
yieldFiles: yieldFiles, | ||
yieldParents: yieldParents, | ||
yieldDirectories: yieldDirectories, | ||
pattern: input.pattern, | ||
yieldStats: input.yieldStats, | ||
yieldRoot: input.yieldRoot, | ||
yieldFiles: input.yieldFiles, | ||
yieldParents: input.yieldParents, | ||
yieldDirectories: input.yieldDirectories, | ||
}); | ||
return fileTree.serializerStreamFactory(fs, fileTreeGen, false); | ||
}); | ||
}); | ||
|
||
const filesDataStream = utils.asyncGeneratorToReadableStream(filesData); | ||
return [{}, filesDataStream]; | ||
}; | ||
} | ||
} | ||
|
||
export default VaultsSecretsGetFileTree; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters