@supatype/client 0.9.2 → 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 -77
- package/.turbo/turbo-typecheck.log +2 -2
- package/CHANGELOG.md +123 -20
- package/README.md +18 -0
- package/dist/auth.d.ts +4 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +38 -13
- package/dist/auth.js.map +1 -1
- package/dist/bindings.d.ts +3 -0
- package/dist/bindings.d.ts.map +1 -1
- package/dist/bindings.js +1 -0
- 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 +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.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.js +1 -1
- 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 +70 -2
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +198 -18
- 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 +5 -0
- package/dist/runtime-env.js.map +1 -1
- package/dist/table-subscription.d.ts +23 -9
- package/dist/table-subscription.d.ts.map +1 -1
- package/dist/table-subscription.js +155 -23
- package/dist/table-subscription.js.map +1 -1
- package/dist/types.d.ts +29 -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 +40 -12
- package/src/bindings.ts +3 -0
- 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 +13 -1
- package/src/public-env.ts +22 -0
- package/src/query-cache.ts +2 -1
- package/src/query.ts +1 -1
- package/src/realtime-refusals.ts +100 -0
- package/src/realtime.ts +256 -20
- package/src/runtime-env.ts +6 -0
- package/src/table-subscription.ts +186 -30
- package/src/types.ts +35 -1
- package/tests/auth-restore-user.test.ts +84 -0
- 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/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/public-env.test.ts +43 -0
- package/tests/query-cache.test.ts +1 -1
- package/tests/realtime-connection.test.ts +228 -0
- package/tests/realtime-exact-values.test.ts +42 -0
- package/tests/realtime-private-channels.test.ts +377 -0
- package/tests/realtime-refusal-error.test.ts +2 -1
- package/tests/realtime-typed-channels.test.ts +49 -0
- package/tests/runtime-env.test.ts +14 -0
- package/tests/table-subscription-rows.test.ts +136 -0
- package/tests/table-subscription.test.ts +4 -3
- 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
|
+
})
|
|
@@ -12,6 +12,7 @@ import { describe, it, expect, afterEach } from "vitest"
|
|
|
12
12
|
import type { ChannelStatus } from "../src/realtime.js"
|
|
13
13
|
import { subscriptionViewOf, watchTable, type SubscriptionView } from "../src/table-subscription.js"
|
|
14
14
|
import { simulateServerMessage, subscribedChannels } from "./helpers/mock-websocket.js"
|
|
15
|
+
import { fakeClient } from "./helpers/fake-client.js"
|
|
15
16
|
import { createSocketSwapHarness, type SocketSwapHarness } from "./helpers/socket-swap.js"
|
|
16
17
|
|
|
17
18
|
const REASON = "table public.notes does not exist: check the table name and schema"
|
|
@@ -59,7 +60,7 @@ describe("a refused join", () => {
|
|
|
59
60
|
it("reaches the bindings' view as the error message", async () => {
|
|
60
61
|
harness = createSocketSwapHarness()
|
|
61
62
|
const views: SubscriptionView[] = []
|
|
62
|
-
const stop = watchTable(harness.
|
|
63
|
+
const stop = watchTable(fakeClient(harness).client, "notes", undefined, {
|
|
63
64
|
rows: () => undefined,
|
|
64
65
|
status: (view) => views.push(view),
|
|
65
66
|
})
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed channel names: a declared pattern autocompletes as a template literal type, and every
|
|
3
|
+
* other string is still a valid name, since an `open` server admits it.
|
|
4
|
+
*
|
|
5
|
+
* Most of this file is checked by `tsc` (the package's typecheck script), not by vitest: a
|
|
6
|
+
* `@ts-expect-error` that stops being an error fails the typecheck.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, it, expect } from "vitest"
|
|
9
|
+
import type { ChannelName, DeclaredTopic, DeclaredTopics } from "../src/index.js"
|
|
10
|
+
|
|
11
|
+
interface Declared {
|
|
12
|
+
"lot:{id}": { params: { id: string } }
|
|
13
|
+
"lobby:*": { params: Record<string, never> }
|
|
14
|
+
"org:{org}:room:{n}": { params: { org: string; n: number } }
|
|
15
|
+
plain: { params: Record<string, never> }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type Topic = DeclaredTopics<Declared>
|
|
19
|
+
|
|
20
|
+
const ok = (topic: Topic): Topic => topic
|
|
21
|
+
|
|
22
|
+
describe("DeclaredTopics", () => {
|
|
23
|
+
it("accepts a topic built from each declared pattern", () => {
|
|
24
|
+
const topics = [
|
|
25
|
+
ok("lot:7f3c"),
|
|
26
|
+
ok("lobby:anything"),
|
|
27
|
+
ok("org:acme:room:12"),
|
|
28
|
+
ok("plain"),
|
|
29
|
+
]
|
|
30
|
+
expect(topics).toHaveLength(4)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it("rejects a topic no declaration can match", () => {
|
|
34
|
+
// @ts-expect-error not a declared topic
|
|
35
|
+
ok("auction:1")
|
|
36
|
+
// @ts-expect-error an Integer parameter takes a number, not words
|
|
37
|
+
ok("org:acme:room:twelve")
|
|
38
|
+
// @ts-expect-error the literal prefix must be there
|
|
39
|
+
ok("other:7f3c")
|
|
40
|
+
expect(true).toBe(true)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it("is never for a project that declares nothing, so ChannelName stays an open string", () => {
|
|
44
|
+
const none: DeclaredTopics<Record<never, never>> extends never ? true : false = true
|
|
45
|
+
const open: ChannelName = "anything:at:all"
|
|
46
|
+
const declared: DeclaredTopic extends string ? true : false = true
|
|
47
|
+
expect([none, open, declared]).toEqual([true, "anything:at:all", true])
|
|
48
|
+
})
|
|
49
|
+
})
|
|
@@ -257,3 +257,17 @@ describe("an address ending in /", () => {
|
|
|
257
257
|
expect(paths[0]).toMatch(/^\/api\/rest\/v1\/posts/)
|
|
258
258
|
})
|
|
259
259
|
})
|
|
260
|
+
|
|
261
|
+
describe("client.anonKey", () => {
|
|
262
|
+
it("is the key as given", () => {
|
|
263
|
+
expect(createClient({ url: "https://a.example.com", anonKey: "given-key" }).anonKey).toBe("given-key")
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
it("is empty until the page sets one, then the page's, read when asked", () => {
|
|
267
|
+
const client = createClient({ url: "", anonKey: undefined })
|
|
268
|
+
expect(client.anonKey).toBe("")
|
|
269
|
+
|
|
270
|
+
vi.stubGlobal("__SUPATYPE_ENV__", DEPLOYED)
|
|
271
|
+
expect(client.anonKey).toBe(DEPLOYED.anonKey)
|
|
272
|
+
})
|
|
273
|
+
})
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows a table subscription keeps: seeded from a read, upserted by key, and read again after a
|
|
3
|
+
* drop.
|
|
4
|
+
*
|
|
5
|
+
* `watchTable` started from an empty list and never read the table, and matched on `id` only for an
|
|
6
|
+
* update or a delete. So an UPDATE or DELETE of a row that existed before the subscription did
|
|
7
|
+
* nothing, an INSERT delivered twice listed the row twice, and anything changed while the socket was
|
|
8
|
+
* down was lost for good.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest"
|
|
12
|
+
import { applyRowChange, watchTable } from "../src/bindings.js"
|
|
13
|
+
import type { RealtimePayload } from "../src/index.js"
|
|
14
|
+
import { simulateServerMessage, subscribedChannels } from "./helpers/mock-websocket.js"
|
|
15
|
+
import { closeEvent } from "./helpers/close-event.js"
|
|
16
|
+
import { fakeClient } from "./helpers/fake-client.js"
|
|
17
|
+
import { createSocketSwapHarness, type SocketSwapHarness } from "./helpers/socket-swap.js"
|
|
18
|
+
|
|
19
|
+
interface Row {
|
|
20
|
+
id: number
|
|
21
|
+
v?: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const change = (eventType: RealtimePayload<Row>["eventType"], row: Row | null, old: Row | null = null): RealtimePayload<Row> => ({
|
|
25
|
+
eventType,
|
|
26
|
+
new: row,
|
|
27
|
+
old,
|
|
28
|
+
schema: "public",
|
|
29
|
+
table: "task",
|
|
30
|
+
commitTimestamp: "2024-01-01T00:00:00Z",
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
describe("applyRowChange", () => {
|
|
34
|
+
it("adds the row of an UPDATE to a row it has not seen", () => {
|
|
35
|
+
expect(applyRowChange<Row>(null, change("UPDATE", { id: 1, v: "b" }, { id: 1 }))).toEqual([{ id: 1, v: "b" }])
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it("lists a row once when its INSERT arrives twice, keeping the later one", () => {
|
|
39
|
+
const once = applyRowChange<Row>(null, change("INSERT", { id: 1, v: "a" }))
|
|
40
|
+
expect(applyRowChange(once, change("INSERT", { id: 1, v: "b" }))).toEqual([{ id: 1, v: "b" }])
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it("leaves the rows alone for a DELETE of a row it has not seen", () => {
|
|
44
|
+
const rows = [{ id: 2, v: "x" }]
|
|
45
|
+
expect(applyRowChange(rows, change("DELETE", null, { id: 1 }))).toEqual(rows)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it("removes the row a DELETE names by its key", () => {
|
|
49
|
+
expect(applyRowChange([{ id: 1, v: "a" }, { id: 2, v: "b" }], change("DELETE", null, { id: 1 }))).toEqual([{ id: 2, v: "b" }])
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it("matches on the key columns it is given, a composite one included", () => {
|
|
53
|
+
const rows = [{ org: 1, user: 1, role: "a" }, { org: 1, user: 2, role: "a" }]
|
|
54
|
+
const update: RealtimePayload<(typeof rows)[number]> = {
|
|
55
|
+
...change("UPDATE", null),
|
|
56
|
+
new: { org: 1, user: 2, role: "b" },
|
|
57
|
+
old: { org: 1, user: 2, role: "a" },
|
|
58
|
+
}
|
|
59
|
+
expect(applyRowChange(rows, update, ["org", "user"])).toEqual([rows[0], { org: 1, user: 2, role: "b" }])
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
let harness: SocketSwapHarness
|
|
64
|
+
beforeEach(() => {
|
|
65
|
+
harness = createSocketSwapHarness()
|
|
66
|
+
})
|
|
67
|
+
afterEach(() => harness.restore())
|
|
68
|
+
|
|
69
|
+
const tick = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 5))
|
|
70
|
+
|
|
71
|
+
function watch(reads: Row[][], filter?: string): { seen: Row[][]; stop: () => void; filters: string[][] } {
|
|
72
|
+
const { client, filters } = fakeClient<Row>(harness, reads)
|
|
73
|
+
const seen: Row[][] = []
|
|
74
|
+
const stop = watchTable<Row>(client, "task", filter === undefined ? undefined : { filter }, {
|
|
75
|
+
rows: (rows) => seen.push(rows),
|
|
76
|
+
status: () => undefined,
|
|
77
|
+
})
|
|
78
|
+
return { seen, stop, filters }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
describe("watchTable seeds its rows", () => {
|
|
82
|
+
it("reads the table once the join is acknowledged and shows those rows", async () => {
|
|
83
|
+
const { seen } = watch([[{ id: 1, v: "a" }]])
|
|
84
|
+
await harness.settle()
|
|
85
|
+
expect(seen).toEqual([])
|
|
86
|
+
|
|
87
|
+
harness.ackJoins()
|
|
88
|
+
await tick()
|
|
89
|
+
expect(seen).toEqual([[{ id: 1, v: "a" }]])
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it("applies the subscription's filter to that read", async () => {
|
|
93
|
+
const { filters } = watch([[]], "room_id=eq.7,score=gte.10")
|
|
94
|
+
await harness.settle()
|
|
95
|
+
harness.ackJoins()
|
|
96
|
+
await tick()
|
|
97
|
+
|
|
98
|
+
expect(filters[0]).toEqual(["room_id eq 7", "score gte 10"])
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it("updates a row that existed before it subscribed", async () => {
|
|
102
|
+
const { seen } = watch([[{ id: 1, v: "a" }]])
|
|
103
|
+
await harness.settle()
|
|
104
|
+
harness.ackJoins()
|
|
105
|
+
await tick()
|
|
106
|
+
|
|
107
|
+
simulateServerMessage(harness.socket(), {
|
|
108
|
+
type: "change",
|
|
109
|
+
channel: subscribedChannels(harness.socket())[0],
|
|
110
|
+
event: "UPDATE",
|
|
111
|
+
payload: { old: { id: 1 }, new: { id: 1, v: "b" } },
|
|
112
|
+
timestamp: "2024-01-01T00:00:00Z",
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
expect(seen[seen.length - 1]).toEqual([{ id: 1, v: "b" }])
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
describe("watchTable after a drop", () => {
|
|
120
|
+
it("reads the table again once the join is back, so a change made while down shows up", async () => {
|
|
121
|
+
const { seen } = watch([[{ id: 1, v: "a" }], [{ id: 1, v: "changed while down" }]])
|
|
122
|
+
await harness.settle()
|
|
123
|
+
harness.ackJoins()
|
|
124
|
+
await tick()
|
|
125
|
+
expect(seen[seen.length - 1]).toEqual([{ id: 1, v: "a" }])
|
|
126
|
+
|
|
127
|
+
harness.socket().onclose?.(closeEvent())
|
|
128
|
+
const before = harness.sockets.length
|
|
129
|
+
await new Promise((resolve) => setTimeout(resolve, 1200))
|
|
130
|
+
expect(harness.sockets.length).toBe(before + 1)
|
|
131
|
+
harness.ackJoins()
|
|
132
|
+
await tick()
|
|
133
|
+
|
|
134
|
+
expect(seen[seen.length - 1]).toEqual([{ id: 1, v: "changed while down" }])
|
|
135
|
+
})
|
|
136
|
+
})
|
|
@@ -9,6 +9,7 @@ import { describe, it, expect, afterEach } from "vitest"
|
|
|
9
9
|
import { applyRowChange, subscriptionViewOf, watchTable, type SubscriptionView } from "../src/table-subscription.js"
|
|
10
10
|
import type { ChannelStatus, RealtimePayload } from "../src/realtime.js"
|
|
11
11
|
import { simulateServerMessage, subscribedChannels } from "./helpers/mock-websocket.js"
|
|
12
|
+
import { fakeClient } from "./helpers/fake-client.js"
|
|
12
13
|
import { createSocketSwapHarness, type SocketSwapHarness } from "./helpers/socket-swap.js"
|
|
13
14
|
|
|
14
15
|
interface Row {
|
|
@@ -69,7 +70,7 @@ describe("watchTable", () => {
|
|
|
69
70
|
harness = createSocketSwapHarness()
|
|
70
71
|
const rowsSeen: Row[][] = []
|
|
71
72
|
const statuses: string[] = []
|
|
72
|
-
const stop = watchTable<Row>(harness.
|
|
73
|
+
const stop = watchTable<Row>(fakeClient<Row>(harness).client, "task", { event: "INSERT" }, {
|
|
73
74
|
rows: (rows) => rowsSeen.push(rows),
|
|
74
75
|
status: (view) => statuses.push(view.status),
|
|
75
76
|
})
|
|
@@ -91,8 +92,8 @@ describe("watchTable", () => {
|
|
|
91
92
|
it("gives two watchers of one table a channel each, as from(table).subscribe() does", async () => {
|
|
92
93
|
harness = createSocketSwapHarness()
|
|
93
94
|
const quiet = { rows: (): void => undefined, status: (): void => undefined }
|
|
94
|
-
const first = watchTable<Row>(harness.
|
|
95
|
-
const second = watchTable<Row>(harness.
|
|
95
|
+
const first = watchTable<Row>(fakeClient<Row>(harness).client, "task", undefined, quiet)
|
|
96
|
+
const second = watchTable<Row>(fakeClient<Row>(harness).client, "task", undefined, quiet)
|
|
96
97
|
await harness.settle()
|
|
97
98
|
|
|
98
99
|
const [a, b] = firstSocketChannels()
|