Skip to content

Commit e801fc4

Browse files
committed
Encryption demo init.
1 parent f0c49f9 commit e801fc4

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# PowerSync Vite bundling test
2+
3+
This is a minimal example demonstrating bundling with Vite. It attempts a connection to verify that web workers load correctly, but networks requests will fail since no credentials are configured. See [src/index.js](src/index.js) for details.
4+
5+
To see it in action:
6+
7+
1. Make sure to run `pnpm install` and `pnpm build:packages` in the root directory of this repo.
8+
2. `cd` into this directory, and run `pnpm start`.
9+
3. Open the localhost URL displayed in the terminal output in your browser.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "example-vite",
3+
"private": true,
4+
"version": "0.0.33",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"preview": "vite preview",
10+
"start": "pnpm build && pnpm preview",
11+
"test:build": "pnpm build"
12+
},
13+
"dependencies": {
14+
"@powersync/web": "workspace:*"
15+
},
16+
"devDependencies": {
17+
"@swc/core": "~1.6.0",
18+
"vite": "^5.0.12",
19+
"vite-plugin-top-level-await": "^1.4.1",
20+
"vite-plugin-wasm": "^3.3.0"
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<script type="module" src="./index.js"></script>
5+
</head>
6+
<body>
7+
Vite bundling test: Check the console to see it in action!
8+
</body>
9+
</html>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { column, Schema, Table, PowerSyncDatabase, WASQLiteVFS } from '@powersync/web';
2+
import Logger from 'js-logger';
3+
4+
Logger.useDefaults();
5+
6+
/**
7+
* A placeholder connector which doesn't do anything.
8+
* This is just used to verify that the sync workers can be loaded
9+
* when connecting.
10+
*/
11+
class DummyConnector {
12+
async fetchCredentials() {
13+
return {
14+
endpoint: '',
15+
token: ''
16+
};
17+
}
18+
19+
async uploadData(database) {}
20+
}
21+
22+
const customers = new Table({ name: column.text });
23+
24+
export const AppSchema = new Schema({ customers });
25+
26+
let PowerSync;
27+
28+
const openDatabase = async (encryptionKey) => {
29+
PowerSync = new PowerSyncDatabase({
30+
schema: AppSchema,
31+
database: { dbFilename: 'example-encryption.db' },
32+
encryptionKey: encryptionKey
33+
});
34+
35+
await PowerSync.init();
36+
37+
// Run local statements.
38+
await PowerSync.execute('INSERT INTO customers(id, name) VALUES(uuid(), ?)', ['Fred']);
39+
40+
const result = await PowerSync.getAll('SELECT * FROM customers');
41+
console.log('contents of customers: ', result);
42+
43+
console.log(
44+
`Attempting to connect in order to verify web workers are correctly loaded.
45+
This doesn't use any actual network credentials.
46+
Network errors will be shown: these can be ignored.`
47+
);
48+
49+
/**
50+
* Try and connect, this will setup shared sync workers
51+
* This will fail due to not having a valid endpoint,
52+
* but it will try - which is all that matters.
53+
*/
54+
await PowerSync.connect(new DummyConnector());
55+
};
56+
57+
document.addEventListener('DOMContentLoaded', async (event) => {
58+
let encryptionKey = '';
59+
60+
while (!encryptionKey) {
61+
encryptionKey = prompt('Enter encryption key (non-empty string):');
62+
}
63+
64+
openDatabase(encryptionKey);
65+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import wasm from 'vite-plugin-wasm';
2+
import topLevelAwait from 'vite-plugin-top-level-await';
3+
import { defineConfig } from 'vite';
4+
5+
// https://vitejs.dev/config/
6+
export default defineConfig({
7+
root: 'src',
8+
build: {
9+
outDir: '../dist',
10+
rollupOptions: {
11+
input: 'src/index.html'
12+
},
13+
emptyOutDir: true
14+
},
15+
envDir: '..', // Use this dir for env vars, not 'src'.
16+
optimizeDeps: {
17+
// Don't optimize these packages as they contain web workers and WASM files.
18+
// https://github.com/vitejs/vite/issues/11672#issuecomment-1415820673
19+
exclude: ['@journeyapps/wa-sqlite', '@powersync/web'],
20+
include: ['@powersync/web > js-logger']
21+
},
22+
plugins: [wasm(), topLevelAwait()],
23+
worker: {
24+
format: 'es',
25+
plugins: () => [wasm(), topLevelAwait()]
26+
}
27+
});

0 commit comments

Comments
 (0)