sliftutils 1.7.3 → 1.7.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sliftutils",
3
- "version": "1.7.3",
3
+ "version": "1.7.4",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -150,7 +150,11 @@ export class BlockCache {
150
150
  let lastMeta = index.blocks[runEnd - 1];
151
151
  let compEnd = index.offsets[runEnd - 1] + lastMeta.len;
152
152
  let runCompressed = rawGetRange(compStart, compEnd);
153
- // Register a per-block promise BEFORE awaiting so concurrent reads dedupe onto it.
153
+ // Hold this run's promises locally. Registering them in the LRU can evict earlier
154
+ // blocks of the same run (or of a concurrent run) before we get to await them, so
155
+ // reading them back out of the cache would silently drop blocks and return a short
156
+ // buffer. We still register in the LRU so concurrent reads dedupe onto them.
157
+ let runPromises: Promise<Buffer>[] = [];
154
158
  for (let i = runStart; i < runEnd; i++) {
155
159
  let meta = index.blocks[i];
156
160
  let relOffset = index.offsets[i] - compStart;
@@ -159,17 +163,20 @@ export class BlockCache {
159
163
  if (meta.c) return LZ4.decompress(stored);
160
164
  return stored;
161
165
  });
166
+ runPromises.push(blockPromise);
162
167
  this.touch(`${fileId}:${i}`, blockPromise);
163
168
  }
164
- for (let i = runStart; i < runEnd; i++) {
165
- let promise = this.blocks.get(`${fileId}:${i}`);
166
- if (promise) parts.push(await promise);
167
- }
169
+ for (let promise of runPromises) parts.push(await promise);
168
170
  block = runEnd;
169
171
  }
170
172
 
171
173
  let combined = parts.length === 1 && parts[0] || Buffer.concat(parts);
172
174
  let sliceStart = start - firstBlock * blockSize;
175
+ // A short combined buffer means we lost blocks; subarray would clamp and hand back
176
+ // truncated data that every caller treats as valid. Fail loudly instead.
177
+ if (combined.length < sliceStart + (end - start)) {
178
+ throw new Error(`Expected ${sliceStart + (end - start)} bytes of blocks for range [${start}, ${end}) of ${fileId}, was ${combined.length}`);
179
+ }
173
180
  return combined.subarray(sliceStart, sliceStart + (end - start));
174
181
  };
175
182
  }