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.
Files changed (252) hide show
  1. package/README.md +180 -10
  2. package/bin/acebase-server.js +8 -0
  3. package/build-vars.js +2 -1
  4. package/dist/cjs/auth.js +88 -88
  5. package/dist/cjs/cluster.js +6 -56
  6. package/dist/cjs/index.d.ts +1 -1
  7. package/dist/cjs/index.js +24 -24
  8. package/dist/cjs/logger.js +73 -73
  9. package/dist/cjs/middleware/404.js +14 -14
  10. package/dist/cjs/middleware/admin-only.js +28 -28
  11. package/dist/cjs/middleware/cache.js +14 -14
  12. package/dist/cjs/middleware/connection.js +49 -49
  13. package/dist/cjs/middleware/context.js +20 -20
  14. package/dist/cjs/middleware/cors.js +50 -50
  15. package/dist/cjs/middleware/swagger.js +17 -17
  16. package/dist/cjs/middleware/user.js +57 -57
  17. package/dist/cjs/oauth-providers/dropbox.js +109 -109
  18. package/dist/cjs/oauth-providers/facebook.js +85 -85
  19. package/dist/cjs/oauth-providers/google.js +119 -119
  20. package/dist/cjs/oauth-providers/index.js +26 -26
  21. package/dist/cjs/oauth-providers/instagram.js +80 -80
  22. package/dist/cjs/oauth-providers/oauth-provider.js +20 -20
  23. package/dist/cjs/oauth-providers/spotify.js +113 -113
  24. package/dist/cjs/routes/auth-change-password.js +87 -87
  25. package/dist/cjs/routes/auth-delete.js +52 -52
  26. package/dist/cjs/routes/auth-forgot-password.js +77 -77
  27. package/dist/cjs/routes/auth-reset-password.js +97 -97
  28. package/dist/cjs/routes/auth-signin.js +46 -46
  29. package/dist/cjs/routes/auth-signout.js +61 -61
  30. package/dist/cjs/routes/auth-signup.js +142 -142
  31. package/dist/cjs/routes/auth-state.js +25 -25
  32. package/dist/cjs/routes/auth-update.js +131 -131
  33. package/dist/cjs/routes/auth-verify-email.js +71 -71
  34. package/dist/cjs/routes/auth.js +57 -54
  35. package/dist/cjs/routes/data-exists.js +33 -33
  36. package/dist/cjs/routes/data-export.js +48 -48
  37. package/dist/cjs/routes/data-get.js +70 -66
  38. package/dist/cjs/routes/data-import.js +54 -54
  39. package/dist/cjs/routes/data-index-create.js +42 -42
  40. package/dist/cjs/routes/data-index-delete.js +33 -33
  41. package/dist/cjs/routes/data-index-list.js +31 -31
  42. package/dist/cjs/routes/data-query.js +80 -70
  43. package/dist/cjs/routes/data-reflect.js +97 -86
  44. package/dist/cjs/routes/data-schema-get.js +36 -36
  45. package/dist/cjs/routes/data-schema-set.js +30 -30
  46. package/dist/cjs/routes/data-schema-test.js +43 -43
  47. package/dist/cjs/routes/data-schemas-list.js +32 -32
  48. package/dist/cjs/routes/data-set.js +85 -75
  49. package/dist/cjs/routes/data-sync-changes.js +81 -67
  50. package/dist/cjs/routes/data-sync-mutations.js +81 -67
  51. package/dist/cjs/routes/data-transaction.js +159 -139
  52. package/dist/cjs/routes/data-update.js +86 -75
  53. package/dist/cjs/routes/data.js +62 -62
  54. package/dist/cjs/routes/docs.js +258 -251
  55. package/dist/cjs/routes/meta-info.js +63 -63
  56. package/dist/cjs/routes/meta-logs.js +49 -49
  57. package/dist/cjs/routes/meta-ping.js +11 -11
  58. package/dist/cjs/routes/meta-stats.js +27 -27
  59. package/dist/cjs/routes/meta.js +19 -19
  60. package/dist/cjs/routes/oauth2-init.js +54 -54
  61. package/dist/cjs/routes/oauth2-refresh.js +42 -42
  62. package/dist/cjs/routes/oauth2-signin.js +265 -265
  63. package/dist/cjs/routes/webmanager.js +26 -26
  64. package/dist/cjs/rules.js +380 -167
  65. package/dist/cjs/sandbox.js +28 -0
  66. package/dist/cjs/schema/user.js +24 -24
  67. package/dist/cjs/server.js +410 -407
  68. package/dist/cjs/settings/email.js +2 -2
  69. package/dist/cjs/settings/index.js +175 -172
  70. package/dist/cjs/shared/clients.js +26 -26
  71. package/dist/cjs/shared/email.js +2 -2
  72. package/dist/cjs/shared/env.js +2 -2
  73. package/dist/cjs/shared/error.js +33 -33
  74. package/dist/cjs/shared/http.js +28 -28
  75. package/dist/cjs/shared/password.js +45 -45
  76. package/dist/cjs/shared/rootpath.js +16 -16
  77. package/dist/cjs/shared/signin.js +143 -143
  78. package/dist/cjs/shared/simple-fetch.js +76 -76
  79. package/dist/cjs/shared/tokens.js +79 -79
  80. package/dist/cjs/shared/validate.js +61 -61
  81. package/dist/cjs/start.js +119 -53
  82. package/dist/cjs/test.js +10 -10
  83. package/dist/cjs/websocket/index.js +331 -319
  84. package/dist/cjs/websocket/manager.js +15 -15
  85. package/dist/cjs/websocket/socket.io.js +76 -76
  86. package/dist/esm/auth.js +75 -75
  87. package/dist/esm/cluster.js +6 -54
  88. package/dist/esm/index.d.ts +1 -1
  89. package/dist/esm/index.js +4 -4
  90. package/dist/esm/logger.js +51 -51
  91. package/dist/esm/middleware/404.js +10 -10
  92. package/dist/esm/middleware/admin-only.js +24 -24
  93. package/dist/esm/middleware/cache.js +10 -10
  94. package/dist/esm/middleware/connection.js +45 -45
  95. package/dist/esm/middleware/context.js +16 -16
  96. package/dist/esm/middleware/cors.js +44 -44
  97. package/dist/esm/middleware/swagger.js +13 -13
  98. package/dist/esm/middleware/user.js +44 -44
  99. package/dist/esm/oauth-providers/dropbox.js +90 -90
  100. package/dist/esm/oauth-providers/facebook.js +66 -66
  101. package/dist/esm/oauth-providers/google.js +96 -96
  102. package/dist/esm/oauth-providers/index.js +18 -18
  103. package/dist/esm/oauth-providers/instagram.js +61 -61
  104. package/dist/esm/oauth-providers/oauth-provider.js +16 -16
  105. package/dist/esm/oauth-providers/spotify.js +92 -92
  106. package/dist/esm/routes/auth-change-password.js +72 -72
  107. package/dist/esm/routes/auth-delete.js +37 -37
  108. package/dist/esm/routes/auth-forgot-password.js +62 -62
  109. package/dist/esm/routes/auth-reset-password.js +81 -81
  110. package/dist/esm/routes/auth-signin.js +33 -33
  111. package/dist/esm/routes/auth-signout.js +47 -47
  112. package/dist/esm/routes/auth-signup.js +127 -127
  113. package/dist/esm/routes/auth-state.js +12 -12
  114. package/dist/esm/routes/auth-update.js +116 -116
  115. package/dist/esm/routes/auth-verify-email.js +57 -57
  116. package/dist/esm/routes/auth.js +53 -50
  117. package/dist/esm/routes/data-exists.js +20 -20
  118. package/dist/esm/routes/data-export.js +35 -35
  119. package/dist/esm/routes/data-get.js +56 -52
  120. package/dist/esm/routes/data-import.js +41 -41
  121. package/dist/esm/routes/data-index-create.js +28 -28
  122. package/dist/esm/routes/data-index-delete.js +20 -20
  123. package/dist/esm/routes/data-index-list.js +18 -18
  124. package/dist/esm/routes/data-query.js +66 -56
  125. package/dist/esm/routes/data-reflect.js +83 -72
  126. package/dist/esm/routes/data-schema-get.js +23 -23
  127. package/dist/esm/routes/data-schema-set.js +17 -17
  128. package/dist/esm/routes/data-schema-test.js +29 -29
  129. package/dist/esm/routes/data-schemas-list.js +19 -19
  130. package/dist/esm/routes/data-set.js +70 -60
  131. package/dist/esm/routes/data-sync-changes.js +67 -53
  132. package/dist/esm/routes/data-sync-mutations.js +67 -53
  133. package/dist/esm/routes/data-transaction.js +142 -123
  134. package/dist/esm/routes/data-update.js +71 -60
  135. package/dist/esm/routes/data.js +58 -58
  136. package/dist/esm/routes/docs.js +252 -245
  137. package/dist/esm/routes/meta-info.js +59 -59
  138. package/dist/esm/routes/meta-logs.js +35 -35
  139. package/dist/esm/routes/meta-ping.js +7 -7
  140. package/dist/esm/routes/meta-stats.js +14 -14
  141. package/dist/esm/routes/meta.js +15 -15
  142. package/dist/esm/routes/oauth2-init.js +41 -41
  143. package/dist/esm/routes/oauth2-refresh.js +29 -29
  144. package/dist/esm/routes/oauth2-signin.js +251 -251
  145. package/dist/esm/routes/webmanager.js +22 -22
  146. package/dist/esm/rules.js +374 -163
  147. package/dist/esm/sandbox.js +12 -0
  148. package/dist/esm/schema/user.js +19 -19
  149. package/dist/esm/server.js +395 -387
  150. package/dist/esm/settings/email.js +1 -1
  151. package/dist/esm/settings/index.js +168 -165
  152. package/dist/esm/shared/clients.js +22 -22
  153. package/dist/esm/shared/email.js +1 -1
  154. package/dist/esm/shared/env.js +1 -1
  155. package/dist/esm/shared/error.js +25 -25
  156. package/dist/esm/shared/http.js +22 -22
  157. package/dist/esm/shared/password.js +38 -38
  158. package/dist/esm/shared/rootpath.js +13 -13
  159. package/dist/esm/shared/signin.js +128 -128
  160. package/dist/esm/shared/simple-fetch.js +59 -59
  161. package/dist/esm/shared/tokens.js +72 -72
  162. package/dist/esm/shared/validate.js +41 -41
  163. package/dist/esm/start.js +116 -50
  164. package/dist/esm/test.js +8 -8
  165. package/dist/esm/websocket/index.js +313 -302
  166. package/dist/esm/websocket/manager.js +11 -11
  167. package/dist/esm/websocket/socket.io.js +71 -71
  168. package/dist/types/auth.d.ts +3 -3
  169. package/dist/types/cluster.d.ts +6 -1
  170. package/dist/types/index.d.ts +4 -4
  171. package/dist/types/logger.d.ts +35 -35
  172. package/dist/types/middleware/404.d.ts +7 -7
  173. package/dist/types/middleware/admin-only.d.ts +19 -19
  174. package/dist/types/middleware/cache.d.ts +3 -3
  175. package/dist/types/middleware/connection.d.ts +7 -7
  176. package/dist/types/middleware/context.d.ts +3 -3
  177. package/dist/types/middleware/cors.d.ts +25 -25
  178. package/dist/types/middleware/swagger.d.ts +3 -3
  179. package/dist/types/middleware/user.d.ts +2 -2
  180. package/dist/types/oauth-providers/dropbox.d.ts +52 -52
  181. package/dist/types/oauth-providers/facebook.d.ts +41 -41
  182. package/dist/types/oauth-providers/google.d.ts +43 -43
  183. package/dist/types/oauth-providers/index.d.ts +10 -10
  184. package/dist/types/oauth-providers/instagram.d.ts +50 -50
  185. package/dist/types/oauth-providers/oauth-provider.d.ts +74 -74
  186. package/dist/types/oauth-providers/spotify.d.ts +53 -53
  187. package/dist/types/routes/auth-change-password.d.ts +20 -20
  188. package/dist/types/routes/auth-delete.d.ts +16 -16
  189. package/dist/types/routes/auth-forgot-password.d.ts +16 -16
  190. package/dist/types/routes/auth-reset-password.d.ts +23 -23
  191. package/dist/types/routes/auth-signin.d.ts +24 -24
  192. package/dist/types/routes/auth-signout.d.ts +14 -14
  193. package/dist/types/routes/auth-signup.d.ts +32 -32
  194. package/dist/types/routes/auth-state.d.ts +13 -13
  195. package/dist/types/routes/auth-update.d.ts +33 -33
  196. package/dist/types/routes/auth-verify-email.d.ts +21 -21
  197. package/dist/types/routes/auth.d.ts +6 -6
  198. package/dist/types/routes/data-exists.d.ts +9 -9
  199. package/dist/types/routes/data-export.d.ts +10 -10
  200. package/dist/types/routes/data-get.d.ts +14 -14
  201. package/dist/types/routes/data-import.d.ts +13 -13
  202. package/dist/types/routes/data-index-create.d.ts +51 -51
  203. package/dist/types/routes/data-index-delete.d.ts +17 -17
  204. package/dist/types/routes/data-index-list.d.ts +21 -21
  205. package/dist/types/routes/data-query.d.ts +49 -49
  206. package/dist/types/routes/data-reflect.d.ts +29 -29
  207. package/dist/types/routes/data-schema-get.d.ts +11 -11
  208. package/dist/types/routes/data-schema-set.d.ts +20 -19
  209. package/dist/types/routes/data-schema-test.d.ts +19 -19
  210. package/dist/types/routes/data-schemas-list.d.ts +11 -11
  211. package/dist/types/routes/data-set.d.ts +27 -27
  212. package/dist/types/routes/data-sync-changes.d.ts +25 -28
  213. package/dist/types/routes/data-sync-mutations.d.ts +25 -28
  214. package/dist/types/routes/data-transaction.d.ts +34 -34
  215. package/dist/types/routes/data-update.d.ts +27 -27
  216. package/dist/types/routes/data.d.ts +3 -3
  217. package/dist/types/routes/docs.d.ts +3 -3
  218. package/dist/types/routes/meta-info.d.ts +11 -11
  219. package/dist/types/routes/meta-logs.d.ts +20 -20
  220. package/dist/types/routes/meta-ping.d.ts +7 -7
  221. package/dist/types/routes/meta-stats.d.ts +13 -13
  222. package/dist/types/routes/meta.d.ts +3 -3
  223. package/dist/types/routes/oauth2-init.d.ts +13 -13
  224. package/dist/types/routes/oauth2-refresh.d.ts +17 -17
  225. package/dist/types/routes/oauth2-signin.d.ts +15 -15
  226. package/dist/types/routes/webmanager.d.ts +3 -3
  227. package/dist/types/rules.d.ts +73 -34
  228. package/dist/types/sandbox.d.ts +3 -0
  229. package/dist/types/schema/user.d.ts +85 -85
  230. package/dist/types/server.d.ts +117 -111
  231. package/dist/types/settings/email.d.ts +14 -14
  232. package/dist/types/settings/index.d.ts +195 -187
  233. package/dist/types/shared/clients.d.ts +46 -46
  234. package/dist/types/shared/email.d.ts +34 -34
  235. package/dist/types/shared/env.d.ts +47 -44
  236. package/dist/types/shared/error.d.ts +15 -15
  237. package/dist/types/shared/http.d.ts +22 -21
  238. package/dist/types/shared/password.d.ts +19 -19
  239. package/dist/types/shared/rootpath.d.ts +2 -2
  240. package/dist/types/shared/signin.d.ts +31 -31
  241. package/dist/types/shared/simple-fetch.d.ts +21 -21
  242. package/dist/types/shared/tokens.d.ts +24 -24
  243. package/dist/types/shared/validate.d.ts +22 -22
  244. package/dist/types/start.d.ts +1 -1
  245. package/dist/types/test.d.ts +1 -1
  246. package/dist/types/websocket/index.d.ts +6 -6
  247. package/dist/types/websocket/manager.d.ts +99 -99
  248. package/dist/types/websocket/socket.io.d.ts +10 -10
  249. package/package.json +7 -4
  250. package/src/routes/data-schema-set.yaml +4 -0
  251. package/src/routes/data-set.yaml +5 -5
  252. package/src/routes/data-update.yaml +4 -4
@@ -1,24 +1,24 @@
1
- import adminOnly from '../middleware/admin-only.js';
2
- import { sendError } from '../shared/error.js';
3
- export const addRoute = (env) => {
4
- env.app.get(`/schema/${env.db.name}/*`, adminOnly(env), async (req, res) => {
5
- // Get defined schema for a specifc path
6
- try {
7
- const path = req.path.slice(env.db.name.length + 9);
8
- const schema = await env.db.schema.get(path);
9
- if (!schema) {
10
- return res.status(410).send('Not Found');
11
- }
12
- res.contentType('application/json').send({
13
- path: schema.path,
14
- schema: typeof schema.schema === 'string' ? schema.schema : schema.text,
15
- text: schema.text,
16
- });
17
- }
18
- catch (err) {
19
- sendError(res, err);
20
- }
21
- });
22
- };
23
- export default addRoute;
1
+ import adminOnly from '../middleware/admin-only.js';
2
+ import { sendError } from '../shared/error.js';
3
+ export const addRoute = (env) => {
4
+ env.router.get(`/schema/${env.db.name}/*`, adminOnly(env), async (req, res) => {
5
+ // Get defined schema for a specifc path
6
+ try {
7
+ const path = req.path.slice(env.db.name.length + 9);
8
+ const schema = await env.db.schema.get(path);
9
+ if (!schema) {
10
+ return res.status(410).send('Not Found');
11
+ }
12
+ res.contentType('application/json').send({
13
+ path: schema.path,
14
+ schema: typeof schema.schema === 'string' ? schema.schema : schema.text,
15
+ text: schema.text,
16
+ });
17
+ }
18
+ catch (err) {
19
+ sendError(res, err);
20
+ }
21
+ });
22
+ };
23
+ export default addRoute;
24
24
  //# sourceMappingURL=data-schema-get.js.map
@@ -1,18 +1,18 @@
1
- import adminOnly from '../middleware/admin-only.js';
2
- import { sendError } from '../shared/error.js';
3
- export const addRoute = (env) => {
4
- env.app.post(`/schema/${env.db.name}`, adminOnly(env), async (req, res) => {
5
- // defines a schema
6
- try {
7
- const data = req.body;
8
- const { path, schema } = data;
9
- await env.db.schema.set(path, schema);
10
- res.contentType('application/json').send({ success: true });
11
- }
12
- catch (err) {
13
- sendError(res, err);
14
- }
15
- });
16
- };
17
- export default addRoute;
1
+ import adminOnly from '../middleware/admin-only.js';
2
+ import { sendError } from '../shared/error.js';
3
+ export const addRoute = (env) => {
4
+ env.router.post(`/schema/${env.db.name}`, adminOnly(env), async (req, res) => {
5
+ // defines a schema
6
+ try {
7
+ const data = req.body;
8
+ const { path, schema, warnOnly = false } = data;
9
+ await env.db.schema.set(path, schema, warnOnly);
10
+ res.contentType('application/json').send({ success: true });
11
+ }
12
+ catch (err) {
13
+ sendError(res, err);
14
+ }
15
+ });
16
+ };
17
+ export default addRoute;
18
18
  //# sourceMappingURL=data-schema-set.js.map
@@ -1,30 +1,30 @@
1
- import { SchemaDefinition, Transport } from 'acebase-core';
2
- import adminOnly from '../middleware/admin-only.js';
3
- import { sendError } from '../shared/error.js';
4
- export const addRoute = (env) => {
5
- env.app.post(`/schema/${env.db.name}/test`, adminOnly(env), async (req, res) => {
6
- // tests a schema
7
- try {
8
- const data = req.body;
9
- if (typeof data.value?.val === 'undefined' || !['string', 'object', 'undefined'].includes(typeof data.value?.map)) {
10
- return sendError(res, { code: 'invalid_serialized_value', message: 'The sent value is not properly serialized' });
11
- }
12
- const value = Transport.deserialize(data.value);
13
- const { path, schema, partial } = data;
14
- let result;
15
- if (schema) {
16
- const definition = new SchemaDefinition(schema);
17
- result = definition.check(path, value, partial);
18
- }
19
- else {
20
- result = await env.db.schema.check(path, schema, partial);
21
- }
22
- res.contentType('application/json').send(result);
23
- }
24
- catch (err) {
25
- sendError(res, err);
26
- }
27
- });
28
- };
29
- export default addRoute;
1
+ import { SchemaDefinition, Transport } from 'acebase-core';
2
+ import adminOnly from '../middleware/admin-only.js';
3
+ import { sendError } from '../shared/error.js';
4
+ export const addRoute = (env) => {
5
+ env.router.post(`/schema/${env.db.name}/test`, adminOnly(env), async (req, res) => {
6
+ // tests a schema
7
+ try {
8
+ const data = req.body;
9
+ if (typeof data.value?.val === 'undefined' || !['string', 'object', 'undefined'].includes(typeof data.value?.map)) {
10
+ return sendError(res, { code: 'invalid_serialized_value', message: 'The sent value is not properly serialized' });
11
+ }
12
+ const value = Transport.deserialize(data.value);
13
+ const { path, schema, partial } = data;
14
+ let result;
15
+ if (schema) {
16
+ const definition = new SchemaDefinition(schema);
17
+ result = definition.check(path, value, partial);
18
+ }
19
+ else {
20
+ result = await env.db.schema.check(path, schema, partial);
21
+ }
22
+ res.contentType('application/json').send(result);
23
+ }
24
+ catch (err) {
25
+ sendError(res, err);
26
+ }
27
+ });
28
+ };
29
+ export default addRoute;
30
30
  //# sourceMappingURL=data-schema-test.js.map
@@ -1,20 +1,20 @@
1
- import adminOnly from '../middleware/admin-only.js';
2
- import { sendError } from '../shared/error.js';
3
- export const addRoute = (env) => {
4
- env.app.get(`/schema/${env.db.name}`, adminOnly(env), async (req, res) => {
5
- // Get all defined schemas
6
- try {
7
- const schemas = await env.db.schema.all();
8
- res.contentType('application/json').send(schemas.map(schema => ({
9
- path: schema.path,
10
- schema: typeof schema.schema === 'string' ? schema.schema : schema.text,
11
- text: schema.text,
12
- })));
13
- }
14
- catch (err) {
15
- sendError(res, err);
16
- }
17
- });
18
- };
19
- export default addRoute;
1
+ import adminOnly from '../middleware/admin-only.js';
2
+ import { sendError } from '../shared/error.js';
3
+ export const addRoute = (env) => {
4
+ env.router.get(`/schema/${env.db.name}`, adminOnly(env), async (req, res) => {
5
+ // Get all defined schemas
6
+ try {
7
+ const schemas = await env.db.schema.all();
8
+ res.contentType('application/json').send(schemas.map(schema => ({
9
+ path: schema.path,
10
+ schema: typeof schema.schema === 'string' ? schema.schema : schema.text,
11
+ text: schema.text,
12
+ })));
13
+ }
14
+ catch (err) {
15
+ sendError(res, err);
16
+ }
17
+ });
18
+ };
19
+ export default addRoute;
20
20
  //# sourceMappingURL=data-schemas-list.js.map
@@ -1,61 +1,71 @@
1
- import { SchemaValidationError } from 'acebase';
2
- import { Transport } from 'acebase-core';
3
- import { sendBadRequestError, sendError, sendUnauthorizedError } from '../shared/error.js';
4
- export class SetDataError extends Error {
5
- constructor(code, message) {
6
- super(message);
7
- this.code = code;
8
- }
9
- }
10
- export const addRoute = (env) => {
11
- env.app.put(`/data/${env.db.name}/*`, async (req, res) => {
12
- const path = req.path.slice(env.db.name.length + 7);
13
- const LOG_ACTION = 'data.set';
14
- const LOG_DETAILS = { ip: req.ip, uid: req.user?.uid ?? null, path };
15
- const access = env.rules.userHasAccess(req.user, path, true);
16
- if (!access.allow) {
17
- env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null, rule_error: access.details?.message ?? null });
18
- return sendUnauthorizedError(res, access.code, access.message);
19
- }
20
- try {
21
- const data = req.body;
22
- if (typeof data?.val === 'undefined' || !['string', 'object', 'undefined'].includes(typeof data?.map)) {
23
- throw new SetDataError('invalid_serialized_value', 'The sent value is not properly serialized');
24
- }
25
- const val = Transport.deserialize(data);
26
- if (path === '' && req.user?.uid !== 'admin' && val !== null && typeof val === 'object') {
27
- // Non-admin user: remove any private properties from the update object
28
- Object.keys(val).filter(key => key.startsWith('__')).forEach(key => delete val[key]);
29
- }
30
- // Schema validation moved to storage, no need to check here but an early check won't do no harm!
31
- const validation = await env.db.schema.check(path, val, false);
32
- if (!validation.ok) {
33
- throw new SchemaValidationError(validation.reason);
34
- }
35
- await env.db.ref(path)
36
- .context(req.context)
37
- .set(val);
38
- // NEW: add cursor to response context, which was added to the request context in `acebase_cursor` if transaction logging is enabled
39
- const returnContext = { acebase_cursor: req.context.acebase_cursor };
40
- res.setHeader('AceBase-Context', JSON.stringify(returnContext));
41
- res.send({ success: true });
42
- }
43
- catch (err) {
44
- if (err instanceof SchemaValidationError) {
45
- env.log.error(LOG_ACTION, 'schema_validation_failed', { ...LOG_DETAILS, reason: err.reason });
46
- res.status(422).send({ code: 'schema_validation_failed', message: err.message });
47
- }
48
- else if (err instanceof SetDataError) {
49
- env.log.error(LOG_ACTION, err.code, { ...LOG_DETAILS, message: err.message });
50
- sendBadRequestError(res, err);
51
- }
52
- else {
53
- env.debug.error(`failed to set "${path}":`, err);
54
- env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
55
- sendError(res, err);
56
- }
57
- }
58
- });
59
- };
60
- export default addRoute;
1
+ import { SchemaValidationError } from 'acebase';
2
+ import { Transport } from 'acebase-core';
3
+ import { AccessRuleValidationError } from '../rules.js';
4
+ import { sendBadRequestError, sendError, sendUnauthorizedError } from '../shared/error.js';
5
+ export class SetDataError extends Error {
6
+ constructor(code, message) {
7
+ super(message);
8
+ this.code = code;
9
+ }
10
+ }
11
+ export const addRoute = (env) => {
12
+ env.router.put(`/data/${env.db.name}/*`, async (req, res) => {
13
+ const path = req.path.slice(env.db.name.length + 7);
14
+ const LOG_ACTION = 'data.set';
15
+ const LOG_DETAILS = { ip: req.ip, uid: req.user?.uid ?? null, path };
16
+ try {
17
+ // Pre-check 'write' access
18
+ let access = await env.rules.isOperationAllowed(req.user, path, 'set');
19
+ if (!access.allow) {
20
+ throw new AccessRuleValidationError(access);
21
+ }
22
+ const data = req.body;
23
+ if (typeof data?.val === 'undefined' || !['string', 'object', 'undefined'].includes(typeof data?.map)) {
24
+ throw new SetDataError('invalid_serialized_value', 'The sent value is not properly serialized');
25
+ }
26
+ const val = Transport.deserialize(data);
27
+ if (path === '' && req.user?.uid !== 'admin' && val !== null && typeof val === 'object') {
28
+ // Non-admin user: remove any private properties from the update object
29
+ Object.keys(val).filter(key => key.startsWith('__')).forEach(key => delete val[key]);
30
+ }
31
+ access = await env.rules.isOperationAllowed(req.user, path, 'set', { value: val, context: req.context });
32
+ if (!access.allow) {
33
+ throw new AccessRuleValidationError(access);
34
+ }
35
+ // Schema validation moved to storage, no need to check here but an early check won't do no harm!
36
+ const validation = await env.db.schema.check(path, val, false);
37
+ if (!validation.ok) {
38
+ throw new SchemaValidationError(validation.reason);
39
+ }
40
+ await env.db.ref(path)
41
+ .context(req.context)
42
+ .set(val);
43
+ // NEW: add cursor to response context, which was added to the request context in `acebase_cursor` if transaction logging is enabled
44
+ const returnContext = { acebase_cursor: req.context.acebase_cursor };
45
+ res.setHeader('AceBase-Context', JSON.stringify(returnContext));
46
+ res.send({ success: true });
47
+ }
48
+ catch (err) {
49
+ if (err instanceof AccessRuleValidationError) {
50
+ const access = err.result;
51
+ env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null, rule_error: access.details?.message ?? null });
52
+ return sendUnauthorizedError(res, access.code, access.message);
53
+ }
54
+ else if (err instanceof SchemaValidationError) {
55
+ env.log.error(LOG_ACTION, 'schema_validation_failed', { ...LOG_DETAILS, reason: err.reason });
56
+ res.status(422).send({ code: 'schema_validation_failed', message: err.message });
57
+ }
58
+ else if (err instanceof SetDataError) {
59
+ env.log.error(LOG_ACTION, err.code, { ...LOG_DETAILS, message: err.message });
60
+ sendBadRequestError(res, err);
61
+ }
62
+ else {
63
+ env.debug.error(`failed to set "${path}":`, err);
64
+ env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
65
+ sendError(res, err);
66
+ }
67
+ }
68
+ });
69
+ };
70
+ export default addRoute;
61
71
  //# sourceMappingURL=data-set.js.map
@@ -1,54 +1,68 @@
1
- import { PathInfo, Transport } from 'acebase-core';
2
- import { sendBadRequestError, sendError, sendUnauthorizedError } from '../shared/error.js';
3
- export const addRoute = (env) => {
4
- env.app.get(`/sync/changes/${env.db.name}`, async (req, res) => {
5
- // Gets effective changes for specific path(s) and event combinations since given cursor
6
- if (!env.config.transactions?.log) {
7
- return sendBadRequestError(res, { code: 'no_transaction_logging', message: 'Transaction logging not enabled' });
8
- }
9
- try {
10
- const data = req.query;
11
- let targets = typeof data.path === 'string'
12
- ? [{ path: data.path, events: ['value'] }]
13
- : typeof data.for === 'string'
14
- ? JSON.parse(data.for)
15
- : null;
16
- if (targets === null) {
17
- return sendBadRequestError(res, { code: 'invalid_request', message: 'Invalid changes request' });
18
- }
19
- if (targets.length === 0) {
20
- targets.push({ path: '', events: ['value'] });
21
- }
22
- // Filter out any requested paths user does not have access to.
23
- targets = targets.filter(target => {
24
- let path = target.path;
25
- if (target.events.every(event => /^(?:notify_)?child_/.test(event))) { //if (!target.events.some(event => ['value','notify_value','mutations','mutated'].includes(event))) {
26
- // Only child_ events, check if they have access to children instead
27
- path = PathInfo.get(path).childPath('*');
28
- }
29
- return env.rules.userHasAccess(req.user, path, false).allow;
30
- });
31
- if (targets.length === 0) {
32
- return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
33
- }
34
- const { cursor } = data;
35
- let timestamp;
36
- if (typeof data.timestamp !== 'undefined') {
37
- timestamp = parseInt(data.timestamp);
38
- if (isNaN(timestamp)) {
39
- return sendBadRequestError(res, { code: 'wrong_timestamp', message: 'Timestamp is not a number' });
40
- }
41
- }
42
- const result = await env.db.api.getChanges({ for: targets, cursor, timestamp });
43
- res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
44
- res.contentType('application/json');
45
- const serialized = Transport.serialize2(result.changes);
46
- res.send(serialized);
47
- }
48
- catch (err) {
49
- sendError(res, err);
50
- }
51
- });
52
- };
53
- export default addRoute;
1
+ import { PathInfo, Transport } from 'acebase-core';
2
+ import { sendBadRequestError, sendError, sendUnauthorizedError } from '../shared/error.js';
3
+ export const addRoute = (env) => {
4
+ env.router.get(`/sync/changes/${env.db.name}`, async (req, res) => {
5
+ // Gets effective changes for specific path(s) and event combinations since given cursor
6
+ if (!env.config.transactions?.log) {
7
+ return sendBadRequestError(res, { code: 'no_transaction_logging', message: 'Transaction logging not enabled' });
8
+ }
9
+ try {
10
+ const data = req.query;
11
+ const targets = typeof data.path === 'string'
12
+ ? [{ path: data.path, events: ['value'] }]
13
+ : typeof data.for === 'string'
14
+ ? JSON.parse(data.for)
15
+ : null;
16
+ if (targets === null) {
17
+ return sendBadRequestError(res, { code: 'invalid_request', message: 'Invalid changes request' });
18
+ }
19
+ if (targets.length === 0) {
20
+ targets.push({ path: '', events: ['value'] });
21
+ }
22
+ // Filter out any requested paths user does not have access to.
23
+ const accessCache = new Map();
24
+ for (let i = 0; i < targets.length; i++) {
25
+ const target = targets[i];
26
+ let path = target.path;
27
+ if (target.events.every(event => /^(?:notify_)?child_/.test(event))) { //if (!target.events.some(event => ['value','notify_value','mutations','mutated'].includes(event))) {
28
+ // Only child_ events, check if they have access to children instead
29
+ path = PathInfo.get(path).childPath('*');
30
+ }
31
+ let allow = false;
32
+ if (accessCache.has(path)) {
33
+ allow = accessCache.get(path);
34
+ }
35
+ else {
36
+ const access = await env.rules.isOperationAllowed(req.user, path, 'get');
37
+ allow = access.allow;
38
+ accessCache.set(path, allow);
39
+ }
40
+ if (!allow) {
41
+ targets.splice(i, 1);
42
+ i--;
43
+ }
44
+ }
45
+ if (targets.length === 0) {
46
+ return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
47
+ }
48
+ const { cursor } = data;
49
+ let timestamp;
50
+ if (typeof data.timestamp !== 'undefined') {
51
+ timestamp = parseInt(data.timestamp);
52
+ if (isNaN(timestamp)) {
53
+ return sendBadRequestError(res, { code: 'wrong_timestamp', message: 'Timestamp is not a number' });
54
+ }
55
+ }
56
+ const result = await env.db.api.getChanges({ for: targets, cursor, timestamp });
57
+ res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
58
+ res.contentType('application/json');
59
+ const serialized = Transport.serialize2(result.changes);
60
+ res.send(serialized);
61
+ }
62
+ catch (err) {
63
+ sendError(res, err);
64
+ }
65
+ });
66
+ };
67
+ export default addRoute;
54
68
  //# sourceMappingURL=data-sync-changes.js.map
@@ -1,54 +1,68 @@
1
- import { PathInfo, Transport } from 'acebase-core';
2
- import { sendBadRequestError, sendError, sendUnauthorizedError } from '../shared/error.js';
3
- export const addRoute = (env) => {
4
- env.app.get(`/sync/mutations/${env.db.name}`, async (req, res) => {
5
- // Gets mutations for specific path(s) and event combinations since given cursor
6
- if (!env.config.transactions?.log) {
7
- return sendBadRequestError(res, { code: 'no_transaction_logging', message: 'Transaction logging not enabled' });
8
- }
9
- try {
10
- const data = req.query;
11
- let targets = typeof data.path === 'string'
12
- ? [{ path: data.path, events: ['value'] }]
13
- : typeof data.for === 'string'
14
- ? JSON.parse(data.for)
15
- : null;
16
- if (targets === null) {
17
- return sendBadRequestError(res, { code: 'invalid_request', message: 'Invalid mutations request' });
18
- }
19
- if (targets.length === 0) {
20
- targets.push({ path: '', events: ['value'] });
21
- }
22
- // Filter out any requested paths user does not have access to.
23
- targets = targets.filter(target => {
24
- let path = target.path;
25
- if (target.events.every(event => /^(?:notify_)?child_/.test(event))) { //if (!target.events.some(event => ['value','notify_value','mutations','mutated'].includes(event))) {
26
- // Only child_ events, check if they have access to children instead
27
- path = PathInfo.get(path).childPath('*');
28
- }
29
- return env.rules.userHasAccess(req.user, path, false).allow;
30
- });
31
- if (targets.length === 0) {
32
- return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
33
- }
34
- const { cursor } = data;
35
- let timestamp;
36
- if (typeof data.timestamp !== 'undefined') {
37
- timestamp = parseInt(data.timestamp);
38
- if (isNaN(timestamp)) {
39
- return sendBadRequestError(res, { code: 'wrong_timestamp', message: 'Timestamp is not a number' });
40
- }
41
- }
42
- const result = await env.db.api.getMutations({ for: targets, cursor, timestamp });
43
- res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
44
- res.contentType('application/json');
45
- const serialized = Transport.serialize2(result.mutations);
46
- res.send(serialized);
47
- }
48
- catch (err) {
49
- sendError(res, err);
50
- }
51
- });
52
- };
53
- export default addRoute;
1
+ import { PathInfo, Transport } from 'acebase-core';
2
+ import { sendBadRequestError, sendError, sendUnauthorizedError } from '../shared/error.js';
3
+ export const addRoute = (env) => {
4
+ env.router.get(`/sync/mutations/${env.db.name}`, async (req, res) => {
5
+ // Gets mutations for specific path(s) and event combinations since given cursor
6
+ if (!env.config.transactions?.log) {
7
+ return sendBadRequestError(res, { code: 'no_transaction_logging', message: 'Transaction logging not enabled' });
8
+ }
9
+ try {
10
+ const data = req.query;
11
+ const targets = typeof data.path === 'string'
12
+ ? [{ path: data.path, events: ['value'] }]
13
+ : typeof data.for === 'string'
14
+ ? JSON.parse(data.for)
15
+ : null;
16
+ if (targets === null) {
17
+ return sendBadRequestError(res, { code: 'invalid_request', message: 'Invalid mutations request' });
18
+ }
19
+ if (targets.length === 0) {
20
+ targets.push({ path: '', events: ['value'] });
21
+ }
22
+ // Filter out any requested paths user does not have access to.
23
+ const accessCache = new Map();
24
+ for (let i = 0; i < targets.length; i++) {
25
+ const target = targets[i];
26
+ let path = target.path;
27
+ if (target.events.every(event => /^(?:notify_)?child_/.test(event))) {
28
+ // Only child_ events, check if they have access to children instead
29
+ path = PathInfo.get(path).childPath('*');
30
+ }
31
+ let allow = false;
32
+ if (accessCache.has(path)) {
33
+ allow = accessCache.get(path);
34
+ }
35
+ else {
36
+ const access = await env.rules.isOperationAllowed(req.user, path, 'get');
37
+ allow = access.allow;
38
+ accessCache.set(path, allow);
39
+ }
40
+ if (!allow) {
41
+ targets.splice(i, 1);
42
+ i--;
43
+ }
44
+ }
45
+ if (targets.length === 0) {
46
+ return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
47
+ }
48
+ const { cursor } = data;
49
+ let timestamp;
50
+ if (typeof data.timestamp !== 'undefined') {
51
+ timestamp = parseInt(data.timestamp);
52
+ if (isNaN(timestamp)) {
53
+ return sendBadRequestError(res, { code: 'wrong_timestamp', message: 'Timestamp is not a number' });
54
+ }
55
+ }
56
+ const result = await env.db.api.getMutations({ for: targets, cursor, timestamp });
57
+ res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
58
+ res.contentType('application/json');
59
+ const serialized = Transport.serialize2(result.mutations);
60
+ res.send(serialized);
61
+ }
62
+ catch (err) {
63
+ sendError(res, err);
64
+ }
65
+ });
66
+ };
67
+ export default addRoute;
54
68
  //# sourceMappingURL=data-sync-mutations.js.map