1
- var Parse = require ( 'parse/node' ) . Parse ;
2
-
1
+ const Parse = require ( 'parse/node' ) . Parse ;
2
+ const url = require ( 'url' ) ;
3
+ const path = require ( 'path' ) ;
3
4
// These methods handle batch requests.
4
- var batchPath = '/batch' ;
5
+ const batchPath = '/batch' ;
5
6
6
7
// Mounts a batch-handler onto a PromiseRouter.
7
8
function mountOnto ( router ) {
@@ -10,6 +11,48 @@ function mountOnto(router) {
10
11
} ) ;
11
12
}
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
+ let apiPrefixLength = originalUrl . length - batchPath . length ;
26
+ let apiPrefix = originalUrl . slice ( 0 , apiPrefixLength ) ;
27
+
28
+ let 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 . join ( '/' , requestPath . slice ( apiPrefix . length ) ) ;
36
+ }
37
+
38
+ if ( serverURL && publicServerURL
39
+ && ( serverURL . path != publicServerURL . path ) ) {
40
+ let localPath = serverURL . path ;
41
+ let 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
+ let newPath = path . 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
+
13
56
// Returns a promise for a {response} object.
14
57
// TODO: pass along auth correctly
15
58
function handleBatch ( router , req ) {
@@ -26,19 +69,13 @@ function handleBatch(router, req) {
26
69
if ( ! req . originalUrl . endsWith ( batchPath ) ) {
27
70
throw 'internal routing problem - expected url to end with batch' ;
28
71
}
29
- var apiPrefixLength = req . originalUrl . length - batchPath . length ;
30
- var apiPrefix = req . originalUrl . slice ( 0 , apiPrefixLength ) ;
72
+
73
+ const makeRoutablePath = makeBatchRoutingPathFunction ( req . originalUrl , req . config . serverURL , req . config . publicServerURL ) ;
31
74
32
75
const promises = req . body . requests . map ( ( restRequest ) => {
33
- // The routablePath is the path minus the api prefix
34
- if ( restRequest . path . slice ( 0 , apiPrefixLength ) != apiPrefix ) {
35
- throw new Parse . Error (
36
- Parse . Error . INVALID_JSON ,
37
- 'cannot route batch path ' + restRequest . path ) ;
38
- }
39
- var routablePath = restRequest . path . slice ( apiPrefixLength ) ;
76
+ const routablePath = makeRoutablePath ( restRequest . path ) ;
40
77
// Construct a request that we can send to a handler
41
- var request = {
78
+ const request = {
42
79
body : restRequest . body ,
43
80
config : req . config ,
44
81
auth : req . auth ,
@@ -58,5 +95,6 @@ function handleBatch(router, req) {
58
95
}
59
96
60
97
module . exports = {
61
- mountOnto : mountOnto
98
+ mountOnto,
99
+ makeBatchRoutingPathFunction
62
100
} ;
0 commit comments