2018-12-21 17:03:26 +01:00
|
|
|
import MyWorker from './worker.js';
|
2019-04-24 17:44:12 +02:00
|
|
|
import localforage from 'localforage';
|
2018-12-21 17:03:26 +01:00
|
|
|
import './third_party/broadcastchannel.js';
|
|
|
|
import uuid4 from 'uuid/v4';
|
|
|
|
import log from './logger.js';
|
|
|
|
|
|
|
|
const sleep = ms => new Promise(res => setTimeout(res, ms));
|
|
|
|
|
2019-02-04 16:06:08 +01:00
|
|
|
/**
|
2019-04-23 11:06:55 +02:00
|
|
|
* TDLib in a browser
|
2019-02-04 16:06:08 +01:00
|
|
|
*
|
2019-04-23 11:06:55 +02:00
|
|
|
* TDLib can be compiled to WebAssembly or asm.js using Emscripten compiler and used in a browser from JavaScript.
|
|
|
|
* This is a convenient wrapper for TDLib in a browser which controls TDLib instance creation, handles interaction
|
|
|
|
* with the TDLib and manages a filesystem for persistent TDLib data.
|
|
|
|
* TDLib instance is created in a Web Worker, because TDLib needs synchronous access to filesystem and the IndexedDB.
|
|
|
|
* TdClient just sends queries to the Web Worker and receive updates and results from it.
|
|
|
|
* <br>
|
|
|
|
* <br>
|
|
|
|
* Differences from TDLib API:<br>
|
|
|
|
* 1. Added the update <code>updateFatalError error:string = Update;</code> which is sent whenever a TDLib fatal error is encountered.<br>
|
2019-04-24 21:05:09 +02:00
|
|
|
* 2. Added the field <code>idb_key</code> to <code>file</code> object, which contains IndexedDB key in which the file content is stored.<br>
|
|
|
|
* This field is non-empty only for fully downloaded files. IndexedDB database name is chosen during TdClient creation via options.instanceName parameter.<br>
|
2019-04-23 11:06:55 +02:00
|
|
|
* 3. Added the method <code>setJsLogVerbosityLevel new_verbosity_level:string = Ok;</code>, which allows to change the verbosity level of tdweb logging.<br>
|
2019-04-24 13:00:02 +02:00
|
|
|
* 4. Added the possibility to use blobs as input files via constructor <code>inputFileBlob data:<JavaScript blob> = InputFile;</code>.<br>
|
2019-04-23 11:06:55 +02:00
|
|
|
* 5. Added the method <code>readFilePart path:string offset:int64 size:int64 = FilePart;</code> and class <code>filePart data:<JavaScript blob> = FilePart;</code><br>
|
|
|
|
* which can be used on a partially downloaded file to support media streaming.<br>
|
|
|
|
* 6. Methods <code>getStorageStatistics</code>, <code>getStorageStatisticsFast</code>, <code>optimizeStorage</code>, <code>addProxy</code> are not supported.<br>
|
2019-02-04 16:06:08 +01:00
|
|
|
* <br>
|
|
|
|
*/
|
2018-12-21 17:03:26 +01:00
|
|
|
class TdClient {
|
2019-02-04 16:06:08 +01:00
|
|
|
/**
|
2019-04-23 11:06:55 +02:00
|
|
|
* @callback TdClient~updateCallback
|
2019-04-23 14:07:36 +02:00
|
|
|
* @param {Object} update The update.
|
2019-02-04 16:06:08 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2019-04-23 11:06:55 +02:00
|
|
|
* Create TdClient.
|
|
|
|
* @param {Object} options - The options for TDLib instance creation.
|
|
|
|
* @param {TdClient~updateCallback} options.onUpdate - The callback for all incoming updates.
|
2019-04-24 13:00:02 +02:00
|
|
|
* @param {string} [options.instanceName=tdlib] - The name of the TDLib instance. Currently only one instance of TdClient with a given name is allowed. All but one created instances with a given name will be automatically closed. Usually, the newest non-background instance is kept alive. Files will be stored in IndexedDb table with the same name.
|
2019-04-23 11:06:55 +02:00
|
|
|
* @param {boolean} [options.isBackground=false] - Pass true, if the instance is opened from the background.
|
2019-04-24 21:05:09 +02:00
|
|
|
* @param {string} [options.jsLogVerbosityLevel=info] - The initial verbosity level of the JavaScript part of the code (one of 'error', 'warning', 'info', 'log', 'debug').
|
2019-04-23 11:06:55 +02:00
|
|
|
* @param {number} [options.logVerbosityLevel=2] - The initial verbosity level for TDLib internal logging (0-1023).
|
2019-04-24 13:00:02 +02:00
|
|
|
* @param {boolean} [options.useDatabase=true] - Pass false to use TDLib without database and secret chats. It will significantly improve load time, but some functionality will be unavailable.
|
|
|
|
* @param {boolean} [options.readOnly=false] - For debug only. Pass true to open TDLib database in read-only mode
|
2019-04-24 21:05:09 +02:00
|
|
|
* @param {string} [options.mode=auto] - For debug only. The type of the TDLib build to use. 'asmjs' for asm.js and 'wasm' for WebAssembly. If mode == 'auto' WebAbassembly will be used if supported by browser, asm.js otherwise.
|
2019-02-04 16:06:08 +01:00
|
|
|
*/
|
2018-12-21 17:03:26 +01:00
|
|
|
constructor(options) {
|
2019-02-14 15:25:38 +01:00
|
|
|
log.setVerbosity(options.jsLogVerbosityLevel);
|
2018-12-21 17:03:26 +01:00
|
|
|
this.worker = new MyWorker();
|
|
|
|
var self = this;
|
2019-04-24 13:00:02 +02:00
|
|
|
this.worker.onmessage = e => {
|
|
|
|
self.onResponse(e.data);
|
2018-12-21 17:03:26 +01:00
|
|
|
};
|
|
|
|
this.query_id = 0;
|
|
|
|
this.query_callbacks = new Map();
|
2019-02-04 16:06:08 +01:00
|
|
|
if ('onUpdate' in options) {
|
|
|
|
this.onUpdate = options.onUpdate;
|
|
|
|
delete options.onUpdate;
|
|
|
|
}
|
2019-04-24 13:00:02 +02:00
|
|
|
options.instanceName = options.instanceName || 'tdlib';
|
2019-04-24 17:44:12 +02:00
|
|
|
this.fileManager = new FileManager(options.instanceName);
|
2018-12-21 17:03:26 +01:00
|
|
|
this.worker.postMessage({ '@type': 'init', options: options });
|
|
|
|
this.closeOtherClients(options);
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:06:08 +01:00
|
|
|
/**
|
2019-04-23 11:06:55 +02:00
|
|
|
* Send a query to TDLib.
|
2019-02-04 16:06:08 +01:00
|
|
|
*
|
2019-04-23 11:06:55 +02:00
|
|
|
* If the query contains an '@extra' field, the same field will be added into the result.
|
2019-02-04 16:06:08 +01:00
|
|
|
*
|
2019-04-23 11:06:55 +02:00
|
|
|
* @param {Object} query - The query for TDLib. See the [td_api.tl]{@link https://github.com/tdlib/td/blob/master/td/generate/scheme/td_api.tl} scheme or
|
|
|
|
* the automatically generated [HTML documentation]{@link https://core.telegram.org/tdlib/docs/td__api_8h.html}
|
|
|
|
* for a list of all available TDLib [methods]{@link https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1_function.html} and
|
|
|
|
* [classes]{@link https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1_object.html}.
|
|
|
|
* @returns {Promise} Promise object represents the result of the query.
|
2019-02-04 16:06:08 +01:00
|
|
|
*/
|
|
|
|
send(query) {
|
|
|
|
this.query_id++;
|
|
|
|
if (query['@extra']) {
|
|
|
|
query['@extra'] = {
|
|
|
|
'@old_extra': JSON.parse(JSON.stringify(query.extra)),
|
|
|
|
query_id: this.query_id
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
query['@extra'] = {
|
|
|
|
query_id: this.query_id
|
|
|
|
};
|
|
|
|
}
|
2019-02-14 15:25:38 +01:00
|
|
|
if (query['@type'] === 'setJsLogVerbosityLevel') {
|
|
|
|
log.setVerbosity(query.new_verbosity_level);
|
2019-02-04 16:06:08 +01:00
|
|
|
}
|
|
|
|
|
2019-02-14 15:25:38 +01:00
|
|
|
log.debug('send to worker: ', query);
|
2019-04-24 13:00:02 +02:00
|
|
|
let res = new Promise((resolve, reject) => {
|
2019-02-04 16:06:08 +01:00
|
|
|
this.query_callbacks.set(this.query_id, [resolve, reject]);
|
|
|
|
});
|
2019-04-24 13:00:02 +02:00
|
|
|
this.externalPostMessage(query);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @private */
|
|
|
|
externalPostMessage(query) {
|
|
|
|
let unsupportedMethods = [
|
|
|
|
'getStorageStatistics',
|
|
|
|
'getStorageStatisticsFast',
|
|
|
|
'optimizeStorage',
|
|
|
|
'addProxy',
|
|
|
|
'init',
|
|
|
|
'start'
|
|
|
|
];
|
|
|
|
if (unsupportedMethods.includes(query['@type'])) {
|
|
|
|
this.onResponse({
|
|
|
|
'@type': 'error',
|
|
|
|
'@extra': query['@extra'],
|
|
|
|
code: 400,
|
2019-04-24 21:05:09 +02:00
|
|
|
message: "Method '" + query['@type'] + "' is not supported"
|
2019-04-24 13:00:02 +02:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2019-04-24 17:44:12 +02:00
|
|
|
if (query['@type'] === 'readFile') {
|
|
|
|
this.readFile(query);
|
|
|
|
return;
|
|
|
|
}
|
2019-04-24 13:00:02 +02:00
|
|
|
this.worker.postMessage(query);
|
|
|
|
}
|
|
|
|
|
2019-04-24 17:44:12 +02:00
|
|
|
/** @private */
|
|
|
|
async readFile(query) {
|
|
|
|
let response = await this.fileManager.readFile(query);
|
|
|
|
this.onResponse(response);
|
|
|
|
}
|
|
|
|
|
2019-04-24 13:00:02 +02:00
|
|
|
/** @private */
|
|
|
|
onResponse(response) {
|
|
|
|
log.debug(
|
|
|
|
'receive from worker: ',
|
|
|
|
JSON.parse(
|
|
|
|
JSON.stringify(response, (key, value) => {
|
|
|
|
if (key === 'arr') {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2019-04-24 17:44:12 +02:00
|
|
|
|
|
|
|
// for FileManager
|
|
|
|
response = this.prepareResponse(response);
|
|
|
|
|
2019-04-24 13:00:02 +02:00
|
|
|
if ('@extra' in response) {
|
|
|
|
var query_id = response['@extra'].query_id;
|
|
|
|
var [resolve, reject] = this.query_callbacks.get(query_id);
|
|
|
|
this.query_callbacks.delete(query_id);
|
|
|
|
if ('@old_extra' in response['@extra']) {
|
|
|
|
response['@extra'] = response['@extra']['@old_extra'];
|
|
|
|
}
|
|
|
|
if (resolve) {
|
|
|
|
if (response['@type'] === 'error') {
|
|
|
|
reject(response);
|
|
|
|
} else {
|
|
|
|
resolve(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (response['@type'] === 'inited') {
|
|
|
|
this.onInited();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
response['@type'] === 'updateAuthorizationState' &&
|
|
|
|
response.authorization_state['@type'] === 'authorizationStateClosed'
|
|
|
|
) {
|
|
|
|
this.onClosed();
|
|
|
|
}
|
|
|
|
this.onUpdate(response);
|
|
|
|
}
|
2019-02-04 16:06:08 +01:00
|
|
|
}
|
|
|
|
|
2019-04-24 17:44:12 +02:00
|
|
|
/** @private */
|
|
|
|
prepareFile(file) {
|
|
|
|
return this.fileManager.registerFile(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @private */
|
|
|
|
prepareResponse(response) {
|
|
|
|
if (response['@type'] === 'file') {
|
|
|
|
return this.prepareFile(response);
|
|
|
|
}
|
|
|
|
for (var key in response) {
|
|
|
|
let field = response[key];
|
|
|
|
if (field && typeof field === 'object') {
|
|
|
|
response[key] = this.prepareResponse(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:06:08 +01:00
|
|
|
/** @private */
|
2018-12-21 17:03:26 +01:00
|
|
|
onBroadcastMessage(e) {
|
|
|
|
var message = e.data;
|
|
|
|
log.info('got broadcast message: ', message);
|
|
|
|
if (message.isBackground && !this.isBackground) {
|
|
|
|
// continue
|
|
|
|
} else if (
|
|
|
|
(!message.isBackground && this.isBackground) ||
|
|
|
|
message.timestamp > this.timestamp
|
|
|
|
) {
|
|
|
|
this.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (message.state === 'closed') {
|
|
|
|
this.waitSet.delete(message.uid);
|
|
|
|
if (this.waitSet.size === 0) {
|
|
|
|
log.info('onWaitSetEmpty');
|
|
|
|
this.onWaitSetEmpty();
|
|
|
|
this.onWaitSetEmpty = () => {};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.waitSet.add(message.uid);
|
|
|
|
if (message.state !== 'closing') {
|
|
|
|
this.postState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:06:08 +01:00
|
|
|
/** @private */
|
2018-12-21 17:03:26 +01:00
|
|
|
postState() {
|
|
|
|
let state = {
|
|
|
|
id: this.uid,
|
|
|
|
state: this.state,
|
|
|
|
timestamp: this.timestamp,
|
|
|
|
isBackground: this.isBackground
|
|
|
|
};
|
|
|
|
log.info('Post state: ', state);
|
|
|
|
this.channel.postMessage(state);
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:06:08 +01:00
|
|
|
/** @private */
|
2018-12-21 17:03:26 +01:00
|
|
|
onWaitSetEmpty() {
|
|
|
|
// nop
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:06:08 +01:00
|
|
|
/** @private */
|
2018-12-21 17:03:26 +01:00
|
|
|
onInited() {
|
|
|
|
this.isInited = true;
|
|
|
|
this.doSendStart();
|
|
|
|
}
|
2019-02-04 16:06:08 +01:00
|
|
|
|
|
|
|
/** @private */
|
2018-12-21 17:03:26 +01:00
|
|
|
sendStart() {
|
|
|
|
this.wantSendStart = true;
|
|
|
|
this.doSendStart();
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:06:08 +01:00
|
|
|
/** @private */
|
2018-12-21 17:03:26 +01:00
|
|
|
doSendStart() {
|
|
|
|
if (!this.isInited || !this.wantSendStart || this.state !== 'start') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.wantSendStart = false;
|
|
|
|
this.state = 'active';
|
|
|
|
let query = { '@type': 'start' };
|
|
|
|
log.info('send to worker: ', query);
|
|
|
|
this.worker.postMessage(query);
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:06:08 +01:00
|
|
|
/** @private */
|
2018-12-21 17:03:26 +01:00
|
|
|
onClosed() {
|
|
|
|
this.isClosing = true;
|
|
|
|
this.worker.terminate();
|
|
|
|
log.info('worker is terminated');
|
|
|
|
this.state = 'closed';
|
|
|
|
this.postState();
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:06:08 +01:00
|
|
|
/** @private */
|
2018-12-21 17:03:26 +01:00
|
|
|
close() {
|
|
|
|
if (this.isClosing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.isClosing = true;
|
|
|
|
|
|
|
|
log.info('close state: ', this.state);
|
|
|
|
|
|
|
|
if (this.state === 'start') {
|
|
|
|
this.onClosed();
|
|
|
|
this.onUpdate({
|
|
|
|
'@type': 'updateAuthorizationState',
|
|
|
|
authorization_state: {
|
|
|
|
'@type': 'authorizationStateClosed'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let query = { '@type': 'close' };
|
|
|
|
log.info('send to worker: ', query);
|
|
|
|
this.worker.postMessage(query);
|
|
|
|
|
|
|
|
this.state = 'closing';
|
|
|
|
this.postState();
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:06:08 +01:00
|
|
|
/** @private */
|
2018-12-21 17:03:26 +01:00
|
|
|
async closeOtherClients(options) {
|
|
|
|
this.uid = uuid4();
|
|
|
|
this.state = 'start';
|
|
|
|
this.isBackground = !!options.isBackground;
|
|
|
|
this.timestamp = Date.now();
|
|
|
|
this.waitSet = new Set();
|
|
|
|
|
|
|
|
log.info('close other clients');
|
2019-04-24 13:00:02 +02:00
|
|
|
this.channel = new BroadcastChannel(options.instanceName);
|
2018-12-21 17:03:26 +01:00
|
|
|
|
|
|
|
this.postState();
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
this.channel.onmessage = message => {
|
|
|
|
self.onBroadcastMessage(message);
|
|
|
|
};
|
|
|
|
|
|
|
|
await sleep(300);
|
|
|
|
if (this.waitSet.size !== 0) {
|
|
|
|
await new Promise(resolve => {
|
|
|
|
self.onWaitSetEmpty = resolve;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.sendStart();
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:06:08 +01:00
|
|
|
/** @private */
|
2019-04-24 21:05:09 +02:00
|
|
|
onUpdate(update) {
|
2018-12-21 17:03:26 +01:00
|
|
|
log.info('ignore onUpdate');
|
|
|
|
//nop
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 17:44:12 +02:00
|
|
|
|
|
|
|
/** @private */
|
|
|
|
class FileManager {
|
|
|
|
constructor(instanceName) {
|
|
|
|
this.cache = new Map();
|
|
|
|
this.idb = new Promise((resolve, reject) => {
|
|
|
|
const request = window.indexedDB.open(instanceName);
|
|
|
|
request.onsuccess = () => resolve(request.result);
|
|
|
|
request.onerror = () => reject(request.error);
|
|
|
|
});
|
|
|
|
//this.store = localforage.createInstance({
|
|
|
|
//name: instanceName
|
|
|
|
//});
|
|
|
|
this.pending = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
registerFile(file) {
|
|
|
|
if (file.idb_key || file.arr) {
|
|
|
|
file.is_downloading_completed = true;
|
|
|
|
var info = {};
|
|
|
|
let cached_info = this.cache.get(file.id);
|
|
|
|
if (cached_info !== undefined) {
|
|
|
|
info = cached_info;
|
|
|
|
} else {
|
|
|
|
this.cache.set(file.id, info);
|
|
|
|
}
|
|
|
|
if (file.idb_key) {
|
|
|
|
info.idb_key = file.idb_key;
|
|
|
|
}
|
|
|
|
if (file.arr) {
|
|
|
|
info.arr = file.arr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
async flushLoad() {
|
|
|
|
let pending = this.pending;
|
|
|
|
this.pending = [];
|
|
|
|
let idb = await this.idb;
|
|
|
|
let read = idb
|
|
|
|
.transaction(['keyvaluepairs'], 'readonly')
|
|
|
|
.objectStore('keyvaluepairs');
|
|
|
|
log.debug('Load group of files from idb', pending.length);
|
|
|
|
for (const query of pending) {
|
|
|
|
const request = read.get(query.key);
|
|
|
|
request.onsuccess = event => {
|
|
|
|
const blob = event.target.result;
|
|
|
|
if (blob) {
|
|
|
|
query.resolve(blob);
|
|
|
|
} else {
|
|
|
|
query.reject();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
request.onerror = query.reject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
load(key, resolve, reject) {
|
|
|
|
if (this.pending.length === 0) {
|
|
|
|
let self = this;
|
|
|
|
setTimeout(() => {
|
|
|
|
self.flushLoad();
|
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
this.pending.push({ key: key, resolve: resolve, reject: reject });
|
|
|
|
}
|
|
|
|
|
|
|
|
async doLoad(info) {
|
|
|
|
if (info.arr) {
|
|
|
|
return new Blob([info.arr]);
|
|
|
|
}
|
|
|
|
let idb_key = info.idb_key;
|
|
|
|
let self = this;
|
|
|
|
//return this.store.getItem(idb_key);
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
self.load(idb_key, resolve, reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async readFile(query) {
|
|
|
|
try {
|
|
|
|
let info = this.cache.get(query.file_id);
|
|
|
|
if (!info) {
|
|
|
|
throw new Error('File is not loaded');
|
|
|
|
}
|
|
|
|
let data = await this.doLoad(info);
|
|
|
|
return {
|
|
|
|
'@type': 'Blob',
|
|
|
|
'@extra': query['@extra'],
|
|
|
|
data: data
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
return {
|
|
|
|
'@type': 'error',
|
|
|
|
'@extra': query['@extra'],
|
|
|
|
code: 400,
|
|
|
|
message: e
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 17:03:26 +01:00
|
|
|
export default TdClient;
|