Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added debug flag in the SuperTokenConfig in the init() for logging #732

Merged
merged 10 commits into from
Oct 31, 2023
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)

## [16.4.0] - 2023-10-26

- Added `debug` property to `TypeInput`. When set to `true`, it will enable debug logs.

## [16.3.4] - 2023-10-22

### Fixes
Expand Down
11 changes: 7 additions & 4 deletions lib/build/framework/fastify/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// @ts-nocheck
/// <reference types="node" />
export type { SessionRequest } from "./framework";
export declare const plugin: import("fastify").FastifyPluginCallback<Record<never, never>, import("http").Server>;
export declare const plugin: import("fastify").FastifyPluginCallback<
Record<never, never>,
import("fastify").RawServerDefault
>;
export declare const errorHandler: () => (
err: any,
req: import("fastify").FastifyRequest<
import("fastify/types/route").RouteGenericInterface,
import("http").Server,
import("fastify").RawServerDefault,
import("http").IncomingMessage
>,
res: import("fastify").FastifyReply<
import("http").Server,
import("fastify").RawServerDefault,
import("http").IncomingMessage,
import("http").ServerResponse,
import("http").ServerResponse<import("http").IncomingMessage>,
import("fastify/types/route").RouteGenericInterface,
unknown
>
Expand Down
2 changes: 1 addition & 1 deletion lib/build/framework/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export declare function setCookieForServerResponse(
expires: number,
path: string,
sameSite: "strict" | "lax" | "none"
): ServerResponse;
): ServerResponse<IncomingMessage>;
export declare function getCookieValueToSetInHeader(
prev: string | string[] | undefined,
val: string | string[],
Expand Down
14 changes: 6 additions & 8 deletions lib/build/logger.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/build/supertokens.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/build/types.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/version.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/build/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions lib/ts/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ const SUPERTOKENS_DEBUG_NAMESPACE = "com.supertokens";
*/

function logDebugMessage(message: string) {
if (debug.enabled(SUPERTOKENS_DEBUG_NAMESPACE)) {
debug(SUPERTOKENS_DEBUG_NAMESPACE)(
`{t: "${new Date().toISOString()}", message: \"${message}\", file: \"${getFileLocation()}\" sdkVer: "${version}"}`
);
console.log();
}
debug(SUPERTOKENS_DEBUG_NAMESPACE)(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why aren't you checking for if (debug.enabled(SUPERTOKENS_DEBUG_NAMESPACE)) {?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already call debug.enable() when initialising the logger, so ideally this should always return true. Added this check, just for safety.

`{t: "${new Date().toISOString()}", message: \"${message}\", file: \"${getFileLocation()}\" sdkVer: "${version}"}`
);
console.log();
}

function enableDebugLogs() {
debug.enable(SUPERTOKENS_DEBUG_NAMESPACE);
}

let getFileLocation = () => {
Expand All @@ -50,4 +52,4 @@ let getFileLocation = () => {
return "N/A";
};

export { logDebugMessage };
export { logDebugMessage, enableDebugLogs, SUPERTOKENS_DEBUG_NAMESPACE };
11 changes: 10 additions & 1 deletion lib/ts/supertokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import NormalisedURLPath from "./normalisedURLPath";
import type { BaseRequest, BaseResponse } from "./framework";
import type { TypeFramework } from "./framework/types";
import STError from "./error";
import { logDebugMessage } from "./logger";
import { enableDebugLogs, logDebugMessage } from "./logger";
import { PostSuperTokensInitCallbacks } from "./postSuperTokensInitCallbacks";
import { DEFAULT_TENANT_ID } from "./recipe/multitenancy/constants";

Expand All @@ -48,7 +48,16 @@ export default class SuperTokens {

telemetryEnabled: boolean;

debugEnabled: boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this variable should not be required, and even if it is, it should be in the logger.ts file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was only used in the tests, removed it.


constructor(config: TypeInput) {
if (config.debug === true) {
this.debugEnabled = true;
enableDebugLogs();
} else {
this.debugEnabled = false;
}

logDebugMessage("Started SuperTokens with debug logging (supertokens.init called)");
const originToPrint =
config.appInfo.origin === undefined
Expand Down
1 change: 1 addition & 0 deletions lib/ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type TypeInput = {
recipeList: RecipeListFunction[];
telemetry?: boolean;
isInServerlessEnv?: boolean;
debug?: boolean;
};

export type RecipeListFunction = (appInfo: NormalisedAppinfo, isInServerlessEnv: boolean) => RecipeModule;
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
export const version = "16.3.4";
export const version = "16.4.0";

export const cdiSupported = ["4.0"];

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "supertokens-node",
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
"version": "16.3.4",
"version": "16.4.0",
"description": "NodeJS driver for SuperTokens core",
"main": "index.js",
"scripts": {
Expand Down
60 changes: 60 additions & 0 deletions test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {
} = require("./utils");
const request = require("supertest");
const express = require("express");
const debug = require("debug");
let STExpress = require("../");
let Session = require("../recipe/session");
let SessionRecipe = require("../lib/build/recipe/session/recipe").default;
Expand All @@ -41,6 +42,7 @@ let EmailPassword = require("../lib/build/recipe/emailpassword");
let EmailPasswordRecipe = require("../lib/build/recipe/emailpassword/recipe").default;
const { getTopLevelDomainForSameSiteResolution } = require("../lib/build/utils");
const { middleware } = require("../framework/express");
const { SUPERTOKENS_DEBUG_NAMESPACE } = require("../lib/build/logger");

describe(`configTest: ${printPath("[test/config.test.js]")}`, function () {
beforeEach(async function () {
Expand Down Expand Up @@ -1192,6 +1194,64 @@ describe(`configTest: ${printPath("[test/config.test.js]")}`, function () {
}
});

it("testing that the debug mode is set", async function () {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
const connectionURI = await startST();
{
STExpress.init({
supertokens: {
connectionURI,
},
appInfo: {
apiDomain: "api.supertokens.io",
appName: "SuperTokens",
websiteDomain: "supertokens.io",
},
recipeList: [Session.init({ getTokenTransferMethod: () => "cookie" })],
debug: true,
});
assert(SuperTokens.getInstanceOrThrowError().debugEnabled === true);
assert(debug.enabled(SUPERTOKENS_DEBUG_NAMESPACE) === true);
resetAll();
}

{
debug.disable();
STExpress.init({
supertokens: {
connectionURI,
},
appInfo: {
apiDomain: "api.supertokens.io",
appName: "SuperTokens",
websiteDomain: "supertokens.io",
},
recipeList: [Session.init({ getTokenTransferMethod: () => "cookie" })],
debug: false,
});
assert(SuperTokens.getInstanceOrThrowError().debugEnabled === false);
assert(debug.enabled(SUPERTOKENS_DEBUG_NAMESPACE) === false);
resetAll();
}

{
debug.disable();
STExpress.init({
supertokens: {
connectionURI,
},
appInfo: {
apiDomain: "api.supertokens.io",
appName: "SuperTokens",
websiteDomain: "supertokens.io",
},
recipeList: [Session.init({ getTokenTransferMethod: () => "cookie" })],
});
assert(SuperTokens.getInstanceOrThrowError().debugEnabled === false);
assert(debug.enabled(SUPERTOKENS_DEBUG_NAMESPACE) === false);
resetAll();
}
});

it("checking for default cookie config", async function () {
const connectionURI = await startST();
STExpress.init({
Expand Down
Loading