@supacloud/js 0.45.5 → 0.46.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/REALTIME.md +84 -0
- package/dist/index.cjs +415 -8
- package/dist/index.d.cts +7 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.mjs +415 -8
- package/dist/realtime.cjs +431 -0
- package/dist/realtime.d.cts +171 -0
- package/dist/realtime.d.mts +171 -0
- package/dist/realtime.mjs +391 -0
- package/package.json +17 -6
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Realtime Adapter for SupaCloud.
|
|
3
|
+
*
|
|
4
|
+
* Provides a pluggable, 100% Supabase-compatible RealtimeChannel facade.
|
|
5
|
+
* Allows switching the underlying transport (WebSocket, Centrifugo, SSE, in-memory)
|
|
6
|
+
* while preserving the standard supabase-js channel syntax (.on('postgres_changes' | 'broadcast' | 'presence')).
|
|
7
|
+
*/
|
|
8
|
+
export type RealtimeChannelStatus = "CLOSED" | "ERRORED" | "SUBSCRIBED" | "TIMED_OUT";
|
|
9
|
+
export type PostgresChangeEvent = "INSERT" | "UPDATE" | "DELETE" | "*";
|
|
10
|
+
export interface PostgresChangesFilter {
|
|
11
|
+
event: PostgresChangeEvent;
|
|
12
|
+
schema: string;
|
|
13
|
+
table?: string;
|
|
14
|
+
filter?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface RealtimePostgresChangesPayload<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
17
|
+
schema: string;
|
|
18
|
+
table: string;
|
|
19
|
+
commit_timestamp: string;
|
|
20
|
+
eventType: "INSERT" | "UPDATE" | "DELETE";
|
|
21
|
+
new: T;
|
|
22
|
+
old: Partial<T>;
|
|
23
|
+
errors: string[];
|
|
24
|
+
}
|
|
25
|
+
export interface RealtimeBroadcastPayload<T = unknown> {
|
|
26
|
+
type: "broadcast";
|
|
27
|
+
event: string;
|
|
28
|
+
payload: T;
|
|
29
|
+
}
|
|
30
|
+
export type RealtimePresenceState<T = Record<string, unknown>> = Record<string, Array<{
|
|
31
|
+
presence_ref: string;
|
|
32
|
+
} & T>>;
|
|
33
|
+
export interface RealtimePresenceJoinPayload<T = Record<string, unknown>> {
|
|
34
|
+
key: string;
|
|
35
|
+
currentPresences: Array<{
|
|
36
|
+
presence_ref: string;
|
|
37
|
+
} & T>;
|
|
38
|
+
newPresences: Array<{
|
|
39
|
+
presence_ref: string;
|
|
40
|
+
} & T>;
|
|
41
|
+
}
|
|
42
|
+
export interface RealtimePresenceLeavePayload<T = Record<string, unknown>> {
|
|
43
|
+
key: string;
|
|
44
|
+
currentPresences: Array<{
|
|
45
|
+
presence_ref: string;
|
|
46
|
+
} & T>;
|
|
47
|
+
leftPresences: Array<{
|
|
48
|
+
presence_ref: string;
|
|
49
|
+
} & T>;
|
|
50
|
+
}
|
|
51
|
+
export type RealtimeCallbackResponse = "ok" | "timed out" | "error";
|
|
52
|
+
export interface RealtimeChannelConfig {
|
|
53
|
+
broadcast?: {
|
|
54
|
+
self?: boolean;
|
|
55
|
+
ack?: boolean;
|
|
56
|
+
};
|
|
57
|
+
presence?: {
|
|
58
|
+
key?: string;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export interface RealtimeTransportMessage {
|
|
62
|
+
topic: string;
|
|
63
|
+
event: string;
|
|
64
|
+
payload: Record<string, unknown>;
|
|
65
|
+
ref?: string | null;
|
|
66
|
+
}
|
|
67
|
+
export interface RealtimeTransport {
|
|
68
|
+
connect?(): Promise<void> | void;
|
|
69
|
+
disconnect?(): Promise<void> | void;
|
|
70
|
+
send(message: RealtimeTransportMessage): Promise<void> | void;
|
|
71
|
+
subscribe(topic: string, onMessage: (message: RealtimeTransportMessage) => void): () => void;
|
|
72
|
+
isConnected?(): boolean;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Evaluates standard Supabase realtime change filter expressions.
|
|
76
|
+
* E.g. "id=eq.123", "status=neq.deleted", "score=gte.10", "role=in.(admin,editor)"
|
|
77
|
+
*/
|
|
78
|
+
export declare function matchesPostgresFilter(filterStr: string | undefined, record: Record<string, unknown>): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* In-memory transport for testing, embedded runtimes, and local event dispatch.
|
|
81
|
+
*/
|
|
82
|
+
export declare class MemoryRealtimeTransport implements RealtimeTransport {
|
|
83
|
+
private subscribers;
|
|
84
|
+
private connected;
|
|
85
|
+
connect(): void;
|
|
86
|
+
disconnect(): void;
|
|
87
|
+
isConnected(): boolean;
|
|
88
|
+
send(message: RealtimeTransportMessage): void;
|
|
89
|
+
subscribe(topic: string, onMessage: (message: RealtimeTransportMessage) => void): () => void;
|
|
90
|
+
clear(): void;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* A standard-compliant RealtimeChannel adapter.
|
|
94
|
+
* Mirroring Supabase RealtimeChannel semantics.
|
|
95
|
+
*/
|
|
96
|
+
export declare class SupaCloudRealtimeChannel {
|
|
97
|
+
readonly topic: string;
|
|
98
|
+
private readonly config;
|
|
99
|
+
private readonly transport;
|
|
100
|
+
private channelStatus;
|
|
101
|
+
private statusCallbacks;
|
|
102
|
+
private postgresCallbacks;
|
|
103
|
+
private broadcastCallbacks;
|
|
104
|
+
private presenceCallbacks;
|
|
105
|
+
private presences;
|
|
106
|
+
private presenceKey;
|
|
107
|
+
private localPresenceRef;
|
|
108
|
+
private transportUnsubscribe;
|
|
109
|
+
private subscriptionTimer;
|
|
110
|
+
constructor(topic: string, transport: RealtimeTransport, config?: RealtimeChannelConfig);
|
|
111
|
+
get state(): RealtimeChannelStatus;
|
|
112
|
+
/**
|
|
113
|
+
* Register an event listener for postgres_changes, broadcast, or presence.
|
|
114
|
+
*/
|
|
115
|
+
on(type: "postgres_changes", filter: PostgresChangesFilter, callback: (payload: RealtimePostgresChangesPayload) => void): this;
|
|
116
|
+
on(type: "broadcast", filter: {
|
|
117
|
+
event: string;
|
|
118
|
+
}, callback: (payload: RealtimeBroadcastPayload) => void): this;
|
|
119
|
+
on(type: "presence", filter: {
|
|
120
|
+
event: "sync" | "join" | "leave";
|
|
121
|
+
}, callback: (payload: unknown) => void): this;
|
|
122
|
+
/**
|
|
123
|
+
* Subscribe to the channel.
|
|
124
|
+
*/
|
|
125
|
+
subscribe(callback?: (status: RealtimeChannelStatus, err?: Error) => void, timeoutMs?: number): this;
|
|
126
|
+
/**
|
|
127
|
+
* Unsubscribe and cleanup all handlers.
|
|
128
|
+
*/
|
|
129
|
+
unsubscribe(): Promise<RealtimeCallbackResponse>;
|
|
130
|
+
/**
|
|
131
|
+
* Broadcast a payload to all clients subscribed to this channel.
|
|
132
|
+
*/
|
|
133
|
+
send(args: {
|
|
134
|
+
type: "broadcast";
|
|
135
|
+
event: string;
|
|
136
|
+
payload: unknown;
|
|
137
|
+
}): Promise<RealtimeCallbackResponse>;
|
|
138
|
+
/**
|
|
139
|
+
* Track state on this channel for Presence.
|
|
140
|
+
*/
|
|
141
|
+
track(payload: Record<string, unknown>): Promise<RealtimeCallbackResponse>;
|
|
142
|
+
/**
|
|
143
|
+
* Stop tracking presence on this channel.
|
|
144
|
+
*/
|
|
145
|
+
untrack(): Promise<RealtimeCallbackResponse>;
|
|
146
|
+
/**
|
|
147
|
+
* Get the current presence state.
|
|
148
|
+
*/
|
|
149
|
+
presenceState<T = Record<string, unknown>>(): RealtimePresenceState<T>;
|
|
150
|
+
private emitStatus;
|
|
151
|
+
private handleIncomingMessage;
|
|
152
|
+
private dispatchPostgresChange;
|
|
153
|
+
private dispatchBroadcast;
|
|
154
|
+
private dispatchPresence;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Options to configure a Realtime Adapter instance.
|
|
158
|
+
*/
|
|
159
|
+
export interface RealtimeAdapterOptions {
|
|
160
|
+
transport?: RealtimeTransport;
|
|
161
|
+
projectRef?: string;
|
|
162
|
+
defaultTimeoutMs?: number;
|
|
163
|
+
}
|
|
164
|
+
export interface SupaCloudRealtimeAdapter {
|
|
165
|
+
channel(topic: string, config?: RealtimeChannelConfig): SupaCloudRealtimeChannel;
|
|
166
|
+
readonly transport: RealtimeTransport;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Creates a standard Realtime Adapter factory conforming to supabase-js conventions.
|
|
170
|
+
*/
|
|
171
|
+
export declare function createRealtimeAdapter(options?: RealtimeAdapterOptions): SupaCloudRealtimeAdapter;
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
// src/realtime.ts
|
|
2
|
+
function matchesPostgresFilter(filterStr, record) {
|
|
3
|
+
if (!filterStr || !filterStr.trim())
|
|
4
|
+
return true;
|
|
5
|
+
const eqIdx = filterStr.indexOf("=");
|
|
6
|
+
if (eqIdx === -1)
|
|
7
|
+
return true;
|
|
8
|
+
const col = filterStr.substring(0, eqIdx).trim();
|
|
9
|
+
const rest = filterStr.substring(eqIdx + 1).trim();
|
|
10
|
+
const dotIdx = rest.indexOf(".");
|
|
11
|
+
if (dotIdx === -1)
|
|
12
|
+
return true;
|
|
13
|
+
const op = rest.substring(0, dotIdx).trim().toLowerCase();
|
|
14
|
+
const rawVal = rest.substring(dotIdx + 1).trim();
|
|
15
|
+
const val = record[col];
|
|
16
|
+
const strVal = val === null || val === undefined ? "" : String(val);
|
|
17
|
+
switch (op) {
|
|
18
|
+
case "eq":
|
|
19
|
+
return strVal === rawVal;
|
|
20
|
+
case "neq":
|
|
21
|
+
return strVal !== rawVal;
|
|
22
|
+
case "gt": {
|
|
23
|
+
const nVal = Number(val);
|
|
24
|
+
const nTarget = Number(rawVal);
|
|
25
|
+
return !isNaN(nVal) && !isNaN(nTarget) ? nVal > nTarget : strVal > rawVal;
|
|
26
|
+
}
|
|
27
|
+
case "gte": {
|
|
28
|
+
const nVal = Number(val);
|
|
29
|
+
const nTarget = Number(rawVal);
|
|
30
|
+
return !isNaN(nVal) && !isNaN(nTarget) ? nVal >= nTarget : strVal >= rawVal;
|
|
31
|
+
}
|
|
32
|
+
case "lt": {
|
|
33
|
+
const nVal = Number(val);
|
|
34
|
+
const nTarget = Number(rawVal);
|
|
35
|
+
return !isNaN(nVal) && !isNaN(nTarget) ? nVal < nTarget : strVal < rawVal;
|
|
36
|
+
}
|
|
37
|
+
case "lte": {
|
|
38
|
+
const nVal = Number(val);
|
|
39
|
+
const nTarget = Number(rawVal);
|
|
40
|
+
return !isNaN(nVal) && !isNaN(nTarget) ? nVal <= nTarget : strVal <= rawVal;
|
|
41
|
+
}
|
|
42
|
+
case "in": {
|
|
43
|
+
const inner = rawVal.replace(/^\(|\)$/g, "");
|
|
44
|
+
const items = inner.split(",").map((s) => s.trim());
|
|
45
|
+
return items.includes(strVal);
|
|
46
|
+
}
|
|
47
|
+
default:
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
class MemoryRealtimeTransport {
|
|
53
|
+
subscribers = new Map;
|
|
54
|
+
connected = true;
|
|
55
|
+
connect() {
|
|
56
|
+
this.connected = true;
|
|
57
|
+
}
|
|
58
|
+
disconnect() {
|
|
59
|
+
this.connected = false;
|
|
60
|
+
}
|
|
61
|
+
isConnected() {
|
|
62
|
+
return this.connected;
|
|
63
|
+
}
|
|
64
|
+
send(message) {
|
|
65
|
+
if (!this.connected)
|
|
66
|
+
return;
|
|
67
|
+
const targets = this.subscribers.get(message.topic);
|
|
68
|
+
if (!targets)
|
|
69
|
+
return;
|
|
70
|
+
for (const callback of targets) {
|
|
71
|
+
try {
|
|
72
|
+
callback(message);
|
|
73
|
+
} catch {}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
subscribe(topic, onMessage) {
|
|
77
|
+
let set = this.subscribers.get(topic);
|
|
78
|
+
if (!set) {
|
|
79
|
+
set = new Set;
|
|
80
|
+
this.subscribers.set(topic, set);
|
|
81
|
+
}
|
|
82
|
+
set.add(onMessage);
|
|
83
|
+
return () => {
|
|
84
|
+
const current = this.subscribers.get(topic);
|
|
85
|
+
if (current) {
|
|
86
|
+
current.delete(onMessage);
|
|
87
|
+
if (current.size === 0) {
|
|
88
|
+
this.subscribers.delete(topic);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
clear() {
|
|
94
|
+
this.subscribers.clear();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
class SupaCloudRealtimeChannel {
|
|
99
|
+
topic;
|
|
100
|
+
config;
|
|
101
|
+
transport;
|
|
102
|
+
channelStatus = "CLOSED";
|
|
103
|
+
statusCallbacks = [];
|
|
104
|
+
postgresCallbacks = [];
|
|
105
|
+
broadcastCallbacks = [];
|
|
106
|
+
presenceCallbacks = [];
|
|
107
|
+
presences = {};
|
|
108
|
+
presenceKey;
|
|
109
|
+
localPresenceRef;
|
|
110
|
+
transportUnsubscribe = null;
|
|
111
|
+
subscriptionTimer = null;
|
|
112
|
+
constructor(topic, transport, config = {}) {
|
|
113
|
+
this.topic = topic;
|
|
114
|
+
this.transport = transport;
|
|
115
|
+
this.config = config;
|
|
116
|
+
this.presenceKey = config.presence?.key ?? (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function" ? crypto.randomUUID() : `sub_${Math.random().toString(36).substring(2, 10)}`);
|
|
117
|
+
this.localPresenceRef = typeof crypto !== "undefined" && typeof crypto.randomUUID === "function" ? crypto.randomUUID() : `ref_${Math.random().toString(36).substring(2, 10)}`;
|
|
118
|
+
}
|
|
119
|
+
get state() {
|
|
120
|
+
return this.channelStatus;
|
|
121
|
+
}
|
|
122
|
+
on(type, filter, callback) {
|
|
123
|
+
if (type === "postgres_changes") {
|
|
124
|
+
this.postgresCallbacks.push({
|
|
125
|
+
filter,
|
|
126
|
+
callback
|
|
127
|
+
});
|
|
128
|
+
} else if (type === "broadcast") {
|
|
129
|
+
const evt = String(filter.event ?? "*");
|
|
130
|
+
this.broadcastCallbacks.push({
|
|
131
|
+
event: evt,
|
|
132
|
+
callback
|
|
133
|
+
});
|
|
134
|
+
} else if (type === "presence") {
|
|
135
|
+
const evt = filter.event ?? "sync";
|
|
136
|
+
this.presenceCallbacks.push({
|
|
137
|
+
event: evt,
|
|
138
|
+
callback
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
subscribe(callback, timeoutMs = 1e4) {
|
|
144
|
+
if (callback) {
|
|
145
|
+
this.statusCallbacks.push(callback);
|
|
146
|
+
}
|
|
147
|
+
if (this.channelStatus === "SUBSCRIBED") {
|
|
148
|
+
if (callback)
|
|
149
|
+
callback("SUBSCRIBED");
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
if (this.transport.connect) {
|
|
153
|
+
Promise.resolve(this.transport.connect()).catch((err) => {
|
|
154
|
+
this.emitStatus("ERRORED", err instanceof Error ? err : new Error(String(err)));
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
if (timeoutMs > 0) {
|
|
158
|
+
this.subscriptionTimer = setTimeout(() => {
|
|
159
|
+
if (this.channelStatus !== "SUBSCRIBED" && this.channelStatus !== "CLOSED") {
|
|
160
|
+
this.emitStatus("TIMED_OUT", new Error(`Subscription timed out after ${timeoutMs}ms`));
|
|
161
|
+
}
|
|
162
|
+
}, timeoutMs);
|
|
163
|
+
}
|
|
164
|
+
this.transportUnsubscribe = this.transport.subscribe(this.topic, (message) => {
|
|
165
|
+
this.handleIncomingMessage(message);
|
|
166
|
+
});
|
|
167
|
+
if (this.subscriptionTimer) {
|
|
168
|
+
clearTimeout(this.subscriptionTimer);
|
|
169
|
+
this.subscriptionTimer = null;
|
|
170
|
+
}
|
|
171
|
+
this.emitStatus("SUBSCRIBED");
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
async unsubscribe() {
|
|
175
|
+
if (this.subscriptionTimer) {
|
|
176
|
+
clearTimeout(this.subscriptionTimer);
|
|
177
|
+
this.subscriptionTimer = null;
|
|
178
|
+
}
|
|
179
|
+
if (this.transportUnsubscribe) {
|
|
180
|
+
this.transportUnsubscribe();
|
|
181
|
+
this.transportUnsubscribe = null;
|
|
182
|
+
}
|
|
183
|
+
await this.untrack();
|
|
184
|
+
this.emitStatus("CLOSED");
|
|
185
|
+
return "ok";
|
|
186
|
+
}
|
|
187
|
+
async send(args) {
|
|
188
|
+
if (this.channelStatus === "CLOSED") {
|
|
189
|
+
return "error";
|
|
190
|
+
}
|
|
191
|
+
const message = {
|
|
192
|
+
topic: this.topic,
|
|
193
|
+
event: "broadcast",
|
|
194
|
+
payload: {
|
|
195
|
+
type: "broadcast",
|
|
196
|
+
event: args.event,
|
|
197
|
+
payload: args.payload,
|
|
198
|
+
__sender: this.localPresenceRef
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
try {
|
|
202
|
+
await this.transport.send(message);
|
|
203
|
+
return "ok";
|
|
204
|
+
} catch {
|
|
205
|
+
return "error";
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
async track(payload) {
|
|
209
|
+
if (this.channelStatus === "CLOSED")
|
|
210
|
+
return "error";
|
|
211
|
+
const entry = {
|
|
212
|
+
presence_ref: this.localPresenceRef,
|
|
213
|
+
...payload
|
|
214
|
+
};
|
|
215
|
+
const message = {
|
|
216
|
+
topic: this.topic,
|
|
217
|
+
event: "presence_join",
|
|
218
|
+
payload: {
|
|
219
|
+
key: this.presenceKey,
|
|
220
|
+
current: [entry]
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
try {
|
|
224
|
+
await this.transport.send(message);
|
|
225
|
+
const prev = this.presences[this.presenceKey] ?? [];
|
|
226
|
+
const updated = [
|
|
227
|
+
...prev.filter((p) => p.presence_ref !== this.localPresenceRef),
|
|
228
|
+
entry
|
|
229
|
+
];
|
|
230
|
+
this.presences[this.presenceKey] = updated;
|
|
231
|
+
this.dispatchPresence("join", {
|
|
232
|
+
key: this.presenceKey,
|
|
233
|
+
currentPresences: updated,
|
|
234
|
+
newPresences: [entry]
|
|
235
|
+
});
|
|
236
|
+
this.dispatchPresence("sync", this.presences);
|
|
237
|
+
return "ok";
|
|
238
|
+
} catch {
|
|
239
|
+
return "error";
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
async untrack() {
|
|
243
|
+
const prev = this.presences[this.presenceKey];
|
|
244
|
+
if (!prev || prev.length === 0)
|
|
245
|
+
return "ok";
|
|
246
|
+
const left = prev.filter((p) => p.presence_ref === this.localPresenceRef);
|
|
247
|
+
if (left.length === 0)
|
|
248
|
+
return "ok";
|
|
249
|
+
const remaining = prev.filter((p) => p.presence_ref !== this.localPresenceRef);
|
|
250
|
+
if (remaining.length === 0) {
|
|
251
|
+
delete this.presences[this.presenceKey];
|
|
252
|
+
} else {
|
|
253
|
+
this.presences[this.presenceKey] = remaining;
|
|
254
|
+
}
|
|
255
|
+
const message = {
|
|
256
|
+
topic: this.topic,
|
|
257
|
+
event: "presence_leave",
|
|
258
|
+
payload: {
|
|
259
|
+
key: this.presenceKey,
|
|
260
|
+
left
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
try {
|
|
264
|
+
await this.transport.send(message);
|
|
265
|
+
} catch {}
|
|
266
|
+
this.dispatchPresence("leave", {
|
|
267
|
+
key: this.presenceKey,
|
|
268
|
+
currentPresences: remaining,
|
|
269
|
+
leftPresences: left
|
|
270
|
+
});
|
|
271
|
+
this.dispatchPresence("sync", this.presences);
|
|
272
|
+
return "ok";
|
|
273
|
+
}
|
|
274
|
+
presenceState() {
|
|
275
|
+
return this.presences;
|
|
276
|
+
}
|
|
277
|
+
emitStatus(status, err) {
|
|
278
|
+
this.channelStatus = status;
|
|
279
|
+
for (const cb of this.statusCallbacks) {
|
|
280
|
+
try {
|
|
281
|
+
cb(status, err);
|
|
282
|
+
} catch {}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
handleIncomingMessage(message) {
|
|
286
|
+
const { event, payload } = message;
|
|
287
|
+
if (event === "postgres_changes") {
|
|
288
|
+
this.dispatchPostgresChange(payload);
|
|
289
|
+
} else if (event === "broadcast") {
|
|
290
|
+
const sender = payload.__sender;
|
|
291
|
+
if (sender === this.localPresenceRef && !this.config.broadcast?.self) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const bPayload = payload.payload !== undefined ? payload.payload : payload;
|
|
295
|
+
const bEvent = String(payload.event ?? "*");
|
|
296
|
+
this.dispatchBroadcast(bEvent, bPayload);
|
|
297
|
+
} else if (event === "presence_join") {
|
|
298
|
+
const key = String(payload.key ?? "");
|
|
299
|
+
const incoming = payload.current ?? [];
|
|
300
|
+
const prev = this.presences[key] ?? [];
|
|
301
|
+
const updated = [
|
|
302
|
+
...prev.filter((p) => !incoming.some((i) => i.presence_ref === p.presence_ref)),
|
|
303
|
+
...incoming
|
|
304
|
+
];
|
|
305
|
+
this.presences[key] = updated;
|
|
306
|
+
this.dispatchPresence("join", {
|
|
307
|
+
key,
|
|
308
|
+
currentPresences: updated,
|
|
309
|
+
newPresences: incoming
|
|
310
|
+
});
|
|
311
|
+
this.dispatchPresence("sync", this.presences);
|
|
312
|
+
} else if (event === "presence_leave") {
|
|
313
|
+
const key = String(payload.key ?? "");
|
|
314
|
+
const left = payload.left ?? [];
|
|
315
|
+
const prev = this.presences[key] ?? [];
|
|
316
|
+
const remaining = prev.filter((p) => !left.some((l) => l.presence_ref === p.presence_ref));
|
|
317
|
+
if (remaining.length === 0) {
|
|
318
|
+
delete this.presences[key];
|
|
319
|
+
} else {
|
|
320
|
+
this.presences[key] = remaining;
|
|
321
|
+
}
|
|
322
|
+
this.dispatchPresence("leave", {
|
|
323
|
+
key,
|
|
324
|
+
currentPresences: remaining,
|
|
325
|
+
leftPresences: left
|
|
326
|
+
});
|
|
327
|
+
this.dispatchPresence("sync", this.presences);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
dispatchPostgresChange(payload) {
|
|
331
|
+
if (!payload || !payload.schema || !payload.table || !payload.eventType)
|
|
332
|
+
return;
|
|
333
|
+
for (const { filter, callback } of this.postgresCallbacks) {
|
|
334
|
+
if (filter.schema !== "*" && filter.schema.toLowerCase() !== payload.schema.toLowerCase()) {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (filter.table && filter.table !== "*" && filter.table.toLowerCase() !== payload.table.toLowerCase()) {
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
if (filter.event !== "*" && filter.event.toUpperCase() !== payload.eventType.toUpperCase()) {
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
const recordToMatch = payload.eventType === "DELETE" ? payload.old : payload.new;
|
|
344
|
+
if (recordToMatch && !matchesPostgresFilter(filter.filter, recordToMatch)) {
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
try {
|
|
348
|
+
callback(payload);
|
|
349
|
+
} catch {}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
dispatchBroadcast(event, payload) {
|
|
353
|
+
for (const listener of this.broadcastCallbacks) {
|
|
354
|
+
if (listener.event === "*" || listener.event === event) {
|
|
355
|
+
try {
|
|
356
|
+
listener.callback({
|
|
357
|
+
type: "broadcast",
|
|
358
|
+
event,
|
|
359
|
+
payload
|
|
360
|
+
});
|
|
361
|
+
} catch {}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
dispatchPresence(event, payload) {
|
|
366
|
+
for (const listener of this.presenceCallbacks) {
|
|
367
|
+
if (listener.event === event) {
|
|
368
|
+
try {
|
|
369
|
+
listener.callback(payload);
|
|
370
|
+
} catch {}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
function createRealtimeAdapter(options = {}) {
|
|
376
|
+
const transport = options.transport ?? new MemoryRealtimeTransport;
|
|
377
|
+
return {
|
|
378
|
+
channel(topic, config) {
|
|
379
|
+
return new SupaCloudRealtimeChannel(topic, transport, config);
|
|
380
|
+
},
|
|
381
|
+
get transport() {
|
|
382
|
+
return transport;
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
export {
|
|
387
|
+
MemoryRealtimeTransport,
|
|
388
|
+
SupaCloudRealtimeChannel,
|
|
389
|
+
createRealtimeAdapter,
|
|
390
|
+
matchesPostgresFilter
|
|
391
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supacloud/js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.46.0",
|
|
4
4
|
"description": "Platform SDK for SupaCloud, built on top of supabase-js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -47,6 +47,16 @@
|
|
|
47
47
|
"default": "./dist/reactive.cjs"
|
|
48
48
|
}
|
|
49
49
|
},
|
|
50
|
+
"./realtime": {
|
|
51
|
+
"import": {
|
|
52
|
+
"types": "./dist/realtime.d.mts",
|
|
53
|
+
"default": "./dist/realtime.mjs"
|
|
54
|
+
},
|
|
55
|
+
"require": {
|
|
56
|
+
"types": "./dist/realtime.d.cts",
|
|
57
|
+
"default": "./dist/realtime.cjs"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
50
60
|
"./query": {
|
|
51
61
|
"import": {
|
|
52
62
|
"types": "./dist/query.d.mts",
|
|
@@ -66,7 +76,8 @@
|
|
|
66
76
|
"COMPATIBILITY.md",
|
|
67
77
|
"PROCEDURES.md",
|
|
68
78
|
"QUERY.md",
|
|
69
|
-
"REACTIVE.md"
|
|
79
|
+
"REACTIVE.md",
|
|
80
|
+
"REALTIME.md"
|
|
70
81
|
],
|
|
71
82
|
"scripts": {
|
|
72
83
|
"test": "bun test",
|
|
@@ -79,8 +90,8 @@
|
|
|
79
90
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
80
91
|
"typecheck:test": "tsc -p tsconfig.test.json --noEmit",
|
|
81
92
|
"check:package": "node ../../.github/scripts/esm-package-policy.mjs --pack .",
|
|
82
|
-
"build:js:esm": "bun build src/index.ts src/task-events.ts --outdir dist --format esm --entry-naming \"[name].mjs\" --chunk-naming \"[name]-[hash].mjs\" --target node --external @supabase/supabase-js --external @supacloud/contracts && bun build src/contracts.ts src/query.ts --outdir dist --format esm --entry-naming \"[name].mjs\" --chunk-naming \"[name]-[hash].mjs\" --target browser --external @supacloud/contracts --external @tanstack/query-core && bun build src/reactive.ts --outdir dist --format esm --entry-naming \"[name].mjs\" --chunk-naming \"[name]-[hash].mjs\" --target browser --external rxjs",
|
|
83
|
-
"build:js:cjs": "bun build src/index.ts src/task-events.ts --outdir dist --format cjs --entry-naming \"[name].cjs\" --chunk-naming \"[name]-[hash].cjs\" --target node --external @supabase/supabase-js --external @supacloud/contracts && bun build src/contracts.ts src/query.ts --outdir dist --format cjs --entry-naming \"[name].cjs\" --chunk-naming \"[name]-[hash].cjs\" --target browser --external @supacloud/contracts --external @tanstack/query-core && bun build src/reactive.ts --outdir dist --format cjs --entry-naming \"[name].cjs\" --chunk-naming \"[name]-[hash].cjs\" --target browser --external rxjs"
|
|
93
|
+
"build:js:esm": "bun build src/index.ts src/task-events.ts --outdir dist --format esm --entry-naming \"[name].mjs\" --chunk-naming \"[name]-[hash].mjs\" --target node --external @supabase/supabase-js --external @supacloud/contracts && bun build src/contracts.ts src/query.ts --outdir dist --format esm --entry-naming \"[name].mjs\" --chunk-naming \"[name]-[hash].mjs\" --target browser --external @supacloud/contracts --external @tanstack/query-core && bun build src/reactive.ts src/realtime.ts --outdir dist --format esm --entry-naming \"[name].mjs\" --chunk-naming \"[name]-[hash].mjs\" --target browser --external rxjs",
|
|
94
|
+
"build:js:cjs": "bun build src/index.ts src/task-events.ts --outdir dist --format cjs --entry-naming \"[name].cjs\" --chunk-naming \"[name]-[hash].cjs\" --target node --external @supabase/supabase-js --external @supacloud/contracts && bun build src/contracts.ts src/query.ts --outdir dist --format cjs --entry-naming \"[name].cjs\" --chunk-naming \"[name]-[hash].cjs\" --target browser --external @supacloud/contracts --external @tanstack/query-core && bun build src/reactive.ts src/realtime.ts --outdir dist --format cjs --entry-naming \"[name].cjs\" --chunk-naming \"[name]-[hash].cjs\" --target browser --external rxjs"
|
|
84
95
|
},
|
|
85
96
|
"keywords": [
|
|
86
97
|
"supacloud",
|
|
@@ -106,7 +117,7 @@
|
|
|
106
117
|
},
|
|
107
118
|
"dependencies": {
|
|
108
119
|
"@supacloud/contracts": "0.15.2",
|
|
109
|
-
"@supacloud/query": "0.5.
|
|
120
|
+
"@supacloud/query": "0.5.3",
|
|
110
121
|
"rxjs": "7.8.2"
|
|
111
122
|
},
|
|
112
123
|
"devDependencies": {
|
|
@@ -119,6 +130,6 @@
|
|
|
119
130
|
"svelte": "5.57.3"
|
|
120
131
|
},
|
|
121
132
|
"engines": {
|
|
122
|
-
"node": ">=
|
|
133
|
+
"node": ">=24.0.0"
|
|
123
134
|
}
|
|
124
135
|
}
|