From b64357605cf55edc2fdd278e057d7f0b9f665195 Mon Sep 17 00:00:00 2001 From: Simon Holloway Date: Fri, 5 Jan 2024 00:44:54 +0000 Subject: [PATCH 1/2] refactor(archive): prepare for noUncheckedIndexedAccess --- archive/_common.ts | 6 ++++-- archive/untar.ts | 13 +++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/archive/_common.ts b/archive/_common.ts index f383bb9a03e6..0f6c427b093b 100644 --- a/archive/_common.ts +++ b/archive/_common.ts @@ -71,7 +71,7 @@ struct posix_header { // byte offset }; */ -export const ustarStructure: Array<{ field: string; length: number }> = [ +export const ustarStructure = [ { field: "fileName", length: 100, @@ -136,7 +136,9 @@ export const ustarStructure: Array<{ field: string; length: number }> = [ field: "padding", length: 12, }, -]; +] as const; + +export type UstarFields = (typeof ustarStructure)[number]["field"]; export async function readBlock( reader: Reader, diff --git a/archive/untar.ts b/archive/untar.ts index bf9feec71a02..0a597f9cd2dc 100644 --- a/archive/untar.ts +++ b/archive/untar.ts @@ -34,6 +34,7 @@ import { HEADER_LENGTH, readBlock, type TarMeta, + UstarFields, ustarStructure, } from "./_common.ts"; import { readAll } from "../streams/read_all.ts"; @@ -47,9 +48,9 @@ export interface TarMetaWithLinkName extends TarMeta { linkName?: string; } -export interface TarHeader { - [key: string]: Uint8Array; -} +export type TarHeader = { + [key in UstarFields]: Uint8Array; +}; // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html#tag_20_92_13_06 // eight checksum bytes taken to be ascii spaces (decimal value 32) @@ -69,8 +70,8 @@ function trim(buffer: Uint8Array): Uint8Array { * Parse file header in a tar archive * @param length */ -function parseHeader(buffer: Uint8Array): { [key: string]: Uint8Array } { - const data: { [key: string]: Uint8Array } = {}; +function parseHeader(buffer: Uint8Array): TarHeader { + const data = {} as TarHeader; let offset = 0; ustarStructure.forEach(function (value) { const arr = buffer.subarray(offset, offset + value.length); @@ -222,7 +223,7 @@ export class Untar { // Ignore checksum header continue; } - sum += header[i]; + sum += header[i]!; } return sum; } From 2061dd5d3dcb9ed18ddaf2cffae2d0aeed307785 Mon Sep 17 00:00:00 2001 From: Simon Holloway Date: Fri, 5 Jan 2024 01:29:08 +0000 Subject: [PATCH 2/2] adjust test to handle new TarHeader requirements --- archive/untar_test.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/archive/untar_test.ts b/archive/untar_test.ts index 1f3405dd1010..9f66e809223c 100644 --- a/archive/untar_test.ts +++ b/archive/untar_test.ts @@ -332,7 +332,22 @@ Deno.test({ // Test TarEntry type const bufSizes = [1, 53, 256, 511]; const header: TarHeader = { - test: new Uint8Array(bufSizes), + fileName: new Uint8Array(bufSizes), + fileMode: new Uint8Array(bufSizes), + uid: new Uint8Array(bufSizes), + gid: new Uint8Array(bufSizes), + fileSize: new Uint8Array(bufSizes), + mtime: new Uint8Array(bufSizes), + checksum: new Uint8Array(bufSizes), + type: new Uint8Array(bufSizes), + linkName: new Uint8Array(bufSizes), + ustar: new Uint8Array(bufSizes), + owner: new Uint8Array(bufSizes), + group: new Uint8Array(bufSizes), + majorNumber: new Uint8Array(bufSizes), + minorNumber: new Uint8Array(bufSizes), + fileNamePrefix: new Uint8Array(bufSizes), + padding: new Uint8Array(bufSizes), }; const content = new TextEncoder().encode("hello tar world!"); const reader = new Buffer(content);