mirror of
https://github.com/ClaytonWWilson/stokpile.git
synced 2025-12-15 18:28:47 +00:00
Organize functions and defaults
This commit is contained in:
parent
7581c5aab1
commit
d2909950cc
123
src/Logger.ts
123
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<LogOutputs> = {
|
||||
console: { enabled: true },
|
||||
tampermonkey: {
|
||||
enabled: false,
|
||||
maxBuckets: 10,
|
||||
bucketIndexKey: "bucket_index",
|
||||
},
|
||||
callback: undefined,
|
||||
};
|
||||
|
||||
const DEFAULT_CONFIG: DeepRequired<LogConfig> = {
|
||||
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<string | ArrayBuffer>((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<string, any> = {};
|
||||
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:
|
||||
|
||||
40
src/lib/defaults.ts
Normal file
40
src/lib/defaults.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import type { DeepRequired } from "../types/generic";
|
||||
import type { ConsoleStyles, LogConfig, LogOutputs } from "../types/logger";
|
||||
|
||||
export const DEFAULT_OUTPUTS: DeepRequired<LogOutputs> = {
|
||||
console: { enabled: true },
|
||||
tampermonkey: {
|
||||
enabled: false,
|
||||
maxBuckets: 10,
|
||||
bucketIndexKey: "bucket_index",
|
||||
},
|
||||
callback: undefined,
|
||||
};
|
||||
|
||||
export const DEFAULT_CONFIG: DeepRequired<LogConfig> = {
|
||||
outputs: DEFAULT_OUTPUTS,
|
||||
bufferCapacity: 100000,
|
||||
};
|
||||
|
||||
export const DEFAULT_STYLES: DeepRequired<ConsoleStyles> = {
|
||||
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",
|
||||
},
|
||||
};
|
||||
73
src/lib/utilities.ts
Normal file
73
src/lib/utilities.ts
Normal file
@ -0,0 +1,73 @@
|
||||
export function blobToBase64(blob: Blob) {
|
||||
return new Promise<string | ArrayBuffer>((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<string, any> = {};
|
||||
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;
|
||||
}
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user