acebase-server 1.16.2 → 1.18.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/README.md +180 -10
- package/bin/acebase-server.js +8 -0
- package/build-vars.js +2 -1
- package/dist/cjs/auth.js +88 -88
- package/dist/cjs/cluster.js +6 -56
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +24 -24
- package/dist/cjs/logger.js +73 -73
- package/dist/cjs/middleware/404.js +14 -14
- package/dist/cjs/middleware/admin-only.js +28 -28
- package/dist/cjs/middleware/cache.js +14 -14
- package/dist/cjs/middleware/connection.js +49 -49
- package/dist/cjs/middleware/context.js +20 -20
- package/dist/cjs/middleware/cors.js +50 -50
- package/dist/cjs/middleware/swagger.js +17 -17
- package/dist/cjs/middleware/user.js +57 -57
- package/dist/cjs/oauth-providers/dropbox.js +109 -109
- package/dist/cjs/oauth-providers/facebook.js +85 -85
- package/dist/cjs/oauth-providers/google.js +119 -119
- package/dist/cjs/oauth-providers/index.js +26 -26
- package/dist/cjs/oauth-providers/instagram.js +80 -80
- package/dist/cjs/oauth-providers/oauth-provider.js +20 -20
- package/dist/cjs/oauth-providers/spotify.js +113 -113
- package/dist/cjs/routes/auth-change-password.js +87 -87
- package/dist/cjs/routes/auth-delete.js +52 -52
- package/dist/cjs/routes/auth-forgot-password.js +77 -77
- package/dist/cjs/routes/auth-reset-password.js +97 -97
- package/dist/cjs/routes/auth-signin.js +46 -46
- package/dist/cjs/routes/auth-signout.js +61 -61
- package/dist/cjs/routes/auth-signup.js +142 -142
- package/dist/cjs/routes/auth-state.js +25 -25
- package/dist/cjs/routes/auth-update.js +131 -131
- package/dist/cjs/routes/auth-verify-email.js +71 -71
- package/dist/cjs/routes/auth.js +57 -54
- package/dist/cjs/routes/data-exists.js +33 -33
- package/dist/cjs/routes/data-export.js +48 -48
- package/dist/cjs/routes/data-get.js +70 -66
- package/dist/cjs/routes/data-import.js +54 -54
- package/dist/cjs/routes/data-index-create.js +42 -42
- package/dist/cjs/routes/data-index-delete.js +33 -33
- package/dist/cjs/routes/data-index-list.js +31 -31
- package/dist/cjs/routes/data-query.js +80 -70
- package/dist/cjs/routes/data-reflect.js +97 -86
- package/dist/cjs/routes/data-schema-get.js +36 -36
- package/dist/cjs/routes/data-schema-set.js +30 -30
- package/dist/cjs/routes/data-schema-test.js +43 -43
- package/dist/cjs/routes/data-schemas-list.js +32 -32
- package/dist/cjs/routes/data-set.js +85 -75
- package/dist/cjs/routes/data-sync-changes.js +81 -67
- package/dist/cjs/routes/data-sync-mutations.js +81 -67
- package/dist/cjs/routes/data-transaction.js +159 -139
- package/dist/cjs/routes/data-update.js +86 -75
- package/dist/cjs/routes/data.js +62 -62
- package/dist/cjs/routes/docs.js +258 -251
- package/dist/cjs/routes/meta-info.js +63 -63
- package/dist/cjs/routes/meta-logs.js +49 -49
- package/dist/cjs/routes/meta-ping.js +11 -11
- package/dist/cjs/routes/meta-stats.js +27 -27
- package/dist/cjs/routes/meta.js +19 -19
- package/dist/cjs/routes/oauth2-init.js +54 -54
- package/dist/cjs/routes/oauth2-refresh.js +42 -42
- package/dist/cjs/routes/oauth2-signin.js +265 -265
- package/dist/cjs/routes/webmanager.js +26 -26
- package/dist/cjs/rules.js +380 -167
- package/dist/cjs/sandbox.js +28 -0
- package/dist/cjs/schema/user.js +24 -24
- package/dist/cjs/server.js +410 -407
- package/dist/cjs/settings/email.js +2 -2
- package/dist/cjs/settings/index.js +175 -172
- package/dist/cjs/shared/clients.js +26 -26
- package/dist/cjs/shared/email.js +2 -2
- package/dist/cjs/shared/env.js +2 -2
- package/dist/cjs/shared/error.js +33 -33
- package/dist/cjs/shared/http.js +28 -28
- package/dist/cjs/shared/password.js +45 -45
- package/dist/cjs/shared/rootpath.js +16 -16
- package/dist/cjs/shared/signin.js +143 -143
- package/dist/cjs/shared/simple-fetch.js +76 -76
- package/dist/cjs/shared/tokens.js +79 -79
- package/dist/cjs/shared/validate.js +61 -61
- package/dist/cjs/start.js +119 -53
- package/dist/cjs/test.js +10 -10
- package/dist/cjs/websocket/index.js +331 -319
- package/dist/cjs/websocket/manager.js +15 -15
- package/dist/cjs/websocket/socket.io.js +76 -76
- package/dist/esm/auth.js +75 -75
- package/dist/esm/cluster.js +6 -54
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +4 -4
- package/dist/esm/logger.js +51 -51
- package/dist/esm/middleware/404.js +10 -10
- package/dist/esm/middleware/admin-only.js +24 -24
- package/dist/esm/middleware/cache.js +10 -10
- package/dist/esm/middleware/connection.js +45 -45
- package/dist/esm/middleware/context.js +16 -16
- package/dist/esm/middleware/cors.js +44 -44
- package/dist/esm/middleware/swagger.js +13 -13
- package/dist/esm/middleware/user.js +44 -44
- package/dist/esm/oauth-providers/dropbox.js +90 -90
- package/dist/esm/oauth-providers/facebook.js +66 -66
- package/dist/esm/oauth-providers/google.js +96 -96
- package/dist/esm/oauth-providers/index.js +18 -18
- package/dist/esm/oauth-providers/instagram.js +61 -61
- package/dist/esm/oauth-providers/oauth-provider.js +16 -16
- package/dist/esm/oauth-providers/spotify.js +92 -92
- package/dist/esm/routes/auth-change-password.js +72 -72
- package/dist/esm/routes/auth-delete.js +37 -37
- package/dist/esm/routes/auth-forgot-password.js +62 -62
- package/dist/esm/routes/auth-reset-password.js +81 -81
- package/dist/esm/routes/auth-signin.js +33 -33
- package/dist/esm/routes/auth-signout.js +47 -47
- package/dist/esm/routes/auth-signup.js +127 -127
- package/dist/esm/routes/auth-state.js +12 -12
- package/dist/esm/routes/auth-update.js +116 -116
- package/dist/esm/routes/auth-verify-email.js +57 -57
- package/dist/esm/routes/auth.js +53 -50
- package/dist/esm/routes/data-exists.js +20 -20
- package/dist/esm/routes/data-export.js +35 -35
- package/dist/esm/routes/data-get.js +56 -52
- package/dist/esm/routes/data-import.js +41 -41
- package/dist/esm/routes/data-index-create.js +28 -28
- package/dist/esm/routes/data-index-delete.js +20 -20
- package/dist/esm/routes/data-index-list.js +18 -18
- package/dist/esm/routes/data-query.js +66 -56
- package/dist/esm/routes/data-reflect.js +83 -72
- package/dist/esm/routes/data-schema-get.js +23 -23
- package/dist/esm/routes/data-schema-set.js +17 -17
- package/dist/esm/routes/data-schema-test.js +29 -29
- package/dist/esm/routes/data-schemas-list.js +19 -19
- package/dist/esm/routes/data-set.js +70 -60
- package/dist/esm/routes/data-sync-changes.js +67 -53
- package/dist/esm/routes/data-sync-mutations.js +67 -53
- package/dist/esm/routes/data-transaction.js +142 -123
- package/dist/esm/routes/data-update.js +71 -60
- package/dist/esm/routes/data.js +58 -58
- package/dist/esm/routes/docs.js +252 -245
- package/dist/esm/routes/meta-info.js +59 -59
- package/dist/esm/routes/meta-logs.js +35 -35
- package/dist/esm/routes/meta-ping.js +7 -7
- package/dist/esm/routes/meta-stats.js +14 -14
- package/dist/esm/routes/meta.js +15 -15
- package/dist/esm/routes/oauth2-init.js +41 -41
- package/dist/esm/routes/oauth2-refresh.js +29 -29
- package/dist/esm/routes/oauth2-signin.js +251 -251
- package/dist/esm/routes/webmanager.js +22 -22
- package/dist/esm/rules.js +374 -163
- package/dist/esm/sandbox.js +12 -0
- package/dist/esm/schema/user.js +19 -19
- package/dist/esm/server.js +395 -387
- package/dist/esm/settings/email.js +1 -1
- package/dist/esm/settings/index.js +168 -165
- package/dist/esm/shared/clients.js +22 -22
- package/dist/esm/shared/email.js +1 -1
- package/dist/esm/shared/env.js +1 -1
- package/dist/esm/shared/error.js +25 -25
- package/dist/esm/shared/http.js +22 -22
- package/dist/esm/shared/password.js +38 -38
- package/dist/esm/shared/rootpath.js +13 -13
- package/dist/esm/shared/signin.js +128 -128
- package/dist/esm/shared/simple-fetch.js +59 -59
- package/dist/esm/shared/tokens.js +72 -72
- package/dist/esm/shared/validate.js +41 -41
- package/dist/esm/start.js +116 -50
- package/dist/esm/test.js +8 -8
- package/dist/esm/websocket/index.js +313 -302
- package/dist/esm/websocket/manager.js +11 -11
- package/dist/esm/websocket/socket.io.js +71 -71
- package/dist/types/auth.d.ts +3 -3
- package/dist/types/cluster.d.ts +6 -1
- package/dist/types/index.d.ts +4 -4
- package/dist/types/logger.d.ts +35 -35
- package/dist/types/middleware/404.d.ts +7 -7
- package/dist/types/middleware/admin-only.d.ts +19 -19
- package/dist/types/middleware/cache.d.ts +3 -3
- package/dist/types/middleware/connection.d.ts +7 -7
- package/dist/types/middleware/context.d.ts +3 -3
- package/dist/types/middleware/cors.d.ts +25 -25
- package/dist/types/middleware/swagger.d.ts +3 -3
- package/dist/types/middleware/user.d.ts +2 -2
- package/dist/types/oauth-providers/dropbox.d.ts +52 -52
- package/dist/types/oauth-providers/facebook.d.ts +41 -41
- package/dist/types/oauth-providers/google.d.ts +43 -43
- package/dist/types/oauth-providers/index.d.ts +10 -10
- package/dist/types/oauth-providers/instagram.d.ts +50 -50
- package/dist/types/oauth-providers/oauth-provider.d.ts +74 -74
- package/dist/types/oauth-providers/spotify.d.ts +53 -53
- package/dist/types/routes/auth-change-password.d.ts +20 -20
- package/dist/types/routes/auth-delete.d.ts +16 -16
- package/dist/types/routes/auth-forgot-password.d.ts +16 -16
- package/dist/types/routes/auth-reset-password.d.ts +23 -23
- package/dist/types/routes/auth-signin.d.ts +24 -24
- package/dist/types/routes/auth-signout.d.ts +14 -14
- package/dist/types/routes/auth-signup.d.ts +32 -32
- package/dist/types/routes/auth-state.d.ts +13 -13
- package/dist/types/routes/auth-update.d.ts +33 -33
- package/dist/types/routes/auth-verify-email.d.ts +21 -21
- package/dist/types/routes/auth.d.ts +6 -6
- package/dist/types/routes/data-exists.d.ts +9 -9
- package/dist/types/routes/data-export.d.ts +10 -10
- package/dist/types/routes/data-get.d.ts +14 -14
- package/dist/types/routes/data-import.d.ts +13 -13
- package/dist/types/routes/data-index-create.d.ts +51 -51
- package/dist/types/routes/data-index-delete.d.ts +17 -17
- package/dist/types/routes/data-index-list.d.ts +21 -21
- package/dist/types/routes/data-query.d.ts +49 -49
- package/dist/types/routes/data-reflect.d.ts +29 -29
- package/dist/types/routes/data-schema-get.d.ts +11 -11
- package/dist/types/routes/data-schema-set.d.ts +20 -19
- package/dist/types/routes/data-schema-test.d.ts +19 -19
- package/dist/types/routes/data-schemas-list.d.ts +11 -11
- package/dist/types/routes/data-set.d.ts +27 -27
- package/dist/types/routes/data-sync-changes.d.ts +25 -28
- package/dist/types/routes/data-sync-mutations.d.ts +25 -28
- package/dist/types/routes/data-transaction.d.ts +34 -34
- package/dist/types/routes/data-update.d.ts +27 -27
- package/dist/types/routes/data.d.ts +3 -3
- package/dist/types/routes/docs.d.ts +3 -3
- package/dist/types/routes/meta-info.d.ts +11 -11
- package/dist/types/routes/meta-logs.d.ts +20 -20
- package/dist/types/routes/meta-ping.d.ts +7 -7
- package/dist/types/routes/meta-stats.d.ts +13 -13
- package/dist/types/routes/meta.d.ts +3 -3
- package/dist/types/routes/oauth2-init.d.ts +13 -13
- package/dist/types/routes/oauth2-refresh.d.ts +17 -17
- package/dist/types/routes/oauth2-signin.d.ts +15 -15
- package/dist/types/routes/webmanager.d.ts +3 -3
- package/dist/types/rules.d.ts +73 -34
- package/dist/types/sandbox.d.ts +3 -0
- package/dist/types/schema/user.d.ts +85 -85
- package/dist/types/server.d.ts +117 -111
- package/dist/types/settings/email.d.ts +14 -14
- package/dist/types/settings/index.d.ts +195 -187
- package/dist/types/shared/clients.d.ts +46 -46
- package/dist/types/shared/email.d.ts +34 -34
- package/dist/types/shared/env.d.ts +47 -44
- package/dist/types/shared/error.d.ts +15 -15
- package/dist/types/shared/http.d.ts +22 -21
- package/dist/types/shared/password.d.ts +19 -19
- package/dist/types/shared/rootpath.d.ts +2 -2
- package/dist/types/shared/signin.d.ts +31 -31
- package/dist/types/shared/simple-fetch.d.ts +21 -21
- package/dist/types/shared/tokens.d.ts +24 -24
- package/dist/types/shared/validate.d.ts +22 -22
- package/dist/types/start.d.ts +1 -1
- package/dist/types/test.d.ts +1 -1
- package/dist/types/websocket/index.d.ts +6 -6
- package/dist/types/websocket/manager.d.ts +99 -99
- package/dist/types/websocket/socket.io.d.ts +10 -10
- package/package.json +7 -4
- package/src/routes/data-schema-set.yaml +4 -0
- package/src/routes/data-set.yaml +5 -5
- package/src/routes/data-update.yaml +4 -4
|
@@ -1,303 +1,314 @@
|
|
|
1
|
-
import { ColorStyle, Transport } from 'acebase-core';
|
|
2
|
-
import { ConnectedClient } from '../shared/clients.js';
|
|
3
|
-
import { decodePublicAccessToken } from '../shared/tokens.js';
|
|
4
|
-
import { createServer } from './socket.io.js';
|
|
5
|
-
export class SocketRequestError extends Error {
|
|
6
|
-
constructor(code, message) {
|
|
7
|
-
super(message);
|
|
8
|
-
this.code = code;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export const addWebsocketServer = (env) => {
|
|
12
|
-
// TODO: Allow using uWebSockets.js server instead of Socket.IO
|
|
13
|
-
const serverManager = createServer(env);
|
|
14
|
-
const getClientBySocketId = (id, event) => {
|
|
15
|
-
const client = env.clients.get(id);
|
|
16
|
-
if (!client) {
|
|
17
|
-
env.debug.error(`Cannot find client "${id}" for socket event "${event}"`);
|
|
18
|
-
}
|
|
19
|
-
return client;
|
|
20
|
-
};
|
|
21
|
-
serverManager.on('connect', event => {
|
|
22
|
-
const client = new ConnectedClient(event.socket);
|
|
23
|
-
env.clients.set(client.id, client);
|
|
24
|
-
env.debug.warn(`New socket connected, total: ${env.clients.size}`);
|
|
25
|
-
serverManager.send(event.socket, 'welcome');
|
|
26
|
-
});
|
|
27
|
-
serverManager.on('disconnect', event => {
|
|
28
|
-
// We lost one
|
|
29
|
-
const client = getClientBySocketId(event.socket_id, 'disconnect');
|
|
30
|
-
if (!client) {
|
|
31
|
-
return;
|
|
32
|
-
} // Disconnected a client we did not know? Don't crash, just ignore.
|
|
33
|
-
const subscribedPaths = Object.keys(client.subscriptions);
|
|
34
|
-
if (subscribedPaths.length > 0) {
|
|
35
|
-
// TODO: Substitute the original callbacks to cache them
|
|
36
|
-
// if the client then reconnects within a certain time,
|
|
37
|
-
// we can send the missed notifications
|
|
38
|
-
//
|
|
39
|
-
// subscribedPaths.forEach(path => {
|
|
40
|
-
// client.subscriptions[path].forEach(subscr => {
|
|
41
|
-
// subscr.callback
|
|
42
|
-
// })
|
|
43
|
-
// });
|
|
44
|
-
const remove = [];
|
|
45
|
-
subscribedPaths.forEach(path => {
|
|
46
|
-
remove.push(...client.subscriptions[path]);
|
|
47
|
-
});
|
|
48
|
-
remove.forEach(subscr => {
|
|
49
|
-
// Unsubscribe them at db level and remove from our list
|
|
50
|
-
env.db.api.unsubscribe(subscr.path, subscr.event, subscr.callback); //db.ref(subscr.path).off(subscr.event, subscr.callback);
|
|
51
|
-
const pathSubs = client.subscriptions[subscr.path];
|
|
52
|
-
pathSubs.splice(pathSubs.indexOf(subscr), 1);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
env.clients.delete(client.id);
|
|
56
|
-
env.debug.verbose(`Socket disconnected, total: ${env.clients.size}`);
|
|
57
|
-
});
|
|
58
|
-
serverManager.on('signin', event => {
|
|
59
|
-
// client sends this request once user has been signed in, binds the user to the socket,
|
|
60
|
-
// deprecated since client v0.9.4, which sends client_id with signin api call
|
|
61
|
-
// const client = clients.get(socket.id);
|
|
62
|
-
const client = getClientBySocketId(event.socket_id, 'signin');
|
|
63
|
-
if (!client) {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
try {
|
|
67
|
-
const uid = decodePublicAccessToken(event.data.accessToken, env.tokenSalt).uid;
|
|
68
|
-
client.user = env.authCache.get(uid) || null;
|
|
69
|
-
}
|
|
70
|
-
catch (err) {
|
|
71
|
-
// no way to bind the user
|
|
72
|
-
env.debug.error(`websocket: invalid access token passed to signin: ${event.data.accessToken}`);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
serverManager.on('signout', event => {
|
|
76
|
-
// deprecated since client v0.9.4, which sends client_id with signout api call
|
|
77
|
-
// const client = clients.get(socket.id);
|
|
78
|
-
const client = getClientBySocketId(event.socket_id, 'signout');
|
|
79
|
-
if (!client) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
client.user = null;
|
|
83
|
-
});
|
|
84
|
-
serverManager.on('oauth2-signin', async (event) => {
|
|
85
|
-
// acebase-client does not use socket oauth flow yet
|
|
86
|
-
const client = getClientBySocketId(event.socket_id, 'oauth2-signin');
|
|
87
|
-
if (!client) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
const request = event.data;
|
|
91
|
-
const providerName = typeof request === 'string' ? request : request.provider;
|
|
92
|
-
try {
|
|
93
|
-
const provider = env.authProviders[providerName];
|
|
94
|
-
const state = Buffer.from(JSON.stringify({ flow: 'socket', provider: providerName, client_id: client.id })).toString('base64');
|
|
95
|
-
const clientAuthUrl = await provider.init({ redirect_url: `${request.server.protocol}://${request.server.host}:${request.server.port}/ouath2/${env.db.name}/signin`, state, options: request.options });
|
|
96
|
-
serverManager.send(event.socket, 'oauth2-signin', { action: 'auth', url: clientAuthUrl });
|
|
97
|
-
}
|
|
98
|
-
catch (err) {
|
|
99
|
-
env.debug.error(`websocket: cannot sign in with oauth provider ${providerName}`);
|
|
100
|
-
serverManager.send(event.socket, 'oauth2-signin', { error: err.message });
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
const acknowledgeRequest = (socket, requestId) => {
|
|
104
|
-
// Send acknowledgement
|
|
105
|
-
serverManager.send(socket, 'result', {
|
|
106
|
-
success: true,
|
|
107
|
-
req_id: requestId,
|
|
108
|
-
});
|
|
109
|
-
};
|
|
110
|
-
const failRequest = (socket, requestId, code) => {
|
|
111
|
-
// Send error
|
|
112
|
-
serverManager.send(socket, 'result', {
|
|
113
|
-
success: false,
|
|
114
|
-
reason: code,
|
|
115
|
-
req_id: requestId,
|
|
116
|
-
});
|
|
117
|
-
};
|
|
118
|
-
serverManager.on('subscribe', event => {
|
|
119
|
-
// Client wants to subscribe to events on a node
|
|
120
|
-
const client = getClientBySocketId(event.socket_id, 'subscribe');
|
|
121
|
-
if (!client) {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const subscriptionPath = event.data.path;
|
|
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
|
-
const
|
|
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
|
-
const
|
|
228
|
-
const
|
|
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
|
-
}
|
|
1
|
+
import { ColorStyle, Transport } from 'acebase-core';
|
|
2
|
+
import { ConnectedClient } from '../shared/clients.js';
|
|
3
|
+
import { decodePublicAccessToken } from '../shared/tokens.js';
|
|
4
|
+
import { createServer } from './socket.io.js';
|
|
5
|
+
export class SocketRequestError extends Error {
|
|
6
|
+
constructor(code, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.code = code;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export const addWebsocketServer = (env) => {
|
|
12
|
+
// TODO: Allow using uWebSockets.js server instead of Socket.IO
|
|
13
|
+
const serverManager = createServer(env);
|
|
14
|
+
const getClientBySocketId = (id, event) => {
|
|
15
|
+
const client = env.clients.get(id);
|
|
16
|
+
if (!client) {
|
|
17
|
+
env.debug.error(`Cannot find client "${id}" for socket event "${event}"`);
|
|
18
|
+
}
|
|
19
|
+
return client;
|
|
20
|
+
};
|
|
21
|
+
serverManager.on('connect', event => {
|
|
22
|
+
const client = new ConnectedClient(event.socket);
|
|
23
|
+
env.clients.set(client.id, client);
|
|
24
|
+
env.debug.warn(`New socket connected, total: ${env.clients.size}`);
|
|
25
|
+
serverManager.send(event.socket, 'welcome');
|
|
26
|
+
});
|
|
27
|
+
serverManager.on('disconnect', event => {
|
|
28
|
+
// We lost one
|
|
29
|
+
const client = getClientBySocketId(event.socket_id, 'disconnect');
|
|
30
|
+
if (!client) {
|
|
31
|
+
return;
|
|
32
|
+
} // Disconnected a client we did not know? Don't crash, just ignore.
|
|
33
|
+
const subscribedPaths = Object.keys(client.subscriptions);
|
|
34
|
+
if (subscribedPaths.length > 0) {
|
|
35
|
+
// TODO: Substitute the original callbacks to cache them
|
|
36
|
+
// if the client then reconnects within a certain time,
|
|
37
|
+
// we can send the missed notifications
|
|
38
|
+
//
|
|
39
|
+
// subscribedPaths.forEach(path => {
|
|
40
|
+
// client.subscriptions[path].forEach(subscr => {
|
|
41
|
+
// subscr.callback
|
|
42
|
+
// })
|
|
43
|
+
// });
|
|
44
|
+
const remove = [];
|
|
45
|
+
subscribedPaths.forEach(path => {
|
|
46
|
+
remove.push(...client.subscriptions[path]);
|
|
47
|
+
});
|
|
48
|
+
remove.forEach(subscr => {
|
|
49
|
+
// Unsubscribe them at db level and remove from our list
|
|
50
|
+
env.db.api.unsubscribe(subscr.path, subscr.event, subscr.callback); //db.ref(subscr.path).off(subscr.event, subscr.callback);
|
|
51
|
+
const pathSubs = client.subscriptions[subscr.path];
|
|
52
|
+
pathSubs.splice(pathSubs.indexOf(subscr), 1);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
env.clients.delete(client.id);
|
|
56
|
+
env.debug.verbose(`Socket disconnected, total: ${env.clients.size}`);
|
|
57
|
+
});
|
|
58
|
+
serverManager.on('signin', event => {
|
|
59
|
+
// client sends this request once user has been signed in, binds the user to the socket,
|
|
60
|
+
// deprecated since client v0.9.4, which sends client_id with signin api call
|
|
61
|
+
// const client = clients.get(socket.id);
|
|
62
|
+
const client = getClientBySocketId(event.socket_id, 'signin');
|
|
63
|
+
if (!client) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const uid = decodePublicAccessToken(event.data.accessToken, env.tokenSalt).uid;
|
|
68
|
+
client.user = env.authCache.get(uid) || null;
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
// no way to bind the user
|
|
72
|
+
env.debug.error(`websocket: invalid access token passed to signin: ${event.data.accessToken}`);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
serverManager.on('signout', event => {
|
|
76
|
+
// deprecated since client v0.9.4, which sends client_id with signout api call
|
|
77
|
+
// const client = clients.get(socket.id);
|
|
78
|
+
const client = getClientBySocketId(event.socket_id, 'signout');
|
|
79
|
+
if (!client) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
client.user = null;
|
|
83
|
+
});
|
|
84
|
+
serverManager.on('oauth2-signin', async (event) => {
|
|
85
|
+
// acebase-client does not use socket oauth flow yet
|
|
86
|
+
const client = getClientBySocketId(event.socket_id, 'oauth2-signin');
|
|
87
|
+
if (!client) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const request = event.data;
|
|
91
|
+
const providerName = typeof request === 'string' ? request : request.provider;
|
|
92
|
+
try {
|
|
93
|
+
const provider = env.authProviders[providerName];
|
|
94
|
+
const state = Buffer.from(JSON.stringify({ flow: 'socket', provider: providerName, client_id: client.id })).toString('base64');
|
|
95
|
+
const clientAuthUrl = await provider.init({ redirect_url: `${request.server.protocol}://${request.server.host}:${request.server.port}/ouath2/${env.db.name}/signin`, state, options: request.options });
|
|
96
|
+
serverManager.send(event.socket, 'oauth2-signin', { action: 'auth', url: clientAuthUrl });
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
env.debug.error(`websocket: cannot sign in with oauth provider ${providerName}`);
|
|
100
|
+
serverManager.send(event.socket, 'oauth2-signin', { error: err.message });
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
const acknowledgeRequest = (socket, requestId) => {
|
|
104
|
+
// Send acknowledgement
|
|
105
|
+
serverManager.send(socket, 'result', {
|
|
106
|
+
success: true,
|
|
107
|
+
req_id: requestId,
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
const failRequest = (socket, requestId, code) => {
|
|
111
|
+
// Send error
|
|
112
|
+
serverManager.send(socket, 'result', {
|
|
113
|
+
success: false,
|
|
114
|
+
reason: code,
|
|
115
|
+
req_id: requestId,
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
serverManager.on('subscribe', async (event) => {
|
|
119
|
+
// Client wants to subscribe to events on a node
|
|
120
|
+
const client = getClientBySocketId(event.socket_id, 'subscribe');
|
|
121
|
+
if (!client) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const eventName = event.data.event;
|
|
125
|
+
const subscriptionPath = event.data.path;
|
|
126
|
+
env.debug.verbose(`Client ${event.socket_id} subscribes to event "${eventName}" on path "/${subscriptionPath}"`.colorize([ColorStyle.bgWhite, ColorStyle.black]));
|
|
127
|
+
const isSubscribed = () => subscriptionPath in client.subscriptions && client.subscriptions[subscriptionPath].some(s => s.event === eventName);
|
|
128
|
+
if (isSubscribed()) {
|
|
129
|
+
return acknowledgeRequest(event.socket, event.data.req_id);
|
|
130
|
+
}
|
|
131
|
+
// Get client
|
|
132
|
+
// const client = clients.get(socket.id);
|
|
133
|
+
if (!await env.rules.isOperationAllowed(client.user, subscriptionPath, 'get')) {
|
|
134
|
+
env.log.error('event.subscribe', 'access_denied', { uid: client.user?.uid ?? 'anonymous', path: subscriptionPath });
|
|
135
|
+
return failRequest(event.socket, event.data.req_id, 'access_denied');
|
|
136
|
+
}
|
|
137
|
+
const callback = async (err, path, currentValue, previousValue, context) => {
|
|
138
|
+
if (!isSubscribed()) {
|
|
139
|
+
// Not subscribed anymore. Cancel sending
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (err) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (!await env.rules.isOperationAllowed(client.user, path, 'get', { value: currentValue, context })) { // 'event', { eventName, subscriptionPath, currentValue, previousValue, context })
|
|
146
|
+
if (!subscriptionPath.includes('*') && !subscriptionPath.includes('$')) {
|
|
147
|
+
// Could potentially be very many callbacks, so
|
|
148
|
+
// DISABLED: logRef.push({ action: `access_revoked`, uid: client.user ? client.user.uid : '-', path: subscriptionPath });
|
|
149
|
+
// Only log when user subscribes again
|
|
150
|
+
failRequest(event.socket, event.data.req_id, 'access_denied');
|
|
151
|
+
}
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const val = Transport.serialize({
|
|
155
|
+
current: currentValue,
|
|
156
|
+
previous: previousValue,
|
|
157
|
+
});
|
|
158
|
+
env.debug.verbose(`Sending data event "${eventName}" for path "/${path}" to client ${event.socket_id}`.colorize([ColorStyle.bgWhite, ColorStyle.black]));
|
|
159
|
+
// TODO: let large data events notify the client, then let them download the data manually so it doesn't have to be transmitted through the websocket
|
|
160
|
+
serverManager.send(event.socket, 'data-event', {
|
|
161
|
+
subscr_path: subscriptionPath,
|
|
162
|
+
path,
|
|
163
|
+
event: eventName,
|
|
164
|
+
val,
|
|
165
|
+
context,
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
let pathSubs = client.subscriptions[subscriptionPath];
|
|
169
|
+
if (!pathSubs) {
|
|
170
|
+
pathSubs = client.subscriptions[subscriptionPath] = [];
|
|
171
|
+
}
|
|
172
|
+
const subscr = { path: subscriptionPath, event: eventName, callback };
|
|
173
|
+
pathSubs.push(subscr);
|
|
174
|
+
env.db.api.subscribe(subscriptionPath, eventName, callback);
|
|
175
|
+
acknowledgeRequest(event.socket, event.data.req_id);
|
|
176
|
+
});
|
|
177
|
+
serverManager.on('unsubscribe', event => {
|
|
178
|
+
// Client unsubscribes from events on a node
|
|
179
|
+
const client = getClientBySocketId(event.socket_id, 'unsubscribe');
|
|
180
|
+
if (!client) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const eventName = event.data.event;
|
|
184
|
+
const subscriptionPath = event.data.path;
|
|
185
|
+
env.debug.verbose(`Client ${event.socket_id} is unsubscribing from event "${eventName || '(any)'}" on path "/${subscriptionPath}"`.colorize([ColorStyle.bgWhite, ColorStyle.black]));
|
|
186
|
+
// const client = clients.get(socket.id);
|
|
187
|
+
const pathSubs = client.subscriptions[subscriptionPath];
|
|
188
|
+
if (!pathSubs) {
|
|
189
|
+
// We have no knowledge of any active subscriptions on this path
|
|
190
|
+
return acknowledgeRequest(event.socket, event.data.req_id);
|
|
191
|
+
}
|
|
192
|
+
let remove = pathSubs;
|
|
193
|
+
if (eventName) {
|
|
194
|
+
// Unsubscribe from a specific event
|
|
195
|
+
remove = pathSubs.filter(subscr => subscr.event === eventName);
|
|
196
|
+
}
|
|
197
|
+
remove.forEach(subscr => {
|
|
198
|
+
// Unsubscribe them at db level and remove from our list
|
|
199
|
+
//this.debug.verbose(` - unsubscribing from event ${subscr.event} with${subscr.callback ? "" : "out"} callback on path "${data.path}"`);
|
|
200
|
+
env.db.api.unsubscribe(subscr.path, subscr.event, subscr.callback); //db.api.unsubscribe(data.path, subscr.event, subscr.callback);
|
|
201
|
+
pathSubs.splice(pathSubs.indexOf(subscr), 1);
|
|
202
|
+
});
|
|
203
|
+
if (pathSubs.length === 0) {
|
|
204
|
+
// No subscriptions left on this path, remove the path entry
|
|
205
|
+
delete client.subscriptions[subscriptionPath];
|
|
206
|
+
}
|
|
207
|
+
return acknowledgeRequest(event.socket, event.data.req_id);
|
|
208
|
+
});
|
|
209
|
+
serverManager.on('query-unsubscribe', event => {
|
|
210
|
+
// Client unsubscribing from realtime query events
|
|
211
|
+
const client = getClientBySocketId(event.socket_id, 'query-unsubscribe');
|
|
212
|
+
if (!client) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
env.debug.verbose(`Client ${event.socket_id} is unsubscribing from realtime query "${event.data.query_id}"`);
|
|
216
|
+
// const client = clients.get(socket.id);
|
|
217
|
+
delete client.realtimeQueries[event.data.query_id];
|
|
218
|
+
acknowledgeRequest(event.socket, event.data.req_id);
|
|
219
|
+
});
|
|
220
|
+
const TRANSACTION_TIMEOUT_MS = 10000; // 10s to finish a started transaction
|
|
221
|
+
serverManager.on('transaction-start', async (event) => {
|
|
222
|
+
// Start transaction
|
|
223
|
+
const client = getClientBySocketId(event.socket_id, 'transaction-start');
|
|
224
|
+
if (!client || !event.data) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const LOG_ACTION = 'socket.transaction.start';
|
|
228
|
+
const LOG_DETAILS = { ip: event.socket.conn.remoteAddress, uid: client.user?.uid ?? null, path: event.data.path };
|
|
229
|
+
env.debug.verbose(`Client ${event.socket_id} is sending transaction start request on path "${event.data.path}"`);
|
|
230
|
+
const data = event.data;
|
|
231
|
+
// Pre-check if reading AND writing is allowed (special transact operation)
|
|
232
|
+
const access = await env.rules.isOperationAllowed(client.user, data.path, 'transact');
|
|
233
|
+
if (!access.allow) {
|
|
234
|
+
env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null }, access.details);
|
|
235
|
+
return serverManager.send(event.socket, 'tx_error', { id: data.id, reason: 'access_denied' });
|
|
236
|
+
}
|
|
237
|
+
const tx = {
|
|
238
|
+
id: data.id,
|
|
239
|
+
started: Date.now(),
|
|
240
|
+
path: data.path,
|
|
241
|
+
context: data.context,
|
|
242
|
+
finish: undefined,
|
|
243
|
+
timeout: setTimeout(() => {
|
|
244
|
+
delete client.transactions[tx.id];
|
|
245
|
+
tx.finish(); // Finish without value cancels the transaction
|
|
246
|
+
env.log.error(LOG_ACTION, 'timeout', LOG_DETAILS);
|
|
247
|
+
serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: 'timeout' });
|
|
248
|
+
}, TRANSACTION_TIMEOUT_MS),
|
|
249
|
+
};
|
|
250
|
+
// Bind to client
|
|
251
|
+
client.transactions[data.id] = tx;
|
|
252
|
+
// Start transaction
|
|
253
|
+
env.debug.verbose(`Transaction ${tx.id} starting...`);
|
|
254
|
+
const donePromise = env.db.api.transaction(tx.path, async (val) => {
|
|
255
|
+
env.debug.verbose(`Transaction ${tx.id} started with value: `, val);
|
|
256
|
+
const access = await env.rules.isOperationAllowed(client.user, data.path, 'get', { value: val, context: tx.context });
|
|
257
|
+
if (!access.allow) {
|
|
258
|
+
env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null }, access.details);
|
|
259
|
+
serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: 'access_denied' });
|
|
260
|
+
return; // Return undefined to cancel transaction
|
|
261
|
+
}
|
|
262
|
+
const currentValue = Transport.serialize(val);
|
|
263
|
+
const promise = new Promise((resolve) => {
|
|
264
|
+
tx.finish = (val) => {
|
|
265
|
+
env.debug.verbose(`Transaction ${tx.id} finishing with value: `, val);
|
|
266
|
+
resolve(val);
|
|
267
|
+
return donePromise;
|
|
268
|
+
};
|
|
269
|
+
});
|
|
270
|
+
serverManager.send(event.socket, 'tx_started', { id: tx.id, value: currentValue });
|
|
271
|
+
return promise;
|
|
272
|
+
}, { context: tx.context });
|
|
273
|
+
});
|
|
274
|
+
serverManager.on('transaction-finish', async (event) => {
|
|
275
|
+
// Finish transaction
|
|
276
|
+
const client = getClientBySocketId(event.socket_id, 'transaction-finish');
|
|
277
|
+
if (!client || !event.data) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const LOG_ACTION = 'socket.transaction.finish';
|
|
281
|
+
const LOG_DETAILS = { ip: event.socket.conn.remoteAddress, uid: client.user?.uid ?? null, path: event.data.path };
|
|
282
|
+
const data = event.data;
|
|
283
|
+
const tx = client.transactions[data.id];
|
|
284
|
+
try {
|
|
285
|
+
if (!tx || data.path !== tx.path) {
|
|
286
|
+
env.log.error(LOG_ACTION, tx ? 'wrong_path' : 'not_found', { ...LOG_DETAILS, id: data.id, tx_path: tx?.path ?? null });
|
|
287
|
+
throw new SocketRequestError('transaction_not_found', 'Transaction not found');
|
|
288
|
+
}
|
|
289
|
+
clearTimeout(tx.timeout);
|
|
290
|
+
delete client.transactions[data.id];
|
|
291
|
+
const newValue = 'val' in data.value ? Transport.deserialize(data.value) : undefined;
|
|
292
|
+
if (typeof newValue !== 'undefined') {
|
|
293
|
+
const access = await env.rules.isOperationAllowed(client.user, data.path, 'set', { value: newValue, context: tx.context });
|
|
294
|
+
if (!access.allow) {
|
|
295
|
+
env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null }, access.details);
|
|
296
|
+
throw new SocketRequestError('access_denied', 'Access denied');
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const { cursor } = await tx.finish(newValue);
|
|
300
|
+
env.debug.verbose(`Transaction ${tx.id} finished`);
|
|
301
|
+
serverManager.send(event.socket, 'tx_completed', { id: tx.id, context: { cursor } });
|
|
302
|
+
}
|
|
303
|
+
catch (err) {
|
|
304
|
+
if (!(err instanceof SocketRequestError)) {
|
|
305
|
+
// Other errors have been logged already
|
|
306
|
+
env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
|
|
307
|
+
}
|
|
308
|
+
serverManager.send(event.socket, 'tx_error', { id: tx.id, reason: err.code ?? err.message, data });
|
|
309
|
+
tx.finish(); // Finish with undefined, canceling the transaction
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
return serverManager;
|
|
313
|
+
};
|
|
303
314
|
//# sourceMappingURL=index.js.map
|