Skip to content

Commit c1995d2

Browse files
rishabhpoddarRishabh
andauthored
adds support for core base path (#253)
Co-authored-by: Rishabh <[email protected]>
1 parent d070226 commit c1995d2

File tree

7 files changed

+143
-14
lines changed

7 files changed

+143
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212

1313
- Added userId as an optional property to the response of `recipe/user/password/reset`.
1414
- Fixes https://github.com/supertokens/supertokens-node/issues/244 - throws an error if a user tries to update email / password of a third party login user.
15+
- Adds ability to give a path for each of the hostnames in the connectionURI: https://github.com/supertokens/supertokens-node/issues/252
1516
- add workflow to verify if pr title follows conventional commits
1617

1718
## [8.5.0] - 2022-01-14

lib/build/querier.d.ts

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/querier.js

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/build/supertokens.js

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ts/querier.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,22 @@ import { PROCESS_STATE, ProcessState } from "./processState";
2222

2323
export class Querier {
2424
private static initCalled = false;
25-
private static hosts: NormalisedURLDomain[] | undefined = undefined;
25+
private static hosts: { domain: NormalisedURLDomain; basePath: NormalisedURLPath }[] | undefined = undefined;
2626
private static apiKey: string | undefined = undefined;
2727
private static apiVersion: string | undefined = undefined;
2828

2929
private static lastTriedIndex = 0;
3030
private static hostsAliveForTesting: Set<string> = new Set<string>();
3131

32-
private __hosts: NormalisedURLDomain[] | undefined;
32+
private __hosts: { domain: NormalisedURLDomain; basePath: NormalisedURLPath }[] | undefined;
3333
private rIdToCore: string | undefined;
3434

3535
// we have rIdToCore so that recipes can force change the rId sent to core. This is a hack until the core is able
3636
// to support multiple rIds per API
37-
private constructor(hosts: NormalisedURLDomain[] | undefined, rIdToCore?: string) {
37+
private constructor(
38+
hosts: { domain: NormalisedURLDomain; basePath: NormalisedURLPath }[] | undefined,
39+
rIdToCore?: string
40+
) {
3841
this.__hosts = hosts;
3942
this.rIdToCore = rIdToCore;
4043
}
@@ -92,7 +95,7 @@ export class Querier {
9295
return new Querier(Querier.hosts, rIdToCore);
9396
}
9497

95-
static init(hosts?: NormalisedURLDomain[], apiKey?: string) {
98+
static init(hosts?: { domain: NormalisedURLDomain; basePath: NormalisedURLPath }[], apiKey?: string) {
9699
if (!Querier.initCalled) {
97100
Querier.initCalled = true;
98101
Querier.hosts = hosts;
@@ -245,14 +248,15 @@ export class Querier {
245248
if (numberOfTries === 0) {
246249
throw Error("No SuperTokens core available to query");
247250
}
248-
let currentHost: string = this.__hosts[Querier.lastTriedIndex].getAsStringDangerous();
251+
let currentDomain: string = this.__hosts[Querier.lastTriedIndex].domain.getAsStringDangerous();
252+
let currentBasePath: string = this.__hosts[Querier.lastTriedIndex].basePath.getAsStringDangerous();
249253
Querier.lastTriedIndex++;
250254
Querier.lastTriedIndex = Querier.lastTriedIndex % this.__hosts.length;
251255
try {
252256
ProcessState.getInstance().addState(PROCESS_STATE.CALLING_SERVICE_IN_REQUEST_HELPER);
253-
let response = await axiosFunction(currentHost + path.getAsStringDangerous());
257+
let response = await axiosFunction(currentDomain + currentBasePath + path.getAsStringDangerous());
254258
if (process.env.TEST_MODE === "testing") {
255-
Querier.hostsAliveForTesting.add(currentHost);
259+
Querier.hostsAliveForTesting.add(currentDomain + currentBasePath);
256260
}
257261
if (response.status !== 200) {
258262
throw response;

lib/ts/supertokens.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ export default class SuperTokens {
5252
config.supertokens?.connectionURI
5353
.split(";")
5454
.filter((h) => h !== "")
55-
.map((h) => new NormalisedURLDomain(h.trim())),
55+
.map((h) => {
56+
return {
57+
domain: new NormalisedURLDomain(h.trim()),
58+
basePath: new NormalisedURLPath(h.trim()),
59+
};
60+
}),
5661
config.supertokens?.apiKey
5762
);
5863

test/querier.test.js

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* License for the specific language governing permissions and limitations
1313
* under the License.
1414
*/
15-
const { printPath, setupST, startST, killAllST, cleanST } = require("./utils");
15+
const { printPath, setupST, startST, killAllST, cleanST, setKeyValueInConfig } = require("./utils");
1616
let ST = require("../");
1717
let { Querier } = require("../lib/build/querier");
1818
let assert = require("assert");
@@ -23,6 +23,8 @@ let nock = require("nock");
2323
const { default: NormalisedURLPath } = require("../lib/build/normalisedURLPath");
2424
let EmailPassword = require("../recipe/emailpassword");
2525
let EmailPasswordRecipe = require("../lib/build/recipe/emailpassword/recipe").default;
26+
const { default: axios } = require("axios");
27+
const { fail } = require("assert");
2628

2729
describe(`Querier: ${printPath("[test/querier.test.js]")}`, function () {
2830
beforeEach(async function () {
@@ -271,4 +273,109 @@ describe(`Querier: ${printPath("[test/querier.test.js]")}`, function () {
271273

272274
assert((await Session.getSessionInformation("someHandle")) === "someHandle");
273275
});
276+
277+
it("test with core base path", async function () {
278+
// first we need to know if the core used supports base_path config
279+
setKeyValueInConfig("base_path", "/test");
280+
await startST();
281+
282+
let response = await axios.get("http://localhost:8080/test/hello");
283+
if (response.status === 404) {
284+
//core must be an older version, so we return early
285+
return;
286+
} else if (response.status !== 200) {
287+
throw new Error("test failed");
288+
}
289+
290+
ST.init({
291+
supertokens: {
292+
connectionURI: "http://localhost:8080/test",
293+
},
294+
appInfo: {
295+
apiDomain: "api.supertokens.io",
296+
appName: "SuperTokens",
297+
websiteDomain: "supertokens.io",
298+
},
299+
recipeList: [Session.init()],
300+
});
301+
302+
// we query the core now
303+
let res = await Session.getAllSessionHandlesForUser("user1");
304+
assert(res.length === 0);
305+
});
306+
307+
it("test with incorrect core base path should fail", async function () {
308+
// first we need to know if the core used supports base_path config
309+
setKeyValueInConfig("base_path", "/some/path");
310+
await startST();
311+
312+
let response = await axios.get("http://localhost:8080/some/path/hello");
313+
if (response.status === 404) {
314+
//core must be an older version, so we return early
315+
return;
316+
} else if (response.status !== 200) {
317+
throw new Error("test failed");
318+
}
319+
320+
ST.init({
321+
supertokens: {
322+
connectionURI: "http://localhost:8080/test",
323+
},
324+
appInfo: {
325+
apiDomain: "api.supertokens.io",
326+
appName: "SuperTokens",
327+
websiteDomain: "supertokens.io",
328+
},
329+
recipeList: [Session.init()],
330+
});
331+
332+
try {
333+
// we query the core now
334+
await Session.getAllSessionHandlesForUser("user1");
335+
fail();
336+
} catch (err) {
337+
assert(err.message.startsWith("SuperTokens core threw an error"));
338+
}
339+
});
340+
341+
it("test with multiple core base path", async function () {
342+
// first we need to know if the core used supports base_path config
343+
setKeyValueInConfig("base_path", "/some/path");
344+
await startST();
345+
346+
setKeyValueInConfig("base_path", "/test");
347+
await startST("localhost", 8082);
348+
349+
let response = await axios.get("http://localhost:8080/some/path/hello");
350+
if (response.status === 404) {
351+
//core must be an older version, so we return early
352+
return;
353+
} else if (response.status !== 200) {
354+
throw new Error("test failed");
355+
}
356+
357+
ST.init({
358+
supertokens: {
359+
connectionURI: "http://localhost:8080/some/path;http://localhost:8082/test",
360+
},
361+
appInfo: {
362+
apiDomain: "api.supertokens.io",
363+
appName: "SuperTokens",
364+
websiteDomain: "supertokens.io",
365+
},
366+
recipeList: [Session.init()],
367+
});
368+
369+
{
370+
// we query the first core now
371+
let res = await Session.getAllSessionHandlesForUser("user1");
372+
assert(res.length === 0);
373+
}
374+
375+
{
376+
// we query the second core now
377+
let res = await Session.getAllSessionHandlesForUser("user1");
378+
assert(res.length === 0);
379+
}
380+
});
274381
});

0 commit comments

Comments
 (0)