Skip to content

Commit 884d5a4

Browse files
committed
fix: detect if the caller is accessing us via local or parse for batch requests (#6980)
1 parent b398894 commit 884d5a4

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

spec/batch.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const serverURL1 = 'http://localhost:1234/1';
99
const serverURLNaked = 'http://localhost:1234/';
1010
const publicServerURL = 'http://domain.com/parse';
1111
const publicServerURLNaked = 'http://domain.com/';
12+
const publicServerURLLong = 'https://domain.com/something/really/long';
1213

1314
const headers = {
1415
'Content-Type': 'application/json',
@@ -24,6 +25,16 @@ describe('batch', () => {
2425
expect(internalURL).toEqual('/classes/Object');
2526
});
2627

28+
it('should return the proper url given a server url-only path', () => {
29+
const originalURL = '/parse/batch';
30+
const internalURL = batch.makeBatchRoutingPathFunction(
31+
originalURL,
32+
serverURL,
33+
publicServerURLLong
34+
)('/parse/classes/Object');
35+
expect(internalURL).toEqual('/classes/Object');
36+
});
37+
2738
it('should return the proper url same public/local endpoint', () => {
2839
const originalURL = '/parse/batch';
2940
const internalURL = batch.makeBatchRoutingPathFunction(

src/batch.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ function makeBatchRoutingPathFunction(originalUrl, serverURL, publicServerURL) {
3636
if (serverURL && publicServerURL && serverURL.path != publicServerURL.path) {
3737
const localPath = serverURL.path;
3838
const publicPath = publicServerURL.path;
39+
3940
// Override the api prefix
4041
apiPrefix = localPath;
4142
return function (requestPath) {
43+
const requestIsToLocal = requestPath.startsWith(localPath);
44+
4245
// Build the new path by removing the public path
4346
// and joining with the local path
44-
const newPath = path.posix.join('/', localPath, '/', requestPath.slice(publicPath.length));
47+
const newPath = path.posix.join(
48+
'/',
49+
localPath,
50+
'/',
51+
requestPath.slice(requestIsToLocal ? localPath.length : publicPath.length)
52+
);
4553
// Use the method for local routing
4654
return makeRoutablePath(newPath);
4755
};

0 commit comments

Comments
 (0)