add file name to assertHashMatch throw, don't download existing pack files with matching hashes
This commit is contained in:
+14
-9
@@ -69,11 +69,13 @@ async function getHash(data: Uint8Array<ArrayBuffer> | string, format: HashForma
|
||||
throw new Error('unknown hash format: ' + format)
|
||||
}
|
||||
|
||||
async function assertHashMatch(bytes: Uint8Array<ArrayBuffer>, hashFormat: HashFormat, hash: Hash) {
|
||||
async function assertHashMatch(bytes: Uint8Array<ArrayBuffer>, hashFormat: HashFormat, hash: Hash, errorFileName?:string) {
|
||||
const bytesHash = await getHash(bytes, hashFormat)
|
||||
if (bytesHash.toString() !== hash) {
|
||||
throw new Error("hash verification failed")
|
||||
if (errorFileName) throw new Error("hash verification failed on " + errorFileName)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
export async function fetchPack(packUrl: Url, packDirectoryHandle: FileSystemDirectoryHandle) {
|
||||
@@ -88,7 +90,7 @@ export async function fetchPack(packUrl: Url, packDirectoryHandle: FileSystemDir
|
||||
const packIndexURL = new URL(packMetaData.index.file, packUrl)
|
||||
const { packIndex, indexDirname: packIndexDirname } = await fetch(packIndexURL).then(async response => {
|
||||
const packIndexBytes = await assertOK(response).bytes()
|
||||
assertHashMatch(packIndexBytes, packMetaData.index["hash-format"], packMetaData.index.hash)
|
||||
await assertHashMatch(packIndexBytes, packMetaData.index["hash-format"], packMetaData.index.hash, packMetaData.index.file)
|
||||
const packIndex = parseTOML(packIndexBytes.toString()) as PackIndex
|
||||
const packIndexFileHandle = await getFileHandle(packDirectoryHandle, packMetaData.index.file, { "create": true })
|
||||
await writeFile(packIndexFileHandle, packIndexBytes)
|
||||
@@ -105,11 +107,14 @@ export async function fetchPack(packUrl: Url, packDirectoryHandle: FileSystemDir
|
||||
packFiles: {}
|
||||
}
|
||||
await forAsync(packIndex.files, async entry => {
|
||||
const fileURL = new URL(entry.file, packIndexURL)
|
||||
const fileBytes = await fetch(fileURL)
|
||||
.then(response => assertOK(response).bytes())
|
||||
assertHashMatch(fileBytes, entry["hash-format"] || packIndex["hash-format"], entry.hash)
|
||||
const fileHandle = await getFileHandle(packDirectoryHandle, packIndexDirname + "/" + entry.file, { "create": true })
|
||||
const file = await fileHandle.getFile()
|
||||
let fileBytes = await file.bytes()
|
||||
if (await assertHashMatch(fileBytes, entry["hash-format"] || packIndex["hash-format"], entry.hash)) return
|
||||
const fileURL = new URL(entry.file, packIndexURL)
|
||||
fileBytes = await fetch(fileURL)
|
||||
.then(response => assertOK(response).bytes())
|
||||
await assertHashMatch(fileBytes, entry["hash-format"] || packIndex["hash-format"], entry.hash, entry.file)
|
||||
await writeFile(fileHandle, fileBytes)
|
||||
if (entry.metafile) pack.packFiles[entry.file] = parseTOML(fileBytes.toString())
|
||||
})
|
||||
@@ -126,7 +131,7 @@ export async function loadPack(packDirectoryHandle: FileSystemDirectoryHandle, p
|
||||
const packIndexFile = await indexFileHandle.getFile()
|
||||
const packIndexBytes = await packIndexFile.bytes()
|
||||
if (!packMetaData.options || !packMetaData.options["no-internal-hashes"]) {
|
||||
assertHashMatch(packIndexBytes, packMetaData.index["hash-format"], packMetaData.index.hash)
|
||||
await assertHashMatch(packIndexBytes, packMetaData.index["hash-format"], packMetaData.index.hash, packMetaData.index.file)
|
||||
}
|
||||
const packIndexPathSegments = packMetaData.index.file.split(pathSeperatorRegex)
|
||||
packIndexPathSegments.pop()
|
||||
@@ -146,7 +151,7 @@ export async function loadPack(packDirectoryHandle: FileSystemDirectoryHandle, p
|
||||
const file = await fileHandle.getFile()
|
||||
const fileBytes = await file.bytes()
|
||||
if (!packMetaData.options || !packMetaData.options["no-internal-hashes"]) {
|
||||
assertHashMatch(fileBytes, entry["hash-format"] || packIndex["hash-format"], entry.hash)
|
||||
await assertHashMatch(fileBytes, entry["hash-format"] || packIndex["hash-format"], entry.hash, entry.file)
|
||||
}
|
||||
if (entry.metafile) pack.packFiles[entry.file] = parseTOML(fileBytes.toString())
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user