Skip to content

Commit

Permalink
build(project): refactored and simplified the API
Browse files Browse the repository at this point in the history
  • Loading branch information
fenying committed Nov 19, 2024
1 parent 8a05fd1 commit cf74e64
Show file tree
Hide file tree
Showing 24 changed files with 2,502 additions and 3,913 deletions.
7 changes: 0 additions & 7 deletions .eslintignore

This file was deleted.

265 changes: 0 additions & 265 deletions .eslintrc

This file was deleted.

1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"
npm run typecheck && npm run lint
4 changes: 0 additions & 4 deletions .husky/pre-commit

This file was deleted.

6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@
/src/examples/
/commitlint.config.js
/test-data/
*.test.js
*.test.js.map
*.test.d.ts
*.test.d.ts.map
/eslint.config.js
/utils/
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib"
"typescript.tsdk": "node_modules\\typescript\\lib",
"cSpell.words": [
"bencode"
]
}
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changes Logs

## v0.2.0

- build(deps): removed dependency of `@litert/exception`.
- build(project): refactored and simplified the API.

## v0.1.1

- fix(deps): upgraded and deprecated old deps.
Expand Down
37 changes: 13 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ npm i @litert/bencode --save

## Usage

Encoding:

```ts
import * as libBencode from "@litert/bencode";

const be = libBEncode.createBEncoder();
const enc = new libBEncode.BencodeEncoder();

const beData = be.encode({ // Encode data into BEncode.
const beData = enc.encode({ // Encode data into BEncode.
"name": "Angus",
"age": 18,
"friends": [
Expand All @@ -36,38 +38,25 @@ const beData = be.encode({ // Encode data into BEncode.
"scores": {
"math": 87
},
"randomBytes": Buffer.allocUnsafe(32)
"randomBytes": Buffer.from('vsm/GvyGjZqUeuPa7ZP8h9ot8VRCe/6arpboI46EIlg=', 'base64')
});

/**
* Don't print as a UTF-8 string, becuase it's binary.
* Don't print as a UTF-8 string, because it's binary.
*/
console.log(beData);
```

/**
* Print Buffer in BASE64 format in JSON.
*/
function buffer4Base64(k: string, v: any): any {
Decoding:

if (typeof v === "object" && "data" in v && v.type === "Buffer") {
```ts
import * as libBencode from "@litert/bencode";

return Buffer.from(v.data).toString("base64");
}
const dec = new LibBencode.BencodeDecoder();

return v;
}
const beData = dec.decode('d4:name5:Angus7:friendsld4:name5:Editheee');

/**
* Print the decoded object as JSON.
*
* The second argument of decode will transform a Buffer into string, if it's
* a UTF-8 string.
*/
console.log(JSON.stringify(
be.decode(beData, true),
buffer4Base64,
2
));
console.log(beData); // Output: { name: 'Angus', friends: [ { name: 'Edith' } ] }
```

## License
Expand Down
5 changes: 1 addition & 4 deletions commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ module.exports = {
'type-enum': [2, 'always', [
'feat',
'fix',
'add',
'test',
'config',
'merge'
'build'
]],
'scope-enum': [2, 'always', [
'encoder',
Expand Down
Loading

0 comments on commit cf74e64

Please sign in to comment.