-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.ts
More file actions
361 lines (315 loc) · 12.6 KB
/
plugin.ts
File metadata and controls
361 lines (315 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import type { Plugin, PluginContext } from '@objectstack/runtime';
import type { PluginHealthReport, PluginCapabilityManifest, PluginSecurityManifest, PluginStartupResult } from './types.js';
import { WebSocketServer, WebSocket } from 'ws';
import { randomUUID } from 'crypto';
export interface RealtimePluginOptions {
port?: number;
path?: string;
}
// Interfaces based on @objectstack/spec/api/websocket.zod
interface BaseMessage {
messageId: string;
timestamp: string;
}
interface SubscribeMessage extends BaseMessage {
type: 'subscribe';
subscription: {
subscriptionId: string;
events: string[];
objects?: string[];
filters?: {
conditions?: Array<{
field: string;
operator: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'nin' | 'contains' | 'startsWith' | 'endsWith' | 'exists' | 'regex';
value?: any;
}>;
};
};
}
interface UnsubscribeMessage extends BaseMessage {
type: 'unsubscribe';
request: {
subscriptionIds?: string[];
all?: boolean;
};
}
interface EventMessage extends BaseMessage {
type: 'event';
subscriptionId: string;
eventName: string;
object?: string;
payload: any;
userId?: string;
}
interface AckMessage extends BaseMessage {
type: 'ack';
ackMessageId: string;
success: boolean;
error?: string;
}
interface PresenceMessage extends BaseMessage {
type: 'presence';
presence: {
userId: string;
status: 'online' | 'offline' | 'away' | 'busy';
context?: any;
lastActive: string;
};
}
interface ClientState {
subscriptions: Map<string, SubscribeMessage['subscription']>;
}
export const createRealtimePlugin = (options: RealtimePluginOptions = {}): Plugin => {
let wss: WebSocketServer;
const clientStates = new Map<WebSocket, ClientState>();
let startedAt: number | undefined;
// Helper: Simple Wildcard Matcher (e.g. "user.*" matches "user.created")
const matchPattern = (pattern: string, event: string): boolean => {
if (pattern === '*') return true;
if (pattern === event) return true;
if (pattern.endsWith('*')) {
const prefix = pattern.slice(0, -1);
return event.startsWith(prefix);
}
return false;
};
// Helper: Filter Logic
const checkFilter = (payload: any, condition: any): boolean => {
const value = payload?.[condition.field];
const target = condition.value;
switch (condition.operator) {
case 'eq': return value === target;
case 'ne': return value !== target;
case 'gt': return value > target;
case 'gte': return value >= target;
case 'lt': return value < target;
case 'lte': return value <= target;
case 'in': return Array.isArray(target) && target.includes(value);
case 'nin': return Array.isArray(target) && !target.includes(value);
case 'contains': return typeof value === 'string' && value.includes(target);
case 'startsWith': return typeof value === 'string' && value.startsWith(target);
case 'endsWith': return typeof value === 'string' && value.endsWith(target);
case 'exists': return value !== undefined && value !== null;
case 'regex': return new RegExp(target).test(value);
default: return false;
}
};
return {
name: '@objectos/realtime',
version: '0.1.0',
// description: 'Provides WebSocket capabilities compliant with @objectstack/spec',
async init(ctx: PluginContext) {
ctx.logger.info('[Realtime] Initializing WebSocket Server...');
startedAt = Date.now();
// 1. Register the service so Adapters can find us
ctx.registerService('realtime', {
broadcast: (eventName: string, payload: any, meta?: { object?: string, userId?: string }) => {
if (!wss) return;
const timestamp = new Date().toISOString();
wss.clients.forEach(client => {
if (client.readyState !== WebSocket.OPEN) return;
const state = clientStates.get(client);
if (!state) return;
// Check all subscriptions for this client
state.subscriptions.forEach(subscription => {
// 1. Check Object filter
if (subscription.objects && meta?.object) {
if (!subscription.objects.includes(meta.object)) return;
}
// 2. Check Event Pattern
const isMatch = subscription.events.some(pattern => matchPattern(pattern, eventName));
if (isMatch) {
// 3. Check Complex Filters
if (subscription.filters?.conditions) {
const pass = subscription.filters.conditions.every((c: any) => checkFilter(payload, c));
if (!pass) return;
}
// Send Event
const message: EventMessage = {
messageId: randomUUID(),
timestamp,
type: 'event',
subscriptionId: subscription.subscriptionId,
eventName,
object: meta?.object,
payload,
userId: meta?.userId
};
client.send(JSON.stringify(message));
}
});
});
},
// Presence API
updatePresence: (userId: string, status: any, context?: any) => {
if (!wss) return;
const message: PresenceMessage = {
messageId: randomUUID(),
timestamp: new Date().toISOString(),
type: 'presence',
presence: {
userId,
status: status.status || 'online',
context,
lastActive: new Date().toISOString()
}
};
const msgStr = JSON.stringify(message);
wss.clients.forEach(c => {
if (c.readyState === WebSocket.OPEN) c.send(msgStr);
});
},
// Allow adapters to get the raw WSS if needed
getServer: () => wss
});
ctx.logger.info('[Realtime] Service registered as "realtime"');
},
async start(ctx: PluginContext) {
const port = options.port || 3001; // Default to separate port for now
ctx.logger.info(`[Realtime] Starting WebSocket Server on port ${port}...`);
wss = new WebSocketServer({ port });
wss.on('connection', (ws) => {
ctx.logger.debug('[Realtime] Client connected');
// Initialize State
clientStates.set(ws, { subscriptions: new Map() });
ws.on('message', (rawMessage) => {
try {
const data = JSON.parse(rawMessage.toString()) as any;
const messageId = data.messageId || randomUUID();
const timestamp = new Date().toISOString();
// Handle Subscriptions
if (data.type === 'subscribe') {
const msg = data as SubscribeMessage;
const state = clientStates.get(ws);
if (state && msg.subscription) {
state.subscriptions.set(msg.subscription.subscriptionId, msg.subscription);
// Send ACK
const ack: AckMessage = {
messageId: randomUUID(),
timestamp,
type: 'ack',
ackMessageId: messageId,
success: true
};
ws.send(JSON.stringify(ack));
ctx.logger.debug(`[Realtime] Subscribed: ${msg.subscription.subscriptionId}`);
}
}
// Handle Unsubscribe
if (data.type === 'unsubscribe') {
const msg = data as UnsubscribeMessage;
const state = clientStates.get(ws);
if (state && msg.request) {
if (msg.request.all) {
state.subscriptions.clear();
} else if (msg.request.subscriptionIds) {
msg.request.subscriptionIds.forEach(id => state.subscriptions.delete(id));
}
// Send ACK
const ack: AckMessage = {
messageId: randomUUID(),
timestamp,
type: 'ack',
ackMessageId: messageId,
success: true
};
ws.send(JSON.stringify(ack));
}
}
// Handle Presence Update from Client
if (data.type === 'presence') {
// Broadcast presence to others (simplified)
// In real impl, we should filter by channel/context
wss.clients.forEach(c => {
if (c !== ws && c.readyState === WebSocket.OPEN) {
c.send(JSON.stringify({
...data,
messageId: randomUUID(),
timestamp
}));
}
});
}
// Handle Cursor/Edit (Pass-through for collaboration)
if (data.type === 'cursor' || data.type === 'edit') {
// Broadcast to others in same context (simplified as global broadcast here)
wss.clients.forEach(c => {
if (c !== ws && c.readyState === WebSocket.OPEN) {
c.send(JSON.stringify({
...data,
messageId: randomUUID(),
timestamp
}));
}
});
}
// Handle Ping
if (data.type === 'ping') {
ws.send(JSON.stringify({
type: 'pong',
messageId: randomUUID(),
timestamp
}));
}
} catch (e) {
// Send Error
ws.send(JSON.stringify({
type: 'error',
messageId: randomUUID(),
timestamp: new Date().toISOString(),
code: 'INVALID_MESSAGE',
message: e instanceof Error ? e.message : 'Invalid JSON'
}));
}
});
ws.on('close', () => {
clientStates.delete(ws);
});
// Send Welcome Message (Optional, standardized as generic event or ignored in strict spec,
// but useful for debugging connectivity)
ws.send(JSON.stringify({
type: 'ack',
messageId: randomUUID(),
timestamp: new Date().toISOString(),
ackMessageId: 'init',
success: true
}));
});
ctx.logger.info('[Realtime] WebSocket Server started');
},
async destroy() {
if (wss) {
wss.clients.forEach(client => client.close());
wss.close();
}
},
async healthCheck(): Promise<PluginHealthReport> {
const clientCount = wss ? wss.clients.size : 0;
const status = wss ? 'healthy' : 'unhealthy';
const message = `${clientCount} connected clients`;
return {
status,
timestamp: new Date().toISOString(),
message,
metrics: {
uptime: startedAt ? Date.now() - startedAt : 0,
},
checks: [{ name: 'realtime', status: wss ? 'passed' : 'failed', message }],
};
},
getManifest(): { capabilities: PluginCapabilityManifest; security: PluginSecurityManifest } {
return {
capabilities: {},
security: {
pluginId: 'realtime',
trustLevel: 'trusted',
permissions: { permissions: [], defaultGrant: 'deny' },
sandbox: { enabled: false, level: 'none' },
},
};
},
getStartupResult(): PluginStartupResult {
return { plugin: { name: '@objectos/realtime', version: '0.1.0' }, success: !!wss, duration: 0 };
},
} as Plugin;
};