Skip to content

Commit

Permalink
chore: cleaned up irrelevant code
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Aug 23, 2024
1 parent 7fe8f76 commit f1eac5e
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 108 deletions.
6 changes: 3 additions & 3 deletions src/client/callers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import vaultsSecretsDelete from './vaultsSecretsDelete';
import vaultsSecretsEdit from './vaultsSecretsEdit';
import vaultsSecretsEnv from './vaultsSecretsEnv';
import vaultsSecretsGet from './vaultsSecretsGet';
import vaultsSecretsGetFileTree from './vaultsSecretsGetFileTree';
import vaultsSecretsList from './vaultsSecretsList';
import vaultsSecretsMkdir from './vaultsSecretsMkdir';
import vaultsSecretsNew from './vaultsSecretsNew';
import vaultsSecretsNewDir from './vaultsSecretsNewDir';
Expand Down Expand Up @@ -148,7 +148,7 @@ const clientManifest = {
vaultsSecretsEdit,
vaultsSecretsEnv,
vaultsSecretsGet,
vaultsSecretsGetFileTree,
vaultsSecretsList,
vaultsSecretsMkdir,
vaultsSecretsNew,
vaultsSecretsNewDir,
Expand Down Expand Up @@ -228,7 +228,7 @@ export {
vaultsSecretsEdit,
vaultsSecretsEnv,
vaultsSecretsGet,
vaultsSecretsGetFileTree,
vaultsSecretsList,
vaultsSecretsMkdir,
vaultsSecretsNew,
vaultsSecretsNewDir,
Expand Down
12 changes: 0 additions & 12 deletions src/client/callers/vaultsSecretsGetFileTree.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/client/callers/vaultsSecretsList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { HandlerTypes } from '@matrixai/rpc';
import type VaultsSecretsList from '../handlers/VaultsSecretsList';
import { ServerCaller } from '@matrixai/rpc';

type CallerTypes = HandlerTypes<VaultsSecretsList>;

const vaultsSecretsList = new ServerCaller<
CallerTypes['input'],
CallerTypes['output']
>();

export default vaultsSecretsList;
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { DB } from '@matrixai/db';
import type VaultManager from '../../vaults/VaultManager';
import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types';
import type { SecretFilesMessage, VaultFileNode } 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 { StatEncoded } from '../../vaults/types';
import * as vaultsUtils from '../../vaults/utils';
import * as vaultsErrors from '../../vaults/errors';
import * as clientErrors from '../errors';
Expand All @@ -16,12 +16,12 @@ class VaultsSecretsGetFileTree extends ServerHandler<
db: DB;
},
ClientRPCRequestParams<SecretFilesMessage>,
ClientRPCResponseResult<VaultFileNode>
ClientRPCResponseResult<SecretFile>
> {
public async *handle(
input: ClientRPCRequestParams<SecretFilesMessage>,
_cancel: any,
): AsyncGenerator<ClientRPCResponseResult<VaultFileNode>, void, void> {
): AsyncGenerator<ClientRPCResponseResult<SecretFile>, void, void> {
const { vaultManager, db } = this.container;
const vaultId = await db.withTransactionF(async (tran) => {
const vaultIdFromName = await vaultManager.getVaultId(
Expand All @@ -36,14 +36,14 @@ class VaultsSecretsGetFileTree extends ServerHandler<

yield* vaultManager.withVaultsG([vaultId], (vault) => {
return vault.readG(async function* (fs): AsyncGenerator<
VaultFileNode,
SecretFile,
void,
void
> {
try {
// @ts-ignore: While the types don't fully match, it matches enough for our usage.
let files: Array<string> = await fs.promises.readdir(input.pattern);
files = files.map((file) => path.join(input.pattern, file));
let files: Array<string> = await fs.promises.readdir(input.path);
files = files.map((file) => path.join(input.path, file));

for await (const file of files) {
try {
Expand Down Expand Up @@ -74,7 +74,7 @@ class VaultsSecretsGetFileTree extends ServerHandler<
}
} catch (e) {
throw new clientErrors.ErrorClientFSReadFailed(
`Failed to read directory: ${input.pattern}`,
`Failed to read directory: ${input.path}`,
{ cause: e },
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/client/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import VaultsSecretsDelete from './VaultsSecretsDelete';
import VaultsSecretsEdit from './VaultsSecretsEdit';
import VaultsSecretsEnv from './VaultsSecretsEnv';
import VaultsSecretsGet from './VaultsSecretsGet';
import VaultsSecretsGetFileTree from './VaultsSecretsGetFileTree';
import VaultsSecretsList from './VaultsSecretsList';
import VaultsSecretsMkdir from './VaultsSecretsMkdir';
import VaultsSecretsNew from './VaultsSecretsNew';
import VaultsSecretsNewDir from './VaultsSecretsNewDir';
Expand Down Expand Up @@ -188,7 +188,7 @@ const serverManifest = (container: {
vaultsSecretsEdit: new VaultsSecretsEdit(container),
vaultsSecretsEnv: new VaultsSecretsEnv(container),
vaultsSecretsGet: new VaultsSecretsGet(container),
vaultsSecretsGetFileTree: new VaultsSecretsGetFileTree(container),
vaultsSecretsList: new VaultsSecretsList(container),
vaultsSecretsMkdir: new VaultsSecretsMkdir(container),
vaultsSecretsNew: new VaultsSecretsNew(container),
vaultsSecretsNewDir: new VaultsSecretsNewDir(container),
Expand Down Expand Up @@ -270,7 +270,7 @@ export {
VaultsSecretsEdit,
VaultsSecretsEnv,
VaultsSecretsGet,
VaultsSecretsGetFileTree,
VaultsSecretsList,
VaultsSecretsMkdir,
VaultsSecretsNew,
VaultsSecretsNewDir,
Expand Down
6 changes: 3 additions & 3 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ type VaultsLatestVersionMessage = {

// Secrets
type SecretFilesMessage = VaultIdentifierMessage & {
pattern: string;
path: string;
yieldStats: boolean;
};

Expand Down Expand Up @@ -336,7 +336,7 @@ type SecretRenameMessage = SecretIdentifierMessage & {
newSecretName: string;
};

type VaultFileNode = {
type SecretFile = {
path: string;
type: 'FILE' | 'DIRECTORY';
stat?: StatEncoded;
Expand Down Expand Up @@ -433,7 +433,7 @@ export type {
SecretMkdirMessage,
SecretDirMessage,
SecretRenameMessage,
VaultFileNode,
SecretFile,
SecretStatMessage,
SignatureMessage,
OverrideRPClientType,
Expand Down
35 changes: 0 additions & 35 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import os from 'os';
import process from 'process';
import path from 'path';
import nodesEvents from 'events';
import { ReadableStream } from 'stream/web';
import lexi from 'lexicographic-integer';
import { PromiseCancellable } from '@matrixai/async-cancellable';
import { timedCancellable } from '@matrixai/contexts/dist/functions';
Expand Down Expand Up @@ -542,38 +541,6 @@ function setMaxListeners(
nodesEvents.setMaxListeners(limit, target);
}

function asyncGeneratorToReadableStream<T>(
generator: AsyncGenerator<T, void, void>,
): ReadableStream<T> {
return new ReadableStream<T>({
pull: async (controller) => {
const { done, value } = await generator.next();
if (done === true) return controller.close();
controller.enqueue(value);
},
cancel: async (reason) => {
if (typeof generator.throw === 'function') {
generator.throw(reason).catch(() => {});
}
},
});
}

async function* readableStreamToAsyncGenerator<T>(
stream: ReadableStream<T>,
): AsyncGenerator<T, void, void> {
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
yield value;
}
} finally {
reader.releaseLock();
}
}

export {
AsyncFunction,
GeneratorFunction,
Expand Down Expand Up @@ -614,6 +581,4 @@ export {
isBufferSource,
yieldMicro,
setMaxListeners,
asyncGeneratorToReadableStream,
readableStreamToAsyncGenerator,
};
80 changes: 47 additions & 33 deletions src/vaults/fileTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
HeaderContent,
} from './types';
import path from 'path';
import { TransformStream } from 'stream/web';
import { ReadableStream, TransformStream } from 'stream/web';
import { minimatch } from 'minimatch';
import { JSONParser, TokenizerError } from '@streamparser/json';
import * as vaultsUtils from './utils';
Expand Down Expand Up @@ -334,17 +334,16 @@ async function* encodeContent(
}

/**
* Takes an AsyncGenerator<TreeNode> and serializes it into a `AsyncGenerator<UInt8Array>`
* Takes an AsyncGenerator<TreeNode> and serializes it into a `ReadableStream<UInt8Array>`
* @param fs
* @param treeGen - An AsyncGenerator<TreeNode> that yields the files and directories of a file tree.
* @param yieldContents - Toggles sending the contents of files after the file tree.
*/
// TODO: change name?
async function* serializerStreamFactory(
function serializerStreamFactory(
fs: FileSystem | FileSystemReadable,
treeGen: AsyncGenerator<TreeNode, void, void>,
yieldContents: boolean = true,
): AsyncGenerator<Uint8Array, void, void> {
): ReadableStream<Uint8Array> {
const files: Array<[number, string]> = [];
let treeDataGen: AsyncGenerator<TreeNode, void, void> | undefined = treeGen;
let contentsGen: AsyncGenerator<Uint8Array, void, void> | undefined =
Expand All @@ -369,38 +368,53 @@ async function* serializerStreamFactory(
await treeDataGen?.throw(reason).catch(() => {});
await contentsGen?.throw(reason).catch(() => {});
}
try {
yield generateGenericHeader({ type: HeaderType.TREE });
while (true) {
if (treeDataGen != null) {
const result = await treeGen.next();
if (!result.done) {
// If a file, add to the file list to encode contents later
if (result.value.type === 'FILE') {
files.push([result.value.iNode, result.value.path]);
return new ReadableStream<Uint8Array>({
start: (controller) => {
controller.enqueue(
generateGenericHeader({
type: HeaderType.TREE,
}),
);
},
pull: async (controller) => {
try {
if (treeDataGen != null) {
const result = await treeGen.next();
if (!result.done) {
// If a file, add to the file list to encode contents later
if (result.value.type === 'FILE') {
files.push([result.value.iNode, result.value.path]);
}
// Normal tree nodes are just serialized and converted to `UInt8Array`
const jsonSerialized = JSON.stringify(result.value);
controller.enqueue(
vaultsUtils.bufferToUint8ArrayCopyless(
Buffer.from(jsonSerialized, 'utf-8'),
),
);
} else {
const treeDoneMessage = JSON.stringify({ type: 'DONE' });
controller.enqueue(
vaultsUtils.bufferToUint8ArrayCopyless(
Buffer.from(treeDoneMessage, 'utf-8'),
),
);
treeDataGen = undefined;
}
// Normal tree nodes are just serialized and converted to `UInt8Array`
const jsonSerialized = JSON.stringify(result.value);
yield vaultsUtils.bufferToUint8ArrayCopyless(
Buffer.from(jsonSerialized, 'utf-8'),
);
} else {
const treeDoneMessage = JSON.stringify({ type: 'DONE' });
yield vaultsUtils.bufferToUint8ArrayCopyless(
Buffer.from(treeDoneMessage, 'utf-8'),
);
treeDataGen = undefined;
const contentDataChunk = await getNextContentChunk();
if (contentDataChunk == null) return controller.close();
controller.enqueue(contentDataChunk);
}
} else {
const contentDataChunk = await getNextContentChunk();
if (contentDataChunk == null) return;
yield contentDataChunk;
} catch (e) {
await cleanup(e);
return controller.error(e);
}
}
} catch (e) {
await cleanup(e);
throw e;
}
},
cancel: async (reason) => {
await cleanup(reason);
},
});
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/vaults/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ type INode = number;

type StatEncoded = {
isSymbolicLink: boolean;
symbolicLinkTarget?: string,
dev: number;
ino: number;
mode: number;
Expand Down
Loading

0 comments on commit f1eac5e

Please sign in to comment.