Skip to content

Commit

Permalink
chore: release 0.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Sloaix committed Aug 7, 2023
1 parent 0d2d6f7 commit 372b0f1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
22 changes: 10 additions & 12 deletions __test__/encode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,16 @@ describe('dictionary', () => {
// bittorrent 编码测试
test('encode complex dictionary', () => {
const torrentStructure = {
announce: 'https://www.github.com',
'created by': 'sloaix-node-bencode',
'creation date': Math.floor(Date.now() / 1000),
info: {
name: 'hello.txt',
'piece length': 16 * 1024,
length: 5,
pieces: new Uint8Array([
170, 244, 198, 29, 220, 197, 232, 162, 218, 190, 222, 15, 59, 72, 44, 217, 174, 169, 67, 77
])
}
pieces: new Uint8Array([
170, 244, 198, 29, 220, 197, 232, 162, 218, 190, 222, 15, 59, 72, 44, 217, 174, 169, 67, 77
])
}
console.log(Buffer.from(encoder.encode(torrentStructure)).toString('hex'))

const expectValue = new Uint8Array([
100, 54, 58, 112, 105, 101, 99, 101, 115, 50, 48, 58, 170, 244, 198, 29, 220, 197, 232, 162, 218, 190, 222, 15,
59, 72, 44, 217, 174, 169, 67, 77, 101
])

expect(encoder.encode(torrentStructure)).toStrictEqual(expectValue)
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sloaix-node-bencode",
"version": "0.1.5",
"version": "0.1.6",
"description": "Bencode encoding and decoding, with both library and CLI versions available. The CLI version can decode bencode-formatted data from files or strings and output the result in JSON format.",
"keywords": [
"bencode",
Expand Down
25 changes: 11 additions & 14 deletions src/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,28 @@ export class Bencoder {

/**
* 编码字节字符串
* @param byteString 字节字符串
* @param bufferString 字节字符串
*
* 支持编码空字符串,例如 0:
* 但不支持null或者undefined
*/
private encodeByteString(byteString: BencodeString): Uint8Array {
if (byteString === null || byteString === undefined) {
private encodeByteString(bufferString: BencodeString): Uint8Array {
if (bufferString === null || bufferString === undefined) {
throw new Error("undefined or null string isn't be supported to be encode")
}

// 将除string外非Buffer类型的数据转换为Buffer
if (byteString instanceof Uint8Array) {
byteString = Buffer.from(byteString)
// 如果不是Buffer,则转换为Buffer
if (!(bufferString instanceof Buffer)) {
bufferString = Buffer.from(bufferString)
}

// 将Buffer类型的数据转换为字节字符串
if (byteString instanceof Buffer) {
byteString = byteString.toString('utf-8')
}
// 字符串的编码格式为:字符串的长度 + ':' + 字符串,例如:4:spam

// 字符串的编码格式为:字符串的长度 + ':' + 字符串
// 例如:4:spam
const buffers: Uint8Array[] = [this.te.encode(`${byteString.toString().length}:`)]
// 编码字符串的长度
const buffers: Uint8Array[] = [this.te.encode(`${bufferString.length}:`)]

buffers.push(Buffer.from(byteString))
// 编码字符串内容
buffers.push(bufferString)

return Uint8Array.from(Buffer.concat(buffers))
}
Expand Down

0 comments on commit 372b0f1

Please sign in to comment.