Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott committed Nov 8, 2021
1 parent 1df00ad commit 10a51e0
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 6,776 deletions.
29 changes: 19 additions & 10 deletions src/client/rpcVaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,21 +644,30 @@ const createVaultRPC = ({
},
secretsEnv: async (
call: grpc.ServerWritableStream<
clientPB.VaultMessage,
clientPB.DirectoryMessage
secretsPB.Directory,
secretsPB.Directory
>,
): Promise<void> => {
const genWritable = grpcUtils.generatorWritable(call);
try {
await utils.checkPassword(call.metadata, sessionManager);
const name = call.request.getName();
const pattern = call.request.getId();
const id = utils.parseVaultInput(name, vaultManager);
const vault = vaultManager.getVault(id);
const res = await utils.glob(vault.EncryptedFS, pattern);
const dirMessage = new clientPB.DirectoryMessage();
await sessionManager.verifyToken(utils.getToken(call.metadata));
const responseMeta = utils.createMetaTokenResponse(
await sessionManager.generateToken(),
);
call.sendMetadata(responseMeta);
//Getting the vault.
const directoryMessage = call.request;
const vaultMessage = directoryMessage.getVault();
if (vaultMessage == null) {
await genWritable.throw({ code: grpc.status.NOT_FOUND });
return;
}
const vaultId = await utils.parseVaultInput(vaultMessage, vaultManager);
const pattern = directoryMessage.getSecretDirectory();
const res = await vaultManager.glob(vaultId, pattern);
const dirMessage = new secretsPB.Directory();
for (const file in res) {
dirMessage.setDir(file);
dirMessage.setSecretDirectory(file);
await genWritable.next(dirMessage);
}
await genWritable.next(null);
Expand Down
75 changes: 0 additions & 75 deletions src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import type { VaultId, VaultName } from '../vaults/types';
import type { Session } from '../sessions';
import type { VaultManager } from '../vaults';
import type { SessionToken } from '../sessions/types';
import type { Path } from 'globrex';

import fs from 'fs';
import path from 'path';
import globrex from 'globrex';
import globalyzer from 'globalyzer';
import * as grpc from '@grpc/grpc-js';
import * as clientErrors from '../client/errors';
import { ErrorVaultUndefined } from '../vaults/errors';
Expand Down Expand Up @@ -74,75 +69,5 @@ function createMetaTokenResponse(token: SessionToken): grpc.Metadata {
return meta;
}

const isHidden = /(^|[\\\/])\.[^\\\/\.]/g;
let CACHE = {};

async function walk(filesystem: typeof fs, output: string[], prefix: string, lexer, opts, dirname='', level=0) {
const readdir = utils.promisify(filesystem.readdir).bind(filesystem);
const lstat = utils.promisify(filesystem.lstat).bind(filesystem);

const rgx = lexer.segments[level];
const dir = path.resolve(opts.cwd, prefix, dirname);
const files = await readdir(dir);
const { dot, filesOnly } = opts;

let i=0, len=files.length, file;
let fullpath, relpath, stats, isMatch;

for (; i < len; i++) {
fullpath = path.join(dir, file=files[i]);
relpath = dirname ? path.join(dirname, file) : file;
// if (!dot && isHidden.test(relpath)) continue;
isMatch = lexer.regex.test(relpath);

if ((stats=CACHE[relpath]) === void 0) {
CACHE[relpath] = stats = await lstat(fullpath);
}

if (!stats.isDirectory()) {
isMatch && output.push(path.relative(opts.cwd, fullpath));
continue;
}

if (rgx && !rgx.test(file)) continue;
!filesOnly && isMatch && output.push(path.join(prefix, relpath));

await walk(filesystem, output, prefix, lexer, opts, relpath, rgx && rgx.toString() !== lexer.globstar && level + 1);
}
}

async function glob(filesystem: typeof fs, str: string, opts={ cwd: '.', absolute: true, filesOnly: false, flush: true }) {
const stat = utils.promisify(filesystem.stat).bind(filesystem);

if (!str) return [];

let glob = globalyzer(str);

opts.cwd = opts.cwd || '.';

if (!glob.isGlob) {
try {
let resolved = path.resolve(opts.cwd, str);
let dirent = await stat(resolved);
if (opts.filesOnly && !dirent.isFile()) return [];

return opts.absolute ? [resolved] : [str];
} catch (err) {
if (err.code != 'ENOENT') throw err;

return [];
}
}

if (opts.flush) CACHE = {};

let matches = [];
const res = globrex(glob.glob, { filepath:true, globstar:true, extended:true });
const globPath = res.path;

await walk(filesystem, matches, glob.base, globPath, opts, '.', 0);

return opts.absolute ? matches.map(x => path.resolve(opts.cwd, x)) : matches;
}

export { parseVaultInput, getToken, refreshSession, createMetaTokenResponse };
87 changes: 0 additions & 87 deletions src/git/GitRequest.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { default as GitRequest } from './GitRequest';
export * as utils from './utils';
export * as types from './types';
export * as errors from './errors';
Loading

0 comments on commit 10a51e0

Please sign in to comment.