@teambit/objects 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/artifacts/__bit_junit.xml +68 -0
  2. package/artifacts/preview/teambit_scope_objects-preview.js +1 -0
  3. package/dist/fixtures/version-model-extended.json +48 -0
  4. package/dist/fixtures/version-model-object.json +87 -0
  5. package/dist/index.d.ts +19 -0
  6. package/dist/index.js +371 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/models/dependencies-graph.d.ts +45 -0
  9. package/dist/models/dependencies-graph.js +106 -0
  10. package/dist/models/dependencies-graph.js.map +1 -0
  11. package/dist/models/detach-heads.d.ts +25 -0
  12. package/dist/models/detach-heads.js +84 -0
  13. package/dist/models/detach-heads.js.map +1 -0
  14. package/dist/models/export-metadata.d.ts +24 -0
  15. package/dist/models/export-metadata.js +76 -0
  16. package/dist/models/export-metadata.js.map +1 -0
  17. package/dist/models/index.d.ts +10 -0
  18. package/dist/models/index.js +125 -0
  19. package/dist/models/index.js.map +1 -0
  20. package/dist/models/lane-history.d.ts +40 -0
  21. package/dist/models/lane-history.js +117 -0
  22. package/dist/models/lane-history.js.map +1 -0
  23. package/dist/models/lane.d.ts +124 -0
  24. package/dist/models/lane.js +463 -0
  25. package/dist/models/lane.js.map +1 -0
  26. package/dist/models/model-component.d.ts +317 -0
  27. package/dist/models/model-component.js +1365 -0
  28. package/dist/models/model-component.js.map +1 -0
  29. package/dist/models/model-component.spec.d.ts +1 -0
  30. package/dist/models/model-component.spec.js +71 -0
  31. package/dist/models/model-component.spec.js.map +1 -0
  32. package/dist/models/scopeMeta.d.ts +20 -0
  33. package/dist/models/scopeMeta.js +71 -0
  34. package/dist/models/scopeMeta.js.map +1 -0
  35. package/dist/models/source.d.ts +10 -0
  36. package/dist/models/source.js +43 -0
  37. package/dist/models/source.js.map +1 -0
  38. package/dist/models/symlink.d.ts +30 -0
  39. package/dist/models/symlink.js +91 -0
  40. package/dist/models/symlink.js.map +1 -0
  41. package/dist/models/version-history.d.ts +59 -0
  42. package/dist/models/version-history.js +285 -0
  43. package/dist/models/version-history.js.map +1 -0
  44. package/dist/models/version.d.ts +279 -0
  45. package/dist/models/version.js +777 -0
  46. package/dist/models/version.js.map +1 -0
  47. package/dist/models/version.spec.d.ts +1 -0
  48. package/dist/models/version.spec.js +340 -0
  49. package/dist/models/version.spec.js.map +1 -0
  50. package/dist/objects/bit-object-list.d.ts +24 -0
  51. package/dist/objects/bit-object-list.js +65 -0
  52. package/dist/objects/bit-object-list.js.map +1 -0
  53. package/dist/objects/index.d.ts +5 -0
  54. package/dist/objects/index.js +60 -0
  55. package/dist/objects/index.js.map +1 -0
  56. package/dist/objects/object-list-to-graph.d.ts +13 -0
  57. package/dist/objects/object-list-to-graph.js +93 -0
  58. package/dist/objects/object-list-to-graph.js.map +1 -0
  59. package/dist/objects/object-list.d.ts +52 -0
  60. package/dist/objects/object-list.js +369 -0
  61. package/dist/objects/object-list.js.map +1 -0
  62. package/dist/objects/object.d.ts +35 -0
  63. package/dist/objects/object.js +190 -0
  64. package/dist/objects/object.js.map +1 -0
  65. package/dist/objects/objects-readable-generator.d.ts +31 -0
  66. package/dist/objects/objects-readable-generator.js +192 -0
  67. package/dist/objects/objects-readable-generator.js.map +1 -0
  68. package/dist/objects/raw-object.d.ts +23 -0
  69. package/dist/objects/raw-object.js +155 -0
  70. package/dist/objects/raw-object.js.map +1 -0
  71. package/dist/objects/ref.d.ts +14 -0
  72. package/dist/objects/ref.js +45 -0
  73. package/dist/objects/ref.js.map +1 -0
  74. package/dist/objects/repository-hooks.d.ts +4 -0
  75. package/dist/objects/repository-hooks.js +56 -0
  76. package/dist/objects/repository-hooks.js.map +1 -0
  77. package/dist/objects/repository.d.ts +148 -0
  78. package/dist/objects/repository.js +842 -0
  79. package/dist/objects/repository.js.map +1 -0
  80. package/dist/objects/scope-index.d.ts +73 -0
  81. package/dist/objects/scope-index.js +251 -0
  82. package/dist/objects/scope-index.js.map +1 -0
  83. package/dist/objects/scope-index.spec.d.ts +1 -0
  84. package/dist/objects/scope-index.spec.js +152 -0
  85. package/dist/objects/scope-index.spec.js.map +1 -0
  86. package/dist/objects.aspect.d.ts +2 -0
  87. package/dist/objects.aspect.js +18 -0
  88. package/dist/objects.aspect.js.map +1 -0
  89. package/dist/objects.main.runtime.d.ts +7 -0
  90. package/dist/objects.main.runtime.js +36 -0
  91. package/dist/objects.main.runtime.js.map +1 -0
  92. package/dist/preview-1736824735631.js +7 -0
  93. package/fixtures/version-model-extended.json +48 -0
  94. package/fixtures/version-model-object.json +87 -0
  95. package/models/dependencies-graph.ts +119 -0
  96. package/models/detach-heads.ts +79 -0
  97. package/models/export-metadata.ts +57 -0
  98. package/models/index.ts +11 -0
  99. package/models/lane-history.ts +106 -0
  100. package/models/lane.ts +367 -0
  101. package/models/model-component.spec.ts +55 -0
  102. package/models/model-component.ts +1367 -0
  103. package/models/scopeMeta.ts +60 -0
  104. package/models/source.ts +32 -0
  105. package/models/symlink.ts +66 -0
  106. package/models/version-history.ts +266 -0
  107. package/models/version.spec.ts +288 -0
  108. package/models/version.ts +818 -0
  109. package/objects/bit-object-list.ts +59 -0
  110. package/objects/index.ts +6 -0
  111. package/objects/object-list-to-graph.ts +69 -0
  112. package/objects/object-list.ts +313 -0
  113. package/objects/object.ts +153 -0
  114. package/objects/objects-readable-generator.ts +167 -0
  115. package/objects/raw-object.ts +142 -0
  116. package/objects/ref.ts +45 -0
  117. package/objects/repository-hooks.ts +42 -0
  118. package/objects/repository.ts +753 -0
  119. package/objects/scope-index.spec.ts +95 -0
  120. package/objects/scope-index.ts +192 -0
  121. package/package.json +98 -0
  122. package/types/asset.d.ts +41 -0
  123. package/types/style.d.ts +42 -0
@@ -0,0 +1,753 @@
1
+ import fs from 'fs-extra';
2
+ import uidNumber from 'uid-number';
3
+ import { Mutex } from 'async-mutex';
4
+ import { compact, uniqBy, differenceWith, isEqual } from 'lodash';
5
+ import { BitError } from '@teambit/bit-error';
6
+ import { ComponentID } from '@teambit/component-id';
7
+ import { HASH_SIZE, isSnap } from '@teambit/component-version';
8
+ import * as path from 'path';
9
+ import { pMapPool } from '@teambit/toolbox.promise.map-pool';
10
+ import { OBJECTS_DIR } from '@teambit/legacy.constants';
11
+ import { logger } from '@teambit/legacy.logger';
12
+ import { glob, writeFile, ChownOptions, PathOsBasedAbsolute } from '@teambit/legacy.utils';
13
+ import { removeEmptyDir } from '@teambit/toolbox.fs.remove-empty-dir';
14
+ import { concurrentIOLimit } from '@teambit/harmony.modules.concurrency';
15
+ import {
16
+ Types,
17
+ HashNotFound,
18
+ OutdatedIndexJson,
19
+ ScopeJson,
20
+ UnmergedComponents,
21
+ RemoteLanes,
22
+ } from '@teambit/legacy.scope';
23
+ import { ScopeIndex, IndexType, IndexItem } from './scope-index';
24
+ import BitObject from './object';
25
+ import { ObjectItem, ObjectList } from './object-list';
26
+ import BitRawObject from './raw-object';
27
+ import Ref from './ref';
28
+ import { ContentTransformer, onPersist, onRead } from './repository-hooks';
29
+ import { getMaxSizeForObjects, InMemoryCache, createInMemoryCache } from '@teambit/harmony.modules.in-memory-cache';
30
+ import { ScopeMeta, Lane, ModelComponent } from '../models';
31
+
32
+ const OBJECTS_BACKUP_DIR = `${OBJECTS_DIR}.bak`;
33
+ const TRASH_DIR = 'trash';
34
+
35
+ export default class Repository {
36
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
37
+ objects: { [key: string]: BitObject } = {};
38
+ objectsToRemove: Ref[] = [];
39
+ scopeJson: ScopeJson;
40
+ onRead: ContentTransformer;
41
+ onPersist: ContentTransformer;
42
+ scopePath: string;
43
+ scopeIndex: ScopeIndex;
44
+ protected cache: InMemoryCache<BitObject>;
45
+ remoteLanes!: RemoteLanes;
46
+ unmergedComponents!: UnmergedComponents;
47
+ persistMutex = new Mutex();
48
+ constructor(scopePath: string, scopeJson: ScopeJson) {
49
+ this.scopePath = scopePath;
50
+ this.scopeJson = scopeJson;
51
+ this.onRead = onRead(scopePath, scopeJson);
52
+ this.onPersist = onPersist(scopePath, scopeJson);
53
+ this.cache = createInMemoryCache({ maxSize: getMaxSizeForObjects() });
54
+ }
55
+
56
+ static async load({ scopePath, scopeJson }: { scopePath: string; scopeJson: ScopeJson }): Promise<Repository> {
57
+ const repository = new Repository(scopePath, scopeJson);
58
+ await repository.init();
59
+ return repository;
60
+ }
61
+
62
+ async init() {
63
+ const scopeIndex = await this.loadOptionallyCreateScopeIndex();
64
+ this.scopeIndex = scopeIndex;
65
+ this.remoteLanes = new RemoteLanes(this.scopePath);
66
+ this.unmergedComponents = await UnmergedComponents.load(this.scopePath);
67
+ }
68
+
69
+ static async create({ scopePath, scopeJson }: { scopePath: string; scopeJson: ScopeJson }): Promise<Repository> {
70
+ const repository = new Repository(scopePath, scopeJson);
71
+ const scopeIndex = ScopeIndex.create(scopePath);
72
+ repository.scopeIndex = scopeIndex;
73
+ repository.unmergedComponents = await UnmergedComponents.load(scopePath);
74
+ repository.remoteLanes = new RemoteLanes(scopePath);
75
+ return repository;
76
+ }
77
+
78
+ static reset(scopePath: string): Promise<void> {
79
+ return ScopeIndex.reset(scopePath);
80
+ }
81
+
82
+ static getPathByScopePath(scopePath: string) {
83
+ return path.join(scopePath, OBJECTS_DIR);
84
+ }
85
+
86
+ static onPostObjectsPersist: () => Promise<void>;
87
+
88
+ async reLoadScopeIndex() {
89
+ this.scopeIndex = await this.loadOptionallyCreateScopeIndex();
90
+ }
91
+
92
+ /**
93
+ * current scope index difference with <scope_folder>/index.json content, reload it
94
+ * @deprecated use Scope aspect `watchSystemFiles` instead, it's way more efficient.
95
+ */
96
+ public async reloadScopeIndexIfNeed(force = false) {
97
+ const latestScopeIndex = await this.loadOptionallyCreateScopeIndex();
98
+ if (force) {
99
+ this.scopeIndex = latestScopeIndex;
100
+ return;
101
+ }
102
+
103
+ const currentAllScopeIndexItems = this.scopeIndex.getAll();
104
+ const latestAllScopeIndexItems = latestScopeIndex.getAll();
105
+
106
+ if (currentAllScopeIndexItems.length !== latestAllScopeIndexItems.length) {
107
+ this.scopeIndex = latestScopeIndex;
108
+ return;
109
+ }
110
+
111
+ if (differenceWith(currentAllScopeIndexItems, latestAllScopeIndexItems, isEqual).length) {
112
+ this.scopeIndex = latestScopeIndex;
113
+ }
114
+ }
115
+
116
+ ensureDir() {
117
+ return fs.ensureDir(this.getPath());
118
+ }
119
+
120
+ getPath() {
121
+ return Repository.getPathByScopePath(this.scopePath);
122
+ }
123
+
124
+ getBackupPath(dirName?: string): string {
125
+ const backupPath = path.join(this.scopePath, OBJECTS_BACKUP_DIR);
126
+ return dirName ? path.join(backupPath, dirName) : backupPath;
127
+ }
128
+
129
+ getTrashDir() {
130
+ return path.join(this.scopePath, TRASH_DIR);
131
+ }
132
+
133
+ getLicense(): Promise<string> {
134
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
135
+ return this.scopeJson.getPopulatedLicense();
136
+ }
137
+
138
+ async getScopeMetaObject(): Promise<ObjectItem> {
139
+ const license = await this.getLicense();
140
+ const object = ScopeMeta.fromObject({ license, name: this.scopeJson.name });
141
+ return { ref: object.hash(), buffer: await object.compress() };
142
+ }
143
+
144
+ objectPath(ref: Ref): string {
145
+ return path.join(this.getPath(), this.hashPath(ref));
146
+ }
147
+
148
+ async has(ref: Ref): Promise<boolean> {
149
+ const objectPath = this.objectPath(ref);
150
+ return fs.pathExists(objectPath);
151
+ }
152
+
153
+ async hasMultiple(refs: Ref[]): Promise<Ref[]> {
154
+ const concurrency = concurrentIOLimit();
155
+ const existingRefs = await pMapPool(
156
+ refs,
157
+ async (ref) => {
158
+ const pathExists = await this.has(ref);
159
+ return pathExists ? ref : null;
160
+ },
161
+ { concurrency }
162
+ );
163
+ return compact(existingRefs);
164
+ }
165
+
166
+ async load(ref: Ref, throws = false): Promise<BitObject> {
167
+ // during tag, the updated objects are in `this.objects`.
168
+ // `this.cache` is less reliable, because if it reaches its max, then it loads from the filesystem, which may not
169
+ // be there yet (in case of "version" object), or may be out-of-date (in case of "component" object).
170
+ const inMemoryObjects = this.objects[ref.hash.toString()];
171
+ if (inMemoryObjects) return inMemoryObjects;
172
+ if (ref.hash.length < HASH_SIZE) {
173
+ ref = await this.getFullRefFromShortHash(ref);
174
+ }
175
+ const cached = this.getCache(ref);
176
+ if (cached) {
177
+ return cached;
178
+ }
179
+ let fileContentsRaw: Buffer;
180
+ const objectPath = this.objectPath(ref);
181
+ try {
182
+ fileContentsRaw = await fs.readFile(objectPath);
183
+ } catch (err: any) {
184
+ if (err.code !== 'ENOENT') {
185
+ logger.error(`Failed reading a ref file ${objectPath}. Error: ${err.message}`);
186
+ throw err;
187
+ }
188
+ logger.trace(`Failed finding a ref file ${objectPath}.`);
189
+ if (throws) {
190
+ // if we just `throw err` we loose the stack trace.
191
+ // see https://stackoverflow.com/questions/68022123/no-stack-in-fs-promises-readfile-enoent-error
192
+ const msg = `fatal: failed finding an object file ${objectPath} in the filesystem at ${err.path}`;
193
+ throw Object.assign(err, { stack: new Error(msg).stack });
194
+ }
195
+ // @ts-ignore @todo: fix! it should return BitObject | null.
196
+ return null;
197
+ }
198
+ const size = fileContentsRaw.byteLength;
199
+ const fileContents = this.onRead(fileContentsRaw);
200
+ // uncomment to debug the transformed objects by onRead
201
+ // console.log('transformedContent load', ref.toString(), BitObject.parseSync(fileContents).getType());
202
+ const parsedObject = await BitObject.parseObject(fileContents, objectPath);
203
+ const maxSizeToCache = 100 * 1024; // 100KB
204
+ if (size < maxSizeToCache) {
205
+ // don't cache big files (mainly artifacts) to prevent out-of-memory
206
+ this.setCache(parsedObject);
207
+ }
208
+ return parsedObject;
209
+ }
210
+
211
+ /**
212
+ * this is restricted to provide objects according to the given types. Otherwise, big scopes (>1GB) could crush.
213
+ * example usage: `this.list([ModelComponent, Symlink, Lane])`
214
+ */
215
+ async list(types: Types): Promise<BitObject[]> {
216
+ const refs = await this.listRefs();
217
+ const concurrency = concurrentIOLimit();
218
+ logger.debug(
219
+ `Repository.list, ${refs.length} refs are going to be loaded, searching for types: ${types.map((t) => t.name).join(', ')}`
220
+ );
221
+ const objects: BitObject[] = [];
222
+ const loadGracefully = process.argv.includes('--never-exported');
223
+ const isTypeIncluded = (obj: BitObject) => types.some((type) => type.name === obj.constructor.name); // avoid using "obj instanceof type" for Harmony to call this function successfully
224
+ await pMapPool(
225
+ refs,
226
+ async (ref) => {
227
+ const object = loadGracefully
228
+ ? await this.loadRefDeleteIfInvalid(ref)
229
+ : await this.loadRefOnlyIfType(ref, types);
230
+ if (!object) return;
231
+ if (loadGracefully && !isTypeIncluded(object)) return;
232
+ objects.push(object);
233
+ },
234
+ {
235
+ concurrency,
236
+ onCompletedChunk: (completed) => {
237
+ if (completed % 1000 === 0) logger.debug(`Repository.list, completed ${completed} out of ${refs.length}`);
238
+ },
239
+ }
240
+ );
241
+ return objects;
242
+ }
243
+
244
+ async loadRefDeleteIfInvalid(ref: Ref) {
245
+ try {
246
+ return await this.load(ref, true);
247
+ } catch (err: any) {
248
+ // this is needed temporarily to allow `bit reset --never-exported` to fix the bit-id-comp-id error.
249
+ // in a few months, we can remove this condition (around min 2024)
250
+ if (err.constructor.name === 'BitIdCompIdError' || err.constructor.name === 'MissingScope') {
251
+ logger.debug(`bit-id-comp-id error, moving an object to trash ${ref.toString()}`);
252
+ await this.moveOneObjectToTrash(ref);
253
+ return undefined;
254
+ }
255
+ throw err;
256
+ }
257
+ }
258
+
259
+ async loadRefOnlyIfType(ref: Ref, types: Types): Promise<BitObject | null> {
260
+ const objectPath = this.objectPath(ref);
261
+ const fileContentsRaw = await fs.readFile(objectPath);
262
+ const fileContents = this.onRead(fileContentsRaw);
263
+ const typeNames = types.map((type) => type.name);
264
+ const parsedObject = await BitObject.parseObjectOnlyIfType(fileContents, typeNames, objectPath);
265
+ return parsedObject;
266
+ }
267
+
268
+ async listRefs(cwd = this.getPath()): Promise<Array<Ref>> {
269
+ const matches = await glob(path.join('*', '*'), { cwd });
270
+ const refs = matches.map((str) => {
271
+ const hash = str.replace(path.sep, '');
272
+ if (!isSnap(hash)) {
273
+ logger.error(`fatal: the file "${str}" is not a valid bit object path`);
274
+ return null;
275
+ }
276
+ return new Ref(hash);
277
+ });
278
+ return compact(refs);
279
+ }
280
+
281
+ async listRefsStartWith(shortHash: Ref): Promise<Array<Ref>> {
282
+ const pathPrefix = this.hashPath(shortHash);
283
+ const matches = await glob(`${pathPrefix}*`, { cwd: this.getPath() });
284
+ const refs = matches.map((str) => {
285
+ const hash = str.replace(path.sep, '');
286
+ if (!isSnap(hash)) {
287
+ logger.error(`fatal: the file "${str}" is not a valid bit object path`);
288
+ return null;
289
+ }
290
+ return new Ref(hash);
291
+ });
292
+ return compact(refs);
293
+ }
294
+
295
+ async listRawObjects(): Promise<any> {
296
+ const refs = await this.listRefs();
297
+ const concurrency = concurrentIOLimit();
298
+ return pMapPool(
299
+ refs,
300
+ async (ref) => {
301
+ try {
302
+ const buffer = await this.loadRaw(ref);
303
+ const bitRawObject = await BitRawObject.fromDeflatedBuffer(buffer, ref.hash);
304
+ return bitRawObject;
305
+ } catch {
306
+ logger.error(`Couldn't load the ref ${ref} this object is probably corrupted and should be delete`);
307
+ return null;
308
+ }
309
+ },
310
+ { concurrency }
311
+ );
312
+ }
313
+
314
+ async listObjectsFromIndex(indexType: IndexType, filter?: Function): Promise<BitObject[]> {
315
+ const hashes = filter ? this.scopeIndex.getHashesByQuery(indexType, filter) : this.scopeIndex.getHashes(indexType);
316
+ return this._getBitObjectsByHashes(hashes);
317
+ }
318
+
319
+ getHashFromIndex(indexType: IndexType, filter: Function): string | null {
320
+ const hashes = this.scopeIndex.getHashesByQuery(indexType, filter);
321
+ if (hashes.length > 2) throw new Error('getHashFromIndex expect to get zero or one result');
322
+ return hashes.length ? hashes[0] : null;
323
+ }
324
+
325
+ async _getBitObjectsByHashes(hashes: string[]): Promise<BitObject[]> {
326
+ const missingIndexItems: IndexItem[] = [];
327
+ const bitObjects = await Promise.all(
328
+ hashes.map(async (hash) => {
329
+ const bitObject = await this.load(new Ref(hash));
330
+ if (!bitObject) {
331
+ const indexItem = this.scopeIndex.find(hash);
332
+ if (!indexItem) throw new Error(`_getBitObjectsByHashes failed finding ${hash}`);
333
+ missingIndexItems.push(indexItem);
334
+ return;
335
+ }
336
+ return bitObject;
337
+ })
338
+ );
339
+ if (missingIndexItems.length) {
340
+ this.scopeIndex.removeMany(missingIndexItems.map((item) => new Ref(item.hash)));
341
+ await this.scopeIndex.write();
342
+ const missingStringified = missingIndexItems.map((item) => item.toIdentifierString());
343
+ throw new OutdatedIndexJson(missingStringified);
344
+ }
345
+ return compact(bitObjects);
346
+ }
347
+
348
+ async loadOptionallyCreateScopeIndex(): Promise<ScopeIndex> {
349
+ try {
350
+ const scopeIndex = await ScopeIndex.load(this.scopePath);
351
+ return scopeIndex;
352
+ } catch (err: any) {
353
+ if (err.code === 'ENOENT') {
354
+ const bitObjects: BitObject[] = await this.list([ModelComponent, Lane]);
355
+ const scopeIndex = ScopeIndex.create(this.scopePath);
356
+ const added = scopeIndex.addMany(bitObjects);
357
+ if (added) await scopeIndex.write();
358
+ return scopeIndex;
359
+ }
360
+ throw err;
361
+ }
362
+ }
363
+
364
+ async loadRaw(ref: Ref): Promise<Buffer> {
365
+ if (ref.hash.length < HASH_SIZE) {
366
+ ref = await this.getFullRefFromShortHash(ref);
367
+ }
368
+ const raw = await fs.readFile(this.objectPath(ref));
369
+ // Run hook to transform content pre reading
370
+ const transformedContent = this.onRead(raw);
371
+ // uncomment to debug the transformed objects by onRead
372
+ // console.log('transformedContent loadRaw', ref.toString(), BitObject.parseSync(transformedContent).getType());
373
+ return transformedContent;
374
+ }
375
+
376
+ async getFullRefFromShortHash(ref: Ref): Promise<Ref> {
377
+ const refs = await this.listRefsStartWith(ref);
378
+ if (refs.length > 1) {
379
+ throw new Error(
380
+ `found ${refs.length} objects with the same short hash ${ref.toString()}, please use longer hash`
381
+ );
382
+ }
383
+ if (refs.length === 0) {
384
+ throw new Error(`failed finding an object with the short hash ${ref.toString()}`);
385
+ }
386
+ return refs[0];
387
+ }
388
+
389
+ async loadManyRaw(refs: Ref[]): Promise<ObjectItem[]> {
390
+ const concurrency = concurrentIOLimit();
391
+ const uniqRefs = uniqBy(refs, 'hash');
392
+ return pMapPool(uniqRefs, async (ref) => ({ ref, buffer: await this.loadRaw(ref) }), { concurrency });
393
+ }
394
+
395
+ async loadManyRawIgnoreMissing(refs: Ref[]): Promise<ObjectItem[]> {
396
+ const concurrency = concurrentIOLimit();
397
+ const results = await pMapPool(
398
+ refs,
399
+ async (ref) => {
400
+ try {
401
+ const buffer = await this.loadRaw(ref);
402
+ return { ref, buffer };
403
+ } catch (err: any) {
404
+ if (err.code === 'ENOENT') return null;
405
+ throw err;
406
+ }
407
+ },
408
+ { concurrency }
409
+ );
410
+ return compact(results);
411
+ }
412
+
413
+ async loadRawObject(ref: Ref): Promise<BitRawObject> {
414
+ const buffer = await this.loadRaw(ref);
415
+ const bitRawObject = await BitRawObject.fromDeflatedBuffer(buffer, ref.hash);
416
+ return bitRawObject as any as BitRawObject;
417
+ }
418
+
419
+ /**
420
+ * prefer using `this.load()` for an async version, which also writes to the cache
421
+ */
422
+ loadSync(ref: Ref, throws = true): BitObject {
423
+ try {
424
+ const objectFile = fs.readFileSync(this.objectPath(ref));
425
+ // Run hook to transform content pre reading
426
+ const transformedContent = this.onRead(objectFile);
427
+ return BitObject.parseSync(transformedContent);
428
+ } catch {
429
+ if (throws) {
430
+ throw new HashNotFound(ref.toString());
431
+ }
432
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
433
+ return null;
434
+ }
435
+ }
436
+
437
+ setCache(object: BitObject) {
438
+ this.cache.set(object.hash().toString(), object);
439
+ return this;
440
+ }
441
+
442
+ getCache(ref: Ref): BitObject | undefined {
443
+ return this.cache.get(ref.toString());
444
+ }
445
+
446
+ removeFromCache(ref: Ref) {
447
+ this.cache.delete(ref.toString());
448
+ }
449
+
450
+ async clearCache() {
451
+ logger.debug('repository.clearCache');
452
+ this.cache.deleteAll();
453
+ await this.init();
454
+ }
455
+ clearObjectsFromCache() {
456
+ logger.debug('repository.clearObjectsFromCache');
457
+ this.cache.deleteAll();
458
+ }
459
+
460
+ backup(dirName?: string) {
461
+ const backupDir = this.getBackupPath(dirName);
462
+ const objectsDir = this.getPath();
463
+ logger.debug(`making a backup of all objects from ${objectsDir} to ${backupDir}`);
464
+ fs.emptyDirSync(backupDir);
465
+ fs.copySync(objectsDir, backupDir);
466
+ }
467
+
468
+ add(object: BitObject | null | undefined): Repository {
469
+ // console.trace(`repository: adding object ${object?.hash().toString()}`);
470
+ if (!object) return this;
471
+ // leave the following commented log message, it is very useful for debugging but too verbose when not needed.
472
+ // logger.debug(`repository: adding object ${object.hash().toString()} which consist of the following id: ${object.id()}`);
473
+ this.objects[object.hash().toString()] = object;
474
+ this.setCache(object);
475
+ return this;
476
+ }
477
+
478
+ addMany(objects: BitObject[]): Repository {
479
+ if (!objects || !objects.length) return this;
480
+ objects.forEach((obj) => this.add(obj));
481
+ return this;
482
+ }
483
+
484
+ removeObject(ref: Ref) {
485
+ this.objectsToRemove.push(ref);
486
+ }
487
+
488
+ removeManyObjects(refs: Ref[]) {
489
+ if (!refs || !refs.length) return;
490
+ refs.forEach((ref) => this.removeObject(ref));
491
+ }
492
+
493
+ findMany(refs: Ref[]): Promise<BitObject[]> {
494
+ return Promise.all(refs.map((ref) => this.load(ref)));
495
+ }
496
+
497
+ /**
498
+ * important! use this method only for commands that are non running on an http server.
499
+ *
500
+ * it's better to remove/delete objects directly and not using the `objects` member.
501
+ * it helps to avoid multiple processes running concurrently on an http server.
502
+ *
503
+ * persist objects changes (added and removed) into the filesystem
504
+ * do not call this function multiple times in parallel, otherwise, it'll damage the index.json file.
505
+ * call this function only once after you added and removed all applicable objects.
506
+ */
507
+ async persist(validate = true): Promise<void> {
508
+ // do not let two requests enter this critical area, otherwise, refs/index.json/objects could
509
+ // be corrupted
510
+ logger.debug(`Repository.persist, going to acquire a lock`);
511
+ await this.persistMutex.runExclusive(async () => {
512
+ logger.debug(`Repository.persist, validate = ${validate.toString()}, a lock has been acquired`);
513
+ await this.deleteObjectsFromFS(this.objectsToRemove);
514
+ this.validateObjects(validate, Object.values(this.objects));
515
+ await this.writeObjectsToTheFS(Object.values(this.objects));
516
+ await this.writeRemoteLanes();
517
+ await this.unmergedComponents.write();
518
+ });
519
+ logger.debug(`Repository.persist, completed. the lock has been released`);
520
+ this.clearObjects();
521
+ if (Repository.onPostObjectsPersist) {
522
+ Repository.onPostObjectsPersist().catch((err) => {
523
+ logger.error('fatal: onPostObjectsPersist encountered an error (this error does not stop the process)', err);
524
+ });
525
+ }
526
+ }
527
+
528
+ async writeRemoteLanes() {
529
+ await this.remoteLanes.write();
530
+ }
531
+
532
+ /**
533
+ * this is especially critical for http server, where one process lives long and serves multiple
534
+ * exports. without this, the objects get accumulated over time and being rewritten over and over
535
+ * again.
536
+ */
537
+ private clearObjects() {
538
+ this.objects = {};
539
+ this.objectsToRemove = [];
540
+ }
541
+
542
+ /**
543
+ * normally, the validation step takes place just before the acutal writing of the file.
544
+ * however, this can be an issue where a component has an invalid version. the component could
545
+ * be saved before validating the version (see #1727). that's why we validate here before writing
546
+ * anything to the filesystem.
547
+ * the open question here is whether should we validate again before the actual writing or it
548
+ * should be enough to validate here?
549
+ * for now, it does validate again before saving, only to be 100% sure nothing happens in a few
550
+ * lines of code until the actual writing. however, if the performance penalty is noticeable, we
551
+ * can easily revert it by changing `bitObject.validateBeforePersist = false` line run regardless
552
+ * the `validate` argument.
553
+ */
554
+ validateObjects(validate: boolean, objects: BitObject[]) {
555
+ objects.forEach((bitObject) => {
556
+ // @ts-ignore some BitObject classes have validate() method
557
+ if (validate && bitObject.validate) {
558
+ // @ts-ignore
559
+ bitObject.validate();
560
+ }
561
+ if (!validate) {
562
+ bitObject.validateBeforePersist = false;
563
+ }
564
+ });
565
+ }
566
+
567
+ async deleteObjectsFromFS(refs: Ref[]): Promise<void> {
568
+ if (!refs.length) return;
569
+ const uniqRefs = uniqBy(refs, 'hash');
570
+ logger.debug(`Repository._deleteMany: deleting ${uniqRefs.length} objects`);
571
+ const concurrency = concurrentIOLimit();
572
+ await pMapPool(uniqRefs, (ref) => this._deleteOne(ref), { concurrency });
573
+ const removed = this.scopeIndex.removeMany(uniqRefs);
574
+ if (removed) await this.scopeIndex.write();
575
+ }
576
+
577
+ async moveObjectsToDir(refs: Ref[], dir: string): Promise<void> {
578
+ if (!refs.length) return;
579
+ const uniqRefs = uniqBy(refs, 'hash');
580
+ logger.debug(`Repository.moveObjectsToDir: ${uniqRefs.length} objects`);
581
+ const concurrency = concurrentIOLimit();
582
+ await pMapPool(uniqRefs, (ref) => this.moveOneObjectToDir(ref, dir), { concurrency });
583
+ const removed = this.scopeIndex.removeMany(uniqRefs);
584
+ if (removed) await this.scopeIndex.write();
585
+ }
586
+
587
+ async moveObjectsToTrash(refs: Ref[]): Promise<void> {
588
+ await this.moveObjectsToDir(refs, TRASH_DIR);
589
+ }
590
+
591
+ async listTrash(): Promise<Ref[]> {
592
+ return this.listRefs(this.getTrashDir());
593
+ }
594
+
595
+ async getFromTrash(refs: Ref[]): Promise<BitObject[]> {
596
+ const objectsFromTrash = await Promise.all(
597
+ refs.map(async (ref) => {
598
+ const trashObjPath = path.join(this.getTrashDir(), this.hashPath(ref));
599
+ let buffer: Buffer;
600
+ try {
601
+ buffer = await fs.readFile(trashObjPath);
602
+ } catch (err: any) {
603
+ if (err.code === 'ENOENT') {
604
+ throw new BitError(`unable to find the object ${ref.toString()} in the trash`);
605
+ }
606
+ throw err;
607
+ }
608
+ return BitObject.parseObject(buffer, trashObjPath);
609
+ })
610
+ );
611
+ return objectsFromTrash;
612
+ }
613
+
614
+ async restoreFromTrash(refs: Ref[]) {
615
+ logger.debug(`Repository.restoreFromTrash: ${refs.length} objects`);
616
+ const objectsFromTrash = await this.getFromTrash(refs);
617
+ await this.writeObjectsToTheFS(objectsFromTrash);
618
+ }
619
+
620
+ async restoreFromDir(dir: string, overwrite = false) {
621
+ await fs.copy(path.join(this.scopePath, dir), this.getPath(), { overwrite });
622
+ }
623
+
624
+ private async moveOneObjectToDir(ref: Ref, dir: string) {
625
+ const currentPath = this.objectPath(ref);
626
+ const absDir = path.join(this.scopePath, dir);
627
+ const fullPath = path.join(absDir, this.hashPath(ref));
628
+ await fs.move(currentPath, fullPath, { overwrite: true });
629
+ this.removeFromCache(ref);
630
+ }
631
+
632
+ private async moveOneObjectToTrash(ref: Ref) {
633
+ await this.moveOneObjectToDir(ref, TRASH_DIR);
634
+ }
635
+
636
+ async deleteRecordsFromUnmergedComponents(compIds: ComponentID[]) {
637
+ this.unmergedComponents.removeMultipleComponents(compIds);
638
+ await this.unmergedComponents.write();
639
+ }
640
+
641
+ /**
642
+ * write all objects to the FS and index the components/lanes/symlink objects
643
+ */
644
+ async writeObjectsToTheFS(objects: BitObject[]): Promise<void> {
645
+ const count = objects.length;
646
+ if (!count) return;
647
+ logger.trace(`Repository.writeObjectsToTheFS: started writing ${count} objects`);
648
+ const concurrency = concurrentIOLimit();
649
+ await pMapPool(objects, (obj) => this._writeOne(obj), {
650
+ concurrency,
651
+ });
652
+ logger.trace(`Repository.writeObjectsToTheFS: completed writing ${count} objects`);
653
+
654
+ const added = this.scopeIndex.addMany(objects);
655
+ if (added) await this.scopeIndex.write();
656
+ }
657
+
658
+ /**
659
+ * do not call this method directly. always call this.removeObject() and once done with all objects,
660
+ * call this.persist()
661
+ */
662
+ _deleteOne(ref: Ref): Promise<boolean> {
663
+ this.removeFromCache(ref);
664
+ const pathToDelete = this.objectPath(ref);
665
+ logger.trace(`repository._deleteOne: deleting ${pathToDelete}`);
666
+ return removeFile(pathToDelete, true);
667
+ }
668
+
669
+ /**
670
+ * always prefer this.persist() or this.writeObjectsToTheFS()
671
+ * this method doesn't write to scopeIndex. so using this method for ModelComponent or
672
+ * Symlink makes the index outdated.
673
+ */
674
+ async _writeOne(object: BitObject): Promise<boolean> {
675
+ const contents = await object.compress();
676
+ const options: ChownOptions = {};
677
+ if (this.scopeJson.groupName) options.gid = await resolveGroupId(this.scopeJson.groupName);
678
+ const hash = object.hash();
679
+ if (this.cache.has(hash.toString())) this.cache.set(hash.toString(), object); // update the cache
680
+ const objectPath = this.objectPath(hash);
681
+ logger.trace(`repository._writeOne: ${objectPath}`);
682
+ // Run hook to transform content pre persisting
683
+ const transformedContent = this.onPersist(contents);
684
+ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
685
+ return writeFile(objectPath, transformedContent, options);
686
+ }
687
+
688
+ async writeObjectsToPendingDir(objectList: ObjectList, pendingDir: PathOsBasedAbsolute) {
689
+ const options: ChownOptions = {};
690
+ if (this.scopeJson.groupName) options.gid = await resolveGroupId(this.scopeJson.groupName);
691
+ await Promise.all(
692
+ objectList.objects.map(async (object) => {
693
+ const objPath = path.join(pendingDir, this.hashPath(object.ref));
694
+ await writeFile(objPath, object.buffer, options);
695
+ })
696
+ );
697
+ }
698
+
699
+ async readObjectsFromPendingDir(pendingDir: PathOsBasedAbsolute) {
700
+ const refs = await this.listRefs(pendingDir);
701
+ const objects = await Promise.all(
702
+ refs.map(async (ref) => {
703
+ const buffer = await fs.readFile(path.join(pendingDir, this.hashPath(ref)));
704
+ return { ref, buffer };
705
+ })
706
+ );
707
+ return new ObjectList(objects);
708
+ }
709
+
710
+ private hashPath(ref: Ref) {
711
+ const hash = ref.toString();
712
+ return path.join(hash.slice(0, 2), hash.slice(2));
713
+ }
714
+ }
715
+
716
+ async function removeFile(filePath: string, propagateDirs = false): Promise<boolean> {
717
+ try {
718
+ await fs.unlink(filePath);
719
+ } catch (err: any) {
720
+ if (err.code === 'ENOENT') {
721
+ // the file doesn't exist, that's fine, no need to do anything
722
+ return false;
723
+ }
724
+ throw err;
725
+ }
726
+ if (!propagateDirs) return true;
727
+ const { dir } = path.parse(filePath);
728
+ await removeEmptyDir(dir);
729
+ return true;
730
+ }
731
+
732
+ function resolveGroupId(groupName: string): Promise<number | null | undefined> {
733
+ return new Promise((resolve, reject) => {
734
+ uidNumber(null, groupName, (err, uid, gid) => {
735
+ if (err) {
736
+ logger.error('resolveGroupId', err);
737
+ if (err.message.includes('EPERM')) {
738
+ return reject(
739
+ new BitError(
740
+ `unable to resolve group id of "${groupName}", current user does not have sufficient permissions`
741
+ )
742
+ );
743
+ }
744
+ if (err.message.includes('group id does not exist')) {
745
+ return reject(new BitError(`unable to resolve group id of "${groupName}", the group does not exist`));
746
+ }
747
+ return reject(new BitError(`unable to resolve group id of "${groupName}", got an error ${err.message}`));
748
+ }
749
+ // on Windows it'll always be null
750
+ return resolve(gid);
751
+ });
752
+ });
753
+ }