morethanwords
5 years ago
45 changed files with 15628 additions and 13772 deletions
@ -1,5 +0,0 @@
@@ -1,5 +0,0 @@
|
||||
class AppSharedMediaManager { |
||||
|
||||
} |
||||
|
||||
export default new AppSharedMediaManager(); |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
import { convertToArrayBuffer, convertToByteArray } from "../bin_utils"; |
||||
|
||||
export default abstract class CryptoWorkerMethods { |
||||
abstract performTaskWorker<T>(task: string, ...args: any[]): Promise<T>; |
||||
|
||||
public sha1Hash(bytes: number[] | ArrayBuffer | Uint8Array): Promise<Uint8Array> { |
||||
return this.performTaskWorker<Uint8Array>('sha1-hash', bytes); |
||||
} |
||||
|
||||
public sha256Hash(bytes: any) { |
||||
return this.performTaskWorker<number[]>('sha256-hash', bytes); |
||||
} |
||||
|
||||
public pbkdf2(buffer: Uint8Array, salt: Uint8Array, iterations: number) { |
||||
return this.performTaskWorker<ArrayBuffer>('pbkdf2', buffer, salt, iterations); |
||||
} |
||||
|
||||
public aesEncrypt(bytes: any, keyBytes: any, ivBytes: any) { |
||||
return this.performTaskWorker<ArrayBuffer>('aes-encrypt', convertToArrayBuffer(bytes), |
||||
convertToArrayBuffer(keyBytes), convertToArrayBuffer(ivBytes)); |
||||
} |
||||
|
||||
public aesDecrypt(encryptedBytes: any, keyBytes: any, ivBytes: any): Promise<ArrayBuffer> { |
||||
return this.performTaskWorker<ArrayBuffer>('aes-decrypt', |
||||
encryptedBytes, keyBytes, ivBytes) |
||||
.then(bytes => convertToArrayBuffer(bytes)); |
||||
} |
||||
|
||||
public rsaEncrypt(publicKey: {modulus: string, exponent: string}, bytes: any): Promise<number[]> { |
||||
return this.performTaskWorker<number[]>('rsa-encrypt', publicKey, bytes); |
||||
} |
||||
|
||||
public factorize(bytes: any) { |
||||
bytes = convertToByteArray(bytes); |
||||
|
||||
return this.performTaskWorker<[number[], number[], number]>('factorize', bytes); |
||||
} |
||||
|
||||
public modPow(x: any, y: any, m: any) { |
||||
return this.performTaskWorker<number[]>('mod-pow', x, y, m); |
||||
} |
||||
|
||||
public gzipUncompress<T>(bytes: ArrayBuffer, toString?: boolean) { |
||||
return this.performTaskWorker<T>('unzip', bytes, toString); |
||||
} |
||||
} |
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
import apiManager from "./apiManager"; |
||||
import AppStorage from '../storage'; |
||||
import cryptoWorker from "../crypto/cryptoworker"; |
||||
import networkerFactory from "./networkerFactory"; |
||||
|
||||
//const ctx: Worker = self as any;
|
||||
const ctx = self; |
||||
|
||||
console.error('INCLUDE !!!', new Error().stack); |
||||
|
||||
networkerFactory.setUpdatesProcessor((obj, bool) => { |
||||
ctx.postMessage({update: {obj, bool}}); |
||||
}); |
||||
|
||||
ctx.onmessage = function(e) { |
||||
var taskID = e.data.taskID; |
||||
|
||||
if(e.data.useLs) { |
||||
AppStorage.finishTask(e.data.taskID, e.data.args); |
||||
return; |
||||
} |
||||
|
||||
switch(e.data.task) { |
||||
case 'unzip': |
||||
return cryptoWorker.gzipUncompress.apply(cryptoWorker, e.data.args).then(result => { |
||||
ctx.postMessage({taskID: taskID, result: result}); |
||||
}); |
||||
|
||||
default: |
||||
return apiManager[e.data.task].apply(apiManager, e.data.args).then(result => { |
||||
console.log(e.data.task + ' result:', result, taskID); |
||||
ctx.postMessage({taskID: taskID, result: result}); |
||||
}).catch(err => { |
||||
console.error(e.data.task + ' err:', err, taskID); |
||||
ctx.postMessage({taskID: taskID, error: err}); |
||||
}); |
||||
//throw new Error('Unknown task: ' + e.data.task);
|
||||
} |
||||
} |
||||
|
||||
ctx.postMessage('ready'); |
@ -0,0 +1,15 @@
@@ -0,0 +1,15 @@
|
||||
export const App = { |
||||
id: 1025907, |
||||
hash: '452b0359b988148995f22ff0f4229750', |
||||
version: '0.3.0', |
||||
domains: [] as string[], |
||||
baseDcID: 2 |
||||
}; |
||||
|
||||
export const Modes = { |
||||
test: location.search.indexOf('test=1') > 0/* || true */, |
||||
debug: location.search.indexOf('debug=1') > 0, |
||||
http: location.search.indexOf('http=1') > 0, |
||||
ssl: location.search.indexOf('ssl=1') > 0 || location.protocol == 'https:' && location.search.indexOf('ssl=0') == -1, |
||||
multipleConnections: true |
||||
}; |
@ -0,0 +1,150 @@
@@ -0,0 +1,150 @@
|
||||
import {dT, isObject} from '../utils'; |
||||
import AppStorage from '../storage'; |
||||
import CryptoWorkerMethods from '../crypto/crypto_methods'; |
||||
|
||||
type Task = { |
||||
taskID: number, |
||||
task: string, |
||||
args: any[] |
||||
}; |
||||
|
||||
class ApiManagerProxy extends CryptoWorkerMethods { |
||||
private webWorker: Worker | boolean = false; |
||||
private taskID = 0; |
||||
private awaiting: { |
||||
[id: number]: { |
||||
resolve: any, |
||||
reject: any, |
||||
taskName: string |
||||
} |
||||
} = {} as any; |
||||
private pending: Array<Task> = []; |
||||
private debug = true; |
||||
|
||||
public updatesProcessor: (obj: any, bool: boolean) => void = null; |
||||
|
||||
constructor() { |
||||
super(); |
||||
console.log(dT(), 'ApiManagerProxy constructor'); |
||||
|
||||
if(window.Worker) { |
||||
import('./mtproto.worker.js').then((worker: any) => { |
||||
var tmpWorker = new worker.default(); |
||||
tmpWorker.onmessage = (e: any) => { |
||||
if(!this.webWorker) { |
||||
this.webWorker = tmpWorker; |
||||
console.info(dT(), 'ApiManagerProxy set webWorker'); |
||||
this.releasePending(); |
||||
} |
||||
|
||||
if(!isObject(e.data)) { |
||||
return; |
||||
} |
||||
|
||||
if(e.data.useLs) { |
||||
// @ts-ignore
|
||||
AppStorage[e.data.task](...e.data.args).then(res => { |
||||
(this.webWorker as Worker).postMessage({useLs: true, taskID: e.data.taskID, args: res}); |
||||
}); |
||||
} else if(e.data.update) { |
||||
if(this.updatesProcessor) { |
||||
this.updatesProcessor(e.data.update.obj, e.data.update.bool); |
||||
} |
||||
} else { |
||||
this.finalizeTask(e.data.taskID, e.data.result, e.data.error); |
||||
} |
||||
}; |
||||
|
||||
tmpWorker.onerror = (error: any) => { |
||||
console.error('ApiManagerProxy error', error); |
||||
this.webWorker = false; |
||||
}; |
||||
}); |
||||
} |
||||
} |
||||
|
||||
private finalizeTask(taskID: number, result: any, error: any) { |
||||
let deferred = this.awaiting[taskID]; |
||||
if(deferred !== undefined) { |
||||
this.debug && console.log(dT(), 'ApiManagerProxy done', deferred.taskName, result, error); |
||||
result === undefined ? deferred.reject(error) : deferred.resolve(result); |
||||
delete this.awaiting[taskID]; |
||||
} |
||||
} |
||||
|
||||
public performTaskWorker<T>(task: string, ...args: any[]) { |
||||
this.debug && console.log(dT(), 'ApiManagerProxy start', task, args); |
||||
|
||||
return new Promise<T>((resolve, reject) => { |
||||
this.awaiting[this.taskID] = {resolve, reject, taskName: task}; |
||||
|
||||
let params = { |
||||
task, |
||||
taskID: this.taskID, |
||||
args |
||||
}; |
||||
|
||||
//(this.webWorker as Worker).postMessage(params);
|
||||
this.pending.push(params); |
||||
this.releasePending(); |
||||
|
||||
this.taskID++; |
||||
}); |
||||
} |
||||
|
||||
private releasePending() { |
||||
if(this.webWorker) { |
||||
this.pending.forEach(pending => { |
||||
(this.webWorker as Worker).postMessage(pending); |
||||
}); |
||||
|
||||
this.pending.length = 0; |
||||
} |
||||
} |
||||
|
||||
public setUpdatesProcessor(callback: (obj: any, bool: boolean) => void) { |
||||
this.updatesProcessor = callback; |
||||
} |
||||
|
||||
public invokeApi(method: string, params: any = {}, options: { |
||||
dcID?: number, |
||||
timeout?: number, |
||||
noErrorBox?: boolean, |
||||
fileUpload?: boolean, |
||||
ignoreErrors?: boolean, |
||||
fileDownload?: boolean, |
||||
createNetworker?: boolean, |
||||
singleInRequest?: boolean, |
||||
startMaxLength?: number, |
||||
|
||||
waitTime?: number, |
||||
stopTime?: number, |
||||
rawError?: any |
||||
} = {}): Promise<any> { |
||||
return this.performTaskWorker('invokeApi', method, params, options); |
||||
} |
||||
|
||||
public setUserAuth(userAuth: {id: number}) { |
||||
return this.performTaskWorker('setUserAuth', userAuth); |
||||
} |
||||
|
||||
public getNetworker(dc_id: number) { |
||||
return this.performTaskWorker('getNetworker', dc_id); |
||||
} |
||||
|
||||
public getUserID(): Promise<number> { |
||||
return this.performTaskWorker('getUserID'); |
||||
} |
||||
|
||||
public logOut(): Promise<void> { |
||||
return this.performTaskWorker('logOut'); |
||||
} |
||||
|
||||
public checkPassword(value: string): Promise<any> { |
||||
return this.performTaskWorker('checkPassword', value); |
||||
} |
||||
} |
||||
|
||||
const apiManagerProxy = new ApiManagerProxy(); |
||||
(window as any).apiManagerProxy = apiManagerProxy; |
||||
export default apiManagerProxy; |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue