-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
83 lines (72 loc) · 2.3 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import type { InsertedListen, Listen } from "@kellnerd/listenbrainz/listen";
import { parseJson, parseJsonLines } from "@kellnerd/listenbrainz/parser/json";
import { assert } from "@std/assert/assert";
import { extname } from "@std/path/extname";
/** Splits the given asynchronous iterable into chunks of the given size. */
export async function* chunk<T>(
input: AsyncIterable<T>,
size: number,
): AsyncGenerator<T[]> {
assert(
Number.isInteger(size) && size >= 1,
`Expected size to be an integer greater than 0 but found ${size}`,
);
let buffer = [];
for await (const element of input) {
if (buffer.push(element) === size) {
yield buffer;
buffer = [];
}
}
if (buffer.length) {
yield buffer;
}
}
/** Logger which writes JSON messages into a JSONL file. */
export class JsonLogger {
#encoder: TextEncoder;
#output: WritableStreamDefaultWriter<Uint8Array> | undefined;
/** Logger does nothing until {@linkcode JsonLogger.open} has been called. */
constructor() {
this.#encoder = new TextEncoder();
}
/** Opens the output file at the given path (in append mode). */
async open(path: string | URL) {
const outputFile = await Deno.open(path, {
create: true,
append: true,
});
this.#output = outputFile.writable.getWriter();
await this.#output.ready;
}
/** Writes a line of stringified JSON into the output file. */
async log(json: unknown) {
if (!this.#output) return;
const line = JSON.stringify(json) + "\n";
await this.#output.write(this.#encoder.encode(line));
}
/** Closes the output file. */
async close() {
await this.#output?.close();
}
}
/** Reads listens from a JSON or JSONL file at the given path. */
export async function* readListensFile(
path: string,
): AsyncGenerator<Listen | InsertedListen> {
const extension = extname(path);
if (extension === ".jsonl") {
const inputFile = await Deno.open(path);
const input = inputFile.readable.pipeThrough(new TextDecoderStream());
for await (const listen of parseJsonLines(input)) {
yield listen;
}
} else if (extension === ".json") {
const input = await Deno.readTextFile(path);
for (const listen of parseJson(input)) {
yield listen;
}
} else {
throw new Error(`Unsupported file extension "${extension}"`);
}
}