Skip to content

Commit 6fde86d

Browse files
fix(site): remove tar file type limitation (#7817)
1 parent cdba074 commit 6fde86d

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

site/src/utils/tar.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TarReader, TarWriter, ITarFileInfo, TarFileType } from "./tar"
1+
import { TarReader, TarWriter, ITarFileInfo, TarFileTypeCodes } from "./tar"
22

33
const mtime = 1666666666666
44

@@ -56,5 +56,5 @@ function verifyFile(
5656

5757
function verifyFolder(info: ITarFileInfo, expected: { name: string }) {
5858
expect(info.name).toEqual(expected.name)
59-
expect(info.type).toEqual(TarFileType.Dir)
59+
expect(info.type).toEqual(TarFileTypeCodes.Dir)
6060
}

site/src/utils/tar.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Based on https://github.com/gera2ld/tarjs
22
// and https://github.com/ankitrohatgi/tarballjs/blob/master/tarball.js
3-
export enum TarFileType {
4-
File = "0",
5-
Dir = "5",
3+
type TarFileType = string
4+
// Added the most common codes
5+
export const TarFileTypeCodes = {
6+
File: "0",
7+
Dir: "5",
68
}
79
const encoder = new TextEncoder()
810
const utf8Encode = (input: string) => encoder.encode(input)
@@ -131,13 +133,7 @@ export class TarReader {
131133
private readFileType(offset: number) {
132134
const typeView = new Uint8Array(this.buffer, offset + 156, 1)
133135
const typeStr = String.fromCharCode(typeView[0])
134-
if (typeStr === "0") {
135-
return TarFileType.File
136-
} else if (typeStr === "5") {
137-
return TarFileType.Dir
138-
} else {
139-
throw new Error("No supported file type")
140-
}
136+
return typeStr
141137
}
142138

143139
private readFileSize(offset: number) {
@@ -192,7 +188,7 @@ export class TarWriter {
192188
const size = (data as ArrayBuffer).byteLength ?? (file as Blob).size
193189
const item: ITarWriteItem = {
194190
name,
195-
type: TarFileType.File,
191+
type: TarFileTypeCodes.File,
196192
data,
197193
size,
198194
opts,
@@ -203,7 +199,7 @@ export class TarWriter {
203199
addFolder(name: string, opts?: Partial<ITarWriteOptions>) {
204200
this.fileData.push({
205201
name,
206-
type: TarFileType.Dir,
202+
type: TarFileTypeCodes.Dir,
207203
data: null,
208204
size: 0,
209205
opts,
@@ -315,7 +311,7 @@ export class TarWriter {
315311
const { uid, gid, mode, mtime, user, group } = {
316312
uid: 1000,
317313
gid: 1000,
318-
mode: fileType === TarFileType.File ? 0o664 : 0o775,
314+
mode: fileType === TarFileTypeCodes.File ? 0o664 : 0o775,
319315
mtime: ~~(Date.now() / 1000),
320316
user: "tarballjs",
321317
group: "tarballjs",

0 commit comments

Comments
 (0)