thi-ng / umbrella

⛱ Broadly scoped ecosystem & mono-repository of 199 TypeScript projects (and ~180 examples) for general purpose, functional, data driven development
https://thi.ng
Apache License 2.0
3.36k stars 149 forks source link

[bencode] decode function not support negative integer #342

Closed blackhuman closed 2 years ago

blackhuman commented 2 years ago

I was trying to parse some fastresume files(encoded via Bencode). When decode from bytes like { foo: -123 }, an error "expected digit, got 0x2d" was thrown. I looked into the code, found that the readInt function does not treat Lit.MINUS as readFloat dose.

https://github.com/thi-ng/umbrella/blob/07105097d48766e456b4d2a15468d7fef9483fe5/packages/bencode/src/decode.ts#L123-L129

I looked up on Wikipedia, and found that the negative integer seems been described in spec.

I'm not sure it's on purpose or I missing something. I add a few code in the readInt function to make it run, but only on locally right now. Also, I tested the added code using following test case.

group("bencode", {
  "roundtrip (integer negative)": () => {
      let src = { foo: -123 };
      assert.deepStrictEqual(decode(encode(src), false), src)
  },
}
postspectacular commented 2 years ago

Thank you for reporting this, @blackhuman! I just pushed some update/fix for this and will do a new release in the next few hours...