It sorts dictionary entries incorrectly.
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 58/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- javascript, node.js
- Domain
- backend
Research direction
Start in lib/encode.js at the dictionary-key sorting code linked in the issue. Reproduce the difference between UTF-16 sorting and byte-sequence sorting using the provided code points, then inspect the existing encoder tests. Done means dictionary keys are ordered according to their bencoded byte sequences, including the non-ASCII case described here.
Written by the indexing model from the issue text.
Description
Bug
// fix for issue #13 - sorted dicts
const keys = Object.keys(data).sort()
This is not the correct way of sorting dictionary entries.
When you say “strings” in the context of Bencoding, you mean “binary strings,” or more specifically, “8-bit byte sequences.”
BEP 52 — The BitTorrent protocol specification version 2
Note that, in the context of bencoding, strings, including dictionary keys, are arbitrary byte sequences (
uint8_t[]).
And Array.prototype.sort compares 16-bit units by default.
If
compareFnis not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order.
The simple .sort() results in a different order (sorted_in_utf16) than the correct one (sorted_in_utf8). Observe:
const A = String.fromCodePoint(0xFF61);
const B = String.fromCodePoint(0x10002);
const sorted_in_utf8 = [A, B].sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b))); // [A, B]
const sorted_in_utf16 = [A, B].sort(); // [B, A]
Related issues
- Dominant language
- JavaScript
- Stars
- 173
- Forks
- 38
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from webtorrent/node-bencode
-
Difficulty 1/5 Under an hour Newbie friendliness 62/100
webtorrent/node-bencode#162 · 1 reaction ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
webtorrent/node-bencode#178 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
webtorrent/node-bencode#146 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
webtorrent/node-bencode#143 ·
-
Dependency Dashboard Open
Difficulty 2/5 1-3 hours Newbie friendliness 15/100
webtorrent/node-bencode#103 ·
All issues in webtorrent/node-bencode
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
HarperFast/skills#96 ·
-
[Block] Latest Posts [Type] Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
sugarlabs/musicblocks#8847 ·