@evolu/web 1.0.0-preview.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +13 -0
- package/dist/Evolu/AppState.d.ts +3 -0
- package/dist/Evolu/AppState.d.ts.map +1 -0
- package/dist/Evolu/AppState.js +15 -0
- package/dist/Evolu/Db.worker.d.ts +2 -0
- package/dist/Evolu/Db.worker.d.ts.map +1 -0
- package/dist/Evolu/Db.worker.js +15 -0
- package/dist/Evolu/index.d.ts +3 -0
- package/dist/Evolu/index.d.ts.map +1 -0
- package/dist/Evolu/index.js +13 -0
- package/dist/SharedWebWorker.d.ts +30 -0
- package/dist/SharedWebWorker.d.ts.map +1 -0
- package/dist/SharedWebWorker.js +104 -0
- package/dist/WasmSqliteDriver.d.ts +3 -0
- package/dist/WasmSqliteDriver.d.ts.map +1 -0
- package/dist/WasmSqliteDriver.js +60 -0
- package/dist/WebWorker.d.ts +17 -0
- package/dist/WebWorker.d.ts.map +1 -0
- package/dist/WebWorker.js +42 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/package.json +57 -0
- package/src/Evolu/AppState.ts +19 -0
- package/src/Evolu/Db.worker.ts +27 -0
- package/src/Evolu/index.ts +26 -0
- package/src/SharedWebWorker.ts +149 -0
- package/src/WasmSqliteDriver.ts +83 -0
- package/src/WebWorker.ts +54 -0
- package/src/index.ts +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Evolu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Evolu for Web
|
|
2
|
+
|
|
3
|
+
## Documentation
|
|
4
|
+
|
|
5
|
+
For detailed information and usage examples, please visit [evolu.dev](https://www.evolu.dev).
|
|
6
|
+
|
|
7
|
+
## Community
|
|
8
|
+
|
|
9
|
+
The Evolu community is on [GitHub Discussions](https://github.com/evoluhq/evolu/discussions), where you can ask questions and voice ideas.
|
|
10
|
+
|
|
11
|
+
To chat with other community members, you can join the [Evolu Discord](https://discord.gg/2J8yyyyxtZ).
|
|
12
|
+
|
|
13
|
+
[](https://twitter.com/evoluhq)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AppState.d.ts","sourceRoot":"","sources":["../../src/Evolu/AppState.ts"],"names":[],"mappings":"AACA,OAAO,EAAY,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE/D,eAAO,MAAM,cAAc,EAAE,cAe5B,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { constVoid } from "@evolu/common";
|
|
2
|
+
export const createAppState = (config) => {
|
|
3
|
+
if (typeof document === "undefined") {
|
|
4
|
+
const appState = {
|
|
5
|
+
reset: constVoid,
|
|
6
|
+
};
|
|
7
|
+
return appState;
|
|
8
|
+
}
|
|
9
|
+
const appState = {
|
|
10
|
+
reset: () => {
|
|
11
|
+
location.replace(config.reloadUrl);
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
return appState;
|
|
15
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Db.worker.d.ts","sourceRoot":"","sources":["../../src/Evolu/Db.worker.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createConsole, createEnglishMnemonic, createNanoIdLib, createRandom, createRandomBytes, createTime, } from "@evolu/common";
|
|
2
|
+
import { createDbWorkerForPlatform, createWebSocketSync, } from "@evolu/common/evolu";
|
|
3
|
+
import { createWasmSqliteDriver } from "../WasmSqliteDriver.js";
|
|
4
|
+
import { wrapWebWorkerSelf } from "../WebWorker.js";
|
|
5
|
+
const dbWorker = createDbWorkerForPlatform({
|
|
6
|
+
createSqliteDriver: createWasmSqliteDriver,
|
|
7
|
+
createSync: createWebSocketSync,
|
|
8
|
+
console: createConsole(),
|
|
9
|
+
time: createTime(),
|
|
10
|
+
random: createRandom(),
|
|
11
|
+
nanoIdLib: createNanoIdLib(),
|
|
12
|
+
createMnemonic: createEnglishMnemonic,
|
|
13
|
+
createRandomBytes,
|
|
14
|
+
});
|
|
15
|
+
wrapWebWorkerSelf(dbWorker);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/Evolu/index.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,SAAS,EACV,MAAM,qBAAqB,CAAC;AAa7B,eAAO,MAAM,YAAY,EAAE,SAM1B,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createConsole, createNanoIdLib, createTime } from "@evolu/common";
|
|
2
|
+
import { createSharedWebWorker } from "../SharedWebWorker.js";
|
|
3
|
+
import { createAppState } from "./AppState.js";
|
|
4
|
+
const createDbWorker = (name) => createSharedWebWorker(name, () => new Worker(new URL("Db.worker.js", import.meta.url), {
|
|
5
|
+
type: "module",
|
|
6
|
+
}));
|
|
7
|
+
export const evoluWebDeps = {
|
|
8
|
+
time: createTime(),
|
|
9
|
+
console: createConsole(),
|
|
10
|
+
nanoIdLib: createNanoIdLib(),
|
|
11
|
+
createAppState,
|
|
12
|
+
createDbWorker,
|
|
13
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SharedWebWorker (WebWorker + BroadcastChannel + Web Locks)
|
|
3
|
+
*
|
|
4
|
+
* A SharedWebWorker is a Web Worker that is shared across multiple browser
|
|
5
|
+
* tabs. This implementation provides a shared worker-like experience even in
|
|
6
|
+
* browsers that do not support the native SharedWorker API.
|
|
7
|
+
*
|
|
8
|
+
* Unlike a true SharedWorker (which uses MessagePorts for direct, tab-specific
|
|
9
|
+
* communication), this approach uses BroadcastChannel for cross-tab messaging
|
|
10
|
+
* and Web Locks to ensure only one tab owns and runs the actual Worker
|
|
11
|
+
* instance.
|
|
12
|
+
*
|
|
13
|
+
* All tabs communicate via BroadcastChannel, so every message is broadcast to
|
|
14
|
+
* all tabs, and each tab must filter/process only relevant messages. This is
|
|
15
|
+
* less efficient than MessagePorts, but it works everywhere and is "good
|
|
16
|
+
* enough" for most use cases.
|
|
17
|
+
*
|
|
18
|
+
* See the protocol and coordination logic below for details.
|
|
19
|
+
*
|
|
20
|
+
* @module SharedWebWorker
|
|
21
|
+
*/
|
|
22
|
+
import { SimpleName, Worker } from "@evolu/common";
|
|
23
|
+
/**
|
|
24
|
+
* Creates a shared Web Worker using BroadcastChannel and Web Locks. This allows
|
|
25
|
+
* multiple tabs to share a single Web Worker instance. The first tab to acquire
|
|
26
|
+
* the lock becomes the owner and runs the worker. Other tabs act as proxies,
|
|
27
|
+
* forwarding messages to the owner.
|
|
28
|
+
*/
|
|
29
|
+
export declare const createSharedWebWorker: <Input, Output>(name: SimpleName, createWebWorker: () => globalThis.Worker) => Worker<Input, Output>;
|
|
30
|
+
//# sourceMappingURL=SharedWebWorker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SharedWebWorker.d.ts","sourceRoot":"","sources":["../src/SharedWebWorker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAa,UAAU,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AA8B9D;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,GAAI,KAAK,EAAE,MAAM,EACjD,MAAM,UAAU,EAChB,iBAAiB,MAAM,UAAU,CAAC,MAAM,KACvC,MAAM,CAAC,KAAK,EAAE,MAAM,CAuFtB,CAAC"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SharedWebWorker (WebWorker + BroadcastChannel + Web Locks)
|
|
3
|
+
*
|
|
4
|
+
* A SharedWebWorker is a Web Worker that is shared across multiple browser
|
|
5
|
+
* tabs. This implementation provides a shared worker-like experience even in
|
|
6
|
+
* browsers that do not support the native SharedWorker API.
|
|
7
|
+
*
|
|
8
|
+
* Unlike a true SharedWorker (which uses MessagePorts for direct, tab-specific
|
|
9
|
+
* communication), this approach uses BroadcastChannel for cross-tab messaging
|
|
10
|
+
* and Web Locks to ensure only one tab owns and runs the actual Worker
|
|
11
|
+
* instance.
|
|
12
|
+
*
|
|
13
|
+
* All tabs communicate via BroadcastChannel, so every message is broadcast to
|
|
14
|
+
* all tabs, and each tab must filter/process only relevant messages. This is
|
|
15
|
+
* less efficient than MessagePorts, but it works everywhere and is "good
|
|
16
|
+
* enough" for most use cases.
|
|
17
|
+
*
|
|
18
|
+
* See the protocol and coordination logic below for details.
|
|
19
|
+
*
|
|
20
|
+
* @module SharedWebWorker
|
|
21
|
+
*/
|
|
22
|
+
import { constVoid } from "@evolu/common";
|
|
23
|
+
/**
|
|
24
|
+
* Creates a shared Web Worker using BroadcastChannel and Web Locks. This allows
|
|
25
|
+
* multiple tabs to share a single Web Worker instance. The first tab to acquire
|
|
26
|
+
* the lock becomes the owner and runs the worker. Other tabs act as proxies,
|
|
27
|
+
* forwarding messages to the owner.
|
|
28
|
+
*/
|
|
29
|
+
export const createSharedWebWorker = (name, createWebWorker) => {
|
|
30
|
+
// Server.
|
|
31
|
+
if (typeof document === "undefined")
|
|
32
|
+
return {
|
|
33
|
+
postMessage: constVoid,
|
|
34
|
+
onMessage: constVoid,
|
|
35
|
+
};
|
|
36
|
+
const namespacedName = `evolu-sharedwebworker-${name}`;
|
|
37
|
+
const channel = new BroadcastChannel(namespacedName);
|
|
38
|
+
let worker;
|
|
39
|
+
let onMessageCallback;
|
|
40
|
+
let ownerReady = false;
|
|
41
|
+
const pendingMessages = [];
|
|
42
|
+
// Listen for owner-ready and worker responses
|
|
43
|
+
channel.onmessage = (event) => {
|
|
44
|
+
const data = event.data;
|
|
45
|
+
if (data.type === "owner-ready") {
|
|
46
|
+
ownerReady = true;
|
|
47
|
+
// Flush pending messages to the new owner
|
|
48
|
+
for (const message of pendingMessages) {
|
|
49
|
+
channel.postMessage({ type: "to-worker", message });
|
|
50
|
+
}
|
|
51
|
+
pendingMessages.length = 0;
|
|
52
|
+
}
|
|
53
|
+
else if (!worker && data.type === "from-worker") {
|
|
54
|
+
onMessageCallback?.(data.message);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
// Request owner-ready when not the owner
|
|
58
|
+
channel.postMessage({ type: "request-owner-ready" });
|
|
59
|
+
// Try to acquire the lock and become the owner
|
|
60
|
+
void navigator.locks.request(namespacedName, async () => {
|
|
61
|
+
worker = createWebWorker();
|
|
62
|
+
// Flush pending messages to the worker
|
|
63
|
+
for (const message of pendingMessages) {
|
|
64
|
+
worker.postMessage(message);
|
|
65
|
+
}
|
|
66
|
+
pendingMessages.length = 0;
|
|
67
|
+
// Forward messages from channel to worker
|
|
68
|
+
channel.onmessage = (event) => {
|
|
69
|
+
const data = event.data;
|
|
70
|
+
if (data.type === "to-worker") {
|
|
71
|
+
worker.postMessage(data.message);
|
|
72
|
+
}
|
|
73
|
+
else if (data.type === "request-owner-ready") {
|
|
74
|
+
// Respond to request
|
|
75
|
+
channel.postMessage({ type: "owner-ready" });
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
// Forward messages from worker to channel
|
|
79
|
+
worker.onmessage = (event) => {
|
|
80
|
+
channel.postMessage({ type: "from-worker", message: event.data });
|
|
81
|
+
onMessageCallback?.(event.data);
|
|
82
|
+
};
|
|
83
|
+
// Announce ownership
|
|
84
|
+
channel.postMessage({ type: "owner-ready" });
|
|
85
|
+
// Hold the lock forever
|
|
86
|
+
await new Promise(constVoid);
|
|
87
|
+
});
|
|
88
|
+
return {
|
|
89
|
+
postMessage: (message) => {
|
|
90
|
+
if (worker) {
|
|
91
|
+
worker.postMessage(message);
|
|
92
|
+
}
|
|
93
|
+
else if (ownerReady) {
|
|
94
|
+
channel.postMessage({ type: "to-worker", message });
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
pendingMessages.push(message);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
onMessage: (callback) => {
|
|
101
|
+
onMessageCallback = callback;
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WasmSqliteDriver.d.ts","sourceRoot":"","sources":["../src/WasmSqliteDriver.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,kBAAkB,EAGnB,MAAM,eAAe,CAAC;AAavB,eAAO,MAAM,sBAAsB,EAAE,kBA+DpC,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { constVoid, createPreparedStatementsCache, } from "@evolu/common";
|
|
2
|
+
import sqlite3InitModule from "@sqlite.org/sqlite-wasm";
|
|
3
|
+
// TODO: Do we still need that?
|
|
4
|
+
// https://github.com/sqlite/sqlite-wasm/issues/62
|
|
5
|
+
// @ts-expect-error Missing types.
|
|
6
|
+
globalThis.sqlite3ApiConfig = {
|
|
7
|
+
warn: constVoid,
|
|
8
|
+
};
|
|
9
|
+
// Init ASAP.
|
|
10
|
+
const sqlite3Promise = sqlite3InitModule();
|
|
11
|
+
export const createWasmSqliteDriver = async (name) => {
|
|
12
|
+
const sqlite3 = await sqlite3Promise;
|
|
13
|
+
const poolUtil = await sqlite3.installOpfsSAHPoolVfs({ name });
|
|
14
|
+
const db = new poolUtil.OpfsSAHPoolDb("/evolu1.db");
|
|
15
|
+
let isDisposed = false;
|
|
16
|
+
const cache = createPreparedStatementsCache((sql) => db.prepare(sql), (statement) => {
|
|
17
|
+
statement.finalize();
|
|
18
|
+
});
|
|
19
|
+
const driver = {
|
|
20
|
+
exec: (query, isMutation) => {
|
|
21
|
+
const prepared = cache.get(query);
|
|
22
|
+
if (prepared) {
|
|
23
|
+
prepared.bind(query.parameters);
|
|
24
|
+
if (isMutation) {
|
|
25
|
+
prepared.stepReset();
|
|
26
|
+
return {
|
|
27
|
+
rows: [],
|
|
28
|
+
changes: db.changes(),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const rows = [];
|
|
32
|
+
while (prepared.step()) {
|
|
33
|
+
rows.push(prepared.get({}));
|
|
34
|
+
}
|
|
35
|
+
prepared.reset();
|
|
36
|
+
return {
|
|
37
|
+
rows: rows,
|
|
38
|
+
changes: 0,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const rows = db.exec(query.sql, {
|
|
42
|
+
returnValue: "resultRows",
|
|
43
|
+
rowMode: "object",
|
|
44
|
+
bind: query.parameters,
|
|
45
|
+
});
|
|
46
|
+
const changes = db.changes();
|
|
47
|
+
return { rows, changes };
|
|
48
|
+
},
|
|
49
|
+
export: () => sqlite3.capi.sqlite3_js_db_export(db),
|
|
50
|
+
[Symbol.dispose]: () => {
|
|
51
|
+
if (isDisposed)
|
|
52
|
+
return;
|
|
53
|
+
isDisposed = true;
|
|
54
|
+
// poolUtil.unlink?
|
|
55
|
+
cache[Symbol.dispose]();
|
|
56
|
+
db.close();
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
return driver;
|
|
60
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed wrapper for Web Worker
|
|
3
|
+
*
|
|
4
|
+
* While Comlink DX is nice, there are still memory leaks and unresolved bugs.
|
|
5
|
+
* Use plain Web Worker with a typed wrapper if maximum performance and
|
|
6
|
+
* reliability are required.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import { Worker } from "@evolu/common";
|
|
11
|
+
/**
|
|
12
|
+
* Wraps a Web Worker to provide a typed interface for sending and receiving
|
|
13
|
+
* messages.
|
|
14
|
+
*/
|
|
15
|
+
export declare const wrapWebWorker: <Input, Output>(createWebWorker: () => globalThis.Worker) => Worker<Input, Output>;
|
|
16
|
+
export declare const wrapWebWorkerSelf: <Input, Output>(worker: Worker<Input, Output>) => void;
|
|
17
|
+
//# sourceMappingURL=WebWorker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebWorker.d.ts","sourceRoot":"","sources":["../src/WebWorker.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAa,MAAM,EAAE,MAAM,eAAe,CAAC;AAElD;;;GAGG;AACH,eAAO,MAAM,aAAa,GAAI,KAAK,EAAE,MAAM,EACzC,iBAAiB,MAAM,UAAU,CAAC,MAAM,KACvC,MAAM,CAAC,KAAK,EAAE,MAAM,CAuBtB,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAAI,KAAK,EAAE,MAAM,EAC7C,QAAQ,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,KAC5B,IAQF,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed wrapper for Web Worker
|
|
3
|
+
*
|
|
4
|
+
* While Comlink DX is nice, there are still memory leaks and unresolved bugs.
|
|
5
|
+
* Use plain Web Worker with a typed wrapper if maximum performance and
|
|
6
|
+
* reliability are required.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import { constVoid } from "@evolu/common";
|
|
11
|
+
/**
|
|
12
|
+
* Wraps a Web Worker to provide a typed interface for sending and receiving
|
|
13
|
+
* messages.
|
|
14
|
+
*/
|
|
15
|
+
export const wrapWebWorker = (createWebWorker) => {
|
|
16
|
+
// Server.
|
|
17
|
+
if (typeof document === "undefined")
|
|
18
|
+
return {
|
|
19
|
+
postMessage: constVoid,
|
|
20
|
+
onMessage: constVoid,
|
|
21
|
+
};
|
|
22
|
+
const webWorker = createWebWorker();
|
|
23
|
+
const worker = {
|
|
24
|
+
postMessage: (message) => {
|
|
25
|
+
webWorker.postMessage(message);
|
|
26
|
+
},
|
|
27
|
+
onMessage: (callback) => {
|
|
28
|
+
webWorker.onmessage = (event) => {
|
|
29
|
+
callback(event.data);
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
return worker;
|
|
34
|
+
};
|
|
35
|
+
export const wrapWebWorkerSelf = (worker) => {
|
|
36
|
+
worker.onMessage((message) => {
|
|
37
|
+
postMessage(message);
|
|
38
|
+
});
|
|
39
|
+
self.onmessage = (event) => {
|
|
40
|
+
worker.postMessage(event.data);
|
|
41
|
+
};
|
|
42
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@evolu/web",
|
|
3
|
+
"version": "1.0.0-preview.0",
|
|
4
|
+
"description": "Evolu for web",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"evolu",
|
|
7
|
+
"web"
|
|
8
|
+
],
|
|
9
|
+
"author": "Daniel Steigerwald <daniel@steigerwald.cz>",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": "evoluhq/evolu",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/evoluhq/evolu/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://evolu.dev",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"browser": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist/**",
|
|
27
|
+
"src/**",
|
|
28
|
+
"README.md"
|
|
29
|
+
],
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@sqlite.org/sqlite-wasm": "3.50.0-build1"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/web-locks-api": "^0.0.5",
|
|
35
|
+
"typescript": "^5.8.3",
|
|
36
|
+
"user-agent-data-types": "^0.4.2",
|
|
37
|
+
"vitest": "^3.2.2",
|
|
38
|
+
"@evolu/common": "6.0.0-preview.0",
|
|
39
|
+
"@evolu/tsconfig": "0.0.2"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@evolu/common": "^6.0.0-preview.0"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22.0.0"
|
|
49
|
+
},
|
|
50
|
+
"sideEffects": [],
|
|
51
|
+
"scripts": {
|
|
52
|
+
"dev": "tsc --watch",
|
|
53
|
+
"build": "shx rm -rf dist && tsc",
|
|
54
|
+
"clean": "shx rm -rf .turbo node_modules dist",
|
|
55
|
+
"format": "prettier --write \"src/*.{ts,tsx,md}\""
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { constVoid } from "@evolu/common";
|
|
2
|
+
import { AppState, CreateAppState } from "@evolu/common/evolu";
|
|
3
|
+
|
|
4
|
+
export const createAppState: CreateAppState = (config) => {
|
|
5
|
+
if (typeof document === "undefined") {
|
|
6
|
+
const appState: AppState = {
|
|
7
|
+
reset: constVoid,
|
|
8
|
+
};
|
|
9
|
+
return appState;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const appState: AppState = {
|
|
13
|
+
reset: () => {
|
|
14
|
+
location.replace(config.reloadUrl);
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
return appState;
|
|
19
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createConsole,
|
|
3
|
+
createEnglishMnemonic,
|
|
4
|
+
createNanoIdLib,
|
|
5
|
+
createRandom,
|
|
6
|
+
createRandomBytes,
|
|
7
|
+
createTime,
|
|
8
|
+
} from "@evolu/common";
|
|
9
|
+
import {
|
|
10
|
+
createDbWorkerForPlatform,
|
|
11
|
+
createWebSocketSync,
|
|
12
|
+
} from "@evolu/common/evolu";
|
|
13
|
+
import { createWasmSqliteDriver } from "../WasmSqliteDriver.js";
|
|
14
|
+
import { wrapWebWorkerSelf } from "../WebWorker.js";
|
|
15
|
+
|
|
16
|
+
const dbWorker = createDbWorkerForPlatform({
|
|
17
|
+
createSqliteDriver: createWasmSqliteDriver,
|
|
18
|
+
createSync: createWebSocketSync,
|
|
19
|
+
console: createConsole(),
|
|
20
|
+
time: createTime(),
|
|
21
|
+
random: createRandom(),
|
|
22
|
+
nanoIdLib: createNanoIdLib(),
|
|
23
|
+
createMnemonic: createEnglishMnemonic,
|
|
24
|
+
createRandomBytes,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
wrapWebWorkerSelf(dbWorker);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createConsole, createNanoIdLib, createTime } from "@evolu/common";
|
|
2
|
+
import {
|
|
3
|
+
CreateDbWorker,
|
|
4
|
+
DbWorkerInput,
|
|
5
|
+
DbWorkerOutput,
|
|
6
|
+
EvoluDeps,
|
|
7
|
+
} from "@evolu/common/evolu";
|
|
8
|
+
import { createSharedWebWorker } from "../SharedWebWorker.js";
|
|
9
|
+
import { createAppState } from "./AppState.js";
|
|
10
|
+
|
|
11
|
+
const createDbWorker: CreateDbWorker = (name) =>
|
|
12
|
+
createSharedWebWorker<DbWorkerInput, DbWorkerOutput>(
|
|
13
|
+
name,
|
|
14
|
+
() =>
|
|
15
|
+
new Worker(new URL("Db.worker.js", import.meta.url), {
|
|
16
|
+
type: "module",
|
|
17
|
+
}),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export const evoluWebDeps: EvoluDeps = {
|
|
21
|
+
time: createTime(),
|
|
22
|
+
console: createConsole(),
|
|
23
|
+
nanoIdLib: createNanoIdLib(),
|
|
24
|
+
createAppState,
|
|
25
|
+
createDbWorker,
|
|
26
|
+
};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SharedWebWorker (WebWorker + BroadcastChannel + Web Locks)
|
|
3
|
+
*
|
|
4
|
+
* A SharedWebWorker is a Web Worker that is shared across multiple browser
|
|
5
|
+
* tabs. This implementation provides a shared worker-like experience even in
|
|
6
|
+
* browsers that do not support the native SharedWorker API.
|
|
7
|
+
*
|
|
8
|
+
* Unlike a true SharedWorker (which uses MessagePorts for direct, tab-specific
|
|
9
|
+
* communication), this approach uses BroadcastChannel for cross-tab messaging
|
|
10
|
+
* and Web Locks to ensure only one tab owns and runs the actual Worker
|
|
11
|
+
* instance.
|
|
12
|
+
*
|
|
13
|
+
* All tabs communicate via BroadcastChannel, so every message is broadcast to
|
|
14
|
+
* all tabs, and each tab must filter/process only relevant messages. This is
|
|
15
|
+
* less efficient than MessagePorts, but it works everywhere and is "good
|
|
16
|
+
* enough" for most use cases.
|
|
17
|
+
*
|
|
18
|
+
* See the protocol and coordination logic below for details.
|
|
19
|
+
*
|
|
20
|
+
* @module SharedWebWorker
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { constVoid, SimpleName, Worker } from "@evolu/common";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Ownership protocol explanation:
|
|
27
|
+
*
|
|
28
|
+
* - When a tab becomes the owner (acquires the lock), it announces ownership by
|
|
29
|
+
* posting { type: "owner-ready" } to the BroadcastChannel. This notifies all
|
|
30
|
+
* currently open tabs.
|
|
31
|
+
* - Tabs that open later may miss this announcement, since BroadcastChannel does
|
|
32
|
+
* not replay messages. Therefore, new tabs request ownership status by
|
|
33
|
+
* posting { type: "request-owner-ready" }, and the owner responds with {
|
|
34
|
+
* type: "owner-ready" }.
|
|
35
|
+
* - Once a tab receives "owner-ready", it knows where to send messages and does
|
|
36
|
+
* not need to ask again.
|
|
37
|
+
* - This ensures all tabs, regardless of when they open, can reliably detect the
|
|
38
|
+
* current owner.
|
|
39
|
+
*
|
|
40
|
+
* Internal note:
|
|
41
|
+
*
|
|
42
|
+
* - Announcing ownership is for immediate notification of already-open tabs.
|
|
43
|
+
* - Requesting ownership is a fallback for tabs that open later and miss the
|
|
44
|
+
* announcement.
|
|
45
|
+
* - Both are required for robust cross-tab coordination.
|
|
46
|
+
*/
|
|
47
|
+
type SharedWebWorkerChannelMessage<Input, Output> =
|
|
48
|
+
| { type: "owner-ready" }
|
|
49
|
+
| { type: "request-owner-ready" }
|
|
50
|
+
| { type: "to-worker"; message: Input }
|
|
51
|
+
| { type: "from-worker"; message: Output };
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Creates a shared Web Worker using BroadcastChannel and Web Locks. This allows
|
|
55
|
+
* multiple tabs to share a single Web Worker instance. The first tab to acquire
|
|
56
|
+
* the lock becomes the owner and runs the worker. Other tabs act as proxies,
|
|
57
|
+
* forwarding messages to the owner.
|
|
58
|
+
*/
|
|
59
|
+
export const createSharedWebWorker = <Input, Output>(
|
|
60
|
+
name: SimpleName,
|
|
61
|
+
createWebWorker: () => globalThis.Worker,
|
|
62
|
+
): Worker<Input, Output> => {
|
|
63
|
+
// Server.
|
|
64
|
+
if (typeof document === "undefined")
|
|
65
|
+
return {
|
|
66
|
+
postMessage: constVoid,
|
|
67
|
+
onMessage: constVoid,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const namespacedName = `evolu-sharedwebworker-${name}`;
|
|
71
|
+
const channel = new BroadcastChannel(namespacedName);
|
|
72
|
+
|
|
73
|
+
let worker: globalThis.Worker | undefined;
|
|
74
|
+
let onMessageCallback: ((message: Output) => void) | undefined;
|
|
75
|
+
let ownerReady = false;
|
|
76
|
+
|
|
77
|
+
const pendingMessages: Array<Input> = [];
|
|
78
|
+
|
|
79
|
+
// Listen for owner-ready and worker responses
|
|
80
|
+
channel.onmessage = (
|
|
81
|
+
event: MessageEvent<SharedWebWorkerChannelMessage<Input, Output>>,
|
|
82
|
+
) => {
|
|
83
|
+
const data = event.data;
|
|
84
|
+
if (data.type === "owner-ready") {
|
|
85
|
+
ownerReady = true;
|
|
86
|
+
// Flush pending messages to the new owner
|
|
87
|
+
for (const message of pendingMessages) {
|
|
88
|
+
channel.postMessage({ type: "to-worker", message });
|
|
89
|
+
}
|
|
90
|
+
pendingMessages.length = 0;
|
|
91
|
+
} else if (!worker && data.type === "from-worker") {
|
|
92
|
+
onMessageCallback?.(data.message);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// Request owner-ready when not the owner
|
|
97
|
+
channel.postMessage({ type: "request-owner-ready" });
|
|
98
|
+
|
|
99
|
+
// Try to acquire the lock and become the owner
|
|
100
|
+
void navigator.locks.request(namespacedName, async () => {
|
|
101
|
+
worker = createWebWorker();
|
|
102
|
+
|
|
103
|
+
// Flush pending messages to the worker
|
|
104
|
+
for (const message of pendingMessages) {
|
|
105
|
+
worker.postMessage(message);
|
|
106
|
+
}
|
|
107
|
+
pendingMessages.length = 0;
|
|
108
|
+
|
|
109
|
+
// Forward messages from channel to worker
|
|
110
|
+
channel.onmessage = (
|
|
111
|
+
event: MessageEvent<SharedWebWorkerChannelMessage<Input, Output>>,
|
|
112
|
+
) => {
|
|
113
|
+
const data = event.data;
|
|
114
|
+
if (data.type === "to-worker") {
|
|
115
|
+
worker!.postMessage(data.message);
|
|
116
|
+
} else if (data.type === "request-owner-ready") {
|
|
117
|
+
// Respond to request
|
|
118
|
+
channel.postMessage({ type: "owner-ready" });
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// Forward messages from worker to channel
|
|
123
|
+
worker.onmessage = (event: MessageEvent<Output>) => {
|
|
124
|
+
channel.postMessage({ type: "from-worker", message: event.data });
|
|
125
|
+
onMessageCallback?.(event.data);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// Announce ownership
|
|
129
|
+
channel.postMessage({ type: "owner-ready" });
|
|
130
|
+
|
|
131
|
+
// Hold the lock forever
|
|
132
|
+
await new Promise(constVoid);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
postMessage: (message: Input) => {
|
|
137
|
+
if (worker) {
|
|
138
|
+
worker.postMessage(message);
|
|
139
|
+
} else if (ownerReady) {
|
|
140
|
+
channel.postMessage({ type: "to-worker", message });
|
|
141
|
+
} else {
|
|
142
|
+
pendingMessages.push(message);
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
onMessage: (callback) => {
|
|
146
|
+
onMessageCallback = callback;
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {
|
|
2
|
+
constVoid,
|
|
3
|
+
createPreparedStatementsCache,
|
|
4
|
+
CreateSqliteDriver,
|
|
5
|
+
SqliteDriver,
|
|
6
|
+
SqliteRow,
|
|
7
|
+
} from "@evolu/common";
|
|
8
|
+
import sqlite3InitModule, { PreparedStatement } from "@sqlite.org/sqlite-wasm";
|
|
9
|
+
|
|
10
|
+
// TODO: Do we still need that?
|
|
11
|
+
// https://github.com/sqlite/sqlite-wasm/issues/62
|
|
12
|
+
// @ts-expect-error Missing types.
|
|
13
|
+
globalThis.sqlite3ApiConfig = {
|
|
14
|
+
warn: constVoid,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// Init ASAP.
|
|
18
|
+
const sqlite3Promise = sqlite3InitModule();
|
|
19
|
+
|
|
20
|
+
export const createWasmSqliteDriver: CreateSqliteDriver = async (name) => {
|
|
21
|
+
const sqlite3 = await sqlite3Promise;
|
|
22
|
+
const poolUtil = await sqlite3.installOpfsSAHPoolVfs({ name });
|
|
23
|
+
const db = new poolUtil.OpfsSAHPoolDb("/evolu1.db");
|
|
24
|
+
let isDisposed = false;
|
|
25
|
+
|
|
26
|
+
const cache = createPreparedStatementsCache<PreparedStatement>(
|
|
27
|
+
(sql) => db.prepare(sql),
|
|
28
|
+
(statement) => {
|
|
29
|
+
statement.finalize();
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const driver: SqliteDriver = {
|
|
34
|
+
exec: (query, isMutation) => {
|
|
35
|
+
const prepared = cache.get(query);
|
|
36
|
+
|
|
37
|
+
if (prepared) {
|
|
38
|
+
prepared.bind(query.parameters);
|
|
39
|
+
|
|
40
|
+
if (isMutation) {
|
|
41
|
+
prepared.stepReset();
|
|
42
|
+
return {
|
|
43
|
+
rows: [] as ReadonlyArray<SqliteRow>,
|
|
44
|
+
changes: db.changes(),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const rows = [];
|
|
49
|
+
while (prepared.step()) {
|
|
50
|
+
rows.push(prepared.get({}));
|
|
51
|
+
}
|
|
52
|
+
prepared.reset();
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
rows: rows as ReadonlyArray<SqliteRow>,
|
|
56
|
+
changes: 0,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const rows = db.exec(query.sql, {
|
|
61
|
+
returnValue: "resultRows",
|
|
62
|
+
rowMode: "object",
|
|
63
|
+
bind: query.parameters,
|
|
64
|
+
}) as ReadonlyArray<SqliteRow>;
|
|
65
|
+
|
|
66
|
+
const changes = db.changes();
|
|
67
|
+
|
|
68
|
+
return { rows, changes };
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
export: () => sqlite3.capi.sqlite3_js_db_export(db),
|
|
72
|
+
|
|
73
|
+
[Symbol.dispose]: () => {
|
|
74
|
+
if (isDisposed) return;
|
|
75
|
+
isDisposed = true;
|
|
76
|
+
// poolUtil.unlink?
|
|
77
|
+
cache[Symbol.dispose]();
|
|
78
|
+
db.close();
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return driver;
|
|
83
|
+
};
|
package/src/WebWorker.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed wrapper for Web Worker
|
|
3
|
+
*
|
|
4
|
+
* While Comlink DX is nice, there are still memory leaks and unresolved bugs.
|
|
5
|
+
* Use plain Web Worker with a typed wrapper if maximum performance and
|
|
6
|
+
* reliability are required.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { constVoid, Worker } from "@evolu/common";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Wraps a Web Worker to provide a typed interface for sending and receiving
|
|
15
|
+
* messages.
|
|
16
|
+
*/
|
|
17
|
+
export const wrapWebWorker = <Input, Output>(
|
|
18
|
+
createWebWorker: () => globalThis.Worker,
|
|
19
|
+
): Worker<Input, Output> => {
|
|
20
|
+
// Server.
|
|
21
|
+
if (typeof document === "undefined")
|
|
22
|
+
return {
|
|
23
|
+
postMessage: constVoid,
|
|
24
|
+
onMessage: constVoid,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const webWorker = createWebWorker();
|
|
28
|
+
|
|
29
|
+
const worker: Worker<Input, Output> = {
|
|
30
|
+
postMessage: (message) => {
|
|
31
|
+
webWorker.postMessage(message);
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
onMessage: (callback) => {
|
|
35
|
+
webWorker.onmessage = (event: MessageEvent<Output>) => {
|
|
36
|
+
callback(event.data);
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
return worker;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const wrapWebWorkerSelf = <Input, Output>(
|
|
45
|
+
worker: Worker<Input, Output>,
|
|
46
|
+
): void => {
|
|
47
|
+
worker.onMessage((message) => {
|
|
48
|
+
postMessage(message);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
self.onmessage = (event: MessageEvent<Input>) => {
|
|
52
|
+
worker.postMessage(event.data);
|
|
53
|
+
};
|
|
54
|
+
};
|
package/src/index.ts
ADDED