bitmessage-js/lib/user-agent.js

113 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2015-01-11 18:24:39 +01:00
/**
* Working with Bitmessage user agents.
2023-01-09 02:46:20 +01:00
* @see {@link https://pybitmessage.rtfd.io/en/v0.6/useragent.html}
2015-01-11 18:24:39 +01:00
* @module bitmessage/user-agent
*/
"use strict";
var var_str = require("./structs").var_str;
2024-02-29 23:58:03 +01:00
var BM_NAME = require("../package.json").name.replace("@bitmessage/", "");
2015-01-11 18:24:39 +01:00
var BM_VERSION = require("../package.json").version;
2015-01-31 12:51:35 +01:00
/**
* User agent of the bitmessage library itself.
* @constant {Object[]}
* @static
*/
2015-01-15 15:28:52 +01:00
var SELF = exports.SELF = [{name: BM_NAME, version: BM_VERSION}];
2015-01-11 18:24:39 +01:00
/**
2015-01-31 12:51:35 +01:00
* Decode user agent's `var_str`. Just an alias for
* [var_str.decode]{@link module:bitmessage/structs.var_str.decode}.
2015-01-24 13:13:21 +01:00
* @function
*/
exports.decode = var_str.decode;
/**
* Parse raw user agent into software stack list. Most underlying
2015-01-31 12:51:35 +01:00
* software comes first.
2015-01-28 21:37:20 +01:00
* NOTE: Decoding is rather loose, it won't fail on bad user agent
* format because it's not that important.
2015-01-24 13:13:21 +01:00
* @param {string} str - Raw user agent string
* @return {Object[]} Parsed user agent.
2015-01-11 18:24:39 +01:00
*/
2015-01-24 13:13:21 +01:00
exports.parse = function(str) {
2015-01-11 18:24:39 +01:00
var software = [];
2015-01-24 13:13:21 +01:00
if (str.length > 2 && str[0] === "/" && str[str.length - 1] === "/") {
software = str.slice(1, -1).split("/");
2015-01-11 18:24:39 +01:00
software = software.map(function(str) {
// That's more readable than /([^:]*)(?::([^(]*)(?:\(([^)]*))?)?/
var soft = {name: str};
var semicolon = soft.name.indexOf(":");
if (semicolon !== -1) {
soft.version = soft.name.slice(semicolon + 1);
soft.name = soft.name.slice(0, semicolon);
var obracket = soft.version.indexOf("(");
if (obracket !== -1) {
soft.comments = soft.version.slice(obracket + 1);
soft.version = soft.version.slice(0, obracket);
var cbracket = soft.comments.indexOf(")");
if (cbracket !== -1) {
soft.comments = soft.comments.slice(0, cbracket);
}
}
}
return soft;
});
}
2015-01-24 13:13:21 +01:00
return software;
2015-01-11 18:24:39 +01:00
};
/**
2015-01-24 13:13:21 +01:00
* Encode user agent into `var_str` Buffer. Most underlying software
* comes first.
2015-02-10 15:39:01 +01:00
* @param {(Object[]|string[]|string|Buffer)} software - List of
* software to encode or just raw user agent string/Buffer
2015-01-11 18:24:39 +01:00
* @return {Buffer} Encoded user agent.
2015-01-31 12:51:35 +01:00
* @function
* @static
2015-01-11 18:24:39 +01:00
*/
var encode = exports.encode = function(software) {
2015-01-24 13:13:21 +01:00
var ua;
if (Array.isArray(software)) {
ua = software.map(function(soft) {
if (typeof soft === "string") {
return soft;
}
var version = soft.version || "0.0.0";
var str = soft.name + ":" + version;
if (soft.comments) {
str += "(" + soft.comments + ")";
}
return str;
}).join("/");
ua = "/" + ua + "/";
2015-02-10 15:39:01 +01:00
} else if (Buffer.isBuffer(software)) {
return software;
2015-01-24 13:13:21 +01:00
} else {
ua = software;
2015-01-15 15:28:52 +01:00
}
2015-01-24 13:13:21 +01:00
return var_str.encode(ua);
2015-01-11 18:24:39 +01:00
};
/**
* Encode user agent of bitmessage library.
2015-01-11 18:24:39 +01:00
* @return {Buffer} Encoded user agent.
*/
exports.encodeSelf = function() {
2015-01-15 15:28:52 +01:00
return encode(SELF);
2015-01-11 18:24:39 +01:00
};
/**
* Encode user agent with user agent of bitmessage library underneath.
2015-02-10 15:39:01 +01:00
* Most underlying software comes first.
2015-01-15 15:28:52 +01:00
* @param {(Object[]|string[]|Object|string)} software - List of
* software to encode
2015-01-11 18:24:39 +01:00
* @return {Buffer} Encoded user agent.
*/
exports.encodeSelfWith = function(software) {
2015-01-15 15:28:52 +01:00
software = SELF.concat(software);
2015-01-11 18:24:39 +01:00
return encode(software);
};