-
Notifications
You must be signed in to change notification settings - Fork 147
Warn about UnsecureWebSocketsProtocol #1248
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4d79353
feat: Remove meta parameter from logging.
RubenVerborgh 976e203
feat: Make LazyLoggerFactory buffer messages.
RubenVerborgh 67affa4
feat: Warn about UnsecureWebSocketsProtocol.
RubenVerborgh 69477d0
refactor: Make Logger an interface.
RubenVerborgh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,92 @@ | ||
| import { LazyLogger } from './LazyLogger'; | ||
| import { WrappingLogger } from './Logger'; | ||
| import type { Logger } from './Logger'; | ||
| import type { LoggerFactory } from './LoggerFactory'; | ||
| import type { LogLevel } from './LogLevel'; | ||
|
|
||
| /** | ||
| * Wraps over another {@link LoggerFactory} that can be set lazily. | ||
| * This is a singleton class, for which the instance can be retrieved using {@link LazyLoggerFactory.getInstance}. | ||
| * | ||
| * Loggers can safely be created before a {@link LoggerFactory} is set. | ||
| * But an error will be thrown if {@link Logger.log} is invoked before a {@link LoggerFactory} is set. | ||
| * | ||
| * This creates instances of {@link LazyLogger}. | ||
| * Temporary {@link LoggerFactory} that buffers log messages in memory | ||
| * until the {@link TemporaryLoggerFactory#switch} method is called. | ||
| */ | ||
| export class LazyLoggerFactory implements LoggerFactory { | ||
| private static readonly instance = new LazyLoggerFactory(); | ||
| class TemporaryLoggerFactory implements LoggerFactory { | ||
| private bufferSpaces: number; | ||
| private readonly wrappers: { wrapper: WrappingLogger; label: string }[] = []; | ||
| private readonly buffer: { logger: Logger; level: LogLevel; message: string }[] = []; | ||
|
|
||
| private ploggerFactory: LoggerFactory | undefined; | ||
| public constructor(bufferSize = 1024) { | ||
| this.bufferSpaces = bufferSize; | ||
| } | ||
|
|
||
| private constructor() { | ||
| // Singleton instance | ||
| public createLogger(label: string): WrappingLogger { | ||
| const wrapper = new WrappingLogger({ | ||
| log: (level: LogLevel, message: string): Logger => | ||
| this.bufferLogEntry(wrapper, level, message), | ||
| }); | ||
| this.wrappers.push({ wrapper, label }); | ||
| return wrapper; | ||
| } | ||
|
|
||
| public static getInstance(): LazyLoggerFactory { | ||
| return LazyLoggerFactory.instance; | ||
| private bufferLogEntry(logger: WrappingLogger, level: LogLevel, message: string): Logger { | ||
| // Buffer the message if spaces are still available | ||
| if (this.bufferSpaces > 0) { | ||
| this.bufferSpaces -= 1; | ||
| // If this is the last space, instead generate a warning through a new logger | ||
| if (this.bufferSpaces === 0) { | ||
| logger = this.createLogger('LazyLoggerFactory'); | ||
| level = 'warn'; | ||
| message = `Memory-buffered logging limit of ${this.buffer.length + 1} reached`; | ||
| } | ||
| this.buffer.push({ logger, level, message }); | ||
| } | ||
| return logger; | ||
| } | ||
|
|
||
| public createLogger(label: string): Logger { | ||
| return new LazyLogger(this, label); | ||
| /** | ||
| * Swaps all lazy loggers to new loggers from the given factory, | ||
| * and emits any buffered messages through those actual loggers. | ||
| */ | ||
| public switch(loggerFactory: LoggerFactory): void { | ||
| // Instantiate an actual logger within every lazy logger | ||
| for (const { wrapper, label } of this.wrappers.splice(0, this.wrappers.length)) { | ||
| wrapper.logger = loggerFactory.createLogger(label); | ||
| } | ||
| // Emit all buffered log messages | ||
| for (const { logger, level, message } of this.buffer.splice(0, this.buffer.length)) { | ||
| logger.log(level, message); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public resetLoggerFactory(): void { | ||
| this.ploggerFactory = undefined; | ||
| /** | ||
| * Wraps around another {@link LoggerFactory} that can be set lazily. | ||
| * This is useful when objects are instantiated (and when they create loggers) | ||
| * before the logging system has been fully instantiated, | ||
| * as is the case when using a dependency injection framework such as Components.js. | ||
| * | ||
| * Loggers can be created even before a {@link LoggerFactory} is set; | ||
| * any log messages will be buffered and re-emitted. | ||
| */ | ||
| export class LazyLoggerFactory implements LoggerFactory { | ||
| private factory: LoggerFactory; | ||
|
|
||
| public constructor(options: { bufferSize?: number } = {}) { | ||
| this.factory = new TemporaryLoggerFactory(options.bufferSize); | ||
| } | ||
|
|
||
| public get loggerFactory(): LoggerFactory { | ||
| if (!this.ploggerFactory) { | ||
| throw new Error('No logger factory has been set. Can be caused by logger invocation during initialization.'); | ||
| if (this.factory instanceof TemporaryLoggerFactory) { | ||
| throw new Error('Logger factory not yet set.'); | ||
| } | ||
| return this.ploggerFactory; | ||
| return this.factory; | ||
| } | ||
|
|
||
| public set loggerFactory(loggerFactory: LoggerFactory) { | ||
| this.ploggerFactory = loggerFactory; | ||
| if (this.factory instanceof TemporaryLoggerFactory) { | ||
| this.factory.switch(loggerFactory); | ||
| } | ||
| this.factory = loggerFactory; | ||
| } | ||
|
|
||
| public createLogger(label: string): Logger { | ||
| return this.factory.createLogger(label); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.