Enhanced support for batch endpoints (#3042)

* Allow to have different endpoint on publicserverURL and serverURL when batching

* nits
This commit is contained in:
Florent Vilmart
2016-11-24 20:14:12 -05:00
committed by GitHub
parent 8c2c76dd26
commit d800ff85e2
2 changed files with 96 additions and 14 deletions

View File

@@ -1,7 +1,8 @@
var Parse = require('parse/node').Parse;
const Parse = require('parse/node').Parse;
const url = require('url');
const path = require('path');
// These methods handle batch requests.
var batchPath = '/batch';
const batchPath = '/batch';
// Mounts a batch-handler onto a PromiseRouter.
function mountOnto(router) {
@@ -10,6 +11,48 @@ function mountOnto(router) {
});
}
function parseURL(URL) {
if (typeof URL === 'string') {
return url.parse(URL)
}
return undefined;
}
function makeBatchRoutingPathFunction(originalUrl, serverURL, publicServerURL) {
serverURL = serverURL ? parseURL(serverURL) : undefined;
publicServerURL = publicServerURL ? parseURL(publicServerURL): undefined;
let apiPrefixLength = originalUrl.length - batchPath.length;
let apiPrefix = originalUrl.slice(0, apiPrefixLength);
let makeRoutablePath = function(requestPath) {
// The routablePath is the path minus the api prefix
if (requestPath.slice(0, apiPrefix.length) != apiPrefix) {
throw new Parse.Error(
Parse.Error.INVALID_JSON,
'cannot route batch path ' + requestPath);
}
return path.join('/', requestPath.slice(apiPrefix.length));
}
if (serverURL && publicServerURL
&& (serverURL.path != publicServerURL.path)) {
let localPath = serverURL.path;
let publicPath = publicServerURL.path;
// Override the api prefix
apiPrefix = localPath;
return function(requestPath) {
// Build the new path by removing the public path
// and joining with the local path
let newPath = path.join('/', localPath, '/' , requestPath.slice(publicPath.length));
// Use the method for local routing
return makeRoutablePath(newPath);
}
}
return makeRoutablePath;
}
// Returns a promise for a {response} object.
// TODO: pass along auth correctly
function handleBatch(router, req) {
@@ -26,19 +69,13 @@ function handleBatch(router, req) {
if (!req.originalUrl.endsWith(batchPath)) {
throw 'internal routing problem - expected url to end with batch';
}
var apiPrefixLength = req.originalUrl.length - batchPath.length;
var apiPrefix = req.originalUrl.slice(0, apiPrefixLength);
const makeRoutablePath = makeBatchRoutingPathFunction(req.originalUrl, req.config.serverURL, req.config.publicServerURL);
const promises = req.body.requests.map((restRequest) => {
// The routablePath is the path minus the api prefix
if (restRequest.path.slice(0, apiPrefixLength) != apiPrefix) {
throw new Parse.Error(
Parse.Error.INVALID_JSON,
'cannot route batch path ' + restRequest.path);
}
var routablePath = restRequest.path.slice(apiPrefixLength);
const routablePath = makeRoutablePath(restRequest.path);
// Construct a request that we can send to a handler
var request = {
const request = {
body: restRequest.body,
config: req.config,
auth: req.auth,
@@ -58,5 +95,6 @@ function handleBatch(router, req) {
}
module.exports = {
mountOnto: mountOnto
mountOnto,
makeBatchRoutingPathFunction
};