This commit is contained in:
Clayton Wilson 2024-03-10 22:54:40 -05:00
commit 506be76409
5 changed files with 640 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
node_modules/
dist/
/test-results/
/playwright-report/
/playwright/.cache/

22
package-lock.json generated Normal file
View File

@ -0,0 +1,22 @@
{
"name": "lognautica",
"version": "0.0.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "lognautica",
"version": "0.0.1",
"license": "MIT",
"devDependencies": {
"@types/tampermonkey": "^5.0.2"
}
},
"node_modules/@types/tampermonkey": {
"version": "5.0.2",
"resolved": "https://registry.npmjs.org/@types/tampermonkey/-/tampermonkey-5.0.2.tgz",
"integrity": "sha512-keKnYemqWts68wO2+TBdEg84iQd5vymK5SEQGxitI9uOfOnjm/ZskBoK9ZH3mnSYp79HidZMA/Y7ODQtiA/VIg==",
"dev": true
}
}
}

30
package.json Normal file
View File

@ -0,0 +1,30 @@
{
"name": "lognautica",
"version": "0.0.1",
"description": "Lightweight javascript logger with tampermonkey storage support",
"main": "Logger.ts",
"directories": {
"test": "tests"
},
"devDependencies": {
"@types/tampermonkey": "^5.0.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ClaytonWWilson/lognautica.git"
},
"keywords": [
"log",
"logging",
"tampermonkey"
],
"author": "Clayton Wilson",
"license": "MIT",
"bugs": {
"url": "https://github.com/ClaytonWWilson/lognautica/issues"
},
"homepage": "https://github.com/ClaytonWWilson/lognautica#readme"
}

470
src/Logger.ts Normal file
View File

@ -0,0 +1,470 @@
export enum LogLevel {
TRACE = 10,
DEBUG = 20,
INFO = 30,
WARN = 40,
FATAL = 50,
}
enum LogLabel {
TRACE = "trace",
DEBUG = "debug",
INFO = "info",
WARN = "warn",
FATAL = "fatal",
}
export interface TampermonkeyOutputOpts {
enabled: boolean;
maxBuckets?: number;
bucketIndexKey?: string;
}
export interface ConsoleOutputOpts {
enabled: boolean;
}
export interface LogOutputs {
console?: ConsoleOutputOpts;
tampermonkey?: TampermonkeyOutputOpts;
callback: ((message: string) => any) | undefined;
}
export interface LogConfig {
outputs?: LogOutputs;
bufferCapacity?: number;
}
export interface LogContext {
level?: number;
[key: string]: any;
}
interface LogMeta {
context: LogContext;
time: number;
}
interface BucketInfo {
name: string;
size: number;
createdAt: number;
}
const DEFAULT_OUTPUTS: Required<LogOutputs> = {
console: { enabled: true },
tampermonkey: {
enabled: false,
maxBuckets: 10,
bucketIndexKey: "bucket_index",
},
callback: undefined,
};
const DEFAULT_CONFIG: Required<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;
} else if (level <= LogLevel.DEBUG) {
return LogLabel.DEBUG;
} else if (level <= LogLevel.INFO) {
return LogLabel.INFO;
} else if (level <= LogLevel.WARN) {
return LogLabel.WARN;
} else {
return LogLabel.FATAL;
}
}
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 = {};
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;
private bucketIndex: BucketInfo[];
private outputs: Required<LogOutputs>;
private bufferCapacity: number;
constructor(config: LogConfig = JSON.parse(JSON.stringify(DEFAULT_CONFIG))) {
this.buffer = [];
this.bufferLength = 0;
this.bufferCapacity = config.bufferCapacity
? config.bufferCapacity
: DEFAULT_CONFIG.bufferCapacity;
// Parse outputs config
if (!config.outputs) {
this.outputs = JSON.parse(JSON.stringify(DEFAULT_OUTPUTS));
} else {
this.outputs = {
console: {
enabled: config.outputs.console
? config.outputs.console.enabled
: DEFAULT_OUTPUTS.console.enabled,
},
tampermonkey: {
enabled: config.outputs.tampermonkey
? config.outputs.tampermonkey.enabled
: DEFAULT_OUTPUTS.tampermonkey.enabled,
bucketIndexKey:
config.outputs.tampermonkey &&
config.outputs.tampermonkey.bucketIndexKey
? config.outputs.tampermonkey.bucketIndexKey
: DEFAULT_OUTPUTS.tampermonkey.bucketIndexKey,
maxBuckets:
config.outputs.tampermonkey &&
config.outputs.tampermonkey.maxBuckets
? config.outputs.tampermonkey.maxBuckets
: DEFAULT_OUTPUTS.tampermonkey.maxBuckets,
},
callback: config.outputs.callback ? config.outputs.callback : undefined,
};
}
if (this.outputs.tampermonkey.enabled) {
this.bucketIndex = JSON.parse(
GM_getValue(this.outputs.tampermonkey.bucketIndexKey, "[]")
);
} else {
this.bucketIndex = [];
}
}
log(message: string, context: LogContext = {}) {
const level = context.level ? context.level : LogLevel.FATAL;
const label = getLabel(level);
const meta: LogMeta = {
context,
time: new Date().valueOf(),
};
if (this.outputs.console.enabled) {
this.consolePrint(label, message, meta);
}
const textOutput = `${new Date(
meta.time
).toISOString()} [${label}] ${message} - ${stringifyInstance(
meta.context
)}`;
this.buffer.push(textOutput);
this.bufferLength += textOutput.length;
if (this.outputs.callback) {
this.outputs.callback(textOutput);
}
if (this.bufferLength >= this.bufferCapacity) {
if (this.outputs.tampermonkey.enabled) {
this.flush();
} else {
while (this.bufferLength >= this.bufferCapacity) {
let stale = this.buffer.shift();
this.bufferLength -= stale.length;
}
}
}
}
trace(message: string, context?: Object) {
this.log(message, {
level: LogLevel.TRACE,
stacktrace: new Error().stack.slice(13), // Remove the "Error\n at "
...context,
});
}
debug(message: string, context?: Object) {
this.log(message, { level: LogLevel.DEBUG, ...context });
}
info(message: string, context?: Object) {
this.log(message, { level: LogLevel.INFO, ...context });
}
warn(message: string, context?: Object) {
this.log(message, { level: LogLevel.WARN, ...context });
}
fatal(message: string, context?: Object) {
this.log(message, { level: LogLevel.FATAL, ...context });
}
private consolePrint(label: LogLabel, message: string, meta: LogMeta) {
const style = `background: ${STYLES[label].background}; color: ${STYLES[label].color}; font-weight: bold; border-radius: 4px;`;
switch (label) {
case LogLabel.TRACE:
console.trace(
`%c ${label} ` + `%c ${message}`,
style,
MESSAGE_STYLE,
meta
);
break;
case LogLabel.DEBUG:
console.debug(
`%c ${label} ` + `%c ${message}`,
style,
MESSAGE_STYLE,
meta
);
break;
case LogLabel.INFO:
console.info(
`%c ${label} ` + `%c ${message}`,
style,
MESSAGE_STYLE,
meta
);
break;
case LogLabel.WARN:
console.warn(
`%c ${label} ` + `%c ${message}`,
style,
MESSAGE_STYLE,
meta
);
break;
case LogLabel.FATAL:
console.error(
`%c ${label} ` + `%c ${message}`,
style,
MESSAGE_STYLE,
meta
);
break;
default:
console.log(
`%c ${label} ` + `%c ${message}`,
style,
MESSAGE_STYLE,
meta
);
break;
}
}
async flush() {
// Clear buffer
const stringifiedBuffer = JSON.stringify(this.buffer);
this.buffer = [];
this.bufferLength = 0;
// Don't flush unless tampermonkey output is enabled
if (!this.outputs.tampermonkey.enabled) {
return;
}
// Generate non-clashing name
let newBucketName = randomString(10).toLowerCase();
while (GM_getValue(newBucketName, undefined) !== undefined) {
newBucketName = randomString(10);
}
// GZip data
const gzipped = await gzip(stringifiedBuffer);
// Update bucketIndex with info
const newBucket: BucketInfo = {
name: newBucketName,
size: gzipped.length,
createdAt: new Date().valueOf(),
};
// Write bucketIndex to disk
this.bucketIndex.push(newBucket);
GM_setValue(
this.outputs.tampermonkey.bucketIndexKey,
JSON.stringify(this.bucketIndex)
);
// Write gzipped data to new bucket
GM_setValue(newBucketName, gzipped);
if (this.bucketIndex.length <= this.outputs.tampermonkey.maxBuckets) {
return;
}
// Delete old buckets if the number is too large
let oldBuckets = this.bucketIndex
.sort((a, b) => a.createdAt - b.createdAt)
.slice(0, -this.bufferCapacity);
oldBuckets.forEach((oldBucket) => {
GM_deleteValue(oldBucket.name);
let deleteIndex = this.bucketIndex.findIndex(
(indexBucket) => indexBucket.name === oldBucket.name
);
if (deleteIndex === -1) {
console.error("Invalid index for bucket");
return;
}
this.bucketIndex.splice(deleteIndex, 1);
});
// Update tampermonkey bucket index
GM_setValue(
this.outputs.tampermonkey.bucketIndexKey,
JSON.stringify(this.bucketIndex)
);
}
async export(amount: number) {
// Check if the buffer has the requested amount
if (this.buffer.length >= amount) {
return this.buffer.slice(this.buffer.length - amount);
}
// Only return buffer if tamppermonkey is disabled
if (!this.outputs.tampermonkey.enabled) {
return [...this.buffer];
}
let logs = [...this.buffer];
for (let i = this.bucketIndex.length - 1; i >= 0; i--) {
// Get name from index
const bucket = this.bucketIndex[i];
// Get data from bucket
const gzipped = GM_getValue(bucket.name, undefined);
if (gzipped === undefined) {
console.error("Bucket does not exist on disk", bucket);
continue;
}
// Ungzip and parse
const ungzipped = await ungzip(gzipped);
let lines: string[] = [];
try {
lines = JSON.parse(ungzipped) as string[];
} catch (err) {
// Bucket has invalid or empty data
lines = [];
}
// prepend to logs up to amount
if (logs.length + lines.length < amount) {
// Need to grab more from storage
logs.unshift(...lines);
} else if (logs.length + lines.length == amount) {
// Have the exact amount
logs.unshift(...lines);
break;
} else {
// Grab a slice of the exact amount needed
logs.unshift(...lines.slice(0, amount - logs.length));
break;
}
}
return logs;
}
async exportGzipped(amount: number) {
const lines = await this.export(amount);
const gzipped = await gzip(JSON.stringify(lines));
return gzipped;
}
}

113
tsconfig.json Normal file
View File

@ -0,0 +1,113 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
/* Projects */
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "ES2017" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */
"esModuleInterop": true,
"module": "ESNext" /* Specify what module code is generated. */,
//"rootDir": "./src", /* Specify the root folder within your source files. */
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"baseUrl": "./" /* Specify the base directory to resolve non-relative module names. */,
"paths": {
"*": ["node_modules/*", "src/types/*"]
} /* Specify a set of entries that re-map imports to additional lookup locations. */,
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
"types": ["tampermonkey"],
/* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
"resolveJsonModule": true /* Enable importing .json files. */,
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
"allowJs": true /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */,
"checkJs": true /* Enable error reporting in type-checked JavaScript files. */,
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
"outDir": "dist" /* Specify an output folder for all emitted files. */,
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": ["src", "node_modules"]
}