acebase-server 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "acebase-server",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "description": "AceBase realtime database server (webserver endpoint to allow remote connections)",
5
5
  "main": "./src/index.js",
6
6
  "private": false,
@@ -44,8 +44,8 @@
44
44
  "author": "Ewout Stortenbeker <me@appy.one> (http://appy.one)",
45
45
  "license": "MIT",
46
46
  "dependencies": {
47
- "acebase": "^1.12.2",
48
- "acebase-core": "^1.10.1",
47
+ "acebase": "^1.14.0",
48
+ "acebase-core": "^1.12.0",
49
49
  "express": "^4.17.1",
50
50
  "socket.io": "^2.4.1"
51
51
  },
@@ -1015,6 +1015,7 @@ class AceBaseServer extends EventEmitter {
1015
1015
  catch(err) {
1016
1016
  this.debug.error(`Failed to parse AceBase-Context header: "${context}" in request at ${req.url} from client ${req.ip}`);
1017
1017
  }
1018
+ // console.log(`Received request on path ` + req.path);
1018
1019
  next();
1019
1020
  });
1020
1021
 
@@ -1950,7 +1951,7 @@ class AceBaseServer extends EventEmitter {
1950
1951
 
1951
1952
  app.get(`/info/${dbname}`, (req, res) => {
1952
1953
  const info = {
1953
- version: '1.7.0', // TODO: Load from package.json
1954
+ version: '1.8.0', // TODO: Load from package.json
1954
1955
  time: Date.now(),
1955
1956
  process: process.pid
1956
1957
  };
@@ -2121,43 +2122,77 @@ class AceBaseServer extends EventEmitter {
2121
2122
  });
2122
2123
  });
2123
2124
 
2124
- app.get(`/export/${dbname}/*`, (req, res) => {
2125
+ app.get(`/export/${dbname}/*`, async (req, res) => {
2125
2126
  // Export API
2126
- const path = req.path.substr(dbname.length + 9);
2127
+ const path = req.path.slice(dbname.length + 9);
2127
2128
  if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
2128
2129
  return;
2129
2130
  }
2130
2131
  const format = req.query.format || 'json';
2132
+ const type_safe = req.query.type_safe !== '0';
2131
2133
 
2132
- const stream = {
2133
- write(chunk) {
2134
- res.write(chunk);
2135
-
2136
- // OLD: causes streaming issues
2137
- // return new Promise((resolve, reject) => {
2138
- // res.write(chunk, err => {
2139
- // if (err) { reject(err); }
2140
- // else { resolve(); }
2141
- // });
2142
- // });
2134
+ const write = async (chunk) => {
2135
+ let ok = res.write(chunk);
2136
+ if (!ok) {
2137
+ await new Promise(resolve => res.once('drain', resolve));
2143
2138
  }
2144
2139
  };
2145
2140
 
2146
2141
  const ref = db.ref(path);
2147
2142
  res.setHeader('Content-Disposition', `attachment; filename=${ref.key || 'export'}.json`); // Will be treated as a download in browser
2148
2143
 
2149
- ref.export(stream, { format })
2150
- .then(() => {
2144
+ try {
2145
+ await ref.export(write, { format, type_safe });
2146
+ }
2147
+ catch (err) {
2148
+ this.debug.error(`Error exporting data for path "/${path}": `, err);
2149
+ if (!res.headersSent) {
2150
+ res.statusCode = 500;
2151
+ res.send(err);
2152
+ }
2153
+ }
2154
+ finally {
2151
2155
  res.end();
2152
- })
2153
- .catch(err => {
2154
- res.statusCode = 500;
2155
- res.send(err);
2156
- })
2157
- .catch(err => {
2158
- // Previous catch raised another exception. Happens when streaming already started: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
2156
+ }
2157
+ });
2158
+
2159
+ app.post(`/import/${dbname}/*`, async (req, res) => {
2160
+ // Import API
2161
+ const path = req.path.slice(dbname.length + 9);
2162
+ if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
2163
+ return;
2164
+ }
2165
+ const format = req.query.format || 'json';
2166
+ const suppress_events = req.query.suppress_events === '1';
2167
+
2168
+ req.pause(); // Switch to non-flowing mode so we can use .read() upon request
2169
+ let eof = false;
2170
+ req.once('end', () => { eof = true; });
2171
+ const read = async (length) => {
2172
+ let chunk = req.read();
2173
+ if (chunk === null && !eof) {
2174
+ await new Promise(resolve => req.once('readable', resolve));
2175
+ chunk = req.read();
2176
+ }
2177
+ console.log(`Received chunk: `, chunk);
2178
+ return chunk;
2179
+ };
2180
+
2181
+ const ref = db.ref(path);
2182
+ try {
2183
+ await ref.import(read, { format, suppress_events });
2184
+ res.send({ success: true });
2185
+ }
2186
+ catch (err) {
2187
+ this.debug.error(`Error importing data for path "/${path}": `, err);
2188
+ if (!res.headersSent) {
2189
+ res.statusCode = 500;
2190
+ res.send({ success: false, reason: err.message });
2191
+ }
2192
+ }
2193
+ finally {
2159
2194
  res.end();
2160
- });
2195
+ }
2161
2196
  });
2162
2197
 
2163
2198
  app.get(`/exists/${dbname}/*`, (req, res) => {
@@ -2215,12 +2250,12 @@ class AceBaseServer extends EventEmitter {
2215
2250
  }
2216
2251
  catch(err) {
2217
2252
  if (err instanceof SchemaValidationError) {
2218
- logRef.push({ action: 'update_data', success: false, code: 'schema_validation_failed', path, error: err.reason });
2253
+ logRef && logRef.push({ action: 'update_data', success: false, code: 'schema_validation_failed', path, error: err.reason });
2219
2254
  res.status(422).send({ code: 'schema_validation_failed', message: err.message });
2220
2255
  }
2221
2256
  else {
2222
2257
  this.debug.error(`failed to update "${path}":`, err);
2223
- logRef.push({ action: 'update_data', success: false, code: `unknown_error`, path, error: err.message });
2258
+ logRef && logRef.push({ action: 'update_data', success: false, code: `unknown_error`, path, error: err.message });
2224
2259
  sendError(res, err);
2225
2260
  }
2226
2261
  }
@@ -263,7 +263,7 @@ M.AutoInit();
263
263
  </script>
264
264
 
265
265
  <!-- AceBase client js -->
266
- <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/acebase-client@1.7.1/dist/browser.min.js"></script>
266
+ <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/acebase-client@1.11.0/dist/browser.min.js"></script>
267
267
  <!-- <script type="text/javascript" src="acebase-client-browser.min.js"></script> -->
268
268
 
269
269
  <!-- AceBase Webmanager js -->