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,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;
};
}