aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorValentin Popov <info@valentineus.link>2017-07-29 03:39:18 +0300
committerValentin Popov <info@valentineus.link>2017-07-29 03:39:18 +0300
commit4d7d17a98ae96cc2de7c398fa730512705741905 (patch)
tree4b0f9fd667433358560ab84e90de1b306985fd71 /src
parenta7e3aec6138e60e4ffc208d07670965b61dddab4 (diff)
downloadiii-client-4d7d17a98ae96cc2de7c398fa730512705741905.tar.xz
iii-client-4d7d17a98ae96cc2de7c398fa730512705741905.zip
Release of version 2.0.0.
Diffstat (limited to 'src')
-rw-r--r--src/index.js209
1 files changed, 89 insertions, 120 deletions
diff --git a/src/index.js b/src/index.js
index 0d1f801..17b68b7 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,174 +1,143 @@
-/**
- * @project iii-client
- * @author Valentin Popov <info@valentineus.link>
- * @license See LICENSE.md file included in this distribution.
- */
-var http = require('http');
+export { connect, send };
+import http from 'http';
-exports.connect = connect;
/**
* Connects to the server and returns the connection data.
- * @param {String} uuid - The bot UUID.
- * @param {requestCallback} callback - The callback that handles the response.
+ * @param {String} The bot ID.
+ * @promise {Object} Answer from the server.
+ * @rejects {Error} If there are errors in operation.
*/
-function connect(uuid, callback) {
- uuid = uuid || '';
-
- if (!isVerification(uuid)) {
- throw new Error('The UUID is not a valid value!');
- }
-
- const query = {
- port: 80,
- hostname: 'iii.ru',
- path: '/api/2.0/json/Chat.init/' + uuid + '/',
- method: 'GET',
- };
-
- const request = http.request(query, function(response) {
- var json = '';
- response.on('data', (raw) => json = decryptJSON(raw));
- response.on('end', () => {
- if (json.error) throw new Error(json.error.message);
- callback(json.result);
+function connect(uuid) {
+ return new Promise((resolve, reject) => {
+ if (!isVerification(uuid)) reject(new Error('The UUID is not a valid value!'));
+
+ const query = {
+ path: '/api/2.0/json/Chat.init/' + uuid,
+ hostname: 'iii.ru',
+ method: 'GET',
+ port: 80,
+ };
+
+ const request = http.request(query, (response) => {
+ let json = '';
+ response.on('data', (raw) => json = decryptJSON(raw));
+ response.on('end', () => {
+ if (json.error) reject(json.error);
+ resolve(json.result);
+ });
});
+ request.on('error', (error) => reject(error));
+ request.end();
});
-
- request.on('error', (error) => Error(error.message));
-
- request.end();
}
-exports.send = send;
/**
* Send a message to the server and return a response.
- * @param {Object} raw - The data to send.
- * @param {String} raw.cuid - Session identifier.
- * @param {String} raw.text - Message text.
- * @param {requestCallback} callback - The callback that handles the response.
+ * @param {String} cuid - Session identifier.
+ * @param {String} text - Message text.
+ * @promise {Object} Answer from the server.
+ * @rejects {Error} If there are errors in operation.
*/
-function send(raw, callback) {
- raw = raw || {};
-
- const query = {
- port: 80,
- hostname: 'iii.ru',
- path: '/api/2.0/json/Chat.request',
- method: 'POST',
- };
-
- const data = createPackage(raw);
-
- const request = http.request(query, function(response) {
- var json = '';
- response.on('data', (raw) => json = decryptJSON(raw));
- response.on('end', () => {
- if (json.error) throw new Error(json.error.message);
- callback(json.result);
+function send(cuid, text) {
+ return new Promise((resolve, reject) => {
+ if (!isVerification(cuid)) reject(new Error('The CUID is not a valid value!'));
+ if (!isString(text)) reject(new Error('The parameter is not a string!'));
+
+ const query = {
+ path: '/api/2.0/json/Chat.request',
+ hostname: 'iii.ru',
+ method: 'POST',
+ port: 80,
+ };
+
+ const request = http.request(query, (response) => {
+ let json = '';
+ response.on('data', (raw) => json = decryptJSON(raw));
+ response.on('end', () => {
+ if (json.error) reject(json.error);
+ resolve(json.result);
+ });
});
+ request.on('error', (error) => reject(error));
+ request.write(createPackage(cuid, text));
+ request.end();
});
-
- request.on('error', (error) => Error(error));
-
- request.write(data);
- request.end();
}
-exports.encrypt = encrypt;
/**
* Encrypts the incoming data.
- * @param {String} raw - Decrypted data.
- * @returns {String} - Encrypted string.
+ * @param {String} Decrypted data.
+ * @returns {String} Encrypted string.
*/
-function encrypt(raw) {
- raw = raw || '';
-
- var base64 = Buffer.from(raw).toString('base64');
- var string = Buffer.from(base64);
+function encrypt(data) {
+ const base64 = Buffer.from(data).toString('base64');
+ const string = Buffer.from(base64);
return mergerString(string).toString('base64');
}
-exports.decrypt = decrypt;
/**
* Decrypts the incoming data.
- * @param {String} raw - Encrypted data.
- * @returns {String} - Decrypted string.
+ * @param {String} Encrypted data.
+ * @returns {String} Decrypted string.
*/
-function decrypt(raw) {
- raw = raw || '';
-
- var string = Buffer.from(raw, 'base64');
- var decrypted = mergerString(string).toString();
+function decrypt(data) {
+ const string = Buffer.from(data, 'base64');
+ const decrypted = mergerString(string).toString();
return Buffer.from(decrypted, 'base64');
}
-exports.decryptJSON = decryptJSON;
/**
* Decrypts an encrypted JSON object.
- * @param {String} raw - Encrypted data.
- * @returns {Object} - Decrypted JSON.
+ * @param {String} Encrypted data.
+ * @returns {Object} Decrypted JSON.
*/
-function decryptJSON(raw) {
- raw = raw || '';
-
- var string = raw.toString('ascii');
- var data = decrypt(string);
+function decryptJSON(json) {
+ const string = json.toString('ascii');
+ const data = decrypt(string);
return JSON.parse(data);
}
-exports.mergerString = mergerString;
/**
* Merge and convert a string.
- * @param {String} raw - The string to convert.
- * @returns {String} - The converted string.
+ * @param {String} The string to convert.
+ * @returns {String} The converted string.
*/
function mergerString(data) {
- data = data || '';
-
const salt = Buffer.from('some very-very long string without any non-latin characters due to different string representations inside of variable programming languages');
-
- for (var i = 0; i < data.length; i++) {
- data[i] = data[i] ^ salt[i % salt.length];
- }
-
- return data;
+ return data.map((item, index) => {
+ return item ^ salt[index % salt.length];
+ });
}
-exports.createPackage = createPackage;
/**
* Creates an encrypted package to send.
- * @param {Object} raw - The data to send.
- * @param {String} raw.cuid - Session identifier.
- * @param {String} raw.text - Message text.
- * @returns {String} - Encrypted string.
+ * @param {String} cuid - Session identifier.
+ * @param {String} text - Message text.
+ * @returns {String} Encrypted string.
*/
-function createPackage(raw) {
- raw = raw || {};
-
- if (!raw.text) {
- throw new Error('There is no data to send!');
- }
-
- if (!isVerification(raw.cuid)) {
- throw new Error('Parameter \'CUID\' is not a valid UUID value!');
- }
-
- var data = [];
- data.push(raw.cuid);
- data.push(raw.text.toString());
-
- var json = JSON.stringify(data);
+function createPackage(cuid, text) {
+ let data = [];
+ data.push(cuid);
+ data.push(text.toString());
+ const json = JSON.stringify(data);
return encrypt(json);
}
-exports.isVerification = isVerification;
/**
* Validation UUID format string.
- * @param {String} data - The string to check.
+ * @param {String} The string to check.
* @returns {Boolean}
*/
function isVerification(data) {
- data = data || '';
const regexp = new RegExp('^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$', 'i');
return regexp.test(data);
}
+
+/**
+ * Determines if a reference is a String.
+ * @param {String} The string to check.
+ * @returns {Boolean}
+ */
+function isString(data) {
+ return typeof data === 'string';
+} \ No newline at end of file