Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed May 20, 2024
1 parent 93db76e commit c510189
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
1 change: 1 addition & 0 deletions functions/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ export {
export {
type AuthInput,
} from "https://dev.zaubrik.com/[email protected]/server/mod.ts";
export { queue } from "https://dev.zaubrik.com/[email protected]/generator.js";
16 changes: 16 additions & 0 deletions functions/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { join, queue } from "./deps.ts";
import { ensureSymlinkedDataDirectorySync } from "./fs.ts";

/**
* ```js
* const log = logTo("server.localhost", "./worker.log");
* log.next("aaaaaaaaa");
* log.next("bbbbbbbbb");
* ```
*/
export function logTo(dirname: string, filename: string) {
return queue(async (message: string) => {
const dataPath = join(ensureSymlinkedDataDirectorySync(dirname), filename);
await Deno.writeTextFile(dataPath, message + "\n", { append: true });
});
}
1 change: 1 addition & 0 deletions middlewares/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export {
type UrlProperties,
} from "https://dev.zaubrik.com/[email protected]/url.js";
export { copyResponse } from "https://dev.zaubrik.com/[email protected]/response.js";
export { queue } from "https://dev.zaubrik.com/[email protected]/generator.js";
export {
isEmpty,
} from "https://dev.zaubrik.com/[email protected]/collections/length.js";
Expand Down
25 changes: 2 additions & 23 deletions middlewares/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isPresent,
isUrl,
join,
queue,
} from "./deps.ts";
import { getPathnameFs, resolveMainModule } from "../functions/path.ts";

Expand All @@ -29,28 +30,6 @@ function createLog<C extends Context>(ctx: C) {
};
}

// deno-lint-ignore no-explicit-any
export function queue(f: any) {
// deno-lint-ignore no-explicit-any
async function* makeGenerator(): any {
// deno-lint-ignore no-explicit-any
let passedValue: any;
// deno-lint-ignore no-explicit-any
let result: any;
let i = 0;
while (true) {
// deno-lint-ignore no-explicit-any
passedValue = yield result as any;
// deno-lint-ignore no-explicit-any
result = await f(passedValue, i) as any;
i++;
}
}
const generator = makeGenerator();
generator.next();
return generator;
}

function logWithOptions(path: string, options: LoggerOptions) {
Deno.mkdirSync(dirname(path), { recursive: true });
return async ({ logObject, error }: {
Expand All @@ -61,7 +40,7 @@ function logWithOptions(path: string, options: LoggerOptions) {
console.error(error);
}
if (options.print) {
console.log(JSON.stringify(logObject, null, 4));
console.log(JSON.stringify(logObject, null, 2));
}
if (options.file) {
await Deno.writeTextFile(path, JSON.stringify(logObject) + "\n", {
Expand Down

0 comments on commit c510189

Please sign in to comment.