Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(archive): prepare for noUncheckedIndexedAccess #4110

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions archive/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct posix_header { // byte offset
};
*/

export const ustarStructure: Array<{ field: string; length: number }> = [
export const ustarStructure = [
{
field: "fileName",
length: 100,
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 7 additions & 6 deletions archive/untar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
HEADER_LENGTH,
readBlock,
type TarMeta,
UstarFields,
ustarStructure,
} from "./_common.ts";
import { readAll } from "../streams/read_all.ts";
Expand All @@ -47,9 +48,9 @@ export interface TarMetaWithLinkName extends TarMeta {
linkName?: string;
}

export interface TarHeader {
[key: string]: Uint8Array;
}
export type TarHeader = {
[key in UstarFields]: Uint8Array;
};
Comment on lines +51 to +53
Copy link
Contributor Author

@syhol syhol Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want this public type to have this slight change, we could look at creating a new non-exported type and casting the header to this new type inside getMetadata: https://github.com/denoland/deno_std/blob/b64357605cf55edc2fdd278e057d7f0b9f665195/archive/untar.ts#L256-L293

However I understand this submodule is likely to change a fair bit soon with the Streams API updates, so perhaps we don't need to be so cautious in this case?


// 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)
Expand All @@ -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);
Expand Down Expand Up @@ -222,7 +223,7 @@ export class Untar {
// Ignore checksum header
continue;
}
sum += header[i];
sum += header[i]!;
iuioiua marked this conversation as resolved.
Show resolved Hide resolved
}
return sum;
}
Expand Down
17 changes: 16 additions & 1 deletion archive/untar_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down