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