added prettier

This commit is contained in:
2021-03-10 21:32:11 -06:00
parent 883e7a7f62
commit d39e087406
15 changed files with 885 additions and 548 deletions

View File

@@ -1,6 +1,6 @@
import Socket from 'net';
import * as constants from '../utils/constants'
import * as constants from '../utils/constants';
import packet from './packet';
@@ -33,13 +33,16 @@ export default class connection {
if (!this.socket) {
try {
await new Promise((resolve, reject) => {
this.socket =
Socket.createConnection(this.port, this.host_or_ip, err => {
this.socket = Socket.createConnection(
this.port,
this.host_or_ip,
(err) => {
if (err) {
return reject(err)
return reject(err);
}
return resolve()
});
return resolve();
}
);
});
} catch (err) {
return Promise.reject(new Error(`'connect()' failed: ${err}`));
@@ -57,7 +60,7 @@ export default class connection {
buffer = null;
};
this.socket.on('data', chunk => {
this.socket.on('data', (chunk) => {
buffer = buffer ? Buffer.concat([buffer, chunk]) : chunk;
if (buffer.length > 4) {
const size = buffer.readUInt32BE(0);
@@ -71,19 +74,20 @@ export default class connection {
const response = new packet(this.password);
response.buffer = new Uint8Array(packet_data);
response.decrypt()
.then(() => {
resolve(response)
})
.catch(e => {
reject(e)
})
response
.decrypt()
.then(() => {
resolve(response);
})
.catch((e) => {
reject(e);
});
}
}
}
});
this.socket.on('error', e => {
this.socket.on('error', (e) => {
if (this.reject) {
const reject = this.reject;
@@ -115,7 +119,7 @@ export default class connection {
this.connected = false;
}
} catch (e) {
console.log(e)
console.log(e);
}
}
@@ -131,7 +135,7 @@ export default class connection {
return new Promise((resolve, reject) => {
this.reject = reject;
this.resolve = resolve;
this.socket.write(Buffer.from(packet.buffer), null, err => {
this.socket.write(Buffer.from(packet.buffer), null, (err) => {
if (err) {
this.cleanup_handlers();
reject(err);

View File

@@ -9,18 +9,18 @@ export default class connection_pool {
this.password = password;
if (pool_size > 1) {
this.pool = new Pool({
connect : {host : host_or_ip, port : port},
connectTimeout : 5000,
pool : {max : pool_size, min : 2}
connect: { host: host_or_ip, port: port },
connectTimeout: 5000,
pool: { max: pool_size, min: 2 },
});
} else {
throw new Error("'pool_size' must be > 1");
}
}
host_or_ip = "";
host_or_ip = '';
next_thread_id = 1;
password = "";
password = '';
port = 20000;
pool;
shutdown = false;
@@ -48,16 +48,19 @@ export default class connection_pool {
};
try {
const result = await new connection(this.host_or_ip, this.port,
this.password, socket)
.send(method_name, packet,
optional_thread_id || socket.thread_id);
const result = await new connection(
this.host_or_ip,
this.port,
this.password,
socket
).send(method_name, packet, optional_thread_id || socket.thread_id);
cleanup();
return result;
} catch (err) {
cleanup();
return Promise.reject(
new Error(`'send(${method_name})' failed: ${err}`));
new Error(`'send(${method_name})' failed: ${err}`)
);
}
} catch (err) {
return Promise.reject(new Error(`'acquire()' socket failed: ${err}`));

View File

@@ -1,7 +1,7 @@
import {randomBytes} from 'crypto';
import {Int64BE, Uint64BE} from 'int64-buffer';
import {sha256} from 'js-sha256';
import {TextEncoder} from 'text-encoding';
import { randomBytes } from 'crypto';
import { Int64BE, Uint64BE } from 'int64-buffer';
import { sha256 } from 'js-sha256';
import { TextEncoder } from 'text-encoding';
import {
be_ui8_array_to_i16,
@@ -20,7 +20,9 @@ import {
import JSChaCha20 from '../utils/jschacha20';
export default class packet {
constructor(token) { this.token = token; }
constructor(token) {
this.token = token;
}
static HEADER = new TextEncoder().encode('repertory');
@@ -28,13 +30,14 @@ export default class packet {
decode_offset = 0;
token;
append_buffer = buffer => {
append_buffer = (buffer) => {
if (!(buffer instanceof Uint8Array)) {
throw new Error('Buffer must be of type Uint8Array');
}
this.buffer =
this.buffer ? new Uint8Array([...this.buffer, ...buffer ]) : buffer;
this.buffer = this.buffer
? new Uint8Array([...this.buffer, ...buffer])
: buffer;
};
clear = () => {
@@ -42,13 +45,15 @@ export default class packet {
this.decode_offset = 0;
};
decode_buffer = length => {
decode_buffer = (length) => {
if (!this.buffer) {
throw new Error('Invalid buffer');
}
const ret =
this.buffer.slice(this.decode_offset, this.decode_offset + length);
const ret = this.buffer.slice(
this.decode_offset,
this.decode_offset + length
);
this.decode_offset += length;
return Buffer.from(ret);
};
@@ -68,9 +73,20 @@ export default class packet {
const flags = this.decode_ui32();
const directory = !!this.decode_ui8();
return {
mode, nlink, uid, gid, atime, mtime, ctime, birth_time, size, blocks,
blksize, flags, directory,
}
mode,
nlink,
uid,
gid,
atime,
mtime,
ctime,
birth_time,
size,
blocks,
blksize,
flags,
directory,
};
};
decode_utf8 = () => {
@@ -92,11 +108,13 @@ export default class packet {
throw new Error('String not found in buffer');
};
decode_i8 =
() => { return ui8_array_to_i8(this.buffer, this.decode_offset++); };
decode_i8 = () => {
return ui8_array_to_i8(this.buffer, this.decode_offset++);
};
decode_ui8 =
() => { return ui8_array_to_ui8(this.buffer, this.decode_offset++); };
decode_ui8 = () => {
return ui8_array_to_ui8(this.buffer, this.decode_offset++);
};
decode_i16 = () => {
const ret = be_ui8_array_to_i16(this.buffer, this.decode_offset);
@@ -124,7 +142,7 @@ export default class packet {
decode_i64 = () => {
const ret = new Int64BE(
this.buffer.slice(this.decode_offset, this.decode_offset + 8),
this.buffer.slice(this.decode_offset, this.decode_offset + 8)
);
this.decode_offset += 8;
return ret.toString(10);
@@ -132,7 +150,7 @@ export default class packet {
decode_ui64 = () => {
const ret = new Uint64BE(
this.buffer.slice(this.decode_offset, this.decode_offset + 8),
this.buffer.slice(this.decode_offset, this.decode_offset + 8)
);
this.decode_offset += 8;
return ret.toString(10);
@@ -146,10 +164,9 @@ export default class packet {
const key = Uint8Array.from(hash.array());
const nonce = this.buffer.slice(0, 12);
this.buffer = new JSChaCha20(key, nonce, 0)
.decrypt(
this.buffer.slice(12),
);
this.buffer = new JSChaCha20(key, nonce, 0).decrypt(
this.buffer.slice(12)
);
this.decode_offset = packet.HEADER.length;
@@ -164,65 +181,93 @@ export default class packet {
}
};
encode_buffer = buffer => { this.append_buffer(new Uint8Array(buffer)); };
encode_buffer = (buffer) => {
this.append_buffer(new Uint8Array(buffer));
};
encode_i8 = num => { this.append_buffer(i8_to_ui8_array(num)); };
encode_i8 = (num) => {
this.append_buffer(i8_to_ui8_array(num));
};
encode_top_i8 = num => { this.push_buffer(i8_to_ui8_array(num)); };
encode_top_i8 = (num) => {
this.push_buffer(i8_to_ui8_array(num));
};
encode_u8 = num => { this.append_buffer(ui8_to_ui8_array(num)); };
encode_u8 = (num) => {
this.append_buffer(ui8_to_ui8_array(num));
};
encode_top_u8 = num => { this.push_buffer(ui8_to_ui8_array(num)); };
encode_top_u8 = (num) => {
this.push_buffer(ui8_to_ui8_array(num));
};
encode_i16 = num => { this.append_buffer(i16_to_be_ui8_array(num)); };
encode_i16 = (num) => {
this.append_buffer(i16_to_be_ui8_array(num));
};
encode_top_i16 = num => { this.push_buffer(i16_to_be_ui8_array(num)); };
encode_top_i16 = (num) => {
this.push_buffer(i16_to_be_ui8_array(num));
};
encode_ui16 = num => { this.append_buffer(ui16_to_be_ui8_array(num)); };
encode_ui16 = (num) => {
this.append_buffer(ui16_to_be_ui8_array(num));
};
encode_top_ui16 = num => { this.push_buffer(ui16_to_be_ui8_array(num)); };
encode_top_ui16 = (num) => {
this.push_buffer(ui16_to_be_ui8_array(num));
};
encode_i32 = num => { this.append_buffer(i32_to_be_ui8_array(num)); };
encode_i32 = (num) => {
this.append_buffer(i32_to_be_ui8_array(num));
};
encode_top_i32 = num => { this.push_buffer(i32_to_be_ui8_array(num)); };
encode_top_i32 = (num) => {
this.push_buffer(i32_to_be_ui8_array(num));
};
encode_ui32 = num => { this.append_buffer(ui32_to_be_ui8_array(num)); };
encode_ui32 = (num) => {
this.append_buffer(ui32_to_be_ui8_array(num));
};
encode_top_ui32 = num => { this.push_buffer(ui32_to_be_ui8_array(num)); };
encode_top_ui32 = (num) => {
this.push_buffer(ui32_to_be_ui8_array(num));
};
encode_i64 = num => {
encode_i64 = (num) => {
this.append_buffer(new Uint8Array(new Int64BE(num).toArray()));
};
encode_top_i64 =
num => { this.push_buffer(new Uint8Array(new Int64BE(num).toArray())); };
encode_top_i64 = (num) => {
this.push_buffer(new Uint8Array(new Int64BE(num).toArray()));
};
encode_ui64 = num => {
encode_ui64 = (num) => {
this.append_buffer(new Uint8Array(new Uint64BE(num).toArray()));
};
encode_top_ui64 =
num => { this.push_buffer(new Uint8Array(new Uint64BE(num).toArray())); };
encode_top_ui64 = (num) => {
this.push_buffer(new Uint8Array(new Uint64BE(num).toArray()));
};
encode_utf8 = str => {
encode_utf8 = (str) => {
if (!(typeof str === 'string' || str instanceof String)) {
throw new Error('Value must be of type string');
}
const buffer = new Uint8Array([...new TextEncoder().encode(str), 0 ]);
const buffer = new Uint8Array([...new TextEncoder().encode(str), 0]);
this.append_buffer(buffer);
};
encode_top_utf8 = str => {
encode_top_utf8 = (str) => {
if (!(typeof str === 'string' || str instanceof String)) {
throw new Error('Value must be of type string');
}
const buffer = new Uint8Array([...new TextEncoder().encode(str), 0 ]);
const buffer = new Uint8Array([...new TextEncoder().encode(str), 0]);
this.push_buffer(buffer);
};
encrypt = async nonce => {
encrypt = async (nonce) => {
try {
this.push_buffer(packet.HEADER);
const hash = sha256.create();
@@ -242,12 +287,13 @@ export default class packet {
}
};
push_buffer = buffer => {
push_buffer = (buffer) => {
if (!(buffer instanceof Uint8Array)) {
throw new Error('Buffer must be of type Uint8Array');
}
this.buffer =
this.buffer ? new Uint8Array([...buffer, ...this.buffer ]) : buffer;
this.buffer = this.buffer
? new Uint8Array([...buffer, ...this.buffer])
: buffer;
};
}