rxdb-server 15.7.0 → 15.13.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 +4 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/plugins/adapter-express/index.js +122 -0
- package/dist/cjs/plugins/adapter-express/index.js.map +1 -0
- package/dist/cjs/plugins/client-rest/index.js.map +1 -1
- package/dist/cjs/plugins/client-rest/utils.js.map +1 -1
- package/dist/cjs/plugins/replication-server/helpers.js.map +1 -1
- package/dist/cjs/plugins/replication-server/index.js +1 -1
- package/dist/cjs/plugins/replication-server/index.js.map +1 -1
- package/dist/cjs/plugins/replication-server/types.js.map +1 -1
- package/dist/cjs/plugins/server/endpoint-replication.js +22 -20
- package/dist/cjs/plugins/server/endpoint-replication.js.map +1 -1
- package/dist/cjs/plugins/server/endpoint-rest.js +33 -31
- package/dist/cjs/plugins/server/endpoint-rest.js.map +1 -1
- package/dist/cjs/plugins/server/helper.js +6 -45
- package/dist/cjs/plugins/server/helper.js.map +1 -1
- package/dist/cjs/plugins/server/index.js +5 -15
- package/dist/cjs/plugins/server/index.js.map +1 -1
- package/dist/cjs/plugins/server/rx-server.js +25 -22
- package/dist/cjs/plugins/server/rx-server.js.map +1 -1
- package/dist/cjs/plugins/server/types.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/plugins/adapter-express/index.js +115 -0
- package/dist/esm/plugins/adapter-express/index.js.map +1 -0
- package/dist/esm/plugins/client-rest/index.js.map +1 -1
- package/dist/esm/plugins/client-rest/utils.js.map +1 -1
- package/dist/esm/plugins/replication-server/helpers.js.map +1 -1
- package/dist/esm/plugins/replication-server/index.js +1 -1
- package/dist/esm/plugins/replication-server/index.js.map +1 -1
- package/dist/esm/plugins/replication-server/types.js.map +1 -1
- package/dist/esm/plugins/server/endpoint-replication.js +23 -21
- package/dist/esm/plugins/server/endpoint-replication.js.map +1 -1
- package/dist/esm/plugins/server/endpoint-rest.js +34 -32
- package/dist/esm/plugins/server/endpoint-rest.js.map +1 -1
- package/dist/esm/plugins/server/helper.js +6 -42
- package/dist/esm/plugins/server/helper.js.map +1 -1
- package/dist/esm/plugins/server/index.js +3 -12
- package/dist/esm/plugins/server/index.js.map +1 -1
- package/dist/esm/plugins/server/rx-server.js +25 -22
- package/dist/esm/plugins/server/rx-server.js.map +1 -1
- package/dist/esm/plugins/server/types.js.map +1 -1
- package/dist/types/plugins/adapter-express/index.d.ts +7 -0
- package/dist/types/plugins/server/endpoint-replication.d.ts +3 -3
- package/dist/types/plugins/server/endpoint-rest.d.ts +3 -3
- package/dist/types/plugins/server/helper.d.ts +6 -8
- package/dist/types/plugins/server/index.d.ts +1 -1
- package/dist/types/plugins/server/rx-server.d.ts +12 -11
- package/dist/types/plugins/server/types.d.ts +26 -3
- package/package.json +33 -27
- package/plugins/adapter-express/index.cjs +2 -0
- package/plugins/adapter-express/index.d.cts +1 -0
- package/plugins/adapter-express/index.d.mts +1 -0
- package/plugins/adapter-express/index.mjs +1 -0
- package/plugins/adapter-express/index.ts +1 -0
- package/plugins/adapter-express/package.json +18 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { normalizeMangoQuery } from 'rxdb/plugins/core';
|
|
2
2
|
import { filter, mergeMap } from 'rxjs';
|
|
3
3
|
import { ensureNotFalsy, getFromMapOrThrow } from 'rxdb/plugins/utils';
|
|
4
|
-
import { addAuthMiddleware, blockPreviousVersionPaths,
|
|
4
|
+
import { addAuthMiddleware, blockPreviousVersionPaths, docContainsServerOnlyFields, doesContainRegexQuerySelector, getDocAllowedMatcher, removeServerOnlyFieldsMonad, setCors } from "./helper.js";
|
|
5
5
|
export var REST_PATHS = ['query', 'query/observe', 'get', 'set', 'delete'
|
|
6
6
|
|
|
7
7
|
// TODO
|
|
@@ -18,6 +18,7 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
18
18
|
this.collection = collection;
|
|
19
19
|
this.serverOnlyFields = serverOnlyFields;
|
|
20
20
|
this.cors = cors;
|
|
21
|
+
var adapter = server.adapter;
|
|
21
22
|
setCors(this.server, [this.name].join('/'), cors);
|
|
22
23
|
blockPreviousVersionPaths(this.server, [this.name].join('/'), collection.schema.version);
|
|
23
24
|
this.urlPath = [this.name, collection.schema.version].join('/');
|
|
@@ -36,19 +37,20 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
36
37
|
return changeValidator(authData, change);
|
|
37
38
|
};
|
|
38
39
|
var removeServerOnlyFields = removeServerOnlyFieldsMonad(this.serverOnlyFields);
|
|
39
|
-
this.server.
|
|
40
|
+
this.server.adapter.post(this.server.serverApp, '/' + this.urlPath + '/query', async (req, res) => {
|
|
41
|
+
ensureNotFalsy(adapter.getRequestBody(req), 'req body is empty');
|
|
40
42
|
var authData = getFromMapOrThrow(authDataByRequest, req);
|
|
41
43
|
var useQuery;
|
|
42
44
|
try {
|
|
43
|
-
useQuery = this.queryModifier(ensureNotFalsy(authData), normalizeMangoQuery(this.collection.schema.jsonSchema, req
|
|
45
|
+
useQuery = this.queryModifier(ensureNotFalsy(authData), normalizeMangoQuery(this.collection.schema.jsonSchema, adapter.getRequestBody(req)));
|
|
44
46
|
} catch (err) {
|
|
45
|
-
closeConnection(res, 400, 'Bad Request');
|
|
47
|
+
adapter.closeConnection(res, 400, 'Bad Request');
|
|
46
48
|
return;
|
|
47
49
|
}
|
|
48
50
|
var rxQuery = this.collection.find(useQuery);
|
|
49
51
|
var result = await rxQuery.exec();
|
|
50
|
-
|
|
51
|
-
|
|
52
|
+
adapter.setResponseHeader(res, 'Content-Type', 'application/json');
|
|
53
|
+
adapter.endResponseJson(res, {
|
|
52
54
|
documents: result.map(d => removeServerOnlyFields(d.toJSON()))
|
|
53
55
|
});
|
|
54
56
|
});
|
|
@@ -58,10 +60,10 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
58
60
|
* so we send the query as query parameter in base64
|
|
59
61
|
* like ?query=e3NlbGVjdG9yOiB7fX0=
|
|
60
62
|
*/
|
|
61
|
-
this.server.
|
|
63
|
+
this.server.adapter.get(this.server.serverApp, '/' + this.urlPath + '/query/observe', async (req, res) => {
|
|
62
64
|
var authData = getFromMapOrThrow(authDataByRequest, req);
|
|
63
|
-
|
|
64
|
-
var useQuery = this.queryModifier(ensureNotFalsy(authData), normalizeMangoQuery(this.collection.schema.jsonSchema, JSON.parse(atob(req.query
|
|
65
|
+
adapter.setSSEHeaders(res);
|
|
66
|
+
var useQuery = this.queryModifier(ensureNotFalsy(authData), normalizeMangoQuery(this.collection.schema.jsonSchema, JSON.parse(atob(adapter.getRequestQuery(req).query))));
|
|
65
67
|
var rxQuery = this.collection.find(useQuery);
|
|
66
68
|
var subscription = rxQuery.$.pipe(mergeMap(async result => {
|
|
67
69
|
var resultData = result.map(doc => removeServerOnlyFields(doc.toJSON()));
|
|
@@ -72,27 +74,27 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
72
74
|
* before emitting the new results.
|
|
73
75
|
*/
|
|
74
76
|
try {
|
|
75
|
-
authData = await server.authHandler(req
|
|
77
|
+
authData = await server.authHandler(adapter.getRequestHeaders(req));
|
|
76
78
|
} catch (err) {
|
|
77
|
-
closeConnection(res, 401, 'Unauthorized');
|
|
79
|
+
adapter.closeConnection(res, 401, 'Unauthorized');
|
|
78
80
|
return null;
|
|
79
81
|
}
|
|
80
82
|
return resultData;
|
|
81
83
|
}), filter(f => f !== null)).subscribe(resultData => {
|
|
82
|
-
|
|
84
|
+
adapter.responseWrite(res, 'data: ' + JSON.stringify(resultData) + '\n\n');
|
|
83
85
|
});
|
|
84
86
|
|
|
85
87
|
/**
|
|
86
88
|
* @link https://youtu.be/0PcMuYGJPzM?si=AxkczxcMaUwhh8k9&t=363
|
|
87
89
|
*/
|
|
88
|
-
|
|
90
|
+
adapter.onRequestClose(req, () => {
|
|
89
91
|
subscription.unsubscribe();
|
|
90
|
-
|
|
92
|
+
adapter.endResponse(req);
|
|
91
93
|
});
|
|
92
94
|
});
|
|
93
|
-
this.server.
|
|
95
|
+
this.server.adapter.post(this.server.serverApp, '/' + this.urlPath + '/get', async (req, res) => {
|
|
94
96
|
var authData = getFromMapOrThrow(authDataByRequest, req);
|
|
95
|
-
var ids = req
|
|
97
|
+
var ids = adapter.getRequestBody(req);
|
|
96
98
|
var rxQuery = this.collection.findByIds(ids);
|
|
97
99
|
var resultMap = await rxQuery.exec();
|
|
98
100
|
var resultValues = Array.from(resultMap.values());
|
|
@@ -100,19 +102,19 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
100
102
|
var useDocs = resultValues.map(d => d.toJSON());
|
|
101
103
|
useDocs = useDocs.filter(d => docMatcher(d));
|
|
102
104
|
useDocs = useDocs.map(d => removeServerOnlyFields(d));
|
|
103
|
-
|
|
104
|
-
|
|
105
|
+
adapter.setResponseHeader(res, 'Content-Type', 'application/json');
|
|
106
|
+
adapter.endResponseJson(res, {
|
|
105
107
|
documents: useDocs
|
|
106
108
|
});
|
|
107
109
|
});
|
|
108
|
-
this.server.
|
|
110
|
+
this.server.adapter.post(this.server.serverApp, '/' + this.urlPath + '/set', async (req, res) => {
|
|
109
111
|
var authData = getFromMapOrThrow(authDataByRequest, req);
|
|
110
112
|
var docDataMatcherWrite = getDocAllowedMatcher(this, ensureNotFalsy(authData));
|
|
111
|
-
var docsData = req
|
|
113
|
+
var docsData = adapter.getRequestBody(req);
|
|
112
114
|
for (var docData of docsData) {
|
|
113
115
|
var allowed = docDataMatcherWrite(docData);
|
|
114
116
|
if (!allowed) {
|
|
115
|
-
closeConnection(res, 403, 'Forbidden');
|
|
117
|
+
adapter.closeConnection(res, 403, 'Forbidden');
|
|
116
118
|
return;
|
|
117
119
|
}
|
|
118
120
|
}
|
|
@@ -121,7 +123,7 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
121
123
|
// just retry on conflicts
|
|
122
124
|
docsData.push(docData);
|
|
123
125
|
} else {
|
|
124
|
-
closeConnection(res, 500, 'Internal Server Error');
|
|
126
|
+
adapter.closeConnection(res, 500, 'Internal Server Error');
|
|
125
127
|
throw err;
|
|
126
128
|
}
|
|
127
129
|
}
|
|
@@ -141,7 +143,7 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
141
143
|
assumedMasterState: removeServerOnlyFields(doc.toJSON(true))
|
|
142
144
|
});
|
|
143
145
|
if (!isAllowed) {
|
|
144
|
-
closeConnection(res, 403, 'Forbidden');
|
|
146
|
+
adapter.closeConnection(res, 403, 'Forbidden');
|
|
145
147
|
return {
|
|
146
148
|
v: void 0
|
|
147
149
|
};
|
|
@@ -156,13 +158,13 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
156
158
|
}
|
|
157
159
|
await Promise.all(promises);
|
|
158
160
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
+
adapter.setResponseHeader(res, 'Content-Type', 'application/json');
|
|
162
|
+
adapter.endResponseJson(res, {});
|
|
161
163
|
});
|
|
162
|
-
this.server.
|
|
164
|
+
this.server.adapter.post(this.server.serverApp, '/' + this.urlPath + '/delete', async (req, res) => {
|
|
163
165
|
var authData = getFromMapOrThrow(authDataByRequest, req);
|
|
164
166
|
var docDataMatcherWrite = getDocAllowedMatcher(this, ensureNotFalsy(authData));
|
|
165
|
-
var ids = req
|
|
167
|
+
var ids = adapter.getRequestBody(req);
|
|
166
168
|
while (ids.length > 0) {
|
|
167
169
|
var useIds = ids.slice(0);
|
|
168
170
|
ids = [];
|
|
@@ -173,7 +175,7 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
173
175
|
if (doc) {
|
|
174
176
|
var isAllowedDoc = docDataMatcherWrite(doc.toJSON(true));
|
|
175
177
|
if (!isAllowedDoc) {
|
|
176
|
-
closeConnection(res, 403, 'Forbidden');
|
|
178
|
+
adapter.closeConnection(res, 403, 'Forbidden');
|
|
177
179
|
return {
|
|
178
180
|
v: void 0
|
|
179
181
|
};
|
|
@@ -183,7 +185,7 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
183
185
|
assumedMasterState: doc.toJSON(true)
|
|
184
186
|
});
|
|
185
187
|
if (!isAllowedChange) {
|
|
186
|
-
closeConnection(res, 403, 'Forbidden');
|
|
188
|
+
adapter.closeConnection(res, 403, 'Forbidden');
|
|
187
189
|
return {
|
|
188
190
|
v: void 0
|
|
189
191
|
};
|
|
@@ -193,7 +195,7 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
193
195
|
// just retry on conflicts
|
|
194
196
|
ids.push(id);
|
|
195
197
|
} else {
|
|
196
|
-
closeConnection(res, 500, 'Internal Server Error');
|
|
198
|
+
adapter.closeConnection(res, 500, 'Internal Server Error');
|
|
197
199
|
throw err;
|
|
198
200
|
}
|
|
199
201
|
}));
|
|
@@ -206,8 +208,8 @@ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, co
|
|
|
206
208
|
}
|
|
207
209
|
await Promise.all(promises);
|
|
208
210
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
+
adapter.setResponseHeader(res, 'Content-Type', 'application/json');
|
|
212
|
+
adapter.endResponseJson(res, {});
|
|
211
213
|
});
|
|
212
214
|
};
|
|
213
215
|
//# sourceMappingURL=endpoint-rest.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-rest.js","names":["normalizeMangoQuery","filter","mergeMap","ensureNotFalsy","getFromMapOrThrow","addAuthMiddleware","blockPreviousVersionPaths","closeConnection","docContainsServerOnlyFields","doesContainRegexQuerySelector","getDocAllowedMatcher","removeServerOnlyFieldsMonad","setCors","writeSSEHeaders","REST_PATHS","RxServerRestEndpoint","server","name","collection","queryModifier","changeValidator","serverOnlyFields","cors","_this","type","join","schema","version","urlPath","primaryPath","authDataByRequest","authData","query","selector","Error","change","assumedMasterState","newDocumentState","removeServerOnlyFields","expressApp","post","req","res","useQuery","jsonSchema","body","err","rxQuery","find","result","exec","setHeader","json","documents","map","d","toJSON","get","JSON","parse","atob","subscription","$","pipe","resultData","doc","authHandler","headers","f","subscribe","write","stringify","on","unsubscribe","end","ids","findByIds","resultMap","resultValues","Array","from","values","docMatcher","useDocs","docDataMatcherWrite","docsData","docData","allowed","onWriteError","rxdb","code","push","length","promises","docs","useDocsData","slice","_loop","_docData","id","insert","catch","isAllowed","v","patch","_ret","Promise","all","useIds","docsMap","_loop2","isAllowedDoc","isAllowedChange","remove","_ret2"],"sources":["../../../../src/plugins/server/endpoint-rest.ts"],"sourcesContent":["import {\n FilledMangoQuery,\n RxCollection,\n RxError,\n normalizeMangoQuery\n} from 'rxdb/plugins/core';\nimport type { RxServer } from './rx-server.ts';\nimport type {\n RxServerChangeValidator,\n RxServerEndpoint,\n RxServerQueryModifier\n} from './types.ts';\nimport { filter, mergeMap } from 'rxjs';\nimport {\n ensureNotFalsy,\n getFromMapOrThrow\n} from 'rxdb/plugins/utils';\n\nimport {\n addAuthMiddleware,\n blockPreviousVersionPaths,\n closeConnection,\n docContainsServerOnlyFields,\n doesContainRegexQuerySelector,\n getDocAllowedMatcher,\n removeServerOnlyFieldsMonad,\n setCors,\n writeSSEHeaders\n} from './helper.ts';\n\n\nexport const REST_PATHS = [\n 'query',\n 'query/observe',\n 'get',\n 'set',\n 'delete',\n\n // TODO\n /*\n 'attachments/add',\n 'attachments/delete',\n 'events'\n */\n] as const;\n\n\nexport class RxServerRestEndpoint<AuthType, RxDocType> implements RxServerEndpoint<AuthType, RxDocType> {\n readonly type = 'rest';\n readonly urlPath: string;\n readonly changeValidator: RxServerChangeValidator<AuthType, RxDocType>;\n readonly queryModifier: RxServerQueryModifier<AuthType, RxDocType>;\n constructor(\n public readonly server: RxServer<AuthType>,\n public readonly name: string,\n public readonly collection: RxCollection<RxDocType>,\n queryModifier: RxServerQueryModifier<AuthType, RxDocType>,\n changeValidator: RxServerChangeValidator<AuthType, RxDocType>,\n public readonly serverOnlyFields: string[],\n public readonly cors?: string\n ) {\n setCors(this.server, [this.name].join('/'), cors);\n blockPreviousVersionPaths(this.server, [this.name].join('/'), collection.schema.version);\n\n this.urlPath = [this.name, collection.schema.version].join('/');\n const primaryPath = this.collection.schema.primaryPath;\n const authDataByRequest = addAuthMiddleware(\n this.server,\n this.urlPath\n );\n\n this.queryModifier = (authData, query) => {\n if (doesContainRegexQuerySelector(query.selector)) {\n throw new Error('$regex queries not allowed because of DOS-attacks');\n }\n return queryModifier(authData, query);\n }\n this.changeValidator = (authData, change) => {\n if (\n (\n change.assumedMasterState &&\n docContainsServerOnlyFields(serverOnlyFields, change.assumedMasterState)\n ) ||\n docContainsServerOnlyFields(serverOnlyFields, change.newDocumentState)\n ) {\n return false;\n }\n return changeValidator(authData, change);\n }\n const removeServerOnlyFields = removeServerOnlyFieldsMonad(this.serverOnlyFields);\n\n this.server.expressApp.post('/' + this.urlPath + '/query', async (req, res) => {\n const authData = getFromMapOrThrow(authDataByRequest, req);\n let useQuery: FilledMangoQuery<RxDocType>\n try {\n useQuery = this.queryModifier(\n ensureNotFalsy(authData),\n normalizeMangoQuery(\n this.collection.schema.jsonSchema,\n req.body\n )\n );\n } catch (err) {\n closeConnection(res, 400, 'Bad Request');\n return;\n }\n const rxQuery = this.collection.find(useQuery as any);\n const result = await rxQuery.exec();\n res.setHeader('Content-Type', 'application/json');\n res.json({\n documents: result.map(d => removeServerOnlyFields(d.toJSON()))\n });\n });\n\n /**\n * It is not possible to send data with server send events,\n * so we send the query as query parameter in base64\n * like ?query=e3NlbGVjdG9yOiB7fX0=\n */\n this.server.expressApp.get('/' + this.urlPath + '/query/observe', async (req, res) => {\n let authData = getFromMapOrThrow(authDataByRequest, req);\n writeSSEHeaders(res);\n\n const useQuery: FilledMangoQuery<RxDocType> = this.queryModifier(\n ensureNotFalsy(authData),\n normalizeMangoQuery(\n this.collection.schema.jsonSchema,\n JSON.parse(atob(req.query.query as string))\n )\n );\n\n const rxQuery = this.collection.find(useQuery as any);\n const subscription = rxQuery.$.pipe(\n mergeMap(async (result) => {\n const resultData = result.map(doc => removeServerOnlyFields(doc.toJSON()));\n\n /**\n * The auth-data might be expired\n * so we re-run the auth parsing each time\n * before emitting the new results.\n */\n try {\n authData = await server.authHandler(req.headers);\n } catch (err) {\n closeConnection(res, 401, 'Unauthorized');\n return null;\n }\n\n return resultData;\n }),\n filter(f => f !== null)\n ).subscribe(resultData => {\n res.write('data: ' + JSON.stringify(resultData) + '\\n\\n');\n });\n\n /**\n * @link https://youtu.be/0PcMuYGJPzM?si=AxkczxcMaUwhh8k9&t=363\n */\n req.on('close', () => {\n subscription.unsubscribe();\n res.end();\n });\n });\n\n\n this.server.expressApp.post('/' + this.urlPath + '/get', async (req, res) => {\n const authData = getFromMapOrThrow(authDataByRequest, req);\n const ids: string[] = req.body;\n\n const rxQuery = this.collection.findByIds(ids);\n const resultMap = await rxQuery.exec();\n const resultValues = Array.from(resultMap.values());\n const docMatcher = getDocAllowedMatcher(this, ensureNotFalsy(authData));\n let useDocs = resultValues.map(d => d.toJSON());\n useDocs = useDocs.filter(d => docMatcher(d as any));\n useDocs = useDocs.map(d => removeServerOnlyFields(d))\n\n res.setHeader('Content-Type', 'application/json');\n res.json({\n documents: useDocs\n });\n });\n\n this.server.expressApp.post('/' + this.urlPath + '/set', async (req, res) => {\n const authData = getFromMapOrThrow(authDataByRequest, req);\n const docDataMatcherWrite = getDocAllowedMatcher(this, ensureNotFalsy(authData));\n\n let docsData: RxDocType[] = req.body;\n\n for (const docData of docsData) {\n const allowed = docDataMatcherWrite(docData as any);\n if (!allowed) {\n closeConnection(res, 403, 'Forbidden');\n return;\n }\n }\n\n function onWriteError(err: RxError, docData: RxDocType) {\n if (err.rxdb && err.code === 'CONFLICT') {\n // just retry on conflicts\n docsData.push(docData);\n } else {\n closeConnection(res, 500, 'Internal Server Error');\n throw err;\n }\n }\n\n while (docsData.length > 0) {\n const promises: Promise<any>[] = [];\n const docs = await collection.findByIds(docsData.map(d => (d as any)[primaryPath])).exec();\n let useDocsData = docsData.slice();\n docsData = [];\n for (const docData of useDocsData) {\n const id = (docData as any)[primaryPath];\n const doc = docs.get(id);\n if (!doc) {\n promises.push(this.collection.insert(docData).catch(err => onWriteError(err, docData)));\n } else {\n const isAllowed = this.changeValidator(authData, {\n newDocumentState: removeServerOnlyFields(docData as any),\n assumedMasterState: removeServerOnlyFields(doc.toJSON(true))\n });\n if (!isAllowed) {\n closeConnection(res, 403, 'Forbidden');\n return;\n }\n promises.push(doc.patch(docData).catch(err => onWriteError(err, docData)));\n }\n }\n await Promise.all(promises);\n }\n\n res.setHeader('Content-Type', 'application/json');\n res.json({\n });\n });\n\n this.server.expressApp.post('/' + this.urlPath + '/delete', async (req, res) => {\n const authData = getFromMapOrThrow(authDataByRequest, req);\n const docDataMatcherWrite = getDocAllowedMatcher(this, ensureNotFalsy(authData));\n\n let ids: string[] = req.body;\n while (ids.length > 0) {\n const useIds = ids.slice(0);\n ids = [];\n const promises: Promise<any>[] = [];\n const docsMap = await this.collection.findByIds(useIds).exec();\n for (const id of useIds) {\n const doc = docsMap.get(id);\n if (doc) {\n const isAllowedDoc = docDataMatcherWrite(doc.toJSON(true) as any);\n if (!isAllowedDoc) {\n closeConnection(res, 403, 'Forbidden');\n return;\n }\n\n const isAllowedChange = this.changeValidator(authData, {\n newDocumentState: doc.toJSON(true) as any,\n assumedMasterState: doc.toJSON(true) as any\n });\n if (!isAllowedChange) {\n closeConnection(res, 403, 'Forbidden');\n return;\n }\n\n promises.push(doc.remove().catch((err: RxError) => {\n if (err.rxdb && err.code === 'CONFLICT') {\n // just retry on conflicts\n ids.push(id);\n } else {\n closeConnection(res, 500, 'Internal Server Error');\n throw err;\n }\n }));\n }\n }\n await Promise.all(promises);\n }\n res.setHeader('Content-Type', 'application/json');\n res.json({});\n });\n }\n}\n"],"mappings":"AAAA,SAIIA,mBAAmB,QAChB,mBAAmB;AAO1B,SAASC,MAAM,EAAEC,QAAQ,QAAQ,MAAM;AACvC,SAC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Map","ensureNotFalsy","getFromMapOrThrow","addAuthMiddleware","blockPreviousVersionPaths","docContainsServerOnlyFields","doesContainRegexQuerySelector","getDocAllowedMatcher","removeServerOnlyFieldsMonad","setCors","REST_PATHS","RxServerRestEndpoint","server","name","collection","queryModifier","changeValidator","serverOnlyFields","cors","_this","type","adapter","join","schema","version","urlPath","primaryPath","authDataByRequest","authData","query","selector","Error","change","assumedMasterState","newDocumentState","removeServerOnlyFields","post","serverApp","req","res","getRequestBody","useQuery","jsonSchema","err","closeConnection","rxQuery","find","result","exec","setResponseHeader","endResponseJson","documents","map","d","toJSON","get","setSSEHeaders","JSON","parse","atob","getRequestQuery","subscription","$","pipe","resultData","doc","authHandler","getRequestHeaders","f","subscribe","responseWrite","stringify","onRequestClose","unsubscribe","endResponse","ids","findByIds","resultMap","resultValues","Array","from","values","docMatcher","useDocs","docDataMatcherWrite","docsData","docData","allowed","onWriteError","rxdb","code","push","length","promises","docs","useDocsData","slice","_loop","_docData","id","insert","catch","isAllowed","v","patch","_ret","Promise","all","useIds","docsMap","_loop2","isAllowedDoc","isAllowedChange","remove","_ret2"],"sources":["../../../../src/plugins/server/endpoint-rest.ts"],"sourcesContent":["import {\n FilledMangoQuery,\n RxCollection,\n RxError,\n normalizeMangoQuery\n} from 'rxdb/plugins/core';\nimport type { RxServer } from './rx-server.ts';\nimport type {\n RxServerChangeValidator,\n RxServerEndpoint,\n RxServerQueryModifier\n} from './types.ts';\nimport { filter, mergeMap } from 'rxjs';\nimport {\n ensureNotFalsy,\n getFromMapOrThrow\n} from 'rxdb/plugins/utils';\n\nimport {\n addAuthMiddleware,\n blockPreviousVersionPaths,\n docContainsServerOnlyFields,\n doesContainRegexQuerySelector,\n getDocAllowedMatcher,\n removeServerOnlyFieldsMonad,\n setCors\n} from './helper.ts';\n\n\nexport const REST_PATHS = [\n 'query',\n 'query/observe',\n 'get',\n 'set',\n 'delete',\n\n // TODO\n /*\n 'attachments/add',\n 'attachments/delete',\n 'events'\n */\n] as const;\n\n\nexport class RxServerRestEndpoint<ServerAppType, AuthType, RxDocType> implements RxServerEndpoint<AuthType, RxDocType> {\n readonly type = 'rest';\n readonly urlPath: string;\n readonly changeValidator: RxServerChangeValidator<AuthType, RxDocType>;\n readonly queryModifier: RxServerQueryModifier<AuthType, RxDocType>;\n constructor(\n public readonly server: RxServer<ServerAppType, AuthType>,\n public readonly name: string,\n public readonly collection: RxCollection<RxDocType>,\n queryModifier: RxServerQueryModifier<AuthType, RxDocType>,\n changeValidator: RxServerChangeValidator<AuthType, RxDocType>,\n public readonly serverOnlyFields: string[],\n public readonly cors?: string\n ) {\n const adapter = server.adapter;\n setCors(this.server, [this.name].join('/'), cors);\n blockPreviousVersionPaths(this.server, [this.name].join('/'), collection.schema.version);\n\n this.urlPath = [this.name, collection.schema.version].join('/');\n const primaryPath = this.collection.schema.primaryPath;\n const authDataByRequest = addAuthMiddleware(\n this.server,\n this.urlPath\n );\n\n this.queryModifier = (authData, query) => {\n if (doesContainRegexQuerySelector(query.selector)) {\n throw new Error('$regex queries not allowed because of DOS-attacks');\n }\n return queryModifier(authData, query);\n }\n this.changeValidator = (authData, change) => {\n if (\n (\n change.assumedMasterState &&\n docContainsServerOnlyFields(serverOnlyFields, change.assumedMasterState)\n ) ||\n docContainsServerOnlyFields(serverOnlyFields, change.newDocumentState)\n ) {\n return false;\n }\n return changeValidator(authData, change);\n }\n const removeServerOnlyFields = removeServerOnlyFieldsMonad(this.serverOnlyFields);\n\n this.server.adapter.post(this.server.serverApp, '/' + this.urlPath + '/query', async (req, res) => {\n ensureNotFalsy(adapter.getRequestBody(req), 'req body is empty');\n const authData = getFromMapOrThrow(authDataByRequest, req);\n let useQuery: FilledMangoQuery<RxDocType>\n try {\n useQuery = this.queryModifier(\n ensureNotFalsy(authData),\n normalizeMangoQuery(\n this.collection.schema.jsonSchema,\n adapter.getRequestBody(req)\n )\n );\n } catch (err) {\n adapter.closeConnection(res, 400, 'Bad Request');\n return;\n }\n const rxQuery = this.collection.find(useQuery as any);\n const result = await rxQuery.exec();\n adapter.setResponseHeader(res, 'Content-Type', 'application/json');\n adapter.endResponseJson(res, {\n documents: result.map(d => removeServerOnlyFields(d.toJSON()))\n });\n });\n\n /**\n * It is not possible to send data with server send events,\n * so we send the query as query parameter in base64\n * like ?query=e3NlbGVjdG9yOiB7fX0=\n */\n this.server.adapter.get(this.server.serverApp, '/' + this.urlPath + '/query/observe', async (req, res) => {\n let authData = getFromMapOrThrow(authDataByRequest, req);\n adapter.setSSEHeaders(res);\n\n const useQuery: FilledMangoQuery<RxDocType> = this.queryModifier(\n ensureNotFalsy(authData),\n normalizeMangoQuery(\n this.collection.schema.jsonSchema,\n JSON.parse(atob(adapter.getRequestQuery(req).query as string))\n )\n );\n\n const rxQuery = this.collection.find(useQuery as any);\n const subscription = rxQuery.$.pipe(\n mergeMap(async (result) => {\n const resultData = result.map(doc => removeServerOnlyFields(doc.toJSON()));\n\n /**\n * The auth-data might be expired\n * so we re-run the auth parsing each time\n * before emitting the new results.\n */\n try {\n authData = await server.authHandler(adapter.getRequestHeaders(req));\n } catch (err) {\n adapter.closeConnection(res, 401, 'Unauthorized');\n return null;\n }\n\n return resultData;\n }),\n filter(f => f !== null)\n ).subscribe(resultData => {\n adapter.responseWrite(res, 'data: ' + JSON.stringify(resultData) + '\\n\\n');\n });\n\n /**\n * @link https://youtu.be/0PcMuYGJPzM?si=AxkczxcMaUwhh8k9&t=363\n */\n adapter.onRequestClose(req, () => {\n subscription.unsubscribe();\n adapter.endResponse(req);\n });\n });\n\n\n this.server.adapter.post(this.server.serverApp, '/' + this.urlPath + '/get', async (req, res) => {\n const authData = getFromMapOrThrow(authDataByRequest, req);\n const ids: string[] = adapter.getRequestBody(req);\n\n const rxQuery = this.collection.findByIds(ids);\n const resultMap = await rxQuery.exec();\n const resultValues = Array.from(resultMap.values());\n const docMatcher = getDocAllowedMatcher(this, ensureNotFalsy(authData));\n let useDocs = resultValues.map(d => d.toJSON());\n useDocs = useDocs.filter(d => docMatcher(d as any));\n useDocs = useDocs.map(d => removeServerOnlyFields(d))\n\n adapter.setResponseHeader(res, 'Content-Type', 'application/json');\n adapter.endResponseJson(res, {\n documents: useDocs\n });\n });\n\n this.server.adapter.post(this.server.serverApp, '/' + this.urlPath + '/set', async (req, res) => {\n const authData = getFromMapOrThrow(authDataByRequest, req);\n const docDataMatcherWrite = getDocAllowedMatcher(this, ensureNotFalsy(authData));\n\n let docsData: RxDocType[] = adapter.getRequestBody(req);\n\n for (const docData of docsData) {\n const allowed = docDataMatcherWrite(docData as any);\n if (!allowed) {\n adapter.closeConnection(res, 403, 'Forbidden');\n return;\n }\n }\n\n function onWriteError(err: RxError, docData: RxDocType) {\n if (err.rxdb && err.code === 'CONFLICT') {\n // just retry on conflicts\n docsData.push(docData);\n } else {\n adapter.closeConnection(res, 500, 'Internal Server Error');\n throw err;\n }\n }\n\n while (docsData.length > 0) {\n const promises: Promise<any>[] = [];\n const docs = await collection.findByIds(docsData.map(d => (d as any)[primaryPath])).exec();\n let useDocsData = docsData.slice();\n docsData = [];\n for (const docData of useDocsData) {\n const id = (docData as any)[primaryPath];\n const doc = docs.get(id);\n if (!doc) {\n promises.push(this.collection.insert(docData).catch(err => onWriteError(err, docData)));\n } else {\n const isAllowed = this.changeValidator(authData, {\n newDocumentState: removeServerOnlyFields(docData as any),\n assumedMasterState: removeServerOnlyFields(doc.toJSON(true))\n });\n if (!isAllowed) {\n adapter.closeConnection(res, 403, 'Forbidden');\n return;\n }\n promises.push(doc.patch(docData).catch(err => onWriteError(err, docData)));\n }\n }\n await Promise.all(promises);\n }\n\n adapter.setResponseHeader(res, 'Content-Type', 'application/json')\n adapter.endResponseJson(res, {});\n });\n\n this.server.adapter.post(this.server.serverApp, '/' + this.urlPath + '/delete', async (req, res) => {\n const authData = getFromMapOrThrow(authDataByRequest, req);\n const docDataMatcherWrite = getDocAllowedMatcher(this, ensureNotFalsy(authData));\n\n let ids: string[] = adapter.getRequestBody(req);\n while (ids.length > 0) {\n const useIds = ids.slice(0);\n ids = [];\n const promises: Promise<any>[] = [];\n const docsMap = await this.collection.findByIds(useIds).exec();\n for (const id of useIds) {\n const doc = docsMap.get(id);\n if (doc) {\n const isAllowedDoc = docDataMatcherWrite(doc.toJSON(true) as any);\n if (!isAllowedDoc) {\n adapter.closeConnection(res, 403, 'Forbidden');\n return;\n }\n\n const isAllowedChange = this.changeValidator(authData, {\n newDocumentState: doc.toJSON(true) as any,\n assumedMasterState: doc.toJSON(true) as any\n });\n if (!isAllowedChange) {\n adapter.closeConnection(res, 403, 'Forbidden');\n return;\n }\n\n promises.push(doc.remove().catch((err: RxError) => {\n if (err.rxdb && err.code === 'CONFLICT') {\n // just retry on conflicts\n ids.push(id);\n } else {\n adapter.closeConnection(res, 500, 'Internal Server Error');\n throw err;\n }\n }));\n }\n }\n await Promise.all(promises);\n }\n adapter.setResponseHeader(res, 'Content-Type', 'application/json');\n adapter.endResponseJson(res, {});\n });\n }\n}\n"],"mappings":"AAAA,SAIIA,mBAAmB,QAChB,mBAAmB;AAO1B,SAASC,MAAM,EAAEC,QAAQ,QAAQ,MAAM;AACvC,SACIC,cAAc,EACdC,iBAAiB,QACd,oBAAoB;AAE3B,SACIC,iBAAiB,EACjBC,yBAAyB,EACzBC,2BAA2B,EAC3BC,6BAA6B,EAC7BC,oBAAoB,EACpBC,2BAA2B,EAC3BC,OAAO,QACJ,aAAa;AAGpB,OAAO,IAAMC,UAAU,GAAG,CACtB,OAAO,EACP,eAAe,EACf,KAAK,EACL,KAAK,EACL;;AAEA;AACA;AACJ;AACA;AACA;AACA,EAJI,CAKM;AAGV,WAAaC,oBAAoB,GAK7B,SAAAA,qBACoBC,MAAyC,EACzCC,IAAY,EACZC,UAAmC,EACnDC,aAAyD,EACzDC,eAA6D,EAC7CC,gBAA0B,EAC1BC,IAAa,EAC/B;EAAA,IAAAC,KAAA;EAAA,KAZOC,IAAI,GAAG,MAAM;EAAA,KAKFR,MAAyC,GAAzCA,MAAyC;EAAA,KACzCC,IAAY,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eAAe,CAACX,GAAG,EAAE,CAAC,CAAC,CAAC;EACpC,CAAC,CAAC;AACN,CAAC","ignoreList":[]}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import expressCors from 'cors';
|
|
2
1
|
import { flatClone, getQueryMatcher, normalizeMangoQuery, uniqueArray } from 'rxdb/plugins/core';
|
|
3
2
|
export function setCors(server, path, cors) {
|
|
4
3
|
var useCors = cors;
|
|
@@ -6,11 +5,7 @@ export function setCors(server, path, cors) {
|
|
|
6
5
|
useCors = server.cors;
|
|
7
6
|
}
|
|
8
7
|
if (useCors) {
|
|
9
|
-
server.
|
|
10
|
-
origin: useCors,
|
|
11
|
-
// some legacy browsers (IE11, various SmartTVs) choke on 204
|
|
12
|
-
optionsSuccessStatus: 200
|
|
13
|
-
}));
|
|
8
|
+
server.adapter.setCors(server.serverApp, path, useCors);
|
|
14
9
|
}
|
|
15
10
|
}
|
|
16
11
|
|
|
@@ -22,8 +17,8 @@ export function blockPreviousVersionPaths(server, path, currentVersion) {
|
|
|
22
17
|
var v = 0;
|
|
23
18
|
var _loop = function () {
|
|
24
19
|
var version = v;
|
|
25
|
-
server.
|
|
26
|
-
closeConnection(res, 426, 'Outdated version ' + version + ' (newest is ' + currentVersion + ')');
|
|
20
|
+
server.adapter.all(server.serverApp, '/' + path + '/' + version + '/*', (req, res) => {
|
|
21
|
+
server.adapter.closeConnection(res, 426, 'Outdated version ' + version + ' (newest is ' + currentVersion + ')');
|
|
27
22
|
});
|
|
28
23
|
v++;
|
|
29
24
|
};
|
|
@@ -31,17 +26,6 @@ export function blockPreviousVersionPaths(server, path, currentVersion) {
|
|
|
31
26
|
_loop();
|
|
32
27
|
}
|
|
33
28
|
}
|
|
34
|
-
export async function closeConnection(response, code, message) {
|
|
35
|
-
var responseWrite = {
|
|
36
|
-
code,
|
|
37
|
-
error: true,
|
|
38
|
-
message
|
|
39
|
-
};
|
|
40
|
-
response.statusCode = code;
|
|
41
|
-
response.set("Connection", "close");
|
|
42
|
-
await response.write(JSON.stringify(responseWrite));
|
|
43
|
-
response.end();
|
|
44
|
-
}
|
|
45
29
|
export function addAuthMiddleware(server, path) {
|
|
46
30
|
var authDataByRequest = new WeakMap();
|
|
47
31
|
async function auth(req, res, next) {
|
|
@@ -50,13 +34,11 @@ export function addAuthMiddleware(server, path) {
|
|
|
50
34
|
authDataByRequest.set(req, authData);
|
|
51
35
|
next();
|
|
52
36
|
} catch (err) {
|
|
53
|
-
closeConnection(res, 401, 'Unauthorized');
|
|
37
|
+
server.adapter.closeConnection(res, 401, 'Unauthorized');
|
|
54
38
|
return;
|
|
55
39
|
}
|
|
56
40
|
}
|
|
57
|
-
server.
|
|
58
|
-
next();
|
|
59
|
-
});
|
|
41
|
+
server.adapter.all(server.serverApp, '/' + path + '/*', auth);
|
|
60
42
|
return authDataByRequest;
|
|
61
43
|
}
|
|
62
44
|
var defaultMatchingQuery = {
|
|
@@ -69,24 +51,6 @@ export function getDocAllowedMatcher(endpoint, authData) {
|
|
|
69
51
|
var docDataMatcher = getQueryMatcher(endpoint.collection.schema.jsonSchema, useQuery);
|
|
70
52
|
return docDataMatcher;
|
|
71
53
|
}
|
|
72
|
-
export function writeSSEHeaders(res) {
|
|
73
|
-
res.writeHead(200, {
|
|
74
|
-
/**
|
|
75
|
-
* Use exact these headers to make is less likely
|
|
76
|
-
* for people to have problems.
|
|
77
|
-
* @link https://www.youtube.com/watch?v=0PcMuYGJPzM
|
|
78
|
-
*/
|
|
79
|
-
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
80
|
-
'Connection': 'keep-alive',
|
|
81
|
-
'Cache-Control': 'no-cache',
|
|
82
|
-
/**
|
|
83
|
-
* Required for nginx
|
|
84
|
-
* @link https://stackoverflow.com/q/61029079/3443137
|
|
85
|
-
*/
|
|
86
|
-
'X-Accel-Buffering': 'no'
|
|
87
|
-
});
|
|
88
|
-
res.flushHeaders();
|
|
89
|
-
}
|
|
90
54
|
export function docContainsServerOnlyFields(serverOnlyFields, doc) {
|
|
91
55
|
var has = serverOnlyFields.find(field => {
|
|
92
56
|
return typeof doc[field] !== 'undefined';
|
|
@@ -126,7 +90,7 @@ export function mergeServerDocumentFieldsMonad(serverOnlyFields) {
|
|
|
126
90
|
}
|
|
127
91
|
|
|
128
92
|
/**
|
|
129
|
-
* $regex queries are dangerous because they can dos-
|
|
93
|
+
* $regex queries are dangerous because they can dos-attack the server.
|
|
130
94
|
*
|
|
131
95
|
* @param selector
|
|
132
96
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.js","names":["expressCors","flatClone","getQueryMatcher","normalizeMangoQuery","uniqueArray","setCors","server","path","cors","useCors","expressApp","options","origin","optionsSuccessStatus","blockPreviousVersionPaths","currentVersion","v","_loop","version","all","req","res","closeConnection","response","code","message","responseWrite","error","statusCode","set","write","JSON","stringify","end","addAuthMiddleware","authDataByRequest","WeakMap","auth","next","authData","authHandler","headers","err","defaultMatchingQuery","selector","skip","sort","getDocAllowedMatcher","endpoint","useQuery","queryModifier","collection","schema","jsonSchema","docDataMatcher","writeSSEHeaders","writeHead","flushHeaders","docContainsServerOnlyFields","serverOnlyFields","doc","has","find","field","removeServerOnlyFieldsMonad","serverOnlyFieldsStencil","_meta","undefined","_rev","_attachments","forEach","docData","Object","assign","mergeServerDocumentFieldsMonad","useFields","slice","clientDoc","serverDoc","ret","doesContainRegexQuerySelector","Array","isArray","found","item","entries","key","value"],"sources":["../../../../src/plugins/server/helper.ts"],"sourcesContent":["import { RxServer } from './rx-server';\nimport expressCors from 'cors';\nimport type {\n Request,\n Response,\n NextFunction\n} from 'express';\nimport { RxServerAuthData, RxServerEndpoint } from './types';\nimport {\n FilledMangoQuery,\n MangoQuerySelector,\n RxDocumentData,\n RxReplicationWriteToMasterRow,\n flatClone,\n getQueryMatcher,\n normalizeMangoQuery,\n uniqueArray\n} from 'rxdb/plugins/core';\n\nexport function setCors(\n server: RxServer<any>,\n path: string,\n cors?: string\n) {\n let useCors = cors;\n if (!useCors) {\n useCors = server.cors;\n }\n if (useCors) {\n server.expressApp.options('/' + path + '/*', expressCors({\n origin: useCors,\n // some legacy browsers (IE11, various SmartTVs) choke on 204\n optionsSuccessStatus: 200\n }));\n }\n}\n\n/**\n * \"block\" the previous version urls and send a 426 on them so that\n * the clients know they must update.\n */\nexport function blockPreviousVersionPaths(\n server: RxServer<any>,\n path: string,\n currentVersion: number\n\n) {\n let v = 0;\n while (v < currentVersion) {\n const version = v;\n server.expressApp.all('/' + path + '/' + version + '/*', (req, res) => {\n closeConnection(res, 426, 'Outdated version ' + version + ' (newest is ' + currentVersion + ')');\n });\n v++;\n }\n}\n\n\nexport async function closeConnection(response: Response, code: number, message: string) {\n const responseWrite = {\n code,\n error: true,\n message\n };\n response.statusCode = code;\n response.set(\"Connection\", \"close\");\n await response.write(JSON.stringify(responseWrite));\n response.end();\n}\n\n\nexport function addAuthMiddleware<AuthType>(\n server: RxServer<AuthType>,\n path: string,\n): WeakMap<Request, RxServerAuthData<AuthType>> {\n const authDataByRequest = new WeakMap<Request, RxServerAuthData<AuthType>>();\n async function auth(req: Request, res: Response, next: NextFunction) {\n try {\n const authData = await server.authHandler(req.headers);\n authDataByRequest.set(req, authData);\n next();\n } catch (err) {\n closeConnection(res, 401, 'Unauthorized');\n return;\n }\n }\n server.expressApp.all('/' + path + '/*', auth, function (req, res, next) {\n next();\n });\n return authDataByRequest;\n}\n\nconst defaultMatchingQuery: FilledMangoQuery<any> = {\n selector: {},\n skip: 0,\n sort: []\n} as const;\n\nexport function getDocAllowedMatcher<RxDocType, AuthType>(\n endpoint: RxServerEndpoint<AuthType, RxDocType>,\n authData: RxServerAuthData<AuthType>\n) {\n const useQuery: FilledMangoQuery<RxDocType> = endpoint.queryModifier ? endpoint.queryModifier(\n authData,\n normalizeMangoQuery(\n endpoint.collection.schema.jsonSchema,\n {}\n )\n ) : defaultMatchingQuery;\n const docDataMatcher = getQueryMatcher(endpoint.collection.schema.jsonSchema, useQuery);\n return docDataMatcher;\n}\n\nexport function writeSSEHeaders(res: Response) {\n res.writeHead(200, {\n /**\n * Use exact these headers to make is less likely\n * for people to have problems.\n * @link https://www.youtube.com/watch?v=0PcMuYGJPzM\n */\n 'Content-Type': 'text/event-stream; charset=utf-8',\n 'Connection': 'keep-alive',\n 'Cache-Control': 'no-cache',\n /**\n * Required for nginx\n * @link https://stackoverflow.com/q/61029079/3443137\n */\n 'X-Accel-Buffering': 'no'\n });\n res.flushHeaders();\n}\n\nexport function docContainsServerOnlyFields(\n serverOnlyFields: string[],\n doc: any\n) {\n const has = serverOnlyFields.find(field => {\n return typeof doc[field] !== 'undefined'\n });\n return has;\n}\n\nexport function removeServerOnlyFieldsMonad<RxDocType>(serverOnlyFields: string[]) {\n const serverOnlyFieldsStencil: any = {\n _meta: undefined,\n _rev: undefined,\n _attachments: undefined\n };\n serverOnlyFields.forEach(field => serverOnlyFieldsStencil[field] = undefined);\n return (\n docData?: RxDocType | RxDocumentData<RxDocType>\n ) => {\n if (!docData) {\n return docData;\n }\n return Object.assign({}, docData, serverOnlyFieldsStencil);\n }\n}\n\nexport function mergeServerDocumentFieldsMonad<RxDocType>(serverOnlyFields: string[]) {\n let useFields = serverOnlyFields.slice(0);\n // useFields.push('_rev');\n // useFields.push('_meta');\n // useFields.push('_attachments');\n useFields = uniqueArray(useFields);\n\n return (\n clientDoc: RxDocType | RxDocumentData<RxDocType>,\n serverDoc?: RxDocType | RxDocumentData<RxDocType>\n ) => {\n if (!serverDoc) {\n return clientDoc;\n }\n const ret = flatClone(clientDoc);\n useFields.forEach(field => {\n (ret as any)[field] = (serverDoc as any)[field];\n });\n return ret;\n }\n}\n\n\n/**\n * $regex queries are dangerous because they can dos-attach the \n * \n * @param selector \n */\nexport function doesContainRegexQuerySelector(selector: MangoQuerySelector<any> | any): boolean {\n if (!selector) {\n return false;\n }\n if (Array.isArray(selector)) {\n const found = !!selector.find(item => doesContainRegexQuerySelector(item));\n return found;\n }\n\n if (typeof selector !== 'object') {\n return false;\n }\n\n const entries = Object.entries(selector);\n for (const [key, value] of entries) {\n if (key === '$regex') {\n return true;\n } else {\n const has = doesContainRegexQuerySelector(value);\n if (has) {\n return true;\n }\n }\n }\n\n return false;\n}\n"],"mappings":"AACA,OAAOA,WAAW,MAAM,MAAM;AAO9B,SAKIC,SAAS,EACTC,eAAe,EACfC,mBAAmB,EACnBC,WAAW,QACR,mBAAmB;AAE1B,OAAO,SAASC,OAAOA,CACnBC,MAAqB,EACrBC,IAAY,EACZC,IAAa,EACf;EACE,IAAIC,OAAO,GAAGD,IAAI;EAClB,IAAI,CAACC,OAAO,EAAE;IACVA,OAAO,GAAGH,MAAM,CAACE,IAAI;EACzB;EACA,IAAIC,OAAO,EAAE;IACTH,MAAM,CAACI,UAAU,CAACC,OAAO,CAAC,GAAG,GAAGJ,IAAI,GAAG,IAAI,EAAEP,WAAW,CAAC;MACrDY,MAAM,EAAEH,OAAO;MACf;MACAI,oBAAoB,EAAE;IAC1B,CAAC,CAAC,CAAC;EACP;AACJ;;AAEA;AACA;AACA;AACA;AACA,OAAO,SAASC,yBAAyBA,CACrCR,MAAqB,EACrBC,IAAY,EACZQ,cAAsB,EAExB;EACE,IAAIC,CAAC,GAAG,CAAC;EAAC,IAAAC,KAAA,YAAAA,CAAA,EACiB;IACvB,IAAMC,OAAO,GAAGF,CAAC;IACjBV,MAAM,CAACI,UAAU,CAACS,GAAG,CAAC,GAAG,GAAGZ,IAAI,GAAG,GAAG,GAAGW,OAAO,GAAG,IAAI,EAAE,CAACE,GAAG,EAAEC,GAAG,KAAK;MACnEC,eAAe,CAACD,GAAG,EAAE,GAAG,EAAE,mBAAmB,GAAGH,OAAO,GAAG,cAAc,GAAGH,cAAc,GAAG,GAAG,CAAC;IACpG,CAAC,CAAC;IACFC,CAAC,EAAE;EACP,CAAC;EAND,OAAOA,CAAC,GAAGD,cAAc;IAAAE,KAAA;EAAA;AAO7B;AAGA,OAAO,eAAeK,eAAeA,CAACC,QAAkB,EAAEC,IAAY,EAAEC,OAAe,EAAE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;AAChB"}
|
|
1
|
+
{"version":3,"file":"helper.js","names":["flatClone","getQueryMatcher","normalizeMangoQuery","uniqueArray","setCors","server","path","cors","useCors","adapter","serverApp","blockPreviousVersionPaths","currentVersion","v","_loop","version","all","req","res","closeConnection","addAuthMiddleware","authDataByRequest","WeakMap","auth","next","authData","authHandler","headers","set","err","defaultMatchingQuery","selector","skip","sort","getDocAllowedMatcher","endpoint","useQuery","queryModifier","collection","schema","jsonSchema","docDataMatcher","docContainsServerOnlyFields","serverOnlyFields","doc","has","find","field","removeServerOnlyFieldsMonad","serverOnlyFieldsStencil","_meta","undefined","_rev","_attachments","forEach","docData","Object","assign","mergeServerDocumentFieldsMonad","useFields","slice","clientDoc","serverDoc","ret","doesContainRegexQuerySelector","Array","isArray","found","item","entries","key","value"],"sources":["../../../../src/plugins/server/helper.ts"],"sourcesContent":["import { RxServer } from './rx-server';\nimport type {\n Request,\n Response,\n NextFunction\n} from 'express';\nimport type { RxServerAdapter, RxServerAuthData, RxServerEndpoint } from './types';\nimport {\n FilledMangoQuery,\n MangoQuerySelector,\n RxDocumentData,\n flatClone,\n getQueryMatcher,\n normalizeMangoQuery,\n uniqueArray\n} from 'rxdb/plugins/core';\n\nexport function setCors(\n server: RxServer<any, any>,\n path: string,\n cors?: string\n) {\n let useCors = cors;\n if (!useCors) {\n useCors = server.cors;\n }\n if (useCors) {\n server.adapter.setCors(server.serverApp, path, useCors);\n }\n}\n\n/**\n * \"block\" the previous version urls and send a 426 on them so that\n * the clients know they must update.\n */\nexport function blockPreviousVersionPaths(\n server: RxServer<any, any>,\n path: string,\n currentVersion: number\n\n) {\n let v = 0;\n while (v < currentVersion) {\n const version = v;\n server.adapter.all(server.serverApp, '/' + path + '/' + version + '/*', (req, res) => {\n server.adapter.closeConnection(res, 426, 'Outdated version ' + version + ' (newest is ' + currentVersion + ')');\n });\n v++;\n }\n}\n\n\nexport function addAuthMiddleware<AuthType>(\n server: RxServer<any, AuthType>,\n path: string,\n): WeakMap<Request, RxServerAuthData<AuthType>> {\n const authDataByRequest = new WeakMap<Request, RxServerAuthData<AuthType>>();\n async function auth(req: any, res: any, next: NextFunction) {\n try {\n const authData = await server.authHandler(req.headers);\n authDataByRequest.set(req, authData);\n next();\n } catch (err) {\n server.adapter.closeConnection(res, 401, 'Unauthorized');\n return;\n }\n }\n server.adapter.all(server.serverApp, '/' + path + '/*', auth);\n return authDataByRequest;\n}\n\nconst defaultMatchingQuery: FilledMangoQuery<any> = {\n selector: {},\n skip: 0,\n sort: []\n} as const;\n\nexport function getDocAllowedMatcher<RxDocType, AuthType>(\n endpoint: RxServerEndpoint<AuthType, RxDocType>,\n authData: RxServerAuthData<AuthType>\n) {\n const useQuery: FilledMangoQuery<RxDocType> = endpoint.queryModifier ? endpoint.queryModifier(\n authData,\n normalizeMangoQuery(\n endpoint.collection.schema.jsonSchema,\n {}\n )\n ) : defaultMatchingQuery;\n const docDataMatcher = getQueryMatcher(endpoint.collection.schema.jsonSchema, useQuery);\n return docDataMatcher;\n}\n\n\nexport function docContainsServerOnlyFields(\n serverOnlyFields: string[],\n doc: any\n) {\n const has = serverOnlyFields.find(field => {\n return typeof doc[field] !== 'undefined'\n });\n return has;\n}\n\nexport function removeServerOnlyFieldsMonad<RxDocType>(serverOnlyFields: string[]) {\n const serverOnlyFieldsStencil: any = {\n _meta: undefined,\n _rev: undefined,\n _attachments: undefined\n };\n serverOnlyFields.forEach(field => serverOnlyFieldsStencil[field] = undefined);\n return (\n docData?: RxDocType | RxDocumentData<RxDocType>\n ) => {\n if (!docData) {\n return docData;\n }\n return Object.assign({}, docData, serverOnlyFieldsStencil);\n }\n}\n\nexport function mergeServerDocumentFieldsMonad<RxDocType>(serverOnlyFields: string[]) {\n let useFields = serverOnlyFields.slice(0);\n // useFields.push('_rev');\n // useFields.push('_meta');\n // useFields.push('_attachments');\n useFields = uniqueArray(useFields);\n\n return (\n clientDoc: RxDocType | RxDocumentData<RxDocType>,\n serverDoc?: RxDocType | RxDocumentData<RxDocType>\n ) => {\n if (!serverDoc) {\n return clientDoc;\n }\n const ret = flatClone(clientDoc);\n useFields.forEach(field => {\n (ret as any)[field] = (serverDoc as any)[field];\n });\n return ret;\n }\n}\n\n\n/**\n * $regex queries are dangerous because they can dos-attack the server.\n * \n * @param selector \n */\nexport function doesContainRegexQuerySelector(selector: MangoQuerySelector<any> | any): boolean {\n if (!selector) {\n return false;\n }\n if (Array.isArray(selector)) {\n const found = !!selector.find(item => doesContainRegexQuerySelector(item));\n return found;\n }\n\n if (typeof selector !== 'object') {\n return false;\n }\n\n const entries = Object.entries(selector);\n for (const [key, value] of entries) {\n if (key === '$regex') {\n return true;\n } else {\n const has = doesContainRegexQuerySelector(value);\n if (has) {\n return true;\n }\n }\n }\n\n return false;\n}\n"],"mappings":"AAOA,SAIIA,SAAS,EACTC,eAAe,EACfC,mBAAmB,EACnBC,WAAW,QACR,mBAAmB;AAE1B,OAAO,SAASC,OAAOA,CACnBC,MAA0B,EAC1BC,IAAY,EACZC,IAAa,EACf;EACE,IAAIC,OAAO,GAAGD,IAAI;EAClB,IAAI,CAACC,OAAO,EAAE;IACVA,OAAO,GAAGH,MAAM,CAACE,IAAI;EACzB;EACA,IAAIC,OAAO,EAAE;IACTH,MAAM,CAACI,OAAO,CAACL,OAAO,CAACC,MAAM,CAACK,SAAS,EAAEJ,IAAI,EAAEE,OAAO,CAAC;EAC3D;AACJ;;AAEA;AACA;AACA;AACA;AACA,OAAO,SAASG,yBAAyBA,CACrCN,MAA0B,EAC1BC,IAAY,EACZM,cAAsB,EAExB;EACE,IAAIC,CAAC,GAAG,CAAC;EAAC,IAAAC,KAAA,YAAAA,CAAA,EACiB;IACvB,IAAMC,OAAO,GAAGF,CAAC;IACjBR,MAAM,CAACI,OAAO,CAACO,GAAG,CAACX,MAAM,CAACK,SAAS,EAAE,GAAG,GAAGJ,IAAI,GAAG,GAAG,GAAGS,OAAO,GAAG,IAAI,EAAE,CAACE,GAAG,EAAEC,GAAG,KAAK;MAClFb,MAAM,CA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ASf,KAAK,CAAC;IACnD,CAAC,CAAC;IACF,OAAOgB,GAAG;EACd,CAAC;AACL;;AAGA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,6BAA6BA,CAACjC,QAAuC,EAAW;EAC5F,IAAI,CAACA,QAAQ,EAAE;IACX,OAAO,KAAK;EAChB;EACA,IAAIkC,KAAK,CAACC,OAAO,CAACnC,QAAQ,CAAC,EAAE;IACzB,IAAMoC,KAAK,GAAG,CAAC,CAACpC,QAAQ,CAACe,IAAI,CAACsB,IAAI,IAAIJ,6BAA6B,CAACI,IAAI,CAAC,CAAC;IAC1E,OAAOD,KAAK;EAChB;EAEA,IAAI,OAAOpC,QAAQ,KAAK,QAAQ,EAAE;IAC9B,OAAO,KAAK;EAChB;EAEA,IAAMsC,OAAO,GAAGb,MAAM,CAACa,OAAO,CAACtC,QAAQ,CAAC;EACxC,KAAK,IAAM,CAACuC,GAAG,EAAEC,KAAK,CAAC,IAAIF,OAAO,EAAE;IAChC,IAAIC,GAAG,KAAK,QAAQ,EAAE;MAClB,OAAO,IAAI;IACf,CAAC,MAAM;MACH,IAAMzB,GAAG,GAAGmB,6BAA6B,CAACO,KAAK,CAAC;MAChD,IAAI1B,GAAG,EAAE;QACL,OAAO,IAAI;MACf;IACJ;EACJ;EAEA,OAAO,KAAK;AAChB","ignoreList":[]}
|
|
@@ -1,30 +1,21 @@
|
|
|
1
1
|
import { ensureNotFalsy, flatClone } from 'rxdb/plugins/utils';
|
|
2
2
|
import { RxServer } from "./rx-server.js";
|
|
3
|
-
import express from 'express';
|
|
4
3
|
export * from "./types.js";
|
|
5
4
|
export * from "./endpoint-replication.js";
|
|
6
5
|
export * from "./endpoint-rest.js";
|
|
7
6
|
export * from "./helper.js";
|
|
8
|
-
export async function
|
|
7
|
+
export async function createRxServer(options) {
|
|
9
8
|
options = flatClone(options);
|
|
10
9
|
if (!options.serverApp) {
|
|
11
|
-
|
|
12
|
-
options.serverApp = app;
|
|
10
|
+
options.serverApp = await options.adapter.create();
|
|
13
11
|
}
|
|
14
|
-
options.serverApp.use(express.json());
|
|
15
|
-
var httpServer = await new Promise((res, rej) => {
|
|
16
|
-
var hostname = options.hostname ? options.hostname : 'localhost';
|
|
17
|
-
var ret = ensureNotFalsy(options.serverApp).listen(options.port, hostname, () => {
|
|
18
|
-
res(ret);
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
12
|
var authHandler = options.authHandler ? options.authHandler : () => {
|
|
22
13
|
return {
|
|
23
14
|
data: {},
|
|
24
15
|
validUntil: Date.now() * 2
|
|
25
16
|
};
|
|
26
17
|
};
|
|
27
|
-
var server = new RxServer(options
|
|
18
|
+
var server = new RxServer(options, authHandler, ensureNotFalsy(options.serverApp), options.cors);
|
|
28
19
|
return server;
|
|
29
20
|
}
|
|
30
21
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["ensureNotFalsy","flatClone","RxServer","
|
|
1
|
+
{"version":3,"file":"index.js","names":["ensureNotFalsy","flatClone","RxServer","createRxServer","options","serverApp","adapter","create","authHandler","data","validUntil","Date","now","server","cors"],"sources":["../../../../src/plugins/server/index.ts"],"sourcesContent":["import { ensureNotFalsy, flatClone } from 'rxdb/plugins/utils';\nimport { RxServer } from './rx-server.ts';\nimport { RxServerAuthHandler, RxServerOptions } from './types.ts';\n\nexport * from './types.ts';\nexport * from './endpoint-replication.ts';\nexport * from './endpoint-rest.ts';\nexport * from './helper.ts';\n\nexport async function createRxServer<ServerAdapterType, AuthType>(\n options: RxServerOptions<ServerAdapterType, AuthType>\n): Promise<RxServer<ServerAdapterType, AuthType>> {\n options = flatClone(options);\n if (!options.serverApp) {\n options.serverApp = await options.adapter.create();\n }\n const authHandler: RxServerAuthHandler<AuthType> = options.authHandler ? options.authHandler : () => {\n return {\n data: {} as any,\n validUntil: Date.now() * 2\n };\n };\n\n const server = new RxServer<ServerAdapterType, AuthType>(\n options,\n authHandler,\n ensureNotFalsy(options.serverApp),\n options.cors\n );\n\n return server;\n}\n"],"mappings":"AAAA,SAASA,cAAc,EAAEC,SAAS,QAAQ,oBAAoB;AAC9D,SAASC,QAAQ,QAAQ,gBAAgB;AAGzC,cAAc,YAAY;AAC1B,cAAc,2BAA2B;AACzC,cAAc,oBAAoB;AAClC,cAAc,aAAa;AAE3B,OAAO,eAAeC,cAAcA,CAChCC,OAAqD,EACP;EAC9CA,OAAO,GAAGH,SAAS,CAACG,OAAO,CAAC;EAC5B,IAAI,CAACA,OAAO,CAACC,SAAS,EAAE;IACpBD,OAAO,CAACC,SAAS,GAAG,MAAMD,OAAO,CAACE,OAAO,CAACC,MAAM,CAAC,CAAC;EACtD;EACA,IAAMC,WAA0C,GAAGJ,OAAO,CAACI,WAAW,GAAGJ,OAAO,CAACI,WAAW,GAAG,MAAM;IACjG,OAAO;MACHC,IAAI,EAAE,CAAC,CAAQ;MACfC,UAAU,EAAEC,IAAI,CAACC,GAAG,CAAC,CAAC,GAAG;IAC7B,CAAC;EACL,CAAC;EAED,IAAMC,MAAM,GAAG,IAAIX,QAAQ,CACvBE,OAAO,EACPI,WAAW,EACXR,cAAc,CAACI,OAAO,CAACC,SAAS,CAAC,EACjCD,OAAO,CAACU,IACZ,CAAC;EAED,OAAOD,MAAM;AACjB","ignoreList":[]}
|
|
@@ -1,44 +1,47 @@
|
|
|
1
1
|
import { RxServerReplicationEndpoint } from "./endpoint-replication.js";
|
|
2
2
|
import { RxServerRestEndpoint } from "./endpoint-rest.js";
|
|
3
3
|
export var RxServer = /*#__PURE__*/function () {
|
|
4
|
-
function RxServer(
|
|
4
|
+
function RxServer(options, authHandler, serverApp, cors = '*') {
|
|
5
5
|
this.endpoints = [];
|
|
6
6
|
this.closeFn = (() => this.close()).bind(this);
|
|
7
|
-
this.
|
|
7
|
+
this.options = options;
|
|
8
8
|
this.authHandler = authHandler;
|
|
9
|
-
this.
|
|
10
|
-
this.expressApp = expressApp;
|
|
9
|
+
this.serverApp = serverApp;
|
|
11
10
|
this.cors = cors;
|
|
12
|
-
database.
|
|
11
|
+
this.database = options.database;
|
|
12
|
+
this.adapter = options.adapter;
|
|
13
|
+
options.database.onDestroy.push(this.closeFn);
|
|
13
14
|
}
|
|
14
15
|
var _proto = RxServer.prototype;
|
|
15
|
-
_proto.
|
|
16
|
+
_proto.ensureNotStarted = function ensureNotStarted() {
|
|
17
|
+
if (this.listenPromise) {
|
|
18
|
+
throw new Error('This operation cannot be run after the RxServer has been started already');
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
_proto.addReplicationEndpoint = function addReplicationEndpoint(opts) {
|
|
22
|
+
this.ensureNotStarted();
|
|
16
23
|
var endpoint = new RxServerReplicationEndpoint(this, opts.name, opts.collection, opts.queryModifier ? opts.queryModifier : (_a, q) => q, opts.changeValidator ? opts.changeValidator : () => true, opts.serverOnlyFields ? opts.serverOnlyFields : [], opts.cors);
|
|
17
24
|
this.endpoints.push(endpoint);
|
|
18
25
|
return endpoint;
|
|
19
26
|
};
|
|
20
|
-
_proto.addRestEndpoint =
|
|
27
|
+
_proto.addRestEndpoint = function addRestEndpoint(opts) {
|
|
28
|
+
this.ensureNotStarted();
|
|
21
29
|
var endpoint = new RxServerRestEndpoint(this, opts.name, opts.collection, opts.queryModifier ? opts.queryModifier : (_a, q) => q, opts.changeValidator ? opts.changeValidator : () => true, opts.serverOnlyFields ? opts.serverOnlyFields : [], opts.cors);
|
|
22
30
|
this.endpoints.push(endpoint);
|
|
23
31
|
return endpoint;
|
|
24
32
|
};
|
|
33
|
+
_proto.start = async function start() {
|
|
34
|
+
this.ensureNotStarted();
|
|
35
|
+
var hostname = this.options.hostname ? this.options.hostname : 'localhost';
|
|
36
|
+
this.listenPromise = this.options.adapter.listen(this.serverApp, this.options.port, hostname);
|
|
37
|
+
return this.listenPromise;
|
|
38
|
+
};
|
|
25
39
|
_proto.close = async function close() {
|
|
26
40
|
this.database.onDestroy = this.database.onDestroy.filter(fn => fn !== this.closeFn);
|
|
27
|
-
|
|
28
|
-
this.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
} else {
|
|
32
|
-
res();
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
/**
|
|
36
|
-
* By default it will await all ongoing connections
|
|
37
|
-
* before it closes. So we have to close it directly.
|
|
38
|
-
* @link https://stackoverflow.com/a/36830072/3443137
|
|
39
|
-
*/
|
|
40
|
-
setImmediate(() => this.httpServer.emit('close'));
|
|
41
|
-
});
|
|
41
|
+
if (this.listenPromise) {
|
|
42
|
+
await this.listenPromise;
|
|
43
|
+
await this.options.adapter.close(this.serverApp);
|
|
44
|
+
}
|
|
42
45
|
};
|
|
43
46
|
return RxServer;
|
|
44
47
|
}();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rx-server.js","names":["RxServerReplicationEndpoint","RxServerRestEndpoint","RxServer","
|
|
1
|
+
{"version":3,"file":"rx-server.js","names":["RxServerReplicationEndpoint","RxServerRestEndpoint","RxServer","options","authHandler","serverApp","cors","endpoints","closeFn","close","bind","database","adapter","onDestroy","push","_proto","prototype","ensureNotStarted","listenPromise","Error","addReplicationEndpoint","opts","endpoint","name","collection","queryModifier","_a","q","changeValidator","serverOnlyFields","addRestEndpoint","start","hostname","listen","port","filter","fn"],"sources":["../../../../src/plugins/server/rx-server.ts"],"sourcesContent":["import type {\n RxCollection,\n RxDatabase\n} from 'rxdb/plugins/core';\nimport { RxServerReplicationEndpoint } from './endpoint-replication.ts';\nimport type {\n RxServerAdapter,\n RxServerAuthHandler,\n RxServerChangeValidator,\n RxServerEndpoint,\n RxServerOptions,\n RxServerQueryModifier\n} from './types.ts';\nimport { RxServerRestEndpoint } from './endpoint-rest.ts';\n\nexport class RxServer<ServerAppType, AuthType> {\n public readonly endpoints: RxServerEndpoint<AuthType, any>[] = [];\n\n private closeFn = (() => this.close()).bind(this);\n public listenPromise?: Promise<void>;\n\n public readonly database: RxDatabase;\n public readonly adapter: RxServerAdapter<ServerAppType>;\n\n constructor(\n public readonly options: RxServerOptions<ServerAppType, AuthType>,\n public readonly authHandler: RxServerAuthHandler<AuthType>,\n public readonly serverApp: ServerAppType,\n public readonly cors: string = '*'\n ) {\n this.database = options.database;\n this.adapter = options.adapter;\n options.database.onDestroy.push(this.closeFn);\n }\n\n private ensureNotStarted() {\n if (this.listenPromise) {\n throw new Error('This operation cannot be run after the RxServer has been started already');\n }\n\n }\n\n public addReplicationEndpoint<RxDocType>(opts: {\n name: string,\n collection: RxCollection<RxDocType>,\n queryModifier?: RxServerQueryModifier<AuthType, RxDocType>,\n changeValidator?: RxServerChangeValidator<AuthType, RxDocType>,\n /**\n * Set a origin for allowed CORS requests.\n * Overwrites the cors option of the server.\n * [default='*']\n */\n cors?: '*' | string,\n serverOnlyFields?: string[]\n }) {\n this.ensureNotStarted();\n const endpoint = new RxServerReplicationEndpoint(\n this,\n opts.name,\n opts.collection,\n opts.queryModifier ? opts.queryModifier : (_a, q) => q,\n opts.changeValidator ? opts.changeValidator : () => true,\n opts.serverOnlyFields ? opts.serverOnlyFields : [],\n opts.cors\n );\n this.endpoints.push(endpoint);\n return endpoint;\n }\n\n public addRestEndpoint<RxDocType>(opts: {\n name: string,\n collection: RxCollection<RxDocType>,\n queryModifier?: RxServerQueryModifier<AuthType, RxDocType>,\n changeValidator?: RxServerChangeValidator<AuthType, RxDocType>,\n /**\n * Set a origin for allowed CORS requests.\n * Overwrites the cors option of the server.\n * [default='*']\n */\n cors?: '*' | string,\n serverOnlyFields?: string[]\n }) {\n this.ensureNotStarted();\n const endpoint = new RxServerRestEndpoint(\n this,\n opts.name,\n opts.collection,\n opts.queryModifier ? opts.queryModifier : (_a, q) => q,\n opts.changeValidator ? opts.changeValidator : () => true,\n opts.serverOnlyFields ? opts.serverOnlyFields : [],\n opts.cors\n );\n this.endpoints.push(endpoint);\n return endpoint;\n }\n\n async start() {\n this.ensureNotStarted();\n const hostname = this.options.hostname ? this.options.hostname : 'localhost';\n this.listenPromise = this.options.adapter.listen(this.serverApp, this.options.port, hostname);\n return this.listenPromise;\n }\n\n async close() {\n this.database.onDestroy = this.database.onDestroy.filter(fn => fn !== this.closeFn);\n if (this.listenPromise) {\n await this.listenPromise;\n await this.options.adapter.close(this.serverApp);\n }\n }\n}\n"],"mappings":"AAIA,SAASA,2BAA2B,QAAQ,2BAA2B;AASvE,SAASC,oBAAoB,QAAQ,oBAAoB;AAEzD,WAAaC,QAAQ;EASjB,SAAAA,SACoBC,OAAiD,EACjDC,WAA0C,EAC1CC,SAAwB,EACxBC,IAAY,GAAG,GAAG,EACpC;IAAA,KAbcC,SAAS,GAAsC,EAAE;IAAA,KAEzDC,OAAO,GAAG,CAAC,MAAM,IAAI,CAACC,KAAK,CAAC,CAAC,EAAEC,IAAI,CAAC,IAAI,CAAC;IAAA,KAO7BP,OAAiD,GAAjDA,OAAiD;IAAA,KACjDC,WAA0C,GAA1CA,WAA0C;IAAA,KAC1CC,SAAwB,GAAxBA,SAAwB;IAAA,KACxBC,IAAY,GAAZA,IAAY;IAE5B,IAAI,CAACK,QAAQ,GAAGR,OAAO,CAACQ,QAAQ;IAChC,IAAI,CAACC,OAAO,GAAGT,OAAO,CAACS,OAAO;IAC9BT,OAAO,CAACQ,QAAQ,CAACE,SAAS,CAACC,IAAI,CAAC,IAAI,CAACN,OAAO,CAAC;EACjD;EAAC,IAAAO,MAAA,GAAAb,QAAA,CAAAc,SAAA;EAAAD,MAAA,CAEOE,gBAAgB,GAAxB,SAAAA,iBAAA,EAA2B;IACvB,IAAI,IAAI,CAACC,aAAa,EAAE;MACpB,MAAM,IAAIC,KAAK,CAAC,0EAA0E,CAAC;IAC/F;EAEJ,CAAC;EAAAJ,MAAA,CAEMK,sBAAsB,GAA7B,SAAAA,uBAAyCC,IAYxC,EAAE;IACC,IAAI,CAACJ,gBAAgB,CAAC,CAAC;IACvB,IAAMK,QAAQ,GAAG,IAAItB,2BAA2B,CAC5C,IAAI,EACJqB,IAAI,CAACE,IAAI,EACTF,IAAI,CAACG,UAAU,EACfH,IAAI,CAACI,aAAa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