Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tar-stream library instead of tar-js (#360) #895

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"isomorphic-ws": "^4.0.1",
"js-sha3": "^0.8.0",
"semver": "^7.3.5",
"tar-js": "^0.3.0",
"tar-stream": "^3.1.6",
"web-streams-polyfill": "^4.0.0-beta.3",
"ws": "^8.7.0"
},
Expand All @@ -92,6 +92,7 @@
"@types/semver": "^7.3.9",
"@types/sinon": "^10.0.13",
"@types/sinon-chai": "^3.2.9",
"@types/tar-stream": "^3.1.3",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.46.0",
"@typescript-eslint/parser": "^5.46.0",
Expand Down
40 changes: 17 additions & 23 deletions src/utils/tar.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import Tar from 'tar-js'
import { pack } from 'tar-stream'
import { Collection } from '../types'
import { PassThrough } from 'stream'

// this is a workaround type so that we are able to pass in Uint8Arrays
// as string to `tar.append`
interface StringLike {
readonly length: number
charCodeAt: (index: number) => number
}

// converts a string to utf8 Uint8Array and returns it as a string-like
// object that `tar.append` accepts as path
function fixUnicodePath(path: string): StringLike {
const codes = new TextEncoder().encode(path)

return {
length: codes.length,
charCodeAt: index => codes[index],
}
}
export function makeTar(data: Collection<Uint8Array>): PassThrough {
const tar = pack()

export function makeTar(data: Collection<Uint8Array>): Uint8Array {
const tar = new Tar(1)
for (const entry of data) {
const path = fixUnicodePath(entry.path)
tar.append(path, entry.data)
tar.entry(
{
name: entry.path,
},
Buffer.from(entry.data),
)
}

return tar.out
tar.finalize()

const stream = new PassThrough()

tar.pipe(stream)

return stream
}
Loading