Skip to content

Commit

Permalink
fix: should respect pause
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Sep 2, 2024
1 parent 6610d87 commit 6e7838e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
19 changes: 18 additions & 1 deletion __tests__/stream.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Writable } from 'stream'
import { Readable, Writable } from 'stream'
import fs from 'fs'
import fsp from 'fs/promises'
import path from 'path'
Expand Down Expand Up @@ -137,6 +137,23 @@ describe('Stream', () => {
await fsp.rm(targetPath, { recursive: true })
expect(c).toBe(files.length)
})
it('From browser stream', async () => {
const sourcePath = 'https://nodejs.org/dist/v22.7.0/node-v22.7.0-darwin-x64.tar.gz'
const resp = await fetch(sourcePath)
// @ts-expect-error
const reader = Readable.fromWeb(resp.body)
const extract = createExtract()
reader.pipe(zlib.createUnzip()).pipe(extract.receiver)
let errorOccurred = false
await new Promise((resolve, reject) => {
extract.on('close', resolve)
extract.on('error', (e) => {
errorOccurred = true
reject(e)
})
})
expect(errorOccurred).toBe(false)
}, { timeout: 40000 })
})
})
})
24 changes: 10 additions & 14 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ export class Pack {
} catch (error) {
callback(error as Error)
}
},
final: (callback) => {
this.reader.push(this.fix(resolvedOptions.size))
callback()
}
})
writer.write(binary, (e) => {
Expand Down Expand Up @@ -204,6 +200,7 @@ export class Extract {
this.pause = false
this.writer = createWriteableStream({
write: (chunk, _, callback) => {
// We must ensure that the chunk is enough to fill the 512 bytes
if (this.pause) {
const bb = this.matrix.shift(this.matrix.bytesLen)
const next = new Uint8Array(bb.length + chunk.length)
Expand Down Expand Up @@ -319,6 +316,11 @@ export class Extract {
}

while (this.matrix.bytesLen > 0) {
if (this.matrix.bytesLen < 512) {
this.pause = true
return
}

if (this.isNonUSTAR) {
handleNonUSTARFormat()
continue
Expand All @@ -336,16 +338,10 @@ export class Extract {
continue
}

try {
const c = this.matrix.peek(512)
if (c[0] === Magic.NULL_CHAR && c[511] === Magic.NULL_CHAR) {
this.matrix.shift(512)
continue
}
} catch (_) {
// In some case the last chunk of the file is not enough to fill the 512 bytes
this.pause = true
return
const c = this.matrix.peek(512)
if (c[0] === Magic.NULL_CHAR && c[511] === Magic.NULL_CHAR) {
this.matrix.shift(512)
continue
}

if (!decodeHead()) return
Expand Down

0 comments on commit 6e7838e

Please sign in to comment.