@rebasepro/client 0.9.1-canary.f2f61da → 0.9.1-canary.faee831
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/dist/auth.d.ts +5 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.es.js +742 -85
- package/dist/index.es.js.map +1 -1
- package/dist/realtime-channel.d.ts +243 -0
- package/dist/websocket.d.ts +83 -2
- package/package.json +7 -5
- package/src/auth.ts +32 -0
- package/src/index.ts +80 -2
- package/src/realtime-channel.test.ts +542 -0
- package/src/realtime-channel.ts +539 -0
- package/src/realtime-optout.test.ts +128 -2
- package/src/transport-baseurl.test.ts +53 -0
- package/src/transport.ts +25 -3
- package/src/websocket.ts +453 -77
|
@@ -40,15 +40,95 @@ describe("realtime opt-out", () => {
|
|
|
40
40
|
jest.restoreAllMocks();
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
-
it("opens
|
|
43
|
+
it("opens NO socket on construction, even with realtime enabled", () => {
|
|
44
|
+
// Changed deliberately. Constructing a client is not a statement that
|
|
45
|
+
// the app wants a socket — `createRebaseClient` builds one whenever
|
|
46
|
+
// realtime is not explicitly disabled, so dialling here cost a
|
|
47
|
+
// connection on every page load of every app that merely *might*
|
|
48
|
+
// subscribe later. Anonymous-first apps paid it on every visit to
|
|
49
|
+
// authenticate with nothing.
|
|
44
50
|
const { FakeWebSocket, opened } = trackingWebSocket();
|
|
45
51
|
globalThis.WebSocket = FakeWebSocket;
|
|
46
52
|
|
|
47
53
|
createRebaseClient({ baseUrl: "http://localhost:3000/api" });
|
|
48
54
|
|
|
55
|
+
expect(opened).toHaveLength(0);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("opens the socket on the first channel operation", () => {
|
|
59
|
+
const { FakeWebSocket, opened } = trackingWebSocket();
|
|
60
|
+
globalThis.WebSocket = FakeWebSocket;
|
|
61
|
+
|
|
62
|
+
const client = createRebaseClient({ baseUrl: "http://localhost:3000/api" });
|
|
63
|
+
|
|
64
|
+
// Asking for the channel is not using it.
|
|
65
|
+
const channel = client.realtime.channel("doc:1");
|
|
66
|
+
expect(opened).toHaveLength(0);
|
|
67
|
+
|
|
68
|
+
void channel.join();
|
|
49
69
|
expect(opened).toHaveLength(1);
|
|
50
70
|
});
|
|
51
71
|
|
|
72
|
+
it("opens the socket on the first collection subscription", () => {
|
|
73
|
+
const { FakeWebSocket, opened } = trackingWebSocket();
|
|
74
|
+
globalThis.WebSocket = FakeWebSocket;
|
|
75
|
+
|
|
76
|
+
const client = createRebaseClient({ baseUrl: "http://localhost:3000/api" });
|
|
77
|
+
expect(opened).toHaveLength(0);
|
|
78
|
+
|
|
79
|
+
client.collection("posts").listen!(undefined, () => { /* noop */ });
|
|
80
|
+
|
|
81
|
+
expect(opened).toHaveLength(1);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("opens exactly one socket however many things subscribe", () => {
|
|
85
|
+
const { FakeWebSocket, opened } = trackingWebSocket();
|
|
86
|
+
globalThis.WebSocket = FakeWebSocket;
|
|
87
|
+
|
|
88
|
+
const client = createRebaseClient({ baseUrl: "http://localhost:3000/api" });
|
|
89
|
+
|
|
90
|
+
client.collection("posts").listen!(undefined, () => { /* noop */ });
|
|
91
|
+
client.collection("authors").listen!(undefined, () => { /* noop */ });
|
|
92
|
+
void client.realtime.channel("doc:1").join();
|
|
93
|
+
|
|
94
|
+
expect(opened).toHaveLength(1);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("stays silent for an anonymous client that never subscribes", () => {
|
|
98
|
+
// The motivating case: most page loads of an anonymous-first app.
|
|
99
|
+
// No socket, and no console noise either.
|
|
100
|
+
const { FakeWebSocket, opened } = trackingWebSocket();
|
|
101
|
+
globalThis.WebSocket = FakeWebSocket;
|
|
102
|
+
const warn = jest.spyOn(console, "warn").mockImplementation(() => { /* capture */ });
|
|
103
|
+
const debug = jest.spyOn(console, "debug").mockImplementation(() => { /* capture */ });
|
|
104
|
+
|
|
105
|
+
const client = createRebaseClient({ baseUrl: "http://localhost:3000/api" });
|
|
106
|
+
void client.realtime.channel("doc:1"); // obtained, never used
|
|
107
|
+
|
|
108
|
+
expect(opened).toHaveLength(0);
|
|
109
|
+
expect(warn).not.toHaveBeenCalled();
|
|
110
|
+
expect(debug).not.toHaveBeenCalled();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("says nothing about a missing WebSocket until something needs one", () => {
|
|
114
|
+
// The environment warning used to fire on construction, so a Node
|
|
115
|
+
// process that never subscribed was told off for a socket it did not
|
|
116
|
+
// want.
|
|
117
|
+
// @ts-expect-error deliberately removing the global
|
|
118
|
+
delete globalThis.WebSocket;
|
|
119
|
+
const warn = jest.spyOn(console, "warn").mockImplementation(() => { /* capture */ });
|
|
120
|
+
|
|
121
|
+
const client = createRebaseClient({ baseUrl: "http://localhost:3000/api" });
|
|
122
|
+
expect(warn).not.toHaveBeenCalled();
|
|
123
|
+
|
|
124
|
+
void client.realtime.channel("doc:1").join();
|
|
125
|
+
expect(warn).toHaveBeenCalledWith(expect.stringMatching(/WebSocket is not defined/));
|
|
126
|
+
|
|
127
|
+
// ...and only once, however many times it is asked.
|
|
128
|
+
void client.realtime.channel("doc:2").join();
|
|
129
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
130
|
+
});
|
|
131
|
+
|
|
52
132
|
it("opens no socket when realtime is disabled", () => {
|
|
53
133
|
const { FakeWebSocket, opened } = trackingWebSocket();
|
|
54
134
|
globalThis.WebSocket = FakeWebSocket;
|
|
@@ -76,11 +156,12 @@ describe("realtime opt-out", () => {
|
|
|
76
156
|
expect(opened).toHaveLength(0);
|
|
77
157
|
});
|
|
78
158
|
|
|
79
|
-
it("close() releases
|
|
159
|
+
it("close() releases a socket that was opened", () => {
|
|
80
160
|
const { FakeWebSocket, opened, closed } = trackingWebSocket();
|
|
81
161
|
globalThis.WebSocket = FakeWebSocket;
|
|
82
162
|
|
|
83
163
|
const client = createRebaseClient({ baseUrl: "http://localhost:3000/api" });
|
|
164
|
+
client.collection("posts").listen!(undefined, () => { /* noop */ });
|
|
84
165
|
expect(opened).toHaveLength(1);
|
|
85
166
|
|
|
86
167
|
client.close();
|
|
@@ -88,6 +169,23 @@ describe("realtime opt-out", () => {
|
|
|
88
169
|
expect(closed).toHaveLength(1);
|
|
89
170
|
});
|
|
90
171
|
|
|
172
|
+
it("close() is final — a later subscribe does not redial", () => {
|
|
173
|
+
// Otherwise one queued frame could reopen the socket that close() just
|
|
174
|
+
// released, and the Node process this method exists to let exit would
|
|
175
|
+
// stay alive anyway.
|
|
176
|
+
const { FakeWebSocket, opened } = trackingWebSocket();
|
|
177
|
+
globalThis.WebSocket = FakeWebSocket;
|
|
178
|
+
|
|
179
|
+
const client = createRebaseClient({ baseUrl: "http://localhost:3000/api" });
|
|
180
|
+
client.collection("posts").listen!(undefined, () => { /* noop */ });
|
|
181
|
+
expect(opened).toHaveLength(1);
|
|
182
|
+
|
|
183
|
+
client.close();
|
|
184
|
+
client.collection("authors").listen!(undefined, () => { /* noop */ });
|
|
185
|
+
|
|
186
|
+
expect(opened).toHaveLength(1);
|
|
187
|
+
});
|
|
188
|
+
|
|
91
189
|
it("close() is safe when realtime was never started, and when called twice", () => {
|
|
92
190
|
const { FakeWebSocket } = trackingWebSocket();
|
|
93
191
|
globalThis.WebSocket = FakeWebSocket;
|
|
@@ -100,6 +198,34 @@ describe("realtime opt-out", () => {
|
|
|
100
198
|
expect(() => live.close()).not.toThrow();
|
|
101
199
|
});
|
|
102
200
|
|
|
201
|
+
it("still refuses channels when realtime is disabled", () => {
|
|
202
|
+
// `realtime: false` remains a hard opt-out. Only the *default* changed
|
|
203
|
+
// — from "connected eagerly" to "connected on use".
|
|
204
|
+
const { FakeWebSocket, opened } = trackingWebSocket();
|
|
205
|
+
globalThis.WebSocket = FakeWebSocket;
|
|
206
|
+
|
|
207
|
+
const client = createRebaseClient({
|
|
208
|
+
baseUrl: "http://localhost:3000/api",
|
|
209
|
+
realtime: false
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
expect(() => client.realtime.channel("doc:1")).toThrow(/realtime: false/);
|
|
213
|
+
expect(opened).toHaveLength(0);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("signing in does not by itself open a socket", () => {
|
|
217
|
+
// Authenticating is not a request for realtime. If it dialled, every
|
|
218
|
+
// app with a login would lose lazy connect at the moment of login.
|
|
219
|
+
const { FakeWebSocket, opened } = trackingWebSocket();
|
|
220
|
+
globalThis.WebSocket = FakeWebSocket;
|
|
221
|
+
|
|
222
|
+
const client = createRebaseClient({ baseUrl: "http://localhost:3000/api" });
|
|
223
|
+
// Drive the auth-state path the way a sign-in would.
|
|
224
|
+
client.auth.onAuthStateChange(() => { /* noop */ });
|
|
225
|
+
|
|
226
|
+
expect(opened).toHaveLength(0);
|
|
227
|
+
});
|
|
228
|
+
|
|
103
229
|
it("leaves listen() absent so callers can feature-detect, and says why via the query builder", () => {
|
|
104
230
|
const { FakeWebSocket } = trackingWebSocket();
|
|
105
231
|
globalThis.WebSocket = FakeWebSocket;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach } from "@jest/globals";
|
|
2
|
+
import { createTransport } from "./transport";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `baseUrl` is optional because the common production shape is a Rebase backend
|
|
6
|
+
* serving its own SPA: the API is the page's origin. These pin the resolution,
|
|
7
|
+
* because getting it wrong pushes apps into baking an absolute host — which
|
|
8
|
+
* breaks the moment a custom domain points at the same app, and which CORS
|
|
9
|
+
* cannot repair (a SameSite=Lax auth cookie is not sent cross-site either).
|
|
10
|
+
*/
|
|
11
|
+
const setWindow = (origin?: string) => {
|
|
12
|
+
if (origin === undefined) { delete (globalThis as never as { window?: unknown }).window; return; }
|
|
13
|
+
(globalThis as never as { window: unknown }).window = { location: { origin, href: origin + "/" } };
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
afterEach(() => setWindow(undefined));
|
|
17
|
+
|
|
18
|
+
describe("transport baseUrl resolution", () => {
|
|
19
|
+
it("uses the page origin when unset in a browser", () => {
|
|
20
|
+
setWindow("https://dadaki.com");
|
|
21
|
+
expect(createTransport({}).baseUrl).toBe("https://dadaki.com");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("gives callers something they can build an absolute URL from", () => {
|
|
25
|
+
setWindow("https://dadaki.com");
|
|
26
|
+
const t = createTransport({});
|
|
27
|
+
// The empty-string version of this threw, which is what drove apps to
|
|
28
|
+
// bake a hostname into the bundle.
|
|
29
|
+
expect(() => new URL(`${t.baseUrl}/api/functions/doc-content/get`)).not.toThrow();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("follows the page, so a second hostname on the same app just works", () => {
|
|
33
|
+
setWindow("https://dadaki.apps.rebase.pro");
|
|
34
|
+
expect(createTransport({}).baseUrl).toBe("https://dadaki.apps.rebase.pro");
|
|
35
|
+
setWindow("https://dadaki.com");
|
|
36
|
+
expect(createTransport({}).baseUrl).toBe("https://dadaki.com");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("still honours an explicit baseUrl, which is what dev needs", () => {
|
|
40
|
+
setWindow("https://dadaki.com");
|
|
41
|
+
expect(createTransport({ baseUrl: "http://localhost:3001" }).baseUrl).toBe("http://localhost:3001");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("strips a trailing slash so joins do not double up", () => {
|
|
45
|
+
setWindow(undefined);
|
|
46
|
+
expect(createTransport({ baseUrl: "https://api.example.com/" }).baseUrl).toBe("https://api.example.com");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("stays relative off-browser, where there is no origin to borrow", () => {
|
|
50
|
+
setWindow(undefined);
|
|
51
|
+
expect(createTransport({}).baseUrl).toBe("");
|
|
52
|
+
});
|
|
53
|
+
});
|
package/src/transport.ts
CHANGED
|
@@ -112,6 +112,29 @@ export interface Transport {
|
|
|
112
112
|
resolveToken: () => Promise<string | null>;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
/**
|
|
116
|
+
* The base every request and every caller-built URL resolves against.
|
|
117
|
+
*
|
|
118
|
+
* `baseUrl` is optional because the common production shape is a Rebase
|
|
119
|
+
* backend serving its own SPA, where the API is simply the page's origin.
|
|
120
|
+
* Leaving it unset is therefore the *correct* configuration there — and the
|
|
121
|
+
* one that keeps working when a second hostname (a custom domain) points at
|
|
122
|
+
* the same app.
|
|
123
|
+
*
|
|
124
|
+
* When unset in a browser this resolves to the page origin rather than "".
|
|
125
|
+
* Requests behave identically either way, but the empty string is a trap for
|
|
126
|
+
* anything that builds a URL from `client.baseUrl`: `new URL("" + path)`
|
|
127
|
+
* throws, so apps "fixed" it by baking an absolute host into their bundle —
|
|
128
|
+
* which is exactly what breaks the day a custom domain is added, and which no
|
|
129
|
+
* amount of CORS configuration repairs, because a SameSite=Lax auth cookie is
|
|
130
|
+
* not sent cross-site either.
|
|
131
|
+
*/
|
|
132
|
+
function resolveBaseUrl(configured?: string): string {
|
|
133
|
+
if (configured) return configured.replace(/\/$/, "");
|
|
134
|
+
if (typeof window !== "undefined" && window.location?.origin) return window.location.origin;
|
|
135
|
+
return "";
|
|
136
|
+
}
|
|
137
|
+
|
|
115
138
|
export function createTransport(config: RebaseClientConfig): Transport {
|
|
116
139
|
const fetchFn = config.fetch || globalThis.fetch;
|
|
117
140
|
const apiPath = config.apiPath || "/api";
|
|
@@ -128,8 +151,7 @@ export function createTransport(config: RebaseClientConfig): Transport {
|
|
|
128
151
|
}
|
|
129
152
|
|
|
130
153
|
async function request<T = unknown>(path: string, init?: RequestInit): Promise<T> {
|
|
131
|
-
const
|
|
132
|
-
const url = base + apiPath + path;
|
|
154
|
+
const url = resolveBaseUrl(config.baseUrl) + apiPath + path;
|
|
133
155
|
|
|
134
156
|
let activeToken = token;
|
|
135
157
|
if (tokenGetter) {
|
|
@@ -242,7 +264,7 @@ headers: retryHeaders });
|
|
|
242
264
|
setToken(newToken: string | null) { token = newToken || undefined; },
|
|
243
265
|
setAuthTokenGetter(getter: () => Promise<string | null>) { tokenGetter = getter; },
|
|
244
266
|
setOnUnauthorized(handler: () => Promise<boolean>) { onUnauthorizedHandler = handler; },
|
|
245
|
-
get baseUrl() { return config.baseUrl
|
|
267
|
+
get baseUrl() { return resolveBaseUrl(config.baseUrl); },
|
|
246
268
|
get apiPath() { return apiPath; },
|
|
247
269
|
get fetchFn() { return fetchFn; },
|
|
248
270
|
getHeaders: (init?: RequestInit) => getHeaders(token, init) as Record<string, string>,
|