@supatype/client 0.9.1 → 0.10.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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +112 -72
- package/.turbo/turbo-typecheck.log +2 -2
- package/CHANGELOG.md +123 -20
- package/README.md +18 -0
- package/dist/auth.d.ts +13 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +53 -13
- package/dist/auth.js.map +1 -1
- package/dist/bindings.d.ts +4 -1
- package/dist/bindings.d.ts.map +1 -1
- package/dist/bindings.js +2 -1
- package/dist/bindings.js.map +1 -1
- package/dist/channel-watch.d.ts +42 -0
- package/dist/channel-watch.d.ts.map +1 -0
- package/dist/channel-watch.js +59 -0
- package/dist/channel-watch.js.map +1 -0
- package/dist/errors.d.ts +40 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +31 -0
- package/dist/errors.js.map +1 -1
- package/dist/exact-values.d.ts +18 -5
- package/dist/exact-values.d.ts.map +1 -1
- package/dist/exact-values.js +76 -12
- package/dist/exact-values.js.map +1 -1
- package/dist/field-kinds-registry.d.ts +1 -1
- package/dist/field-kinds-registry.d.ts.map +1 -1
- package/dist/field-kinds-registry.js +20 -2
- package/dist/field-kinds-registry.js.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/latest-rows.d.ts +15 -0
- package/dist/latest-rows.d.ts.map +1 -1
- package/dist/latest-rows.js +14 -7
- package/dist/latest-rows.js.map +1 -1
- package/dist/public-env.d.ts +13 -0
- package/dist/public-env.d.ts.map +1 -0
- package/dist/public-env.js +23 -0
- package/dist/public-env.js.map +1 -0
- package/dist/query-cache.d.ts +2 -1
- package/dist/query-cache.d.ts.map +1 -1
- package/dist/query-cache.js.map +1 -1
- package/dist/query.d.ts +7 -0
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +12 -4
- package/dist/query.js.map +1 -1
- package/dist/realtime-refusals.d.ts +35 -0
- package/dist/realtime-refusals.d.ts.map +1 -0
- package/dist/realtime-refusals.js +73 -0
- package/dist/realtime-refusals.js.map +1 -0
- package/dist/realtime.d.ts +87 -8
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +304 -51
- package/dist/realtime.js.map +1 -1
- package/dist/runtime-env.d.ts +2 -0
- package/dist/runtime-env.d.ts.map +1 -1
- package/dist/runtime-env.js +45 -5
- package/dist/runtime-env.js.map +1 -1
- package/dist/table-subscription.d.ts +27 -11
- package/dist/table-subscription.d.ts.map +1 -1
- package/dist/table-subscription.js +159 -25
- package/dist/table-subscription.js.map +1 -1
- package/dist/types.d.ts +34 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
- package/src/auth.ts +55 -12
- package/src/bindings.ts +4 -1
- package/src/channel-watch.ts +86 -0
- package/src/errors.ts +54 -0
- package/src/exact-values.ts +101 -13
- package/src/field-kinds-registry.ts +2 -3
- package/src/index.ts +14 -2
- package/src/latest-rows.ts +24 -7
- package/src/public-env.ts +22 -0
- package/src/query-cache.ts +2 -1
- package/src/query.ts +12 -4
- package/src/realtime-refusals.ts +100 -0
- package/src/realtime.ts +385 -56
- package/src/runtime-env.ts +53 -5
- package/src/table-subscription.ts +190 -32
- package/src/types.ts +40 -1
- package/tests/auth-restore-user.test.ts +84 -0
- package/tests/auth-sso.test.ts +32 -1
- package/tests/auth.test.ts +11 -0
- package/tests/channel-watch-private.test.ts +105 -0
- package/tests/channel-watch.test.ts +97 -0
- package/tests/exact-response.test.ts +101 -0
- package/tests/exact-values.test.ts +57 -0
- package/tests/examples-runtime-env.test.ts +135 -0
- package/tests/field-kinds-registry.test.ts +12 -0
- package/tests/helpers/close-event.ts +12 -0
- package/tests/helpers/fake-client.ts +38 -0
- package/tests/helpers/mock-websocket.ts +22 -0
- package/tests/helpers/reactive-options-binding.ts +99 -0
- package/tests/public-env.test.ts +43 -0
- package/tests/query-cache.test.ts +1 -1
- package/tests/query.test.ts +39 -2
- package/tests/realtime-connection.test.ts +228 -0
- package/tests/realtime-exact-values.test.ts +42 -0
- package/tests/realtime-multi-tenant-integration.test.ts +0 -1
- package/tests/realtime-private-channels.test.ts +377 -0
- package/tests/realtime-refusal-error.test.ts +80 -0
- package/tests/realtime-rows-after-broadcast.test.ts +205 -0
- package/tests/realtime-session-auth.test.ts +0 -1
- package/tests/realtime-shared-channel.test.ts +9 -27
- package/tests/realtime-typed-channels.test.ts +49 -0
- package/tests/runtime-env-expo.test.ts +58 -0
- package/tests/runtime-env.test.ts +49 -0
- package/tests/subscribe-frame.test.ts +48 -0
- package/tests/table-subscription-rows.test.ts +136 -0
- package/tests/table-subscription.test.ts +4 -3
- package/tsconfig.json +2 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/type-tests/declared-channels.ts +30 -0
- package/type-tests/tsconfig.json +4 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Private channels on the client (plan section 6, stage D).
|
|
3
|
+
*
|
|
4
|
+
* The wire is the one packages/realtime speaks: the join carries `private: true`, the first system
|
|
5
|
+
* frame of a socket announces `features: ["private_channels"]`, and a refusal or a withdrawal is a
|
|
6
|
+
* channel-scoped system error frame. The tests drive the client with those frames.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, expect, afterEach } from "vitest"
|
|
10
|
+
import type { ChannelStatus } from "../src/realtime.js"
|
|
11
|
+
import { RealtimeChannelError, RealtimeError, SupatypeError, isRealtimeChannelError } from "../src/errors.js"
|
|
12
|
+
import { simulateServerMessage, sentFrames, subscribeFrames, ackNewestJoin } from "./helpers/mock-websocket.js"
|
|
13
|
+
import { createSocketSwapHarness, type SocketSwapHarness } from "./helpers/socket-swap.js"
|
|
14
|
+
|
|
15
|
+
let harness: SocketSwapHarness
|
|
16
|
+
|
|
17
|
+
afterEach(() => harness.restore())
|
|
18
|
+
|
|
19
|
+
interface Seen {
|
|
20
|
+
status: ChannelStatus
|
|
21
|
+
err: Error | undefined
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function greet(features?: string[]): void {
|
|
25
|
+
simulateServerMessage(harness.socket(), {
|
|
26
|
+
type: "system",
|
|
27
|
+
status: "ok",
|
|
28
|
+
message: "authenticated",
|
|
29
|
+
...(features !== undefined && { features }),
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function refuse(channel: string, message: string): void {
|
|
34
|
+
const ref = subscribeFrames(harness.socket(), channel).at(-1)?.["ref"]
|
|
35
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "error", message, channel, ref })
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function watch(channel: string, opts?: { private?: boolean }): Seen[] {
|
|
39
|
+
const seen: Seen[] = []
|
|
40
|
+
harness.realtime.channel(channel, opts).onBroadcast("bid", () => {}).subscribe((status, err) => seen.push({ status, err }))
|
|
41
|
+
return seen
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function lastError(seen: Seen[]): RealtimeChannelError {
|
|
45
|
+
const err = seen.at(-1)?.err
|
|
46
|
+
if (!(err instanceof RealtimeChannelError)) throw new Error(`expected a RealtimeChannelError, got ${String(err)}`)
|
|
47
|
+
return err
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe("the private option on a join", () => {
|
|
51
|
+
it("sends private: true once the server has announced private_channels", async () => {
|
|
52
|
+
harness = createSocketSwapHarness()
|
|
53
|
+
watch("lot:42", { private: true })
|
|
54
|
+
await harness.settle()
|
|
55
|
+
greet(["private_channels"])
|
|
56
|
+
expect(subscribeFrames(harness.socket(), "lot:42")).toHaveLength(1)
|
|
57
|
+
expect(subscribeFrames(harness.socket(), "lot:42")[0]?.["private"]).toBe(true)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it("holds a private join until the first frame says what the server can do", async () => {
|
|
61
|
+
harness = createSocketSwapHarness()
|
|
62
|
+
watch("lot:42", { private: true })
|
|
63
|
+
await harness.settle()
|
|
64
|
+
expect(subscribeFrames(harness.socket(), "lot:42")).toHaveLength(0)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it("sends a join that did not ask for privacy at once, with no private key", async () => {
|
|
68
|
+
harness = createSocketSwapHarness()
|
|
69
|
+
watch("lobby")
|
|
70
|
+
await harness.settle()
|
|
71
|
+
const frames = subscribeFrames(harness.socket(), "lobby")
|
|
72
|
+
expect(frames).toHaveLength(1)
|
|
73
|
+
expect("private" in (frames[0] ?? {})).toBe(false)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it("joins again as private when a second handle on the name asks for it", async () => {
|
|
77
|
+
harness = createSocketSwapHarness()
|
|
78
|
+
watch("lot:42")
|
|
79
|
+
await harness.settle()
|
|
80
|
+
greet(["private_channels"])
|
|
81
|
+
ackNewestJoin(harness.socket(), "lot:42")
|
|
82
|
+
watch("lot:42", { private: true })
|
|
83
|
+
const frames = subscribeFrames(harness.socket(), "lot:42")
|
|
84
|
+
expect(frames).toHaveLength(2)
|
|
85
|
+
expect(frames[1]?.["private"]).toBe(true)
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
describe("a server that does not enforce private channels", () => {
|
|
90
|
+
it("fails a private join with a clear error when the greeting names no features", async () => {
|
|
91
|
+
harness = createSocketSwapHarness()
|
|
92
|
+
const seen = watch("lot:42", { private: true })
|
|
93
|
+
await harness.settle()
|
|
94
|
+
greet()
|
|
95
|
+
expect(seen.map((s) => s.status)).toEqual(["CHANNEL_ERROR"])
|
|
96
|
+
const err = lastError(seen)
|
|
97
|
+
expect(err.reason).toBe("unsupported_server")
|
|
98
|
+
expect(err.channel).toBe("lot:42")
|
|
99
|
+
expect(err.message).toBe("this realtime server does not enforce private channels; upgrade it")
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it("never sends the join, so no open channel is created", async () => {
|
|
103
|
+
harness = createSocketSwapHarness()
|
|
104
|
+
watch("lot:42", { private: true })
|
|
105
|
+
await harness.settle()
|
|
106
|
+
greet(["something_else"])
|
|
107
|
+
expect(subscribeFrames(harness.socket(), "lot:42")).toHaveLength(0)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it("decides again on each socket, since a reconnect may reach a different server", async () => {
|
|
111
|
+
harness = createSocketSwapHarness()
|
|
112
|
+
const seen = watch("lot:42", { private: true })
|
|
113
|
+
await harness.settle()
|
|
114
|
+
greet(["private_channels"])
|
|
115
|
+
ackNewestJoin(harness.socket(), "lot:42")
|
|
116
|
+
await harness.signIn()
|
|
117
|
+
expect(subscribeFrames(harness.socket(), "lot:42")).toHaveLength(0)
|
|
118
|
+
greet()
|
|
119
|
+
expect(seen.at(-1)?.status).toBe("CHANNEL_ERROR")
|
|
120
|
+
expect(lastError(seen).reason).toBe("unsupported_server")
|
|
121
|
+
})
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
describe("a refused join", () => {
|
|
125
|
+
const cases: Array<[string, string, RealtimeChannelError["reason"], boolean]> = [
|
|
126
|
+
["join_denied", "not allowed to join lot:42: the join rule of LotChannel denied it", "join_denied", false],
|
|
127
|
+
["not_declared", "lot:42 was opened as private, but no Channel declaration matches it", "not_declared", false],
|
|
128
|
+
["does_not_fit", "lot:abc does not fit lot:{id}: id must be a UUID", "does_not_fit", false],
|
|
129
|
+
["unavailable", "could not check access to lot:42 because the database did not answer; retry", "unavailable", true],
|
|
130
|
+
["shared_instance", "private channels are not available on a shared realtime instance", "shared_instance", false],
|
|
131
|
+
["other", "permission denied", "refused", false],
|
|
132
|
+
]
|
|
133
|
+
for (const [name, message, reason, retryable] of cases) {
|
|
134
|
+
it(`is a RealtimeChannelError with reason ${name}`, async () => {
|
|
135
|
+
harness = createSocketSwapHarness()
|
|
136
|
+
const seen = watch("lot:42", { private: true })
|
|
137
|
+
await harness.settle()
|
|
138
|
+
greet(["private_channels"])
|
|
139
|
+
refuse("lot:42", message)
|
|
140
|
+
const err = lastError(seen)
|
|
141
|
+
expect(err.reason).toBe(reason)
|
|
142
|
+
expect(err.retryable).toBe(retryable)
|
|
143
|
+
expect(err.message).toBe(message)
|
|
144
|
+
expect(err).toBeInstanceOf(RealtimeError)
|
|
145
|
+
expect(err).toBeInstanceOf(SupatypeError)
|
|
146
|
+
})
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
describe("access withdrawn from a channel the socket holds", () => {
|
|
151
|
+
const withdrawn = "access to lot:42 was withdrawn: the join rule of LotChannel no longer admits this user"
|
|
152
|
+
|
|
153
|
+
async function joined(): Promise<Seen[]> {
|
|
154
|
+
harness = createSocketSwapHarness()
|
|
155
|
+
const seen = watch("lot:42", { private: true })
|
|
156
|
+
await harness.settle()
|
|
157
|
+
greet(["private_channels"])
|
|
158
|
+
ackNewestJoin(harness.socket(), "lot:42")
|
|
159
|
+
return seen
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function withdraw(message = withdrawn): void {
|
|
163
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "error", message, channel: "lot:42" })
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
it("tells the channel CHANNEL_ERROR with reason withdrawn", async () => {
|
|
167
|
+
const seen = await joined()
|
|
168
|
+
withdraw()
|
|
169
|
+
expect(seen.map((s) => s.status)).toEqual(["SUBSCRIBED", "CHANNEL_ERROR"])
|
|
170
|
+
expect(lastError(seen).reason).toBe("withdrawn")
|
|
171
|
+
expect(lastError(seen).retryable).toBe(false)
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it("also reads a withdrawal because the declaration went away", async () => {
|
|
175
|
+
const seen = await joined()
|
|
176
|
+
withdraw("access to lot:42 was withdrawn: it was opened as private, and no Channel declaration matches it any more")
|
|
177
|
+
expect(lastError(seen).reason).toBe("withdrawn")
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
it("does not rejoin on its own, which the server would only refuse again", async () => {
|
|
181
|
+
await joined()
|
|
182
|
+
withdraw()
|
|
183
|
+
expect(subscribeFrames(harness.socket(), "lot:42")).toHaveLength(1)
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
it("joins again when the app subscribes again", async () => {
|
|
187
|
+
const seen = await joined()
|
|
188
|
+
withdraw()
|
|
189
|
+
harness.realtime.channel("lot:42", { private: true }).subscribe((status, err) => seen.push({ status, err }))
|
|
190
|
+
expect(subscribeFrames(harness.socket(), "lot:42")).toHaveLength(2)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
it("says SUBSCRIBED only to a handle added after the withdrawal, once the server takes the new join", async () => {
|
|
194
|
+
const seen = await joined()
|
|
195
|
+
withdraw()
|
|
196
|
+
harness.realtime.channel("lot:42", { private: true }).subscribe((status, err) => seen.push({ status, err }))
|
|
197
|
+
ackNewestJoin(harness.socket(), "lot:42")
|
|
198
|
+
expect(seen.at(-1)?.status).toBe("SUBSCRIBED")
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
describe("a broadcast sent from the database", () => {
|
|
203
|
+
it("reaches onBroadcast listeners like any other broadcast, with no sender", async () => {
|
|
204
|
+
harness = createSocketSwapHarness()
|
|
205
|
+
const received: Array<Record<string, unknown>> = []
|
|
206
|
+
harness.realtime.channel("lot:42", { private: true }).onBroadcast("bid", (p) => received.push(p)).subscribe()
|
|
207
|
+
await harness.settle()
|
|
208
|
+
greet(["private_channels"])
|
|
209
|
+
ackNewestJoin(harness.socket(), "lot:42")
|
|
210
|
+
simulateServerMessage(harness.socket(), { type: "broadcast", channel: "lot:42", event: "bid", payload: { amount: 12 } })
|
|
211
|
+
expect(received).toEqual([{ amount: 12 }])
|
|
212
|
+
expect(sentFrames(harness.socket()).filter((f) => f["type"] === "broadcast")).toHaveLength(0)
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
describe("isRealtimeChannelError", () => {
|
|
217
|
+
it("recognises the error, and an error of the same shape from another copy of the package", () => {
|
|
218
|
+
const own = new RealtimeChannelError("denied", "lot:42", "join_denied")
|
|
219
|
+
const other = Object.assign(new Error("denied"), { name: "RealtimeChannelError", channel: "lot:42", reason: "join_denied" })
|
|
220
|
+
expect(isRealtimeChannelError(own)).toBe(true)
|
|
221
|
+
expect(isRealtimeChannelError(other)).toBe(true)
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
it("does not take any other error for one", () => {
|
|
225
|
+
expect(isRealtimeChannelError(new Error("denied"))).toBe(false)
|
|
226
|
+
expect(isRealtimeChannelError(new RealtimeError("denied"))).toBe(false)
|
|
227
|
+
expect(isRealtimeChannelError("denied")).toBe(false)
|
|
228
|
+
expect(isRealtimeChannelError(undefined)).toBe(false)
|
|
229
|
+
})
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
describe("a refusal's code, not its sentence, says what it is", () => {
|
|
233
|
+
const codes: Array<[string, RealtimeChannelError["reason"], boolean]> = [
|
|
234
|
+
["join_denied", "join_denied", false],
|
|
235
|
+
["not_declared", "not_declared", false],
|
|
236
|
+
["does_not_fit", "does_not_fit", false],
|
|
237
|
+
["unavailable", "unavailable", true],
|
|
238
|
+
["shared_instance", "shared_instance", false],
|
|
239
|
+
["refused", "refused", false],
|
|
240
|
+
]
|
|
241
|
+
for (const [code, reason, retryable] of codes) {
|
|
242
|
+
it(`reads code ${code} as reason ${reason} whatever the sentence says`, async () => {
|
|
243
|
+
harness = createSocketSwapHarness()
|
|
244
|
+
const seen = watch("lot:42", { private: true })
|
|
245
|
+
await harness.settle()
|
|
246
|
+
greet(["private_channels"])
|
|
247
|
+
const ref = subscribeFrames(harness.socket(), "lot:42").at(-1)?.["ref"]
|
|
248
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "error", code, message: "worded some new way", channel: "lot:42", ref })
|
|
249
|
+
expect(lastError(seen).reason).toBe(reason)
|
|
250
|
+
expect(lastError(seen).retryable).toBe(retryable)
|
|
251
|
+
expect(lastError(seen).message).toBe("worded some new way")
|
|
252
|
+
})
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
it("reads a code over a sentence that says something else", async () => {
|
|
256
|
+
harness = createSocketSwapHarness()
|
|
257
|
+
const seen = watch("lot:42", { private: true })
|
|
258
|
+
await harness.settle()
|
|
259
|
+
greet(["private_channels"])
|
|
260
|
+
const ref = subscribeFrames(harness.socket(), "lot:42").at(-1)?.["ref"]
|
|
261
|
+
simulateServerMessage(harness.socket(), {
|
|
262
|
+
type: "system",
|
|
263
|
+
status: "error",
|
|
264
|
+
code: "unavailable",
|
|
265
|
+
message: "not allowed to join lot:42: the join rule of LotChannel denied it",
|
|
266
|
+
channel: "lot:42",
|
|
267
|
+
ref,
|
|
268
|
+
})
|
|
269
|
+
expect(lastError(seen).reason).toBe("unavailable")
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
it("reads a code this client does not know as refused, never by the sentence", async () => {
|
|
273
|
+
harness = createSocketSwapHarness()
|
|
274
|
+
const seen = watch("lot:42", { private: true })
|
|
275
|
+
await harness.settle()
|
|
276
|
+
greet(["private_channels"])
|
|
277
|
+
const ref = subscribeFrames(harness.socket(), "lot:42").at(-1)?.["ref"]
|
|
278
|
+
simulateServerMessage(harness.socket(), {
|
|
279
|
+
type: "system",
|
|
280
|
+
status: "error",
|
|
281
|
+
code: "from_the_future",
|
|
282
|
+
message: "not allowed to join lot:42: the join rule of LotChannel denied it",
|
|
283
|
+
channel: "lot:42",
|
|
284
|
+
ref,
|
|
285
|
+
})
|
|
286
|
+
expect(lastError(seen).reason).toBe("refused")
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
it("reads a withdrawal by its code", async () => {
|
|
290
|
+
harness = createSocketSwapHarness()
|
|
291
|
+
const seen = watch("lot:42", { private: true })
|
|
292
|
+
await harness.settle()
|
|
293
|
+
greet(["private_channels"])
|
|
294
|
+
ackNewestJoin(harness.socket(), "lot:42")
|
|
295
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "error", code: "withdrawn", message: "gone", channel: "lot:42" })
|
|
296
|
+
expect(seen.map((s) => s.status)).toEqual(["SUBSCRIBED", "CHANNEL_ERROR"])
|
|
297
|
+
expect(lastError(seen).reason).toBe("withdrawn")
|
|
298
|
+
})
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
describe("a send, track or payload refused on a healthy channel", () => {
|
|
302
|
+
interface Refused {
|
|
303
|
+
channel: string
|
|
304
|
+
code: string
|
|
305
|
+
message: string
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
async function healthy(): Promise<{ seen: Seen[]; refused: Refused[] }> {
|
|
309
|
+
harness = createSocketSwapHarness()
|
|
310
|
+
const seen: Seen[] = []
|
|
311
|
+
const refused: Refused[] = []
|
|
312
|
+
harness.realtime
|
|
313
|
+
.channel("lot:42", { private: true })
|
|
314
|
+
.onBroadcast("bid", () => {})
|
|
315
|
+
.onRefused((event) => refused.push(event))
|
|
316
|
+
.subscribe((status, err) => seen.push({ status, err }))
|
|
317
|
+
await harness.settle()
|
|
318
|
+
greet(["private_channels"])
|
|
319
|
+
ackNewestJoin(harness.socket(), "lot:42")
|
|
320
|
+
return { seen, refused }
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const cases: Array<[string, string]> = [
|
|
324
|
+
["send_denied", "not allowed to broadcast on lot:42: the send rule of LotChannel denied it"],
|
|
325
|
+
["track_denied", "not allowed to track presence on lot:42: the track rule of LotChannel denied it"],
|
|
326
|
+
["payload_refused", "not allowed to broadcast bid on lot:42: the send rule of LotChannel refused the payload"],
|
|
327
|
+
]
|
|
328
|
+
for (const [code, message] of cases) {
|
|
329
|
+
it(`reports ${code} as an event and leaves the channel SUBSCRIBED`, async () => {
|
|
330
|
+
const { seen, refused } = await healthy()
|
|
331
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "error", code, message, channel: "lot:42" })
|
|
332
|
+
expect(refused).toEqual([{ channel: "lot:42", code, message }])
|
|
333
|
+
expect(seen.map((s) => s.status)).toEqual(["SUBSCRIBED"])
|
|
334
|
+
})
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
it("tells every handle on the channel that listens", async () => {
|
|
338
|
+
const { refused } = await healthy()
|
|
339
|
+
const second: Refused[] = []
|
|
340
|
+
harness.realtime.channel("lot:42", { private: true }).onRefused((event) => second.push(event)).subscribe()
|
|
341
|
+
harness.realtime.channel("lot:42", { private: true }).subscribe()
|
|
342
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "error", code: "send_denied", message: "no", channel: "lot:42" })
|
|
343
|
+
expect(refused).toHaveLength(1)
|
|
344
|
+
expect(second).toHaveLength(1)
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
it("keeps the join intact", async () => {
|
|
348
|
+
const { seen } = await healthy()
|
|
349
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "error", code: "send_denied", message: "no", channel: "lot:42" })
|
|
350
|
+
simulateServerMessage(harness.socket(), { type: "broadcast", channel: "lot:42", event: "bid", payload: { amount: 1 } })
|
|
351
|
+
expect(seen.map((s) => s.status)).toEqual(["SUBSCRIBED"])
|
|
352
|
+
expect(subscribeFrames(harness.socket(), "lot:42")).toHaveLength(1)
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
it("reads the sentence of an older server that sends no code", async () => {
|
|
356
|
+
const { seen, refused } = await healthy()
|
|
357
|
+
simulateServerMessage(harness.socket(), {
|
|
358
|
+
type: "system",
|
|
359
|
+
status: "error",
|
|
360
|
+
message: "not allowed to broadcast on lot:42: the send rule of LotChannel denied it",
|
|
361
|
+
channel: "lot:42",
|
|
362
|
+
})
|
|
363
|
+
expect(refused.map((r) => r.code)).toEqual(["send_denied"])
|
|
364
|
+
expect(seen.map((s) => s.status)).toEqual(["SUBSCRIBED"])
|
|
365
|
+
})
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
describe("an older server that sends no code", () => {
|
|
369
|
+
it("still classifies a refused join by its sentence", async () => {
|
|
370
|
+
harness = createSocketSwapHarness()
|
|
371
|
+
const seen = watch("lot:42", { private: true })
|
|
372
|
+
await harness.settle()
|
|
373
|
+
greet(["private_channels"])
|
|
374
|
+
refuse("lot:42", "not allowed to join lot:42: the join rule of LotChannel denied it")
|
|
375
|
+
expect(lastError(seen).reason).toBe("join_denied")
|
|
376
|
+
})
|
|
377
|
+
})
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A refused join hands the status callback the server's reason, as `(status, err)`.
|
|
3
|
+
*
|
|
4
|
+
* 0.9.2 release test (N-FF5): the server now refuses a join for a table that does not exist, and
|
|
5
|
+
* says which table. The client reported CHANNEL_ERROR and dropped the reason, so the bindings could
|
|
6
|
+
* only say "Subscription error" and the developer had to read the socket's frames to find the typo.
|
|
7
|
+
* The callback's second argument is how Supabase's client reports it too, so an existing
|
|
8
|
+
* `(status) => ...` callback is unaffected.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it, expect, afterEach } from "vitest"
|
|
12
|
+
import type { ChannelStatus } from "../src/realtime.js"
|
|
13
|
+
import { subscriptionViewOf, watchTable, type SubscriptionView } from "../src/table-subscription.js"
|
|
14
|
+
import { simulateServerMessage, subscribedChannels } from "./helpers/mock-websocket.js"
|
|
15
|
+
import { fakeClient } from "./helpers/fake-client.js"
|
|
16
|
+
import { createSocketSwapHarness, type SocketSwapHarness } from "./helpers/socket-swap.js"
|
|
17
|
+
|
|
18
|
+
const REASON = "table public.notes does not exist: check the table name and schema"
|
|
19
|
+
|
|
20
|
+
let harness: SocketSwapHarness
|
|
21
|
+
|
|
22
|
+
afterEach(() => harness.restore())
|
|
23
|
+
|
|
24
|
+
/** The server refuses the newest socket's first join, naming its channel, with {@link REASON}. */
|
|
25
|
+
function refuseFirstJoin(): void {
|
|
26
|
+
const channel = subscribedChannels(harness.socket())[0]
|
|
27
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "error", message: REASON, channel, ref: "1" })
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("a refused join", () => {
|
|
31
|
+
it("passes the server's message to the status callback as an Error", async () => {
|
|
32
|
+
harness = createSocketSwapHarness()
|
|
33
|
+
const seen: Array<{ status: ChannelStatus; err: Error | undefined }> = []
|
|
34
|
+
harness.realtime
|
|
35
|
+
.channel("notes:team")
|
|
36
|
+
.on("postgres_changes", { event: "*", table: "notes" }, () => {})
|
|
37
|
+
.subscribe((status, err) => seen.push({ status, err }))
|
|
38
|
+
await harness.settle()
|
|
39
|
+
refuseFirstJoin()
|
|
40
|
+
|
|
41
|
+
expect(seen).toHaveLength(1)
|
|
42
|
+
expect(seen[0]?.status).toBe("CHANNEL_ERROR")
|
|
43
|
+
expect(seen[0]?.err).toBeInstanceOf(Error)
|
|
44
|
+
expect(seen[0]?.err?.message).toBe(REASON)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it("passes no error with any other status", async () => {
|
|
48
|
+
harness = createSocketSwapHarness()
|
|
49
|
+
const errs: Array<Error | undefined> = []
|
|
50
|
+
harness.realtime
|
|
51
|
+
.channel("notes:ok")
|
|
52
|
+
.on("postgres_changes", { event: "*", table: "note" }, () => {})
|
|
53
|
+
.subscribe((_status, err) => errs.push(err))
|
|
54
|
+
await harness.settle()
|
|
55
|
+
harness.ackJoins()
|
|
56
|
+
|
|
57
|
+
expect(errs).toEqual([undefined])
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it("reaches the bindings' view as the error message", async () => {
|
|
61
|
+
harness = createSocketSwapHarness()
|
|
62
|
+
const views: SubscriptionView[] = []
|
|
63
|
+
const stop = watchTable(fakeClient(harness).client, "notes", undefined, {
|
|
64
|
+
rows: () => undefined,
|
|
65
|
+
status: (view) => views.push(view),
|
|
66
|
+
})
|
|
67
|
+
await harness.settle()
|
|
68
|
+
refuseFirstJoin()
|
|
69
|
+
stop()
|
|
70
|
+
|
|
71
|
+
expect(views.map((view) => view.error?.message)).toEqual([REASON])
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
describe("subscriptionViewOf with an error", () => {
|
|
76
|
+
it("shows the error's message, and the old wording when there is none", () => {
|
|
77
|
+
expect(subscriptionViewOf("CHANNEL_ERROR", new Error(REASON)).error?.message).toBe(REASON)
|
|
78
|
+
expect(subscriptionViewOf("CHANNEL_ERROR").error?.message).toBe("Subscription error")
|
|
79
|
+
})
|
|
80
|
+
})
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A row listener added to a channel already joined without one gets a join that delivers its rows.
|
|
3
|
+
*
|
|
4
|
+
* A join made for broadcast or presence alone carries `changes: false`, and the server sends it no
|
|
5
|
+
* rows. A row listener added after that, on the same handle or a second handle with the same name,
|
|
6
|
+
* used to find the join already sent: `subscribe()` reported SUBSCRIBED at once, no second frame
|
|
7
|
+
* went out, and no row ever arrived (0.9.2 release test, FF-1).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest"
|
|
11
|
+
import type { ChannelStatus, RealtimePayload } from "../src/realtime.js"
|
|
12
|
+
import { ackNewestJoin, deliverInsert, simulateServerMessage, subscribeFrames } from "./helpers/mock-websocket.js"
|
|
13
|
+
import { createSocketSwapHarness, type SocketSwapHarness } from "./helpers/socket-swap.js"
|
|
14
|
+
|
|
15
|
+
let harness: SocketSwapHarness
|
|
16
|
+
|
|
17
|
+
/** A channel joined for broadcast alone, with that join sent and acknowledged. */
|
|
18
|
+
async function joinedForBroadcast(channel: string): Promise<{ statuses: ChannelStatus[] }> {
|
|
19
|
+
const statuses: ChannelStatus[] = []
|
|
20
|
+
harness.realtime.channel(channel).onBroadcast("ping", () => {}).subscribe((s) => statuses.push(s))
|
|
21
|
+
await harness.settle()
|
|
22
|
+
ackNewestJoin(harness.socket(), channel)
|
|
23
|
+
return { statuses }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
harness = createSocketSwapHarness()
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
afterEach(() => harness.restore())
|
|
31
|
+
|
|
32
|
+
describe("a row listener added to a channel joined for broadcast", () => {
|
|
33
|
+
it("on a second handle joins again for its rows and reports SUBSCRIBED only once that join is acknowledged", async () => {
|
|
34
|
+
const first = await joinedForBroadcast("room:x")
|
|
35
|
+
const statuses: ChannelStatus[] = []
|
|
36
|
+
const rows: Array<RealtimePayload<Record<string, unknown>>> = []
|
|
37
|
+
harness.realtime
|
|
38
|
+
.channel("room:x")
|
|
39
|
+
.on("postgres_changes", { event: "INSERT", table: "note" }, (p) => rows.push(p))
|
|
40
|
+
.subscribe((s) => statuses.push(s))
|
|
41
|
+
|
|
42
|
+
const joins = subscribeFrames(harness.socket(), "room:x")
|
|
43
|
+
expect(joins.map((frame) => frame["changes"])).toEqual([false, true])
|
|
44
|
+
expect(joins[1]).toMatchObject({ schema: "public", table: "note", event: "INSERT" })
|
|
45
|
+
expect(statuses).toEqual([])
|
|
46
|
+
|
|
47
|
+
ackNewestJoin(harness.socket(), "room:x")
|
|
48
|
+
deliverInsert(harness.socket(), "room:x", 1)
|
|
49
|
+
expect(statuses).toEqual(["SUBSCRIBED"])
|
|
50
|
+
// The broadcast join kept serving it until the server took the rows join in its place.
|
|
51
|
+
expect(first.statuses).toEqual(["SUBSCRIBED"])
|
|
52
|
+
expect(rows.map((p) => [p.table, p.new])).toEqual([["note", { id: 1 }]])
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it("on the same handle joins again for its rows", async () => {
|
|
56
|
+
const statuses: ChannelStatus[] = []
|
|
57
|
+
const rows: Array<RealtimePayload<Record<string, unknown>>> = []
|
|
58
|
+
const handle = harness.realtime.channel("room:x").onBroadcast("ping", () => {}).subscribe()
|
|
59
|
+
await harness.settle()
|
|
60
|
+
ackNewestJoin(harness.socket(), "room:x")
|
|
61
|
+
|
|
62
|
+
handle.on("postgres_changes", { event: "INSERT", table: "note" }, (p) => rows.push(p)).subscribe((s) => statuses.push(s))
|
|
63
|
+
expect(subscribeFrames(harness.socket(), "room:x").map((frame) => frame["changes"])).toEqual([false, true])
|
|
64
|
+
expect(statuses).toEqual([])
|
|
65
|
+
|
|
66
|
+
ackNewestJoin(harness.socket(), "room:x")
|
|
67
|
+
deliverInsert(harness.socket(), "room:x", 1)
|
|
68
|
+
expect(statuses).toEqual(["SUBSCRIBED"])
|
|
69
|
+
expect(rows.map((p) => p.new)).toEqual([{ id: 1 }])
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it("on a channel named after that table joins again for its rows", async () => {
|
|
73
|
+
await joinedForBroadcast("note")
|
|
74
|
+
const rows: Array<RealtimePayload<Record<string, unknown>>> = []
|
|
75
|
+
harness.realtime.channel("note").on("postgres_changes", { event: "INSERT", table: "note" }, (p) => rows.push(p)).subscribe()
|
|
76
|
+
|
|
77
|
+
const joins = subscribeFrames(harness.socket(), "note")
|
|
78
|
+
expect(joins.map((frame) => frame["changes"])).toEqual([false, true])
|
|
79
|
+
expect(joins[1]).toMatchObject({ schema: "public", table: "note", event: "INSERT" })
|
|
80
|
+
|
|
81
|
+
ackNewestJoin(harness.socket(), "note")
|
|
82
|
+
deliverInsert(harness.socket(), "note", 1)
|
|
83
|
+
expect(rows.map((p) => p.new)).toEqual([{ id: 1 }])
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it("joins again when the broadcast join is still waiting for its reply, and ignores that stale reply", async () => {
|
|
87
|
+
const statuses: ChannelStatus[] = []
|
|
88
|
+
harness.realtime.channel("room:x").onBroadcast("ping", () => {}).subscribe()
|
|
89
|
+
await harness.settle()
|
|
90
|
+
const broadcastJoin = subscribeFrames(harness.socket(), "room:x")[0]
|
|
91
|
+
|
|
92
|
+
harness.realtime.channel("room:x").on("postgres_changes", { event: "INSERT", table: "note" }, () => {}).subscribe((s) => statuses.push(s))
|
|
93
|
+
expect(subscribeFrames(harness.socket(), "room:x").map((frame) => frame["changes"])).toEqual([false, true])
|
|
94
|
+
|
|
95
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "ok", message: "subscribed to room:x", ref: broadcastJoin?.["ref"] })
|
|
96
|
+
expect(statuses).toEqual([])
|
|
97
|
+
ackNewestJoin(harness.socket(), "room:x")
|
|
98
|
+
expect(statuses).toEqual(["SUBSCRIBED"])
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
/** The server refuses the newest join on `channel`, as it refuses a table the caller cannot read. */
|
|
103
|
+
function refuseNewestJoin(channel: string): void {
|
|
104
|
+
const newest = subscribeFrames(harness.socket(), channel).at(-1)
|
|
105
|
+
simulateServerMessage(harness.socket(), {
|
|
106
|
+
type: "system",
|
|
107
|
+
status: "error",
|
|
108
|
+
message: "role anon cannot read public.note",
|
|
109
|
+
channel,
|
|
110
|
+
ref: newest?.["ref"],
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A refused rejoin registered nothing, so the join it was to replace still stands on the server and
|
|
116
|
+
* still serves broadcast and presence. Every handle on the name was told CHANNEL_ERROR and stayed
|
|
117
|
+
* there, and every member was said to have left (0.9.2 round 4, S-1).
|
|
118
|
+
*/
|
|
119
|
+
describe("a rows rejoin the server refuses", () => {
|
|
120
|
+
it("fails the handle whose rows it asked for, and leaves the broadcast handle SUBSCRIBED", async () => {
|
|
121
|
+
const first = await joinedForBroadcast("room:x")
|
|
122
|
+
const statuses: Array<[ChannelStatus, string | undefined]> = []
|
|
123
|
+
harness.realtime
|
|
124
|
+
.channel("room:x")
|
|
125
|
+
.on("postgres_changes", { event: "INSERT", table: "note" }, () => {})
|
|
126
|
+
.subscribe((s, err) => statuses.push([s, err?.message]))
|
|
127
|
+
|
|
128
|
+
refuseNewestJoin("room:x")
|
|
129
|
+
expect(statuses).toEqual([["CHANNEL_ERROR", "role anon cannot read public.note"]])
|
|
130
|
+
expect(first.statuses).toEqual(["SUBSCRIBED"])
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it("keeps who is present, as the standing join keeps reporting it", async () => {
|
|
134
|
+
const seen: Array<{ joins: string[]; leaves: string[] }> = []
|
|
135
|
+
harness.realtime
|
|
136
|
+
.channel("room:x")
|
|
137
|
+
.onPresence(({ joins, leaves }) => seen.push({ joins: joins.map((m) => m.user_id), leaves: leaves.map((m) => m.user_id) }))
|
|
138
|
+
.subscribe()
|
|
139
|
+
await harness.settle()
|
|
140
|
+
ackNewestJoin(harness.socket(), "room:x")
|
|
141
|
+
const presence = (joins: string[], leaves: string[]): void =>
|
|
142
|
+
simulateServerMessage(harness.socket(), {
|
|
143
|
+
type: "presence",
|
|
144
|
+
channel: "room:x",
|
|
145
|
+
joins: joins.map((user_id) => ({ user_id })),
|
|
146
|
+
leaves: leaves.map((user_id) => ({ user_id })),
|
|
147
|
+
})
|
|
148
|
+
presence(["u1", "u2"], [])
|
|
149
|
+
|
|
150
|
+
harness.realtime.channel("room:x").on("postgres_changes", { event: "INSERT", table: "note" }, () => {}).subscribe()
|
|
151
|
+
// While the rejoin waits, the join still standing reports a leave.
|
|
152
|
+
presence([], ["u2"])
|
|
153
|
+
refuseNewestJoin("room:x")
|
|
154
|
+
expect(seen).toEqual([
|
|
155
|
+
{ joins: ["u1", "u2"], leaves: [] },
|
|
156
|
+
{ joins: [], leaves: ["u2"] },
|
|
157
|
+
])
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it("is told who is present again once a rejoin is taken, after its old members leave", async () => {
|
|
161
|
+
const seen: Array<{ joins: string[]; leaves: string[] }> = []
|
|
162
|
+
harness.realtime
|
|
163
|
+
.channel("room:x")
|
|
164
|
+
.onPresence(({ joins, leaves }) => seen.push({ joins: joins.map((m) => m.user_id), leaves: leaves.map((m) => m.user_id) }))
|
|
165
|
+
.subscribe()
|
|
166
|
+
await harness.settle()
|
|
167
|
+
ackNewestJoin(harness.socket(), "room:x")
|
|
168
|
+
simulateServerMessage(harness.socket(), { type: "presence", channel: "room:x", joins: [{ user_id: "u1" }], leaves: [] })
|
|
169
|
+
|
|
170
|
+
harness.realtime.channel("room:x").on("postgres_changes", { event: "INSERT", table: "note" }, () => {}).subscribe()
|
|
171
|
+
expect(seen).toHaveLength(1)
|
|
172
|
+
ackNewestJoin(harness.socket(), "room:x")
|
|
173
|
+
// The server follows the reply with who is present, so a page counting joins minus leaves
|
|
174
|
+
// counts u1 once.
|
|
175
|
+
simulateServerMessage(harness.socket(), { type: "presence", channel: "room:x", joins: [{ user_id: "u1" }], leaves: [] })
|
|
176
|
+
expect(seen).toEqual([
|
|
177
|
+
{ joins: ["u1"], leaves: [] },
|
|
178
|
+
{ joins: [], leaves: ["u1"] },
|
|
179
|
+
{ joins: ["u1"], leaves: [] },
|
|
180
|
+
])
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
it("sends no further join once the refused handle leaves", async () => {
|
|
184
|
+
const first = await joinedForBroadcast("room:x")
|
|
185
|
+
const rows = harness.realtime.channel("room:x").on("postgres_changes", { event: "INSERT", table: "note" }, () => {}).subscribe()
|
|
186
|
+
refuseNewestJoin("room:x")
|
|
187
|
+
rows.unsubscribe()
|
|
188
|
+
expect(subscribeFrames(harness.socket(), "room:x")).toHaveLength(2)
|
|
189
|
+
expect(first.statuses.at(-1)).toBe("SUBSCRIBED")
|
|
190
|
+
})
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
describe("a row listener added to a channel whose join already delivers rows", () => {
|
|
194
|
+
it("sends no second join", async () => {
|
|
195
|
+
harness.realtime.channel("room:x").on("postgres_changes", { event: "INSERT", table: "note" }, () => {}).subscribe()
|
|
196
|
+
await harness.settle()
|
|
197
|
+
ackNewestJoin(harness.socket(), "room:x")
|
|
198
|
+
|
|
199
|
+
const statuses: ChannelStatus[] = []
|
|
200
|
+
harness.realtime.channel("room:x").onBroadcast("ping", () => {}).on("postgres_changes", { event: "*", table: "note" }, () => {}).subscribe((s) => statuses.push(s))
|
|
201
|
+
|
|
202
|
+
expect(subscribeFrames(harness.socket(), "room:x")).toHaveLength(1)
|
|
203
|
+
expect(statuses).toEqual(["SUBSCRIBED"])
|
|
204
|
+
})
|
|
205
|
+
})
|