forked from JavaScriptSolidServer/JavaScriptSolidServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (47 loc) · 1.79 KB
/
Copy pathindex.js
File metadata and controls
52 lines (47 loc) · 1.79 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
/**
* Notifications Plugin
*
* Fastify plugin that adds WebSocket notification support.
* Implements the legacy "solid-0.1" protocol for SolidOS compatibility.
*
* Usage:
* createServer({ notifications: true })
*
* Discovery:
* OPTIONS /resource returns Updates-Via header with WebSocket URL
*
* Client usage:
* const ws = new WebSocket(updatesViaUrl);
* ws.send('sub http://example.org/resource');
* ws.onmessage = (e) => { if (e.data.startsWith('pub ')) ... }
*/
import websocket from '@fastify/websocket';
import { handleWebSocket, getConnectionCount, getSubscriptionCount } from './websocket.js';
import { getWebIdFromRequestAsync } from '../auth/token.js';
export { emitChange } from './events.js';
/**
* Register the notifications plugin with Fastify
* @param {FastifyInstance} fastify
* @param {object} options
*/
export async function notificationsPlugin(fastify, options) {
// Register the WebSocket plugin
await fastify.register(websocket);
// WebSocket route for notifications (dedicated path to avoid route conflicts)
// Clients discover this via Updates-Via header
// In @fastify/websocket v8, handler receives (connection, request) where connection.socket is the raw WebSocket
fastify.get('/.notifications', { websocket: true }, async (connection, request) => {
// Get WebID from auth token (if present) for ACL checking on subscriptions
const { webId } = await getWebIdFromRequestAsync(request);
handleWebSocket(connection.socket, request, webId);
});
// Optional: Status endpoint for monitoring
fastify.get('/.well-known/solid/notifications', async (request, reply) => {
return {
connections: getConnectionCount(),
subscriptions: getSubscriptionCount(),
protocol: 'solid-0.1'
};
});
}
export default notificationsPlugin;