@supatype/client 0.7.0 → 0.8.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 +67 -55
- package/.turbo/turbo-typecheck.log +1 -1
- package/dist/auth.d.ts +8 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +11 -2
- package/dist/auth.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -10
- package/dist/index.js.map +1 -1
- package/dist/realtime.d.ts +38 -1
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +201 -82
- package/dist/realtime.js.map +1 -1
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +10 -3
- package/dist/storage.js.map +1 -1
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/auth.ts +14 -2
- package/src/index.ts +20 -10
- package/src/realtime.ts +222 -82
- package/src/storage.ts +11 -3
- package/src/types.ts +14 -0
- package/tests/auth.test.ts +65 -0
- package/tests/helpers/socket-swap.ts +3 -0
- package/tests/realtime-ack-ref.test.ts +112 -0
- package/tests/realtime-shared-channel.test.ts +209 -0
- package/tests/server-bodies.test.ts +48 -0
- package/tests/storage-quota-integration.test.ts +4 -7
- package/tests/storage-upload-limit-integration.test.ts +5 -9
- package/tests/storage-url-encoding.test.ts +52 -0
- package/tests/support/blob.ts +10 -0
- package/tests/table-subscribe.test.ts +28 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A server reply is matched to the join that asked for it, not to whatever join on that channel
|
|
3
|
+
* name happens to be pending when it arrives.
|
|
4
|
+
*
|
|
5
|
+
* Replies used to be read by channel name alone. Two joins on one name close together (a rejoin
|
|
6
|
+
* after the joining handle leaves, or a leave followed at once by a new join) each got the other's
|
|
7
|
+
* answer: the first join's acknowledgement reported SUBSCRIBED for a second join the server went on
|
|
8
|
+
* to refuse, and the first join's refusal put a handle whose own join succeeded into CHANNEL_ERROR.
|
|
9
|
+
*
|
|
10
|
+
* The fake server here answers every frame in the order it was sent, as the real one does, and
|
|
11
|
+
* echoes the frame's `ref` when it carried one.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest"
|
|
15
|
+
import type { ChannelStatus } from "../src/realtime.js"
|
|
16
|
+
import { channelOf, sentFrames, simulateServerMessage } from "./helpers/mock-websocket.js"
|
|
17
|
+
import { createSocketSwapHarness, type SocketSwapHarness } from "./helpers/socket-swap.js"
|
|
18
|
+
|
|
19
|
+
const CHANNEL = "x"
|
|
20
|
+
const BAD_FILTER = "status=like.open"
|
|
21
|
+
|
|
22
|
+
let harness: SocketSwapHarness
|
|
23
|
+
let answered = 0
|
|
24
|
+
|
|
25
|
+
/** The server's reply to one subscribe or unsubscribe frame, with a join's ref when it had one. */
|
|
26
|
+
function replyTo(frame: Record<string, unknown>, echoRef: boolean): Record<string, unknown> {
|
|
27
|
+
const channel = channelOf(frame)
|
|
28
|
+
if (frame["type"] === "unsubscribe") return { type: "system", status: "ok", message: `unsubscribed from ${channel}` }
|
|
29
|
+
const ref = echoRef && typeof frame["ref"] === "string" ? { ref: frame["ref"] } : {}
|
|
30
|
+
const filter = frame["filter"]
|
|
31
|
+
const refused = typeof filter === "object" && filter !== null && "status" in filter
|
|
32
|
+
if (refused) return { type: "system", status: "error", message: `invalid filter ${BAD_FILTER}`, channel, ...ref }
|
|
33
|
+
return { type: "system", status: "ok", message: `subscribed to ${channel}`, ...ref }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Answer every join and leave sent since the last call, in the order they went out. */
|
|
37
|
+
function answerInOrder({ echoRef = true }: { echoRef?: boolean } = {}): void {
|
|
38
|
+
const pending = sentFrames(harness.socket())
|
|
39
|
+
.filter((frame) => frame["type"] === "subscribe" || frame["type"] === "unsubscribe")
|
|
40
|
+
.slice(answered)
|
|
41
|
+
answered += pending.length
|
|
42
|
+
for (const frame of pending) simulateServerMessage(harness.socket(), replyTo(frame, echoRef))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
harness = createSocketSwapHarness()
|
|
47
|
+
answered = 0
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
afterEach(() => harness.restore())
|
|
51
|
+
|
|
52
|
+
describe("a rejoin sent before the join it replaces is answered", () => {
|
|
53
|
+
it("is not reported SUBSCRIBED by the earlier join's acknowledgement when it is itself refused", async () => {
|
|
54
|
+
const statuses: ChannelStatus[] = []
|
|
55
|
+
const a = harness.realtime.channel(CHANNEL).on("postgres_changes", { event: "*", table: "audit_log" }, () => {}).subscribe()
|
|
56
|
+
harness.realtime
|
|
57
|
+
.channel(CHANNEL)
|
|
58
|
+
.on("postgres_changes", { event: "*", table: "messages", filter: BAD_FILTER }, () => {})
|
|
59
|
+
.subscribe((s) => statuses.push(s))
|
|
60
|
+
await harness.settle()
|
|
61
|
+
|
|
62
|
+
// The first join is still unanswered when its handle leaves, so the channel joins again.
|
|
63
|
+
a.unsubscribe()
|
|
64
|
+
answerInOrder()
|
|
65
|
+
|
|
66
|
+
expect(statuses).toEqual(["SUBSCRIBING", "CHANNEL_ERROR"])
|
|
67
|
+
// Nothing on this channel is subscribed, so a handle arriving now is not told it is.
|
|
68
|
+
const late: ChannelStatus[] = []
|
|
69
|
+
harness.realtime.channel(CHANNEL).subscribe((s) => late.push(s))
|
|
70
|
+
expect(late).toEqual([])
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
describe("a join sent right after the channel was left", () => {
|
|
75
|
+
function join(filter: string | undefined, onStatus?: (status: ChannelStatus) => void): { unsubscribe(): void } {
|
|
76
|
+
return harness.realtime.channel(CHANNEL).on("postgres_changes", { event: "*", table: "t", filter }, () => {}).subscribe(onStatus)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function leaveThenJoin(firstFilter: string | undefined, secondFilter: string | undefined): Promise<ChannelStatus[]> {
|
|
80
|
+
// Another channel keeps the socket open across the leave.
|
|
81
|
+
harness.realtime.channel("keep").subscribe()
|
|
82
|
+
const first = join(firstFilter)
|
|
83
|
+
await harness.settle()
|
|
84
|
+
first.unsubscribe()
|
|
85
|
+
const statuses: ChannelStatus[] = []
|
|
86
|
+
join(secondFilter, (s) => statuses.push(s))
|
|
87
|
+
answerInOrder()
|
|
88
|
+
return statuses
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
it("is not put into CHANNEL_ERROR by the refusal of the join before it", async () => {
|
|
92
|
+
expect(await leaveThenJoin(BAD_FILTER, undefined)).toEqual(["SUBSCRIBED"])
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it("is not reported SUBSCRIBED by the acknowledgement of the join before it", async () => {
|
|
96
|
+
expect(await leaveThenJoin(undefined, BAD_FILTER)).toEqual(["CHANNEL_ERROR"])
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
describe("a server that echoes no ref", () => {
|
|
101
|
+
it("still has its replies read by channel name, as before refs existed", async () => {
|
|
102
|
+
const statuses: ChannelStatus[] = []
|
|
103
|
+
harness.realtime.channel(CHANNEL).subscribe((s) => statuses.push(s))
|
|
104
|
+
await harness.settle()
|
|
105
|
+
|
|
106
|
+
const join = sentFrames(harness.socket()).find((frame) => frame["type"] === "subscribe")
|
|
107
|
+
expect(typeof join?.["ref"]).toBe("string")
|
|
108
|
+
answerInOrder({ echoRef: false })
|
|
109
|
+
|
|
110
|
+
expect(statuses).toEqual(["SUBSCRIBED"])
|
|
111
|
+
})
|
|
112
|
+
})
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two handles on one channel name are independent until the last one leaves.
|
|
3
|
+
*
|
|
4
|
+
* `channel(name)` handed every caller the same state: the second `subscribe()` replaced the first
|
|
5
|
+
* one's status callback, and the first `unsubscribe()` told the server to leave and deleted the
|
|
6
|
+
* channel, listeners and all. Two `useSubscription` hooks with one channel name broke each other:
|
|
7
|
+
* when one unmounted, the other stopped receiving changes while still reading SUBSCRIBED.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest"
|
|
11
|
+
import type { ChannelStatus, RealtimePayload } from "../src/realtime.js"
|
|
12
|
+
import { sentFrames, simulateServerMessage } from "./helpers/mock-websocket.js"
|
|
13
|
+
import { createSocketSwapHarness, type SocketSwapHarness } from "./helpers/socket-swap.js"
|
|
14
|
+
|
|
15
|
+
const CHANNEL = "x"
|
|
16
|
+
|
|
17
|
+
let harness: SocketSwapHarness
|
|
18
|
+
|
|
19
|
+
function unsubscribeFrames(): Array<Record<string, unknown>> {
|
|
20
|
+
return sentFrames(harness.socket()).filter((frame) => frame["type"] === "unsubscribe" && frame["channel"] === CHANNEL)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function deliverInsert(id: number): void {
|
|
24
|
+
simulateServerMessage(harness.socket(), {
|
|
25
|
+
type: "change",
|
|
26
|
+
channel: CHANNEL,
|
|
27
|
+
event: "INSERT",
|
|
28
|
+
payload: { old: null, new: { id } },
|
|
29
|
+
timestamp: "2026-10-28T00:00:00Z",
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
harness = createSocketSwapHarness()
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
afterEach(() => harness.restore())
|
|
38
|
+
|
|
39
|
+
describe("two handles on one channel name", () => {
|
|
40
|
+
it("keeps the second receiving changes once the first unsubscribes", async () => {
|
|
41
|
+
const first: Array<RealtimePayload<Record<string, unknown>>> = []
|
|
42
|
+
const second: Array<RealtimePayload<Record<string, unknown>>> = []
|
|
43
|
+
const a = harness.realtime.channel(CHANNEL).on("postgres_changes", { event: "*", table: "t" }, (p) => first.push(p)).subscribe()
|
|
44
|
+
harness.realtime.channel(CHANNEL).on("postgres_changes", { event: "*", table: "t" }, (p) => second.push(p)).subscribe()
|
|
45
|
+
await harness.settle()
|
|
46
|
+
harness.ackJoins()
|
|
47
|
+
|
|
48
|
+
a.unsubscribe()
|
|
49
|
+
deliverInsert(1)
|
|
50
|
+
|
|
51
|
+
expect(first).toEqual([])
|
|
52
|
+
expect(second.map((p) => p.new)).toEqual([{ id: 1 }])
|
|
53
|
+
expect(unsubscribeFrames()).toEqual([])
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it("tells the server to leave only when the last handle unsubscribes", async () => {
|
|
57
|
+
const a = harness.realtime.channel(CHANNEL).subscribe()
|
|
58
|
+
const b = harness.realtime.channel(CHANNEL).subscribe()
|
|
59
|
+
await harness.settle()
|
|
60
|
+
harness.ackJoins()
|
|
61
|
+
|
|
62
|
+
a.unsubscribe()
|
|
63
|
+
expect(unsubscribeFrames()).toEqual([])
|
|
64
|
+
expect(harness.socket().close).not.toHaveBeenCalled()
|
|
65
|
+
|
|
66
|
+
b.unsubscribe()
|
|
67
|
+
expect(unsubscribeFrames()).toHaveLength(1)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it("reports status to each handle's own callback", async () => {
|
|
71
|
+
const first: ChannelStatus[] = []
|
|
72
|
+
const second: ChannelStatus[] = []
|
|
73
|
+
harness.realtime.channel(CHANNEL).subscribe((s) => first.push(s))
|
|
74
|
+
harness.realtime.channel(CHANNEL).subscribe((s) => second.push(s))
|
|
75
|
+
await harness.settle()
|
|
76
|
+
harness.ackJoins()
|
|
77
|
+
|
|
78
|
+
expect(first).toEqual(["SUBSCRIBED"])
|
|
79
|
+
expect(second).toEqual(["SUBSCRIBED"])
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it("stops a handle's presence and broadcast listeners when it unsubscribes, and only its own", async () => {
|
|
83
|
+
const heard: string[] = []
|
|
84
|
+
const a = harness.realtime.channel(CHANNEL)
|
|
85
|
+
.onBroadcast("ping", () => heard.push("a"))
|
|
86
|
+
.onPresence(() => heard.push("a-presence"))
|
|
87
|
+
.subscribe()
|
|
88
|
+
harness.realtime.channel(CHANNEL)
|
|
89
|
+
.onBroadcast("ping", () => heard.push("b"))
|
|
90
|
+
.onPresence(() => heard.push("b-presence"))
|
|
91
|
+
.subscribe()
|
|
92
|
+
await harness.settle()
|
|
93
|
+
harness.ackJoins()
|
|
94
|
+
|
|
95
|
+
a.unsubscribe()
|
|
96
|
+
simulateServerMessage(harness.socket(), { type: "broadcast", channel: CHANNEL, event: "ping", payload: {} })
|
|
97
|
+
simulateServerMessage(harness.socket(), { type: "presence", channel: CHANNEL, joins: [{ user_id: "u" }], leaves: [] })
|
|
98
|
+
|
|
99
|
+
expect(heard).toEqual(["b", "b-presence"])
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The join is made once, with the oldest handle's first row listener, and stays registered for that
|
|
105
|
+
* table after that handle leaves. Rows used to be matched and labelled against whichever listener
|
|
106
|
+
* was first now, so once the oldest handle unsubscribed, the next handle's listener for another
|
|
107
|
+
* table was handed this table's rows under its own table's name.
|
|
108
|
+
*/
|
|
109
|
+
describe("rows on a channel whose joining handle has left", () => {
|
|
110
|
+
it("go to listeners for the joined table, labelled with it, and not to one for another table", async () => {
|
|
111
|
+
const audit: Array<RealtimePayload<Record<string, unknown>>> = []
|
|
112
|
+
const messages: Array<RealtimePayload<Record<string, unknown>>> = []
|
|
113
|
+
const a = harness.realtime.channel(CHANNEL).on("postgres_changes", { event: "*", table: "messages" }, () => {}).subscribe()
|
|
114
|
+
harness.realtime.channel(CHANNEL).on("postgres_changes", { event: "*", table: "audit_log" }, (p) => audit.push(p)).subscribe()
|
|
115
|
+
harness.realtime.channel(CHANNEL).on("postgres_changes", { event: "*", table: "messages" }, (p) => messages.push(p)).subscribe()
|
|
116
|
+
await harness.settle()
|
|
117
|
+
harness.ackJoins()
|
|
118
|
+
|
|
119
|
+
a.unsubscribe()
|
|
120
|
+
deliverInsert(1)
|
|
121
|
+
|
|
122
|
+
expect(audit).toEqual([])
|
|
123
|
+
expect(messages.map((p) => [p.table, p.new])).toEqual([["messages", { id: 1 }]])
|
|
124
|
+
})
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
function subscribeFrames(): Array<Record<string, unknown>> {
|
|
128
|
+
return sentFrames(harness.socket()).filter((frame) => frame["type"] === "subscribe" && frame["channel"] === CHANNEL)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function ackJoin(channel: string): void {
|
|
132
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "ok", message: `subscribed to ${channel}` })
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* When the handle whose listener made the join leaves and no remaining listener is for that join,
|
|
137
|
+
* the channel joins again for the oldest remaining listener. It used to stay registered for the
|
|
138
|
+
* table the leaver listened to, and the others reported SUBSCRIBED and were sent nothing.
|
|
139
|
+
*/
|
|
140
|
+
describe("a channel whose joining handle leaves while the others listen to another table", () => {
|
|
141
|
+
it("joins again for the remaining handle's table, event and filter, and delivers its rows", async () => {
|
|
142
|
+
const statuses: ChannelStatus[] = []
|
|
143
|
+
const messages: Array<RealtimePayload<Record<string, unknown>>> = []
|
|
144
|
+
const a = harness.realtime.channel(CHANNEL).on("postgres_changes", { event: "*", table: "audit_log" }, () => {}).subscribe()
|
|
145
|
+
harness.realtime
|
|
146
|
+
.channel(CHANNEL)
|
|
147
|
+
.on("postgres_changes", { event: "INSERT", table: "messages", filter: "room=eq.1" }, (p) => messages.push(p))
|
|
148
|
+
.subscribe((s) => statuses.push(s))
|
|
149
|
+
await harness.settle()
|
|
150
|
+
harness.ackJoins()
|
|
151
|
+
|
|
152
|
+
a.unsubscribe()
|
|
153
|
+
const joins = subscribeFrames()
|
|
154
|
+
expect(joins).toHaveLength(2)
|
|
155
|
+
expect(joins[1]).toMatchObject({ schema: "public", table: "messages", event: "INSERT", filter: { room: "eq.1" } })
|
|
156
|
+
// A join on a channel already joined replaces it on the server; leaving first would drop the
|
|
157
|
+
// presence this socket tracks there.
|
|
158
|
+
expect(unsubscribeFrames()).toEqual([])
|
|
159
|
+
|
|
160
|
+
ackJoin(CHANNEL)
|
|
161
|
+
deliverInsert(1)
|
|
162
|
+
expect(messages.map((p) => [p.table, p.new])).toEqual([["messages", { id: 1 }]])
|
|
163
|
+
expect(statuses).toEqual(["SUBSCRIBED", "SUBSCRIBING", "SUBSCRIBED"])
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it("does not hand the new table's listeners a row that arrives before the new join is acknowledged", async () => {
|
|
167
|
+
const messages: Array<RealtimePayload<Record<string, unknown>>> = []
|
|
168
|
+
const a = harness.realtime.channel(CHANNEL).on("postgres_changes", { event: "*", table: "audit_log" }, () => {}).subscribe()
|
|
169
|
+
harness.realtime.channel(CHANNEL).on("postgres_changes", { event: "*", table: "messages" }, (p) => messages.push(p)).subscribe()
|
|
170
|
+
await harness.settle()
|
|
171
|
+
harness.ackJoins()
|
|
172
|
+
|
|
173
|
+
a.unsubscribe()
|
|
174
|
+
// Sent before the server took the new join: an audit_log row.
|
|
175
|
+
deliverInsert(1)
|
|
176
|
+
ackJoin(CHANNEL)
|
|
177
|
+
deliverInsert(2)
|
|
178
|
+
expect(messages.map((p) => p.new)).toEqual([{ id: 2 }])
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* A join sent with no row listener names no table, and the server reads one from the channel name.
|
|
184
|
+
* Rows were matched and labelled against whichever row listener came first instead, so a listener
|
|
185
|
+
* for another table added afterwards was handed the channel's rows under its own table's name.
|
|
186
|
+
*/
|
|
187
|
+
describe("rows on a channel joined before any row listener", () => {
|
|
188
|
+
it("belong to the table the channel name gives, as the server reads it", async () => {
|
|
189
|
+
const invoices: Array<RealtimePayload<Record<string, unknown>>> = []
|
|
190
|
+
const orders: Array<RealtimePayload<Record<string, unknown>>> = []
|
|
191
|
+
const name = "public:orders"
|
|
192
|
+
const handle = harness.realtime.channel(name).onBroadcast("ping", () => {}).subscribe()
|
|
193
|
+
await harness.settle()
|
|
194
|
+
harness.ackJoins()
|
|
195
|
+
|
|
196
|
+
handle.on("postgres_changes", { event: "*", table: "invoices" }, (p) => invoices.push(p))
|
|
197
|
+
handle.on("postgres_changes", { event: "*", table: "orders" }, (p) => orders.push(p))
|
|
198
|
+
simulateServerMessage(harness.socket(), {
|
|
199
|
+
type: "change",
|
|
200
|
+
channel: name,
|
|
201
|
+
event: "INSERT",
|
|
202
|
+
payload: { old: null, new: { id: 1 } },
|
|
203
|
+
timestamp: "2026-10-28T00:00:00Z",
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
expect(invoices).toEqual([])
|
|
207
|
+
expect(orders.map((p) => [p.schema, p.table, p.new])).toEqual([["public", "orders", { id: 1 }]])
|
|
208
|
+
})
|
|
209
|
+
})
|
|
@@ -137,3 +137,51 @@ describe("a refusal worded under `error`", () => {
|
|
|
137
137
|
expect(error?.message).toBe(REFUSED)
|
|
138
138
|
})
|
|
139
139
|
})
|
|
140
|
+
|
|
141
|
+
describe("a functions worker refusal", () => {
|
|
142
|
+
// The worker answers its own failures as `{ error: <code>, message }`; the code is under `error`.
|
|
143
|
+
const client = (): ReturnType<typeof createClient> =>
|
|
144
|
+
createClient({ url: "http://api.test", anonKey: "anon", auth: { persistSession: false } })
|
|
145
|
+
|
|
146
|
+
it("carries the worker's code for a function that does not exist", async () => {
|
|
147
|
+
vi.stubGlobal("fetch", fetchReturning({ error: "not_found", message: 'Function "nope" not found' }, 404))
|
|
148
|
+
const { error } = await client().functions.invoke("nope")
|
|
149
|
+
expect(error).toMatchObject({ code: "not_found", message: 'Function "nope" not found', status: 404 })
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
it("carries the worker's code for a handler that threw", async () => {
|
|
153
|
+
vi.stubGlobal("fetch", fetchReturning({ error: "function_error", message: "boom" }, 500))
|
|
154
|
+
const { error } = await client().functions.invoke("fn")
|
|
155
|
+
expect(error).toMatchObject({ code: "function_error", message: "boom", status: 500 })
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
it("keeps the worker's code when the thrown error had no message", async () => {
|
|
159
|
+
vi.stubGlobal("fetch", fetchReturning({ error: "function_error", message: "" }, 500))
|
|
160
|
+
const { error } = await client().functions.invoke("fn")
|
|
161
|
+
expect(error).toMatchObject({ code: "function_error", status: 500 })
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
it("prefers a handler's own `code` over `error`", async () => {
|
|
165
|
+
vi.stubGlobal("fetch", fetchReturning({ code: "out_of_stock", error: "conflict", message: "gone" }, 409))
|
|
166
|
+
const { error } = await client().functions.invoke("fn")
|
|
167
|
+
expect(error?.code).toBe("out_of_stock")
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it("reads a handler's lone `error` string as the message, not the code", async () => {
|
|
171
|
+
vi.stubGlobal("fetch", fetchReturning({ error: "Email already taken" }, 400))
|
|
172
|
+
const { error } = await client().functions.invoke("fn")
|
|
173
|
+
expect(error).toEqual({ message: "Email already taken", status: 400 })
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it("reads the message the way every other refusal is read, `msg` and an empty `message` included", async () => {
|
|
177
|
+
vi.stubGlobal("fetch", fetchReturning({ message: "", msg: "Rate limited" }, 429))
|
|
178
|
+
const { error } = await client().functions.invoke("fn")
|
|
179
|
+
expect(error).toEqual({ message: "Rate limited", status: 429 })
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
it("leaves the code unset when `error` is not a string", async () => {
|
|
183
|
+
vi.stubGlobal("fetch", fetchReturning({ error: { detail: "x" }, message: "bad" }, 400))
|
|
184
|
+
const { error } = await client().functions.invoke("fn")
|
|
185
|
+
expect(error).not.toHaveProperty("code")
|
|
186
|
+
})
|
|
187
|
+
})
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { describe, it, expect, vi, beforeEach } from "vitest"
|
|
8
8
|
import { StorageClient } from "../src/storage.js"
|
|
9
9
|
import { UNOWNED } from "./support/storage-object.js"
|
|
10
|
+
import { createMockBlob } from "./support/blob.js"
|
|
10
11
|
|
|
11
12
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
12
13
|
|
|
@@ -18,10 +19,6 @@ function freshClient(): StorageClient {
|
|
|
18
19
|
return new StorageClient(STORAGE_URL, () => Promise.resolve(HEADERS))
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
function createMockBlob(sizeBytes: number): Blob {
|
|
22
|
-
return new Blob([new Uint8Array(sizeBytes)])
|
|
23
|
-
}
|
|
24
|
-
|
|
25
22
|
function mockFetchResponse(
|
|
26
23
|
status: number,
|
|
27
24
|
body: unknown,
|
|
@@ -49,7 +46,7 @@ describe("Task 93: Storage quota integration", () => {
|
|
|
49
46
|
}, false))
|
|
50
47
|
|
|
51
48
|
const client = freshClient()
|
|
52
|
-
const blob = createMockBlob(
|
|
49
|
+
const blob = createMockBlob(1024)
|
|
53
50
|
const { data, error } = await client.from("uploads").upload("file.dat", blob)
|
|
54
51
|
|
|
55
52
|
expect(data).toBeNull()
|
|
@@ -115,10 +112,10 @@ describe("Task 93: Storage quota integration", () => {
|
|
|
115
112
|
|
|
116
113
|
const client = freshClient()
|
|
117
114
|
|
|
118
|
-
const first = await client.from("bucket").upload("first.txt", createMockBlob(
|
|
115
|
+
const first = await client.from("bucket").upload("first.txt", createMockBlob(1024))
|
|
119
116
|
expect(first.error).toBeNull()
|
|
120
117
|
|
|
121
|
-
const second = await client.from("bucket").upload("second.txt", createMockBlob(
|
|
118
|
+
const second = await client.from("bucket").upload("second.txt", createMockBlob(1024))
|
|
122
119
|
expect(second.data).toBeNull()
|
|
123
120
|
expect(second.error!.status).toBe(507)
|
|
124
121
|
})
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { describe, it, expect, vi, beforeEach } from "vitest"
|
|
8
8
|
import { StorageClient } from "../src/storage.js"
|
|
9
9
|
import { fetchCall, fetchReturning, header, type FetchMock } from "./support/fetch-mock.js"
|
|
10
|
+
import { createMockBlob } from "./support/blob.js"
|
|
10
11
|
|
|
11
12
|
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
12
13
|
|
|
@@ -18,11 +19,6 @@ function freshClient(): StorageClient {
|
|
|
18
19
|
return new StorageClient(STORAGE_URL, () => Promise.resolve(HEADERS))
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
function createMockBlob(sizeBytes: number): Blob {
|
|
22
|
-
const data = new Uint8Array(sizeBytes)
|
|
23
|
-
return new Blob([data])
|
|
24
|
-
}
|
|
25
|
-
|
|
26
22
|
function mockFetchResponse(status: number, body: unknown): FetchMock {
|
|
27
23
|
return fetchReturning(body, status)
|
|
28
24
|
}
|
|
@@ -49,7 +45,7 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
49
45
|
vi.stubGlobal("fetch", mockFetchResponse(200, { Key: "docs/file.pdf" }))
|
|
50
46
|
|
|
51
47
|
const client = freshClient()
|
|
52
|
-
const blob = createMockBlob(
|
|
48
|
+
const blob = createMockBlob(1024) // the mocked server's answer stands for a file at the limit
|
|
53
49
|
const { data, error } = await client.from("docs").upload("file.pdf", blob, {
|
|
54
50
|
contentType: "application/pdf",
|
|
55
51
|
})
|
|
@@ -68,7 +64,7 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
68
64
|
}))
|
|
69
65
|
|
|
70
66
|
const client = freshClient()
|
|
71
|
-
const blob = createMockBlob(
|
|
67
|
+
const blob = createMockBlob(1024) // the mocked 413 stands for a file over the limit
|
|
72
68
|
const { data, error } = await client.from("uploads").upload("large-video.mp4", blob)
|
|
73
69
|
|
|
74
70
|
expect(data).toBeNull()
|
|
@@ -83,7 +79,7 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
83
79
|
}))
|
|
84
80
|
|
|
85
81
|
const client = freshClient()
|
|
86
|
-
const { error } = await client.from("uploads").upload("huge.zip", createMockBlob(
|
|
82
|
+
const { error } = await client.from("uploads").upload("huge.zip", createMockBlob(1024))
|
|
87
83
|
|
|
88
84
|
expect(error!.message).toContain("50MB")
|
|
89
85
|
})
|
|
@@ -94,7 +90,7 @@ describe("Task 92: File upload size limit integration", () => {
|
|
|
94
90
|
}))
|
|
95
91
|
|
|
96
92
|
const client = freshClient()
|
|
97
|
-
const blob = createMockBlob(
|
|
93
|
+
const blob = createMockBlob(1024) // the mocked 413 stands for one byte over
|
|
98
94
|
const { data, error } = await client.from("uploads").upload("slightly-too-big.dat", blob)
|
|
99
95
|
|
|
100
96
|
expect(data).toBeNull()
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An object path goes into a URL one encoded segment at a time.
|
|
3
|
+
*
|
|
4
|
+
* The client interpolated the path as given, so a name holding `?` or `#` cut the URL short: the
|
|
5
|
+
* server saw `a b/c` and a query or fragment it never asked for, and answered for another object
|
|
6
|
+
* or none. The `/` between folders stays a `/`, since the server matches the path on it.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { beforeEach, describe, expect, it, vi } from "vitest"
|
|
10
|
+
import { StorageClient } from "../src/storage.js"
|
|
11
|
+
import { fetchCall, fetchReturning } from "./support/fetch-mock.js"
|
|
12
|
+
|
|
13
|
+
const STORAGE_URL = "http://localhost:18473/storage/v1"
|
|
14
|
+
const NAME = "a b/c?d#e.png"
|
|
15
|
+
const ENCODED = "a%20b/c%3Fd%23e.png"
|
|
16
|
+
|
|
17
|
+
function bucket() {
|
|
18
|
+
return new StorageClient(STORAGE_URL, () => Promise.resolve({})).from("docs")
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
vi.restoreAllMocks()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
describe("object paths in storage URLs", () => {
|
|
26
|
+
it("are encoded per segment in a public URL", () => {
|
|
27
|
+
expect(bucket().getPublicUrl(NAME).data.publicUrl).toBe(`${STORAGE_URL}/object/public/docs/${ENCODED}`)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it("are encoded per segment when asking for a signed URL", async () => {
|
|
31
|
+
const fetch = fetchReturning({ signedURL: `/object/sign/docs/${ENCODED}?token=t` })
|
|
32
|
+
vi.stubGlobal("fetch", fetch)
|
|
33
|
+
await bucket().createSignedUrl(NAME, 60)
|
|
34
|
+
expect(fetchCall(fetch).url).toBe(`${STORAGE_URL}/object/sign/docs/${ENCODED}`)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it("are encoded per segment on upload and download", async () => {
|
|
38
|
+
const fetch = fetchReturning({})
|
|
39
|
+
vi.stubGlobal("fetch", fetch)
|
|
40
|
+
await bucket().upload(NAME, new ArrayBuffer(1))
|
|
41
|
+
await bucket().download(NAME)
|
|
42
|
+
expect(fetchCall(fetch, 0).url).toBe(`${STORAGE_URL}/object/docs/${ENCODED}`)
|
|
43
|
+
expect(fetchCall(fetch, 1).url).toBe(`${STORAGE_URL}/object/authenticated/docs/${ENCODED}`)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it("read back as the name they were built from", () => {
|
|
47
|
+
const url = new URL(bucket().getPublicUrl(NAME).data.publicUrl)
|
|
48
|
+
expect(url.search).toBe("")
|
|
49
|
+
expect(url.hash).toBe("")
|
|
50
|
+
expect(decodeURIComponent(url.pathname)).toBe(`/storage/v1/object/public/docs/${NAME}`)
|
|
51
|
+
})
|
|
52
|
+
})
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A file body for upload tests. The client never checks a file's size: the storage server
|
|
3
|
+
* decides limits and quotas, and these suites mock its answer. So the tests upload small blobs,
|
|
4
|
+
* because a real 500 MB one costs real memory and timed out on CI runners.
|
|
5
|
+
*
|
|
6
|
+
* Not named `*.test.ts`, so vitest's default include does not collect it as a suite.
|
|
7
|
+
*/
|
|
8
|
+
export function createMockBlob(sizeBytes: number): Blob {
|
|
9
|
+
return new Blob([new Uint8Array(sizeBytes)])
|
|
10
|
+
}
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
|
|
14
14
|
import { createClient } from "../src/index.js"
|
|
15
|
-
import type { RealtimePayload } from "../src/realtime.js"
|
|
15
|
+
import type { ChannelStatus, RealtimePayload } from "../src/realtime.js"
|
|
16
16
|
import {
|
|
17
17
|
channelOf,
|
|
18
18
|
createMockWebSocketClass,
|
|
@@ -288,3 +288,30 @@ describe("review fixes", () => {
|
|
|
288
288
|
expect(comments).toEqual([])
|
|
289
289
|
})
|
|
290
290
|
})
|
|
291
|
+
|
|
292
|
+
describe("a refusal that names its channel", () => {
|
|
293
|
+
it("fails only that channel, leaving the others on the socket subscribed", async () => {
|
|
294
|
+
// A refused join used to reach every channel on the socket: one bad filter put each healthy,
|
|
295
|
+
// subscribed channel beside it into CHANNEL_ERROR.
|
|
296
|
+
const client = newClient()
|
|
297
|
+
const healthy: ChannelStatus[] = []
|
|
298
|
+
const refused: ChannelStatus[] = []
|
|
299
|
+
client.realtime.channel("posts").on("postgres_changes", { event: "*", table: "posts" }, () => {}).subscribe((s) => healthy.push(s))
|
|
300
|
+
const bad = client.realtime
|
|
301
|
+
.channel("orders")
|
|
302
|
+
.on("postgres_changes", { event: "*", table: "orders", filter: "status=like.open" }, () => {})
|
|
303
|
+
bad.subscribe((s) => refused.push(s))
|
|
304
|
+
await settle()
|
|
305
|
+
const ws = onlySocket()
|
|
306
|
+
|
|
307
|
+
simulateServerMessage(ws, ack("posts"))
|
|
308
|
+
simulateServerMessage(ws, { type: "system", status: "error", message: "invalid filter status=like.open", channel: "orders" })
|
|
309
|
+
|
|
310
|
+
expect(healthy).toEqual(["SUBSCRIBED"])
|
|
311
|
+
expect(refused).toEqual(["CHANNEL_ERROR"])
|
|
312
|
+
// Refused, so it joined nothing and may join again on this socket.
|
|
313
|
+
bad.subscribe()
|
|
314
|
+
await settle()
|
|
315
|
+
expect(framesOfType("subscribe").filter((frame) => frame["channel"] === "orders")).toHaveLength(2)
|
|
316
|
+
})
|
|
317
|
+
})
|