Skip to content

Commit

Permalink
use p-limit to pool requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmihalcik-virtru committed May 9, 2024
1 parent 71a9746 commit c6027f5
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 82 deletions.
90 changes: 61 additions & 29 deletions web-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@opentdf/client": "file:../lib/opentdf-client-2.0.0.tgz",
"clsx": "^2.0.0",
"native-file-system-adapter": "^3.0.1",
"p-limit": "^5.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
124 changes: 71 additions & 53 deletions web-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import './App.css';
import { type Chunker, type DecryptSource, NanoTDFClient, TDF3Client } from '@opentdf/client';
import { type SessionInformation, OidcClient } from './session.js';
import { c } from './config.js';
import pLimit from 'p-limit';

const limit = pLimit(16);

function decryptedFileName(encryptedFileName: string): string {
// Groups: 1 file 'name' bit
Expand Down Expand Up @@ -390,40 +393,48 @@ function App() {
}
setDownloadState('Encrypt Complete');
}
for (const inputSource of inputSources) {
const inputFileName = fileNameFor(inputSource);
console.log(`Encrypting [${inputFileName}] as ${encryptContainerType} to ${sinkType}`);
switch (encryptContainerType) {
case 'nano': {
let promises;
switch (encryptContainerType) {
case 'nano': {
promises = inputSources.map((inputSource): () => Promise<void> => async () => {
const nanoClient = new NanoTDFClient({
authProvider: oidcClient,
kasEndpoint: c.kas,
dpopKeys: oidcClient.getSigningKey(),
});
const inputFileName = fileNameFor(inputSource);
console.log(`Encrypting [${inputFileName}] as ${encryptContainerType} to ${sinkType}`);
await encryptNano(nanoClient, inputSource, inputFileName);
break;
}
case 'html': {
const client = new TDF3Client({
authProvider: oidcClient,
dpopKeys: oidcClient.getSigningKey(),
kasEndpoint: c.kas,
readerUrl: c.reader,
});
});
break;
}
case 'html': {
const client = new TDF3Client({
authProvider: oidcClient,
dpopKeys: oidcClient.getSigningKey(),
kasEndpoint: c.kas,
readerUrl: c.reader,
});
promises = inputSources.map((inputSource): () => Promise<void> => async () => {
const inputFileName = fileNameFor(inputSource);
await encryptTdfHtml(inputSource, inputFileName, client);
break;
}
case 'tdf': {
const client = new TDF3Client({
authProvider: oidcClient,
dpopKeys: oidcClient.getSigningKey(),
kasEndpoint: c.kas,
});
});
break;
}
case 'tdf': {
const client = new TDF3Client({
authProvider: oidcClient,
dpopKeys: oidcClient.getSigningKey(),
kasEndpoint: c.kas,
});
promises = inputSources.map((inputSource): () => Promise<void> => async () => {
const inputFileName = fileNameFor(inputSource);
await encryptTdf(inputSource, inputFileName, client);
break;
}
});
break;
}
}
await Promise.all(promises.map(limit));

if (memory.length) {
setInputSources(memory);
Expand Down Expand Up @@ -604,11 +615,16 @@ function App() {
await writable.close();
}
break;
case 'memory':
memory.push({ type: 'memory', name: dfn, src: cipherText });
break;
case 'none':
break;
}
}

let promises: (() => Promise<void>)[];
const memory = [];
const handleDecrypt = async () => {
if (!inputSources.length) {
console.log('PLEASE SELECT FILE');
Expand All @@ -619,41 +635,43 @@ function App() {
return false;
}

for (const inputSource of inputSources) {
const dfn = decryptedFileName(fileNameFor(inputSource));
console.log(
`Decrypting ${decryptContainerType} ${JSON.stringify(inputSource)} to ${sinkType} ${dfn}`
);
switch (decryptContainerType) {
case 'tdf': {
const client = new TDF3Client({
authProvider: oidcClient,
dpopKeys: oidcClient.getSigningKey(),
kasEndpoint: c.kas,
});

switch (decryptContainerType) {
case 'tdf': {
const client = new TDF3Client({
authProvider: oidcClient,
dpopKeys: oidcClient.getSigningKey(),
kasEndpoint: c.kas,
});
promises = inputSources.map((inputSource): () => Promise<void> => async () => {
const dfn = decryptedFileName(fileNameFor(inputSource));
console.log(
`Decrypting ${decryptContainerType} ${JSON.stringify(inputSource)} to ${sinkType} ${dfn}`
);
await decryptTdf(client, inputSource, dfn);
break;
}
case 'nano': {
});
break;
}
case 'nano': {
const nanoClient = new NanoTDFClient({
authProvider: oidcClient,
kasEndpoint: c.kas,
dpopKeys: oidcClient.getSigningKey(),
});
promises = inputSources.map((inputSource): () => Promise<void> => async () => {
if ('url' in inputSource) {
throw new Error('Unsupported : fetch the url I guess?');
}
const nanoClient = new NanoTDFClient({
authProvider: oidcClient,
kasEndpoint: c.kas,
dpopKeys: oidcClient.getSigningKey(),
});
try {
await decryptNano(nanoClient, inputSource, dfn);
} catch (e) {
console.error('Decrypt Failed', e);
setDownloadState(`Decrypt Failed: ${e}`);
}
break;
}
const dfn = decryptedFileName(fileNameFor(inputSource));
await decryptNano(nanoClient, inputSource, dfn);
});
break;
}
}
await Promise.all(promises.map(limit));

if (memory.length) {
setInputSources(memory);
}
return false;

async function decryptTdf(client: TDF3Client, inputSource: InputSource, dfn: string) {
Expand Down

0 comments on commit c6027f5

Please sign in to comment.