forked from teambit/bit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects-readable-generator.ts
More file actions
158 lines (150 loc) · 6.04 KB
/
objects-readable-generator.ts
File metadata and controls
158 lines (150 loc) · 6.04 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import pMapSeries from 'p-map-series';
import { Readable } from 'stream';
import { Ref, Repository } from '.';
import { Scope } from '..';
import ShowDoctorError from '../../error/show-doctor-error';
import logger from '../../logger/logger';
import { getAllVersionHashesMemoized } from '../component-ops/traverse-versions';
import { HashMismatch } from '../exceptions';
import { Lane, ModelComponent, Version } from '../models';
import { ObjectItem } from './object-list';
export type ComponentWithCollectOptions = {
component: ModelComponent;
version: string;
collectParents: boolean;
collectParentsUntil?: Ref | null; // stop traversing when this hash found. helps to import only the delta.
collectArtifacts: boolean;
includeVersionHistory?: boolean; // send VersionHistory object if exists rather than collecting parents
};
export class ObjectsReadableGenerator {
public readable: Readable;
private pushed: string[] = [];
constructor(private repo: Repository) {
this.readable = new Readable({ objectMode: true, read() {} });
}
async pushObjectsToReadable(componentsWithOptions: ComponentWithCollectOptions[]) {
try {
await this.pushScopeMeta();
await pMapSeries(componentsWithOptions, async (componentWithOptions) =>
this.pushComponentObjects(componentWithOptions)
);
logger.debug(`pushObjectsToReadable, pushed ${this.pushed.length} objects`);
this.readable.push(null);
} catch (err: any) {
this.readable.destroy(err);
}
}
async pushLanes(lanesToFetch: Lane[]) {
try {
await Promise.all(
lanesToFetch.map(async (laneToFetch) => {
const laneBuffer = await laneToFetch.compress();
this.push({ ref: laneToFetch.hash(), buffer: laneBuffer });
})
);
this.readable.push(null);
} catch (err: any) {
this.readable.destroy(err);
}
}
async pushObjects(refs: Ref[], scope: Scope) {
try {
await pMapSeries(refs, async (ref) => {
const objectItem = await this.getObjectGracefully(ref, scope);
if (objectItem) this.push(objectItem);
});
this.readable.push(null);
} catch (err: any) {
this.readable.destroy(err);
}
}
private async getObjectGracefully(ref: Ref, scope: Scope) {
try {
return await scope.getObjectItem(ref);
} catch (err: any) {
logger.warn(
`getObjectGracefully: failed retrieving an object ${ref.toString()} from the filesystem.\n${err.message}`
);
return null;
}
}
private async pushScopeMeta() {
const scopeMeta = await this.repo.getScopeMetaObject();
this.push(scopeMeta);
}
private push(obj: ObjectItem) {
const hashStr = obj.ref.toString();
if (this.pushed.includes(hashStr)) {
return;
}
logger.trace('ObjectsReadableGenerator.push', hashStr);
this.readable.push(obj);
this.pushed.push(hashStr);
}
private pushManyObjects(objects: ObjectItem[]) {
objects.map((obj) => this.push(obj));
}
private async pushComponentObjects(componentWithOptions: ComponentWithCollectOptions): Promise<void> {
const { component, collectParents, collectArtifacts, collectParentsUntil, includeVersionHistory } =
componentWithOptions;
const version = await component.loadVersion(componentWithOptions.version, this.repo, false);
if (!version)
throw new ShowDoctorError(`failed loading version ${componentWithOptions.version} of ${component.id()}`);
if (collectParentsUntil && version.hash().isEqual(collectParentsUntil)) {
return;
}
const collectVersionObjects = async (ver: Version): Promise<ObjectItem[]> => {
const versionRefs = ver.refsWithOptions(false, collectArtifacts);
const missingVersionRefs = versionRefs.filter((ref) => !this.pushed.includes(ref.toString()));
const versionObjects = await ver.collectManyObjects(this.repo, missingVersionRefs);
const versionData = { ref: ver.hash(), buffer: await ver.asRaw(this.repo), type: ver.getType() };
return [...versionObjects, versionData];
};
try {
if (!this.pushed.includes(component.hash().toString())) {
const componentData = {
ref: component.hash(),
buffer: await component.asRaw(this.repo),
type: component.getType(),
};
this.push(componentData);
}
const allVersions: Version[] = [];
if (includeVersionHistory) {
const versionHistory = await component.getAndPopulateVersionHistory(this.repo, version.hash());
const versionHistoryData = {
ref: versionHistory.hash(),
buffer: await versionHistory.asRaw(this.repo),
type: versionHistory.getType(),
};
this.push(versionHistoryData);
}
if (collectParents) {
const allParentsHashes = await getAllVersionHashesMemoized({
modelComponent: component,
repo: this.repo,
startFrom: version.hash(),
stopAt: collectParentsUntil ? [collectParentsUntil] : undefined,
});
const missingParentsHashes = allParentsHashes.filter((h) => !h.isEqual(version.hash()));
const parentVersions = await pMapSeries(missingParentsHashes, (parentHash) => parentHash.load(this.repo));
allVersions.push(...(parentVersions as Version[]));
// note: don't bring the head. otherwise, component-delta of the head won't bring all history of this comp.
}
allVersions.push(version);
await pMapSeries(allVersions, async (ver) => {
const versionObjects = await collectVersionObjects(ver);
this.pushManyObjects(versionObjects);
});
} catch (err: any) {
logger.error(`component-version.toObjects ${componentWithOptions.component.id()} got an error`, err);
// @ts-ignore
const originalVersionHash = component.getRef(componentWithOptions.version).toString();
const currentVersionHash = version.hash().toString();
if (originalVersionHash !== currentVersionHash) {
throw new HashMismatch(component.id(), componentWithOptions.version, originalVersionHash, currentVersionHash);
}
throw err;
}
}
}