Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.8 KiB

import { bytesToHex, bytesFromHex, bufferConcats } from '../helpers/bytes';
import { nextRandomInt } from '../helpers/random';
4 years ago
//export const secureRandom = new SecureRandom();
4 years ago
Object.defineProperty(Uint8Array.prototype, 'hex', {
get: function(): string {
return bytesToHex([...this]);
},
4 years ago
set: function(str: string) {
this.set(bytesFromHex(str));
},
enumerable: true,
configurable: true
});
Uint8Array.prototype.randomize = function() {
//secureRandom.nextBytes(this);
for(let i = 0; i < this.length; ++i) {
this[i] = nextRandomInt(255);
}
4 years ago
return this;
};
Uint8Array.prototype.concat = function(...args: Array<Uint8Array | ArrayBuffer | number[]>) {
return bufferConcats(this, ...args);
};
/* Uint8Array.prototype.toString = function() {
return String.fromCharCode.apply(null, [...this]);
}; */
Uint8Array.prototype.toJSON = function() {
return [...this];
//return {type: 'bytes', value: [...this]};
};
4 years ago
Array.prototype.findAndSplice = function<T>(verify: (value: T, index?: number, array?: Array<T>) => boolean) {
let index = this.findIndex(verify);
return index !== -1 ? this.splice(index, 1)[0] : undefined;
};
String.prototype.toHHMMSS = function(leadZero = false) {
const sec_num = parseInt(this + '', 10);
const hours = Math.floor(sec_num / 3600);
4 years ago
let minutes: any = Math.floor((sec_num - (hours * 3600)) / 60);
let seconds: any = sec_num - (hours * 3600) - (minutes * 60);
if(hours) leadZero = true;
4 years ago
if(minutes < 10) minutes = leadZero ? "0" + minutes : minutes;
if(seconds < 10) seconds = "0" + seconds;
return (hours ? /* ('0' + hours).slice(-2) */hours + ':' : '') + minutes + ':' + seconds;
};
4 years ago
/* Promise.prototype.finally = Promise.prototype.finally || {
finally(fn: () => any) {
const onFinally = (callback: typeof fn) => Promise.resolve(fn()).then(callback);
return this.then(
result => onFinally(() => result),
reason => onFinally(() => Promise.reject(reason))
);
}
}.finally; */
Promise.prototype.finally = Promise.prototype.finally || function<T>(this: Promise<T>, fn: () => any) {
const onFinally = (callback: typeof fn) => Promise.resolve(fn()).then(callback);
return this.then(
result => onFinally(() => result),
reason => onFinally(() => Promise.reject(reason))
);
};
4 years ago
declare global {
interface Uint8Array {
hex: string;
randomize: () => Uint8Array,
concat: (...args: Array<Uint8Array | ArrayBuffer | number[]>) => Uint8Array,
//toString: () => string,
toJSON: () => number[],
//toJSON: () => {type: 'bytes', value: number[]},
4 years ago
}
4 years ago
interface Array<T> {
4 years ago
findAndSplice(verify: (value: T, index?: number, array?: Array<T>) => boolean): T;
}
4 years ago
interface String {
toHHMMSS(leadZero?: boolean): string;
4 years ago
}
interface Promise<T> {
finally: (onfinally?: () => void) => Promise<T>;
}
4 years ago
}