Skip to content

Commit

Permalink
feat: Adds trace types and default packs. Updates logging in lua
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodedrift committed Oct 30, 2024
1 parent 3c43972 commit c5f776f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
19 changes: 8 additions & 11 deletions src/lua/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type Options = {
permissions: {
request?: string[];
response?: string[];
captures?: Record<
capture?: Record<
string,
{
type: string;
Expand All @@ -26,7 +26,7 @@ export type Options = {

export const createSandbox = async (requestId: string, options: Options) => {
const { logger } = options;
const captureKeys = Object.keys(options.permissions?.captures ?? {});
const captureKeys = Object.keys(options.permissions?.capture ?? {});
const requestKeys = options.permissions?.request ?? [];
const responseKeys =
(options.response ? options.permissions?.response : undefined) ?? [];
Expand All @@ -35,20 +35,17 @@ export const createSandbox = async (requestId: string, options: Options) => {
// log utility
function log(message: string): void;
function log(
level: "debug" | "info" | "warn" | "error",
level: "trace" | "debug" | "info" | "warn" | "error",
message?: string
): void;
function log(...args: any[]): void {
const [level, message] =
args.length === 1 ? ["info", `${args[1]}`] : [`${args[1]}`, `${args[2]}`];
if (
level in logger &&
typeof logger[level as keyof typeof logger] === "function"
) {
logger[level as keyof typeof logger](message);
}
args.length === 1 ? ["info", `${args[0]}`] : [`${args[0]}`, `${args[1]}`];

logger.info(message);
const logFunction = logger[level as keyof typeof logger];
if (logFunction) {
logFunction(`[${requestId}] ${message}`);
}
}

const extractBody = async (httpObject: Request | Response) => {
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type MaybePromise<T> = T | Promise<T>;
type LogLevel = "debug" | "info" | "warn" | "error";

export type Logger = {
trace?: (message: string) => void;
debug: (message: string) => void;
info: (message: string) => void;
warn: (message: string) => void;
Expand Down Expand Up @@ -65,6 +66,7 @@ export type InitOptions = {
export type TasklessAPI = {
logger: Logger;
add(pack: string | Pack): void;
addDefaultPacks(): void;
flush(): Promise<void>;
flushSync(): void;
load(): Promise<{
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: >
permissions:
domains:
- ".*" # All domains
captures:
capture:
testData:
type: string
description: Test data
Expand Down

0 comments on commit c5f776f

Please sign in to comment.