Skip to content

Commit a6df78b

Browse files
authored
Merge pull request #261 from earshinov/fix/extract-tarball-chunks
Fix "Cannot read properties of undefined (reading 'filename')"
2 parents 5616ed2 + 6d217ba commit a6df78b

4 files changed

Lines changed: 29 additions & 10 deletions

File tree

.changeset/fix-extract-tarball.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@arethetypeswrong/core": patch
3+
---
4+
5+
- Fix "Cannot read properties of undefined (reading 'filename')" caused by fflate 0.8.3 (https://github.com/arethetypeswrong/arethetypeswrong.github.io/issues/258)
6+
- Bump fflate to ^0.8.3

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@andrewbranch/untar.js": "^1.0.3",
5454
"@loaderkit/resolve": "^1.0.2",
5555
"cjs-module-lexer": "^1.2.3",
56-
"fflate": "^0.8.2",
56+
"fflate": "^0.8.3",
5757
"lru-cache": "^11.0.1",
5858
"semver": "^7.5.4",
5959
"typescript": "5.6.1-rc",

packages/core/src/createPackage.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { untar } from "@andrewbranch/untar.js";
2-
import { Gunzip } from "fflate";
2+
import { Gunzip, FlateErrorCode } from "fflate";
33
import { major, maxSatisfying, minor, valid, validRange } from "semver";
44
import ts from "typescript";
55
import { parsePackageSpec, type ParsedPackageSpec } from "./utils.js";
@@ -295,9 +295,22 @@ export function createPackageFromTarballData(tarball: Uint8Array): Package {
295295

296296
function extractTarball(tarball: Uint8Array) {
297297
// Use streaming API to work around https://github.com/101arrowz/fflate/issues/207
298-
let unzipped: Uint8Array;
299-
new Gunzip((chunk) => (unzipped = chunk)).push(tarball, /*final*/ true);
300-
const data = untar(unzipped!);
298+
const chunks: Uint8Array[] = [];
299+
try {
300+
new Gunzip((chunk) => chunks.push(chunk)).push(tarball, /*final*/ true);
301+
} catch (err: any) {
302+
// this happens for zero-padded tarballs; can safely ignore
303+
if (err.code != FlateErrorCode.InvalidHeader) {
304+
throw err;
305+
}
306+
}
307+
const unzipped = new Uint8Array(chunks.reduce((a, b) => a + b.length, 0));
308+
let offset = 0;
309+
for (const chunk of chunks) {
310+
unzipped.set(chunk, offset);
311+
offset += chunk.length;
312+
}
313+
const data = untar(unzipped);
301314
const prefix = data[0].filename.substring(0, data[0].filename.indexOf("/") + 1);
302315
const packageJsonText = data.find((f) => f.filename === `${prefix}package.json`)?.fileData;
303316
const packageJson = JSON.parse(new TextDecoder().decode(packageJsonText));

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)