@evolu/nodejs 2.4.0 → 3.0.0-next.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/README.md +4 -2
- package/dist/src/Crypto.d.ts.map +1 -0
- package/dist/src/Sqlite.d.ts +3 -0
- package/dist/src/Sqlite.d.ts.map +1 -0
- package/dist/src/Sqlite.js +38 -0
- package/dist/src/Task.d.ts +45 -0
- package/dist/src/Task.d.ts.map +1 -0
- package/dist/src/Task.js +59 -0
- package/dist/src/Worker.d.ts +2 -0
- package/dist/src/Worker.d.ts.map +1 -0
- package/dist/src/Worker.js +12 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +4 -0
- package/dist/src/local-first/Relay.d.ts +55 -0
- package/dist/src/local-first/Relay.d.ts.map +1 -0
- package/dist/src/local-first/Relay.js +243 -0
- package/package.json +22 -20
- package/src/Crypto.ts +1 -1
- package/src/Sqlite.ts +60 -0
- package/src/Task.ts +93 -0
- package/src/Worker.ts +11 -0
- package/src/index.ts +3 -1
- package/src/local-first/Relay.ts +134 -120
- package/LICENSE +0 -21
- package/dist/BetterSqliteDriver.d.ts +0 -3
- package/dist/BetterSqliteDriver.d.ts.map +0 -1
- package/dist/BetterSqliteDriver.js +0 -31
- package/dist/Crypto.d.ts.map +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -2
- package/dist/local-first/Relay.d.ts +0 -22
- package/dist/local-first/Relay.d.ts.map +0 -1
- package/dist/local-first/Relay.js +0 -165
- package/src/BetterSqliteDriver.ts +0 -47
- /package/dist/{Crypto.d.ts → src/Crypto.d.ts} +0 -0
- /package/dist/{Crypto.js → src/Crypto.js} +0 -0
package/src/Task.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js-specific Task utilities.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
createRun as createCommonRun,
|
|
9
|
+
createUnknownError,
|
|
10
|
+
type CreateRun,
|
|
11
|
+
type Run,
|
|
12
|
+
type RunDeps,
|
|
13
|
+
} from "@evolu/common";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A promise that resolves when a termination signal is received.
|
|
17
|
+
*
|
|
18
|
+
* Resolves on `SIGINT` (Ctrl-C), `SIGTERM` (OS/k8s/Docker termination),
|
|
19
|
+
* `SIGHUP` (console close/terminal disconnect), or `SIGBREAK` (Windows
|
|
20
|
+
* Ctrl-Break).
|
|
21
|
+
*
|
|
22
|
+
* @group Node.js Run
|
|
23
|
+
*/
|
|
24
|
+
export type Shutdown = Promise<void>;
|
|
25
|
+
|
|
26
|
+
export interface ShutdownDep {
|
|
27
|
+
readonly shutdown: Shutdown;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Creates {@link Run} for Node.js with global error handling and graceful
|
|
32
|
+
* shutdown.
|
|
33
|
+
*
|
|
34
|
+
* Registers `uncaughtException` and `unhandledRejection` handlers that log
|
|
35
|
+
* errors and initiate graceful shutdown. Adds a `shutdown` promise to deps that
|
|
36
|
+
* resolves on termination signals (`SIGINT`, `SIGTERM`, `SIGHUP`). Handlers are
|
|
37
|
+
* removed when the Run is disposed.
|
|
38
|
+
*
|
|
39
|
+
* ### Example
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* const deps = { ...createRelayDeps(), console };
|
|
43
|
+
*
|
|
44
|
+
* await using run = createRun(deps);
|
|
45
|
+
* await using stack = new AsyncDisposableStack();
|
|
46
|
+
*
|
|
47
|
+
* stack.use(await run.orThrow(startRelay({ port: 4000 })));
|
|
48
|
+
*
|
|
49
|
+
* await run.deps.shutdown;
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @group Node.js Run
|
|
53
|
+
*/
|
|
54
|
+
export const createRun: CreateRun<RunDeps & ShutdownDep> = <D>(
|
|
55
|
+
deps?: D,
|
|
56
|
+
): Run<RunDeps & ShutdownDep & D> => {
|
|
57
|
+
const { promise: shutdown, resolve: resolveShutdown } =
|
|
58
|
+
Promise.withResolvers<void>();
|
|
59
|
+
|
|
60
|
+
const run = createCommonRun({ ...deps, shutdown } as D & ShutdownDep);
|
|
61
|
+
|
|
62
|
+
const console = run.deps.console.child("global");
|
|
63
|
+
|
|
64
|
+
const handleError = (source: string) => (error: unknown) => {
|
|
65
|
+
console.error(source, createUnknownError(error));
|
|
66
|
+
process.exitCode = 1;
|
|
67
|
+
|
|
68
|
+
// Resolve shutdown so `await run.deps.shutdown` unblocks
|
|
69
|
+
// and allows the stack to be disposed.
|
|
70
|
+
resolveShutdown();
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const handleUncaughtException = handleError("uncaughtException");
|
|
74
|
+
const handleUnhandledRejection = handleError("unhandledRejection");
|
|
75
|
+
|
|
76
|
+
process.on("uncaughtException", handleUncaughtException);
|
|
77
|
+
process.on("unhandledRejection", handleUnhandledRejection);
|
|
78
|
+
process.on("SIGINT", resolveShutdown); // Ctrl-C (all platforms)
|
|
79
|
+
process.on("SIGTERM", resolveShutdown); // OS/k8s/Docker termination (Unix)
|
|
80
|
+
process.on("SIGHUP", resolveShutdown); // Console close (Windows), terminal disconnect (Unix)
|
|
81
|
+
process.on("SIGBREAK", resolveShutdown); // Ctrl-Break (Windows)
|
|
82
|
+
|
|
83
|
+
run.onAbort(() => {
|
|
84
|
+
process.off("uncaughtException", handleUncaughtException);
|
|
85
|
+
process.off("unhandledRejection", handleUnhandledRejection);
|
|
86
|
+
process.off("SIGINT", resolveShutdown);
|
|
87
|
+
process.off("SIGTERM", resolveShutdown);
|
|
88
|
+
process.off("SIGHUP", resolveShutdown);
|
|
89
|
+
process.off("SIGBREAK", resolveShutdown);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return run;
|
|
93
|
+
};
|
package/src/Worker.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// TODO: Implement Node.js Worker API
|
|
2
|
+
//
|
|
3
|
+
// This module should provide Node.js implementations of the common Worker API:
|
|
4
|
+
// - createWorker
|
|
5
|
+
// - createWorkerSelf (with onError hooking process.on('uncaughtException') and
|
|
6
|
+
// process.on('unhandledRejection'))
|
|
7
|
+
// - createMessageChannel
|
|
8
|
+
// - createMessagePort
|
|
9
|
+
//
|
|
10
|
+
// Node.js uses worker_threads module for Worker/MessageChannel/MessagePort.
|
|
11
|
+
// Error handling uses process events instead of globalThis.onerror.
|
package/src/index.ts
CHANGED
package/src/local-first/Relay.ts
CHANGED
|
@@ -1,105 +1,120 @@
|
|
|
1
1
|
import {
|
|
2
|
-
ConsoleDep,
|
|
3
|
-
createRelation,
|
|
4
2
|
createRandom,
|
|
3
|
+
createRelation,
|
|
5
4
|
createSqlite,
|
|
6
|
-
CreateSqliteDriverDep,
|
|
7
|
-
|
|
5
|
+
type CreateSqliteDriverDep,
|
|
6
|
+
isPromiseLike,
|
|
7
|
+
Name,
|
|
8
8
|
ok,
|
|
9
9
|
OwnerId,
|
|
10
|
-
RandomDep,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
SqliteError,
|
|
14
|
-
TimingSafeEqualDep,
|
|
10
|
+
type RandomDep,
|
|
11
|
+
type Task,
|
|
12
|
+
type TimingSafeEqualDep,
|
|
15
13
|
Uint8Array,
|
|
16
14
|
} from "@evolu/common";
|
|
17
15
|
import {
|
|
18
16
|
applyProtocolMessageAsRelay,
|
|
19
|
-
ApplyProtocolMessageAsRelayOptions,
|
|
17
|
+
type ApplyProtocolMessageAsRelayOptions,
|
|
20
18
|
createBaseSqliteStorageTables,
|
|
21
|
-
createRelayLogger,
|
|
22
19
|
createRelaySqliteStorage,
|
|
23
20
|
createRelayStorageTables,
|
|
24
21
|
defaultProtocolMessageMaxSize,
|
|
25
22
|
parseOwnerIdFromOwnerWebSocketTransportUrl,
|
|
26
|
-
Relay,
|
|
27
|
-
RelayConfig,
|
|
23
|
+
type Relay,
|
|
24
|
+
type RelayConfig,
|
|
28
25
|
} from "@evolu/common/local-first";
|
|
29
26
|
import { existsSync } from "fs";
|
|
30
27
|
import { createServer } from "http";
|
|
31
28
|
import { WebSocket, WebSocketServer } from "ws";
|
|
32
|
-
import { createBetterSqliteDriver } from "../BetterSqliteDriver.js";
|
|
33
29
|
import { createTimingSafeEqual } from "../Crypto.js";
|
|
30
|
+
import { createBetterSqliteDriver } from "../Sqlite.js";
|
|
34
31
|
|
|
35
32
|
export interface NodeJsRelayConfig extends RelayConfig {
|
|
36
33
|
/** The port number for the HTTP server. */
|
|
37
34
|
readonly port?: number;
|
|
38
35
|
}
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
(deps: ConsoleDep) =>
|
|
49
|
-
(config: NodeJsRelayConfig): Promise<Result<Relay, SqliteError>> =>
|
|
50
|
-
createNodeJsRelayWithSqliteDriver({
|
|
51
|
-
...deps,
|
|
52
|
-
createSqliteDriver: createBetterSqliteDriver,
|
|
53
|
-
})(config);
|
|
37
|
+
export type RelayDeps = CreateSqliteDriverDep & RandomDep & TimingSafeEqualDep;
|
|
38
|
+
|
|
39
|
+
/** Dependencies for {@link startRelay} using better-sqlite3. */
|
|
40
|
+
export const createRelayDeps = (): RelayDeps => ({
|
|
41
|
+
createSqliteDriver: createBetterSqliteDriver,
|
|
42
|
+
random: createRandom(),
|
|
43
|
+
timingSafeEqual: createTimingSafeEqual(),
|
|
44
|
+
});
|
|
54
45
|
|
|
55
46
|
/**
|
|
56
|
-
*
|
|
47
|
+
* Starts an Evolu relay server using Node.js.
|
|
48
|
+
*
|
|
49
|
+
* Use {@link createRelayDeps} to create dependencies for better-sqlite3, or
|
|
50
|
+
* provide a custom SQLite driver implementation.
|
|
51
|
+
*
|
|
52
|
+
* ### Example
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* // Ensure the database is created in a predictable location for Docker.
|
|
56
|
+
* mkdirSync("data", { recursive: true });
|
|
57
|
+
* process.chdir("data");
|
|
58
|
+
*
|
|
59
|
+
* const console = createConsole({
|
|
60
|
+
* // level: "debug",
|
|
61
|
+
* formatter: createConsoleFormatter()({
|
|
62
|
+
* timestampFormat: "relative",
|
|
63
|
+
* }),
|
|
64
|
+
* });
|
|
65
|
+
*
|
|
66
|
+
* const deps = { ...createRelayDeps(), console };
|
|
57
67
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
68
|
+
* await using run = createRun(deps);
|
|
69
|
+
* await using stack = new AsyncDisposableStack();
|
|
70
|
+
*
|
|
71
|
+
* stack.use(
|
|
72
|
+
* await run.orThrow(
|
|
73
|
+
* startRelay({
|
|
74
|
+
* port: 4000,
|
|
75
|
+
*
|
|
76
|
+
* // Note: Relay requires URL in format ws://host:port/<ownerId>
|
|
77
|
+
* // isOwnerAllowed: (_ownerId) => true,
|
|
78
|
+
*
|
|
79
|
+
* isOwnerWithinQuota: (_ownerId, requiredBytes) => {
|
|
80
|
+
* const maxBytes = 1024 * 1024; // 1MB
|
|
81
|
+
* return requiredBytes <= maxBytes;
|
|
82
|
+
* },
|
|
83
|
+
* }),
|
|
84
|
+
* ),
|
|
85
|
+
* );
|
|
86
|
+
*
|
|
87
|
+
* await run.deps.shutdown;
|
|
88
|
+
* ```
|
|
60
89
|
*/
|
|
61
|
-
export const
|
|
62
|
-
(
|
|
63
|
-
(config: NodeJsRelayConfig): Promise<Result<Relay, SqliteError>> =>
|
|
64
|
-
createNodeJsRelayWithDeps({
|
|
65
|
-
...deps,
|
|
66
|
-
random: createRandom(),
|
|
67
|
-
timingSafeEqual: createTimingSafeEqual(),
|
|
68
|
-
})(config);
|
|
69
|
-
|
|
70
|
-
const createNodeJsRelayWithDeps =
|
|
71
|
-
(deps: ConsoleDep & CreateSqliteDriverDep & RandomDep & TimingSafeEqualDep) =>
|
|
72
|
-
async ({
|
|
90
|
+
export const startRelay =
|
|
91
|
+
({
|
|
73
92
|
port = 443,
|
|
74
|
-
name =
|
|
75
|
-
enableLogging = false,
|
|
93
|
+
name = Name.orThrow("evolu-relay"),
|
|
76
94
|
isOwnerAllowed,
|
|
77
95
|
isOwnerWithinQuota,
|
|
78
|
-
}: NodeJsRelayConfig):
|
|
79
|
-
|
|
80
|
-
|
|
96
|
+
}: NodeJsRelayConfig): Task<Relay, never, RelayDeps> =>
|
|
97
|
+
async (run) => {
|
|
98
|
+
await using stack = new AsyncDisposableStack();
|
|
99
|
+
const console = run.deps.console.child("relay");
|
|
81
100
|
|
|
82
101
|
const dbFileExists = existsSync(`${name}.db`);
|
|
83
102
|
|
|
84
|
-
const
|
|
85
|
-
if (!
|
|
103
|
+
const sqliteResult = await run(createSqlite(name));
|
|
104
|
+
if (!sqliteResult.ok) return sqliteResult;
|
|
105
|
+
const sqlite = stack.use(sqliteResult.value);
|
|
86
106
|
|
|
87
|
-
const
|
|
107
|
+
const deps = { ...run.deps, sqlite };
|
|
88
108
|
|
|
89
109
|
if (!dbFileExists) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (!result.ok) return result;
|
|
93
|
-
}
|
|
94
|
-
{
|
|
95
|
-
const result = createRelayStorageTables(depsWithSqlite);
|
|
96
|
-
if (!result.ok) return result;
|
|
97
|
-
}
|
|
110
|
+
createBaseSqliteStorageTables(deps);
|
|
111
|
+
createRelayStorageTables(deps);
|
|
98
112
|
}
|
|
99
113
|
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
114
|
+
const relayRun = run.create().addDeps({
|
|
115
|
+
storage: createRelaySqliteStorage(deps)({
|
|
116
|
+
isOwnerWithinQuota,
|
|
117
|
+
}),
|
|
103
118
|
});
|
|
104
119
|
|
|
105
120
|
const server = createServer();
|
|
@@ -107,14 +122,14 @@ const createNodeJsRelayWithDeps =
|
|
|
107
122
|
maxPayload: defaultProtocolMessageMaxSize,
|
|
108
123
|
noServer: true,
|
|
109
124
|
});
|
|
110
|
-
|
|
111
125
|
const ownerSocketRelation = createRelation<OwnerId, WebSocket>();
|
|
112
126
|
|
|
113
127
|
server.on("upgrade", (request, socket, head) => {
|
|
114
|
-
socket.on("error",
|
|
128
|
+
socket.on("error", console.debug);
|
|
115
129
|
|
|
116
130
|
const completeUpgrade = () => {
|
|
117
|
-
socket.removeListener("error",
|
|
131
|
+
socket.removeListener("error", console.debug);
|
|
132
|
+
|
|
118
133
|
wss.handleUpgrade(request, socket, head, (ws) => {
|
|
119
134
|
wss.emit("connection", ws, request);
|
|
120
135
|
});
|
|
@@ -130,7 +145,7 @@ const createNodeJsRelayWithDeps =
|
|
|
130
145
|
);
|
|
131
146
|
|
|
132
147
|
if (!ownerId) {
|
|
133
|
-
|
|
148
|
+
console.debug("invalid or missing ownerId in URL", request.url);
|
|
134
149
|
socket.write("HTTP/1.1 400 Bad Request\r\n\r\n");
|
|
135
150
|
socket.destroy();
|
|
136
151
|
return;
|
|
@@ -138,9 +153,9 @@ const createNodeJsRelayWithDeps =
|
|
|
138
153
|
|
|
139
154
|
void (async () => {
|
|
140
155
|
const result = isOwnerAllowed(ownerId);
|
|
141
|
-
const isAllowed =
|
|
156
|
+
const isAllowed = isPromiseLike(result) ? await result : result;
|
|
142
157
|
if (!isAllowed) {
|
|
143
|
-
|
|
158
|
+
console.debug("unauthorized owner", ownerId);
|
|
144
159
|
socket.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
|
|
145
160
|
socket.destroy();
|
|
146
161
|
return;
|
|
@@ -150,97 +165,96 @@ const createNodeJsRelayWithDeps =
|
|
|
150
165
|
});
|
|
151
166
|
|
|
152
167
|
wss.on("connection", (ws) => {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
ws.on("error", (error) => {
|
|
156
|
-
log.connectionWebSocketError(error);
|
|
157
|
-
});
|
|
168
|
+
console.debug("on connection", wss.clients.size);
|
|
158
169
|
|
|
159
170
|
const options: ApplyProtocolMessageAsRelayOptions = {
|
|
160
171
|
subscribe: (ownerId) => {
|
|
161
172
|
ownerSocketRelation.add(ownerId, ws);
|
|
162
|
-
|
|
173
|
+
console.debug(
|
|
174
|
+
"subscribe",
|
|
163
175
|
ownerId,
|
|
164
|
-
|
|
176
|
+
ownerSocketRelation.bCountForA(ownerId),
|
|
165
177
|
);
|
|
166
178
|
},
|
|
167
179
|
|
|
168
180
|
unsubscribe: (ownerId) => {
|
|
169
181
|
ownerSocketRelation.remove(ownerId, ws);
|
|
170
|
-
|
|
182
|
+
console.debug(
|
|
183
|
+
"unsubscribe",
|
|
171
184
|
ownerId,
|
|
172
|
-
|
|
185
|
+
ownerSocketRelation.bCountForA(ownerId),
|
|
173
186
|
);
|
|
174
187
|
},
|
|
175
188
|
|
|
176
189
|
broadcast: (ownerId, message) => {
|
|
177
|
-
const
|
|
178
|
-
if (!sockets) return;
|
|
179
|
-
|
|
180
|
-
let broadcastCount = 0;
|
|
181
|
-
for (const socket of sockets) {
|
|
190
|
+
for (const socket of ownerSocketRelation.iterateB(ownerId)) {
|
|
182
191
|
if (socket !== ws && socket.readyState === WebSocket.OPEN) {
|
|
183
192
|
socket.send(message, { binary: true });
|
|
184
|
-
broadcastCount++;
|
|
185
193
|
}
|
|
186
194
|
}
|
|
187
195
|
|
|
188
|
-
|
|
196
|
+
console.debug(
|
|
197
|
+
"broadcast",
|
|
198
|
+
ownerId,
|
|
199
|
+
ownerSocketRelation.bCountForA(ownerId),
|
|
200
|
+
);
|
|
189
201
|
},
|
|
190
202
|
};
|
|
191
203
|
|
|
192
204
|
ws.on("message", (message) => {
|
|
193
205
|
if (!Uint8Array.is(message)) return;
|
|
194
|
-
log.messageLength(message.length);
|
|
195
206
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
})
|
|
205
|
-
|
|
207
|
+
void (async () => {
|
|
208
|
+
const response = await relayRun(
|
|
209
|
+
applyProtocolMessageAsRelay(message, options),
|
|
210
|
+
);
|
|
211
|
+
if (!response.ok) {
|
|
212
|
+
console.error(response);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
ws.send(response.value.message, { binary: true });
|
|
216
|
+
})();
|
|
206
217
|
});
|
|
207
218
|
|
|
208
219
|
ws.on("close", () => {
|
|
209
|
-
ownerSocketRelation.
|
|
210
|
-
|
|
220
|
+
ownerSocketRelation.removeByB(ws);
|
|
221
|
+
console.debug("ws close", wss.clients.size);
|
|
211
222
|
});
|
|
212
223
|
});
|
|
213
224
|
|
|
214
|
-
server
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
225
|
+
// Cleanup runs in LIFO order: clients → WebSocketServer → HTTP server
|
|
226
|
+
stack.defer(() => {
|
|
227
|
+
console.info("Shutdown complete");
|
|
228
|
+
});
|
|
218
229
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
230
|
+
stack.defer(() => {
|
|
231
|
+
server.close(() => {
|
|
232
|
+
console.info("HTTP server closed");
|
|
223
233
|
});
|
|
234
|
+
});
|
|
224
235
|
|
|
236
|
+
stack.defer(() => {
|
|
237
|
+
// wss.close() emits 'close' when all clients have disconnected
|
|
238
|
+
// https://github.com/websockets/ws/blob/master/doc/ws.md#serverclosecallback
|
|
225
239
|
wss.close(() => {
|
|
226
|
-
|
|
240
|
+
console.info("WebSocketServer closed");
|
|
241
|
+
ok();
|
|
227
242
|
});
|
|
243
|
+
});
|
|
228
244
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
245
|
+
stack.defer(() => {
|
|
246
|
+
console.info("Shutting down...");
|
|
247
|
+
for (const client of wss.clients) {
|
|
248
|
+
if (client.readyState === WebSocket.OPEN) {
|
|
249
|
+
client.close(1000, "Evolu Relay shutting down");
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
});
|
|
233
253
|
|
|
234
|
-
|
|
254
|
+
stack.use(relayRun);
|
|
235
255
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
if (isDisposed) return;
|
|
239
|
-
isDisposed = true;
|
|
240
|
-
sqlite.value[Symbol.dispose]();
|
|
241
|
-
dispose();
|
|
242
|
-
},
|
|
243
|
-
};
|
|
256
|
+
server.listen(port);
|
|
257
|
+
console.info(`Started on port ${port}`);
|
|
244
258
|
|
|
245
|
-
return ok(
|
|
259
|
+
return ok(stack.move());
|
|
246
260
|
};
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"BetterSqliteDriver.d.ts","sourceRoot":"","sources":["../src/BetterSqliteDriver.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,kBAAkB,EAGnB,MAAM,eAAe,CAAC;AAGvB,eAAO,MAAM,wBAAwB,EAAE,kBAqCtC,CAAC"}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { constVoid, createPreparedStatementsCache, } from "@evolu/common";
|
|
2
|
-
import BetterSQLite from "better-sqlite3";
|
|
3
|
-
export const createBetterSqliteDriver = (name, options) => {
|
|
4
|
-
const filename = options?.memory ? ":memory:" : `${name}.db`;
|
|
5
|
-
const db = new BetterSQLite(filename);
|
|
6
|
-
let isDisposed = false;
|
|
7
|
-
const cache = createPreparedStatementsCache((sql) => db.prepare(sql),
|
|
8
|
-
// Not needed.
|
|
9
|
-
// https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#class-statement
|
|
10
|
-
constVoid);
|
|
11
|
-
const driver = {
|
|
12
|
-
exec: (query, isMutation) => {
|
|
13
|
-
// Always prepare is recommended for better-sqlite3
|
|
14
|
-
const prepared = cache.get(query, true);
|
|
15
|
-
const rows = isMutation
|
|
16
|
-
? []
|
|
17
|
-
: prepared.all(query.parameters);
|
|
18
|
-
const changes = isMutation ? prepared.run(query.parameters).changes : 0;
|
|
19
|
-
return { rows, changes };
|
|
20
|
-
},
|
|
21
|
-
export: () => db.serialize(),
|
|
22
|
-
[Symbol.dispose]: () => {
|
|
23
|
-
if (isDisposed)
|
|
24
|
-
return;
|
|
25
|
-
isDisposed = true;
|
|
26
|
-
cache[Symbol.dispose]();
|
|
27
|
-
db.close();
|
|
28
|
-
},
|
|
29
|
-
};
|
|
30
|
-
return Promise.resolve(driver);
|
|
31
|
-
};
|
package/dist/Crypto.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Crypto.d.ts","sourceRoot":"","sources":["../src/Crypto.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAErD,eAAO,MAAM,qBAAqB,QAAO,eAAkC,CAAC"}
|
package/dist/index.d.ts
DELETED
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC"}
|
package/dist/index.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { ConsoleDep, CreateSqliteDriverDep, Result, SqliteError } from "@evolu/common";
|
|
2
|
-
import { Relay, RelayConfig } from "@evolu/common/local-first";
|
|
3
|
-
export interface NodeJsRelayConfig extends RelayConfig {
|
|
4
|
-
/** The port number for the HTTP server. */
|
|
5
|
-
readonly port?: number;
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Creates an Evolu relay server.
|
|
9
|
-
*
|
|
10
|
-
* This implementation uses Node.js and better-sqlite3. Additional relay
|
|
11
|
-
* implementations will be provided for other platforms (Bun, Deno, Cloudflare
|
|
12
|
-
* Workers, Vercel Edge, etc.).
|
|
13
|
-
*/
|
|
14
|
-
export declare const createNodeJsRelay: (deps: ConsoleDep) => (config: NodeJsRelayConfig) => Promise<Result<Relay, SqliteError>>;
|
|
15
|
-
/**
|
|
16
|
-
* Creates an Evolu relay server with a custom SQLite driver.
|
|
17
|
-
*
|
|
18
|
-
* Use this when you need to provide a different SQLite driver implementation
|
|
19
|
-
* (e.g., using alternative SQLite libraries).
|
|
20
|
-
*/
|
|
21
|
-
export declare const createNodeJsRelayWithSqliteDriver: (deps: ConsoleDep & CreateSqliteDriverDep) => (config: NodeJsRelayConfig) => Promise<Result<Relay, SqliteError>>;
|
|
22
|
-
//# sourceMappingURL=Relay.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Relay.d.ts","sourceRoot":"","sources":["../../src/local-first/Relay.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EAIV,qBAAqB,EAKrB,MAAM,EAEN,WAAW,EAGZ,MAAM,eAAe,CAAC;AACvB,OAAO,EASL,KAAK,EACL,WAAW,EACZ,MAAM,2BAA2B,CAAC;AAOnC,MAAM,WAAW,iBAAkB,SAAQ,WAAW;IACpD,2CAA2C;IAC3C,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAC3B,MAAM,UAAU,MAChB,QAAQ,iBAAiB,KAAG,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,WAAW,CAAC,CAInD,CAAC;AAEf;;;;;GAKG;AACH,eAAO,MAAM,iCAAiC,GAC3C,MAAM,UAAU,GAAG,qBAAqB,MACxC,QAAQ,iBAAiB,KAAG,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,WAAW,CAAC,CAKnD,CAAC"}
|