acebase-server 1.5.1 → 1.6.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/package.json +3 -3
- package/src/acebase-server.js +92 -30
- package/src/index.d.ts +10 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "acebase-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "AceBase realtime database server (webserver endpoint to allow remote connections)",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"private": false,
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"author": "Ewout Stortenbeker <me@appy.one> (http://appy.one)",
|
|
45
45
|
"license": "MIT",
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"acebase": "^1.
|
|
48
|
-
"acebase-core": "^1.
|
|
47
|
+
"acebase": "^1.11.0",
|
|
48
|
+
"acebase-core": "^1.10.0",
|
|
49
49
|
"express": "^4.17.1",
|
|
50
50
|
"socket.io": "^2.4.1"
|
|
51
51
|
},
|
package/src/acebase-server.js
CHANGED
|
@@ -2076,7 +2076,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
2076
2076
|
res.send(info);
|
|
2077
2077
|
});
|
|
2078
2078
|
|
|
2079
|
-
app.get(`/data/${dbname}/*`, (req, res) => {
|
|
2079
|
+
app.get(`/data/${dbname}/*`, async (req, res) => {
|
|
2080
2080
|
// Request data
|
|
2081
2081
|
const path = req.path.substr(dbname.length + 7); //.replace(/^\/+/g, '').replace(/\/+$/g, '');
|
|
2082
2082
|
if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
|
|
@@ -2107,25 +2107,25 @@ class AceBaseServer extends EventEmitter {
|
|
|
2107
2107
|
options.exclude = [...options.exclude || [], '__auth__', '__log__'];
|
|
2108
2108
|
}
|
|
2109
2109
|
|
|
2110
|
-
|
|
2111
|
-
|
|
2110
|
+
try {
|
|
2111
|
+
// const snap = await db.ref(path).get(options);
|
|
2112
|
+
// const value = snap.val(), context = snap.context();
|
|
2113
|
+
const { value, context } = await db.api.get(path, options);
|
|
2114
|
+
if (!this.config.transactionLoggingEnabled) {
|
|
2115
|
+
delete context.acebase_cursor;
|
|
2116
|
+
}
|
|
2117
|
+
const serialized = Transport.serialize(value);
|
|
2118
|
+
|
|
2119
|
+
res.setHeader('AceBase-Context', JSON.stringify(context));
|
|
2120
|
+
res.send({
|
|
2121
|
+
exists: value !== null,
|
|
2122
|
+
val: serialized.val,
|
|
2123
|
+
map: serialized.map
|
|
2124
|
+
});
|
|
2125
|
+
}
|
|
2126
|
+
catch (err) {
|
|
2127
|
+
res.status(500).send(err);
|
|
2112
2128
|
}
|
|
2113
|
-
|
|
2114
|
-
db.ref(path)
|
|
2115
|
-
.get(options) //.once("value")
|
|
2116
|
-
.then(snap => {
|
|
2117
|
-
const ser = Transport.serialize(snap.val());
|
|
2118
|
-
const data = {
|
|
2119
|
-
exists: snap.exists(),
|
|
2120
|
-
val: ser.val,
|
|
2121
|
-
map: ser.map
|
|
2122
|
-
}
|
|
2123
|
-
res.send(data);
|
|
2124
|
-
})
|
|
2125
|
-
.catch(err => {
|
|
2126
|
-
res.statusCode = 500;
|
|
2127
|
-
res.send(err);
|
|
2128
|
-
});
|
|
2129
2129
|
});
|
|
2130
2130
|
|
|
2131
2131
|
app.get(`/reflect/${dbname}/*`, (req, res) => {
|
|
@@ -2335,7 +2335,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
2335
2335
|
};
|
|
2336
2336
|
});
|
|
2337
2337
|
|
|
2338
|
-
app.post(`/query/${dbname}/*`, (req, res) => {
|
|
2338
|
+
app.post(`/query/${dbname}/*`, async (req, res) => {
|
|
2339
2339
|
// Execute query
|
|
2340
2340
|
const path = req.path.substr(dbname.length + 8);
|
|
2341
2341
|
if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
|
|
@@ -2343,6 +2343,9 @@ class AceBaseServer extends EventEmitter {
|
|
|
2343
2343
|
}
|
|
2344
2344
|
|
|
2345
2345
|
const data = Transport.deserialize(req.body);
|
|
2346
|
+
if (typeof data !== 'object' || typeof data.query !== 'object' || typeof data.options !== 'object') {
|
|
2347
|
+
return sendError(res, { code: 'invalid_request', message: 'Invalid query request' });
|
|
2348
|
+
}
|
|
2346
2349
|
const query = data.query;
|
|
2347
2350
|
const options = data.options;
|
|
2348
2351
|
if (options.monitor === true) {
|
|
@@ -2366,14 +2369,21 @@ class AceBaseServer extends EventEmitter {
|
|
|
2366
2369
|
}
|
|
2367
2370
|
options.eventHandler = sendEvent;
|
|
2368
2371
|
}
|
|
2369
|
-
|
|
2370
|
-
|
|
2372
|
+
try {
|
|
2373
|
+
const { results, context } = await db.api.query(path, query, options);
|
|
2374
|
+
if (!this.config.transactionLoggingEnabled) {
|
|
2375
|
+
delete context.acebase_cursor;
|
|
2376
|
+
}
|
|
2371
2377
|
const response = {
|
|
2372
2378
|
count: results.length,
|
|
2373
2379
|
list: results // []
|
|
2374
2380
|
};
|
|
2381
|
+
res.setHeader('AceBase-Context', JSON.stringify(context));
|
|
2375
2382
|
res.send(Transport.serialize(response));
|
|
2376
|
-
}
|
|
2383
|
+
}
|
|
2384
|
+
catch(err) {
|
|
2385
|
+
sendError(res, { code: 'unknown', message: err.message });
|
|
2386
|
+
}
|
|
2377
2387
|
});
|
|
2378
2388
|
|
|
2379
2389
|
app.get(`/index/${dbname}`, async (req, res) => {
|
|
@@ -2477,9 +2487,16 @@ class AceBaseServer extends EventEmitter {
|
|
|
2477
2487
|
}
|
|
2478
2488
|
try {
|
|
2479
2489
|
const data = req.query;
|
|
2480
|
-
let targets =
|
|
2490
|
+
let targets = typeof data.path === 'string'
|
|
2491
|
+
? [{ path: data.path, events: ['value'] }]
|
|
2492
|
+
: typeof data.for === 'string'
|
|
2493
|
+
? JSON.parse(data.for)
|
|
2494
|
+
: null;
|
|
2495
|
+
if (targets === null) {
|
|
2496
|
+
return sendError(res, { code: 'invalid_request', message: 'Invalid mutations request' });
|
|
2497
|
+
}
|
|
2481
2498
|
if (targets.length === 0) {
|
|
2482
|
-
targets.push({ path: '', events: '
|
|
2499
|
+
targets.push({ path: '', events: ['value'] });
|
|
2483
2500
|
}
|
|
2484
2501
|
// Filter out any requested paths user does not have access to.
|
|
2485
2502
|
targets = targets.filter(target => {
|
|
@@ -2494,10 +2511,55 @@ class AceBaseServer extends EventEmitter {
|
|
|
2494
2511
|
return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
|
|
2495
2512
|
}
|
|
2496
2513
|
|
|
2497
|
-
const cursor = data
|
|
2498
|
-
const
|
|
2499
|
-
|
|
2500
|
-
res.
|
|
2514
|
+
const { cursor, timestamp } = data;
|
|
2515
|
+
const result = await db.api.getMutations({ for: targets, cursor, timestamp });
|
|
2516
|
+
|
|
2517
|
+
res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
|
|
2518
|
+
res.contentType('application/json');
|
|
2519
|
+
res.send(result.mutations);
|
|
2520
|
+
}
|
|
2521
|
+
catch(err) {
|
|
2522
|
+
sendError(res, err);
|
|
2523
|
+
}
|
|
2524
|
+
});
|
|
2525
|
+
|
|
2526
|
+
app.get(`/sync/changes/${dbname}`, async (req, res) => {
|
|
2527
|
+
// Gets effective changes for specific path(s) and event combinations since given cursor
|
|
2528
|
+
if (!this.config.transactionLoggingEnabled) {
|
|
2529
|
+
return sendError(res, { code: 'no_transaction_logging', message: 'Transaction logging not enabled' });
|
|
2530
|
+
}
|
|
2531
|
+
try {
|
|
2532
|
+
const data = req.query;
|
|
2533
|
+
let targets = typeof data.path === 'string'
|
|
2534
|
+
? [{ path: data.path, events: ['value'] }]
|
|
2535
|
+
: typeof data.for === 'string'
|
|
2536
|
+
? JSON.parse(data.for)
|
|
2537
|
+
: null;
|
|
2538
|
+
if (targets === null) {
|
|
2539
|
+
return sendError(res, { code: 'invalid_request', message: 'Invalid mutations request' });
|
|
2540
|
+
}
|
|
2541
|
+
if (targets.length === 0) {
|
|
2542
|
+
targets.push({ path: '', events: ['value'] });
|
|
2543
|
+
}
|
|
2544
|
+
// Filter out any requested paths user does not have access to.
|
|
2545
|
+
targets = targets.filter(target => {
|
|
2546
|
+
let path = target.path;
|
|
2547
|
+
if (target.events.every(event => /^(?:notify_)?child_/.test(event))) { //if (!target.events.some(event => ['value','notify_value','mutations','mutated'].includes(event))) {
|
|
2548
|
+
// Only child_ events, check if they have access to children instead
|
|
2549
|
+
path = PathInfo.get(path).childPath('*');
|
|
2550
|
+
}
|
|
2551
|
+
return userHasAccess(req.user, path, false);
|
|
2552
|
+
});
|
|
2553
|
+
if (targets.length === 0) {
|
|
2554
|
+
return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
|
|
2555
|
+
}
|
|
2556
|
+
|
|
2557
|
+
const { cursor, timestamp } = data;
|
|
2558
|
+
const result = await db.api.getChanges({ for: targets, cursor, timestamp });
|
|
2559
|
+
|
|
2560
|
+
res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
|
|
2561
|
+
res.contentType('application/json');
|
|
2562
|
+
res.send(result.changes);
|
|
2501
2563
|
}
|
|
2502
2564
|
catch(err) {
|
|
2503
2565
|
sendError(res, err);
|
|
@@ -2637,7 +2699,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
2637
2699
|
fs.unwatchFile(rulesFilePath);
|
|
2638
2700
|
await new Promise((resolve) => {
|
|
2639
2701
|
server.close(resolve);
|
|
2640
|
-
clients.list.forEach(client => {
|
|
2702
|
+
clients.list.slice().forEach(client => {
|
|
2641
2703
|
client.socket.disconnect(true);
|
|
2642
2704
|
});
|
|
2643
2705
|
});
|
package/src/index.d.ts
CHANGED
|
@@ -4,7 +4,14 @@ import { AceBase } from 'acebase';
|
|
|
4
4
|
declare namespace acebaseserver {
|
|
5
5
|
class AceBaseServer {
|
|
6
6
|
constructor(dbname: string, options?: AceBaseServerSettings)
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Wait for the server to be ready to accept incoming connections
|
|
10
|
+
* @param callback (optional) callback function that is called when ready. You can also use the returned promise
|
|
11
|
+
* @returns returns a promise that resolves when ready
|
|
12
|
+
*/
|
|
13
|
+
ready(callback?: () => void): Promise<void>
|
|
14
|
+
|
|
8
15
|
get url(): string
|
|
9
16
|
|
|
10
17
|
/**
|
|
@@ -118,6 +125,8 @@ declare namespace acebaseserver {
|
|
|
118
125
|
/** 'allow', 'deny' or 'auth' */
|
|
119
126
|
defaultAccessRule?: string
|
|
120
127
|
defaultAdminPassword?: string
|
|
128
|
+
separateDb?: boolean
|
|
129
|
+
/** @deprecated Misspelled, use separateDb instead */
|
|
121
130
|
seperateDb?: boolean
|
|
122
131
|
}
|
|
123
132
|
|