-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
67 lines (56 loc) · 1.49 KB
/
Copy pathutil.ts
File metadata and controls
67 lines (56 loc) · 1.49 KB
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
import { assertEquals } from "@std/assert/equals";
export function splitOnce(str: string, sep: string): [string, string] {
const idx = str.indexOf(sep);
if (idx === -1) return [str, ""];
return [str.slice(0, idx), str.slice(idx + sep.length)];
}
export function parseKeyValue(data: string, sep: string) {
return Object.fromEntries(
data.split("\n")
.filter(Boolean)
.map((line) => splitOnce(line, sep)),
);
}
interface DataConstructor {
new <T extends object>(data: T): Data<T>;
}
interface DataMethods {
clone(): this;
}
export type Data<T extends object> = T & DataMethods;
class DataClass {
constructor(data: object) {
Object.assign(this, data);
}
clone() {
return new (this.constructor as DataConstructor)(this);
}
}
export const Data = DataClass as DataConstructor;
export class ProgressReportingStream extends TransformStream {
bytes = 0;
constructor(report: (bytes: number) => void) {
super({
transform: (chunk, controller) => {
controller.enqueue(chunk);
this.bytes += chunk.length;
report(this.bytes);
},
});
}
}
export class LengthVerifierStream
extends TransformStream<Uint8Array, Uint8Array> {
constructor(expectedLength: number) {
let actualLength = 0;
super({
transform(chunk, controller) {
actualLength += chunk.length;
controller.enqueue(chunk);
},
flush() {
assertEquals(actualLength, expectedLength, `Length mismatch`);
},
});
}
}