Improve usability of UserAgent.encode
This commit is contained in:
parent
ecdcd30a76
commit
df5e024004
|
@ -10,6 +10,9 @@ var var_str = require("./structs").var_str;
|
||||||
var BM_NAME = require("../package.json").name;
|
var BM_NAME = require("../package.json").name;
|
||||||
var BM_VERSION = require("../package.json").version;
|
var BM_VERSION = require("../package.json").version;
|
||||||
|
|
||||||
|
/** User agent of the bitmessage library itself. */
|
||||||
|
var SELF = exports.SELF = [{name: BM_NAME, version: BM_VERSION}];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decode user agent stack.
|
* Decode user agent stack.
|
||||||
* NOTE: Decoding is rather loose and non-strict, it won't fail on bad
|
* NOTE: Decoding is rather loose and non-strict, it won't fail on bad
|
||||||
|
@ -49,11 +52,18 @@ exports.decode = function(buf) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode user agent. Most underlying software comes first.
|
* Encode user agent. Most underlying software comes first.
|
||||||
* @param {Object[]} software - List of software to encode
|
* @param {(Object[]|string[]|Object|string)} software - List of
|
||||||
|
* software to encode
|
||||||
* @return {Buffer} Encoded user agent.
|
* @return {Buffer} Encoded user agent.
|
||||||
*/
|
*/
|
||||||
var encode = exports.encode = function(software) {
|
var encode = exports.encode = function(software) {
|
||||||
|
if (!Array.isArray(software)) {
|
||||||
|
software = [software];
|
||||||
|
}
|
||||||
var ua = software.map(function(soft) {
|
var ua = software.map(function(soft) {
|
||||||
|
if (typeof soft === "string") {
|
||||||
|
return soft;
|
||||||
|
}
|
||||||
var version = soft.version || "0.0.0";
|
var version = soft.version || "0.0.0";
|
||||||
var str = soft.name + ":" + version;
|
var str = soft.name + ":" + version;
|
||||||
if (soft.comments) {
|
if (soft.comments) {
|
||||||
|
@ -69,16 +79,17 @@ var encode = exports.encode = function(software) {
|
||||||
* @return {Buffer} Encoded user agent.
|
* @return {Buffer} Encoded user agent.
|
||||||
*/
|
*/
|
||||||
exports.encodeSelf = function() {
|
exports.encodeSelf = function() {
|
||||||
return encode([{name: BM_NAME, version: BM_VERSION}]);
|
return encode(SELF);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode user agent with bitmessage's user agent underneath. Most
|
* Encode user agent with bitmessage's user agent underneath. Most
|
||||||
* underlying software comes first.
|
* underlying software comes first.
|
||||||
* @param {Object[]} software - List of software to encode
|
* @param {(Object[]|string[]|Object|string)} software - List of
|
||||||
|
* software to encode
|
||||||
* @return {Buffer} Encoded user agent.
|
* @return {Buffer} Encoded user agent.
|
||||||
*/
|
*/
|
||||||
exports.encodeSelfWith = function(software) {
|
exports.encodeSelfWith = function(software) {
|
||||||
software = [{name: BM_NAME, version: BM_VERSION}].concat(software);
|
software = SELF.concat(software);
|
||||||
return encode(software);
|
return encode(software);
|
||||||
};
|
};
|
||||||
|
|
12
test.js
12
test.js
|
@ -433,5 +433,17 @@ describe("High-level classes", function() {
|
||||||
expect(software[2].name).to.equal(bweb.name);
|
expect(software[2].name).to.equal(bweb.name);
|
||||||
expect(software[2].version).to.equal("0.0.0");
|
expect(software[2].version).to.equal("0.0.0");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should accept just object or string(s) on encode", function() {
|
||||||
|
var enc1 = UserAgent.encode({name: "test", version: "0.0.1"});
|
||||||
|
var enc2 = UserAgent.encode("test:0.0.1");
|
||||||
|
var res = [{name: "test", version: "0.0.1"}];
|
||||||
|
expect(UserAgent.decode(enc1).software).to.deep.equal(res);
|
||||||
|
expect(UserAgent.decode(enc2).software).to.deep.equal(res);
|
||||||
|
var enc3 = UserAgent.encodeSelfWith("test:0.0.1");
|
||||||
|
var software = UserAgent.decode(enc3).software;
|
||||||
|
expect(software[0].name).to.equal("bitmessage");
|
||||||
|
expect(software[1]).to.deep.equal(res[0]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user