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,124 +1,143 @@
|
|
|
1
|
-
import { SchemaValidationError } from 'acebase';
|
|
2
|
-
import { ID, Transport } from 'acebase-core';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
if (!
|
|
76
|
-
env.log.error(LOG_ACTION, '
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
//
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
tx.
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
1
|
+
import { SchemaValidationError } from 'acebase';
|
|
2
|
+
import { ID, Transport } from 'acebase-core';
|
|
3
|
+
import { AccessRuleValidationError } from '../rules.js';
|
|
4
|
+
import { sendBadRequestError, sendError, sendUnauthorizedError, sendUnexpectedError } from '../shared/error.js';
|
|
5
|
+
export const TRANSACTION_TIMEOUT_MS = 10000; // 10s to finish a started transaction
|
|
6
|
+
export class DataTransactionError extends Error {
|
|
7
|
+
constructor(code, message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.code = code;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export const addRoutes = (env) => {
|
|
13
|
+
const _transactions = new Map();
|
|
14
|
+
// Start transaction endpoint:
|
|
15
|
+
env.router.post(`/transaction/${env.db.name}/start`, async (req, res) => {
|
|
16
|
+
const data = req.body;
|
|
17
|
+
const LOG_ACTION = 'data.transaction.start';
|
|
18
|
+
const LOG_DETAILS = { ip: req.ip, uid: req.user?.uid ?? null, path: data.path };
|
|
19
|
+
// Pre-check read/write access
|
|
20
|
+
const access = await env.rules.isOperationAllowed(req.user, data.path, 'transact');
|
|
21
|
+
if (!access.allow) {
|
|
22
|
+
env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null }, access.details);
|
|
23
|
+
return sendUnauthorizedError(res, access.code, access.message);
|
|
24
|
+
}
|
|
25
|
+
// Start transaction
|
|
26
|
+
const tx = {
|
|
27
|
+
id: ID.generate(),
|
|
28
|
+
started: Date.now(),
|
|
29
|
+
path: data.path,
|
|
30
|
+
context: req.context,
|
|
31
|
+
finish: undefined,
|
|
32
|
+
timeout: setTimeout(() => {
|
|
33
|
+
_transactions.delete(tx.id);
|
|
34
|
+
tx.finish(); // Finish without value cancels the transaction
|
|
35
|
+
}, TRANSACTION_TIMEOUT_MS),
|
|
36
|
+
};
|
|
37
|
+
_transactions.set(tx.id, tx);
|
|
38
|
+
try {
|
|
39
|
+
env.debug.verbose(`Transaction ${tx.id} starting...`);
|
|
40
|
+
// const ref = db.ref(tx.path);
|
|
41
|
+
const donePromise = env.db.api.transaction(tx.path, async (val) => {
|
|
42
|
+
env.debug.verbose(`Transaction ${tx.id} started with value: `, val);
|
|
43
|
+
const access = await env.rules.isOperationAllowed(req.user, data.path, 'get', { value: val, context: req.context });
|
|
44
|
+
if (!access.allow) {
|
|
45
|
+
env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null }, access.details);
|
|
46
|
+
sendUnauthorizedError(res, access.code, access.message);
|
|
47
|
+
return; // Return undefined to cancel transaction
|
|
48
|
+
}
|
|
49
|
+
const currentValue = Transport.serialize(val);
|
|
50
|
+
const promise = new Promise((resolve) => {
|
|
51
|
+
tx.finish = (val) => {
|
|
52
|
+
env.debug.verbose(`Transaction ${tx.id} finishing with value: `, val);
|
|
53
|
+
_transactions.delete(tx.id);
|
|
54
|
+
resolve(val);
|
|
55
|
+
return donePromise;
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
res.send({ id: tx.id, value: currentValue });
|
|
59
|
+
return promise;
|
|
60
|
+
}, { context: tx.context });
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
await tx?.finish(); // Finish without value to cancel the transaction
|
|
64
|
+
env.debug.error(`failed to start transaction on "${tx.path}":`, err);
|
|
65
|
+
env.log.error(LOG_ACTION, err.code ?? 'unexpected', LOG_DETAILS, typeof err.code === 'undefined' ? err : null);
|
|
66
|
+
sendUnexpectedError(res, err);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
// Finish transaction endpoint:
|
|
70
|
+
env.router.post(`/transaction/${env.db.name}/finish`, async (req, res) => {
|
|
71
|
+
const data = req.body;
|
|
72
|
+
const LOG_ACTION = 'data.transaction.finish';
|
|
73
|
+
const LOG_DETAILS = { ip: req.ip, uid: req.user?.uid ?? null, path: data.path };
|
|
74
|
+
const tx = _transactions.get(data.id);
|
|
75
|
+
if (!tx || tx.path !== data.path) {
|
|
76
|
+
env.log.error(LOG_ACTION, tx ? 'wrong_path' : 'not_found', { ...LOG_DETAILS, id: data.id, tx_path: tx?.path ?? null });
|
|
77
|
+
res.statusCode = 410; // Gone
|
|
78
|
+
res.send(`transaction not found`);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
clearTimeout(tx.timeout);
|
|
82
|
+
_transactions.delete(tx.id);
|
|
83
|
+
// Finish transaction
|
|
84
|
+
try {
|
|
85
|
+
// Check again if a 'write' to this path is allowed by this user
|
|
86
|
+
let access = await env.rules.isOperationAllowed(req.user, tx.path, 'set');
|
|
87
|
+
if (!access.allow) {
|
|
88
|
+
throw new AccessRuleValidationError(access);
|
|
89
|
+
}
|
|
90
|
+
let cancel = false;
|
|
91
|
+
if (typeof data.value === 'object' && (data.value === null || Object.keys(data.value).length === 0)) {
|
|
92
|
+
// Returning undefined from a transaction callback should cancel the transaction
|
|
93
|
+
// acebase-client (Transport.serialize) serializes value undefined as { val: undefined, map: undefined }, which
|
|
94
|
+
// then is sent to the server as an empty object: {}
|
|
95
|
+
cancel = true;
|
|
96
|
+
}
|
|
97
|
+
else if (typeof data.value?.val === 'undefined' || !['string', 'object', 'undefined'].includes(typeof data.value?.map)) {
|
|
98
|
+
throw new DataTransactionError('invalid_serialized_value', 'The sent value is not properly serialized');
|
|
99
|
+
}
|
|
100
|
+
const newValue = cancel ? undefined : Transport.deserialize(data.value);
|
|
101
|
+
if (tx.path === '' && req.user?.uid !== 'admin' && newValue !== null && typeof newValue === 'object') {
|
|
102
|
+
// Non-admin user: remove any private properties from the update object
|
|
103
|
+
Object.keys(newValue).filter(key => key.startsWith('__')).forEach(key => delete newValue[key]);
|
|
104
|
+
}
|
|
105
|
+
// Check if the value to be written is allowed
|
|
106
|
+
access = await env.rules.isOperationAllowed(req.user, tx.path, 'set', { value: newValue, context: tx.context });
|
|
107
|
+
if (!access.allow) {
|
|
108
|
+
throw new AccessRuleValidationError(access);
|
|
109
|
+
}
|
|
110
|
+
const result = await tx.finish(newValue);
|
|
111
|
+
// NEW: capture cursor and return it in the response context header
|
|
112
|
+
if (!tx.context) {
|
|
113
|
+
tx.context = {};
|
|
114
|
+
}
|
|
115
|
+
tx.context.acebase_cursor = result.cursor;
|
|
116
|
+
res.setHeader('AceBase-Context', JSON.stringify(tx.context));
|
|
117
|
+
res.send('done');
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
tx.finish(); // Finish without value cancels the transaction
|
|
121
|
+
if (err instanceof AccessRuleValidationError) {
|
|
122
|
+
const access = err.result;
|
|
123
|
+
env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null }, access.details);
|
|
124
|
+
return sendUnauthorizedError(res, access.code, access.message);
|
|
125
|
+
}
|
|
126
|
+
else if (err instanceof SchemaValidationError) {
|
|
127
|
+
env.log.error(LOG_ACTION, 'schema_validation_failed', { ...LOG_DETAILS, reason: err.reason });
|
|
128
|
+
res.status(422).send({ code: 'schema_validation_failed', message: err.message });
|
|
129
|
+
}
|
|
130
|
+
else if (err instanceof DataTransactionError) {
|
|
131
|
+
env.log.error(LOG_ACTION, err.code, { ...LOG_DETAILS, message: err.message });
|
|
132
|
+
sendBadRequestError(res, err);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
env.debug.error(`failed to finish transaction on "${tx.path}":`, err);
|
|
136
|
+
env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
|
|
137
|
+
sendError(res, err);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
export default addRoutes;
|
|
124
143
|
//# sourceMappingURL=data-transaction.js.map
|
|
@@ -1,61 +1,72 @@
|
|
|
1
|
-
import { SchemaValidationError } from 'acebase';
|
|
2
|
-
import { Transport } from 'acebase-core';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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 UpdateDataError extends Error {
|
|
6
|
+
constructor(code, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.code = code;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export const addRoute = (env) => {
|
|
12
|
+
env.router.post(`/data/${env.db.name}/*`, async (req, res) => {
|
|
13
|
+
const path = req.path.slice(env.db.name.length + 7);
|
|
14
|
+
const LOG_ACTION = 'data.update';
|
|
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, 'update');
|
|
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 UpdateDataError('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
|
+
// Check 'update' access
|
|
32
|
+
access = await env.rules.isOperationAllowed(req.user, path, 'update', { value: val, context: req.context });
|
|
33
|
+
if (!access.allow) {
|
|
34
|
+
throw new AccessRuleValidationError(access);
|
|
35
|
+
}
|
|
36
|
+
// Schema validation moved to storage, no need to check here but an early check won't do no harm!
|
|
37
|
+
const validation = await env.db.schema.check(path, val, true);
|
|
38
|
+
if (!validation.ok) {
|
|
39
|
+
throw new SchemaValidationError(validation.reason);
|
|
40
|
+
}
|
|
41
|
+
await env.db.ref(path)
|
|
42
|
+
.context(req.context)
|
|
43
|
+
.update(val);
|
|
44
|
+
// NEW: add cursor to response context, which was added to the request context in `acebase_cursor` if transaction logging is enabled
|
|
45
|
+
const returnContext = { acebase_cursor: req.context.acebase_cursor };
|
|
46
|
+
res.setHeader('AceBase-Context', JSON.stringify(returnContext));
|
|
47
|
+
res.send({ success: true });
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
if (err instanceof AccessRuleValidationError) {
|
|
51
|
+
const access = err.result;
|
|
52
|
+
env.log.error(LOG_ACTION, 'unauthorized', { ...LOG_DETAILS, rule_code: access.code, rule_path: access.rulePath ?? null, rule_error: access.details?.message ?? null });
|
|
53
|
+
return sendUnauthorizedError(res, access.code, access.message);
|
|
54
|
+
}
|
|
55
|
+
if (err instanceof SchemaValidationError) {
|
|
56
|
+
env.log.error(LOG_ACTION, 'schema_validation_failed', { ...LOG_DETAILS, reason: err.reason });
|
|
57
|
+
res.status(422).send({ code: 'schema_validation_failed', message: err.message });
|
|
58
|
+
}
|
|
59
|
+
else if (err instanceof UpdateDataError) {
|
|
60
|
+
env.log.error(LOG_ACTION, err.code, { ...LOG_DETAILS, message: err.message });
|
|
61
|
+
sendBadRequestError(res, err);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
env.debug.error(`failed to update "${path}":`, err);
|
|
65
|
+
env.log.error(LOG_ACTION, 'unexpected', LOG_DETAILS, err);
|
|
66
|
+
sendError(res, err);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
export default addRoute;
|
|
61
72
|
//# sourceMappingURL=data-update.js.map
|
package/dist/esm/routes/data.js
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import addContextMiddleware from '../middleware/context.js';
|
|
2
|
-
import addGetDataRoute from './data-get.js';
|
|
3
|
-
import addUpdateDataRoute from './data-update.js';
|
|
4
|
-
import addSetDataRoute from './data-set.js';
|
|
5
|
-
import addTransactionRoutes from './data-transaction.js';
|
|
6
|
-
import addExistsRoute from './data-exists.js';
|
|
7
|
-
import addReflectRoute from './data-reflect.js';
|
|
8
|
-
import addImportDataRoute from './data-import.js';
|
|
9
|
-
import addExportDataRoute from './data-export.js';
|
|
10
|
-
import addQueryRoute from './data-query.js';
|
|
11
|
-
// Indexes:
|
|
12
|
-
import addListIndexesRoute from './data-index-list.js';
|
|
13
|
-
import addCreateIndexRoute from './data-index-create.js';
|
|
14
|
-
import addDeleteIndexRoute from './data-index-delete.js';
|
|
15
|
-
// Schemas:
|
|
16
|
-
import addListSchemasRoute from './data-schemas-list.js';
|
|
17
|
-
import addGetSchemaRoute from './data-schema-get.js';
|
|
18
|
-
import addSetSchemaRoute from './data-schema-set.js';
|
|
19
|
-
import addTestSchemaRoute from './data-schema-test.js';
|
|
20
|
-
// Sycnronization:
|
|
21
|
-
import addSyncMutationsRoute from './data-sync-mutations.js';
|
|
22
|
-
import addSyncChangesRoute from './data-sync-changes.js';
|
|
23
|
-
export const addRoutes = (env) => {
|
|
24
|
-
// Add context middleware that handles AceBase-Context header
|
|
25
|
-
addContextMiddleware(env);
|
|
26
|
-
// Add get data endpoint
|
|
27
|
-
addGetDataRoute(env);
|
|
28
|
-
// Add update data endpoint
|
|
29
|
-
addUpdateDataRoute(env);
|
|
30
|
-
// Add set data endpoint
|
|
31
|
-
addSetDataRoute(env);
|
|
32
|
-
// add transaction routes (start & finish)
|
|
33
|
-
addTransactionRoutes(env);
|
|
34
|
-
// Add exists endpoint
|
|
35
|
-
addExistsRoute(env);
|
|
36
|
-
// Add reflect endpoint
|
|
37
|
-
addReflectRoute(env);
|
|
38
|
-
// Add import endpoint
|
|
39
|
-
addImportDataRoute(env);
|
|
40
|
-
// Add export endpoint
|
|
41
|
-
addExportDataRoute(env);
|
|
42
|
-
// Add query endpoint
|
|
43
|
-
addQueryRoute(env);
|
|
44
|
-
// Add index endpoints:
|
|
45
|
-
addListIndexesRoute(env); // list indexes
|
|
46
|
-
addCreateIndexRoute(env); // create index
|
|
47
|
-
addDeleteIndexRoute(env); // delete index
|
|
48
|
-
// Add schema endpoints:
|
|
49
|
-
addListSchemasRoute(env); // list all
|
|
50
|
-
addGetSchemaRoute(env); // get schema
|
|
51
|
-
addSetSchemaRoute(env); // set schema
|
|
52
|
-
addTestSchemaRoute(env); // test
|
|
53
|
-
// Add sync mutations endpoint
|
|
54
|
-
addSyncMutationsRoute(env);
|
|
55
|
-
// add sync changes endpoint
|
|
56
|
-
addSyncChangesRoute(env);
|
|
57
|
-
};
|
|
58
|
-
export default addRoutes;
|
|
1
|
+
import addContextMiddleware from '../middleware/context.js';
|
|
2
|
+
import addGetDataRoute from './data-get.js';
|
|
3
|
+
import addUpdateDataRoute from './data-update.js';
|
|
4
|
+
import addSetDataRoute from './data-set.js';
|
|
5
|
+
import addTransactionRoutes from './data-transaction.js';
|
|
6
|
+
import addExistsRoute from './data-exists.js';
|
|
7
|
+
import addReflectRoute from './data-reflect.js';
|
|
8
|
+
import addImportDataRoute from './data-import.js';
|
|
9
|
+
import addExportDataRoute from './data-export.js';
|
|
10
|
+
import addQueryRoute from './data-query.js';
|
|
11
|
+
// Indexes:
|
|
12
|
+
import addListIndexesRoute from './data-index-list.js';
|
|
13
|
+
import addCreateIndexRoute from './data-index-create.js';
|
|
14
|
+
import addDeleteIndexRoute from './data-index-delete.js';
|
|
15
|
+
// Schemas:
|
|
16
|
+
import addListSchemasRoute from './data-schemas-list.js';
|
|
17
|
+
import addGetSchemaRoute from './data-schema-get.js';
|
|
18
|
+
import addSetSchemaRoute from './data-schema-set.js';
|
|
19
|
+
import addTestSchemaRoute from './data-schema-test.js';
|
|
20
|
+
// Sycnronization:
|
|
21
|
+
import addSyncMutationsRoute from './data-sync-mutations.js';
|
|
22
|
+
import addSyncChangesRoute from './data-sync-changes.js';
|
|
23
|
+
export const addRoutes = (env) => {
|
|
24
|
+
// Add context middleware that handles AceBase-Context header
|
|
25
|
+
addContextMiddleware(env);
|
|
26
|
+
// Add get data endpoint
|
|
27
|
+
addGetDataRoute(env);
|
|
28
|
+
// Add update data endpoint
|
|
29
|
+
addUpdateDataRoute(env);
|
|
30
|
+
// Add set data endpoint
|
|
31
|
+
addSetDataRoute(env);
|
|
32
|
+
// add transaction routes (start & finish)
|
|
33
|
+
addTransactionRoutes(env);
|
|
34
|
+
// Add exists endpoint
|
|
35
|
+
addExistsRoute(env);
|
|
36
|
+
// Add reflect endpoint
|
|
37
|
+
addReflectRoute(env);
|
|
38
|
+
// Add import endpoint
|
|
39
|
+
addImportDataRoute(env);
|
|
40
|
+
// Add export endpoint
|
|
41
|
+
addExportDataRoute(env);
|
|
42
|
+
// Add query endpoint
|
|
43
|
+
addQueryRoute(env);
|
|
44
|
+
// Add index endpoints:
|
|
45
|
+
addListIndexesRoute(env); // list indexes
|
|
46
|
+
addCreateIndexRoute(env); // create index
|
|
47
|
+
addDeleteIndexRoute(env); // delete index
|
|
48
|
+
// Add schema endpoints:
|
|
49
|
+
addListSchemasRoute(env); // list all
|
|
50
|
+
addGetSchemaRoute(env); // get schema
|
|
51
|
+
addSetSchemaRoute(env); // set schema
|
|
52
|
+
addTestSchemaRoute(env); // test
|
|
53
|
+
// Add sync mutations endpoint
|
|
54
|
+
addSyncMutationsRoute(env);
|
|
55
|
+
// add sync changes endpoint
|
|
56
|
+
addSyncChangesRoute(env);
|
|
57
|
+
};
|
|
58
|
+
export default addRoutes;
|
|
59
59
|
//# sourceMappingURL=data.js.map
|