Skip to content

Commit

Permalink
Asar.writeFile (tests pass but ASAR is invalid)
Browse files Browse the repository at this point in the history
  • Loading branch information
lafkpages committed Oct 14, 2023
1 parent 430479e commit aabbcb2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
51 changes: 43 additions & 8 deletions src/asar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,54 @@ export class Asar extends DirectoryEntry {
return bytes.subarray(offset, offset + size);
}

writeFile(path: string, data: string | Uint8Array) {
path = normalizePath(path);
// TODO: split into chunks
writeFile(
path: string | string[],
data: string | Uint8Array,
createDirs = false
) {
if (typeof path == "string") {
path = normalizePath(path).split("/");
}

if (typeof data == "string") {
data = Buffer.from(data, "utf-8");
}

this.files[path] = {
size: data.length,
offset: "",
data,
};
// Save the file data to its entry in this.files
let currentDir = this.files;
for (const _pathChunkIndex in path) {
const pathChunkIndex = parseInt(_pathChunkIndex);
const pathChunk = path[pathChunkIndex];
const isLastChunk = pathChunkIndex == path.length - 1;

if (!(pathChunk in currentDir)) {
if (isLastChunk) {
currentDir[pathChunk]! = {
data,
size: data.length,
offset: "",
};
return;
} else if (createDirs) {
currentDir[pathChunk] = {
files: {},
};
} else {
throw new Error(
"[new Asar] File entry not found in directory structure"
);
}
}

if (isLastChunk) {
// This is the last chunk, so we can assume it's a file
(currentDir[pathChunk] as FileEntryData).data = data;
(currentDir[pathChunk] as FileEntryData).size = data.length;
(currentDir[pathChunk] as FileEntryData).offset = "";
}

currentDir = (currentDir[pathChunk] as DirectoryEntryData).files;
}
}

getData(opts: Partial<AsarGetDataOptions> = {}) {
Expand Down
6 changes: 6 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ test("Asar.writeFile [foo.txt]", () => {
asar.writeFile("foo.txt", data);
});

test("Asar.writeFile [foo/bar.txt]", () => {
const data = "Hello, world! (again)";

asar.writeFile("foo/bar.txt", data, true);
});

// Save modified ASAR
test("Asar.getData", async () => {
const {
Expand Down

0 comments on commit aabbcb2

Please sign in to comment.