-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathDuplexTracker.js
More file actions
84 lines (77 loc) · 2.3 KB
/
DuplexTracker.js
File metadata and controls
84 lines (77 loc) · 2.3 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import AbstractReaderWriter from "./AbstractReaderWriter.js";
// TODO: Alternative name: Inspector/Interceptor/...
export default class Trace extends AbstractReaderWriter {
#readerWriter;
#sealed = false;
#pathsRead = [];
#patterns = [];
#resourcesRead = Object.create(null);
#resourcesWritten = Object.create(null);
constructor(readerWriter) {
super(readerWriter.getName());
this.#readerWriter = readerWriter;
}
getResults() {
this.#sealed = true;
return {
requests: {
pathsRead: this.#pathsRead,
patterns: this.#patterns,
},
resourcesRead: this.#resourcesRead,
resourcesWritten: this.#resourcesWritten,
};
}
async _byGlob(virPattern, options, trace) {
if (this.#sealed) {
throw new Error(`Unexpected read operation after reader has been sealed`);
}
if (this.#readerWriter.resolvePattern) {
const resolvedPattern = this.#readerWriter.resolvePattern(virPattern);
this.#patterns.push(resolvedPattern);
} else if (virPattern instanceof Array) {
for (const pattern of virPattern) {
this.#patterns.push(pattern);
}
} else {
this.#patterns.push(virPattern);
}
const resources = await this.#readerWriter._byGlob(virPattern, options, trace);
for (const resource of resources) {
if (!resource.getStatInfo()?.isDirectory()) {
this.#resourcesRead[resource.getOriginalPath()] = resource;
}
}
return resources;
}
async _byPath(virPath, options, trace) {
if (this.#sealed) {
throw new Error(`Unexpected read operation after reader has been sealed`);
}
if (this.#readerWriter.resolvePath) {
const resolvedPath = this.#readerWriter.resolvePath(virPath);
if (resolvedPath) {
this.#pathsRead.push(resolvedPath);
}
} else {
this.#pathsRead.push(virPath);
}
const resource = await this.#readerWriter._byPath(virPath, options, trace);
if (resource) {
if (!resource.getStatInfo()?.isDirectory()) {
this.#resourcesRead[resource.getOriginalPath()] = resource;
}
}
return resource;
}
async _write(resource, options) {
if (this.#sealed) {
throw new Error(`Unexpected write operation after writer has been sealed`);
}
if (!resource) {
throw new Error(`Cannot write undefined resource`);
}
this.#resourcesWritten[resource.getOriginalPath()] = resource;
return this.#readerWriter.write(resource, options);
}
}