isaacs / node-tar

tar for node
ISC License
837 stars 183 forks source link

[BUG] Syntax error when building a package with parcel.js #368

Closed ansobolev closed 1 year ago

ansobolev commented 1 year ago

Is there an existing issue for this?

Current Behavior

I'm building a Web application using Babel and Parcel.js. After trying to import tar npm package to the app, I get Uncaught SyntaxError: octal escape sequences can't be used in untagged template literals or in strict mode code in the console. It comes from this line in the code, which gets transformed by Parcel to

if (buf.slice(off + 257, off + 265).toString() === "ustar\000") {

Expected Behavior

The package is built successfully; there should be no errors whatsoever.

Steps To Reproduce

package.json:

{
  "name": "MWE",
  "version": "0.0.1",
  "dependencies": {
    "@babel/core": "^7.21.0",
    "@parcel/babel-preset-env": "^2.8.3",
    "http-server": "^14.1.1",
    "parcel": "^2.8.3",
    "tar": "^6.1.13"
  },
  "scripts": {
    "dev": "parcel watch ./index.html --dist-dir ./public --public-url ./",
    "build": "parcel build ./index.html --dist-dir ./public --public-url ./",
    "start": "http-server -a localhost -p 8000"
  }
}

index.html:

<html>
  <head>
  </head>
  <body>
    <script type="module" src="app.js"></script>
  </body>
</html>

app.js:

const tar = require('tar');

Run either npm run build, or npm run dev and npm run start. The first command fail to finish; the second two give te described behavior.

Environment

isaacs commented 1 year ago

Well, whichever program is thinking that '\u000000' is equivalent to '\000' is the thing with the bug, because that is 100% wrong.

\u0000 is a unicode escape code for a null byte. \u000000 is that followed by two bytes of 0x30, a literal '0' character.

'\000' is an octal escape, for a null byte. But your transpiler is throwing away the '00' which is required for the ustar field.

> Buffer.from('\u000000')
<Buffer 00 30 30>
> Buffer.from('\000')
<Buffer 00>

Please report this bug to parcel. I'm sorry, there's nothing I can do to help you if you're using a tool that transforms my code into something broken. Good luck.