Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation and utility for importing native Files #232

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [With Yarn/NPM](#with-yarnnpm)
- [Vanilla JS](#vanilla-js)
- [RemoteResource](#remoteresource)
- [PenumbraFile](#penumbrafile)
- [.get](#get)
- [.encrypt](#encrypt)
- [.encrypt() examples:](#encrypt-examples)
Expand Down Expand Up @@ -134,6 +135,35 @@ type RemoteResource = {
};
```

### PenumbraFile

Encryption & decryption APIs work on PenumbraFile descriptors to that store file data and metadata.

```ts
/** Penumbra file composition */
export interface PenumbraFile extends Omit<RemoteResource, 'url'> {
/** Backing stream */
stream: ReadableStream;
/** File size (if backed by a ReadableStream) */
size?: number;
/** Optional ID for tracking encryption completion */
id?: number | string;
/** Last modified date */
lastModified?: Date;
}
```

Native File objects can be converted for use with penumbra APIs through `penumbra.importFile(file: File, path?: string)` and can be used to encrypt and decrypt files.

```ts
// Automatically encrypt & save files selected in a file selector
const fileSelector = document.getElementById('file-selector');
fileSelector.addEventListener('change', (event) => {
const file = await penumbra.importFile(event.target.files[0]);
penumbra.encrypt(file).then(penumbra.save);
});
```

### .get

Fetch and decrypt remote files.
Expand Down Expand Up @@ -346,7 +376,7 @@ await writer.close();

### .setWorkerLocation

Configure the location of Penumbra's worker threads.
Configure the location of Penumbra's worker threads. This should be called before any other Penumbra methods and is not currently reconfigurable post-initialization.

```ts
penumbra.setWorkerLocation(location: WorkerLocationOptions | string): Promise<void>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@transcend-io/penumbra",
"version": "5.3.2",
"version": "5.4.1",
"description": "Crypto streams for the browser.",
"main": "dist/main.penumbra.js",
"types": "ts-build/src/index.d.ts",
Expand Down
17 changes: 17 additions & 0 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,26 @@ function getTextOrURI(files: PenumbraFile[]): Promise<PenumbraTextOrURI>[] {
});
}

/**
* Convert HTML File object to a PenumbraFile
*
* @param file - File to import
* @param path - File path
* @returns PenumbraFile
*/
async function importFile(file: File, path?: string): Promise<PenumbraFile> {
return {
stream: new Response(await file.arrayBuffer()).body as ReadableStream,
size: file.size,
mimetype: file.type,
path,
};
}

const penumbra = {
preconnect,
preload,
importFile,
get,
encrypt,
decrypt,
Expand Down
6 changes: 6 additions & 0 deletions src/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const supported = (): PenumbraSupportLevel => -0;
supported.levels = PenumbraSupportLevel;

const MOCK_API: PenumbraAPI = {
importFile: async (file, path) => ({
stream: new Response(await file.arrayBuffer()).body as ReadableStream,
size: file.size,
mimetype: file.type,
path,
}),
get: async () => [],
save: () => new AbortController(),
supported,
Expand Down