rxdb-server 15.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/LICENSE.txt +555 -0
  2. package/README.md +7 -0
  3. package/dist/cjs/index.js +10 -0
  4. package/dist/cjs/index.js.map +1 -0
  5. package/dist/cjs/plugins/client-rest/index.js +86 -0
  6. package/dist/cjs/plugins/client-rest/index.js.map +1 -0
  7. package/dist/cjs/plugins/client-rest/utils.js +19 -0
  8. package/dist/cjs/plugins/client-rest/utils.js.map +1 -0
  9. package/dist/cjs/plugins/replication-server/helpers.js +38 -0
  10. package/dist/cjs/plugins/replication-server/helpers.js.map +1 -0
  11. package/dist/cjs/plugins/replication-server/index.js +169 -0
  12. package/dist/cjs/plugins/replication-server/index.js.map +1 -0
  13. package/dist/cjs/plugins/replication-server/types.js +2 -0
  14. package/dist/cjs/plugins/replication-server/types.js.map +1 -0
  15. package/dist/cjs/plugins/server/endpoint-replication.js +162 -0
  16. package/dist/cjs/plugins/server/endpoint-replication.js.map +1 -0
  17. package/dist/cjs/plugins/server/endpoint-rest.js +219 -0
  18. package/dist/cjs/plugins/server/endpoint-rest.js.map +1 -0
  19. package/dist/cjs/plugins/server/helper.js +173 -0
  20. package/dist/cjs/plugins/server/helper.js.map +1 -0
  21. package/dist/cjs/plugins/server/index.js +84 -0
  22. package/dist/cjs/plugins/server/index.js.map +1 -0
  23. package/dist/cjs/plugins/server/rx-server.js +51 -0
  24. package/dist/cjs/plugins/server/rx-server.js.map +1 -0
  25. package/dist/cjs/plugins/server/types.js +37 -0
  26. package/dist/cjs/plugins/server/types.js.map +1 -0
  27. package/dist/esm/index.js +4 -0
  28. package/dist/esm/index.js.map +1 -0
  29. package/dist/esm/package.json +1 -0
  30. package/dist/esm/plugins/client-rest/index.js +64 -0
  31. package/dist/esm/plugins/client-rest/index.js.map +1 -0
  32. package/dist/esm/plugins/client-rest/utils.js +13 -0
  33. package/dist/esm/plugins/client-rest/utils.js.map +1 -0
  34. package/dist/esm/plugins/replication-server/helpers.js +32 -0
  35. package/dist/esm/plugins/replication-server/helpers.js.map +1 -0
  36. package/dist/esm/plugins/replication-server/index.js +146 -0
  37. package/dist/esm/plugins/replication-server/index.js.map +1 -0
  38. package/dist/esm/plugins/replication-server/types.js +3 -0
  39. package/dist/esm/plugins/replication-server/types.js.map +1 -0
  40. package/dist/esm/plugins/server/endpoint-replication.js +156 -0
  41. package/dist/esm/plugins/server/endpoint-replication.js.map +1 -0
  42. package/dist/esm/plugins/server/endpoint-rest.js +213 -0
  43. package/dist/esm/plugins/server/endpoint-rest.js.map +1 -0
  44. package/dist/esm/plugins/server/helper.js +157 -0
  45. package/dist/esm/plugins/server/helper.js.map +1 -0
  46. package/dist/esm/plugins/server/index.js +30 -0
  47. package/dist/esm/plugins/server/index.js.map +1 -0
  48. package/dist/esm/plugins/server/rx-server.js +45 -0
  49. package/dist/esm/plugins/server/rx-server.js.map +1 -0
  50. package/dist/esm/plugins/server/types.js +37 -0
  51. package/dist/esm/plugins/server/types.js.map +1 -0
  52. package/dist/types/index.d.ts +1 -0
  53. package/dist/types/plugins/client-rest/index.d.ts +22 -0
  54. package/dist/types/plugins/client-rest/utils.d.ts +2 -0
  55. package/dist/types/plugins/replication-server/helpers.d.ts +2 -0
  56. package/dist/types/plugins/replication-server/index.d.ts +21 -0
  57. package/dist/types/plugins/replication-server/types.d.ts +17 -0
  58. package/dist/types/plugins/server/endpoint-replication.d.ts +20 -0
  59. package/dist/types/plugins/server/endpoint-rest.d.ts +16 -0
  60. package/dist/types/plugins/server/helper.d.ts +23 -0
  61. package/dist/types/plugins/server/index.d.ts +7 -0
  62. package/dist/types/plugins/server/rx-server.d.ts +44 -0
  63. package/dist/types/plugins/server/types.d.ts +66 -0
  64. package/package.json +139 -0
  65. package/plugins/client-rest/index.cjs +2 -0
  66. package/plugins/client-rest/index.d.cts +1 -0
  67. package/plugins/client-rest/index.d.mts +1 -0
  68. package/plugins/client-rest/index.mjs +1 -0
  69. package/plugins/client-rest/index.ts +1 -0
  70. package/plugins/client-rest/package.json +18 -0
  71. package/plugins/replication-server/index.cjs +2 -0
  72. package/plugins/replication-server/index.d.cts +1 -0
  73. package/plugins/replication-server/index.d.mts +1 -0
  74. package/plugins/replication-server/index.mjs +1 -0
  75. package/plugins/replication-server/index.ts +1 -0
  76. package/plugins/replication-server/package.json +18 -0
  77. package/plugins/server/index.cjs +2 -0
  78. package/plugins/server/index.d.cts +1 -0
  79. package/plugins/server/index.d.mts +1 -0
  80. package/plugins/server/index.mjs +1 -0
  81. package/plugins/server/index.ts +1 -0
  82. package/plugins/server/package.json +18 -0
@@ -0,0 +1,213 @@
1
+ import { normalizeMangoQuery } from 'rxdb/plugins/core';
2
+ import { filter, mergeMap } from 'rxjs';
3
+ import { ensureNotFalsy, getFromMapOrThrow } from 'rxdb/plugins/utils';
4
+ import { addAuthMiddleware, blockPreviousVersionPaths, closeConnection, docContainsServerOnlyFields, doesContainRegexQuerySelector, getDocAllowedMatcher, removeServerOnlyFieldsMonad, setCors, writeSSEHeaders } from "./helper.js";
5
+ export var REST_PATHS = ['query', 'query/observe', 'get', 'set', 'delete'
6
+
7
+ // TODO
8
+ /*
9
+ 'attachments/add',
10
+ 'attachments/delete',
11
+ 'events'
12
+ */];
13
+ export var RxServerRestEndpoint = function RxServerRestEndpoint(server, name, collection, queryModifier, changeValidator, serverOnlyFields, cors) {
14
+ var _this = this;
15
+ this.type = 'rest';
16
+ this.server = server;
17
+ this.name = name;
18
+ this.collection = collection;
19
+ this.serverOnlyFields = serverOnlyFields;
20
+ this.cors = cors;
21
+ setCors(this.server, [this.name].join('/'), cors);
22
+ blockPreviousVersionPaths(this.server, [this.name].join('/'), collection.schema.version);
23
+ this.urlPath = [this.name, collection.schema.version].join('/');
24
+ var primaryPath = this.collection.schema.primaryPath;
25
+ var authDataByRequest = addAuthMiddleware(this.server, this.urlPath);
26
+ this.queryModifier = (authData, query) => {
27
+ if (doesContainRegexQuerySelector(query.selector)) {
28
+ throw new Error('$regex queries not allowed because of DOS-attacks');
29
+ }
30
+ return queryModifier(authData, query);
31
+ };
32
+ this.changeValidator = (authData, change) => {
33
+ if (change.assumedMasterState && docContainsServerOnlyFields(serverOnlyFields, change.assumedMasterState) || docContainsServerOnlyFields(serverOnlyFields, change.newDocumentState)) {
34
+ return false;
35
+ }
36
+ return changeValidator(authData, change);
37
+ };
38
+ var removeServerOnlyFields = removeServerOnlyFieldsMonad(this.serverOnlyFields);
39
+ this.server.expressApp.post('/' + this.urlPath + '/query', async (req, res) => {
40
+ var authData = getFromMapOrThrow(authDataByRequest, req);
41
+ var useQuery;
42
+ try {
43
+ useQuery = this.queryModifier(ensureNotFalsy(authData), normalizeMangoQuery(this.collection.schema.jsonSchema, req.body));
44
+ } catch (err) {
45
+ closeConnection(res, 400, 'Bad Request');
46
+ return;
47
+ }
48
+ var rxQuery = this.collection.find(useQuery);
49
+ var result = await rxQuery.exec();
50
+ res.setHeader('Content-Type', 'application/json');
51
+ res.json({
52
+ documents: result.map(d => removeServerOnlyFields(d.toJSON()))
53
+ });
54
+ });
55
+
56
+ /**
57
+ * It is not possible to send data with server send events,
58
+ * so we send the query as query parameter in base64
59
+ * like ?query=e3NlbGVjdG9yOiB7fX0=
60
+ */
61
+ this.server.expressApp.get('/' + this.urlPath + '/query/observe', async (req, res) => {
62
+ var authData = getFromMapOrThrow(authDataByRequest, req);
63
+ writeSSEHeaders(res);
64
+ var useQuery = this.queryModifier(ensureNotFalsy(authData), normalizeMangoQuery(this.collection.schema.jsonSchema, JSON.parse(atob(req.query.query))));
65
+ var rxQuery = this.collection.find(useQuery);
66
+ var subscription = rxQuery.$.pipe(mergeMap(async result => {
67
+ var resultData = result.map(doc => removeServerOnlyFields(doc.toJSON()));
68
+
69
+ /**
70
+ * The auth-data might be expired
71
+ * so we re-run the auth parsing each time
72
+ * before emitting the new results.
73
+ */
74
+ try {
75
+ authData = await server.authHandler(req.headers);
76
+ } catch (err) {
77
+ closeConnection(res, 401, 'Unauthorized');
78
+ return null;
79
+ }
80
+ return resultData;
81
+ }), filter(f => f !== null)).subscribe(resultData => {
82
+ res.write('data: ' + JSON.stringify(resultData) + '\n\n');
83
+ });
84
+
85
+ /**
86
+ * @link https://youtu.be/0PcMuYGJPzM?si=AxkczxcMaUwhh8k9&t=363
87
+ */
88
+ req.on('close', () => {
89
+ subscription.unsubscribe();
90
+ res.end();
91
+ });
92
+ });
93
+ this.server.expressApp.post('/' + this.urlPath + '/get', async (req, res) => {
94
+ var authData = getFromMapOrThrow(authDataByRequest, req);
95
+ var ids = req.body;
96
+ var rxQuery = this.collection.findByIds(ids);
97
+ var resultMap = await rxQuery.exec();
98
+ var resultValues = Array.from(resultMap.values());
99
+ var docMatcher = getDocAllowedMatcher(this, ensureNotFalsy(authData));
100
+ var useDocs = resultValues.map(d => d.toJSON());
101
+ useDocs = useDocs.filter(d => docMatcher(d));
102
+ useDocs = useDocs.map(d => removeServerOnlyFields(d));
103
+ res.setHeader('Content-Type', 'application/json');
104
+ res.json({
105
+ documents: useDocs
106
+ });
107
+ });
108
+ this.server.expressApp.post('/' + this.urlPath + '/set', async (req, res) => {
109
+ var authData = getFromMapOrThrow(authDataByRequest, req);
110
+ var docDataMatcherWrite = getDocAllowedMatcher(this, ensureNotFalsy(authData));
111
+ var docsData = req.body;
112
+ for (var docData of docsData) {
113
+ var allowed = docDataMatcherWrite(docData);
114
+ if (!allowed) {
115
+ closeConnection(res, 403, 'Forbidden');
116
+ return;
117
+ }
118
+ }
119
+ function onWriteError(err, docData) {
120
+ if (err.rxdb && err.code === 'CONFLICT') {
121
+ // just retry on conflicts
122
+ docsData.push(docData);
123
+ } else {
124
+ closeConnection(res, 500, 'Internal Server Error');
125
+ throw err;
126
+ }
127
+ }
128
+ while (docsData.length > 0) {
129
+ var promises = [];
130
+ var docs = await collection.findByIds(docsData.map(d => d[primaryPath])).exec();
131
+ var useDocsData = docsData.slice();
132
+ docsData = [];
133
+ var _loop = async function (_docData) {
134
+ var id = _docData[primaryPath];
135
+ var doc = docs.get(id);
136
+ if (!doc) {
137
+ promises.push(_this.collection.insert(_docData).catch(err => onWriteError(err, _docData)));
138
+ } else {
139
+ var isAllowed = _this.changeValidator(authData, {
140
+ newDocumentState: removeServerOnlyFields(_docData),
141
+ assumedMasterState: removeServerOnlyFields(doc.toJSON(true))
142
+ });
143
+ if (!isAllowed) {
144
+ closeConnection(res, 403, 'Forbidden');
145
+ return {
146
+ v: void 0
147
+ };
148
+ }
149
+ promises.push(doc.patch(_docData).catch(err => onWriteError(err, _docData)));
150
+ }
151
+ },
152
+ _ret;
153
+ for (var _docData of useDocsData) {
154
+ _ret = await _loop(_docData);
155
+ if (_ret) return _ret.v;
156
+ }
157
+ await Promise.all(promises);
158
+ }
159
+ res.setHeader('Content-Type', 'application/json');
160
+ res.json({});
161
+ });
162
+ this.server.expressApp.post('/' + this.urlPath + '/delete', async (req, res) => {
163
+ var authData = getFromMapOrThrow(authDataByRequest, req);
164
+ var docDataMatcherWrite = getDocAllowedMatcher(this, ensureNotFalsy(authData));
165
+ var ids = req.body;
166
+ while (ids.length > 0) {
167
+ var useIds = ids.slice(0);
168
+ ids = [];
169
+ var promises = [];
170
+ var docsMap = await this.collection.findByIds(useIds).exec();
171
+ var _loop2 = async function (id) {
172
+ var doc = docsMap.get(id);
173
+ if (doc) {
174
+ var isAllowedDoc = docDataMatcherWrite(doc.toJSON(true));
175
+ if (!isAllowedDoc) {
176
+ closeConnection(res, 403, 'Forbidden');
177
+ return {
178
+ v: void 0
179
+ };
180
+ }
181
+ var isAllowedChange = _this.changeValidator(authData, {
182
+ newDocumentState: doc.toJSON(true),
183
+ assumedMasterState: doc.toJSON(true)
184
+ });
185
+ if (!isAllowedChange) {
186
+ closeConnection(res, 403, 'Forbidden');
187
+ return {
188
+ v: void 0
189
+ };
190
+ }
191
+ promises.push(doc.remove().catch(err => {
192
+ if (err.rxdb && err.code === 'CONFLICT') {
193
+ // just retry on conflicts
194
+ ids.push(id);
195
+ } else {
196
+ closeConnection(res, 500, 'Internal Server Error');
197
+ throw err;
198
+ }
199
+ }));
200
+ }
201
+ },
202
+ _ret2;
203
+ for (var id of useIds) {
204
+ _ret2 = await _loop2(id);
205
+ if (_ret2) return _ret2.v;
206
+ }
207
+ await Promise.all(promises);
208
+ }
209
+ res.setHeader('Content-Type', 'application/json');
210
+ res.json({});
211
+ });
212
+ };
213
+ //# sourceMappingURL=endpoint-rest.js.map
@@ -0,0 +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,SACIC,cAAc,EACdC,iBAAiB,QACd,oBAAoB;AAE3B,SACIC,iBAAiB,EACjBC,yBAAyB,EACzBC,eAAe,EACfC,2BAA2B,EAC3BC,6BAA6B,EAC7BC,oBAAoB,EACpBC,2BAA2B,EAC3BC,OAAO,EACPC,eAAe,QACZ,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,MAA0B,EAC1BC,IAAY,EACZC,UAAmC,EACnDC,aAAyD,EACzDC,eAA6D,EAC7CC,gBAA0B,EAC1BC,IAAa,EAC/B;EAAA,IAAAC,KAAA;EAAA,KAZOC,IAAI,GAAG,MAAM;EAAA,KAKFR,MAA0B,GAA1BA,MAA0B;EAAA,KAC1BC,IAAY,GAAZA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IAAI,IAAI3C,GAAG,CAAC4C,IAAI,KAAK,UAAU,EAAE;gBACrC;gBACAf,GAAG,CAACgB,IAAI,CAACQ,EAAE,CAAC;cAChB,CAAC,MAAM;gBACH5F,eAAe,CAACmC,GAAG,EAAE,GAAG,EAAE,uBAAuB,CAAC;gBAClD,MAAMI,GAAG;cACb;YACJ,CAAC,CAAC,CAAC;UACP;QACJ,CAAC;QAAAoE,KAAA;MA5BD,KAAK,IAAMf,EAAE,IAAIS,MAAM;QAAAM,KAAA,SAAAJ,MAAA,CAAAX,EAAA;QAAA,IAAAe,KAAA,SAAAA,KAAA,CAAAX,CAAA;MAAA;MA6BvB,MAAMG,OAAO,CAACC,GAAG,CAACd,QAAQ,CAAC;IAC/B;IACAnD,GAAG,CAACS,SAAS,CAAC,cAAc,EAAE,kBAAkB,CAAC;IACjDT,GAAG,CAACU,IAAI,CAAC,CAAC,CAAC,CAAC;EAChB,CAAC,CAAC;AACN,CAAC"}
@@ -0,0 +1,157 @@
1
+ import expressCors from 'cors';
2
+ import { flatClone, getQueryMatcher, normalizeMangoQuery, uniqueArray } from 'rxdb/plugins/core';
3
+ export function setCors(server, path, cors) {
4
+ var useCors = cors;
5
+ if (!useCors) {
6
+ useCors = server.cors;
7
+ }
8
+ if (useCors) {
9
+ server.expressApp.options('/' + path + '/*', expressCors({
10
+ origin: useCors,
11
+ // some legacy browsers (IE11, various SmartTVs) choke on 204
12
+ optionsSuccessStatus: 200
13
+ }));
14
+ }
15
+ }
16
+
17
+ /**
18
+ * "block" the previous version urls and send a 426 on them so that
19
+ * the clients know they must update.
20
+ */
21
+ export function blockPreviousVersionPaths(server, path, currentVersion) {
22
+ var v = 0;
23
+ var _loop = function () {
24
+ var version = v;
25
+ server.expressApp.all('/' + path + '/' + version + '/*', (req, res) => {
26
+ closeConnection(res, 426, 'Outdated version ' + version + ' (newest is ' + currentVersion + ')');
27
+ });
28
+ v++;
29
+ };
30
+ while (v < currentVersion) {
31
+ _loop();
32
+ }
33
+ }
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
+ export function addAuthMiddleware(server, path) {
46
+ var authDataByRequest = new WeakMap();
47
+ async function auth(req, res, next) {
48
+ try {
49
+ var authData = await server.authHandler(req.headers);
50
+ authDataByRequest.set(req, authData);
51
+ next();
52
+ } catch (err) {
53
+ closeConnection(res, 401, 'Unauthorized');
54
+ return;
55
+ }
56
+ }
57
+ server.expressApp.all('/' + path + '/*', auth, function (req, res, next) {
58
+ next();
59
+ });
60
+ return authDataByRequest;
61
+ }
62
+ var defaultMatchingQuery = {
63
+ selector: {},
64
+ skip: 0,
65
+ sort: []
66
+ };
67
+ export function getDocAllowedMatcher(endpoint, authData) {
68
+ var useQuery = endpoint.queryModifier ? endpoint.queryModifier(authData, normalizeMangoQuery(endpoint.collection.schema.jsonSchema, {})) : defaultMatchingQuery;
69
+ var docDataMatcher = getQueryMatcher(endpoint.collection.schema.jsonSchema, useQuery);
70
+ return docDataMatcher;
71
+ }
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
+ export function docContainsServerOnlyFields(serverOnlyFields, doc) {
91
+ var has = serverOnlyFields.find(field => {
92
+ return typeof doc[field] !== 'undefined';
93
+ });
94
+ return has;
95
+ }
96
+ export function removeServerOnlyFieldsMonad(serverOnlyFields) {
97
+ var serverOnlyFieldsStencil = {
98
+ _meta: undefined,
99
+ _rev: undefined,
100
+ _attachments: undefined
101
+ };
102
+ serverOnlyFields.forEach(field => serverOnlyFieldsStencil[field] = undefined);
103
+ return docData => {
104
+ if (!docData) {
105
+ return docData;
106
+ }
107
+ return Object.assign({}, docData, serverOnlyFieldsStencil);
108
+ };
109
+ }
110
+ export function mergeServerDocumentFieldsMonad(serverOnlyFields) {
111
+ var useFields = serverOnlyFields.slice(0);
112
+ // useFields.push('_rev');
113
+ // useFields.push('_meta');
114
+ // useFields.push('_attachments');
115
+ useFields = uniqueArray(useFields);
116
+ return (clientDoc, serverDoc) => {
117
+ if (!serverDoc) {
118
+ return clientDoc;
119
+ }
120
+ var ret = flatClone(clientDoc);
121
+ useFields.forEach(field => {
122
+ ret[field] = serverDoc[field];
123
+ });
124
+ return ret;
125
+ };
126
+ }
127
+
128
+ /**
129
+ * $regex queries are dangerous because they can dos-attach the
130
+ *
131
+ * @param selector
132
+ */
133
+ export function doesContainRegexQuerySelector(selector) {
134
+ if (!selector) {
135
+ return false;
136
+ }
137
+ if (Array.isArray(selector)) {
138
+ var found = !!selector.find(item => doesContainRegexQuerySelector(item));
139
+ return found;
140
+ }
141
+ if (typeof selector !== 'object') {
142
+ return false;
143
+ }
144
+ var entries = Object.entries(selector);
145
+ for (var [key, value] of entries) {
146
+ if (key === '$regex') {
147
+ return true;
148
+ } else {
149
+ var has = doesContainRegexQuerySelector(value);
150
+ if (has) {
151
+ return true;
152
+ }
153
+ }
154
+ }
155
+ return false;
156
+ }
157
+ //# sourceMappingURL=helper.js.map
@@ -0,0 +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;M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,EAAW;EAC5F,IAAI,CAACA,QAAQ,EAAE;IACX,OAAO,KAAK;EAChB;EACA,IAAIqC,KAAK,CAACC,OAAO,CAACtC,QAAQ,CAAC,EAAE;IACzB,IAAMuC,KAAK,GAAG,CAAC,CAACvC,QAAQ,CAACkB,IAAI,CAACsB,IAAI,IAAIJ,6BAA6B,CAACI,IAAI,CAAC,CAAC;IAC1E,OAAOD,KAAK;EAChB;EAEA,IAAI,OAAOvC,QAAQ,KAAK,QAAQ,EAAE;IAC9B,OAAO,KAAK;EAChB;EAEA,IAAMyC,OAAO,GAAGb,MAAM,CAACa,OAAO,CAACzC,QAAQ,CAAC;EACxC,KAAK,IAAM,CAAC0C,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"}
@@ -0,0 +1,30 @@
1
+ import { ensureNotFalsy, flatClone } from 'rxdb/plugins/utils';
2
+ import { RxServer } from "./rx-server.js";
3
+ import express from 'express';
4
+ export * from "./types.js";
5
+ export * from "./endpoint-replication.js";
6
+ export * from "./endpoint-rest.js";
7
+ export * from "./helper.js";
8
+ export async function startRxServer(options) {
9
+ options = flatClone(options);
10
+ if (!options.serverApp) {
11
+ var app = express();
12
+ options.serverApp = app;
13
+ }
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
+ var authHandler = options.authHandler ? options.authHandler : () => {
22
+ return {
23
+ data: {},
24
+ validUntil: Date.now() * 2
25
+ };
26
+ };
27
+ var server = new RxServer(options.database, authHandler, httpServer, ensureNotFalsy(options.serverApp), options.cors);
28
+ return server;
29
+ }
30
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["ensureNotFalsy","flatClone","RxServer","express","startRxServer","options","serverApp","app","use","json","httpServer","Promise","res","rej","hostname","ret","listen","port","authHandler","data","validUntil","Date","now","server","database","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';\nimport express from 'express';\nimport {\n Server as HttpServer\n} from 'http';\n\nexport * from './types.ts';\nexport * from './endpoint-replication.ts';\nexport * from './endpoint-rest.ts';\nexport * from './helper.ts';\n\nexport async function startRxServer<AuthType>(options: RxServerOptions<AuthType>): Promise<RxServer<AuthType>> {\n options = flatClone(options);\n if (!options.serverApp) {\n const app = express();\n options.serverApp = app;\n }\n\n options.serverApp.use(express.json());\n\n\n const httpServer: HttpServer = await new Promise((res, rej) => {\n const hostname = options.hostname ? options.hostname : 'localhost';\n const ret = ensureNotFalsy(options.serverApp).listen(options.port, hostname, () => {\n res(ret);\n });\n });\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<AuthType>(\n options.database,\n authHandler,\n httpServer,\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;AAEzC,OAAOC,OAAO,MAAM,SAAS;AAK7B,cAAc,YAAY;AAC1B,cAAc,2BAA2B;AACzC,cAAc,oBAAoB;AAClC,cAAc,aAAa;AAE3B,OAAO,eAAeC,aAAaA,CAAWC,OAAkC,EAA+B;EAC3GA,OAAO,GAAGJ,SAAS,CAACI,OAAO,CAAC;EAC5B,IAAI,CAACA,OAAO,CAACC,SAAS,EAAE;IACpB,IAAMC,GAAG,GAAGJ,OAAO,CAAC,CAAC;IACrBE,OAAO,CAACC,SAAS,GAAGC,GAAG;EAC3B;EAEAF,OAAO,CAACC,SAAS,CAACE,GAAG,CAACL,OAAO,CAACM,IAAI,CAAC,CAAC,CAAC;EAGrC,IAAMC,UAAsB,GAAG,MAAM,IAAIC,OAAO,CAAC,CAACC,GAAG,EAAEC,GAAG,KAAK;IAC3D,IAAMC,QAAQ,GAAGT,OAAO,CAACS,QAAQ,GAAGT,OAAO,CAACS,QAAQ,GAAG,WAAW;IAClE,IAAMC,GAAG,GAAGf,cAAc,CAACK,OAAO,CAACC,SAAS,CAAC,CAACU,MAAM,CAACX,OAAO,CAACY,IAAI,EAAEH,QAAQ,EAAE,MAAM;MAC/EF,GAAG,CAACG,GAAG,CAAC;IACZ,CAAC,CAAC;EACN,CAAC,CAAC;EAEF,IAAMG,WAA0C,GAAGb,OAAO,CAACa,WAAW,GAAGb,OAAO,CAACa,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,IAAIrB,QAAQ,CACvBG,OAAO,CAACmB,QAAQ,EAChBN,WAAW,EACXR,UAAU,EACVV,cAAc,CAACK,OAAO,CAACC,SAAS,CAAC,EACjCD,OAAO,CAACoB,IACZ,CAAC;EAED,OAAOF,MAAM;AACjB"}
@@ -0,0 +1,45 @@
1
+ import { RxServerReplicationEndpoint } from "./endpoint-replication.js";
2
+ import { RxServerRestEndpoint } from "./endpoint-rest.js";
3
+ export var RxServer = /*#__PURE__*/function () {
4
+ function RxServer(database, authHandler, httpServer, expressApp, cors = '*') {
5
+ this.endpoints = [];
6
+ this.closeFn = (() => this.close()).bind(this);
7
+ this.database = database;
8
+ this.authHandler = authHandler;
9
+ this.httpServer = httpServer;
10
+ this.expressApp = expressApp;
11
+ this.cors = cors;
12
+ database.onDestroy.push(this.closeFn);
13
+ }
14
+ var _proto = RxServer.prototype;
15
+ _proto.addReplicationEndpoint = async function addReplicationEndpoint(opts) {
16
+ 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
+ this.endpoints.push(endpoint);
18
+ return endpoint;
19
+ };
20
+ _proto.addRestEndpoint = async function addRestEndpoint(opts) {
21
+ 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
+ this.endpoints.push(endpoint);
23
+ return endpoint;
24
+ };
25
+ _proto.close = async function close() {
26
+ this.database.onDestroy = this.database.onDestroy.filter(fn => fn !== this.closeFn);
27
+ await new Promise((res, rej) => {
28
+ this.httpServer.close(err => {
29
+ if (err) {
30
+ rej(err);
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
+ });
42
+ };
43
+ return RxServer;
44
+ }();
45
+ //# sourceMappingURL=rx-server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rx-server.js","names":["RxServerReplicationEndpoint","RxServerRestEndpoint","RxServer","database","authHandler","httpServer","expressApp","cors","endpoints","closeFn","close","bind","onDestroy","push","_proto","prototype","addReplicationEndpoint","opts","endpoint","name","collection","queryModifier","_a","q","changeValidator","serverOnlyFields","addRestEndpoint","filter","fn","Promise","res","rej","err","setImmediate","emit"],"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 RxServerAuthHandler,\n RxServerChangeValidator,\n RxServerEndpoint,\n RxServerQueryModifier\n} from './types.ts';\nimport {\n Server as HttpServer\n} from 'http';\nimport { Express } from 'express';\nimport { RxServerRestEndpoint } from './endpoint-rest.ts';\n\nexport class RxServer<AuthType> {\n public readonly endpoints: RxServerEndpoint<AuthType, any>[] = [];\n\n private closeFn = (() => this.close()).bind(this);\n\n constructor(\n public readonly database: RxDatabase,\n public readonly authHandler: RxServerAuthHandler<AuthType>,\n public readonly httpServer: HttpServer,\n public readonly expressApp: Express,\n public readonly cors: string = '*'\n ) {\n database.onDestroy.push(this.closeFn);\n }\n\n public async 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 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 async 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 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 close() {\n this.database.onDestroy = this.database.onDestroy.filter(fn => fn !== this.closeFn);\n await new Promise<void>((res, rej) => {\n this.httpServer.close((err) => {\n if (err) { rej(err); } else { res(); }\n });\n /**\n * By default it will await all ongoing connections\n * before it closes. So we have to close it directly.\n * @link https://stackoverflow.com/a/36830072/3443137\n */\n setImmediate(() => this.httpServer.emit('close'));\n });\n\n }\n}\n"],"mappings":"AAIA,SAASA,2BAA2B,QAAQ,2BAA2B;AAWvE,SAASC,oBAAoB,QAAQ,oBAAoB;AAEzD,WAAaC,QAAQ;EAKjB,SAAAA,SACoBC,QAAoB,EACpBC,WAA0C,EAC1CC,UAAsB,EACtBC,UAAmB,EACnBC,IAAY,GAAG,GAAG,EACpC;IAAA,KAVcC,SAAS,GAAsC,EAAE;IAAA,KAEzDC,OAAO,GAAG,CAAC,MAAM,IAAI,CAACC,KAAK,CAAC,CAAC,EAAEC,IAAI,CAAC,IAAI,CAAC;IAAA,KAG7BR,QAAoB,GAApBA,QAAoB;IAAA,KACpBC,WAA0C,GAA1CA,WAA0C;IAAA,KAC1CC,UAAsB,GAAtBA,UAAsB;IAAA,KACtBC,UAAmB,GAAnBA,UAAmB;IAAA,KACnBC,IAAY,GAAZA,IAAY;IAE5BJ,QAAQ,CAACS,SAAS,CAACC,IAAI,CAAC,IAAI,CAACJ,OAAO,CAAC;EACzC;EAAC,IAAAK,MAAA,GAAAZ,QAAA,CAAAa,SAAA;EAAAD,MAAA,CAEYE,sBAAsB,GAAnC,eAAAA,uBAA+CC,IAY9C,EAAE;IACC,IAAMC,QAAQ,GAAG,IAAIlB,2BAA2B,CAC5C,IAAI,EACJiB,IAAI,CAACE,IAAI,EACTF,IAAI,CAACG,UAAU,EACfH,IAAI,CAACI,aAAa,GAAGJ,IAAI,CAACI,aAAa,GAAG,C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QAAE;MACzC,CAAC,CAAC;MACF;AACZ;AACA;AACA;AACA;MACYG,YAAY,CAAC,MAAM,IAAI,CAAC5B,UAAU,CAAC6B,IAAI,CAAC,OAAO,CAAC,CAAC;IACrD,CAAC,CAAC;EAEN,CAAC;EAAA,OAAAhC,QAAA;AAAA"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Returns the auth state by the given request headers.
3
+ * Throws if auth not valid.
4
+ */
5
+
6
+ /**
7
+ * Modifies a given query in a way to limit the results
8
+ * to what the authenticated user is allowed to see.
9
+ * For example the query selector
10
+ * input: {
11
+ * selector: {
12
+ * myField: { $gt: 100 }
13
+ * }
14
+ * }
15
+ * could be modified to restrict the results to only return
16
+ * documents that are "owned" by the user
17
+ * return: {
18
+ * selector: {
19
+ * myField: { $gt: 100 },
20
+ * userId: { $eq: authData.userId }
21
+ * }
22
+ * }
23
+ *
24
+ *
25
+ */
26
+
27
+ /**
28
+ * Validates if a given change is allowed to be performed on the server.
29
+ * Returns true if allowed, false if not.
30
+ * If a client tries to make a non-allowed change,
31
+ * the client will be disconnected.
32
+ */
33
+
34
+ ;
35
+ export {};
36
+ export {};
37
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","names":[],"sources":["../../../../src/plugins/server/types.ts"],"sourcesContent":["import type {\n FilledMangoQuery,\n RxDatabase,\n RxReplicationWriteToMasterRow,\n MaybePromise,\n RxCollection,\n WithDeleted\n} from 'rxdb/plugins/core';\nimport { IncomingHttpHeaders } from 'http';\nimport { Express } from 'express';\n\nexport type RxServerOptions<AuthType> = {\n database: RxDatabase;\n authHandler?: RxServerAuthHandler<AuthType>;\n serverApp?: Express;\n appOptions?: any;\n /**\n * [default=localhost]\n */\n hostname?: 'localhost' | '0.0.0.0' | string;\n port: number;\n /**\n * Set a origin for allowed CORS requests.\n * Can be overwritten by the cors option of the endpoints.\n * [default='*']\n */\n cors?: '*' | string;\n};\n\nexport type RxServerAuthData<AuthType> = {\n data: AuthType;\n validUntil: number;\n};\n\n/**\n * Returns the auth state by the given request headers.\n * Throws if auth not valid.\n */\nexport type RxServerAuthHandler<AuthType> =\n (headers: IncomingHttpHeaders) => MaybePromise<RxServerAuthData<AuthType>>;\n\n/**\n * Modifies a given query in a way to limit the results\n * to what the authenticated user is allowed to see.\n * For example the query selector\n * input: {\n * selector: {\n * myField: { $gt: 100 }\n * }\n * }\n * could be modified to restrict the results to only return\n * documents that are \"owned\" by the user\n * return: {\n * selector: {\n * myField: { $gt: 100 },\n * userId: { $eq: authData.userId }\n * }\n * }\n * \n * \n */\nexport type RxServerQueryModifier<AuthType, RxDocType> = (\n authData: RxServerAuthData<AuthType>,\n query: FilledMangoQuery<RxDocType>\n) => FilledMangoQuery<RxDocType>;\n\n/**\n * Validates if a given change is allowed to be performed on the server.\n * Returns true if allowed, false if not.\n * If a client tries to make a non-allowed change,\n * the client will be disconnected.\n */\nexport type RxServerChangeValidator<AuthType, RxDocType> = (\n authData: RxServerAuthData<AuthType>,\n change: RxReplicationWriteToMasterRow<RxDocType>\n) => boolean;\n\n\nexport interface RxServerEndpoint<AuthType, RxDocType> {\n collection: RxCollection<RxDocType>;\n name: string;\n type: 'replication' | 'rest' | string;\n urlPath: string;\n queryModifier?: RxServerQueryModifier<AuthType, RxDocType>;\n changeValidator?: RxServerChangeValidator<AuthType, RxDocType>;\n};\n"],"mappings":"AAkCA;AACA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAMA;AACA;AACA;AACA;AACA;AACA;;AAcC;AAAC;AAAA"}
@@ -0,0 +1 @@
1
+ export declare function wrongImport(): void;
@@ -0,0 +1,22 @@
1
+ import { ById, MangoQuery } from 'rxdb/plugins/core';
2
+ import { Observable } from 'rxjs';
3
+ import EventSource from 'eventsource';
4
+ export declare class RxRestClient<RxDocType> {
5
+ readonly endpointUrl: string;
6
+ headers: ById<string>;
7
+ readonly eventSource: typeof EventSource | any;
8
+ constructor(endpointUrl: string, headers?: ById<string>, eventSource?: typeof EventSource | any);
9
+ setHeaders(headers: ById<string>): void;
10
+ handleError(response: any): void;
11
+ query(query: MangoQuery<RxDocType>): Promise<{
12
+ documents: RxDocType[];
13
+ }>;
14
+ observeQuery(query: MangoQuery<RxDocType>): Observable<RxDocType[]>;
15
+ get(ids: string[]): Promise<{
16
+ documents: RxDocType[];
17
+ }>;
18
+ set(docs: RxDocType[]): Promise<any>;
19
+ delete(ids: string[]): Promise<any>;
20
+ }
21
+ export declare function createRestClient<RxDocType>(endpointUrl: string, headers: ById<string>, eventSource?: typeof EventSource | any): RxRestClient<RxDocType>;
22
+ export * from './utils.ts';
@@ -0,0 +1,2 @@
1
+ import { ById } from 'rxdb/plugins/core';
2
+ export declare function postRequest(url: string, body: any, headers?: ById<string>): Promise<any>;
@@ -0,0 +1,2 @@
1
+ import type { RxServerReplicationState } from './index.ts';
2
+ export declare function parseResponse(replicationState: RxServerReplicationState<any>, fetchResponse: Response): Promise<any>;