-
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: revamped the entire VaultsSecretsList handler
- Loading branch information
1 parent
536820f
commit 527556d
Showing
6 changed files
with
115 additions
and
74 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 |
---|---|---|
|
@@ -36,9 +36,6 @@ | |
}, | ||
{ | ||
"name": "Aditya Varma" | ||
}, | ||
{ | ||
"name": "Aryan Jassal" | ||
} | ||
], | ||
"description": "Polykey Core Library", | ||
|
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,12 +1,5 @@ | ||
import type { HandlerTypes } from '@matrixai/rpc'; | ||
import type VaultsSecretsList from '../handlers/VaultsSecretsList'; | ||
import { UnaryCaller } from '@matrixai/rpc'; | ||
import { RawCaller } from '@matrixai/rpc'; | ||
|
||
type CallerTypes = HandlerTypes<VaultsSecretsList>; | ||
|
||
const vaultsSecretsList = new UnaryCaller< | ||
CallerTypes['input'], | ||
CallerTypes['output'] | ||
>(); | ||
const vaultsSecretsList = new RawCaller(); | ||
|
||
export default vaultsSecretsList; |
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,73 +1,97 @@ | ||
import type { DB } from '@matrixai/db'; | ||
import type { | ||
ClientRPCRequestParams, | ||
ClientRPCResponseResult, | ||
SecretFilesList, | ||
SecretPatternMessage, | ||
} from '../types'; | ||
import type { JSONObject, JSONRPCRequest } from '@matrixai/rpc'; | ||
import type VaultManager from '../../vaults/VaultManager'; | ||
import type { ContentNode, TreeNode } from '../../vaults/types'; | ||
import { UnaryHandler } from '@matrixai/rpc'; | ||
import type { TreeNode, ContentNode } from '../../vaults/types'; | ||
import { ReadableStream } from 'stream/web'; | ||
import { RawHandler } from '@matrixai/rpc'; | ||
import * as vaultsUtils from '../../vaults/utils'; | ||
import * as vaultsErrors from '../../vaults/errors'; | ||
import * as utils from '../../utils'; | ||
import { fileTree } from '../../vaults'; | ||
|
||
class VaultsSecretsList extends UnaryHandler< | ||
{ | ||
vaultManager: VaultManager; | ||
db: DB; | ||
}, | ||
ClientRPCRequestParams<SecretPatternMessage>, | ||
ClientRPCResponseResult<SecretFilesList> | ||
> { | ||
class VaultsSecretsGet extends RawHandler<{ | ||
vaultManager: VaultManager; | ||
db: DB; | ||
}> { | ||
public handle = async ( | ||
input: ClientRPCRequestParams<SecretPatternMessage>, | ||
): Promise<ClientRPCResponseResult<SecretFilesList>> => { | ||
input: [JSONRPCRequest, ReadableStream<Uint8Array>], | ||
_cancel: any, | ||
// meta: any, | ||
// ctx, | ||
): Promise<[JSONObject, ReadableStream<Uint8Array>]> => { | ||
const { vaultManager, db } = this.container; | ||
return await db.withTransactionF(async (tran) => { | ||
const [headerMessage, _] = input; | ||
|
||
const params = headerMessage.params; | ||
if (params == null || !utils.isObject(params)) utils.never(); | ||
utils.checkKeyInJSON('vaultNameOrId', 'string', params); | ||
utils.checkKeyInJSON('pattern', 'string', params); | ||
utils.checkKeyInJSON('yieldStats', 'boolean', params); | ||
utils.checkKeyInJSON('yieldRoot', 'boolean', params); | ||
utils.checkKeyInJSON('yieldFiles', 'boolean', params); | ||
utils.checkKeyInJSON('yieldParents', 'boolean', params); | ||
utils.checkKeyInJSON('yieldDirectories', 'boolean', params); | ||
utils.checkKeyInJSON('yieldContents', 'boolean', params); | ||
|
||
const vaultNameOrId = params.vaultNameOrId as string; | ||
const vaultId = await db.withTransactionF(async (tran) => { | ||
const vaultIdFromName = await vaultManager.getVaultId( | ||
input.nameOrId, | ||
vaultNameOrId, | ||
tran, | ||
); | ||
const vaultId = | ||
vaultIdFromName ?? vaultsUtils.decodeVaultId(input.nameOrId); | ||
if (vaultId == null) { | ||
throw new vaultsErrors.ErrorVaultsVaultUndefined(); | ||
vaultIdFromName ?? vaultsUtils.decodeVaultId(vaultNameOrId); | ||
if (vaultId == null) throw new vaultsErrors.ErrorVaultsVaultUndefined(); | ||
return vaultId; | ||
}); | ||
|
||
const filesData = vaultManager.withVaultsG([vaultId], (vault) => { | ||
return vault.readG(async function* (fs): AsyncGenerator< | ||
TreeNode | ContentNode | Uint8Array | ||
> { | ||
const fileTreeGen = fileTree.globWalk({ | ||
fs: fs, | ||
basePath: '.', | ||
pattern: params.pattern as string, | ||
yieldStats: params.yieldStats as boolean, | ||
yieldRoot: params.yieldRoot as boolean, | ||
yieldFiles: params.yieldFiles as boolean, | ||
yieldParents: params.yieldParents as boolean, | ||
yieldDirectories: params.yieldDirectories as boolean, | ||
}); | ||
return fileTree.serializerStreamFactory( | ||
fs, | ||
fileTreeGen, | ||
params.yieldContents as boolean, | ||
); | ||
}); | ||
}); | ||
|
||
async function* filterTransform( | ||
source: AsyncGenerator<TreeNode | ContentNode | Uint8Array>, | ||
): AsyncGenerator<Uint8Array> { | ||
for await (const chunk of source) { | ||
if (chunk instanceof Uint8Array) { | ||
// @ts-ignore while the types don't fully match, they match enough | ||
yield Buffer.from(chunk).toString(); | ||
} | ||
} | ||
} | ||
const filteredFilesData = filterTransform(filesData); | ||
|
||
const filesDataStream = new ReadableStream<Uint8Array>({ | ||
pull: async (controller) => { | ||
const next = await filteredFilesData.next(); | ||
if (next.done === true) return controller.close(); | ||
controller.enqueue(next.value); | ||
}, | ||
cancel: async() => { | ||
filteredFilesData.return('Closed'); | ||
} | ||
return await vaultManager.withVaults( | ||
[vaultId], | ||
async (vault) => { | ||
const data = await vault.readF(async (fs) => { | ||
const fileTreeGen = fileTree.globWalk({ | ||
fs: fs, | ||
basePath: '.', | ||
pattern: input.pattern, | ||
yieldStats: true, | ||
yieldRoot: false, | ||
yieldFiles: true, | ||
yieldParents: true, | ||
yieldDirectories: true, | ||
}); | ||
const data: Array<TreeNode | ContentNode | Uint8Array> = []; | ||
const serializedStream = fileTree.serializerStreamFactory( | ||
fs, | ||
fileTreeGen, | ||
true, | ||
); | ||
for await (const output of serializedStream) { | ||
data.push(output); | ||
} | ||
return data; | ||
}); | ||
const contents = data | ||
.filter((v) => v instanceof Uint8Array) | ||
.map((v) => Buffer.from(v as Uint8Array).toString()); | ||
return { secretFilesList: contents }; | ||
}, | ||
tran, | ||
); | ||
}); | ||
|
||
return [{}, filesDataStream]; | ||
}; | ||
} | ||
|
||
export default VaultsSecretsList; | ||
export default VaultsSecretsGet; |
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