@supatype/solid 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 +19 -11
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +40 -1
- package/dist/createChannel.d.ts +26 -0
- package/dist/createChannel.d.ts.map +1 -0
- package/dist/createChannel.js +40 -0
- package/dist/createChannel.js.map +1 -0
- package/dist/createQuery.d.ts +1 -1
- package/dist/createQuery.d.ts.map +1 -1
- package/dist/createQuery.js +21 -7
- package/dist/createQuery.js.map +1 -1
- package/dist/createRealtimeConnection.d.ts +14 -0
- package/dist/createRealtimeConnection.d.ts.map +1 -0
- package/dist/createRealtimeConnection.js +16 -0
- package/dist/createRealtimeConnection.js.map +1 -0
- package/dist/createSubscription.d.ts.map +1 -1
- package/dist/createSubscription.js +1 -1
- package/dist/createSubscription.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/createChannel.ts +56 -0
- package/src/createQuery.ts +22 -7
- package/src/createRealtimeConnection.ts +27 -0
- package/src/createSubscription.ts +2 -2
- package/src/index.ts +6 -0
- package/tests/createChannel-private.test.ts +61 -0
- package/tests/createChannel-reactive-name.test.ts +68 -0
- package/tests/createChannel-typed-name.test.ts +15 -0
- package/tests/createChannel.test.ts +61 -0
- package/tests/createQuery-first-render.test.ts +44 -0
- package/tests/createQuery-reactive-options.test.ts +40 -0
- package/tests/createSubscription-status.test.ts +19 -4
- package/tests/solid-browser-build.d.ts +6 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** `createChannel(name, setup, { private: true })` joins as private and shows the typed refusal. */
|
|
2
|
+
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest"
|
|
4
|
+
import { isRealtimeChannelError } from "@supatype/client"
|
|
5
|
+
import { ackNewestJoin, subscribeFrames, simulateServerMessage } from "../../client/tests/helpers/mock-websocket.js"
|
|
6
|
+
import { createSocketSwapHarness, type SocketSwapHarness } from "../../client/tests/helpers/socket-swap.js"
|
|
7
|
+
|
|
8
|
+
const lifecycle = vi.hoisted(() => ({ cleanup: [] as Array<() => void>, harness: null as SocketSwapHarness | null }))
|
|
9
|
+
|
|
10
|
+
vi.mock("solid-js", async (importOriginal) => ({
|
|
11
|
+
...(await importOriginal<typeof import("solid-js")>()),
|
|
12
|
+
onMount: (fn: () => void) => fn(),
|
|
13
|
+
onCleanup: (fn: () => void) => lifecycle.cleanup.push(fn),
|
|
14
|
+
}))
|
|
15
|
+
|
|
16
|
+
vi.mock("../src/context.js", () => ({
|
|
17
|
+
useSupatype: () => ({ realtime: lifecycle.harness?.realtime }),
|
|
18
|
+
}))
|
|
19
|
+
|
|
20
|
+
import { createChannel } from "../src/createChannel.js"
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
for (const fn of lifecycle.cleanup.splice(0)) fn()
|
|
24
|
+
lifecycle.harness?.restore()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
describe("createChannel with private: true", () => {
|
|
28
|
+
it("joins as private once the server announces private_channels", async () => {
|
|
29
|
+
const harness = createSocketSwapHarness()
|
|
30
|
+
lifecycle.harness = harness
|
|
31
|
+
createChannel("lot:42", () => undefined, { private: true })
|
|
32
|
+
await harness.settle()
|
|
33
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "ok", message: "authenticated", features: ["private_channels"] })
|
|
34
|
+
expect(subscribeFrames(harness.socket(), "lot:42")[0]?.["private"]).toBe(true)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it("exposes the unsupported-server error as a RealtimeChannelError", async () => {
|
|
38
|
+
const harness = createSocketSwapHarness()
|
|
39
|
+
lifecycle.harness = harness
|
|
40
|
+
const { status, error } = createChannel("lot:42", () => undefined, { private: true })
|
|
41
|
+
await harness.settle()
|
|
42
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "ok", message: "authenticated" })
|
|
43
|
+
expect(status()).toBe("error")
|
|
44
|
+
expect(isRealtimeChannelError(error())).toBe(true)
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
describe("createChannel with a refused send", () => {
|
|
49
|
+
it("exposes the refusal and keeps the status connected", async () => {
|
|
50
|
+
const harness = createSocketSwapHarness()
|
|
51
|
+
lifecycle.harness = harness
|
|
52
|
+
const { status, refusal } = createChannel("lot:42", () => undefined, { private: true })
|
|
53
|
+
await harness.settle()
|
|
54
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "ok", message: "authenticated", features: ["private_channels"] })
|
|
55
|
+
ackNewestJoin(harness.socket(), "lot:42")
|
|
56
|
+
expect(refusal()).toBeNull()
|
|
57
|
+
simulateServerMessage(harness.socket(), { type: "system", status: "error", code: "send_denied", message: "not allowed", channel: "lot:42" })
|
|
58
|
+
expect(refusal()).toEqual({ channel: "lot:42", code: "send_denied", message: "not allowed" })
|
|
59
|
+
expect(status()).toBe("connected")
|
|
60
|
+
})
|
|
61
|
+
})
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `createChannel` follows its name when it is an accessor: the old channel is left, its listeners
|
|
3
|
+
* go with it, and `setup` runs on the new one.
|
|
4
|
+
*
|
|
5
|
+
* Under Node, `solid-js` resolves to its server build, where effects never run, so this suite swaps
|
|
6
|
+
* in the browser build: what is under test is that an effect tracks the name.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { afterEach, describe, expect, it, vi } from "vitest"
|
|
10
|
+
import { createSocketSwapHarness, type SocketSwapHarness } from "../../client/tests/helpers/socket-swap.js"
|
|
11
|
+
import { sentFrames, simulateServerMessage, subscribedChannels } from "../../client/tests/helpers/mock-websocket.js"
|
|
12
|
+
|
|
13
|
+
const lifecycle = vi.hoisted(() => ({ dispose: [] as Array<() => void>, harness: null as SocketSwapHarness | null }))
|
|
14
|
+
|
|
15
|
+
vi.mock("solid-js", () => import("solid-js/dist/solid.js"))
|
|
16
|
+
|
|
17
|
+
vi.mock("../src/context.js", () => ({
|
|
18
|
+
useSupatype: () => ({ realtime: lifecycle.harness?.realtime }),
|
|
19
|
+
}))
|
|
20
|
+
|
|
21
|
+
import { createRoot, createSignal } from "solid-js"
|
|
22
|
+
import { createChannel } from "../src/createChannel.js"
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
for (const fn of lifecycle.dispose.splice(0)) fn()
|
|
26
|
+
lifecycle.harness?.restore()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
describe("createChannel with a reactive name", () => {
|
|
30
|
+
it("moves to the new channel when the accessor changes, leaving the old channel behind", async () => {
|
|
31
|
+
const harness = createSocketSwapHarness()
|
|
32
|
+
lifecycle.harness = harness
|
|
33
|
+
const heard: string[] = []
|
|
34
|
+
const [name, setName] = createSignal("room:1")
|
|
35
|
+
const result = createRoot((dispose) => {
|
|
36
|
+
lifecycle.dispose.push(dispose)
|
|
37
|
+
return createChannel(name, (channel) => channel.onBroadcast("ping", () => heard.push("ping")))
|
|
38
|
+
})
|
|
39
|
+
await harness.settle()
|
|
40
|
+
harness.ackJoins()
|
|
41
|
+
expect(result.status()).toBe("connected")
|
|
42
|
+
|
|
43
|
+
setName("room:2")
|
|
44
|
+
expect(result.status()).toBe("connecting")
|
|
45
|
+
await harness.settle()
|
|
46
|
+
|
|
47
|
+
const first = harness.sockets[0]
|
|
48
|
+
expect(first === undefined ? [] : sentFrames(first).some((f) => f["type"] === "unsubscribe" && f["channel"] === "room:1")).toBe(true)
|
|
49
|
+
expect(subscribedChannels(harness.socket())).toEqual(["room:2"])
|
|
50
|
+
simulateServerMessage(harness.socket(), { type: "broadcast", channel: "room:1", event: "ping", payload: {} })
|
|
51
|
+
expect(heard).toEqual([])
|
|
52
|
+
harness.ackJoins()
|
|
53
|
+
simulateServerMessage(harness.socket(), { type: "broadcast", channel: "room:2", event: "ping", payload: {} })
|
|
54
|
+
expect(heard).toEqual(["ping"])
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it("keeps a plain string working", async () => {
|
|
58
|
+
const harness = createSocketSwapHarness()
|
|
59
|
+
lifecycle.harness = harness
|
|
60
|
+
const result = createRoot((dispose) => {
|
|
61
|
+
lifecycle.dispose.push(dispose)
|
|
62
|
+
return createChannel("room:1", () => undefined)
|
|
63
|
+
})
|
|
64
|
+
await harness.settle()
|
|
65
|
+
harness.ackJoins()
|
|
66
|
+
expect(result.status()).toBe("connected")
|
|
67
|
+
})
|
|
68
|
+
})
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** The channel binding's name takes a ChannelName, so a declared topic autocompletes and any other string still works. */
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from "vitest"
|
|
4
|
+
import type { ChannelName } from "@supatype/client"
|
|
5
|
+
import type { Accessor } from "solid-js"
|
|
6
|
+
import type { createChannel } from "../src/createChannel.js"
|
|
7
|
+
|
|
8
|
+
type Same<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false
|
|
9
|
+
|
|
10
|
+
describe("createChannel name type", () => {
|
|
11
|
+
it("is a ChannelName", () => {
|
|
12
|
+
const same: Same<Parameters<typeof createChannel>[0], ChannelName | Accessor<ChannelName>> = true
|
|
13
|
+
expect(same).toBe(true)
|
|
14
|
+
})
|
|
15
|
+
})
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `createChannel` and `createRealtimeConnection`: the status across the sign-in swap, and the
|
|
3
|
+
* socket's own state through a drop. `onMount` runs directly and `onCleanup` is collected, as in the
|
|
4
|
+
* `createSubscription` suite.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { afterEach, describe, expect, it, vi } from "vitest"
|
|
8
|
+
import { closeEvent } from "../../client/tests/helpers/close-event.js"
|
|
9
|
+
import { describeStatusAcrossSignIn, createSocketSwapHarness, type SocketSwapHarness } from "../../client/tests/helpers/socket-swap.js"
|
|
10
|
+
|
|
11
|
+
const lifecycle = vi.hoisted(() => ({ cleanup: [] as Array<() => void>, harness: null as SocketSwapHarness | null }))
|
|
12
|
+
|
|
13
|
+
vi.mock("solid-js", async (importOriginal) => ({
|
|
14
|
+
...(await importOriginal<typeof import("solid-js")>()),
|
|
15
|
+
onMount: (fn: () => void) => fn(),
|
|
16
|
+
onCleanup: (fn: () => void) => lifecycle.cleanup.push(fn),
|
|
17
|
+
}))
|
|
18
|
+
|
|
19
|
+
vi.mock("../src/context.js", () => ({
|
|
20
|
+
useSupatype: () => ({ realtime: lifecycle.harness?.realtime }),
|
|
21
|
+
}))
|
|
22
|
+
|
|
23
|
+
import { createChannel } from "../src/createChannel.js"
|
|
24
|
+
|
|
25
|
+
const cleanup = (): void => {
|
|
26
|
+
for (const fn of lifecycle.cleanup.splice(0)) fn()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describeStatusAcrossSignIn(
|
|
30
|
+
"createChannel",
|
|
31
|
+
(harness) => {
|
|
32
|
+
lifecycle.harness = harness
|
|
33
|
+
const { status, error } = createChannel("public:task", () => undefined)
|
|
34
|
+
return { status, error }
|
|
35
|
+
},
|
|
36
|
+
cleanup,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
describe("createChannel's connection", () => {
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
cleanup()
|
|
42
|
+
lifecycle.harness?.restore()
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it("tells a drop from a first connect, and leaves the channel on cleanup", async () => {
|
|
46
|
+
const harness = createSocketSwapHarness()
|
|
47
|
+
lifecycle.harness = harness
|
|
48
|
+
const { isConnected, connectionState } = createChannel("auction:1", () => undefined)
|
|
49
|
+
expect(connectionState()).toBe("connecting")
|
|
50
|
+
await harness.settle()
|
|
51
|
+
expect(connectionState()).toBe("connected")
|
|
52
|
+
expect(isConnected()).toBe(true)
|
|
53
|
+
|
|
54
|
+
harness.socket().onclose?.(closeEvent())
|
|
55
|
+
expect(connectionState()).toBe("connecting")
|
|
56
|
+
expect(isConnected()).toBe(false)
|
|
57
|
+
|
|
58
|
+
cleanup()
|
|
59
|
+
expect(harness.realtime.connectionState).toBe("disconnected")
|
|
60
|
+
})
|
|
61
|
+
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.9.2 release test (N-FF9): the first render comes before `onMount` starts the read, and
|
|
3
|
+
* `loading` started false, so an enabled query first showed `loading: false, data: null`. An app
|
|
4
|
+
* reads that pair as "done, nothing found". `onMount` is held here so the state is read at that
|
|
5
|
+
* first render.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { afterEach, describe, expect, it, vi } from "vitest"
|
|
9
|
+
import { createClient, type AnyDatabase, type SupatypeClient } from "@supatype/client"
|
|
10
|
+
|
|
11
|
+
const lifecycle = vi.hoisted(() => ({
|
|
12
|
+
mounted: [] as Array<() => void>,
|
|
13
|
+
client: null as SupatypeClient<AnyDatabase> | null,
|
|
14
|
+
}))
|
|
15
|
+
|
|
16
|
+
vi.mock("solid-js", async (importOriginal) => ({
|
|
17
|
+
...(await importOriginal<typeof import("solid-js")>()),
|
|
18
|
+
onMount: (fn: () => void) => lifecycle.mounted.push(fn),
|
|
19
|
+
onCleanup: () => undefined,
|
|
20
|
+
}))
|
|
21
|
+
|
|
22
|
+
vi.mock("../src/context.js", () => ({
|
|
23
|
+
useSupatype: () => lifecycle.client,
|
|
24
|
+
}))
|
|
25
|
+
|
|
26
|
+
import { createQuery } from "../src/createQuery.js"
|
|
27
|
+
|
|
28
|
+
lifecycle.client = createClient<AnyDatabase>({ url: "http://localhost:9999", anonKey: "anon" })
|
|
29
|
+
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
lifecycle.mounted.splice(0)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
describe("createQuery before it is mounted", () => {
|
|
35
|
+
it("is loading when enabled", () => {
|
|
36
|
+
const q = createQuery<AnyDatabase>("profiles")
|
|
37
|
+
expect({ loading: q.loading(), data: q.data() }).toEqual({ loading: true, data: null })
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it("is not loading when disabled", () => {
|
|
41
|
+
const q = createQuery<AnyDatabase>("profiles", () => ({ enabled: false }))
|
|
42
|
+
expect(q.loading()).toBe(false)
|
|
43
|
+
})
|
|
44
|
+
})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `createQuery` reads again when its options change, passed as an accessor.
|
|
3
|
+
*
|
|
4
|
+
* Under Node, `solid-js` resolves to its server build, where effects never run, so this suite
|
|
5
|
+
* swaps in the browser build: what is under test is that an effect tracks the options.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { vi } from "vitest"
|
|
9
|
+
import type { AnyDatabase, SupatypeClient } from "@supatype/client"
|
|
10
|
+
import { describeReactiveOptionsBinding, type ReadOptions } from "../../client/tests/helpers/reactive-options-binding.js"
|
|
11
|
+
|
|
12
|
+
const lifecycle = vi.hoisted(() => ({
|
|
13
|
+
dispose: [] as Array<() => void>,
|
|
14
|
+
client: null as SupatypeClient<AnyDatabase> | null,
|
|
15
|
+
}))
|
|
16
|
+
|
|
17
|
+
vi.mock("solid-js", () => import("solid-js/dist/solid.js"))
|
|
18
|
+
|
|
19
|
+
vi.mock("../src/context.js", () => ({
|
|
20
|
+
useSupatype: () => lifecycle.client,
|
|
21
|
+
}))
|
|
22
|
+
|
|
23
|
+
import { createRoot, createSignal } from "solid-js"
|
|
24
|
+
import { createQuery } from "../src/createQuery.js"
|
|
25
|
+
|
|
26
|
+
describeReactiveOptionsBinding(
|
|
27
|
+
"createQuery with an accessor",
|
|
28
|
+
(client, initial) => {
|
|
29
|
+
lifecycle.client = client
|
|
30
|
+
const [options, setOptions] = createSignal<ReadOptions>(initial)
|
|
31
|
+
createRoot((dispose) => {
|
|
32
|
+
lifecycle.dispose.push(dispose)
|
|
33
|
+
createQuery<AnyDatabase>("posts", options)
|
|
34
|
+
})
|
|
35
|
+
return { change: (next) => setOptions(next) }
|
|
36
|
+
},
|
|
37
|
+
() => {
|
|
38
|
+
for (const fn of lifecycle.dispose.splice(0)) fn()
|
|
39
|
+
},
|
|
40
|
+
)
|
|
@@ -9,10 +9,11 @@
|
|
|
9
9
|
* build, where `onMount` never runs. What is under test is how the primitive wires the channel.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { vi } from "vitest"
|
|
13
|
-
import { describeStatusAcrossSignIn, type SocketSwapHarness } from "../../client/tests/helpers/socket-swap.js"
|
|
12
|
+
import { vi, it, expect } from "vitest"
|
|
13
|
+
import { describeStatusAcrossSignIn, createSocketSwapHarness, type SocketSwapHarness } from "../../client/tests/helpers/socket-swap.js"
|
|
14
|
+
import { fakeClient } from "../../client/tests/helpers/fake-client.js"
|
|
14
15
|
|
|
15
|
-
const lifecycle = vi.hoisted(() => ({ cleanup: [] as Array<() => void>, harness: null as SocketSwapHarness | null }))
|
|
16
|
+
const lifecycle = vi.hoisted(() => ({ cleanup: [] as Array<() => void>, harness: null as SocketSwapHarness | null, client: null as unknown }))
|
|
16
17
|
|
|
17
18
|
vi.mock("solid-js", async (importOriginal) => ({
|
|
18
19
|
...(await importOriginal<typeof import("solid-js")>()),
|
|
@@ -21,7 +22,7 @@ vi.mock("solid-js", async (importOriginal) => ({
|
|
|
21
22
|
}))
|
|
22
23
|
|
|
23
24
|
vi.mock("../src/context.js", () => ({
|
|
24
|
-
useSupatype: () =>
|
|
25
|
+
useSupatype: () => lifecycle.client,
|
|
25
26
|
}))
|
|
26
27
|
|
|
27
28
|
import { createSubscription } from "../src/createSubscription.js"
|
|
@@ -30,6 +31,7 @@ describeStatusAcrossSignIn(
|
|
|
30
31
|
"createSubscription",
|
|
31
32
|
(harness) => {
|
|
32
33
|
lifecycle.harness = harness
|
|
34
|
+
lifecycle.client = fakeClient(harness).client
|
|
33
35
|
const { status, error } = createSubscription("task")
|
|
34
36
|
return { status, error }
|
|
35
37
|
},
|
|
@@ -37,3 +39,16 @@ describeStatusAcrossSignIn(
|
|
|
37
39
|
for (const fn of lifecycle.cleanup.splice(0)) fn()
|
|
38
40
|
},
|
|
39
41
|
)
|
|
42
|
+
|
|
43
|
+
it("createSubscription shows the rows the table already holds once the join is acknowledged", async () => {
|
|
44
|
+
const harness = createSocketSwapHarness()
|
|
45
|
+
lifecycle.harness = harness
|
|
46
|
+
lifecycle.client = fakeClient(harness, [[{ id: 1 }]]).client
|
|
47
|
+
const { data } = createSubscription("task")
|
|
48
|
+
await harness.settle()
|
|
49
|
+
harness.ackJoins()
|
|
50
|
+
await harness.settle()
|
|
51
|
+
expect(data()).toEqual([{ id: 1 }])
|
|
52
|
+
for (const fn of lifecycle.cleanup.splice(0)) fn()
|
|
53
|
+
harness.restore()
|
|
54
|
+
})
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// The browser build of solid-js, imported by path so a suite can run effects under Node, where
|
|
2
|
+
// the package's own exports resolve to the server build. It has the package's API; only the
|
|
3
|
+
// declarations are missing, because solid-js ships them for the bare specifier alone.
|
|
4
|
+
declare module "solid-js/dist/solid.js" {
|
|
5
|
+
export * from "solid-js"
|
|
6
|
+
}
|