-
Notifications
You must be signed in to change notification settings - Fork 58
demo: Vite Web Encryption Example #538
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # PowerSync Web Encryption example | ||
|
|
||
| This is a minimal example demonstrating web encryption. It prompts an encryption key, which is used for the database. | ||
| If the database doesn't exist yet, it will apply the encryption key when creating the database for the specified database name. | ||
| If the database does exist, it will use the encryption key to access the database - should the provided key be different to the one provided upon creation it fail to open the database. | ||
|
|
||
| To see it in action: | ||
|
|
||
| 1. Make sure to run `pnpm install` and `pnpm build:packages` in the root directory of this repo. | ||
| 2. `cd` into this directory, and run `pnpm start`. | ||
| 3. Open the localhost URL displayed in the terminal output in your browser. | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "name": "example-vite-encryption", | ||
| "private": true, | ||
| "version": "0.0.1", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite", | ||
| "build": "vite build", | ||
| "preview": "vite preview", | ||
| "start": "pnpm build && pnpm preview", | ||
| "test:build": "pnpm build" | ||
| }, | ||
| "dependencies": { | ||
| "@powersync/web": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "@swc/core": "~1.6.0", | ||
| "vite": "^5.0.12", | ||
| "vite-plugin-top-level-await": "^1.4.1", | ||
| "vite-plugin-wasm": "^3.3.0" | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <script type="module" src="./index.js"></script> | ||
| </head> | ||
| <body> | ||
| Vite bundling test: Check the console to see it in action! | ||
| </body> | ||
| </html> |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { column, Schema, Table, PowerSyncDatabase } from '@powersync/web'; | ||
| import Logger from 'js-logger'; | ||
|
|
||
| Logger.useDefaults(); | ||
|
|
||
| const customers = new Table({ name: column.text }); | ||
|
|
||
| export const AppSchema = new Schema({ customers }); | ||
|
|
||
| let PowerSync; | ||
|
|
||
| const openDatabase = async (encryptionKey) => { | ||
| PowerSync = new PowerSyncDatabase({ | ||
| schema: AppSchema, | ||
| database: { dbFilename: 'example-encryption.db' }, | ||
| encryptionKey: encryptionKey | ||
| }); | ||
|
|
||
| await PowerSync.init(); | ||
|
|
||
| // Run local statements. | ||
| await PowerSync.execute('INSERT INTO customers(id, name) VALUES(uuid(), ?)', ['Fred']); | ||
|
|
||
| const result = await PowerSync.getAll('SELECT * FROM customers'); | ||
| console.log('contents of customers: ', result); | ||
| console.table(result); | ||
| }; | ||
|
|
||
| document.addEventListener('DOMContentLoaded', async (event) => { | ||
| let encryptionKey = ''; | ||
|
|
||
| while (!encryptionKey) { | ||
| encryptionKey = prompt('Enter encryption key (non-empty string):'); | ||
| } | ||
|
|
||
| openDatabase(encryptionKey); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import wasm from 'vite-plugin-wasm'; | ||
| import topLevelAwait from 'vite-plugin-top-level-await'; | ||
| import { defineConfig } from 'vite'; | ||
|
|
||
| // https://vitejs.dev/config/ | ||
| export default defineConfig({ | ||
| root: 'src', | ||
| build: { | ||
| outDir: '../dist', | ||
| rollupOptions: { | ||
| input: 'src/index.html' | ||
| }, | ||
| emptyOutDir: true | ||
| }, | ||
| envDir: '..', // Use this dir for env vars, not 'src'. | ||
| optimizeDeps: { | ||
| // Don't optimize these packages as they contain web workers and WASM files. | ||
| // https://github.com/vitejs/vite/issues/11672#issuecomment-1415820673 | ||
| exclude: ['@journeyapps/wa-sqlite', '@powersync/web'], | ||
| include: ['@powersync/web > js-logger'] | ||
| }, | ||
| plugins: [wasm(), topLevelAwait()], | ||
| worker: { | ||
| format: 'es', | ||
| plugins: () => [wasm(), topLevelAwait()] | ||
| } | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.