Skip to content

Commit

Permalink
chore: release 0.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Sloaix committed Aug 7, 2023
1 parent acf4faa commit 0d2d6f7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
18 changes: 18 additions & 0 deletions __test__/encode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,22 @@ describe('dictionary', () => {
test('encode empty dictionary', () => {
expect(encoder.encode({})).toStrictEqual(textEncoder.encode('de'))
})

// 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
])
}
}
console.log(Buffer.from(encoder.encode(torrentStructure)).toString('hex'))
})
})
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.4",
"version": "0.1.5",
"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
16 changes: 8 additions & 8 deletions src/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from './type'

export class Bencoder {
private encoder: TextEncoder = new TextEncoder()
private te: TextEncoder = new TextEncoder()

encode(data: BencodeType) {
if (isBencodeString(data)) {
Expand Down Expand Up @@ -51,12 +51,12 @@ export class Bencoder {

// 将Buffer类型的数据转换为字节字符串
if (byteString instanceof Buffer) {
byteString = byteString.toString('binary')
byteString = byteString.toString('utf-8')
}

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

buffers.push(Buffer.from(byteString))

Expand All @@ -73,7 +73,7 @@ export class Bencoder {
}
// 数字的编码格式为:'i' + 数字 + 'e'
// 例如:i3e或者 i-5e
return this.encoder.encode(`i${integer}e`)
return this.te.encode(`i${integer}e`)
}

/**
Expand All @@ -82,7 +82,7 @@ export class Bencoder {
*/
private encodeList(list: BencodeList): Uint8Array {
logd(` start encodeList`)
const buffers: Uint8Array[] = [this.encoder.encode('l')]
const buffers: Uint8Array[] = [this.te.encode('l')]
// 遍历列表中的元素
for (const element of list) {
logd(` start itre ${element}`)
Expand All @@ -91,7 +91,7 @@ export class Bencoder {
}
logd(` end encodeList`)

buffers.push(this.encoder.encode('e'))
buffers.push(this.te.encode('e'))

return Uint8Array.from(Buffer.concat(buffers))
}
Expand All @@ -102,7 +102,7 @@ export class Bencoder {
*/
private encodeDict(dict: BencodeDict): Uint8Array {
// 字典的编码格式为:'d' + 字典中的key-value + 'e'
const buffers: Uint8Array[] = [this.encoder.encode('d')]
const buffers: Uint8Array[] = [this.te.encode('d')]

// 创建一个数组,用于存储字典的key
let keys: string[] = Object.keys(dict)
Expand All @@ -124,7 +124,7 @@ export class Bencoder {
}

// 写入结束符 e
buffers.push(this.encoder.encode('e'))
buffers.push(this.te.encode('e'))
return Uint8Array.from(Buffer.concat(buffers))
}
}

0 comments on commit 0d2d6f7

Please sign in to comment.