Skip to content

Commit

Permalink
feat: #73 improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Nov 2, 2021
1 parent 7490191 commit 3bb7d4e
Show file tree
Hide file tree
Showing 35 changed files with 483 additions and 374 deletions.
32 changes: 16 additions & 16 deletions docs/oidc-client-ts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class AccessTokenEvents {
addAccessTokenExpiring(cb: AccessTokenCallback): void;
// (undocumented)
load(container: User): void;
// Warning: (ae-forgotten-export) The symbol "Logger" needs to be exported by the entry point index.d.ts
//
// (undocumented)
protected _logger: Logger;
// (undocumented)
removeAccessTokenExpired(cb: AccessTokenCallback): void;
// (undocumented)
Expand Down Expand Up @@ -104,36 +108,28 @@ export class InMemoryWebStorage implements Storage {
setItem(key: string, value: string): void;
}

// @public (undocumented)
// @public
export class Log {
// (undocumented)
static get DEBUG(): number;
// (undocumented)
static debug(...args: any[]): void;
// (undocumented)
static get ERROR(): number;
// (undocumented)
static error(...args: any[]): void;
// (undocumented)
static get INFO(): number;
// (undocumented)
static info(...args: any[]): void;
// (undocumented)
static get level(): number;
static set level(value: number);
// Warning: (ae-forgotten-export) The symbol "Logger" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "ILogger" needs to be exported by the entry point index.d.ts
//
// (undocumented)
static get logger(): Logger;
static set logger(value: Logger);
static get logger(): ILogger;
static set logger(value: ILogger);
// (undocumented)
static get NONE(): number;
// (undocumented)
static reset(): void;
// (undocumented)
static get WARN(): number;
// (undocumented)
static warn(...args: any[]): void;
}

// @public (undocumented)
Expand Down Expand Up @@ -182,6 +178,8 @@ export class OidcClient {
// (undocumented)
createSignoutRequest({ state, id_token_hint, post_logout_redirect_uri, extraQueryParams, request_type }?: CreateSignoutRequestArgs): Promise<SignoutRequest>;
// (undocumented)
protected readonly _logger: Logger;
// (undocumented)
readonly metadataService: MetadataService;
// (undocumented)
processSigninResponse(url?: string): Promise<SigninResponse>;
Expand Down Expand Up @@ -384,6 +382,8 @@ export class UserManager {
// (undocumented)
protected _loadUser(): Promise<User | null>;
// (undocumented)
protected readonly _logger: Logger;
// (undocumented)
get metadataService(): MetadataService;
// Warning: (ae-forgotten-export) The symbol "PopupNavigator" needs to be exported by the entry point index.d.ts
//
Expand Down Expand Up @@ -577,10 +577,10 @@ export class WebStorageStateStore implements StateStore {

// Warnings were encountered during analysis:
//
// src/OidcClient.ts:114:88 - (ae-forgotten-export) The symbol "SigninState" needs to be exported by the entry point index.d.ts
// src/OidcClient.ts:114:108 - (ae-forgotten-export) The symbol "SigninResponse" needs to be exported by the entry point index.d.ts
// src/OidcClient.ts:181:89 - (ae-forgotten-export) The symbol "State" needs to be exported by the entry point index.d.ts
// src/OidcClient.ts:181:115 - (ae-forgotten-export) The symbol "SignoutResponse" needs to be exported by the entry point index.d.ts
// src/OidcClient.ts:117:88 - (ae-forgotten-export) The symbol "SigninState" needs to be exported by the entry point index.d.ts
// src/OidcClient.ts:117:108 - (ae-forgotten-export) The symbol "SigninResponse" needs to be exported by the entry point index.d.ts
// src/OidcClient.ts:184:89 - (ae-forgotten-export) The symbol "State" needs to be exported by the entry point index.d.ts
// src/OidcClient.ts:184:115 - (ae-forgotten-export) The symbol "SignoutResponse" needs to be exported by the entry point index.d.ts

// (No @packageDocumentation comment for this package)

Expand Down
16 changes: 10 additions & 6 deletions src/AccessTokenEvents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log, Timer } from "./utils";
import { Logger, Timer } from "./utils";
import type { User } from "./User";

export type AccessTokenCallback = (...ev: any[]) => void;
Expand All @@ -10,11 +10,15 @@ export type AccessTokenCallback = (...ev: any[]) => void;
* @public
*/
export class AccessTokenEvents {
protected _logger: Logger;

private _expiringNotificationTimeInSeconds: number
private _expiringTimer: Timer
private _expiredTimer: Timer

public constructor({ expiringNotificationTimeInSeconds }: { expiringNotificationTimeInSeconds: number }) {
this._logger = new Logger("AccessTokenEvents");

this._expiringNotificationTimeInSeconds = expiringNotificationTimeInSeconds;
this._expiringTimer = new Timer("Access token expiring");
this._expiredTimer = new Timer("Access token expired");
Expand All @@ -24,7 +28,7 @@ export class AccessTokenEvents {
// only register events if there's an access token and it has an expiration
if (container.access_token && container.expires_in !== undefined) {
const duration = container.expires_in;
Log.debug("AccessTokenEvents.load: access token present, remaining duration:", duration);
this._logger.debug("load: access token present, remaining duration:", duration);

if (duration > 0) {
// only register expiring if we still have time
Expand All @@ -33,17 +37,17 @@ export class AccessTokenEvents {
expiring = 1;
}

Log.debug("AccessTokenEvents.load: registering expiring timer in:", expiring);
this._logger.debug("load: registering expiring timer in:", expiring);
this._expiringTimer.init(expiring);
}
else {
Log.debug("AccessTokenEvents.load: canceling existing expiring timer becase we're past expiration.");
this._logger.debug("load: canceling existing expiring timer because we're past expiration.");
this._expiringTimer.cancel();
}

// if it's negative, it will still fire
const expired = duration + 1;
Log.debug("AccessTokenEvents.load: registering expired timer in:", expired);
this._logger.debug("load: registering expired timer in:", expired);
this._expiredTimer.init(expired);
}
else {
Expand All @@ -53,7 +57,7 @@ export class AccessTokenEvents {
}

public unload(): void {
Log.debug("AccessTokenEvents.unload: canceling existing access token timers");
this._logger.debug("unload: canceling existing access token timers");
this._expiringTimer.cancel();
this._expiredTimer.cancel();
}
Expand Down
15 changes: 9 additions & 6 deletions src/CheckSessionIFrame.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log } from "./utils";
import { Logger } from "./utils";

/**
* @public
*/
export class CheckSessionIFrame {
private readonly _logger: Logger;
private _frame_origin: string;
private _frame: HTMLIFrameElement;
private _timer: number | null = null
Expand All @@ -19,6 +20,8 @@ export class CheckSessionIFrame {
private _intervalInSeconds: number,
private _stopOnError: boolean
) {
this._logger = new Logger("CheckSessionIFrame");

const idx = url.indexOf("/", url.indexOf("//") + 2);
this._frame_origin = url.substr(0, idx);

Expand Down Expand Up @@ -50,18 +53,18 @@ export class CheckSessionIFrame {
e.source === this._frame.contentWindow
) {
if (e.data === "error") {
Log.error("CheckSessionIFrame: error message from check session op iframe");
this._logger.error("error message from check session op iframe");
if (this._stopOnError) {
this.stop();
}
}
else if (e.data === "changed") {
Log.debug("CheckSessionIFrame: changed message from check session op iframe");
this._logger.debug("changed message from check session op iframe");
this.stop();
void this._callback();
}
else {
Log.debug("CheckSessionIFrame: " + e.data + " message from check session op iframe");
this._logger.debug(e.data + " message from check session op iframe");
}
}
}
Expand All @@ -71,7 +74,7 @@ export class CheckSessionIFrame {
return;
}

Log.debug("CheckSessionIFrame.start");
this._logger.debug("start");

this.stop();

Expand All @@ -96,7 +99,7 @@ export class CheckSessionIFrame {
this._session_state = null;

if (this._timer) {
Log.debug("CheckSessionIFrame.stop");
this._logger.debug("stop");

window.clearInterval(this._timer);
this._timer = null;
Expand Down
10 changes: 5 additions & 5 deletions src/ErrorResponse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log } from "./utils";
import { Logger } from "./utils";

export class ErrorResponse extends Error {
public readonly name: string;
Expand All @@ -18,13 +18,13 @@ export class ErrorResponse extends Error {
public constructor(args: {
error?: string; error_description?: string; error_uri?: string; state?: unknown; session_state?: string;
}) {
super(args.error_description || args.error);

if (!args.error) {
Log.error("No error passed to ErrorResponse");
throw new Error("error");
Logger.error("ErrorResponse", "No error passed");
throw new Error("No error passed");
}

super(args.error_description || args.error);

this.name = "ErrorResponse";

this.error = args.error;
Expand Down
12 changes: 7 additions & 5 deletions src/InMemoryWebStorage.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log } from "./utils";
import { Logger } from "./utils";

/**
* @public
*/
export class InMemoryWebStorage implements Storage {
private readonly _logger: Logger;
private _data: Record<string, string>;

public constructor() {
this._logger = new Logger("InMemoryWebStorage");
this._data = {};
}

public clear(): void {
Log.debug("InMemoryWebStorage.clear");
this._logger.debug("clear");
this._data = {};
}

public getItem(key: string): string {
Log.debug("InMemoryWebStorage.getItem", key);
this._logger.debug("getItem", key);
return this._data[key];
}

public setItem(key: string, value: string): void {
Log.debug("InMemoryWebStorage.setItem", key);
this._logger.debug("setItem", key);
this._data[key] = value;
}

public removeItem(key: string): void {
Log.debug("InMemoryWebStorage.removeItem", key);
this._logger.debug("removeItem", key);
delete this._data[key];
}

Expand Down
Loading

0 comments on commit 3bb7d4e

Please sign in to comment.