Comparing version 2.0.0 to 2.1.0
/// <reference types="node" /> | ||
export declare class BencodeDecoder { | ||
/** | ||
* Check if character in integer | ||
*/ | ||
private static _isInteger; | ||
private _index; | ||
private readonly _stringify; | ||
private readonly _buffer; | ||
constructor(data: Buffer | string); | ||
decode(): number | Buffer | (number | object | Buffer)[] | { | ||
/** | ||
* Constructor | ||
*/ | ||
constructor(data: Buffer | string, stringify?: boolean); | ||
/** | ||
* Decode bencoded data | ||
*/ | ||
decode(): string | number | Buffer | (string | number | object | Buffer)[] | { | ||
[key: string]: string | number | object | Buffer; | ||
}; | ||
/** | ||
* Get character by current index | ||
*/ | ||
private _currentChar; | ||
/** | ||
* Get character by current index and increment | ||
*/ | ||
private _next; | ||
/** | ||
* Decode bencoded string | ||
*/ | ||
private _decodeString; | ||
/** | ||
* Decode bencoded integer | ||
*/ | ||
private _decodeInteger; | ||
/** | ||
* Decode bencoded list | ||
*/ | ||
private _decodeList; | ||
/** | ||
* Decode bencoded dictionary | ||
*/ | ||
private _decodeDictionary; | ||
} | ||
declare const _default: (data: string | Buffer) => number | Buffer | (number | object | Buffer)[] | { | ||
export declare function decode(data: Buffer | string, stringify?: boolean): string | number | Buffer | (string | number | object | Buffer)[] | { | ||
[key: string]: string | number | object | Buffer; | ||
}; | ||
export default _default; | ||
export default decode; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = require("./types"); | ||
var BencodeDecoder = (function () { | ||
function BencodeDecoder(data) { | ||
const types_1 = require("./types"); | ||
class BencodeDecoder { | ||
/** | ||
* Constructor | ||
*/ | ||
constructor(data, stringify = false) { | ||
if (!data) { | ||
@@ -10,8 +13,15 @@ throw new Error('Nothing to decode'); | ||
this._index = 0; | ||
this._stringify = stringify; | ||
this._buffer = typeof data === 'string' ? Buffer.from(data) : data; | ||
} | ||
BencodeDecoder._isInteger = function (char) { | ||
/** | ||
* Check if character in integer | ||
*/ | ||
static _isInteger(char) { | ||
return char >= 0x30 && char <= 0x39; | ||
}; | ||
BencodeDecoder.prototype.decode = function () { | ||
} | ||
/** | ||
* Decode bencoded data | ||
*/ | ||
decode() { | ||
if (BencodeDecoder._isInteger(this._currentChar())) { | ||
@@ -30,21 +40,35 @@ return this._decodeString(); | ||
throw new Error('Invalid bencode data'); | ||
}; | ||
BencodeDecoder.prototype._currentChar = function () { | ||
} | ||
/** | ||
* Get character by current index | ||
*/ | ||
_currentChar() { | ||
return this._buffer[this._index]; | ||
}; | ||
BencodeDecoder.prototype._next = function () { | ||
} | ||
/** | ||
* Get character by current index and increment | ||
*/ | ||
_next() { | ||
return this._buffer[this._index++]; | ||
}; | ||
BencodeDecoder.prototype._decodeString = function () { | ||
var length = this._decodeInteger(); | ||
var acc = []; | ||
for (var i = 0; i < length; i++) { | ||
} | ||
/** | ||
* Decode bencoded string | ||
*/ | ||
_decodeString() { | ||
const length = this._decodeInteger(); | ||
const acc = []; | ||
for (let i = 0; i < length; i++) { | ||
acc.push(this._next()); | ||
} | ||
return Buffer.from(acc); | ||
}; | ||
BencodeDecoder.prototype._decodeInteger = function () { | ||
var sign = 1; | ||
var float = false; | ||
var number = 0; | ||
return this._stringify | ||
? Buffer.from(acc).toString('utf8') | ||
: Buffer.from(acc); | ||
} | ||
/** | ||
* Decode bencoded integer | ||
*/ | ||
_decodeInteger() { | ||
let sign = 1; | ||
let float = false; | ||
let number = 0; | ||
if (this._currentChar() === types_1.FLAG.INTEGER) { | ||
@@ -75,5 +99,9 @@ this._index++; | ||
return number * sign; | ||
}; | ||
BencodeDecoder.prototype._decodeList = function () { | ||
var acc = []; | ||
} | ||
/** | ||
* Decode bencoded list | ||
*/ | ||
_decodeList() { | ||
const acc = []; | ||
// skip LIST flag | ||
this._next(); | ||
@@ -83,18 +111,27 @@ while (this._currentChar() !== types_1.FLAG.END) { | ||
} | ||
// skip END flag | ||
this._next(); | ||
return acc; | ||
}; | ||
BencodeDecoder.prototype._decodeDictionary = function () { | ||
var acc = {}; | ||
} | ||
/** | ||
* Decode bencoded dictionary | ||
*/ | ||
_decodeDictionary() { | ||
const acc = {}; | ||
// skip DICTIONARY flag | ||
this._next(); | ||
while (this._currentChar() !== types_1.FLAG.END) { | ||
var key = this._decodeString(); | ||
const key = this._decodeString(); | ||
acc[key.toString()] = this.decode(); | ||
} | ||
// skip END flag | ||
this._next(); | ||
return acc; | ||
}; | ||
return BencodeDecoder; | ||
}()); | ||
} | ||
} | ||
exports.BencodeDecoder = BencodeDecoder; | ||
exports.default = (function (data) { return new BencodeDecoder(data).decode(); }); | ||
function decode(data, stringify) { | ||
return new BencodeDecoder(data, stringify).decode(); | ||
} | ||
exports.decode = decode; | ||
exports.default = decode; |
@@ -9,12 +9,35 @@ /// <reference types="node" /> | ||
private _endIdentifier; | ||
private _buffer; | ||
encode(data: EncodeTypes): Buffer; | ||
private readonly _buffer; | ||
private readonly _stringify; | ||
constructor(stringify?: boolean); | ||
/** | ||
* Encode data | ||
*/ | ||
encode(data: EncodeTypes): string | Buffer; | ||
/** | ||
* Encode data by type | ||
*/ | ||
private _encodeType; | ||
/** | ||
* Encode buffer | ||
*/ | ||
private _encodeBuffer; | ||
/** | ||
* Encode string | ||
*/ | ||
private _encodeString; | ||
/** | ||
* Encode integer | ||
*/ | ||
private _encodeInteger; | ||
/** | ||
* Encode list | ||
*/ | ||
private _encodeList; | ||
/** | ||
* Encode dictionary | ||
*/ | ||
private _encodeDictionary; | ||
} | ||
declare const _default: (data: EncodeTypes) => Buffer; | ||
export default _default; | ||
export declare function encode(data: EncodeTypes, stringify?: boolean): string | Buffer; | ||
export default encode; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = require("./types"); | ||
var BencodeEncoder = (function () { | ||
function BencodeEncoder() { | ||
const types_1 = require("./types"); | ||
class BencodeEncoder { | ||
constructor(stringify = false) { | ||
this._integerIdentifier = Buffer.from([types_1.FLAG.INTEGER]); | ||
@@ -12,8 +12,17 @@ this._stringDelimiterIdentifier = Buffer.from([types_1.FLAG.STR_DELIMITER]); | ||
this._buffer = []; | ||
this._stringify = stringify; | ||
} | ||
BencodeEncoder.prototype.encode = function (data) { | ||
/** | ||
* Encode data | ||
*/ | ||
encode(data) { | ||
this._encodeType(data); | ||
return Buffer.concat(this._buffer); | ||
}; | ||
BencodeEncoder.prototype._encodeType = function (data) { | ||
return this._stringify | ||
? Buffer.concat(this._buffer).toString('utf8') | ||
: Buffer.concat(this._buffer); | ||
} | ||
/** | ||
* Encode data by type | ||
*/ | ||
_encodeType(data) { | ||
if (Buffer.isBuffer(data)) { | ||
@@ -43,16 +52,28 @@ return this._encodeBuffer(data); | ||
} | ||
throw new Error(typeof data + " is unsupported type."); | ||
}; | ||
BencodeEncoder.prototype._encodeBuffer = function (data) { | ||
throw new Error(`${typeof data} is unsupported type.`); | ||
} | ||
/** | ||
* Encode buffer | ||
*/ | ||
_encodeBuffer(data) { | ||
this._buffer.push(Buffer.from(String(data.length)), this._stringDelimiterIdentifier, data); | ||
}; | ||
BencodeEncoder.prototype._encodeString = function (data) { | ||
} | ||
/** | ||
* Encode string | ||
*/ | ||
_encodeString(data) { | ||
this._buffer.push(Buffer.from(String(Buffer.byteLength(data))), this._stringDelimiterIdentifier, Buffer.from(data)); | ||
}; | ||
BencodeEncoder.prototype._encodeInteger = function (data) { | ||
} | ||
/** | ||
* Encode integer | ||
*/ | ||
_encodeInteger(data) { | ||
this._buffer.push(this._integerIdentifier, Buffer.from(String(Math.round(data))), this._endIdentifier); | ||
}; | ||
BencodeEncoder.prototype._encodeList = function (data) { | ||
} | ||
/** | ||
* Encode list | ||
*/ | ||
_encodeList(data) { | ||
this._buffer.push(this._listIdentifier); | ||
for (var i = 0; i < data.length; i++) { | ||
for (let i = 0; i < data.length; i++) { | ||
if (data[i] === null || data[i] === undefined) { | ||
@@ -64,8 +85,10 @@ continue; | ||
this._buffer.push(this._endIdentifier); | ||
}; | ||
BencodeEncoder.prototype._encodeDictionary = function (data) { | ||
} | ||
/** | ||
* Encode dictionary | ||
*/ | ||
_encodeDictionary(data) { | ||
this._buffer.push(this._dictionaryIdentifier); | ||
var keys = Object.keys(data).sort(); | ||
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) { | ||
var key = keys_1[_i]; | ||
const keys = Object.keys(data).sort(); | ||
for (const key of keys) { | ||
if (data[key] === null || data[key] === undefined) { | ||
@@ -78,6 +101,9 @@ continue; | ||
this._buffer.push(this._endIdentifier); | ||
}; | ||
return BencodeEncoder; | ||
}()); | ||
} | ||
} | ||
exports.BencodeEncoder = BencodeEncoder; | ||
exports.default = (function (data) { return new BencodeEncoder().encode(data); }); | ||
function encode(data, stringify) { | ||
return new BencodeEncoder(stringify).encode(data); | ||
} | ||
exports.encode = encode; | ||
exports.default = encode; |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import decode from './decode'; | ||
@@ -6,7 +5,5 @@ import encode from './encode'; | ||
declare const _default: { | ||
decode: (data: string | Buffer) => number | Buffer | (number | object | Buffer)[] | { | ||
[key: string]: string | number | object | Buffer; | ||
}; | ||
encode: (data: import("./types").EncodeTypes) => Buffer; | ||
decode: typeof decode; | ||
encode: typeof encode; | ||
}; | ||
export default _default; |
@@ -6,6 +6,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var decode_1 = __importDefault(require("./decode")); | ||
const decode_1 = __importDefault(require("./decode")); | ||
exports.decode = decode_1.default; | ||
var encode_1 = __importDefault(require("./encode")); | ||
const encode_1 = __importDefault(require("./encode")); | ||
exports.encode = encode_1.default; | ||
exports.default = { decode: decode_1.default, encode: encode_1.default }; |
{ | ||
"name": "bencodec", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Fast and easy to use bencode codec.", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -30,11 +30,25 @@ | ||
##### Decode data | ||
```typescript | ||
const decoded = bencodec.decode('d3:bar4:spam3:fooi42ee'); | ||
To convert byte strings to strings pass `true` as the second parameter. | ||
``` | ||
bencodec.decode( 'd3:bar4:spam3:fooi42ee' ); | ||
// { bar: <Buffer 73 70 61 6d>, foo: 42 } | ||
bencodec.decode( 'd3:bar4:spam3:fooi42ee', true ); | ||
// { bar: 'spam', foo: 42 } | ||
bencodec.decode( Buffer.from('d3:bar4:spam3:fooi42ee'), true ); | ||
// { bar: 'spam', foo: 42 } | ||
``` | ||
##### Encode data | ||
```typescript | ||
const encoded = bencodec.encode({ bar: 'spam', foo: 42 }); | ||
By default method encode will return the byte string. | ||
Pass `true` as the second parameter to 'stringify' result. | ||
``` | ||
bencodec.encode({ bar: 'spam', foo: 42 }); | ||
// <Buffer 64 33 ... 65 65> | ||
bencodec.encode({ bar: 'spam', foo: 42 }, true); | ||
// 'd3:bar4:spam3:fooi42ee' | ||
``` | ||
## Tests | ||
@@ -46,2 +60,2 @@ ``` | ||
## License | ||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. | ||
This project is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14490
372
60
0