isomorphic-git 1.44.5 → 1.44.6

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.
package/index.cjs CHANGED
@@ -3374,6 +3374,69 @@ function otherVarIntDecode(reader, startWith) {
3374
3374
  return result
3375
3375
  }
3376
3376
 
3377
+ // Memory that `fromPack` may use to keep resolved objects for delta bases.
3378
+ // An object larger than a quarter of this is not kept.
3379
+ const RESOLVE_CACHE_BYTES = 32 * 1024 * 1024;
3380
+
3381
+ // A cache that drops the least recently used object when it holds too many bytes.
3382
+ class ObjectCache {
3383
+ constructor(maxBytes) {
3384
+ this.maxBytes = maxBytes;
3385
+ this.bytes = 0;
3386
+ this.map = new Map();
3387
+ }
3388
+
3389
+ get(key) {
3390
+ const value = this.map.get(key);
3391
+ if (value !== undefined) {
3392
+ this.map.delete(key);
3393
+ this.map.set(key, value);
3394
+ }
3395
+ return value
3396
+ }
3397
+
3398
+ set(key, value) {
3399
+ const size = value.object.byteLength;
3400
+ if (size > this.maxBytes / 4) return
3401
+ this.map.set(key, value);
3402
+ this.bytes += size;
3403
+ for (const [oldKey, old] of this.map) {
3404
+ if (this.bytes <= this.maxBytes) break
3405
+ this.map.delete(oldKey);
3406
+ this.bytes -= old.object.byteLength;
3407
+ }
3408
+ }
3409
+ }
3410
+
3411
+ // Resolve an object from the data that `listpack` already inflated.
3412
+ // Return null when the object needs `readSlice`. This happens when the base of
3413
+ // a delta is not in the cache, or when the delta is corrupt.
3414
+ function resolveInflated(
3415
+ { type, data, reference, offset },
3416
+ cache,
3417
+ offsetByOid
3418
+ ) {
3419
+ // `applyDelta` and `shasum` need a Buffer. This is a view and copies nothing.
3420
+ const object = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
3421
+ if (type !== 'ofs-delta' && type !== 'ref-delta') return { type, object }
3422
+ const baseOffset =
3423
+ type === 'ofs-delta'
3424
+ ? offset - decodeVarInt(new BufferCursor(reference))
3425
+ : offsetByOid.get(reference.toString('hex'));
3426
+ const base = cache.get(baseOffset);
3427
+ if (!base) return null
3428
+ try {
3429
+ return {
3430
+ type: base.type,
3431
+ object: Buffer.from(applyDelta(object, base.object)),
3432
+ }
3433
+ } catch (err) {
3434
+ // A corrupt delta. `readSlice` reports it again. Any other error is a bug.
3435
+ if (err instanceof InternalError || err instanceof RangeError) return null
3436
+ throw err
3437
+ }
3438
+ }
3439
+
3377
3440
  class GitPackIndex {
3378
3441
  constructor(stuff) {
3379
3442
  Object.assign(this, stuff);
@@ -3445,6 +3508,9 @@ class GitPackIndex {
3445
3508
  const offsets = new Map();
3446
3509
  let totalObjectCount = null;
3447
3510
  let lastPercent = null;
3511
+ // Objects that `listpack` inflated, by pack offset. A delta finds its base here.
3512
+ const cache = new ObjectCache(RESOLVE_CACHE_BYTES);
3513
+ const offsetByOid = new Map();
3448
3514
 
3449
3515
  await listpack([pack], async ({ data, type, reference, offset, num }) => {
3450
3516
  if (totalObjectCount === null) totalObjectCount = num;
@@ -3480,6 +3546,20 @@ class GitPackIndex {
3480
3546
  offset,
3481
3547
  };
3482
3548
  }
3549
+
3550
+ // The object is already inflated, so get its oid now. Then the loop below
3551
+ // does not inflate it again. That loop reads all other objects.
3552
+ const o = offsetToObject[offset];
3553
+ if (!o) return
3554
+ const resolved = resolveInflated(
3555
+ { type, data, reference, offset },
3556
+ cache,
3557
+ offsetByOid
3558
+ );
3559
+ if (!resolved) return
3560
+ o.oid = await shasum(GitObject.wrap(resolved));
3561
+ offsetByOid.set(o.oid, offset);
3562
+ cache.set(offset, resolved);
3483
3563
  });
3484
3564
 
3485
3565
  // We need to know the lengths of the slices to compute the CRCs.
@@ -3523,20 +3603,21 @@ class GitPackIndex {
3523
3603
  lastPercent = percent;
3524
3604
 
3525
3605
  const o = offsetToObject[offset];
3526
- if (o.oid) continue
3527
- try {
3528
- p.readDepth = 0;
3529
- p.externalReadDepth = 0;
3530
- const { type, object } = await p.readSlice({ start: offset });
3531
- objectsByDepth[p.readDepth] += 1;
3532
- const oid = await shasum(GitObject.wrap({ type, object }));
3533
- o.oid = oid;
3534
- hashes.push(oid);
3535
- offsets.set(oid, offset);
3536
- crcs[oid] = o.crc;
3537
- } catch (err) {
3538
- continue
3606
+ if (!o.oid) {
3607
+ // The first pass did not resolve this object, so read it.
3608
+ try {
3609
+ p.readDepth = 0;
3610
+ p.externalReadDepth = 0;
3611
+ const { type, object } = await p.readSlice({ start: offset });
3612
+ objectsByDepth[p.readDepth] += 1;
3613
+ o.oid = await shasum(GitObject.wrap({ type, object }));
3614
+ } catch (err) {
3615
+ continue
3616
+ }
3539
3617
  }
3618
+ hashes.push(o.oid);
3619
+ offsets.set(o.oid, offset);
3620
+ crcs[o.oid] = o.crc;
3540
3621
  }
3541
3622
 
3542
3623
  hashes.sort();
@@ -10129,8 +10210,8 @@ function filterCapabilities(server, client) {
10129
10210
 
10130
10211
  const pkg = {
10131
10212
  name: 'isomorphic-git',
10132
- version: '1.44.5',
10133
- agent: 'git/isomorphic-git@1.44.5',
10213
+ version: '1.44.6',
10214
+ agent: 'git/isomorphic-git@1.44.6',
10134
10215
  };
10135
10216
 
10136
10217
  class FIFO {
package/index.js CHANGED
@@ -3367,6 +3367,69 @@ function otherVarIntDecode(reader, startWith) {
3367
3367
  return result
3368
3368
  }
3369
3369
 
3370
+ // Memory that `fromPack` may use to keep resolved objects for delta bases.
3371
+ // An object larger than a quarter of this is not kept.
3372
+ const RESOLVE_CACHE_BYTES = 32 * 1024 * 1024;
3373
+
3374
+ // A cache that drops the least recently used object when it holds too many bytes.
3375
+ class ObjectCache {
3376
+ constructor(maxBytes) {
3377
+ this.maxBytes = maxBytes;
3378
+ this.bytes = 0;
3379
+ this.map = new Map();
3380
+ }
3381
+
3382
+ get(key) {
3383
+ const value = this.map.get(key);
3384
+ if (value !== undefined) {
3385
+ this.map.delete(key);
3386
+ this.map.set(key, value);
3387
+ }
3388
+ return value
3389
+ }
3390
+
3391
+ set(key, value) {
3392
+ const size = value.object.byteLength;
3393
+ if (size > this.maxBytes / 4) return
3394
+ this.map.set(key, value);
3395
+ this.bytes += size;
3396
+ for (const [oldKey, old] of this.map) {
3397
+ if (this.bytes <= this.maxBytes) break
3398
+ this.map.delete(oldKey);
3399
+ this.bytes -= old.object.byteLength;
3400
+ }
3401
+ }
3402
+ }
3403
+
3404
+ // Resolve an object from the data that `listpack` already inflated.
3405
+ // Return null when the object needs `readSlice`. This happens when the base of
3406
+ // a delta is not in the cache, or when the delta is corrupt.
3407
+ function resolveInflated(
3408
+ { type, data, reference, offset },
3409
+ cache,
3410
+ offsetByOid
3411
+ ) {
3412
+ // `applyDelta` and `shasum` need a Buffer. This is a view and copies nothing.
3413
+ const object = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
3414
+ if (type !== 'ofs-delta' && type !== 'ref-delta') return { type, object }
3415
+ const baseOffset =
3416
+ type === 'ofs-delta'
3417
+ ? offset - decodeVarInt(new BufferCursor(reference))
3418
+ : offsetByOid.get(reference.toString('hex'));
3419
+ const base = cache.get(baseOffset);
3420
+ if (!base) return null
3421
+ try {
3422
+ return {
3423
+ type: base.type,
3424
+ object: Buffer.from(applyDelta(object, base.object)),
3425
+ }
3426
+ } catch (err) {
3427
+ // A corrupt delta. `readSlice` reports it again. Any other error is a bug.
3428
+ if (err instanceof InternalError || err instanceof RangeError) return null
3429
+ throw err
3430
+ }
3431
+ }
3432
+
3370
3433
  class GitPackIndex {
3371
3434
  constructor(stuff) {
3372
3435
  Object.assign(this, stuff);
@@ -3438,6 +3501,9 @@ class GitPackIndex {
3438
3501
  const offsets = new Map();
3439
3502
  let totalObjectCount = null;
3440
3503
  let lastPercent = null;
3504
+ // Objects that `listpack` inflated, by pack offset. A delta finds its base here.
3505
+ const cache = new ObjectCache(RESOLVE_CACHE_BYTES);
3506
+ const offsetByOid = new Map();
3441
3507
 
3442
3508
  await listpack([pack], async ({ data, type, reference, offset, num }) => {
3443
3509
  if (totalObjectCount === null) totalObjectCount = num;
@@ -3473,6 +3539,20 @@ class GitPackIndex {
3473
3539
  offset,
3474
3540
  };
3475
3541
  }
3542
+
3543
+ // The object is already inflated, so get its oid now. Then the loop below
3544
+ // does not inflate it again. That loop reads all other objects.
3545
+ const o = offsetToObject[offset];
3546
+ if (!o) return
3547
+ const resolved = resolveInflated(
3548
+ { type, data, reference, offset },
3549
+ cache,
3550
+ offsetByOid
3551
+ );
3552
+ if (!resolved) return
3553
+ o.oid = await shasum(GitObject.wrap(resolved));
3554
+ offsetByOid.set(o.oid, offset);
3555
+ cache.set(offset, resolved);
3476
3556
  });
3477
3557
 
3478
3558
  // We need to know the lengths of the slices to compute the CRCs.
@@ -3516,20 +3596,21 @@ class GitPackIndex {
3516
3596
  lastPercent = percent;
3517
3597
 
3518
3598
  const o = offsetToObject[offset];
3519
- if (o.oid) continue
3520
- try {
3521
- p.readDepth = 0;
3522
- p.externalReadDepth = 0;
3523
- const { type, object } = await p.readSlice({ start: offset });
3524
- objectsByDepth[p.readDepth] += 1;
3525
- const oid = await shasum(GitObject.wrap({ type, object }));
3526
- o.oid = oid;
3527
- hashes.push(oid);
3528
- offsets.set(oid, offset);
3529
- crcs[oid] = o.crc;
3530
- } catch (err) {
3531
- continue
3599
+ if (!o.oid) {
3600
+ // The first pass did not resolve this object, so read it.
3601
+ try {
3602
+ p.readDepth = 0;
3603
+ p.externalReadDepth = 0;
3604
+ const { type, object } = await p.readSlice({ start: offset });
3605
+ objectsByDepth[p.readDepth] += 1;
3606
+ o.oid = await shasum(GitObject.wrap({ type, object }));
3607
+ } catch (err) {
3608
+ continue
3609
+ }
3532
3610
  }
3611
+ hashes.push(o.oid);
3612
+ offsets.set(o.oid, offset);
3613
+ crcs[o.oid] = o.crc;
3533
3614
  }
3534
3615
 
3535
3616
  hashes.sort();
@@ -10116,8 +10197,8 @@ function filterCapabilities(server, client) {
10116
10197
 
10117
10198
  const pkg = {
10118
10199
  name: 'isomorphic-git',
10119
- version: '1.44.5',
10120
- agent: 'git/isomorphic-git@1.44.5',
10200
+ version: '1.44.6',
10201
+ agent: 'git/isomorphic-git@1.44.6',
10121
10202
  };
10122
10203
 
10123
10204
  class FIFO {