From d2909950cc974566285ce2163fb14fcd4c4ea2c5 Mon Sep 17 00:00:00 2001 From: Clayton Wilson Date: Mon, 11 Mar 2024 02:05:46 -0500 Subject: [PATCH] Organize functions and defaults --- src/Logger.ts | 123 ++++--------------------------------------- src/lib/defaults.ts | 40 ++++++++++++++ src/lib/utilities.ts | 73 +++++++++++++++++++++++++ src/types/logger.ts | 23 ++++++++ 4 files changed, 145 insertions(+), 114 deletions(-) create mode 100644 src/lib/defaults.ts create mode 100644 src/lib/utilities.ts diff --git a/src/Logger.ts b/src/Logger.ts index 754f9cb..200a28a 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -1,3 +1,11 @@ +import { LogLabel, LogLevel } from "./types/logger"; +import { gzip, randomString, stringifyInstance, ungzip } from "./lib/utilities"; +import { + DEFAULT_OUTPUTS, + DEFAULT_CONFIG, + DEFAULT_STYLES, +} from "./lib/defaults"; + import type { LogOutputs, LogContext, @@ -6,49 +14,10 @@ import type { LogConfig, } from "./types/logger"; -import { LogLabel, LogLevel } from "./types/logger"; import type { DeepRequired } from "generic"; -const DEFAULT_OUTPUTS: DeepRequired = { - console: { enabled: true }, - tampermonkey: { - enabled: false, - maxBuckets: 10, - bucketIndexKey: "bucket_index", - }, - callback: undefined, -}; - -const DEFAULT_CONFIG: DeepRequired = { - outputs: DEFAULT_OUTPUTS, - bufferCapacity: 100_000, -}; - const MESSAGE_STYLE = "background: inherit; color: inherit;"; -const STYLES = { - trace: { - background: "#949494", - color: "#fff", - }, - debug: { - background: "#fe7bf3", - color: "#fff", - }, - info: { - background: "#65f10e", - color: "#fff", - }, - warn: { - background: "#faf200", - color: "#000", - }, - fatal: { - background: "#cc0018", - color: "#fff", - }, -}; - function getLabel(level: number) { if (level <= LogLevel.TRACE) { return LogLabel.TRACE; @@ -63,80 +32,6 @@ function getLabel(level: number) { } } -export function blobToBase64(blob: Blob) { - return new Promise((resolve, _) => { - const reader = new FileReader(); - reader.onloadend = () => resolve(btoa(reader.result as string)); - reader.readAsBinaryString(blob); - }); -} - -export async function gzip(data: string) { - const cs = new CompressionStream("gzip"); - const blob = new Blob([data]); - const compressedStream = blob.stream().pipeThrough(cs); - const gzipData = await new Response(compressedStream).blob(); - return (await blobToBase64(gzipData)) as string; -} - -export async function ungzip(base64: string) { - const b64decoded = atob(base64); - - const arrayBuffer = new ArrayBuffer(b64decoded.length); - - // Create a new Uint8Array from the ArrayBuffer - const uint8Array = new Uint8Array(arrayBuffer); - - // Copy the binary string to the Uint8Array - for (let i = 0; i < b64decoded.length; i++) { - uint8Array[i] = b64decoded.charCodeAt(i); - } - - const blobgzip = new Blob([uint8Array], { - type: "application/octet-stream", - }); - - const ds = new DecompressionStream("gzip"); - const decompressedStream = blobgzip.stream().pipeThrough(ds); - - const originalText = await new Response(decompressedStream).text(); - return originalText; -} - -export function stringifyInstance(instance: {}) { - return JSON.stringify(objectifyInstance(instance)); -} - -export function objectifyInstance(instance: any) { - let ret: Record = {}; - if (typeof instance !== "object") { - ret[`${typeof instance}`] = instance; - } - - for (let key in instance) { - if (typeof instance[key] === "object") { - ret[key] = objectifyInstance(instance[key]); - } else if (typeof instance[key] === "function") { - ret[key] = "function"; - } else { - ret[key] = instance[key]; - } - } - - return ret; -} - -export function randomString(length: number) { - const characters = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - let result = ""; - const charactersLength = characters.length; - for (let i = 0; i < length; i++) { - result += characters.charAt(Math.floor(Math.random() * charactersLength)); - } - return result; -} - export class Logger { private buffer: string[]; private bufferLength: number; @@ -253,7 +148,7 @@ export class Logger { } private consolePrint(label: LogLabel, message: string, meta: LogMeta) { - const style = `background: ${STYLES[label].background}; color: ${STYLES[label].color}; font-weight: bold; border-radius: 4px;`; + const style = `background: ${DEFAULT_STYLES[label].background}; color: ${DEFAULT_STYLES[label].color}; font-weight: bold; border-radius: 4px;`; switch (label) { case LogLabel.TRACE: diff --git a/src/lib/defaults.ts b/src/lib/defaults.ts new file mode 100644 index 0000000..30ebe71 --- /dev/null +++ b/src/lib/defaults.ts @@ -0,0 +1,40 @@ +import type { DeepRequired } from "../types/generic"; +import type { ConsoleStyles, LogConfig, LogOutputs } from "../types/logger"; + +export const DEFAULT_OUTPUTS: DeepRequired = { + console: { enabled: true }, + tampermonkey: { + enabled: false, + maxBuckets: 10, + bucketIndexKey: "bucket_index", + }, + callback: undefined, +}; + +export const DEFAULT_CONFIG: DeepRequired = { + outputs: DEFAULT_OUTPUTS, + bufferCapacity: 100000, +}; + +export const DEFAULT_STYLES: DeepRequired = { + trace: { + background: "#949494", + color: "#fff", + }, + debug: { + background: "#fe7bf3", + color: "#fff", + }, + info: { + background: "#65f10e", + color: "#fff", + }, + warn: { + background: "#faf200", + color: "#000", + }, + fatal: { + background: "#cc0018", + color: "#fff", + }, +}; diff --git a/src/lib/utilities.ts b/src/lib/utilities.ts new file mode 100644 index 0000000..0487a83 --- /dev/null +++ b/src/lib/utilities.ts @@ -0,0 +1,73 @@ +export function blobToBase64(blob: Blob) { + return new Promise((resolve, _) => { + const reader = new FileReader(); + reader.onloadend = () => resolve(btoa(reader.result as string)); + reader.readAsBinaryString(blob); + }); +} + +export async function gzip(data: string) { + const cs = new CompressionStream("gzip"); + const blob = new Blob([data]); + const compressedStream = blob.stream().pipeThrough(cs); + const gzipData = await new Response(compressedStream).blob(); + return (await blobToBase64(gzipData)) as string; +} + +export async function ungzip(base64: string) { + const b64decoded = atob(base64); + + const arrayBuffer = new ArrayBuffer(b64decoded.length); + + // Create a new Uint8Array from the ArrayBuffer + const uint8Array = new Uint8Array(arrayBuffer); + + // Copy the binary string to the Uint8Array + for (let i = 0; i < b64decoded.length; i++) { + uint8Array[i] = b64decoded.charCodeAt(i); + } + + const blobgzip = new Blob([uint8Array], { + type: "application/octet-stream", + }); + + const ds = new DecompressionStream("gzip"); + const decompressedStream = blobgzip.stream().pipeThrough(ds); + + const originalText = await new Response(decompressedStream).text(); + return originalText; +} + +export function stringifyInstance(instance: {}) { + return JSON.stringify(objectifyInstance(instance)); +} + +export function objectifyInstance(instance: any) { + let ret: Record = {}; + if (typeof instance !== "object") { + ret[`${typeof instance}`] = instance; + } + + for (let key in instance) { + if (typeof instance[key] === "object") { + ret[key] = objectifyInstance(instance[key]); + } else if (typeof instance[key] === "function") { + ret[key] = "function"; + } else { + ret[key] = instance[key]; + } + } + + return ret; +} + +export function randomString(length: number) { + const characters = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + let result = ""; + const charactersLength = characters.length; + for (let i = 0; i < length; i++) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +} diff --git a/src/types/logger.ts b/src/types/logger.ts index 9ae1e06..ee11277 100644 --- a/src/types/logger.ts +++ b/src/types/logger.ts @@ -50,3 +50,26 @@ export interface BucketInfo { size: number; createdAt: number; } + +export interface ConsoleStyles { + trace?: { + background?: string; + color?: string; + }; + debug?: { + background?: string; + color?: string; + }; + info?: { + background?: string; + color?: string; + }; + warn?: { + background?: string; + color?: string; + }; + fatal?: { + background?: string; + color?: string; + }; +}