2018-12-21 17:03:26 +01:00
|
|
|
import localforage from 'localforage';
|
|
|
|
import log from './logger.js';
|
2019-04-24 09:04:52 +02:00
|
|
|
import { instantiateAny } from './wasm-utils.js';
|
2018-12-21 17:03:26 +01:00
|
|
|
|
|
|
|
import td_wasm_release from './prebuilt/release/td_wasm.wasm';
|
2019-04-23 19:21:54 +02:00
|
|
|
import td_asmjs_mem_release from './prebuilt/release/td_asmjs.js.mem';
|
2018-12-21 17:03:26 +01:00
|
|
|
|
2018-12-27 18:27:26 +01:00
|
|
|
const tdlibVersion = 6;
|
2019-04-24 09:04:52 +02:00
|
|
|
const localForageDrivers = [
|
|
|
|
localforage.INDEXEDDB,
|
|
|
|
localforage.LOCALSTORAGE,
|
|
|
|
'memoryDriver'
|
|
|
|
];
|
2018-12-21 17:03:26 +01:00
|
|
|
|
2018-12-27 18:27:26 +01:00
|
|
|
async function initLocalForage() {
|
|
|
|
// Implement the driver here.
|
2019-04-29 17:22:46 +02:00
|
|
|
const memoryDriver = {
|
2018-12-27 18:27:26 +01:00
|
|
|
_driver: 'memoryDriver',
|
|
|
|
_initStorage: function(options) {
|
2019-04-29 17:22:46 +02:00
|
|
|
const dbInfo = {};
|
2018-12-27 18:27:26 +01:00
|
|
|
if (options) {
|
2019-04-29 17:22:46 +02:00
|
|
|
for (const i in options) {
|
2018-12-27 18:27:26 +01:00
|
|
|
dbInfo[i] = options[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._dbInfo = dbInfo;
|
|
|
|
this._map = new Map();
|
|
|
|
},
|
|
|
|
clear: async function() {
|
|
|
|
this._map.clear();
|
|
|
|
},
|
|
|
|
getItem: async function(key) {
|
2019-04-29 17:22:46 +02:00
|
|
|
const value = this._map.get(key);
|
2019-04-24 09:04:52 +02:00
|
|
|
console.log('getItem', this._map, key, value);
|
2018-12-27 18:27:26 +01:00
|
|
|
return value;
|
|
|
|
},
|
|
|
|
iterate: async function(iteratorCallback) {
|
2019-04-24 09:04:52 +02:00
|
|
|
log.error('iterate is not supported');
|
2018-12-27 18:27:26 +01:00
|
|
|
},
|
|
|
|
key: async function(n) {
|
2019-04-24 09:04:52 +02:00
|
|
|
log.error('key n is not supported');
|
2018-12-27 18:27:26 +01:00
|
|
|
},
|
|
|
|
keys: async function() {
|
|
|
|
return this._map.keys();
|
|
|
|
},
|
|
|
|
length: async function() {
|
|
|
|
return this._map.size();
|
|
|
|
},
|
|
|
|
removeItem: async function(key) {
|
2019-04-24 09:04:52 +02:00
|
|
|
this._map.delete(key);
|
2018-12-27 18:27:26 +01:00
|
|
|
},
|
|
|
|
setItem: async function(key, value) {
|
2019-04-29 17:22:46 +02:00
|
|
|
const originalValue = this._map.get(key);
|
2019-04-24 09:04:52 +02:00
|
|
|
console.log('setItem', this._map, key, value);
|
2018-12-27 18:27:26 +01:00
|
|
|
this._map.set(key, value);
|
|
|
|
return originalValue;
|
|
|
|
}
|
2019-04-24 09:04:52 +02:00
|
|
|
};
|
2018-12-27 18:27:26 +01:00
|
|
|
|
|
|
|
// Add the driver to localForage.
|
|
|
|
localforage.defineDriver(memoryDriver);
|
|
|
|
}
|
|
|
|
|
2019-07-02 15:06:05 +02:00
|
|
|
async function loadTdlibWasm(onFS, wasmUrl) {
|
2019-04-24 21:05:09 +02:00
|
|
|
console.log('loadTdlibWasm');
|
2019-04-29 17:22:46 +02:00
|
|
|
const Module = await import('./prebuilt/release/td_wasm.js');
|
2018-12-21 17:03:26 +01:00
|
|
|
log.info('got td_wasm.js');
|
2019-07-02 15:06:05 +02:00
|
|
|
let td_wasm = td_wasm_release;
|
|
|
|
if (wasmUrl) {
|
|
|
|
td_wasm = wasmUrl;
|
|
|
|
}
|
2019-04-29 17:22:46 +02:00
|
|
|
const module = Module.default({
|
2019-04-13 13:17:03 +02:00
|
|
|
onRuntimeInitialized: () => {
|
|
|
|
log.info('runtime intialized');
|
|
|
|
},
|
|
|
|
instantiateWasm: (imports, successCallback) => {
|
2019-07-02 15:06:05 +02:00
|
|
|
log.info('start instantiateWasm', td_wasm);
|
2019-04-29 17:22:46 +02:00
|
|
|
const next = instance => {
|
2019-04-13 13:17:03 +02:00
|
|
|
log.info('finish instantiateWasm');
|
|
|
|
successCallback(instance);
|
|
|
|
};
|
|
|
|
instantiateAny(tdlibVersion, td_wasm, imports).then(next);
|
|
|
|
return {};
|
|
|
|
},
|
|
|
|
ENVIROMENT: 'WORKER'
|
|
|
|
});
|
|
|
|
log.info('Got module', module);
|
|
|
|
onFS(module.FS);
|
2019-04-29 17:22:46 +02:00
|
|
|
const TdModule = new Promise((resolve, reject) =>
|
2019-04-13 13:17:03 +02:00
|
|
|
module.then(m => {
|
2018-12-21 17:03:26 +01:00
|
|
|
delete m.then;
|
|
|
|
resolve(m);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return TdModule;
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:05:09 +02:00
|
|
|
async function loadTdlibAsmjs(onFS) {
|
|
|
|
console.log('loadTdlibAsmjs');
|
2019-04-29 17:22:46 +02:00
|
|
|
const Module = await import('./prebuilt/release/td_asmjs.js');
|
2019-04-23 19:21:54 +02:00
|
|
|
console.log('got td_asm.js');
|
2019-04-29 17:22:46 +02:00
|
|
|
const fromFile = 'td_asmjs.js.mem';
|
|
|
|
const toFile = td_asmjs_mem_release;
|
2019-06-22 16:44:53 +02:00
|
|
|
const module = Module.default({
|
2019-04-23 19:21:54 +02:00
|
|
|
onRuntimeInitialized: () => {
|
|
|
|
console.log('runtime intialized');
|
|
|
|
},
|
|
|
|
locateFile: name => {
|
|
|
|
if (name === fromFile) {
|
|
|
|
return toFile;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
},
|
|
|
|
ENVIROMENT: 'WORKER'
|
|
|
|
});
|
|
|
|
onFS(module.FS);
|
2019-04-29 17:22:46 +02:00
|
|
|
const TdModule = new Promise((resolve, reject) =>
|
2019-04-23 19:21:54 +02:00
|
|
|
module.then(m => {
|
|
|
|
delete m.then;
|
|
|
|
resolve(m);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return TdModule;
|
|
|
|
}
|
2018-12-21 17:03:26 +01:00
|
|
|
|
2019-07-02 15:06:05 +02:00
|
|
|
async function loadTdlib(mode, onFS, wasmUrl) {
|
2019-04-23 19:21:54 +02:00
|
|
|
const wasmSupported = (() => {
|
|
|
|
try {
|
2019-04-24 09:04:52 +02:00
|
|
|
if (
|
|
|
|
typeof WebAssembly === 'object' &&
|
|
|
|
typeof WebAssembly.instantiate === 'function'
|
|
|
|
) {
|
|
|
|
const module = new WebAssembly.Module(
|
|
|
|
Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)
|
|
|
|
);
|
2019-04-23 19:21:54 +02:00
|
|
|
if (module instanceof WebAssembly.Module)
|
2019-04-24 09:04:52 +02:00
|
|
|
return (
|
|
|
|
new WebAssembly.Instance(module) instanceof WebAssembly.Instance
|
|
|
|
);
|
2019-04-23 19:21:54 +02:00
|
|
|
}
|
2019-04-24 09:04:52 +02:00
|
|
|
} catch (e) {}
|
2019-04-23 19:21:54 +02:00
|
|
|
return false;
|
|
|
|
})();
|
|
|
|
if (!wasmSupported) {
|
2019-04-24 13:00:02 +02:00
|
|
|
if (mode === 'wasm') {
|
|
|
|
log.error('WebAssembly is not supported, trying to use it anyway');
|
|
|
|
} else {
|
2019-07-23 22:41:47 +02:00
|
|
|
log.warn('WebAssembly is not supported, trying to use asm.js');
|
2019-04-24 13:00:02 +02:00
|
|
|
mode = 'asmjs';
|
|
|
|
}
|
2019-04-23 19:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode === 'asmjs') {
|
2019-04-24 21:05:09 +02:00
|
|
|
return loadTdlibAsmjs(onFS);
|
2019-04-23 19:21:54 +02:00
|
|
|
}
|
2019-07-02 15:06:05 +02:00
|
|
|
return loadTdlibWasm(onFS, wasmUrl);
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class OutboundFileSystem {
|
|
|
|
constructor(root, FS) {
|
|
|
|
this.root = root;
|
|
|
|
this.nextFileId = 0;
|
|
|
|
this.FS = FS;
|
|
|
|
this.files = new Set();
|
|
|
|
FS.mkdir(root);
|
|
|
|
}
|
|
|
|
blobToPath(blob, name) {
|
2019-04-29 17:22:46 +02:00
|
|
|
const dir = this.root + '/' + this.nextFileId;
|
2018-12-21 17:03:26 +01:00
|
|
|
if (!name) {
|
|
|
|
name = 'blob';
|
|
|
|
}
|
|
|
|
this.nextFileId++;
|
|
|
|
this.FS.mkdir(dir);
|
|
|
|
this.FS.mount(
|
|
|
|
this.FS.filesystems.WORKERFS,
|
|
|
|
{
|
|
|
|
blobs: [{ name: name, data: blob }]
|
|
|
|
},
|
|
|
|
dir
|
|
|
|
);
|
2019-04-29 17:22:46 +02:00
|
|
|
const path = dir + '/' + name;
|
2018-12-21 17:03:26 +01:00
|
|
|
this.files.add(path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
forgetPath(path) {
|
|
|
|
if (this.files.has(path)) {
|
|
|
|
this.FS.unmount(path);
|
|
|
|
this.files.delete(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class InboundFileSystem {
|
2019-04-12 20:23:17 +02:00
|
|
|
static async create(dbName, root, FS_promise) {
|
2019-04-29 17:22:46 +02:00
|
|
|
const start = performance.now();
|
2018-12-21 17:03:26 +01:00
|
|
|
try {
|
2019-04-29 17:22:46 +02:00
|
|
|
const ifs = new InboundFileSystem();
|
2019-07-24 14:07:55 +02:00
|
|
|
ifs.pending = [];
|
|
|
|
ifs.pendingHasTimeout = false;
|
|
|
|
ifs.persistCount = 0;
|
|
|
|
ifs.persistSize = 0;
|
|
|
|
ifs.pendingI = 0;
|
|
|
|
ifs.inPersist = false;
|
|
|
|
ifs.totalCount = 0;
|
|
|
|
|
2018-12-21 17:03:26 +01:00
|
|
|
ifs.root = root;
|
|
|
|
|
2019-07-24 14:07:55 +02:00
|
|
|
//ifs.store = localforage.createInstance({
|
|
|
|
//name: dbName,
|
|
|
|
//driver: localForageDrivers
|
|
|
|
//});
|
|
|
|
log.debug('IDB name: ' + dbName);
|
|
|
|
ifs.idb = new Promise((resolve, reject) => {
|
|
|
|
const request = indexedDB.open(dbName);
|
|
|
|
request.onsuccess = () => resolve(request.result);
|
|
|
|
request.onerror = () => reject(request.error);
|
2019-07-25 17:27:03 +02:00
|
|
|
request.onupgradeneeded = () => {
|
|
|
|
request.result.createObjectStore('keyvaluepairs');
|
|
|
|
};
|
2018-12-21 17:03:26 +01:00
|
|
|
});
|
|
|
|
|
2019-04-12 20:23:17 +02:00
|
|
|
ifs.load_pids();
|
|
|
|
|
2019-04-29 17:22:46 +02:00
|
|
|
const FS = await FS_promise;
|
2019-07-25 17:27:03 +02:00
|
|
|
await ifs.idb;
|
2019-04-12 20:23:17 +02:00
|
|
|
ifs.FS = FS;
|
|
|
|
ifs.FS.mkdir(root);
|
2019-04-29 17:22:46 +02:00
|
|
|
const create_time = (performance.now() - start) / 1000;
|
2019-04-12 20:23:17 +02:00
|
|
|
log.debug('InboundFileSystem::create ' + create_time);
|
2018-12-21 17:03:26 +01:00
|
|
|
return ifs;
|
|
|
|
} catch (e) {
|
|
|
|
log.error('Failed to init Inbound FileSystem: ', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:23:17 +02:00
|
|
|
async load_pids() {
|
2019-04-29 17:22:46 +02:00
|
|
|
const keys_start = performance.now();
|
2019-04-12 20:23:17 +02:00
|
|
|
log.debug('InboundFileSystem::create::keys start');
|
2019-07-24 14:07:55 +02:00
|
|
|
//const keys = await this.store.keys();
|
|
|
|
|
|
|
|
let idb = await this.idb;
|
|
|
|
let read = idb
|
|
|
|
.transaction(['keyvaluepairs'], 'readonly')
|
|
|
|
.objectStore('keyvaluepairs');
|
|
|
|
const keys = await new Promise((resolve, reject) => {
|
|
|
|
const request = read.getAllKeys();
|
|
|
|
request.onsuccess = () => resolve(request.result);
|
|
|
|
request.onerror = () => reject(request.error);
|
|
|
|
});
|
|
|
|
|
2019-04-29 17:22:46 +02:00
|
|
|
const keys_time = (performance.now() - keys_start) / 1000;
|
2019-04-24 09:04:52 +02:00
|
|
|
log.debug(
|
|
|
|
'InboundFileSystem::create::keys ' + keys_time + ' ' + keys.length
|
|
|
|
);
|
2019-04-12 20:23:17 +02:00
|
|
|
this.pids = new Set(keys);
|
|
|
|
}
|
|
|
|
|
2018-12-21 17:03:26 +01:00
|
|
|
has(pid) {
|
2019-04-12 20:23:17 +02:00
|
|
|
if (!this.pids) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-21 17:03:26 +01:00
|
|
|
return this.pids.has(pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
forget(pid) {
|
2019-04-12 20:23:17 +02:00
|
|
|
if (this.pids) {
|
|
|
|
this.pids.delete(pid);
|
|
|
|
}
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
|
|
|
|
2019-07-24 14:07:55 +02:00
|
|
|
async doPersist(pid, path, arr, resolve, reject, write) {
|
|
|
|
this.persistCount++;
|
|
|
|
let size = arr.length;
|
|
|
|
this.persistSize += size;
|
2018-12-21 17:03:26 +01:00
|
|
|
try {
|
2019-07-24 14:07:55 +02:00
|
|
|
//log.debug('persist.do start', pid, path, arr.length);
|
|
|
|
//await this.store.setItem(pid, new Blob([arr]));
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
const request = write.put(new Blob([arr]), pid);
|
|
|
|
request.onsuccess = () => resolve(request.result);
|
|
|
|
request.onerror = () => reject(request.error);
|
|
|
|
});
|
2019-04-12 20:23:17 +02:00
|
|
|
if (this.pids) {
|
|
|
|
this.pids.add(pid);
|
|
|
|
}
|
2018-12-21 17:03:26 +01:00
|
|
|
this.FS.unlink(path);
|
|
|
|
} catch (e) {
|
|
|
|
log.error('Failed persist ' + path + ' ', e);
|
|
|
|
}
|
2019-07-24 14:07:55 +02:00
|
|
|
//log.debug('persist.do finish', pid, path, arr.length);
|
|
|
|
this.persistCount--;
|
|
|
|
this.persistSize -= size;
|
|
|
|
resolve();
|
|
|
|
|
|
|
|
this.tryFinishPersist();
|
|
|
|
}
|
|
|
|
|
|
|
|
async flushPersist() {
|
|
|
|
if (this.inPersist) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log.debug('persist.flush');
|
|
|
|
this.inPersist = true;
|
|
|
|
let idb = await this.idb;
|
|
|
|
this.writeBegin = performance.now();
|
|
|
|
let write = idb
|
|
|
|
.transaction(['keyvaluepairs'], 'readwrite')
|
|
|
|
.objectStore('keyvaluepairs');
|
|
|
|
while (
|
|
|
|
this.pendingI < this.pending.length &&
|
|
|
|
this.persistCount < 20 &&
|
|
|
|
this.persistSize < 50 << 20
|
|
|
|
) {
|
|
|
|
var q = this.pending[this.pendingI];
|
|
|
|
this.pending[this.pendingI] = null;
|
|
|
|
// TODO: add to transaction
|
|
|
|
this.doPersist(q.pid, q.path, q.arr, q.resolve, q.reject, write);
|
|
|
|
this.pendingI++;
|
|
|
|
this.totalCount++;
|
|
|
|
}
|
|
|
|
log.debug(
|
|
|
|
'persist.flush transaction cnt=' +
|
|
|
|
this.persistCount +
|
|
|
|
', size=' +
|
|
|
|
this.persistSize
|
|
|
|
);
|
|
|
|
this.inPersist = false;
|
|
|
|
this.tryFinishPersist();
|
|
|
|
}
|
|
|
|
|
|
|
|
async tryFinishPersist() {
|
|
|
|
if (this.inPersist) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.persistCount !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log.debug('persist.finish ' + (performance.now() - this.writeBegin) / 1000);
|
|
|
|
if (this.pendingI === this.pending.length) {
|
|
|
|
this.pending = [];
|
|
|
|
this.pendingHasTimeout = false;
|
|
|
|
this.pendingI = 0;
|
|
|
|
log.debug('persist.finish done');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log.debug('persist.finish continue');
|
|
|
|
this.flushPersist();
|
|
|
|
}
|
|
|
|
|
|
|
|
async persist(pid, path, arr) {
|
|
|
|
if (!this.pendingHasTimeout) {
|
|
|
|
this.pendingHasTimeout = true;
|
|
|
|
log.debug('persist set timeout');
|
|
|
|
setTimeout(() => {
|
|
|
|
this.flushPersist();
|
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
this.pending.push({
|
|
|
|
pid: pid,
|
|
|
|
path: path,
|
|
|
|
arr: arr,
|
|
|
|
resolve: resolve,
|
|
|
|
reject: reject
|
|
|
|
});
|
|
|
|
});
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
2019-07-24 14:07:55 +02:00
|
|
|
|
2019-04-29 17:22:46 +02:00
|
|
|
async unlink(pid) {
|
|
|
|
log.debug('Unlink ' + pid);
|
|
|
|
try {
|
|
|
|
this.forget(pid);
|
2019-07-24 14:07:55 +02:00
|
|
|
//await this.store.removeItem(pid);
|
2019-07-25 17:27:03 +02:00
|
|
|
let idb = await this.idb;
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
let write = idb
|
|
|
|
.transaction(['keyvaluepairs'], 'readwrite')
|
|
|
|
.objectStore('keyvaluepairs');
|
|
|
|
const request = write.delete(pid);
|
|
|
|
request.onsuccess = () => resolve(request.result);
|
|
|
|
request.onerror = () => reject(request.error);
|
|
|
|
});
|
2019-04-29 17:22:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
log.error('Failed unlink ' + pid + ' ', e);
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class DbFileSystem {
|
2019-04-12 20:23:17 +02:00
|
|
|
static async create(root, FS_promise, readOnly = false) {
|
2019-04-29 17:22:46 +02:00
|
|
|
const start = performance.now();
|
2018-12-21 17:03:26 +01:00
|
|
|
try {
|
2019-04-29 17:22:46 +02:00
|
|
|
const dbfs = new DbFileSystem();
|
2018-12-21 17:03:26 +01:00
|
|
|
dbfs.root = root;
|
2019-04-29 17:22:46 +02:00
|
|
|
const FS = await FS_promise;
|
2018-12-21 17:03:26 +01:00
|
|
|
dbfs.FS = FS;
|
|
|
|
dbfs.syncfs_total_time = 0;
|
|
|
|
dbfs.readOnly = readOnly;
|
2019-07-26 17:43:56 +02:00
|
|
|
dbfs.syncActive = 0;
|
2018-12-21 17:03:26 +01:00
|
|
|
FS.mkdir(root);
|
|
|
|
FS.mount(FS.filesystems.IDBFS, {}, root);
|
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
FS.syncfs(true, err => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-04-29 17:22:46 +02:00
|
|
|
const rmrf = path => {
|
2019-04-24 09:04:52 +02:00
|
|
|
log.debug('rmrf ', path);
|
2019-04-29 17:22:46 +02:00
|
|
|
let info;
|
2019-04-13 22:43:29 +02:00
|
|
|
try {
|
|
|
|
info = FS.lookupPath(path);
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-24 09:04:52 +02:00
|
|
|
log.debug('rmrf ', path, info);
|
2019-04-13 13:17:03 +02:00
|
|
|
if (info.node.isFolder) {
|
2019-04-29 17:22:46 +02:00
|
|
|
for (const key in info.node.contents) {
|
2019-04-13 13:17:03 +02:00
|
|
|
rmrf(info.path + '/' + info.node.contents[key].name);
|
|
|
|
}
|
2019-04-24 09:04:52 +02:00
|
|
|
log.debug('rmdir ', path);
|
2019-04-13 13:17:03 +02:00
|
|
|
FS.rmdir(path);
|
|
|
|
} else {
|
2019-04-24 09:04:52 +02:00
|
|
|
log.debug('unlink ', path);
|
2019-04-13 13:17:03 +02:00
|
|
|
FS.unlink(path);
|
|
|
|
}
|
|
|
|
};
|
2019-04-29 17:22:46 +02:00
|
|
|
//const dirs = ['thumbnails', 'profile_photos', 'secret', 'stickers', 'temp', 'wallpapers', 'secret_thumbnails', 'passport'];
|
|
|
|
const dirs = [];
|
|
|
|
const root_dir = FS.lookupPath(root);
|
|
|
|
for (const key in root_dir.node.contents) {
|
|
|
|
const value = root_dir.node.contents[key];
|
2019-04-24 09:04:52 +02:00
|
|
|
log.debug('node ', key, value);
|
2019-04-13 22:43:29 +02:00
|
|
|
if (!value.isFolder) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-24 09:04:52 +02:00
|
|
|
dirs.push(root_dir.path + '/' + value.name);
|
2019-04-13 22:43:29 +02:00
|
|
|
}
|
2019-04-29 17:22:46 +02:00
|
|
|
for (const i in dirs) {
|
|
|
|
const dir = dirs[i];
|
2019-04-24 09:04:52 +02:00
|
|
|
rmrf(dir);
|
2019-04-13 13:55:52 +02:00
|
|
|
//FS.mkdir(dir);
|
|
|
|
//FS.mount(FS.filesystems.MEMFS, {}, dir);
|
2019-04-13 13:17:03 +02:00
|
|
|
}
|
2018-12-21 17:03:26 +01:00
|
|
|
dbfs.syncfsInterval = setInterval(() => {
|
|
|
|
dbfs.sync();
|
|
|
|
}, 5000);
|
2019-04-29 17:22:46 +02:00
|
|
|
const create_time = (performance.now() - start) / 1000;
|
2019-04-12 20:23:17 +02:00
|
|
|
log.debug('DbFileSystem::create ' + create_time);
|
2018-12-21 17:03:26 +01:00
|
|
|
return dbfs;
|
|
|
|
} catch (e) {
|
|
|
|
log.error('Failed to init DbFileSystem: ', e);
|
|
|
|
}
|
|
|
|
}
|
2019-07-26 17:43:56 +02:00
|
|
|
async sync(force) {
|
2018-12-21 17:03:26 +01:00
|
|
|
if (this.readOnly) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-26 17:43:56 +02:00
|
|
|
if (this.syncActive > 0 && !force) {
|
|
|
|
log.debug('SYNC: skip');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.syncActive++;
|
2019-04-29 17:22:46 +02:00
|
|
|
const start = performance.now();
|
2018-12-21 17:03:26 +01:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
this.FS.syncfs(false, () => {
|
2019-04-29 17:22:46 +02:00
|
|
|
const syncfs_time = (performance.now() - start) / 1000;
|
2018-12-21 17:03:26 +01:00
|
|
|
this.syncfs_total_time += syncfs_time;
|
|
|
|
log.debug('SYNC: ' + syncfs_time);
|
|
|
|
log.debug('SYNC total: ' + this.syncfs_total_time);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
2019-07-26 17:43:56 +02:00
|
|
|
this.syncActive--;
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
|
|
|
async close() {
|
|
|
|
clearInterval(this.syncfsInterval);
|
2019-07-26 17:43:56 +02:00
|
|
|
await this.sync(true);
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
2019-02-14 17:50:00 +01:00
|
|
|
async destroy() {
|
|
|
|
clearInterval(this.syncfsInterval);
|
|
|
|
if (this.readOnly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.FS.unmount(this.root);
|
2019-04-29 17:22:46 +02:00
|
|
|
const req = indexedDB.deleteDatabase(this.root);
|
2019-02-14 17:50:00 +01:00
|
|
|
await new Promise((resolve, reject) => {
|
2019-04-24 09:04:52 +02:00
|
|
|
req.onsuccess = function(e) {
|
2019-02-14 17:50:00 +01:00
|
|
|
log.info('SUCCESS');
|
|
|
|
resolve(e.result);
|
|
|
|
};
|
|
|
|
req.onerror = function(e) {
|
|
|
|
log.info('ONERROR');
|
|
|
|
reject(e.error);
|
2019-04-24 09:04:52 +02:00
|
|
|
};
|
2019-02-14 17:50:00 +01:00
|
|
|
req.onblocked = function(e) {
|
|
|
|
log.info('ONBLOCKED');
|
|
|
|
reject('blocked');
|
2019-04-24 09:04:52 +02:00
|
|
|
};
|
2019-02-14 17:50:00 +01:00
|
|
|
});
|
|
|
|
}
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class TdFileSystem {
|
2019-04-12 20:23:17 +02:00
|
|
|
static async init_fs(prefix, FS_promise) {
|
2019-04-29 17:22:46 +02:00
|
|
|
const FS = await FS_promise;
|
2019-04-12 20:23:17 +02:00
|
|
|
FS.mkdir(prefix);
|
|
|
|
return FS;
|
|
|
|
}
|
2019-04-24 13:00:02 +02:00
|
|
|
static async create(instanceName, FS_promise, readOnly = false) {
|
2018-12-21 17:03:26 +01:00
|
|
|
try {
|
2019-04-29 17:22:46 +02:00
|
|
|
const tdfs = new TdFileSystem();
|
|
|
|
const prefix = '/' + instanceName;
|
2018-12-21 17:03:26 +01:00
|
|
|
tdfs.prefix = prefix;
|
2019-04-12 20:23:17 +02:00
|
|
|
FS_promise = TdFileSystem.init_fs(prefix, FS_promise);
|
2018-12-21 17:03:26 +01:00
|
|
|
|
|
|
|
//MEMFS. Store to IDB and delete files as soon as possible
|
2019-04-29 17:22:46 +02:00
|
|
|
const inboundFileSystem = InboundFileSystem.create(
|
2019-04-24 13:00:02 +02:00
|
|
|
instanceName,
|
2018-12-21 17:03:26 +01:00
|
|
|
prefix + '/inboundfs',
|
2019-04-12 20:23:17 +02:00
|
|
|
FS_promise
|
2018-12-21 17:03:26 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
//IDBFS. MEMFS which is flushed to IDB from time to time
|
2019-04-29 17:22:46 +02:00
|
|
|
const dbFileSystem = DbFileSystem.create(
|
2019-04-24 09:04:52 +02:00
|
|
|
prefix + '/dbfs',
|
|
|
|
FS_promise,
|
|
|
|
readOnly
|
|
|
|
);
|
2019-04-12 20:23:17 +02:00
|
|
|
|
2019-04-29 17:22:46 +02:00
|
|
|
const FS = await FS_promise;
|
2019-04-12 20:23:17 +02:00
|
|
|
tdfs.FS = FS;
|
|
|
|
|
|
|
|
//WORKERFS. Temporary stores Blobs for outbound files
|
|
|
|
tdfs.outboundFileSystem = new OutboundFileSystem(
|
|
|
|
prefix + '/outboundfs',
|
|
|
|
tdfs.FS
|
|
|
|
);
|
2018-12-21 17:03:26 +01:00
|
|
|
|
|
|
|
tdfs.inboundFileSystem = await inboundFileSystem;
|
|
|
|
tdfs.dbFileSystem = await dbFileSystem;
|
|
|
|
return tdfs;
|
|
|
|
} catch (e) {
|
|
|
|
log.error('Failed to init TdFileSystem: ', e);
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 17:50:00 +01:00
|
|
|
async destroy() {
|
|
|
|
await this.dbFileSystem.destroy();
|
|
|
|
}
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class TdClient {
|
|
|
|
constructor(callback) {
|
|
|
|
log.info('Start worker');
|
|
|
|
this.pendingQueries = [];
|
|
|
|
this.isPending = true;
|
|
|
|
this.callback = callback;
|
|
|
|
this.wasInit = false;
|
|
|
|
}
|
|
|
|
|
2018-12-27 18:27:26 +01:00
|
|
|
async testLocalForage() {
|
|
|
|
await initLocalForage();
|
2019-04-29 17:22:46 +02:00
|
|
|
const DRIVERS = [
|
2018-12-27 18:27:26 +01:00
|
|
|
localforage.INDEXEDDB,
|
|
|
|
'memoryDriver',
|
|
|
|
localforage.LOCALSTORAGE,
|
|
|
|
localforage.WEBSQL,
|
|
|
|
localForageDrivers
|
|
|
|
];
|
|
|
|
for (const driverName of DRIVERS) {
|
2019-04-24 09:04:52 +02:00
|
|
|
console.log('Test ', driverName);
|
2018-12-27 18:27:26 +01:00
|
|
|
try {
|
|
|
|
await localforage.setDriver(driverName);
|
2019-04-24 09:04:52 +02:00
|
|
|
console.log('A');
|
2018-12-27 18:27:26 +01:00
|
|
|
await localforage.setItem('hello', 'world');
|
2019-04-24 09:04:52 +02:00
|
|
|
console.log('B');
|
2019-04-29 17:22:46 +02:00
|
|
|
const x = await localforage.getItem('hello');
|
2019-04-24 09:04:52 +02:00
|
|
|
console.log('got ', x);
|
2018-12-27 18:27:26 +01:00
|
|
|
await localforage.clear();
|
2019-04-24 09:04:52 +02:00
|
|
|
console.log('C');
|
|
|
|
} catch (error) {
|
|
|
|
console.log('Error', error);
|
2018-12-27 18:27:26 +01:00
|
|
|
}
|
2019-04-24 09:04:52 +02:00
|
|
|
}
|
2018-12-27 18:27:26 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 17:03:26 +01:00
|
|
|
async init(options) {
|
|
|
|
if (this.wasInit) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-12 20:23:17 +02:00
|
|
|
//await this.testLocalForage();
|
2019-02-14 15:25:38 +01:00
|
|
|
log.setVerbosity(options.jsLogVerbosityLevel);
|
2018-12-21 17:03:26 +01:00
|
|
|
this.wasInit = true;
|
|
|
|
|
|
|
|
options = options || {};
|
2019-04-29 17:22:46 +02:00
|
|
|
const mode = options.mode || 'wasm';
|
2018-12-21 17:03:26 +01:00
|
|
|
|
2019-04-29 17:22:46 +02:00
|
|
|
const FS_promise = new Promise(resolve => {
|
|
|
|
this.onFS = resolve;
|
2019-04-12 20:23:17 +02:00
|
|
|
});
|
|
|
|
|
2019-04-29 17:22:46 +02:00
|
|
|
const tdfs_promise = TdFileSystem.create(
|
2019-04-24 13:00:02 +02:00
|
|
|
options.instanceName,
|
2019-04-12 20:23:17 +02:00
|
|
|
FS_promise,
|
|
|
|
options.readOnly
|
|
|
|
);
|
|
|
|
|
2019-04-24 13:00:02 +02:00
|
|
|
this.useDatabase = true;
|
|
|
|
if ('useDatabase' in options) {
|
|
|
|
this.useDatabase = options.useDatabase;
|
|
|
|
}
|
2019-04-22 14:07:31 +02:00
|
|
|
|
2019-04-12 20:23:17 +02:00
|
|
|
log.info('load TdModule');
|
2019-07-02 15:06:05 +02:00
|
|
|
this.TdModule = await loadTdlib(mode, this.onFS, options.wasmUrl);
|
2018-12-21 17:03:26 +01:00
|
|
|
log.info('got TdModule');
|
|
|
|
this.td_functions = {
|
|
|
|
td_create: this.TdModule.cwrap('td_create', 'number', []),
|
|
|
|
td_destroy: this.TdModule.cwrap('td_destroy', null, ['number']),
|
|
|
|
td_send: this.TdModule.cwrap('td_send', null, ['number', 'string']),
|
|
|
|
td_execute: this.TdModule.cwrap('td_execute', 'string', [
|
|
|
|
'number',
|
|
|
|
'string'
|
|
|
|
]),
|
|
|
|
td_receive: this.TdModule.cwrap('td_receive', 'string', ['number']),
|
|
|
|
td_set_verbosity: verbosity => {
|
|
|
|
this.td_functions.td_execute(
|
|
|
|
0,
|
|
|
|
JSON.stringify({
|
|
|
|
'@type': 'setLogVerbosityLevel',
|
|
|
|
new_verbosity_level: verbosity
|
|
|
|
})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
td_get_timeout: this.TdModule.cwrap('td_get_timeout', 'number', [])
|
|
|
|
};
|
2019-04-13 13:17:03 +02:00
|
|
|
//this.onFS(this.TdModule.FS);
|
2018-12-21 17:03:26 +01:00
|
|
|
this.FS = this.TdModule.FS;
|
|
|
|
this.TdModule['websocket']['on']('error', error => {
|
|
|
|
this.scheduleReceiveSoon();
|
|
|
|
});
|
|
|
|
this.TdModule['websocket']['on']('open', fd => {
|
|
|
|
this.scheduleReceiveSoon();
|
|
|
|
});
|
|
|
|
this.TdModule['websocket']['on']('listen', fd => {
|
|
|
|
this.scheduleReceiveSoon();
|
|
|
|
});
|
|
|
|
this.TdModule['websocket']['on']('connection', fd => {
|
|
|
|
this.scheduleReceiveSoon();
|
|
|
|
});
|
|
|
|
this.TdModule['websocket']['on']('message', fd => {
|
|
|
|
this.scheduleReceiveSoon();
|
|
|
|
});
|
|
|
|
this.TdModule['websocket']['on']('close', fd => {
|
|
|
|
this.scheduleReceiveSoon();
|
|
|
|
});
|
|
|
|
|
|
|
|
// wait till it is allowed to start
|
|
|
|
this.callback({ '@type': 'inited' });
|
|
|
|
await new Promise(resolve => {
|
2019-04-29 17:22:46 +02:00
|
|
|
this.onStart = resolve;
|
2018-12-21 17:03:26 +01:00
|
|
|
});
|
|
|
|
this.isStarted = true;
|
|
|
|
|
|
|
|
log.info('may start now');
|
|
|
|
if (this.isClosing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log.info('FS start init');
|
2019-04-12 20:23:17 +02:00
|
|
|
this.tdfs = await tdfs_promise;
|
2018-12-21 17:03:26 +01:00
|
|
|
log.info('FS inited');
|
2019-04-25 20:50:26 +02:00
|
|
|
this.callback({ '@type': 'fsInited' });
|
2018-12-21 17:03:26 +01:00
|
|
|
|
|
|
|
// no async initialization after this point
|
2019-02-14 15:25:38 +01:00
|
|
|
if (options.logVerbosityLevel === undefined) {
|
|
|
|
options.logVerbosityLevel = 2;
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
2019-02-14 15:25:38 +01:00
|
|
|
this.td_functions.td_set_verbosity(options.logVerbosityLevel);
|
2018-12-21 17:03:26 +01:00
|
|
|
this.client = this.td_functions.td_create();
|
|
|
|
|
|
|
|
this.savingFiles = new Map();
|
2019-07-24 14:07:55 +02:00
|
|
|
this.send({
|
|
|
|
'@type': 'setOption',
|
|
|
|
name: 'store_all_files_in_files_directory',
|
|
|
|
value: {
|
|
|
|
'@type': 'optionValueBoolean',
|
|
|
|
value: true
|
|
|
|
}
|
|
|
|
});
|
2019-01-17 10:33:53 +01:00
|
|
|
this.send({
|
|
|
|
'@type': 'setOption',
|
2019-02-04 16:06:08 +01:00
|
|
|
name: 'language_pack_database_path',
|
2019-01-17 10:33:53 +01:00
|
|
|
value: {
|
|
|
|
'@type': 'optionValueString',
|
2019-02-12 17:48:52 +01:00
|
|
|
value: this.tdfs.dbFileSystem.root + '/language'
|
2019-01-17 10:33:53 +01:00
|
|
|
}
|
|
|
|
});
|
2019-04-22 14:07:31 +02:00
|
|
|
this.send({
|
|
|
|
'@type': 'setOption',
|
|
|
|
name: 'ignore_background_updates',
|
|
|
|
value: {
|
|
|
|
'@type': 'optionValueBoolean',
|
2019-04-24 13:00:02 +02:00
|
|
|
value: !this.useDatabase
|
2019-04-22 14:07:31 +02:00
|
|
|
}
|
|
|
|
});
|
2019-01-17 10:33:53 +01:00
|
|
|
|
2018-12-21 17:03:26 +01:00
|
|
|
this.flushPendingQueries();
|
|
|
|
|
|
|
|
this.receive();
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareQueryRecursive(query) {
|
|
|
|
if (query['@type'] === 'inputFileBlob') {
|
|
|
|
return {
|
|
|
|
'@type': 'inputFileLocal',
|
2019-04-24 13:00:02 +02:00
|
|
|
path: this.tdfs.outboundFileSystem.blobToPath(query.data, query.name)
|
2018-12-21 17:03:26 +01:00
|
|
|
};
|
|
|
|
}
|
2019-04-29 17:22:46 +02:00
|
|
|
for (const key in query) {
|
|
|
|
const field = query[key];
|
2018-12-21 17:03:26 +01:00
|
|
|
if (field && typeof field === 'object') {
|
|
|
|
query[key] = this.prepareQueryRecursive(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareQuery(query) {
|
|
|
|
if (query['@type'] === 'setTdlibParameters') {
|
|
|
|
query.parameters.database_directory = this.tdfs.dbFileSystem.root;
|
|
|
|
query.parameters.files_directory = this.tdfs.inboundFileSystem.root;
|
2019-04-22 14:07:31 +02:00
|
|
|
|
2019-04-29 17:22:46 +02:00
|
|
|
const useDb = this.useDatabase;
|
2019-04-22 14:07:31 +02:00
|
|
|
query.parameters.use_file_database = useDb;
|
|
|
|
query.parameters.use_chat_info_database = useDb;
|
|
|
|
query.parameters.use_message_database = useDb;
|
|
|
|
query.parameters.use_secret_chats = useDb;
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
2019-01-17 10:33:53 +01:00
|
|
|
if (query['@type'] === 'getLanguagePackString') {
|
2019-04-24 09:04:52 +02:00
|
|
|
query.language_pack_database_path =
|
|
|
|
this.tdfs.dbFileSystem.root + '/language';
|
2019-01-17 10:33:53 +01:00
|
|
|
}
|
2018-12-21 17:03:26 +01:00
|
|
|
return this.prepareQueryRecursive(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
onStart() {
|
|
|
|
//nop
|
|
|
|
log.info('ignore on_start');
|
|
|
|
}
|
|
|
|
|
2019-04-29 17:22:46 +02:00
|
|
|
deleteIdbKey(query) {
|
|
|
|
try {
|
|
|
|
} catch (e) {
|
|
|
|
this.callback({
|
|
|
|
'@type': 'error',
|
|
|
|
'@extra': query['@extra'],
|
|
|
|
code: 400,
|
|
|
|
message: e
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.callback({
|
|
|
|
'@type': 'ok',
|
|
|
|
'@extra': query['@extra']
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-15 17:19:46 +01:00
|
|
|
readFilePart(query) {
|
2019-04-29 17:22:46 +02:00
|
|
|
let res;
|
2019-02-15 17:19:46 +01:00
|
|
|
try {
|
2019-04-29 17:22:46 +02:00
|
|
|
//const file_size = this.FS.stat(query.path).size;
|
|
|
|
const stream = this.FS.open(query.path, 'r');
|
|
|
|
const buf = new Uint8Array(query.size);
|
2019-02-15 17:19:46 +01:00
|
|
|
this.FS.read(stream, buf, 0, query.size, query.offset);
|
|
|
|
this.FS.close(stream);
|
2019-04-24 09:04:52 +02:00
|
|
|
res = buf;
|
2019-02-15 17:19:46 +01:00
|
|
|
} catch (e) {
|
2019-04-24 09:04:52 +02:00
|
|
|
this.callback({
|
|
|
|
'@type': 'error',
|
|
|
|
'@extra': query['@extra'],
|
|
|
|
code: 400,
|
2019-05-10 15:47:39 +02:00
|
|
|
message: e.toString()
|
2019-04-24 09:04:52 +02:00
|
|
|
});
|
2019-02-15 17:19:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-04-24 09:04:52 +02:00
|
|
|
this.callback(
|
|
|
|
{
|
2019-05-10 15:47:39 +02:00
|
|
|
'@type': 'filePart',
|
2019-04-24 09:04:52 +02:00
|
|
|
'@extra': query['@extra'],
|
|
|
|
data: res
|
|
|
|
},
|
|
|
|
[res.buffer]
|
|
|
|
);
|
2019-02-15 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 17:03:26 +01:00
|
|
|
send(query) {
|
2019-02-14 17:50:00 +01:00
|
|
|
if (this.isClosing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.wasFatalError) {
|
|
|
|
if (query['@type'] === 'destroy') {
|
2019-04-29 17:22:46 +02:00
|
|
|
this.destroy({ '@type': 'ok', '@extra': query['@extra'] });
|
2019-02-14 17:50:00 +01:00
|
|
|
}
|
2018-12-21 17:03:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (query['@type'] === 'init') {
|
|
|
|
this.init(query.options);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (query['@type'] === 'start') {
|
|
|
|
log.info('on_start');
|
|
|
|
this.onStart();
|
|
|
|
return;
|
|
|
|
}
|
2019-02-14 15:25:38 +01:00
|
|
|
if (query['@type'] === 'setJsLogVerbosityLevel') {
|
|
|
|
log.setVerbosity(query.new_verbosity_level);
|
2018-12-21 17:03:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-02-15 17:19:46 +01:00
|
|
|
if (this.isPending) {
|
|
|
|
this.pendingQueries.push(query);
|
|
|
|
return;
|
|
|
|
}
|
2019-04-24 09:04:52 +02:00
|
|
|
if (
|
|
|
|
query['@type'] === 'setLogVerbosityLevel' ||
|
|
|
|
query['@type'] === 'getLogVerbosityLevel' ||
|
|
|
|
query['@type'] === 'setLogTagVerbosityLevel' ||
|
|
|
|
query['@type'] === 'getLogTagVerbosityLevel' ||
|
|
|
|
query['@type'] === 'getLogTags'
|
|
|
|
) {
|
2019-02-14 15:25:38 +01:00
|
|
|
this.execute(query);
|
2018-12-21 17:03:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-02-15 17:19:46 +01:00
|
|
|
if (query['@type'] === 'readFilePart') {
|
|
|
|
this.readFilePart(query);
|
2018-12-21 17:03:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-04-29 17:22:46 +02:00
|
|
|
if (query['@type'] === 'deleteIdbKey') {
|
|
|
|
this.deleteIdbKey(query);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-21 17:03:26 +01:00
|
|
|
query = this.prepareQuery(query);
|
|
|
|
this.td_functions.td_send(this.client, JSON.stringify(query));
|
|
|
|
this.scheduleReceiveSoon();
|
|
|
|
}
|
|
|
|
|
2019-02-14 15:25:38 +01:00
|
|
|
execute(query) {
|
|
|
|
try {
|
2019-04-29 17:22:46 +02:00
|
|
|
const res = this.td_functions.td_execute(0, JSON.stringify(query));
|
|
|
|
const response = JSON.parse(res);
|
2019-02-14 15:25:38 +01:00
|
|
|
this.callback(response);
|
|
|
|
} catch (error) {
|
|
|
|
this.onFatalError(error);
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 17:03:26 +01:00
|
|
|
receive() {
|
|
|
|
this.cancelReceive();
|
|
|
|
if (this.wasFatalError) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
while (true) {
|
2019-04-29 17:22:46 +02:00
|
|
|
const msg = this.td_functions.td_receive(this.client);
|
2018-12-21 17:03:26 +01:00
|
|
|
if (!msg) {
|
|
|
|
break;
|
|
|
|
}
|
2019-04-29 17:22:46 +02:00
|
|
|
const response = this.prepareResponse(JSON.parse(msg));
|
2018-12-21 17:03:26 +01:00
|
|
|
if (
|
|
|
|
response['@type'] === 'updateAuthorizationState' &&
|
|
|
|
response.authorization_state['@type'] === 'authorizationStateClosed'
|
|
|
|
) {
|
|
|
|
this.close(response);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.callback(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.scheduleReceive();
|
|
|
|
} catch (error) {
|
|
|
|
this.onFatalError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelReceive() {
|
|
|
|
if (this.receiveTimeout) {
|
|
|
|
clearTimeout(this.receiveTimeout);
|
|
|
|
delete this.receiveTimeout;
|
|
|
|
}
|
|
|
|
delete this.receiveSoon;
|
|
|
|
}
|
|
|
|
scheduleReceiveSoon() {
|
|
|
|
if (this.receiveSoon) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.cancelReceive();
|
|
|
|
this.receiveSoon = true;
|
|
|
|
this.scheduleReceiveIn(0.001);
|
|
|
|
}
|
|
|
|
scheduleReceive() {
|
|
|
|
if (this.receiveSoon) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.cancelReceive();
|
2019-04-29 17:22:46 +02:00
|
|
|
const timeout = this.td_functions.td_get_timeout();
|
2018-12-21 17:03:26 +01:00
|
|
|
this.scheduleReceiveIn(timeout);
|
|
|
|
}
|
|
|
|
scheduleReceiveIn(timeout) {
|
|
|
|
//return;
|
|
|
|
log.debug('Scheduler receive in ' + timeout + 's');
|
|
|
|
this.receiveTimeout = setTimeout(() => this.receive(), timeout * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
onFatalError(error) {
|
|
|
|
this.wasFatalError = true;
|
|
|
|
this.asyncOnFatalError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
async close(last_update) {
|
|
|
|
// close db and cancell all timers
|
|
|
|
this.isClosing = true;
|
|
|
|
if (this.isStarted) {
|
|
|
|
log.debug('close worker: start');
|
|
|
|
await this.tdfs.dbFileSystem.close();
|
|
|
|
this.cancelReceive();
|
|
|
|
log.debug('close worker: finish');
|
|
|
|
}
|
|
|
|
this.callback(last_update);
|
|
|
|
}
|
|
|
|
|
2019-02-14 17:50:00 +01:00
|
|
|
async destroy(result) {
|
|
|
|
try {
|
|
|
|
log.info('destroy tdfs ...');
|
|
|
|
await this.tdfs.destroy();
|
|
|
|
log.info('destroy tdfs ok');
|
|
|
|
} catch (e) {
|
|
|
|
log.error('Failed destroy', e);
|
|
|
|
}
|
|
|
|
this.callback(result);
|
2019-02-17 14:52:34 +01:00
|
|
|
this.callback({
|
2019-04-24 09:04:52 +02:00
|
|
|
'@type': 'updateAuthorizationState',
|
|
|
|
authorization_state: {
|
|
|
|
'@type': 'authorizationStateClosed'
|
2019-02-14 17:50:00 +01:00
|
|
|
}
|
2019-04-24 09:04:52 +02:00
|
|
|
});
|
2019-02-14 17:50:00 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 17:03:26 +01:00
|
|
|
async asyncOnFatalError(error) {
|
|
|
|
await this.tdfs.dbFileSystem.sync();
|
|
|
|
this.callback({ '@type': 'updateFatalError', error: error });
|
|
|
|
}
|
|
|
|
|
2019-03-14 01:47:50 +01:00
|
|
|
saveFile(pid, file) {
|
2019-04-29 17:22:46 +02:00
|
|
|
const isSaving = this.savingFiles.has(pid);
|
2018-12-21 17:03:26 +01:00
|
|
|
this.savingFiles.set(pid, file);
|
|
|
|
if (isSaving) {
|
2019-03-14 01:47:50 +01:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
try {
|
2019-04-29 17:22:46 +02:00
|
|
|
const arr = this.FS.readFile(file.local.path);
|
2019-03-14 01:47:50 +01:00
|
|
|
if (arr) {
|
2019-03-21 10:58:52 +01:00
|
|
|
file = Object.assign({}, file);
|
2019-03-14 01:47:50 +01:00
|
|
|
file.arr = arr;
|
|
|
|
this.doSaveFile(pid, file, arr);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log.error('Failed to readFile: ', e);
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
2019-03-14 01:47:50 +01:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
async doSaveFile(pid, file, arr) {
|
|
|
|
await this.tdfs.inboundFileSystem.persist(pid, file.local.path, arr);
|
2018-12-21 17:03:26 +01:00
|
|
|
file = this.savingFiles.get(pid);
|
|
|
|
file.idb_key = pid;
|
2019-03-14 01:47:50 +01:00
|
|
|
this.callback({ '@type': 'updateFile', file: file });
|
|
|
|
|
2018-12-21 17:03:26 +01:00
|
|
|
this.savingFiles.delete(pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareFile(file) {
|
2019-04-29 17:22:46 +02:00
|
|
|
const pid = file.remote.id;
|
2018-12-21 17:03:26 +01:00
|
|
|
if (!pid) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.local.is_downloading_active) {
|
|
|
|
this.tdfs.inboundFileSystem.forget(pid);
|
|
|
|
} else if (this.tdfs.inboundFileSystem.has(pid)) {
|
|
|
|
file.idb_key = pid;
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.local.is_downloading_completed) {
|
2019-03-14 01:47:50 +01:00
|
|
|
file = this.saveFile(pid, file);
|
2018-12-21 17:03:26 +01:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareResponse(response) {
|
|
|
|
if (response['@type'] === 'file') {
|
|
|
|
return this.prepareFile(response);
|
|
|
|
}
|
2019-04-29 17:22:46 +02:00
|
|
|
for (const key in response) {
|
|
|
|
const field = response[key];
|
2018-12-21 17:03:26 +01:00
|
|
|
if (field && typeof field === 'object') {
|
|
|
|
response[key] = this.prepareResponse(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
flushPendingQueries() {
|
|
|
|
this.isPending = false;
|
2019-04-29 17:22:46 +02:00
|
|
|
for (const query of this.pendingQueries) {
|
2018-12-21 17:03:26 +01:00
|
|
|
this.send(query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 17:22:46 +02:00
|
|
|
const client = new TdClient((e, t = []) => postMessage(e, t));
|
2018-12-21 17:03:26 +01:00
|
|
|
|
|
|
|
onmessage = function(e) {
|
|
|
|
try {
|
|
|
|
client.send(e.data);
|
|
|
|
} catch (error) {
|
|
|
|
client.onFatalError(error);
|
|
|
|
}
|
|
|
|
};
|