Skip to content

Commit e2547d2

Browse files
committed
isOwnerWithinQuota is required, improve docs
1 parent b04aeaa commit e2547d2

5 files changed

Lines changed: 30 additions & 11 deletions

File tree

.changeset/olive-bars-count.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@evolu/common": patch
3+
"@evolu/nodejs": patch
4+
"@evolu/relay": patch
5+
---
6+
7+
isOwnerWithinQuota is required, improve docs

apps/relay/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const relay = await createNodeJsRelay({
1212
port: 4000,
1313
enableLogging: false,
1414

15-
isOwnerAllowed: (_ownerId) => true,
15+
// Note: Relay requires URL in format ws://host:port/<ownerId>
16+
// isOwnerAllowed: (_ownerId) => true,
1617

1718
isOwnerWithinQuota: (_ownerId, requiredBytes) => {
1819
const maxBytes = 1024 * 1024; // 1MB

packages/common/src/Evolu/Relay.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ export interface RelayConfig extends ConsoleConfig, StorageConfig {
3535
readonly name?: SimpleName;
3636

3737
/**
38-
* Callback to check if an {@link OwnerId} is allowed to access the relay.
38+
* Optional callback to check if an {@link OwnerId} is allowed to access the
39+
* relay. If this callback is not provided, all owners are allowed.
3940
*
40-
* The callback receives the OwnerId and returns a {@link MaybeAsync} boolean:
41-
* `true` to allow access, or `false` to deny.
41+
* The callback receives the {@link OwnerId} and returns a {@link MaybeAsync}
42+
* boolean: `true` to allow access, or `false` to deny.
4243
*
4344
* The callback can be synchronous (for SQLite or in-memory checks) or
4445
* asynchronous (for calling remote APIs).
@@ -54,7 +55,8 @@ export interface RelayConfig extends ConsoleConfig, StorageConfig {
5455
* Owners specify which relays to connect to via {@link OwnerTransport}. In
5556
* WebSocket-based implementations, this check occurs before accepting the
5657
* connection, with the OwnerId typically extracted from the URL Path (e.g.,
57-
* `ws://localhost:4000/<ownerId>`).
58+
* `ws://localhost:4000/<ownerId>`). The relay requires the URL to be in the
59+
* correct format for OwnerId extraction.
5860
*
5961
* ### Example
6062
*
@@ -75,7 +77,7 @@ export interface RelayConfig extends ConsoleConfig, StorageConfig {
7577
* Promise.resolve(ownerId === "6jy_2F4RT5qqeLgJ14_dnQ"),
7678
* ```
7779
*/
78-
readonly isOwnerAllowed: (ownerId: OwnerId) => MaybeAsync<boolean>;
80+
readonly isOwnerAllowed?: (ownerId: OwnerId) => MaybeAsync<boolean>;
7981
}
8082

8183
/**

packages/common/src/Result.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@
287287
*
288288
* ```ts
289289
* for (const query of [
290-
* sql`drop table evolu_owner;`,
290+
* sql`drop table evolu_config;`,
291291
* sql`drop table evolu_message;`,
292292
* ]) {
293293
* const result = deps.sqlite.exec(query);

packages/nodejs/src/Evolu/Relay.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ const createNodeJsRelayWithDeps =
110110
server.on("upgrade", (request, socket, head) => {
111111
socket.on("error", log.upgradeSocketError);
112112

113+
const completeUpgrade = () => {
114+
socket.removeListener("error", log.upgradeSocketError);
115+
wss.handleUpgrade(request, socket, head, (ws) => {
116+
wss.emit("connection", ws, request);
117+
});
118+
};
119+
120+
if (!isOwnerAllowed) {
121+
completeUpgrade();
122+
return;
123+
}
124+
113125
const ownerId = parseOwnerIdFromOwnerWebSocketTransportUrl(
114126
request.url ?? "",
115127
);
@@ -130,10 +142,7 @@ const createNodeJsRelayWithDeps =
130142
socket.destroy();
131143
return;
132144
}
133-
socket.removeListener("error", log.upgradeSocketError);
134-
wss.handleUpgrade(request, socket, head, (ws) => {
135-
wss.emit("connection", ws, request);
136-
});
145+
completeUpgrade();
137146
})();
138147
});
139148

0 commit comments

Comments
 (0)