Skip to content

Commit

Permalink
feat: complete size padding
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Aug 12, 2024
1 parent 59cdfc1 commit a48b1a1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export class Pack {
this.reader.push(null)
}

private fix(size: number) {
const padding = (512 - (size % 512)) % 512
if (padding > 0) this.reader.push(new Uint8Array(padding))
}

private transport(binary: Uint8Array, resolvedOptions: EncodingHeadOptions) {
const writer = createWriteableStream({
write: (chunk, encoding, callback) => {
Expand All @@ -70,11 +75,12 @@ export class Pack {
callback()
},
final: (callback) => {
this.reader.push(null)
this.reader.push(this.fix(resolvedOptions.size))
callback()
}
})
writer.write(binary)
writer.end()
}

get receiver() {
Expand Down Expand Up @@ -163,13 +169,20 @@ export class Extract {
})
}

private removePadding(size: number) {
// Content is fixed at 512 bytes base, so we need to remove the padding.
return 512 - size % 512
}

private scan() {
try {
if (this.matrix.bytesLen === 512 * 2) {
return false
}
const head = decode(this.matrix.shift(512), this.decodeOptions)
const b = this.matrix.shift(head.size)
const offset = this.removePadding(head.size)
this.matrix.shift(offset)
this.writer.emit('entry', head, new Uint8Array(b))
return true
} catch (error) {
Expand Down

0 comments on commit a48b1a1

Please sign in to comment.