Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-extract-tarball.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@arethetypeswrong/core": patch
---

Fix "Cannot read properties of undefined (reading 'filename')" caused by fflate 0.8.3 (https://github.com/arethetypeswrong/arethetypeswrong.github.io/issues/258)
21 changes: 17 additions & 4 deletions packages/core/src/createPackage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { untar } from "@andrewbranch/untar.js";
import { Gunzip } from "fflate";
import { Gunzip, FlateErrorCode } from "fflate";
import { major, maxSatisfying, minor, valid, validRange } from "semver";
import ts from "typescript";
import { parsePackageSpec, type ParsedPackageSpec } from "./utils.js";
Expand Down Expand Up @@ -295,9 +295,22 @@ export function createPackageFromTarballData(tarball: Uint8Array): Package {

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