From 7ec22ff4459caef9de12ec6d2f8b12c306d401b5 Mon Sep 17 00:00:00 2001 From: Vladan Date: Fri, 15 Dec 2023 10:59:49 +0100 Subject: [PATCH] feat: tar-stream library instead of tar-js (#360) --- package-lock.json | 47 ++++++++++++++++++++++++++++++++++++++++++++--- package.json | 3 ++- src/utils/tar.ts | 40 +++++++++++++++++----------------------- 3 files changed, 63 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8656867c..0343acb6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,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" }, @@ -43,6 +43,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", @@ -2855,6 +2856,15 @@ "integrity": "sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==", "dev": true }, + "node_modules/@types/tar-stream": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/@types/tar-stream/-/tar-stream-3.1.3.tgz", + "integrity": "sha512-Zbnx4wpkWBMBSu5CytMbrT5ZpMiF55qgM+EpHzR4yIDu7mv52cej8hTkOc6K+LzpkOAbxwn/m7j3iO+/l42YkQ==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/ws": { "version": "8.5.3", "dev": true, @@ -3539,6 +3549,11 @@ "form-data": "^4.0.0" } }, + "node_modules/b4a": { + "version": "1.6.4", + "resolved": "https://registry.npmjs.org/b4a/-/b4a-1.6.4.tgz", + "integrity": "sha512-fpWrvyVHEKyeEvbKZTVOeZF3VSKKWtJxFIxX/jaVPf+cLbGUSitjb49pHLqPV2BUNNZ0LcoeEGfE/YCpyDYHIw==" + }, "node_modules/babel-loader": { "version": "9.1.0", "dev": true, @@ -5188,6 +5203,11 @@ "dev": true, "license": "Apache-2.0" }, + "node_modules/fast-fifo": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/fast-fifo/-/fast-fifo-1.3.2.tgz", + "integrity": "sha512-/d9sfos4yxzpwkDkuN7k2SqFKtYNmCTzgfEpz82x34IM9/zc8KGxQoXg1liNC/izpRM/MBdt44Nmx41ZWqk+FQ==" + }, "node_modules/fast-glob": { "version": "3.2.12", "dev": true, @@ -8459,6 +8479,11 @@ ], "license": "MIT" }, + "node_modules/queue-tick": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/queue-tick/-/queue-tick-1.0.1.tgz", + "integrity": "sha512-kJt5qhMxoszgU/62PLP1CJytzd2NKetjSRnyuj31fDd3Rlcz3fzlFdFLD1SItunPwyqEOkca6GbV612BWfaBag==" + }, "node_modules/quick-lru": { "version": "4.0.1", "dev": true, @@ -9245,6 +9270,15 @@ "readable-stream": "^3.5.0" } }, + "node_modules/streamx": { + "version": "2.15.6", + "resolved": "https://registry.npmjs.org/streamx/-/streamx-2.15.6.tgz", + "integrity": "sha512-q+vQL4AAz+FdfT137VF69Cc/APqUbxy+MDOImRrMvchJpigHj9GksgDU2LYbO9rx7RX6osWgxJB2WxhYv4SZAw==", + "dependencies": { + "fast-fifo": "^1.1.0", + "queue-tick": "^1.0.1" + } + }, "node_modules/string_decoder": { "version": "1.3.0", "dev": true, @@ -9473,8 +9507,15 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/tar-js": { - "version": "0.3.0" + "node_modules/tar-stream": { + "version": "3.1.6", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-3.1.6.tgz", + "integrity": "sha512-B/UyjYwPpMBv+PaFSWAmtYjwdrlEaZQEhMIBFNC5oEG8lpiW8XjcSdmEaClj28ArfKScKHs2nshz3k2le6crsg==", + "dependencies": { + "b4a": "^1.6.4", + "fast-fifo": "^1.2.0", + "streamx": "^2.15.0" + } }, "node_modules/temp-dir": { "version": "2.0.0", diff --git a/package.json b/package.json index 646f55a1..d38f0bc4 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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", diff --git a/src/utils/tar.ts b/src/utils/tar.ts index 06e3efe8..3f6594bd 100644 --- a/src/utils/tar.ts +++ b/src/utils/tar.ts @@ -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): PassThrough { + const tar = pack() -export function makeTar(data: Collection): 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 }