Add zod validation library

This commit is contained in:
Clayton Wilson 2024-03-11 05:10:44 -05:00
parent d2909950cc
commit 4b0097f941
6 changed files with 189 additions and 89 deletions

158
package-lock.json generated
View File

@ -8,8 +8,14 @@
"name": "lognautica",
"version": "0.0.1",
"license": "MIT",
"dependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"zod": "^3.22.4"
},
"devDependencies": {
"@playwright/test": "^1.42.1",
"@rollup/plugin-typescript": "^11.1.6",
"@types/node": "^20.11.25",
"@types/rollup": "^0.54.0",
"@types/tampermonkey": "^5.0.2",
"rollup": "^2.3.4",
@ -112,6 +118,45 @@
"@jridgewell/sourcemap-codec": "^1.4.14"
}
},
"node_modules/@playwright/test": {
"version": "1.42.1",
"resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.42.1.tgz",
"integrity": "sha512-Gq9rmS54mjBL/7/MvBaNOBwbfnh7beHvS6oS4srqXFcQHpQCV1+c8JXWE8VLPyRDhgS3H8x8A7hztqI9VnwrAQ==",
"dev": true,
"dependencies": {
"playwright": "1.42.1"
},
"bin": {
"playwright": "cli.js"
},
"engines": {
"node": ">=16"
}
},
"node_modules/@rollup/plugin-node-resolve": {
"version": "15.2.3",
"resolved": "https://registry.npmjs.org/@rollup/plugin-node-resolve/-/plugin-node-resolve-15.2.3.tgz",
"integrity": "sha512-j/lym8nf5E21LwBT4Df1VD6hRO2L2iwUeUmP7litikRsVp1H6NWx20NEp0Y7su+7XGc476GnXXc4kFeZNGmaSQ==",
"dependencies": {
"@rollup/pluginutils": "^5.0.1",
"@types/resolve": "1.20.2",
"deepmerge": "^4.2.2",
"is-builtin-module": "^3.2.1",
"is-module": "^1.0.0",
"resolve": "^1.22.1"
},
"engines": {
"node": ">=14.0.0"
},
"peerDependencies": {
"rollup": "^2.78.0||^3.0.0||^4.0.0"
},
"peerDependenciesMeta": {
"rollup": {
"optional": true
}
}
},
"node_modules/@rollup/plugin-typescript": {
"version": "11.1.6",
"resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-11.1.6.tgz",
@ -142,7 +187,6 @@
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.1.0.tgz",
"integrity": "sha512-XTIWOPPcpvyKI6L1NHo0lFlCyznUEyPmPY1mc3KpPVDYulHSTvyeLNVW00QTLIAFNhR3kYnJTQHeGqU4M3n09g==",
"dev": true,
"dependencies": {
"@types/estree": "^1.0.0",
"estree-walker": "^2.0.2",
@ -163,8 +207,7 @@
"node_modules/@types/estree": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz",
"integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==",
"dev": true
"integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw=="
},
"node_modules/@types/node": {
"version": "20.11.25",
@ -175,6 +218,11 @@
"undici-types": "~5.26.4"
}
},
"node_modules/@types/resolve": {
"version": "1.20.2",
"resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-1.20.2.tgz",
"integrity": "sha512-60BCwRFOZCQhDncwQdxxeOEEkbc5dIMccYLwbxsS4TUNeVECQ/pBJ0j09mrHOl/JJvpRPGwO9SvE4nR2Nb/a4Q=="
},
"node_modules/@types/rollup": {
"version": "0.54.0",
"resolved": "https://registry.npmjs.org/@types/rollup/-/rollup-0.54.0.tgz",
@ -221,6 +269,17 @@
"integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==",
"dev": true
},
"node_modules/builtin-modules": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.3.0.tgz",
"integrity": "sha512-zhaCDicdLuWN5UbN5IMnFqNMhNfo919sH85y2/ea+5Yg9TsTkeZxpL+JLbp6cgYFS4sRLp3YV4S6yDuqVWHYOw==",
"engines": {
"node": ">=6"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/chalk": {
"version": "2.4.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
@ -256,6 +315,14 @@
"integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==",
"dev": true
},
"node_modules/deepmerge": {
"version": "4.3.1",
"resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz",
"integrity": "sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/escape-string-regexp": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
@ -268,8 +335,7 @@
"node_modules/estree-walker": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz",
"integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==",
"dev": true
"integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w=="
},
"node_modules/fsevents": {
"version": "2.3.3",
@ -289,7 +355,6 @@
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
"integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==",
"dev": true,
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
@ -307,7 +372,6 @@
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz",
"integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==",
"dev": true,
"dependencies": {
"function-bind": "^1.1.2"
},
@ -315,11 +379,24 @@
"node": ">= 0.4"
}
},
"node_modules/is-builtin-module": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-3.2.1.tgz",
"integrity": "sha512-BSLE3HnV2syZ0FK0iMA/yUGplUeMmNz4AW5fnTunbCIqZi4vG3WjJT9FHMy5D69xmAYBHXQhJdALdpwVxV501A==",
"dependencies": {
"builtin-modules": "^3.3.0"
},
"engines": {
"node": ">=6"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/is-core-module": {
"version": "2.13.1",
"resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.13.1.tgz",
"integrity": "sha512-hHrIjvZsftOsvKSn2TRYl63zvxsgE0K+0mYMoH6gD4omR5IWB2KynivBQczo3+wF1cCkjzvptnI9Q0sPU66ilw==",
"dev": true,
"dependencies": {
"hasown": "^2.0.0"
},
@ -327,6 +404,11 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/is-module": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-module/-/is-module-1.0.0.tgz",
"integrity": "sha512-51ypPSPCoTEIN9dy5Oy+h4pShgJmPCygKfyRCISBI+JoWT/2oJvK8QPxmwv7b/p239jXrm9M1mlQbyKJ5A152g=="
},
"node_modules/jest-worker": {
"version": "26.6.2",
"resolved": "https://registry.npmjs.org/jest-worker/-/jest-worker-26.6.2.tgz",
@ -377,14 +459,12 @@
"node_modules/path-parse": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz",
"integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==",
"dev": true
"integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw=="
},
"node_modules/picomatch": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz",
"integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==",
"dev": true,
"engines": {
"node": ">=8.6"
},
@ -392,6 +472,50 @@
"url": "https://github.com/sponsors/jonschlinkert"
}
},
"node_modules/playwright": {
"version": "1.42.1",
"resolved": "https://registry.npmjs.org/playwright/-/playwright-1.42.1.tgz",
"integrity": "sha512-PgwB03s2DZBcNRoW+1w9E+VkLBxweib6KTXM0M3tkiT4jVxKSi6PmVJ591J+0u10LUrgxB7dLRbiJqO5s2QPMg==",
"dev": true,
"dependencies": {
"playwright-core": "1.42.1"
},
"bin": {
"playwright": "cli.js"
},
"engines": {
"node": ">=16"
},
"optionalDependencies": {
"fsevents": "2.3.2"
}
},
"node_modules/playwright-core": {
"version": "1.42.1",
"resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.42.1.tgz",
"integrity": "sha512-mxz6zclokgrke9p1vtdy/COWBH+eOZgYUVVU34C73M+4j4HLlQJHtfcqiqqxpP0o8HhMkflvfbquLX5dg6wlfA==",
"dev": true,
"bin": {
"playwright-core": "cli.js"
},
"engines": {
"node": ">=16"
}
},
"node_modules/playwright/node_modules/fsevents": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"dev": true,
"hasInstallScript": true,
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/randombytes": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
@ -405,7 +529,6 @@
"version": "1.22.8",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.8.tgz",
"integrity": "sha512-oKWePCxqpd6FlLvGV1VU0x7bkPmmCNolxzjMf4NczoDnQcIWrAF+cPtZn5i6n+RfD2d9i0tzpKnG6Yk168yIyw==",
"dev": true,
"dependencies": {
"is-core-module": "^2.13.0",
"path-parse": "^1.0.7",
@ -422,7 +545,7 @@
"version": "2.79.1",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.79.1.tgz",
"integrity": "sha512-uKxbd0IhMZOhjAiD5oAFp7BqvkA4Dv47qpOCtaNvng4HBwdbWtdOh8f5nZNuk2rp51PMGk3bzfWu5oayNEuYnw==",
"dev": true,
"devOptional": true,
"bin": {
"rollup": "dist/bin/rollup"
},
@ -513,7 +636,6 @@
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz",
"integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==",
"dev": true,
"engines": {
"node": ">= 0.4"
},
@ -563,6 +685,14 @@
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
"integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==",
"dev": true
},
"node_modules/zod": {
"version": "3.22.4",
"resolved": "https://registry.npmjs.org/zod/-/zod-3.22.4.tgz",
"integrity": "sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==",
"funding": {
"url": "https://github.com/sponsors/colinhacks"
}
}
}
}

View File

@ -7,7 +7,9 @@
"test": "tests"
},
"devDependencies": {
"@playwright/test": "^1.42.1",
"@rollup/plugin-typescript": "^11.1.6",
"@types/node": "^20.11.25",
"@types/rollup": "^0.54.0",
"@types/tampermonkey": "^5.0.2",
"rollup": "^2.3.4",
@ -33,5 +35,9 @@
"bugs": {
"url": "https://github.com/ClaytonWWilson/lognautica/issues"
},
"homepage": "https://github.com/ClaytonWWilson/lognautica#readme"
"homepage": "https://github.com/ClaytonWWilson/lognautica#readme",
"dependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"zod": "^3.22.4"
}
}

View File

@ -1,5 +1,6 @@
import { terser } from "rollup-plugin-terser";
import typescript from "@rollup/plugin-typescript";
import { nodeResolve } from "@rollup/plugin-node-resolve";
const production = !process.env.ROLLUP_WATCH;
@ -12,6 +13,7 @@ const productionBuildMinified = {
file: "dist/lognautica.min.js",
},
plugins: [
nodeResolve({ preferBuiltins: true }),
typescript({
sourceMap: false,
inlineSourceMap: false,
@ -35,6 +37,7 @@ const productionBuild = {
file: "dist/lognautica.js",
},
plugins: [
nodeResolve({ preferBuiltins: true }),
typescript({
sourceMap: false,
inlineSourceMap: false,
@ -56,6 +59,7 @@ const devBuild = {
file: "dist/lognautica.dev.js",
},
plugins: [
nodeResolve({ preferBuiltins: true }),
typescript({
sourceMap: true,
inlineSources: true,

View File

@ -1,20 +1,8 @@
import { LogLabel, LogLevel } from "./types/logger";
import { LogLabel, LogLevel, LogOutputs } from "./types/logger";
import { gzip, randomString, stringifyInstance, ungzip } from "./lib/utilities";
import {
DEFAULT_OUTPUTS,
DEFAULT_CONFIG,
DEFAULT_STYLES,
} from "./lib/defaults";
import { DEFAULT_STYLES } from "./lib/defaults";
import type {
LogOutputs,
LogContext,
BucketInfo,
LogMeta,
LogConfig,
} from "./types/logger";
import type { DeepRequired } from "generic";
import { LogContext, BucketInfo, LogMeta, LogConfig } from "./types/logger";
const MESSAGE_STYLE = "background: inherit; color: inherit;";
@ -36,44 +24,15 @@ export class Logger {
private buffer: string[];
private bufferLength: number;
private bucketIndex: BucketInfo[];
private outputs: DeepRequired<LogOutputs>;
private outputs: LogOutputs;
private bufferCapacity: number;
constructor(config: LogConfig = JSON.parse(JSON.stringify(DEFAULT_CONFIG))) {
constructor(config: Partial<LogConfig> = {}) {
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,
};
}
const parsedConfig = LogConfig.parse(config);
this.bufferCapacity = parsedConfig.bufferCapacity;
this.outputs = parsedConfig.outputs;
if (this.outputs.tampermonkey.enabled) {
this.bucketIndex = JSON.parse(

View File

@ -1,20 +1,5 @@
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,
};
import type { ConsoleStyles } from "../types/logger";
export const DEFAULT_STYLES: DeepRequired<ConsoleStyles> = {
trace: {

View File

@ -1,3 +1,5 @@
import { z } from "zod";
export enum LogLevel {
TRACE = 10,
DEBUG = 20,
@ -24,16 +26,30 @@ export interface ConsoleOutputOpts {
enabled: boolean;
}
export interface LogOutputs {
console?: ConsoleOutputOpts;
tampermonkey?: TampermonkeyOutputOpts;
callback: ((message: string) => any) | undefined;
}
export const LogOutputs = z.object({
console: z
.object({
enabled: z.boolean().default(true),
})
.default({}),
tampermonkey: z
.object({
enabled: z.boolean().default(false),
maxBuckets: z.number().default(10),
bucketIndexKey: z.string().default("bucket_index"),
})
.default({}),
callback: z.function().args(z.string()).nullish(),
});
export interface LogConfig {
outputs?: LogOutputs;
bufferCapacity?: number;
}
export type LogOutputs = z.infer<typeof LogOutputs>;
export const LogConfig = z.object({
outputs: LogOutputs.default({}),
bufferCapacity: z.number().default(100000),
});
export type LogConfig = z.infer<typeof LogConfig>;
export interface LogContext {
level?: number;