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