BVB Source Codes

parse-server Show batch.js Source code

Return Download parse-server: download batch.js Source code - Download parse-server Source code - Type:.js
  1. const Parse = require('parse/node').Parse;
  2. const url = require('url');
  3. const path = require('path');
  4. // These methods handle batch requests.
  5. const batchPath = '/batch';
  6.  
  7. // Mounts a batch-handler onto a PromiseRouter.
  8. function mountOnto(router) {
  9.   router.route('POST', batchPath, (req) => {
  10.     return handleBatch(router, req);
  11.   });
  12. }
  13.  
  14. function parseURL(URL) {
  15.   if (typeof URL === 'string') {
  16.     return url.parse(URL)
  17.   }
  18.   return undefined;
  19. }
  20.  
  21. function makeBatchRoutingPathFunction(originalUrl, serverURL, publicServerURL) {
  22.   serverURL = serverURL ? parseURL(serverURL) : undefined;
  23.   publicServerURL = publicServerURL ? parseURL(publicServerURL) : undefined;
  24.  
  25.   const apiPrefixLength = originalUrl.length - batchPath.length;
  26.   let apiPrefix = originalUrl.slice(0, apiPrefixLength);
  27.  
  28.   const makeRoutablePath = function(requestPath) {
  29.       // The routablePath is the path minus the api prefix
  30.     if (requestPath.slice(0, apiPrefix.length) != apiPrefix) {
  31.       throw new Parse.Error(
  32.           Parse.Error.INVALID_JSON,
  33.           'cannot route batch path ' + requestPath);
  34.     }
  35.     return path.posix.join('/', requestPath.slice(apiPrefix.length));
  36.   }
  37.  
  38.   if (serverURL && publicServerURL
  39.         && (serverURL.path != publicServerURL.path)) {
  40.     const localPath = serverURL.path;
  41.     const publicPath = publicServerURL.path;
  42.       // Override the api prefix
  43.     apiPrefix = localPath;
  44.     return function(requestPath) {
  45.         // Build the new path by removing the public path
  46.         // and joining with the local path
  47.       const newPath = path.posix.join('/', localPath, '/' , requestPath.slice(publicPath.length));
  48.         // Use the method for local routing
  49.       return makeRoutablePath(newPath);
  50.     }
  51.   }
  52.  
  53.   return makeRoutablePath;
  54. }
  55.  
  56. // Returns a promise for a {response} object.
  57. // TODO: pass along auth correctly
  58. function handleBatch(router, req) {
  59.   if (!Array.isArray(req.body.requests)) {
  60.     throw new Parse.Error(Parse.Error.INVALID_JSON,
  61.                           'requests must be an array');
  62.   }
  63.  
  64.   // The batch paths are all from the root of our domain.
  65.   // That means they include the API prefix, that the API is mounted
  66.   // to. However, our promise router does not route the api prefix. So
  67.   // we need to figure out the API prefix, so that we can strip it
  68.   // from all the subrequests.
  69.   if (!req.originalUrl.endsWith(batchPath)) {
  70.     throw 'internal routing problem - expected url to end with batch';
  71.   }
  72.  
  73.   const makeRoutablePath = makeBatchRoutingPathFunction(req.originalUrl, req.config.serverURL, req.config.publicServerURL);
  74.  
  75.   const promises = req.body.requests.map((restRequest) => {
  76.     const routablePath = makeRoutablePath(restRequest.path);
  77.     // Construct a request that we can send to a handler
  78.     const request = {
  79.       body: restRequest.body,
  80.       config: req.config,
  81.       auth: req.auth,
  82.       info: req.info
  83.     };
  84.  
  85.     return router.tryRouteRequest(restRequest.method, routablePath, request).then((response) => {
  86.       return {success: response.response};
  87.     }, (error) => {
  88.       return {error: {code: error.code, error: error.message}};
  89.     });
  90.   });
  91.  
  92.   return Promise.all(promises).then((results) => {
  93.     return {response: results};
  94.   });
  95. }
  96.  
  97. module.exports = {
  98.   mountOnto,
  99.   makeBatchRoutingPathFunction
  100. };
  101.  
downloadbatch.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top