refactor(domain): move ZIP member-region accounting and CP437 name decoding into src/domain/zip (#11475)

This commit is contained in:
morluto authored and GitHub committed 2026-11-16 05:08:26 +08:00
1 parent 88fcdd8f6d
commit ebcfbed2c5
5 files changed
+35 -22

No files matched your search

+3 -19
View File
@@ -45,7 +45,7 @@ import {
accountZipMemberRegion,
type ZipLocalRecord,
type ZipMemberRegionAccount,
} from "./ZipMemberRegion.js";
} from "../domain/zip/zipMemberRegion.js";
import type { ZipPackageFormat } from "../domain/zipPackageFormat.js";
import { artifactExpansionBudgetBytes } from "../domain/artifactExpansionBudget.js";
import { artifactReaderLimitFailure } from "./ArtifactReaderLimits.js";
@@ -53,6 +53,7 @@ import type { StableRegularFileDescriptor } from "../filesystem/RegularFile.js";
import { ArtifactFileSource } from "./ArtifactFileSource.js";
import { readFileHandleRange } from "../filesystem/readFileHandleRange.js";
import { strictUtf8Decoder } from "../domain/strictUtf8.js";
import { decodeZipCp437 } from "../domain/zip/zipEntryText.js";
import { invariantViolation } from "../domain/internalInvariant.js";
const ZIP_UTF8_DECODER = strictUtf8Decoder("preserve");
@@ -497,26 +498,9 @@ export const decodeZipEntryText = (
if (type !== "filename") return undefined;
if (encoding === "utf-8")
return decodeStrictZipUtf8(bytes, "ZIP filename is not valid UTF-8");
return encoding === "cp437" ? decodeCp437(bytes) : undefined;
return encoding === "cp437" ? decodeZipCp437(bytes) : undefined;
};
/** CP437 characters for bytes 0x80-0xFF. */
const CP437_HIGH_HALF =
"ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■\u00A0";
/**
* Decode a legacy ZIP name with the Unicode CP437 mapping, which Python's
* zipfile also uses: bytes below 0x80 are ASCII. zip.js shows 0x01-0x1F and
* 0x7F as IBM PC display glyphs, which turns the macOS folder icon file
* `Icon\r` into `Icon♪`.
*/
const decodeCp437 = (bytes: Uint8Array): string =>
Array.from(bytes, (byte) =>
byte < 0x80
? String.fromCharCode(byte)
: CP437_HIGH_HALF.charAt(byte - 0x80),
).join("");
/** An entry's name, from a valid Unicode Path extra field when it has one. */
export const zipEntryPath = (entry: Entry): string => {
const field = entry.extraFieldUnicodePath;
+1 -1
View File
@@ -9,7 +9,7 @@ import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory
import type { ArtifactReaderTraversal } from "../resources/ArtifactReader.js";
import { scanReader } from "./inventory/scanReader.js";
import { ZipArtifactReader } from "./ZipArtifactReader.js";
import { accountZipMemberRegion } from "./ZipMemberRegion.js";
import { accountZipMemberRegion } from "../domain/zip/zipMemberRegion.js";
interface Member {
readonly name: string;
+13
View File
@@ -0,0 +1,13 @@
import { expect, it } from "vitest";
import { decodeZipCp437 } from "./zipEntryText.js";
it("decodes legacy ZIP names as ASCII below 0x80 and Unicode CP437 above", () => {
// macOS folder icon file: the trailing CR stays a control character.
expect(decodeZipCp437(Uint8Array.of(0x49, 0x63, 0x6f, 0x6e, 0x0d))).toBe(
"Icon\r",
);
expect(decodeZipCp437(Uint8Array.of(0x80, 0x9e, 0xe1, 0xff))).toBe(
`Ç₧ß${String.fromCharCode(0xa0)}`,
);
});
+16
View File
@@ -0,0 +1,16 @@
/** CP437 characters for bytes 0x80-0xFF. */
const CP437_HIGH_HALF =
"ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■\u00A0";
/**
* Decode a legacy ZIP name with the Unicode CP437 mapping, which Python's
* zipfile also uses: bytes below 0x80 are ASCII. zip.js shows 0x01-0x1F and
* 0x7F as IBM PC display glyphs, which turns the macOS folder icon file
* `Icon\r` into `Icon♪`.
*/
export const decodeZipCp437 = (bytes: Uint8Array): string =>
Array.from(bytes, (byte) =>
byte < 0x80
? String.fromCharCode(byte)
: CP437_HIGH_HALF.charAt(byte - 0x80),
).join("");
@@ -1,8 +1,8 @@
import { BinaryReader } from "../domain/binaryContainer.js";
import { BinaryReader } from "../binaryContainer.js";
import {
apkSigningPairEnd,
APK_SIGNING_BLOCK_MAX_PAIRS,
} from "../domain/apkSignature/apkSigningBlock.js";
} from "../apkSignature/apkSigningBlock.js";
/** One central-directory record's claim on the member region before the directory. */
export interface ZipLocalRecord {